Extend the Python Grammar

This example demonstrates how to use the %extend statement, to add new syntax to the example Python grammar.

from lark.lark import Lark
from lark.indenter import PythonIndenter

GRAMMAR = r"""
%import python (compound_stmt, single_input, file_input, eval_input, test, suite, _NEWLINE, _INDENT, _DEDENT, COMMENT)

%extend compound_stmt: match_stmt

match_stmt: "match" test ":" cases

case: "case" test ":" suite // test is not quite correct.

%ignore /[\t \f]+/          // WS
%ignore /\\[\t \f]*\r?\n/   // LINE_CONT
%ignore COMMENT

parser = Lark(GRAMMAR, parser='lalr', start=['single_input', 'file_input', 'eval_input'], postlex=PythonIndenter())

tree = parser.parse(r"""
def name(n):
    match n:
        case 1:
        case 2:
        case _:
            print("number is too big")

""", start='file_input')

# Remove the 'python3__' prefix that was added to the implicitly imported rules.
for t in tree.iter_subtrees():
    t.data = t.data.rsplit('__', 1)[-1]


Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery