11//! Parse error reporting
22
3- use crate :: { Parse , RuleResult } ;
3+ use crate :: { Parse , RuleResult , ParseResults , ParseResult } ;
44use std:: collections:: HashSet ;
55use std:: fmt:: { self , Debug , Display } ;
66
@@ -15,6 +15,13 @@ impl ExpectedSet {
1515 pub fn tokens < ' a > ( & ' a self ) -> impl Iterator < Item = & ' static str > + ' a {
1616 self . expected . iter ( ) . map ( |x| * x)
1717 }
18+
19+ /// Construct a new singleton set.
20+ pub ( crate ) fn singleton ( error : & ' static str ) -> Self {
21+ let mut expected = HashSet :: new ( ) ;
22+ expected. insert ( error) ;
23+ ExpectedSet { expected }
24+ }
1825}
1926
2027impl Display for ExpectedSet {
@@ -65,6 +72,7 @@ impl<L: Display + Debug> ::std::error::Error for ParseError<L> {
6572}
6673
6774#[ doc( hidden) ]
75+ #[ derive( Debug ) ]
6876pub struct ErrorState {
6977 /// Furthest failure we've hit so far.
7078 pub max_err_pos : usize ,
@@ -75,9 +83,9 @@ pub struct ErrorState {
7583
7684 /// Are we reparsing after a failure? If so, compute and store expected set of all alternative expectations
7785 /// when we are at offset `max_err_pos`.
78- pub reparsing_on_error : bool ,
86+ pub reparsing_on_failure : bool ,
7987
80- /// The set of tokens we expected to find when we hit the failure. Updated when `reparsing_on_error `.
88+ /// The set of tokens we expected to find when we hit the failure. Updated when `reparsing_on_failure `.
8189 pub expected : ExpectedSet ,
8290}
8391
@@ -86,21 +94,21 @@ impl ErrorState {
8694 ErrorState {
8795 max_err_pos : initial_pos,
8896 suppress_fail : 0 ,
89- reparsing_on_error : false ,
97+ reparsing_on_failure : false ,
9098 expected : ExpectedSet {
9199 expected : HashSet :: new ( ) ,
92100 } ,
93101 }
94102 }
95103
96104 /// Set up for reparsing to record the details of the furthest failure.
97- pub fn reparse_for_error ( & mut self ) {
105+ pub fn reparse_for_failure ( & mut self ) {
98106 self . suppress_fail = 0 ;
99- self . reparsing_on_error = true ;
107+ self . reparsing_on_failure = true ;
100108 }
101109
102110 #[ inline( never) ]
103- pub fn mark_failure_slow_path ( & mut self , pos : usize , expected : & ' static str ) {
111+ fn mark_failure_slow_path ( & mut self , pos : usize , expected : & ' static str ) {
104112 if pos == self . max_err_pos {
105113 self . expected . expected . insert ( expected) ;
106114 }
@@ -110,7 +118,7 @@ impl ErrorState {
110118 #[ inline( always) ]
111119 pub fn mark_failure ( & mut self , pos : usize , expected : & ' static str ) -> RuleResult < ( ) > {
112120 if self . suppress_fail == 0 {
113- if self . reparsing_on_error {
121+ if self . reparsing_on_failure {
114122 self . mark_failure_slow_path ( pos, expected) ;
115123 } else if pos > self . max_err_pos {
116124 self . max_err_pos = pos;
@@ -119,10 +127,25 @@ impl ErrorState {
119127 RuleResult :: Failed
120128 }
121129
122- pub fn into_parse_error < I : Parse + ?Sized > ( self , input : & I ) -> ParseError < I :: PositionRepr > {
123- ParseError {
124- location : Parse :: position_repr ( input, self . max_err_pos . into ( ) ) ,
125- expected : self . expected ,
130+ }
131+ }
132+
133+ /// Build `Matched` parse result.
134+ pub fn into_matched < T , I : Parse + ?Sized > ( self , v : T , input : & I ) -> ParseResults < T , I :: PositionRepr > {
135+ ParseResults {
136+ result : ParseResult :: Matched ( v) ,
137+ errors : errors_positioned_in ( self . errors , input) ,
138+ }
139+ }
140+
141+ /// Build `Failed` parse result.
142+ pub fn into_failure < T , I : Parse + ?Sized > ( self , input : & I ) -> ParseResults < T , I :: PositionRepr > {
143+ ParseResults {
144+ result : ParseResult :: Failed ( ParseError {
145+ expected : self . expected ,
146+ location : input. position_repr ( self . max_err_pos )
147+ } ) ,
148+ errors : errors_positioned_in ( self . errors , input) ,
126149 }
127150 }
128151}
0 commit comments