Skip to content

Commit

Permalink
make traversal more useful
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Jul 4, 2009
1 parent b69833f commit d9e9aad
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/ripper2ruby.rb
@@ -0,0 +1 @@
require 'ripper/ruby_builder'
24 changes: 14 additions & 10 deletions lib/ruby/node/traversal.rb
@@ -1,17 +1,21 @@
module Ruby
class Node
module Traversal
def select(*args)
def select(*args, &block)
result = []
result << self if matches?(args.dup) || block_given? && yield(self)
result << self if matches?(args.dup, &block)

children = (prolog.try(:elements).to_a || []) + nodes
children.flatten.compact.inject(result) { |result, node| result + node.select(*args) }
children.flatten.compact.inject(result) do |result, node|
result + node.select(*args, &block)
end
end

def matches?(args)
def matches?(args, &block)
conditions = args.last.is_a?(::Hash) ? args.pop : {}
conditions[:is_a] = args unless args.empty?
conditions.inject(true) do |result, (type, value)|

conditions.inject(!conditions.empty?) do |result, (type, value)|
result && case type
when :is_a
has_type?(value)
Expand All @@ -28,7 +32,7 @@ def matches?(args)
when :left_of
left_of?(value)
end
end
end && (!block_given? || block.call(self))
end

def has_type?(klass)
Expand All @@ -37,16 +41,16 @@ def has_type?(klass)
klass.each { |klass| return true if has_type?(klass) } and false
else
is_a?(klass) # allow to pass a symbol or string, too
end
end
end

def is_instance_of?(klass)
case klass
when ::Array
klass.each { |klass| return true if has_type?(klass) } and false
else
instance_of?(klass) # allow to pass a symbol or string, too
end
end
end

def has_token?(token)
Expand All @@ -61,7 +65,7 @@ def has_token?(token)
def has_value?(value)
self.value == value if respond_to?(:value)
end

def position?(pos)
position == pos
end
Expand Down
3 changes: 1 addition & 2 deletions lib/ruby/statements.rb
Expand Up @@ -33,8 +33,7 @@ def line_pos(row)
end

def replace_src(row, column, length, src)
@src[line_pos(row) + column, length] = src
save_src if filename
self.src[line_pos(row) + column, length] = src
offset_column = src.length - length
update_positions(row, column + length, offset_column)
end
Expand Down
68 changes: 68 additions & 0 deletions notes/funky.rb
@@ -0,0 +1,68 @@
# FUNKY RUBY

begin
puts 'this was'
nah!
rescue Exception => e then puts 'rescued'
else
puts 'not rescued'
ensure
puts 'and ensured'
end

# ----------------------------------------------------------------------------

a = { 1 => 2, 3 => 4 }
a = { 1, 2, 3, 4 }
a = { 1: 2, 3: 4 }

# ----------------------------------------------------------------------------

Foo::Bar :bar

# ----------------------------------------------------------------------------

puts <<stuff.strip + '!', <<too.strip
i can haz stuff
stuff
me too
too

# ----------------------------------------------------------------------------

puts ?a

# ----------------------------------------------------------------------------

BEGIN {
puts 'BEGIN'
}
END {
puts 'END'
}

# ----------------------------------------------------------------------------

puts DATA

__END__

HA!

# ----------------------------------------------------------------------------

foo = []
foo.<< :bar
foo.<=> bar


def foo(*args)
super()
super
end



# ============================================================================

module Foo; def self.Bar(bar); puts bar; end; end
24 changes: 24 additions & 0 deletions notes/valid.rb
@@ -0,0 +1,24 @@
# valid?

1+ 1 +1
1 /2.0

if true; :foo; end

when true; case TrueClass; :foo; end

a = lambda { |@foo| }

lambda { |FOO[:bar]| }

each { |a, (foo, bar)| }


A.foo (:bar)
hash [:foo]


foo(:baz => :buz, *options)


a[0]

0 comments on commit d9e9aad

Please sign in to comment.