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

ABNF review pass #155

Merged
merged 3 commits into from
Jan 5, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 0 additions & 127 deletions src/augmented.rs

This file was deleted.

4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![doc = include_str!("../README.md")]

mod append_vec;
#[cfg(feature = "ABNF")]
mod augmented;
mod earley;
mod error;
mod expression;
Expand All @@ -18,7 +16,7 @@ pub use crate::production::Production;
pub use crate::term::Term;

#[cfg(feature = "ABNF")]
pub use augmented::ABNF;
pub use parsers::ABNF;
Copy link
Owner

Choose a reason for hiding this comment

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

fiddly thing, surprised clippy didn't spot but can ABNF be added to the use below with Format, BNF?

Copy link
Owner

Choose a reason for hiding this comment

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

ah no because it's under a feature flag ✨

pub use parsers::{Format, BNF};

pub(crate) use hashbrown::HashMap;
105 changes: 105 additions & 0 deletions src/parsers/augmented.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::parsers::Format;
use crate::term::Term;

use nom::{
bytes::complete::{tag, take, take_till},
character::complete::{self, satisfy},
combinator::{complete, not},
error::VerboseError,
sequence::{preceded, terminated},
IResult,
};

#[non_exhaustive]
pub struct ABNF;

impl Format for ABNF {
fn prod_lhs(input: &str) -> IResult<&str, Term, VerboseError<&str>> {
let (input, nt) = take_till(char::is_whitespace)(input)?;

let (input, _) = preceded(complete::multispace0, complete::char('='))(input)?;

Ok((input, Term::Nonterminal(nt.to_string())))
}

fn nonterminal(input: &str) -> IResult<&str, Term, VerboseError<&str>> {
satisfy(|c: char| c.is_alphanumeric() || c == '_')(input)?;
let (input, nt) = complete(terminated(
take_till(char::is_whitespace),
complete::multispace0,
))(input)?;
take(1_usize)(nt)?;

not(complete(tag("=")))(input)?;

Ok((input, Term::Nonterminal(nt.to_string())))
}
}

#[cfg(test)]
mod tests {
use super::ABNF;
use crate::parsers::*;

use crate::expression::Expression;
use crate::grammar::Grammar;
use crate::production::Production;
use crate::term::Term;

#[test]
fn nonterminal_match() {
let input = "nonterminal-pattern";
let expected = Term::Nonterminal("nonterminal-pattern".to_string());

let (_, actual) = ABNF::nonterminal(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn expression_match() {
let input = r#"nonterminal-pattern "terminal-pattern""#;
let expected = Expression::from_parts(vec![
Term::Nonterminal("nonterminal-pattern".to_string()),
Term::Terminal("terminal-pattern".to_string()),
]);

let (_, actual) = expression::<ABNF>(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn production_match() {
let input = r#"nonterminal-pattern = nonterminal-pattern "terminal-pattern" | "terminal-pattern";\r\n"#;
let expected = Production::from_parts(
Term::Nonterminal("nonterminal-pattern".to_string()),
vec![
Expression::from_parts(vec![
Term::Nonterminal("nonterminal-pattern".to_string()),
Term::Terminal("terminal-pattern".to_string()),
]),
Expression::from_parts(vec![Term::Terminal("terminal-pattern".to_string())]),
],
);

let (_, actual) = production::<ABNF>(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn grammar_match() {
let input = r#"nonterminal-pattern = nonterminal-pattern "terminal-pattern" | "terminal-pattern";\r\n"#;
let expected = Grammar::from_parts(vec![Production::from_parts(
Term::Nonterminal("nonterminal-pattern".to_string()),
vec![
Expression::from_parts(vec![
Term::Nonterminal("nonterminal-pattern".to_string()),
Term::Terminal("terminal-pattern".to_string()),
]),
Expression::from_parts(vec![Term::Terminal("terminal-pattern".to_string())]),
],
)]);

let (_, actual) = grammar::<ABNF>(input).unwrap();
assert_eq!(expected, actual);
}
}
101 changes: 101 additions & 0 deletions src/parsers/bnf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use super::Format;

use crate::term::Term;

use nom::{
bytes::complete::{tag, take_until},
character::complete,
combinator::{complete, not},
error::VerboseError,
sequence::{delimited, preceded, terminated},
IResult,
};

#[non_exhaustive]
pub struct BNF;

impl Format for BNF {
fn prod_lhs(input: &str) -> IResult<&str, Term, VerboseError<&str>> {
let (input, nt) =
delimited(complete::char('<'), take_until(">"), complete::char('>'))(input)?;

let (input, _) = preceded(complete::multispace0, tag("::="))(input)?;

Ok((input, Term::Nonterminal(nt.to_string())))
}

fn nonterminal(input: &str) -> IResult<&str, Term, VerboseError<&str>> {
let (input, nt) = complete(delimited(
complete::char('<'),
take_until(">"),
terminated(complete::char('>'), complete::multispace0),
))(input)?;

not(complete(tag("::=")))(input)?;

Ok((input, Term::Nonterminal(nt.to_string())))
}
}

#[cfg(test)]
mod tests {
use super::BNF;
use crate::parsers::*;

#[test]
fn nonterminal_match() {
let input = "<nonterminal-pattern>";
let expected = Term::Nonterminal("nonterminal-pattern".to_string());

let (_, actual) = BNF::nonterminal(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn expression_match() {
let input = r#"<nonterminal-pattern> "terminal-pattern""#;
let expected = Expression::from_parts(vec![
Term::Nonterminal("nonterminal-pattern".to_string()),
Term::Terminal("terminal-pattern".to_string()),
]);

let (_, actual) = expression::<BNF>(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn production_match() {
let input = r#"<nonterminal-pattern> ::= <nonterminal-pattern> "terminal-pattern" | "terminal-pattern";\r\n"#;
let expected = Production::from_parts(
Term::Nonterminal("nonterminal-pattern".to_string()),
vec![
Expression::from_parts(vec![
Term::Nonterminal("nonterminal-pattern".to_string()),
Term::Terminal("terminal-pattern".to_string()),
]),
Expression::from_parts(vec![Term::Terminal("terminal-pattern".to_string())]),
],
);

let (_, actual) = production::<BNF>(input).unwrap();
assert_eq!(expected, actual);
}

#[test]
fn grammar_match() {
let input = r#"<nonterminal-pattern> ::= <nonterminal-pattern> "terminal-pattern" | "terminal-pattern";\r\n"#;
let expected = Grammar::from_parts(vec![Production::from_parts(
Term::Nonterminal("nonterminal-pattern".to_string()),
vec![
Expression::from_parts(vec![
Term::Nonterminal("nonterminal-pattern".to_string()),
Term::Terminal("terminal-pattern".to_string()),
]),
Expression::from_parts(vec![Term::Terminal("terminal-pattern".to_string())]),
],
)]);

let (_, actual) = grammar::<BNF>(input).unwrap();
assert_eq!(expected, actual);
}
}
Loading
Loading