1

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
DVG
  • 17,392
  • 7
  • 61
  • 88
  • Hmm... looking at the rule 'div' in the [mathematica grammar](https://github.com/farleyknight/mathematica_parser/blob/master/lib/mathematica.treetop), it has a `'/'` and it seems to work. – Mark Thomas Dec 07 '12 at 01:31
  • I've seen lots of example that do. Hence my confusion :/ – DVG Dec 07 '12 at 01:34

1 Answers1

1

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
DVG
  • 17,392
  • 7
  • 61
  • 88
  • On a side note, you don't need the parenthesis on the outside of your rules, unless your tagging them for use with classes later on. – Josh Voigts Dec 10 '12 at 03:06