Browse Source

Grammar: Added support for line breaks, using \\

gm/2021-09-23T00Z/github.com--lark-parser-lark/v0.12.0
Erez Sh 3 years ago
parent
commit
9230c67d28
2 changed files with 12 additions and 1 deletions
  1. +2
    -1
      lark/load_grammar.py
  2. +10
    -0
      tests/test_grammar.py

+ 2
- 1
lark/load_grammar.py View File

@@ -94,6 +94,7 @@ TERMINALS = {
'_NL_OR': r'(\r?\n)+\s*\|', '_NL_OR': r'(\r?\n)+\s*\|',
'WS': r'[ \t]+', 'WS': r'[ \t]+',
'COMMENT': r'\s*//[^\n]*', 'COMMENT': r'\s*//[^\n]*',
'BACKSLASH': r'\\[ ]*\n',
'_TO': '->', '_TO': '->',
'_IGNORE': r'%ignore', '_IGNORE': r'%ignore',
'_OVERRIDE': r'%override', '_OVERRIDE': r'%override',
@@ -917,7 +918,7 @@ def _get_parser():
for r, _p, xs, o in rules for i, x in enumerate(xs)] for r, _p, xs, o in rules for i, x in enumerate(xs)]
callback = ParseTreeBuilder(rules, ST).create_callback() callback = ParseTreeBuilder(rules, ST).create_callback()
import re import re
lexer_conf = LexerConf(terminals, re, ['WS', 'COMMENT'])
lexer_conf = LexerConf(terminals, re, ['WS', 'COMMENT', 'BACKSLASH'])
parser_conf = ParserConf(rules, callback, ['start']) parser_conf = ParserConf(rules, callback, ['start'])
lexer_conf.lexer_type = 'standard' lexer_conf.lexer_type = 'standard'
parser_conf.parser_type = 'lalr' parser_conf.parser_type = 'lalr'


+ 10
- 0
tests/test_grammar.py View File

@@ -270,6 +270,16 @@ class TestGrammar(TestCase):
imports = list_grammar_imports('%import common.WS', []) imports = list_grammar_imports('%import common.WS', [])
assert len(imports) == 1 and imports[0].pkg_name == 'lark' assert len(imports) == 1 and imports[0].pkg_name == 'lark'


def test_line_breaks(self):
p = Lark(r"""start: "a" \
"b"
""")
p.parse('ab')







if __name__ == '__main__': if __name__ == '__main__':
main() main()


Loading…
Cancel
Save