1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/ripper: ripper extention added.

* ext/ripper/MANIFEST: new file.
* ext/ripper/README: new file.
* ext/ripper/depend: new file.
* ext/ripper/extconf.rb: new file.
* ext/ripper/eventids2.c: new file.
* ext/ripper/ripper.rb.in: new file.
* ext/ripper/lib/ripper.rb: new file.
* ext/ripper/test/check-event-arity.rb: new file.
* ext/ripper/test/check-event-coverage.sh: new file.
* ext/ripper/test/check-scanner-event-coverage.rb: new file.
* ext/ripper/test/list-called-events.rb: new file.
* ext/ripper/test/src_rb: new file.
* ext/ripper/test/validate.rb: new file.
* ext/ripper/tools/generate-eventids1.rb: new file.
* ext/ripper/tools/generate-param-macros.rb: new file.
* ext/ripper/tools/generate-ripper_rb.rb: new file.
* ext/ripper/tools/list-parse-event-ids.rb: new file.
* ext/ripper/tools/list-scan-event-ids.rb: new file.
* ext/ripper/tools/preproc.rb: new file.
* ext/ripper/tools/strip.rb: new file.
* test/ripper: ripper tests added.
* test/ripper/dummyparser.rb: new file.
* test/ripper/test_parser_events.rb: new file.
* test/ripper/test_scanner_events.rb: new file.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6891 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2004-09-12 17:34:30 +00:00
parent 5bea219a9d
commit c971589817
25 changed files with 3911 additions and 0 deletions

526
test/ripper/dummyparser.rb Normal file
View file

