tree-sitter/test/fixtures/test_grammars/immediate_tokens/grammar.js
2025-09-16 02:24:11 -04:00

39 lines
835 B
JavaScript

// This grammar demonstrates the usage of the IMMEDIATE_TOKEN rule. It allows the parser to produce
// a different token based on whether or not there are `extras` preceding the token's main content.
// When there are *no* leading `extras`, an immediate token is preferred over a normal token which
// would otherwise match.
export default grammar({
name: "immediate_tokens",
extras: $ => [/\s/],
rules: {
program: $ => $._expression,
_expression: $ => choice(
$.call,
$.infix,
$.prefix,
$.identifier,
),
call: $ => prec.left(-1, seq(
$._expression,
$._expression,
)),
prefix: $ => seq(
'::',
$.identifier,
),
infix: $ => seq(
$._expression,
token.immediate('::'),
$.identifier,
),
identifier: $ => /[a-z]+/
}
})