-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a parser to process the path we've described such as: ".-0-1-b"
- Loading branch information
1 parent
cc24c34
commit 154da35
Showing
4 changed files
with
161 additions
and
0 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,13 @@ | ||
root = { "." } | ||
|
||
separator = { "-" } | ||
|
||
body = { "b" } | ||
|
||
num = { ASCII_DIGIT+ } | ||
|
||
branch = {"t" | "f"} | ||
|
||
clause = { separator ~ (body | num | branch) } | ||
|
||
path = { SOI ~ root ~ clause* ~ EOI } |
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,116 @@ | ||
use super::{core::ParseNodes, ParsePath}; | ||
|
||
use pest_consume::{match_nodes, Error, Parser}; | ||
|
||
type ParseResult<T> = std::result::Result<T, Error<Rule>>; | ||
type Node<'i> = pest_consume::Node<'i, Rule, ()>; | ||
|
||
// include the grammar file so that Cargo knows to rebuild this file on grammar changes | ||
const _GRAMMAR: &str = include_str!("path_parser.pest"); | ||
|
||
#[derive(Parser)] | ||
#[grammar = "debugger/commands/path_parser.pest"] | ||
|
||
pub struct PathParser; | ||
|
||
#[pest_consume::parser] | ||
impl PathParser { | ||
fn EOI(_input: Node) -> ParseResult<()> { | ||
Ok(()) | ||
} | ||
|
||
fn root(_input: Node) -> ParseResult<()> { | ||
Ok(()) | ||
} | ||
|
||
fn body(_input: Node) -> ParseResult<()> { | ||
Ok(()) | ||
} | ||
|
||
fn separator(_input: Node) -> ParseResult<()> { | ||
Ok(()) | ||
} | ||
|
||
fn num(input: Node) -> ParseResult<u32> { | ||
input | ||
.as_str() | ||
.parse::<u32>() | ||
.map_err(|_| input.error("Expected non-negative number")) | ||
} | ||
|
||
fn branch(input: Node) -> ParseResult<bool> { | ||
let b = input.as_str(); | ||
let result = b != "f"; | ||
Ok(result) | ||
} | ||
|
||
fn clause(input: Node) -> ParseResult<ParseNodes> { | ||
Ok(match_nodes!(input.into_children(); | ||
[separator(_), num(n)] => ParseNodes::Offset(n), | ||
[separator(_), body(_)] => ParseNodes::Body, | ||
[separator(_), branch(b)] => ParseNodes::If(b) | ||
)) | ||
} | ||
|
||
fn path(input: Node) -> ParseResult<ParsePath> { | ||
Ok(match_nodes!(input.into_children(); | ||
[root(_), clause(c).., EOI(_)] => ParsePath::from_iter(c), | ||
)) | ||
} | ||
} | ||
|
||
// Parse the path | ||
#[allow(dead_code)] | ||
pub fn parse_path(input_str: &str) -> Result<ParsePath, Box<Error<Rule>>> { | ||
let entries = PathParser::parse(Rule::path, input_str)?; | ||
let entry = entries.single()?; | ||
|
||
PathParser::path(entry).map_err(Box::new) | ||
} | ||
|
||
#[cfg(test)] | ||
#[test] | ||
fn root() { | ||
let path = parse_path(".").unwrap(); | ||
dbg!(path.get_path()); | ||
assert_eq!(path.get_path(), Vec::new()) | ||
} | ||
|
||
#[test] | ||
fn body() { | ||
let path = parse_path(".-b").unwrap(); | ||
dbg!(path.get_path()); | ||
assert_eq!(path.get_path(), vec![ParseNodes::Body]) | ||
} | ||
|
||
#[test] | ||
fn branch() { | ||
let path = parse_path(".-f").unwrap(); | ||
dbg!(path.get_path()); | ||
assert_eq!(path.get_path(), vec![ParseNodes::If(false)]) | ||
} | ||
|
||
#[test] | ||
fn offset() { | ||
let path = parse_path(".-0-1").unwrap(); | ||
dbg!(path.get_path()); | ||
assert_eq!( | ||
path.get_path(), | ||
vec![ParseNodes::Offset(0), ParseNodes::Offset(1)] | ||
) | ||
} | ||
|
||
#[test] | ||
fn multiple() { | ||
let path = parse_path(".-0-1-b-t").unwrap(); | ||
dbg!(path.get_path()); | ||
assert_eq!( | ||
path.get_path(), | ||
vec![ | ||
ParseNodes::Offset(0), | ||
ParseNodes::Offset(1), | ||
ParseNodes::Body, | ||
ParseNodes::If(true) | ||
] | ||
) | ||
} |