2017-02-05 02:54:32 -05:00
|
|
|
# frozen_string_literal: true
|
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
|
2017-11-23 23:26:23 -05:00
|
|
|
|
|
|
|
class Sym < self
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
":#{@name}"
|
|
|
|
end
|
|
|
|
end
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class NodeList
|
2014-11-22 02:10:31 -05:00
|
|
|
def initialize(list = [])
|
|
|
|
@list = list
|
2004-09-12 13:34:30 -04:00
|
|
|
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
|
|
|
|
|
2017-06-30 21:29:22 -04:00
|
|
|
def concat(item)
|
|
|
|
@list.concat 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
|
|
|
|
|
2020-12-15 00:14:13 -05:00
|
|
|
def on_assign_error(mesg = nil, a)
|
|
|
|
Node.new('assign_error', a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_alias_error(mesg = nil, 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
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
def on_paren(params)
|
|
|
|
params
|
|
|
|
end
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
def on_brace_block(params, code)
|
|
|
|
Node.new('block', params, code)
|
|
|
|
end
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
def on_block_var(params, shadow)
|
|
|
|
params
|
|
|
|
end
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
def on_rest_param(var)
|
|
|
|
"*#{var}"
|
|
|
|
end
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2017-07-21 00:29:46 -04:00
|
|
|
def on_kwrest_param(var)
|
|
|
|
"**#{var}"
|
|
|
|
end
|
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
def on_blockarg(var)
|
|
|
|
"&#{var}"
|
|
|
|
end
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2011-12-26 09:20:09 -05:00
|
|
|
def on_params(required, optional, rest, more, keyword, keyword_rest, block)
|
2009-10-02 11:47:54 -04:00
|
|
|
args = NodeList.new
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
required.each do |req|
|
|
|
|
args.push(req)
|
|
|
|
end if required
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
optional.each do |var, val|
|
|
|
|
args.push("#{var}=#{val}")
|
|
|
|
end if optional
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
args.push(rest) if rest
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
more.each do |m|
|
|
|
|
args.push(m)
|
|
|
|
end if more
|
2011-02-05 22:18:44 -05:00
|
|
|
|
2009-10-02 11:47:54 -04:00
|
|
|
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
|
|
|
|
2011-02-06 00:56:14 -05:00
|
|
|
def on_word_new
|
2017-02-05 02:54:32 -05:00
|
|
|
"".dup
|
2011-02-06 00:56:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_word_add(word, w)
|
|
|
|
word << w
|
|
|
|
end
|
|
|
|
|
2011-02-05 22:18:44 -05:00
|
|
|
def on_words_new
|
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_words_add(words, word)
|
|
|
|
words.push word
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_qwords_new
|
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_qwords_add(words, word)
|
|
|
|
words.push word
|
|
|
|
end
|
|
|
|
|
2017-11-23 23:26:23 -05:00
|
|
|
def on_symbols_new
|
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_symbols_add(symbols, symbol)
|
|
|
|
symbols.push Node::Sym.new(symbol)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_qsymbols_new
|
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_qsymbols_add(symbols, symbol)
|
|
|
|
symbols.push Node::Sym.new(symbol)
|
|
|
|
end
|
|
|
|
|
2017-06-30 21:29:22 -04:00
|
|
|
def on_mlhs_new
|
|
|
|
NodeList.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_mlhs_paren(list)
|
|
|
|
Node.new(:mlhs, list)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_mlhs_add(list, node)
|
|
|
|
list.push node
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_mlhs_add_block(list, blk)
|
|
|
|
if blk
|
|
|
|
list.push('&' + blk.to_s)
|
|
|
|
else
|
|
|
|
list
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_mlhs_add_star(list, arg)
|
|
|
|
list.push('*' + arg.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_mlhs_add_post(list, post)
|
|
|
|
list.concat(post.list)
|
|
|
|
end
|
|
|
|
|
2014-11-22 02:10:31 -05:00
|
|
|
def on_rescue(exc, *rest)
|
|
|
|
Node.new('rescue', (exc && NodeList.new(exc)), *rest)
|
|
|
|
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
|