
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/advanced/error_reporting_lalr.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_error_reporting_lalr.py>`
        to download the full example code.

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

.. _sphx_glr_examples_advanced_error_reporting_lalr.py:


Example-Driven Error Reporting
==============================

A demonstration of example-driven error reporting with the LALR parser
(See also: error_reporting_earley.py)

.. GENERATED FROM PYTHON SOURCE LINES 8-78

.. code-block:: Python

    from lark import Lark, UnexpectedInput

    from _json_parser import json_grammar   # Using the grammar from the json_parser example

    json_parser = Lark(json_grammar, parser='lalr')

    class JsonSyntaxError(SyntaxError):
        def __str__(self):
            context, line, column = self.args
            return '%s at line %s, column %s.\n\n%s' % (self.label, line, column, context)

    class JsonMissingValue(JsonSyntaxError):
        label = 'Missing Value'

    class JsonMissingOpening(JsonSyntaxError):
        label = 'Missing Opening'

    class JsonMissingClosing(JsonSyntaxError):
        label = 'Missing Closing'

    class JsonMissingComma(JsonSyntaxError):
        label = 'Missing Comma'

    class JsonTrailingComma(JsonSyntaxError):
        label = 'Trailing Comma'


    def parse(json_text):
        try:
            j = json_parser.parse(json_text)
        except UnexpectedInput as u:
            exc_class = u.match_examples(json_parser.parse, {
                JsonMissingOpening: ['{"foo": ]}',
                                     '{"foor": }}',
                                     '{"foo": }'],
                JsonMissingClosing: ['{"foo": [}',
                                     '{',
                                     '{"a": 1',
                                     '[1'],
                JsonMissingComma: ['[1 2]',
                                   '[false 1]',
                                   '["b" 1]',
                                   '{"a":true 1:4}',
                                   '{"a":1 1:4}',
                                   '{"a":"b" 1:4}'],
                JsonTrailingComma: ['[,]',
                                    '[1,]',
                                    '[1,2,]',
                                    '{"foo":1,}',
                                    '{"foo":false,"bar":true,}']
            }, use_accepts=True)
            if not exc_class:
                raise
            raise exc_class(u.get_context(json_text), u.line, u.column)


    def test():
        try:
            parse('{"example1": "value"')
        except JsonMissingClosing as e:
            print(e)

        try:
            parse('{"example2": ] ')
        except JsonMissingOpening as e:
            print(e)


    if __name__ == '__main__':
        test()


.. _sphx_glr_download_examples_advanced_error_reporting_lalr.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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