我正在编写一个 peg js 语法来将逻辑查询文字解析为 json 格式,但面临在条件值中选择多值词的问题。像(city = 'a' OR city = 'b')可以正常工作,但(city = 'a b' OR city = 'b')不起作用,因为它对于第一个条件具有多字值。我有一个正则表达式'((?:''|[^'])*)' 可以匹配单引号值中的字符串,但 PEG js 解析器无法解析它。
任何人都可以帮助我实现这一目标吗?下面是我的语法:在线编译器:https ://pegjs.org/online 输入:(city = 'a b' OR city = 'b')语法:
Set = col: Column right: (_ 'in'
i_ '('
Series ')') {
return {
column: col,
operator: right[1],
value: right[4].split(','),
conditions: []
}
}
Series = left: Column right: (_ ','
_(Series / Column)) ? {
if (right) {
var leftValue = left.slice(1).substring(0, left.length - 2);
var rightValue = right[3];
return leftValue + ',' + rightValue;
} else {
var leftValue = left.slice(1).substring(0, left.length - 2);
return leftValue;
}
}
LogicalCondition = left: LogicalSeparator right: (_('AND'
i / 'OR') _ LogicalSeparator) * {
var conditions = right.reduce(function (result, element) {
var condition = element[3];
if (condition.groupCondition) {
condition.groupOperator = element[1];
}
result.push(condition);
return result;
}, [left]);
if (right.length > 0) {
return {
"groupCondition": true,
"conditions": conditions,
"groupOperator": right[0][1]
}
} else if (left.groupCondition === true) {
return left;
} else {
return {
"groupCondition": true,
"conditions": [left]
}
}
}
/ Set
LogicalSeparator = Set /
"("
_ expr: LogicalCondition _ ")" {
return expr;
}
/ Condition
Condition = left: Column right: (_('customequalto'
i / 'customnotequalto'
i / '='
i / '!='
i / 'contains'
i / 'not contains'
i / 'in'
i / 'matches'
i / 'not matches'
i / 'is empty'
i / 'is not empty'
i / '<>') _ ConditionValue) * {
var validColumns = Array.of('employee_name', 'city', 'disabled', 'birthDate', 'age');
if (!validColumns.includes(left)) {
error('invalid column name ' + left);
}
return right.reduce(function (result, element) {
var valueText = right[0][3];
return {
conditions: [],
column: left,
operator: right[0][1],
value: valueText.slice(1).substring(0, valueText.length - 2),
};
}, left);
}
ConditionValue "string" = _['/A-Za-z0-9-_'] + {
return text();
}
Column "string" = _['/A-Za-z0-9-_'] + {
return text();
}
_ "whitespace" = [\t\ n\ r] *```
It would be a great help if you can help!.
Thank you in advance.