我有以下规格
it "parses a document with only an expression" do
puts parser.document.should parse("[b]Hello World[/b]")
end
it "parses a document with only text" do
puts parser.document.should parse(" Hello World")
end
it "parses a document with both an expression and text" do
puts parser.document.should parse("[b]Hello World[/b] Yes hello")
end
对于以下 Parslet 解析器
class Parser < Parslet::Parser
rule(:open_tag) do
parslet = str('[')
parslet = parslet >> (str(']').absent? >> match("[a-zA-Z]")).repeat(1).as(:open_tag_name)
parslet = parslet >> str(']')
parslet
end
rule(:close_tag) do
parslet = str('[/')
parslet = parslet >> (str(']').absent? >> match("[a-zA-Z]")).repeat(1).as(:close_tag_name)
parslet = parslet >> str(']')
parslet
end
rule(:text) { any.repeat(1).as(:text) }
rule(:expression) do
# [b]Hello World[/b]
# open tag, any text up until closing tag, closing tag
open_tag.present?
close_tag.present?
parslet = open_tag >> match("[a-zA-Z\s?]").repeat(1).as(:enclosed_text) >> close_tag
parslet
end
rule(:document) do
expression | text
end
前两个测试通过就好了,我可以通过put
将它们输出到命令行来看到原子的类型是正确的。但是,当我尝试使用表达式和纯文本解析文档时,它无法解析纯文本,并出现以下错误
Parslet::UnconsumedInput: Don't know what to do with " Yes hello" at line 1 char 19.
我想我在定义 :document 规则方面遗漏了一些东西。我想要的是会消耗任意数量的序列表达式和纯文本的东西,虽然我拥有的规则将消耗每个原子个体,但在同一个字符串中使用它们会导致失败。