【问题标题】:Treetop Grammar does not recognize "/"树顶语法无法识别“/”
【发布时间】:2012-12-07 01:23:05
【问题描述】:

我是 Treetop 的新手,我有一个非常简单的语法,我就是无法使用。我有一些测试:

it "parses a open tag as some text surrouded by brackets" do
  document = "[b]"
  Parser.parse(document).should_not be_nil
end
it "parses a close tag as a tag with a / preceeding the tag name" do
  document = '[/b]'
  Parser.parse(document).should_not be_nil
end

这是我的语法:

grammar BBCode
  rule open_tag
    "[" tag_name "]"
  end

  rule tag_name
    [a-zA-Z\*]+
  end

  rule close_tag
    "[/" tag_name "]"
  end
end

第一个测试通过,第二个测试失败。我还尝试了这些替代规则:

"[" [\/] tag_name "]"
"[" "/" tag_name  "]"
"[\/" tag_name "]"

所有这些都失败了。

无论我尝试什么,我似乎都无法让它识别结束标签。

【问题讨论】:

  • 嗯...查看mathematica grammar 中的规则“div”,它有一个'/',它似乎有效。
  • 我见过很多这样的例子。因此我感到困惑:/

标签: ruby grammar treetop


【解决方案1】:

我发现了这个问题:https://github.com/nathansobo/treetop/issues/25,它似乎已经回答了我的问题。

我的语法不包含允许打开或关闭标签的顶级规则,因此甚至没有考虑第二种可能性:

grammar BBCode
  rule document
    (open_tag / close_tag)
  end

  rule open_tag
    ("[" tag_name "]")
  end

  rule tag_name
    [a-zA-Z\*]+
  end

  rule close_tag
    ("[/" tag_name "]")
  end
end

【讨论】:

  • 附带说明,您不需要在规则外部使用括号,除非您将它们标记为以后与类一起使用。
猜你喜欢
  • 2012-06-19
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多