Skip to content

Commit

Permalink
Docs: Added Indenter
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Jun 15, 2024
1 parent 7bdb507 commit b005812
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ For an example of using ``ast_utils``, see `/examples/advanced/create_ast.py`_
.. autofunction:: lark.ast_utils.create_transformer

.. _/examples/advanced/create_ast.py: examples/advanced/create_ast.html

Indenter
--------

.. autoclass:: lark.indenter.Indenter
.. autoclass:: lark.indenter.PythonIndenter
28 changes: 28 additions & 0 deletions lark/indenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ class DedentError(LarkError):
pass

class Indenter(PostLex, ABC):
"""A postlexer that "injects" indent/dedent tokens based on indentation.
Note: This is an abstract class. To use it, inherit and implement all its abstract methods:
- tab_len
- NL_type
- OPEN_PAREN_types, CLOSE_PAREN_types
- INDENT_type, DEDENT_type
See also: the ``postlex`` option in `Lark`.
"""
paren_level: int
indent_level: List[int]

Expand Down Expand Up @@ -73,35 +83,53 @@ def always_accept(self):
@property
@abstractmethod
def NL_type(self) -> str:
"The name of the newline token"
raise NotImplementedError()

@property
@abstractmethod
def OPEN_PAREN_types(self) -> List[str]:
"The names of the tokens that open a parenthesis"
raise NotImplementedError()

@property
@abstractmethod
def CLOSE_PAREN_types(self) -> List[str]:
"""The names of the tokens that close a parenthesis
"""
raise NotImplementedError()

@property
@abstractmethod
def INDENT_type(self) -> str:
"""The name of the token that starts an indentation in the grammar.
See also: %declare
"""
raise NotImplementedError()

@property
@abstractmethod
def DEDENT_type(self) -> str:
"""The name of the token that end an indentation in the grammar.
See also: %declare
"""
raise NotImplementedError()

@property
@abstractmethod
def tab_len(self) -> int:
"""How many spaces does a tab equal"""
raise NotImplementedError()


class PythonIndenter(Indenter):
"""A postlexer that "injects" _INDENT/_DEDENT tokens based on indentation, according to the Python syntax.
See also: the ``postlex`` option in `Lark`.
"""

NL_type = '_NEWLINE'
OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
Expand Down

0 comments on commit b005812

Please sign in to comment.