Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Created April 1, 2013 03:09
Show Gist options
  • Save ravinggenius/5283036 to your computer and use it in GitHub Desktop.
Save ravinggenius/5283036 to your computer and use it in GitHub Desktop.
require 'parslet'
require 'pry'
module Rip::Compiler
class AST < Parslet::Transform
attr_reader :origin
def initialize(origin)
@origin = origin
end
rule(:comment => simple(:comment)) do |tokens|
# this block never gets called
comment = tokens[:comment]
location = location_for(comment)
Rip::Nodes::Comment.new(location, comment)
end
def apply(tree, context = nil)
binding.pry
super
end
protected
def location_for(slice)
Rip::Utilities::Location.new(origin, *slice.line_and_column)
end
end
end
% cat >> spec/fixtures/comment.rip
# comment me, bro
^d
% ./bin/rip debug -t syntax spec/fixtures/comment.rip
From: /Users/thomas/Code/rip-rip/lib/rip/compiler/ast.rb @ line 20 Rip::Compiler::AST#apply:
19: def apply(tree, context = nil)
=> 20: binding.pry
21: super
22: end
[1] pry(#<Rip::Compiler::AST>)> tree
=> [{:comment=>" comment me, bro"@1}]
[2] pry(#<Rip::Compiler::AST>)> exit
From: /Users/thomas/Code/rip-rip/lib/rip/compiler/ast.rb @ line 20 Rip::Compiler::AST#apply:
19: def apply(tree, context = nil)
=> 20: binding.pry
21: super
22: end
[1] pry(#<Rip::Compiler::AST>)> tree
=> {:comment=>" comment me, bro"@1}
[2] pry(#<Rip::Compiler::AST>)> exit
From: /Users/thomas/Code/rip-rip/lib/rip/compiler/ast.rb @ line 20 Rip::Compiler::AST#apply:
19: def apply(tree, context = nil)
=> 20: binding.pry
21: super
22: end
[1] pry(#<Rip::Compiler::AST>)> tree
=> " comment me, bro"@1
[2] pry(#<Rip::Compiler::AST>)> exit
/Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:206:in `+': no implicit conversion of nil into Array (TypeError)
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:206:in `rules'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:212:in `transform_elt'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:165:in `apply'
from /Users/thomas/Code/rip-rip/lib/rip/compiler/ast.rb:21:in `apply'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:227:in `block in recurse_hash'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:226:in `each'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:226:in `inject'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:226:in `recurse_hash'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:168:in `apply'
from /Users/thomas/Code/rip-rip/lib/rip/compiler/ast.rb:21:in `apply'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:234:in `block in recurse_array'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:234:in `map'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:234:in `recurse_array'
from /Users/thomas/Code/rip-parslet/lib/parslet/transform.rb:170:in `apply'
from /Users/thomas/Code/rip-rip/lib/rip/compiler/ast.rb:21:in `apply'
from /Users/thomas/Code/rip-rip/lib/rip/compiler/parser.rb:16:in `syntax_tree'
from /Users/thomas/Code/rip-rip/lib/rip/cli.rb:86:in `syntax_tree'
from /Users/thomas/Code/rip-rip/lib/rip/cli.rb:56:in `debug'
from /usr/local/opt/rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/thor-0.17.0/lib/thor/task.rb:27:in `run'
from /usr/local/opt/rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/thor-0.17.0/lib/thor/invocation.rb:120:in `invoke_task'
from /usr/local/opt/rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/thor-0.17.0/lib/thor.rb:344:in `dispatch'
from /usr/local/opt/rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/thor-0.17.0/lib/thor/base.rb:434:in `start'
from ./bin/rip:5:in `<main>'
@NigelThorne
Copy link

Try this.

require 'parslet'
require 'pry'

module Rip::Compiler
  class AST < Parslet::Transform
    attr_reader :origin

    def initialize(origin,&block)
      @@origin = origin
      super(&block)
    end

    rule(:comment => simple(:comment)) do |tokens|
      # this block never gets called
      comment = tokens[:comment]
      location = location_for(comment)
      Rip::Nodes::Comment.new(location, comment)
    end

    def apply(tree, context = nil)
#      binding.pry
      super
    end

    protected

    def self.location_for(slice)
      Rip::Utilities::Location.new(@@origin, "??", *(slice.line_and_column))
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment