Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): various tests and bugfixes #112

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/v2_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ fn escaped_char(input: &mut Input<'_>) -> PResult<char> {
)),
(
"u{",
cut_err(take_while(1..6, AsChar::is_hex_digit)),
cut_err(take_while(1..=6, AsChar::is_hex_digit)),
cut_err("}"),
)
.context(cx().lbl("unicode escape char"))
Expand All @@ -1250,6 +1250,7 @@ fn escaped_char(input: &mut Input<'_>) -> PResult<char> {
/// multi-line-raw-string-body := (unicode - disallowed-literal-code-points)*?
/// ```
fn raw_string(input: &mut Input<'_>) -> PResult<KdlValue> {
let _start_loc = input.location();
let hashes: String = repeat(1.., "#").parse_next(input)?;
let quotes = alt((("\"\"\"", newline).take(), "\"")).parse_next(input)?;
let is_multiline = quotes.len() > 1;
Expand Down Expand Up @@ -1343,7 +1344,17 @@ fn raw_string(input: &mut Input<'_>) -> PResult<KdlValue> {
"\"".context(cx().lbl("raw string closing quotes"))
};
cut_err((closing_quotes, &hashes[..])).parse_next(input)?;
Ok(KdlValue::String(body))
if body == "\"" {
Err(ErrMode::Cut(KdlParseError {
message: Some("Single-line raw strings cannot look like multi-line ones".into()),
span: Some((_start_loc..input.location()).into()),
label: Some("triple quotes".into()),
help: Some("Consider using a regular escaped string if all you want is a single quote: \"\\\"\"".into()),
severity: Some(Severity::Error),
}))
} else {
Ok(KdlValue::String(body))
}
}

/// Like badval, but is able to slurp up invalid raw strings, which contain whitespace.
Expand Down Expand Up @@ -1383,6 +1394,10 @@ mod string_tests {
string.parse(new_input("\"foo\\u{0a}\"")).unwrap(),
Some(KdlValue::String("foo\u{0a}".into()))
);
assert_eq!(
string.parse(new_input("\"\\u{10FFFF}\"")).unwrap(),
Some(KdlValue::String("\u{10ffff}".into()))
);
}

#[test]
Expand Down Expand Up @@ -1449,6 +1464,7 @@ mod string_tests {
string.parse(new_input("#\"foo\"#")).unwrap(),
Some(KdlValue::String("foo".into()))
);
assert!(string.parse(new_input("#\"\"\"#")).is_err());
}

#[test]
Expand Down Expand Up @@ -1686,6 +1702,8 @@ fn slashdash(input: &mut Input<'_>) -> PResult<()> {
#[test]
fn slashdash_tests() {
assert!(document.parse(new_input("/- foo bar")).is_ok());
assert!(document.parse(new_input("/- foo bar;")).is_ok());
assert!(document.parse(new_input("/-n 1;")).is_ok());
assert!(node.parse(new_input("/- foo\nbar baz")).is_ok());
assert!(node_entry.parse(new_input("/-commented tada")).is_ok());
assert!(node.parse(new_input("foo /- { }")).is_ok());
Expand Down
Loading