0

我有这样的语法:

terminator = '\n';

module.exports = grammar({
  name: 'sciname',

  rules: {
    source_file: ($) => repeat($._sci_name),

    _sci_name: ($) => seq($.uninomial, optional($.tail), terminator),

    _uninomial: ($) => /[A-Z][a-z]+/,

    uninomial: ($) => choice($._uninomial, seq($.genus, $.subgenus)),

    genus: ($) => $._uninomial,

    subgenus: ($) => seq('(', $._uninomial, ')'),

    tail: ($) => /.+/,
  },
});

tail规则假设捕获之后的任何东西uninomial。但是我在这个测试中遇到了麻烦:

=====================
Subgenus
=====================

Bubo (Bubo)

---------------------

(source_file (uninomial (genus) (subgenus)))

生产:

(source_file (uninomial) (tail))

代替

(source_file (uninomial (genus) (subgenus)))

更改优先级似乎不会影响此行为。有没有办法制定一个“包罗万象”的规则,比如tail只有在所有其他以前的规则都失败的情况下才参与?

4

1 回答 1

0

根据https://github.com/tree-sitter/tree-sitter/discussions/1407

阿林克写道:

尝试使用以下 token(prec(-1, ...)) 构造为您的尾部分配较低的词法分析器优先级:

tail: ($) => token(prec(-1, /[^\s]+/)),
于 2021-09-23T13:36:34.443 回答