I'm having following input string:
(
(
(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)
wants to transform into:
[
[
[
'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'
]
How do I do this in the pegjs grammar?
I can certainly write a javascript code but I'm looks for some help in PEGjs.
This should get you started, it does roughly what you are looking for.
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 ]*
Output:
[
[
[
"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"
]
]
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.