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;
263 VALUE parsing_thread;
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 lex_strterm (parser->parser_lex_strterm)
281 #define lex_state (parser->parser_lex_state)
282 #define cond_stack (parser->parser_cond_stack)
283 #define cmdarg_stack (parser->parser_cmdarg_stack)
284 #define class_nest (parser->parser_class_nest)
285 #define paren_nest (parser->parser_paren_nest)
286 #define lpar_beg (parser->parser_lpar_beg)
287 #define in_single (parser->parser_in_single)
288 #define in_def (parser->parser_in_def)
289 #define compile_for_eval (parser->parser_compile_for_eval)
290 #define cur_mid (parser->parser_cur_mid)
291 #define in_defined (parser->parser_in_defined)
292 #define tokenbuf (parser->parser_tokenbuf)
293 #define tokidx (parser->parser_tokidx)
294 #define toksiz (parser->parser_toksiz)
295 #define lex_input (parser->parser_lex_input)
296 #define lex_lastline (parser->parser_lex_lastline)
297 #define lex_nextline (parser->parser_lex_nextline)
298 #define lex_pbeg (parser->parser_lex_pbeg)
299 #define lex_p (parser->parser_lex_p)
300 #define lex_pend (parser->parser_lex_pend)
301 #define heredoc_end (parser->parser_heredoc_end)
302 #define command_start (parser->parser_command_start)
303 #define deferred_nodes (parser->parser_deferred_nodes)
304 #define lex_gets_ptr (parser->parser_lex_gets_ptr)
305 #define lex_gets (parser->parser_lex_gets)
306 #define lvtbl (parser->parser_lvtbl)
307 #define ruby__end__seen (parser->parser_ruby__end__seen)
308 #define ruby_sourceline (parser->parser_ruby_sourceline)
309 #define ruby_sourcefile (parser->parser_ruby_sourcefile)
310 #define current_enc (parser->enc)
311 #define yydebug (parser->parser_yydebug)
314 #define ruby_eval_tree (parser->parser_eval_tree)
315 #define ruby_eval_tree_begin (parser->parser_eval_tree_begin)
316 #define ruby_debug_lines (parser->debug_lines)
317 #define ruby_coverage (parser->coverage)
321 static int yylex(
void*,
void*);
327 #define yyparse ruby_yyparse
330 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
333 #define cond(node) cond_gen(parser, (node))
335 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
343 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
344 #define void_expr0(node) void_expr_gen(parser, (node))
345 #define void_expr(node) void_expr0((node) = remove_begin(node))
347 #define void_stmts(node) void_stmts_gen(parser, (node))
349 #define reduce_nodes(n) reduce_nodes_gen(parser,(n))
351 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
354 #define block_append(h,t) block_append_gen(parser,(h),(t))
356 #define list_append(l,i) list_append_gen(parser,(l),(i))
358 #define list_concat(h,t) list_concat_gen(parser,(h),(t))
360 #define arg_append(h,t) arg_append_gen(parser,(h),(t))
362 #define arg_concat(h,t) arg_concat_gen(parser,(h),(t))
364 #define literal_concat(h,t) literal_concat_gen(parser,(h),(t))
367 #define new_evstr(n) new_evstr_gen(parser,(n))
369 #define evstr2dstr(n) evstr2dstr_gen(parser,(n))
373 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1))
375 #define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id))
378 #define new_args(f,o,r,p,b) new_args_gen(parser, (f),(o),(r),(p),(b))
382 #define ret_args(node) ret_args_gen(parser, (node))
385 #define new_yield(node) new_yield_gen(parser, (node))
388 #define gettable(id) gettable_gen(parser,(id))
390 #define assignable(id,node) assignable_gen(parser, (id), (node))
393 #define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
395 #define attrset(node,id) attrset_gen(parser, (node), (id))
398 #define rb_backref_error(n) rb_backref_error_gen(parser,(n))
400 #define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2))
403 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
406 #define local_tbl() local_tbl_gen(parser)
411 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
413 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
415 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
417 #define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match))
419 #define get_id(id) (id)
420 #define get_value(val) (val)
422 #define remove_begin(node) (node)
423 #define rb_dvar_defined(id) 0
424 #define rb_local_defined(id) 0
426 #define get_id(id) ripper_get_id(id)
428 #define get_value(val) ripper_get_value(val)
430 #define assignable(lhs,node) assignable_gen(parser, (lhs))
432 #define id_is_var(id) id_is_var_gen(parser, (id))
436 #define formal_argument(id) formal_argument_gen(parser, (id))
438 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
440 #define new_bv(id) new_bv_gen(parser, (id))
443 #define local_push(top) local_push_gen(parser,(top))
445 #define local_pop() local_pop_gen(parser)
447 #define local_var(id) local_var_gen(parser, (id));
449 #define arg_var(id) arg_var_gen(parser, (id))
451 #define local_id(id) local_id_gen(parser, (id))
453 #define internal_id() internal_id_gen(parser)
456 #define dyna_push() dyna_push_gen(parser)
458 #define dyna_pop(node) dyna_pop_gen(parser, (node))
460 #define dyna_in_block() dyna_in_block_gen(parser)
461 #define dyna_var(id) local_var(id)
463 #define dvar_defined(id) dvar_defined_gen(parser, (id), 0)
464 #define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1)
466 #define dvar_curr(id) dvar_curr_gen(parser, (id))
469 #define lvar_defined(id) lvar_defined_gen(parser, (id))
471 #define RE_OPTION_ONCE (1<<16)
472 #define RE_OPTION_ENCODING_SHIFT 8
473 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
474 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
475 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
476 #define RE_OPTION_MASK 0xff
477 #define RE_OPTION_ARG_ENCODING_NONE 32
479 #define NODE_STRTERM NODE_ZARRAY
480 #define NODE_HEREDOC NODE_ARRAY
481 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
482 #define nd_func u1.id
483 #if SIZEOF_SHORT == 2
484 #define nd_term(node) ((signed short)(node)->u2.id)
486 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
488 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
489 #define nd_nest u3.cnt
494 #define RIPPER_VERSION "0.1.0"
498 static ID ripper_id_gets;
507 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
508 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
509 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
510 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
511 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
512 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
514 #define yyparse ripper_yyparse
516 #define ripper_intern(s) ID2SYM(rb_intern(s))
519 #define ripper_id2sym(id) ((id) < 256 && rb_ispunct(id) ? \
520 ID2SYM(id) : ripper_id2sym(id))
523 #define arg_new() dispatch0(args_new)
524 #define arg_add(l,a) dispatch2(args_add, (l), (a))
525 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
526 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
527 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
528 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
529 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
531 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
532 #define mrhs_new() dispatch0(mrhs_new)
533 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
534 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
536 #define mlhs_new() dispatch0(mlhs_new)
537 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
538 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
540 #define params_new(pars, opts, rest, pars2, blk) \
541 dispatch5(params, (pars), (opts), (rest), (pars2), (blk))
543 #define blockvar_new(p,v) dispatch2(block_var, (p), (v))
544 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a))
545 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a))
547 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
548 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
549 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
551 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
558 # define ifndef_ripper(x) (x)
560 # define ifndef_ripper(x)
564 # define rb_warn0(fmt) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt))
565 # define rb_warnI(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
566 # define rb_warnS(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
567 # define rb_warning0(fmt) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt))
568 # define rb_warningS(fmt,a) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt), (a))
570 # define rb_warn0(fmt) ripper_warn0(parser, (fmt))
571 # define rb_warnI(fmt,a) ripper_warnI(parser, (fmt), (a))
572 # define rb_warnS(fmt,a) ripper_warnS(parser, (fmt), (a))
573 # define rb_warning0(fmt) ripper_warning0(parser, (fmt))
574 # define rb_warningS(fmt,a) ripper_warningS(parser, (fmt), (a))
575 static void ripper_warn0(
struct parser_params*,
const char*);
576 static void ripper_warnI(
struct parser_params*,
const char*,
int);
578 static void ripper_warnS(
struct parser_params*,
const char*,
const char*);
580 static void ripper_warning0(
struct parser_params*,
const char*);
581 static void ripper_warningS(
struct parser_params*,
const char*,
const char*);
585 static void ripper_compile_error(
struct parser_params*,
const char *fmt, ...);
586 # define rb_compile_error ripper_compile_error
587 # define compile_error ripper_compile_error
588 # define PARSER_ARG parser,
590 # define rb_compile_error rb_compile_error_with_enc
591 # define compile_error parser->nerr++,rb_compile_error_with_enc
592 # define PARSER_ARG ruby_sourcefile, ruby_sourceline, current_enc,
600 #define YYMAXDEPTH 10000
607 #define token_info_push(token) (RTEST(ruby_verbose) ? token_info_push(parser, (token)) : (void)0)
608 #define token_info_pop(token) (RTEST(ruby_verbose) ? token_info_pop(parser, (token)) : (void)0)
610 #define token_info_push(token)
611 #define token_info_pop(token)
624 const struct vtable *vars;
687 %type <node> singleton strings
string string1 xstring regexp
688 %type <node> string_contents xstring_contents regexp_contents string_content
689 %type <node> words qwords word_list qword_list word
690 %type <node> literal numeric dsym cpath
691 %type <node> top_compstmt top_stmts
top_stmt
693 %type <node>
expr_value arg_value primary_value
694 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
696 %type <node> paren_args opt_paren_args
697 %type <node>
command_args aref_args opt_block_arg block_arg var_ref var_lhs
698 %type <node> command_asgn mrhs superclass block_call block_command
699 %type <node> f_block_optarg f_block_opt
700 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
701 %type <node> assoc_list assocs assoc
undef_list backref string_dvar for_var
702 %type <node> block_param opt_block_param block_param_def f_opt
703 %type <node> bv_decls opt_bv_decl bvar
704 %type <node> lambda f_larglist lambda_body
708 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
801 while (node->nd_next) {
802 node = node->nd_next;
816 top_compstmt : top_stmts opt_terms
894 rb_warn0(
"else without rescue is useless");
979 buf[1] = (char)$3->nd_nth;
988 yyerror(
"can't make alias for the number variables");
1059 rb_warn0(
"END in method; use at_exit");
1084 ID vid = $1->nd_vid;
1166 yyerror(
"constant re-assignment");
1210 | mlhs
'=' arg_value
1304 block_command : block_call
1381 $5->nd_iter =
NEW_CALL($1, $3, $4);
1402 $5->nd_iter =
NEW_CALL($1, $3, $4);
1617 | primary_value
'[' opt_call_args rbracket
1653 yyerror(
"dynamic constant assignment");
1665 yyerror(
"dynamic constant assignment");
1701 | primary_value
'[' opt_call_args rbracket
1737 yyerror(
"dynamic constant assignment");
1750 yyerror(
"dynamic constant assignment");
1773 yyerror(
"class/module name must be CONSTANT");
1928 ID vid = $1->nd_vid;
1958 ID vid = $1->nd_vid;
1983 | primary_value
'[' opt_call_args rbracket
tOP_ASGN arg
2063 yyerror(
"constant re-assignment");
2074 yyerror(
"constant re-assignment");
2401 | args
',' assocs trailer
2419 paren_args :
'(' opt_call_args rparen
2429 opt_paren_args : none
2433 opt_call_args : none
2439 | args
',' assocs
','
2466 | args opt_block_arg
2474 | assocs opt_block_arg
2484 | args
',' assocs opt_block_arg
2514 block_arg :
tAMPER arg_value
2524 opt_block_arg :
',' block_arg
2550 | args
',' arg_value
2564 | args
',' tSTAR arg_value
2580 mrhs : args
',' arg_value
2594 | args
',' tSTAR arg_value
2662 rb_warning0(
"(...) interpreted as grouped expression");
2772 | operation brace_block
2784 | method_call brace_block
2856 | k_case opt_terms case_body k_end
2923 tbl[0] = 1; tbl[1] =
id;
2930 | k_class cpath superclass
2933 yyerror(
"class definition in method body");
2978 yyerror(
"module definition in method body");
3074 primary_value : primary
3224 f_marg_list : f_marg
3232 | f_marg_list
',' f_marg
3242 f_margs : f_marg_list
3250 | f_marg_list
',' tSTAR f_norm_arg
3259 | f_marg_list
',' tSTAR f_norm_arg
',' f_marg_list
3268 | f_marg_list
',' tSTAR
3276 | f_marg_list
',' tSTAR ',' f_marg_list
3293 |
tSTAR f_norm_arg
',' f_marg_list
3313 |
tSTAR ',' f_marg_list
3323 block_param : f_arg
',' f_block_optarg
',' f_rest_arg opt_f_block_arg
3331 | f_arg
',' f_block_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
3339 | f_arg
',' f_block_optarg opt_f_block_arg
3347 | f_arg
',' f_block_optarg
',' f_arg opt_f_block_arg
3355 | f_arg
',' f_rest_arg opt_f_block_arg
3372 | f_arg
',' f_rest_arg
',' f_arg opt_f_block_arg
3380 | f_arg opt_f_block_arg
3388 | f_block_optarg
',' f_rest_arg opt_f_block_arg
3396 | f_block_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
3404 | f_block_optarg opt_f_block_arg
3412 | f_block_optarg
',' f_arg opt_f_block_arg
3420 | f_rest_arg opt_f_block_arg
3428 | f_rest_arg
',' f_arg opt_f_block_arg
3446 opt_block_param : none
3453 block_param_def :
'|' opt_bv_decl
'|'
3471 |
'|' block_param opt_bv_decl
'|'
3536 $$->nd_body =
NEW_SCOPE($3->nd_head, $4);
3544 f_larglist :
'(' f_args opt_bv_decl rparen
3593 block_call : command do_block
3609 | block_call
'.' operation2 opt_paren_args
3618 | block_call
tCOLON2 operation2 opt_paren_args
3629 method_call : operation paren_args
3638 | primary_value
'.' operation2 opt_paren_args
3648 | primary_value
tCOLON2 operation2 paren_args
3658 | primary_value
tCOLON2 operation3
3666 | primary_value
'.' paren_args
3677 | primary_value
tCOLON2 paren_args
3704 | primary_value
'[' opt_call_args rbracket
3796 exc_list : arg_value
3923 VALUE src = node->nd_lit;
3939 for (list = (prev = node)->nd_next;
list; list = list->nd_next) {
3941 VALUE tail = list->nd_head->nd_lit;
3943 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
3949 prev->nd_next = list->nd_next;
3962 if (!node->nd_next) {
3963 VALUE src = node->nd_lit;
4003 | word_list word
' '
4013 word : string_content
4021 | word string_content
4076 | string_contents string_content
4094 | xstring_contents string_content
4112 | regexp_contents string_content
4115 NODE *head = $1, *tail = $2;
4294 var_ref : user_variable
4317 var_lhs : user_variable
4367 f_arglist :
'(' f_args rparen
4385 f_args : f_arg
',' f_optarg
',' f_rest_arg opt_f_block_arg
4393 | f_arg
',' f_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
4401 | f_arg
',' f_optarg opt_f_block_arg
4409 | f_arg
',' f_optarg
',' f_arg opt_f_block_arg
4417 | f_arg
',' f_rest_arg opt_f_block_arg
4425 | f_arg
',' f_rest_arg
',' f_arg opt_f_block_arg
4433 | f_arg opt_f_block_arg
4441 | f_optarg
',' f_rest_arg opt_f_block_arg
4449 | f_optarg
',' f_rest_arg
',' f_arg opt_f_block_arg
4457 | f_optarg opt_f_block_arg
4465 | f_optarg
',' f_arg opt_f_block_arg
4473 | f_rest_arg opt_f_block_arg
4481 | f_rest_arg
',' f_arg opt_f_block_arg
4510 yyerror(
"formal argument cannot be a constant");
4519 yyerror(
"formal argument cannot be an instance variable");
4528 yyerror(
"formal argument cannot be a global variable");
4537 yyerror(
"formal argument cannot be a class variable");
4545 f_norm_arg : f_bad_arg
4553 f_arg_item : f_norm_arg
4588 | f_arg
',' f_arg_item
4625 f_block_optarg : f_block_opt
4633 | f_block_optarg
',' f_block_opt
4638 while (opts->nd_next) {
4639 opts = opts->nd_next;
4657 | f_optarg
',' f_opt
4662 while (opts->nd_next) {
4663 opts = opts->nd_next;
4681 yyerror(
"rest argument must be local variable");
4709 yyerror(
"block argument must be local variable");
4711 yyerror(
"duplicated block argument name");
4722 opt_f_block_arg :
',' f_block_arg
4750 yyerror(
"can't define singleton method for ().");
4762 yyerror(
"can't define singleton method for literals");
4803 assoc : arg_value
tASSOC arg_value
4860 rbracket : opt_nl
']'
4889 # define yylval (*((YYSTYPE*)(parser->parser_yylval)))
4898 # define nextc() parser_nextc(parser)
4899 # define pushback(c) parser_pushback(parser, (c))
4900 # define newtok() parser_newtok(parser)
4901 # define tokspace(n) parser_tokspace(parser, (n))
4902 # define tokadd(c) parser_tokadd(parser, (c))
4903 # define tok_hex(numlen) parser_tok_hex(parser, (numlen))
4904 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e))
4905 # define tokadd_escape(e) parser_tokadd_escape(parser, (e))
4906 # define regx_options() parser_regx_options(parser)
4907 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e))
4908 # define parse_string(n) parser_parse_string(parser,(n))
4909 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc))
4910 # define here_document(n) parser_here_document(parser,(n))
4911 # define heredoc_identifier() parser_heredoc_identifier(parser)
4912 # define heredoc_restore(n) parser_heredoc_restore(parser,(n))
4913 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i))
4916 # define set_yylval_str(x) (yylval.node = NEW_STR(x))
4917 # define set_yylval_num(x) (yylval.num = (x))
4918 # define set_yylval_id(x) (yylval.id = (x))
4919 # define set_yylval_name(x) (yylval.id = (x))
4920 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x))
4921 # define set_yylval_node(x) (yylval.node = (x))
4922 # define yylval_id() (yylval.id)
4925 ripper_yylval_id(
ID x)
4929 # define set_yylval_str(x) (void)(x)
4930 # define set_yylval_num(x) (void)(x)
4931 # define set_yylval_id(x) (void)(x)
4932 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
4933 # define set_yylval_literal(x) (void)(x)
4934 # define set_yylval_node(x) (void)(x)
4935 # define yylval_id() yylval.id
4939 #define ripper_flush(p) (void)(p)
4941 #define ripper_flush(p) ((p)->tokp = (p)->parser_lex_p)
4943 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
4950 return lex_p > parser->tokp;
4963 ripper_dispatch_scan_event(
struct parser_params *parser,
int t)
4965 if (!ripper_has_scan_event(parser))
return;
4966 yylval_rval = ripper_scan_event_val(parser, t);
4970 ripper_dispatch_ignored_scan_event(
struct parser_params *parser,
int t)
4972 if (!ripper_has_scan_event(parser))
return;
4973 (void)ripper_scan_event_val(parser, t);
4977 ripper_dispatch_delayed_token(
struct parser_params *parser,
int t)
4980 const char *saved_tokp = parser->tokp;
4983 parser->tokp =
lex_pbeg + parser->delayed_col;
4985 parser->delayed =
Qnil;
4987 parser->tokp = saved_tokp;
4998 #undef SIGN_EXTEND_CHAR
5000 # define SIGN_EXTEND_CHAR(c) ((signed char)(c))
5003 # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
5006 #define parser_encoding_name() (parser->enc->name)
5007 #define parser_mbclen() mbclen((lex_p-1),lex_pend,parser->enc)
5008 #define parser_precise_mbclen() rb_enc_precise_mbclen((lex_p-1),lex_pend,parser->enc)
5009 #define is_identchar(p,e,enc) (rb_enc_isalnum(*(p),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
5010 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,parser->enc))
5012 #define parser_isascii() ISASCII(*(lex_p-1))
5020 for (p =
lex_pbeg; p < pend; p++) {
5022 column = (((column - 1) / 8) + 1) * 8;
5033 for (p =
lex_pbeg; p < pend; p++) {
5034 if (*p !=
' ' && *p !=
'\t') {
5041 #undef token_info_push
5058 #undef token_info_pop
5065 if (!ptinfo)
return;
5071 if (linenum == ptinfo->
linenum) {
5079 "mismatched indentations at '%s' with '%s' at %d",
5092 const int max_line_margin = 30;
5101 if (*p ==
'\n')
break;
5108 if (*pe ==
'\n')
break;
5115 const char *pre =
"", *post =
"";
5117 if (len > max_line_margin * 2 + 10) {
5118 if (
lex_p - p > max_line_margin) {
5122 if (pe -
lex_p > max_line_margin) {
5129 MEMCPY(buf, p,
char, len);
5133 i = (int)(
lex_p - p);
5134 p2 =
buf; pe = buf +
len;
5137 if (*p2 !=
'\t') *p2 =
' ';
5145 dispatch1(parse_error,
STR_NEW2(msg));
5157 CONST_ID(script_lines,
"SCRIPT_LINES__");
5174 if (
RTEST(coverages) &&
RBASIC(coverages)->klass == 0) {
5178 RBASIC(lines)->klass = 0;
5180 RARRAY(lines)->as.heap.len = n;
5263 char *beg, *end, *pend;
5273 while (end < pend) {
5274 if (*end++ ==
'\n')
break;
5284 if (
NIL_P(line))
return line;
5385 #define STR_FUNC_ESCAPE 0x01
5386 #define STR_FUNC_EXPAND 0x02
5387 #define STR_FUNC_REGEXP 0x04
5388 #define STR_FUNC_QWORDS 0x08
5389 #define STR_FUNC_SYMBOL 0x10
5390 #define STR_FUNC_INDENT 0x20
5396 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
5398 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
5400 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
5420 #define lex_goto_eol(parser) ((parser)->parser_lex_p = (parser)->parser_lex_pend)
5421 #define lex_eol_p() (lex_p >= lex_pend)
5422 #define peek(c) peek_n((c), 0)
5423 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n])
5446 if (
NIL_P(parser->delayed)) {
5450 parser->tokp,
lex_pend - parser->tokp);
5452 parser->delayed_col = (int)(parser->tokp -
lex_pbeg);
5456 parser->tokp,
lex_pend - parser->tokp);
5472 c = (
unsigned char)*
lex_p++;
5473 if (c ==
'\r' &&
peek(
'\n')) {
5484 if (c == -1)
return;
5491 #define was_bol() (lex_p == lex_pbeg + 1)
5493 #define tokfix() (tokenbuf[tokidx]='\0')
5494 #define tok() tokenbuf
5495 #define toklen() tokidx
5496 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
5542 yyerror(
"invalid hex escape");
5549 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5553 int string_literal,
int symbol_literal,
int regexp_literal)
5573 yyerror(
"invalid Unicode escape");
5576 if (codepoint > 0x10ffff) {
5577 yyerror(
"invalid Unicode codepoint (too large)");
5581 if (regexp_literal) {
5584 else if (codepoint >= 0x80) {
5586 if (string_literal)
tokaddmbc(codepoint, *encp);
5588 else if (string_literal) {
5591 }
while (string_literal && (
peek(
' ') ||
peek(
'\t')));
5594 yyerror(
"unterminated Unicode escape");
5598 if (regexp_literal) {
tokadd(
'}'); }
5604 yyerror(
"invalid Unicode escape");
5608 if (regexp_literal) {
5611 else if (codepoint >= 0x80) {
5613 if (string_literal)
tokaddmbc(codepoint, *encp);
5615 else if (string_literal) {
5623 #define ESCAPE_CONTROL 1
5624 #define ESCAPE_META 2
5633 switch (c =
nextc()) {
5658 case '0':
case '1':
case '2':
case '3':
5659 case '4':
case '5':
case '6':
case '7':
5667 if (numlen == 0)
return 0;
5677 if (flags & ESCAPE_META)
goto eof;
5678 if ((c =
nextc()) !=
'-') {
5682 if ((c =
nextc()) ==
'\\') {
5683 if (
peek(
'u'))
goto eof;
5684 return read_escape(flags|ESCAPE_META, encp) | 0x80;
5686 else if (c == -1 || !
ISASCII(c))
goto eof;
5688 return ((c & 0xff) | 0x80);
5692 if ((c =
nextc()) !=
'-') {
5697 if (flags & ESCAPE_CONTROL)
goto eof;
5698 if ((c =
nextc())==
'\\') {
5699 if (
peek(
'u'))
goto eof;
5704 else if (c == -1 || !
ISASCII(c))
goto eof;
5709 yyerror(
"Invalid escape character syntax");
5732 switch (c =
nextc()) {
5736 case '0':
case '1':
case '2':
case '3':
5737 case '4':
case '5':
case '6':
case '7':
5740 if (numlen == 0)
goto eof;
5749 if (numlen == 0)
return -1;
5755 if (flags & ESCAPE_META)
goto eof;
5756 if ((c =
nextc()) !=
'-') {
5761 flags |= ESCAPE_META;
5765 if (flags & ESCAPE_CONTROL)
goto eof;
5766 if ((c =
nextc()) !=
'-') {
5774 if (flags & ESCAPE_CONTROL)
goto eof;
5776 flags |= ESCAPE_CONTROL;
5778 if ((c =
nextc()) ==
'\\') {
5781 else if (c == -1)
goto eof;
5787 yyerror(
"Invalid escape character syntax");
5856 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
5860 int func,
int term,
int paren,
long *nest,
5864 int has_nonascii = 0;
5867 static const char mixed_msg[] =
"%s mixed within %s source";
5869 #define mixed_error(enc1, enc2) if (!errbuf) { \
5870 size_t len = sizeof(mixed_msg) - 4; \
5871 len += strlen(rb_enc_name(enc1)); \
5872 len += strlen(rb_enc_name(enc2)); \
5873 errbuf = ALLOCA_N(char, len); \
5874 snprintf(errbuf, len, mixed_msg, \
5875 rb_enc_name(enc1), \
5876 rb_enc_name(enc2)); \
5879 #define mixed_escape(beg, enc1, enc2) do { \
5880 const char *pos = lex_p; \
5882 mixed_error((enc1), (enc2)); \
5886 while ((c =
nextc()) != -1) {
5887 if (paren && c == paren) {
5890 else if (c == term) {
5891 if (!nest || !*nest) {
5897 else if ((func & STR_FUNC_EXPAND) && c ==
'#' &&
lex_p <
lex_pend) {
5899 if (c2 ==
'$' || c2 ==
'@' || c2 ==
'{') {
5904 else if (c ==
'\\') {
5905 const char *beg =
lex_p - 1;
5909 if (func & STR_FUNC_QWORDS)
break;
5910 if (func & STR_FUNC_EXPAND)
continue;
5915 if (func & STR_FUNC_ESCAPE)
tokadd(c);
5919 if ((func & STR_FUNC_EXPAND) == 0) {
5924 func & STR_FUNC_SYMBOL,
5925 func & STR_FUNC_REGEXP);
5926 if (has_nonascii && enc != *encp) {
5932 if (c == -1)
return -1;
5934 if ((func & STR_FUNC_EXPAND) == 0)
tokadd(
'\\');
5937 if (func & STR_FUNC_REGEXP) {
5941 if (has_nonascii && enc != *encp) {
5946 else if (func & STR_FUNC_EXPAND) {
5948 if (func & STR_FUNC_ESCAPE)
tokadd(
'\\');
5951 else if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
5954 else if (c != term && !(paren && c == paren)) {
5971 else if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
5988 #define NEW_STRTERM(func, term, paren) \
5989 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
5995 if (!
NIL_P(parser->delayed)) {
5996 ptrdiff_t len =
lex_p - parser->tokp;
6001 parser->tokp =
lex_p;
6005 #define flush_string_content(enc) ripper_flush_string_content(parser, (enc))
6007 #define flush_string_content(enc) ((void)(enc))
6014 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6015 #define SPECIAL_PUNCT(idx) ( \
6016 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6017 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6018 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6019 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6020 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6028 #undef SPECIAL_PUNCT
6034 if (c <= 0x20 || 0x7e < c)
return 0;
6035 return (ruby_global_name_punct_bits[(c - 0x20) / 32] >> (c % 32)) & 1;
6042 const char *p =
lex_p;
6048 if ((c = *p) ==
'-') {
6057 if ((c = *p) ==
'@') {
6077 int func = (int)quote->nd_func;
6085 if ((func & STR_FUNC_QWORDS) &&
ISSPACE(c)) {
6089 if (c == term && !quote->nd_nest) {
6090 if (func & STR_FUNC_QWORDS) {
6091 quote->nd_func = -1;
6094 if (!(func & STR_FUNC_REGEXP))
return tSTRING_END;
6103 if ((func & STR_FUNC_EXPAND) && c ==
'#') {
6113 if (func & STR_FUNC_REGEXP) {
6135 int c =
nextc(), term, func = 0;
6153 while ((c =
nextc()) != -1 && c != term) {
6165 if (func & STR_FUNC_INDENT) {
6200 line = here->nd_orig;
6204 lex_p = lex_pbeg + here->nd_nth;
6214 const char *eos,
long len,
int indent)
6220 while (*p &&
ISSPACE(*p)) p++;
6223 if (n < 0 || (n > 0 && p[len] !=
'\n' && p[len] !=
'\r'))
return FALSE;
6224 return strncmp(eos, p, len) == 0;
6231 if (!
NIL_P(parser->delayed))
6234 ripper_dispatch_ignored_scan_event(parser,
tHEREDOC_END);
6237 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser)
6239 #define dispatch_heredoc_end() ((void)0)
6245 int c,
func, indent = 0;
6246 const char *eos, *
p, *pend;
6253 indent = (func = *eos++) & STR_FUNC_INDENT;
6255 if ((c =
nextc()) == -1) {
6259 if (
NIL_P(parser->delayed)) {
6264 ((len =
lex_p - parser->tokp) > 0 &&
6265 (str =
STR_NEW3(parser->tokp, len, enc, func), 1))) {
6283 if (!(func & STR_FUNC_EXPAND)) {
6290 if (--pend == p || pend[-1] !=
'\r') {
6304 if (
nextc() == -1) {
6322 if (parser->
eofp)
goto error;
6332 if ((c =
nextc()) == -1)
goto error;
6349 rb_warning0(
"ambiguous first argument; put parentheses or even spaces");
6354 #define arg_ambiguous() (arg_ambiguous_gen(parser), 1)
6361 yyerror(
"formal argument must be local variable");
6379 if (len > 5 && name[nlen = len - 5] ==
'-') {
6383 if (len > 4 && name[nlen = len - 4] ==
'-') {
6386 if (
rb_memcicmp(name + nlen + 1,
"mac", 3) == 0 &&
6387 !(len == 8 &&
rb_memcicmp(name,
"utf8-mac", len) == 0))
6402 excargs[1] =
rb_sprintf(
"unknown encoding name: %s", name);
6419 for (i = 0; i < n; ++
i) {
6494 if (str[i-1] ==
'*' && str[i-2] ==
'-') {
6500 if (i + 1 >= len)
return 0;
6501 if (str[i+1] !=
'-') {
6504 else if (str[i-1] !=
'-') {
6522 VALUE name = 0, val = 0;
6523 const char *beg, *end, *vbeg, *vend;
6524 #define str_copy(_s, _p, _n) ((_s) \
6525 ? (void)(rb_str_resize((_s), (_n)), \
6526 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
6527 : (void)((_s) = STR_NEW((_p), (_n))))
6529 if (len <= 7)
return FALSE;
6533 len = end - beg - 3;
6544 for (; len > 0 && *str; str++, --
len) {
6546 case '\'':
case '"':
case ':':
case ';':
6551 for (beg = str; len > 0; str++, --
len) {
6553 case '\'':
case '"':
case ':':
case ';':
6561 for (end = str; len > 0 &&
ISSPACE(*str); str++, --
len);
6563 if (*str !=
':')
continue;
6565 do str++;
while (--len > 0 &&
ISSPACE(*str));
6568 for (vbeg = ++str; --len > 0 && *str !=
'"'; str++) {
6581 for (vbeg = str; len > 0 && *str !=
'"' && *str !=
';' && !
ISSPACE(*str); --
len, str++);
6584 while (len > 0 && (*str ==
';' ||
ISSPACE(*str))) --len, str++;
6589 for (i = 0; i < n; ++
i) {
6590 if (s[i] ==
'-') s[
i] =
'_';
6597 n = (*p->
length)(parser, vbeg, n);
6603 }
while (++p < magic_comments +
numberof(magic_comments));
6617 const char *beg = str;
6621 if (send - str <= 6)
return;
6623 case 'C':
case 'c': str += 6;
continue;
6624 case 'O':
case 'o': str += 5;
continue;
6625 case 'D':
case 'd': str += 4;
continue;
6626 case 'I':
case 'i': str += 3;
continue;
6627 case 'N':
case 'n': str += 2;
continue;
6628 case 'G':
case 'g': str += 1;
continue;
6642 if (++str >= send)
return;
6645 if (*str !=
'=' && *str !=
':')
return;
6650 while ((*str ==
'-' || *str ==
'_' ||
ISALNUM(*str)) && ++str < send);
6666 (
unsigned char)
lex_p[0] == 0xbb &&
6667 (
unsigned char)
lex_p[1] == 0xbf) {
6681 #define IS_ARG() (lex_state == EXPR_ARG || lex_state == EXPR_CMDARG)
6682 #define IS_END() (lex_state == EXPR_END || lex_state == EXPR_ENDARG || lex_state == EXPR_ENDFN)
6683 #define IS_BEG() (lex_state == EXPR_BEG || lex_state == EXPR_MID || lex_state == EXPR_VALUE || lex_state == EXPR_CLASS)
6684 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
6685 #define IS_LABEL_POSSIBLE() ((lex_state == EXPR_BEG && !cmd_state) || IS_ARG())
6686 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1))
6689 #define ambiguous_operator(op, syn) ( \
6690 rb_warning0("`"op"' after local variable is interpreted as binary operator"), \
6691 rb_warning0("even though it seems like "syn""))
6693 #define ambiguous_operator(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn))
6695 #define warn_balanced(op, syn) ((void) \
6696 (last_state != EXPR_CLASS && last_state != EXPR_DOT && \
6697 last_state != EXPR_FNAME && last_state != EXPR_ENDFN && \
6698 last_state != EXPR_ENDARG && \
6699 space_seen && !ISSPACE(c) && \
6700 (ambiguous_operator(op, syn), 0)))
6712 int fallthru =
FALSE;
6738 switch (c =
nextc()) {
6746 case ' ':
case '\t':
case '\f':
case '\r':
6750 while ((c =
nextc())) {
6752 case ' ':
case '\t':
case '\f':
case '\r':
6761 ripper_dispatch_scan_event(parser,
tSP);
6774 ripper_dispatch_scan_event(parser,
tCOMMENT);
6795 while ((c =
nextc())) {
6797 case ' ':
case '\t':
case '\f':
case '\r':
6802 if ((c =
nextc()) !=
'.') {
6815 parser->tokp =
lex_p;
6818 goto normal_newline;
6827 if ((c =
nextc()) ==
'*') {
6828 if ((c =
nextc()) ==
'=') {
6844 rb_warning0(
"`*' interpreted as argument prefix");
6897 ripper_dispatch_scan_event(parser,
tEMBDOC);
6906 if (c !=
'=')
continue;
6907 if (strncmp(
lex_p,
"end", 3) == 0 &&
6926 if ((c =
nextc()) ==
'=') {
6927 if ((c =
nextc()) ==
'=') {
6936 else if (c ==
'>') {
6949 (!
IS_ARG() || space_seen)) {
6951 if (token)
return token;
6960 if ((c =
nextc()) ==
'>') {
6967 if ((c =
nextc()) ==
'=') {
6986 if ((c =
nextc()) ==
'=') {
6990 if ((c =
nextc()) ==
'=') {
7058 rb_warnI(
"invalid character syntax; use ?\\%c", c2);
7075 else if (c ==
'\\') {
7104 if ((c =
nextc()) ==
'&') {
7106 if ((c =
nextc()) ==
'=') {
7114 else if (c ==
'=') {
7121 rb_warning0(
"`&' interpreted as argument prefix");
7140 if ((c =
nextc()) ==
'|') {
7142 if ((c =
nextc()) ==
'=') {
7227 if ((c =
nextc()) ==
'.') {
7228 if ((c =
nextc()) ==
'.') {
7236 yyerror(
"no .<digit> floating literal anymore; put 0 before dot");
7242 case '0':
case '1':
case '2':
case '3':
case '4':
7243 case '5':
case '6':
case '7':
case '8':
case '9':
7245 int is_float, seen_point, seen_e, nondigit;
7247 is_float = seen_point = seen_e = nondigit = 0;
7250 if (c ==
'-' || c ==
'+') {
7255 #define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0)
7258 if (c ==
'x' || c ==
'X') {
7264 if (nondigit)
break;
7271 }
while ((c =
nextc()) != -1);
7278 else if (nondigit)
goto trailing_uc;
7282 if (c ==
'b' || c ==
'B') {
7285 if (c ==
'0' || c ==
'1') {
7288 if (nondigit)
break;
7292 if (c !=
'0' && c !=
'1')
break;
7295 }
while ((c =
nextc()) != -1);
7302 else if (nondigit)
goto trailing_uc;
7306 if (c ==
'd' || c ==
'D') {
7312 if (nondigit)
break;
7319 }
while ((c =
nextc()) != -1);
7326 else if (nondigit)
goto trailing_uc;
7334 if (c ==
'o' || c ==
'O') {
7337 if (c == -1 || c ==
'_' || !
ISDIGIT(c)) {
7341 if (c >=
'0' && c <=
'7') {
7346 if (nondigit)
break;
7350 if (c < '0' || c >
'9')
break;
7351 if (c >
'7')
goto invalid_octal;
7354 }
while ((c =
nextc()) != -1);
7358 if (nondigit)
goto trailing_uc;
7367 if (c >
'7' && c <=
'9') {
7369 yyerror(
"Invalid octal digit");
7371 else if (c ==
'.' || c ==
'e' || c ==
'E') {
7383 case '0':
case '1':
case '2':
case '3':
case '4':
7384 case '5':
case '6':
case '7':
case '8':
case '9':
7390 if (nondigit)
goto trailing_uc;
7391 if (seen_point || seen_e) {
7396 if (c0 == -1 || !
ISDIGIT(c0)) {
7424 if (c !=
'-' && c !=
'+')
continue;
7430 if (nondigit)
goto decode_num;
7445 snprintf(tmp,
sizeof(tmp),
"trailing `%c' in number", nondigit);
7451 if (errno == ERANGE) {
7509 if ((c =
nextc()) ==
'=') {
7530 if ((c =
nextc()) ==
'=') {
7555 if ((c =
nextc()) !=
'@') {
7582 if ((c =
nextc()) ==
']') {
7583 if ((c =
nextc()) ==
'=') {
7595 else if (
IS_ARG() && space_seen) {
7629 ripper_dispatch_scan_event(parser,
tSP);
7650 yyerror(
"unknown type of %string");
7654 if (c == -1 || term == -1) {
7659 if (term ==
'(') term =
')';
7660 else if (term ==
'[') term =
']';
7661 else if (term ==
'{') term =
'}';
7662 else if (term ==
'<') term =
'>';
7700 yyerror(
"unknown type of %string");
7704 if ((c =
nextc()) ==
'=') {
7786 case '1':
case '2':
case '3':
7787 case '4':
case '5':
case '6':
7788 case '7':
case '8':
case '9':
7793 }
while (c != -1 &&
ISDIGIT(c));
7839 ripper_dispatch_scan_event(parser,
k__END__);
7867 if ((c ==
'!' || c ==
'?') && !
peek(
'=')) {
7887 if (
tok()[1] ==
'@')
7954 if (kw->
id[0] != kw->
id[1])
7993 yylex(
void *lval,
void *p)
8007 if (!
NIL_P(parser->delayed)) {
8008 ripper_dispatch_delayed_token(parser, t);
8012 ripper_dispatch_scan_event(parser, t);
8054 if (orig == (
NODE*)1)
return;
8063 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg))
8070 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg))
8075 NODE *end, *h = head, *nd;
8077 if (tail == 0)
return head;
8079 if (h == 0)
return tail;
8118 tail->nd_end = tail;
8120 end->nd_next = tail;
8121 h->nd_end = tail->nd_end;
8131 if (list == 0)
return NEW_LIST(item);
8132 if (list->nd_next) {
8133 last = list->nd_next->nd_end;
8141 list->nd_next->nd_end = last->nd_next;
8151 if (head->nd_next) {
8152 last = head->nd_next->nd_end;
8158 head->nd_alen += tail->nd_alen;
8159 last->nd_next = tail;
8160 if (tail->nd_next) {
8161 head->nd_next->nd_end = tail->nd_next->nd_end;
8164 head->nd_next->nd_end = tail;
8173 if (
NIL_P(tail))
return 1;
8192 if (!head)
return tail;
8193 if (!tail)
return head;
8220 tail->nd_lit = head->nd_lit;
8224 else if (
NIL_P(tail->nd_lit)) {
8225 head->nd_alen += tail->nd_alen - 1;
8226 head->nd_next->nd_end->nd_next = tail->nd_next;
8227 head->nd_next->nd_end = tail->nd_next->nd_end;
8232 tail->nd_head =
NEW_STR(tail->nd_lit);
8371 switch (
id & ID_SCOPE_MASK) {
8396 # define assignable_result(x) get_value(lhs)
8397 # define parser_yyerror(parser, x) dispatch1(assign_error, lhs)
8399 # define assignable_result(x) (x)
8403 yyerror(
"Can't change the value of self");
8406 yyerror(
"Can't assign to nil");
8409 yyerror(
"Can't assign to true");
8412 yyerror(
"Can't assign to false");
8415 yyerror(
"Can't assign to __FILE__");
8418 yyerror(
"Can't assign to __LINE__");
8421 yyerror(
"Can't assign to __ENCODING__");
8455 yyerror(
"dynamic constant assignment");
8464 #undef assignable_result
8465 #undef parser_yyerror
8468 #define LVAR_USED ((int)1 << (sizeof(int) * CHAR_BIT - 1))
8473 if (idUScore == name)
return name;
8476 yyerror(
"duplicated argument name");
8488 yyerror(
"duplicated argument name");
8527 id &= ~ID_SCOPE_MASK;
8556 if (!node2)
return node1;
8560 node1->nd_head =
arg_concat(node1->nd_head, node2);
8572 node1->nd_body =
list_concat(node1->nd_body, node2);
8581 if (!node1)
return NEW_LIST(node2);
8586 node1->nd_head =
arg_append(node1->nd_head, node2);
8619 lhs->nd_value = rhs;
8624 lhs->nd_args =
arg_append(lhs->nd_args, rhs);
8655 if (!cond)
yyerror(
"void value expression");
8660 while (node->nd_next) {
8661 node = node->nd_next;
8663 node = node->nd_head;
8667 node = node->nd_body;
8671 if (!node->nd_body) {
8672 node = node->nd_else;
8675 else if (!node->nd_else) {
8676 node = node->nd_body;
8680 node = node->nd_else;
8686 node = node->nd_2nd;
8700 const char *useless = 0;
8707 switch (node->nd_mid) {
8738 useless =
"a variable";
8741 useless =
"a constant";
8748 useless =
"a literal";
8773 useless =
"defined?";
8781 rb_warnS(
"possibly useless use of %s in void context", useless);
8794 if (!node->nd_next)
return;
8796 node = node->nd_next;
8803 NODE **n = &node, *n1 = node;
8805 *n = n1 = n1->nd_body;
8819 #define subnodes(n1, n2) \
8820 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
8821 (!node->n2) ? (body = &node->n1, 1) : \
8822 (reduce_nodes(&node->n1), body = &node->n2, 1))
8825 int newline = (int)(node->
flags & NODE_FL_NEWLINE);
8832 *body = node = node->nd_stts;
8836 *body = node = node->nd_body;
8840 body = &node->nd_end->nd_head;
8843 if (
subnodes(nd_body, nd_else))
break;
8846 body = &node->nd_body;
8849 if (!
subnodes(nd_body, nd_next))
goto end;
8852 if (!
subnodes(nd_head, nd_resq))
goto end;
8855 if (node->nd_else) {
8856 body = &node->nd_resq;
8859 if (!
subnodes(nd_head, nd_resq))
goto end;
8876 yyerror(
"multiple assignment in conditional");
8890 if (!node->nd_value)
return 1;
8891 switch (
nd_type(node->nd_value)) {
8898 parser_warn(node->nd_value,
"found = in conditional, should be ==");
8927 NODE *node, *next, *head;
8929 for (node = *rootnode; node; node = next) {
8933 next = node->nd_next;
8934 head = node->nd_head;
8937 switch (type =
nd_type(head)) {
8940 val =
rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit,
8960 if (node == 0)
return 0;
8968 return cond0(parser, node);
8974 if (!node)
return 1;
8995 if (node == 0)
return 0;
9002 rb_warn0(
"string literal in condition");
9012 node->nd_1st =
cond0(parser, node->nd_1st);
9013 node->nd_2nd =
cond0(parser, node->nd_2nd);
9018 node->nd_beg =
range_op(parser, node->nd_beg);
9019 node->nd_end =
range_op(parser, node->nd_end);
9052 if (node == 0)
return 0;
9053 return cond0(parser, node);
9061 NODE *node = left, *second;
9062 while ((second = node->nd_2nd) != 0 && (
enum node_type)
nd_type(second) == type) {
9065 node->nd_2nd =
NEW_NODE(type, second, right, 0);
9068 return NEW_NODE(type, left, right, 0);
9085 if (node->nd_next == 0) {
9086 node = node->nd_head;
9116 switch (
TYPE(node->nd_lit)) {
9136 node2->nd_head = node1;
9149 node =
NEW_ARGS(m ? m->nd_plen : 0, o);
9150 i1 = m ? m->nd_next : 0;
9155 node->nd_next->nd_next =
NEW_ARGS_AUX(p->nd_pid, p->nd_plen);
9174 if (!local->
used)
return;
9178 if (cnt != local->
vars->
pos) {
9179 rb_bug(
"local->used->pos != local->vars->pos");
9181 for (i = 0; i <
cnt; ++
i) {
9182 if (!v[i] || (u[i] & LVAR_USED))
continue;
9183 if (idUScore == v[i])
continue;
9223 for (i = 0; i <
cnt; i++) {
9224 buf[
i] = src->
tbl[
i];
9237 if (cnt <= 0)
return 0;
9275 if (used) used = used->
prev;
9286 if (i && used) used->
tbl[i-1] |= LVAR_USED;
9291 static const struct vtable *
9307 if ((tmp =
lvtbl->used) != 0) {
9323 while (
lvtbl->args != lvargs) {
9355 if (used) used->
tbl[i-1] |= LVAR_USED;
9361 if (used) used = used->
prev;
9414 "regexp encoding option '%c' differs from source encoding '%s'",
9443 int back_num,
int *back_refs,
OnigRegex regex,
void *arg0)
9448 long len = name_end -
name;
9449 const char *s = (
const char *)name;
9466 rb_warningS(
"named capture conflicts a local variable - %s",
9549 if (!node)
return node;
9553 node = node->nd_body;
9557 node = node->nd_body;
9564 prelude->nd_body = node;
9565 scope->nd_body = prelude;
9568 scope->nd_body = node;
9581 if (!node)
return node;
9585 node = node->nd_body;
9589 node = node->nd_body;
9605 prelude->nd_body = node;
9606 scope->nd_body = prelude;
9609 scope->nd_body = node;
9615 static const struct {
9639 #define op_tbl_count numberof(op_tbl)
9641 #ifndef ENABLE_SELECTOR_NAMESPACE
9642 #define ENABLE_SELECTOR_NAMESPACE 0
9649 #if ENABLE_SELECTOR_NAMESPACE
9661 #if ENABLE_SELECTOR_NAMESPACE
9668 ivar2_cmp(
struct ivar2_key *key1,
struct ivar2_key *key2)
9670 if (key1->id == key2->id && key1->klass == key2->klass) {
9677 ivar2_hash(
struct ivar2_key *
key)
9679 return (key->id << 8) ^ (key->klass >> 2);
9693 #if ENABLE_SELECTOR_NAMESPACE
9724 if (m >= e)
return 0;
9728 else if (*m ==
'-') {
9742 return m == e ? mb + 1 : 0;
9760 const char *m =
name;
9761 const char *e = m +
len;
9762 int localid =
FALSE;
9764 if (!m || len <= 0)
return FALSE;
9774 if (*++m ==
'@') ++m;
9779 case '<': ++m;
break;
9780 case '=':
if (*++m ==
'>') ++m;
break;
9787 case '>':
case '=': ++m;
break;
9793 case '~': ++m;
break;
9794 case '=':
if (*++m ==
'=') ++m;
break;
9795 default:
return FALSE;
9800 if (*++m ==
'*') ++m;
9804 if (*++m ==
'@') ++m;
9807 case '|':
case '^':
case '&':
case '/':
case '%':
case '~':
case '`':
9812 if (*++m !=
']')
return FALSE;
9813 if (*++m ==
'=') ++m;
9817 if (len == 1)
return TRUE;
9819 case '=':
case '~': ++m;
break;
9820 default:
return FALSE;
9832 case '!':
case '?':
case '=': ++m;
9853 const char *m =
name;
9854 const char *e = m +
len;
9864 fake_str.as.heap.len =
len;
9865 fake_str.as.heap.ptr = (
char *)name;
9866 fake_str.as.heap.aux.capa =
len;
9867 str = (
VALUE)&fake_str;
9913 if (*
op_tbl[i].name == *m &&
9914 strcmp(
op_tbl[i].name, m) == 0) {
9921 if (m[last] ==
'=') {
9951 if (m - name < len)
id =
ID_JUNK;
9958 for (; m <= name +
len; ++m) {
10023 name[0] = (char)
id;
10031 for (i = 0; i < op_tbl_count; i++) {
10032 if (
op_tbl[i].token ==
id) {
10046 if (
RBASIC(str)->klass == 0)
10052 ID id2 = (
id & ~ID_SCOPE_MASK) | ID_LOCAL;
10057 id2 = (
id & ~ID_SCOPE_MASK) | ID_CONST;
10064 if (
RBASIC(str)->klass == 0)
10077 if (!str)
return 0;
10179 parser->parser_ruby_sourcefile_string =
Qnil;
10180 parser->delayed =
Qnil;
10182 parser->result =
Qnil;
10183 parser->parsing_thread =
Qnil;
10184 parser->toplevel_p =
TRUE;
10193 #define parser_mark ripper_parser_mark
10194 #define parser_free ripper_parser_free
10212 rb_gc_mark(p->parser_ruby_sourcefile_string);
10234 prev = local->
prev;
10248 size_t size =
sizeof(*p);
10250 if (!ptr)
return 0;
10253 size +=
sizeof(*local);
10278 #undef rb_reserved_word
10367 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
10368 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
10369 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \
10370 (n)->u3.cnt = (c), (p))
10375 size_t cnt =
HEAPCNT(1, size);
10385 size_t cnt =
HEAPCNT(nelem, size);
10387 void *ptr =
xcalloc(nelem, size);
10396 size_t cnt =
HEAPCNT(1, size);
10398 if (ptr && (n = parser->
heap) != NULL) {
10400 if (n->
u1.
node == ptr) {
10405 }
while ((n = n->
u2.
node) != NULL);
10417 while ((n = *prev) != NULL) {
10418 if (n->
u1.
node == ptr) {
10431 #ifdef RIPPER_DEBUG
10432 extern int rb_is_pointer_to_heap(
VALUE);
10438 if (x ==
Qfalse)
return x;
10439 if (x ==
Qtrue)
return x;
10440 if (x ==
Qnil)
return x;
10445 if (!rb_is_pointer_to_heap(x))
10458 return ((
NODE *)x)->nd_rval;
10467 #define validate(x) ((x) = get_value(x))
10479 return rb_funcall(parser->value, mid, 1, a);
10487 return rb_funcall(parser->value, mid, 2, a, b);
10496 return rb_funcall(parser->value, mid, 3, a, b, c);
10506 return rb_funcall(parser->value, mid, 4, a, b, c, d);
10517 return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
10520 static const struct kw_assoc {
10523 } keyword_to_name[] = {
10576 keyword_id_to_str(
ID id)
10578 const struct kw_assoc *a;
10580 for (a = keyword_to_name; a->id; a++) {
10587 #undef ripper_id2sym
10599 if ((name = keyword_id_to_str(
id))) {
10612 rb_bug(
"cannot convert ID to string: %ld", (
unsigned long)
id);
10620 ripper_get_id(
VALUE v)
10630 ripper_get_value(
VALUE v)
10637 return nd->nd_rval;
10641 ripper_compile_error(
struct parser_params *parser,
const char *fmt, ...)
10646 va_start(args, fmt);
10653 ripper_warn0(
struct parser_params *parser,
const char *fmt)
10659 ripper_warnI(
struct parser_params *parser,
const char *fmt,
int a)
10667 ripper_warnS(
struct parser_params *parser,
const char *fmt,
const char *str)
10675 ripper_warning0(
struct parser_params *parser,
const char *fmt)
10681 ripper_warningS(
struct parser_params *parser,
const char *fmt,
const char *str)
10694 ripper_s_allocate(
VALUE klass)
10706 #define ripper_initialized_p(r) ((r)->parser_lex_input != 0)
10722 VALUE src, fname, lineno;
10725 rb_scan_args(argc, argv,
"12", &src, &fname, &lineno);
10735 if (
NIL_P(fname)) {
10743 parser->parser_ruby_sourcefile_string = fname;
10750 struct ripper_args {
10757 ripper_parse0(
VALUE parser_v)
10763 ripper_yyparse((
void*)parser);
10764 return parser->result;
10768 ripper_ensure(
VALUE parser_v)
10773 parser->parsing_thread =
Qnil;
10784 ripper_parse(
VALUE self)
10789 if (!ripper_initialized_p(parser)) {
10792 if (!
NIL_P(parser->parsing_thread)) {
10799 rb_ensure(ripper_parse0,
self, ripper_ensure,
self);
10801 return parser->result;
10812 ripper_column(
VALUE self)
10818 if (!ripper_initialized_p(parser)) {
10821 if (
NIL_P(parser->parsing_thread))
return Qnil;
10833 ripper_filename(
VALUE self)
10838 if (!ripper_initialized_p(parser)) {
10841 return parser->parser_ruby_sourcefile_string;
10852 ripper_lineno(
VALUE self)
10857 if (!ripper_initialized_p(parser)) {
10860 if (
NIL_P(parser->parsing_thread))
return Qnil;
10864 #ifdef RIPPER_DEBUG
10886 InitVM_ripper(
void)
10909 #ifdef RIPPER_DEBUG