1

I have written a treetop grammer file that mostly works. For tags like [b] I want to pass them into a function that has a hash of configured BBCodes for that forum. If bold was allowed it would return the HTML, otherwise it would ignore the BB code.

rule tag
  tag:('[' [a-zA-Z]+ ']')
  inner_tag:(
    !('[/' [a-zA-Z]+ ']')
    (tag <ForumBB::TagNode> / .)
  )+
  '[/' [a-zA-Z]+ ']'
end

This doesn't work with nested tags. For example, [b][i]Bold and italics[/i][/b] won't be handled correctly because they match the first closing tag of [/i].

How can I make it so when it finds a tag it looks for the closing tag in the negative lookhead?

I'd rather not have to write all the rules out for each kind of BBCode as this is a dynamic system where forum administrators turn on/off certain tags.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kansha
  • 570
  • 4
  • 12

1 Answers1

2

I would say that your parser shouldn't have anything to do with your business logic; if forum administrators can turn tags on or off that should be handled when you're traversing your AST, not in the parser.

Your parser should merely be responsible for building the best syntax tree it can with the full knowledge of the grammar it's given. With that in mind I would recommend creating a rule for each valid BBCode tag and handle excluded tags after the parsing phase.

I also wonder what do you do when your parser recognizes invalid BBCode such as [z]invalid[/z]?

Ron Warholic
  • 9,994
  • 31
  • 47
  • when converting it if it comes to a tag not in the hash it just outputs what it got such as [z]invalid[/z] as-is. You may be right I maybe should look at it from another approach and find a way to only include the grammer rules dynamically. It certainly makes things cleaner. – Kansha Jan 22 '13 at 15:59
  • Worst case in your grammer file you can build a string for all the allowed tags and `eval` them as ruby code. Not pretty but it's cheap to do. – Ron Warholic Jan 22 '13 at 16:01