2004-09-12 13:34:30 -04:00
|
|
|
#
|
|
|
|
# dummyparser.rb
|
|
|
|
#
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
require 'ripper'
|
2004-09-12 13:34:30 -04:00
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
class Node
|
|
|
|
def initialize(name, *nodes)
|
2004-09-12 13:34:30 -04:00
|
|
|
@name = name
|
|
|
|
@children = nodes
|
|
|
|
end
|
|
|
|
|
2010-12-25 21:39:14 -05:00
|
|
|
attr_reader :name, :children
|
2004-09-12 13:34:30 -04:00
|
|
|
|
|
|
|
def to_s
|
2009-10-02 07:37:44 -04:00
|
|
|
"#{@name}(#{Node.trim_nil(@children).map {|n| n.to_s }.join(',')})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.trim_nil(list)
|
|
|
|
if !list.empty? and list.last.nil?
|
|
|
|
list = list[0...-1]
|
|
|
|
list.pop while !list.empty? and list.last.nil?
|
|
|
|
end
|
|
|
|
list
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NodeList
|
|
|
|
def initialize
|
|
|
|
@list = []
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :list
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def push(item)
|
2004-09-12 13:34:30 -04:00
|
|
|
@list.push item
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def prepend(items)
|
2007-11-19 02:10:09 -05:00
|
|
|
@list.unshift items
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2009-10-02 07:37:44 -04:00
|
|
|
"[#{@list.join(',')}]"
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DummyParser < Ripper
|
2010-12-25 21:39:14 -05:00
|
|
|
def hook(*names)
|
2009-10-02 07:37:44 -04:00
|
|
|
class << self; self; end.class_eval do
|
2010-12-25 21:39:14 -05:00
|
|
|
names.each do |name|
|
|
|
|
define_method(name) do |*a, &b|
|
|
|
|
result = super(*a, &b)
|
|
|
|
yield(name, *a)
|
|
|
|
result
|
|
|
|
end
|
2009-10-02 07:37:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
2004-09-12 13:34:30 -04:00
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_program(stmts)
|
2004-09-12 13:34:30 -04:00
|
|
|
stmts
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_stmts_new
|
2004-09-12 13:34:30 -04:00
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_stmts_add(stmts, st)
|
2004-09-12 13:34:30 -04:00
|
|
|
stmts.push st
|
|
|
|
stmts
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_void_stmt
|
2004-09-12 13:34:30 -04:00
|
|
|
Node.new('void')
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_var_ref(name)
|
2004-09-12 13:34:30 -04:00
|
|
|
Node.new('ref', name)
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_var_alias(a, b)
|
2004-09-12 13:34:30 -04:00
|
|
|
Node.new('valias', a, b)
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_alias_error(a)
|
2004-09-12 13:34:30 -04:00
|
|
|
Node.new('aliaserr', a)
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_arg_paren(args)
|
2004-09-12 13:34:30 -04:00
|
|
|
args
|
|
|
|
end
|
|
|
|
|
2007-11-19 02:10:09 -05:00
|
|
|
def on_args_new
|
2004-09-12 13:34:30 -04:00
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
2007-11-19 02:10:09 -05:00
|
|
|
def on_args_add(list, arg)
|
2004-09-12 13:34:30 -04:00
|
|
|
list.push(arg)
|
|
|
|
end
|
|
|
|
|
2007-11-19 02:10:09 -05:00
|
|
|
def on_args_add_block(list, blk)
|
|
|
|
if blk
|
|
|
|
list.push('&' + blk.to_s)
|
|
|
|
else
|
|
|
|
list
|
|
|
|
end
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
|
2007-11-19 02:10:09 -05:00
|
|
|
def on_args_add_star(list, arg)
|
2004-09-12 13:34:30 -04:00
|
|
|
list.push('*' + arg.to_s)
|
|
|
|
end
|
|
|
|
|
2007-11-19 02:10:09 -05:00
|
|
|
def on_args_prepend(list, args)
|
2004-09-12 13:34:30 -04:00
|
|
|
list.prepend args
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_method_add_arg(m, arg)
|
2007-11-19 02:10:09 -05:00
|
|
|
if arg == nil
|
|
|
|
arg = on_args_new
|
|
|
|
end
|
2004-09-12 13:34:30 -04:00
|
|
|
m.children.push arg
|
|
|
|
m
|
|
|
|
end
|
|
|
|
|
2007-11-19 02:10:09 -05:00
|
|
|
def on_method_add_block(m, b)
|
|
|
|
on_args_add_block(m.children, b)
|
|
|
|
m
|
|
|
|
end
|
2009-10-02 11:47:54 -04:00
|
|
|
|
|
|
|
def on_paren(params)
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_brace_block(params, code)
|
|
|
|
Node.new('block', params, code)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_block_var(params, shadow)
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_rest_param(var)
|
|
|
|
"*#{var}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_blockarg(var)
|
|
|
|
"&#{var}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_params(required, optional, rest, more, block)
|
|
|
|
args = NodeList.new
|
|
|
|
|
|
|
|
required.each do |req|
|
|
|
|
args.push(req)
|
|
|
|
end if required
|
|
|
|
|
|
|
|
optional.each do |var, val|
|
|
|
|
args.push("#{var}=#{val}")
|
|
|
|
end if optional
|
|
|
|
|
|
|
|
args.push(rest) if rest
|
|
|
|
|
|
|
|
more.each do |m|
|
|
|
|
args.push(m)
|
|
|
|
end if more
|
|
|
|
|
|
|
|
args.push(block) if block
|
|
|
|
args
|
|
|
|
end
|
2007-11-19 02:10:09 -05:00
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_assoc_new(a, b)
|
2004-09-12 13:34:30 -04:00
|
|
|
Node.new('assoc', a, b)
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_bare_assoc_hash(assoc_list)
|
2004-09-12 13:34:30 -04:00
|
|
|
Node.new('assocs', *assoc_list)
|
|
|
|
end
|
|
|
|
|
2004-09-23 06:49:11 -04:00
|
|
|
def on_assoclist_from_args(a)
|
2009-12-07 20:01:04 -05:00
|
|
|
Node.new('assocs', *a)
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
2009-10-03 08:25:32 -04:00
|
|
|
|
|
|
|
(Ripper::PARSER_EVENTS.map(&:to_s) - instance_methods(false).map {|n|n.to_s.sub(/^on_/, '')}).each do |event|
|
|
|
|
define_method(:"on_#{event}") do |*args|
|
|
|
|
Node.new(event, *args)
|
|
|
|
end
|
|
|
|
end
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|