Module: LiveAST::RubyParser::Test

Defined in:
lib/live_ast/ruby_parser/test.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unified_sexp?Boolean

Whether this is Ryan Davis’s unified sexp format.

Returns:

  • (Boolean)


13
14
15
# File 'lib/live_ast/ruby_parser/test.rb', line 13

def unified_sexp?
  true
end

.unparser_matches_ruby2ruby?Boolean

Whether the unparser output matches that of ruby2ruby.

Returns:

  • (Boolean)


20
21
22
# File 'lib/live_ast/ruby_parser/test.rb', line 20

def unparser_matches_ruby2ruby?
  true
end

Instance Method Details

#binop_block(name, op) ⇒ Object

binop_block(:foo, :+) returns the ast of

foo { |x, y| x + y }


146
147
148
149
150
151
# File 'lib/live_ast/ruby_parser/test.rb', line 146

def binop_block(name, op)
  s(:iter,
    s(:call, nil, name),
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#binop_def(name, op) ⇒ Object

binop_def(:f, :+) returns the ast of

def f(x, y)
  x + y
end


61
62
63
64
65
66
# File 'lib/live_ast/ruby_parser/test.rb', line 61

def binop_def(name, op)
  s(:defn,
    name,
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#binop_define_method(name, op, using = :define_method) ⇒ Object

binop_define_method(:f, :*) returns the ast of

define_method :f do |x, y|
  x * y
end

binop_define_method(:f, :-, :my_def) returns the ast of

my_def :f do |x, y|
  x - y
end


96
97
98
99
100
101
# File 'lib/live_ast/ruby_parser/test.rb', line 96

def binop_define_method(name, op, using = :define_method)
  s(:iter,
    s(:call, nil, using, s(:lit, name)),
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#binop_define_method_with_var(var_name, op) ⇒ Object

binop_define_method_with_var(:method_name, :/) returns the ast of

define_method method_name do |x, y|
  x / y
end


110
111
112
113
114
115
# File 'lib/live_ast/ruby_parser/test.rb', line 110

def binop_define_method_with_var(var_name, op)
  s(:iter,
    s(:call, nil, :define_method, s(:lvar, var_name)),
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#binop_define_singleton_method(name, op, receiver) ⇒ Object

binop_define_singleton_method(:f, :+, :a) returns the ast of

a.define_singleton_method :f do |x, y|
  x + y
end


124
125
126
127
128
129
130
# File 'lib/live_ast/ruby_parser/test.rb', line 124

def binop_define_singleton_method(name, op, receiver)
  s(:iter,
    s(:call, s(:lvar, receiver), :define_singleton_method,
      s(:lit, name)),
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#binop_lambda(op) ⇒ Object

binop_lambda(:+) returns the ast of

lambda { |x, y| x + y }


158
159
160
161
162
163
# File 'lib/live_ast/ruby_parser/test.rb', line 158

def binop_lambda(op)
  s(:iter,
    s(:lambda),
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#binop_proc_new(op) ⇒ Object

binop_proc_new(:*) returns the ast of

Proc.new { |x, y| x * y }


170
171
172
173
174
175
# File 'lib/live_ast/ruby_parser/test.rb', line 170

def binop_proc_new(op)
  s(:iter,
    s(:call, s(:const, :Proc), :new),
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#nested_defs(u, v, str) ⇒ Object

nested_defs(:f, :g, “foo”) returns the ast of

def f
  Class.new do
    def g
      "foo"
    end
  end
end


203
204
205
206
207
208
209
210
211
# File 'lib/live_ast/ruby_parser/test.rb', line 203

def nested_defs(u, v, str)
  s(:defn,
    u,
    s(:args),
    s(:iter,
      s(:call, s(:const, :Class), :new),
      0,
      s(:defn, v, s(:args), s(:str, str))))
end

#nested_lambdas(str) ⇒ Object

nested_lambdas(“foo”) returns the ast of

lambda {
  lambda {
    "foo"
  }
}


186
187
188
189
190
191
# File 'lib/live_ast/ruby_parser/test.rb', line 186

def nested_lambdas(str)
  s(:iter,
    s(:call, nil, :lambda),
    0,
    s(:iter, s(:call, nil, :lambda), 0, s(:str, str)))
end

#no_arg_block(name, ret) ⇒ Object

no_arg_block(:foo, “bar”) returns the ast of

foo { "bar" }


137
138
139
# File 'lib/live_ast/ruby_parser/test.rb', line 137

def no_arg_block(name, ret)
  s(:iter, s(:call, nil, name), 0, s(:str, ret))
end

#no_arg_def(name, ret) ⇒ Object

no_arg_def(:f, “A#f”) returns the ast of

def f
  "A#f"
end


32
33
34
# File 'lib/live_ast/ruby_parser/test.rb', line 32

def no_arg_def(name, ret)
  s(:defn, name, s(:args), s(:str, ret))
end

#no_arg_def_return(ast) ⇒ Object

no_arg_def_return(no_arg_def(:f, “A#f”)) == “A#f”



50
51
52
# File 'lib/live_ast/ruby_parser/test.rb', line 50

def no_arg_def_return(ast)
  ast[3][1]
end

#singleton_binop_def(const, name, op) ⇒ Object

singleton_binop_def(:A, :f, :+) returns the ast of

def A.f(x, y)
  x + y
end


75
76
77
78
79
80
81
# File 'lib/live_ast/ruby_parser/test.rb', line 75

def singleton_binop_def(const, name, op)
  s(:defs,
    s(:const, const),
    name,
    s(:args, :x, :y),
    s(:call, s(:lvar, :x), op, s(:lvar, :y)))
end

#singleton_no_arg_def(name, ret) ⇒ Object

singleton_no_arg_def(:f, “foo”) returns the ast of

def self.f
  "foo"
end


43
44
45
# File 'lib/live_ast/ruby_parser/test.rb', line 43

def singleton_no_arg_def(name, ret)
  s(:defs, s(:self), name, s(:args), s(:str, ret))
end