Browse Source

Merge pull request #945 from lark-parser/docs_jul22

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.12.0
Erez Shinan 3 years ago
committed by GitHub
parent
commit
61973a70f5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 9 deletions
  1. +2
    -0
      docs/classes.rst
  2. +5
    -0
      docs/visitors.rst
  3. +2
    -2
      lark/ast_utils.py
  4. +25
    -6
      lark/exceptions.py
  5. +7
    -1
      lark/lark.py

+ 2
- 0
docs/classes.rst View File

@@ -66,6 +66,8 @@ UnexpectedInput


.. autoclass:: lark.exceptions.UnexpectedCharacters .. autoclass:: lark.exceptions.UnexpectedCharacters


.. autoclass:: lark.exceptions.UnexpectedEOF

InteractiveParser InteractiveParser
----------------- -----------------




+ 5
- 0
docs/visitors.rst View File

@@ -107,3 +107,8 @@ Discard
------- -------


.. autoclass:: lark.visitors.Discard .. autoclass:: lark.visitors.Discard

VisitError
-------

.. autoclass:: lark.exceptions.VisitError

+ 2
- 2
lark/ast_utils.py View File

@@ -36,8 +36,8 @@ def create_transformer(ast_module, transformer=None):
Classes starting with an underscore (`_`) will be skipped. Classes starting with an underscore (`_`) will be skipped.


Parameters: Parameters:
ast_module - A Python module containing all the subclasses of `ast_utils.Ast`
transformer (Optional[Transformer]) - An initial transformer. Its attributes may be overwritten.
ast_module: A Python module containing all the subclasses of ``ast_utils.Ast``
transformer (Optional[Transformer]): An initial transformer. Its attributes may be overwritten.
""" """
t = transformer or Transformer() t = transformer or Transformer()




+ 25
- 6
lark/exceptions.py View File

@@ -36,8 +36,9 @@ class UnexpectedInput(LarkError):


Used as a base class for the following exceptions: Used as a base class for the following exceptions:


- ``UnexpectedToken``: The parser received an unexpected token
- ``UnexpectedCharacters``: The lexer encountered an unexpected string - ``UnexpectedCharacters``: The lexer encountered an unexpected string
- ``UnexpectedToken``: The parser received an unexpected token
- ``UnexpectedEOF``: The parser expected a token, but the input ended


After catching one of these exceptions, you may call the following helper methods to create a nicer error message. After catching one of these exceptions, you may call the following helper methods to create a nicer error message.
""" """
@@ -128,6 +129,9 @@ class UnexpectedInput(LarkError):




class UnexpectedEOF(ParseError, UnexpectedInput): class UnexpectedEOF(ParseError, UnexpectedInput):
"""An exception that is raised by the parser, when the input ends while it still expects a token.
"""

def __init__(self, expected, state=None, terminals_by_name=None): def __init__(self, expected, state=None, terminals_by_name=None):
super(UnexpectedEOF, self).__init__() super(UnexpectedEOF, self).__init__()


@@ -148,6 +152,10 @@ class UnexpectedEOF(ParseError, UnexpectedInput):




class UnexpectedCharacters(LexError, UnexpectedInput): class UnexpectedCharacters(LexError, UnexpectedInput):
"""An exception that is raised by the lexer, when it cannot match the next
string of characters to any of its terminals.
"""

def __init__(self, seq, lex_pos, line, column, allowed=None, considered_tokens=None, state=None, token_history=None, def __init__(self, seq, lex_pos, line, column, allowed=None, considered_tokens=None, state=None, token_history=None,
terminals_by_name=None, considered_rules=None): terminals_by_name=None, considered_rules=None):
super(UnexpectedCharacters, self).__init__() super(UnexpectedCharacters, self).__init__()
@@ -185,10 +193,15 @@ class UnexpectedToken(ParseError, UnexpectedInput):
"""An exception that is raised by the parser, when the token it received """An exception that is raised by the parser, when the token it received
doesn't match any valid step forward. doesn't match any valid step forward.