@ -0,0 +1,526 @@
#
# dummyparser.rb
#
class Node
def initialize( name, *nodes )
@name = name
@children = nodes
end
attr_reader :children
def to_s
"#{@name}(#{@children.map {|n| n.to_s }.join(',')})"
end
end
class NodeList
def initialize
@list = []
end
attr_reader :list
def push( item )
@list.push item
self
end
def prepend( items )
@list[0,0] = items
end
def to_s
'[' + @list.join(',') + ']'
end
end
class DummyParser < Ripper
def method_missing( mid, *args )
raise NoMethodError, "wrong method: #{mid}" unless /\Aon__/ === mid.to_s
args[0]
end
def on__program( stmts )
$thru_program = true
stmts
end
def on__stmts_new
NodeList.new
end
def on__stmts_add( stmts, st )
stmts.push st
stmts
end
def on__void_stmt
Node.new('void')
end
def on__BEGIN( stmts )
Node.new('BEGIN', stmts)
end
def on__END( stmts )
Node.new('END', stmts)
end
def on__var_ref( name )
Node.new('ref', name)
end
def on__alias(a, b)
Node.new('alias', a, b)
end
def on__var_alias(a, b)
Node.new('valias', a, b)
end
def on__alias_error(a)
Node.new('aliaserr', a)
end
def on__aref(a, b)
Node.new('aref', a, b)
end
def on__aref_field(a, b)
Node.new('aref_field', a, b)
end
def on__arg_ambiguous
Node.new('arg_ambiguous')
end
def on__arg_paren( args )
args
end
def on__arglist_new
NodeList.new
end
def on__arglist_add( list, arg )
list.push(arg)
end
def on__arglist_add_block( list, blk )
list.push('&' + blk.to_s)
end
def on__arglist_add_star( list, arg )
list.push('*' + arg.to_s)
end
def on__arglist_prepend( list, args )
list.prepend args
list
end
def on__method_add_arg( m, arg )
m.children.push arg
m
end
def on__assoc_new(a, b)
Node.new('assoc', a, b)
end
def on__bare_assoc_hash( assoc_list )
Node.new('assocs', *assoc_list)
end
def on__assoclist_from_args(a)
Node.new('assocs', *a.list)
end
######## untested
def on__array(a)
Node.new('array', a)
end
def on__assign(a, b)
Node.new('assign', a, b)
end
def on__assign_error(a)
Node.new('assign_error', a)
end
def on__begin(a)
Node.new('begin', a)
end
def on__binary(a, b, c)
Node.new('binary', a, b, c)
end
def on__block_var(a)
Node.new('block_var', a)
end
def on__bodystmt(a, b, c, d)
Node.new('bodystmt', a, b, c, d)
end
def on__brace_block(a, b)
Node.new('brace_block', a, b)
end
def on__break(a)
Node.new('break', a)
end
def on__call(a, b, c)
Node.new('call', a, b, c)
end
def on__case(a, b)
Node.new('case', a, b)
end
def on__class(a, b, c)
Node.new('class', a, b, c)
end
def on__class_name_error(a)
Node.new('class_name_error', a)
end
def on__command(a, b)
Node.new('command', a, b)
end
def on__command_call(a, b, c, d)
Node.new('command_call', a, b, c, d)
end
def on__const_ref(a)
Node.new('const_ref', a)
end
def on__constpath_field(a, b)
Node.new('constpath_field', a, b)
end
def on__constpath_ref(a, b)
Node.new('constpath_ref', a, b)
end
def on__def(a, b, c)
Node.new('def', a, b, c)
end
def on__defined(a)
Node.new('defined', a)
end
def on__defs(a, b, c, d, e)
Node.new('defs', a, b, c, d, e)
end
def on__do_block(a, b)
Node.new('do_block', a, b)
end
def on__dot2(a, b)
Node.new('dot2', a, b)
end
def on__dot3(a, b)
Node.new('dot3', a, b)
end
def on__dyna_symbol(a)
Node.new('dyna_symbol', a)
end
def on__else(a)
Node.new('else', a)
end
def on__elsif(a, b, c)
Node.new('elsif', a, b, c)
end
def on__ensure(a)
Node.new('ensure', a)
end
def on__fcall(a)
Node.new('fcall', a)
end
def on__field(a, b, c)
Node.new('field', a, b, c)
end
def on__for(a, b, c)
Node.new('for', a, b, c)
end
def on__hash(a)
Node.new('hash', a)
end
def on__if(a, b, c)
Node.new('if', a, b, c)
end
def on__if_mod(a, b)
Node.new('if_mod', a, b)
end
def on__ifop(a, b, c)
Node.new('ifop', a, b, c)
end
def on__iter_block(a, b)
Node.new('iter_block', a, b)
end
def on__massign(a, b)
Node.new('massign', a, b)
end
def on__mlhs_add(a, b)
Node.new('mlhs_add', a, b)
end
def on__mlhs_add_star(a, b)
Node.new('mlhs_add_star', a, b)
end
def on__mlhs_new
Node.new('mlhs_new')
end
def on__mlhs_paren(a)
Node.new('mlhs_paren', a)
end
def on__module(a, b)
Node.new('module', a, b)
end
def on__mrhs_add(a, b)
Node.new('mrhs_add', a, b)
end
def on__mrhs_add_star(a, b)
Node.new('mrhs_add_star', a, b)
end
def on__mrhs_new
Node.new('mrhs_new')
end
def on__mrhs_new_from_arglist(a)
Node.new('mrhs_new_from_arglist', a)
end
def on__next(a)
Node.new('next', a)
end
def on__opassign(a, b, c)
Node.new('opassign', a, b, c)
end
def on__param_error(a)
Node.new('param_error', a)
end
def on__params(a, b, c, d)
Node.new('params', a, b, c, d)
end
def on__paren(a)
Node.new('paren', a)
end
def on__parse_error(a)
Node.new('parse_error', a)
end
def on__qwords_add(a, b)
Node.new('qwords_add', a, b)
end
def on__qwords_new
Node.new('qwords_new')
end
def on__redo
Node.new('redo')
end
def on__regexp_literal(a)
Node.new('regexp_literal', a)
end
def on__rescue(a, b, c, d)
Node.new('rescue', a, b, c, d)
end
def on__rescue_mod(a, b)
Node.new('rescue_mod', a, b)
end
def on__restparam(a)
Node.new('restparam', a)
end
def on__retry
Node.new('retry')
end
def on__return(a)
Node.new('return', a)
end
def on__return0
Node.new('return0')
end
def on__sclass(a, b)
Node.new('sclass', a, b)
end
def on__space(a)
Node.new('space', a)
end
def on__string_add(a, b)
Node.new('string_add', a, b)
end
def on__string_concat(a, b)
Node.new('string_concat', a, b)
end
def on__string_content
Node.new('string_content')
end
def on__string_dvar(a)
Node.new('string_dvar', a)
end
def on__string_embexpr(a)
Node.new('string_embexpr', a)
end
def on__string_literal(a)
Node.new('string_literal', a)
end
def on__super(a)
Node.new('super', a)
end
def on__symbol(a)
Node.new('symbol', a)
end
def on__symbol_literal(a)
Node.new('symbol_literal', a)
end
def on__topconst_field(a)
Node.new('topconst_field', a)
end
def on__topconst_ref(a)
Node.new('topconst_ref', a)
end
def on__unary(a, b)
Node.new('unary', a, b)
end
def on__undef(a)
Node.new('undef', a)
end
def on__unless(a, b, c)
Node.new('unless', a, b, c)
end
def on__unless_mod(a, b)
Node.new('unless_mod', a, b)
end
def on__until_mod(a, b)
Node.new('until_mod', a, b)
end
def on__var_field(a)
Node.new('var_field', a)
end
def on__when(a, b, c)
Node.new('when', a, b, c)
end
def on__while(a, b)
Node.new('while', a, b)
end
def on__while_mod(a, b)
Node.new('while_mod', a, b)
end
def on__word_add(a, b)
Node.new('word_add', a, b)
end
def on__word_new
Node.new('word_new')
end
def on__words_add(a, b)
Node.new('words_add', a, b)
end
def on__words_new
Node.new('words_new')
end
def on__xstring_add(a, b)
Node.new('xstring_add', a, b)
end
def on__xstring_literal(a)
Node.new('xstring_literal', a)
end
def on__xstring_new
Node.new('xstring_new')
end
def on__yield(a)
Node.new('yield', a)
end
def on__yield0
Node.new('yield0')
end
def on__zsuper
Node.new('zsuper')
end
end

View file

