15 #define PARSER_DEBUG 0
18 #define YYERROR_VERBOSE 1
19 #define YYSTACK_USE_ALLOCA 0
34 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
36 #define YYMALLOC(size) rb_parser_malloc(parser, (size))
37 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size))
38 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size))
39 #define YYFREE(ptr) rb_parser_free(parser, (ptr))
40 #define malloc YYMALLOC
41 #define realloc YYREALLOC
42 #define calloc YYCALLOC
48 #define REGISTER_SYMID(id, name) register_symid((id), (name), strlen(name), enc)
52 #define is_notop_id(id) ((id)>tLAST_OP_ID)
53 #define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
54 #define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL)
55 #define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE)
56 #define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET)
57 #define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
58 #define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
59 #define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK)
60 #define id_type(id) (is_notop_id(id) ? (int)((id)&ID_SCOPE_MASK) : -1)
62 #define is_asgn_or_id(id) ((is_notop_id(id)) && \
63 (((id)&ID_SCOPE_MASK) == ID_GLOBAL || \
64 ((id)&ID_SCOPE_MASK) == ID_INSTANCE || \
65 ((id)&ID_SCOPE_MASK) == ID_CLASS))
83 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
99 #define IS_lex_state_for(x, ls) ((x) & (ls))
100 #define IS_lex_state(ls) IS_lex_state_for(lex_state, (ls))
108 # define BITSTACK_PUSH(stack, n) ((stack) = ((stack)<<1)|((n)&1))
109 # define BITSTACK_POP(stack) ((stack) = (stack) >> 1)
110 # define BITSTACK_LEXPOP(stack) ((stack) = ((stack) >> 1) | ((stack) & 1))
111 # define BITSTACK_SET_P(stack) ((stack)&1)
113 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
114 #define COND_POP() BITSTACK_POP(cond_stack)
115 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
116 #define COND_P() BITSTACK_SET_P(cond_stack)
118 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
119 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
120 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
121 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
137 #define DVARS_INHERIT ((void*)1)
138 #define DVARS_TOPSCOPE NULL
139 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
140 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
183 rb_bug(
"vtable_add: vtable is not allocated (%p)", (
void *)tbl);
200 for (i = 0; i < tbl->
pos; i++) {
286 VALUE parser_ruby_sourcefile_string;
299 #define STR_NEW(p,n) rb_enc_str_new((p),(n),current_enc)
300 #define STR_NEW0() rb_enc_str_new(0,0,current_enc)
301 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),current_enc)
302 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),current_enc)
303 #define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT)
304 #define TOK_INTERN(mb) rb_intern3(tok(), toklen(), current_enc)
307 #define yyerror(msg) parser_yyerror(parser, (msg))
309 #define YYLEX_PARAM parser
311 #define lex_strterm (parser->parser_lex_strterm)
312 #define lex_state (parser->parser_lex_state)
313 #define cond_stack (parser->parser_cond_stack)
314 #define cmdarg_stack (parser->parser_cmdarg_stack)
315 #define class_nest (parser->parser_class_nest)
316 #define paren_nest (parser->parser_paren_nest)
317 #define lpar_beg (parser->parser_lpar_beg)
318 #define brace_nest (parser->parser_brace_nest)
319 #define in_single (parser->parser_in_single)
320 #define in_def (parser->parser_in_def)
321 #define compile_for_eval (parser->parser_compile_for_eval)
322 #define cur_mid (parser->parser_cur_mid)
323 #define in_defined (parser->parser_in_defined)
324 #define tokenbuf (parser->parser_tokenbuf)
325 #define tokidx (parser->parser_tokidx)
326 #define toksiz (parser->parser_toksiz)
327 #define tokline (parser->parser_tokline)
328 #define lex_input (parser->parser_lex_input)
329 #define lex_lastline (parser->parser_lex_lastline)
330 #define lex_nextline (parser->parser_lex_nextline)
331 #define lex_pbeg (parser->parser_lex_pbeg)
332 #define lex_p (parser->parser_lex_p)
333 #define lex_pend (parser->parser_lex_pend)
334 #define heredoc_end (parser->parser_heredoc_end)
335 #define command_start (parser->parser_command_start)
336 #define deferred_nodes (parser->parser_deferred_nodes)
337 #define lex_gets_ptr (parser->parser_lex_gets_ptr)
338 #define lex_gets (parser->parser_lex_gets)
339 #define lvtbl (parser->parser_lvtbl)
340 #define ruby__end__seen (parser->parser_ruby__end__seen)
341 #define ruby_sourceline (parser->parser_ruby_sourceline)
342 #define ruby_sourcefile (parser->parser_ruby_sourcefile)
343 #define current_enc (parser->enc)
344 #define yydebug (parser->parser_yydebug)
347 #define ruby_eval_tree (parser->parser_eval_tree)
348 #define ruby_eval_tree_begin (parser->parser_eval_tree_begin)
349 #define ruby_debug_lines (parser->debug_lines)
350 #define ruby_coverage (parser->coverage)
356 #define yyparse ruby_yyparse
359 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
362 #define cond(node) cond_gen(parser, (node))
364 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
372 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
373 #define void_expr0(node) void_expr_gen(parser, (node))
374 #define void_expr(node) void_expr0((node) = remove_begin(node))
376 #define void_stmts(node) void_stmts_gen(parser, (node))
378 #define reduce_nodes(n) reduce_nodes_gen(parser,(n))
380 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
383 #define block_append(h,t) block_append_gen(parser,(h),(t))
385 #define list_append(l,i) list_append_gen(parser,(l),(i))
387 #define list_concat(h,t) list_concat_gen(parser,(h),(t))
389 #define arg_append(h,t) arg_append_gen(parser,(h),(t))
391 #define arg_concat(h,t) arg_concat_gen(parser,(h),(t))
393 #define literal_concat(h,t) literal_concat_gen(parser,(h),(t))
396 #define new_evstr(n) new_evstr_gen(parser,(n))
398 #define evstr2dstr(n) evstr2dstr_gen(parser,(n))
402 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1))
404 #define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id))
407 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
409 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
413 #define ret_args(node) ret_args_gen(parser, (node))
416 #define new_yield(node) new_yield_gen(parser, (node))
418 #define dsym_node(node) dsym_node_gen(parser, (node))
421 #define gettable(id) gettable_gen(parser,(id))
423 #define assignable(id,node) assignable_gen(parser, (id), (node))
426 #define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
428 #define attrset(node,id) attrset_gen(parser, (node), (id))
431 #define rb_backref_error(n) rb_backref_error_gen(parser,(n))
433 #define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2))
437 #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (attr), (op), (rhs))
439 #define new_const_op_assign(lhs, op, rhs) new_const_op_assign_gen(parser, (lhs), (op), (rhs))
442 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
445 #define local_tbl() local_tbl_gen(parser)
450 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
452 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
454 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
456 #define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match))
458 #define get_id(id) (id)
459 #define get_value(val) (val)
461 #define value_expr(node) ((void)(node))
462 #define remove_begin(node) (node)
463 #define rb_dvar_defined(id) 0
464 #define rb_local_defined(id) 0
466 #define get_id(id) ripper_get_id(id)
468 #define get_value(val) ripper_get_value(val)
470 #define assignable(lhs,node) assignable_gen(parser, (lhs))
472 #define id_is_var(id) id_is_var_gen(parser, (id))
474 #define node_assign(node1, node2) dispatch2(assign, (node1), (node2))
478 #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs))
482 #define new_op_assign(lhs, op, rhs) new_op_assign_gen(parser, (lhs), (op), (rhs))
485 #define formal_argument(id) formal_argument_gen(parser, (id))
487 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
489 #define new_bv(id) new_bv_gen(parser, (id))
492 #define local_push(top) local_push_gen(parser,(top))
494 #define local_pop() local_pop_gen(parser)
496 #define local_var(id) local_var_gen(parser, (id))
498 #define arg_var(id) arg_var_gen(parser, (id))
500 #define local_id(id) local_id_gen(parser, (id))
502 #define internal_id() internal_id_gen(parser)
505 #define dyna_push() dyna_push_gen(parser)
507 #define dyna_pop(node) dyna_pop_gen(parser, (node))
509 #define dyna_in_block() dyna_in_block_gen(parser)
510 #define dyna_var(id) local_var(id)
512 #define dvar_defined(id) dvar_defined_gen(parser, (id), 0)
513 #define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1)
515 #define dvar_curr(id) dvar_curr_gen(parser, (id))
518 #define lvar_defined(id) lvar_defined_gen(parser, (id))
520 #define RE_OPTION_ONCE (1<<16)
521 #define RE_OPTION_ENCODING_SHIFT 8
522 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
523 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
524 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
525 #define RE_OPTION_MASK 0xff
526 #define RE_OPTION_ARG_ENCODING_NONE 32
528 #define NODE_STRTERM NODE_ZARRAY
529 #define NODE_HEREDOC NODE_ARRAY
530 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
531 #define nd_func u1.id
532 #if SIZEOF_SHORT == 2
533 #define nd_term(node) ((signed short)(node)->u2.id)
535 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
537 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
538 #define nd_nest u3.cnt
543 #define RIPPER_VERSION "0.1.0"
556 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
557 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
558 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
559 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
560 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
561 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
562 #define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
564 #define yyparse ripper_yyparse
566 #define ripper_intern(s) ID2SYM(rb_intern(s))
569 #define ripper_id2sym(id) ((id) < 256 && rb_ispunct(id) ? \
570 ID2SYM(id) : ripper_id2sym(id))
573 #define arg_new() dispatch0(args_new)
574 #define arg_add(l,a) dispatch2(args_add, (l), (a))
575 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
576 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
577 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
578 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
579 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
581 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
582 #define mrhs_new() dispatch0(mrhs_new)
583 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
584 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
586 #define mlhs_new() dispatch0(mlhs_new)
587 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
588 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
590 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
591 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
593 #define blockvar_new(p,v) dispatch2(block_var, (p), (v))
594 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a))
595 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a))
597 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
598 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
599 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
601 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
610 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
617 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
625 # define ifndef_ripper(x) (x)
628 # define ifndef_ripper(x)
632 # define rb_warn0(fmt) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt))
633 # define rb_warnI(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
634 # define rb_warnS(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
635 # define rb_warn4S(file,line,fmt,a) rb_compile_warn((file), (line), (fmt), (a))
636 # define rb_warning0(fmt) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt))
637 # define rb_warningS(fmt,a) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt), (a))
639 # define rb_warn0(fmt) ripper_warn0(parser, (fmt))
640 # define rb_warnI(fmt,a) ripper_warnI(parser, (fmt), (a))
641 # define rb_warnS(fmt,a) ripper_warnS(parser, (fmt), (a))
642 # define rb_warn4S(file,line,fmt,a) ripper_warnS(parser, (fmt), (a))
643 # define rb_warning0(fmt) ripper_warning0(parser, (fmt))
644 # define rb_warningS(fmt,a) ripper_warningS(parser, (fmt), (a))
645 static void ripper_warn0(
struct parser_params*,
const char*);
647 static void ripper_warnS(
struct parser_params*,
const char*,
const char*);
649 static void ripper_warningS(
struct parser_params*,
const char*,
const char*);
653 static void ripper_compile_error(
struct parser_params*,
const char *fmt, ...);
654 # define rb_compile_error ripper_compile_error
655 # define compile_error ripper_compile_error
656 # define PARSER_ARG parser,
658 # define rb_compile_error rb_compile_error_with_enc
659 # define compile_error parser->nerr++,rb_compile_error_with_enc
660 # define PARSER_ARG ruby_sourcefile, ruby_sourceline, current_enc,
668 #define YYMAXDEPTH 10000
675 #define token_info_push(token) (RTEST(ruby_verbose) ? token_info_push(parser, (token)) : (void)0)
676 #define token_info_pop(token) (RTEST(ruby_verbose) ? token_info_pop(parser, (token)) : (void)0)
678 #define token_info_push(token)
679 #define token_info_pop(token)
691 const struct vtable *vars;
754 %type <node> singleton strings
string string1 xstring regexp
755 %type <node> string_contents xstring_contents regexp_contents string_content
756 %type <node> words
symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
757 %type <node> literal numeric dsym cpath
758 %type <node> top_compstmt top_stmts
top_stmt
760 %type <node> expr_value arg_value primary_value fcall
761 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
763 %type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
764 %type <node>
command_args aref_args opt_block_arg block_arg var_ref var_lhs
765 %type <node> command_asgn mrhs superclass block_call block_command
766 %type <node> f_block_optarg f_block_opt
767 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
768 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
769 %type <node> block_param opt_block_param block_param_def f_opt
770 %type <node> f_kwarg f_kw f_block_kwarg f_block_kw
771 %type <node> bv_decls opt_bv_decl bvar
772 %type <node> lambda f_larglist lambda_body
776 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
866 while (node->nd_next) {
867 node = node->nd_next;
881 top_compstmt : top_stmts opt_terms
956 rb_warn0(
"else without rescue is useless");
1026 yyerror(
"BEGIN is permitted only at toplevel");
1032 '{' top_compstmt
'}'
1066 buf[1] = (char)$3->
nd_nth;
1075 yyerror(
"can't make alias for the number variables");
1236 | mlhs
'=' arg_value
1330 block_command : block_call
1409 $5->nd_iter =
NEW_CALL($1, $3, $4);
1430 $5->nd_iter =
NEW_CALL($1, $3, $4);
1645 | primary_value
'[' opt_call_args rbracket
1681 yyerror(
"dynamic constant assignment");
1693 yyerror(
"dynamic constant assignment");
1729 | primary_value
'[' opt_call_args rbracket
1765 yyerror(
"dynamic constant assignment");
1778 yyerror(
"dynamic constant assignment");
1801 yyerror(
"class/module name must be CONSTANT");
1877 | undef_list
',' {
lex_state = EXPR_FNAME;} fitem
1967 | primary_value
'[' opt_call_args rbracket
tOP_ASGN arg
2347 | args
',' assocs trailer
2365 paren_args :
'(' opt_call_args rparen
2375 opt_paren_args : none
2379 opt_call_args : none
2385 | args
',' assocs
','
2412 | args opt_block_arg
2420 | assocs opt_block_arg
2430 | args
',' assocs opt_block_arg
2460 block_arg :
tAMPER arg_value
2470 opt_block_arg :
',' block_arg
2496 | args
',' arg_value
2510 | args
',' tSTAR arg_value
2526 mrhs : args
',' arg_value
2540 | args
',' tSTAR arg_value
2741 | method_call brace_block
2755 | k_if expr_value then
2767 | k_unless expr_value then
2801 | k_case expr_value opt_terms
2812 | k_case opt_terms case_body k_end
2879 tbl[0] = 1; tbl[1] =
id;
2886 | k_class cpath superclass
2889 yyerror(
"class definition in method body");
2934 yyerror(
"module definition in method body");
2975 | k_def singleton dot_or_colon {
lex_state = EXPR_FNAME;} fname
3030 primary_value : primary
3180 f_marg_list : f_marg
3188 | f_marg_list
',' f_marg
3198 f_margs : f_marg_list
3206 | f_marg_list
',' tSTAR f_norm_arg
3215 | f_marg_list
',' tSTAR f_norm_arg
',' f_marg_list
3224 | f_marg_list
',' tSTAR
3232 | f_marg_list
',' tSTAR ',' f_marg_list
3249 |
tSTAR f_norm_arg
',' f_marg_list
3269 |
tSTAR ',' f_marg_list
3280 block_args_tail : f_block_kwarg
',' f_kwrest opt_f_block_arg
3284 | f_block_kwarg opt_f_block_arg
3288 | f_kwrest opt_f_block_arg
3298 opt_block_args_tail :
',' block_args_tail
3308 block_param : f_arg
',' f_block_optarg
',' f_rest_arg opt_block_args_tail
3312 | f_arg
',' f_block_optarg
',' f_rest_arg
',' f_arg opt_block_args_tail
3316 | f_arg
',' f_block_optarg opt_block_args_tail
3320 | f_arg
',' f_block_optarg
',' f_arg opt_block_args_tail
3324 | f_arg
',' f_rest_arg opt_block_args_tail
3336 | f_arg
',' f_rest_arg
',' f_arg opt_block_args_tail
3340 | f_arg opt_block_args_tail
3344 | f_block_optarg
',' f_rest_arg opt_block_args_tail
3348 | f_block_optarg
',' f_rest_arg
',' f_arg opt_block_args_tail
3352 | f_block_optarg opt_block_args_tail
3356 | f_block_optarg
',' f_arg opt_block_args_tail
3360 | f_rest_arg opt_block_args_tail
3364 | f_rest_arg
',' f_arg opt_block_args_tail
3374 opt_block_param : none
3381 block_param_def :
'|' opt_bv_decl
'|'
3399 |
'|' block_param opt_bv_decl
'|'
3410 opt_bv_decl : opt_nl
3414 | opt_nl
';' bv_decls opt_nl
3474 f_larglist :
'(' f_args opt_bv_decl
')'
3523 block_call : command do_block
3539 | block_call dot_or_colon operation2 opt_paren_args
3548 | block_call dot_or_colon operation2 opt_paren_args brace_block
3552 $5->nd_iter =
NEW_CALL($1, $3, $4);
3560 | block_call dot_or_colon operation2
command_args do_block
3564 $5->nd_iter =
NEW_CALL($1, $3, $4);
3574 method_call : fcall paren_args
3583 | primary_value
'.' operation2
3599 | primary_value
tCOLON2 operation2
3615 | primary_value
tCOLON2 operation3
3673 | primary_value
'[' opt_call_args rbracket
3765 exc_list : arg_value
3892 VALUE src = node->nd_lit;
3908 for (list = (prev = node)->nd_next;
list; list = list->nd_next) {
3912 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
3918 prev->nd_next = list->nd_next;
3931 if (!node->nd_next) {
3932 VALUE src = node->nd_lit;
3972 | word_list word
' '
3982 word : string_content
3990 | word string_content
4027 | symbol_list word
' '
4125 | string_contents string_content
4143 | xstring_contents string_content
4161 | regexp_contents string_content
4329 var_ref : user_variable
4352 var_lhs : user_variable
4403 f_arglist :
'(' f_args rparen
4421 args_tail : f_kwarg
',' f_kwrest opt_f_block_arg
4425 | f_kwarg opt_f_block_arg
4429 | f_kwrest opt_f_block_arg
4439 opt_args_tail :
',' args_tail
4449 f_args : f_arg
',' f_optarg
',' f_rest_arg opt_args_tail
4453 | f_arg
',' f_optarg
',' f_rest_arg
',' f_arg opt_args_tail
4457 | f_arg
',' f_optarg opt_args_tail
4461 | f_arg
',' f_optarg
',' f_arg opt_args_tail
4465 | f_arg
',' f_rest_arg opt_args_tail
4469 | f_arg
',' f_rest_arg
',' f_arg opt_args_tail
4473 | f_arg opt_args_tail
4477 | f_optarg
',' f_rest_arg opt_args_tail
4481 | f_optarg
',' f_rest_arg
',' f_arg opt_args_tail
4485 | f_optarg opt_args_tail
4489 | f_optarg
',' f_arg opt_args_tail
4493 | f_rest_arg opt_args_tail
4497 | f_rest_arg
',' f_arg opt_args_tail
4515 yyerror(
"formal argument cannot be a constant");
4524 yyerror(
"formal argument cannot be an instance variable");
4533 yyerror(
"formal argument cannot be a global variable");
4542 yyerror(
"formal argument cannot be a class variable");
4550 f_norm_arg : f_bad_arg
4558 f_arg_item : f_norm_arg
4593 | f_arg
',' f_arg_item
4618 f_block_kw :
tLABEL primary_value
4630 f_block_kwarg : f_block_kw
4638 | f_block_kwarg
',' f_block_kw
4643 while (kws->nd_next) {
4668 while (kws->nd_next) {
4718 f_block_optarg : f_block_opt
4726 | f_block_optarg
',' f_block_opt
4731 while (opts->nd_next) {
4732 opts = opts->nd_next;
4750 | f_optarg
',' f_opt
4755 while (opts->nd_next) {
4756 opts = opts->nd_next;
4774 yyerror(
"rest argument must be local variable");
4802 yyerror(
"block argument must be local variable");
4804 yyerror(
"duplicated block argument name");
4815 opt_f_block_arg :
',' f_block_arg
4843 yyerror(
"can't define singleton method for ().");
4855 yyerror(
"can't define singleton method for literals");
4896 assoc : arg_value
tASSOC arg_value
4963 rbracket : opt_nl
']'
4992 # define yylval (*((YYSTYPE*)(parser->parser_yylval)))
5001 # define nextc() parser_nextc(parser)
5002 # define pushback(c) parser_pushback(parser, (c))
5003 # define newtok() parser_newtok(parser)
5004 # define tokspace(n) parser_tokspace(parser, (n))
5005 # define tokadd(c) parser_tokadd(parser, (c))
5006 # define tok_hex(numlen) parser_tok_hex(parser, (numlen))
5007 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e))
5008 # define tokadd_escape(e) parser_tokadd_escape(parser, (e))
5009 # define regx_options() parser_regx_options(parser)
5010 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e))
5011 # define parse_string(n) parser_parse_string(parser,(n))
5012 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc))
5013 # define here_document(n) parser_here_document(parser,(n))
5014 # define heredoc_identifier() parser_heredoc_identifier(parser)
5015 # define heredoc_restore(n) parser_heredoc_restore(parser,(n))
5016 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i))
5019 # define set_yylval_str(x) (yylval.node = NEW_STR(x))
5020 # define set_yylval_num(x) (yylval.num = (x))
5021 # define set_yylval_id(x) (yylval.id = (x))
5022 # define set_yylval_name(x) (yylval.id = (x))
5023 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x))
5024 # define set_yylval_node(x) (yylval.node = (x))
5025 # define yylval_id() (yylval.id)
5028 ripper_yylval_id(
ID x)
5032 # define set_yylval_str(x) (void)(x)
5033 # define set_yylval_num(x) (void)(x)
5034 # define set_yylval_id(x) (void)(x)
5035 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
5036 # define set_yylval_literal(x) (void)(x)
5037 # define set_yylval_node(x) (void)(x)
5038 # define yylval_id() yylval.id
5042 #define ripper_flush(p) (void)(p)
5044 #define ripper_flush(p) ((p)->tokp = (p)->parser_lex_p)
5046 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5053 return lex_p > parser->tokp;
5066 ripper_dispatch_scan_event(
struct parser_params *parser,
int t)
5068 if (!ripper_has_scan_event(parser))
return;
5069 yylval_rval = ripper_scan_event_val(parser, t);
5073 ripper_dispatch_ignored_scan_event(
struct parser_params *parser,
int t)
5075 if (!ripper_has_scan_event(parser))
return;
5076 (void)ripper_scan_event_val(parser, t);
5080 ripper_dispatch_delayed_token(
struct parser_params *parser,
int t)
5083 const char *saved_tokp = parser->tokp;
5086 parser->tokp =
lex_pbeg + parser->delayed_col;
5088 parser->delayed =
Qnil;
5090 parser->tokp = saved_tokp;
5101 #undef SIGN_EXTEND_CHAR
5103 # define SIGN_EXTEND_CHAR(c) ((signed char)(c))
5106 # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
5109 #define parser_encoding_name() (current_enc->name)
5110 #define parser_mbclen() mbclen((lex_p-1),lex_pend,current_enc)
5111 #define parser_precise_mbclen() rb_enc_precise_mbclen((lex_p-1),lex_pend,current_enc)
5112 #define is_identchar(p,e,enc) (rb_enc_isalnum(*(p),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
5113 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,current_enc))
5115 #define parser_isascii() ISASCII(*(lex_p-1))
5123 for (p =
lex_pbeg; p < pend; p++) {
5125 column = (((column - 1) / 8) + 1) * 8;
5136 for (p =
lex_pbeg; p < pend; p++) {
5137 if (*p !=
' ' && *p !=
'\t') {
5144 #undef token_info_push
5161 #undef token_info_pop
5168 if (!ptinfo)
return;
5174 if (linenum == ptinfo->
linenum) {
5182 "mismatched indentations at '%s' with '%s' at %d",
5195 const int max_line_margin = 30;
5204 if (*p ==
'\n')
break;
5211 if (*pe ==
'\n')
break;
5218 const char *pre =
"", *post =
"";
5220 if (len > max_line_margin * 2 + 10) {
5221 if (
lex_p - p > max_line_margin) {
5225 if (pe -
lex_p > max_line_margin) {
5232 MEMCPY(buf, p,
char, len);
5236 i = (int)(
lex_p - p);
5237 p2 =
buf; pe = buf + len;
5240 if (*p2 !=
'\t') *p2 =
' ';
5248 dispatch1(parse_error,
STR_NEW2(msg));
5260 CONST_ID(script_lines,
"SCRIPT_LINES__");
5277 if (
RTEST(coverages) &&
RBASIC(coverages)->klass == 0) {
5281 RBASIC(lines)->klass = 0;
5283 RARRAY(lines)->as.heap.len = n;
5378 char *beg, *end, *pend;
5388 while (end < pend) {
5389 if (*end++ ==
'\n')
break;
5399 if (
NIL_P(line))
return line;
5498 #define STR_FUNC_ESCAPE 0x01
5499 #define STR_FUNC_EXPAND 0x02
5500 #define STR_FUNC_REGEXP 0x04
5501 #define STR_FUNC_QWORDS 0x08
5502 #define STR_FUNC_SYMBOL 0x10
5503 #define STR_FUNC_INDENT 0x20
5509 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
5511 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
5513 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
5533 #define lex_goto_eol(parser) ((parser)->parser_lex_p = (parser)->parser_lex_pend)
5534 #define lex_eol_p() (lex_p >= lex_pend)
5535 #define peek(c) peek_n((c), 0)
5536 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n])
5559 if (
NIL_P(parser->delayed)) {
5563 parser->tokp,
lex_pend - parser->tokp);
5565 parser->delayed_col = (int)(parser->tokp -
lex_pbeg);
5569 parser->tokp,
lex_pend - parser->tokp);
5585 c = (
unsigned char)*
lex_p++;
5586 if (c ==
'\r' &&
peek(
'\n')) {
5597 if (c == -1)
return;
5604 #define was_bol() (lex_p == lex_pbeg + 1)
5606 #define tokfix() (tokenbuf[tokidx]='\0')
5607 #define tok() tokenbuf
5608 #define toklen() tokidx
5609 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
5656 yyerror(
"invalid hex escape");
5663 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5668 int string_literal,
int symbol_literal,
int regexp_literal)
5688 yyerror(
"invalid Unicode escape");
5691 if (codepoint > 0x10ffff) {
5692 yyerror(
"invalid Unicode codepoint (too large)");
5696 if (regexp_literal) {
5699 else if (codepoint >= 0x80) {
5701 if (string_literal)
tokaddmbc(codepoint, *encp);
5703 else if (string_literal) {
5706 }
while (string_literal && (
peek(
' ') ||
peek(
'\t')));
5709 yyerror(
"unterminated Unicode escape");
5713 if (regexp_literal) {
tokadd(
'}'); }
5719 yyerror(
"invalid Unicode escape");
5723 if (regexp_literal) {
5726 else if (codepoint >= 0x80) {
5728 if (string_literal)
tokaddmbc(codepoint, *encp);
5730 else if (string_literal) {
5738 #define ESCAPE_CONTROL 1
5739 #define ESCAPE_META 2
5748 switch (c =
nextc()) {
5773 case '0':
case '1':
case '2':
case '3':
5774 case '4':
case '5':
case '6':
case '7':
5782 if (numlen == 0)
return 0;
5792 if (flags & ESCAPE_META)
goto eof;
5793 if ((c =
nextc()) !=
'-') {
5797 if ((c =
nextc()) ==
'\\') {
5798 if (
peek(
'u'))
goto eof;
5799 return read_escape(flags|ESCAPE_META, encp) | 0x80;
5801 else if (c == -1 || !
ISASCII(c))
goto eof;
5803 return ((c & 0xff) | 0x80);
5807 if ((c =
nextc()) !=
'-') {
5812 if (flags & ESCAPE_CONTROL)
goto eof;
5813 if ((c =
nextc())==
'\\') {
5814 if (
peek(
'u'))
goto eof;
5819 else if (c == -1 || !
ISASCII(c))
goto eof;
5824 yyerror(
"Invalid escape character syntax");
5847 switch (c =
nextc()) {
5851 case '0':
case '1':
case '2':
case '3':
5852 case '4':
case '5':
case '6':
case '7':
5855 if (numlen == 0)
goto eof;
5864 if (numlen == 0)
return -1;
5870 if (flags & ESCAPE_META)
goto eof;
5871 if ((c =
nextc()) !=
'-') {
5876 flags |= ESCAPE_META;
5880 if (flags & ESCAPE_CONTROL)
goto eof;
5881 if ((c =
nextc()) !=
'-') {
5889 if (flags & ESCAPE_CONTROL)
goto eof;
5891 flags |= ESCAPE_CONTROL;
5893 if ((c =
nextc()) ==
'\\') {
5896 else if (c == -1)
goto eof;
5902 yyerror(
"Invalid escape character syntax");
5969 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
5975 case '$':
case '*':
case '+':
case '.':
5976 case '?':
case '^':
case '|':
5977 case ')':
case ']':
case '}':
case '>':
5986 int func,
int term,
int paren,
long *nest,
5990 int has_nonascii = 0;
5993 static const char mixed_msg[] =
"%s mixed within %s source";
5995 #define mixed_error(enc1, enc2) if (!errbuf) { \
5996 size_t len = sizeof(mixed_msg) - 4; \
5997 len += strlen(rb_enc_name(enc1)); \
5998 len += strlen(rb_enc_name(enc2)); \
5999 errbuf = ALLOCA_N(char, len); \
6000 snprintf(errbuf, len, mixed_msg, \
6001 rb_enc_name(enc1), \
6002 rb_enc_name(enc2)); \
6005 #define mixed_escape(beg, enc1, enc2) do { \
6006 const char *pos = lex_p; \
6008 mixed_error((enc1), (enc2)); \
6012 while ((c =
nextc()) != -1) {
6013 if (paren && c == paren) {
6016 else if (c == term) {
6017 if (!nest || !*nest) {
6023 else if ((func & STR_FUNC_EXPAND) && c ==
'#' &&
lex_p <
lex_pend) {
6025 if (c2 ==
'$' || c2 ==
'@' || c2 ==
'{') {
6030 else if (c ==
'\\') {
6031 const char *beg =
lex_p - 1;
6035 if (func & STR_FUNC_QWORDS)
break;
6036 if (func & STR_FUNC_EXPAND)
continue;
6041 if (func & STR_FUNC_ESCAPE)
tokadd(c);
6045 if ((func & STR_FUNC_EXPAND) == 0) {
6050 func & STR_FUNC_SYMBOL,
6051 func & STR_FUNC_REGEXP);
6052 if (has_nonascii && enc != *encp) {
6058 if (c == -1)
return -1;
6060 if ((func & STR_FUNC_EXPAND) == 0)
tokadd(
'\\');
6063 if (func & STR_FUNC_REGEXP) {
6071 if (has_nonascii && enc != *encp) {
6076 else if (func & STR_FUNC_EXPAND) {
6078 if (func & STR_FUNC_ESCAPE)
tokadd(
'\\');
6081 else if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
6084 else if (c != term && !(paren && c == paren)) {
6101 else if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
6118 #define NEW_STRTERM(func, term, paren) \
6119 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
6125 if (!
NIL_P(parser->delayed)) {
6126 ptrdiff_t len =
lex_p - parser->tokp;
6131 parser->tokp =
lex_p;
6135 #define flush_string_content(enc) ripper_flush_string_content(parser, (enc))
6137 #define flush_string_content(enc) ((void)(enc))
6144 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6145 #define SPECIAL_PUNCT(idx) ( \
6146 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6147 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6148 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6149 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6150 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6158 #undef SPECIAL_PUNCT
6164 if (c <= 0x20 || 0x7e < c)
return 0;
6165 return (ruby_global_name_punct_bits[(c - 0x20) / 32] >> (c % 32)) & 1;
6172 const char *p =
lex_p;
6178 if ((c = *p) ==
'-') {
6187 if ((c = *p) ==
'@') {
6207 int func = (int)quote->nd_func;
6215 if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
6219 if (c == term && !quote->nd_nest) {
6220 if (func & STR_FUNC_QWORDS) {
6221 quote->nd_func = -1;
6224 if (!(func & STR_FUNC_REGEXP))
return tSTRING_END;
6233 if ((func & STR_FUNC_EXPAND) && c ==
'#') {
6243 if (func & STR_FUNC_REGEXP) {
6265 int c =
nextc(), term, func = 0;
6283 while ((c =
nextc()) != -1 && c != term) {
6295 if (func & STR_FUNC_INDENT) {
6330 line = here->nd_orig;
6334 lex_p = lex_pbeg + here->nd_nth;
6344 const char *eos,
long len,
int indent)
6350 while (*p &&
ISSPACE(*p)) p++;
6353 if (n < 0 || (n > 0 && p[len] !=
'\n' && p[len] !=
'\r'))
return FALSE;
6354 return strncmp(eos, p, len) == 0;
6361 if (!
NIL_P(parser->delayed))
6364 ripper_dispatch_ignored_scan_event(parser,
tHEREDOC_END);
6367 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser)
6369 #define dispatch_heredoc_end() ((void)0)
6375 int c,
func, indent = 0;
6376 const char *eos, *
p, *pend;
6383 indent = (func = *eos++) & STR_FUNC_INDENT;
6385 if ((c =
nextc()) == -1) {
6389 if (
NIL_P(parser->delayed)) {
6394 ((len =
lex_p - parser->tokp) > 0 &&
6395 (str =
STR_NEW3(parser->tokp, len, enc, func), 1))) {
6413 if (!(func & STR_FUNC_EXPAND)) {
6420 if (--pend == p || pend[-1] !=
'\r') {
6434 if (
nextc() == -1) {
6452 if (parser->
eofp)
goto error;
6462 if ((c =
nextc()) == -1)
goto error;
6479 rb_warning0(
"ambiguous first argument; put parentheses or even spaces");
6484 #define arg_ambiguous() (arg_ambiguous_gen(parser), 1)
6491 yyerror(
"formal argument must be local variable");
6509 if (len > 5 && name[nlen = len - 5] ==
'-') {
6513 if (len > 4 && name[nlen = len - 4] ==
'-') {
6516 if (
rb_memcicmp(name + nlen + 1,
"mac", 3) == 0 &&
6517 !(len == 8 &&
rb_memcicmp(name,
"utf8-mac", len) == 0))
6532 excargs[1] =
rb_sprintf(
"unknown encoding name: %s", name);
6549 for (i = 0; i < n; ++
i) {
6624 if (str[i-1] ==
'*' && str[i-2] ==
'-') {
6630 if (i + 1 >= len)
return 0;
6631 if (str[i+1] !=
'-') {
6634 else if (str[i-1] !=
'-') {
6652 VALUE name = 0, val = 0;
6653 const char *beg, *end, *vbeg, *vend;
6654 #define str_copy(_s, _p, _n) ((_s) \
6655 ? (void)(rb_str_resize((_s), (_n)), \
6656 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
6657 : (void)((_s) = STR_NEW((_p), (_n))))
6659 if (len <= 7)
return FALSE;
6663 len = end - beg - 3;
6674 for (; len > 0 && *str; str++, --len) {
6676 case '\'':
case '"':
case ':':
case ';':
6681 for (beg = str; len > 0; str++, --len) {
6683 case '\'':
case '"':
case ':':
case ';':
6691 for (end = str; len > 0 &&
ISSPACE(*str); str++, --len);
6693 if (*str !=
':')
continue;
6695 do str++;
while (--len > 0 &&
ISSPACE(*str));
6698 for (vbeg = ++str; --len > 0 && *str !=
'"'; str++) {
6711 for (vbeg = str; len > 0 && *str !=
'"' && *str !=
';' && !
ISSPACE(*str); --len, str++);
6714 while (len > 0 && (*str ==
';' ||
ISSPACE(*str))) --len, str++;
6719 for (i = 0; i < n; ++
i) {
6720 if (s[i] ==
'-') s[
i] =
'_';
6727 n = (*p->
length)(parser, vbeg, n);
6733 }
while (++p < magic_comments +
numberof(magic_comments));
6747 const char *beg = str;
6751 if (send - str <= 6)
return;
6753 case 'C':
case 'c': str += 6;
continue;
6754 case 'O':
case 'o': str += 5;
continue;
6755 case 'D':
case 'd': str += 4;
continue;
6756 case 'I':
case 'i': str += 3;
continue;
6757 case 'N':
case 'n': str += 2;
continue;
6758 case 'G':
case 'g': str += 1;
continue;
6772 if (++str >= send)
return;
6775 if (*str !=
'=' && *str !=
':')
return;
6780 while ((*str ==
'-' || *str ==
'_' ||
ISALNUM(*str)) && ++str < send);
6796 (
unsigned char)
lex_p[0] == 0xbb &&
6797 (
unsigned char)
lex_p[1] == 0xbf) {
6811 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
6812 #define IS_END() IS_lex_state(EXPR_END_ANY)
6813 #define IS_BEG() IS_lex_state(EXPR_BEG_ANY)
6814 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
6815 #define IS_LABEL_POSSIBLE() ((IS_lex_state(EXPR_BEG | EXPR_ENDFN) && !cmd_state) || IS_ARG())
6816 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1))
6817 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
6820 #define ambiguous_operator(op, syn) ( \
6821 rb_warning0("`"op"' after local variable is interpreted as binary operator"), \
6822 rb_warning0("even though it seems like "syn""))
6824 #define ambiguous_operator(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn))
6826 #define warn_balanced(op, syn) ((void) \
6827 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN|EXPR_ENDARG) && \
6828 space_seen && !ISSPACE(c) && \
6829 (ambiguous_operator(op, syn), 0)))
6841 int fallthru =
FALSE;
6867 switch (c =
nextc()) {
6875 case ' ':
case '\t':
case '\f':
case '\r':
6879 while ((c =
nextc())) {
6881 case ' ':
case '\t':
case '\f':
case '\r':
6890 ripper_dispatch_scan_event(parser,
tSP);
6903 ripper_dispatch_scan_event(parser,
tCOMMENT);
6908 if (
IS_lex_state(EXPR_BEG | EXPR_VALUE | EXPR_CLASS | EXPR_FNAME | EXPR_DOT)) {
6917 while ((c =
nextc())) {
6919 case ' ':
case '\t':
case '\f':
case '\r':
6924 if ((c =
nextc()) !=
'.') {
6937 parser->tokp =
lex_p;
6940 goto normal_newline;
6949 if ((c =
nextc()) ==
'*') {
6950 if ((c =
nextc()) ==
'=') {
6957 rb_warning0(
"`**' interpreted as argument prefix");
6976 rb_warning0(
"`*' interpreted as argument prefix");
7024 ripper_dispatch_scan_event(parser,
tEMBDOC);
7033 if (c !=
'=')
continue;
7034 if (strncmp(
lex_p,
"end", 3) == 0 &&
7048 if ((c =
nextc()) ==
'=') {
7049 if ((c =
nextc()) ==
'=') {
7058 else if (c ==
'>') {
7070 (!
IS_ARG() || space_seen)) {
7072 if (token)
return token;
7083 if ((c =
nextc()) ==
'>') {
7090 if ((c =
nextc()) ==
'=') {
7104 if ((c =
nextc()) ==
'=') {
7108 if ((c =
nextc()) ==
'=') {
7176 rb_warnI(
"invalid character syntax; use ?\\%c", c2);
7193 else if (c ==
'\\') {
7222 if ((c =
nextc()) ==
'&') {
7224 if ((c =
nextc()) ==
'=') {
7232 else if (c ==
'=') {
7239 rb_warning0(
"`&' interpreted as argument prefix");
7253 if ((c =
nextc()) ==
'|') {
7255 if ((c =
nextc()) ==
'=') {
7335 if ((c =
nextc()) ==
'.') {
7336 if ((c =
nextc()) ==
'.') {
7344 yyerror(
"no .<digit> floating literal anymore; put 0 before dot");
7350 case '0':
case '1':
case '2':
case '3':
case '4':
7351 case '5':
case '6':
case '7':
case '8':
case '9':
7353 int is_float, seen_point, seen_e, nondigit;
7355 is_float = seen_point = seen_e = nondigit = 0;
7358 if (c ==
'-' || c ==
'+') {
7363 #define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0)
7366 if (c ==
'x' || c ==
'X') {
7372 if (nondigit)
break;
7379 }
while ((c =
nextc()) != -1);
7386 else if (nondigit)
goto trailing_uc;
7390 if (c ==
'b' || c ==
'B') {
7393 if (c ==
'0' || c ==
'1') {
7396 if (nondigit)
break;
7400 if (c !=
'0' && c !=
'1')
break;
7403 }
while ((c =
nextc()) != -1);
7410 else if (nondigit)
goto trailing_uc;
7414 if (c ==
'd' || c ==
'D') {
7420 if (nondigit)
break;
7427 }
while ((c =
nextc()) != -1);
7434 else if (nondigit)
goto trailing_uc;
7442 if (c ==
'o' || c ==
'O') {
7445 if (c == -1 || c ==
'_' || !
ISDIGIT(c)) {
7449 if (c >=
'0' && c <=
'7') {
7454 if (nondigit)
break;
7458 if (c < '0' || c >
'9')
break;
7459 if (c >
'7')
goto invalid_octal;
7462 }
while ((c =
nextc()) != -1);
7466 if (nondigit)
goto trailing_uc;
7475 if (c >
'7' && c <=
'9') {
7477 yyerror(
"Invalid octal digit");
7479 else if (c ==
'.' || c ==
'e' || c ==
'E') {
7491 case '0':
case '1':
case '2':
case '3':
case '4':
7492 case '5':
case '6':
case '7':
case '8':
case '9':
7498 if (nondigit)
goto trailing_uc;
7499 if (seen_point || seen_e) {
7504 if (c0 == -1 || !
ISDIGIT(c0)) {
7532 if (c !=
'-' && c !=
'+')
continue;
7538 if (nondigit)
goto decode_num;
7553 snprintf(tmp,
sizeof(tmp),
"trailing `%c' in number", nondigit);
7559 if (errno == ERANGE) {
7636 if ((c =
nextc()) ==
'=') {
7656 if ((c =
nextc()) !=
'@') {
7683 if ((c =
nextc()) ==
']') {
7684 if ((c =
nextc()) ==
'=') {
7696 else if (
IS_ARG() && space_seen) {
7731 ripper_dispatch_scan_event(parser,
tSP);
7752 yyerror(
"unknown type of %string");
7756 if (c == -1 || term == -1) {
7761 if (term ==
'(') term =
')';
7762 else if (term ==
'[') term =
']';
7763 else if (term ==
'{') term =
'}';
7764 else if (term ==
'<') term =
'>';
7814 yyerror(
"unknown type of %string");
7818 if ((c =
nextc()) ==
'=') {
7895 case '1':
case '2':
case '3':
7896 case '4':
case '5':
case '6':
7897 case '7':
case '8':
case '9':
7902 }
while (c != -1 &&
ISDIGIT(c));
7948 ripper_dispatch_scan_event(parser,
k__END__);
7976 if ((c ==
'!' || c ==
'?') && !
peek(
'=')) {
7996 if (
tok()[1] ==
'@')
8034 if (mb == ENC_CODERANGE_7BIT && !
IS_lex_state(EXPR_DOT)) {
8042 if (state == EXPR_FNAME) {
8056 if (
CMDARG_P() && state != EXPR_CMDARG)
8058 if (state & (EXPR_BEG | EXPR_ENDARG))
8062 if (state & (EXPR_BEG | EXPR_VALUE))
8065 if (kw->
id[0] != kw->
id[1])
8102 yylex(
void *lval,
void *p)
8116 if (!
NIL_P(parser->delayed)) {
8117 ripper_dispatch_delayed_token(parser, t);
8121 ripper_dispatch_scan_event(parser, t);
8163 if (orig == (
NODE*)1)
return;
8172 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg))
8179 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg))
8186 if (tail == 0)
return head;
8188 if (h == 0)
return tail;
8216 if (
RTEST(ruby_verbose)) {
8227 tail->nd_end =
tail;
8229 end->nd_next =
tail;
8230 h->nd_end = tail->nd_end;
8240 if (list == 0)
return NEW_LIST(item);
8241 if (list->nd_next) {
8242 last = list->nd_next->nd_end;
8250 list->nd_next->nd_end = last->nd_next;
8260 if (head->nd_next) {
8261 last = head->nd_next->nd_end;
8267 head->nd_alen += tail->nd_alen;
8268 last->nd_next =
tail;
8269 if (tail->nd_next) {
8270 head->nd_next->nd_end = tail->nd_next->nd_end;
8273 head->nd_next->nd_end =
tail;
8282 if (
NIL_P(tail))
return 1;
8303 if (!head)
return tail;
8304 if (!tail)
return head;
8314 if (htype ==
NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8317 lit = headlast->nd_lit;
8340 tail->nd_lit = head->nd_lit;
8344 else if (
NIL_P(tail->nd_lit)) {
8346 head->nd_alen += tail->nd_alen - 1;
8347 head->nd_next->nd_end->nd_next = tail->nd_next;
8348 head->nd_next->nd_end = tail->nd_next->nd_end;
8351 else if (htype ==
NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8353 lit = headlast->nd_lit;
8356 tail->nd_lit =
Qnil;
8361 tail->nd_head =
NEW_STR(tail->nd_lit);
8427 if (
RB_TYPE_P(node1->nd_lit, T_REGEXP)) {
8440 if (
RB_TYPE_P(node2->nd_lit, T_REGEXP)) {
8492 switch (
id & ID_SCOPE_MASK) {
8511 static const char names[][12] = {
8512 "EXPR_BEG",
"EXPR_END",
"EXPR_ENDARG",
"EXPR_ENDFN",
"EXPR_ARG",
8513 "EXPR_CMDARG",
"EXPR_MID",
"EXPR_FNAME",
"EXPR_DOT",
"EXPR_CLASS",
8518 return names[
ffs(state)];
8533 # define assignable_result(x) get_value(lhs)
8534 # define parser_yyerror(parser, x) dispatch1(assign_error, lhs)
8536 # define assignable_result(x) (x)
8541 yyerror(
"Can't change the value of self");
8544 yyerror(
"Can't assign to nil");
8547 yyerror(
"Can't assign to true");
8550 yyerror(
"Can't assign to false");
8553 yyerror(
"Can't assign to __FILE__");
8556 yyerror(
"Can't assign to __LINE__");
8559 yyerror(
"Can't assign to __ENCODING__");
8593 yyerror(
"dynamic constant assignment");
8602 #undef assignable_result
8603 #undef parser_yyerror
8610 if (name == idUScore)
return 1;
8617 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
8625 yyerror(
"duplicated argument name");
8637 yyerror(
"duplicated argument name");
8676 id &= ~ID_SCOPE_MASK;
8705 if (!node2)
return node1;
8709 node1->nd_head =
arg_concat(node1->nd_head, node2);
8721 node1->nd_body =
list_concat(node1->nd_body, node2);
8730 if (!node1)
return NEW_LIST(node2);
8735 node1->nd_head =
arg_append(node1->nd_head, node2);
8768 lhs->nd_value = rhs;
8773 lhs->nd_args =
arg_append(lhs->nd_args, rhs);
8804 if (!cond)
yyerror(
"void value expression");
8809 while (node->nd_next) {
8810 node = node->nd_next;
8812 node = node->nd_head;
8816 node = node->nd_body;
8820 if (!node->nd_body) {
8821 node = node->nd_else;
8824 else if (!node->nd_else) {
8825 node = node->nd_body;
8829 node = node->nd_else;
8835 node = node->nd_2nd;
8849 const char *useless = 0;
8851 if (!
RTEST(ruby_verbose))
return;
8856 switch (node->nd_mid) {
8887 useless =
"a variable";
8890 useless =
"a constant";
8897 useless =
"a literal";
8922 useless =
"defined?";
8930 rb_warnS(
"possibly useless use of %s in void context", useless);
8938 if (!
RTEST(ruby_verbose))
return;
8943 if (!node->nd_next)
return;
8945 node = node->nd_next;
8952 NODE **n = &node, *n1 = node;
8954 *n = n1 = n1->nd_body;
8968 #define subnodes(n1, n2) \
8969 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
8970 (!node->n2) ? (body = &node->n1, 1) : \
8971 (reduce_nodes(&node->n1), body = &node->n2, 1))
8974 int newline = (int)(node->
flags & NODE_FL_NEWLINE);
8981 *body = node = node->nd_stts;
8985 *body = node = node->nd_body;
8989 body = &node->nd_end->nd_head;
8992 if (
subnodes(nd_body, nd_else))
break;
8995 body = &node->nd_body;
8998 if (!
subnodes(nd_body, nd_next))
goto end;
9001 if (!
subnodes(nd_head, nd_resq))
goto end;
9004 if (node->nd_else) {
9005 body = &node->nd_resq;
9008 if (!
subnodes(nd_head, nd_resq))
goto end;
9023 if (!node)
return 1;
9026 if (!(node = node->nd_head))
break;
9030 }
while ((node = node->nd_next) != 0);
9049 yyerror(
"multiple assignment in conditional");
9063 if (!node->nd_value)
return 1;
9066 parser_warn(node->nd_value,
"found = in conditional, should be ==");
9088 for (node = *rootnode; node; node = next) {
9092 next = node->nd_next;
9093 head = node->nd_head;
9096 switch (type =
nd_type(head)) {
9099 val =
rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit,
9119 if (node == 0)
return 0;
9127 return cond0(parser, node);
9133 if (!node)
return 1;
9154 if (node == 0)
return 0;
9161 rb_warn0(
"string literal in condition");
9171 node->nd_1st =
cond0(parser, node->nd_1st);
9172 node->nd_2nd =
cond0(parser, node->nd_2nd);
9177 node->nd_beg =
range_op(parser, node->nd_beg);
9178 node->nd_end =
range_op(parser, node->nd_end);
9184 if ((b == 1 && e == 1) || (b + e >= 2 &&
RTEST(ruby_verbose))) {
9195 if (
RB_TYPE_P(node->nd_lit, T_REGEXP)) {
9211 if (node == 0)
return 0;
9212 return cond0(parser, node);
9220 NODE *node = left, *second;
9221 while ((second = node->nd_2nd) != 0 && (
enum node_type)
nd_type(second) == type) {
9224 node->nd_2nd =
NEW_NODE(type, second, right, 0);
9227 return NEW_NODE(type, left, right, 0);
9244 if (node->nd_next == 0) {
9245 node = node->nd_head;
9266 switch (
TYPE(node->nd_lit)) {
9295 node2->nd_head = node1;
9309 args->
pre_init = m ? m->nd_next : 0;
9382 ID vid = lhs->nd_vid;
9384 lhs->nd_value = rhs;
9391 lhs->nd_value = rhs;
9445 return dispatch3(opassign, lhs, op, rhs);
9451 VALUE recv = dispatch3(field, lhs, type, attr);
9452 return dispatch3(opassign, recv, op, rhs);
9462 if (!local->
used)
return;
9466 if (cnt != local->
vars->
pos) {
9467 rb_bug(
"local->used->pos != local->vars->pos");
9469 for (i = 0; i <
cnt; ++
i) {
9470 if (!v[i] || (u[i] & LVAR_USED))
continue;
9485 local->
used = !(inherit_dvars &&
9513 for (i = 0; i <
cnt; i++) {
9514 buf[
i] = src->
tbl[
i];
9527 if (cnt <= 0)
return 0;
9565 if (used) used = used->
prev;
9576 if (i && used) used->
tbl[i-1] |= LVAR_USED;
9581 static const struct vtable *
9597 if ((tmp =
lvtbl->used) != 0) {
9613 while (
lvtbl->args != lvargs) {
9645 if (used) used->
tbl[i-1] |= LVAR_USED;
9651 if (used) used = used->
prev;
9704 "regexp encoding option '%c' differs from source encoding '%s'",
9733 int back_num,
int *back_refs,
OnigRegex regex,
void *arg0)
9738 long len = name_end -
name;
9739 const char *s = (
const char *)name;
9756 rb_warningS(
"named capture conflicts a local variable - %s",
9839 if (!node)
return node;
9843 node = node->nd_body;
9847 node = node->nd_body;
9854 prelude->nd_body = node;
9855 scope->nd_body = prelude;
9858 scope->nd_body = node;
9871 if (!node)
return node;
9875 node = node->nd_body;
9879 node = node->nd_body;
9895 prelude->nd_body = node;
9896 scope->nd_body = prelude;
9899 scope->nd_body = node;
9905 static const struct {
9932 #define op_tbl_count numberof(op_tbl)
9934 #ifndef ENABLE_SELECTOR_NAMESPACE
9935 #define ENABLE_SELECTOR_NAMESPACE 0
9942 #if ENABLE_SELECTOR_NAMESPACE
9954 #if ENABLE_SELECTOR_NAMESPACE
9961 ivar2_cmp(
struct ivar2_key *key1,
struct ivar2_key *key2)
9963 if (key1->id == key2->id && key1->klass == key2->klass) {
9970 ivar2_hash(
struct ivar2_key *
key)
9972 return (key->id << 8) ^ (key->klass >> 2);
9986 #if ENABLE_SELECTOR_NAMESPACE
9994 (void)lex_state_name(-1);
10023 if (m >= e)
return 0;
10027 else if (*m ==
'-') {
10041 return m == e ? mb + 1 : 0;
10059 const char *m =
name;
10060 const char *e = m + len;
10063 if (!m || len <= 0)
return -1;
10083 case '<': ++m;
break;
10084 case '=':
if (*++m ==
'>') ++m;
break;
10091 case '>':
case '=': ++m;
break;
10097 case '~': ++m;
break;
10098 case '=':
if (*++m ==
'=') ++m;
break;
10099 default:
return -1;
10104 if (*++m ==
'*') ++m;
10107 case '+':
case '-':
10108 if (*++m ==
'@') ++m;
10111 case '|':
case '^':
case '&':
case '/':
case '%':
case '~':
case '`':
10116 if (*++m !=
']')
return -1;
10117 if (*++m ==
'=') ++m;
10121 if (len == 1)
return ID_JUNK;
10123 case '=':
case '~': ++m;
break;
10124 default:
return -1;
10135 case '!':
case '?':
10136 if (type == ID_GLOBAL || type == ID_CLASS || type == ID_INSTANCE)
return -1;
10141 if (type != ID_CONST && type != ID_LOCAL)
return -1;
10148 return m == e ? type : -1;
10211 fake_str.as.heap.len =
len;
10212 fake_str.as.heap.ptr = (
char *)name;
10213 fake_str.as.heap.aux.capa =
len;
10214 str = (
VALUE)&fake_str;
10228 const char *
name, *m, *e;
10276 if (*
op_tbl[i].name == *m &&
10277 strcmp(
op_tbl[i].name, m) == 0) {
10284 if (m[last] ==
'=') {
10312 if (m - name < len)
id =
ID_JUNK;
10366 name[0] = (char)
id;
10374 for (i = 0; i < op_tbl_count; i++) {
10375 if (
op_tbl[i].token ==
id) {
10389 if (
RBASIC(str)->klass == 0)
10395 ID id2 = (
id & ~ID_SCOPE_MASK) | ID_LOCAL;
10400 id2 = (
id & ~ID_SCOPE_MASK) | ID_CONST;
10407 if (
RBASIC(str)->klass == 0)
10420 if (!str)
return 0;
10514 VALUE name = *namep;
10543 fake_str.as.heap.aux.capa = fake_str.as.heap.len;
10564 fake_str.as.heap.len =
len;
10565 fake_str.as.heap.ptr = (
char *)ptr;
10566 fake_str.as.heap.aux.capa =
len;
10575 fake_str.as.heap.len = len - 1;
10673 parser->parser_ruby_sourcefile_string =
Qnil;
10674 parser->delayed =
Qnil;
10676 parser->result =
Qnil;
10677 parser->parsing_thread =
Qnil;
10678 parser->toplevel_p =
TRUE;
10687 #define parser_mark ripper_parser_mark
10688 #define parser_free ripper_parser_free
10706 rb_gc_mark(p->parser_ruby_sourcefile_string);
10728 prev = local->
prev;
10742 size_t size =
sizeof(*p);
10744 if (!ptr)
return 0;
10747 size +=
sizeof(*local);
10772 #undef rb_reserved_word
10861 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
10862 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
10863 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \
10864 (n)->u3.cnt = (c), (p))
10869 size_t cnt =
HEAPCNT(1, size);
10879 size_t cnt =
HEAPCNT(nelem, size);
10881 void *ptr =
xcalloc(nelem, size);
10890 size_t cnt =
HEAPCNT(1, size);
10892 if (ptr && (n = parser->
heap) !=
NULL) {
10894 if (n->
u1.
node == ptr) {
10911 while ((n = *prev) !=
NULL) {
10912 if (n->
u1.
node == ptr) {
10925 #ifdef RIPPER_DEBUG
10926 extern int rb_is_pointer_to_heap(
VALUE);
10932 if (x == Qfalse)
return x;
10933 if (x == Qtrue)
return x;
10934 if (x == Qnil)
return x;
10939 if (!rb_is_pointer_to_heap(x))
10952 return ((
NODE *)x)->nd_rval;
10961 #define validate(x) ((x) = get_value(x))
10973 return rb_funcall(parser->value, mid, 1, a);
10981 return rb_funcall(parser->value, mid, 2, a, b);
10990 return rb_funcall(parser->value, mid, 3, a, b, c);
11000 return rb_funcall(parser->value, mid, 4, a, b, c, d);
11011 return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
11024 return rb_funcall(parser->value, mid, 7, a, b, c, d, e, f, g);
11027 static const struct kw_assoc {
11030 } keyword_to_name[] = {
11083 keyword_id_to_str(
ID id)
11085 const struct kw_assoc *a;
11087 for (a = keyword_to_name; a->id; a++) {
11094 #undef ripper_id2sym
11096 ripper_id2sym(
ID id)
11106 if ((name = keyword_id_to_str(
id))) {
11119 rb_bug(
"cannot convert ID to string: %ld", (
unsigned long)
id);
11127 ripper_get_id(
VALUE v)
11137 ripper_get_value(
VALUE v)
11140 if (v == Qundef)
return Qnil;
11144 return nd->nd_rval;
11148 ripper_compile_error(
struct parser_params *parser,
const char *fmt, ...)
11153 va_start(args, fmt);
11160 ripper_warn0(
struct parser_params *parser,
const char *fmt)
11166 ripper_warnI(
struct parser_params *parser,
const char *fmt,
int a)
11173 ripper_warnS(
struct parser_params *parser,
const char *fmt,
const char *str)
11180 ripper_warning0(
struct parser_params *parser,
const char *fmt)
11186 ripper_warningS(
struct parser_params *parser,
const char *fmt,
const char *str)
11199 ripper_s_allocate(
VALUE klass)
11211 #define ripper_initialized_p(r) ((r)->parser_lex_input != 0)
11227 VALUE src, fname, lineno;
11230 rb_scan_args(argc, argv,
"12", &src, &fname, &lineno);
11240 if (
NIL_P(fname)) {
11248 parser->parser_ruby_sourcefile_string = fname;
11255 struct ripper_args {
11262 ripper_parse0(
VALUE parser_v)
11268 ripper_yyparse((
void*)parser);
11269 return parser->result;
11273 ripper_ensure(
VALUE parser_v)
11278 parser->parsing_thread =
Qnil;
11289 ripper_parse(
VALUE self)
11294 if (!ripper_initialized_p(parser)) {
11297 if (!
NIL_P(parser->parsing_thread)) {
11304 rb_ensure(ripper_parse0,
self, ripper_ensure,
self);
11306 return parser->result;
11317 ripper_column(
VALUE self)
11323 if (!ripper_initialized_p(parser)) {
11326 if (
NIL_P(parser->parsing_thread))
return Qnil;
11338 ripper_filename(
VALUE self)
11343 if (!ripper_initialized_p(parser)) {
11346 return parser->parser_ruby_sourcefile_string;
11357 ripper_lineno(
VALUE self)
11362 if (!ripper_initialized_p(parser)) {
11365 if (
NIL_P(parser->parsing_thread))
return Qnil;
11369 #ifdef RIPPER_DEBUG
11375 if (obj == Qundef) {
11405 InitVM_ripper(
void)
11421 #ifdef RIPPER_DEBUG