这是我使用的语法。匹配规则 'Func' 后的操作对输入字符串 if(diff("col")) 调用了 12 次,而不是 2 次。
调试我意识到在解析 Eq 规则时变量 peg$currPos 被设置了。我无法理解确切的行为
Main = Eq
Eq = (Ar (('equal'i/'>='/'<='/'<>'/'!='/'<'/'>'/'=') Ar))
/Ar ('equal'i/'>='/'<='/'<>'/'!='/'<'/'>'/'=')
/Ar
Ar = (Mul (('+'/'-') Mul )*) ('+'/'-')?
Mul = (Ex (('*'/'/'/'%'/'^') Ex )*) ('*'/'/'/'%'/'^')?
Ex = __ ('-'__ Main/'(' __ Main __ ')'/Func/Lit) __ /'-'
Func = __ id (('('__ Main __')')/'('__')') __ {
console.log(text());
}
Lit = (col/charLit/numLit)
/ id:id
charLit "character" = string:$("'"[^']*"'")
numLit "number" = $([0-9.]+)
col "column" = col:$('"'[^"]*'"')
id "identifier" = $([a-zA-Z][a-zA-Z0-9_]*)
__ "space" = [ \t\n]*