require 'test/unit' require 'rockit/dparser' class ATestArithmeticGrammar < Test::Unit::TestCase ArithmeticTestGrammar = Rockit::DParser::Grammar.new do start :Expr, [ [:Digit, "+", :Digit, ast(:Add, 0, 2)], [:Digit, "*", :Digit, ast(:Mult, 0, 2)], ] term :Digit, /[0-9]+/ end def test_03_use_parser parser = ArithmeticTestGrammar.new_parser ast = parser.parse "12+34" assert_equal("Add", ast.name) assert_kind_of(parser.astclass_of_name(:Add), ast) assert_equal(2, ast.num_children) assert_equal("12", ast[0]) assert_equal("34", ast[1]) ast = parser.parse "567*980" assert_equal("Mult", ast.name) assert_kind_of(parser.astclass_of_name(:Mult), ast) assert_equal(2, ast.num_children) assert_equal("567", ast[0]) assert_equal("980", ast[1]) end end