Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
CrockAgile committed Feb 4, 2023
1 parent c23801e commit a734d6c
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 88 deletions.
24 changes: 12 additions & 12 deletions benches/bnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ fn init_tracing() -> impl Drop {
fn init_tracing() {}

fn examples(c: &mut Criterion) {
let _tracing = init_tracing();
init_tracing();

#[cfg(feature = "tracing")]
let _span = tracing::span!(tracing::Level::DEBUG, "BENCH ITER").entered();
let _span = tracing::span!(tracing::Level::DEBUG, "BENCH EXAMPLES").entered();

// c.bench_function("parse postal", |b| {
// let input = std::include_str!("../tests/fixtures/postal_address.terminated.input.bnf");
// b.iter(|| input.parse::<Grammar>().unwrap());
// });
c.bench_function("parse postal", |b| {
let input = std::include_str!("../tests/fixtures/postal_address.terminated.input.bnf");
b.iter(|| input.parse::<Grammar>().unwrap());
});

// c.bench_function("generate DNA", |b| {
// let input = "<dna> ::= <base> | <base> <dna>
// <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
// let grammar: Grammar = input.parse().unwrap();
// b.iter(|| grammar.generate().unwrap());
// });
c.bench_function("generate DNA", |b| {
let input = "<dna> ::= <base> | <base> <dna>
<base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
let grammar: Grammar = input.parse().unwrap();
b.iter(|| grammar.generate().unwrap());
});

let polish_calc_grammar: Grammar = "<product> ::= <number> | <op> <product> <product>
<op> ::= \"+\" | \"-\" | \"*\" | \"/\"
Expand Down
6 changes: 3 additions & 3 deletions src/earley/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TraversalQueue {
input: &InputRange<'gram>,
) {
for starting_prod in grammar.get_productions_by_lhs(starting_term) {
let traversal = traversal_tree.predict_starting(starting_prod, &input);
let traversal = traversal_tree.predict_starting(starting_prod, input);
self.push_back(traversal.id);
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'gram> Iterator for ParseTreeIter<'gram> {

earley(queue, traversal_tree, completions, grammar).map(|traversal_id| {
let _span = tracing::span!(tracing::Level::DEBUG, "next_parse_tree").entered();
let parse_tree = parse_tree(&traversal_tree, &grammar, traversal_id);
let parse_tree = parse_tree(traversal_tree, grammar, traversal_id);
tracing::event!(tracing::Level::TRACE, "\n{parse_tree}");
parse_tree
})
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'gram> CompletionMap<'gram> {
input_range: &InputRange<'gram>,
) -> impl Iterator<Item = TraversalId> + '_ {
let _span = tracing::span!(tracing::Level::DEBUG, "get_complete").entered();
let key = CompletionKey::new_total(term, &input_range);
let key = CompletionKey::new_total(term, input_range);
self.complete.get(&key).into_iter().flatten().cloned()
}
pub fn insert(&mut self, traversal: &Traversal<'gram>, lhs: &'gram Term) {
Expand Down
4 changes: 3 additions & 1 deletion src/earley/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ impl<'gram, 'tree> Iterator for TraversalMatchIter<'gram, 'tree> {

/// A tree of [Traversal], with [TermMatch] used for edges.
/// Earley "predictions" start a tree root, and each scan/complete creates child nodes. e.g.
/// ```txt
/// <start> ::= • <a> (this is the root of the prediction tree)
/// ├── <start> ::= <a=1> • (this is a child traversal created by matching <a>)
/// └── <start> ::= <a=2> • (this is a different child traversal created by a different match with <a>)
/// ```
#[derive(Debug, Default)]
pub(crate) struct TraversalTree<'gram> {
arena: TraversalArena<'gram>,
Expand Down Expand Up @@ -264,7 +266,7 @@ mod tests {
grammar: &'a Grammar,
input: &'static str,
) -> (ParseGrammar<'a>, InputRange<'static>, TraversalTree<'a>) {
let matching = ParseGrammar::new(&grammar);
let matching = ParseGrammar::new(grammar);
let input = InputRange::new(input);
let tree = TraversalTree::default();

Expand Down
20 changes: 9 additions & 11 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl fmt::Display for Error {
match *self {
Error::ParseError(ref s)
| Error::GenerateError(ref s)
| Error::RecursionLimit(ref s) => write!(f, "{}", s),
| Error::RecursionLimit(ref s) => write!(f, "{s}"),
}
}
}
Expand All @@ -32,13 +32,13 @@ impl error::Error for Error {

impl<'a> From<VerboseError<(&'a str, VerboseErrorKind)>> for Error {
fn from(err: VerboseError<(&str, VerboseErrorKind)>) -> Self {
Error::ParseError(format!("Parsing error: {:?}", err))
Error::ParseError(format!("Parsing error: {err:?}"))
}
}

impl From<Err<VerboseError<&str>>> for Error {
fn from(err: Err<VerboseError<&str>>) -> Self {
Error::ParseError(format!("Parsing error: {:?}", err))
Error::ParseError(format!("Parsing error: {err:?}"))
}
}

Expand Down Expand Up @@ -76,13 +76,12 @@ mod tests {

assert!(
bnf_error.is_err(),
"production result should be error {:?}",
bnf_error
"production result should be error {bnf_error:?}"
);

match bnf_error.unwrap_err() {
Error::ParseError(_) => (),
e => panic!("production error should be error parsing: {:?}", e),
e => panic!("production error should be error parsing: {e:?}"),
}
}

Expand All @@ -98,12 +97,11 @@ mod tests {

assert!(
bnf_error.is_err(),
"production result should be error {:?}",
bnf_error
"production result should be error {bnf_error:?}"
);
match bnf_error.unwrap_err() {
Error::ParseError(_) => (),
e => panic!("production error should be parse error: {:?}", e),
e => panic!("production error should be parse error: {e:?}"),
}
}

Expand All @@ -112,7 +110,7 @@ mod tests {
let bnf_error = Error::RecursionLimit(String::from("reucrsion limit reached!"));
match bnf_error {
Error::RecursionLimit(_) => (),
e => panic!("should match on reursion limit: {:?}", e),
e => panic!("should match on reursion limit: {e:?}"),
}
}

Expand All @@ -121,7 +119,7 @@ mod tests {
let bnf_error = Error::GenerateError(String::from("error generating!"));
match bnf_error {
Error::GenerateError(_) => (),
e => panic!("should match on generate error: {:?}", e),
e => panic!("should match on generate error: {e:?}"),
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl fmt::Display for Expression {
.collect::<Vec<_>>()
.join(" ");

write!(f, "{}", display)
write!(f, "{display}")
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ mod tests {
let from_str: Result<Expression, _> = to_string.parse();
match from_str {
Ok(from_expr) => TestResult::from_bool(from_expr == expr),
_ => TestResult::error(format!("{} to string and back should be safe", expr)),
_ => TestResult::error(format!("{expr} to string and back should be safe")),
}
}

Expand Down Expand Up @@ -201,7 +201,7 @@ mod tests {

// check all terms are there
for term in dna_expression.terms_iter() {
assert!(terms.contains(term), "{} was not in terms", term);
assert!(terms.contains(term), "{term} was not in terms");
}
}

Expand Down Expand Up @@ -274,25 +274,25 @@ mod tests {
#[test]
fn parse_error() {
let expression = Expression::from_str("<base> <dna");
assert!(expression.is_err(), "{:?} should be error", expression);
assert!(expression.is_err(), "{expression:?} should be error");

let error = expression.unwrap_err();
match error {
Error::ParseError(_) => (),
_ => panic!("{} should be should be error", error),
_ => panic!("{error} should be should be error"),
}
}

#[test]
fn parse_incomplete() {
let result = Expression::from_str("");
assert!(result.is_err(), "{:?} should be err", result);
assert!(result.is_err(), "{result:?} should be err");
match result {
Err(e) => match e {
Error::ParseError(_) => (),
e => panic!("should should be Error::ParseError: {:?}", e),
e => panic!("should should be Error::ParseError: {e:?}"),
},
Ok(s) => panic!("should should be Error::ParseError: {}", s),
Ok(s) => panic!("should should be Error::ParseError: {s}"),
}
}

Expand Down
46 changes: 22 additions & 24 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'gram> ParseTree<'gram> {

for matched in &self.rhs {
match matched {
ParseTreeNode::Terminal(terminal) => write!(f, " \"{}\"", terminal)?,
ParseTreeNode::Terminal(terminal) => write!(f, " \"{terminal}\"")?,
ParseTreeNode::Nonterminal(parse_tree) => write!(f, " {}", parse_tree.lhs)?,
}
}
Expand All @@ -72,7 +72,7 @@ impl<'gram> ParseTree<'gram> {
match child {
ParseTreeNode::Terminal(terminal) => {
Self::fmt_node_prefix(f, depth_format_set, child_depth, is_last_child)?;
writeln!(f, "\"{}\"", terminal)?;
writeln!(f, "\"{terminal}\"")?;
}
ParseTreeNode::Nonterminal(nonterminal) => {
nonterminal.fmt(f, depth_format_set, child_depth, is_last_child)?;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'gram> ParseTree<'gram> {
} else {
LAST_GRANDCHILD_PREFIX
};
write!(f, "{}", prefix)?;
write!(f, "{prefix}")?;
}

Ok(())
Expand Down Expand Up @@ -286,8 +286,7 @@ impl Grammar {
if let Some(remaining) = stacker::remaining_stack() {
if remaining < STACK_RED_ZONE {
return Err(Error::RecursionLimit(format!(
"Limit for recursion reached processing <{}>!",
ident
"Limit for recursion reached processing <{ident}>!",
)));
}
}
Expand Down Expand Up @@ -376,8 +375,7 @@ impl Grammar {
Term::Nonterminal(ref nt) => start_rule = nt.clone(),
Term::Terminal(_) => {
return Err(Error::GenerateError(format!(
"Termainal type cannot define a production in '{}'!",
term
"Termainal type cannot define a production in '{term}'!"
)));
}
},
Expand Down Expand Up @@ -481,7 +479,7 @@ mod tests {
let from_str: Result<Grammar, _> = to_string.parse();
match from_str {
Ok(from_prod) => TestResult::from_bool(from_prod == gram),
_ => TestResult::error(format!("{} to string and back should be safe", gram)),
_ => TestResult::error(format!("{gram} to string and back should be safe")),
}
}

Expand Down Expand Up @@ -607,51 +605,51 @@ mod tests {
#[test]
fn parse_error() {
let grammar: Result<Grammar, _> = "<almost_grammar> ::= <test".parse();
assert!(grammar.is_err(), "{:?} should be error", grammar);
assert!(grammar.is_err(), "{grammar:?} should be error");
}

#[test]
fn parse_error_on_incomplete() {
let result: Result<Grammar, _> = "".parse();
assert!(result.is_err(), "{:?} should be err", result);
assert!(result.is_err(), "{result:?} should be err");
match result {
Err(e) => match e {
Error::ParseError(_) => (),
e => panic!("should should be Error::ParseError: {:?}", e),
e => panic!("should should be Error::ParseError: {e:?}"),
},
Ok(s) => panic!("should should be Error::ParseError: {}", s),
Ok(s) => panic!("should should be Error::ParseError: {s}"),
}
}

#[cfg(feature = "stacker")]
#[test]
fn recursion_limit() {
let grammar: Result<Grammar, _> = "<nonterm> ::= <nonterm>".parse();
assert!(grammar.is_ok(), "{:?} should be ok", grammar);
assert!(grammar.is_ok(), "{grammar:?} should be ok");
let sentence = grammar.unwrap().generate();
assert!(sentence.is_err(), "{:?} should be err", sentence);
assert!(sentence.is_err(), "{sentence:?} should be err");
match sentence {
Err(e) => match e {
Error::RecursionLimit(_) => (),
e => panic!("should should be Error::RecursionLimit: {:?}", e),
e => panic!("should should be Error::RecursionLimit: {e:?}"),
},
Ok(s) => panic!("should should be Error::RecursionLimit: {}", s),
Ok(s) => panic!("should should be Error::RecursionLimit: {s}"),
}
}

#[test]
fn lhs_not_found() {
let grammar: Result<Grammar, _> = "<start> ::= <not-used>".parse();
assert!(grammar.is_ok(), "{:?} should be ok", grammar);
assert!(grammar.is_ok(), "{grammar:?} should be ok");
let sentence = grammar.unwrap().generate();
assert!(sentence.is_ok(), "{:?} should be ok", sentence);
assert!(sentence.is_ok(), "{sentence:?} should be ok");
assert_eq!(sentence.unwrap(), String::from("<not-used>"));
}

#[test]
fn lhs_is_terminal_parse() {
let grammar: Result<Grammar, _> = "\"wrong place\" ::= <not-used>".parse();
assert!(grammar.is_err(), "{:?} should be error", grammar);
assert!(grammar.is_err(), "{grammar:?} should be error");
}

#[test]
Expand All @@ -662,14 +660,14 @@ mod tests {
let production = Production::from_parts(lhs, vec![expression]);
let grammar = Grammar::from_parts(vec![production]);
let sentence = grammar.generate();
assert!(sentence.is_err(), "{:?} should be error", sentence);
assert!(sentence.is_err(), "{sentence:?} should be error");
}

#[test]
fn no_productions() {
let grammar = Grammar::from_parts(vec![]);
let sentence = grammar.generate();
assert!(sentence.is_err(), "{:?} should be error", sentence);
assert!(sentence.is_err(), "{sentence:?} should be error");
}

#[test]
Expand All @@ -678,7 +676,7 @@ mod tests {
let production = Production::from_parts(lhs, vec![]);
let grammar = Grammar::from_parts(vec![production]);
let sentence = grammar.generate();
assert!(sentence.is_err(), "{:?} should be error", sentence);
assert!(sentence.is_err(), "{sentence:?} should be error");
}

#[test]
Expand Down Expand Up @@ -855,7 +853,7 @@ mod tests {

let input = "GATTACA";
let parsed = grammar.parse_input(input).next().unwrap();
let formatted = format!("{}", parsed);
let formatted = format!("{parsed}");
let expected = "
<dna> ::= <base> <dna>
├── <base> ::= \"G\"
Expand Down Expand Up @@ -889,7 +887,7 @@ mod tests {
<base> ::= \"A\" | \"C\" | \"G\" | \"T\""
.parse()
.unwrap();
let format = format!("{}", grammar);
let format = format!("{grammar}");
assert_eq!(
format,
"<dna> ::= <base> | <base> <dna>\n<base> ::= \"A\" | \"C\" | \"G\" | \"T\"\n"
Expand Down
Loading

0 comments on commit a734d6c

Please sign in to comment.