忽略PEG.js的空白 [英] Ignore whitespace with PEG.js
本文介绍了忽略PEG.js的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I want to ignore whitespaces and new lines with my grammar so they are missing in the PEG.js output. Also, a literal within brackets should be returned in a new array.
语法
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"
]
]
推荐答案
您必须使用更多的非终结符"来分解语法(不确定是否在PEG中称呼它们):
You have to break up the grammar more, using more "non-terminals" (not sure if that's what you call them in a 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]+
感谢您提出这个问题!
修改:
要提高可读性,请使用以下两种形式:_
和__
To increase readability, have two productions: _
and __
这篇关于忽略PEG.js的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文