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

Add options to specify cache type #338

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
62 changes: 40 additions & 22 deletions peg-macros/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ impl LeftRecursionError {
}

impl<'a> LeftRecursionVisitor<'a> {
fn check(grammar: &'a Grammar, rules: &HashMap<String, &'a Rule>) -> (HashMap<String, bool>, Vec<LeftRecursionError>) {
fn check(
grammar: &'a Grammar,
rules: &HashMap<String, &'a Rule>,
) -> (HashMap<String, bool>, Vec<LeftRecursionError>) {
let mut visitor = LeftRecursionVisitor {
rules,
errors: Vec::new(),
Expand All @@ -64,7 +67,9 @@ impl<'a> LeftRecursionVisitor<'a> {
for rule in grammar.iter_rules() {
let nullable = visitor.walk_rule(rule);
debug_assert!(visitor.stack.is_empty());
rule_nullability.entry(rule.name.to_string()).or_insert(nullable);
rule_nullability
.entry(rule.name.to_string())
.or_insert(nullable);
}

(rule_nullability, visitor.errors)
Expand Down Expand Up @@ -98,20 +103,18 @@ impl<'a> LeftRecursionVisitor<'a> {
let mut recursive_loop = self.stack[loop_start..].to_vec();
recursive_loop.push(name.clone());
match rule.cache {
None | Some(Cache::Simple) =>
self.errors.push(LeftRecursionError {
path: recursive_loop,
span: rule_ident.span(),
}),
_ => ()

None | Some(Cache::Simple(_)) => self.errors.push(LeftRecursionError {
path: recursive_loop,
span: rule_ident.span(),
}),
_ => (),
}
return false;
}
self.walk_rule(rule)
} else {
// Missing rule would have already been reported
false
false
}
}

Expand All @@ -138,7 +141,11 @@ impl<'a> LeftRecursionVisitor<'a> {
true
}

Repeat { ref inner, ref bound, .. } => {
Repeat {
ref inner,
ref bound,
..
} => {
let inner_nullable = self.walk_expr(inner);
inner_nullable | !bound.has_lower_bound()
}
Expand All @@ -161,10 +168,12 @@ impl<'a> LeftRecursionVisitor<'a> {
}
}

nullable
nullable
}

LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => false,
LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => {
false
}

PositionExpr => true,
}
Expand All @@ -188,9 +197,11 @@ impl LoopNullabilityError {
}
}


impl<'a> LoopNullabilityVisitor<'a> {
fn check(grammar: &'a Grammar, rule_nullability: &HashMap<String, bool>) -> Vec<LoopNullabilityError> {
fn check(
grammar: &'a Grammar,
rule_nullability: &HashMap<String, bool>,
) -> Vec<LoopNullabilityError> {
let mut visitor = LoopNullabilityVisitor {
rule_nullability,
errors: Vec::new(),
Expand All @@ -203,7 +214,6 @@ impl<'a> LoopNullabilityVisitor<'a> {
visitor.errors
}


/// Walk an expr and its children analyzing the nullability of loop bodies.
///
/// Returns true if the rule is known to match completely without consuming
Expand All @@ -220,7 +230,7 @@ impl<'a> LoopNullabilityVisitor<'a> {
let name = rule_ident.to_string();
*self.rule_nullability.get(&name).unwrap_or(&false)
}

ActionExpr(ref elems, ..) => {
let mut nullable = true;
for elem in elems {
Expand All @@ -242,15 +252,21 @@ impl<'a> LoopNullabilityVisitor<'a> {
true
}

Repeat { ref inner, ref bound, ref sep } => {
Repeat {
ref inner,
ref bound,
ref sep,
} => {
let inner_nullable = self.walk_expr(inner);
let sep_nullable = sep.as_ref().map_or(true, |sep| self.walk_expr(sep));

// The entire purpose of this analysis: report errors if the loop body is nullable
if inner_nullable && sep_nullable && !bound.has_upper_bound() {
self.errors.push(LoopNullabilityError { span: this_expr.span });
self.errors.push(LoopNullabilityError {
span: this_expr.span,
});
}

inner_nullable | !bound.has_lower_bound()
}

Expand All @@ -269,10 +285,12 @@ impl<'a> LoopNullabilityVisitor<'a> {
}
}

nullable
nullable
}

LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => false,
LiteralExpr(_) | PatternExpr(_) | MethodExpr(_, _) | FailExpr(_) | MarkerExpr(_) => {
false
}
PositionExpr => true,
}
}
Expand Down
21 changes: 14 additions & 7 deletions peg-macros/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub enum Item {

#[derive(Debug)]
pub enum Cache {
Simple,
Recursive
Simple(Option<TokenStream>),
Recursive(Option<TokenStream>),
}

#[derive(Debug)]
Expand Down Expand Up @@ -77,7 +77,11 @@ pub enum Expr {
MethodExpr(Ident, TokenStream),
ChoiceExpr(Vec<SpannedExpr>),
OptionalExpr(Box<SpannedExpr>),
Repeat { inner: Box<SpannedExpr>, bound: BoundedRepeat, sep: Option<Box<SpannedExpr>> },
Repeat {
inner: Box<SpannedExpr>,
bound: BoundedRepeat,
sep: Option<Box<SpannedExpr>>,
},
PosAssertExpr(Box<SpannedExpr>),
NegAssertExpr(Box<SpannedExpr>),
ActionExpr(Vec<TaggedExpr>, Option<Group>),
Expand All @@ -93,7 +97,10 @@ pub enum Expr {

impl Expr {
pub fn at(self, sp: Span) -> SpannedExpr {
SpannedExpr { expr: self, span:sp }
SpannedExpr {
expr: self,
span: sp,
}
}
}

Expand Down Expand Up @@ -127,14 +134,14 @@ impl BoundedRepeat {
pub fn has_lower_bound(&self) -> bool {
match self {
BoundedRepeat::None | BoundedRepeat::Both(None, _) => false,
BoundedRepeat::Plus | BoundedRepeat::Exact(_) | BoundedRepeat::Both(Some(_), _) => true
BoundedRepeat::Plus | BoundedRepeat::Exact(_) | BoundedRepeat::Both(Some(_), _) => true,
}
}

pub fn has_upper_bound(&self) -> bool {
match self {
BoundedRepeat::None | BoundedRepeat::Plus | BoundedRepeat::Both(_, None) => false,
BoundedRepeat::Exact(_) | BoundedRepeat::Both(_, Some(_)) => true
BoundedRepeat::None | BoundedRepeat::Plus | BoundedRepeat::Both(_, None) => false,
BoundedRepeat::Exact(_) | BoundedRepeat::Both(_, Some(_)) => true,
}
}
}
Loading