Question

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.

Was it helpful?

Solution

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]?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top