From d846627fb7ae0088bd6a687e88b424794d95a67e Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Thu, 16 Nov 2017 08:54:16 +0200 Subject: [PATCH] A few fixes to escaping --- examples/python3.g | 6 +++--- lark/load_grammar.py | 6 ++++-- lark/tools/nearley.py | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/python3.g b/examples/python3.g index 61dc1ee..bb5a4e8 100644 --- a/examples/python3.g +++ b/examples/python3.g @@ -165,7 +165,7 @@ string: STRING | LONG_STRING // Tokens NAME: /[a-zA-Z_]\w*/ -COMMENT: /\#[^\n]*/ +COMMENT: /#[^\n]*/ _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+ @@ -177,8 +177,8 @@ _NEWLINE: ( /\r?\n[\t ]*/ | COMMENT )+ // STRING : /[ub]?r?("(?!"").*?(?', '_IGNORE': r'%ignore', '_IMPORT': r'%import', - 'NUMBER': '\d+', + 'NUMBER': r'\d+', } RULES = { @@ -294,7 +294,9 @@ def _literal_to_pattern(literal): flags = None assert v[0] == v[-1] and v[0] in '"/' - x = v[1:-1].replace("'", r"\'") + x = v[1:-1] + x = re.sub(r'(\\[wd/]|\\\[|\\\])', r'\\\1', x) + x = x.replace("'", r"\'") s = literal_eval("u'''%s'''" % x) return { 'STRING': PatternStr, 'REGEXP': PatternRE }[literal.type](s, flags) diff --git a/lark/tools/nearley.py b/lark/tools/nearley.py index c6f9a2d..107b8ae 100644 --- a/lark/tools/nearley.py +++ b/lark/tools/nearley.py @@ -30,7 +30,7 @@ nearley_grammar = r""" js: JS? NAME: /[a-zA-Z_$]\w*/ - COMMENT: /\#[^\n]*/ + COMMENT: /#[^\n]*/ REGEXP: /\[.*?\]/ STRING: /".*?"/