Skip to content

Commit

Permalink
Reformat with cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmehall committed Jul 22, 2020
1 parent fbec4fc commit cd43387
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 267 deletions.
10 changes: 5 additions & 5 deletions benches/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ rule atom() = ['0'..='9']+ / "(" expr() ")"

#[bench]
fn expr(b: &mut Bencher) {
let bench_str = "1+2+3+4*5*6^7^8^(0^1*2+1)";
let bench_str = "1+2+3+4*5*6^7^8^(0^1*2+1)";

b.bytes = bench_str.len() as u64;
b.iter(|| {
parser::expr(bench_str).unwrap();
});
b.bytes = bench_str.len() as u64;
b.iter(|| {
parser::expr(bench_str).unwrap();
});
}
10 changes: 5 additions & 5 deletions benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ rule string()

#[bench]
fn json(b: &mut Bencher) {
let bench_str = r#"
let bench_str = r#"
{
"X": 0.6e2,
"Y": 5,
Expand All @@ -69,8 +69,8 @@ fn json(b: &mut Bencher) {
}
"#;

b.bytes = bench_str.len() as u64;
b.iter(|| {
parser::json(bench_str).unwrap();
});
b.bytes = bench_str.len() as u64;
b.iter(|| {
parser::json(bench_str).unwrap();
});
}
57 changes: 33 additions & 24 deletions peg-macros/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use proc_macro2::Span;
use std::collections::HashMap;

use crate::ast::*;

Expand All @@ -14,10 +14,13 @@ pub fn check<'a>(grammar: &'a Grammar) -> GrammarAnalysis<'a> {
for rule in grammar.iter_rules() {
rules.entry(rule.name.to_string()).or_insert(rule);
}

let left_recursion = RecursionVisitor::check(grammar, &rules);

GrammarAnalysis { rules, left_recursion }
let left_recursion = RecursionVisitor::check(grammar, &rules);

GrammarAnalysis {
rules,
left_recursion,
}
}

