treetop parser?
parent
43d5f93206
commit
fc69b7d45e
@ -0,0 +1,3 @@
|
||||
Gemfile.lock
|
||||
.bundle
|
||||
|
@ -0,0 +1,18 @@
|
||||
require 'sanitize'
|
||||
require 'treetop'
|
||||
|
||||
base_path = File.expand_path(File.dirname(__FILE__))
|
||||
require File.join(base_path, 'bristlecode_nodes.rb')
|
||||
|
||||
class Bristle
|
||||
Treetop.load(File.join(File.expand_path(File.dirname(__FILE__)), 'bristlecode_parser.treetop'))
|
||||
@@parser = BristlecodeParser.new
|
||||
|
||||
def self.parse(doc)
|
||||
tree = @@parser.parse(doc)
|
||||
if tree.nil?
|
||||
raise Exception, "Bristlecode parse error at offset: #{@@parser.index}"
|
||||
end
|
||||
tree
|
||||
end
|
||||
end
|
@ -0,0 +1,37 @@
|
||||
module Bristlecode
|
||||
class RootNode < Treetop::Runtime::SyntaxNode
|
||||
def to_html
|
||||
elements.each{|elem| elem.to_html}
|
||||
end
|
||||
end
|
||||
|
||||
class TextNode < Treetop::Runtime::SyntaxNode
|
||||
def initialize(input, interval, elements)
|
||||
@text = input[interval]
|
||||
end
|
||||
|
||||
def text_value
|
||||
@text
|
||||
end
|
||||
|
||||
def inspect(indent="")
|
||||
"#{indent}TextNode: #{text_value}"
|
||||
end
|
||||
|
||||
def to_html
|
||||
text_value
|
||||
end
|
||||
end
|
||||
|
||||
class BoldOpenNode < Treetop::Runtime::SyntaxNode
|
||||
end
|
||||
|
||||
class BoldCloseNode < Treetop::Runtime::SyntaxNode
|
||||
end
|
||||
|
||||
class BoldNode < Treetop::Runtime::SyntaxNode
|
||||
end
|
||||
|
||||
class TagNode < Treetop::Runtime::SyntaxNode
|
||||
end
|
||||
end
|
@ -0,0 +1,32 @@
|
||||
grammar Bristlecode
|
||||
rule root
|
||||
(tag / text)+ <RootNode>
|
||||
end
|
||||
|
||||
rule text
|
||||
(!tag .)+ <TextNode>
|
||||
end
|
||||
|
||||
rule tag
|
||||
(bold) <TagNode>
|
||||
end
|
||||
|
||||
##############################
|
||||
# bold
|
||||
##############################
|
||||
|
||||
rule bold
|
||||
bold_open
|
||||
((!bold_close .)+ <TextNode>)
|
||||
bold_close
|
||||
<BoldNode>
|
||||
end
|
||||
|
||||
rule bold_open
|
||||
'[b]' <BoldOpenNode> / '[B]' <BoldOpenNode>
|
||||
end
|
||||
|
||||
rule bold_close
|
||||
'[/b]' <BoldCloseNode> / '[/B]' <BoldCloseNode>
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue