Rus*_*hPL 5 javascript parsing peg pegjs
我正在尝试为PEG.js写一个简单的语法来匹配这样的东西:
some text;
arbitrary other text that can also have µnicode; different expression;
let's escape the \; semicolon, and \not recognized escapes are not a problem;
possibly last expression not ending with semicolon
Run Code Online (Sandbox Code Playgroud)
所以基本上这些是用分号分隔的一些文本.我的简化语法看起来像这样:
start
= flow:Flow
Flow
= instructions:Instruction*
Instruction
= Empty / Text
TextCharacter
= "\\;" /
.
Text
= text:TextCharacter+ ';' {return text.join('')}
Empty
= Semicolon
Semicolon "semicolon"
= ';'
Run Code Online (Sandbox Code Playgroud)
问题是,如果我在输入中放入除分号以外的任何内容,我会得到:
SyntaxError: Expected ";", "\\;" or any character but end of input found.
Run Code Online (Sandbox Code Playgroud)
怎么解决这个?我已经读过PEG.js无法匹配输入结束.
你有(至少)2个问题:
你TextCharacter不应该匹配任何角色(.).它应匹配除反斜杠和分号之外的任何字符,或者它应匹配转义字符:
TextCharacter
= [^\\;]
/ "\\" .
Run Code Online (Sandbox Code Playgroud)
第二个问题是你的语法要求你的输入以分号结尾(但你的输入并不以a结尾;).
相反,这样的事情怎么样:
start
= instructions
instructions
= instruction (";" instruction)* ";"?
instruction
= chars:char+ {return chars.join("").trim();}
char
= [^\\;]
/ "\\" c:. {return ""+c;}
Run Code Online (Sandbox Code Playgroud)
这将解析您的输入如下:
[
"some text",
[
[
";",
"arbitrary other text that can also have µnicode"
],
[
";",
"different expression"
],
[
";",
"let's escape the ; semicolon, and not recognized escapes are not a problem"
],
[
";",
"possibly last expression not ending with semicolon"
]
]
]
Run Code Online (Sandbox Code Playgroud)
请注意,尾部分号现在是可选的.
| 归档时间: |
|
| 查看次数: |
2566 次 |
| 最近记录: |