Skip to content

Commit

Permalink
Added docstring to TextSlice. Fixed other docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Sep 15, 2024
1 parent deda6aa commit 51deaf9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ Indenter

.. autoclass:: lark.indenter.Indenter
.. autoclass:: lark.indenter.PythonIndenter

TextSlice
---------

.. autoclass:: lark.utils.TextSlice
4 changes: 2 additions & 2 deletions lark/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def parse_interactive(self, text: Optional[TextOrSlice]=None, start: Optional[st
"""Start an interactive parsing session.
Parameters:
text (str, optional): Text to be parsed. Required for ``resume_parse()``.
text (TextOrSlice, optional): Text to be parsed. Required for ``resume_parse()``.
start (str, optional): Start symbol
Returns:
Expand All @@ -638,7 +638,7 @@ def parse(self, text: TextOrSlice, start: Optional[str]=None, on_error: 'Optiona
"""Parse the given text, according to the options provided.
Parameters:
text (str): Text to be parsed.
text (TextOrSlice): Text to be parsed.
start (str, optional): Required if Lark was given multiple possible start symbols (using the start option).
on_error (function, optional): if provided, will be called on UnexpectedToken error. Return true to resume parsing.
LALR only. See examples/advanced/error_handling.py for an example of how to use on_error.
Expand Down
24 changes: 24 additions & 0 deletions lark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@ def get_regexp_width(expr: str) -> Union[Tuple[int, int], List[int]]:

@dataclass(frozen=True)
class TextSlice(Generic[AnyStr]):
"""A view of a string or bytes object, between the start and end indices.
Never creates a copy.
Lark accepts instances of TextSlice as input (instead of a string),
when the lexer is 'basic' or 'contextual'.
Args:
text (str or bytes): The text to slice.
start (int): The start index. Negative indices are supported.
end (int): The end index. Negative indices are supported.
Raises:
TypeError: If `text` is not a `str` or `bytes`.
AssertionError: If `start` or `end` are out of bounds.
Examples:
>>> TextSlice("Hello, World!", 7, -1)
TextSlice(text='Hello, World!', start=7, end=12)
>>> TextSlice("Hello, World!", 7, None).count("o")
1
"""
text: AnyStr
start: int
end: int
Expand Down

0 comments on commit 51deaf9

Please sign in to comment.