@ -0,0 +1,489 @@
#
# test_parser_events.rb
#
require 'ripper.so'
require 'dummyparser'
raise "ripper version differ" unless Ripper::Version == '0.1.0'
require 'test/unit'
class TestRipper_ParserEvents < Test::Unit::TestCase
def parse(str)
DummyParser.new(str).parse.to_s
end
$thru_program = false
def test_program
assert_equal '[void()]', parse('')
assert_equal true, $thru_program
end
def test_stmts_new
assert_equal '[void()]', parse('')
end
def test_stmts_add
assert_equal '[ref(nil)]', parse('nil')
assert_equal '[ref(nil),ref(nil)]', parse('nil;nil')
assert_equal '[ref(nil),ref(nil),ref(nil)]', parse('nil;nil;nil')
end
def test_void_stmt
assert_equal '[void()]', parse('')
assert_equal '[void()]', parse(';;')
end
def test_var_ref
assert_equal '[ref(a)]', parse('a')
assert_equal '[ref(nil)]', parse('nil')
assert_equal '[ref(true)]', parse('true')
end
def test_BEGIN
assert_equal '[BEGIN([void()])]', parse('BEGIN{}')
assert_equal '[BEGIN([ref(nil)])]', parse('BEGIN{nil}')
end
def test_END
assert_equal '[END([void()])]', parse('END{}')
assert_equal '[END([ref(nil)])]', parse('END{nil}')
end
def test_alias
assert_equal '[alias(a,b)]', parse('alias a b')
end
def test_var_alias
assert_equal '[valias($a,$g)]', parse('alias $a $g')
end
def test_alias_error
assert_equal '[aliaserr(valias($a,$1))]', parse('alias $a $1')
end
def test_arglist
assert_equal '[fcall(m,[])]', parse('m()')
assert_equal '[fcall(m,[1])]', parse('m(1)')
assert_equal '[fcall(m,[1,2])]', parse('m(1,2)')
assert_equal '[fcall(m,[*ref(r)])]', parse('m(*r)')
assert_equal '[fcall(m,[1,*ref(r)])]', parse('m(1,*r)')
assert_equal '[fcall(m,[1,2,*ref(r)])]', parse('m(1,2,*r)')
assert_equal '[fcall(m,[&ref(r)])]', parse('m(&r)')
assert_equal '[fcall(m,[1,&ref(r)])]', parse('m(1,&r)')
assert_equal '[fcall(m,[1,2,&ref(r)])]', parse('m(1,2,&r)')
assert_equal '[fcall(m,[*ref(a),&ref(b)])]', parse('m(*a,&b)')
assert_equal '[fcall(m,[1,*ref(a),&ref(b)])]', parse('m(1,*a,&b)')
assert_equal '[fcall(m,[1,2,*ref(a),&ref(b)])]', parse('m(1,2,*a,&b)')
end
def test_arg_paren
# FIXME
end
def test_aref
assert_equal '[aref(ref(v),[1])]', parse('v[1]')
assert_equal '[aref(ref(v),[1,2])]', parse('v[1,2]')
end
def test_assocs
assert_equal '[fcall(m,[assocs(assoc(1,2))])]', parse('m(1=>2)')
assert_equal '[fcall(m,[assocs(assoc(1,2),assoc(3,4))])]', parse('m(1=>2,3=>4)')
assert_equal '[fcall(m,[3,assocs(assoc(1,2))])]', parse('m(3,1=>2)')
end
def test_aref_field
assert_equal '[assign(aref_field(ref(a),[1]),2)]', parse('a[1]=2')
end
=begin
def test_arg_ambiguous
assert_equal true, $thru__arg_ambiguous
end
=end
def test_array # array literal
assert_equal '[array([1,2,3])]', parse('[1,2,3]')
end
def test_assign # generic assignment
assert_equal '[assign(var_field(v),1)]', parse('v=1')
end
=begin
def test_assign_error
assert_equal true, $thru__assign_error
end
def test_begin
assert_equal true, $thru__begin
end
def test_binary
assert_equal true, $thru__binary
end
def test_block_var
assert_equal true, $thru__block_var
end
def test_bodystmt
assert_equal true, $thru__bodystmt
end
def test_brace_block
assert_equal true, $thru__brace_block
end
def test_break
assert_equal true, $thru__break
end
def test_call
assert_equal true, $thru__call
end
def test_case
assert_equal true, $thru__case
end
def test_class
assert_equal true, $thru__class
end
def test_class_name_error
assert_equal true, $thru__class_name_error
end
def test_command
assert_equal true, $thru__command
end
def test_command_call
assert_equal true, $thru__command_call
end
def test_const_ref
assert_equal true, $thru__const_ref
end
def test_constpath_field
assert_equal true, $thru__constpath_field
end
def test_constpath_ref
assert_equal true, $thru__constpath_ref
end
def test_def
assert_equal true, $thru__def
end
def test_defined
assert_equal true, $thru__defined
end
def test_defs
assert_equal true, $thru__defs
end
def test_do_block
assert_equal true, $thru__do_block
end
def test_dot2
assert_equal true, $thru__dot2
end
def test_dot3
assert_equal true, $thru__dot3
end
def test_dyna_symbol
assert_equal true, $thru__dyna_symbol
end
def test_else
assert_equal true, $thru__else
end
def test_elsif
assert_equal true, $thru__elsif
end
def test_ensure
assert_equal true, $thru__ensure
end
def test_fcall
assert_equal true, $thru__fcall
end
def test_field
assert_equal true, $thru__field
end
def test_for
assert_equal true, $thru__for
end
def test_hash
assert_equal true, $thru__hash
end
def test_if
assert_equal true, $thru__if
end
def test_if_mod
assert_equal true, $thru__if_mod
end
def test_ifop
assert_equal true, $thru__ifop
end
def test_iter_block
assert_equal true, $thru__iter_block
end
def test_massign
assert_equal true, $thru__massign
end
def test_method_add_arg
assert_equal true, $thru__method_add_arg
end
def test_mlhs_add
assert_equal true, $thru__mlhs_add
end
def test_mlhs_add_star
assert_equal true, $thru__mlhs_add_star
end
def test_mlhs_new
assert_equal true, $thru__mlhs_new
end
def test_mlhs_paren
assert_equal true, $thru__mlhs_paren
end
def test_module
assert_equal true, $thru__module
end
def test_mrhs_add
assert_equal true, $thru__mrhs_add
end
def test_mrhs_add_star
assert_equal true, $thru__mrhs_add_star
end
def test_mrhs_new
assert_equal true, $thru__mrhs_new
end
def test_mrhs_new_from_arglist
assert_equal true, $thru__mrhs_new_from_arglist
end
def test_next
assert_equal true, $thru__next
end
def test_opassign
assert_equal true, $thru__opassign
end
def test_param_error
assert_equal true, $thru__param_error
end
def test_params
assert_equal true, $thru__params
end
def test_paren
assert_equal true, $thru__paren
end
def test_parse_error
assert_equal true, $thru__parse_error
end
def test_qwords_add
assert_equal true, $thru__qwords_add
end
def test_qwords_new
assert_equal true, $thru__qwords_new
end
def test_redo
assert_equal true, $thru__redo
end
def test_regexp_literal
assert_equal true, $thru__regexp_literal
end
def test_rescue
assert_equal true, $thru__rescue
end
def test_rescue_mod
assert_equal true, $thru__rescue_mod
end
def test_restparam
assert_equal true, $thru__restparam
end
def test_retry
assert_equal true, $thru__retry
end
def test_return
assert_equal true, $thru__return
end
def test_return0
assert_equal true, $thru__return0
end
def test_sclass
assert_equal true, $thru__sclass
end
def test_space
assert_equal true, $thru__space
end
def test_string_add
assert_equal true, $thru__string_add
end
def test_string_concat
assert_equal true, $thru__string_concat
end
def test_string_content
assert_equal true, $thru__string_content
end
def test_string_dvar
assert_equal true, $thru__string_dvar
end
def test_string_embexpr
assert_equal true, $thru__string_embexpr
end
def test_string_literal
assert_equal true, $thru__string_literal
end
def test_super
assert_equal true, $thru__super
end
def test_symbol
assert_equal true, $thru__symbol
end
def test_symbol_literal
assert_equal true, $thru__symbol_literal
end
def test_topconst_field
assert_equal true, $thru__topconst_field
end
def test_topconst_ref
assert_equal true, $thru__topconst_ref
end
def test_unary
assert_equal true, $thru__unary
end
def test_undef
assert_equal true, $thru__undef
end
def test_unless
assert_equal true, $thru__unless
end
def test_unless_mod
assert_equal true, $thru__unless_mod
end
def test_until_mod
assert_equal true, $thru__until_mod
end
def test_var_field
assert_equal true, $thru__var_field
end
def test_when
assert_equal true, $thru__when
end
def test_while
assert_equal true, $thru__while
end
def test_while_mod
assert_equal true, $thru__while_mod
end
def test_word_add
assert_equal true, $thru__word_add
end
def test_word_new
assert_equal true, $thru__word_new
end
def test_words_add
assert_equal true, $thru__words_add
end
def test_words_new
assert_equal true, $thru__words_new
end
def test_xstring_add
assert_equal true, $thru__xstring_add
end
def test_xstring_literal
assert_equal true, $thru__xstring_literal
end
def test_xstring_new
assert_equal true, $thru__xstring_new
end
def test_yield
assert_equal true, $thru__yield
end
def test_yield0
assert_equal true, $thru__yield0
end
def test_zsuper
assert_equal true, $thru__zsuper
end
=end
end

View file

@ -0,0 +1,744 @@
#
# test_scanner_events.rb
#
require 'ripper.so'
raise 'ripper version differ' unless Ripper::Version == '0.1.0'
require 'test/unit'
class TestRipper_ScannerEvents < Test::Unit::TestCase
class R < Ripper
def R.scan(target, src)
r = new(src, target)
r.parse
r.tokens.map {|id, tok| tok }
end
def R.lex(src)
r = new(src, 'scan')
r.parse
r.tokens
end
def initialize(src, target)
super src
@target = ('on__' + target).intern
@tokens = []
end
attr_reader :tokens
def method_missing(mid, *args)
case mid.to_s
when /\Aon__scan/
if @target == :on__scan
@tokens.push args
else
@tokens.push args if @target == args[0]
end
when /\Aon__/
;
else
raise NoMethodError, "no such method: #{mid}"
end
end
def warn(fmt, *args)
#p [fmt, args]
end
def warning(fmt, *args)
#p [fmt, args]
end
def compile_error(msg)
end
end
def test_scan
assert_equal [],
R.scan('scan', '')
assert_equal ['a'],
R.scan('scan', 'a')
assert_equal ['1'],
R.scan('scan', '1')
assert_equal ['1', ';', 'def', ' ', 'm', '(', 'arg', ')', 'end'],
R.scan('scan', "1;def m(arg)end")
end
def test_backref
assert_equal ["$`", "$&", "$'", '$1', '$2', '$3'],
R.scan('backref', %q[m($~, $`, $&, $', $1, $2, $3)])
end
def test_backtick
assert_equal ["`"],
R.scan('backtick', %q[p `make all`])
end
def test_comma
assert_equal [','] * 6,
R.scan('comma', %q[ m(0,1,2,3,4,5,6) ])
assert_equal [],
R.scan('comma', %q[".,.,.,.,.,.,.."])
assert_equal [],
R.scan('comma', %Q[<<EOS\n,,,,,,,,,,\nEOS])
end
def test_period
assert_equal [],
R.scan('period', '')
assert_equal ['.'],
R.scan('period', 'a.b')
assert_equal ['.'],
R.scan('period', 'Object.new')
assert_equal [],
R.scan('period', '"."')
assert_equal [],
R.scan('period', '1..2')
assert_equal [],
R.scan('period', '1...3')
end
def test_const
assert_equal ['CONST'],
R.scan('const', 'CONST')
assert_equal ['C'],
R.scan('const', 'C')
assert_equal ['CONST_A'],
R.scan('const', 'CONST_A')
assert_equal ['Const', 'Const2', 'Const3'],
R.scan('const', 'Const; Const2; Const3')
assert_equal ['Const'],
R.scan('const', 'Const(a)')
assert_equal ['M', 'A', 'A2'],
R.scan('const', 'M(A,A2)')
assert_equal [],
R.scan('const', '')
assert_equal [],
R.scan('const', 'm(lvar, @ivar, @@cvar, $gvar)')
end
def test_cvar
assert_equal [],
R.scan('cvar', '')
assert_equal ['@@cvar'],
R.scan('cvar', '@@cvar')
assert_equal ['@@__cvar__'],
R.scan('cvar', '@@__cvar__')
assert_equal ['@@CVAR'],
R.scan('cvar', '@@CVAR')
assert_equal ['@@cvar'],
R.scan('cvar', ' @@cvar#comment')
assert_equal ['@@cvar'],
R.scan('cvar', ':@@cvar')
assert_equal ['@@cvar'],
R.scan('cvar', 'm(lvar, @ivar, @@cvar, $gvar)')
assert_equal [],
R.scan('cvar', '"@@cvar"')
end
def test_embexpr_beg
assert_equal [],
R.scan('embexpr_beg', '')
assert_equal ['#{'],
R.scan('embexpr_beg', '"#{expr}"')
assert_equal [],
R.scan('embexpr_beg', '%q[#{expr}]')
assert_equal ['#{'],
R.scan('embexpr_beg', '%Q[#{expr}]')
assert_equal ['#{'],
R.scan('embexpr_beg', "m(<<EOS)\n\#{expr}\nEOS")
end
=begin
def test_embexpr_end
assert_equal [],
R.scan('embexpr_end', '')
assert_equal ['}'],
R.scan('embexpr_end', '"#{expr}"')
assert_equal [],
R.scan('embexpr_end', '%q[#{expr}]')
assert_equal ['}'],
R.scan('embexpr_end', '%Q[#{expr}]')
assert_equal ['}'],
R.scan('embexpr_end', "m(<<EOS)\n\#{expr}\nEOS")
end
=end
def test_embvar
assert_equal [],
R.scan('embvar', '')
assert_equal ['#'],
R.scan('embvar', '"#$gvar"')
assert_equal ['#'],
R.scan('embvar', '"#@ivar"')
assert_equal ['#'],
R.scan('embvar', '"#@@cvar"')
assert_equal [],
R.scan('embvar', '"#lvar"')
assert_equal [],
R.scan('embvar', '"#"')
assert_equal [],
R.scan('embvar', '"\#$gvar"')
assert_equal [],
R.scan('embvar', '"\#@ivar"')
assert_equal [],
R.scan('embvar', '%q[#@ivar]')
assert_equal ['#'],
R.scan('embvar', '%Q[#@ivar]')
end
def test_float
assert_equal [],
R.scan('float', '')
assert_equal ['1.000'],
R.scan('float', '1.000')
assert_equal ['123.456'],
R.scan('float', '123.456')
assert_equal ['1.2345678901234567890123456789'],
R.scan('float', '1.2345678901234567890123456789')
assert_equal ['1.000'],
R.scan('float', ' 1.000# comment')
assert_equal ['1.234e5'],
R.scan('float', '1.234e5')
assert_equal ['1.234e1234567890'],
R.scan('float', '1.234e1234567890')
assert_equal ['1.0'],
R.scan('float', 'm(a,b,1.0,c,d)')
end
def test_gvar
assert_equal [],
R.scan('gvar', '')
assert_equal ['$a'],
R.scan('gvar', '$a')
assert_equal ['$A'],
R.scan('gvar', '$A')
assert_equal ['$gvar'],
R.scan('gvar', 'm(lvar, @ivar, @@cvar, $gvar)')
assert_equal %w($_ $~ $* $$ $? $! $@ $/ $\\ $; $, $. $= $: $< $> $"),
R.scan('gvar', 'm($_, $~, $*, $$, $?, $!, $@, $/, $\\, $;, $,, $., $=, $:, $<, $>, $")')
end
def test_ident
assert_equal [],
R.scan('ident', '')
assert_equal ['lvar'],
R.scan('ident', 'lvar')
assert_equal ['m', 'lvar'],
R.scan('ident', 'm(lvar, @ivar, @@cvar, $gvar)')
end
def test_int
assert_equal [],
R.scan('int', '')
assert_equal ['1', '10', '100000000000000'],
R.scan('int', 'm(1,10,100000000000000)')
end
def test_ivar
assert_equal [],
R.scan('ivar', '')
assert_equal ['@ivar'],
R.scan('ivar', '@ivar')
assert_equal ['@__ivar__'],
R.scan('ivar', '@__ivar__')
assert_equal ['@IVAR'],
R.scan('ivar', '@IVAR')
assert_equal ['@ivar'],
R.scan('ivar', 'm(lvar, @ivar, @@cvar, $gvar)')
end
def test_kw
assert_equal [],
R.scan('kw', '')
assert_equal %w(not),
R.scan('kw', 'not 1')
assert_equal %w(and),
R.scan('kw', '1 and 2')
assert_equal %w(or),
R.scan('kw', '1 or 2')
assert_equal %w(if then else end),
R.scan('kw', 'if 1 then 2 else 3 end')
assert_equal %w(if then elsif else end),
R.scan('kw', 'if 1 then 2 elsif 3 else 4 end')
assert_equal %w(unless then end),
R.scan('kw', 'unless 1 then end')
assert_equal %w(if true),
R.scan('kw', '1 if true')
assert_equal %w(unless false),
R.scan('kw', '2 unless false')
assert_equal %w(case when when else end),
R.scan('kw', 'case n; when 1; when 2; else 3 end')
assert_equal %w(while do nil end),
R.scan('kw', 'while 1 do nil end')
assert_equal %w(until do nil end),
R.scan('kw', 'until 1 do nil end')
assert_equal %w(while),
R.scan('kw', '1 while 2')
assert_equal %w(until),
R.scan('kw', '1 until 2')
assert_equal %w(while break next retry end),
R.scan('kw', 'while 1; break; next; retry end')
assert_equal %w(for in next break end),
R.scan('kw', 'for x in obj; next 1; break 2 end')
assert_equal %w(begin rescue retry end),
R.scan('kw', 'begin 1; rescue; retry; end')
assert_equal %w(rescue),
R.scan('kw', '1 rescue 2')
assert_equal %w(def redo return end),
R.scan('kw', 'def m() redo; return end')
assert_equal %w(def yield yield end),
R.scan('kw', 'def m() yield; yield 1 end')
assert_equal %w(def super super super end),
R.scan('kw', 'def m() super; super(); super(1) end')
assert_equal %w(alias),
R.scan('kw', 'alias a b')
assert_equal %w(undef),
R.scan('kw', 'undef public')
assert_equal %w(class end),
R.scan('kw', 'class A < Object; end')
assert_equal %w(module end),
R.scan('kw', 'module M; end')
assert_equal %w(class end),
R.scan('kw', 'class << obj; end')
assert_equal %w(BEGIN),
R.scan('kw', 'BEGIN { }')
assert_equal %w(END),
R.scan('kw', 'END { }')
assert_equal %w(self),
R.scan('kw', 'self.class')
assert_equal %w(nil true false),
R.scan('kw', 'p(nil, true, false)')
assert_equal %w(__FILE__ __LINE__),
R.scan('kw', 'p __FILE__, __LINE__')
assert_equal %w(defined?),
R.scan('kw', 'defined?(Object)')
end
def test_lbrace
assert_equal [],
R.scan('lbrace', '')
assert_equal ['{'],
R.scan('lbrace', '3.times{ }')
assert_equal ['{'],
R.scan('lbrace', '3.times { }')
assert_equal ['{'],
R.scan('lbrace', '3.times{}')
assert_equal [],
R.scan('lbrace', '"{}"')
assert_equal ['{'],
R.scan('lbrace', '{1=>2}')
end
def test_rbrace
assert_equal [],
R.scan('rbrace', '')
assert_equal ['}'],
R.scan('rbrace', '3.times{ }')
assert_equal ['}'],
R.scan('rbrace', '3.times { }')
assert_equal ['}'],
R.scan('rbrace', '3.times{}')
assert_equal [],
R.scan('rbrace', '"{}"')
assert_equal ['}'],
R.scan('rbrace', '{1=>2}')
end
def test_lbracket
assert_equal [],
R.scan('lbracket', '')
assert_equal ['['],
R.scan('lbracket', '[]')
assert_equal ['['],
R.scan('lbracket', 'a[1]')
assert_equal [],
R.scan('lbracket', 'm(%q[])')
end
def test_rbracket
assert_equal [],
R.scan('rbracket', '')
assert_equal [']'],
R.scan('rbracket', '[]')
assert_equal [']'],
R.scan('rbracket', 'a[1]')
assert_equal [],
R.scan('rbracket', 'm(%q[])')
end
def test_lparen
assert_equal [],
R.scan('lparen', '')
assert_equal ['('],
R.scan('lparen', '()')
assert_equal ['('],
R.scan('lparen', 'm()')
assert_equal ['('],
R.scan('lparen', 'm (a)')
assert_equal [],
R.scan('lparen', '"()"')
assert_equal [],
R.scan('lparen', '"%w()"')
end
def test_rparen
assert_equal [],
R.scan('rparen', '')
assert_equal [')'],
R.scan('rparen', '()')
assert_equal [')'],
R.scan('rparen', 'm()')
assert_equal [')'],
R.scan('rparen', 'm (a)')
assert_equal [],
R.scan('rparen', '"()"')
assert_equal [],
R.scan('rparen', '"%w()"')
end
def test_op
assert_equal [],
R.scan('op', '')
assert_equal ['|'],
R.scan('op', '1 | 1')
assert_equal ['^'],
R.scan('op', '1 ^ 1')
assert_equal ['&'],
R.scan('op', '1 & 1')
assert_equal ['<=>'],
R.scan('op', '1 <=> 1')
assert_equal ['=='],
R.scan('op', '1 == 1')
assert_equal ['==='],
R.scan('op', '1 === 1')
assert_equal ['=~'],
R.scan('op', '1 =~ 1')
assert_equal ['>'],
R.scan('op', '1 > 1')
assert_equal ['>='],
R.scan('op', '1 >= 1')
assert_equal ['<'],
R.scan('op', '1 < 1')
assert_equal ['<='],
R.scan('op', '1 <= 1')
assert_equal ['<<'],
R.scan('op', '1 << 1')
assert_equal ['>>'],
R.scan('op', '1 >> 1')
assert_equal ['+'],
R.scan('op', '1 + 1')
assert_equal ['-'],
R.scan('op', '1 - 1')
assert_equal ['*'],
R.scan('op', '1 * 1')
assert_equal ['/'],
R.scan('op', '1 / 1')
assert_equal ['%'],
R.scan('op', '1 % 1')
assert_equal ['**'],
R.scan('op', '1 ** 1')
assert_equal ['~'],
R.scan('op', '~1')
assert_equal ['-'],
R.scan('op', '-a')
assert_equal ['+'],
R.scan('op', '+a')
assert_equal ['[]'],
R.scan('op', ':[]')
assert_equal ['[]='],
R.scan('op', ':[]=')
assert_equal [],
R.scan('op', %q[`make all`])
end
def test_symbeg
assert_equal [],
R.scan('symbeg', '')
assert_equal [':'],
R.scan('symbeg', ':sym')
assert_equal [':'],
R.scan('symbeg', '[1,2,3,:sym]')
assert_equal [],
R.scan('symbeg', '":sym"')
assert_equal [],
R.scan('symbeg', 'a ? b : c')
end
def test_tstring_beg
assert_equal [],
R.scan('tstring_beg', '')
assert_equal ['"'],
R.scan('tstring_beg', '"abcdef"')
assert_equal ['%q['],
R.scan('tstring_beg', '%q[abcdef]')
assert_equal ['%Q['],
R.scan('tstring_beg', '%Q[abcdef]')
end
def test_tstring_content
assert_equal [],
R.scan('tstring_content', '')
assert_equal ['abcdef'],
R.scan('tstring_content', '"abcdef"')
assert_equal ['abcdef'],
R.scan('tstring_content', '%q[abcdef]')
assert_equal ['abcdef'],
R.scan('tstring_content', '%Q[abcdef]')
assert_equal ['abc', 'def'],
R.scan('tstring_content', '"abc#{1}def"')
assert_equal ['sym'],
R.scan('tstring_content', ':"sym"')
end
def test_tstring_end
assert_equal [],
R.scan('tstring_end', '')
assert_equal ['"'],
R.scan('tstring_end', '"abcdef"')
assert_equal [']'],
R.scan('tstring_end', '%q[abcdef]')
assert_equal [']'],
R.scan('tstring_end', '%Q[abcdef]')
end
def test_regexp_beg
assert_equal [],
R.scan('regexp_beg', '')
assert_equal ['/'],
R.scan('regexp_beg', '/re/')
assert_equal ['%r<'],
R.scan('regexp_beg', '%r<re>')
assert_equal [],
R.scan('regexp_beg', '5 / 5')
end
def test_regexp_end
assert_equal [],
R.scan('regexp_end', '')
assert_equal ['/'],
R.scan('regexp_end', '/re/')
assert_equal ['>'],
R.scan('regexp_end', '%r<re>')
end
def test_words_beg
assert_equal [],
R.scan('words_beg', '')
assert_equal ['%W('],
R.scan('words_beg', '%W()')
assert_equal ['%W('],
R.scan('words_beg', '%W(w w w)')
assert_equal ['%W( '],
R.scan('words_beg', '%W( w w w )')
end
def test_qwords_beg
assert_equal [],
R.scan('qwords_beg', '')
assert_equal ['%w('],
R.scan('qwords_beg', '%w()')
assert_equal ['%w('],
R.scan('qwords_beg', '%w(w w w)')
assert_equal ['%w( '],
R.scan('qwords_beg', '%w( w w w )')
end
# FIXME: Close paren must not present (`words_end' scanner event?).
def test_words_sep
assert_equal [],
R.scan('words_sep', '')
assert_equal [')'],
R.scan('words_sep', '%w()')
assert_equal [' ', ' ', ')'],
R.scan('words_sep', '%w(w w w)')
assert_equal [' ', ' ', ' )'],
R.scan('words_sep', '%w( w w w )')
assert_equal ["\n", ' ', ' )'],
R.scan('words_sep', "%w( w\nw w )")
end
def test_heredoc_beg
assert_equal [],
R.scan('heredoc_beg', '')
assert_equal ['<<EOS'],
R.scan('heredoc_beg', "<<EOS\nheredoc\nEOS")
assert_equal ['<<EOS'],
R.scan('heredoc_beg', "<<EOS\nheredoc\nEOS\n")
assert_equal ['<<EOS'],
R.scan('heredoc_beg', "<<EOS\nheredoc\nEOS \n")
assert_equal ['<<-EOS'],
R.scan('heredoc_beg', "<<-EOS\nheredoc\n\tEOS \n")
assert_equal ['<<"EOS"'],
R.scan('heredoc_beg', %Q[<<"EOS"\nheredoc\nEOS])
assert_equal [%q(<<'EOS')],
R.scan('heredoc_beg', "<<'EOS'\nheredoc\nEOS")
assert_equal [%q(<<`EOS`)],
R.scan('heredoc_beg', "<<`EOS`\nheredoc\nEOS")
end
def test_heredoc_content
assert_equal [],
R.scan('heredoc_content', '')
assert_equal ["heredoc\n"],
R.scan('heredoc_content', "<<EOS\nheredoc\nEOS")
assert_equal ["heredoc\n"],
R.scan('heredoc_content', "<<EOS\nheredoc\nEOS\n")
assert_equal ["heredoc \n"],
R.scan('heredoc_content', "<<EOS\nheredoc \nEOS \n")
assert_equal ["heredoc\n"],
R.scan('heredoc_content', "<<-EOS\nheredoc\n\tEOS \n")
end
def test_heredoc_end
assert_equal [],
R.scan('heredoc_end', '')
assert_equal ["EOS"],
R.scan('heredoc_end', "<<EOS\nheredoc\nEOS")
assert_equal ["EOS\n"],
R.scan('heredoc_end', "<<EOS\nheredoc\nEOS\n")
assert_equal ["EOS \n"],
R.scan('heredoc_end', "<<EOS\nheredoc\nEOS \n")
assert_equal ["\tEOS \n"],
R.scan('heredoc_end', "<<-EOS\nheredoc\n\tEOS \n")
end
def test_semicolon
assert_equal [],
R.scan('semicolon', '')
assert_equal %w(;),
R.scan('semicolon', ';')
assert_equal %w(; ;),
R.scan('semicolon', ';;')
assert_equal %w(; ; ;),
R.scan('semicolon', 'nil;nil;nil;')
assert_equal %w(; ; ;),
R.scan('semicolon', 'nil;nil;nil;nil')
assert_equal [],
R.scan('semicolon', '";"')
assert_equal [],
R.scan('semicolon', '%w(;)')
assert_equal [],
R.scan('semicolon', '/;/')
end
def test_comment
assert_equal [],
R.scan('comment', '')
assert_equal ['# comment'],
R.scan('comment', '# comment')
assert_equal ["# comment\n"],
R.scan('comment', "# comment\n")
assert_equal ["# comment\n"],
R.scan('comment', "# comment\n1 + 1")
assert_equal ["# comment\n"],
R.scan('comment', "1 + 1 + 1# comment\n1 + 1")
end
def test_embdoc_beg
assert_equal [],
R.scan('embdoc_beg', '')
assert_equal ["=begin\n"],
R.scan('embdoc_beg', "=begin\ndoc\n=end")
assert_equal ["=begin \n"],
R.scan('embdoc_beg', "=begin \ndoc\n=end\n")
assert_equal ["=begin comment\n"],
R.scan('embdoc_beg', "=begin comment\ndoc\n=end\n")
end
def test_embdoc
assert_equal [],
R.scan('embdoc', '')
assert_equal ["doc\n"],
R.scan('embdoc', "=begin\ndoc\n=end")
assert_equal ["doc\n"],
R.scan('embdoc', "=begin\ndoc\n=end\n")
end
def test_embdoc_end
assert_equal [],
R.scan('embdoc_end', '')
assert_equal ["=end"],
R.scan('embdoc_end', "=begin\ndoc\n=end")
assert_equal ["=end\n"],
R.scan('embdoc_end', "=begin\ndoc\n=end\n")
end
def test_sp
assert_equal [],
R.scan('sp', '')
assert_equal [' '],
R.scan('sp', ' ')
assert_equal [' '],
R.scan('sp', ' 1')
assert_equal [],
R.scan('sp', "\n")
assert_equal [' '],
R.scan('sp', " \n")
assert_equal [' ', ' '],
R.scan('sp', "1 + 1")
assert_equal [],
R.scan('sp', "' '")
assert_equal [],
R.scan('sp', "%w( )")
assert_equal [],
R.scan('sp', "%w( w )")
assert_equal [],
R.scan('sp', "p(/ /)")
end
# `nl' event always means End-Of-Statement.
def test_nl
assert_equal [],
R.scan('nl', '')
assert_equal [],
R.scan('nl', "\n")
assert_equal ["\n"],
R.scan('nl', "1 + 1\n")
assert_equal ["\n", "\n"],
R.scan('nl', "1 + 1\n2 + 2\n")
assert_equal [],
R.scan('nl', "1 +\n1")
assert_equal [],
R.scan('nl', "1;\n")
assert_equal ["\n"],
R.scan('nl', "1 + 1\r\n")
assert_equal [],
R.scan('nl', "1;\r\n")
end
def test_ignored_nl
assert_equal [],
R.scan('ignored_nl', '')
assert_equal ["\n"],
R.scan('ignored_nl', "\n")
assert_equal [],
R.scan('ignored_nl', "1 + 1\n")
assert_equal [],
R.scan('ignored_nl', "1 + 1\n2 + 2\n")
assert_equal ["\n"],
R.scan('ignored_nl', "1 +\n1")
assert_equal ["\n"],
R.scan('ignored_nl', "1;\n")
assert_equal [],
R.scan('ignored_nl', "1 + 1\r\n")
assert_equal ["\n"],
R.scan('ignored_nl', "1;\r\n")
end
def test___end__
end
def test_CHAR
assert_equal [],
R.scan('CHAR', "")
assert_equal ["@"],
R.scan('CHAR', "@")
assert_equal [],
R.scan('CHAR', "@ivar")
end
end