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

refactor(parser): Prepare for nom parser #401

Merged
merged 5 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/toml_edit/src/document.rs
Expand Up @@ -70,7 +70,7 @@ impl FromStr for Document {

/// Parses a document from a &str
fn from_str(s: &str) -> Result<Self, Self::Err> {
parser::TomlParser::parse(s.as_bytes())
parser::document(s.as_bytes())
}
}

Expand Down
62 changes: 59 additions & 3 deletions crates/toml_edit/src/parser/array.rs
@@ -1,11 +1,12 @@
use crate::parser::trivia::ws_comment_newline;
use crate::parser::value::value;
use crate::{Array, Value};
use combine::parser::byte::byte;
use combine::parser::range::recognize_with_value;
use combine::stream::RangeStream;
use combine::*;

use crate::parser::trivia::ws_comment_newline;
use crate::parser::value::value;
use crate::{Array, Value};

// ;; Array

// array = array-open array-values array-close
Expand Down Expand Up @@ -55,3 +56,58 @@ parse!(array_value() -> Value, {
Ok(v)
})
});

#[cfg(test)]
mod test {
use super::*;

use combine::stream::position::Stream;

#[test]
fn arrays() {
let inputs = [
r#"[]"#,
r#"[ ]"#,
r#"[
1, 2, 3
]"#,
r#"[
1,
2, # this is ok
]"#,
r#"[# comment
# comment2


]"#,
r#"[# comment
# comment2
1

#sd
,
# comment3

]"#,
r#"[1]"#,
r#"[1,]"#,
r#"[ "all", 'strings', """are the same""", '''type''']"#,
r#"[ 100, -2,]"#,
r#"[1, 2, 3]"#,
r#"[1.1, 2.1, 3.1]"#,
r#"["a", "b", "c"]"#,
r#"[ [ 1, 2 ], [3, 4, 5] ]"#,
r#"[ [ 1, 2 ], ["a", "b", "c"] ]"#,
r#"[ { x = 1, a = "2" }, {a = "a",b = "b", c = "c"} ]"#,
];
for input in inputs {
parsed_value_eq!(input);
}

let invalid_inputs = [r#"["#, r#"[,]"#, r#"[,2]"#, r#"[1e165,,]"#];
for input in invalid_inputs {
let parsed = array().easy_parse(Stream::new(input.as_bytes()));
assert!(parsed.is_err());
}
}
}
45 changes: 45 additions & 0 deletions crates/toml_edit/src/parser/datetime.rs
Expand Up @@ -208,3 +208,48 @@ parse!(unsigned_digits(count: usize) -> u32, {
s.parse::<u32>()
})
});

#[cfg(test)]
mod test {
#[test]
fn offset_date_time() {
let inputs = [
"1979-05-27T07:32:00Z",
"1979-05-27T00:32:00-07:00",
"1979-05-27T00:32:00.999999-07:00",
];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn local_date_time() {
let inputs = ["1979-05-27T07:32:00", "1979-05-27T00:32:00.999999"];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn local_date() {
let inputs = ["1979-05-27", "2017-07-20"];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn local_time() {
let inputs = ["07:32:00", "00:32:00.999999"];
for input in inputs {
parsed_date_time_eq!(input, is_datetime);
}
}

#[test]
fn time_fraction_truncated() {
let input = "1987-07-05T17:45:00.123456789012345Z";
parsed_date_time_eq!(input, is_datetime);
}
}