The parser provides an interactive instance through `interactive_parser`,
which is initialized to the point of failture, and can be used for debugging and error handling.
Parameters:
token: The mismatched token
expected: The set of expected tokens
considered_rules: Which rules were considered, to deduce the expected tokens
state: A value representing the parser state. Do not rely on its value or type.
interactive_parser: An instance of ``InteractiveParser``, that is initialized to the point of failture,
and can be used for debugging and error handling.


see: ``InteractiveParser``.
Note: These parameters are available as attributes of the instance.
""" """


def __init__(self, token, expected, considered_rules=None, state=None, interactive_parser=None, terminals_by_name=None, token_history=None): def __init__(self, token, expected, considered_rules=None, state=None, interactive_parser=None, terminals_by_name=None, token_history=None):
@@ -234,14 +247,20 @@ class VisitError(LarkError):
"""VisitError is raised when visitors are interrupted by an exception """VisitError is raised when visitors are interrupted by an exception


It provides the following attributes for inspection: It provides the following attributes for inspection:
- obj: the tree node or token it was processing when the exception was raised
- orig_exc: the exception that cause it to fail

Parameters:
rule: the name of the visit rule that failed
obj: the tree-node or token that was being processed
orig_exc: the exception that cause it to fail

Note: These parameters are available as attributes
""" """


def __init__(self, rule, obj, orig_exc): def __init__(self, rule, obj, orig_exc):
message = 'Error trying to process rule "%s":\n\n%s' % (rule, orig_exc) message = 'Error trying to process rule "%s":\n\n%s' % (rule, orig_exc)
super(VisitError, self).__init__(message) super(VisitError, self).__init__(message)


self.rule = rule
self.obj = obj self.obj = obj
self.orig_exc = orig_exc self.orig_exc = orig_exc




+ 7
- 1
lark/lark.py View File

@@ -102,7 +102,7 @@ class LarkOptions(Serialize):
A List of either paths or loader functions to specify from where grammars are imported A List of either paths or loader functions to specify from where grammars are imported
source_path source_path
Override the source of from where the grammar was loaded. Useful for relative imports and unconventional grammar loading Override the source of from where the grammar was loaded. Useful for relative imports and unconventional grammar loading
**=== End Options ===**
**=== End of Options ===**
""" """
if __doc__: if __doc__:
__doc__ += OPTIONS_DOC __doc__ += OPTIONS_DOC
@@ -527,6 +527,8 @@ class Lark(Serialize):
"""Only lex (and postlex) the text, without parsing it. Only relevant when lexer='standard' """Only lex (and postlex) the text, without parsing it. Only relevant when lexer='standard'


When dont_ignore=True, the lexer will return all tokens, even those marked for %ignore. When dont_ignore=True, the lexer will return all tokens, even those marked for %ignore.

:raises UnexpectedCharacters: In case the lexer cannot find a suitable match.
""" """
if not hasattr(self, 'lexer') or dont_ignore: if not hasattr(self, 'lexer') or dont_ignore:
lexer = self._build_lexer(dont_ignore) lexer = self._build_lexer(dont_ignore)
@@ -569,6 +571,10 @@ class Lark(Serialize):
If a transformer is supplied to ``__init__``, returns whatever is the If a transformer is supplied to ``__init__``, returns whatever is the
result of the transformation. Otherwise, returns a Tree instance. result of the transformation. Otherwise, returns a Tree instance.


:raises UnexpectedInput: On a parse error, one of these sub-exceptions will rise:
``UnexpectedCharacters``, ``UnexpectedToken``, or ``UnexpectedEOF``.
For convenience, these sub-exceptions also inherit from ``ParserError`` and ``LexerError``.

""" """
return self.parser.parse(text, start=start, on_error=on_error) return self.parser.parse(text, start=start, on_error=on_error)




Loading…
Cancel
Save