Skip to content

Commit

Permalink
Merge pull request #453 from ltratt/prec_defines_a_token
Browse files Browse the repository at this point in the history
Allow `%prec` to define a token in the grammar.
  • Loading branch information
ratmice authored May 15, 2024
2 parents b4d2b98 + 2311084 commit 1cbbd2a
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,12 +677,11 @@ impl YaccParser {
syms.push(Symbol::Token(sym, span));
} else if let Some(j) = self.lookahead_is("%prec", i) {
i = self.parse_ws(j, true)?;
let (k, sym, _) = self.parse_token(i)?;
if self.ast.tokens.contains(&sym) {
prec = Some(sym);
} else {
return Err(self.mk_error(YaccGrammarErrorKind::PrecNotFollowedByToken, i));
let (k, sym, span) = self.parse_token(i)?;
if self.ast.tokens.insert(sym.clone()) {
self.ast.spans.push(span);
}
prec = Some(sym);
i = k;
} else if self.lookahead_is("{", i).is_some() {
let (j, a) = self.parse_action(i)?;
Expand Down Expand Up @@ -1736,17 +1735,6 @@ x"
src,
)
.expect_error_at_line(src, YaccGrammarErrorKind::IllegalString, 3);

let src = "
%%
S: 'A' %prec B;
B: ;
";
parse(
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
src,
)
.expect_error_at_line(src, YaccGrammarErrorKind::PrecNotFollowedByToken, 3);
}

#[test]
Expand Down

0 comments on commit 1cbbd2a

Please sign in to comment.