Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in parsing hex char escapes #36

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix bug in parsing hex char escapes
It was returning an int not a string. This commit also adds
some test cases I used to find the problem.
Eric Moyer committed Apr 15, 2024
commit e2e18ad20843d58b7c8c19963f3984de2c02c753
32 changes: 32 additions & 0 deletions tests/test_parsing/test_parsing.py
Original file line number Diff line number Diff line change
@@ -138,6 +138,29 @@ def test__parse_literal_string(self):
self.assertEqual(3, len(outbuf), f"len(outbuf): {len(outbuf)} != 3")
self.assertListEqual([2, ord("你"), ord("你")], outbuf)

escaped_char_src = '"\\n"'
outbuf = []

remaining_src = _parse_rhs_literal_string(escaped_char_src, outbuf)
self.assertEqual(3, len(outbuf), f"len(outbuf): {len(outbuf)} != 3")
self.assertListEqual([2, ord("\n"), ord("\n")], outbuf)

escaped_backslash_src = '"\\\\"'
outbuf = []

remaining_src = _parse_rhs_literal_string(escaped_backslash_src, outbuf)
self.assertEqual(3, len(outbuf), f"len(outbuf): {len(outbuf)} != 3")
self.assertListEqual([2, ord("\\"), ord("\\")], outbuf)
self.assertEqual("", remaining_src, f"remaining_src: {remaining_src} != ''")

escaped_backslash_src = '"\\x5C"'
outbuf = []

remaining_src = _parse_rhs_literal_string(escaped_backslash_src, outbuf)
self.assertEqual(3, len(outbuf), f"len(outbuf): {len(outbuf)} != 3")
self.assertListEqual([2, ord("\\"), ord("\\")], outbuf)
self.assertEqual("", remaining_src, f"remaining_src: {remaining_src} != ''")

def test_null(self):
src = "root ::= "
rhs_src = ""
@@ -296,6 +319,15 @@ def test_parse_rhs(self):
)
logging.debug(f"state.grammar_encoding of {rhs_src}: {state.grammar_encoding}")

src = 'root ::= "\\\\"'
rhs_src = '"\\\\"'
state = ParseState()
state.symbol_table["root"] = 9
_ = parse_rhs(
state=state, rhs=rhs_src, rule_name="root", rule_id=9, is_nested=False
)
logging.debug(f"state.grammar_encoding of {rhs_src}: {state.grammar_encoding}")

def test__parse_symbol_reference(self):
state = ParseState()
outbuf = []
2 changes: 1 addition & 1 deletion transformers_cfg/parser.py
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ def parse_char(src) -> (str, str):
if first > -1:
second = hex_to_int(src[3])
if second > -1:
return (first << 4) + second, src[4:]
return chr((first << 4) + second), src[4:]
raise RuntimeError("expecting \\xNN at " + src)
elif esc in ('"', "[", "]"):
return esc, src[2:]