Skip to content

Commit d039ba1

Browse files
wip: Could use more refactors, but everything has spans
1 parent 668d2df commit d039ba1

File tree

3 files changed

+155
-59
lines changed

3 files changed

+155
-59
lines changed

keyvalues-parser/src/error.rs

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// TODO: move most of this into a `parse` module?
44

55
use std::{
6-
fmt::{self, Write},
6+
fmt,
77
ops::{RangeFrom, RangeInclusive},
88
};
99

@@ -117,9 +117,7 @@ impl fmt::Display for ParseError {
117117
} else {
118118
0
119119
};
120-
let padding_before = on_start_line
121-
.then(|| " ".repeat(num_before))
122-
.unwrap_or_default();
120+
let padding_before = " ".repeat(num_before);
123121

124122
let (num_after, append_extra_arrow) = if let Some(display_end) = display_end {
125123
let num_after = line.len().checked_sub(display_end.col).unwrap();
@@ -195,7 +193,7 @@ pub enum ParseErrorInner {
195193
/// assert_eq!(err.inner(), ParseErrorInner::ExpectedNewlineAfterMacro);
196194
/// # print!("{err}");
197195
/// let expected = r#"
198-
/// error: Expected newline after macro
196+
/// error: Expected newline after macro definition
199197
/// at: 1:25 to the end of input
200198
/// 1 | #base robot_standard.pop
201199
/// | ^
@@ -210,18 +208,25 @@ pub enum ParseErrorInner {
210208
/// ```
211209
/// # use keyvalues_parser::{Vdf, error::ParseErrorInner};
212210
/// let err = Vdf::parse("key \"incomplete ...").unwrap_err();
213-
/// assert_eq!(err.inner(), ParseErrorInner::EoiParsingString);
211+
/// assert_eq!(err.inner(), ParseErrorInner::EoiParsingQuotedString);
214212
/// # print!("{err}");
215213
/// 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
217215
/// at: 1:5 to the end of input
218216
/// 1 | key "incomplete ...
219217
/// | ^^^^^^^^^^^^^^^^
220218
/// "#.trim_start();
221219
/// assert_eq!(err.to_string(), expected);
222220
/// ```
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,
225230
/// Encountered an invalid escape character in a quoted string
226231
///
227232
/// # Example
@@ -261,25 +266,72 @@ pub enum ParseErrorInner {
261266
/// assert_eq!(err.to_string(), expected);
262267
/// ```
263268
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?
264287
// TODO: store the invalid character
265-
InvalidComment,
288+
CommentControlCharacter,
266289
}
267290

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
268299
impl fmt::Display for ParseErrorInner {
269300
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
270301
match self {
271302
Self::LingeringBytes => f.write_str("Found bytes after the top-level pair"),
272303
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")
276327
}
277-
Self::ExpectedUnquotedString => f.write_str("Expected unquoted string"),
278328
Self::InvalidEscapedCharacter { invalid } => {
279329
write!(f, "Invalid escaped string character \\{invalid}")
280330
}
281331
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+
}
283335
}
284336
}
285337
}

keyvalues-parser/src/text/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
// TODO: remove the `text` module since we're only focused on text atm
12
pub mod parse;
23
pub mod render;

0 commit comments

Comments
 (0)