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

Parser #2353

Merged
merged 7 commits into from
Nov 29, 2024
Merged

Parser #2353

Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions interp/src/debugger/commands/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,33 @@
}
}

#[derive(Debug, PartialEq, Clone)]
EclecticGriffin marked this conversation as resolved.
Show resolved Hide resolved
pub enum ParseNodes {
Body,
Offset(u32),
If(bool),
}
pub struct ParsePath {
nodes: Vec<ParseNodes>,
}

impl ParsePath {
pub fn new(nodes: Vec<ParseNodes>) -> ParsePath {
ParsePath { nodes }
}

pub fn get_path(&self) -> Vec<ParseNodes> {
self.nodes.clone()
}
Comment on lines +322 to +324
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend making this a standard accessor which returns a reference to the vec
or a slice and cloning externally when necessary


pub fn from_iter<I>(iter: I) -> ParsePath

Check failure on line 321 in interp/src/debugger/commands/core.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

method `from_iter` can be confused for the standard trait method `std::iter::FromIterator::from_iter`

Check failure on line 321 in interp/src/debugger/commands/core.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

method `from_iter` can be confused for the standard trait method `std::iter::FromIterator::from_iter`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As clippy points out, there is a trait called FromIterator which you should move this implementation into.

See: https://doc.rust-lang.org/std/iter/trait.FromIterator.html

where
I: IntoIterator<Item = ParseNodes>,
{
ParsePath::new(iter.into_iter().collect())
}
}

// Different types of printing commands
pub enum PrintCommand {
Normal,
Expand Down
1 change: 1 addition & 0 deletions interp/src/debugger/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This module contains the structures for the debugger commands
pub(crate) mod command_parser;
pub mod core;
mod path_parser;
pub use command_parser::parse_command;
pub use core::Command;

Expand Down
13 changes: 13 additions & 0 deletions interp/src/debugger/commands/path_parser.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = { "." }

seperator = { "-" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo


body = { "b" }

num = { ASCII_DIGIT+ }

branch = {"t" | "f"}

clause = { seperator ~ (body | num | branch) }

path = { SOI ~ root ~ clause* ~ EOI }
115 changes: 115 additions & 0 deletions interp/src/debugger/commands/path_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use super::{core::ParseNodes, ParsePath};

use pest_consume::{match_nodes, Error, Parser};

type ParseResult<T> = std::result::Result<T, Error<Rule>>;

Check failure on line 5 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

type alias `ParseResult` is never used

Check failure on line 5 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

type alias `ParseResult` is never used
type Node<'i> = pest_consume::Node<'i, Rule, ()>;

Check failure on line 6 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

type alias `Node` is never used

Check failure on line 6 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

type alias `Node` is never used

// 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 seperator(_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();
[seperator(_), num(n)] => ParseNodes::Offset(n),
[seperator(_), body(_)] => ParseNodes::Body,
[seperator(_), 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
pub fn parse_path(input_str: &str) -> Result<ParsePath, Error<Rule>> {

Check failure on line 63 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `parse_path` is never used

Check failure on line 63 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

the `Err`-variant returned from this function is very large

Check failure on line 63 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `parse_path` is never used

Check failure on line 63 in interp/src/debugger/commands/path_parser.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

the `Err`-variant returned from this function is very large
let entries = PathParser::parse(Rule::path, input_str)?;
let entry = entries.single()?;

PathParser::path(entry)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add #[allow(dead_code)] here for the moment since this PR doesn't use the
function. This will quiet clippy down.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix the size issue change the return type to Box<Error<Rule>> and add
.map_err(Box::new) to the final line

#[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)
]
)
}
Loading