struct RecursionVisitor<'a> {
Expand All @@ -33,7 +36,10 @@ pub struct RecursionError {

impl RecursionError {
pub fn msg(&self) -> String {
format!("left recursive rules create an infinite loop: {}", self.path.join(" -> "))
format!(
"left recursive rules create an infinite loop: {}",
self.path.join(" -> ")
)
}
}

Expand All @@ -45,7 +51,6 @@ struct RuleInfo {
nullable: bool,
}


impl<'a> RecursionVisitor<'a> {
fn check(grammar: &'a Grammar, rules: &HashMap<String, &'a Rule>) -> Vec<RecursionError> {
let mut visitor = RecursionVisitor {
Expand Down Expand Up @@ -75,10 +80,17 @@ impl<'a> RecursionVisitor<'a> {
RuleExpr(ref rule_ident, _) => {
let name = rule_ident.to_string();

if let Some(loop_start) = self.stack.iter().position(|caller_name| { caller_name == &name}) {
if let Some(loop_start) = self
.stack
.iter()
.position(|caller_name| caller_name == &name)
{
let mut recursive_loop = self.stack[loop_start..].to_vec();
recursive_loop.push(name);
self.errors.push(RecursionError { path: recursive_loop, span: rule_ident.span()});
self.errors.push(RecursionError {
path: recursive_loop,
span: rule_ident.span(),
});
return RuleInfo { nullable: false };
}

Expand All @@ -92,10 +104,10 @@ impl<'a> RecursionVisitor<'a> {
ActionExpr(ref elems, ..) => {
for elem in elems {
if !self.walk_expr(&elem.expr).nullable {
return RuleInfo { nullable: false }
return RuleInfo { nullable: false };
}
}

RuleInfo { nullable: true }
}
ChoiceExpr(ref choices) => {
Expand All @@ -108,34 +120,31 @@ impl<'a> RecursionVisitor<'a> {
RuleInfo { nullable }
}

OptionalExpr(ref expr) |
PosAssertExpr(ref expr) |
NegAssertExpr(ref expr) => {
OptionalExpr(ref expr) | PosAssertExpr(ref expr) | NegAssertExpr(ref expr) => {
self.walk_expr(expr);
RuleInfo { nullable: true }
}

Repeat(ref expr, ref bounds, _) => {
Repeat(ref expr, ref bounds, _) => {
let nullable = match bounds {
BoundedRepeat::None => true,
_ => false,
};

let res = self.walk_expr(expr);
RuleInfo { nullable: res.nullable | nullable }
RuleInfo {
nullable: res.nullable | nullable,
}
}

MatchStrExpr(ref expr) |
QuietExpr(ref expr) => self.walk_expr(expr),
MatchStrExpr(ref expr) | QuietExpr(ref expr) => self.walk_expr(expr),

PrecedenceExpr{ .. } => { RuleInfo { nullable: false } },
PrecedenceExpr { .. } => RuleInfo { nullable: false },

| LiteralExpr(_)
| PatternExpr(_)
| MethodExpr(_, _)
| FailExpr(_)
| MarkerExpr(_) => { RuleInfo { nullable: false } }
PositionExpr => { RuleInfo { nullable: true} }
LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => {
RuleInfo { nullable: false }
}
PositionExpr => RuleInfo { nullable: true },
}
}
}
24 changes: 14 additions & 10 deletions peg-macros/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::{ TokenStream, Ident, Literal };
use proc_macro2::{Ident, Literal, TokenStream};

#[derive(Debug)]
pub struct Grammar {
Expand All @@ -11,12 +11,10 @@ pub struct Grammar {
}

impl Grammar {
pub fn iter_rules(&self) -> impl Iterator<Item=&Rule> {
self.items.iter().filter_map(|item| {
match item {
Item::Rule(r) => Some(r),
_ => None
}
pub fn iter_rules(&self) -> impl Iterator<Item = &Rule> {
self.items.iter().filter_map(|item| match item {
Item::Rule(r) => Some(r),
_ => None,
})
}
}
Expand Down Expand Up @@ -68,12 +66,18 @@ pub enum Expr {
Repeat(Box<Expr>, BoundedRepeat, /*sep*/ Option<Box<Expr>>),
PosAssertExpr(Box<Expr>),
NegAssertExpr(Box<Expr>),
ActionExpr(Vec<TaggedExpr>, /*action*/ Option<TokenStream>, /*cond*/ bool),
ActionExpr(
Vec<TaggedExpr>,
/*action*/ Option<TokenStream>,
/*cond*/ bool,
),
MatchStrExpr(Box<Expr>),
PositionExpr,
QuietExpr(Box<Expr>),
FailExpr(Literal),
PrecedenceExpr{ levels: Vec<PrecedenceLevel> },
PrecedenceExpr {
levels: Vec<PrecedenceLevel>,
},
MarkerExpr(bool),
}

Expand All @@ -100,4 +104,4 @@ pub enum BoundedRepeat {
Plus,
Exact(TokenStream),
Both(Option<TokenStream>, Option<TokenStream>),
}
}
57 changes: 30 additions & 27 deletions peg-macros/bin.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
//! Standalone version of rust-peg used for bootstrapping the meta-grammar

extern crate quote;
extern crate proc_macro2;
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;

use std::env;
use std::fs::File;
use std::io::{stderr, stdin, stdout};
use std::io::{Read, Write};
use std::io::{stdin, stdout, stderr};
use std::path::Path;
use std::process;

// This can't use the `peg` crate as it would be a circular dependency, but the generated code in grammar.rs
// requires `::peg` paths.
extern crate peg_runtime as peg;

mod analysis;
mod ast;
mod tokens;
mod grammar;
mod tokens;
mod translate;
mod analysis;

fn main() {
let args = env::args_os().collect::<Vec<_>>();
let progname = &args[0];
let mut log = stderr();

let mut source = String::new();

if args.len() == 2 && &args[1] != "-h" {
File::open(Path::new(&args[1])).unwrap().read_to_string(&mut source).unwrap();
} else if args.len() == 1 {
stdin().read_to_string(&mut source).unwrap();
} else {
writeln!(log, "Usage: {} [file]", progname.to_string_lossy()).unwrap();
process::exit(0);
}

let source_tokens = source.parse().expect("Error tokenizing input");
let input_tokens = tokens::FlatTokenStream::new(source_tokens);
let args = env::args_os().collect::<Vec<_>>();
let progname = &args[0];
let mut log = stderr();

let mut source = String::new();

if args.len() == 2 && &args[1] != "-h" {
File::open(Path::new(&args[1]))
.unwrap()
.read_to_string(&mut source)
.unwrap();
} else if args.len() == 1 {
stdin().read_to_string(&mut source).unwrap();
} else {
writeln!(log, "Usage: {} [file]", progname.to_string_lossy()).unwrap();
process::exit(0);
}

let source_tokens = source.parse().expect("Error tokenizing input");
let input_tokens = tokens::FlatTokenStream::new(source_tokens);
let grammar = match grammar::peg::peg_grammar(&input_tokens) {
Ok(g) => g,
Err(err) => {
eprintln!("Failed to parse grammar: expected {}", err.expected);
process::exit(1);
process::exit(1);
}
};
let parser_tokens = translate::compile_grammar(&grammar);
let mut out = stdout();
writeln!(&mut out, "// Generated by rust-peg. Do not edit.").unwrap();
write!(&mut out, "{}", parser_tokens).unwrap();
let parser_tokens = translate::compile_grammar(&grammar);
let mut out = stdout();
writeln!(&mut out, "// Generated by rust-peg. Do not edit.").unwrap();
write!(&mut out, "{}", parser_tokens).unwrap();
}
10 changes: 5 additions & 5 deletions peg-macros/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
extern crate quote;
extern crate proc_macro2;
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;

use quote::quote_spanned;

// This can't use the `peg` crate as it would be a circular dependency, but the generated code in grammar.rs
// requires `::peg` paths.
extern crate peg_runtime as peg;

mod analysis;
mod ast;
mod tokens;
mod grammar;
mod tokens;
mod translate;
mod analysis;

/// The main macro for creating a PEG parser.
///
///
/// For the grammar syntax, see the `peg` crate documentation.
#[proc_macro]
pub fn parser(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand Down
Loading

0 comments on commit cd43387

Please sign in to comment.