@@ -23,7 +23,7 @@ can be used and manipulated as below.
23
23
The following methods are for actually **using ** the parsers that you have
24
24
created:
25
25
26
- .. method :: parse(string_or_list)
26
+ .. method :: parse(string_or_list[, source=None] )
27
27
28
28
Attempts to parse the given string (or list). If the parse is successful
29
29
and consumes the entire string, the result is returned - otherwise, a
@@ -36,7 +36,11 @@ can be used and manipulated as below.
36
36
library will work with tokens just as well. See :doc: `/howto/lexing ` for
37
37
more information.
38
38
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])
40
44
41
45
Similar to ``parse ``, except that it does not require the entire
42
46
string (or list) to be consumed. Returns a tuple of
@@ -401,6 +405,20 @@ can be used and manipulated as below.
401
405
</howto/lexing/>` and want subsequent parsing of the token stream to be
402
406
able to report original positions in error messages etc.
403
407
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
+
404
422
.. _operators :
405
423
406
424
Parser operators
@@ -594,3 +612,26 @@ Parsy does not try to include every possible combinator - there is no reason why
594
612
you cannot create your own for your needs using the built- in combinators and
595
613
primitives. If you find something that is very generic and would be very useful
596
614
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