Skip to content

Commit

Permalink
Allow %prec to define a token in the grammar.
Browse files Browse the repository at this point in the history
Previously if `%prec 'x'` was the first mention of the token `x` then
cfgrammar would say "'x' isn't a token" which is clearly incorrect.
This commit is maybe best thought of as a quick fix of sorts: it makes
`%prec 'x'` define a token `x` if it doesn't already exist.

However -- and my memory is completely fuzzy as to whether this is a
Yacc compatibility thing or not -- this means that `%prec 'x'` and
`%prec x' (i.e. with and without quote marks) have the semantic meaning
because `parse_token` treats both interchangeably. This seems consistent
with other parts of the grammar even if a little odd.
  • Loading branch information
ltratt committed May 15, 2024
1 parent 86f0aca commit 2311084
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 2311084

Please sign in to comment.