
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/advanced/create_ast.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_advanced_create_ast.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_advanced_create_ast.py:


Creating an AST from the parse tree
===================================

    This example demonstrates how to transform a parse-tree into an AST using `lark.ast_utils`.

    create_transformer() collects every subclass of `Ast` subclass from the module,
    and creates a Lark transformer that builds the AST with no extra code.

    This example only works with Python 3.

.. GENERATED FROM PYTHON SOURCE LINES 12-122

.. code-block:: Python


    import sys
    from typing import List
    from dataclasses import dataclass

    from lark import Lark, ast_utils, Transformer, v_args
    from lark.tree import Meta

    this_module = sys.modules[__name__]


    #
    #   Define AST
    #
    class _Ast(ast_utils.Ast):
        # This will be skipped by create_transformer(), because it starts with an underscore
        pass

    class _Statement(_Ast):
        # This will be skipped by create_transformer(), because it starts with an underscore
        pass

    @dataclass
    class Value(_Ast, ast_utils.WithMeta):
        "Uses WithMeta to include line-number metadata in the meta attribute"
        meta: Meta
        value: object

    @dataclass
    class Name(_Ast):
        name: str

    @dataclass
    class CodeBlock(_Ast, ast_utils.AsList):
        # Corresponds to code_block in the grammar
        statements: List[_Statement]

    @dataclass
    class If(_Statement):
        cond: Value
        then: CodeBlock

    @dataclass
    class SetVar(_Statement):
        # Corresponds to set_var in the grammar
        name: str
        value: Value

    @dataclass
    class Print(_Statement):
        value: Value


    class ToAst(Transformer):
        # Define extra transformation functions, for rules that don't correspond to an AST class.

        def STRING(self, s):
            # Remove quotation marks
            return s[1:-1]

        def DEC_NUMBER(self, n):
            return int(n)

        @v_args(inline=True)
        def start(self, x):
            return x

    #
    #   Define Parser
    #

    parser = Lark("""
        start: code_block

        code_block: statement+

        ?statement: if | set_var | print

        if: "if" value "{" code_block "}"
        set_var: NAME "=" value ";"
        print: "print" value ";"

        value: name | STRING | DEC_NUMBER
        name: NAME

        %import python (NAME, STRING, DEC_NUMBER)
        %import common.WS
        %ignore WS
        """,
        parser="lalr",
    )

    transformer = ast_utils.create_transformer(this_module, ToAst())

    def parse(text):
        tree = parser.parse(text)
        return transformer.transform(tree)

    #
    #   Test
    #

    if __name__ == '__main__':
        print(parse("""
            a = 1;
            if a {
                print "a is 1";
                a = 2;
            }
        """))


.. _sphx_glr_download_examples_advanced_create_ast.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: create_ast.ipynb <create_ast.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: create_ast.py <create_ast.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: create_ast.zip <create_ast.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
