Question

I'm new to Treetop, and have a very simple grammar that I just can't make work. I have some tests:

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

This is my grammar:

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

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

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

The first test passes, the second test fails. I also tried these alternate rules:

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

all of which fail.

I can't seem to get it to recognize the closing tag no matter what I try.

Was it helpful?

Solution

I found this issue: https://github.com/nathansobo/treetop/issues/25, and it appears to have answered my question.

My grammar did not contain a top level rule that would allow an opening or closing tag, therefore the second possibility was not even considered:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top