Browse Source

Allow comments in rule definitions

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.8.0
Mike Roberts 4 years ago
parent
commit
ed3c131ca8
3 changed files with 12 additions and 2 deletions
  1. +1
    -1
      examples/lark.lark
  2. +1
    -1
      lark/load_grammar.py
  3. +10
    -0
      tests/test_parser.py

+ 1
- 1
examples/lark.lark View File

@@ -44,7 +44,7 @@ _NL: /(\r?\n)+\s*/
%import common.INT -> NUMBER %import common.INT -> NUMBER
%import common.WS_INLINE %import common.WS_INLINE


COMMENT: "//" /[^\n]/*
COMMENT: /\s*/ "//" /[^\n]/*


%ignore WS_INLINE %ignore WS_INLINE
%ignore COMMENT %ignore COMMENT

+ 1
- 1
lark/load_grammar.py View File

@@ -85,7 +85,7 @@ TERMINALS = {
'REGEXP': r'/(?!/)(\\/|\\\\|[^/\n])*?/[%s]*' % _RE_FLAGS, 'REGEXP': r'/(?!/)(\\/|\\\\|[^/\n])*?/[%s]*' % _RE_FLAGS,
'_NL': r'(\r?\n)+\s*', '_NL': r'(\r?\n)+\s*',
'WS': r'[ \t]+', 'WS': r'[ \t]+',
'COMMENT': r'//[^\n]*',
'COMMENT': r'\s*//[^\n]*',
'_TO': '->', '_TO': '->',
'_IGNORE': r'%ignore', '_IGNORE': r'%ignore',
'_DECLARE': r'%declare', '_DECLARE': r'%declare',


+ 10
- 0
tests/test_parser.py View File

@@ -94,6 +94,16 @@ class TestParsers(unittest.TestCase):
r = g.parse('xx') r = g.parse('xx')
self.assertEqual( r.children[0].data, "c" ) self.assertEqual( r.children[0].data, "c" )


def test_comment_in_rule_definition(self):
g = Lark("""start: a
a: "a"
// A comment
// Another
| "b"
""")
r = g.parse('b')
self.assertEqual( r.children[0].data, "a" )

def test_visit_tokens(self): def test_visit_tokens(self):
class T(Transformer): class T(Transformer):
def a(self, children): def a(self, children):


Loading…
Cancel
Save