例如,考虑以下语法:
source_file: $ => $._expression,
_expression: $ => choice(
$.identifier,
$.operator
),
identifier: $ => /\w*[A-Za-z]\w*/,
operator: $ => seq(
repeat1(seq($._expression, '\\X')),
$._expression
)
因此,如果我有输入字符串a \X b \X c \X d
,我希望它匹配为:
(source_file
(operator
(identifier)
(identifier)
(identifier)
(identifier)))
但是,我实际上可以获得此行为的唯一方法是执行以下操作:
operator: $ => choice(
$._operator_2,
$._operator_3,
...
),
_operator_2: $ => prec(1, seq(
$._expression, '\\X', $._expression)
),
_operator_3: $ => prec(2, seq(
$._expression, '\\X', $._expression, '\\X', $._expression)
),
...
所以我必须硬编码所有的表达式长度,随着长度的增加优先级增加,并且无法弄清楚如何编写一个包罗万象的_operator_n
规则。我该如何做到这一点?指定冲突然后分配动态优先级的某种组合?