From 744235407ea6d74e681480132ba98010b7b20f57 Mon Sep 17 00:00:00 2001 From: Attila Szakacs Date: Fri, 3 Jun 2022 14:13:26 +0200 Subject: [PATCH] yacc: unquote single characters Signed-off-by: Attila Szakacs --- neologism/yacc.py | 10 ++++++++-- tests/test_yacc.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/neologism/yacc.py b/neologism/yacc.py index 2e22572..e2817e2 100644 --- a/neologism/yacc.py +++ b/neologism/yacc.py @@ -33,13 +33,19 @@ def __yacc2xml(yacc_file_path: str, custom_path: str = None): return xml_file +def __unescape(keyword: str) -> str: + if len(keyword) == 3 and keyword[0] == "'" and keyword[-1] == "'": + return keyword.strip("'") + return keyword + + def __xml2rules(xml_file): rules = set() root = xml_parser.parse(xml_file.name).getroot() for rule in root.iter("rule"): - lhs = rule.find("lhs").text - rhs = [symbol.text for symbol in rule.find("rhs") if symbol.tag != "empty"] + lhs = __unescape(rule.find("lhs").text) + rhs = [__unescape(symbol.text) for symbol in rule.find("rhs") if symbol.tag != "empty"] rules.add(Rule(lhs, rhs)) return rules diff --git a/tests/test_yacc.py b/tests/test_yacc.py index e7ce305..eb158af 100644 --- a/tests/test_yacc.py +++ b/tests/test_yacc.py @@ -63,7 +63,7 @@ def test_parse_yacc(yacc_file): Rule("start", ("test",)), Rule("test", ("test1", "test1next", "test1next")), Rule("test", ("test2", "test2next", "test")), - Rule("test", ("KW_TEST", "'('", "test_opts", "')'")), + Rule("test", ("KW_TEST", "(", "test_opts", ")")), Rule("test", ()), Rule("test_opts", ("number",)), Rule("test_opts", ("string",)),