From 8b4f874d3d99d1381dd196df5ae0a31da19364c3 Mon Sep 17 00:00:00 2001 From: MegaIng1 Date: Fri, 1 Jan 2021 15:44:08 +0100 Subject: [PATCH] added example grammar_building.py --- examples/advanced/grammar_building.py | 59 +++++++++++++++++++++++++++ lark-stubs/reconstruct.pyi | 4 +- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 examples/advanced/grammar_building.py diff --git a/examples/advanced/grammar_building.py b/examples/advanced/grammar_building.py new file mode 100644 index 0000000..0967045 --- /dev/null +++ b/examples/advanced/grammar_building.py @@ -0,0 +1,59 @@ +from pathlib import Path + +from lark.indenter import Indenter +from lark.lark import Lark +from lark.load_grammar import GrammarBuilder + +MATCH_GRAMMAR = ('match', """ + +%extend compound_stmt: match_stmt + +match_stmt: "match" test ":" cases + +cases: _NEWLINE _INDENT case+ _DEDENT + +case: "case" test ":" suite // test is not quite correct. + +""", ('compound_stmt', 'test', 'suite', '_DEDENT', '_INDENT', '_NEWLINE')) + +EXTENSIONS = (MATCH_GRAMMAR,) + +builder = GrammarBuilder() + +builder.load_grammar((Path(__file__).with_name('python3.lark')).read_text(), 'python3') + +for name, ext_grammar, needed_names in EXTENSIONS: + mangle = builder.get_mangle(name, dict(zip(needed_names, needed_names))) + builder.load_grammar(ext_grammar, name, mangle) + +grammar = builder.build() + + +class PythonIndenter(Indenter): + NL_type = '_NEWLINE' + OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE'] + CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE'] + INDENT_type = '_INDENT' + DEDENT_type = '_DEDENT' + tab_len = 8 + + +parser = Lark(grammar, parser='lalr', start=['single_input', 'file_input', 'eval_input'], postlex=PythonIndenter()) + +tree = parser.parse(r""" + +a = 5 + +def name(n): + match n: + case 1: + print("one") + case 2: + print("two") + case _: + print("number is to big") + +name(a) +""", start='file_input') + +print(tree.pretty()) diff --git a/lark-stubs/reconstruct.pyi b/lark-stubs/reconstruct.pyi index 2220c46..5a4aede 100644 --- a/lark-stubs/reconstruct.pyi +++ b/lark-stubs/reconstruct.pyi @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from typing import List, Dict, Union +from typing import List, Dict, Union, Callable from .lark import Lark from .tree import Tree from .visitors import Transformer_InPlace @@ -30,7 +30,7 @@ class MakeMatchTree: class Reconstructor: - def __init__(self, parser: Lark, term_subs: Dict[str, str] = ...): + def __init__(self, parser: Lark, term_subs: Dict[str, Callable[[str], str]] = ...): ... def reconstruct(self, tree: Tree) -> str: