Skip to content

Commit

Permalink
fix(grammar): Support trailing quotes (#220)
Browse files Browse the repository at this point in the history
Required munging the grammar so `combine` could understand, but we got
it!

Fixes #58
  • Loading branch information
epage authored Sep 29, 2021
1 parent 13d027f commit c007620
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
46 changes: 38 additions & 8 deletions src/parser/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,29 @@ parse!(hexescape(n: usize) -> char, {
// ml-basic-string = ml-basic-string-delim [ newline ] ml-basic-body
// ml-basic-string-delim
parse!(ml_basic_string() -> String, {
between(
range(ML_BASIC_STRING_DELIM),
(
range(ML_BASIC_STRING_DELIM),
(
optional(newline()),
ml_basic_body(),
).map(|t| t.1),
).message("While parsing a Multiline Basic String")
// Deviate from grammar by pulling mll_quotes into here so we can handle the confusion with
// it and the closing delim
choice((
attempt((
bytes(b"\"\""), range(ML_BASIC_STRING_DELIM)
)).map(|_| Some("\"\"")),
attempt((
bytes(b"\""), range(ML_BASIC_STRING_DELIM)
)).map(|_| Some("\"")),
range(ML_BASIC_STRING_DELIM).map(|_| None),
)),
).map(|(_, mut b, q)| {
if let Some(q) = q {
b.push_str(q);
}
b
}).message("While parsing a Multiline Basic String")
});

// ml-basic-string-delim = 3quotation-mark
Expand All @@ -141,7 +156,7 @@ parse!(ml_basic_body() -> String, {
total.push_str(&c);
total
}))),
// BUG: See #128
// Deviate: see `ml_basic_string`
//optional(mll_quotes()),
).map(|(mut c, qc): (String, String)| {
c.push_str(&qc);
Expand Down Expand Up @@ -216,14 +231,29 @@ fn is_literal_char(c: u8) -> bool {
// ml-literal-string = ml-literal-string-delim [ newline ] ml-literal-body
// ml-literal-string-delim
parse!(ml_literal_string() -> String, {
between(
range(ML_LITERAL_STRING_DELIM),
(
range(ML_LITERAL_STRING_DELIM),
(
optional(newline()),
ml_literal_body(),
).map(|t| t.1.replace("\r\n", "\n")),
).message("While parsing a Multiline Literal String")
// Deviate from grammar by pulling mll_quotes into here so we can handle the confusion with
// it and the closing delim
choice((
attempt((
bytes(b"''"), range(ML_LITERAL_STRING_DELIM)
)).map(|_| Some("''")),
attempt((
bytes(b"'"), range(ML_LITERAL_STRING_DELIM)
)).map(|_| Some("'")),
range(ML_LITERAL_STRING_DELIM).map(|_| None),
))
).map(|(_, mut b, q)| {
if let Some(q) = q {
b.push_str(q);
}
b
}).message("While parsing a Multiline Literal String")
});

// ml-literal-string-delim = 3apostrophe
Expand All @@ -234,7 +264,7 @@ parse!(ml_literal_body() -> &'a str, {
recognize((
skip_many(mll_content()),
skip_many(attempt((mll_quotes(), skip_many1(mll_content())))),
// BUG: See #128
// Deviate: see ml_literal_string
//optional(mll_quotes()),
)).and_then(std::str::from_utf8)
});
Expand Down
5 changes: 1 addition & 4 deletions tests/decoder_compliance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ mod decoder;

fn main() {
let decoder = decoder::Decoder;
let mut harness = toml_test_harness::DecoderHarness::new(decoder);
harness
.ignore(["valid/string/multiline-quotes.toml"])
.unwrap();
let harness = toml_test_harness::DecoderHarness::new(decoder);
harness.test();
}
5 changes: 1 addition & 4 deletions tests/easy_decoder_compliance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ mod easy_decoder;
#[cfg(feature = "easy")]
fn main() {
let decoder = easy_decoder::Decoder;
let mut harness = toml_test_harness::DecoderHarness::new(decoder);
harness
.ignore(["valid/string/multiline-quotes.toml"])
.unwrap();
let harness = toml_test_harness::DecoderHarness::new(decoder);
harness.test();
}

Expand Down
8 changes: 1 addition & 7 deletions tests/easy_encoder_compliance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ mod easy_encoder;
fn main() {
let encoder = easy_encoder::Encoder;
let decoder = easy_decoder::Decoder;
let mut harness = toml_test_harness::EncoderHarness::new(encoder, decoder);
harness
.ignore([
// Can't verify until decoder is fixed
"valid/string/multiline-quotes.toml",
])
.unwrap();
let harness = toml_test_harness::EncoderHarness::new(encoder, decoder);
harness.test();
}

Expand Down
8 changes: 1 addition & 7 deletions tests/encoder_compliance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ mod encoder;
fn main() {
let encoder = encoder::Encoder;
let decoder = decoder::Decoder;
let mut harness = toml_test_harness::EncoderHarness::new(encoder, decoder);
harness
.ignore([
// Can't verify until decoder is fixed
"valid/string/multiline-quotes.toml",
])
.unwrap();
let harness = toml_test_harness::EncoderHarness::new(encoder, decoder);
harness.test();
}

0 comments on commit c007620

Please sign in to comment.