examples/general/SRL/srl_demo.rb in rley-0.5.14 vs examples/general/SRL/srl_demo.rb in rley-0.6.00

- old
+ new

@@ -1,6 +1,7 @@ -require_relative './lib/parser' +require_relative './lib/tokenizer' +require_relative './lib/grammar' require_relative './lib/ast_builder' def print_title(aTitle) puts aTitle puts '=' * aTitle.size @@ -15,13 +16,10 @@ renderer = Rley::Formatter::Asciitree.new($stdout) renderer.render(visitor) puts '' end -# Create a calculator parser object -parser = SRL::Parser.new - # Parse the input expression in command-line if ARGV.empty? my_name = File.basename(__FILE__) msg = <<-END_MSG WORK IN PROGRESS @@ -40,26 +38,35 @@ END_MSG puts msg exit(1) end puts ARGV[0] -result = parser.parse_SRL(ARGV[0]) +# Create a Rley facade object +engine = Rley::Engine.new + +######################################## +# Step 1. Load a grammar for calculator +engine.use_grammar(SRL::Grammar) + +lexer = SRL::Tokenizer.new(ARGV[0]) +result = engine.parse(lexer.tokens) + unless result.success? # Stop if the parse failed... puts "Parsing of '#{ARGV[0]}' failed" puts "Reason: #{result.failure_reason.message}" exit(1) end # Generate a concrete syntax parse tree from the parse result -cst_ptree = result.parse_tree +cst_ptree = engine.convert(result) print_tree('Concrete Syntax Tree (CST)', cst_ptree) -# Generate a regexp literal representation from the parse result -tree_builder = ASTBuilder -ast_ptree = result.parse_tree(tree_builder) +# Generate an abstract syntax tree (AST) from the parse result +engine.configuration.repr_builder = ASTBuilder +ast_ptree = engine.convert(result) # Now output the regexp literal root = ast_ptree.root print_title('SRL to Regexp representation:') puts "#{ARGV[0]} => #{root.to_str}" # Output the expression result