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)


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

def unified_sexp?
  true
end

.unparser_matches_ruby2ruby?Boolean

Whether the unparser output matches that of ruby2ruby.

Returns:

  • (Boolean)


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

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 }


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

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


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

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


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

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


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

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


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

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 }


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

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 }


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

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


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

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"
  }
}


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

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" }


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

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


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

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”



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

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


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

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


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

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