3
3
// TODO: move most of this into a `parse` module?
4
4
5
5
use std:: {
6
- fmt:: { self , Write } ,
6
+ fmt,
7
7
ops:: { RangeFrom , RangeInclusive } ,
8
8
} ;
9
9
@@ -117,9 +117,7 @@ impl fmt::Display for ParseError {
117
117
} else {
118
118
0
119
119
} ;
120
- let padding_before = on_start_line
121
- . then ( || " " . repeat ( num_before) )
122
- . unwrap_or_default ( ) ;
120
+ let padding_before = " " . repeat ( num_before) ;
123
121
124
122
let ( num_after, append_extra_arrow) = if let Some ( display_end) = display_end {
125
123
let num_after = line. len ( ) . checked_sub ( display_end. col ) . unwrap ( ) ;
@@ -195,7 +193,7 @@ pub enum ParseErrorInner {
195
193
/// assert_eq!(err.inner(), ParseErrorInner::ExpectedNewlineAfterMacro);
196
194
/// # print!("{err}");
197
195
/// let expected = r#"
198
- /// error: Expected newline after macro
196
+ /// error: Expected newline after macro definition
199
197
/// at: 1:25 to the end of input
200
198
/// 1 | #base robot_standard.pop
201
199
/// | ^
@@ -210,18 +208,25 @@ pub enum ParseErrorInner {
210
208
/// ```
211
209
/// # use keyvalues_parser::{Vdf, error::ParseErrorInner};
212
210
/// let err = Vdf::parse("key \"incomplete ...").unwrap_err();
213
- /// assert_eq!(err.inner(), ParseErrorInner::EoiParsingString );
211
+ /// assert_eq!(err.inner(), ParseErrorInner::EoiParsingQuotedString );
214
212
/// # print!("{err}");
215
213
/// let expected = r#"
216
- /// error: Encountered the end of input while parsing a string
214
+ /// error: Encountered the end of input while parsing a quoted string
217
215
/// at: 1:5 to the end of input
218
216
/// 1 | key "incomplete ...
219
217
/// | ^^^^^^^^^^^^^^^^
220
218
/// "#.trim_start();
221
219
/// assert_eq!(err.to_string(), expected);
222
220
/// ```
223
- EoiParsingString ,
224
- ExpectedUnquotedString ,
221
+ EoiParsingQuotedString ,
222
+ EoiExpectedMacroPath ,
223
+ InvalidMacroPath ,
224
+ // TODO: remove this error variant in favor of a MissingTopLevelPair and
225
+ // ExpectedPairKeyOrMapClose
226
+ EoiExpectedPairKey ,
227
+ InvalidPairKey ,
228
+ EoiExpectedPairValue ,
229
+ InvalidPairValue ,
225
230
/// Encountered an invalid escape character in a quoted string
226
231
///
227
232
/// # Example
@@ -261,25 +266,72 @@ pub enum ParseErrorInner {
261
266
/// assert_eq!(err.to_string(), expected);
262
267
/// ```
263
268
EoiParsingMap ,
269
+ /// Encountered an invalid control character while parsing a comment
270
+ ///
271
+ /// # Example
272
+ ///
273
+ /// ```
274
+ /// # use keyvalues_parser::{Vdf, error::ParseErrorInner};
275
+ /// let err = Vdf::parse("// Only valid before a newline -> \r uh oh").unwrap_err();
276
+ /// assert_eq!(err.inner(), ParseErrorInner::CommentControlCharacter);
277
+ /// # print!("{err}");
278
+ /// let expected = r#"
279
+ /// error: Encountered an invalid control character while parsing a comment
280
+ /// at: 1:35
281
+ /// 1 | // Only valid before a newline -> uh oh
282
+ /// | ^
283
+ /// "#.trim_start();
284
+ /// assert_eq!(err.to_string(), expected);
285
+ /// ```
286
+ // TODO: pretty up the display of `\r` akin to how we did in sd?
264
287
// TODO: store the invalid character
265
- InvalidComment ,
288
+ CommentControlCharacter ,
266
289
}
267
290
291
+ pub enum Component {
292
+ MacroPath ,
293
+ PairKey ,
294
+ PairValue ,
295
+ }
296
+
297
+ // TODO: can we group together the pairs of EoiExpected* and Invalid* to store a kind of
298
+ // macro/key/value
268
299
impl fmt:: Display for ParseErrorInner {
269
300
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
270
301
match self {
271
302
Self :: LingeringBytes => f. write_str ( "Found bytes after the top-level pair" ) ,
272
303
Self :: ExpectedWhitespace => f. write_str ( "Expected whitespace" ) ,
273
- Self :: ExpectedNewlineAfterMacro => f. write_str ( "Expected newline after macro" ) ,
274
- Self :: EoiParsingString => {
275
- f. write_str ( "Encountered the end of input while parsing a string" )
304
+ Self :: ExpectedNewlineAfterMacro => {
305
+ f. write_str ( "Expected newline after macro definition" )
306
+ }
307
+ Self :: EoiExpectedMacroPath => {
308
+ f. write_str ( "Found the end of input while looking for a macro path" )
309
+ }
310
+ Self :: InvalidMacroPath => {
311
+ f. write_str ( "Encountered an invalid character while parsing a macro path" )
312
+ }
313
+ Self :: EoiExpectedPairKey => {
314
+ f. write_str ( "Encountered the end of input while looking for a pair's key" )
315
+ }
316
+ Self :: InvalidPairKey => {
317
+ f. write_str ( "Encountered an invalid character while looking for a pair's key" )
318
+ }
319
+ Self :: EoiExpectedPairValue => {
320
+ f. write_str ( "Encountered the end of input while looking for a pair's key" )
321
+ }
322
+ Self :: InvalidPairValue => {
323
+ f. write_str ( "Encountered an invalid character while looking for a pair's key" )
324
+ }
325
+ Self :: EoiParsingQuotedString => {
326
+ f. write_str ( "Encountered the end of input while parsing a quoted string" )
276
327
}
277
- Self :: ExpectedUnquotedString => f. write_str ( "Expected unquoted string" ) ,
278
328
Self :: InvalidEscapedCharacter { invalid } => {
279
329
write ! ( f, "Invalid escaped string character \\ {invalid}" )
280
330
}
281
331
Self :: EoiParsingMap => f. write_str ( "Encountered the end of input while parsing a map" ) ,
282
- Self :: InvalidComment => f. write_str ( "Invalid character in comment" ) ,
332
+ Self :: CommentControlCharacter => {
333
+ f. write_str ( "Encountered an invalid control character while parsing a comment" )
334
+ }
283
335
}
284
336
}
285
337
}
0 commit comments