A scaffold for Treetop projects.
somebody@somewhere ~/Code $ wget "https://github.com/RyanScottLewis/treetop_bootstrap/archive/master.zip"
somebody@somewhere ~/Code $ unzip master.zip && rm master.zip
somebody@somewhere ~/Code $ mv treetop_bootstrap-master my_cool_parsersomebody@somewhere ~/Code $ git clone [email protected]:RyanScottLewis/treetop_bootstrap.git
somebody@somewhere ~/Code $ mv treetop_bootstrap my_cool_parser
somebody@somewhere ~/Code $ rm -rf my_cool_parser/.gitRemember to replace all occurrences of
TreetopBootstrapwithMyCoolParserand all occurrences oftreetop_bootstrapwithmy_cool_parser!
This comes with an example SyntaxNode named HelloKeyword.
lib/treetop_bootstrap/language_parser.treetop
rule hello_keyword
'hello' space ( string / integer / float ) <HelloKeyword>
end
lib/treetop_bootstrap/language/hello_keyword.rb
class HelloKeyword < Node
def run
node = elements[0]
value = node.value
value_cls = value.class
node_cls = node.class.to_s.split('::').last
puts "Hello #{value}! <#{value_cls} - #{node_cls}>"
end
endrequire 'my_cool_parser'
TreetopBootstrap.parse('hello "world"').run # => Hello world! <String - StringLiteral>
TreetopBootstrap.parse('hello 12').run # => Hello 12! <Fixnum - IntegerLiteral>See examples/simple.rb for more.
Copyright © 2012 Ryan Scott Lewis [email protected].
The MIT License (MIT) - See LICENSE for further details.