Hello,

I am new to Treetop : in the following example, I'd like to get an
AST, but I am having troubles with Treetop::Runtime::SyntaxNode. I
though I was manipulating my own ast types instead...

It is probably very awkward.

Can someone help ?
Thanks
JC

ruby FoobarCompiler.rb gives :
test 0  : parsing aa:1,2,3,14
	-Success for parsing
	-top level AST instanceof : Program
./ast.rb:8:in `print': private method `print' called for
#<Treetop::Runtime::SyntaxNode:0xb7d70db8> (NoMethodError)
	from ./ast.rb:8:in `map'
	from ./ast.rb:8:in `print'
	from FoobarCompiler.rb:18:in `parse'
	from FoobarCompiler.rb:27:in `test'
	from FoobarCompiler.rb:40

# ========treetop grammar for things like " aa:1,2,3,4" =============
require 'ast'

grammar Foobar

  rule program
     ws lit ':' num tail:(',' num)*
     {
       def build
	Program.new all
       end

       def all
        [lit] + [num.build] + tail.elements.map {|e| e.num.build}
       end
     }
  end

  rule lit
    [a-z]+
    {
     def build
      Lit.new("foo")
     end
    }
  end

  rule num
     [0-9]+
    {
     def build
       Num.new(text_value)
     end
    }
  end

  rule ws
   [ \n\t]*
  end
end

#==========================================================
class FoobarCompiler

  def initialize
    @parser = FoobarParser.new
  end

  def parse(txt)
    root=@parser.parse(txt)
    if root
      puts "\t-Success for parsing"
      @tree=@parser.parse(txt).build
      puts "\t-top level AST instanceof : #{@tree.class}"
      @tree.print
    else
      puts "Failure for parsing : #{@parser.failure_message}"
    end

  end

  def test num,goal,txt
    puts "test #{num}".ljust(7)+" : #{goal} ".ljust(50)
    parse txt
  end

end

program_0 = "aa:1,2,3,14"
c=FoobarCompiler.new()
c.test 0, "parsing #{program_0}", program_0

#=====ast.rb ==============
include Treetop::Runtime

  class Ast  < Treetop::Runtime::SyntaxNode
    def initialize(sub=nil)
      @subtrees=sub
    end
    def print
      puts @subtrees.map{|t| t.print}
    end
  end

  class Program < Ast
    def initialize(subt=nil)
      super(subt)
    end
  end

  class Num < Ast
    def initialize(val)
      super
      @val=val
    end

    def print
      "value=#{@val}"
    end
  end

  class Lit < Ast
    def initialize(val)
      super
      @txt=val
    end
    def print
      "literal=#{@txt}"
    end
  end