1
1
use std:: { borrow:: Cow , str:: Chars } ;
2
2
3
3
use crate :: {
4
- error:: { ParseError , ParseResult } ,
4
+ error:: { ParseError , ParseErrorKind , ParseResult , Span } ,
5
5
Key , Obj , PartialVdf , Value , Vdf ,
6
6
} ;
7
7
@@ -51,7 +51,7 @@ pub fn parse_(s: &str, escape_chars: bool) -> ParseResult<PartialVdf> {
51
51
eat_comments_whitespace_and_newlines ( & mut chars) ?;
52
52
53
53
if chars. peek ( ) . is_some ( ) {
54
- Err ( ParseError :: LingeringBytes )
54
+ Err ( chars . err ( ParseErrorKind :: LingeringBytes , 0 . into ( ) ) )
55
55
} else {
56
56
Ok ( PartialVdf { bases, key, value } )
57
57
}
@@ -79,7 +79,7 @@ fn parse_maybe_macro<'text>(
79
79
}
80
80
81
81
if !eat_whitespace ( chars) {
82
- return Err ( ParseError :: InvalidMacro ) ;
82
+ return Err ( chars . err ( ParseErrorKind :: InvalidMacro , 0 . into ( ) ) ) ;
83
83
}
84
84
85
85
let macro_ = parse_quoted_raw_or_unquoted_string ( chars) ?;
@@ -90,7 +90,7 @@ fn parse_maybe_macro<'text>(
90
90
if eat_newlines ( chars) {
91
91
Ok ( Some ( ( ) ) )
92
92
} else {
93
- Err ( ParseError :: MissingTopLevelPair )
93
+ Err ( chars . err ( ParseErrorKind :: MissingTopLevelPair , 0 . into ( ) ) )
94
94
}
95
95
}
96
96
@@ -108,17 +108,24 @@ fn parse_quoted_raw_or_unquoted_string<'text>(
108
108
fn parse_quoted_raw_string < ' text > ( chars : & mut CharIter < ' text > ) -> ParseResult < & ' text str > {
109
109
assert ! ( chars. ensure_next( '"' ) ) ;
110
110
let start_idx = chars. index ( ) ;
111
- while chars. next ( ) . ok_or ( ParseError :: EoiParsingString ) ? != '"' { }
111
+ while chars
112
+ . next ( )
113
+ . ok_or_else ( || chars. err ( ParseErrorKind :: EoiParsingString , 0 . into ( ) ) ) ?
114
+ != '"'
115
+ { }
112
116
let end_idx = chars. index ( ) - '"' . len_utf8 ( ) ;
113
117
Ok ( & chars. original_str ( ) [ start_idx..end_idx] )
114
118
}
115
119
116
120
fn parse_unquoted_string < ' text > ( chars : & mut CharIter < ' text > ) -> ParseResult < & ' text str > {
117
121
let start_idx = chars. index ( ) ;
118
122
119
- match chars. next ( ) . ok_or ( ParseError :: EoiParsingString ) ? {
123
+ match chars
124
+ . next ( )
125
+ . ok_or_else ( || chars. err ( ParseErrorKind :: EoiParsingString , 0 . into ( ) ) ) ?
126
+ {
120
127
'"' | '{' | '}' | ' ' | '\t' | '\r' | '\n' => {
121
- return Err ( ParseError :: ExpectedUnquotedString )
128
+ return Err ( chars . err ( ParseErrorKind :: ExpectedUnquotedString , 0 . into ( ) ) )
122
129
}
123
130
_ => { }
124
131
}
@@ -162,7 +169,9 @@ fn parse_quoted_string<'text>(
162
169
163
170
let start_idx = chars. index ( ) ;
164
171
loop {
165
- let peeked = chars. peek ( ) . ok_or ( ParseError :: EoiParsingString ) ?;
172
+ let peeked = chars
173
+ . peek ( )
174
+ . ok_or_else ( || chars. err ( ParseErrorKind :: EoiParsingString , 0 . into ( ) ) ) ?;
166
175
// We only care about potential escaped characters if `escape_chars` is set. Otherwise we
167
176
// only break on " for a quoted string
168
177
if peeked == '"' || ( peeked == '\\' && escape_chars) {
@@ -175,23 +184,32 @@ fn parse_quoted_string<'text>(
175
184
let text_chunk = & chars. original_str ( ) [ start_idx..end_idx] ;
176
185
// If our string contains escaped characters then it has to be a `Cow::Owned` otherwise it can
177
186
// be `Cow::Borrowed`
178
- let key = if chars. peek ( ) . ok_or ( ParseError :: EoiParsingString ) ? == '"' {
187
+ let key = if chars
188
+ . peek ( )
189
+ . ok_or_else ( || chars. err ( ParseErrorKind :: EoiParsingString , 0 . into ( ) ) ) ?
190
+ == '"'
191
+ {
179
192
assert ! ( chars. ensure_next( '"' ) ) ;
180
193
Cow :: Borrowed ( text_chunk)
181
194
} else {
182
195
assert ! ( chars. peek( ) . unwrap( ) == '\\' ) ;
183
196
let mut escaped = text_chunk. to_owned ( ) ;
184
197
loop {
185
- let ch = chars. next ( ) . ok_or ( ParseError :: InvalidEscapedCharacter ) ?;
198
+ let ch = chars
199
+ . next ( )
200
+ . ok_or_else ( || chars. err ( ParseErrorKind :: InvalidEscapedCharacter , 0 . into ( ) ) ) ?;
186
201
match ch {
187
202
'"' => break Cow :: Owned ( escaped) ,
188
- '\\' => match chars. next ( ) . ok_or ( ParseError :: InvalidEscapedCharacter ) ? {
203
+ '\\' => match chars
204
+ . next ( )
205
+ . ok_or_else ( || chars. err ( ParseErrorKind :: InvalidEscapedCharacter , 0 . into ( ) ) ) ?
206
+ {
189
207
'n' => escaped. push ( '\n' ) ,
190
208
'r' => escaped. push ( '\r' ) ,
191
209
't' => escaped. push ( '\t' ) ,
192
210
'\\' => escaped. push ( '\\' ) ,
193
211
'\"' => escaped. push ( '\"' ) ,
194
- _ => return Err ( ParseError :: InvalidEscapedCharacter ) ,
212
+ _ => return Err ( chars . err ( ParseErrorKind :: InvalidEscapedCharacter , 0 . into ( ) ) ) ,
195
213
} ,
196
214
regular => escaped. push ( regular) ,
197
215
}
@@ -224,7 +242,11 @@ fn parse_obj<'text>(chars: &mut CharIter<'text>, escape_chars: bool) -> ParseRes
224
242
225
243
let mut obj = Obj :: new ( ) ;
226
244
227
- while chars. peek ( ) . ok_or ( ParseError :: EoiParsingMap ) ? != '}' {
245
+ while chars
246
+ . peek ( )
247
+ . ok_or_else ( || chars. err ( ParseErrorKind :: EoiParsingMap , 0 . into ( ) ) ) ?
248
+ != '}'
249
+ {
228
250
let Vdf { key, value } = parse_pair ( chars, escape_chars) ?;
229
251
obj. 0 . entry ( key) . or_default ( ) . push ( value) ;
230
252
@@ -273,13 +295,13 @@ fn eat_comments(chars: &mut CharIter<'_>) -> ParseResult<bool> {
273
295
chars. unwrap_next ( ) ;
274
296
match chars. next ( ) {
275
297
Some ( '\n' ) => break ,
276
- _ => return Err ( ParseError :: InvalidComment ) ,
298
+ _ => return Err ( chars . err ( ParseErrorKind :: InvalidComment , 0 . into ( ) ) ) ,
277
299
}
278
300
}
279
301
None | Some ( '\n' ) => break ,
280
302
// Various control characters
281
303
Some ( '\u{00}' ..='\u{08}' | '\u{0A}' ..='\u{1F}' | '\u{7F}' ) => {
282
- return Err ( ParseError :: InvalidComment ) ;
304
+ return Err ( chars . err ( ParseErrorKind :: InvalidComment , 0 . into ( ) ) ) ;
283
305
}
284
306
_ => _ = chars. unwrap_next ( ) ,
285
307
}
@@ -403,6 +425,11 @@ impl<'text> CharIter<'text> {
403
425
}
404
426
arr
405
427
}
428
+
429
+ #[ must_use]
430
+ fn err ( & self , kind : ParseErrorKind , span : Span ) -> ParseError {
431
+ todo ! ( ) ;
432
+ }
406
433
}
407
434
408
435
impl Iterator for CharIter < ' _ > {
0 commit comments