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

.. only:: html

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

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

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

.. _sphx_glr_examples_indented_tree.py:


Parsing Indentation
===================

A demonstration of parsing indentation (“whitespace significant” language)
and the usage of the ``Indenter`` class.

Since indentation is context-sensitive, a postlex stage is introduced to
manufacture ``INDENT``/``DEDENT`` tokens.

It is crucial for the indenter that the ``NL_type`` matches the spaces (and
tabs) after the newline.

If your whitespace-significant grammar supports comments, then ``NL_type``
must match those comments too. Otherwise, comments that appear in the middle
of a line will `confuse Lark`_.

.. _`confuse Lark`: https://github.com/lark-parser/lark/issues/863

.. GENERATED FROM PYTHON SOURCE LINES 20-62

.. code-block:: Python

    from lark import Lark
    from lark.indenter import Indenter

    tree_grammar = r"""
        %import common.CNAME -> NAME
        %import common.WS_INLINE
        %import common.SH_COMMENT
        %ignore WS_INLINE
        %ignore SH_COMMENT
        %declare _INDENT _DEDENT

        ?start: _NL* tree
        tree: NAME _NL [_INDENT tree+ _DEDENT]
        _NL: (/\r?\n[\t ]*/ | SH_COMMENT)+
    """

    class TreeIndenter(Indenter):
        NL_type = '_NL'
        OPEN_PAREN_types = []
        CLOSE_PAREN_types = []
        INDENT_type = '_INDENT'
        DEDENT_type = '_DEDENT'
        tab_len = 8

    parser = Lark(tree_grammar, parser='lalr', postlex=TreeIndenter())

    test_tree = """
    a
        # check this comment out
        b
        c
            d
            e
        f
            g
    """

    def test():
        print(parser.parse(test_tree).pretty())

    if __name__ == '__main__':
        test()


.. _sphx_glr_download_examples_indented_tree.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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