我有以下输入字符串:
(
(
(App = smtp AND "Server Port" != 25)
OR
(App = pop3 AND "Server Port" == 20)
)
AND
(App = smtp AND "Server Port" != 35)
)
OR
(App = pop3 AND "Server Port" != 110)
AND
(
(App = imap AND "Server Port" != 143)
OR
(App = pop3 AND "Server Port" == 20)
)
AND (App = imap OR "Server Port" != 143)
希望转变为:
[
[
[
'App = smtp AND "Server Port" != 25',
'OR',
'App = pop3 AND "Server Port" == 20'
],
'AND',
'App = smtp AND "Server Port" != 35'
],
'OR',
'App = pop3 AND "Server Port" != 110',
'AND',
[
[
'App = imap AND "Server Port" != 143',
'OR',
'App = pop3 AND "Server Port" == 20'
]
],
'AND',
'App = imap OR "Server Port" != 143'
]
我如何在pegjs语法中做到这一点?
我当然可以写一个javascript代码,但我在寻找PEGjs的帮助。
答案 0 :(得分:0)
这应该让你开始,它大致是你正在寻找的。 p>
start =
_ ex:expression _
{
return ex;
}
expression =
op:operand bi:binary?
{
return bi ? [op].concat(bi) : op;
}
operand =
paren / terminal
paren =
'(' _ ex:expression _ ')'
{
return ex;
}
binary =
_ op:operator _ ex:expression
{
return [op, ex];
}
operator =
'OR' / 'AND'
terminal =
$((!paren [^)])+)
_ = [\r\n ]*
输出:
[
[
[
"App = smtp AND \"Server Port\" != 25",
"OR",
"App = pop3 AND \"Server Port\" == 20"
],
"AND",
"App = smtp AND \"Server Port\" != 35"
],
"OR",
[
"App = pop3 AND \"Server Port\" != 110",
"AND",
[
[
"App = imap AND \"Server Port\" != 143",
"OR",
"App = pop3 AND \"Server Port\" == 20"
],
"AND",
"App = imap OR \"Server Port\" != 143"
]
]
]