-
Notifications
You must be signed in to change notification settings - Fork 5
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
Update fixes #2
Update fixes #2
Changes from 13 commits
3c44943
235fe04
36ecbc2
2617d0e
ed11445
1d3a52e
6e9f022
9cfbba9
31bf0f3
e67575b
0366682
9a09d14
9516746
0faa187
8dc3206
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//! A parser for JSON file. | ||
//! | ||
//! And this is a example for JSON parser. | ||
//! | ||
//! Created by [Tomas Tauber](https://github.com/tomtau). | ||
//! Modified by [Boyi Huang](https://github.com/TheVeryDarkness). | ||
|
||
json = pest::SOI ~ value ~ pest::EOI | ||
|
||
/// Matches object, e.g.: `{ "foo": "bar" }` | ||
/// Foobar | ||
object = "{" ~ pair^* ~ "}" | ||
pair = string ~ ":" ~ value | ||
|
||
array = "[" ~ value ~ ("," ~ value)* ~ "]" | "[" ~ "]" | ||
|
||
|
||
////////////////////// | ||
/// Matches value, e.g.: `"foo"`, `42`, `true`, `null`, `[]`, `{}`. | ||
////////////////////// | ||
value = string | number | object | array | bool | null | ||
|
||
string = "\"" - inner - "\"" | ||
inner = (!("\"" | "\\") - pest::any)* - (escape ~ inner)? | ||
escape = "\\" - ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t" | unicode) | ||
// FIXME: should be under pest::* | ||
ascii_hex_digit = ascii_digit | "A" | "B" | "C" | "D" | "E" | "F" | ||
ascii_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | ||
ascii_nonzero_digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | ||
unicode = "u" - ascii_hex_digit{4} | ||
|
||
number = "-"? - int - ("." ~ ascii_digit+ ~ exp? | exp)? | ||
int = "0" | ascii_nonzero_digit - ascii_digit* | ||
exp = ("E" | "e") - ("+" | "-")? - ascii_digit+ | ||
|
||
bool = "true" | "false" | ||
|
||
null = "null" | ||
|
||
space = " " | "\t" | "\r" | "\n" | ||
~ = space* | ||
^ = space* - "," - space* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Created by [Tomas Tauber](https://github.com/tomtau). | ||
//! Modified by [Boyi Huang](https://github.com/TheVeryDarkness). | ||
|
||
/// Grammar rules of a sample JSON parser | ||
#[allow(missing_docs)] | ||
pub mod json { | ||
use pest_derive::Parser; | ||
|
||
/// JSON parser. | ||
#[derive(Parser)] | ||
#[grammar = "tests/json.pest"] | ||
pub struct JsonParser; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use pest::typed::{PairContainer, TypedParser}; | ||
|
||
use crate::{json, json::generics::*}; | ||
#[test] | ||
fn json_basic_test() { | ||
let sample1 = "{\"key\": \"value\"}"; | ||
let s1: json::rules::json = json::JsonParser::try_parse(sample1).unwrap(); | ||
let values = s1.vec_children_pairs(); | ||
assert_eq!(&values[0].rule, &json::Rule::value); | ||
let obj = &values[0].children; | ||
assert_eq!(obj[0].rule, json::Rule::object); | ||
let pair = &obj[0].children; | ||
assert_eq!(pair[0].rule, json::Rule::pair); | ||
let key = &pair[0].children; | ||
assert_eq!(key[0].rule, json::Rule::string); | ||
assert_eq!(&sample1[key[0].start..key[0].end], "\"key\""); | ||
let value = &key[1].children; | ||
assert_eq!(value[0].rule, json::Rule::string); | ||
assert_eq!(&sample1[value[0].start..value[0].end], "\"value\""); | ||
} | ||
|
||
/// Test in structural style. | ||
#[test] | ||
fn json_structural_test() { | ||
let sample1 = "{\"key\": \"value\"}"; | ||
let s1: json::rules::json = json::JsonParser::try_parse(sample1).unwrap(); | ||
let value: json::rules::value<'_> = s1.content.field_1; | ||
match *value.content { | ||
Choice6::Choice2(object) => { | ||
let (_, mut pairs, _) = object.content.into_tuple(); | ||
assert_eq!(pairs.content.len(), 1); | ||
let pair = pairs.content.pop().unwrap(); | ||
let (key, _, value) = pair.content.into_tuple(); | ||
let (_, key, _) = key.content.into_tuple(); | ||
assert_eq!(key.span.as_str(), "key"); | ||
assert_eq!(value.span.as_str(), "\"value\""); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,36 @@ | ||||||||||||||||||||||||||||||
#![allow(unused_variables)] | ||||||||||||||||||||||||||||||
use anyhow::Result; | ||||||||||||||||||||||||||||||
use pest::typed::TypedNode as _; | ||||||||||||||||||||||||||||||
use pest_derive::Parser; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[derive(Parser)] | ||||||||||||||||||||||||||||||
#[grammar = "../meta/tests/pest3sample.pest"] | ||||||||||||||||||||||||||||||
struct Parser; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||
fn comment() -> Result<()> { | ||||||||||||||||||||||||||||||
rules::main::try_parse_partial("x x x /*x*/")?; | ||||||||||||||||||||||||||||||
rules::main::try_parse("x x x /*x*/").unwrap_err(); | ||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||
fn skip_on_two_end() -> Result<()> { | ||||||||||||||||||||||||||||||
rules::main::try_parse_partial("x x ")?; | ||||||||||||||||||||||||||||||
rules::main::try_parse(" x x").unwrap_err(); | ||||||||||||||||||||||||||||||
rules::main::try_parse("x x ").unwrap_err(); | ||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||
fn post_skip() -> Result<()> { | ||||||||||||||||||||||||||||||
let program = rules::program::try_parse("x x /*x*/")?; | ||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||
fn pre_skip() -> Result<()> { | ||||||||||||||||||||||||||||||
rules::program::try_parse("/* x x */ ")?; | ||||||||||||||||||||||||||||||
rules::program::try_parse("/* x x */ x x")?; | ||||||||||||||||||||||||||||||
Ok(()) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+31
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test function + assert!(rules::program::try_parse("/* x x */ ").is_ok());
+ assert!(rules::program::try_parse("/* x x */ x x").is_ok()); Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test function
post_skip
lacks assertions to verify the correctness of the parsing result. It's crucial to assert the expected outcome to ensure the test is effective.+ assert_eq!(program, /* expected parsing result */);
Committable suggestion