1 /**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
15 # error needs pure parser
18 #define PARSER_DEBUG 0
21 #define YYERROR_VERBOSE 1
22 #define YYSTACK_USE_ALLOCA 0
24 #include "ruby/ruby.h"
26 #include "ruby/encoding.h"
37 #ifndef WARN_PAST_SCOPE
38 # define WARN_PAST_SCOPE 0
43 #define YYMALLOC(size) rb_parser_malloc(parser, (size))
44 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size))
45 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size))
46 #define YYFREE(ptr) rb_parser_free(parser, (ptr))
47 #define YYFPRINTF rb_parser_printf
48 #if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL
49 # define YY_LOCATION_PRINT(File, Loc) \
50 rb_parser_printf(parser, "%d.%d-%d.%d", \
51 (Loc).first_line, (Loc).first_column, \
52 (Loc).last_line, (Loc).last_column)
58 #define malloc YYMALLOC
59 #define realloc YYREALLOC
60 #define calloc YYCALLOC
64 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
65 EXPR_END_bit, /* newline significant, +/- is an operator. */
66 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
67 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
68 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
69 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
70 EXPR_MID_bit, /* newline significant, +/- is an operator. */
71 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
72 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
73 EXPR_CLASS_bit, /* immediate after `class', no here document. */
74 EXPR_LABEL_bit, /* flag bit, label is allowed. */
75 EXPR_LABELED_bit, /* flag bit, just after a label. */
76 EXPR_FITEM_bit, /* symbol literal as FNAME. */
79 /* examine combinations */
81 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
95 EXPR_VALUE = EXPR_BEG,
96 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
97 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
98 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN)
100 #define IS_lex_state_for(x, ls) ((x) & (ls))
101 #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
102 #define IS_lex_state(ls) IS_lex_state_for(lex_state, (ls))
103 #define IS_lex_state_all(ls) IS_lex_state_all_for(lex_state, (ls))
105 # define SET_LEX_STATE(ls) \
106 (lex_state = (yydebug ? trace_lex_state(lex_state, (ls), __LINE__) : \
107 (enum lex_state_e)(ls)))
108 static enum lex_state_e trace_lex_state(enum lex_state_e from, enum lex_state_e to, int line);
110 typedef VALUE stack_type;
112 static void show_bitstack(stack_type, const char *, int);
113 # define SHOW_BITSTACK(stack, name) (yydebug ? show_bitstack(stack, name, __LINE__) : (void)0)
114 # define BITSTACK_PUSH(stack, n) (((stack) = ((stack)<<1)|((n)&1)), SHOW_BITSTACK(stack, #stack"(push)"))
115 # define BITSTACK_POP(stack) (((stack) = (stack) >> 1), SHOW_BITSTACK(stack, #stack"(pop)"))
116 # define BITSTACK_LEXPOP(stack) (((stack) = ((stack) >> 1) | ((stack) & 1)), SHOW_BITSTACK(stack, #stack"(lexpop)"))
117 # define BITSTACK_SET_P(stack) (SHOW_BITSTACK(stack, #stack), (stack)&1)
118 # define BITSTACK_SET(stack, n) ((stack)=(n), SHOW_BITSTACK(stack, #stack"(set)"))
120 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
121 #define COND_POP() BITSTACK_POP(cond_stack)
122 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
123 #define COND_P() BITSTACK_SET_P(cond_stack)
124 #define COND_SET(n) BITSTACK_SET(cond_stack, (n))
126 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
127 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
128 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
129 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
130 #define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
146 struct local_vars *prev;
150 #define DVARS_INHERIT ((void*)1)
151 #define DVARS_TOPSCOPE NULL
152 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
153 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
156 vtable_size(const struct vtable *tbl)
158 if (POINTER_P(tbl)) {
168 static struct vtable *
169 vtable_alloc(struct vtable *prev)
171 struct vtable *tbl = ALLOC(struct vtable);
174 tbl->tbl = ALLOC_N(ID, tbl->capa);
176 if (VTBL_DEBUG) printf("vtable_alloc: %p\n", (void *)tbl);
181 vtable_free(struct vtable *tbl)
183 if (VTBL_DEBUG)printf("vtable_free: %p\n", (void *)tbl);
184 if (POINTER_P(tbl)) {
193 vtable_add(struct vtable *tbl, ID id)
195 if (!POINTER_P(tbl)) {
196 rb_bug("vtable_add: vtable is not allocated (%p)", (void *)tbl);
198 if (VTBL_DEBUG) printf("vtable_add: %p, %"PRIsVALUE"\n", (void *)tbl, rb_id2str(id));
200 if (tbl->pos == tbl->capa) {
201 tbl->capa = tbl->capa * 2;
202 REALLOC_N(tbl->tbl, ID, tbl->capa);
204 tbl->tbl[tbl->pos++] = id;
209 vtable_pop(struct vtable *tbl, int n)
211 if (tbl->pos < n) rb_bug("vtable_pop: unreachable");
217 vtable_included(const struct vtable * tbl, ID id)
221 if (POINTER_P(tbl)) {
222 for (i = 0; i < tbl->pos; i++) {
223 if (tbl->tbl[i] == id) {
231 typedef struct token_info {
236 struct token_info *next;
240 Structure of Lexer Buffer:
242 lex_pbeg tokp lex_p lex_pend
244 |-----------+--------------+------------|
248 struct parser_params {
255 VALUE (*gets)(struct parser_params*,VALUE);
263 enum lex_state_e state;
268 stack_type cond_stack;
269 stack_type cmdarg_stack;
275 int heredoc_line_indent;
277 struct local_vars *lvtbl;
279 int ruby_sourceline; /* current line no. */
280 char *ruby_sourcefile; /* current source file */
281 VALUE ruby_sourcefile_string;
283 token_info *token_info;
284 VALUE compile_option;
290 unsigned int command_start:1;
291 unsigned int eofp: 1;
292 unsigned int ruby__end__seen: 1;
293 unsigned int yydebug: 1;
294 unsigned int has_shebang: 1;
295 unsigned int in_defined: 1;
296 unsigned int in_main: 1;
297 unsigned int in_kwarg: 1;
298 unsigned int in_single: 1;
299 unsigned int in_def: 1;
300 unsigned int token_seen: 1;
301 unsigned int token_info_enabled: 1;
303 unsigned int past_scope_enabled: 1;
305 unsigned int error_p: 1;
306 unsigned int cr_seen: 1;
311 NODE *eval_tree_begin;
316 const struct rb_block *base_block;
327 VALUE parsing_thread;
332 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
334 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
337 #define STR_NEW(p,n) rb_enc_str_new((p),(n),current_enc)
338 #define STR_NEW0() rb_enc_str_new(0,0,current_enc)
339 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),current_enc)
340 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),current_enc)
341 #define TOK_INTERN() intern_cstr(tok(), toklen(), current_enc)
343 static int parser_yyerror(struct parser_params*, const char*);
344 #define yyerror(msg) parser_yyerror(parser, (msg))
346 #define lex_strterm (parser->lex.strterm)
347 #define lex_state (parser->lex.state)
348 #define cond_stack (parser->cond_stack)
349 #define cmdarg_stack (parser->cmdarg_stack)
350 #define paren_nest (parser->lex.paren_nest)
351 #define lpar_beg (parser->lex.lpar_beg)
352 #define brace_nest (parser->lex.brace_nest)
353 #define in_single (parser->in_single)
354 #define in_def (parser->in_def)
355 #define in_main (parser->in_main)
356 #define in_defined (parser->in_defined)
357 #define tokenbuf (parser->tokenbuf)
358 #define tokidx (parser->tokidx)
359 #define toksiz (parser->toksiz)
360 #define tokline (parser->tokline)
361 #define lex_input (parser->lex.input)
362 #define lex_lastline (parser->lex.lastline)
363 #define lex_nextline (parser->lex.nextline)
364 #define lex_pbeg (parser->lex.pbeg)
365 #define lex_p (parser->lex.pcur)
366 #define lex_pend (parser->lex.pend)
367 #define heredoc_end (parser->heredoc_end)
368 #define heredoc_indent (parser->heredoc_indent)
369 #define heredoc_line_indent (parser->heredoc_line_indent)
370 #define command_start (parser->command_start)
371 #define lex_gets_ptr (parser->lex.gets_ptr)
372 #define lex_gets (parser->lex.gets)
373 #define lvtbl (parser->lvtbl)
374 #define ruby__end__seen (parser->ruby__end__seen)
375 #define ruby_sourceline (parser->ruby_sourceline)
376 #define ruby_sourcefile (parser->ruby_sourcefile)
377 #define ruby_sourcefile_string (parser->ruby_sourcefile_string)
378 #define current_enc (parser->enc)
379 #define current_arg (parser->cur_arg)
380 #define yydebug (parser->yydebug)
382 #define compile_for_eval (0)
384 #define compile_for_eval (parser->base_block != 0 && !in_main)
385 #define ruby_eval_tree (parser->eval_tree)
386 #define ruby_eval_tree_begin (parser->eval_tree_begin)
387 #define ruby_debug_lines (parser->debug_lines)
388 #define ruby_coverage (parser->coverage)
391 #define CALL_Q_P(q) ((q) == tANDDOT)
392 #define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
393 #define NEW_QCALL(q,r,m,a) NEW_NODE(NODE_CALL_Q(q),r,m,a)
395 #define lambda_beginning_p() (lpar_beg && lpar_beg == paren_nest)
397 static int yylex(YYSTYPE*, struct parser_params*);
400 #define yyparse ruby_yyparse
402 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE);
403 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
405 static NODE *cond_gen(struct parser_params*,NODE*,int);
406 #define cond(node) cond_gen(parser, (node), FALSE)
407 #define method_cond(node) cond_gen(parser, (node), TRUE)
408 static NODE *new_if_gen(struct parser_params*,NODE*,NODE*,NODE*);
409 #define new_if(cc,left,right) new_if_gen(parser, (cc), (left), (right))
410 #define new_unless(cc,left,right) new_if_gen(parser, (cc), (right), (left))
411 static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*);
412 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
414 static NODE *newline_node(NODE*);
415 static void fixpos(NODE*,NODE*);
417 static int value_expr_gen(struct parser_params*,NODE*);
418 static void void_expr_gen(struct parser_params*,NODE*);
419 static NODE *remove_begin(NODE*);
420 static NODE *remove_begin_all(NODE*);
421 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
422 #define void_expr0(node) void_expr_gen(parser, (node))
423 #define void_expr(node) void_expr0((node) = remove_begin(node))
424 static void void_stmts_gen(struct parser_params*,NODE*);
425 #define void_stmts(node) void_stmts_gen(parser, (node))
426 static void reduce_nodes_gen(struct parser_params*,NODE**);
427 #define reduce_nodes(n) reduce_nodes_gen(parser,(n))
428 static void block_dup_check_gen(struct parser_params*,NODE*,NODE*);
429 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
431 static NODE *block_append_gen(struct parser_params*,NODE*,NODE*);
432 #define block_append(h,t) block_append_gen(parser,(h),(t))
433 static NODE *list_append_gen(struct parser_params*,NODE*,NODE*);
434 #define list_append(l,i) list_append_gen(parser,(l),(i))
435 static NODE *list_concat(NODE*,NODE*);
436 static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*);
437 #define arg_append(h,t) arg_append_gen(parser,(h),(t))
438 static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*);
439 #define arg_concat(h,t) arg_concat_gen(parser,(h),(t))
440 static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*);
441 #define literal_concat(h,t) literal_concat_gen(parser,(h),(t))
442 static int literal_concat0(struct parser_params *, VALUE, VALUE);
443 static NODE *new_evstr_gen(struct parser_params*,NODE*);
444 #define new_evstr(n) new_evstr_gen(parser,(n))
445 static NODE *evstr2dstr_gen(struct parser_params*,NODE*);
446 #define evstr2dstr(n) evstr2dstr_gen(parser,(n))
447 static NODE *splat_array(NODE*);
449 static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*);
450 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1))
451 static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID);
452 #define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id))
454 static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*);
455 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
456 static NODE *new_args_tail_gen(struct parser_params*,NODE*,ID,ID);
457 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
458 #define new_kw_arg(k) ((k) ? NEW_KW_ARG(0, (k)) : 0)
460 static VALUE negate_lit(VALUE);
461 static NODE *ret_args_gen(struct parser_params*,NODE*);
462 #define ret_args(node) ret_args_gen(parser, (node))
463 static NODE *arg_blk_pass(NODE*,NODE*);
464 static NODE *new_yield_gen(struct parser_params*,NODE*);
465 #define new_yield(node) new_yield_gen(parser, (node))
466 static NODE *dsym_node_gen(struct parser_params*,NODE*);
467 #define dsym_node(node) dsym_node_gen(parser, (node))
469 static NODE *gettable_gen(struct parser_params*,ID);
470 #define gettable(id) gettable_gen(parser,(id))
471 static NODE *assignable_gen(struct parser_params*,ID,NODE*);
472 #define assignable(id,node) assignable_gen(parser, (id), (node))
474 static NODE *aryset_gen(struct parser_params*,NODE*,NODE*);
475 #define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
476 static NODE *attrset_gen(struct parser_params*,NODE*,ID,ID);
477 #define attrset(node,q,id) attrset_gen(parser, (node), (q), (id))
479 static void rb_backref_error_gen(struct parser_params*,NODE*);
480 #define rb_backref_error(n) rb_backref_error_gen(parser,(n))
481 static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*);
482 #define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2))
484 static NODE *new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs);
485 static NODE *new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs);
486 #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs))
487 static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs);
488 #define new_const_op_assign(lhs, op, rhs) new_const_op_assign_gen(parser, (lhs), (op), (rhs))
490 #define const_path_field(w, n) NEW_COLON2(w, n)
491 #define top_const_field(n) NEW_COLON3(n)
492 static NODE *const_decl_gen(struct parser_params *parser, NODE* path);
493 #define const_decl(path) const_decl_gen(parser, path)
495 #define var_field(n) (n)
496 #define backref_assign_error(n, a) (rb_backref_error(n), NEW_BEGIN(0))
498 static NODE *kwd_append(NODE*, NODE*);
500 static NODE *new_hash_gen(struct parser_params *parser, NODE *hash);
501 #define new_hash(hash) new_hash_gen(parser, (hash))
503 #define new_defined(expr) NEW_DEFINED(remove_begin_all(expr))
505 static NODE *new_regexp_gen(struct parser_params *, NODE *, int);
506 #define new_regexp(node, opt) new_regexp_gen(parser, node, opt)
508 static NODE *new_xstring_gen(struct parser_params *, NODE *);
509 #define new_xstring(node) new_xstring_gen(parser, node)
510 #define new_string1(str) (str)
512 #define new_brace_body(param, stmt) NEW_ITER(param, stmt)
513 #define new_do_body(param, stmt) NEW_ITER(param, stmt)
515 static NODE *match_op_gen(struct parser_params*,NODE*,NODE*);
516 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
518 static ID *local_tbl_gen(struct parser_params*);
519 #define local_tbl() local_tbl_gen(parser)
521 static VALUE reg_compile_gen(struct parser_params*, VALUE, int);
522 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
523 static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int);
524 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
525 static int reg_fragment_check_gen(struct parser_params*, VALUE, int);
526 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
527 static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp);
528 #define reg_named_capture_assign(regexp) reg_named_capture_assign_gen(parser,(regexp))
530 static NODE *parser_heredoc_dedent(struct parser_params*,NODE*);
531 # define heredoc_dedent(str) parser_heredoc_dedent(parser, (str))
533 #define get_id(id) (id)
534 #define get_value(val) (val)
536 #define NODE_RIPPER NODE_CDECL
539 ripper_new_yylval(ID a, VALUE b, VALUE c)
541 return (VALUE)NEW_CDECL(a, b, c);
545 ripper_is_node_yylval(VALUE n)
547 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
550 #define value_expr(node) ((void)(node))
551 #define remove_begin(node) (node)
552 #define rb_dvar_defined(id, base) 0
553 #define rb_local_defined(id, base) 0
554 static ID ripper_get_id(VALUE);
555 #define get_id(id) ripper_get_id(id)
556 static VALUE ripper_get_value(VALUE);
557 #define get_value(val) ripper_get_value(val)
558 static VALUE assignable_gen(struct parser_params*,VALUE);
559 #define assignable(lhs,node) assignable_gen(parser, (lhs))
560 static int id_is_var_gen(struct parser_params *parser, ID id);
561 #define id_is_var(id) id_is_var_gen(parser, (id))
563 #define node_assign(node1, node2) dispatch2(assign, (node1), (node2))
565 static VALUE new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs);
566 static VALUE new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs);
567 #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs))
568 #define new_const_op_assign(lhs, op, rhs) new_op_assign(lhs, op, rhs)
570 static VALUE new_regexp_gen(struct parser_params *, VALUE, VALUE);
571 #define new_regexp(node, opt) new_regexp_gen(parser, node, opt)
573 static VALUE new_xstring_gen(struct parser_params *, VALUE);
574 #define new_xstring(str) new_xstring_gen(parser, str)
575 #define new_string1(str) dispatch1(string_literal, str)
577 #define new_brace_body(param, stmt) dispatch2(brace_block, escape_Qundef(param), stmt)
578 #define new_do_body(param, stmt) dispatch2(do_block, escape_Qundef(param), stmt)
580 #define const_path_field(w, n) dispatch2(const_path_field, (w), (n))
581 #define top_const_field(n) dispatch1(top_const_field, (n))
582 static VALUE const_decl_gen(struct parser_params *parser, VALUE path);
583 #define const_decl(path) const_decl_gen(parser, path)
585 #define var_field(n) dispatch1(var_field, (n))
586 static VALUE assign_error_gen(struct parser_params *parser, VALUE a);
587 #define assign_error(a) assign_error_gen(parser, (a))
588 #define backref_assign_error(n, a) assign_error(a)
590 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
594 #define new_op_assign(lhs, op, rhs) new_op_assign_gen(parser, (lhs), (op), (rhs))
596 RUBY_FUNC_EXPORTED VALUE rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options);
597 RUBY_FUNC_EXPORTED int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
600 static ID formal_argument_gen(struct parser_params*, ID);
601 #define formal_argument(id) formal_argument_gen(parser, (id))
602 static ID shadowing_lvar_gen(struct parser_params*,ID);
603 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
604 static void new_bv_gen(struct parser_params*,ID);
605 #define new_bv(id) new_bv_gen(parser, (id))
607 static void local_push_gen(struct parser_params*,int);
608 #define local_push(top) local_push_gen(parser,(top))
609 static void local_pop_gen(struct parser_params*);
610 #define local_pop() local_pop_gen(parser)
611 static void local_var_gen(struct parser_params*, ID);
612 #define local_var(id) local_var_gen(parser, (id))
613 static void arg_var_gen(struct parser_params*, ID);
614 #define arg_var(id) arg_var_gen(parser, (id))
615 static int local_id_gen(struct parser_params*, ID);
616 #define local_id(id) local_id_gen(parser, (id))
617 static ID internal_id_gen(struct parser_params*);
618 #define internal_id() internal_id_gen(parser)
620 static const struct vtable *dyna_push_gen(struct parser_params *);
621 #define dyna_push() dyna_push_gen(parser)
622 static void dyna_pop_gen(struct parser_params*, const struct vtable *);
623 #define dyna_pop(node) dyna_pop_gen(parser, (node))
624 static int dyna_in_block_gen(struct parser_params*);
625 #define dyna_in_block() dyna_in_block_gen(parser)
626 #define dyna_var(id) local_var(id)
627 static int dvar_defined_gen(struct parser_params*,ID,int);
628 #define dvar_defined(id) dvar_defined_gen(parser, (id), 0)
629 #define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1)
630 static int dvar_curr_gen(struct parser_params*,ID);
631 #define dvar_curr(id) dvar_curr_gen(parser, (id))
633 static int lvar_defined_gen(struct parser_params*, ID);
634 #define lvar_defined(id) lvar_defined_gen(parser, (id))
636 #define RE_OPTION_ONCE (1<<16)
637 #define RE_OPTION_ENCODING_SHIFT 8
638 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
639 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
640 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
641 #define RE_OPTION_MASK 0xff
642 #define RE_OPTION_ARG_ENCODING_NONE 32
644 #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
645 #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
646 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
647 #define nd_func u1.id
648 #if SIZEOF_SHORT == 2
649 #define nd_term(node) ((signed short)(node)->u2.id)
651 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
653 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
654 #define nd_nest u3.cnt
656 /****** Ripper *******/
659 #define RIPPER_VERSION "0.1.0"
661 static inline VALUE intern_sym(const char *name);
663 #include "eventids1.c"
664 #include "eventids2.c"
666 static VALUE ripper_dispatch0(struct parser_params*,ID);
667 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
668 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
669 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
670 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
671 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
672 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
673 static void ripper_error_gen(struct parser_params *parser);
674 #define ripper_error() ripper_error_gen(parser)
676 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
677 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
678 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
679 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
680 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
681 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
682 #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))
684 #define yyparse ripper_yyparse
686 #define ripper_intern(s) ID2SYM(rb_intern(s))
687 static VALUE ripper_id2sym(ID);
689 #define ripper_id2sym(id) (rb_ispunct((int)(id)) ? \
690 ID2SYM(id) : ripper_id2sym(id))
693 #define arg_new() dispatch0(args_new)
694 #define arg_add(l,a) dispatch2(args_add, (l), (a))
695 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
696 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
697 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
698 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
699 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
701 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
702 #define mrhs_new() dispatch0(mrhs_new)
703 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
704 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
706 #define mlhs_new() dispatch0(mlhs_new)
707 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
708 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
710 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
711 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
713 #define blockvar_new(p,v) dispatch2(block_var, (p), (v))
714 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a))
715 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a))
717 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
718 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
719 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
721 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
724 new_args_gen(struct parser_params *parser, VALUE f, VALUE o, VALUE r, VALUE p, VALUE tail)
726 NODE *t = (NODE *)tail;
727 VALUE k = t->u1.value, kr = t->u2.value, b = t->u3.value;
728 return params_new(f, o, r, p, k, kr, escape_Qundef(b));
730 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
733 new_args_tail_gen(struct parser_params *parser, VALUE k, VALUE kr, VALUE b)
735 return (VALUE)MEMO_NEW(k, kr, b);
737 #define new_args_tail(k,kr,b) new_args_tail_gen(parser, (k),(kr),(b))
739 #define new_defined(expr) dispatch1(defined, (expr))
741 static VALUE parser_heredoc_dedent(struct parser_params*,VALUE);
742 # define heredoc_dedent(str) parser_heredoc_dedent(parser, (str))
747 #define ripper_id2sym(id) id
752 # define ifndef_ripper(x) (x)
755 # define ifndef_ripper(x)
758 # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
759 # define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
760 # define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
761 # define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
762 # define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
763 # define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
764 # define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
765 # define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
766 # define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
767 # define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
768 # define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
769 # define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
770 # define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
771 # define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
772 # define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
773 # define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
774 # define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
775 # define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
776 # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
777 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
779 static ID id_warn, id_warning, id_gets;
780 # define WARN_S_L(s,l) STR_NEW(s,l)
781 # define WARN_S(s) STR_NEW2(s)
782 # define WARN_I(i) INT2NUM(i)
783 # define PRIsWARN "s"
784 # define WARN_ARGS(fmt,n) parser->value, id_warn, n, rb_usascii_str_new_lit(fmt)
785 # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
786 # define WARN_CALL rb_funcall
787 # define WARNING_ARGS(fmt,n) parser->value, id_warning, n, rb_usascii_str_new_lit(fmt)
788 # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
789 # define WARNING_CALL rb_funcall
790 static void ripper_compile_error(struct parser_params*, const char *fmt, ...);
791 # define compile_error ripper_compile_error
792 # define PARSER_ARG parser,
794 # define WARN_S_L(s,l) s
797 # define PRIsWARN PRIsVALUE
798 # define WARN_ARGS(fmt,n) WARN_ARGS_L(ruby_sourceline,fmt,n)
799 # define WARN_ARGS_L(l,fmt,n) ruby_sourcefile, (l), (fmt)
800 # define WARN_CALL rb_compile_warn
801 # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
802 # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
803 # define WARNING_CALL rb_compile_warning
804 static void parser_compile_error(struct parser_params*, const char *fmt, ...);
805 # define compile_error parser_compile_error
806 # define PARSER_ARG parser,
809 /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
810 for instance). This is too low for Ruby to parse some files, such as
811 date/format.rb, therefore bump the value up to at least Bison's default. */
814 #define YYMAXDEPTH 10000
818 static void token_info_push_gen(struct parser_params*, const char *token, size_t len);
819 static void token_info_pop_gen(struct parser_params*, const char *token, size_t len);
820 #define token_info_push(token) token_info_push_gen(parser, (token), rb_strlen_lit(token))
821 #define token_info_pop(token) token_info_pop_gen(parser, (token), rb_strlen_lit(token))
825 %lex-param {struct parser_params *parser}
826 %parse-param {struct parser_params *parser}
833 const struct vtable *vars;
891 %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL
892 %token <node> tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
893 %token <node> tNTH_REF tBACK_REF
894 %token <num> tREGEXP_END
896 %type <node> singleton strings string string1 xstring regexp
897 %type <node> string_contents xstring_contents regexp_contents string_content
898 %type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
899 %type <node> literal numeric simple_numeric dsym cpath
900 %type <node> top_compstmt top_stmts top_stmt
901 %type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
902 %type <node> expr_value arg_value primary_value fcall
903 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
904 %type <node> args call_args opt_call_args
905 %type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
906 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
907 %type <node> command_rhs arg_rhs
908 %type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
909 %type <node> f_block_optarg f_block_opt
910 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
911 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
912 %type <node> block_param opt_block_param block_param_def f_opt
913 %type <node> f_kwarg f_kw f_block_kwarg f_block_kw
914 %type <node> bv_decls opt_bv_decl bvar
915 %type <node> lambda f_larglist lambda_body brace_body do_body
916 %type <node> brace_block cmd_brace_block do_block lhs none fitem
917 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
918 %type <id> fsym keyword_variable user_variable sym symbol operation operation2 operation3
919 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
920 %type <id> f_kwrest f_label f_arg_asgn call_op call_op2
923 %type <val> program reswords then do dot_or_colon
925 %token END_OF_INPUT 0 "end-of-input"
926 %token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
927 %token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
928 %token tPOW RUBY_TOKEN(POW) "**"
929 %token tCMP RUBY_TOKEN(CMP) "<=>"
930 %token tEQ RUBY_TOKEN(EQ) "=="
931 %token tEQQ RUBY_TOKEN(EQQ) "==="
932 %token tNEQ RUBY_TOKEN(NEQ) "!="
933 %token tGEQ RUBY_TOKEN(GEQ) ">="
934 %token tLEQ RUBY_TOKEN(LEQ) "<="
935 %token tANDOP RUBY_TOKEN(ANDOP) "&&"
936 %token tOROP RUBY_TOKEN(OROP) "||"
937 %token tMATCH RUBY_TOKEN(MATCH) "=~"
938 %token tNMATCH RUBY_TOKEN(NMATCH) "!~"
939 %token tDOT2 RUBY_TOKEN(DOT2) ".."
940 %token tDOT3 RUBY_TOKEN(DOT3) "..."
941 %token tAREF RUBY_TOKEN(AREF) "[]"
942 %token tASET RUBY_TOKEN(ASET) "[]="
943 %token tLSHFT RUBY_TOKEN(LSHFT) "<<"
944 %token tRSHFT RUBY_TOKEN(RSHFT) ">>"
945 %token tANDDOT RUBY_TOKEN(ANDDOT) "&."
947 %token tCOLON3 ":: at EXPR_BEG"
948 %token <id> tOP_ASGN /* +=, -= etc. */
951 %token tLPAREN_ARG "( arg"
955 %token tLBRACE_ARG "{ arg"
957 %token tDSTAR "**arg"
960 %token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG
961 %token tSTRING_DBEG tSTRING_DEND tSTRING_DVAR tSTRING_END tLAMBEG tLABEL_END
968 %nonassoc tLBRACE_ARG
970 %nonassoc modifier_if modifier_unless modifier_while modifier_until
971 %left keyword_or keyword_and
973 %nonassoc keyword_defined
975 %left modifier_rescue
977 %nonassoc tDOT2 tDOT3
980 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
981 %left '>' tGEQ '<' tLEQ
987 %right tUMINUS_NUM tUMINUS
989 %right '!' '~' tUPLUS
995 SET_LEX_STATE(EXPR_BEG);
997 local_push(compile_for_eval || in_main);
1005 if ($2 && !compile_for_eval) {
1006 /* last expression should not be void */
1007 if (nd_type($2) != NODE_BLOCK) void_expr($2);
1010 while (node->nd_next) {
1011 node = node->nd_next;
1013 void_expr(node->nd_head);
1016 ruby_eval_tree = NEW_SCOPE(0, block_append(ruby_eval_tree, $2));
1019 parser->result = dispatch1(program, $$);
1025 top_compstmt : top_stmts opt_terms
1040 $$ = dispatch2(stmts_add, dispatch0(stmts_new),
1041 dispatch0(void_stmt));
1047 $$ = newline_node($1);
1049 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
1052 | top_stmts terms top_stmt
1055 $$ = block_append($1, newline_node($3));
1057 $$ = dispatch2(stmts_add, $1, $3);
1062 $$ = remove_begin($2);
1070 /* local_push(0); */
1074 '{' top_compstmt '}'
1077 ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
1079 /* NEW_PREEXE($4)); */
1083 $$ = dispatch1(BEGIN, $4);
1096 $$ = NEW_RESCUE($1, $2, $3);
1099 rb_warn0("else without rescue is useless");
1100 $$ = block_append($$, $3);
1104 $$ = NEW_ENSURE($$, $4);
1107 $$ = block_append($4, NEW_NIL());
1112 $$ = dispatch4(bodystmt,
1121 compstmt : stmts opt_terms
1136 $$ = dispatch2(stmts_add, dispatch0(stmts_new),
1137 dispatch0(void_stmt));
1143 $$ = newline_node($1);
1145 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
1148 | stmts terms stmt_or_begin
1151 $$ = block_append($1, newline_node($3));
1153 $$ = dispatch2(stmts_add, $1, $3);
1158 $$ = remove_begin($2);
1162 stmt_or_begin : stmt
1168 yyerror("BEGIN is permitted only at toplevel");
1170 /* local_push(0); */
1174 '{' top_compstmt '}'
1177 ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
1179 /* NEW_PREEXE($4)); */
1183 $$ = dispatch1(BEGIN, $4);
1187 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1190 $$ = NEW_ALIAS($2, $4);
1192 $$ = dispatch2(alias, $2, $4);
1195 | keyword_alias tGVAR tGVAR
1198 $$ = NEW_VALIAS($2, $3);
1200 $$ = dispatch2(var_alias, $2, $3);
1203 | keyword_alias tGVAR tBACK_REF
1208 buf[1] = (char)$3->nd_nth;
1209 $$ = NEW_VALIAS($2, rb_intern2(buf, 2));
1211 $$ = dispatch2(var_alias, $2, $3);
1214 | keyword_alias tGVAR tNTH_REF
1217 yyerror("can't make alias for the number variables");
1220 $$ = dispatch2(var_alias, $2, $3);
1221 $$ = dispatch1(alias_error, $$);
1225 | keyword_undef undef_list
1230 $$ = dispatch1(undef, $2);
1233 | stmt modifier_if expr_value
1236 $$ = new_if($3, remove_begin($1), 0);
1239 $$ = dispatch2(if_mod, $3, $1);
1242 | stmt modifier_unless expr_value
1245 $$ = new_unless($3, remove_begin($1), 0);
1248 $$ = dispatch2(unless_mod, $3, $1);
1251 | stmt modifier_while expr_value
1254 if ($1 && nd_type($1) == NODE_BEGIN) {
1255 $$ = NEW_WHILE(cond($3), $1->nd_body, 0);
1258 $$ = NEW_WHILE(cond($3), $1, 1);
1261 $$ = dispatch2(while_mod, $3, $1);
1264 | stmt modifier_until expr_value
1267 if ($1 && nd_type($1) == NODE_BEGIN) {
1268 $$ = NEW_UNTIL(cond($3), $1->nd_body, 0);
1271 $$ = NEW_UNTIL(cond($3), $1, 1);
1274 $$ = dispatch2(until_mod, $3, $1);
1277 | stmt modifier_rescue stmt
1280 NODE *resq = NEW_RESBODY(0, remove_begin($3), 0);
1281 $$ = NEW_RESCUE(remove_begin($1), resq, 0);
1283 $$ = dispatch2(rescue_mod, $1, $3);
1286 | keyword_END '{' compstmt '}'
1288 if (in_def || in_single) {
1289 rb_warn0("END in method; use at_exit");
1292 $$ = NEW_POSTEXE(NEW_NODE(
1293 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */));
1295 $$ = dispatch1(END, $3);
1299 | mlhs '=' command_call
1306 $$ = dispatch2(massign, $1, $3);
1312 $$ = node_assign($1, $3);
1320 $$ = dispatch2(massign, $1, $3);
1326 command_asgn : lhs '=' command_rhs
1329 $$ = node_assign($1, $3);
1331 | var_lhs tOP_ASGN command_rhs
1334 $$ = new_op_assign($1, $2, $3);
1336 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1342 if (!$3) $3 = NEW_ZARRAY();
1343 args = arg_concat($3, $6);
1347 else if ($5 == tANDOP) {
1350 $$ = NEW_OP_ASGN1($1, $5, args);
1353 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1354 $$ = dispatch3(opassign, $$, $5, $6);
1357 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1360 $$ = new_attr_op_assign($1, $2, $3, $4, $5);
1362 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1365 $$ = new_attr_op_assign($1, $2, $3, $4, $5);
1367 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1369 $$ = const_path_field($1, $3);
1370 $$ = new_const_op_assign($$, $4, $5);
1372 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1375 $$ = new_attr_op_assign($1, ripper_id2sym(idCOLON2), $3, $4, $5);
1377 | backref tOP_ASGN command_rhs
1380 $$ = backref_assign_error($1, node_assign($1, $3));
1384 command_rhs : command_call %prec tOP_ASGN
1392 | command_call modifier_rescue stmt
1396 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0), 0);
1398 $$ = dispatch2(rescue_mod, $1, $3);
1405 | expr keyword_and expr
1408 $$ = logop(NODE_AND, $1, $3);
1410 $$ = dispatch3(binary, $1, ripper_intern("and"), $3);
1413 | expr keyword_or expr
1416 $$ = logop(NODE_OR, $1, $3);
1418 $$ = dispatch3(binary, $1, ripper_intern("or"), $3);
1421 | keyword_not opt_nl expr
1424 $$ = call_uni_op(method_cond($3), '!');
1426 $$ = dispatch2(unary, ripper_intern("not"), $3);
1432 $$ = call_uni_op(method_cond($2), '!');
1434 $$ = dispatch2(unary, ripper_id2sym('!'), $2);
1445 if (!$$) $$ = NEW_NIL();
1452 command_call : command
1456 block_command : block_call
1457 | block_call call_op2 operation2 command_args
1460 $$ = NEW_QCALL($2, $1, $3, $4);
1462 $$ = dispatch3(call, $1, $2, $3);
1463 $$ = method_arg($$, $4);
1468 cmd_brace_block : tLBRACE_ARG
1471 $<num>$ = ruby_sourceline;
1479 nd_set_line($$, $<num>2);
1487 $$ = NEW_FCALL($1, 0);
1488 nd_set_line($$, tokline);
1494 command : fcall command_args %prec tLOWEST
1500 $$ = dispatch2(command, $1, $2);
1503 | fcall command_args cmd_brace_block
1506 block_dup_check($2,$3);
1512 $$ = dispatch2(command, $1, $2);
1513 $$ = method_add_block($$, $3);
1516 | primary_value call_op operation2 command_args %prec tLOWEST
1519 $$ = NEW_QCALL($2, $1, $3, $4);
1522 $$ = dispatch4(command_call, $1, $2, $3, $4);
1525 | primary_value call_op operation2 command_args cmd_brace_block
1528 block_dup_check($4,$5);
1529 $5->nd_iter = NEW_QCALL($2, $1, $3, $4);
1533 $$ = dispatch4(command_call, $1, $2, $3, $4);
1534 $$ = method_add_block($$, $5);
1537 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1540 $$ = NEW_CALL($1, $3, $4);
1543 $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4);
1546 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1549 block_dup_check($4,$5);
1550 $5->nd_iter = NEW_CALL($1, $3, $4);
1554 $$ = dispatch4(command_call, $1, ID2SYM(idCOLON2), $3, $4);
1555 $$ = method_add_block($$, $5);
1558 | keyword_super command_args
1564 $$ = dispatch1(super, $2);
1567 | keyword_yield command_args
1573 $$ = dispatch1(yield, $2);
1576 | keyword_return call_args
1579 $$ = NEW_RETURN(ret_args($2));
1581 $$ = dispatch1(return, $2);
1584 | keyword_break call_args
1587 $$ = NEW_BREAK(ret_args($2));
1589 $$ = dispatch1(break, $2);
1592 | keyword_next call_args
1595 $$ = NEW_NEXT(ret_args($2));
1597 $$ = dispatch1(next, $2);
1603 | tLPAREN mlhs_inner rparen
1608 $$ = dispatch1(mlhs_paren, $2);
1613 mlhs_inner : mlhs_basic
1614 | tLPAREN mlhs_inner rparen
1617 $$ = NEW_MASGN(NEW_LIST($2), 0);
1619 $$ = dispatch1(mlhs_paren, $2);
1624 mlhs_basic : mlhs_head
1627 $$ = NEW_MASGN($1, 0);
1632 | mlhs_head mlhs_item
1635 $$ = NEW_MASGN(list_append($1,$2), 0);
1637 $$ = mlhs_add($1, $2);
1640 | mlhs_head tSTAR mlhs_node
1643 $$ = NEW_MASGN($1, $3);
1645 $$ = mlhs_add_star($1, $3);
1648 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1651 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5));
1653 $1 = mlhs_add_star($1, $3);
1654 $$ = mlhs_add($1, $5);
1660 $$ = NEW_MASGN($1, -1);
1662 $$ = mlhs_add_star($1, Qnil);
1665 | mlhs_head tSTAR ',' mlhs_post
1668 $$ = NEW_MASGN($1, NEW_POSTARG(-1, $4));
1670 $1 = mlhs_add_star($1, Qnil);
1671 $$ = mlhs_add($1, $4);
1677 $$ = NEW_MASGN(0, $2);
1679 $$ = mlhs_add_star(mlhs_new(), $2);
1682 | tSTAR mlhs_node ',' mlhs_post
1685 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4));
1687 $2 = mlhs_add_star(mlhs_new(), $2);
1688 $$ = mlhs_add($2, $4);
1694 $$ = NEW_MASGN(0, -1);
1696 $$ = mlhs_add_star(mlhs_new(), Qnil);
1699 | tSTAR ',' mlhs_post
1702 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
1704 $$ = mlhs_add_star(mlhs_new(), Qnil);
1705 $$ = mlhs_add($$, $3);
1710 mlhs_item : mlhs_node
1711 | tLPAREN mlhs_inner rparen
1716 $$ = dispatch1(mlhs_paren, $2);
1721 mlhs_head : mlhs_item ','
1726 $$ = mlhs_add(mlhs_new(), $1);
1729 | mlhs_head mlhs_item ','
1732 $$ = list_append($1, $2);
1734 $$ = mlhs_add($1, $2);
1739 mlhs_post : mlhs_item
1744 $$ = mlhs_add(mlhs_new(), $1);
1747 | mlhs_post ',' mlhs_item
1750 $$ = list_append($1, $3);
1752 $$ = mlhs_add($1, $3);
1757 mlhs_node : user_variable
1759 $$ = assignable($1, 0);
1763 $$ = assignable($1, 0);
1765 | primary_value '[' opt_call_args rbracket
1768 $$ = aryset($1, $3);
1770 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1773 | primary_value call_op tIDENTIFIER
1776 $$ = attrset($1, $2, $3);
1778 $$ = dispatch3(field, $1, $2, $3);
1781 | primary_value tCOLON2 tIDENTIFIER
1784 $$ = attrset($1, idCOLON2, $3);
1786 $$ = dispatch2(const_path_field, $1, $3);
1789 | primary_value call_op tCONSTANT
1792 $$ = attrset($1, $2, $3);
1794 $$ = dispatch3(field, $1, $2, $3);
1797 | primary_value tCOLON2 tCONSTANT
1799 $$ = const_decl(const_path_field($1, $3));
1803 $$ = const_decl(top_const_field($2));
1808 $$ = backref_assign_error($1, $1);
1814 $$ = assignable($1, 0);
1816 if (!$$) $$ = NEW_BEGIN(0);
1818 $$ = dispatch1(var_field, $$);
1823 $$ = assignable($1, 0);
1825 if (!$$) $$ = NEW_BEGIN(0);
1827 $$ = dispatch1(var_field, $$);
1830 | primary_value '[' opt_call_args rbracket
1833 $$ = aryset($1, $3);
1835 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1838 | primary_value call_op tIDENTIFIER
1841 $$ = attrset($1, $2, $3);
1843 $$ = dispatch3(field, $1, $2, $3);
1846 | primary_value tCOLON2 tIDENTIFIER
1849 $$ = attrset($1, idCOLON2, $3);
1851 $$ = dispatch3(field, $1, ID2SYM(idCOLON2), $3);
1854 | primary_value call_op tCONSTANT
1857 $$ = attrset($1, $2, $3);
1859 $$ = dispatch3(field, $1, $2, $3);
1862 | primary_value tCOLON2 tCONSTANT
1864 $$ = const_decl(const_path_field($1, $3));
1868 $$ = const_decl(top_const_field($2));
1873 $$ = backref_assign_error($1, $1);
1880 yyerror("class/module name must be CONSTANT");
1882 $$ = dispatch1(class_name_error, $1);
1889 cpath : tCOLON3 cname
1892 $$ = NEW_COLON3($2);
1894 $$ = dispatch1(top_const_ref, $2);
1900 $$ = NEW_COLON2(0, $$);
1902 $$ = dispatch1(const_ref, $1);
1905 | primary_value tCOLON2 cname
1908 $$ = NEW_COLON2($1, $3);
1910 $$ = dispatch2(const_path_ref, $1, $3);
1920 SET_LEX_STATE(EXPR_ENDFN);
1925 SET_LEX_STATE(EXPR_ENDFN);
1941 $$ = NEW_LIT(ID2SYM($1));
1943 $$ = dispatch1(symbol_literal, $1);
1954 $$ = rb_ary_new3(1, $1);
1957 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1960 $$ = block_append($1, NEW_UNDEF($4));
1962 rb_ary_push($1, $4);
1967 op : '|' { ifndef_ripper($$ = '|'); }
1968 | '^' { ifndef_ripper($$ = '^'); }
1969 | '&' { ifndef_ripper($$ = '&'); }
1970 | tCMP { ifndef_ripper($$ = tCMP); }
1971 | tEQ { ifndef_ripper($$ = tEQ); }
1972 | tEQQ { ifndef_ripper($$ = tEQQ); }
1973 | tMATCH { ifndef_ripper($$ = tMATCH); }
1974 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
1975 | '>' { ifndef_ripper($$ = '>'); }
1976 | tGEQ { ifndef_ripper($$ = tGEQ); }
1977 | '<' { ifndef_ripper($$ = '<'); }
1978 | tLEQ { ifndef_ripper($$ = tLEQ); }
1979 | tNEQ { ifndef_ripper($$ = tNEQ); }
1980 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
1981 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
1982 | '+' { ifndef_ripper($$ = '+'); }
1983 | '-' { ifndef_ripper($$ = '-'); }
1984 | '*' { ifndef_ripper($$ = '*'); }
1985 | tSTAR { ifndef_ripper($$ = '*'); }
1986 | '/' { ifndef_ripper($$ = '/'); }
1987 | '%' { ifndef_ripper($$ = '%'); }
1988 | tPOW { ifndef_ripper($$ = tPOW); }
1989 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
1990 | '!' { ifndef_ripper($$ = '!'); }
1991 | '~' { ifndef_ripper($$ = '~'); }
1992 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
1993 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
1994 | tAREF { ifndef_ripper($$ = tAREF); }
1995 | tASET { ifndef_ripper($$ = tASET); }
1996 | '`' { ifndef_ripper($$ = '`'); }
1999 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2000 | keyword_BEGIN | keyword_END
2001 | keyword_alias | keyword_and | keyword_begin
2002 | keyword_break | keyword_case | keyword_class | keyword_def
2003 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2004 | keyword_end | keyword_ensure | keyword_false
2005 | keyword_for | keyword_in | keyword_module | keyword_next
2006 | keyword_nil | keyword_not | keyword_or | keyword_redo
2007 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2008 | keyword_super | keyword_then | keyword_true | keyword_undef
2009 | keyword_when | keyword_yield | keyword_if | keyword_unless
2010 | keyword_while | keyword_until
2013 arg : lhs '=' arg_rhs
2015 $$ = node_assign($1, $3);
2017 | var_lhs tOP_ASGN arg_rhs
2019 $$ = new_op_assign($1, $2, $3);
2021 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2027 if (!$3) $3 = NEW_ZARRAY();
2028 if (nd_type($3) == NODE_BLOCK_PASS) {
2029 args = NEW_ARGSCAT($3, $6);
2032 args = arg_concat($3, $6);
2037 else if ($5 == tANDOP) {
2040 $$ = NEW_OP_ASGN1($1, $5, args);
2043 $1 = dispatch2(aref_field, $1, escape_Qundef($3));
2044 $$ = dispatch3(opassign, $1, $5, $6);
2047 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2050 $$ = new_attr_op_assign($1, $2, $3, $4, $5);
2052 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2055 $$ = new_attr_op_assign($1, $2, $3, $4, $5);
2057 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2060 $$ = new_attr_op_assign($1, ripper_id2sym(idCOLON2), $3, $4, $5);
2062 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2064 $$ = const_path_field($1, $3);
2065 $$ = new_const_op_assign($$, $4, $5);
2067 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2069 $$ = top_const_field($2);
2070 $$ = new_const_op_assign($$, $3, $4);
2072 | backref tOP_ASGN arg_rhs
2075 $$ = backref_assign_error($1, new_op_assign($1, $2, $3));
2082 $$ = NEW_DOT2($1, $3);
2084 $$ = dispatch2(dot2, $1, $3);
2092 $$ = NEW_DOT3($1, $3);
2094 $$ = dispatch2(dot3, $1, $3);
2100 $$ = call_bin_op($1, '+', $3);
2102 $$ = dispatch3(binary, $1, ID2SYM('+'), $3);
2108 $$ = call_bin_op($1, '-', $3);
2110 $$ = dispatch3(binary, $1, ID2SYM('-'), $3);
2116 $$ = call_bin_op($1, '*', $3);
2118 $$ = dispatch3(binary, $1, ID2SYM('*'), $3);
2124 $$ = call_bin_op($1, '/', $3);
2126 $$ = dispatch3(binary, $1, ID2SYM('/'), $3);
2132 $$ = call_bin_op($1, '%', $3);
2134 $$ = dispatch3(binary, $1, ID2SYM('%'), $3);
2140 $$ = call_bin_op($1, tPOW, $3);
2142 $$ = dispatch3(binary, $1, ID2SYM(idPow), $3);
2145 | tUMINUS_NUM simple_numeric tPOW arg
2148 $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
2150 $$ = dispatch3(binary, $2, ID2SYM(idPow), $4);
2151 $$ = dispatch2(unary, ID2SYM(idUMinus), $$);
2157 $$ = call_uni_op($2, tUPLUS);
2159 $$ = dispatch2(unary, ID2SYM(idUPlus), $2);
2165 $$ = call_uni_op($2, tUMINUS);
2167 $$ = dispatch2(unary, ID2SYM(idUMinus), $2);
2173 $$ = call_bin_op($1, '|', $3);
2175 $$ = dispatch3(binary, $1, ID2SYM('|'), $3);
2181 $$ = call_bin_op($1, '^', $3);
2183 $$ = dispatch3(binary, $1, ID2SYM('^'), $3);
2189 $$ = call_bin_op($1, '&', $3);
2191 $$ = dispatch3(binary, $1, ID2SYM('&'), $3);
2197 $$ = call_bin_op($1, tCMP, $3);
2199 $$ = dispatch3(binary, $1, ID2SYM(idCmp), $3);
2205 $$ = call_bin_op($1, '>', $3);
2207 $$ = dispatch3(binary, $1, ID2SYM('>'), $3);
2213 $$ = call_bin_op($1, tGEQ, $3);
2215 $$ = dispatch3(binary, $1, ID2SYM(idGE), $3);
2221 $$ = call_bin_op($1, '<', $3);
2223 $$ = dispatch3(binary, $1, ID2SYM('<'), $3);
2229 $$ = call_bin_op($1, tLEQ, $3);
2231 $$ = dispatch3(binary, $1, ID2SYM(idLE), $3);
2237 $$ = call_bin_op($1, tEQ, $3);
2239 $$ = dispatch3(binary, $1, ID2SYM(idEq), $3);
2245 $$ = call_bin_op($1, tEQQ, $3);
2247 $$ = dispatch3(binary, $1, ID2SYM(idEqq), $3);
2253 $$ = call_bin_op($1, tNEQ, $3);
2255 $$ = dispatch3(binary, $1, ID2SYM(idNeq), $3);
2261 $$ = match_op($1, $3);
2262 if (nd_type($1) == NODE_LIT) {
2263 VALUE lit = $1->nd_lit;
2264 if (RB_TYPE_P(lit, T_REGEXP)) {
2265 $$->nd_args = reg_named_capture_assign(lit);
2269 $$ = dispatch3(binary, $1, ID2SYM(idEqTilde), $3);
2275 $$ = call_bin_op($1, tNMATCH, $3);
2277 $$ = dispatch3(binary, $1, ID2SYM(idNeqTilde), $3);
2283 $$ = call_uni_op(method_cond($2), '!');
2285 $$ = dispatch2(unary, ID2SYM('!'), $2);
2291 $$ = call_uni_op($2, '~');
2293 $$ = dispatch2(unary, ID2SYM('~'), $2);
2299 $$ = call_bin_op($1, tLSHFT, $3);
2301 $$ = dispatch3(binary, $1, ID2SYM(idLTLT), $3);
2307 $$ = call_bin_op($1, tRSHFT, $3);
2309 $$ = dispatch3(binary, $1, ID2SYM(idGTGT), $3);
2315 $$ = logop(NODE_AND, $1, $3);
2317 $$ = dispatch3(binary, $1, ID2SYM(idANDOP), $3);
2323 $$ = logop(NODE_OR, $1, $3);
2325 $$ = dispatch3(binary, $1, ID2SYM(idOROP), $3);
2328 | keyword_defined opt_nl {in_defined = 1;} arg
2332 $$ = new_defined($4);
2334 $$ = dispatch1(defined, $4);
2337 | arg '?' arg opt_nl ':' arg
2341 $$ = new_if($1, $3, $6);
2344 $$ = dispatch3(ifop, $1, $3, $6);
2358 if (!$$) $$ = NEW_NIL();
2370 | args ',' assocs trailer
2373 $$ = $3 ? arg_append($1, new_hash($3)) : $1;
2375 $$ = arg_add_assocs($1, $3);
2381 $$ = $1 ? NEW_LIST(new_hash($1)) : 0;
2383 $$ = arg_add_assocs(arg_new(), $1);
2388 arg_rhs : arg %prec tOP_ASGN
2396 | arg modifier_rescue arg
2400 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0), 0);
2402 $$ = dispatch2(rescue_mod, $1, $3);
2407 paren_args : '(' opt_call_args rparen
2412 $$ = dispatch1(arg_paren, escape_Qundef($2));
2417 opt_paren_args : none
2421 opt_call_args : none
2427 | args ',' assocs ','
2430 $$ = $3 ? arg_append($1, new_hash($3)) : $1;
2432 $$ = arg_add_assocs($1, $3);
2438 $$ = $1 ? NEW_LIST(new_hash($1)) : 0;
2440 $$ = arg_add_assocs(arg_new(), $1);
2451 $$ = arg_add(arg_new(), $1);
2454 | args opt_block_arg
2457 $$ = arg_blk_pass($1, $2);
2459 $$ = arg_add_optblock($1, $2);
2462 | assocs opt_block_arg
2465 $$ = $1 ? NEW_LIST(new_hash($1)) : 0;
2466 $$ = arg_blk_pass($$, $2);
2468 $$ = arg_add_assocs(arg_new(), $1);
2469 $$ = arg_add_optblock($$, $2);
2472 | args ',' assocs opt_block_arg
2475 $$ = $3 ? arg_append($1, new_hash($3)) : $1;
2476 $$ = arg_blk_pass($$, $4);
2478 $$ = arg_add_optblock(arg_add_assocs($1, $3), $4);
2485 $$ = arg_add_block(arg_new(), $1);
2491 $<val>$ = cmdarg_stack;
2497 CMDARG_SET($<val>1);
2502 block_arg : tAMPER arg_value
2505 $$ = NEW_BLOCK_PASS($2);
2512 opt_block_arg : ',' block_arg
2527 $$ = arg_add(arg_new(), $1);
2535 $$ = arg_add_star(arg_new(), $2);
2538 | args ',' arg_value
2542 if ((n1 = splat_array($1)) != 0) {
2543 $$ = list_append(n1, $3);
2546 $$ = arg_append($1, $3);
2549 $$ = arg_add($1, $3);
2552 | args ',' tSTAR arg_value
2556 if ((nd_type($4) == NODE_ARRAY) && (n1 = splat_array($1)) != 0) {
2557 $$ = list_concat(n1, $4);
2560 $$ = arg_concat($1, $4);
2563 $$ = arg_add_star($1, $4);
2572 mrhs : args ',' arg_value
2576 if ((n1 = splat_array($1)) != 0) {
2577 $$ = list_append(n1, $3);
2580 $$ = arg_append($1, $3);
2583 $$ = mrhs_add(args2mrhs($1), $3);
2586 | args ',' tSTAR arg_value
2590 if (nd_type($4) == NODE_ARRAY &&
2591 (n1 = splat_array($1)) != 0) {
2592 $$ = list_concat(n1, $4);
2595 $$ = arg_concat($1, $4);
2598 $$ = mrhs_add_star(args2mrhs($1), $4);
2606 $$ = mrhs_add_star(mrhs_new(), $2);
2624 $$ = NEW_FCALL($1, 0);
2626 $$ = method_arg(dispatch1(fcall, $1), arg_new());
2631 $<val>1 = cmdarg_stack;
2634 $<num>$ = ruby_sourceline;
2641 CMDARG_SET($<val>1);
2647 if (nd_type($3) == NODE_RESCUE ||
2648 nd_type($3) == NODE_ENSURE)
2649 nd_set_line($3, $<num>2);
2652 nd_set_line($$, $<num>2);
2654 $$ = dispatch1(begin, $3);
2657 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2662 $$ = dispatch1(paren, 0);
2667 $<val>1 = cmdarg_stack;
2670 stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2672 CMDARG_SET($<val>1);
2676 $$ = dispatch1(paren, $3);
2679 | tLPAREN compstmt ')'
2684 $$ = dispatch1(paren, $2);
2687 | primary_value tCOLON2 tCONSTANT
2690 $$ = NEW_COLON2($1, $3);
2692 $$ = dispatch2(const_path_ref, $1, $3);
2698 $$ = NEW_COLON3($2);
2700 $$ = dispatch1(top_const_ref, $2);
2703 | tLBRACK aref_args ']'
2707 $$ = NEW_ZARRAY(); /* zero length array*/
2713 $$ = dispatch1(array, escape_Qundef($2));
2716 | tLBRACE assoc_list '}'
2721 $$ = dispatch1(hash, escape_Qundef($2));
2729 $$ = dispatch0(return0);
2732 | keyword_yield '(' call_args rparen
2737 $$ = dispatch1(yield, dispatch1(paren, $3));
2740 | keyword_yield '(' rparen
2745 $$ = dispatch1(yield, dispatch1(paren, arg_new()));
2753 $$ = dispatch0(yield0);
2756 | keyword_defined opt_nl '(' {in_defined = 1;} expr rparen
2760 $$ = new_defined($5);
2762 $$ = dispatch1(defined, $5);
2765 | keyword_not '(' expr rparen
2768 $$ = call_uni_op(method_cond($3), '!');
2770 $$ = dispatch2(unary, ripper_intern("not"), $3);
2773 | keyword_not '(' rparen
2776 $$ = call_uni_op(method_cond(NEW_NIL()), '!');
2778 $$ = dispatch2(unary, ripper_intern("not"), Qnil);
2787 $$ = method_arg(dispatch1(fcall, $1), arg_new());
2788 $$ = method_add_block($$, $2);
2792 | method_call brace_block
2795 block_dup_check($1->nd_args, $2);
2799 $$ = method_add_block($1, $2);
2806 | k_if expr_value then
2812 $$ = new_if($2, $4, $5);
2815 $$ = dispatch3(if, $2, $4, escape_Qundef($5));
2818 | k_unless expr_value then
2824 $$ = new_unless($2, $4, $5);
2827 $$ = dispatch3(unless, $2, $4, escape_Qundef($5));
2830 | k_while {COND_PUSH(1);} expr_value do {COND_POP();}
2835 $$ = NEW_WHILE(cond($3), $6, 1);
2838 $$ = dispatch2(while, $3, $6);
2841 | k_until {COND_PUSH(1);} expr_value do {COND_POP();}
2846 $$ = NEW_UNTIL(cond($3), $6, 1);
2849 $$ = dispatch2(until, $3, $6);
2852 | k_case expr_value opt_terms
2857 $$ = NEW_CASE($2, $4);
2860 $$ = dispatch2(case, $2, $4);
2863 | k_case opt_terms case_body k_end
2866 $$ = NEW_CASE(0, $3);
2868 $$ = dispatch2(case, Qnil, $3);
2871 | k_for for_var keyword_in
2882 * e.each{|*x| a, b, c = x}
2886 * e.each{|x| a, = x}
2888 ID id = internal_id();
2889 ID *tbl = ALLOC_N(ID, 2);
2890 NODE *m = NEW_ARGS_AUX(0, 0);
2893 switch (nd_type($2)) {
2895 m->nd_next = node_assign($2, NEW_FOR(NEW_DVAR(id), 0, 0));
2896 args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0));
2900 case NODE_DASGN_CURR:
2901 $2->nd_value = NEW_DVAR(id);
2904 args = new_args(m, 0, 0, 0, new_args_tail(0, 0, 0));
2907 m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id));
2908 args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0));
2911 scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
2912 tbl[0] = 1; tbl[1] = id;
2913 $$ = NEW_FOR(0, $5, scope);
2916 $$ = dispatch3(for, $2, $5, $8);
2919 | k_class cpath superclass
2921 if (in_def || in_single)
2922 yyerror("class definition in method body");
2925 $<num>$ = ruby_sourceline;
2933 $$ = NEW_CLASS($2, $5, $3);
2934 nd_set_line($$, $<num>4);
2936 $$ = dispatch3(class, $2, $3, $5);
2940 | k_class tLSHFT expr
2942 $<num>$ = (in_def << 1) | in_single;
2952 $$ = NEW_SCLASS($3, $6);
2955 $$ = dispatch2(sclass, $3, $6);
2958 in_def = ($<num>4 >> 1) & 1;
2959 in_single = $<num>4 & 1;
2963 if (in_def || in_single)
2964 yyerror("module definition in method body");
2967 $<num>$ = ruby_sourceline;
2975 $$ = NEW_MODULE($2, $4);
2976 nd_set_line($$, $<num>3);
2978 $$ = dispatch2(module, $2, $4);
2985 $<id>$ = current_arg;
2997 NODE *body = remove_begin($6);
2998 reduce_nodes(&body);
2999 $$ = NEW_DEFN($2, $5, body, METHOD_VISI_PRIVATE);
3000 nd_set_line($$, $<num>1);
3002 $$ = dispatch3(def, $2, $5, $6);
3005 in_def = $<num>4 & 1;
3006 current_arg = $<id>3;
3008 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3010 $<num>4 = in_single;
3012 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3014 $<id>$ = current_arg;
3022 NODE *body = remove_begin($8);
3023 reduce_nodes(&body);
3024 $$ = NEW_DEFS($2, $5, $7, body);
3025 nd_set_line($$, $<num>1);
3027 $$ = dispatch5(defs, $2, $3, $5, $7, $8);
3030 in_single = $<num>4 & 1;
3031 current_arg = $<id>6;
3038 $$ = dispatch1(break, arg_new());
3046 $$ = dispatch1(next, arg_new());
3054 $$ = dispatch0(redo);
3062 $$ = dispatch0(retry);
3067 primary_value : primary
3072 if (!$$) $$ = NEW_NIL();
3079 k_begin : keyword_begin
3081 token_info_push("begin");
3087 token_info_push("if");
3091 k_unless : keyword_unless
3093 token_info_push("unless");
3097 k_while : keyword_while
3099 token_info_push("while");
3103 k_until : keyword_until
3105 token_info_push("until");
3109 k_case : keyword_case
3111 token_info_push("case");
3117 token_info_push("for");
3121 k_class : keyword_class
3123 token_info_push("class");
3127 k_module : keyword_module
3129 token_info_push("module");
3135 token_info_push("def");
3137 $<num>$ = ruby_sourceline;
3145 token_info_pop("end");
3171 | keyword_elsif expr_value then
3176 $$ = new_if($2, $4, $5);
3179 $$ = dispatch3(elsif, $2, $4, escape_Qundef($5));
3185 | keyword_else compstmt
3190 $$ = dispatch1(else, $2);
3201 $$ = assignable($1, 0);
3204 $$ = dispatch1(mlhs_paren, $$);
3207 | tLPAREN f_margs rparen
3212 $$ = dispatch1(mlhs_paren, $2);
3217 f_marg_list : f_marg
3222 $$ = mlhs_add(mlhs_new(), $1);
3225 | f_marg_list ',' f_marg
3228 $$ = list_append($1, $3);
3230 $$ = mlhs_add($1, $3);
3235 f_margs : f_marg_list
3238 $$ = NEW_MASGN($1, 0);
3243 | f_marg_list ',' tSTAR f_norm_arg
3245 $$ = assignable($4, 0);
3247 $$ = NEW_MASGN($1, $$);
3249 $$ = mlhs_add_star($1, $$);
3252 | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list
3254 $$ = assignable($4, 0);
3256 $$ = NEW_MASGN($1, NEW_POSTARG($$, $6));
3258 $$ = mlhs_add_star($1, $$);
3261 | f_marg_list ',' tSTAR
3264 $$ = NEW_MASGN($1, -1);
3266 $$ = mlhs_add_star($1, Qnil);
3269 | f_marg_list ',' tSTAR ',' f_marg_list
3272 $$ = NEW_MASGN($1, NEW_POSTARG(-1, $5));
3274 $$ = mlhs_add_star($1, $5);
3279 $$ = assignable($2, 0);
3281 $$ = NEW_MASGN(0, $$);
3283 $$ = mlhs_add_star(mlhs_new(), $$);
3286 | tSTAR f_norm_arg ',' f_marg_list
3288 $$ = assignable($2, 0);
3290 $$ = NEW_MASGN(0, NEW_POSTARG($$, $4));
3295 $$ = mlhs_add_star($$, $4);
3301 $$ = NEW_MASGN(0, -1);
3303 $$ = mlhs_add_star(mlhs_new(), Qnil);
3306 | tSTAR ',' f_marg_list
3309 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
3311 $$ = mlhs_add_star(mlhs_new(), Qnil);
3317 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3319 $$ = new_args_tail($1, $3, $4);
3321 | f_block_kwarg opt_f_block_arg
3323 $$ = new_args_tail($1, Qnone, $2);
3325 | f_kwrest opt_f_block_arg
3327 $$ = new_args_tail(Qnone, $1, $2);
3331 $$ = new_args_tail(Qnone, Qnone, $1);
3335 opt_block_args_tail : ',' block_args_tail
3341 $$ = new_args_tail(Qnone, Qnone, Qnone);
3345 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3347 $$ = new_args($1, $3, $5, Qnone, $6);
3349 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3351 $$ = new_args($1, $3, $5, $7, $8);
3353 | f_arg ',' f_block_optarg opt_block_args_tail
3355 $$ = new_args($1, $3, Qnone, Qnone, $4);
3357 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3359 $$ = new_args($1, $3, Qnone, $5, $6);
3361 | f_arg ',' f_rest_arg opt_block_args_tail
3363 $$ = new_args($1, Qnone, $3, Qnone, $4);
3367 $$ = new_args($1, Qnone, 1, Qnone, new_args_tail(Qnone, Qnone, Qnone));
3370 dispatch1(excessed_comma, $$);
3373 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3375 $$ = new_args($1, Qnone, $3, $5, $6);
3377 | f_arg opt_block_args_tail
3379 $$ = new_args($1, Qnone, Qnone, Qnone, $2);
3381 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3383 $$ = new_args(Qnone, $1, $3, Qnone, $4);
3385 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3387 $$ = new_args(Qnone, $1, $3, $5, $6);
3389 | f_block_optarg opt_block_args_tail
3391 $$ = new_args(Qnone, $1, Qnone, Qnone, $2);
3393 | f_block_optarg ',' f_arg opt_block_args_tail
3395 $$ = new_args(Qnone, $1, Qnone, $3, $4);
3397 | f_rest_arg opt_block_args_tail
3399 $$ = new_args(Qnone, Qnone, $1, Qnone, $2);
3401 | f_rest_arg ',' f_arg opt_block_args_tail
3403 $$ = new_args(Qnone, Qnone, $1, $3, $4);
3407 $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1);
3411 opt_block_param : none
3414 command_start = TRUE;
3418 block_param_def : '|' opt_bv_decl '|'
3424 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil),
3433 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil),
3437 | '|' block_param opt_bv_decl '|'
3443 $$ = blockvar_new(escape_Qundef($2), escape_Qundef($3));
3449 opt_bv_decl : opt_nl
3453 | opt_nl ';' bv_decls opt_nl
3467 $$ = rb_ary_new3(1, $1);
3474 rb_ary_push($1, $3);
3494 $<vars>$ = dyna_push();
3498 lpar_beg = ++paren_nest;
3502 $<num>$ = ruby_sourceline;
3505 $<val>$ = cmdarg_stack;
3511 CMDARG_SET($<val>5);
3514 $$ = NEW_LAMBDA($3, $6);
3515 nd_set_line($$, $<num>4);
3517 $$ = dispatch2(lambda, $3, $6);
3523 f_larglist : '(' f_args opt_bv_decl ')'
3528 $$ = dispatch1(paren, $2);
3537 lambda_body : tLAMBEG compstmt '}'
3539 token_info_pop("}");
3542 | keyword_do_LAMBDA compstmt k_end
3548 do_block : keyword_do_block
3551 $<num>$ = ruby_sourceline;
3558 nd_set_line($$, $<num>2);
3563 block_call : command do_block
3566 if (nd_type($1) == NODE_YIELD) {
3567 compile_error(PARSER_ARG "block given to yield");
3570 block_dup_check($1->nd_args, $2);
3576 $$ = method_add_block($1, $2);
3579 | block_call call_op2 operation2 opt_paren_args
3582 $$ = NEW_QCALL($2, $1, $3, $4);
3584 $$ = dispatch3(call, $1, $2, $3);
3585 $$ = method_optarg($$, $4);
3588 | block_call call_op2 operation2 opt_paren_args brace_block
3591 block_dup_check($4, $5);
3592 $5->nd_iter = NEW_QCALL($2, $1, $3, $4);
3596 $$ = dispatch4(command_call, $1, $2, $3, $4);
3597 $$ = method_add_block($$, $5);
3600 | block_call call_op2 operation2 command_args do_block
3603 block_dup_check($4, $5);
3604 $5->nd_iter = NEW_QCALL($2, $1, $3, $4);
3608 $$ = dispatch4(command_call, $1, $2, $3, $4);
3609 $$ = method_add_block($$, $5);
3614 method_call : fcall paren_args
3620 $$ = method_arg(dispatch1(fcall, $1), $2);
3623 | primary_value call_op operation2
3626 $<num>$ = ruby_sourceline;
3632 $$ = NEW_QCALL($2, $1, $3, $5);
3633 nd_set_line($$, $<num>4);
3635 $$ = dispatch3(call, $1, $2, $3);
3636 $$ = method_optarg($$, $5);
3639 | primary_value tCOLON2 operation2
3642 $<num>$ = ruby_sourceline;
3648 $$ = NEW_CALL($1, $3, $5);
3649 nd_set_line($$, $<num>4);
3651 $$ = dispatch3(call, $1, ripper_id2sym(idCOLON2), $3);
3652 $$ = method_optarg($$, $5);
3655 | primary_value tCOLON2 operation3
3658 $$ = NEW_CALL($1, $3, 0);
3660 $$ = dispatch3(call, $1, ID2SYM(idCOLON2), $3);
3663 | primary_value call_op
3666 $<num>$ = ruby_sourceline;
3672 $$ = NEW_QCALL($2, $1, idCall, $4);
3673 nd_set_line($$, $<num>3);
3675 $$ = dispatch3(call, $1, $2, ID2SYM(idCall));
3676 $$ = method_optarg($$, $4);
3679 | primary_value tCOLON2
3682 $<num>$ = ruby_sourceline;
3688 $$ = NEW_CALL($1, idCall, $4);
3689 nd_set_line($$, $<num>3);
3691 $$ = dispatch3(call, $1, ID2SYM(idCOLON2),
3693 $$ = method_optarg($$, $4);
3696 | keyword_super paren_args
3701 $$ = dispatch1(super, $2);
3709 $$ = dispatch0(zsuper);
3712 | primary_value '[' opt_call_args rbracket
3715 if ($1 && nd_type($1) == NODE_SELF)
3716 $$ = NEW_FCALL(tAREF, $3);
3718 $$ = NEW_CALL($1, tAREF, $3);
3721 $$ = dispatch2(aref, $1, escape_Qundef($3));
3729 $<num>$ = ruby_sourceline;
3736 nd_set_line($$, $<num>2);
3742 $<num>$ = ruby_sourceline;
3749 nd_set_line($$, $<num>2);
3754 brace_body : {$<vars>$ = dyna_push();}
3755 {$<val>$ = cmdarg_stack >> 1; CMDARG_SET(0);}
3756 opt_block_param compstmt
3758 $$ = new_brace_body($3, $4);
3760 CMDARG_SET($<val>2);
3764 do_body : {$<vars>$ = dyna_push();}
3765 {$<val>$ = cmdarg_stack; CMDARG_SET(0);}
3766 opt_block_param compstmt
3768 $$ = new_do_body($3, $4);
3770 CMDARG_SET($<val>2);
3774 case_body : keyword_when args then
3779 $$ = NEW_WHEN($2, $4, $5);
3781 $$ = dispatch3(when, $2, $4, escape_Qundef($5));
3790 opt_rescue : keyword_rescue exc_list exc_var then
3796 $3 = node_assign($3, NEW_ERRINFO());
3797 $5 = block_append($3, $5);
3799 $$ = NEW_RESBODY($2, $5, $6);
3800 fixpos($$, $2?$2:$5);
3802 $$ = dispatch4(rescue,
3812 exc_list : arg_value
3817 $$ = rb_ary_new3(1, $1);
3823 if (!($$ = splat_array($1))) $$ = $1;
3831 exc_var : tASSOC lhs
3838 opt_ensure : keyword_ensure compstmt
3843 $$ = dispatch1(ensure, $2);
3853 $$ = NEW_LIT(ID2SYM($1));
3855 $$ = dispatch1(symbol_literal, $1);
3866 node = NEW_STR(STR_NEW0());
3869 node = evstr2dstr(node);
3883 $$ = literal_concat($1, $2);
3885 $$ = dispatch2(string_concat, $1, $2);
3890 string1 : tSTRING_BEG string_contents tSTRING_END
3892 $$ = new_string1(heredoc_dedent($2));
3896 xstring : tXSTRING_BEG xstring_contents tSTRING_END
3898 $$ = new_xstring(heredoc_dedent($2));
3902 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
3904 $$ = new_regexp($2, $3);
3908 words : tWORDS_BEG ' ' tSTRING_END
3913 $$ = dispatch0(words_new);
3914 $$ = dispatch1(array, $$);
3917 | tWORDS_BEG word_list tSTRING_END
3922 $$ = dispatch1(array, $2);
3927 word_list : /* none */
3932 $$ = dispatch0(words_new);
3935 | word_list word ' '
3938 $$ = list_append($1, evstr2dstr($2));
3940 $$ = dispatch2(words_add, $1, $2);
3945 word : string_content
3949 $$ = dispatch0(word_new);
3950 $$ = dispatch2(word_add, $$, $1);
3953 | word string_content
3956 $$ = literal_concat($1, $2);
3958 $$ = dispatch2(word_add, $1, $2);
3963 symbols : tSYMBOLS_BEG ' ' tSTRING_END
3968 $$ = dispatch0(symbols_new);
3969 $$ = dispatch1(array, $$);
3972 | tSYMBOLS_BEG symbol_list tSTRING_END
3977 $$ = dispatch1(array, $2);
3982 symbol_list : /* none */
3987 $$ = dispatch0(symbols_new);
3990 | symbol_list word ' '
3993 $2 = evstr2dstr($2);
3994 if (nd_type($2) == NODE_DSTR) {
3995 nd_set_type($2, NODE_DSYM);
3998 nd_set_type($2, NODE_LIT);
3999 $2->nd_lit = rb_str_intern($2->nd_lit);
4001 $$ = list_append($1, $2);
4003 $$ = dispatch2(symbols_add, $1, $2);
4008 qwords : tQWORDS_BEG ' ' tSTRING_END
4013 $$ = dispatch0(qwords_new);
4014 $$ = dispatch1(array, $$);
4017 | tQWORDS_BEG qword_list tSTRING_END
4022 $$ = dispatch1(array, $2);
4027 qsymbols : tQSYMBOLS_BEG ' ' tSTRING_END
4032 $$ = dispatch0(qsymbols_new);
4033 $$ = dispatch1(array, $$);
4036 | tQSYMBOLS_BEG qsym_list tSTRING_END
4041 $$ = dispatch1(array, $2);
4046 qword_list : /* none */
4051 $$ = dispatch0(qwords_new);
4054 | qword_list tSTRING_CONTENT ' '
4057 $$ = list_append($1, $2);
4059 $$ = dispatch2(qwords_add, $1, $2);
4064 qsym_list : /* none */
4069 $$ = dispatch0(qsymbols_new);
4072 | qsym_list tSTRING_CONTENT ' '
4077 $2->nd_lit = ID2SYM(rb_intern_str(lit));
4078 nd_set_type($2, NODE_LIT);
4079 $$ = list_append($1, $2);
4081 $$ = dispatch2(qsymbols_add, $1, $2);
4086 string_contents : /* none */
4091 $$ = dispatch0(string_content);
4094 | string_contents string_content
4097 $$ = literal_concat($1, $2);
4099 $$ = dispatch2(string_add, $1, $2);
4104 xstring_contents: /* none */
4109 $$ = dispatch0(xstring_new);
4112 | xstring_contents string_content
4115 $$ = literal_concat($1, $2);
4117 $$ = dispatch2(xstring_add, $1, $2);
4122 regexp_contents: /* none */
4127 $$ = ripper_new_yylval(0, dispatch0(regexp_new), 0);
4130 | regexp_contents string_content
4133 NODE *head = $1, *tail = $2;
4141 switch (nd_type(head)) {
4143 nd_set_type(head, NODE_DSTR);
4148 head = list_append(NEW_DSTR(Qnil), head);
4151 $$ = list_append(head, tail);
4154 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4155 if (ripper_is_node_yylval(n1)) {
4156 s1 = RNODE(n1)->nd_cval;
4157 n1 = RNODE(n1)->nd_rval;
4159 if (ripper_is_node_yylval(n2)) {
4160 s2 = RNODE(n2)->nd_cval;
4161 n2 = RNODE(n2)->nd_rval;
4163 $$ = dispatch2(regexp_add, n1, n2);
4165 $$ = ripper_new_yylval(0, $$, s2);
4171 string_content : tSTRING_CONTENT
4174 $<node>$ = lex_strterm;
4176 SET_LEX_STATE(EXPR_BEG);
4180 lex_strterm = $<node>2;
4184 $$ = dispatch1(string_dvar, $3);
4189 $<val>1 = cond_stack;
4190 $<val>$ = cmdarg_stack;
4195 $<node>$ = lex_strterm;
4199 $<num>$ = lex_state;
4200 SET_LEX_STATE(EXPR_BEG);
4203 $<num>$ = brace_nest;
4207 $<num>$ = heredoc_indent;
4210 compstmt tSTRING_DEND
4213 CMDARG_SET($<val>2);
4214 lex_strterm = $<node>3;
4215 SET_LEX_STATE($<num>4);
4216 brace_nest = $<num>5;
4217 heredoc_indent = $<num>6;
4218 heredoc_line_indent = -1;
4220 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4223 $$ = dispatch1(string_embexpr, $7);
4233 $$ = dispatch1(var_ref, $1);
4241 $$ = dispatch1(var_ref, $1);
4249 $$ = dispatch1(var_ref, $1);
4255 symbol : tSYMBEG sym
4257 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
4261 $$ = dispatch1(symbol, $2);
4272 dsym : tSYMBEG xstring_contents tSTRING_END
4274 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
4278 $$ = dispatch1(dyna_symbol, $2);
4283 numeric : simple_numeric
4284 | tUMINUS_NUM simple_numeric %prec tLOWEST
4288 $$->nd_lit = negate_lit($$->nd_lit);
4290 $$ = dispatch2(unary, ID2SYM(idUMinus), $2);
4295 simple_numeric : tINTEGER
4301 user_variable : tIDENTIFIER
4308 keyword_variable: keyword_nil {ifndef_ripper($$ = keyword_nil);}
4309 | keyword_self {ifndef_ripper($$ = keyword_self);}
4310 | keyword_true {ifndef_ripper($$ = keyword_true);}
4311 | keyword_false {ifndef_ripper($$ = keyword_false);}
4312 | keyword__FILE__ {ifndef_ripper($$ = keyword__FILE__);}
4313 | keyword__LINE__ {ifndef_ripper($$ = keyword__LINE__);}
4314 | keyword__ENCODING__ {ifndef_ripper($$ = keyword__ENCODING__);}
4317 var_ref : user_variable
4320 if (!($$ = gettable($1))) $$ = NEW_BEGIN(0);
4322 if (id_is_var(get_id($1))) {
4323 $$ = dispatch1(var_ref, $1);
4326 $$ = dispatch1(vcall, $1);
4333 if (!($$ = gettable($1))) $$ = NEW_BEGIN(0);
4335 $$ = dispatch1(var_ref, $1);
4340 var_lhs : user_variable
4342 $$ = assignable($1, 0);
4345 $$ = dispatch1(var_field, $$);
4350 $$ = assignable($1, 0);
4353 $$ = dispatch1(var_field, $$);
4364 SET_LEX_STATE(EXPR_BEG);
4365 command_start = TRUE;
4381 f_arglist : '(' f_args rparen
4386 $$ = dispatch1(paren, $2);
4388 SET_LEX_STATE(EXPR_BEG);
4389 command_start = TRUE;
4392 $<num>$ = parser->in_kwarg;
4393 parser->in_kwarg = 1;
4394 lex_state |= EXPR_LABEL; /* force for args */
4398 parser->in_kwarg = !!$<num>1;
4400 SET_LEX_STATE(EXPR_BEG);
4401 command_start = TRUE;
4405 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4407 $$ = new_args_tail($1, $3, $4);
4409 | f_kwarg opt_f_block_arg
4411 $$ = new_args_tail($1, Qnone, $2);
4413 | f_kwrest opt_f_block_arg
4415 $$ = new_args_tail(Qnone, $1, $2);
4419 $$ = new_args_tail(Qnone, Qnone, $1);
4423 opt_args_tail : ',' args_tail
4429 $$ = new_args_tail(Qnone, Qnone, Qnone);
4433 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4435 $$ = new_args($1, $3, $5, Qnone, $6);
4437 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4439 $$ = new_args($1, $3, $5, $7, $8);
4441 | f_arg ',' f_optarg opt_args_tail
4443 $$ = new_args($1, $3, Qnone, Qnone, $4);
4445 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4447 $$ = new_args($1, $3, Qnone, $5, $6);
4449 | f_arg ',' f_rest_arg opt_args_tail
4451 $$ = new_args($1, Qnone, $3, Qnone, $4);
4453 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4455 $$ = new_args($1, Qnone, $3, $5, $6);
4457 | f_arg opt_args_tail
4459 $$ = new_args($1, Qnone, Qnone, Qnone, $2);
4461 | f_optarg ',' f_rest_arg opt_args_tail
4463 $$ = new_args(Qnone, $1, $3, Qnone, $4);
4465 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4467 $$ = new_args(Qnone, $1, $3, $5, $6);
4469 | f_optarg opt_args_tail
4471 $$ = new_args(Qnone, $1, Qnone, Qnone, $2);
4473 | f_optarg ',' f_arg opt_args_tail
4475 $$ = new_args(Qnone, $1, Qnone, $3, $4);
4477 | f_rest_arg opt_args_tail
4479 $$ = new_args(Qnone, Qnone, $1, Qnone, $2);
4481 | f_rest_arg ',' f_arg opt_args_tail
4483 $$ = new_args(Qnone, Qnone, $1, $3, $4);
4487 $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1);
4491 $$ = new_args_tail(Qnone, Qnone, Qnone);
4492 $$ = new_args(Qnone, Qnone, Qnone, Qnone, $$);
4496 f_bad_arg : tCONSTANT
4499 yyerror("formal argument cannot be a constant");
4502 $$ = dispatch1(param_error, $1);
4509 yyerror("formal argument cannot be an instance variable");
4512 $$ = dispatch1(param_error, $1);
4519 yyerror("formal argument cannot be a global variable");
4522 $$ = dispatch1(param_error, $1);
4529 yyerror("formal argument cannot be a class variable");
4532 $$ = dispatch1(param_error, $1);
4538 f_norm_arg : f_bad_arg
4541 formal_argument(get_id($1));
4546 f_arg_asgn : f_norm_arg
4555 f_arg_item : f_arg_asgn
4559 $$ = NEW_ARGS_AUX($1, 1);
4564 | tLPAREN f_margs rparen
4566 ID tid = internal_id();
4569 if (dyna_in_block()) {
4570 $2->nd_value = NEW_DVAR(tid);
4573 $2->nd_value = NEW_LVAR(tid);
4575 $$ = NEW_ARGS_AUX(tid, 1);
4578 $$ = dispatch1(mlhs_paren, $2);
4587 $$ = rb_ary_new3(1, $1);
4590 | f_arg ',' f_arg_item
4595 $$->nd_next = block_append($$->nd_next, $3->nd_next);
4596 rb_gc_force_recycle((VALUE)$3);
4598 $$ = rb_ary_push($1, $3);
4607 arg_var(formal_argument(id));
4613 f_kw : f_label arg_value
4616 $$ = assignable($1, $2);
4618 $$ = new_kw_arg($$);
4620 $$ = rb_assoc_new($$, $2);
4626 $$ = assignable($1, (NODE *)-1);
4628 $$ = new_kw_arg($$);
4630 $$ = rb_assoc_new($$, 0);
4635 f_block_kw : f_label primary_value
4637 $$ = assignable($1, $2);
4639 $$ = new_kw_arg($$);
4641 $$ = rb_assoc_new($$, $2);
4646 $$ = assignable($1, (NODE *)-1);
4648 $$ = new_kw_arg($$);
4650 $$ = rb_assoc_new($$, 0);
4655 f_block_kwarg : f_block_kw
4660 $$ = rb_ary_new3(1, $1);
4663 | f_block_kwarg ',' f_block_kw
4666 $$ = kwd_append($1, $3);
4668 $$ = rb_ary_push($1, $3);
4679 $$ = rb_ary_new3(1, $1);
4685 $$ = kwd_append($1, $3);
4687 $$ = rb_ary_push($1, $3);
4696 f_kwrest : kwrest_mark tIDENTIFIER
4698 shadowing_lvar(get_id($2));
4708 f_opt : f_arg_asgn '=' arg_value
4711 $$ = assignable($1, $3);
4713 $$ = NEW_OPT_ARG(0, $$);
4715 $$ = rb_assoc_new($$, $3);
4720 f_block_opt : f_arg_asgn '=' primary_value
4723 $$ = assignable($1, $3);
4725 $$ = NEW_OPT_ARG(0, $$);
4727 $$ = rb_assoc_new($$, $3);
4732 f_block_optarg : f_block_opt
4737 $$ = rb_ary_new3(1, $1);
4740 | f_block_optarg ',' f_block_opt
4745 while (opts->nd_next) {
4746 opts = opts->nd_next;
4751 $$ = rb_ary_push($1, $3);
4761 $$ = rb_ary_new3(1, $1);
4764 | f_optarg ',' f_opt
4769 while (opts->nd_next) {
4770 opts = opts->nd_next;
4775 $$ = rb_ary_push($1, $3);
4784 f_rest_arg : restarg_mark tIDENTIFIER
4787 if (!is_local_id($2))
4788 yyerror("rest argument must be local variable");
4790 arg_var(shadowing_lvar(get_id($2)));
4794 $$ = dispatch1(rest_param, $2);
4803 $$ = dispatch1(rest_param, Qnil);
4812 f_block_arg : blkarg_mark tIDENTIFIER
4815 if (!is_local_id($2))
4816 yyerror("block argument must be local variable");
4817 else if (!dyna_in_block() && local_id($2))
4818 yyerror("duplicated block argument name");
4820 arg_var(shadowing_lvar(get_id($2)));
4824 $$ = dispatch1(blockarg, $2);
4829 opt_f_block_arg : ',' f_block_arg
4848 if (!$$) $$ = NEW_NIL();
4853 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
4857 yyerror("can't define singleton method for ().");
4860 switch (nd_type($3)) {
4869 yyerror("can't define singleton method for literals");
4877 $$ = dispatch1(paren, $3);
4888 $$ = dispatch1(assoclist_from_args, $1);
4897 $$ = rb_ary_new3(1, $1);
4909 if (assocs->nd_head &&
4910 !tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY &&
4911 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
4913 tail = tail->nd_next->nd_head->nd_head;
4915 assocs = list_concat(assocs, tail);
4919 $$ = rb_ary_push($1, $3);
4924 assoc : arg_value tASSOC arg_value
4927 if (nd_type($1) == NODE_STR) {
4928 nd_set_type($1, NODE_LIT);
4929 $1->nd_lit = rb_fstring($1->nd_lit);
4931 $$ = list_append(NEW_LIST($1), $3);
4933 $$ = dispatch2(assoc_new, $1, $3);
4939 $$ = list_append(NEW_LIST(NEW_LIT(ID2SYM($1))), $2);
4941 $$ = dispatch2(assoc_new, $1, $2);
4944 | tSTRING_BEG string_contents tLABEL_END arg_value
4947 $$ = list_append(NEW_LIST(dsym_node($2)), $4);
4949 $$ = dispatch2(assoc_new, dispatch1(dyna_symbol, $2), $4);
4955 if (nd_type($2) == NODE_HASH &&
4956 !($2->nd_head && $2->nd_head->nd_alen))
4959 $$ = list_append(NEW_LIST(0), $2);
4961 $$ = dispatch1(assoc_splat, $2);
4966 operation : tIDENTIFIER
4971 operation2 : tIDENTIFIER
4977 operation3 : tIDENTIFIER
4999 $$ = ripper_id2sym('.');
5007 $$ = ripper_id2sym(idANDDOT);
5018 $$ = ripper_id2sym(idCOLON2);
5023 opt_terms : /* none */
5034 rbracket : opt_nl ']'
5037 trailer : /* none */
5042 term : ';' {yyerrok;}
5047 | terms ';' {yyerrok;}
5063 # define yylval (*parser->lval)
5065 static int parser_regx_options(struct parser_params*);
5066 static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**);
5067 static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc);
5068 static int parser_parse_string(struct parser_params*,NODE*);
5069 static int parser_here_document(struct parser_params*,NODE*);
5072 # define nextc() parser_nextc(parser)
5073 # define pushback(c) parser_pushback(parser, (c))
5074 # define newtok() parser_newtok(parser)
5075 # define tokspace(n) parser_tokspace(parser, (n))
5076 # define tokadd(c) parser_tokadd(parser, (c))
5077 # define tok_hex(numlen) parser_tok_hex(parser, (numlen))
5078 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e))
5079 # define tokadd_escape(e) parser_tokadd_escape(parser, (e))
5080 # define regx_options() parser_regx_options(parser)
5081 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e))
5082 # define parse_string(n) parser_parse_string(parser,(n))
5083 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc))
5084 # define here_document(n) parser_here_document(parser,(n))
5085 # define heredoc_identifier() parser_heredoc_identifier(parser)
5086 # define heredoc_restore(n) parser_heredoc_restore(parser,(n))
5087 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i))
5088 # define number_literal_suffix(f) parser_number_literal_suffix(parser, (f))
5089 # define set_number_literal(v, t, f) parser_set_number_literal(parser, (v), (t), (f))
5090 # define set_integer_literal(v, f) parser_set_integer_literal(parser, (v), (f))
5093 # define set_yylval_str(x) (yylval.node = NEW_STR(x))
5094 # define set_yylval_num(x) (yylval.num = (x))
5095 # define set_yylval_id(x) (yylval.id = (x))
5096 # define set_yylval_name(x) (yylval.id = (x))
5097 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x))
5098 # define set_yylval_node(x) (yylval.node = (x))
5099 # define yylval_id() (yylval.id)
5102 ripper_yylval_id(ID x)
5104 return ripper_new_yylval(x, ID2SYM(x), 0);
5106 # define set_yylval_str(x) (yylval.val = (x))
5107 # define set_yylval_num(x) (yylval.val = ripper_new_yylval((x), 0, 0))
5108 # define set_yylval_id(x) (void)(x)
5109 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
5110 # define set_yylval_literal(x) (void)(x)
5111 # define set_yylval_node(x) (void)(x)
5112 # define yylval_id() yylval.id
5116 #define ripper_flush(p) (void)(p)
5117 #define dispatch_scan_event(t) ((void)0)
5118 #define dispatch_delayed_token(t) ((void)0)
5119 #define has_delayed_token() (0)
5121 #define ripper_flush(p) ((p)->tokp = (p)->lex.pcur)
5123 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5126 intern_sym(const char *name)
5128 ID id = rb_intern_const(name);
5133 ripper_has_scan_event(struct parser_params *parser)
5136 if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
5137 return lex_p > parser->tokp;
5141 ripper_scan_event_val(struct parser_params *parser, int t)
5143 VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp);
5144 VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
5145 ripper_flush(parser);
5150 ripper_dispatch_scan_event(struct parser_params *parser, int t)
5152 if (!ripper_has_scan_event(parser)) return;
5153 yylval_rval = ripper_scan_event_val(parser, t);
5155 #define dispatch_scan_event(t) ripper_dispatch_scan_event(parser, t)
5158 ripper_dispatch_delayed_token(struct parser_params *parser, int t)
5160 int saved_line = ruby_sourceline;
5161 const char *saved_tokp = parser->tokp;
5163 ruby_sourceline = parser->delayed_line;
5164 parser->tokp = lex_pbeg + parser->delayed_col;
5165 yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed);
5166 parser->delayed = Qnil;
5167 ruby_sourceline = saved_line;
5168 parser->tokp = saved_tokp;
5170 #define dispatch_delayed_token(t) ripper_dispatch_delayed_token(parser, t)
5171 #define has_delayed_token() (!NIL_P(parser->delayed))
5174 #include "ruby/regex.h"
5175 #include "ruby/util.h"
5177 #define parser_encoding_name() (current_enc->name)
5178 #define parser_mbclen() mbclen((lex_p-1),lex_pend,current_enc)
5179 #define is_identchar(p,e,enc) (rb_enc_isalnum((unsigned char)(*(p)),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
5180 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,current_enc))
5182 #define parser_isascii() ISASCII(*(lex_p-1))
5185 token_info_get_column(struct parser_params *parser, const char *pend)
5189 for (p = lex_pbeg; p < pend; p++) {
5191 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5199 token_info_has_nonspaces(struct parser_params *parser, const char *pend)
5202 for (p = lex_pbeg; p < pend; p++) {
5203 if (*p != ' ' && *p != '\t') {
5211 token_info_push_gen(struct parser_params *parser, const char *token, size_t len)
5214 const char *t = lex_p - len;
5216 if (!parser->token_info_enabled) return;
5217 ptinfo = ALLOC(token_info);
5218 ptinfo->token = token;
5219 ptinfo->linenum = ruby_sourceline;
5220 ptinfo->column = token_info_get_column(parser, t);
5221 ptinfo->nonspc = token_info_has_nonspaces(parser, t);
5222 ptinfo->next = parser->token_info;
5224 parser->token_info = ptinfo;
5228 token_info_pop_gen(struct parser_params *parser, const char *token, size_t len)
5231 token_info *ptinfo = parser->token_info;
5232 const char *t = lex_p - len;
5234 if (!ptinfo) return;
5235 parser->token_info = ptinfo->next;
5236 linenum = ruby_sourceline;
5237 if (parser->token_info_enabled &&
5238 linenum != ptinfo->linenum && !ptinfo->nonspc &&
5239 !token_info_has_nonspaces(parser, t) &&
5240 token_info_get_column(parser, t) != ptinfo->column) {
5242 "mismatched indentations at '%s' with '%s' at %d",
5243 WARN_S(token), WARN_S(ptinfo->token), WARN_I(ptinfo->linenum));
5250 parser_precise_mbclen(struct parser_params *parser, const char *p)
5252 int len = rb_enc_precise_mbclen(p, lex_pend, current_enc);
5253 if (!MBCLEN_CHARFOUND_P(len)) {
5254 compile_error(PARSER_ARG "invalid multibyte char (%s)", parser_encoding_name());
5261 parser_yyerror(struct parser_params *parser, const char *msg)
5264 const int max_line_margin = 30;
5266 const char *pre = "", *post = "";
5267 const char *code = "", *caret = "", *newline = "";
5274 lim = p - lex_pbeg > max_line_margin ? p - max_line_margin : lex_pbeg;
5276 if (*(p-1) == '\n') break;
5281 lim = lex_pend - pe > max_line_margin ? pe + max_line_margin : lex_pend;
5283 if (*pe == '\n') break;
5291 if (len > max_line_margin * 2 + 10) {
5292 if (lex_p - p > max_line_margin) {
5293 p = rb_enc_prev_char(p, lex_p - max_line_margin, pe, rb_enc_get(lex_lastline));
5296 if (pe - lex_p > max_line_margin) {
5297 pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, pe, rb_enc_get(lex_lastline));
5302 i = (int)(lex_p - p);
5303 buf = ALLOCA_N(char, i+2);
5307 *p2++ = *p++ == '\t' ? '\t' : ' ';
5316 compile_error(PARSER_ARG "%s%s""%s%.*s%s%s""%s%s",
5318 pre, (int)len, code, post, newline,
5321 dispatch1(parse_error, STR_NEW2(msg));
5323 #endif /* !RIPPER */
5327 static void parser_prepare(struct parser_params *parser);
5331 debug_lines(VALUE fname)
5334 CONST_ID(script_lines, "SCRIPT_LINES__");
5335 if (rb_const_defined_at(rb_cObject, script_lines)) {
5336 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5337 if (RB_TYPE_P(hash, T_HASH)) {
5338 VALUE lines = rb_ary_new();
5339 rb_hash_aset(hash, fname, lines);
5347 coverage(VALUE fname, int n)
5349 VALUE coverages = rb_get_coverages();
5350 if (RTEST(coverages) && RBASIC(coverages)->klass == 0) {
5351 VALUE lines = n > 0 ? rb_ary_tmp_new_fill(n) : rb_ary_tmp_new(0);
5352 rb_hash_aset(coverages, fname, lines);
5359 e_option_supplied(struct parser_params *parser)
5361 return strcmp(ruby_sourcefile, "-e") == 0;
5365 yycompile0(VALUE arg)
5369 struct parser_params *parser = (struct parser_params *)arg;
5372 if (!compile_for_eval && rb_safe_level() == 0) {
5373 ruby_debug_lines = debug_lines(ruby_sourcefile_string);
5374 if (ruby_debug_lines && ruby_sourceline > 0) {
5375 VALUE str = STR_NEW0();
5376 n = ruby_sourceline;
5378 rb_ary_push(ruby_debug_lines, str);
5382 if (!e_option_supplied(parser)) {
5383 ruby_coverage = coverage(ruby_sourcefile_string, ruby_sourceline);
5388 parser_prepare(parser);
5390 #define RUBY_DTRACE_PARSE_HOOK(name) \
5391 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5392 RUBY_DTRACE_PARSE_##name(ruby_sourcefile, ruby_sourceline); \
5394 RUBY_DTRACE_PARSE_HOOK(BEGIN);
5396 n = yyparse((void*)parser);
5398 RUBY_DTRACE_PARSE_HOOK(END);
5400 ruby_debug_lines = 0;
5404 lex_p = lex_pbeg = lex_pend = 0;
5405 lex_lastline = lex_nextline = 0;
5406 if (parser->error_p) {
5407 VALUE mesg = parser->error_buffer;
5409 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5411 rb_set_errinfo(mesg);
5414 tree = ruby_eval_tree;
5419 VALUE opt = parser->compile_option;
5420 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5421 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5422 tree->nd_body = NEW_PRELUDE(ruby_eval_tree_begin, tree->nd_body, opt);
5428 yycompile(struct parser_params *parser, VALUE fname, int line)
5430 ruby_sourcefile_string = rb_str_new_frozen(fname);
5431 ruby_sourcefile = RSTRING_PTR(fname);
5432 ruby_sourceline = line - 1;
5433 return (NODE *)rb_suppress_tracing(yycompile0, (VALUE)parser);
5435 #endif /* !RIPPER */
5437 static rb_encoding *
5438 must_be_ascii_compatible(VALUE s)
5440 rb_encoding *enc = rb_enc_get(s);
5441 if (!rb_enc_asciicompat(enc)) {
5442 rb_raise(rb_eArgError, "invalid source encoding");
5448 lex_get_str(struct parser_params *parser, VALUE s)
5450 char *beg, *end, *start;
5453 beg = RSTRING_PTR(s);
5454 len = RSTRING_LEN(s);
5457 if (len == lex_gets_ptr) return Qnil;
5458 beg += lex_gets_ptr;
5459 len -= lex_gets_ptr;
5461 end = memchr(beg, '\n', len);
5462 if (end) len = ++end - beg;
5463 lex_gets_ptr += len;
5464 return rb_str_subseq(s, beg - start, len);
5468 lex_getline(struct parser_params *parser)
5470 VALUE line = (*lex_gets)(parser, lex_input);
5471 if (NIL_P(line)) return line;
5472 must_be_ascii_compatible(line);
5474 if (ruby_debug_lines) {
5475 rb_enc_associate(line, current_enc);
5476 rb_ary_push(ruby_debug_lines, line);
5478 if (ruby_coverage) {
5479 rb_ary_push(ruby_coverage, Qnil);
5485 static const rb_data_type_t parser_data_type;
5489 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
5491 struct parser_params *parser;
5494 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
5495 lex_gets = lex_get_str;
5497 lex_input = rb_str_new_frozen(s);
5498 lex_pbeg = lex_p = lex_pend = 0;
5500 node = yycompile(parser, fname, line);
5501 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
5507 rb_compile_string(const char *f, VALUE s, int line)
5509 must_be_ascii_compatible(s);
5510 return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), s, line);
5514 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
5516 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
5520 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
5522 must_be_ascii_compatible(s);
5523 return parser_compile_string(vparser, f, s, line);
5527 rb_compile_cstr(const char *f, const char *s, int len, int line)
5529 VALUE str = rb_str_new(s, len);
5530 return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), str, line);
5534 rb_parser_compile_cstr(VALUE vparser, const char *f, const char *s, int len, int line)
5536 VALUE str = rb_str_new(s, len);
5537 return parser_compile_string(vparser, rb_filesystem_str_new_cstr(f), str, line);
5540 VALUE rb_io_gets_internal(VALUE io);
5543 lex_io_gets(struct parser_params *parser, VALUE io)
5545 return rb_io_gets_internal(io);
5549 rb_compile_file(const char *f, VALUE file, int start)
5551 VALUE vparser = rb_parser_new();
5553 return rb_parser_compile_file(vparser, f, file, start);
5557 rb_parser_compile_file(VALUE vparser, const char *f, VALUE file, int start)
5559 return rb_parser_compile_file_path(vparser, rb_filesystem_str_new_cstr(f), file, start);
5563 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
5565 struct parser_params *parser;
5568 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
5569 lex_gets = lex_io_gets;
5571 lex_pbeg = lex_p = lex_pend = 0;
5573 node = yycompile(parser, fname, start);
5574 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
5578 #endif /* !RIPPER */
5580 #define STR_FUNC_ESCAPE 0x01
5581 #define STR_FUNC_EXPAND 0x02
5582 #define STR_FUNC_REGEXP 0x04
5583 #define STR_FUNC_QWORDS 0x08
5584 #define STR_FUNC_SYMBOL 0x10
5585 #define STR_FUNC_INDENT 0x20
5586 #define STR_FUNC_LABEL 0x40
5587 #define STR_TERM_END -1
5590 str_label = STR_FUNC_LABEL,
5592 str_dquote = (STR_FUNC_EXPAND),
5593 str_xquote = (STR_FUNC_EXPAND),
5594 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
5595 str_sword = (STR_FUNC_QWORDS),
5596 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
5597 str_ssym = (STR_FUNC_SYMBOL),
5598 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
5602 parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *enc0)
5606 str = rb_enc_str_new(p, n, enc);
5607 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
5608 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
5610 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
5611 rb_enc_associate(str, rb_ascii8bit_encoding());
5618 #define lex_goto_eol(parser) ((parser)->lex.pcur = (parser)->lex.pend)
5619 #define lex_eol_p() (lex_p >= lex_pend)
5620 #define peek(c) peek_n((c), 0)
5621 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n])
5622 #define peekc() peekc_n(0)
5623 #define peekc_n(n) (lex_p+(n) < lex_pend ? (unsigned char)lex_p[n] : -1)
5626 parser_nextline(struct parser_params *parser)
5628 VALUE v = lex_nextline;
5634 if (!lex_input || NIL_P(v = lex_getline(parser))) {
5636 lex_goto_eol(parser);
5639 parser->cr_seen = FALSE;
5642 if (parser->tokp < lex_pend) {
5643 if (!has_delayed_token()) {
5644 parser->delayed = rb_str_buf_new(1024);
5645 rb_enc_associate(parser->delayed, current_enc);
5646 rb_str_buf_cat(parser->delayed,
5647 parser->tokp, lex_pend - parser->tokp);
5648 parser->delayed_line = ruby_sourceline;
5649 parser->delayed_col = (int)(parser->tokp - lex_pbeg);
5652 rb_str_buf_cat(parser->delayed,
5653 parser->tokp, lex_pend - parser->tokp);
5657 if (heredoc_end > 0) {
5658 ruby_sourceline = heredoc_end;
5662 parser->line_count++;
5663 lex_pbeg = lex_p = RSTRING_PTR(v);
5664 lex_pend = lex_p + RSTRING_LEN(v);
5665 ripper_flush(parser);
5671 parser_cr(struct parser_params *parser, int c)
5677 else if (!parser->cr_seen) {
5678 parser->cr_seen = TRUE;
5679 /* carried over with lex_nextline for nextc() */
5680 rb_warn0("encountered \\r in middle of line, treated as a mere space");
5686 parser_nextc(struct parser_params *parser)
5690 if (UNLIKELY(lex_p == lex_pend)) {
5691 if (parser_nextline(parser)) return -1;
5693 c = (unsigned char)*lex_p++;
5694 if (UNLIKELY(c == '\r')) {
5695 c = parser_cr(parser, c);
5702 parser_pushback(struct parser_params *parser, int c)
5704 if (c == -1) return;
5706 if (lex_p > lex_pbeg && lex_p[0] == '\n' && lex_p[-1] == '\r') {
5711 #define was_bol() (lex_p == lex_pbeg + 1)
5713 #define tokfix() (tokenbuf[tokidx]='\0')
5714 #define tok() tokenbuf
5715 #define toklen() tokidx
5716 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
5719 parser_newtok(struct parser_params *parser)
5722 tokline = ruby_sourceline;
5725 tokenbuf = ALLOC_N(char, 60);
5727 if (toksiz > 4096) {
5729 REALLOC_N(tokenbuf, char, 60);
5735 parser_tokspace(struct parser_params *parser, int n)
5739 if (tokidx >= toksiz) {
5740 do {toksiz *= 2;} while (toksiz < tokidx);
5741 REALLOC_N(tokenbuf, char, toksiz);
5743 return &tokenbuf[tokidx-n];
5747 parser_tokadd(struct parser_params *parser, int c)
5749 tokenbuf[tokidx++] = (char)c;
5750 if (tokidx >= toksiz) {
5752 REALLOC_N(tokenbuf, char, toksiz);
5757 parser_tok_hex(struct parser_params *parser, size_t *numlen)
5761 c = scan_hex(lex_p, 2, numlen);
5763 yyerror("invalid hex escape");
5770 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5773 parser_tokadd_codepoint(struct parser_params *parser, rb_encoding **encp,
5774 int regexp_literal, int wide)
5777 int codepoint = scan_hex(lex_p, wide ? 6 : 4, &numlen);
5778 if (wide ? (numlen == 0) : (numlen < 4)) {
5779 yyerror("invalid Unicode escape");
5782 if (codepoint > 0x10ffff) {
5783 yyerror("invalid Unicode codepoint (too large)");
5786 if ((codepoint & 0xfffff800) == 0xd800) {
5787 yyerror("invalid Unicode codepoint");
5791 if (regexp_literal) {
5792 tokcopy((int)numlen);
5794 else if (codepoint >= 0x80) {
5795 *encp = rb_utf8_encoding();
5796 tokaddmbc(codepoint, *encp);
5804 /* return value is for ?\u3042 */
5806 parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp,
5807 int string_literal, int symbol_literal, int regexp_literal)
5810 * If string_literal is true, then we allow multiple codepoints
5811 * in \u{}, and add the codepoints to the current token.
5812 * Otherwise we're parsing a character literal and return a single
5813 * codepoint without adding it
5816 const int open_brace = '{', close_brace = '}';
5818 if (regexp_literal) { tokadd('\\'); tokadd('u'); }
5820 if (peek(open_brace)) { /* handle \u{...} form */
5821 int c, last = nextc();
5822 do c = nextc(); while (ISSPACE(c));
5824 while (!string_literal || c != close_brace) {
5825 if (regexp_literal) tokadd(last);
5826 if (!parser_tokadd_codepoint(parser, encp, regexp_literal, TRUE)) {
5829 while (ISSPACE(c = nextc())) last = c;
5831 if (!string_literal) break;
5834 if (c != close_brace) {
5835 yyerror("unterminated Unicode escape");
5839 if (regexp_literal) tokadd(close_brace);
5842 else { /* handle \uxxxx form */
5843 if (!parser_tokadd_codepoint(parser, encp, regexp_literal, FALSE)) {
5851 #define ESCAPE_CONTROL 1
5852 #define ESCAPE_META 2
5855 parser_read_escape(struct parser_params *parser, int flags,
5861 switch (c = nextc()) {
5862 case '\\': /* Backslash */
5865 case 'n': /* newline */
5868 case 't': /* horizontal tab */
5871 case 'r': /* carriage-return */
5874 case 'f': /* form-feed */
5877 case 'v': /* vertical tab */
5880 case 'a': /* alarm(bell) */
5883 case 'e': /* escape */
5886 case '0': case '1': case '2': case '3': /* octal constant */
5887 case '4': case '5': case '6': case '7':
5889 c = scan_oct(lex_p, 3, &numlen);
5893 case 'x': /* hex constant */
5894 c = tok_hex(&numlen);
5895 if (numlen == 0) return 0;
5898 case 'b': /* backspace */
5901 case 's': /* space */
5905 if (flags & ESCAPE_META) goto eof;
5906 if ((c = nextc()) != '-') {
5910 if ((c = nextc()) == '\\') {
5911 if (peek('u')) goto eof;
5912 return read_escape(flags|ESCAPE_META, encp) | 0x80;
5914 else if (c == -1 || !ISASCII(c)) goto eof;
5916 return ((c & 0xff) | 0x80);
5920 if ((c = nextc()) != '-') {
5925 if (flags & ESCAPE_CONTROL) goto eof;
5926 if ((c = nextc())== '\\') {
5927 if (peek('u')) goto eof;
5928 c = read_escape(flags|ESCAPE_CONTROL, encp);
5932 else if (c == -1 || !ISASCII(c)) goto eof;
5937 yyerror("Invalid escape character syntax");
5946 parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc)
5948 int len = rb_enc_codelen(c, enc);
5949 rb_enc_mbcput(c, tokspace(len), enc);
5953 parser_tokadd_escape(struct parser_params *parser, rb_encoding **encp)
5960 switch (c = nextc()) {
5962 return 0; /* just ignore */
5964 case '0': case '1': case '2': case '3': /* octal constant */
5965 case '4': case '5': case '6': case '7':
5967 ruby_scan_oct(--lex_p, 3, &numlen);
5968 if (numlen == 0) goto eof;
5970 tokcopy((int)numlen + 1);
5974 case 'x': /* hex constant */
5977 if (numlen == 0) return -1;
5978 tokcopy((int)numlen + 2);
5983 if (flags & ESCAPE_META) goto eof;
5984 if ((c = nextc()) != '-') {
5989 flags |= ESCAPE_META;
5993 if (flags & ESCAPE_CONTROL) goto eof;
5994 if ((c = nextc()) != '-') {
6002 if (flags & ESCAPE_CONTROL) goto eof;
6004 flags |= ESCAPE_CONTROL;
6006 if ((c = nextc()) == '\\') {
6009 else if (c == -1) goto eof;
6015 yyerror("Invalid escape character syntax");
6026 parser_regx_options(struct parser_params *parser)
6034 while (c = nextc(), ISALPHA(c)) {
6036 options |= RE_OPTION_ONCE;
6038 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6040 if (kc != rb_ascii8bit_encindex()) kcode = c;
6055 compile_error(PARSER_ARG "unknown regexp option%s - %s",
6056 toklen() > 1 ? "s" : "", tok());
6058 return options | RE_OPTION_ENCODING(kcode);
6062 dispose_string(VALUE str)
6065 rb_gc_force_recycle(str);
6069 parser_tokadd_mbchar(struct parser_params *parser, int c)
6071 int len = parser_precise_mbclen(parser, lex_p-1);
6072 if (len < 0) return -1;
6075 if (len > 0) tokcopy(len);
6079 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
6082 simple_re_meta(int c)
6085 case '$': case '*': case '+': case '.':
6086 case '?': case '^': case '|':
6087 case ')': case ']': case '}': case '>':
6095 parser_update_heredoc_indent(struct parser_params *parser, int c)
6097 if (heredoc_line_indent == -1) {
6098 if (c == '\n') heredoc_line_indent = 0;
6102 heredoc_line_indent++;
6105 else if (c == '\t') {
6106 int w = (heredoc_line_indent / TAB_WIDTH) + 1;
6107 heredoc_line_indent = w * TAB_WIDTH;
6110 else if (c != '\n') {
6111 if (heredoc_indent > heredoc_line_indent) {
6112 heredoc_indent = heredoc_line_indent;
6114 heredoc_line_indent = -1;
6121 parser_tokadd_string(struct parser_params *parser,
6122 int func, int term, int paren, long *nest,
6126 int has_nonascii = 0;
6127 rb_encoding *enc = *encp;
6129 static const char mixed_msg[] = "%s mixed within %s source";
6131 #define mixed_error(enc1, enc2) if (!errbuf) { \
6132 size_t len = sizeof(mixed_msg) - 4; \
6133 len += strlen(rb_enc_name(enc1)); \
6134 len += strlen(rb_enc_name(enc2)); \
6135 errbuf = ALLOCA_N(char, len); \
6136 snprintf(errbuf, len, mixed_msg, \
6137 rb_enc_name(enc1), \
6138 rb_enc_name(enc2)); \
6141 #define mixed_escape(beg, enc1, enc2) do { \
6142 const char *pos = lex_p; \
6144 mixed_error((enc1), (enc2)); \
6148 while ((c = nextc()) != -1) {
6149 if (heredoc_indent > 0) {
6150 parser_update_heredoc_indent(parser, c);
6153 if (paren && c == paren) {
6156 else if (c == term) {
6157 if (!nest || !*nest) {
6163 else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) {
6165 if (c2 == '$' || c2 == '@' || c2 == '{') {
6170 else if (c == '\\') {
6171 const char *beg = lex_p - 1;
6175 if (func & STR_FUNC_QWORDS) break;
6176 if (func & STR_FUNC_EXPAND) continue;
6181 if (func & STR_FUNC_ESCAPE) tokadd(c);
6185 if ((func & STR_FUNC_EXPAND) == 0) {
6189 parser_tokadd_utf8(parser, &enc, 1,
6190 func & STR_FUNC_SYMBOL,
6191 func & STR_FUNC_REGEXP);
6192 if (has_nonascii && enc != *encp) {
6193 mixed_escape(beg, enc, *encp);
6198 if (c == -1) return -1;
6200 if ((func & STR_FUNC_EXPAND) == 0) tokadd('\\');
6203 if (func & STR_FUNC_REGEXP) {
6204 if (c == term && !simple_re_meta(c)) {
6209 if ((c = tokadd_escape(&enc)) < 0)
6211 if (has_nonascii && enc != *encp) {
6212 mixed_escape(beg, enc, *encp);
6216 else if (func & STR_FUNC_EXPAND) {
6218 if (func & STR_FUNC_ESCAPE) tokadd('\\');
6219 c = read_escape(0, &enc);
6221 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6222 /* ignore backslashed spaces in %w */
6224 else if (c != term && !(paren && c == paren)) {
6231 else if (!parser_isascii()) {
6235 mixed_error(enc, *encp);
6238 if (tokadd_mbchar(c) == -1) return -1;
6241 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6248 mixed_error(enc, *encp);
6258 #define NEW_STRTERM(func, term, paren) \
6259 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
6263 ripper_flush_string_content(struct parser_params *parser, rb_encoding *enc)
6265 VALUE content = yylval.val;
6266 if (!ripper_is_node_yylval(content))
6267 content = ripper_new_yylval(0, 0, content);
6268 if (has_delayed_token()) {
6269 ptrdiff_t len = lex_p - parser->tokp;
6271 rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc);
6273 dispatch_delayed_token(tSTRING_CONTENT);
6274 parser->tokp = lex_p;
6275 RNODE(content)->nd_rval = yylval.val;
6277 dispatch_scan_event(tSTRING_CONTENT);
6278 if (yylval.val != content)
6279 RNODE(content)->nd_rval = yylval.val;
6280 yylval.val = content;
6283 #define flush_string_content(enc) ripper_flush_string_content(parser, (enc))
6285 #define flush_string_content(enc) ((void)(enc))
6288 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6289 /* this can be shared with ripper, since it's independent from struct
6292 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6293 #define SPECIAL_PUNCT(idx) ( \
6294 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6295 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6296 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6297 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6298 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6300 const unsigned int ruby_global_name_punct_bits[] = {
6306 #undef SPECIAL_PUNCT
6310 parser_peek_variable_name(struct parser_params *parser)
6313 const char *p = lex_p;
6315 if (p + 1 >= lex_pend) return 0;
6319 if ((c = *p) == '-') {
6320 if (++p >= lex_pend) return 0;
6323 else if (is_global_name_punct(c) || ISDIGIT(c)) {
6324 return tSTRING_DVAR;
6328 if ((c = *p) == '@') {
6329 if (++p >= lex_pend) return 0;
6335 command_start = TRUE;
6336 return tSTRING_DBEG;
6340 if (!ISASCII(c) || c == '_' || ISALPHA(c))
6341 return tSTRING_DVAR;
6346 parser_string_term(struct parser_params *parser, int func)
6348 if (!(func & STR_FUNC_REGEXP)) return tSTRING_END;
6349 set_yylval_num(regx_options());
6350 dispatch_scan_event(tREGEXP_END);
6355 parser_parse_string(struct parser_params *parser, NODE *quote)
6357 int func = (int)quote->nd_func;
6358 int term = nd_term(quote);
6359 int paren = nd_paren(quote);
6361 rb_encoding *enc = current_enc;
6363 if (term == STR_TERM_END) return tSTRING_END;
6365 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6366 do {c = nextc();} while (ISSPACE(c));
6369 if (c == term && !quote->nd_nest) {
6370 if (func & STR_FUNC_QWORDS) {
6371 quote->u2.id = STR_TERM_END;
6374 return parser_string_term(parser, func);
6381 if ((func & STR_FUNC_EXPAND) && c == '#') {
6382 int t = parser_peek_variable_name(parser);
6388 if (tokadd_string(func, term, paren, "e->nd_nest,
6390 ruby_sourceline = nd_line(quote);
6391 if (func & STR_FUNC_REGEXP) {
6393 compile_error(PARSER_ARG "unterminated regexp meets end of file");
6398 compile_error(PARSER_ARG "unterminated string meets end of file");
6404 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
6405 flush_string_content(enc);
6407 return tSTRING_CONTENT;
6411 parser_heredoc_identifier(struct parser_params *parser)
6413 int c = nextc(), term, func = 0;
6414 int token = tSTRING_BEG;
6421 func = STR_FUNC_INDENT;
6423 else if (c == '~') {
6425 func = STR_FUNC_INDENT;
6430 func |= str_squote; goto quoted;
6432 func |= str_dquote; goto quoted;
6434 token = tXSTRING_BEG;
6435 func |= str_xquote; goto quoted;
6441 while ((c = nextc()) != -1 && c != term) {
6442 if (tokadd_mbchar(c) == -1) return 0;
6443 if (!newline && c == '\n') newline = 1;
6444 else if (newline) newline = 2;
6447 compile_error(PARSER_ARG "unterminated here document identifier");
6452 rb_warn0("here document identifier ends with a newline");
6453 if (--tokidx > 0 && tokenbuf[tokidx] == '\r') --tokidx;
6456 compile_error(PARSER_ARG "here document identifier across newlines, never match");
6462 if (!parser_is_identchar()) {
6464 if (func & STR_FUNC_INDENT) {
6465 pushback(indent > 0 ? '~' : '-');
6470 tokadd(func |= str_dquote);
6472 if (tokadd_mbchar(c) == -1) return 0;
6473 } while ((c = nextc()) != -1 && parser_is_identchar());
6479 dispatch_scan_event(tHEREDOC_BEG);
6480 len = lex_p - lex_pbeg;
6481 lex_goto_eol(parser);
6482 lex_strterm = rb_node_newnode(NODE_HEREDOC,
6483 STR_NEW(tok(), toklen()), /* nd_lit */
6485 lex_lastline); /* nd_orig */
6486 nd_set_line(lex_strterm, ruby_sourceline);
6487 ripper_flush(parser);
6488 heredoc_indent = indent;
6489 heredoc_line_indent = 0;
6494 parser_heredoc_restore(struct parser_params *parser, NODE *here)
6499 line = here->nd_orig;
6500 lex_lastline = line;
6501 lex_pbeg = RSTRING_PTR(line);
6502 lex_pend = lex_pbeg + RSTRING_LEN(line);
6503 lex_p = lex_pbeg + here->nd_nth;
6504 heredoc_end = ruby_sourceline;
6505 ruby_sourceline = nd_line(here);
6506 dispose_string(here->nd_lit);
6507 rb_gc_force_recycle((VALUE)here);
6508 ripper_flush(parser);
6512 dedent_string(VALUE string, int width)
6518 RSTRING_GETMEM(string, str, len);
6519 for (i = 0; i < len && col < width; i++) {
6520 if (str[i] == ' ') {
6523 else if (str[i] == '\t') {
6524 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
6525 if (n > width) break;
6533 rb_str_modify(string);
6534 str = RSTRING_PTR(string);
6535 if (RSTRING_LEN(string) != len)
6536 rb_fatal("literal string changed: %+"PRIsVALUE, string);
6537 MEMMOVE(str, str + i, char, len - i);
6538 rb_str_set_len(string, len - i);
6544 parser_heredoc_dedent(struct parser_params *parser, NODE *root)
6546 NODE *node, *str_node;
6548 int indent = heredoc_indent;
6550 if (indent <= 0) return root;
6552 if (!root) return root;
6554 node = str_node = root;
6555 if (nd_type(root) == NODE_ARRAY) str_node = root->nd_head;
6558 VALUE lit = str_node->nd_lit;
6559 if (bol) dedent_string(lit, indent);
6563 while ((node = node->nd_next) != 0 && nd_type(node) == NODE_ARRAY) {
6564 if ((str_node = node->nd_head) != 0) {
6565 enum node_type type = nd_type(str_node);
6566 if (type == NODE_STR || type == NODE_DSTR) break;
6576 parser_heredoc_dedent(struct parser_params *parser, VALUE array)
6578 int indent = heredoc_indent;
6580 if (indent <= 0) return array;
6582 dispatch2(heredoc_dedent, array, INT2NUM(indent));
6587 parser_dedent_string(VALUE self, VALUE input, VALUE width)
6592 wid = NUM2UINT(width);
6593 col = dedent_string(input, wid);
6594 return INT2NUM(col);
6599 parser_whole_match_p(struct parser_params *parser,
6600 const char *eos, long len, int indent)
6602 const char *p = lex_pbeg;
6606 while (*p && ISSPACE(*p)) p++;
6608 n = lex_pend - (p + len);
6609 if (n < 0) return FALSE;
6610 if (n > 0 && p[len] != '\n') {
6611 if (p[len] != '\r') return FALSE;
6612 if (n <= 1 || p[len+1] != '\n') return FALSE;
6614 return strncmp(eos, p, len) == 0;
6617 #define NUM_SUFFIX_R (1<<0)
6618 #define NUM_SUFFIX_I (1<<1)
6619 #define NUM_SUFFIX_ALL 3
6622 parser_number_literal_suffix(struct parser_params *parser, int mask)
6625 const char *lastp = lex_p;
6627 while ((c = nextc()) != -1) {
6628 if ((mask & NUM_SUFFIX_I) && c == 'i') {
6629 result |= (mask & NUM_SUFFIX_I);
6630 mask &= ~NUM_SUFFIX_I;
6631 /* r after i, rational of complex is disallowed */
6632 mask &= ~NUM_SUFFIX_R;
6635 if ((mask & NUM_SUFFIX_R) && c == 'r') {
6636 result |= (mask & NUM_SUFFIX_R);
6637 mask &= ~NUM_SUFFIX_R;
6640 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
6648 yyerror("unexpected fraction part after numeric literal");
6650 while (parser_is_identchar()) nextc();
6659 parser_set_number_literal(struct parser_params *parser, VALUE v, int type, int suffix)
6661 if (suffix & NUM_SUFFIX_I) {
6662 v = rb_complex_raw(INT2FIX(0), v);
6665 set_yylval_literal(v);
6666 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
6671 parser_set_integer_literal(struct parser_params *parser, VALUE v, int suffix)
6673 int type = tINTEGER;
6674 if (suffix & NUM_SUFFIX_R) {
6675 v = rb_rational_raw1(v);
6678 return set_number_literal(v, type, suffix);
6683 ripper_dispatch_heredoc_end(struct parser_params *parser)
6686 if (has_delayed_token())
6687 dispatch_delayed_token(tSTRING_CONTENT);
6688 str = STR_NEW(parser->tokp, lex_pend - parser->tokp);
6689 ripper_dispatch1(parser, ripper_token2eventid(tHEREDOC_END), str);
6690 lex_goto_eol(parser);
6691 ripper_flush(parser);
6694 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser)
6696 #define dispatch_heredoc_end() ((void)0)
6700 parser_here_document(struct parser_params *parser, NODE *here)
6702 int c, func, indent = 0;
6703 const char *eos, *p, *pend;
6706 rb_encoding *enc = current_enc;
6708 eos = RSTRING_PTR(here->nd_lit);
6709 len = RSTRING_LEN(here->nd_lit) - 1;
6710 indent = (func = *eos++) & STR_FUNC_INDENT;
6712 if ((c = nextc()) == -1) {
6714 compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos);
6716 if (!has_delayed_token()) {
6717 dispatch_scan_event(tSTRING_CONTENT);
6721 rb_str_append(parser->delayed, str);
6723 else if ((len = lex_p - parser->tokp) > 0) {
6724 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6725 int cr = ENC_CODERANGE_UNKNOWN;
6726 rb_str_coderange_scan_restartable(parser->tokp, lex_p, enc, &cr);
6727 if (cr != ENC_CODERANGE_7BIT &&
6728 current_enc == rb_usascii_encoding() &&
6729 enc != rb_utf8_encoding()) {
6730 enc = rb_ascii8bit_encoding();
6733 rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc);
6735 dispatch_delayed_token(tSTRING_CONTENT);
6737 lex_goto_eol(parser);
6740 heredoc_restore(lex_strterm);
6743 if (was_bol() && whole_match_p(eos, len, indent)) {
6744 dispatch_heredoc_end();
6745 heredoc_restore(lex_strterm);
6749 if (!(func & STR_FUNC_EXPAND)) {
6751 p = RSTRING_PTR(lex_lastline);
6756 if (--pend == p || pend[-1] != '\r') {
6765 if (heredoc_indent > 0) {
6767 while (p + i < pend && parser_update_heredoc_indent(parser, p[i]))
6769 heredoc_line_indent = 0;
6773 rb_str_cat(str, p, pend - p);
6775 str = STR_NEW(p, pend - p);
6776 if (pend < lex_pend) rb_str_cat(str, "\n", 1);
6777 lex_goto_eol(parser);
6778 if (heredoc_indent > 0) {
6779 set_yylval_str(str);
6780 flush_string_content(enc);
6781 return tSTRING_CONTENT;
6783 if (nextc() == -1) {
6785 dispose_string(str);
6790 } while (!whole_match_p(eos, len, indent));
6793 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
6796 int t = parser_peek_variable_name(parser);
6803 if ((c = tokadd_string(func, '\n', 0, NULL, &enc)) == -1) {
6804 if (parser->eofp) goto error;
6809 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
6810 flush_string_content(enc);
6811 return tSTRING_CONTENT;
6814 if (heredoc_indent > 0) {
6815 lex_goto_eol(parser);
6818 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
6819 if ((c = nextc()) == -1) goto error;
6820 } while (!whole_match_p(eos, len, indent));
6821 str = STR_NEW3(tok(), toklen(), enc, func);
6823 dispatch_heredoc_end();
6825 str = ripper_new_yylval(ripper_token2eventid(tSTRING_CONTENT),
6828 heredoc_restore(lex_strterm);
6829 lex_strterm = NEW_STRTERM(func, STR_TERM_END, 0);
6830 set_yylval_str(str);
6831 return tSTRING_CONTENT;
6837 arg_ambiguous_gen(struct parser_params *parser, char c)
6840 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
6842 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
6845 #define arg_ambiguous(c) (arg_ambiguous_gen(parser, (c)), 1)
6848 formal_argument_gen(struct parser_params *parser, ID lhs)
6850 switch (id_type(lhs)) {
6855 yyerror("formal argument cannot be a constant");
6858 yyerror("formal argument cannot be an instance variable");
6861 yyerror("formal argument cannot be a global variable");
6864 yyerror("formal argument cannot be a class variable");
6867 yyerror("formal argument must be local variable");
6871 lhs = dispatch1(param_error, lhs);
6876 shadowing_lvar(lhs);
6881 lvar_defined_gen(struct parser_params *parser, ID id)
6883 return (dyna_in_block() && dvar_defined_get(id)) || local_id(id);
6886 /* emacsen -*- hack */
6888 parser_encode_length(struct parser_params *parser, const char *name, long len)
6892 if (len > 5 && name[nlen = len - 5] == '-') {
6893 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
6896 if (len > 4 && name[nlen = len - 4] == '-') {
6897 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
6899 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
6900 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
6901 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
6908 parser_set_encode(struct parser_params *parser, const char *name)
6910 int idx = rb_enc_find_index(name);
6915 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
6917 excargs[0] = rb_eArgError;
6918 excargs[2] = rb_make_backtrace();
6919 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", ruby_sourcefile_string, ruby_sourceline));
6920 rb_exc_raise(rb_make_exception(3, excargs));
6922 enc = rb_enc_from_index(idx);
6923 if (!rb_enc_asciicompat(enc)) {
6924 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
6929 if (ruby_debug_lines) {
6930 VALUE lines = ruby_debug_lines;
6931 long i, n = RARRAY_LEN(lines);
6932 for (i = 0; i < n; ++i) {
6933 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
6940 comment_at_top(struct parser_params *parser)
6942 const char *p = lex_pbeg, *pend = lex_p - 1;
6943 if (parser->line_count != (parser->has_shebang ? 2 : 1)) return 0;
6945 if (!ISSPACE(*p)) return 0;
6951 typedef long (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, long len);
6952 typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val);
6955 magic_comment_encoding(struct parser_params *parser, const char *name, const char *val)
6957 if (!comment_at_top(parser)) {
6960 parser_set_encode(parser, val);
6964 parser_get_bool(struct parser_params *parser, const char *name, const char *val)
6968 if (strcasecmp(val, "true") == 0) {
6973 if (strcasecmp(val, "false") == 0) {
6978 rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val);
6983 parser_set_token_info(struct parser_params *parser, const char *name, const char *val)
6985 int b = parser_get_bool(parser, name, val);
6986 if (b >= 0) parser->token_info_enabled = b;
6990 parser_set_compile_option_flag(struct parser_params *parser, const char *name, const char *val)
6994 if (parser->token_seen) {
6995 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
6999 b = parser_get_bool(parser, name, val);
7002 if (!parser->compile_option)
7003 parser->compile_option = rb_obj_hide(rb_ident_hash_new());
7004 rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)),
7005 (b ? Qtrue : Qfalse));
7008 # if WARN_PAST_SCOPE
7010 parser_set_past_scope(struct parser_params *parser, const char *name, const char *val)
7012 int b = parser_get_bool(parser, name, val);
7013 if (b >= 0) parser->past_scope_enabled = b;
7017 struct magic_comment {
7019 rb_magic_comment_setter_t func;
7020 rb_magic_comment_length_t length;
7023 static const struct magic_comment magic_comments[] = {
7024 {"coding", magic_comment_encoding, parser_encode_length},
7025 {"encoding", magic_comment_encoding, parser_encode_length},
7026 {"frozen_string_literal", parser_set_compile_option_flag},
7027 {"warn_indent", parser_set_token_info},
7028 # if WARN_PAST_SCOPE
7029 {"warn_past_scope", parser_set_past_scope},
7034 magic_comment_marker(const char *str, long len)
7041 if (str[i-1] == '*' && str[i-2] == '-') {
7047 if (i + 1 >= len) return 0;
7048 if (str[i+1] != '-') {
7051 else if (str[i-1] != '-') {
7067 parser_magic_comment(struct parser_params *parser, const char *str, long len)
7070 VALUE name = 0, val = 0;
7071 const char *beg, *end, *vbeg, *vend;
7072 #define str_copy(_s, _p, _n) ((_s) \
7073 ? (void)(rb_str_resize((_s), (_n)), \
7074 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7075 : (void)((_s) = STR_NEW((_p), (_n))))
7077 if (len <= 7) return FALSE;
7078 if (!!(beg = magic_comment_marker(str, len))) {
7079 if (!(end = magic_comment_marker(beg, str + len - beg)))
7083 len = end - beg - 3;
7086 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7088 const struct magic_comment *p = magic_comments;
7093 for (; len > 0 && *str; str++, --len) {
7095 case '\'': case '"': case ':': case ';':
7098 if (!ISSPACE(*str)) break;
7100 for (beg = str; len > 0; str++, --len) {
7102 case '\'': case '"': case ':': case ';':
7105 if (ISSPACE(*str)) break;
7110 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7113 if (!indicator) return FALSE;
7117 do str++; while (--len > 0 && ISSPACE(*str));
7120 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7133 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7137 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7140 while (len > 0 && (ISSPACE(*str))) --len, str++;
7141 if (len) return FALSE;
7145 str_copy(name, beg, n);
7146 s = RSTRING_PTR(name);
7147 for (i = 0; i < n; ++i) {
7148 if (s[i] == '-') s[i] = '_';
7151 if (STRNCASECMP(p->name, s, n) == 0 && !p->name[n]) {
7154 n = (*p->length)(parser, vbeg, n);
7156 str_copy(val, vbeg, n);
7157 (*p->func)(parser, p->name, RSTRING_PTR(val));
7160 } while (++p < magic_comments + numberof(magic_comments));
7162 str_copy(val, vbeg, vend - vbeg);
7163 dispatch2(magic_comment, name, val);
7171 set_file_encoding(struct parser_params *parser, const char *str, const char *send)
7174 const char *beg = str;
7178 if (send - str <= 6) return;
7180 case 'C': case 'c': str += 6; continue;
7181 case 'O': case 'o': str += 5; continue;
7182 case 'D': case 'd': str += 4; continue;
7183 case 'I': case 'i': str += 3; continue;
7184 case 'N': case 'n': str += 2; continue;
7185 case 'G': case 'g': str += 1; continue;
7192 if (ISSPACE(*str)) break;
7195 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7199 if (++str >= send) return;
7200 } while (ISSPACE(*str));
7202 if (*str != '=' && *str != ':') return;
7207 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
7208 s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg));
7209 parser_set_encode(parser, RSTRING_PTR(s));
7210 rb_str_resize(s, 0);
7214 parser_prepare(struct parser_params *parser)
7219 if (peek('!')) parser->has_shebang = 1;
7221 case 0xef: /* UTF-8 BOM marker */
7222 if (lex_pend - lex_p >= 2 &&
7223 (unsigned char)lex_p[0] == 0xbb &&
7224 (unsigned char)lex_p[1] == 0xbf) {
7225 parser->enc = rb_utf8_encoding();
7235 parser->enc = rb_enc_get(lex_lastline);
7236 parser->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
7239 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7240 #define IS_END() IS_lex_state(EXPR_END_ANY)
7241 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7242 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7243 #define IS_LABEL_POSSIBLE() (\
7244 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7246 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1))
7247 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7250 #define ambiguous_operator(op, syn) ( \
7251 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
7252 rb_warning0("even though it seems like "syn""))
7254 #define ambiguous_operator(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn))
7256 #define warn_balanced(op, syn) ((void) \
7257 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
7258 space_seen && !ISSPACE(c) && \
7259 (ambiguous_operator(op, syn), 0)))
7262 parse_rational(struct parser_params *parser, char *str, int len, int seen_point)
7265 char *point = &str[seen_point];
7266 size_t fraclen = len-seen_point-1;
7267 memmove(point, point+1, fraclen+1);
7268 v = rb_cstr_to_inum(str, 10, FALSE);
7269 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
7273 parse_numeric(struct parser_params *parser, int c)
7275 int is_float, seen_point, seen_e, nondigit;
7278 is_float = seen_point = seen_e = nondigit = 0;
7279 SET_LEX_STATE(EXPR_END);
7281 if (c == '-' || c == '+') {
7286 #define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0)
7287 int start = toklen();
7289 if (c == 'x' || c == 'X') {
7292 if (c != -1 && ISXDIGIT(c)) {
7295 if (nondigit) break;
7299 if (!ISXDIGIT(c)) break;
7302 } while ((c = nextc()) != -1);
7306 if (toklen() == start) {
7309 else if (nondigit) goto trailing_uc;
7310 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7311 return set_integer_literal(rb_cstr_to_inum(tok(), 16, FALSE), suffix);
7313 if (c == 'b' || c == 'B') {
7316 if (c == '0' || c == '1') {
7319 if (nondigit) break;
7323 if (c != '0' && c != '1') break;
7326 } while ((c = nextc()) != -1);
7330 if (toklen() == start) {
7333 else if (nondigit) goto trailing_uc;
7334 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7335 return set_integer_literal(rb_cstr_to_inum(tok(), 2, FALSE), suffix);
7337 if (c == 'd' || c == 'D') {
7340 if (c != -1 && ISDIGIT(c)) {
7343 if (nondigit) break;
7347 if (!ISDIGIT(c)) break;
7350 } while ((c = nextc()) != -1);
7354 if (toklen() == start) {
7357 else if (nondigit) goto trailing_uc;
7358 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7359 return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix);
7365 if (c == 'o' || c == 'O') {
7366 /* prefixed octal */
7368 if (c == -1 || c == '_' || !ISDIGIT(c)) {
7372 if (c >= '0' && c <= '7') {
7377 if (nondigit) break;
7381 if (c < '0' || c > '9') break;
7382 if (c > '7') goto invalid_octal;
7385 } while ((c = nextc()) != -1);
7386 if (toklen() > start) {
7389 if (nondigit) goto trailing_uc;
7390 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7391 return set_integer_literal(rb_cstr_to_inum(tok(), 8, FALSE), suffix);
7398 if (c > '7' && c <= '9') {
7400 yyerror("Invalid octal digit");
7402 else if (c == '.' || c == 'e' || c == 'E') {
7407 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7408 return set_integer_literal(INT2FIX(0), suffix);
7414 case '0': case '1': case '2': case '3': case '4':
7415 case '5': case '6': case '7': case '8': case '9':
7421 if (nondigit) goto trailing_uc;
7422 if (seen_point || seen_e) {
7427 if (c0 == -1 || !ISDIGIT(c0)) {
7433 seen_point = toklen();
7452 if (c != '-' && c != '+' && !ISDIGIT(c)) {
7461 nondigit = (c == '-' || c == '+') ? c : 0;
7464 case '_': /* `_' in number just ignored */
7465 if (nondigit) goto decode_num;
7480 snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit);
7488 suffix = number_literal_suffix(seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
7489 if (suffix & NUM_SUFFIX_R) {
7491 v = parse_rational(parser, tok(), toklen(), seen_point);
7494 double d = strtod(tok(), 0);
7495 if (errno == ERANGE) {
7496 rb_warning1("Float %s out of range", WARN_S(tok()));
7501 return set_number_literal(v, type, suffix);
7503 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7504 return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix);
7508 parse_qmark(struct parser_params *parser, int space_seen)
7514 SET_LEX_STATE(EXPR_VALUE);
7519 compile_error(PARSER_ARG "incomplete character syntax");
7522 if (rb_enc_isspace(c, current_enc)) {
7546 rb_warn1("invalid character syntax; use ?\\%c", WARN_I(c2));
7551 SET_LEX_STATE(EXPR_VALUE);
7556 if (!parser_isascii()) {
7557 if (tokadd_mbchar(c) == -1) return 0;
7559 else if ((rb_enc_isalnum(c, current_enc) || c == '_') &&
7560 lex_p < lex_pend && is_identchar(lex_p, lex_pend, current_enc)) {
7562 const char *start = lex_p - 1, *p = start;
7564 int n = parser_precise_mbclen(parser, p);
7565 if (n < 0) return -1;
7567 } while (p < lex_pend && is_identchar(p, lex_pend, current_enc));
7568 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
7569 " a conditional operator, put a space after `?'",
7570 WARN_I((int)(p - start)), WARN_S_L(start, (p - start)));
7574 else if (c == '\\') {
7577 if (!parser_tokadd_utf8(parser, &enc, 0, 0, 0))
7580 else if (!lex_eol_p() && !(c = *lex_p, ISASCII(c))) {
7582 if (tokadd_mbchar(c) == -1) return 0;
7585 c = read_escape(0, &enc);
7593 set_yylval_str(STR_NEW3(tok(), toklen(), enc, 0));
7594 SET_LEX_STATE(EXPR_END);
7599 parse_percent(struct parser_params *parser, const int space_seen, const enum lex_state_e last_state)
7609 if (c == -1 || !ISALNUM(c)) {
7615 if (rb_enc_isalnum(term, current_enc) || !parser_isascii()) {
7616 yyerror("unknown type of %string");
7620 if (c == -1 || term == -1) {
7621 compile_error(PARSER_ARG "unterminated quoted string meets end of file");
7625 if (term == '(') term = ')';
7626 else if (term == '[') term = ']';
7627 else if (term == '{') term = '}';
7628 else if (term == '<') term = '>';
7633 lex_strterm = NEW_STRTERM(str_dquote, term, paren);
7637 lex_strterm = NEW_STRTERM(str_squote, term, paren);
7641 lex_strterm = NEW_STRTERM(str_dword, term, paren);
7642 do {c = nextc();} while (ISSPACE(c));
7647 lex_strterm = NEW_STRTERM(str_sword, term, paren);
7648 do {c = nextc();} while (ISSPACE(c));
7653 lex_strterm = NEW_STRTERM(str_dword, term, paren);
7654 do {c = nextc();} while (ISSPACE(c));
7656 return tSYMBOLS_BEG;
7659 lex_strterm = NEW_STRTERM(str_sword, term, paren);
7660 do {c = nextc();} while (ISSPACE(c));
7662 return tQSYMBOLS_BEG;
7665 lex_strterm = NEW_STRTERM(str_xquote, term, paren);
7666 return tXSTRING_BEG;
7669 lex_strterm = NEW_STRTERM(str_regexp, term, paren);
7673 lex_strterm = NEW_STRTERM(str_ssym, term, paren);
7674 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
7678 yyerror("unknown type of %string");
7682 if ((c = nextc()) == '=') {
7684 SET_LEX_STATE(EXPR_BEG);
7687 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
7690 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
7692 warn_balanced("%%", "string literal");
7697 tokadd_ident(struct parser_params *parser, int c)
7700 if (tokadd_mbchar(c) == -1) return -1;
7702 } while (parser_is_identchar());
7708 tokenize_ident(struct parser_params *parser, const enum lex_state_e last_state)
7710 ID ident = TOK_INTERN();
7712 set_yylval_name(ident);
7718 parse_numvar(struct parser_params *parser)
7722 unsigned long n = ruby_scan_digits(tok()+1, toklen()-1, 10, &len, &overflow);
7723 const unsigned long nth_ref_max =
7724 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
7725 /* NTH_REF is left-shifted to be ORed with back-ref flag and
7726 * turned into a Fixnum, in compile.c */
7728 if (overflow || n > nth_ref_max) {
7729 /* compile_error()? */
7730 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok()));
7731 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
7739 parse_gvar(struct parser_params *parser, const enum lex_state_e last_state)
7743 SET_LEX_STATE(EXPR_END);
7747 case '_': /* $_: last read line string */
7749 if (parser_is_identchar()) {
7757 case '~': /* $~: match-data */
7758 case '*': /* $*: argv */
7759 case '$': /* $$: pid */
7760 case '?': /* $?: last status */
7761 case '!': /* $!: error string */
7762 case '@': /* $@: error position */
7763 case '/': /* $/: input record separator */
7764 case '\\': /* $\: output record separator */
7765 case ';': /* $;: field separator */
7766 case ',': /* $,: output field separator */
7767 case '.': /* $.: last read line number */
7768 case '=': /* $=: ignorecase */
7769 case ':': /* $:: load path */
7770 case '<': /* $<: reading filename */
7771 case '>': /* $>: default output handle */
7772 case '\"': /* $": already loaded files */
7781 if (parser_is_identchar()) {
7782 if (tokadd_mbchar(c) == -1) return 0;
7790 set_yylval_name(TOK_INTERN());
7793 case '&': /* $&: last match */
7794 case '`': /* $`: string before last match */
7795 case '\'': /* $': string after last match */
7796 case '+': /* $+: string matches last paren. */
7797 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
7802 set_yylval_node(NEW_BACK_REF(c));
7805 case '1': case '2': case '3':
7806 case '4': case '5': case '6':
7807 case '7': case '8': case '9':
7812 } while (c != -1 && ISDIGIT(c));
7814 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
7816 set_yylval_node(NEW_NTH_REF(parse_numvar(parser)));
7820 if (!parser_is_identchar()) {
7821 if (c == -1 || ISSPACE(c)) {
7822 compile_error(PARSER_ARG "`$' without identifiers is not allowed as a global variable name");
7826 compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
7834 if (tokadd_ident(parser, c)) return 0;
7835 SET_LEX_STATE(EXPR_END);
7836 tokenize_ident(parser, last_state);
7841 parse_atmark(struct parser_params *parser, const enum lex_state_e last_state)
7844 register int c = nextc();
7853 if (c == -1 || ISSPACE(c)) {
7854 if (result == tIVAR) {
7855 compile_error(PARSER_ARG "`@' without identifiers is not allowed as an instance variable name");
7858 compile_error(PARSER_ARG "`@@' without identifiers is not allowed as a class variable name");
7862 else if (ISDIGIT(c) || !parser_is_identchar()) {
7864 if (result == tIVAR) {
7865 compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
7868 compile_error(PARSER_ARG "`@@%c' is not allowed as a class variable name", c);
7873 if (tokadd_ident(parser, c)) return 0;
7874 SET_LEX_STATE(EXPR_END);
7875 tokenize_ident(parser, last_state);
7880 parse_ident(struct parser_params *parser, int c, int cmd_state)
7883 int mb = ENC_CODERANGE_7BIT;
7884 const enum lex_state_e last_state = lex_state;
7888 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
7889 if (tokadd_mbchar(c) == -1) return 0;
7891 } while (parser_is_identchar());
7892 if ((c == '!' || c == '?') && !peek('=')) {
7900 if (toklast() == '!' || toklast() == '?') {
7904 if (IS_lex_state(EXPR_FNAME)) {
7905 register int c = nextc();
7906 if (c == '=' && !peek('~') && !peek('>') &&
7907 (!peek('=') || (peek_n('>', 1)))) {
7908 result = tIDENTIFIER;
7916 if (result == 0 && ISUPPER(tok()[0])) {
7920 result = tIDENTIFIER;
7924 if (IS_LABEL_POSSIBLE()) {
7925 if (IS_LABEL_SUFFIX(0)) {
7926 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7928 set_yylval_name(TOK_INTERN());
7932 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
7933 const struct kwtable *kw;
7935 /* See if it is a reserved word. */
7936 kw = rb_reserved_word(tok(), toklen());
7938 enum lex_state_e state = lex_state;
7939 SET_LEX_STATE(kw->state);
7940 if (IS_lex_state_for(state, EXPR_FNAME)) {
7941 set_yylval_name(rb_intern2(tok(), toklen()));
7944 if (IS_lex_state(EXPR_BEG)) {
7945 command_start = TRUE;
7947 if (kw->id[0] == keyword_do) {
7948 if (lambda_beginning_p()) {
7951 return keyword_do_LAMBDA;
7953 if (COND_P()) return keyword_do_cond;
7954 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
7955 return keyword_do_block;
7956 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_ENDARG)))
7957 return keyword_do_block;
7960 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
7963 if (kw->id[0] != kw->id[1])
7964 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
7970 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
7972 SET_LEX_STATE(EXPR_CMDARG);
7975 SET_LEX_STATE(EXPR_ARG);
7978 else if (lex_state == EXPR_FNAME) {
7979 SET_LEX_STATE(EXPR_ENDFN);
7982 SET_LEX_STATE(EXPR_END);
7985 ident = tokenize_ident(parser, last_state);
7986 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
7987 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
7988 lvar_defined(ident)) {
7989 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
7995 parser_yylex(struct parser_params *parser)
8001 enum lex_state_e last_state;
8002 int fallthru = FALSE;
8003 int token_seen = parser->token_seen;
8007 if (nd_type(lex_strterm) == NODE_HEREDOC) {
8008 token = here_document(lex_strterm);
8009 if (token == tSTRING_END) {
8011 SET_LEX_STATE(EXPR_END);
8015 token = parse_string(lex_strterm);
8016 if ((token == tSTRING_END) && (lex_strterm->nd_func & STR_FUNC_LABEL)) {
8017 if (((IS_lex_state(EXPR_BEG | EXPR_ENDFN) && !COND_P()) || IS_ARG()) &&
8018 IS_LABEL_SUFFIX(0)) {
8023 if (token == tSTRING_END || token == tREGEXP_END || token == tLABEL_END) {
8024 const enum lex_state_e next_state =
8025 token == tLABEL_END ? EXPR_BEG|EXPR_LABEL : EXPR_END|EXPR_ENDARG;
8026 rb_gc_force_recycle((VALUE)lex_strterm);
8028 SET_LEX_STATE(next_state);
8033 cmd_state = command_start;
8034 command_start = FALSE;
8035 parser->token_seen = TRUE;
8037 last_state = lex_state;
8038 switch (c = nextc()) {
8039 case '\0': /* NUL */
8040 case '\004': /* ^D */
8041 case '\032': /* ^Z */
8042 case -1: /* end of script. */
8046 case ' ': case '\t': case '\f': case '\r':
8047 case '\13': /* '\v' */
8050 while ((c = nextc())) {
8052 case ' ': case '\t': case '\f': case '\r':
8053 case '\13': /* '\v' */
8061 dispatch_scan_event(tSP);
8065 case '#': /* it's a comment */
8066 parser->token_seen = token_seen;
8067 /* no magic_comment in shebang line */
8068 if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
8069 if (comment_at_top(parser)) {
8070 set_file_encoding(parser, lex_p, lex_pend);
8074 dispatch_scan_event(tCOMMENT);
8078 parser->token_seen = token_seen;
8079 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8080 !IS_lex_state(EXPR_LABELED));
8081 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8083 dispatch_scan_event(tIGNORED_NL);
8086 if (!c && parser->in_kwarg) {
8087 goto normal_newline;
8091 while ((c = nextc())) {
8093 case ' ': case '\t': case '\f': case '\r':
8094 case '\13': /* '\v' */
8099 dispatch_delayed_token(tIGNORED_NL);
8100 if (peek('.') == (c == '&')) {
8102 dispatch_scan_event(tSP);
8108 lex_nextline = lex_lastline;
8109 case -1: /* EOF no decrement*/
8110 lex_goto_eol(parser);
8113 parser->tokp = lex_p;
8116 goto normal_newline;
8120 command_start = TRUE;
8121 SET_LEX_STATE(EXPR_BEG);
8125 if ((c = nextc()) == '*') {
8126 if ((c = nextc()) == '=') {
8127 set_yylval_id(tPOW);
8128 SET_LEX_STATE(EXPR_BEG);
8133 rb_warning0("`**' interpreted as argument prefix");
8136 else if (IS_BEG()) {
8140 warn_balanced("**", "argument prefix");
8147 SET_LEX_STATE(EXPR_BEG);
8152 rb_warning0("`*' interpreted as argument prefix");
8155 else if (IS_BEG()) {
8159 warn_balanced("*", "argument prefix");
8163 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8168 if (IS_AFTER_OPERATOR()) {
8169 SET_LEX_STATE(EXPR_ARG);
8175 SET_LEX_STATE(EXPR_BEG);
8188 /* skip embedded rd document */
8189 if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) {
8192 lex_goto_eol(parser);
8193 dispatch_scan_event(tEMBDOC_BEG);
8195 lex_goto_eol(parser);
8197 dispatch_scan_event(tEMBDOC);
8202 compile_error(PARSER_ARG "embedded document meets end of file");
8205 if (c != '=') continue;
8206 if (c == '=' && strncmp(lex_p, "end", 3) == 0 &&
8207 (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
8211 lex_goto_eol(parser);
8212 dispatch_scan_event(tEMBDOC_END);
8217 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8218 if ((c = nextc()) == '=') {
8219 if ((c = nextc()) == '=') {
8228 else if (c == '>') {
8235 last_state = lex_state;
8238 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
8240 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
8241 int token = heredoc_identifier();
8242 if (token) return token;
8244 if (IS_AFTER_OPERATOR()) {
8245 SET_LEX_STATE(EXPR_ARG);
8248 if (IS_lex_state(EXPR_CLASS))
8249 command_start = TRUE;
8250 SET_LEX_STATE(EXPR_BEG);
8253 if ((c = nextc()) == '>') {
8260 if ((c = nextc()) == '=') {
8261 set_yylval_id(tLSHFT);
8262 SET_LEX_STATE(EXPR_BEG);
8266 warn_balanced("<<", "here document");
8273 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8274 if ((c = nextc()) == '=') {
8278 if ((c = nextc()) == '=') {
8279 set_yylval_id(tRSHFT);
8280 SET_LEX_STATE(EXPR_BEG);
8290 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
8291 lex_strterm = NEW_STRTERM(str_dquote | label, '"', 0);
8295 if (IS_lex_state(EXPR_FNAME)) {
8296 SET_LEX_STATE(EXPR_ENDFN);
8299 if (IS_lex_state(EXPR_DOT)) {
8301 SET_LEX_STATE(EXPR_CMDARG);
8303 SET_LEX_STATE(EXPR_ARG);
8306 lex_strterm = NEW_STRTERM(str_xquote, '`', 0);
8307 return tXSTRING_BEG;
8310 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
8311 lex_strterm = NEW_STRTERM(str_squote | label, '\'', 0);
8315 return parse_qmark(parser, space_seen);
8318 if ((c = nextc()) == '&') {
8319 SET_LEX_STATE(EXPR_BEG);
8320 if ((c = nextc()) == '=') {
8321 set_yylval_id(tANDOP);
8322 SET_LEX_STATE(EXPR_BEG);
8328 else if (c == '=') {
8330 SET_LEX_STATE(EXPR_BEG);
8333 else if (c == '.') {
8334 SET_LEX_STATE(EXPR_DOT);
8339 rb_warning0("`&' interpreted as argument prefix");
8342 else if (IS_BEG()) {
8346 warn_balanced("&", "argument prefix");
8349 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8353 if ((c = nextc()) == '|') {
8354 SET_LEX_STATE(EXPR_BEG);
8355 if ((c = nextc()) == '=') {
8356 set_yylval_id(tOROP);
8357 SET_LEX_STATE(EXPR_BEG);
8365 SET_LEX_STATE(EXPR_BEG);
8368 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
8374 if (IS_AFTER_OPERATOR()) {
8375 SET_LEX_STATE(EXPR_ARG);
8384 SET_LEX_STATE(EXPR_BEG);
8387 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('+'))) {
8388 SET_LEX_STATE(EXPR_BEG);
8390 if (c != -1 && ISDIGIT(c)) {
8391 return parse_numeric(parser, '+');
8395 SET_LEX_STATE(EXPR_BEG);
8397 warn_balanced("+", "unary operator");
8402 if (IS_AFTER_OPERATOR()) {
8403 SET_LEX_STATE(EXPR_ARG);
8412 SET_LEX_STATE(EXPR_BEG);
8416 SET_LEX_STATE(EXPR_ENDFN);
8417 token_info_push("->");
8420 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('-'))) {
8421 SET_LEX_STATE(EXPR_BEG);
8423 if (c != -1 && ISDIGIT(c)) {
8428 SET_LEX_STATE(EXPR_BEG);
8430 warn_balanced("-", "unary operator");
8434 SET_LEX_STATE(EXPR_BEG);
8435 if ((c = nextc()) == '.') {
8436 if ((c = nextc()) == '.') {
8443 if (c != -1 && ISDIGIT(c)) {
8444 yyerror("no .<digit> floating literal anymore; put 0 before dot");
8446 SET_LEX_STATE(EXPR_DOT);
8449 case '0': case '1': case '2': case '3': case '4':
8450 case '5': case '6': case '7': case '8': case '9':
8451 return parse_numeric(parser, c);
8460 SET_LEX_STATE(EXPR_ENDFN);
8462 SET_LEX_STATE(EXPR_ENDARG);
8464 if (!brace_nest--) c = tSTRING_DEND;
8471 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
8472 SET_LEX_STATE(EXPR_BEG);
8475 SET_LEX_STATE(EXPR_DOT);
8478 if (IS_END() || ISSPACE(c) || c == '#') {
8480 warn_balanced(":", "symbol literal");
8481 SET_LEX_STATE(EXPR_BEG);
8486 lex_strterm = NEW_STRTERM(str_ssym, c, 0);
8489 lex_strterm = NEW_STRTERM(str_dsym, c, 0);
8495 SET_LEX_STATE(EXPR_FNAME);
8500 lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
8503 if ((c = nextc()) == '=') {
8505 SET_LEX_STATE(EXPR_BEG);
8510 (void)arg_ambiguous('/');
8511 lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
8514 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8515 warn_balanced("/", "regexp literal");
8519 if ((c = nextc()) == '=') {
8521 SET_LEX_STATE(EXPR_BEG);
8524 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8529 SET_LEX_STATE(EXPR_BEG);
8530 command_start = TRUE;
8534 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8538 if (IS_AFTER_OPERATOR()) {
8539 if ((c = nextc()) != '@') {
8542 SET_LEX_STATE(EXPR_ARG);
8545 SET_LEX_STATE(EXPR_BEG);
8553 else if (IS_SPCARG(-1)) {
8556 else if (IS_lex_state(EXPR_ENDFN) && space_seen && !lambda_beginning_p()) {
8557 rb_warning0("parentheses after method name is interpreted as "
8558 "an argument list, not a decomposed argument");
8563 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8568 if (IS_AFTER_OPERATOR()) {
8569 SET_LEX_STATE(EXPR_ARG);
8570 if ((c = nextc()) == ']') {
8571 if ((c = nextc()) == '=') {
8578 lex_state |= EXPR_LABEL;
8581 else if (IS_BEG()) {
8584 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
8587 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8594 if (lambda_beginning_p()) {
8595 SET_LEX_STATE(EXPR_BEG);
8602 if (IS_lex_state(EXPR_LABELED))
8603 c = tLBRACE; /* hash */
8604 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
8605 c = '{'; /* block (primary) */
8606 else if (IS_lex_state(EXPR_ENDARG))
8607 c = tLBRACE_ARG; /* block (expr) */
8609 c = tLBRACE; /* hash */
8612 SET_LEX_STATE(EXPR_BEG);
8613 if (c != tLBRACE_ARG) lex_state |= EXPR_LABEL;
8614 if (c != tLBRACE) command_start = TRUE;
8621 dispatch_scan_event(tSP);
8622 goto retry; /* skip \\n */
8628 return parse_percent(parser, space_seen, last_state);
8631 return parse_gvar(parser, last_state);
8634 return parse_atmark(parser, last_state);
8637 if (was_bol() && whole_match_p("__END__", 7, 0)) {
8638 ruby__end__seen = 1;
8643 lex_goto_eol(parser);
8644 dispatch_scan_event(k__END__);
8652 if (!parser_is_identchar()) {
8653 compile_error(PARSER_ARG "Invalid char `\\x%02X' in expression", c);
8661 return parse_ident(parser, c, cmd_state);
8665 yylex(YYSTYPE *lval, struct parser_params *parser)
8669 parser->lval = lval;
8671 t = parser_yylex(parser);
8672 if (has_delayed_token())
8673 dispatch_delayed_token(t);
8675 dispatch_scan_event(t);
8682 node_newnode(struct parser_params *parser, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
8684 NODE *n = (rb_node_newnode)(type, a0, a1, a2);
8685 nd_set_line(n, ruby_sourceline);
8689 static enum node_type
8690 nodetype(NODE *node) /* for debug */
8692 return (enum node_type)nd_type(node);
8696 nodeline(NODE *node)
8698 return nd_line(node);
8702 newline_node(NODE *node)
8705 node = remove_begin(node);
8706 node->flags |= NODE_FL_NEWLINE;
8712 fixpos(NODE *node, NODE *orig)
8716 if (orig == (NODE*)1) return;
8717 nd_set_line(node, nd_line(orig));
8721 parser_warning(struct parser_params *parser, NODE *node, const char *mesg)
8723 rb_compile_warning(ruby_sourcefile, nd_line(node), "%s", mesg);
8725 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg))
8728 parser_warn(struct parser_params *parser, NODE *node, const char *mesg)
8730 rb_compile_warn(ruby_sourcefile, nd_line(node), "%s", mesg);
8732 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg))
8735 block_append_gen(struct parser_params *parser, NODE *head, NODE *tail)
8737 NODE *end, *h = head, *nd;
8739 if (tail == 0) return head;
8741 if (h == 0) return tail;
8742 switch (nd_type(h)) {
8749 parser_warning(h, "unused literal ignored");
8752 h = end = NEW_BLOCK(head);
8763 switch (nd_type(nd)) {
8769 if (RTEST(ruby_verbose)) {
8770 parser_warning(tail, "statement not reached");
8778 if (nd_type(tail) != NODE_BLOCK) {
8779 tail = NEW_BLOCK(tail);
8780 tail->nd_end = tail;
8782 end->nd_next = tail;
8783 h->nd_end = tail->nd_end;
8787 /* append item to the list */
8789 list_append_gen(struct parser_params *parser, NODE *list, NODE *item)
8793 if (list == 0) return NEW_LIST(item);
8794 if (list->nd_next) {
8795 last = list->nd_next->nd_end;
8802 last->nd_next = NEW_LIST(item);
8803 list->nd_next->nd_end = last->nd_next;
8807 /* concat two lists */
8809 list_concat(NODE *head, NODE *tail)
8813 if (head->nd_next) {
8814 last = head->nd_next->nd_end;
8820 head->nd_alen += tail->nd_alen;
8821 last->nd_next = tail;
8822 if (tail->nd_next) {
8823 head->nd_next->nd_end = tail->nd_next->nd_end;
8826 head->nd_next->nd_end = tail;
8833 literal_concat0(struct parser_params *parser, VALUE head, VALUE tail)
8835 if (NIL_P(tail)) return 1;
8836 if (!rb_enc_compatible(head, tail)) {
8837 compile_error(PARSER_ARG "string literal encodings differ (%s / %s)",
8838 rb_enc_name(rb_enc_get(head)),
8839 rb_enc_name(rb_enc_get(tail)));
8840 rb_str_resize(head, 0);
8841 rb_str_resize(tail, 0);
8844 rb_str_buf_append(head, tail);
8848 /* concat two string literals */
8850 literal_concat_gen(struct parser_params *parser, NODE *head, NODE *tail)
8852 enum node_type htype;
8856 if (!head) return tail;
8857 if (!tail) return head;
8859 htype = nd_type(head);
8860 if (htype == NODE_EVSTR) {
8861 NODE *node = NEW_DSTR(STR_NEW0());
8862 head = list_append(node, head);
8865 if (heredoc_indent > 0) {
8868 nd_set_type(head, NODE_DSTR);
8870 return list_append(head, tail);
8875 switch (nd_type(tail)) {
8877 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8878 nd_type(headlast) == NODE_STR) {
8880 lit = headlast->nd_lit;
8885 if (htype == NODE_STR) {
8886 if (!literal_concat0(parser, lit, tail->nd_lit)) {
8888 rb_gc_force_recycle((VALUE)head);
8889 rb_gc_force_recycle((VALUE)tail);
8892 rb_gc_force_recycle((VALUE)tail);
8895 list_append(head, tail);
8900 if (htype == NODE_STR) {
8901 if (!literal_concat0(parser, head->nd_lit, tail->nd_lit))
8903 tail->nd_lit = head->nd_lit;
8904 rb_gc_force_recycle((VALUE)head);
8907 else if (NIL_P(tail->nd_lit)) {
8909 head->nd_alen += tail->nd_alen - 1;
8910 head->nd_next->nd_end->nd_next = tail->nd_next;
8911 head->nd_next->nd_end = tail->nd_next->nd_end;
8912 rb_gc_force_recycle((VALUE)tail);
8914 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8915 nd_type(headlast) == NODE_STR) {
8916 lit = headlast->nd_lit;
8917 if (!literal_concat0(parser, lit, tail->nd_lit))
8919 tail->nd_lit = Qnil;
8923 nd_set_type(tail, NODE_ARRAY);
8924 tail->nd_head = NEW_STR(tail->nd_lit);
8925 list_concat(head, tail);
8930 if (htype == NODE_STR) {
8931 nd_set_type(head, NODE_DSTR);
8934 list_append(head, tail);
8941 evstr2dstr_gen(struct parser_params *parser, NODE *node)
8943 if (nd_type(node) == NODE_EVSTR) {
8944 node = list_append(NEW_DSTR(STR_NEW0()), node);
8950 new_evstr_gen(struct parser_params *parser, NODE *node)
8955 switch (nd_type(node)) {
8956 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
8960 return NEW_EVSTR(head);
8964 call_bin_op_gen(struct parser_params *parser, NODE *recv, ID id, NODE *arg1)
8968 return NEW_CALL(recv, id, NEW_LIST(arg1));
8972 call_uni_op_gen(struct parser_params *parser, NODE *recv, ID id)
8975 return NEW_CALL(recv, id, 0);
8979 match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2)
8984 switch (nd_type(node1)) {
8986 case NODE_DREGX_ONCE:
8987 return NEW_MATCH2(node1, node2);
8990 if (RB_TYPE_P(node1->nd_lit, T_REGEXP)) {
8991 return NEW_MATCH2(node1, node2);
8997 switch (nd_type(node2)) {
8999 case NODE_DREGX_ONCE:
9000 return NEW_MATCH3(node2, node1);
9003 if (RB_TYPE_P(node2->nd_lit, T_REGEXP)) {
9004 return NEW_MATCH3(node2, node1);
9009 return NEW_CALL(node1, tMATCH, NEW_LIST(node2));
9012 # if WARN_PAST_SCOPE
9014 past_dvar_p(struct parser_params *parser, ID id)
9016 struct vtable *past = lvtbl->past;
9018 if (vtable_included(past, id)) return 1;
9026 gettable_gen(struct parser_params *parser, ID id)
9037 case keyword__FILE__:
9038 return NEW_STR(rb_str_dup(ruby_sourcefile_string));
9039 case keyword__LINE__:
9040 return NEW_LIT(INT2FIX(tokline));
9041 case keyword__ENCODING__:
9042 return NEW_LIT(rb_enc_from_encoding(current_enc));
9044 switch (id_type(id)) {
9046 if (dyna_in_block() && dvar_defined(id)) {
9047 if (id == current_arg) {
9048 rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id));
9050 return NEW_DVAR(id);
9053 if (id == current_arg) {
9054 rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id));
9056 return NEW_LVAR(id);
9058 # if WARN_PAST_SCOPE
9059 if (!in_defined && RTEST(ruby_verbose) && past_dvar_p(parser, id)) {
9060 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
9063 /* method call without arguments */
9064 return NEW_VCALL(id);
9066 return NEW_GVAR(id);
9068 return NEW_IVAR(id);
9070 return NEW_CONST(id);
9072 return NEW_CVAR(id);
9074 compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
9079 kwd_append(NODE *kwlist, NODE *kw)
9083 while (kws->nd_next) {
9092 new_regexp_gen(struct parser_params *parser, NODE *node, int options)
9097 return NEW_LIT(reg_compile(STR_NEW0(), options));
9099 switch (nd_type(node)) {
9102 VALUE src = node->nd_lit;
9103 nd_set_type(node, NODE_LIT);
9104 node->nd_lit = reg_compile(src, options);
9108 node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node));
9110 if (options & RE_OPTION_ONCE) {
9111 nd_set_type(node, NODE_DREGX_ONCE);
9114 nd_set_type(node, NODE_DREGX);
9116 node->nd_cflag = options & RE_OPTION_MASK;
9117 if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options);
9118 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
9119 if (nd_type(list->nd_head) == NODE_STR) {
9120 VALUE tail = list->nd_head->nd_lit;
9121 if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) {
9122 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
9123 if (!literal_concat0(parser, lit, tail)) {
9127 rb_str_resize(tail, 0);
9128 prev->nd_next = list->nd_next;
9129 rb_gc_force_recycle((VALUE)list->nd_head);
9130 rb_gc_force_recycle((VALUE)list);
9141 if (!node->nd_next) {
9142 VALUE src = node->nd_lit;
9143 nd_set_type(node, NODE_LIT);
9144 node->nd_lit = reg_compile(src, options);
9152 new_xstring_gen(struct parser_params *parser, NODE *node)
9155 return NEW_XSTR(STR_NEW0());
9157 switch (nd_type(node)) {
9159 nd_set_type(node, NODE_XSTR);
9162 nd_set_type(node, NODE_DXSTR);
9165 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node));
9172 id_is_var_gen(struct parser_params *parser, ID id)
9174 if (is_notop_id(id)) {
9175 switch (id & ID_SCOPE_MASK) {
9176 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
9179 if (dyna_in_block() && dvar_defined(id)) return 1;
9180 if (local_id(id)) return 1;
9181 /* method call without arguments */
9185 compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2str(id));
9190 new_regexp_gen(struct parser_params *parser, VALUE re, VALUE opt)
9194 if (ripper_is_node_yylval(re)) {
9195 src = RNODE(re)->nd_cval;
9196 re = RNODE(re)->nd_rval;
9198 if (ripper_is_node_yylval(opt)) {
9199 options = (int)RNODE(opt)->nd_tag;
9200 opt = RNODE(opt)->nd_rval;
9202 if (src && NIL_P(parser_reg_compile(parser, src, options, &err))) {
9203 compile_error(PARSER_ARG "%"PRIsVALUE, err);
9205 return dispatch2(regexp_literal, re, opt);
9209 new_xstring_gen(struct parser_params *parser, VALUE str)
9211 return dispatch1(xstring_literal, str);
9213 #endif /* !RIPPER */
9215 static const char lex_state_names[][13] = {
9216 "EXPR_BEG", "EXPR_END", "EXPR_ENDARG", "EXPR_ENDFN", "EXPR_ARG",
9217 "EXPR_CMDARG", "EXPR_MID", "EXPR_FNAME", "EXPR_DOT", "EXPR_CLASS",
9218 "EXPR_LABEL", "EXPR_LABELED","EXPR_FITEM",
9222 append_lex_state_name(enum lex_state_e state, VALUE buf)
9225 unsigned int mask = 1;
9226 static const char none[] = "EXPR_NONE";
9228 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
9229 if ((unsigned)state & mask) {
9231 rb_str_cat(buf, "|", 1);
9234 rb_str_cat_cstr(buf, lex_state_names[i]);
9238 rb_str_cat(buf, none, sizeof(none)-1);
9243 static enum lex_state_e
9244 trace_lex_state(enum lex_state_e from, enum lex_state_e to, int line)
9247 mesg = rb_str_new_cstr("lex_state: ");
9248 append_lex_state_name(from, mesg);
9249 rb_str_cat_cstr(mesg, " -> ");
9250 append_lex_state_name(to, mesg);
9251 rb_str_catf(mesg, " at line %d\n", line);
9252 rb_io_write(rb_stdout, mesg);
9257 show_bitstack(stack_type stack, const char *name, int line)
9259 VALUE mesg = rb_sprintf("%s: ", name);
9261 rb_str_cat_cstr(mesg, "0");
9264 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
9265 for (; mask && !(stack & mask); mask >>= 1) continue;
9266 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
9268 rb_str_catf(mesg, " at line %d\n", line);
9269 rb_io_write(rb_stdout, mesg);
9274 assignable_gen(struct parser_params *parser, VALUE lhs)
9277 assignable_gen(struct parser_params *parser, ID id, NODE *val)
9281 ID id = get_id(lhs);
9282 # define assignable_result(x) get_value(lhs)
9283 # define parser_yyerror(parser, x) assign_error_gen(parser, lhs)
9285 # define assignable_result(x) (x)
9287 if (!id) return assignable_result(0);
9290 yyerror("Can't change the value of self");
9293 yyerror("Can't assign to nil");
9296 yyerror("Can't assign to true");
9299 yyerror("Can't assign to false");
9301 case keyword__FILE__:
9302 yyerror("Can't assign to __FILE__");
9304 case keyword__LINE__:
9305 yyerror("Can't assign to __LINE__");
9307 case keyword__ENCODING__:
9308 yyerror("Can't assign to __ENCODING__");
9311 switch (id_type(id)) {
9313 if (dyna_in_block()) {
9314 if (dvar_curr(id)) {
9315 return assignable_result(NEW_DASGN_CURR(id, val));
9317 else if (dvar_defined(id)) {
9318 return assignable_result(NEW_DASGN(id, val));
9320 else if (local_id(id)) {
9321 return assignable_result(NEW_LASGN(id, val));
9325 return assignable_result(NEW_DASGN_CURR(id, val));
9329 if (!local_id(id)) {
9332 return assignable_result(NEW_LASGN(id, val));
9336 return assignable_result(NEW_GASGN(id, val));
9338 return assignable_result(NEW_IASGN(id, val));
9340 if (!in_def && !in_single)
9341 return assignable_result(NEW_CDECL(id, val, 0));
9342 yyerror("dynamic constant assignment");
9345 return assignable_result(NEW_CVASGN(id, val));
9347 compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
9350 return assignable_result(0);
9351 #undef assignable_result
9352 #undef parser_yyerror
9356 is_private_local_id(ID name)
9359 if (name == idUScore) return 1;
9360 if (!is_local_id(name)) return 0;
9361 s = rb_id2str(name);
9363 return RSTRING_PTR(s)[0] == '_';
9366 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9369 shadowing_lvar_0(struct parser_params *parser, ID name)
9371 if (is_private_local_id(name)) return 1;
9372 if (dyna_in_block()) {
9373 if (dvar_curr(name)) {
9374 yyerror("duplicated argument name");
9376 else if (dvar_defined_get(name) || local_id(name)) {
9377 rb_warning1("shadowing outer local variable - %"PRIsWARN, rb_id2str(name));
9378 vtable_add(lvtbl->vars, name);
9380 vtable_add(lvtbl->used, (ID)ruby_sourceline | LVAR_USED);
9386 if (local_id(name)) {
9387 yyerror("duplicated argument name");
9394 shadowing_lvar_gen(struct parser_params *parser, ID name)
9396 shadowing_lvar_0(parser, name);
9401 new_bv_gen(struct parser_params *parser, ID name)
9404 if (!is_local_id(name)) {
9405 compile_error(PARSER_ARG "invalid local variable - %"PRIsVALUE,
9409 if (!shadowing_lvar_0(parser, name)) return;
9415 aryset_gen(struct parser_params *parser, NODE *recv, NODE *idx)
9417 return NEW_ATTRASGN(recv, tASET, idx);
9421 block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9423 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
9424 compile_error(PARSER_ARG "both block arg and actual block given");
9429 attrset_gen(struct parser_params *parser, NODE *recv, ID atype, ID id)
9431 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
9432 return NEW_ATTRASGN(recv, id, 0);
9436 rb_backref_error_gen(struct parser_params *parser, NODE *node)
9438 switch (nd_type(node)) {
9440 compile_error(PARSER_ARG "Can't set variable $%ld", node->nd_nth);
9443 compile_error(PARSER_ARG "Can't set variable $%c", (int)node->nd_nth);
9449 arg_concat_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9451 if (!node2) return node1;
9452 switch (nd_type(node1)) {
9453 case NODE_BLOCK_PASS:
9455 node1->nd_head = arg_concat(node1->nd_head, node2);
9457 node1->nd_head = NEW_LIST(node2);
9460 if (nd_type(node2) != NODE_ARRAY) break;
9461 node1->nd_body = list_concat(NEW_LIST(node1->nd_body), node2);
9462 nd_set_type(node1, NODE_ARGSCAT);
9465 if (nd_type(node2) != NODE_ARRAY ||
9466 nd_type(node1->nd_body) != NODE_ARRAY) break;
9467 node1->nd_body = list_concat(node1->nd_body, node2);
9470 return NEW_ARGSCAT(node1, node2);
9474 arg_append_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9476 if (!node1) return NEW_LIST(node2);
9477 switch (nd_type(node1)) {
9479 return list_append(node1, node2);
9480 case NODE_BLOCK_PASS:
9481 node1->nd_head = arg_append(node1->nd_head, node2);
9484 node1->nd_body = list_append(NEW_LIST(node1->nd_body), node2);
9485 nd_set_type(node1, NODE_ARGSCAT);
9488 return NEW_ARGSPUSH(node1, node2);
9492 splat_array(NODE* node)
9494 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
9495 if (nd_type(node) == NODE_ARRAY) return node;
9500 node_assign_gen(struct parser_params *parser, NODE *lhs, NODE *rhs)
9504 switch (nd_type(lhs)) {
9510 case NODE_DASGN_CURR:
9514 lhs->nd_value = rhs;
9519 lhs->nd_args = arg_append(lhs->nd_args, rhs);
9523 /* should not happen */
9531 value_expr_gen(struct parser_params *parser, NODE *node)
9536 rb_warning0("empty expression");
9539 switch (nd_type(node)) {
9545 if (!cond) yyerror("void value expression");
9546 /* or "control never reach"? */
9550 while (node->nd_next) {
9551 node = node->nd_next;
9553 node = node->nd_head;
9557 node = node->nd_body;
9561 if (!node->nd_body) {
9562 node = node->nd_else;
9565 else if (!node->nd_else) {
9566 node = node->nd_body;
9569 if (!value_expr(node->nd_body)) return FALSE;
9570 node = node->nd_else;
9576 node = node->nd_2nd;
9588 void_expr_gen(struct parser_params *parser, NODE *node)
9590 const char *useless = 0;
9592 if (!RTEST(ruby_verbose)) return;
9595 switch (nd_type(node)) {
9597 switch (node->nd_mid) {
9616 useless = rb_id2name(node->nd_mid);
9628 useless = "a variable";
9631 useless = "a constant";
9637 case NODE_DREGX_ONCE:
9638 useless = "a literal";
9663 useless = "defined?";
9668 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
9673 void_stmts_gen(struct parser_params *parser, NODE *node)
9675 if (!RTEST(ruby_verbose)) return;
9677 if (nd_type(node) != NODE_BLOCK) return;
9680 if (!node->nd_next) return;
9681 void_expr0(node->nd_head);
9682 node = node->nd_next;
9687 remove_begin(NODE *node)
9689 NODE **n = &node, *n1 = node;
9690 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
9691 *n = n1 = n1->nd_body;
9697 remove_begin_all(NODE *node)
9699 NODE **n = &node, *n1 = node;
9700 while (n1 && nd_type(n1) == NODE_BEGIN) {
9701 *n = n1 = n1->nd_body;
9707 reduce_nodes_gen(struct parser_params *parser, NODE **body)
9715 #define subnodes(n1, n2) \
9716 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
9717 (!node->n2) ? (body = &node->n1, 1) : \
9718 (reduce_nodes(&node->n1), body = &node->n2, 1))
9721 int newline = (int)(node->flags & NODE_FL_NEWLINE);
9722 switch (nd_type(node)) {
9728 *body = node = node->nd_stts;
9729 if (newline && node) node->flags |= NODE_FL_NEWLINE;
9732 *body = node = node->nd_body;
9733 if (newline && node) node->flags |= NODE_FL_NEWLINE;
9736 body = &node->nd_end->nd_head;
9739 if (subnodes(nd_body, nd_else)) break;
9742 body = &node->nd_body;
9745 if (!subnodes(nd_body, nd_next)) goto end;
9748 if (!subnodes(nd_head, nd_resq)) goto end;
9751 if (node->nd_else) {
9752 body = &node->nd_resq;
9755 if (!subnodes(nd_head, nd_resq)) goto end;
9761 if (newline && node) node->flags |= NODE_FL_NEWLINE;
9768 is_static_content(NODE *node)
9770 if (!node) return 1;
9771 switch (nd_type(node)) {
9773 if (!(node = node->nd_head)) break;
9776 if (!is_static_content(node->nd_head)) return 0;
9777 } while ((node = node->nd_next) != 0);
9792 assign_in_cond(struct parser_params *parser, NODE *node)
9794 switch (nd_type(node)) {
9798 case NODE_DASGN_CURR:
9807 if (!node->nd_value) return 1;
9808 if (is_static_content(node->nd_value)) {
9809 /* reports always */
9810 parser_warn(node->nd_value, "found = in conditional, should be ==");
9816 warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
9818 if (!e_option_supplied(parser)) parser_warn(node, str);
9822 warning_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
9824 if (!e_option_supplied(parser)) parser_warning(node, str);
9827 static NODE *cond0(struct parser_params*,NODE*,int);
9830 range_op(struct parser_params *parser, NODE *node)
9832 enum node_type type;
9834 if (node == 0) return 0;
9836 type = nd_type(node);
9838 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
9839 warn_unless_e_option(parser, node, "integer literal in conditional range");
9840 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."))));
9842 return cond0(parser, node, FALSE);
9846 literal_node(NODE *node)
9848 if (!node) return 1; /* same as NODE_NIL */
9849 switch (nd_type(node)) {
9855 case NODE_DREGX_ONCE:
9867 cond0(struct parser_params *parser, NODE *node, int method_op)
9869 if (node == 0) return 0;
9870 assign_in_cond(parser, node);
9872 switch (nd_type(node)) {
9876 if (!method_op) rb_warn0("string literal in condition");
9880 case NODE_DREGX_ONCE:
9882 warning_unless_e_option(parser, node, "regex literal in condition");
9883 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE));
9887 node->nd_1st = cond0(parser, node->nd_1st, FALSE);
9888 node->nd_2nd = cond0(parser, node->nd_2nd, FALSE);
9893 node->nd_beg = range_op(parser, node->nd_beg);
9894 node->nd_end = range_op(parser, node->nd_end);
9895 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
9896 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
9897 if (!method_op && !e_option_supplied(parser)) {
9898 int b = literal_node(node->nd_beg);
9899 int e = literal_node(node->nd_end);
9900 if ((b == 1 && e == 1) || (b + e >= 2 && RTEST(ruby_verbose))) {
9901 parser_warn(node, "range literal in condition");
9907 if (!method_op) parser_warning(node, "literal in condition");
9911 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
9913 warn_unless_e_option(parser, node, "regex literal in condition");
9914 nd_set_type(node, NODE_MATCH);
9918 parser_warning(node, "literal in condition");
9927 cond_gen(struct parser_params *parser, NODE *node, int method_op)
9929 if (node == 0) return 0;
9930 return cond0(parser, node, method_op);
9934 new_if_gen(struct parser_params *parser, NODE *cc, NODE *left, NODE *right)
9936 if (!cc) return right;
9937 cc = cond0(parser, cc, FALSE);
9938 return newline_node(NEW_IF(cc, left, right));
9942 logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right)
9945 if (left && (enum node_type)nd_type(left) == type) {
9946 NODE *node = left, *second;
9947 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
9950 node->nd_2nd = NEW_NODE(type, second, right, 0);
9953 return NEW_NODE(type, left, right, 0);
9957 no_blockarg(struct parser_params *parser, NODE *node)
9959 if (node && nd_type(node) == NODE_BLOCK_PASS) {
9960 compile_error(PARSER_ARG "block argument should not be given");
9965 ret_args_gen(struct parser_params *parser, NODE *node)
9968 no_blockarg(parser, node);
9969 if (nd_type(node) == NODE_ARRAY) {
9970 if (node->nd_next == 0) {
9971 node = node->nd_head;
9974 nd_set_type(node, NODE_VALUES);
9982 new_yield_gen(struct parser_params *parser, NODE *node)
9984 if (node) no_blockarg(parser, node);
9986 return NEW_YIELD(node);
9990 negate_lit(VALUE lit)
9992 int type = TYPE(lit);
9995 lit = LONG2FIX(-FIX2LONG(lit));
9999 lit = rb_big_norm(lit);
10002 RRATIONAL_SET_NUM(lit, negate_lit(RRATIONAL(lit)->num));
10005 RCOMPLEX_SET_REAL(lit, negate_lit(RCOMPLEX(lit)->real));
10006 RCOMPLEX_SET_IMAG(lit, negate_lit(RCOMPLEX(lit)->imag));
10010 if (FLONUM_P(lit)) {
10011 lit = DBL2NUM(-RFLOAT_VALUE(lit));
10015 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
10018 rb_bug("unknown literal type (%d) passed to negate_lit", type);
10025 arg_blk_pass(NODE *node1, NODE *node2)
10028 node2->nd_head = node1;
10036 new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, NODE *tail)
10038 int saved_line = ruby_sourceline;
10039 struct rb_args_info *args = tail->nd_ainfo;
10041 args->pre_args_num = m ? rb_long2int(m->nd_plen) : 0;
10042 args->pre_init = m ? m->nd_next : 0;
10044 args->post_args_num = p ? rb_long2int(p->nd_plen) : 0;
10045 args->post_init = p ? p->nd_next : 0;
10046 args->first_post_arg = p ? p->nd_pid : 0;
10048 args->rest_arg = r;
10050 args->opt_args = o;
10052 ruby_sourceline = saved_line;
10058 new_args_tail_gen(struct parser_params *parser, NODE *k, ID kr, ID b)
10060 int saved_line = ruby_sourceline;
10061 struct rb_args_info *args;
10064 args = ZALLOC(struct rb_args_info);
10065 node = NEW_NODE(NODE_ARGS, 0, 0, args);
10067 args->block_arg = b;
10072 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
10073 * variable order: k1, kr1, k2, &b, internal_id, krest
10075 * variable order: kr1, k1, k2, internal_id, krest, &b
10079 struct vtable *required_kw_vars = vtable_alloc(NULL);
10080 struct vtable *kw_vars = vtable_alloc(NULL);
10084 NODE *val_node = kwn->nd_body->nd_value;
10085 ID vid = kwn->nd_body->nd_vid;
10087 if (val_node == (NODE *)-1) {
10088 vtable_add(required_kw_vars, vid);
10091 vtable_add(kw_vars, vid);
10094 kwn = kwn->nd_next;
10097 kw_bits = internal_id();
10098 if (kr && is_junk_id(kr)) vtable_pop(lvtbl->args, 1);
10099 vtable_pop(lvtbl->args, vtable_size(required_kw_vars) + vtable_size(kw_vars) + (b != 0));
10101 for (i=0; i<vtable_size(required_kw_vars); i++) arg_var(required_kw_vars->tbl[i]);
10102 for (i=0; i<vtable_size(kw_vars); i++) arg_var(kw_vars->tbl[i]);
10103 vtable_free(required_kw_vars);
10104 vtable_free(kw_vars);
10107 if (kr) arg_var(kr);
10110 args->kw_rest_arg = NEW_DVAR(kw_bits);
10111 args->kw_rest_arg->nd_cflag = kr;
10114 if (b) vtable_pop(lvtbl->args, 1); /* reorder */
10117 args->kw_rest_arg = NEW_DVAR(kr);
10120 ruby_sourceline = saved_line;
10125 dsym_node_gen(struct parser_params *parser, NODE *node)
10130 return NEW_LIT(ID2SYM(idNULL));
10133 switch (nd_type(node)) {
10135 nd_set_type(node, NODE_DSYM);
10138 lit = node->nd_lit;
10139 node->nd_lit = ID2SYM(rb_intern_str(lit));
10140 nd_set_type(node, NODE_LIT);
10143 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node));
10150 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
10152 NODE *node = (NODE *)v;
10153 NODE **result = (NODE **)h;
10155 node->nd_next->nd_end = node->nd_next;
10156 node->nd_next->nd_next = 0;
10158 list_concat(*result, node);
10161 return ST_CONTINUE;
10165 remove_duplicate_keys(struct parser_params *parser, NODE *hash)
10167 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
10169 while (hash && hash->nd_head && hash->nd_next) {
10170 NODE *head = hash->nd_head;
10171 NODE *value = hash->nd_next;
10172 NODE *next = value->nd_next;
10173 VALUE key = (VALUE)head;
10175 if (nd_type(head) == NODE_LIT &&
10176 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
10177 rb_compile_warn(ruby_sourcefile, nd_line((NODE *)data),
10178 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
10179 head->nd_lit, nd_line(head));
10180 head = ((NODE *)data)->nd_next;
10181 head->nd_head = block_append(head->nd_head, value->nd_head);
10184 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
10188 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
10189 st_free_table(literal_keys);
10191 if (!result) result = hash;
10192 else list_concat(result, hash);
10198 new_hash_gen(struct parser_params *parser, NODE *hash)
10200 if (hash) hash = remove_duplicate_keys(parser, hash);
10201 return NEW_HASH(hash);
10203 #endif /* !RIPPER */
10207 new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs)
10212 ID vid = lhs->nd_vid;
10214 lhs->nd_value = rhs;
10215 asgn = NEW_OP_ASGN_OR(gettable(vid), lhs);
10216 if (is_notop_id(vid)) {
10217 switch (id_type(vid)) {
10221 asgn->nd_aid = vid;
10225 else if (op == tANDOP) {
10226 lhs->nd_value = rhs;
10227 asgn = NEW_OP_ASGN_AND(gettable(vid), lhs);
10231 asgn->nd_value = NEW_CALL(gettable(vid), op, NEW_LIST(rhs));
10235 asgn = NEW_BEGIN(0);
10241 new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs,
10242 ID atype, ID attr, ID op, NODE *rhs)
10249 else if (op == tANDOP) {
10252 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs);
10258 new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs)
10265 else if (op == tANDOP) {
10269 asgn = NEW_OP_CDECL(lhs, op, rhs);
10272 asgn = NEW_BEGIN(0);
10279 const_decl_gen(struct parser_params *parser, NODE *path)
10281 if (in_def || in_single) {
10282 yyerror("dynamic constant assignment");
10284 return NEW_CDECL(0, 0, (path));
10288 new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs)
10290 return dispatch3(opassign, lhs, op, rhs);
10294 new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs)
10296 VALUE recv = dispatch3(field, lhs, type, attr);
10297 return dispatch3(opassign, recv, op, rhs);
10301 const_decl_gen(struct parser_params *parser, VALUE path)
10303 if (in_def || in_single) {
10304 assign_error(path);
10310 assign_error_gen(struct parser_params *parser, VALUE a)
10312 a = dispatch1(assign_error, a);
10319 warn_unused_var(struct parser_params *parser, struct local_vars *local)
10324 if (!local->used) return;
10325 v = local->vars->tbl;
10326 u = local->used->tbl;
10327 cnt = local->used->pos;
10328 if (cnt != local->vars->pos) {
10329 rb_bug("local->used->pos != local->vars->pos");
10331 for (i = 0; i < cnt; ++i) {
10332 if (!v[i] || (u[i] & LVAR_USED)) continue;
10333 if (is_private_local_id(v[i])) continue;
10334 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
10339 local_push_gen(struct parser_params *parser, int inherit_dvars)
10341 struct local_vars *local;
10343 local = ALLOC(struct local_vars);
10344 local->prev = lvtbl;
10345 local->args = vtable_alloc(0);
10346 local->vars = vtable_alloc(inherit_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
10347 local->used = !(inherit_dvars &&
10348 (ifndef_ripper(compile_for_eval || e_option_supplied(parser))+0)) &&
10349 RTEST(ruby_verbose) ? vtable_alloc(0) : 0;
10350 # if WARN_PAST_SCOPE
10353 local->cmdargs = cmdarg_stack;
10359 local_pop_gen(struct parser_params *parser)
10361 struct local_vars *local = lvtbl->prev;
10363 warn_unused_var(parser, lvtbl);
10364 vtable_free(lvtbl->used);
10366 # if WARN_PAST_SCOPE
10367 while (lvtbl->past) {
10368 struct vtable *past = lvtbl->past;
10369 lvtbl->past = past->prev;
10373 vtable_free(lvtbl->args);
10374 vtable_free(lvtbl->vars);
10375 CMDARG_SET(lvtbl->cmdargs);
10382 local_tbl_gen(struct parser_params *parser)
10384 int cnt_args = vtable_size(lvtbl->args);
10385 int cnt_vars = vtable_size(lvtbl->vars);
10386 int cnt = cnt_args + cnt_vars;
10390 if (cnt <= 0) return 0;
10391 buf = ALLOC_N(ID, cnt + 1);
10392 MEMCPY(buf+1, lvtbl->args->tbl, ID, cnt_args);
10393 /* remove IDs duplicated to warn shadowing */
10394 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
10395 ID id = lvtbl->vars->tbl[i];
10396 if (!vtable_included(lvtbl->args, id)) {
10400 if (--j < cnt) REALLOC_N(buf, ID, (cnt = j) + 1);
10407 arg_var_gen(struct parser_params *parser, ID id)
10409 vtable_add(lvtbl->args, id);
10413 local_var_gen(struct parser_params *parser, ID id)
10415 vtable_add(lvtbl->vars, id);
10417 vtable_add(lvtbl->used, (ID)ruby_sourceline);
10422 local_id_gen(struct parser_params *parser, ID id)
10424 struct vtable *vars, *args, *used;
10426 vars = lvtbl->vars;
10427 args = lvtbl->args;
10428 used = lvtbl->used;
10430 while (vars && POINTER_P(vars->prev)) {
10433 if (used) used = used->prev;
10436 if (vars && vars->prev == DVARS_INHERIT) {
10437 return rb_local_defined(id, parser->base_block);
10439 else if (vtable_included(args, id)) {
10443 int i = vtable_included(vars, id);
10444 if (i && used) used->tbl[i-1] |= LVAR_USED;
10449 static const struct vtable *
10450 dyna_push_gen(struct parser_params *parser)
10452 lvtbl->args = vtable_alloc(lvtbl->args);
10453 lvtbl->vars = vtable_alloc(lvtbl->vars);
10455 lvtbl->used = vtable_alloc(lvtbl->used);
10457 return lvtbl->args;
10461 dyna_pop_vtable(struct parser_params *parser, struct vtable **vtblp)
10463 struct vtable *tmp = *vtblp;
10464 *vtblp = tmp->prev;
10465 # if WARN_PAST_SCOPE
10466 if (parser->past_scope_enabled) {
10467 tmp->prev = lvtbl->past;
10476 dyna_pop_1(struct parser_params *parser)
10478 struct vtable *tmp;
10480 if ((tmp = lvtbl->used) != 0) {
10481 warn_unused_var(parser, lvtbl);
10482 lvtbl->used = lvtbl->used->prev;
10485 dyna_pop_vtable(parser, &lvtbl->args);
10486 dyna_pop_vtable(parser, &lvtbl->vars);
10490 dyna_pop_gen(struct parser_params *parser, const struct vtable *lvargs)
10492 while (lvtbl->args != lvargs) {
10493 dyna_pop_1(parser);
10494 if (!lvtbl->args) {
10495 struct local_vars *local = lvtbl->prev;
10500 dyna_pop_1(parser);
10504 dyna_in_block_gen(struct parser_params *parser)
10506 return POINTER_P(lvtbl->vars) && lvtbl->vars->prev != DVARS_TOPSCOPE;
10510 dvar_defined_gen(struct parser_params *parser, ID id, int get)
10512 struct vtable *vars, *args, *used;
10515 args = lvtbl->args;
10516 vars = lvtbl->vars;
10517 used = lvtbl->used;
10519 while (POINTER_P(vars)) {
10520 if (vtable_included(args, id)) {
10523 if ((i = vtable_included(vars, id)) != 0) {
10524 if (used) used->tbl[i-1] |= LVAR_USED;
10530 if (used) used = used->prev;
10533 if (vars == DVARS_INHERIT) {
10534 return rb_dvar_defined(id, parser->base_block);
10541 dvar_curr_gen(struct parser_params *parser, ID id)
10543 return (vtable_included(lvtbl->args, id) ||
10544 vtable_included(lvtbl->vars, id));
10548 reg_fragment_enc_error(struct parser_params* parser, VALUE str, int c)
10550 compile_error(PARSER_ARG
10551 "regexp encoding option '%c' differs from source encoding '%s'",
10552 c, rb_enc_name(rb_enc_get(str)));
10557 rb_reg_fragment_setenc(struct parser_params* parser, VALUE str, int options)
10559 int c = RE_OPTION_ENCODING_IDX(options);
10563 rb_char_to_option_kcode(c, &opt, &idx);
10564 if (idx != ENCODING_GET(str) &&
10565 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
10568 ENCODING_SET(str, idx);
10570 else if (RE_OPTION_ENCODING_NONE(options)) {
10571 if (!ENCODING_IS_ASCII8BIT(str) &&
10572 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
10576 rb_enc_associate(str, rb_ascii8bit_encoding());
10578 else if (current_enc == rb_usascii_encoding()) {
10579 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
10580 /* raise in re.c */
10581 rb_enc_associate(str, rb_usascii_encoding());
10584 rb_enc_associate(str, rb_ascii8bit_encoding());
10594 reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options)
10596 int c = rb_reg_fragment_setenc(parser, str, options);
10597 if (c) reg_fragment_enc_error(parser, str, c);
10601 reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options)
10604 reg_fragment_setenc(str, options);
10605 err = rb_reg_check_preprocess(str);
10607 err = rb_obj_as_string(err);
10608 compile_error(PARSER_ARG "%"PRIsVALUE, err);
10615 struct parser_params* parser;
10618 } reg_named_capture_assign_t;
10621 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
10622 int back_num, int *back_refs, OnigRegex regex, void *arg0)
10624 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
10625 struct parser_params* parser = arg->parser;
10626 rb_encoding *enc = arg->enc;
10627 long len = name_end - name;
10628 const char *s = (const char *)name;
10632 if (!len || (*name != '_' && ISASCII(*name) && !rb_enc_islower(*name, enc)) ||
10633 (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) ||
10634 !rb_enc_symname2_p(s, len, enc)) {
10635 return ST_CONTINUE;
10637 var = intern_cstr(s, len, enc);
10638 node = node_assign(assignable(var, 0), NEW_LIT(ID2SYM(var)));
10639 succ = arg->succ_block;
10640 if (!succ) succ = NEW_BEGIN(0);
10641 succ = block_append(succ, node);
10642 arg->succ_block = succ;
10643 return ST_CONTINUE;
10647 reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp)
10649 reg_named_capture_assign_t arg;
10651 arg.parser = parser;
10652 arg.enc = rb_enc_get(regexp);
10653 arg.succ_block = 0;
10654 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
10656 if (!arg.succ_block) return 0;
10657 return arg.succ_block->nd_next;
10661 parser_reg_compile(struct parser_params* parser, VALUE str, int options)
10663 reg_fragment_setenc(str, options);
10664 return rb_parser_reg_compile(parser, str, options);
10668 rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options)
10670 return rb_reg_compile(str, options & RE_OPTION_MASK, ruby_sourcefile, ruby_sourceline);
10674 reg_compile_gen(struct parser_params* parser, VALUE str, int options)
10679 err = rb_errinfo();
10680 re = parser_reg_compile(parser, str, options);
10682 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
10683 rb_set_errinfo(err);
10684 compile_error(PARSER_ARG "%"PRIsVALUE, m);
10691 parser_reg_compile(struct parser_params* parser, VALUE str, int options, VALUE *errmsg)
10693 VALUE err = rb_errinfo();
10695 int c = rb_reg_fragment_setenc(parser, str, options);
10696 if (c) reg_fragment_enc_error(parser, str, c);
10697 re = rb_parser_reg_compile(parser, str, options);
10699 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
10700 rb_set_errinfo(err);
10708 rb_parser_append_print(VALUE vparser, NODE *node)
10711 NODE *scope = node;
10712 struct parser_params *parser;
10714 if (!node) return node;
10716 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10718 node = node->nd_body;
10720 if (nd_type(node) == NODE_PRELUDE) {
10722 node = node->nd_body;
10725 node = block_append(node,
10726 NEW_FCALL(rb_intern("print"),
10727 NEW_ARRAY(NEW_GVAR(idLASTLINE))));
10729 prelude->nd_body = node;
10730 scope->nd_body = prelude;
10733 scope->nd_body = node;
10740 rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split)
10743 NODE *scope = node;
10744 struct parser_params *parser;
10746 if (!node) return node;
10748 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10750 node = node->nd_body;
10752 if (nd_type(node) == NODE_PRELUDE) {
10754 node = node->nd_body;
10757 node = block_append(NEW_GASGN(rb_intern("$F"),
10758 NEW_CALL(NEW_GVAR(idLASTLINE),
10759 rb_intern("split"), 0)),
10763 node = block_append(NEW_CALL(NEW_GVAR(idLASTLINE),
10764 rb_intern("chop!"), 0), node);
10767 node = NEW_OPT_N(node);
10770 prelude->nd_body = node;
10771 scope->nd_body = prelude;
10774 scope->nd_body = node;
10781 rb_init_parse(void)
10783 /* just to suppress unused-function warnings */
10787 #endif /* !RIPPER */
10790 internal_id_gen(struct parser_params *parser)
10792 ID id = (ID)vtable_size(lvtbl->args) + (ID)vtable_size(lvtbl->vars);
10793 id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1;
10794 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
10798 parser_initialize(struct parser_params *parser)
10800 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
10801 command_start = TRUE;
10802 ruby_sourcefile_string = Qnil;
10804 parser->delayed = Qnil;
10805 parser->result = Qnil;
10806 parser->parsing_thread = Qnil;
10808 parser->error_buffer = Qfalse;
10810 parser->debug_buffer = Qnil;
10811 parser->enc = rb_utf8_encoding();
10815 #define parser_mark ripper_parser_mark
10816 #define parser_free ripper_parser_free
10820 parser_mark(void *ptr)
10822 struct parser_params *parser = (struct parser_params*)ptr;
10824 rb_gc_mark((VALUE)lex_strterm);
10825 rb_gc_mark(lex_input);
10826 rb_gc_mark(lex_lastline);
10827 rb_gc_mark(lex_nextline);
10828 rb_gc_mark(ruby_sourcefile_string);
10830 rb_gc_mark((VALUE)ruby_eval_tree_begin);
10831 rb_gc_mark((VALUE)ruby_eval_tree);
10832 rb_gc_mark(ruby_debug_lines);
10833 rb_gc_mark(parser->compile_option);
10834 rb_gc_mark(parser->error_buffer);
10836 rb_gc_mark(parser->delayed);
10837 rb_gc_mark(parser->value);
10838 rb_gc_mark(parser->result);
10839 rb_gc_mark(parser->parsing_thread);
10841 rb_gc_mark(parser->debug_buffer);
10843 rb_gc_mark((VALUE)parser->heap);
10848 parser_free(void *ptr)
10850 struct parser_params *parser = (struct parser_params*)ptr;
10851 struct local_vars *local, *prev;
10856 for (local = lvtbl; local; local = prev) {
10857 if (local->vars) xfree(local->vars);
10858 prev = local->prev;
10862 token_info *ptinfo;
10863 while ((ptinfo = parser->token_info) != 0) {
10864 parser->token_info = ptinfo->next;
10872 parser_memsize(const void *ptr)
10874 struct parser_params *parser = (struct parser_params*)ptr;
10875 struct local_vars *local;
10876 size_t size = sizeof(*parser);
10879 for (local = lvtbl; local; local = local->prev) {
10880 size += sizeof(*local);
10881 if (local->vars) size += local->vars->capa * sizeof(ID);
10886 static const rb_data_type_t parser_data_type = {
10897 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
10901 #undef rb_reserved_word
10903 const struct kwtable *
10904 rb_reserved_word(const char *str, unsigned int len)
10906 return reserved_word(str, len);
10910 rb_parser_new(void)
10912 struct parser_params *p;
10913 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
10914 &parser_data_type, p);
10915 parser_initialize(p);
10920 rb_parser_set_context(VALUE vparser, const struct rb_block *base, int main)
10922 struct parser_params *parser;
10924 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10925 parser->error_buffer = main ? Qfalse : Qnil;
10926 parser->base_block = base;
10933 #define rb_parser_end_seen_p ripper_parser_end_seen_p
10934 #define rb_parser_encoding ripper_parser_encoding
10935 #define rb_parser_get_yydebug ripper_parser_get_yydebug
10936 #define rb_parser_set_yydebug ripper_parser_set_yydebug
10937 static VALUE ripper_parser_end_seen_p(VALUE vparser);
10938 static VALUE ripper_parser_encoding(VALUE vparser);
10939 static VALUE ripper_parser_get_yydebug(VALUE self);
10940 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
10944 * ripper#error? -> Boolean
10946 * Return true if parsed source has errors.
10949 ripper_error_p(VALUE vparser)
10951 struct parser_params *parser;
10953 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10954 return parser->error_p ? Qtrue : Qfalse;
10960 * ripper#end_seen? -> Boolean
10962 * Return true if parsed source ended by +\_\_END\_\_+.
10965 rb_parser_end_seen_p(VALUE vparser)
10967 struct parser_params *parser;
10969 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10970 return ruby__end__seen ? Qtrue : Qfalse;
10975 * ripper#encoding -> encoding
10977 * Return encoding of the source.
10980 rb_parser_encoding(VALUE vparser)
10982 struct parser_params *parser;
10984 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
10985 return rb_enc_from_encoding(current_enc);
10990 * ripper.yydebug -> true or false
10995 rb_parser_get_yydebug(VALUE self)
10997 struct parser_params *parser;
10999 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11000 return yydebug ? Qtrue : Qfalse;
11005 * ripper.yydebug = flag
11010 rb_parser_set_yydebug(VALUE self, VALUE flag)
11012 struct parser_params *parser;
11014 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11015 yydebug = RTEST(flag);
11021 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
11022 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
11023 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \
11024 (n)->u3.cnt = (c), (p))
11027 rb_parser_malloc(struct parser_params *parser, size_t size)
11029 size_t cnt = HEAPCNT(1, size);
11030 NODE *n = NEWHEAP();
11031 void *ptr = xmalloc(size);
11033 return ADD2HEAP(n, cnt, ptr);
11037 rb_parser_calloc(struct parser_params *parser, size_t nelem, size_t size)
11039 size_t cnt = HEAPCNT(nelem, size);
11040 NODE *n = NEWHEAP();
11041 void *ptr = xcalloc(nelem, size);
11043 return ADD2HEAP(n, cnt, ptr);
11047 rb_parser_realloc(struct parser_params *parser, void *ptr, size_t size)
11050 size_t cnt = HEAPCNT(1, size);
11052 if (ptr && (n = parser->heap) != NULL) {
11054 if (n->u1.node == ptr) {
11055 n->u1.node = ptr = xrealloc(ptr, size);
11056 if (n->u3.cnt) n->u3.cnt = cnt;
11059 } while ((n = n->u2.node) != NULL);
11062 ptr = xrealloc(ptr, size);
11063 return ADD2HEAP(n, cnt, ptr);
11067 rb_parser_free(struct parser_params *parser, void *ptr)
11069 NODE **prev = &parser->heap, *n;
11071 while ((n = *prev) != NULL) {
11072 if (n->u1.node == ptr) {
11073 *prev = n->u2.node;
11074 rb_gc_force_recycle((VALUE)n);
11077 prev = &n->u2.node;
11084 rb_parser_printf(struct parser_params *parser, const char *fmt, ...)
11087 VALUE mesg = parser->debug_buffer;
11089 if (NIL_P(mesg)) parser->debug_buffer = mesg = rb_str_new(0, 0);
11091 rb_str_vcatf(mesg, fmt, ap);
11093 if (RSTRING_END(mesg)[-1] == '\n') {
11094 rb_io_write(rb_stdout, mesg);
11095 parser->debug_buffer = Qnil;
11100 parser_compile_error(struct parser_params *parser, const char *fmt, ...)
11104 parser->error_p = 1;
11106 parser->error_buffer =
11107 rb_syntax_error_append(parser->error_buffer,
11108 ruby_sourcefile_string,
11110 rb_long2int(lex_p - lex_pbeg),
11111 current_enc, fmt, ap);
11117 #ifdef RIPPER_DEBUG
11118 extern int rb_is_pointer_to_heap(VALUE);
11122 ripper_validate_object(VALUE self, VALUE x)
11124 if (x == Qfalse) return x;
11125 if (x == Qtrue) return x;
11126 if (x == Qnil) return x;
11128 rb_raise(rb_eArgError, "Qundef given");
11129 if (FIXNUM_P(x)) return x;
11130 if (SYMBOL_P(x)) return x;
11131 if (!rb_is_pointer_to_heap(x))
11132 rb_raise(rb_eArgError, "invalid pointer: %p", x);
11133 switch (BUILTIN_TYPE(x)) {
11143 if (nd_type(x) != NODE_RIPPER) {
11144 rb_raise(rb_eArgError, "NODE given: %p", x);
11146 return ((NODE *)x)->nd_rval;
11148 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
11149 x, rb_obj_classname(x));
11155 #define validate(x) ((x) = get_value(x))
11158 ripper_dispatch0(struct parser_params *parser, ID mid)
11160 return rb_funcall(parser->value, mid, 0);
11164 ripper_dispatch1(struct parser_params *parser, ID mid, VALUE a)
11167 return rb_funcall(parser->value, mid, 1, a);
11171 ripper_dispatch2(struct parser_params *parser, ID mid, VALUE a, VALUE b)
11175 return rb_funcall(parser->value, mid, 2, a, b);
11179 ripper_dispatch3(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c)
11184 return rb_funcall(parser->value, mid, 3, a, b, c);
11188 ripper_dispatch4(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
11194 return rb_funcall(parser->value, mid, 4, a, b, c, d);
11198 ripper_dispatch5(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
11205 return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
11209 ripper_dispatch7(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
11218 return rb_funcall(parser->value, mid, 7, a, b, c, d, e, f, g);
11221 static const struct kw_assoc {
11224 } keyword_to_name[] = {
11225 {keyword_class, "class"},
11226 {keyword_module, "module"},
11227 {keyword_def, "def"},
11228 {keyword_undef, "undef"},
11229 {keyword_begin, "begin"},
11230 {keyword_rescue, "rescue"},
11231 {keyword_ensure, "ensure"},
11232 {keyword_end, "end"},
11233 {keyword_if, "if"},
11234 {keyword_unless, "unless"},
11235 {keyword_then, "then"},
11236 {keyword_elsif, "elsif"},
11237 {keyword_else, "else"},
11238 {keyword_case, "case"},
11239 {keyword_when, "when"},
11240 {keyword_while, "while"},
11241 {keyword_until, "until"},
11242 {keyword_for, "for"},
11243 {keyword_break, "break"},
11244 {keyword_next, "next"},
11245 {keyword_redo, "redo"},
11246 {keyword_retry, "retry"},
11247 {keyword_in, "in"},
11248 {keyword_do, "do"},
11249 {keyword_do_cond, "do"},
11250 {keyword_do_block, "do"},
11251 {keyword_return, "return"},
11252 {keyword_yield, "yield"},
11253 {keyword_super, "super"},
11254 {keyword_self, "self"},
11255 {keyword_nil, "nil"},
11256 {keyword_true, "true"},
11257 {keyword_false, "false"},
11258 {keyword_and, "and"},
11259 {keyword_or, "or"},
11260 {keyword_not, "not"},
11261 {modifier_if, "if"},
11262 {modifier_unless, "unless"},
11263 {modifier_while, "while"},
11264 {modifier_until, "until"},
11265 {modifier_rescue, "rescue"},
11266 {keyword_alias, "alias"},
11267 {keyword_defined, "defined?"},
11268 {keyword_BEGIN, "BEGIN"},
11269 {keyword_END, "END"},
11270 {keyword__LINE__, "__LINE__"},
11271 {keyword__FILE__, "__FILE__"},
11272 {keyword__ENCODING__, "__ENCODING__"},
11277 keyword_id_to_str(ID id)
11279 const struct kw_assoc *a;
11281 for (a = keyword_to_name; a->id; a++) {
11288 #undef ripper_id2sym
11290 ripper_id2sym(ID id)
11295 if (id == (ID)(signed char)id) {
11298 return ID2SYM(rb_intern2(buf, 1));
11300 if ((name = keyword_id_to_str(id))) {
11301 return ID2SYM(rb_intern(name));
11303 if (!rb_id2str(id)) {
11304 rb_bug("cannot convert ID to string: %ld", (unsigned long)id);
11310 ripper_get_id(VALUE v)
11313 if (!RB_TYPE_P(v, T_NODE)) return 0;
11315 if (nd_type(nd) != NODE_RIPPER) return 0;
11320 ripper_get_value(VALUE v)
11323 if (v == Qundef) return Qnil;
11324 if (!RB_TYPE_P(v, T_NODE)) return v;
11326 if (nd_type(nd) != NODE_RIPPER) return Qnil;
11327 return nd->nd_rval;
11331 ripper_error_gen(struct parser_params *parser)
11333 parser->error_p = TRUE;
11337 ripper_compile_error(struct parser_params *parser, const char *fmt, ...)
11342 va_start(args, fmt);
11343 str = rb_vsprintf(fmt, args);
11345 rb_funcall(parser->value, rb_intern("compile_error"), 1, str);
11346 ripper_error_gen(parser);
11350 ripper_lex_get_generic(struct parser_params *parser, VALUE src)
11352 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
11353 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
11354 rb_raise(rb_eTypeError,
11355 "gets returned %"PRIsVALUE" (expected String or nil)",
11356 rb_obj_class(line));
11362 ripper_lex_io_get(struct parser_params *parser, VALUE src)
11364 return rb_io_gets(src);
11368 ripper_s_allocate(VALUE klass)
11370 struct parser_params *p;
11371 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
11372 &parser_data_type, p);
11377 #define ripper_initialized_p(r) ((r)->lex.input != 0)
11381 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
11383 * Create a new Ripper object.
11384 * _src_ must be a String, an IO, or an Object which has #gets method.
11386 * This method does not starts parsing.
11387 * See also Ripper#parse and Ripper.parse.
11390 ripper_initialize(int argc, VALUE *argv, VALUE self)
11392 struct parser_params *parser;
11393 VALUE src, fname, lineno;
11395 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11396 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
11397 if (RB_TYPE_P(src, T_FILE)) {
11398 lex_gets = ripper_lex_io_get;
11400 else if (rb_respond_to(src, id_gets)) {
11401 lex_gets = ripper_lex_get_generic;
11405 lex_gets = lex_get_str;
11409 if (NIL_P(fname)) {
11410 fname = STR_NEW2("(ripper)");
11414 StringValue(fname);
11415 fname = rb_str_new_frozen(fname);
11417 parser_initialize(parser);
11419 ruby_sourcefile_string = fname;
11420 ruby_sourcefile = RSTRING_PTR(fname);
11421 ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
11426 struct ripper_args {
11427 struct parser_params *parser;
11433 ripper_parse0(VALUE parser_v)
11435 struct parser_params *parser;
11437 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser);
11438 parser_prepare(parser);
11439 ripper_yyparse((void*)parser);
11440 return parser->result;
11444 ripper_ensure(VALUE parser_v)
11446 struct parser_params *parser;
11448 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser);
11449 parser->parsing_thread = Qnil;
11457 * Start parsing and returns the value of the root action.
11460 ripper_parse(VALUE self)
11462 struct parser_params *parser;
11464 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11465 if (!ripper_initialized_p(parser)) {
11466 rb_raise(rb_eArgError, "method called for uninitialized object");
11468 if (!NIL_P(parser->parsing_thread)) {
11469 if (parser->parsing_thread == rb_thread_current())
11470 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
11472 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
11474 parser->parsing_thread = rb_thread_current();
11475 rb_ensure(ripper_parse0, self, ripper_ensure, self);
11477 return parser->result;
11482 * ripper#column -> Integer
11484 * Return column number of current parsing line.
11485 * This number starts from 0.
11488 ripper_column(VALUE self)
11490 struct parser_params *parser;
11493 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11494 if (!ripper_initialized_p(parser)) {
11495 rb_raise(rb_eArgError, "method called for uninitialized object");
11497 if (NIL_P(parser->parsing_thread)) return Qnil;
11498 col = parser->tokp - lex_pbeg;
11499 return LONG2NUM(col);
11504 * ripper#filename -> String
11506 * Return current parsing filename.
11509 ripper_filename(VALUE self)
11511 struct parser_params *parser;
11513 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11514 if (!ripper_initialized_p(parser)) {
11515 rb_raise(rb_eArgError, "method called for uninitialized object");
11517 return ruby_sourcefile_string;
11522 * ripper#lineno -> Integer
11524 * Return line number of current parsing line.
11525 * This number starts from 1.
11528 ripper_lineno(VALUE self)
11530 struct parser_params *parser;
11532 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11533 if (!ripper_initialized_p(parser)) {
11534 rb_raise(rb_eArgError, "method called for uninitialized object");
11536 if (NIL_P(parser->parsing_thread)) return Qnil;
11537 return INT2NUM(ruby_sourceline);
11540 #ifdef RIPPER_DEBUG
11543 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
11546 if (obj == Qundef) {
11547 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
11554 ripper_value(VALUE self, VALUE obj)
11556 return ULONG2NUM(obj);
11564 ripper_init_eventids1();
11565 ripper_init_eventids2();
11566 id_warn = rb_intern_const("warn");
11567 id_warning = rb_intern_const("warning");
11568 id_gets = rb_intern_const("gets");
11574 InitVM_ripper(void)
11578 Ripper = rb_define_class("Ripper", rb_cObject);
11579 /* version of Ripper */
11580 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
11581 rb_define_alloc_func(Ripper, ripper_s_allocate);
11582 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
11583 rb_define_method(Ripper, "parse", ripper_parse, 0);
11584 rb_define_method(Ripper, "column", ripper_column, 0);
11585 rb_define_method(Ripper, "filename", ripper_filename, 0);
11586 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
11587 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
11588 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
11589 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
11590 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
11591 rb_define_method(Ripper, "error?", ripper_error_p, 0);
11592 #ifdef RIPPER_DEBUG
11593 rb_define_method(rb_mKernel, "assert_Qundef", ripper_assert_Qundef, 2);
11594 rb_define_method(rb_mKernel, "rawVALUE", ripper_value, 1);
11595 rb_define_method(rb_mKernel, "validate_object", ripper_validate_object, 1);
11598 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
11599 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
11601 ripper_init_eventids1_table(Ripper);
11602 ripper_init_eventids2_table(Ripper);
11605 /* Hack to let RDoc document SCRIPT_LINES__ */
11608 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
11609 * after the assignment will be added as an Array of lines with the file
11612 rb_define_global_const("SCRIPT_LINES__", Qnil);
11616 #endif /* RIPPER */