From 9230c67d28e766c836850668dba51029dde80f90 Mon Sep 17 00:00:00 2001 From: Erez Sh Date: Mon, 6 Sep 2021 14:15:46 +0100 Subject: [PATCH] Grammar: Added support for line breaks, using \\ --- lark/load_grammar.py | 3 ++- tests/test_grammar.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index 309826b..1ae832f 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -94,6 +94,7 @@ TERMINALS = { '_NL_OR': r'(\r?\n)+\s*\|', 'WS': r'[ \t]+', 'COMMENT': r'\s*//[^\n]*', + 'BACKSLASH': r'\\[ ]*\n', '_TO': '->', '_IGNORE': r'%ignore', '_OVERRIDE': r'%override', @@ -917,7 +918,7 @@ def _get_parser(): for r, _p, xs, o in rules for i, x in enumerate(xs)] callback = ParseTreeBuilder(rules, ST).create_callback() import re - lexer_conf = LexerConf(terminals, re, ['WS', 'COMMENT']) + lexer_conf = LexerConf(terminals, re, ['WS', 'COMMENT', 'BACKSLASH']) parser_conf = ParserConf(rules, callback, ['start']) lexer_conf.lexer_type = 'standard' parser_conf.parser_type = 'lalr' diff --git a/tests/test_grammar.py b/tests/test_grammar.py index c771f2b..c60d15b 100644 --- a/tests/test_grammar.py +++ b/tests/test_grammar.py @@ -270,6 +270,16 @@ class TestGrammar(TestCase): imports = list_grammar_imports('%import common.WS', []) 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__': main()