Browse Source

Bugfix in tools.nearley: Added support for null keyword (Issue #342)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.0
Erez Shinan 6 years ago
parent
commit
475312c608
2 changed files with 14 additions and 1 deletions
  1. +5
    -1
      lark/tools/nearley.py
  2. +9
    -0
      tests/test_nearley/test_nearley.py

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

@@ -20,12 +20,13 @@ nearley_grammar = r"""


?expr: item [":" /[+*?]/] ?expr: item [":" /[+*?]/]


?item: rule|string|regexp
?item: rule|string|regexp|null
| "(" expansions ")" | "(" expansions ")"


rule: NAME rule: NAME
string: STRING string: STRING
regexp: REGEXP regexp: REGEXP
null: "null"
JS: /{%.*?%}/s JS: /{%.*?%}/s
js: JS? js: JS?


@@ -83,6 +84,9 @@ class NearleyToLark(InlineTransformer):
def regexp(self, r): def regexp(self, r):
return '/%s/' % r return '/%s/' % r


def null(self):
return ''

def string(self, s): def string(self, s):
return self._extra_rule(s) return self._extra_rule(s)




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

@@ -83,6 +83,15 @@ class TestNearley(unittest.TestCase):
parse = d['parse'] parse = d['parse']
parse(u'"') parse(u'"')


def test_null(self):
grammar = r'main -> "a" | null'
code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
d = {}
exec (code, d)
parse = d['parse']
parse('a')
parse('')

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)


Loading…
Cancel
Save