Skip to content

Commit

Permalink
Fix unescaping strings in native parser containing ECHARs or UCHARs.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Oct 23, 2023
1 parent 2a591ad commit 031ed22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/ebnf/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ def terminal(s)
case m = s[0,1]
when '"', "'" # STRING1 or STRING2
l, s = s[1..-1].split(m.rstrip, 2)
[Unescape.unescape_string(l).tap {|str| str.quote_style = (m == "'" ? :squote : :dquote)}, s]
[Unescape.unescape(l).tap {|str| str.quote_style = (m == "'" ? :squote : :dquote)}, s]
when '[' # RANGE, O_RANGE
# Includes RANGE and O_RANGE which can't include a ']'
l, s = s[1..-1].split(']', 2)
[[:range, Unescape.unescape_string(l)], s]
[[:range, Unescape.unescape(l)], s]
when '#' # HEX
s.match(/(#x\h+)(.*)$/)
l, s = $1, $2
Expand Down
2 changes: 1 addition & 1 deletion lib/ebnf/unescape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def unescape_codepoints(string)
# @return [String]
# @see https://www.w3.org/TR/rdf-sparql-query/#grammarEscapes
def unescape_string(input)
input.gsub(ECHAR) { |escaped| ESCAPE_CHARS[escaped] || escaped[1..-1]}
input.gsub(ECHAR) {|escaped| ESCAPE_CHARS[escaped] || escaped}
end
module_function :unescape_string

Expand Down
6 changes: 6 additions & 0 deletions spec/native_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
UCHAR))
'>')))},
],
"UCHAR": [
%(UCHAR ::= ( '\\u' HEX HEX HEX HEX ) | ( '\\U' HEX HEX HEX HEX HEX HEX HEX HEX )),
%{(
(terminal UCHAR
(alt (seq '\\\\u' HEX HEX HEX HEX) (seq '\\\\U' HEX HEX HEX HEX HEX HEX HEX HEX))) )}
]
}.each do |title, (input, expect)|
it title do
expect(parse(input).to_sxp).to produce(expect, logger)
Expand Down

0 comments on commit 031ed22

Please sign in to comment.