From 51deaf9a2173e4f2210cedeaa9d295084f1340a9 Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sun, 15 Sep 2024 20:32:28 +0300 Subject: [PATCH] Added docstring to TextSlice. Fixed other docstrings --- docs/classes.rst | 5 +++++ lark/lark.py | 4 ++-- lark/utils.py | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/docs/classes.rst b/docs/classes.rst index 78b4bc04..7e88cefb 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -96,3 +96,8 @@ Indenter .. autoclass:: lark.indenter.Indenter .. autoclass:: lark.indenter.PythonIndenter + +TextSlice +--------- + +.. autoclass:: lark.utils.TextSlice diff --git a/lark/lark.py b/lark/lark.py index 0f3caf91..4d8c99c6 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -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: @@ -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. diff --git a/lark/utils.py b/lark/utils.py index a9ddeb5e..51d40d45 100644 --- a/lark/utils.py +++ b/lark/utils.py @@ -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