@@ -4,36 +4,35 @@ use crate::language::*;
44use crate :: regex:: * ;
55
66#[ derive( Debug , PartialEq , Eq ) ]
7- pub struct ParseResult < ' i > {
7+ struct ParseResult < ' i > {
88 pub document : Document < ' i > ,
99 pub errors : Vec < ParsingError < ' i > > ,
1010}
1111
12- impl < ' i > ParseResult < ' i > {
13- pub fn has_errors ( & self ) -> bool {
14- !self
15- . errors
16- . is_empty ( )
17- }
18- }
19-
20- pub fn parse_with_recovery < ' i > ( path : & ' i Path , content : & ' i str ) -> ParseResult < ' i > {
21- let mut input = Parser :: new ( ) ;
22- input. filename ( path) ;
23- input. initialize ( content) ;
24-
25- input. parse_collecting_errors ( )
26- }
27-
28- pub fn parse_via_taking < ' i > (
12+ // This could be adapted to return both the partial document and the errors.
13+ // But for our purposes if the parse fails then there's no point trying to do
14+ // deeper validation or analysis; the input syntax is broken and the user
15+ // needs to fix it. Should a partial parse turn out have meaning then the
16+ // return type of this can change to ParseResult<'i> but for now it is fine
17+ // to use Result.
18+ pub fn parse_with_recovery < ' i > (
2919 path : & ' i Path ,
3020 content : & ' i str ,
31- ) -> Result < Document < ' i > , ParsingError < ' i > > {
21+ ) -> Result < Document < ' i > , Vec < ParsingError < ' i > > > {
3222 let mut input = Parser :: new ( ) ;
3323 input. filename ( path) ;
3424 input. initialize ( content) ;
3525
36- input. parse_from_start ( )
26+ let result = input. parse_collecting_errors ( ) ;
27+
28+ if result
29+ . errors
30+ . is_empty ( )
31+ {
32+ Ok ( result. document )
33+ } else {
34+ Err ( result. errors )
35+ }
3736}
3837
3938#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -140,8 +139,12 @@ impl<'i> Parser<'i> {
140139 self . offset += width;
141140 }
142141
143- fn parse_from_start ( & mut self ) -> Result < Document < ' i > , ParsingError < ' i > > {
144- // Check if header is present by looking for magic line
142+ fn parse_collecting_errors ( & mut self ) -> ParseResult < ' i > {
143+ // Clear any existing errors
144+ self . problems
145+ . clear ( ) ;
146+
147+ // Parse header, collecting errors instead of propagating
145148 let header = if is_magic_line ( self . source ) {
146149 Some ( self . read_technique_header ( ) ?)
147150 } else {
0 commit comments