I have written the code to parse the following expression
!=10 AND <=99
To
{
operator: 'AND',
values: [
{
op: '!=',
value: '10',
},
{
op: '<=',
value: '99',
},
],
}
using PEG.js. Please check the code sandbox.
But when I parse the following expression I am not getting the expected output.
=10 AND <=99 OR =1000
I want the following output
{
operator: 'or',
values: [
{
operator: 'and',
values: [
{
op: '=',
value: '10',
},
{
op: '<=',
value: '99',
},
],
},
{
op: '=',
value: '1000',
},
],
}
code sandbox:https://codesandbox.io/s/javascript-testing-sandbox-forked-h5xh5?file=/src/index.js
Do not use string functions to parse comparison operators, make them a part of your grammar. For chained operations like 'AND' use a rule like this:
and handle
headandtailaccordingly in the code block.Complete example: