15 #define YYERROR_VERBOSE 1
16 #define YYSTACK_USE_ALLOCA 0
30 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
32 #define YYMALLOC(size) rb_parser_malloc(parser, (size))
33 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size))
34 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size))
35 #define YYFREE(ptr) rb_parser_free(parser, (ptr))
36 #define malloc YYMALLOC
37 #define realloc YYREALLOC
38 #define calloc YYCALLOC
43 #define REGISTER_SYMID(id, name) register_symid((id), (name), strlen(name), enc)
47 #define is_notop_id(id) ((id)>tLAST_TOKEN)
48 #define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
49 #define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL)
50 #define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE)
51 #define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET)
52 #define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
53 #define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
54 #define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK)
56 #define is_asgn_or_id(id) ((is_notop_id(id)) && \
57 (((id)&ID_SCOPE_MASK) == ID_GLOBAL || \
58 ((id)&ID_SCOPE_MASK) == ID_INSTANCE || \
59 ((id)&ID_SCOPE_MASK) == ID_CLASS))
78 # define BITSTACK_PUSH(stack, n) ((stack) = ((stack)<<1)|((n)&1))
79 # define BITSTACK_POP(stack) ((stack) = (stack) >> 1)
80 # define BITSTACK_LEXPOP(stack) ((stack) = ((stack) >> 1) | ((stack) & 1))
81 # define BITSTACK_SET_P(stack) ((stack)&1)
83 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
84 #define COND_POP() BITSTACK_POP(cond_stack)
85 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
86 #define COND_P() BITSTACK_SET_P(cond_stack)
88 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
89 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
90 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
91 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
107 #define DVARS_INHERIT ((void*)1)
108 #define DVARS_TOPSCOPE NULL
109 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
110 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
153 rb_bug(
"vtable_add: vtable is not allocated (%p)", (
void *)tbl);
170 for (i = 0; i < tbl->
pos; i++) {
255 VALUE parser_ruby_sourcefile_string;
268 #define UTF8_ENC() (parser->utf8 ? parser->utf8 : \
269 (parser->utf8 = rb_utf8_encoding()))
270 #define STR_NEW(p,n) rb_enc_str_new((p),(n),parser->enc)
271 #define STR_NEW0() rb_enc_str_new(0,0,parser->enc)
272 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),parser->enc)
273 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),parser->enc)
274 #define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT)
275 #define TOK_INTERN(mb) rb_intern3(tok(), toklen(), parser->enc)
278 #define yyerror(msg) parser_yyerror(parser, (msg))
280 #define YYLEX_PARAM parser
282 #define lex_strterm (parser->parser_lex_strterm)
283 #define lex_state (parser->parser_lex_state)
284 #define cond_stack (parser->parser_cond_stack)
285 #define cmdarg_stack (parser->parser_cmdarg_stack)
286 #define class_nest (parser->parser_class_nest)
287 #define paren_nest (parser->parser_paren_nest)
288 #define lpar_beg (parser->parser_lpar_beg)
289 #define in_single (parser->parser_in_single)
290 #define in_def (parser->parser_in_def)
291 #define compile_for_eval (parser->parser_compile_for_eval)
292 #define cur_mid (parser->parser_cur_mid)
293 #define in_defined (parser->parser_in_defined)
294 #define tokenbuf (parser->parser_tokenbuf)
295 #define tokidx (parser->parser_tokidx)
296 #define toksiz (parser->parser_toksiz)
297 #define lex_input (parser->parser_lex_input)
298 #define lex_lastline (parser->parser_lex_lastline)
299 #define lex_nextline (parser->parser_lex_nextline)
300 #define lex_pbeg (parser->parser_lex_pbeg)
301 #define lex_p (parser->parser_lex_p)
302 #define lex_pend (parser->parser_lex_pend)
303 #define heredoc_end (parser->parser_heredoc_end)
304 #define command_start (parser->parser_command_start)
305 #define deferred_nodes (parser->parser_deferred_nodes)
306 #define lex_gets_ptr (parser->parser_lex_gets_ptr)
307 #define lex_gets (parser->parser_lex_gets)
308 #define lvtbl (parser->parser_lvtbl)
309 #define ruby__end__seen (parser->parser_ruby__end__seen)
310 #define ruby_sourceline (parser->parser_ruby_sourceline)
311 #define ruby_sourcefile (parser->parser_ruby_sourcefile)
312 #define current_enc (parser->enc)
313 #define yydebug (parser->parser_yydebug)
316 #define ruby_eval_tree (parser->parser_eval_tree)
317 #define ruby_eval_tree_begin (parser->parser_eval_tree_begin)
318 #define ruby_debug_lines (parser->debug_lines)
319 #define ruby_coverage (parser->coverage)
325 #define yyparse ruby_yyparse
328 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
331 #define cond(node) cond_gen(parser, (node))
333 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
341 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
342 #define void_expr0(node) void_expr_gen(parser, (node))
343 #define void_expr(node) void_expr0((node) = remove_begin(node))
345 #define void_stmts(node) void_stmts_gen(parser, (node))
347 #define reduce_nodes(n) reduce_nodes_gen(parser,(n))
349 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
352 #define block_append(h,t) block_append_gen(parser,(h),(t))
354 #define list_append(l,i) list_append_gen(parser,(l),(i))
356 #define list_concat(h,t) list_concat_gen(parser,(h),(t))
358 #define arg_append(h,t) arg_append_gen(parser,(h),(t))
360 #define arg_concat(h,t) arg_concat_gen(parser,(h),(t))
362 #define literal_concat(h,t) literal_concat_gen(parser,(h),(t))
365 #define new_evstr(n) new_evstr_gen(parser,(n))
367 #define evstr2dstr(n) evstr2dstr_gen(parser,(n))
371 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1))
373 #define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id))
376 #define new_args(f,o,r,p,b) new_args_gen(parser, (f),(o),(r),(p),(b))
380 #define ret_args(node) ret_args_gen(parser, (node))
383 #define new_yield(node) new_yield_gen(parser, (node))
386 #define gettable(id) gettable_gen(parser,(id))
388 #define assignable(id,node) assignable_gen(parser, (id), (node))
391 #define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
393 #define attrset(node,id) attrset_gen(parser, (node), (id))
396 #define rb_backref_error(n) rb_backref_error_gen(parser,(n))
398 #define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2))
401 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
404 #define local_tbl() local_tbl_gen(parser)
409 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
411 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
413 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
415 #define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match))
417 #define get_id(id) (id)
418 #define get_value(val) (val)
420 #define remove_begin(node) (node)
421 #define rb_dvar_defined(id) 0
422 #define rb_local_defined(id) 0
424 #define get_id(id) ripper_get_id(id)
426 #define get_value(val) ripper_get_value(val)
428 #define assignable(lhs,node) assignable_gen(parser, (lhs))
430 #define id_is_var(id) id_is_var_gen(parser, (id))
434 #define formal_argument(id) formal_argument_gen(parser, (id))
436 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
438 #define new_bv(id) new_bv_gen(parser, (id))
441 #define local_push(top) local_push_gen(parser,(top))
443 #define local_pop() local_pop_gen(parser)
445 #define local_var(id) local_var_gen(parser, (id));
447 #define arg_var(id) arg_var_gen(parser, (id))
449 #define local_id(id) local_id_gen(parser, (id))
451 #define internal_id() internal_id_gen(parser)
454 #define dyna_push() dyna_push_gen(parser)
456 #define dyna_pop(node) dyna_pop_gen(parser, (node))
458 #define dyna_in_block() dyna_in_block_gen(parser)
459 #define dyna_var(id) local_var(id)
461 #define dvar_defined(id) dvar_defined_gen(parser, (id), 0)
462 #define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1)
464 #define dvar_curr(id) dvar_curr_gen(parser, (id))
467 #define lvar_defined(id) lvar_defined_gen(parser, (id))
469 #define RE_OPTION_ONCE (1<<16)
470 #define RE_OPTION_ENCODING_SHIFT 8
471 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
472 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
473 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
474 #define RE_OPTION_MASK 0xff
475 #define RE_OPTION_ARG_ENCODING_NONE 32
477 #define NODE_STRTERM NODE_ZARRAY
478 #define NODE_HEREDOC NODE_ARRAY
479 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
480 #define nd_func u1.id
481 #if SIZEOF_SHORT == 2
482 #define nd_term(node) ((signed short)(node)->u2.id)
484 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
486 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
487 #define nd_nest u3.cnt
492 #define RIPPER_VERSION "0.1.0"
496 static ID ripper_id_gets;
505 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
506 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
507 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
508 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
509 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
510 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
512 #define yyparse ripper_yyparse
514 #define ripper_intern(s) ID2SYM(rb_intern(s))
517 #define ripper_id2sym(id) ((id) < 256 && rb_ispunct(id) ? \
518 ID2SYM(id) : ripper_id2sym(id))
521 #define arg_new() dispatch0(args_new)
522 #define arg_add(l,a) dispatch2(args_add, (l), (a))
523 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
524 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
525 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
526 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
527 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
529 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
530 #define mrhs_new() dispatch0(mrhs_new)
531 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
532 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
534 #define mlhs_new() dispatch0(mlhs_new)
535 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
536 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
538 #define params_new(pars, opts, rest, pars2, blk) \
539 dispatch5(params, (pars), (opts), (rest), (pars2), (blk))
541 #define blockvar_new(p,v) dispatch2(block_var, (p), (v))
542 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a))
543 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a))
545 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
546 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
547 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
549 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
556 # define ifndef_ripper(x) (x)
558 # define ifndef_ripper(x)
562 # define rb_warn0(fmt) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt))
563 # define rb_warnI(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
564 # define rb_warnS(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
565 # define rb_warning0(fmt) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt))
566 # define rb_warningS(fmt,a) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt), (a))
568 # define rb_warn0(fmt) ripper_warn0(parser, (fmt))
569 # define rb_warnI(fmt,a) ripper_warnI(parser, (fmt), (a))
570 # define rb_warnS(fmt,a) ripper_warnS(parser, (fmt), (a))
571 # define rb_warning0(fmt) ripper_warning0(parser, (fmt))
572 # define rb_warningS(fmt,a) ripper_warningS(parser, (fmt), (a))
573 static void ripper_warn0(
struct parser_params*,
const char*);
574 static void ripper_warnI(
struct parser_params*,
const char*,
int);
576 static void ripper_warnS(
struct parser_params*,
const char*,
const char*);
578 static void ripper_warning0(
struct parser_params*,
const char*);
579 static void ripper_warningS(
struct parser_params*,
const char*,
const char*);
583 static void ripper_compile_error(
struct parser_params*,
const char *fmt, ...);
584 # define rb_compile_error ripper_compile_error
585 # define compile_error ripper_compile_error
586 # define PARSER_ARG parser,
588 # define rb_compile_error rb_compile_error_with_enc
589 # define compile_error parser->nerr++,rb_compile_error_with_enc
590 # define PARSER_ARG ruby_sourcefile, ruby_sourceline, current_enc,
598 #define YYMAXDEPTH 10000
605 #define token_info_push(token) (RTEST(ruby_verbose) ? token_info_push(parser, (token)) : (void)0)
606 #define token_info_pop(token) (RTEST(ruby_verbose) ? token_info_pop(parser, (token)) : (void)0)
608 #define token_info_push(token)
609 #define token_info_pop(token)
621 const struct vtable *vars;
684 %type <node> singleton strings
string string1 xstring regexp
685 %type <node> string_contents xstring_contents regexp_contents string_content
686 %type <node> words qwords word_list qword_list word
687 %type <node> literal numeric dsym cpath
688 %type <node> top_compstmt top_stmts
top_stmt
690 %type <node>
expr_value arg_value primary_value
691 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
693 %type <node> paren_args opt_paren_args
694 %type <node>
command_args aref_args opt_block_arg block_arg var_ref var_lhs
695 %type <node> command_asgn mrhs superclass block_call block_command
696 %type <node> f_block_optarg f_block_opt
697 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
698 %type <node> assoc_list assocs assoc
undef_list backref string_dvar for_var
699 %type <node> block_param opt_block_param block_param_def f_opt
700 %type <node> bv_decls opt_bv_decl bvar
701 %type <node> lambda f_larglist lambda_body
705 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
798 while (node->nd_next) {
799 node = node->nd_next;
813 top_compstmt : top_stmts opt_terms
891 rb_warn0(
"else without rescue is useless");
976 buf[1] = (char)$3->nd_nth;
985 yyerror(
"can't make alias for the number variables");
1056 rb_warn0(
"END in method; use at_exit");
1081 ID vid = $1->nd_vid;
1163 yyerror(
"constant re-assignment");
1207 | mlhs
'=' arg_value
1301 block_command : block_call
1378 $5->nd_iter =
NEW_CALL($1, $3, $4);
1399 $5->nd_iter =
NEW_CALL($1, $3, $4);
1614 | primary_value
'[' opt_call_args rbracket
1650 yyerror(
"dynamic constant assignment");
1662 yyerror(
"dynamic constant assignment");
1698 | primary_value
'[' opt_call_args rbracket
1734 yyerror(
"dynamic constant assignment");
1747 yyerror(
"dynamic constant assignment");
1770 yyerror(
"class/module name must be CONSTANT");
1925 ID vid = $1->nd_vid;
1955 ID vid = $1->nd_vid;
1980 | primary_value
'[' opt_call_args rbracket
tOP_ASGN arg
2060 yyerror(
"constant re-assignment");
2071 yyerror(
"constant re-assignment");
2398 | args
',' assocs trailer
2416 paren_args :
'(' opt_call_args rparen
2426 opt_paren_args : none
2430 opt_call_args : none
2436 | args
',' assocs
','
2463 | args opt_block_arg
2471 | assocs opt_block_arg
2481 | args
',' assocs opt_block_arg
2511 block_arg :
tAMPER arg_value
2521 opt_block_arg :
',' block_arg
2547 | args
',' arg_value
2561 | args
',' tSTAR arg_value
2577 mrhs : args
',' arg_value
2591 | args
',' tSTAR arg_value
2659 rb_warning0(
"(...) interpreted as grouped expression");
2769 | operation brace_block
2781 | method_call brace_block
2853 | k_case opt_terms case_body k_end
2920 tbl[0] = 1; tbl[1] =
id;
2927 | k_class cpath superclass
2930 yyerror(
"class definition in method body");
2975 yyerror(
"module definition in method body");
3071 primary_value : primary
3221 f_marg_list : f_marg
3229 | f_marg_list
',' f_marg
3239 f_margs : f_marg_list
3247 | f_marg_list
',' tSTAR f_norm_arg
3256 | f_marg_list
',' tSTAR f_norm_arg
',' f_marg_list
3265 | f_marg_list
',' tSTAR
3273 | f_marg_list
',' tSTAR ',' f_marg_list
3290 |
tSTAR f_norm_arg
',' f_marg_list
3310 |
tSTAR ',' f_marg_list
3320 block_param : f_arg
',' f_block_optarg
',' f_rest_arg opt_f_block_arg
3328 | f_arg
',' f_block_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
3336 | f_arg
',' f_block_optarg opt_f_block_arg
3344 | f_arg
',' f_block_optarg
',' f_arg opt_f_block_arg
3352 | f_arg
',' f_rest_arg opt_f_block_arg
3369 | f_arg
',' f_rest_arg
',' f_arg opt_f_block_arg
3377 | f_arg opt_f_block_arg
3385 | f_block_optarg
',' f_rest_arg opt_f_block_arg
3393 | f_block_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
3401 | f_block_optarg opt_f_block_arg
3409 | f_block_optarg
',' f_arg opt_f_block_arg
3417 | f_rest_arg opt_f_block_arg
3425 | f_rest_arg
',' f_arg opt_f_block_arg
3443 opt_block_param : none
3450 block_param_def :
'|' opt_bv_decl
'|'
3468 |
'|' block_param opt_bv_decl
'|'
3533 $$->nd_body =
NEW_SCOPE($3->nd_head, $4);
3541 f_larglist :
'(' f_args opt_bv_decl rparen
3590 block_call : command do_block
3606 | block_call
'.' operation2 opt_paren_args
3615 | block_call
tCOLON2 operation2 opt_paren_args
3626 method_call : operation paren_args
3635 | primary_value
'.' operation2 opt_paren_args
3645 | primary_value
tCOLON2 operation2 paren_args
3655 | primary_value
tCOLON2 operation3
3663 | primary_value
'.' paren_args
3674 | primary_value
tCOLON2 paren_args
3701 | primary_value
'[' opt_call_args rbracket
3793 exc_list : arg_value
3920 VALUE src = node->nd_lit;
3936 for (list = (prev = node)->nd_next;
list; list = list->nd_next) {
3938 VALUE tail = list->nd_head->nd_lit;
3940 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
3946 prev->nd_next = list->nd_next;
3959 if (!node->nd_next) {
3960 VALUE src = node->nd_lit;
4000 | word_list word
' '
4010 word : string_content
4018 | word string_content
4073 | string_contents string_content
4091 | xstring_contents string_content
4109 | regexp_contents string_content
4112 NODE *head = $1, *tail = $2;
4291 var_ref : user_variable
4314 var_lhs : user_variable
4364 f_arglist :
'(' f_args rparen
4382 f_args : f_arg
',' f_optarg
',' f_rest_arg opt_f_block_arg
4390 | f_arg
',' f_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
4398 | f_arg
',' f_optarg opt_f_block_arg
4406 | f_arg
',' f_optarg
',' f_arg opt_f_block_arg
4414 | f_arg
',' f_rest_arg opt_f_block_arg
4422 | f_arg
',' f_rest_arg
',' f_arg opt_f_block_arg
4430 | f_arg opt_f_block_arg
4438 | f_optarg
',' f_rest_arg opt_f_block_arg
4446 | f_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
4454 | f_optarg opt_f_block_arg
4462 | f_optarg
',' f_arg opt_f_block_arg
4470 | f_rest_arg opt_f_block_arg
4478 | f_rest_arg
',' f_arg opt_f_block_arg
4507 yyerror(
"formal argument cannot be a constant");
4516 yyerror(
"formal argument cannot be an instance variable");
4525 yyerror(
"formal argument cannot be a global variable");
4534 yyerror(
"formal argument cannot be a class variable");
4542 f_norm_arg : f_bad_arg
4550 f_arg_item : f_norm_arg
4585 | f_arg
',' f_arg_item
4622 f_block_optarg : f_block_opt
4630 | f_block_optarg
',' f_block_opt
4635 while (opts->nd_next) {
4636 opts = opts->nd_next;
4654 | f_optarg
',' f_opt
4659 while (opts->nd_next) {
4660 opts = opts->nd_next;
4678 yyerror(
"rest argument must be local variable");
4706 yyerror(
"block argument must be local variable");
4708 yyerror(
"duplicated block argument name");
4719 opt_f_block_arg :
',' f_block_arg
4747 yyerror(
"can't define singleton method for ().");
4759 yyerror(
"can't define singleton method for literals");
4800 assoc : arg_value
tASSOC arg_value
4857 rbracket : opt_nl
']'
4886 # define yylval (*((YYSTYPE*)(parser->parser_yylval)))
4895 # define nextc() parser_nextc(parser)
4896 # define pushback(c) parser_pushback(parser, (c))
4897 # define newtok() parser_newtok(parser)
4898 # define tokspace(n) parser_tokspace(parser, (n))
4899 # define tokadd(c) parser_tokadd(parser, (c))
4900 # define tok_hex(numlen) parser_tok_hex(parser, (numlen))
4901 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e))
4902 # define tokadd_escape(e) parser_tokadd_escape(parser, (e))
4903 # define regx_options() parser_regx_options(parser)
4904 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e))
4905 # define parse_string(n) parser_parse_string(parser,(n))
4906 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc))
4907 # define here_document(n) parser_here_document(parser,(n))
4908 # define heredoc_identifier() parser_heredoc_identifier(parser)
4909 # define heredoc_restore(n) parser_heredoc_restore(parser,(n))
4910 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i))
4913 # define set_yylval_str(x) (yylval.node = NEW_STR(x))
4914 # define set_yylval_num(x) (yylval.num = (x))
4915 # define set_yylval_id(x) (yylval.id = (x))
4916 # define set_yylval_name(x) (yylval.id = (x))
4917 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x))
4918 # define set_yylval_node(x) (yylval.node = (x))
4919 # define yylval_id() (yylval.id)
4922 ripper_yylval_id(
ID x)
4926 # define set_yylval_str(x) (void)(x)
4927 # define set_yylval_num(x) (void)(x)
4928 # define set_yylval_id(x) (void)(x)
4929 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
4930 # define set_yylval_literal(x) (void)(x)
4931 # define set_yylval_node(x) (void)(x)
4932 # define yylval_id() yylval.id
4936 #define ripper_flush(p) (void)(p)
4938 #define ripper_flush(p) ((p)->tokp = (p)->parser_lex_p)
4940 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
4947 return lex_p > parser->tokp;
4960 ripper_dispatch_scan_event(
struct parser_params *parser,
int t)
4962 if (!ripper_has_scan_event(parser))
return;
4963 yylval_rval = ripper_scan_event_val(parser, t);
4967 ripper_dispatch_ignored_scan_event(
struct parser_params *parser,
int t)
4969 if (!ripper_has_scan_event(parser))
return;
4970 (void)ripper_scan_event_val(parser, t);
4974 ripper_dispatch_delayed_token(
struct parser_params *parser,
int t)
4977 const char *saved_tokp = parser->tokp;
4980 parser->tokp =
lex_pbeg + parser->delayed_col;
4982 parser->delayed =
Qnil;
4984 parser->tokp = saved_tokp;
4995 #undef SIGN_EXTEND_CHAR
4997 # define SIGN_EXTEND_CHAR(c) ((signed char)(c))
5000 # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
5003 #define parser_encoding_name() (parser->enc->name)
5004 #define parser_mbclen() mbclen((lex_p-1),lex_pend,parser->enc)
5005 #define parser_precise_mbclen() rb_enc_precise_mbclen((lex_p-1),lex_pend,parser->enc)
5006 #define is_identchar(p,e,enc) (rb_enc_isalnum(*(p),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
5007 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,parser->enc))
5009 #define parser_isascii() ISASCII(*(lex_p-1))
5017 for (p =
lex_pbeg; p < pend; p++) {
5019 column = (((column - 1) / 8) + 1) * 8;
5030 for (p =
lex_pbeg; p < pend; p++) {
5031 if (*p !=
' ' && *p !=
'\t') {
5038 #undef token_info_push
5055 #undef token_info_pop
5062 if (!ptinfo)
return;
5068 if (linenum == ptinfo->
linenum) {
5076 "mismatched indentations at '%s' with '%s' at %d",
5089 const int max_line_margin = 30;
5098 if (*p ==
'\n')
break;
5105 if (*pe ==
'\n')
break;
5112 const char *pre =
"", *post =
"";
5114 if (len > max_line_margin * 2 + 10) {
5115 if (
lex_p - p > max_line_margin) {
5119 if (pe -
lex_p > max_line_margin) {
5126 MEMCPY(buf, p,
char, len);
5130 i = (int)(
lex_p - p);
5131 p2 =
buf; pe = buf +
len;
5134 if (*p2 !=
'\t') *p2 =
' ';
5142 dispatch1(parse_error,
STR_NEW2(msg));
5154 CONST_ID(script_lines,
"SCRIPT_LINES__");
5171 if (
RTEST(coverages) &&
RBASIC(coverages)->klass == 0) {
5175 RBASIC(lines)->klass = 0;
5177 RARRAY(lines)->as.heap.len = n;
5260 char *beg, *end, *pend;
5270 while (end < pend) {
5271 if (*end++ ==
'\n')
break;
5281 if (
NIL_P(line))
return line;
5382 #define STR_FUNC_ESCAPE 0x01
5383 #define STR_FUNC_EXPAND 0x02
5384 #define STR_FUNC_REGEXP 0x04
5385 #define STR_FUNC_QWORDS 0x08
5386 #define STR_FUNC_SYMBOL 0x10
5387 #define STR_FUNC_INDENT 0x20
5393 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
5395 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
5397 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
5417 #define lex_goto_eol(parser) ((parser)->parser_lex_p = (parser)->parser_lex_pend)
5418 #define lex_eol_p() (lex_p >= lex_pend)
5419 #define peek(c) peek_n((c), 0)
5420 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n])
5443 if (
NIL_P(parser->delayed)) {
5447 parser->tokp,
lex_pend - parser->tokp);
5449 parser->delayed_col = (int)(parser->tokp -
lex_pbeg);
5453 parser->tokp,
lex_pend - parser->tokp);
5469 c = (
unsigned char)*
lex_p++;
5470 if (c ==
'\r' &&
peek(
'\n')) {
5481 if (c == -1)
return;
5488 #define was_bol() (lex_p == lex_pbeg + 1)
5490 #define tokfix() (tokenbuf[tokidx]='\0')
5491 #define tok() tokenbuf
5492 #define toklen() tokidx
5493 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
5539 yyerror(
"invalid hex escape");
5546 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5550 int string_literal,
int symbol_literal,
int regexp_literal)
5570 yyerror(
"invalid Unicode escape");
5573 if (codepoint > 0x10ffff) {
5574 yyerror(
"invalid Unicode codepoint (too large)");
5578 if (regexp_literal) {
5581 else if (codepoint >= 0x80) {
5583 if (string_literal)
tokaddmbc(codepoint, *encp);
5585 else if (string_literal) {
5588 }
while (string_literal && (
peek(
' ') ||
peek(
'\t')));
5591 yyerror(
"unterminated Unicode escape");
5595 if (regexp_literal) {
tokadd(
'}'); }
5601 yyerror(
"invalid Unicode escape");
5605 if (regexp_literal) {
5608 else if (codepoint >= 0x80) {
5610 if (string_literal)
tokaddmbc(codepoint, *encp);
5612 else if (string_literal) {
5620 #define ESCAPE_CONTROL 1
5621 #define ESCAPE_META 2
5630 switch (c =
nextc()) {
5655 case '0':
case '1':
case '2':
case '3':
5656 case '4':
case '5':
case '6':
case '7':
5664 if (numlen == 0)
return 0;
5674 if (flags & ESCAPE_META)
goto eof;
5675 if ((c =
nextc()) !=
'-') {
5679 if ((c =
nextc()) ==
'\\') {
5680 if (
peek(
'u'))
goto eof;
5681 return read_escape(flags|ESCAPE_META, encp) | 0x80;
5683 else if (c == -1 || !
ISASCII(c))
goto eof;
5685 return ((c & 0xff) | 0x80);
5689 if ((c =
nextc()) !=
'-') {
5694 if (flags & ESCAPE_CONTROL)
goto eof;
5695 if ((c =
nextc())==
'\\') {
5696 if (
peek(
'u'))
goto eof;
5701 else if (c == -1 || !
ISASCII(c))
goto eof;
5706 yyerror(
"Invalid escape character syntax");
5729 switch (c =
nextc()) {
5733 case '0':
case '1':
case '2':
case '3':
5734 case '4':
case '5':
case '6':
case '7':
5737 if (numlen == 0)
goto eof;
5746 if (numlen == 0)
return -1;
5752 if (flags & ESCAPE_META)
goto eof;
5753 if ((c =
nextc()) !=
'-') {
5758 flags |= ESCAPE_META;
5762 if (flags & ESCAPE_CONTROL)
goto eof;
5763 if ((c =
nextc()) !=
'-') {
5771 if (flags & ESCAPE_CONTROL)
goto eof;
5773 flags |= ESCAPE_CONTROL;
5775 if ((c =
nextc()) ==
'\\') {
5778 else if (c == -1)
goto eof;
5784 yyerror(
"Invalid escape character syntax");
5853 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
5857 int func,
int term,
int paren,
long *nest,
5861 int has_nonascii = 0;
5864 static const char mixed_msg[] =
"%s mixed within %s source";
5866 #define mixed_error(enc1, enc2) if (!errbuf) { \
5867 size_t len = sizeof(mixed_msg) - 4; \
5868 len += strlen(rb_enc_name(enc1)); \
5869 len += strlen(rb_enc_name(enc2)); \
5870 errbuf = ALLOCA_N(char, len); \
5871 snprintf(errbuf, len, mixed_msg, \
5872 rb_enc_name(enc1), \
5873 rb_enc_name(enc2)); \
5876 #define mixed_escape(beg, enc1, enc2) do { \
5877 const char *pos = lex_p; \
5879 mixed_error((enc1), (enc2)); \
5883 while ((c =
nextc()) != -1) {
5884 if (paren && c == paren) {
5887 else if (c == term) {
5888 if (!nest || !*nest) {
5894 else if ((func & STR_FUNC_EXPAND) && c ==
'#' &&
lex_p <
lex_pend) {
5896 if (c2 ==
'$' || c2 ==
'@' || c2 ==
'{') {
5901 else if (c ==
'\\') {
5902 const char *beg =
lex_p - 1;
5906 if (func & STR_FUNC_QWORDS)
break;
5907 if (func & STR_FUNC_EXPAND)
continue;
5912 if (func & STR_FUNC_ESCAPE)
tokadd(c);
5916 if ((func & STR_FUNC_EXPAND) == 0) {
5921 func & STR_FUNC_SYMBOL,
5922 func & STR_FUNC_REGEXP);
5923 if (has_nonascii && enc != *encp) {
5929 if (c == -1)
return -1;
5931 if ((func & STR_FUNC_EXPAND) == 0)
tokadd(
'\\');
5934 if (func & STR_FUNC_REGEXP) {
5938 if (has_nonascii && enc != *encp) {
5943 else if (func & STR_FUNC_EXPAND) {
5945 if (func & STR_FUNC_ESCAPE)
tokadd(
'\\');
5948 else if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
5951 else if (c != term && !(paren && c == paren)) {
5968 else if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
5985 #define NEW_STRTERM(func, term, paren) \
5986 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
5992 if (!
NIL_P(parser->delayed)) {
5993 ptrdiff_t len =
lex_p - parser->tokp;
5998 parser->tokp =
lex_p;
6002 #define flush_string_content(enc) ripper_flush_string_content(parser, (enc))
6004 #define flush_string_content(enc) ((void)(enc))
6010 int func = (int)quote->nd_func;
6018 if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
6022 if (c == term && !quote->nd_nest) {
6023 if (func & STR_FUNC_QWORDS) {
6024 quote->nd_func = -1;
6027 if (!(func & STR_FUNC_REGEXP))
return tSTRING_END;
6036 if ((func & STR_FUNC_EXPAND) && c ==
'#') {
6037 switch (c =
nextc()) {
6051 if (func & STR_FUNC_REGEXP) {
6073 int c =
nextc(), term, func = 0;
6091 while ((c =
nextc()) != -1 && c != term) {
6103 if (func & STR_FUNC_INDENT) {
6138 line = here->nd_orig;
6142 lex_p = lex_pbeg + here->nd_nth;
6152 const char *eos,
long len,
int indent)
6158 while (*p &&
ISSPACE(*p)) p++;
6161 if (n < 0 || (n > 0 && p[len] !=
'\n' && p[len] !=
'\r'))
return FALSE;
6162 return strncmp(eos, p, len) == 0;
6169 if (!
NIL_P(parser->delayed))
6172 ripper_dispatch_ignored_scan_event(parser,
tHEREDOC_END);
6175 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser)
6177 #define dispatch_heredoc_end() ((void)0)
6183 int c,
func, indent = 0;
6184 const char *eos, *
p, *pend;
6191 indent = (func = *eos++) & STR_FUNC_INDENT;
6193 if ((c =
nextc()) == -1) {
6197 if (
NIL_P(parser->delayed)) {
6202 ((len =
lex_p - parser->tokp) > 0 &&
6203 (str =
STR_NEW3(parser->tokp, len, enc, func), 1))) {
6221 if (!(func & STR_FUNC_EXPAND)) {
6228 if (--pend == p || pend[-1] !=
'\r') {
6242 if (
nextc() == -1) {
6252 switch (c =
nextc()) {
6265 if (parser->
eofp)
goto error;
6275 if ((c =
nextc()) == -1)
goto error;
6292 rb_warning0(
"ambiguous first argument; put parentheses or even spaces");
6297 #define arg_ambiguous() (arg_ambiguous_gen(parser), 1)
6304 yyerror(
"formal argument must be local variable");
6322 if (len > 5 && name[nlen = len - 5] ==
'-') {
6326 if (len > 4 && name[nlen = len - 4] ==
'-') {
6329 if (
rb_memcicmp(name + nlen + 1,
"mac", 3) == 0 &&
6330 !(len == 8 &&
rb_memcicmp(name,
"utf8-mac", len) == 0))
6345 excargs[1] =
rb_sprintf(
"unknown encoding name: %s", name);
6362 for (i = 0; i < n; ++
i) {
6437 if (str[i-1] ==
'*' && str[i-2] ==
'-') {
6443 if (i + 1 >= len)
return 0;
6444 if (str[i+1] !=
'-') {
6447 else if (str[i-1] !=
'-') {
6465 VALUE name = 0, val = 0;
6466 const char *beg, *end, *vbeg, *vend;
6467 #define str_copy(_s, _p, _n) ((_s) \
6468 ? (void)(rb_str_resize((_s), (_n)), \
6469 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
6470 : (void)((_s) = STR_NEW((_p), (_n))))
6472 if (len <= 7)
return FALSE;
6476 len = end - beg - 3;
6487 for (; len > 0 && *str; str++, --
len) {
6489 case '\'':
case '"':
case ':':
case ';':
6494 for (beg = str; len > 0; str++, --
len) {
6496 case '\'':
case '"':
case ':':
case ';':
6504 for (end = str; len > 0 &&
ISSPACE(*str); str++, --
len);
6506 if (*str !=
':')
continue;
6508 do str++;
while (--len > 0 &&
ISSPACE(*str));
6511 for (vbeg = ++str; --len > 0 && *str !=
'"'; str++) {
6524 for (vbeg = str; len > 0 && *str !=
'"' && *str !=
';' && !
ISSPACE(*str); --
len, str++);
6527 while (len > 0 && (*str ==
';' ||
ISSPACE(*str))) --len, str++;
6532 for (i = 0; i < n; ++
i) {
6533 if (s[i] ==
'-') s[
i] =
'_';
6540 n = (*p->
length)(parser, vbeg, n);
6546 }
while (++p < magic_comments +
numberof(magic_comments));
6559 const char *beg = str;
6563 if (send - str <= 6)
return;
6565 case 'C':
case 'c': str += 6;
continue;
6566 case 'O':
case 'o': str += 5;
continue;
6567 case 'D':
case 'd': str += 4;
continue;
6568 case 'I':
case 'i': str += 3;
continue;
6569 case 'N':
case 'n': str += 2;
continue;
6570 case 'G':
case 'g': str += 1;
continue;
6584 if (++str >= send)
return;
6587 if (*str !=
'=' && *str !=
':')
return;
6592 while ((*str ==
'-' || *str ==
'_' ||
ISALNUM(*str)) && ++str < send);
6608 (
unsigned char)
lex_p[0] == 0xbb &&
6609 (
unsigned char)
lex_p[1] == 0xbf) {
6623 #define IS_ARG() (lex_state == EXPR_ARG || lex_state == EXPR_CMDARG)
6624 #define IS_END() (lex_state == EXPR_END || lex_state == EXPR_ENDARG || lex_state == EXPR_ENDFN)
6625 #define IS_BEG() (lex_state == EXPR_BEG || lex_state == EXPR_MID || lex_state == EXPR_VALUE || lex_state == EXPR_CLASS)
6626 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
6627 #define IS_LABEL_POSSIBLE() ((lex_state == EXPR_BEG && !cmd_state) || IS_ARG())
6628 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1))
6631 #define ambiguous_operator(op, syn) ( \
6632 rb_warning0("`"op"' after local variable is interpreted as binary operator"), \
6633 rb_warning0("even though it seems like "syn""))
6635 #define ambiguous_operator(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn))
6637 #define warn_balanced(op, syn) ((void) \
6638 (last_state != EXPR_CLASS && last_state != EXPR_DOT && \
6639 last_state != EXPR_FNAME && last_state != EXPR_ENDFN && \
6640 last_state != EXPR_ENDARG && \
6641 space_seen && !ISSPACE(c) && \
6642 (ambiguous_operator(op, syn), 0)))
6654 int fallthru =
FALSE;
6680 switch (c =
nextc()) {
6688 case ' ':
case '\t':
case '\f':
case '\r':
6692 while ((c =
nextc())) {
6694 case ' ':
case '\t':
case '\f':
case '\r':
6703 ripper_dispatch_scan_event(parser,
tSP);
6716 ripper_dispatch_scan_event(parser,
tCOMMENT);
6737 while ((c =
nextc())) {
6739 case ' ':
case '\t':
case '\f':
case '\r':
6744 if ((c =
nextc()) !=
'.') {
6757 parser->tokp =
lex_p;
6760 goto normal_newline;
6769 if ((c =
nextc()) ==
'*') {
6770 if ((c =
nextc()) ==
'=') {
6786 rb_warning0(
"`*' interpreted as argument prefix");
6839 ripper_dispatch_scan_event(parser,
tEMBDOC);
6848 if (c !=
'=')
continue;
6849 if (strncmp(
lex_p,
"end", 3) == 0 &&
6868 if ((c =
nextc()) ==
'=') {
6869 if ((c =
nextc()) ==
'=') {
6878 else if (c ==
'>') {
6891 (!
IS_ARG() || space_seen)) {
6893 if (token)
return token;
6902 if ((c =
nextc()) ==
'>') {
6909 if ((c =
nextc()) ==
'=') {
6928 if ((c =
nextc()) ==
'=') {
6932 if ((c =
nextc()) ==
'=') {
7000 rb_warnI(
"invalid character syntax; use ?\\%c", c2);
7017 else if (c ==
'\\') {
7046 if ((c =
nextc()) ==
'&') {
7048 if ((c =
nextc()) ==
'=') {
7056 else if (c ==
'=') {
7063 rb_warning0(
"`&' interpreted as argument prefix");
7082 if ((c =
nextc()) ==
'|') {
7084 if ((c =
nextc()) ==
'=') {
7169 if ((c =
nextc()) ==
'.') {
7170 if ((c =
nextc()) ==
'.') {
7178 yyerror(
"no .<digit> floating literal anymore; put 0 before dot");
7184 case '0':
case '1':
case '2':
case '3':
case '4':
7185 case '5':
case '6':
case '7':
case '8':
case '9':
7187 int is_float, seen_point, seen_e, nondigit;
7189 is_float = seen_point = seen_e = nondigit = 0;
7192 if (c ==
'-' || c ==
'+') {
7197 #define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0)
7200 if (c ==
'x' || c ==
'X') {
7206 if (nondigit)
break;
7213 }
while ((c =
nextc()) != -1);
7220 else if (nondigit)
goto trailing_uc;
7224 if (c ==
'b' || c ==
'B') {
7227 if (c ==
'0' || c ==
'1') {
7230 if (nondigit)
break;
7234 if (c !=
'0' && c !=
'1')
break;
7237 }
while ((c =
nextc()) != -1);
7244 else if (nondigit)
goto trailing_uc;
7248 if (c ==
'd' || c ==
'D') {
7254 if (nondigit)
break;
7261 }
while ((c =
nextc()) != -1);
7268 else if (nondigit)
goto trailing_uc;
7276 if (c ==
'o' || c ==
'O') {
7279 if (c == -1 || c ==
'_' || !
ISDIGIT(c)) {
7283 if (c >=
'0' && c <=
'7') {
7288 if (nondigit)
break;
7292 if (c < '0' || c >
'9')
break;
7293 if (c >
'7')
goto invalid_octal;
7296 }
while ((c =
nextc()) != -1);
7300 if (nondigit)
goto trailing_uc;
7309 if (c >
'7' && c <=
'9') {
7311 yyerror(
"Invalid octal digit");
7313 else if (c ==
'.' || c ==
'e' || c ==
'E') {
7325 case '0':
case '1':
case '2':
case '3':
case '4':
7326 case '5':
case '6':
case '7':
case '8':
case '9':
7332 if (nondigit)
goto trailing_uc;
7333 if (seen_point || seen_e) {
7338 if (c0 == -1 || !
ISDIGIT(c0)) {
7366 if (c !=
'-' && c !=
'+')
continue;
7372 if (nondigit)
goto decode_num;
7387 snprintf(tmp,
sizeof(tmp),
"trailing `%c' in number", nondigit);
7393 if (errno == ERANGE) {
7451 if ((c =
nextc()) ==
'=') {
7472 if ((c =
nextc()) ==
'=') {
7497 if ((c =
nextc()) !=
'@') {
7524 if ((c =
nextc()) ==
']') {
7525 if ((c =
nextc()) ==
'=') {
7537 else if (
IS_ARG() && space_seen) {
7571 ripper_dispatch_scan_event(parser,
tSP);
7592 yyerror(
"unknown type of %string");
7596 if (c == -1 || term == -1) {
7601 if (term ==
'(') term =
')';
7602 else if (term ==
'[') term =
']';
7603 else if (term ==
'{') term =
'}';
7604 else if (term ==
'<') term =
'>';
7642 yyerror(
"unknown type of %string");
7646 if ((c =
nextc()) ==
'=') {
7728 case '1':
case '2':
case '3':
7729 case '4':
case '5':
case '6':
7730 case '7':
case '8':
case '9':
7735 }
while (c != -1 &&
ISDIGIT(c));
7783 ripper_dispatch_scan_event(parser,
k__END__);
7811 if ((c ==
'!' || c ==
'?') && !
peek(
'=')) {
7831 if (
tok()[1] ==
'@')
7898 if (kw->
id[0] != kw->
id[1])
7937 yylex(
void *lval,
void *p)
7951 if (!
NIL_P(parser->delayed)) {
7952 ripper_dispatch_delayed_token(parser, t);
7956 ripper_dispatch_scan_event(parser, t);
7998 if (orig == (
NODE*)1)
return;
8007 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg))
8014 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg))
8019 NODE *end, *h = head, *nd;
8021 if (tail == 0)
return head;
8023 if (h == 0)
return tail;
8062 tail->nd_end = tail;
8064 end->nd_next = tail;
8065 h->nd_end = tail->nd_end;
8075 if (list == 0)
return NEW_LIST(item);
8076 if (list->nd_next) {
8077 last = list->nd_next->nd_end;
8085 list->nd_next->nd_end = last->nd_next;
8095 if (head->nd_next) {
8096 last = head->nd_next->nd_end;
8102 head->nd_alen += tail->nd_alen;
8103 last->nd_next = tail;
8104 if (tail->nd_next) {
8105 head->nd_next->nd_end = tail->nd_next->nd_end;
8108 head->nd_next->nd_end = tail;
8117 if (
NIL_P(tail))
return 1;
8136 if (!head)
return tail;
8137 if (!tail)
return head;
8164 tail->nd_lit = head->nd_lit;
8168 else if (
NIL_P(tail->nd_lit)) {
8169 head->nd_alen += tail->nd_alen - 1;
8170 head->nd_next->nd_end->nd_next = tail->nd_next;
8171 head->nd_next->nd_end = tail->nd_next->nd_end;
8176 tail->nd_head =
NEW_STR(tail->nd_lit);
8315 switch (
id & ID_SCOPE_MASK) {
8340 # define assignable_result(x) get_value(lhs)
8341 # define parser_yyerror(parser, x) dispatch1(assign_error, lhs)
8343 # define assignable_result(x) (x)
8347 yyerror(
"Can't change the value of self");
8350 yyerror(
"Can't assign to nil");
8353 yyerror(
"Can't assign to true");
8356 yyerror(
"Can't assign to false");
8359 yyerror(
"Can't assign to __FILE__");
8362 yyerror(
"Can't assign to __LINE__");
8365 yyerror(
"Can't assign to __ENCODING__");
8399 yyerror(
"dynamic constant assignment");
8408 #undef assignable_result
8409 #undef parser_yyerror
8412 #define LVAR_USED ((int)1 << (sizeof(int) * CHAR_BIT - 1))
8417 if (idUScore == name)
return name;
8420 yyerror(
"duplicated argument name");
8432 yyerror(
"duplicated argument name");
8471 id &= ~ID_SCOPE_MASK;
8500 if (!node2)
return node1;
8504 node1->nd_head =
arg_concat(node1->nd_head, node2);
8516 node1->nd_body =
list_concat(node1->nd_body, node2);
8525 if (!node1)
return NEW_LIST(node2);
8530 node1->nd_head =
arg_append(node1->nd_head, node2);
8563 lhs->nd_value = rhs;
8568 lhs->nd_args =
arg_append(lhs->nd_args, rhs);
8599 if (!cond)
yyerror(
"void value expression");
8604 while (node->nd_next) {
8605 node = node->nd_next;
8607 node = node->nd_head;
8611 node = node->nd_body;
8615 if (!node->nd_body) {
8616 node = node->nd_else;
8619 else if (!node->nd_else) {
8620 node = node->nd_body;
8624 node = node->nd_else;
8630 node = node->nd_2nd;
8644 const char *useless = 0;
8651 switch (node->nd_mid) {
8682 useless =
"a variable";
8685 useless =
"a constant";
8692 useless =
"a literal";
8717 useless =
"defined?";
8725 rb_warnS(
"possibly useless use of %s in void context", useless);
8738 if (!node->nd_next)
return;
8740 node = node->nd_next;
8747 NODE **n = &node, *n1 = node;
8749 *n = n1 = n1->nd_body;
8763 #define subnodes(n1, n2) \
8764 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
8765 (!node->n2) ? (body = &node->n1, 1) : \
8766 (reduce_nodes(&node->n1), body = &node->n2, 1))
8769 int newline = (int)(node->
flags & NODE_FL_NEWLINE);
8776 *body = node = node->nd_stts;
8780 *body = node = node->nd_body;
8784 body = &node->nd_end->nd_head;
8787 if (
subnodes(nd_body, nd_else))
break;
8790 body = &node->nd_body;
8793 if (!
subnodes(nd_body, nd_next))
goto end;
8796 if (!
subnodes(nd_head, nd_resq))
goto end;
8799 if (node->nd_else) {
8800 body = &node->nd_resq;
8803 if (!
subnodes(nd_head, nd_resq))
goto end;
8820 yyerror(
"multiple assignment in conditional");
8834 if (!node->nd_value)
return 1;
8835 switch (
nd_type(node->nd_value)) {
8842 parser_warn(node->nd_value,
"found = in conditional, should be ==");
8871 NODE *node, *next, *head;
8873 for (node = *rootnode; node; node = next) {
8877 next = node->nd_next;
8878 head = node->nd_head;
8881 switch (type =
nd_type(head)) {
8884 val =
rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit,
8904 if (node == 0)
return 0;
8912 return cond0(parser, node);
8918 if (!node)
return 1;
8939 if (node == 0)
return 0;
8946 rb_warn0(
"string literal in condition");
8956 node->nd_1st =
cond0(parser, node->nd_1st);
8957 node->nd_2nd =
cond0(parser, node->nd_2nd);
8962 node->nd_beg =
range_op(parser, node->nd_beg);
8963 node->nd_end =
range_op(parser, node->nd_end);
8996 if (node == 0)
return 0;
8997 return cond0(parser, node);
9005 NODE *node = left, *second;
9006 while ((second = node->nd_2nd) != 0 && (
enum node_type)
nd_type(second) == type) {
9009 node->nd_2nd =
NEW_NODE(type, second, right, 0);
9012 return NEW_NODE(type, left, right, 0);
9029 if (node->nd_next == 0) {
9030 node = node->nd_head;
9060 switch (
TYPE(node->nd_lit)) {
9080 node2->nd_head = node1;
9093 node =
NEW_ARGS(m ? m->nd_plen : 0, o);
9094 i1 = m ? m->nd_next : 0;
9099 node->nd_next->nd_next =
NEW_ARGS_AUX(p->nd_pid, p->nd_plen);
9118 if (!local->
used)
return;
9122 if (cnt != local->
vars->
pos) {
9123 rb_bug(
"local->used->pos != local->vars->pos");
9125 for (i = 0; i <
cnt; ++
i) {
9126 if (!v[i] || (u[i] & LVAR_USED))
continue;
9127 if (idUScore == v[i])
continue;
9167 for (i = 0; i <
cnt; i++) {
9168 buf[
i] = src->
tbl[
i];
9181 if (cnt <= 0)
return 0;
9219 if (used) used = used->
prev;
9230 if (i && used) used->
tbl[i-1] |= LVAR_USED;
9235 static const struct vtable *
9251 if ((tmp =
lvtbl->used) != 0) {
9267 while (
lvtbl->args != lvargs) {
9299 if (used) used->
tbl[i-1] |= LVAR_USED;
9305 if (used) used = used->
prev;
9358 "regexp encoding option '%c' differs from source encoding '%s'",
9387 int back_num,
int *back_refs,
OnigRegex regex,
void *arg0)
9392 long len = name_end -
name;
9393 const char *s = (
const char *)name;
9410 rb_warningS(
"named capture conflicts a local variable - %s",
9493 if (!node)
return node;
9497 node = node->nd_body;
9501 node = node->nd_body;
9508 prelude->nd_body = node;
9509 scope->nd_body = prelude;
9512 scope->nd_body = node;
9525 if (!node)
return node;
9529 node = node->nd_body;
9533 node = node->nd_body;
9549 prelude->nd_body = node;
9550 scope->nd_body = prelude;
9553 scope->nd_body = node;
9559 static const struct {
9585 #define op_tbl_count numberof(op_tbl)
9587 #ifndef ENABLE_SELECTOR_NAMESPACE
9588 #define ENABLE_SELECTOR_NAMESPACE 0
9595 #if ENABLE_SELECTOR_NAMESPACE
9607 #if ENABLE_SELECTOR_NAMESPACE
9614 ivar2_cmp(
struct ivar2_key *key1,
struct ivar2_key *key2)
9616 if (key1->id == key2->id && key1->klass == key2->klass) {
9623 ivar2_hash(
struct ivar2_key *
key)
9625 return (key->id << 8) ^ (key->klass >> 2);
9639 #if ENABLE_SELECTOR_NAMESPACE
9670 if (m >= e)
return 0;
9672 case '~':
case '*':
case '$':
case '?':
case '!':
case '@':
9673 case '/':
case '\\':
case ';':
case ',':
case '.':
case '=':
9674 case ':':
case '<':
case '>':
case '\"':
9675 case '&':
case '`':
case '\'':
case '+':
9693 return m == e ? mb + 1 : 0;
9711 const char *m =
name;
9712 const char *e = m +
len;
9713 int localid =
FALSE;
9715 if (!m || len <= 0)
return FALSE;
9725 if (*++m ==
'@') ++m;
9730 case '<': ++m;
break;
9731 case '=':
if (*++m ==
'>') ++m;
break;
9738 case '>':
case '=': ++m;
break;
9744 case '~': ++m;
break;
9745 case '=':
if (*++m ==
'=') ++m;
break;
9746 default:
return FALSE;
9751 if (*++m ==
'*') ++m;
9755 if (*++m ==
'@') ++m;
9758 case '|':
case '^':
case '&':
case '/':
case '%':
case '~':
case '`':
9763 if (*++m !=
']')
return FALSE;
9764 if (*++m ==
'=') ++m;
9768 if (len == 1)
return TRUE;
9770 case '=':
case '~': ++m;
break;
9771 default:
return FALSE;
9783 case '!':
case '?':
case '=': ++m;
9804 const char *m =
name;
9805 const char *e = m +
len;
9815 fake_str.as.heap.len =
len;
9816 fake_str.as.heap.ptr = (
char *)name;
9817 fake_str.as.heap.aux.capa =
len;
9818 str = (
VALUE)&fake_str;
9864 if (*
op_tbl[i].name == *m &&
9865 strcmp(
op_tbl[i].name, m) == 0) {
9872 if (m[last] ==
'=') {
9902 if (m - name < len)
id =
ID_JUNK;
9909 for (; m <= name +
len; ++m) {
9982 for (i = 0; i < op_tbl_count; i++) {
9983 if (
op_tbl[i].token ==
id) {
9997 if (
RBASIC(str)->klass == 0)
10003 ID id2 = (
id & ~ID_SCOPE_MASK) | ID_LOCAL;
10008 id2 = (
id & ~ID_SCOPE_MASK) | ID_CONST;
10015 if (
RBASIC(str)->klass == 0)
10028 if (!str)
return 0;
10130 parser->parser_ruby_sourcefile_string =
Qnil;
10131 parser->delayed =
Qnil;
10133 parser->result =
Qnil;
10134 parser->parsing_thread =
Qnil;
10135 parser->toplevel_p =
TRUE;
10144 #define parser_mark ripper_parser_mark
10145 #define parser_free ripper_parser_free
10163 rb_gc_mark(p->parser_ruby_sourcefile_string);
10185 prev = local->
prev;
10199 size_t size =
sizeof(*p);
10201 if (!ptr)
return 0;
10204 size +=
sizeof(*local);
10229 #undef rb_reserved_word
10318 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
10319 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
10320 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \
10321 (n)->u3.cnt = (c), (p))
10326 size_t cnt =
HEAPCNT(1, size);
10336 size_t cnt =
HEAPCNT(nelem, size);
10338 void *ptr =
xcalloc(nelem, size);
10347 size_t cnt =
HEAPCNT(1, size);
10349 if (ptr && (n = parser->
heap) != NULL) {
10351 if (n->
u1.
node == ptr) {
10356 }
while ((n = n->
u2.
node) != NULL);
10368 while ((n = *prev) != NULL) {
10369 if (n->
u1.
node == ptr) {
10382 #ifdef RIPPER_DEBUG
10383 extern int rb_is_pointer_to_heap(
VALUE);
10389 if (x ==
Qfalse)
return x;
10390 if (x ==
Qtrue)
return x;
10391 if (x ==
Qnil)
return x;
10396 if (!rb_is_pointer_to_heap(x))
10409 return ((
NODE *)x)->nd_rval;
10418 #define validate(x) ((x) = get_value(x))
10430 return rb_funcall(parser->value, mid, 1, a);
10438 return rb_funcall(parser->value, mid, 2, a, b);
10447 return rb_funcall(parser->value, mid, 3, a, b, c);
10457 return rb_funcall(parser->value, mid, 4, a, b, c, d);
10468 return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
10471 static const struct kw_assoc {
10474 } keyword_to_name[] = {
10527 keyword_id_to_str(
ID id)
10529 const struct kw_assoc *a;
10531 for (a = keyword_to_name; a->id; a++) {
10538 #undef ripper_id2sym
10550 if ((name = keyword_id_to_str(
id))) {
10563 rb_bug(
"cannot convert ID to string: %ld", (
unsigned long)
id);
10571 ripper_get_id(
VALUE v)
10581 ripper_get_value(
VALUE v)
10588 return nd->nd_rval;
10592 ripper_compile_error(
struct parser_params *parser,
const char *fmt, ...)
10597 va_start(args, fmt);
10604 ripper_warn0(
struct parser_params *parser,
const char *fmt)
10610 ripper_warnI(
struct parser_params *parser,
const char *fmt,
int a)
10618 ripper_warnS(
struct parser_params *parser,
const char *fmt,
const char *str)
10626 ripper_warning0(
struct parser_params *parser,
const char *fmt)
10632 ripper_warningS(
struct parser_params *parser,
const char *fmt,
const char *str)
10645 ripper_s_allocate(
VALUE klass)
10657 #define ripper_initialized_p(r) ((r)->parser_lex_input != 0)
10673 VALUE src, fname, lineno;
10676 rb_scan_args(argc, argv,
"12", &src, &fname, &lineno);
10686 if (
NIL_P(fname)) {
10694 parser->parser_ruby_sourcefile_string = fname;
10701 struct ripper_args {
10708 ripper_parse0(
VALUE parser_v)
10714 ripper_yyparse((
void*)parser);
10715 return parser->result;
10719 ripper_ensure(
VALUE parser_v)
10724 parser->parsing_thread =
Qnil;
10735 ripper_parse(
VALUE self)
10740 if (!ripper_initialized_p(parser)) {
10743 if (!
NIL_P(parser->parsing_thread)) {
10750 rb_ensure(ripper_parse0,
self, ripper_ensure,
self);
10752 return parser->result;
10763 ripper_column(
VALUE self)
10769 if (!ripper_initialized_p(parser)) {
10772 if (
NIL_P(parser->parsing_thread))
return Qnil;
10784 ripper_filename(
VALUE self)
10789 if (!ripper_initialized_p(parser)) {
10792 return parser->parser_ruby_sourcefile_string;
10803 ripper_lineno(
VALUE self)
10808 if (!ripper_initialized_p(parser)) {
10811 if (
NIL_P(parser->parsing_thread))
return Qnil;
10815 #ifdef RIPPER_DEBUG
10837 InitVM_ripper(
void)
10860 #ifdef RIPPER_DEBUG