Created
November 2, 2012 23:45
-
-
Save danneu/4005062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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