Skip to content

Commit

Permalink
Merge pull request #460 from ltratt/check_nonterminated_productions
Browse files Browse the repository at this point in the history
Catch incorrectly terminated productions.
  • Loading branch information
vext01 authored Jun 6, 2024
2 parents ff41257 + ec9ad7a commit e30e481
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum YaccGrammarErrorKind {
MismatchedBrace,
NonEmptyProduction,
PrematureEnd,
ProductionNotTerminated,
ProgramsNotSupported,
UnknownDeclaration,
PrecNotFollowedByToken,
Expand Down Expand Up @@ -91,6 +92,7 @@ impl fmt::Display for YaccGrammarErrorKind {
YaccGrammarErrorKind::MismatchedBrace => "Mismatched brace",
YaccGrammarErrorKind::NonEmptyProduction => "%empty used in non-empty production",
YaccGrammarErrorKind::PrematureEnd => "File ends prematurely",
YaccGrammarErrorKind::ProductionNotTerminated => "Production not terminated correctly",
YaccGrammarErrorKind::ProgramsNotSupported => "Programs not currently supported",
YaccGrammarErrorKind::UnknownDeclaration => "Unknown declaration",
YaccGrammarErrorKind::DuplicatePrecedence => "Token has multiple precedences specified",
Expand Down Expand Up @@ -231,6 +233,7 @@ impl Spanned for YaccGrammarError {
| YaccGrammarErrorKind::MismatchedBrace
| YaccGrammarErrorKind::NonEmptyProduction
| YaccGrammarErrorKind::PrematureEnd
| YaccGrammarErrorKind::ProductionNotTerminated
| YaccGrammarErrorKind::PrecNotFollowedByToken
| YaccGrammarErrorKind::ProgramsNotSupported
| YaccGrammarErrorKind::UnknownDeclaration
Expand Down Expand Up @@ -685,8 +688,14 @@ impl YaccParser {
i = k;
} else if self.lookahead_is("{", i).is_some() {
let (j, a) = self.parse_action(i)?;
i = j;
i = self.parse_ws(j, true)?;
action = Some(a);

if syms.is_empty()
|| !(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)?;
// %empty could be followed by all sorts of weird syntax errors: all we try and do
Expand Down Expand Up @@ -2606,4 +2615,19 @@ B";
16,
);
}

#[test]
fn test_action_successor() {
let src = "
%%
A: B {} B;
B: ;
";
parse(YaccKind::Original(YaccOriginalActionKind::NoAction), src).expect_error_at_line_col(
src,
YaccGrammarErrorKind::ProductionNotTerminated,
3,
17,
);
}
}

0 comments on commit e30e481

Please sign in to comment.