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

Issue 473 assertion #476

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions cfgrammar/src/lib/yacc/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct Production {
pub enum Symbol {
Rule(String, Span),
Token(String, Span),
Empty(Span),
}

/// Specifies an index into a `GrammarAst.tokens` or a `GrammarAST.rules`.
Expand Down Expand Up @@ -133,6 +134,7 @@ impl fmt::Display for Symbol {
match *self {
Symbol::Rule(ref s, _) => write!(f, "{}", s),
Symbol::Token(ref s, _) => write!(f, "{}", s),
Symbol::Empty(_) => write!(f, "%empty"),
}
}
}
Expand Down Expand Up @@ -262,6 +264,7 @@ impl GrammarAST {
});
}
}
Symbol::Empty(_) => {}
}
}
}
Expand Down Expand Up @@ -300,6 +303,7 @@ impl GrammarAST {
});
}
}
Symbol::Empty(_) => {}
}
}
Ok(())
Expand All @@ -311,6 +315,9 @@ impl GrammarAST {
let (kind, span) = match symidx.symbol(self) {
Symbol::Rule(_, span) => (YaccGrammarWarningKind::UnusedRule, span),
Symbol::Token(_, span) => (YaccGrammarWarningKind::UnusedToken, span),
Symbol::Empty(_) => {
unreachable!()
}
};
YaccGrammarWarning {
kind,
Expand Down Expand Up @@ -340,6 +347,9 @@ impl GrammarAST {
Symbol::Token(sym_name, _) => {
expected_unused_tokens.insert(sym_name);
}
Symbol::Empty(_) => {
unreachable!();
}
}
}
if let Some(implicit_tokens) = self.implicit_tokens.as_ref() {
Expand All @@ -364,6 +374,8 @@ impl GrammarAST {
Symbol::Token(name, _) => {
seen_tokens.insert(name);
}
Symbol::Empty(_) => {
}
};
}
}
Expand Down
1 change: 1 addition & 0 deletions cfgrammar/src/lib/yacc/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ where
prod.push(Symbol::Rule(rule_map[implicit_rule]));
}
}
ast::Symbol::Empty(_) => {}
};
}
let mut prec = None;
Expand Down
28 changes: 18 additions & 10 deletions cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,13 +660,19 @@ impl YaccParser {
i = self.parse_ws(i, true)?;
while i < self.src.len() {
if let Some(j) = self.lookahead_is("|", i) {
if syms.is_empty() {
syms.push(Symbol::Empty(Span::new(i, i)));
}
self.ast.add_prod(rn.clone(), syms, prec, action);
syms = Vec::new();
prec = None;
action = None;
i = self.parse_ws(j, true)?;
continue;
} else if let Some(j) = self.lookahead_is(";", i) {
if syms.is_empty() {
syms.push(Symbol::Empty(Span::new(i, i)));
}
self.ast.add_prod(rn, syms, prec, action);
return Ok(j);
}
Expand Down Expand Up @@ -694,19 +700,20 @@ impl YaccParser {
if !(self.lookahead_is("|", i).is_some() || self.lookahead_is(";", i).is_some()) {
return Err(self.mk_error(YaccGrammarErrorKind::ProductionNotTerminated, i));
}
} else if let Some(mut j) = self.lookahead_is("%empty", i) {
j = self.parse_ws(j, true)?;
} else if let Some(j) = self.lookahead_is("%empty", i) {
let k = self.parse_ws(j, true)?;
// %empty could be followed by all sorts of weird syntax errors: all we try and do
// is say "does this production look like it's finished" and trust that the other
// errors will be caught by other parts of the parser.
if !syms.is_empty()
| !(self.lookahead_is("|", j).is_some()
|| self.lookahead_is(";", j).is_some()
|| self.lookahead_is("{", j).is_some())
| !(self.lookahead_is("|", k).is_some()
|| self.lookahead_is(";", k).is_some()
|| self.lookahead_is("{", k).is_some())
{
return Err(self.mk_error(YaccGrammarErrorKind::NonEmptyProduction, i));
}
i = j;
syms.push(Symbol::Empty(Span::new(i, j)));
i = k;
} else {
let (j, sym, span) = self.parse_token(i)?;
if self.ast.tokens.contains(&sym) {
Expand Down Expand Up @@ -1238,7 +1245,7 @@ mod test {
assert_eq!(
grm.prods[grm.get_rule("A").unwrap().pidxs[0]],
Production {
symbols: vec![],
symbols: vec![Symbol::Empty(Span::new(32, 32))],
precedence: None,
action: None
}
Expand All @@ -1257,7 +1264,7 @@ mod test {
assert_eq!(
grm.prods[grm.get_rule("B").unwrap().pidxs[1]],
Production {
symbols: vec![],
symbols: vec![Symbol::Empty(Span::new(56, 56))],
precedence: None,
action: None
}
Expand All @@ -1266,7 +1273,7 @@ mod test {
assert_eq!(
grm.prods[grm.get_rule("C").unwrap().pidxs[0]],
Production {
symbols: vec![],
symbols: vec![Symbol::Empty(Span::new(74, 74))],
precedence: None,
action: None
}
Expand Down Expand Up @@ -2396,14 +2403,15 @@ Expr -> () : D;
for sym in &ast.prods[*pidx].symbols {
let name = match sym {
Symbol::Token(name, _) | Symbol::Rule(name, _) => name.clone(),
Symbol::Empty(_) => "%empty".to_string(),
};
prod_names.insert(name);
}
}
assert_eq!(ast.prods.len(), 5);
assert_eq!(
prod_names,
HashSet::from_iter(["A", "B", "C", "D"].map(|s| s.to_owned()))
HashSet::from_iter(["A", "B", "C", "D", "%empty"].map(|s| s.to_owned()))
);
}

Expand Down
2 changes: 2 additions & 0 deletions lrtable/src/lib/statetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ S: S | ;";
match sym {
ast::Symbol::Rule(_, span) => assert_eq!(span, &Span::new(6, 7)),
ast::Symbol::Token(_, _) => panic!("Incorrect symbol"),
ast::Symbol::Empty(_) => panic!("Empty symbol"),
}
}
Err(e) => panic!("Incorrect error returned {:?}", e),
Expand Down Expand Up @@ -1072,6 +1073,7 @@ T: S | ;";
match sym {
ast::Symbol::Rule(_, span) => assert_eq!(span, &Span::new(15, 16)),
ast::Symbol::Token(_, _) => panic!("Incorrect symbol"),
ast::Symbol::Empty(_) => panic!("Empty symbol"),
}
}
Err(e) => panic!("Incorrect error returned {:?}", e),
Expand Down
23 changes: 11 additions & 12 deletions nimbleparse/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ where
usize: num_traits::AsPrimitive<StorageT>,
StorageT: 'static + num_traits::PrimInt + num_traits::Unsigned,
{
if usize::from(pidx) < ast.prods.len() {
let prod = &ast.prods[usize::from(pidx)];
prod.symbols
.iter()
.map(|sym| match sym {
Symbol::Rule(name, span) => (format!("'{}'", name), span),
Symbol::Token(name, span) => (format!("'{}'", name), span),
})
.unzip()
} else {
(vec![], vec![])
}
let pidx = usize::from(pidx);
assert!(pidx < ast.prods.len());
let prod = &ast.prods[pidx];
prod.symbols
.iter()
.map(|sym| match sym {
Symbol::Rule(name, span) => (format!("'{}'", name), span),
Symbol::Token(name, span) => (format!("'{}'", name), span),
Symbol::Empty(span) => (format!("%empty"), span),
})
.unzip()
}

impl<'a> SpannedDiagnosticFormatter<'a> {
Expand Down
Loading