mirror of
https://github.com/tree-sitter/tree-sitter.git
synced 2026-09-10 07:36:22 -04:00
This commit adds an `eof()` function for grammars, which is easier to use than the NUL byte directly. It compiles down to a constraint that the enclosing production can only reduce at end of input, not to a shiftable token. BREAKING CHANGE: Public error types for `tree-sitter-generate` were modified. Co-authored-by: Will Lillis <will.lillis24@gmail.com>
31 lines
928 B
JavaScript
31 lines
928 B
JavaScript
// The `eof()` alternative of `terminator` is unreachable inside the parens
|
|
// (because `)` always has to come after) so the generator silently drops
|
|
// that production. The grammar still parses because the newline and `;`
|
|
// alternatives remain.
|
|
|
|
const terminator = choice('\n', ';', eof());
|
|
|
|
export default grammar({
|
|
name: 'eof_dropped_branch',
|
|
|
|
inline: $ => [$._inline_eof],
|
|
|
|
rules: {
|
|
source_file: $ => choice(
|
|
seq(
|
|
'(',
|
|
optional(seq($.item, repeat(seq(terminator, $.item)), optional(terminator))),
|
|
')',
|
|
),
|
|
$.inline_test,
|
|
),
|
|
|
|
item: _ => 'x',
|
|
|
|
// Inlining the second alternative would put `bad` after EOF, so that
|
|
// alternative must be discarded while the reachable one remains.
|
|
inline_test: $ => choice('ok', seq($._inline_eof, 'bad')),
|
|
_inline_eof: _ => eof(),
|
|
}
|
|
});
|