Open osa1 opened 2 years ago
We also generate a function that maps tokens to terminal indices:
fn token_terminal_idx(token: &Token) -> usize {
match token {
Token::LParen => 0usize,
Token::RParen => 1usize,
Token::Backslash => 2usize,
Token::Dot => 3usize,
Token::Colon => 4usize,
Token::Arrow => 5usize,
Token::Top => 6usize,
Token::Bot => 7usize,
Token::Id(_) => 8usize,
}
}
These indices are then used to index the action table, and the table that maps tokens to their strings used in error messages.
If I have this token type:
and don't use the
Var
token in my parser:I get a compile error in generated code, with a terrible "help":
The problem is in the
token_value
function that we generate to extract values (fields) of tokens and convert them toSemanticActionRersult
type: (which should probably be called "value", as it defines the values for the value stack)Here unused tokens are not matched, causing the non-exhaustive pattern match error.
I think we should be able to just add a
_ => unreachable!()
at the end here. We should also probably add#[inline(always)]
as in the call sites this function will be called after the next token matches a pattern with the token, so the constructor is always known, and the compiler should be able to easily eliminate thismatch
expression using the known shape (constructor) of the token.