我想用我的语法忽略空格和新行,所以它们在PEG.js输出中缺失了。此外,方括号中的文字应该在一个新数组中返回。
语法
start
= 'a'? sep+ ('cat'/'dog') sep* '(' sep* stmt_list sep* ')'
stmt_list
= exp: [a-zA-Z]+ { return new Array(exp.join('')) }
sep
= [' '\t\r\n]测试用例
a dog( Harry )输出
[
"a",
[
" "
],
"dog",
[],
"(",
[
" "
],
[
"Harry"
],
[
" "
],
")"
]输出我想要
[
"a",
"dog",
[
"Harry"
]
]发布于 2011-11-24 13:24:26
您必须更多地拆分语法,使用更多的“非终端”(不确定这是否是PEG中的名称):
start
= article? animal stmt_list
article
= article:'a' __ { return article; }
animal
= animal:('cat'/'dog') _ { return animal; }
stmt_list
= '(' _ exp:[a-zA-Z]+ _ ')' { return [ exp.join('') ]; }
// optional whitespace
_ = [ \t\r\n]*
// mandatory whitespace
__ = [ \t\r\n]+谢谢你问这个问题!
编辑:增加可读性,有两个产品:_和__
https://stackoverflow.com/questions/8257184
复制相似问题