Skip to content

Commit 78ed55a

Browse files
committed
Added docstring to TextSlice. Fixed other docstrings
1 parent 9f45875 commit 78ed55a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/classes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ Indenter
9696

9797
.. autoclass:: lark.indenter.Indenter
9898
.. autoclass:: lark.indenter.PythonIndenter
99+
100+
TextSlice
101+
---------
102+
103+
.. autoclass:: lark.utils.TextSlice

lark/lark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def parse_interactive(self, text: Optional[TextOrSlice]=None, start: Optional[st
624624
"""Start an interactive parsing session.
625625
626626
Parameters:
627-
text (str, optional): Text to be parsed. Required for ``resume_parse()``.
627+
text (TextOrSlice, optional): Text to be parsed. Required for ``resume_parse()``.
628628
start (str, optional): Start symbol
629629
630630
Returns:
@@ -638,7 +638,7 @@ def parse(self, text: TextOrSlice, start: Optional[str]=None, on_error: 'Optiona
638638
"""Parse the given text, according to the options provided.
639639
640640
Parameters:
641-
text (str): Text to be parsed.
641+
text (TextOrSlice): Text to be parsed.
642642
start (str, optional): Required if Lark was given multiple possible start symbols (using the start option).
643643
on_error (function, optional): if provided, will be called on UnexpectedToken error. Return true to resume parsing.
644644
LALR only. See examples/advanced/error_handling.py for an example of how to use on_error.

lark/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ def get_regexp_width(expr: str) -> Union[Tuple[int, int], List[int]]:
163163

164164
@dataclass(frozen=True)
165165
class TextSlice(Generic[AnyStr]):
166+
"""A view of a string or bytes object, between the start and end indices.
167+
168+
Never creates a copy.
169+
170+
Lark accepts instances of TextSlice as input (instead of a string),
171+
when the lexer is 'basic' or 'contextual'.
172+
173+
Args:
174+
text (str or bytes): The text to slice.
175+
start (int): The start index. Negative indices are supported.
176+
end (int): The end index. Negative indices are supported.
177+
178+
Raises:
179+
TypeError: If `text` is not a `str` or `bytes`.
180+
AssertionError: If `start` or `end` are out of bounds.
181+
182+
Examples:
183+
>>> TextSlice("Hello, World!", 7, -1)
184+
TextSlice(text='Hello, World!', start=7, end=12)
185+
186+
>>> TextSlice("Hello, World!", 7, None).count("o")
187+
1
188+
189+
"""
166190
text: AnyStr
167191
start: int
168192
end: int

0 commit comments

Comments
 (0)