PEG.js输入结束的麻烦

时间:2012-10-05 09:58:03

标签: 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

所以基本上这些是用分号分隔的一些文本。我的简化语法看起来像这样:

start
= flow:Flow

Flow
= instructions:Instruction*

Instruction
= Empty / Text

TextCharacter
= "\\;" /
.

Text
= text:TextCharacter+ ';' {return text.join('')}

Empty
= Semicolon

Semicolon "semicolon"
= ';'

问题是,如果我在输入中放入除分号以外的任何内容,我会得到:

SyntaxError: Expected ";", "\\;" or any character but end of input found.

如何解决这个问题?我已经读过PEG.js无法匹配输入结束。

1 个答案:

答案 0 :(得分:8)

你有(至少)2个问题:

您的TextCharacter不应与任何字符.)匹配。它应匹配除反斜杠和分号之外的任何字符,或者它应匹配转义字符:

TextCharacter
 = [^\\;]
 / "\\" .

第二个问题是你的语法要求输入以分号结尾(但你的输入并不以;结尾)。

这样的事情怎么样:

start
 = instructions

instructions
 = instruction (";" instruction)* ";"?

instruction
 = chars:char+ {return chars.join("").trim();}

char
 = [^\\;]
 / "\\" c:. {return ""+c;}

将解析您的输入,如下所示:

[
   "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"
      ]
   ]
]

请注意,尾部分号现在是可选的。