Skip to content

Instantly share code, notes, and snippets.

@danneu
Created November 2, 2012 23:45
Show Gist options
  • Save danneu/4005062 to your computer and use it in GitHub Desktop.
Save danneu/4005062 to your computer and use it in GitHub Desktop.
# bbcode.treetop
grammar BBCode
rule bbcode_post # [b]hey[/b]
opening_tag enclosed_text closing_tag
end
rule enclosed_text # hey
[^\[]+ # Match until '['
end
rule code # b
[^\]]+ # Match until ']'
end
rule opening_tag # [b]
'[' code ']'
end
rule closing_tag #[/b]
'[/' code ']'
end
end
require "treetop"
Treetop.load "bbcode"
parser = BBCodeParser.new
text = "[b]hey[/b]"
tree = parser.parse(text)
puts tree.opening_tag.text_value #=> [b]
puts tree.enclosed_text.text_value #=> hey
puts tree.closing_tag.text_value #=> [/b]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment