Browse Source

Bugfix in tools.nearley: Backslash now handled correctly in strings (Issue #341)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.0
Erez Shinan 6 years ago
parent
commit
20329e0bf5
3 changed files with 12 additions and 4 deletions
  1. +1
    -1
      lark/tools/nearley.py
  2. +8
    -0
      tests/test_nearley/test_nearley.py
  3. +3
    -3
      tests/test_reconstructor.py

+ 1
- 1
lark/tools/nearley.py View File

@@ -32,8 +32,8 @@ nearley_grammar = r"""
NAME: /[a-zA-Z_$]\w*/ NAME: /[a-zA-Z_$]\w*/
COMMENT: /#[^\n]*/ COMMENT: /#[^\n]*/
REGEXP: /\[.*?\]/ REGEXP: /\[.*?\]/
STRING: /".*?"/


%import common.ESCAPED_STRING -> STRING
%import common.WS %import common.WS
%ignore WS %ignore WS
%ignore COMMENT %ignore COMMENT


+ 8
- 0
tests/test_nearley/test_nearley.py View File

@@ -75,6 +75,14 @@ class TestNearley(unittest.TestCase):


parse(u'±a') parse(u'±a')


def test_backslash(self):
grammar = r'main -> "\""'
code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
d = {}
exec (code, d)
parse = d['parse']
parse(u'"')

def test_utf8_2(self): def test_utf8_2(self):
fn = os.path.join(TEST_PATH, 'grammars/unicode.ne') fn = os.path.join(TEST_PATH, 'grammars/unicode.ne')
nearley_tool_main(fn, 'x', NEARLEY_PATH) nearley_tool_main(fn, 'x', NEARLEY_PATH)


+ 3
- 3
tests/test_reconstructor.py View File

@@ -28,7 +28,7 @@ class TestReconstructor(TestCase):
item: NL item: NL
| rule | rule
rule: WORD ":" NUMBER rule: WORD ":" NUMBER
NL: /(\\r?\\n)+\s*/
NL: /(\\r?\\n)+\\s*/
""" + common """ + common


code = """ code = """
@@ -42,7 +42,7 @@ class TestReconstructor(TestCase):
g = """ g = """
start: (rule | NL)* start: (rule | NL)*
rule: WORD ":" NUMBER rule: WORD ":" NUMBER
NL: /(\\r?\\n)+\s*/
NL: /(\\r?\\n)+\\s*/
""" + common """ + common


code = """ code = """
@@ -59,7 +59,7 @@ class TestReconstructor(TestCase):
| rule | rule
| "hello" -> hi | "hello" -> hi
rule: WORD ":" NUMBER rule: WORD ":" NUMBER
NL: /(\\r?\\n)+\s*/
NL: /(\\r?\\n)+\\s*/
""" + common """ + common


code = """ code = """


Loading…
Cancel
Save