由于评估先前规则时出错,解析器无法匹配后续规则

parser unable to match the consequent rule due to error evaluating a former rule

示例:

start = name / invocation;

name = [a-zA-Z]+ { return text() };

invocation = a:name "()" { return {type: 'inv', value: a } };

如果输入是 abc() 我收到错误:

Expected [a-zA-Z] or end of input but "(" found

但是,如果 start 定义如下,则不会出现问题:

start = invocation / name;

对于前面的情况,不应该是name规则不匹配,从而进入invocation规则吗?不然怎么办?

有关工作示例,请参阅 playground

这是 属性 解析器表达式语法。更改 choice-operator 中表达式的顺序可以更改结果。

来自wikipedia

The fundamental difference between context-free grammars and parsing expression grammars is that the PEG's choice operator is ordered. If the first alternative succeeds, the second alternative is ignored. Thus ordered choice is not commutative, unlike unordered choice as in context-free grammars.