-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some tests for user-defined meta rules that accept some PEGs as argum…
…ents. (#14) * CI: enable benchmarks on pushes to all branches * CI: update names and add a few comments. * Add a test for meta rules. * CI: enable impact analysis of codecov. * Update the test for meta rules. * Update documents about meta rules.
- Loading branch information
1 parent
b597196
commit 53d8839
Showing
5 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
comment: | ||
layout: "diff,flags,tree,betaprofiling" | ||
show_critical_paths: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use anyhow::Result; | ||
use pest3::typed::TypedParser; | ||
use pest3_derive::Parser; | ||
use serde_json::{json, Value}; | ||
use std::iter::once; | ||
|
||
#[derive(Parser)] | ||
#[grammar_inline = r#" | ||
separated(i, sep) = i - (sep - i)* | ||
cell = ('a'..'z' | 'A'..'Z' | '0'..'9' | ".")+ | ||
main = separated(separated(cell, " "* - "," - " "*), "\n") | ||
"#] | ||
struct Parser; | ||
|
||
fn to_json(input: &str) -> Result<Value> { | ||
let file = Parser::try_parse::<rules::main>(input)?; | ||
let (first, following) = file.separated().i(); | ||
let lines = once(first).chain(following); | ||
let lines = lines.map(|line| { | ||
let (first, following) = line.i(); | ||
let cells = once(first).chain(following); | ||
let cells = cells.map(|cell| Value::String(cell.span.as_str().to_owned())); | ||
Value::Array(cells.collect()) | ||
}); | ||
let res = Value::Array(lines.collect()); | ||
Ok(res) | ||
} | ||
|
||
#[test] | ||
fn main() -> Result<()> { | ||
assert_eq!(to_json("123,456")?, json!([["123", "456"]])); | ||
assert_eq!( | ||
to_json("123,456\n789,abc")?, | ||
json!([["123", "456"], ["789", "abc"]]) | ||
); | ||
assert!(to_json("").is_err()); | ||
assert!(to_json("123,").is_err()); | ||
assert!(to_json("123,456,").is_err()); | ||
assert!(to_json("123\n").is_err()); | ||
assert!(to_json("123,\n456").is_err()); | ||
Ok(()) | ||
} |