- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 PEGjs 新手,我正在尝试编写 PEGjs 语法转换正则表达式 (\s*[\(])|(\s*[\)])|(\"[^\(\)]+?\")|([^\(\)\s]+)
语法。
基本上我想做的是转换测试输入
(App= smtp AND "SPort"!= 25) OR (App= pop3 AND "SPort"!= 110) OR (App = imap AND "SPort"!= 143) AND (App= imap OR “运动”!= 143)
转换成如下的json格式
{
"eventTypes": [
"All"
],
"condition": {
"operator": "and",
"terms": [
{
"operator": "or",
"terms": [
{
"operator": "or",
"terms": [
{
"operator": "and",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "smtp"
},
{
"name": "Sport",
"operator": "notEquals",
"value": "25"
}
]
},
{
"operator": "and",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "pop3"
},
{
"name": "Sport",
"operator": "notEquals",
"value": "110"
}
]
}
]
},
{
"operator": "and",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "imap"
},
{
"name": "Sport",
"operator": "notEquals",
"value": "143"
}
]
}
]
},
{
"operator": "or",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "imap"
},
{
"name": "Sport",
"operator": "notEquals",
"value": "143"
}
]
}
]
}
}
我编写了一些复杂的 javascript 代码来将示例输入转换为 JSON 格式,但代码有点复杂,并且从长远来看不容易维护,所以我想尝试一下语法解析器。由于我是语法世界的新手,我寻求一些帮助或指导来实现执行上述操作的语法,以便我可以根据需要增强/编写?
You can see the output of the Regex here
编辑
Javascript解决方案:
var str = '((Application = smtp AND "Server Port" != 25) AND (Application = smtp AND "Server Port" != 25)) OR (Application = pop3 AND "Server Port" != 110) OR (Application = imap AND "Server Port" != 143) AND (Application = imap OR "Server Port" != 143)';
var final = str.replace(/\((?!\()/g,"['") //replace ( with [' if it's not preceded with (
.replace(/\(/g,"[") //replace ( with [
.replace(/\)/g,"']") //replace ) with ']
.replace(/\sAND\s/g,"','AND','") //replace AND with ','AND','
.replace(/\sOR\s/g,"','OR','") //replace OR with ','OR','
.replace(/'\[/g,"[") //replace '[ with [
.replace(/\]'/g,"]") //replace ]' with ]
.replace(/"/g,"\\\"") //escape double quotes
.replace(/'/g,"\""); //replace ' with "
console.log(JSON.parse("["+final+"]"))
最佳答案
据我所知,您无法准确获得您想要的结果,因为它需要无限循环。具体来说,给定以下输入:
A OR B OR C
您要求此输出:
(A OR B) OR C
要获得此结果,您需要有这样的规则:
BOOL = left:( BOOL / Expression ) "OR" right:( Expression )
这会造成无限循环,因为 BOOL 永远无法解析。 BOOL 无法解析,因为 BOOL 中的第一条规则是匹配 BOOL。然而,我们可以得到
A OR ( B OR C )
因为
BOOL = left:( Expression ) "OR" right:( BOOL / Expression )
不会创建无限循环。这是因为我们可以在递归回 BOOL 之前开始匹配某些东西。我知道这有点令人兴奋,但相信我...在递归之前,您必须有一些东西让 PegJS 开始匹配。
如果这是可以接受的,那么我相信这个语法将使您非常接近所需的输出:
// Our top-level rule is Expression
Expression
= BOOL
/ SubExpression
/ Comparison
/ Term
// A sub expression is just an expression wrapped in parentheses
// Note that this does not cause an infinite loop because the first term is always "("
SubExpression
= _ "(" _ innards: Expression _ ")" _ { return innards; }
Comparison
= name:Term _ operator:("=" / "!=") _ value:Term {
return {
name: name,
operator: operator === '=' ? 'equals' : 'notEquals',
value: value,
};
}
BOOL = AND / OR
// We separate the AND and OR because we want AND to take precendence over OR
AND
= _ left:( OR / SubExpression / Comparison ) _ "AND" _ right:( AND / OR / SubExpression / Comparison ) _ {
return {
operator: 'and',
terms: [ left, right ]
}
}
OR
= _ left:( SubExpression / Comparison ) _ "OR" _ right:( OR / SubExpression / Comparison ) _ {
return {
operator: 'or',
terms: [ left, right ]
}
}
Term
= '"'? value:$( [0-9a-zA-Z]+ ) '"'? {
return value;
}
Integer "integer"
= _ [0-9]+ { return parseInt(text(), 10); }
_ "whitespace"
= [ \t\n\r]*
根据您的输入,我们会得到:
{
"operator": "and",
"terms": [
{
"operator": "or",
"terms": [
{
"operator": "and",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "smtp"
},
{
"name": "SPort",
"operator": "notEquals",
"value": "25"
}
]
},
{
"operator": "or",
"terms": [
{
"operator": "and",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "pop3"
},
{
"name": "SPort",
"operator": "notEquals",
"value": "110"
}
]
},
{
"operator": "and",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "imap"
},
{
"name": "SPort",
"operator": "notEquals",
"value": "143"
}
]
}
]
}
]
},
{
"operator": "or",
"terms": [
{
"name": "App",
"operator": "equals",
"value": "imap"
},
{
"name": "SPort",
"operator": "notEquals",
"value": "143"
}
]
}
]
}
关于javascript - 将正则表达式转换为 PegJs 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43269126/
我有以下 PEGjs 作品: NameStartChar = ":" / [A-Z] / "_" / [a-z] / [\u00C0-\u00D6] / [\u00D8-\u00F6] / [\u00
start = intExp intExp = andIntExp / orIntExp andIntExp = integer (andExp intExp)* orIntExp
我有以下输入字符串: ( ( (App = smtp AND "Server Port" != 25) OR (App = pop3 AND "Server Por
我正在用 Pegjs 编写我的语言,像往常一样,我的语言有一些关键字,例如 true , false , if , else和today例如。现在,我想声明一个变量,但显然,变量名不能是保留关键字之一
我得到了支持这个的语法: AND, OR, NOT, ( and ), ", ' 我需要能够解析的东西很少: a1 或 a2 a1 a2(同上) a1 和 a2 “a1”和“a2” a1 或 a2 不
引自PEGJS Tutorial : To generate a parser, call the PEG.buildParser method and pass your grammar as a
考虑以下语法: list = head:item (',' tail:item)* { return [head].concat(tail); } item = $ ([0-9]*) 它应该描述
我写了一个 pegjs 语法,它应该可以解析任何类型的 js/c 风格的注释。但是,它不太有效,因为我只设法捕获了评论本身,而忽略了其他所有内容。我应该如何更改此语法以仅从任何类型的输入中解析注释?
我是 PEGjs 新手,我正在尝试编写 PEGjs 语法转换正则表达式 (\s*[\(])|(\s*[\)])|(\"[^\(\)]+?\")|([^\(\)\s]+) 语法。 基本上我想做的是转换测
我正在学习 PEGjs 语法,我请求以下方面的帮助或指导: 我有诸如 PRODUCT()、SUM()、DIVIDE() 之类的函数 PRODUCT 可以将数字/PRODUCT()/SUM()/DIVI
我又回到探索 pegjs 并且显然还没有掌握核心概念。我正在尝试解析以谓词开头然后是操作数列表(可能包含另一个谓词)的“查询语言”。所以一个简单的例子是: OR( "string1" "s
我对编写语法非常陌生(准确地说是第一次),我想创建一个可以为基本逻辑语句返回 AST 的语法。到目前为止,我有一个可以处理 AND、OR 逻辑的语法(我只是修改了官方 pegjs 站点上的基本计算器示
我正在编写一个 PegJS 语法来解析 SQL 语句。我正在努力将函数拆分为 function_id(function_args)。对于函数参数,我想匹配所有字符,包括 ( 和 ) 除了最后一个 ),
我有一个 atom 规则,它尝试首先将所有内容解析为数字或带引号的字符串,如果失败,则将其视为字符串。 一切解析都很好,除了一个特殊情况,即这个非常具体的字符串: DUD 123abc 无法解析 预期
尝试使用 PegJS 和 requirejs 测试项目。我有几个源文件,实现为通过 require API 加载的 AMD 模块(定义)。 目录结构下方: js/ somefile.js
我有一个 bbcode -> html 转换器,它响应文本区域中的更改事件。目前,这是使用一系列正则表达式来完成的,并且存在一些病态案例。我一直想在这个语法上削尖铅笔,但不想涉足剃牛毛。但是.....
我是一名优秀的程序员,十分优秀!