2017-02-05 10:54:32 +03:00
|
|
|
# frozen_string_literal: true
|
2004-09-12 21:34:30 +04:00
|
|
|
#
|
|
|
|
# dummyparser.rb
|
|
|
|
#
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
require 'ripper'
|
2023-04-03 23:17:10 +03:00
|
|
|
module TestRipper; end
|
2004-09-12 21:34:30 +04:00
|
|
|
|
2023-04-03 23:17:10 +03:00
|
|
|
class TestRipper::Node
|
2004-09-23 14:49:11 +04:00
|
|
|
def initialize(name, *nodes)
|
2004-09-12 21:34:30 +04:00
|
|
|
@name = name
|
|
|
|
@children = nodes
|
|
|
|
end
|
|
|
|
|
2010-12-26 05:39:14 +03:00
|
|
|
attr_reader :name, :children
|
2004-09-12 21:34:30 +04:00
|
|
|
|
|
|
|
def to_s
|
2023-04-03 23:17:10 +03:00
|
|
|
"#{@name}(#{TestRipper::Node.trim_nil(@children).map {|n| n.to_s }.join(',')})"
|
2009-10-02 15:37:44 +04:00
|
|
|
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 21:34:30 +04:00
|
|
|
end
|
2017-11-24 07:26:23 +03:00
|
|
|
|
|
|
|
class Sym < self
|
|
|
|
def initialize(name)
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
":#{@name}"
|
|
|
|
end
|
|
|
|
end
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2023-04-03 23:17:10 +03:00
|
|
|
class TestRipper::NodeList
|
2014-11-22 10:10:31 +03:00
|
|
|
def initialize(list = [])
|
|
|
|
@list = list
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :list
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def push(item)
|
2004-09-12 21:34:30 +04:00
|
|
|
@list.push item
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-07-01 04:29:22 +03:00
|
|
|
def concat(item)
|
|
|
|
@list.concat item
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def prepend(items)
|
2007-11-19 10:10:09 +03:00
|
|
|
@list.unshift items
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2009-10-02 15:37:44 +04:00
|
|
|
"[#{@list.join(',')}]"
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-03 23:17:10 +03:00
|
|
|
class TestRipper::DummyParser < Ripper
|
2010-12-26 05:39:14 +03:00
|
|
|
def hook(*names)
|
2009-10-02 15:37:44 +04:00
|
|
|
class << self; self; end.class_eval do
|
2010-12-26 05:39:14 +03:00
|
|
|
names.each do |name|
|
|
|
|
define_method(name) do |*a, &b|
|
|
|
|
result = super(*a, &b)
|
|
|
|
yield(name, *a)
|
|
|
|
result
|
|
|
|
end
|
2009-10-02 15:37:44 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
2004-09-12 21:34:30 +04:00
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_program(stmts)
|
2004-09-12 21:34:30 +04:00
|
|
|
stmts
|
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_stmts_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_stmts_add(stmts, st)
|
2004-09-12 21:34:30 +04:00
|
|
|
stmts.push st
|
|
|
|
stmts
|
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_void_stmt
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('void')
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_var_ref(name)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('ref', name)
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_var_alias(a, b)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('valias', a, b)
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2020-12-15 08:14:13 +03:00
|
|
|
def on_assign_error(mesg = nil, a)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('assign_error', a)
|
2020-12-15 08:14:13 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_alias_error(mesg = nil, a)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('aliaserr', a)
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_arg_paren(args)
|
2004-09-12 21:34:30 +04:00
|
|
|
args
|
|
|
|
end
|
|
|
|
|
2007-11-19 10:10:09 +03:00
|
|
|
def on_args_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2007-11-19 10:10:09 +03:00
|
|
|
def on_args_add(list, arg)
|
2004-09-12 21:34:30 +04:00
|
|
|
list.push(arg)
|
|
|
|
end
|
|
|
|
|
2007-11-19 10:10:09 +03:00
|
|
|
def on_args_add_block(list, blk)
|
|
|
|
if blk
|
|
|
|
list.push('&' + blk.to_s)
|
|
|
|
else
|
|
|
|
list
|
|
|
|
end
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2007-11-19 10:10:09 +03:00
|
|
|
def on_args_add_star(list, arg)
|
2004-09-12 21:34:30 +04:00
|
|
|
list.push('*' + arg.to_s)
|
|
|
|
end
|
|
|
|
|
2007-11-19 10:10:09 +03:00
|
|
|
def on_args_prepend(list, args)
|
2004-09-12 21:34:30 +04:00
|
|
|
list.prepend args
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_method_add_arg(m, arg)
|
2007-11-19 10:10:09 +03:00
|
|
|
if arg == nil
|
|
|
|
arg = on_args_new
|
|
|
|
end
|
2004-09-12 21:34:30 +04:00
|
|
|
m.children.push arg
|
|
|
|
m
|
|
|
|
end
|
|
|
|
|
2007-11-19 10:10:09 +03:00
|
|
|
def on_method_add_block(m, b)
|
|
|
|
on_args_add_block(m.children, b)
|
|
|
|
m
|
|
|
|
end
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
def on_paren(params)
|
|
|
|
params
|
|
|
|
end
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
def on_brace_block(params, code)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('block', params, code)
|
2009-10-02 19:47:54 +04:00
|
|
|
end
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
def on_block_var(params, shadow)
|
|
|
|
params
|
|
|
|
end
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
def on_rest_param(var)
|
|
|
|
"*#{var}"
|
|
|
|
end
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2017-07-21 07:29:46 +03:00
|
|
|
def on_kwrest_param(var)
|
|
|
|
"**#{var}"
|
|
|
|
end
|
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
def on_blockarg(var)
|
|
|
|
"&#{var}"
|
|
|
|
end
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2011-12-26 18:20:09 +04:00
|
|
|
def on_params(required, optional, rest, more, keyword, keyword_rest, block)
|
2023-04-03 23:17:10 +03:00
|
|
|
args = TestRipper::NodeList.new
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
required.each do |req|
|
|
|
|
args.push(req)
|
|
|
|
end if required
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
optional.each do |var, val|
|
|
|
|
args.push("#{var}=#{val}")
|
|
|
|
end if optional
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
args.push(rest) if rest
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
more.each do |m|
|
|
|
|
args.push(m)
|
|
|
|
end if more
|
2011-02-06 06:18:44 +03:00
|
|
|
|
2009-10-02 19:47:54 +04:00
|
|
|
args.push(block) if block
|
|
|
|
args
|
|
|
|
end
|
2007-11-19 10:10:09 +03:00
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_assoc_new(a, b)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('assoc', a, b)
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_bare_assoc_hash(assoc_list)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('assocs', *assoc_list)
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
|
|
|
|
2004-09-23 14:49:11 +04:00
|
|
|
def on_assoclist_from_args(a)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('assocs', *a)
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|
2009-10-03 16:25:32 +04:00
|
|
|
|
2011-02-06 08:56:14 +03:00
|
|
|
def on_word_new
|
2017-02-05 10:54:32 +03:00
|
|
|
"".dup
|
2011-02-06 08:56:14 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_word_add(word, w)
|
|
|
|
word << w
|
|
|
|
end
|
|
|
|
|
2011-02-06 06:18:44 +03:00
|
|
|
def on_words_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2011-02-06 06:18:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_words_add(words, word)
|
|
|
|
words.push word
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_qwords_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2011-02-06 06:18:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_qwords_add(words, word)
|
|
|
|
words.push word
|
|
|
|
end
|
|
|
|
|
2017-11-24 07:26:23 +03:00
|
|
|
def on_symbols_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2017-11-24 07:26:23 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_symbols_add(symbols, symbol)
|
2023-04-03 23:17:10 +03:00
|
|
|
symbols.push TestRipper::Node::Sym.new(symbol)
|
2017-11-24 07:26:23 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_qsymbols_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2017-11-24 07:26:23 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_qsymbols_add(symbols, symbol)
|
2023-04-03 23:17:10 +03:00
|
|
|
symbols.push TestRipper::Node::Sym.new(symbol)
|
2017-11-24 07:26:23 +03:00
|
|
|
end
|
|
|
|
|
2017-07-01 04:29:22 +03:00
|
|
|
def on_mlhs_new
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::NodeList.new
|
2017-07-01 04:29:22 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_mlhs_paren(list)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new(:mlhs, list)
|
2017-07-01 04:29:22 +03:00
|
|
|
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 10:10:31 +03:00
|
|
|
def on_rescue(exc, *rest)
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new('rescue', (exc && TestRipper::NodeList.new(exc)), *rest)
|
2014-11-22 10:10:31 +03:00
|
|
|
end
|
|
|
|
|
2009-10-03 16: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|
|
2023-04-03 23:17:10 +03:00
|
|
|
TestRipper::Node.new(event, *args)
|
2009-10-03 16:25:32 +04:00
|
|
|
end
|
|
|
|
end
|
2004-09-12 21:34:30 +04:00
|
|
|
end
|