Skip to content

Commit 5c5b293

Browse files
committed
document .span() and SourceSpan
1 parent 381afac commit 5c5b293

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

docs/ref/methods_and_combinators.rst

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ can be used and manipulated as below.
2323
The following methods are for actually **using** the parsers that you have
2424
created:
2525

26-
.. method:: parse(string_or_list)
26+
.. method:: parse(string_or_list[, source=None])
2727

2828
Attempts to parse the given string (or list). If the parse is successful
2929
and consumes the entire string, the result is returned - otherwise, a
@@ -36,7 +36,11 @@ can be used and manipulated as below.
3636
library will work with tokens just as well. See :doc:`/howto/lexing` for
3737
more information.
3838

39-
.. method:: parse_partial(string_or_list)
39+
When a non-None ``source`` is given, this name is reported automatically
40+
in parse errors. Typically, this is the file path or URL where the data
41+
to parse originates from.
42+
43+
.. method:: parse_partial(string_or_list[, source=None])
4044

4145
Similar to ``parse``, except that it does not require the entire
4246
string (or list) to be consumed. Returns a tuple of
@@ -401,6 +405,20 @@ can be used and manipulated as below.
401405
</howto/lexing/>` and want subsequent parsing of the token stream to be
402406
able to report original positions in error messages etc.
403407

408+
.. method:: span()
409+
410+
Returns a parser that augments the initial parser's result with a :class:`SourceSpan`
411+
containing information about where that parser started and stopped within the
412+
source data. The new value is a tuple:
413+
414+
.. code:: python
415+
416+
(source_span, original_value)
417+
418+
This enables reporting of custom errors involving source locations, such as when
419+
using parsy as a :doc:`lexer</howto/lexing/>` or when building a syntax tree that will be
420+
further analyzed.
421+
404422
.. _operators:
405423

406424
Parser operators
@@ -594,3 +612,26 @@ Parsy does not try to include every possible combinator - there is no reason why
594612
you cannot create your own for your needs using the built-in combinators and
595613
primitives. If you find something that is very generic and would be very useful
596614
to have as a built-in, please :doc:`submit </contributing>` as a PR!
615+
616+
Auxiliary data structures
617+
=========================
618+
619+
.. class:: SourceSpan
620+
621+
Identifies a span of material from the data being parsed by its start row and column and its end
622+
row and column. If the data stream was equipped with a source, that value is also available in
623+
this object.
624+
625+
.. attribute:: start
626+
627+
The starting position of this span as a tuple (row, col)
628+
629+
.. attribute:: end
630+
631+
The stopping position of this span as a tuple (row, col)
632+
633+
.. attribute:: source
634+
635+
The name of the source the data was parsed from. This is the same value
636+
that was passed to :meth:`Parser.parse` or :meth:`Parser.parse_partial`,
637+
or `None` if no value was given.

0 commit comments

Comments
 (0)