From d559e495ee22f6bb97aa1abda3be4fa653edb9f3 Mon Sep 17 00:00:00 2001 From: Erez Sh Date: Thu, 2 Jul 2020 13:47:54 +0300 Subject: [PATCH] Added templates example --- examples/templates.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/templates.py diff --git a/examples/templates.py b/examples/templates.py new file mode 100644 index 0000000..2acc6eb --- /dev/null +++ b/examples/templates.py @@ -0,0 +1,26 @@ +# +# This example shows how to use Lark's templates to achieve cleaner grammars +# + +from lark import Lark + +grammar = r""" +start: list | dict + +list: "[" _seperated{atom, ","} "]" +dict: "{" _seperated{key_value, ","} "}" +key_value: atom ":" atom + +_seperated{x, sep}: x (sep x)* // Define a sequence of 'x sep x sep x ...' + +atom: NUMBER | ESCAPED_STRING + +%import common (NUMBER, ESCAPED_STRING, WS) +%ignore WS +""" + + +parser = Lark(grammar) + +print(parser.parse('[1, "a", 2]')) +print(parser.parse('{"a": 2, "b": 6}')) \ No newline at end of file