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 YYERROR_VERBOSE 1
19 #define YYSTACK_USE_ALLOCA 0
20 #define YYLTYPE rb_code_location_t
21 #define YYLTYPE_IS_DECLARED 1
23 #include "ruby/ruby.h"
25 #include "ruby/encoding.h"
36 #ifndef WARN_PAST_SCOPE
37 # define WARN_PAST_SCOPE 0
42 #define yydebug (p->debug) /* disable the global variable definition */
44 #define YYMALLOC(size) rb_parser_malloc(p, (size))
45 #define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
46 #define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
47 #define YYFREE(ptr) rb_parser_free(p, (ptr))
48 #define YYFPRINTF rb_parser_printf
49 #define YYPRINT(out, tok, val) parser_token_value_print(p, (tok), &(val))
50 #define YY_LOCATION_PRINT(File, loc) \
51 rb_parser_printf(p, "%d.%d-%d.%d", \
52 (loc).beg_pos.lineno, (loc).beg_pos.column,\
53 (loc).end_pos.lineno, (loc).end_pos.column)
54 #define YYLLOC_DEFAULT(Current, Rhs, N) \
58 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
59 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
63 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
64 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
68 #define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
69 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
70 #define RUBY_SET_YYLLOC_OF_NONE(Current) \
71 rb_parser_set_location_of_none(p, &(Current))
72 #define RUBY_SET_YYLLOC(Current) \
73 rb_parser_set_location(p, &(Current))
74 #define RUBY_INIT_YYLLOC() \
76 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
77 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
81 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
82 EXPR_END_bit, /* newline significant, +/- is an operator. */
83 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
84 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
85 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
86 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
87 EXPR_MID_bit, /* newline significant, +/- is an operator. */
88 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
89 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
90 EXPR_CLASS_bit, /* immediate after `class', no here document. */
91 EXPR_LABEL_bit, /* flag bit, label is allowed. */
92 EXPR_LABELED_bit, /* flag bit, just after a label. */
93 EXPR_FITEM_bit, /* symbol literal as FNAME. */
96 /* examine combinations */
98 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
112 EXPR_VALUE = EXPR_BEG,
113 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
114 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
115 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
118 #define IS_lex_state_for(x, ls) ((x) & (ls))
119 #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
120 #define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
121 #define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
123 # define SET_LEX_STATE(ls) \
126 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
127 (enum lex_state_e)(ls)))
129 typedef VALUE stack_type;
131 static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
133 # define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
134 # define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
135 # define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
136 # define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
137 # define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
139 /* A flag to identify keyword_do_cond, "do" keyword after condition expression.
140 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
141 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
142 #define COND_POP() BITSTACK_POP(cond_stack)
143 #define COND_P() BITSTACK_SET_P(cond_stack)
144 #define COND_SET(n) BITSTACK_SET(cond_stack, (n))
146 /* A flag to identify keyword_do_block; "do" keyword after command_call.
147 Example: `foo 1, 2 do`. */
148 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
149 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
150 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
151 #define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
167 struct local_vars *prev;
170 NODE *outer, *inner, *current;
181 #define NUMPARAM_ID_P(id) numparam_id_p(id)
182 #define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
183 #define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
187 if (!is_local_id(id)) return 0;
188 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
189 return idx > 0 && idx <= NUMPARAM_MAX;
191 static void numparam_name(struct parser_params *p, ID id);
193 #define DVARS_INHERIT ((void*)1)
194 #define DVARS_TOPSCOPE NULL
195 #define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
197 typedef struct token_info {
199 rb_code_position_t beg;
202 struct token_info *next;
205 typedef struct rb_strterm_struct rb_strterm_t;
208 Structure of Lexer Buffer:
210 lex.pbeg lex.ptok lex.pcur lex.pend
212 |------------+------------+------------|
216 struct parser_params {
217 rb_imemo_tmpbuf_t *heap;
222 rb_strterm_t *strterm;
223 VALUE (*gets)(struct parser_params*,VALUE);
234 VALUE (*call)(VALUE, int);
236 enum lex_state_e state;
237 /* track the nest level of any parens "()[]{}" */
239 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
241 /* track the nest level of only braces "{}" */
244 stack_type cond_stack;
245 stack_type cmdarg_stack;
251 int heredoc_line_indent;
253 struct local_vars *lvtbl;
257 int ruby_sourceline; /* current line no. */
258 const char *ruby_sourcefile; /* current source file */
259 VALUE ruby_sourcefile_string;
261 token_info *token_info;
263 VALUE compile_option;
275 unsigned int command_start:1;
276 unsigned int eofp: 1;
277 unsigned int ruby__end__seen: 1;
278 unsigned int debug: 1;
279 unsigned int has_shebang: 1;
280 unsigned int in_defined: 1;
281 unsigned int in_kwarg: 1;
282 unsigned int in_def: 1;
283 unsigned int in_class: 1;
284 unsigned int token_seen: 1;
285 unsigned int token_info_enabled: 1;
287 unsigned int past_scope_enabled: 1;
289 unsigned int error_p: 1;
290 unsigned int cr_seen: 1;
295 unsigned int do_print: 1;
296 unsigned int do_loop: 1;
297 unsigned int do_chomp: 1;
298 unsigned int do_split: 1;
299 unsigned int warn_location: 1;
301 NODE *eval_tree_begin;
305 const struct rb_iseq_struct *parent_iseq;
317 VALUE parsing_thread;
321 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
323 #define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
324 #define STR_NEW0() rb_enc_str_new(0,0,p->enc)
325 #define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
326 #define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
327 #define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
330 push_pvtbl(struct parser_params *p)
332 st_table *tbl = p->pvtbl;
333 p->pvtbl = st_init_numtable();
338 pop_pvtbl(struct parser_params *p, st_table *tbl)
340 st_free_table(p->pvtbl);
345 push_pktbl(struct parser_params *p)
347 st_table *tbl = p->pktbl;
353 pop_pktbl(struct parser_params *p, st_table *tbl)
355 if (p->pktbl) st_free_table(p->pktbl);
359 static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
360 #define yyerror0(msg) parser_yyerror(p, NULL, (msg))
361 #define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
362 #define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
363 #define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
366 #define compile_for_eval (0)
368 #define compile_for_eval (p->parent_iseq != 0)
371 #define token_column ((int)(p->lex.ptok - p->lex.pbeg))
373 #define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
374 #define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
375 #define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
377 #define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
379 static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
383 rb_discard_node(struct parser_params *p, NODE *n)
385 rb_ast_delete_node(p->ast, n);
391 add_mark_object(struct parser_params *p, VALUE obj)
393 if (!SPECIAL_CONST_P(obj)
394 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
396 rb_ast_add_mark_object(p->ast, obj);
401 static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
404 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
405 #define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
407 static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
410 parser_get_node_id(struct parser_params *p)
412 int node_id = p->node_id;
419 set_line_body(NODE *body, int line)
422 switch (nd_type(body)) {
425 nd_set_line(body, line);
429 #define yyparse ruby_yyparse
431 static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432 static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
433 #define new_nil(loc) NEW_NIL(loc)
434 static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435 static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
436 static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
438 static NODE *newline_node(NODE*);
439 static void fixpos(NODE*,NODE*);
441 static int value_expr_gen(struct parser_params*,NODE*);
442 static void void_expr(struct parser_params*,NODE*);
443 static NODE *remove_begin(NODE*);
444 static NODE *remove_begin_all(NODE*);
445 #define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
446 static NODE *void_stmts(struct parser_params*,NODE*);
447 static void reduce_nodes(struct parser_params*,NODE**);
448 static void block_dup_check(struct parser_params*,NODE*,NODE*);
450 static NODE *block_append(struct parser_params*,NODE*,NODE*);
451 static NODE *list_append(struct parser_params*,NODE*,NODE*);
452 static NODE *list_concat(NODE*,NODE*);
453 static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
454 static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
455 static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
456 static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
457 static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
458 static NODE *evstr2dstr(struct parser_params*,NODE*);
459 static NODE *splat_array(NODE*);
460 static void mark_lvar_used(struct parser_params *p, NODE *rhs);
462 static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
463 static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
464 static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
465 static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
466 static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {b->nd_iter = m; b->nd_loc = *loc; return b;}
468 static bool args_info_empty_p(struct rb_args_info *args);
469 static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
470 static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
471 static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
472 static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
473 static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
474 static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
475 static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
477 static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
478 static NODE *args_with_numbered(struct parser_params*,NODE*,int);
480 static VALUE negate_lit(struct parser_params*, VALUE);
481 static NODE *ret_args(struct parser_params*,NODE*);
482 static NODE *arg_blk_pass(NODE*,NODE*);
483 static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
484 static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
486 static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
487 static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
489 static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
490 static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
492 static void rb_backref_error(struct parser_params*,NODE*);
493 static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
495 static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
496 static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
497 static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
498 static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
499 static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
501 static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
503 static NODE *opt_arg_append(NODE*, NODE*);
504 static NODE *kwd_append(NODE*, NODE*);
506 static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
507 static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
509 static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
511 static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
513 #define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
515 static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
517 static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
519 static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
521 static ID *local_tbl(struct parser_params*);
523 static VALUE reg_compile(struct parser_params*, VALUE, int);
524 static void reg_fragment_setenc(struct parser_params*, VALUE, int);
525 static int reg_fragment_check(struct parser_params*, VALUE, int);
526 static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
528 static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
529 static NODE *heredoc_dedent(struct parser_params*,NODE*);
531 static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
533 #define get_id(id) (id)
534 #define get_value(val) (val)
535 #define get_num(num) (num)
537 #define NODE_RIPPER NODE_CDECL
539 static inline int ripper_is_node_yylval(VALUE n);
542 ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
544 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
545 add_mark_object(p, b);
546 add_mark_object(p, c);
547 return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
551 ripper_is_node_yylval(VALUE n)
553 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
556 #define value_expr(node) ((void)(node))
557 #define remove_begin(node) (node)
558 #define void_stmts(p,x) (x)
559 #define rb_dvar_defined(id, base) 0
560 #define rb_local_defined(id, base) 0
561 static ID ripper_get_id(VALUE);
562 #define get_id(id) ripper_get_id(id)
563 static VALUE ripper_get_value(VALUE);
564 #define get_value(val) ripper_get_value(val)
565 #define get_num(num) (int)get_id(num)
566 static VALUE assignable(struct parser_params*,VALUE);
567 static int id_is_var(struct parser_params *p, ID id);
569 #define method_cond(p,node,loc) (node)
570 #define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
571 #define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
572 #define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
573 #define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
575 #define new_nil(loc) Qnil
577 static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
579 static VALUE const_decl(struct parser_params *p, VALUE path);
581 static VALUE var_field(struct parser_params *p, VALUE a);
582 static VALUE assign_error(struct parser_params *p, VALUE a);
584 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
588 /* forward declaration */
589 typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
591 RUBY_SYMBOL_EXPORT_BEGIN
592 VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
593 int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
594 enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
595 VALUE rb_parser_lex_state_name(enum lex_state_e state);
596 void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
597 PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
598 YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
599 YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
600 YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
601 RUBY_SYMBOL_EXPORT_END
603 static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
604 static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
605 static void parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp);
606 static ID formal_argument(struct parser_params*, ID);
607 static ID shadowing_lvar(struct parser_params*,ID);
608 static void new_bv(struct parser_params*,ID);
610 static void local_push(struct parser_params*,int);
611 static void local_pop(struct parser_params*);
612 static void local_var(struct parser_params*, ID);
613 static void arg_var(struct parser_params*, ID);
614 static int local_id(struct parser_params *p, ID id);
615 static int local_id_ref(struct parser_params*, ID, ID **);
617 static ID internal_id(struct parser_params*);
620 static const struct vtable *dyna_push(struct parser_params *);
621 static void dyna_pop(struct parser_params*, const struct vtable *);
622 static int dyna_in_block(struct parser_params*);
623 #define dyna_var(p, id) local_var(p, id)
624 static int dvar_defined(struct parser_params*, ID);
625 static int dvar_defined_ref(struct parser_params*, ID, ID**);
626 static int dvar_curr(struct parser_params*,ID);
628 static int lvar_defined(struct parser_params*, ID);
630 static NODE *numparam_push(struct parser_params *p);
631 static void numparam_pop(struct parser_params *p, NODE *prev_inner);
634 # define METHOD_NOT idNOT
636 # define METHOD_NOT '!'
639 #define idFWD_REST '*'
640 #ifdef RUBY3_KEYWORDS
641 #define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
643 #define idFWD_KWREST 0
645 #define idFWD_BLOCK '&'
647 #define RE_OPTION_ONCE (1<<16)
648 #define RE_OPTION_ENCODING_SHIFT 8
649 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
650 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
651 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
652 #define RE_OPTION_MASK 0xff
653 #define RE_OPTION_ARG_ENCODING_NONE 32
655 /* structs for managing terminator of string literal and heredocment */
656 typedef struct rb_strterm_literal_struct {
663 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
667 long paren; /* '(' of `%q(...)` */
671 long term; /* ')' of `%q(...)` */
673 } rb_strterm_literal_t;
675 #define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
677 struct rb_strterm_heredoc_struct {
678 VALUE lastline; /* the string of line that contains `<<"END"` */
679 long offset; /* the column of END in `<<"END"` */
680 int sourceline; /* lineno of the line that contains `<<"END"` */
681 unsigned length /* the length of END in `<<"END"` */
682 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
683 : HERETERM_LENGTH_BITS
684 # define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
686 # define HERETERM_LENGTH_MAX UINT_MAX
689 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
697 STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
699 #define STRTERM_HEREDOC IMEMO_FL_USER0
701 struct rb_strterm_struct {
704 rb_strterm_literal_t literal;
705 rb_strterm_heredoc_t heredoc;
711 rb_strterm_mark(VALUE obj)
713 rb_strterm_t *strterm = (rb_strterm_t*)obj;
714 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
715 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
716 rb_gc_mark(heredoc->lastline);
721 #define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
722 size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
724 #define TOKEN2ID(tok) ( \
725 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
726 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
727 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
728 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
729 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
730 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
731 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
733 /****** Ripper *******/
736 #define RIPPER_VERSION "0.1.0"
738 static inline VALUE intern_sym(const char *name);
740 #include "eventids1.c"
741 #include "eventids2.c"
743 static VALUE ripper_dispatch0(struct parser_params*,ID);
744 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
745 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
746 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
747 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
748 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
749 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
750 static void ripper_error(struct parser_params *p);
752 #define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
753 #define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
754 #define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
755 #define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
756 #define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
757 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
758 #define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
760 #define yyparse ripper_yyparse
762 #define ID2VAL(id) STATIC_ID2SYM(id)
763 #define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
764 #define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
766 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
767 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
769 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
772 new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
774 NODE *t = (NODE *)tail;
775 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
776 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
780 new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
782 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
783 add_mark_object(p, kw_args);
784 add_mark_object(p, kw_rest_arg);
785 add_mark_object(p, block);
790 args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
796 new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
798 NODE *t = (NODE *)aryptn;
799 struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
800 VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
803 pre_args = rb_ary_entry(apinfo->imemo, 0);
804 rest_arg = rb_ary_entry(apinfo->imemo, 1);
805 post_args = rb_ary_entry(apinfo->imemo, 2);
808 if (!NIL_P(pre_arg)) {
809 if (!NIL_P(pre_args)) {
810 rb_ary_unshift(pre_args, pre_arg);
813 pre_args = rb_ary_new_from_args(1, pre_arg);
816 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
820 new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
823 struct rb_ary_pattern_info *apinfo;
826 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
832 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
833 apinfo = ZALLOC(struct rb_ary_pattern_info);
834 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
835 apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
837 t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
838 RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
843 #define new_hash(p,h,l) rb_ary_new_from_args(0)
846 new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
852 new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
854 NODE *t = (NODE *)hshptn;
855 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
856 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
860 new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
864 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
869 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
871 add_mark_object(p, kw_args);
872 add_mark_object(p, kw_rest_arg);
876 #define new_defined(p,expr,loc) dispatch1(defined, (expr))
878 static VALUE heredoc_dedent(struct parser_params*,VALUE);
881 #define ID2VAL(id) (id)
882 #define TOKEN2VAL(t) ID2VAL(t)
883 #define KWD2EID(t, v) keyword_##t
889 # define ifndef_ripper(x) (x)
892 # define Qnull Qundef
893 # define ifndef_ripper(x)
896 # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
897 # define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
898 # define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
899 # define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
900 # define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
901 # define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
902 # define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
903 # define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
904 # define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
905 # define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
906 # define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
907 # define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
908 # define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
909 # define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
910 # define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
911 # define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
912 # define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
913 # define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
914 # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
915 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
917 static ID id_warn, id_warning, id_gets, id_assoc;
918 # define WARN_S_L(s,l) STR_NEW(s,l)
919 # define WARN_S(s) STR_NEW2(s)
920 # define WARN_I(i) INT2NUM(i)
921 # define WARN_ID(i) rb_id2str(i)
922 # define WARN_IVAL(i) i
923 # define PRIsWARN "s"
924 # define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
925 # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
926 # ifdef HAVE_VA_ARGS_MACRO
927 # define WARN_CALL(...) rb_funcall(__VA_ARGS__)
929 # define WARN_CALL rb_funcall
931 # define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
932 # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
933 # ifdef HAVE_VA_ARGS_MACRO
934 # define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
936 # define WARNING_CALL rb_funcall
938 PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
939 # define compile_error ripper_compile_error
941 # define WARN_S_L(s,l) s
944 # define WARN_ID(i) rb_id2name(i)
945 # define WARN_IVAL(i) NUM2INT(i)
946 # define PRIsWARN PRIsVALUE
947 # define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
948 # define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
949 # define WARN_CALL rb_compile_warn
950 # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
951 # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
952 # define WARNING_CALL rb_compile_warning
953 PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
954 # define compile_error parser_compile_error
957 static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
958 static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
959 static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
960 static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
962 #define WARN_EOL(tok) \
963 (looking_at_eol_p(p) ? \
964 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
966 static int looking_at_eol_p(struct parser_params *p);
971 %define parse.error verbose
972 %lex-param {struct parser_params *p}
973 %parse-param {struct parser_params *p}
976 RUBY_SET_YYLLOC_OF_NONE(@$);
985 const struct vtable *vars;
986 struct rb_strterm_struct *strterm;
990 keyword_class "`class'"
991 keyword_module "`module'"
993 keyword_undef "`undef'"
994 keyword_begin "`begin'"
995 keyword_rescue "`rescue'"
996 keyword_ensure "`ensure'"
999 keyword_unless "`unless'"
1000 keyword_then "`then'"
1001 keyword_elsif "`elsif'"
1002 keyword_else "`else'"
1003 keyword_case "`case'"
1004 keyword_when "`when'"
1005 keyword_while "`while'"
1006 keyword_until "`until'"
1008 keyword_break "`break'"
1009 keyword_next "`next'"
1010 keyword_redo "`redo'"
1011 keyword_retry "`retry'"
1014 keyword_do_cond "`do' for condition"
1015 keyword_do_block "`do' for block"
1016 keyword_do_LAMBDA "`do' for lambda"
1017 keyword_return "`return'"
1018 keyword_yield "`yield'"
1019 keyword_super "`super'"
1020 keyword_self "`self'"
1022 keyword_true "`true'"
1023 keyword_false "`false'"
1027 modifier_if "`if' modifier"
1028 modifier_unless "`unless' modifier"
1029 modifier_while "`while' modifier"
1030 modifier_until "`until' modifier"
1031 modifier_rescue "`rescue' modifier"
1032 keyword_alias "`alias'"
1033 keyword_defined "`defined?'"
1034 keyword_BEGIN "`BEGIN'"
1036 keyword__LINE__ "`__LINE__'"
1037 keyword__FILE__ "`__FILE__'"
1038 keyword__ENCODING__ "`__ENCODING__'"
1040 %token <val> tIDENTIFIER "local variable or method"
1041 %token <val> tFID "method"
1042 %token <val> tGVAR "global variable"
1043 %token <val> tIVAR "instance variable"
1044 %token <val> tCONSTANT "constant"
1045 %token <val> tCVAR "class variable"
1047 %token <val> tINTEGER "integer literal"
1048 %token <val> tFLOAT "float literal"
1049 %token <val> tRATIONAL "rational literal"
1050 %token <val> tIMAGINARY "imaginary literal"
1051 %token <val> tCHAR "char literal"
1052 %token <val> tNTH_REF "numbered reference"
1053 %token <val> tBACK_REF "back reference"
1054 %token <val> tSTRING_CONTENT "literal content"
1055 %token <val> tREGEXP_END
1057 %type <val> singleton strings string string1 xstring regexp
1058 %type <val> string_contents xstring_contents regexp_contents string_content
1059 %type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1060 %type <val> literal numeric simple_numeric ssym dsym symbol cpath
1061 %type <val> top_compstmt top_stmts top_stmt begin_block
1062 %type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1063 %type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1064 %type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1065 %type <val> args call_args opt_call_args
1066 %type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1067 %type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1068 %type <val> command_rhs arg_rhs
1069 %type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1070 %type <val> f_block_optarg f_block_opt
1071 %type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1072 %type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1073 %type <val> block_param opt_block_param block_param_def f_opt
1074 %type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1075 %type <val> bv_decls opt_bv_decl bvar
1076 %type <val> lambda f_larglist lambda_body brace_body do_body
1077 %type <val> brace_block cmd_brace_block do_block lhs none fitem
1078 %type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1079 %type <val> p_case_body p_cases p_top_expr p_top_expr_body
1080 %type <val> p_expr p_as p_alt p_expr_basic
1081 %type <val> p_args p_args_head p_args_tail p_args_post p_arg
1082 %type <val> p_value p_primitive p_variable p_var_ref p_const
1083 %type <val> p_kwargs p_kwarg p_kw
1084 %type <val> keyword_variable user_variable sym operation operation2 operation3
1085 %type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1086 %type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1087 %type <val> p_kwrest p_kwnorest p_kw_label
1088 %type <val> f_no_kwarg args_forward
1089 %token END_OF_INPUT 0 "end-of-input"
1091 /* escaped chars, should be ignored otherwise */
1092 %token <val> '\\' "backslash"
1093 %token tSP "escaped space"
1094 %token <val> '\t' "escaped horizontal tab"
1095 %token <val> '\f' "escaped form feed"
1096 %token <val> '\r' "escaped carriage return"
1097 %token <val> '\13' "escaped vertical tab"
1098 %token tUPLUS 132 "unary+"
1099 %token tUMINUS 133 "unary-"
1100 %token tPOW 134 "**"
1101 %token tCMP 135 "<=>"
1103 %token tEQQ 141 "==="
1104 %token tNEQ 142 "!="
1105 %token tGEQ 139 ">="
1106 %token tLEQ 138 "<="
1107 %token tANDOP 148 "&&"
1108 %token tOROP 149 "||"
1109 %token tMATCH 143 "=~"
1110 %token tNMATCH 144 "!~"
1111 %token tDOT2 128 ".."
1112 %token tDOT3 129 "..."
1113 %token tBDOT2 130 "(.."
1114 %token tBDOT3 131 "(..."
1115 %token tAREF 145 "[]"
1116 %token tASET 146 "[]="
1117 %token tLSHFT 136 "<<"
1118 %token tRSHFT 137 ">>"
1119 %token <val> tANDDOT 150 "&."
1120 %token <val> tCOLON2 147 "::"
1121 %token tCOLON3 ":: at EXPR_BEG"
1122 %token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1125 %token tLPAREN_ARG "( arg"
1129 %token tLBRACE_ARG "{ arg"
1131 %token tDSTAR "**arg"
1134 %token tSYMBEG "symbol literal"
1135 %token tSTRING_BEG "string literal"
1136 %token tXSTRING_BEG "backtick literal"
1137 %token tREGEXP_BEG "regexp literal"
1138 %token tWORDS_BEG "word list"
1139 %token tQWORDS_BEG "verbatim word list"
1140 %token tSYMBOLS_BEG "symbol list"
1141 %token tQSYMBOLS_BEG "verbatim symbol list"
1142 %token tSTRING_END "terminator"
1143 %token tSTRING_DEND "'}'"
1144 %token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1151 %nonassoc tLBRACE_ARG
1153 %nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1154 %left keyword_or keyword_and
1156 %nonassoc keyword_defined
1158 %left modifier_rescue
1160 %nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1163 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1164 %left '>' tGEQ '<' tLEQ
1170 %right tUMINUS_NUM tUMINUS
1172 %right '!' '~' tUPLUS
1178 SET_LEX_STATE(EXPR_BEG);
1179 local_push(p, ifndef_ripper(1)+0);
1184 if ($2 && !compile_for_eval) {
1186 /* last expression should not be void */
1187 if (nd_type(node) == NODE_BLOCK) {
1188 while (node->nd_next) {
1189 node = node->nd_next;
1191 node = node->nd_head;
1193 node = remove_begin(node);
1196 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1198 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1203 top_compstmt : top_stmts opt_terms
1205 $$ = void_stmts(p, $1);
1212 $$ = NEW_BEGIN(0, &@$);
1214 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1219 $$ = newline_node($1);
1221 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1223 | top_stmts terms top_stmt
1226 $$ = block_append(p, $1, newline_node($3));
1228 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1232 $$ = remove_begin($2);
1237 | keyword_BEGIN begin_block
1243 begin_block : '{' top_compstmt '}'
1246 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1247 NEW_BEGIN($2, &@$));
1248 $$ = NEW_BEGIN(0, &@$);
1250 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1256 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1261 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1263 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1270 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1272 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=Qnil;v4=escape_Qundef($3);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1276 compstmt : stmts opt_terms
1278 $$ = void_stmts(p, $1);
1285 $$ = NEW_BEGIN(0, &@$);
1287 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1292 $$ = newline_node($1);
1294 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1296 | stmts terms stmt_or_begin
1299 $$ = block_append(p, $1, newline_node($3));
1301 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1305 $$ = remove_begin($2);
1309 stmt_or_begin : stmt
1315 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1323 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1326 $$ = NEW_ALIAS($2, $4, &@$);
1328 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1330 | keyword_alias tGVAR tGVAR
1333 $$ = NEW_VALIAS($2, $3, &@$);
1335 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1337 | keyword_alias tGVAR tBACK_REF
1342 buf[1] = (char)$3->nd_nth;
1343 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1345 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1347 | keyword_alias tGVAR tNTH_REF
1350 yyerror1(&@3, "can't make alias for the number variables");
1351 $$ = NEW_BEGIN(0, &@$);
1353 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);v4=v3;v5=dispatch1(alias_error,v4);$$=v5;}ripper_error(p);
1355 | keyword_undef undef_list
1360 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1362 | stmt modifier_if expr_value
1365 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1368 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1370 | stmt modifier_unless expr_value
1373 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1376 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1378 | stmt modifier_while expr_value
1381 if ($1 && nd_type($1) == NODE_BEGIN) {
1382 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1385 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1388 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1390 | stmt modifier_until expr_value
1393 if ($1 && nd_type($1) == NODE_BEGIN) {
1394 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1397 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1400 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1402 | stmt modifier_rescue stmt
1406 YYLTYPE loc = code_loc_gen(&@2, &@3);
1407 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1408 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1410 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1412 | keyword_END '{' compstmt '}'
1415 rb_warn0("END in method; use at_exit");
1419 NODE *scope = NEW_NODE(
1420 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1421 $$ = NEW_POSTEXE(scope, &@$);
1424 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1427 | mlhs '=' command_call
1431 $$ = node_assign(p, $1, $3, &@$);
1433 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1439 $$ = node_assign(p, $1, $3, &@$);
1441 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1443 | mlhs '=' mrhs_arg modifier_rescue stmt
1446 YYLTYPE loc = code_loc_gen(&@4, &@5);
1448 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1450 {VALUE v1,v2,v3,v4,v5,v6;v1=$3;v2=$5;v3=dispatch2(rescue_mod,v1,v2);v4=$1;v5=v3;v6=dispatch2(massign,v4,v5);$$=v6;}
1455 $$ = node_assign(p, $1, $3, &@$);
1457 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1462 command_asgn : lhs '=' command_rhs
1465 $$ = node_assign(p, $1, $3, &@$);
1467 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1469 | var_lhs tOP_ASGN command_rhs
1472 $$ = new_op_assign(p, $1, $2, $3, &@$);
1474 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1476 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1479 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1481 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1484 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1487 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1489 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1491 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1494 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1496 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1498 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1501 YYLTYPE loc = code_loc_gen(&@1, &@3);
1502 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1504 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1506 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1509 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1511 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1513 | backref tOP_ASGN command_rhs
1516 rb_backref_error(p, $1);
1517 $$ = NEW_BEGIN(0, &@$);
1519 {VALUE v1,v2,v3,v4,v5;v1=var_field(p, $1);v2=$3;v3=dispatch2(assign,v1,v2);v4=v3;v5=dispatch1(assign_error,v4);$$=v5;}ripper_error(p);
1523 command_rhs : command_call %prec tOP_ASGN
1528 | command_call modifier_rescue stmt
1531 YYLTYPE loc = code_loc_gen(&@2, &@3);
1533 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1535 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1541 | expr keyword_and expr
1543 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1545 | expr keyword_or expr
1547 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1549 | keyword_not opt_nl expr
1551 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1555 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1560 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1561 p->command_start = FALSE;
1562 $<num>$ = p->in_kwarg;
1565 {$<tbl>$ = push_pvtbl(p);}
1567 {pop_pvtbl(p, $<tbl>4);}
1569 p->in_kwarg = !!$<num>3;
1571 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1573 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$5;v2=Qnil;v3=Qnil;v4=dispatch3(in,v1,v2,v3);v5=$1;v6=v4;v7=dispatch2(case,v5,v6);$$=v7;}
1575 | arg %prec tLBRACE_ARG
1585 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1591 command_call : command
1595 block_command : block_call
1596 | block_call call_op2 operation2 command_args
1599 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1601 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
1605 cmd_brace_block : tLBRACE_ARG brace_body '}'
1609 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1610 nd_set_line($$, @1.end_pos.lineno);
1618 $$ = NEW_FCALL($1, 0, &@$);
1619 nd_set_line($$, p->tokline);
1625 command : fcall command_args %prec tLOWEST
1629 nd_set_last_loc($1, @2.end_pos);
1632 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1634 | fcall command_args cmd_brace_block
1637 block_dup_check(p, $2, $3);
1639 $$ = method_add_block(p, $1, $3, &@$);
1641 nd_set_last_loc($1, @2.end_pos);
1643 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);v4=v3;v5=$3;v6=dispatch2(method_add_block,v4,v5);$$=v6;}
1645 | primary_value call_op operation2 command_args %prec tLOWEST
1648 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1650 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1652 | primary_value call_op operation2 command_args cmd_brace_block
1655 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1657 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1659 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1662 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1664 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1666 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1669 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1671 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1673 | keyword_super command_args
1676 $$ = NEW_SUPER($2, &@$);
1679 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1681 | keyword_yield command_args
1684 $$ = new_yield(p, $2, &@$);
1687 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1689 | k_return call_args
1692 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1694 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1696 | keyword_break call_args
1699 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1701 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1703 | keyword_next call_args
1706 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1708 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1713 | tLPAREN mlhs_inner rparen
1718 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1722 mlhs_inner : mlhs_basic
1723 | tLPAREN mlhs_inner rparen
1726 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1728 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1732 mlhs_basic : mlhs_head
1735 $$ = NEW_MASGN($1, 0, &@$);
1739 | mlhs_head mlhs_item
1742 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1744 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1746 | mlhs_head tSTAR mlhs_node
1749 $$ = NEW_MASGN($1, $3, &@$);
1751 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1753 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1756 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1758 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1763 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1765 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1767 | mlhs_head tSTAR ',' mlhs_post
1770 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1772 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$4;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1777 $$ = NEW_MASGN(0, $2, &@$);
1779 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1781 | tSTAR mlhs_node ',' mlhs_post
1784 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1786 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$4;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1791 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1793 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1795 | tSTAR ',' mlhs_post
1798 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1800 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1804 mlhs_item : mlhs_node
1805 | tLPAREN mlhs_inner rparen
1810 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1814 mlhs_head : mlhs_item ','
1817 $$ = NEW_LIST($1, &@1);
1819 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1821 | mlhs_head mlhs_item ','
1824 $$ = list_append(p, $1, $2);
1826 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1830 mlhs_post : mlhs_item
1833 $$ = NEW_LIST($1, &@$);
1835 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1837 | mlhs_post ',' mlhs_item
1840 $$ = list_append(p, $1, $3);
1842 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1846 mlhs_node : user_variable
1849 $$ = assignable(p, $1, 0, &@$);
1851 $$=assignable(p, var_field(p, $1));
1856 $$ = assignable(p, $1, 0, &@$);
1858 $$=assignable(p, var_field(p, $1));
1860 | primary_value '[' opt_call_args rbracket
1863 $$ = aryset(p, $1, $3, &@$);
1865 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1867 | primary_value call_op tIDENTIFIER
1869 if ($2 == tANDDOT) {
1870 yyerror1(&@2, "&. inside multiple assignment destination");
1873 $$ = attrset(p, $1, $2, $3, &@$);
1875 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1877 | primary_value tCOLON2 tIDENTIFIER
1880 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1882 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1884 | primary_value call_op tCONSTANT
1886 if ($2 == tANDDOT) {
1887 yyerror1(&@2, "&. inside multiple assignment destination");
1890 $$ = attrset(p, $1, $2, $3, &@$);
1892 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1894 | primary_value tCOLON2 tCONSTANT
1897 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1899 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1904 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1906 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1911 rb_backref_error(p, $1);
1912 $$ = NEW_BEGIN(0, &@$);
1914 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1921 $$ = assignable(p, $1, 0, &@$);
1923 $$=assignable(p, var_field(p, $1));
1928 $$ = assignable(p, $1, 0, &@$);
1930 $$=assignable(p, var_field(p, $1));
1932 | primary_value '[' opt_call_args rbracket
1935 $$ = aryset(p, $1, $3, &@$);
1937 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1939 | primary_value call_op tIDENTIFIER
1942 $$ = attrset(p, $1, $2, $3, &@$);
1944 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1946 | primary_value tCOLON2 tIDENTIFIER
1949 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1951 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1953 | primary_value call_op tCONSTANT
1956 $$ = attrset(p, $1, $2, $3, &@$);
1958 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1960 | primary_value tCOLON2 tCONSTANT
1963 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1965 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1970 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1972 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1977 rb_backref_error(p, $1);
1978 $$ = NEW_BEGIN(0, &@$);
1980 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1987 yyerror1(&@1, "class/module name must be CONSTANT");
1989 {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
1994 cpath : tCOLON3 cname
1997 $$ = NEW_COLON3($2, &@$);
1999 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2004 $$ = NEW_COLON2(0, $$, &@$);
2006 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2008 | primary_value tCOLON2 cname
2011 $$ = NEW_COLON2($1, $3, &@$);
2013 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2022 SET_LEX_STATE(EXPR_ENDFN);
2031 $$ = NEW_LIT(ID2SYM($1), &@$);
2033 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2041 $$ = NEW_UNDEF($1, &@$);
2043 $$=rb_ary_new3(1, get_value($1));
2045 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2048 NODE *undef = NEW_UNDEF($4, &@4);
2049 $$ = block_append(p, $1, undef);
2051 $$=rb_ary_push($1, get_value($4));
2055 op : '|' { ifndef_ripper($$ = '|'); }
2056 | '^' { ifndef_ripper($$ = '^'); }
2057 | '&' { ifndef_ripper($$ = '&'); }
2058 | tCMP { ifndef_ripper($$ = tCMP); }
2059 | tEQ { ifndef_ripper($$ = tEQ); }
2060 | tEQQ { ifndef_ripper($$ = tEQQ); }
2061 | tMATCH { ifndef_ripper($$ = tMATCH); }
2062 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2063 | '>' { ifndef_ripper($$ = '>'); }
2064 | tGEQ { ifndef_ripper($$ = tGEQ); }
2065 | '<' { ifndef_ripper($$ = '<'); }
2066 | tLEQ { ifndef_ripper($$ = tLEQ); }
2067 | tNEQ { ifndef_ripper($$ = tNEQ); }
2068 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2069 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2070 | '+' { ifndef_ripper($$ = '+'); }
2071 | '-' { ifndef_ripper($$ = '-'); }
2072 | '*' { ifndef_ripper($$ = '*'); }
2073 | tSTAR { ifndef_ripper($$ = '*'); }
2074 | '/' { ifndef_ripper($$ = '/'); }
2075 | '%' { ifndef_ripper($$ = '%'); }
2076 | tPOW { ifndef_ripper($$ = tPOW); }
2077 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2078 | '!' { ifndef_ripper($$ = '!'); }
2079 | '~' { ifndef_ripper($$ = '~'); }
2080 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2081 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2082 | tAREF { ifndef_ripper($$ = tAREF); }
2083 | tASET { ifndef_ripper($$ = tASET); }
2084 | '`' { ifndef_ripper($$ = '`'); }
2087 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2088 | keyword_BEGIN | keyword_END
2089 | keyword_alias | keyword_and | keyword_begin
2090 | keyword_break | keyword_case | keyword_class | keyword_def
2091 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2092 | keyword_end | keyword_ensure | keyword_false
2093 | keyword_for | keyword_in | keyword_module | keyword_next
2094 | keyword_nil | keyword_not | keyword_or | keyword_redo
2095 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2096 | keyword_super | keyword_then | keyword_true | keyword_undef
2097 | keyword_when | keyword_yield | keyword_if | keyword_unless
2098 | keyword_while | keyword_until
2101 arg : lhs '=' arg_rhs
2104 $$ = node_assign(p, $1, $3, &@$);
2106 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2108 | var_lhs tOP_ASGN arg_rhs
2111 $$ = new_op_assign(p, $1, $2, $3, &@$);
2113 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2115 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2119 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2121 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2123 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2127 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2129 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2131 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2135 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2137 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2139 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2143 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2145 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2147 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2150 YYLTYPE loc = code_loc_gen(&@1, &@3);
2151 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2153 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2155 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2158 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2160 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=dispatch1(top_const_field,v1);v3=v2;v4=$3;v5=$4;v6=dispatch3(opassign,v3,v4,v5);$$=v6;}
2162 | backref tOP_ASGN arg_rhs
2165 rb_backref_error(p, $1);
2166 $$ = NEW_BEGIN(0, &@$);
2168 {VALUE v1,v2,v3,v4,v5,v6;v1=var_field(p, $1);v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);v5=v4;v6=dispatch1(assign_error,v5);$$=v6;}ripper_error(p);
2175 $$ = NEW_DOT2($1, $3, &@$);
2177 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2184 $$ = NEW_DOT3($1, $3, &@$);
2186 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2192 loc.beg_pos = @2.end_pos;
2193 loc.end_pos = @2.end_pos;
2196 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2198 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2204 loc.beg_pos = @2.end_pos;
2205 loc.end_pos = @2.end_pos;
2208 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2210 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2216 loc.beg_pos = @1.beg_pos;
2217 loc.end_pos = @1.beg_pos;
2220 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2222 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2228 loc.beg_pos = @1.beg_pos;
2229 loc.end_pos = @1.beg_pos;
2232 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2234 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2238 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2242 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2246 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2250 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2254 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2258 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2260 | tUMINUS_NUM simple_numeric tPOW arg
2262 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2266 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2270 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2274 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2278 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2282 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2286 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2288 | rel_expr %prec tCMP
2291 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2295 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2299 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2303 $$ = match_op(p, $1, $3, &@2, &@$);
2307 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2311 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2315 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2319 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2323 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2327 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2331 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2333 | keyword_defined opt_nl {p->in_defined = 1;} arg
2336 $$ = new_defined(p, $4, &@$);
2338 | arg '?' arg opt_nl ':' arg
2342 $$ = new_if(p, $1, $3, $6, &@$);
2345 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2353 relop : '>' {$$ = '>';}
2359 rel_expr : arg relop arg %prec '>'
2361 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2363 | rel_expr relop arg %prec '>'
2365 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2366 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2382 | args ',' assocs trailer
2385 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2387 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2392 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2394 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2398 arg_rhs : arg %prec tOP_ASGN
2403 | arg modifier_rescue arg
2406 YYLTYPE loc = code_loc_gen(&@2, &@3);
2408 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2410 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2414 paren_args : '(' opt_call_args rparen
2419 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2421 | '(' args_forward rparen
2423 if (!local_id(p, idFWD_REST) ||
2425 !local_id(p, idFWD_KWREST) ||
2427 !local_id(p, idFWD_BLOCK)) {
2428 compile_error(p, "unexpected ...");
2433 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2435 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2437 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2439 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2443 $$ = arg_blk_pass($$, block);
2445 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2450 opt_paren_args : none
2454 opt_call_args : none
2460 | args ',' assocs ','
2463 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2465 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2470 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2472 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2480 $$ = NEW_LIST($1, &@$);
2482 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2484 | args opt_block_arg
2487 $$ = arg_blk_pass($1, $2);
2489 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2491 | assocs opt_block_arg
2494 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2495 $$ = arg_blk_pass($$, $2);
2497 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);v7=v6;v8=$2;v9=dispatch2(args_add_block,v7,v8);$$=v9;}
2499 | args ',' assocs opt_block_arg
2502 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2503 $$ = arg_blk_pass($$, $4);
2505 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);v6=v5;v7=$4;v8=dispatch2(args_add_block,v6,v7);$$=v8;}
2508 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2512 /* If call_args starts with a open paren '(' or '[',
2513 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2514 * but the push must be done after CMDARG_PUSH(1).
2515 * So this code makes them consistent by first cancelling
2516 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2517 * and finally redoing CMDARG_PUSH(0).
2521 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2524 if (lookahead) CMDARG_POP();
2526 if (lookahead) CMDARG_PUSH(0);
2530 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2531 * but the push must be done after CMDARG_POP() in the parser.
2532 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2533 * CMDARG_POP() to pop 1 pushed by command_args,
2534 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2541 if (lookahead) CMDARG_POP();
2543 if (lookahead) CMDARG_PUSH(0);
2548 block_arg : tAMPER arg_value
2551 $$ = NEW_BLOCK_PASS($2, &@$);
2557 opt_block_arg : ',' block_arg
2570 $$ = NEW_LIST($1, &@$);
2572 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2577 $$ = NEW_SPLAT($2, &@$);
2579 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2581 | args ',' arg_value
2584 $$ = last_arg_append(p, $1, $3, &@$);
2586 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2588 | args ',' tSTAR arg_value
2591 $$ = rest_arg_append(p, $1, $4, &@$);
2593 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2601 mrhs : args ',' arg_value
2604 $$ = last_arg_append(p, $1, $3, &@$);
2606 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$3;v5=dispatch2(mrhs_add,v3,v4);$$=v5;}
2608 | args ',' tSTAR arg_value
2611 $$ = rest_arg_append(p, $1, $4, &@$);
2613 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$4;v5=dispatch2(mrhs_add_star,v3,v4);$$=v5;}
2618 $$ = NEW_SPLAT($2, &@$);
2620 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2637 $$ = NEW_FCALL($1, 0, &@$);
2639 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);$$=v6;}
2650 set_line_body($3, @1.end_pos.lineno);
2651 $$ = NEW_BEGIN($3, &@$);
2652 nd_set_line($$, @1.end_pos.lineno);
2654 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2656 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2659 $$ = NEW_BEGIN(0, &@$);
2661 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2663 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2666 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2669 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2671 | tLPAREN compstmt ')'
2674 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2677 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2679 | primary_value tCOLON2 tCONSTANT
2682 $$ = NEW_COLON2($1, $3, &@$);
2684 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2689 $$ = NEW_COLON3($2, &@$);
2691 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2693 | tLBRACK aref_args ']'
2696 $$ = make_list($2, &@$);
2698 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2700 | tLBRACE assoc_list '}'
2703 $$ = new_hash(p, $2, &@$);
2704 $$->nd_brace = TRUE;
2706 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2711 $$ = NEW_RETURN(0, &@$);
2713 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2715 | keyword_yield '(' call_args rparen
2718 $$ = new_yield(p, $3, &@$);
2720 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2722 | keyword_yield '(' rparen
2725 $$ = NEW_YIELD(0, &@$);
2727 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2732 $$ = NEW_YIELD(0, &@$);
2734 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2736 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2739 $$ = new_defined(p, $5, &@$);
2741 | keyword_not '(' expr rparen
2743 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2745 | keyword_not '(' rparen
2747 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2752 $$ = method_add_block(p, $1, $2, &@$);
2754 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);v7=v6;v8=$2;v9=dispatch2(method_add_block,v7,v8);$$=v9;}
2757 | method_call brace_block
2760 block_dup_check(p, $1->nd_args, $2);
2761 $$ = method_add_block(p, $1, $2, &@$);
2763 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2767 token_info_push(p, "->", &@1);
2773 nd_set_first_loc($$, @1.beg_pos);
2776 | k_if expr_value then
2782 $$ = new_if(p, $2, $4, $5, &@$);
2785 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2787 | k_unless expr_value then
2793 $$ = new_unless(p, $2, $4, $5, &@$);
2796 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2798 | k_while expr_value_do
2803 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2806 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2808 | k_until expr_value_do
2813 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2816 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2818 | k_case expr_value opt_terms
2820 $<val>$ = p->case_labels;
2821 p->case_labels = Qnil;
2826 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2827 p->case_labels = $<val>4;
2829 $$ = NEW_CASE($2, $5, &@$);
2832 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2836 $<val>$ = p->case_labels;
2842 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2843 p->case_labels = $<val>3;
2845 $$ = NEW_CASE2($4, &@$);
2847 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2849 | k_case expr_value opt_terms
2854 $$ = new_case3(p, $2, $4, &@$);
2856 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2858 | k_for for_var keyword_in expr_value_do
2866 * e.each{|*x| a, b, c = x}
2870 * e.each{|x| a, = x}
2872 ID id = internal_id(p);
2873 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2874 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2875 ID *tbl = ALLOC_N(ID, 3);
2876 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2877 rb_ast_add_local_table(p->ast, tbl);
2879 switch (nd_type($2)) {
2882 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2883 $2->nd_value = internal_var;
2888 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2889 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2891 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2892 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2894 /* {|*internal_id| <m> = internal_id; ... } */
2895 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2896 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2897 $$ = NEW_FOR($4, scope, &@$);
2900 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2902 | k_class cpath superclass
2905 YYLTYPE loc = code_loc_gen(&@1, &@2);
2906 yyerror1(&loc, "class definition in method body");
2908 $<num>1 = p->in_class;
2916 $$ = NEW_CLASS($2, $5, $3, &@$);
2917 nd_set_line($$->nd_body, @6.end_pos.lineno);
2918 set_line_body($5, @3.end_pos.lineno);
2919 nd_set_line($$, @3.end_pos.lineno);
2921 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2923 p->in_class = $<num>1 & 1;
2925 | k_class tLSHFT expr
2927 $<num>$ = (p->in_class << 1) | p->in_def;
2937 $$ = NEW_SCLASS($3, $6, &@$);
2938 nd_set_line($$->nd_body, @7.end_pos.lineno);
2939 set_line_body($6, nd_line($3));
2942 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2944 p->in_def = $<num>4 & 1;
2945 p->in_class = ($<num>4 >> 1) & 1;
2950 YYLTYPE loc = code_loc_gen(&@1, &@2);
2951 yyerror1(&loc, "module definition in method body");
2953 $<num>1 = p->in_class;
2961 $$ = NEW_MODULE($2, $4, &@$);
2962 nd_set_line($$->nd_body, @5.end_pos.lineno);
2963 set_line_body($4, @2.end_pos.lineno);
2964 nd_set_line($$, @2.end_pos.lineno);
2966 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
2968 p->in_class = $<num>1 & 1;
2972 numparam_name(p, get_id($2));
2974 $<id>$ = p->cur_arg;
2978 $<num>$ = p->in_def;
2986 NODE *body = remove_begin($6);
2987 reduce_nodes(p, &body);
2988 $$ = NEW_DEFN($2, $5, body, &@$);
2989 nd_set_line($$->nd_defn, @7.end_pos.lineno);
2990 set_line_body(body, @1.beg_pos.lineno);
2992 {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
2994 p->in_def = $<num>4 & 1;
2995 p->cur_arg = $<id>3;
2997 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
2999 numparam_name(p, get_id($5));
3000 $<num>4 = p->in_def;
3002 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3004 $<id>$ = p->cur_arg;
3012 NODE *body = remove_begin($8);
3013 reduce_nodes(p, &body);
3014 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3015 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3016 set_line_body(body, @1.beg_pos.lineno);
3018 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=$3;v3=$5;v4=$7;v5=$8;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
3020 p->in_def = $<num>4 & 1;
3021 p->cur_arg = $<id>6;
3026 $$ = NEW_BREAK(0, &@$);
3028 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3033 $$ = NEW_NEXT(0, &@$);
3035 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3042 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3047 $$ = NEW_RETRY(&@$);
3049 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3053 primary_value : primary
3060 k_begin : keyword_begin
3062 token_info_push(p, "begin", &@$);
3069 token_info_push(p, "if", &@$);
3070 if (p->token_info && p->token_info->nonspc &&
3071 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3072 const char *tok = p->lex.ptok;
3073 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3074 beg += rb_strlen_lit("else");
3075 while (beg < tok && ISSPACE(*beg)) beg++;
3077 p->token_info->nonspc = 0;
3083 k_unless : keyword_unless
3085 token_info_push(p, "unless", &@$);
3089 k_while : keyword_while
3091 token_info_push(p, "while", &@$);
3095 k_until : keyword_until
3097 token_info_push(p, "until", &@$);
3101 k_case : keyword_case
3103 token_info_push(p, "case", &@$);
3109 token_info_push(p, "for", &@$);
3113 k_class : keyword_class
3115 token_info_push(p, "class", &@$);
3119 k_module : keyword_module
3121 token_info_push(p, "module", &@$);
3127 token_info_push(p, "def", &@$);
3133 token_info_push(p, "do", &@$);
3137 k_do_block : keyword_do_block
3139 token_info_push(p, "do", &@$);
3143 k_rescue : keyword_rescue
3145 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3149 k_ensure : keyword_ensure
3151 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3155 k_when : keyword_when
3157 token_info_warn(p, "when", p->token_info, 0, &@$);
3161 k_else : keyword_else
3163 token_info *ptinfo_beg = p->token_info;
3164 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3165 token_info_warn(p, "else", p->token_info, same, &@$);
3168 e.next = ptinfo_beg->next;
3170 token_info_setup(&e, p->lex.pbeg, &@$);
3171 if (!e.nonspc) *ptinfo_beg = e;
3176 k_elsif : keyword_elsif
3179 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3185 token_info_pop(p, "end", &@$);
3189 k_return : keyword_return
3191 if (p->in_class && !p->in_def && !dyna_in_block(p))
3192 yyerror1(&@1, "Invalid return in class/module body");
3206 | k_elsif expr_value then
3211 $$ = new_if(p, $2, $4, $5, &@$);
3214 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3224 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3235 $$ = assignable(p, $1, 0, &@$);
3236 mark_lvar_used(p, $$);
3238 $$=assignable(p, $1);
3240 | tLPAREN f_margs rparen
3245 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3249 f_marg_list : f_marg
3252 $$ = NEW_LIST($1, &@$);
3254 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3256 | f_marg_list ',' f_marg
3259 $$ = list_append(p, $1, $3);
3261 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3265 f_margs : f_marg_list
3268 $$ = NEW_MASGN($1, 0, &@$);
3272 | f_marg_list ',' f_rest_marg
3275 $$ = NEW_MASGN($1, $3, &@$);
3277 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3279 | f_marg_list ',' f_rest_marg ',' f_marg_list
3282 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3284 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
3289 $$ = NEW_MASGN(0, $1, &@$);
3291 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3293 | f_rest_marg ',' f_marg_list
3296 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3298 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
3302 f_rest_marg : tSTAR f_norm_arg
3305 $$ = assignable(p, $2, 0, &@$);
3306 mark_lvar_used(p, $$);
3308 $$=assignable(p, $2);
3313 $$ = NODE_SPECIAL_NO_NAME_REST;
3319 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3321 $$ = new_args_tail(p, $1, $3, $4, &@3);
3323 | f_block_kwarg opt_f_block_arg
3325 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3327 | f_kwrest opt_f_block_arg
3329 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3331 | f_no_kwarg opt_f_block_arg
3333 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3337 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3341 opt_block_args_tail : ',' block_args_tail
3347 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3351 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3353 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3355 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3357 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3359 | f_arg ',' f_block_optarg opt_block_args_tail
3361 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3363 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3365 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3367 | f_arg ',' f_rest_arg opt_block_args_tail
3369 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3374 /* magic number for rest_id in iseq_set_arguments() */
3375 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3377 {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3379 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3381 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3383 | f_arg opt_block_args_tail
3385 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3387 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3389 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3391 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3393 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3395 | f_block_optarg opt_block_args_tail
3397 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3399 | f_block_optarg ',' f_arg opt_block_args_tail
3401 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3403 | f_rest_arg opt_block_args_tail
3405 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3407 | f_rest_arg ',' f_arg opt_block_args_tail
3409 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3413 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3417 opt_block_param : none
3420 p->command_start = TRUE;
3424 block_param_def : '|' opt_bv_decl '|'
3427 p->max_numparam = ORDINAL_PARAM;
3431 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11;v1=Qnil;v2=Qnil;v3=Qnil;v4=Qnil;v5=Qnil;v6=Qnil;v7=Qnil;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=escape_Qundef($2);v11=dispatch2(block_var,v9,v10);$$=v11;}
3433 | '|' block_param opt_bv_decl '|'
3436 p->max_numparam = ORDINAL_PARAM;
3440 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3445 opt_bv_decl : opt_nl
3449 | opt_nl ';' bv_decls opt_nl
3459 {$$=rb_ary_new3(1, get_value($1));}
3461 {$$=rb_ary_push($1, get_value($3));}
3466 new_bv(p, get_id($1));
3476 $<vars>$ = dyna_push(p);
3479 $<num>$ = p->lex.lpar_beg;
3480 p->lex.lpar_beg = p->lex.paren_nest;
3483 $<num>$ = p->max_numparam;
3484 p->max_numparam = 0;
3487 $<node>$ = numparam_push(p);
3495 int max_numparam = p->max_numparam;
3496 p->lex.lpar_beg = $<num>2;
3497 p->max_numparam = $<num>3;
3499 $5 = args_with_numbered(p, $5, max_numparam);
3502 YYLTYPE loc = code_loc_gen(&@5, &@7);
3503 $$ = NEW_LAMBDA($5, $7, &loc);
3504 nd_set_line($$->nd_body, @7.end_pos.lineno);
3505 nd_set_line($$, @5.end_pos.lineno);
3508 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3509 numparam_pop(p, $<node>4);
3510 dyna_pop(p, $<vars>1);
3514 f_larglist : '(' f_args opt_bv_decl ')'
3518 p->max_numparam = ORDINAL_PARAM;
3520 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3525 if (!args_info_empty_p($1->nd_ainfo))
3526 p->max_numparam = ORDINAL_PARAM;
3532 lambda_body : tLAMBEG compstmt '}'
3534 token_info_pop(p, "}", &@3);
3537 | keyword_do_LAMBDA bodystmt k_end
3543 do_block : k_do_block do_body k_end
3547 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3548 nd_set_line($$, @1.end_pos.lineno);
3553 block_call : command do_block
3556 if (nd_type($1) == NODE_YIELD) {
3557 compile_error(p, "block given to yield");
3560 block_dup_check(p, $1->nd_args, $2);
3562 $$ = method_add_block(p, $1, $2, &@$);
3565 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3567 | block_call call_op2 operation2 opt_paren_args
3570 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3572 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3574 | block_call call_op2 operation2 opt_paren_args brace_block
3577 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3579 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=v7==Qundef ? v6 : dispatch2(method_add_block,v6,v7);$$=v8;}
3581 | block_call call_op2 operation2 command_args do_block
3584 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3586 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
3590 method_call : fcall paren_args
3595 nd_set_last_loc($1, @2.end_pos);
3597 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3599 | primary_value call_op operation2 opt_paren_args
3602 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3603 nd_set_line($$, @3.end_pos.lineno);
3605 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3607 | primary_value tCOLON2 operation2 paren_args
3610 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3611 nd_set_line($$, @3.end_pos.lineno);
3613 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3615 | primary_value tCOLON2 operation3
3618 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3620 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3622 | primary_value call_op paren_args
3625 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3626 nd_set_line($$, @2.end_pos.lineno);
3628 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3630 | primary_value tCOLON2 paren_args
3633 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3634 nd_set_line($$, @2.end_pos.lineno);
3636 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3638 | keyword_super paren_args
3641 $$ = NEW_SUPER($2, &@$);
3643 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3648 $$ = NEW_ZSUPER(&@$);
3650 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3652 | primary_value '[' opt_call_args rbracket
3655 if ($1 && nd_type($1) == NODE_SELF)
3656 $$ = NEW_FCALL(tAREF, $3, &@$);
3658 $$ = NEW_CALL($1, tAREF, $3, &@$);
3661 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3665 brace_block : '{' brace_body '}'
3669 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3670 nd_set_line($$, @1.end_pos.lineno);
3673 | k_do do_body k_end
3677 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3678 nd_set_line($$, @1.end_pos.lineno);
3683 brace_body : {$<vars>$ = dyna_push(p);}
3685 $<num>$ = p->max_numparam;
3686 p->max_numparam = 0;
3689 $<node>$ = numparam_push(p);
3691 opt_block_param compstmt
3693 int max_numparam = p->max_numparam;
3694 p->max_numparam = $<num>2;
3695 $4 = args_with_numbered(p, $4, max_numparam);
3697 $$ = NEW_ITER($4, $5, &@$);
3699 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3700 numparam_pop(p, $<node>3);
3701 dyna_pop(p, $<vars>1);
3705 do_body : {$<vars>$ = dyna_push(p);}
3707 $<num>$ = p->max_numparam;
3708 p->max_numparam = 0;
3711 $<node>$ = numparam_push(p);
3714 opt_block_param bodystmt
3716 int max_numparam = p->max_numparam;
3717 p->max_numparam = $<num>2;
3718 $4 = args_with_numbered(p, $4, max_numparam);
3720 $$ = NEW_ITER($4, $5, &@$);
3722 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3724 numparam_pop(p, $<node>3);
3725 dyna_pop(p, $<vars>1);
3729 case_args : arg_value
3732 check_literal_when(p, $1, &@1);
3733 $$ = NEW_LIST($1, &@$);
3735 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3740 $$ = NEW_SPLAT($2, &@$);
3742 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3744 | case_args ',' arg_value
3747 check_literal_when(p, $3, &@3);
3748 $$ = last_arg_append(p, $1, $3, &@$);
3750 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3752 | case_args ',' tSTAR arg_value
3755 $$ = rest_arg_append(p, $1, $4, &@$);
3757 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3761 case_body : k_when case_args then
3766 $$ = NEW_WHEN($2, $4, $5, &@$);
3769 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3777 p_case_body : keyword_in
3779 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3780 p->command_start = FALSE;
3781 $<num>$ = p->in_kwarg;
3784 {$<tbl>$ = push_pvtbl(p);}
3785 {$<tbl>$ = push_pktbl(p);}
3787 {pop_pktbl(p, $<tbl>4);}
3788 {pop_pvtbl(p, $<tbl>3);}
3790 p->in_kwarg = !!$<num>2;
3796 $$ = NEW_IN($5, $10, $11, &@$);
3798 {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3806 p_top_expr : p_top_expr_body
3807 | p_top_expr_body modifier_if expr_value
3810 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3813 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3815 | p_top_expr_body modifier_unless expr_value
3818 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3821 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3825 p_top_expr_body : p_expr
3828 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3829 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3833 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3835 nd_set_first_loc($$, @1.beg_pos);
3841 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3845 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3852 p_as : p_expr tASSOC p_variable
3855 NODE *n = NEW_LIST($1, &@$);
3856 n = list_append(p, n, $3);
3857 $$ = new_hash(p, n, &@$);
3859 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3864 p_alt : p_alt '|' p_expr_basic
3867 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3869 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3874 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3875 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3877 p_expr_basic : p_value
3878 | p_const p_lparen p_args rparen
3880 pop_pktbl(p, $<tbl>2);
3881 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3883 nd_set_first_loc($$, @1.beg_pos);
3887 | p_const p_lparen p_kwargs rparen
3889 pop_pktbl(p, $<tbl>2);
3890 $$ = new_hash_pattern(p, $1, $3, &@$);
3892 nd_set_first_loc($$, @1.beg_pos);
3896 | p_const '(' rparen
3898 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3899 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3901 | p_const p_lbracket p_args rbracket
3903 pop_pktbl(p, $<tbl>2);
3904 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3906 nd_set_first_loc($$, @1.beg_pos);
3910 | p_const p_lbracket p_kwargs rbracket
3912 pop_pktbl(p, $<tbl>2);
3913 $$ = new_hash_pattern(p, $1, $3, &@$);
3915 nd_set_first_loc($$, @1.beg_pos);
3919 | p_const '[' rbracket
3921 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3922 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3924 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3926 pop_pktbl(p, $<tbl>2);
3927 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3931 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3932 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3936 $<tbl>$ = push_pktbl(p);
3937 $<num>1 = p->in_kwarg;
3942 pop_pktbl(p, $<tbl>2);
3943 p->in_kwarg = $<num>1;
3944 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3948 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3949 $$ = new_hash_pattern(p, Qnone, $$, &@$);
3951 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3953 pop_pktbl(p, $<tbl>2);
3961 NODE *pre_args = NEW_LIST($1, &@$);
3962 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3964 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3969 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3974 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
3976 VALUE pre_args = rb_ary_concat($1, get_value($2));
3977 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3980 | p_args_head tSTAR tIDENTIFIER
3982 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
3984 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
3986 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
3990 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3992 | p_args_head tSTAR ',' p_args_post
3994 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
3999 p_args_head : p_arg ','
4003 | p_args_head p_arg ','
4006 $$ = list_concat($1, $2);
4008 $$=rb_ary_concat($1, get_value($2));
4012 p_args_tail : tSTAR tIDENTIFIER
4014 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4016 | tSTAR tIDENTIFIER ',' p_args_post
4018 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4022 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4024 | tSTAR ',' p_args_post
4026 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4031 | p_args_post ',' p_arg
4034 $$ = list_concat($1, $3);
4036 $$=rb_ary_concat($1, get_value($3));
4043 $$ = NEW_LIST($1, &@$);
4045 $$=rb_ary_new_from_args(1, get_value($1));
4049 p_kwargs : p_kwarg ',' p_kwrest
4051 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4055 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4059 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4063 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4065 | p_kwarg ',' p_kwnorest
4067 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4071 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4076 {$$=rb_ary_new_from_args(1, $1);}
4080 $$ = list_concat($1, $3);
4082 $$=rb_ary_push($1, $3);
4086 p_kw : p_kw_label p_expr
4088 error_duplicate_pattern_key(p, get_id($1), &@1);
4090 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4092 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4096 error_duplicate_pattern_key(p, get_id($1), &@1);
4097 if ($1 && !is_local_id(get_id($1))) {
4098 yyerror1(&@1, "key must be valid as local variables");
4100 error_duplicate_pattern_variable(p, get_id($1), &@1);
4102 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4104 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4109 | tSTRING_BEG string_contents tLABEL_END
4111 YYLTYPE loc = code_loc_gen(&@1, &@3);
4113 if (!$2 || nd_type($2) == NODE_STR) {
4114 NODE *node = dsym_node(p, $2, &loc);
4115 $$ = SYM2ID(node->nd_lit);
4118 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4119 VALUE label = RNODE($2)->nd_cval;
4120 VALUE rval = RNODE($2)->nd_rval;
4121 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4122 RNODE($$)->nd_loc = loc;
4126 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4132 p_kwrest : kwrest_mark tIDENTIFIER
4142 p_kwnorest : kwrest_mark keyword_nil
4148 p_value : p_primitive
4149 | p_primitive tDOT2 p_primitive
4154 $$ = NEW_DOT2($1, $3, &@$);
4156 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4158 | p_primitive tDOT3 p_primitive
4163 $$ = NEW_DOT3($1, $3, &@$);
4165 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4171 loc.beg_pos = @2.end_pos;
4172 loc.end_pos = @2.end_pos;
4175 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4177 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4183 loc.beg_pos = @2.end_pos;
4184 loc.end_pos = @2.end_pos;
4187 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4189 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4194 | tBDOT2 p_primitive
4198 loc.beg_pos = @1.beg_pos;
4199 loc.end_pos = @1.beg_pos;
4202 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4204 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4206 | tBDOT3 p_primitive
4210 loc.beg_pos = @1.beg_pos;
4211 loc.end_pos = @1.beg_pos;
4214 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4216 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4220 p_primitive : literal
4231 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4233 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4237 token_info_push(p, "->", &@1);
4243 nd_set_first_loc($$, @1.beg_pos);
4248 p_variable : tIDENTIFIER
4251 error_duplicate_pattern_variable(p, $1, &@1);
4252 $$ = assignable(p, $1, 0, &@$);
4254 $$=assignable(p, var_field(p, $1));
4258 p_var_ref : '^' tIDENTIFIER
4261 NODE *n = gettable(p, $2, &@$);
4262 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4263 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4267 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4271 p_const : tCOLON3 cname
4274 $$ = NEW_COLON3($2, &@$);
4276 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4278 | p_const tCOLON2 cname
4281 $$ = NEW_COLON2($1, $3, &@$);
4283 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4288 $$ = gettable(p, $1, &@$);
4290 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4294 opt_rescue : k_rescue exc_list exc_var then
4299 $$ = NEW_RESBODY($2,
4300 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4302 fixpos($$, $2?$2:$5);
4304 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(rescue,v1,v2,v3,v4);$$=v5;}
4309 exc_list : arg_value
4312 $$ = NEW_LIST($1, &@$);
4314 $$=rb_ary_new3(1, get_value($1));
4319 if (!($$ = splat_array($1))) $$ = $1;
4326 exc_var : tASSOC lhs
4333 opt_ensure : k_ensure compstmt
4338 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4352 node = NEW_STR(STR_NEW0(), &@$);
4353 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4356 node = evstr2dstr(p, node);
4369 $$ = literal_concat(p, $1, $2, &@$);
4371 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4375 string1 : tSTRING_BEG string_contents tSTRING_END
4378 $$ = heredoc_dedent(p, $2);
4379 if ($$) nd_set_loc($$, &@$);
4381 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4385 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4388 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4390 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4394 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4396 $$ = new_regexp(p, $2, $3, &@$);
4400 words : tWORDS_BEG ' ' word_list tSTRING_END
4403 $$ = make_list($3, &@$);
4405 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4409 word_list : /* none */
4414 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4416 | word_list word ' '
4419 $$ = list_append(p, $1, evstr2dstr(p, $2));
4421 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4425 word : string_content
4426 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4427 | word string_content
4430 $$ = literal_concat(p, $1, $2, &@$);
4432 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4436 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4439 $$ = make_list($3, &@$);
4441 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4445 symbol_list : /* none */
4450 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4452 | symbol_list word ' '
4455 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4457 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4461 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4464 $$ = make_list($3, &@$);
4466 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4470 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4473 $$ = make_list($3, &@$);
4475 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4479 qword_list : /* none */
4484 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4486 | qword_list tSTRING_CONTENT ' '
4489 $$ = list_append(p, $1, $2);
4491 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4495 qsym_list : /* none */
4500 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4502 | qsym_list tSTRING_CONTENT ' '
4505 $$ = symbol_append(p, $1, $2);
4507 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4511 string_contents : /* none */
4516 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4519 $$ = ripper_new_yylval(p, 0, $$, 0);
4522 | string_contents string_content
4525 $$ = literal_concat(p, $1, $2, &@$);
4527 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4530 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4531 !RNODE($1)->nd_cval) {
4532 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4533 RNODE($1)->nd_rval = add_mark_object(p, $$);
4540 xstring_contents: /* none */
4545 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4547 | xstring_contents string_content
4550 $$ = literal_concat(p, $1, $2, &@$);
4552 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4556 regexp_contents: /* none */
4561 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4564 $$ = ripper_new_yylval(p, 0, $$, 0);
4567 | regexp_contents string_content
4570 NODE *head = $1, *tail = $2;
4578 switch (nd_type(head)) {
4580 nd_set_type(head, NODE_DSTR);
4585 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4588 $$ = list_append(p, head, tail);
4591 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4592 if (ripper_is_node_yylval(n1)) {
4593 s1 = RNODE(n1)->nd_cval;
4594 n1 = RNODE(n1)->nd_rval;
4596 if (ripper_is_node_yylval(n2)) {
4597 s2 = RNODE(n2)->nd_cval;
4598 n2 = RNODE(n2)->nd_rval;
4600 $$ = dispatch2(regexp_add, n1, n2);
4602 $$ = ripper_new_yylval(p, 0, $$, s2);
4608 string_content : tSTRING_CONTENT
4609 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4612 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4613 $<strterm>$ = p->lex.strterm;
4615 SET_LEX_STATE(EXPR_BEG);
4619 p->lex.strterm = $<strterm>2;
4621 $$ = NEW_EVSTR($3, &@$);
4622 nd_set_line($$, @3.end_pos.lineno);
4624 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4632 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4633 $<strterm>$ = p->lex.strterm;
4637 $<num>$ = p->lex.state;
4638 SET_LEX_STATE(EXPR_BEG);
4641 $<num>$ = p->lex.brace_nest;
4642 p->lex.brace_nest = 0;
4645 $<num>$ = p->heredoc_indent;
4646 p->heredoc_indent = 0;
4648 compstmt tSTRING_DEND
4652 p->lex.strterm = $<strterm>3;
4653 SET_LEX_STATE($<num>4);
4654 p->lex.brace_nest = $<num>5;
4655 p->heredoc_indent = $<num>6;
4656 p->heredoc_line_indent = -1;
4658 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4659 $$ = new_evstr(p, $7, &@$);
4661 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4668 $$ = NEW_GVAR($1, &@$);
4670 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4675 $$ = NEW_IVAR($1, &@$);
4677 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4682 $$ = NEW_CVAR($1, &@$);
4684 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4695 SET_LEX_STATE(EXPR_END);
4697 $$ = NEW_LIT(ID2SYM($2), &@$);
4699 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4709 dsym : tSYMBEG string_contents tSTRING_END
4711 SET_LEX_STATE(EXPR_END);
4713 $$ = dsym_node(p, $2, &@$);
4715 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4719 numeric : simple_numeric
4720 | tUMINUS_NUM simple_numeric %prec tLOWEST
4724 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4726 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4730 simple_numeric : tINTEGER
4736 user_variable : tIDENTIFIER
4743 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4744 | keyword_self {$$ = KWD2EID(self, $1);}
4745 | keyword_true {$$ = KWD2EID(true, $1);}
4746 | keyword_false {$$ = KWD2EID(false, $1);}
4747 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4748 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4749 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4752 var_ref : user_variable
4755 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4757 if (id_is_var(p, get_id($1))) {
4758 $$ = dispatch1(var_ref, $1);
4761 $$ = dispatch1(vcall, $1);
4768 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4770 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4774 var_lhs : user_variable
4777 $$ = assignable(p, $1, 0, &@$);
4779 $$=assignable(p, var_field(p, $1));
4784 $$ = assignable(p, $1, 0, &@$);
4786 $$=assignable(p, var_field(p, $1));
4796 SET_LEX_STATE(EXPR_BEG);
4797 p->command_start = TRUE;
4812 f_arglist : '(' f_args rparen
4817 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4818 SET_LEX_STATE(EXPR_BEG);
4819 p->command_start = TRUE;
4821 | '(' args_forward rparen
4823 arg_var(p, idFWD_REST);
4825 arg_var(p, idFWD_KWREST);
4827 arg_var(p, idFWD_BLOCK);
4829 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4830 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4832 {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4833 SET_LEX_STATE(EXPR_BEG);
4834 p->command_start = TRUE;
4837 $<num>$ = p->in_kwarg;
4839 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4843 p->in_kwarg = !!$<num>1;
4845 SET_LEX_STATE(EXPR_BEG);
4846 p->command_start = TRUE;
4850 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4852 $$ = new_args_tail(p, $1, $3, $4, &@3);
4854 | f_kwarg opt_f_block_arg
4856 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4858 | f_kwrest opt_f_block_arg
4860 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4862 | f_no_kwarg opt_f_block_arg
4864 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4868 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4872 opt_args_tail : ',' args_tail
4878 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4882 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4884 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4886 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4888 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4890 | f_arg ',' f_optarg opt_args_tail
4892 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4894 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4896 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4898 | f_arg ',' f_rest_arg opt_args_tail
4900 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4902 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4904 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4906 | f_arg opt_args_tail
4908 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4910 | f_optarg ',' f_rest_arg opt_args_tail
4912 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4914 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4916 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4918 | f_optarg opt_args_tail
4920 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4922 | f_optarg ',' f_arg opt_args_tail
4924 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4926 | f_rest_arg opt_args_tail
4928 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4930 | f_rest_arg ',' f_arg opt_args_tail
4932 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4936 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4940 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4941 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4945 args_forward : tBDOT3
4950 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
4954 f_bad_arg : tCONSTANT
4957 yyerror1(&@1, "formal argument cannot be a constant");
4960 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4965 yyerror1(&@1, "formal argument cannot be an instance variable");
4968 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4973 yyerror1(&@1, "formal argument cannot be a global variable");
4976 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4981 yyerror1(&@1, "formal argument cannot be a class variable");
4984 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4988 f_norm_arg : f_bad_arg
4991 formal_argument(p, get_id($1));
4992 p->max_numparam = ORDINAL_PARAM;
4997 f_arg_asgn : f_norm_arg
5006 f_arg_item : f_arg_asgn
5010 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5014 | tLPAREN f_margs rparen
5017 ID tid = internal_id(p);
5019 loc.beg_pos = @2.beg_pos;
5020 loc.end_pos = @2.beg_pos;
5022 if (dyna_in_block(p)) {
5023 $2->nd_value = NEW_DVAR(tid, &loc);
5026 $2->nd_value = NEW_LVAR(tid, &loc);
5028 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5031 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5036 {$$=rb_ary_new3(1, get_value($1));}
5037 | f_arg ',' f_arg_item
5042 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5043 rb_discard_node(p, $3);
5045 $$=rb_ary_push($1, get_value($3));
5053 arg_var(p, formal_argument(p, id));
5055 p->max_numparam = ORDINAL_PARAM;
5060 f_kw : f_label arg_value
5064 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5066 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5072 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5074 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5078 f_block_kw : f_label primary_value
5081 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5083 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5088 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5090 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5094 f_block_kwarg : f_block_kw
5099 $$=rb_ary_new3(1, get_value($1));
5101 | f_block_kwarg ',' f_block_kw
5104 $$ = kwd_append($1, $3);
5106 $$=rb_ary_push($1, get_value($3));
5116 $$=rb_ary_new3(1, get_value($1));
5121 $$ = kwd_append($1, $3);
5123 $$=rb_ary_push($1, get_value($3));
5131 f_no_kwarg : kwrest_mark keyword_nil
5135 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5139 f_kwrest : kwrest_mark tIDENTIFIER
5141 arg_var(p, shadowing_lvar(p, get_id($2)));
5145 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5150 $$ = internal_id(p);
5153 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5157 f_opt : f_arg_asgn '=' arg_value
5161 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5163 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5167 f_block_opt : f_arg_asgn '=' primary_value
5171 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5173 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5177 f_block_optarg : f_block_opt
5182 $$=rb_ary_new3(1, get_value($1));
5184 | f_block_optarg ',' f_block_opt
5187 $$ = opt_arg_append($1, $3);
5189 $$=rb_ary_push($1, get_value($3));
5198 $$=rb_ary_new3(1, get_value($1));
5200 | f_optarg ',' f_opt
5203 $$ = opt_arg_append($1, $3);
5205 $$=rb_ary_push($1, get_value($3));
5213 f_rest_arg : restarg_mark tIDENTIFIER
5215 arg_var(p, shadowing_lvar(p, get_id($2)));
5219 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5224 $$ = internal_id(p);
5227 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5235 f_block_arg : blkarg_mark tIDENTIFIER
5237 arg_var(p, shadowing_lvar(p, get_id($2)));
5241 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5245 opt_f_block_arg : ',' f_block_arg
5260 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5263 switch (nd_type($3)) {
5272 yyerror1(&@3, "can't define singleton method for literals");
5280 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5290 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5295 {$$=rb_ary_new3(1, get_value($1));}
5305 if (assocs->nd_head &&
5306 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5307 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5309 tail = tail->nd_next->nd_head->nd_head;
5311 assocs = list_concat(assocs, tail);
5315 $$=rb_ary_push($1, get_value($3));
5319 assoc : arg_value tASSOC arg_value
5322 if (nd_type($1) == NODE_STR) {
5323 nd_set_type($1, NODE_LIT);
5324 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5326 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5328 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5333 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5335 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5337 | tSTRING_BEG string_contents tLABEL_END arg_value
5340 YYLTYPE loc = code_loc_gen(&@1, &@3);
5341 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5343 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5348 if (nd_type($2) == NODE_HASH &&
5349 !($2->nd_head && $2->nd_head->nd_alen)) {
5350 static VALUE empty_hash;
5352 empty_hash = rb_obj_freeze(rb_hash_new());
5353 rb_gc_register_mark_object(empty_hash);
5355 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5358 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5360 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5364 operation : tIDENTIFIER
5369 operation2 : tIDENTIFIER
5375 operation3 : tIDENTIFIER
5392 opt_terms : /* none */
5403 rbracket : opt_nl ']'
5409 trailer : /* none */
5414 term : ';' {yyerrok;token_flush(p);}
5415 | '\n' {token_flush(p);}
5419 | terms ';' {yyerrok;}
5431 # define yylval (*p->lval)
5433 static int regx_options(struct parser_params*);
5434 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5435 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5436 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5437 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5440 # define set_yylval_node(x) { \
5442 rb_parser_set_location(p, &_cur_loc); \
5443 yylval.node = (x); \
5445 # define set_yylval_str(x) \
5447 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5448 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5450 # define set_yylval_literal(x) \
5452 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5453 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5455 # define set_yylval_num(x) (yylval.num = (x))
5456 # define set_yylval_id(x) (yylval.id = (x))
5457 # define set_yylval_name(x) (yylval.id = (x))
5458 # define yylval_id() (yylval.id)
5461 ripper_yylval_id(struct parser_params *p, ID x)
5463 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5465 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5466 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5467 # define set_yylval_id(x) (void)(x)
5468 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5469 # define set_yylval_literal(x) add_mark_object(p, (x))
5470 # define set_yylval_node(x) (void)(x)
5471 # define yylval_id() yylval.id
5472 # define _cur_loc NULL_LOC /* dummy */
5475 #define set_yylval_noname() set_yylval_id(keyword_nil)
5478 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5479 #define dispatch_scan_event(p, t) ((void)0)
5480 #define dispatch_delayed_token(p, t) ((void)0)
5481 #define has_delayed_token(p) (0)
5483 #define literal_flush(p, ptr) ((void)(ptr))
5485 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5488 intern_sym(const char *name)
5490 ID id = rb_intern_const(name);
5495 ripper_has_scan_event(struct parser_params *p)
5497 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5498 return p->lex.pcur > p->lex.ptok;
5502 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5504 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5505 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5511 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5513 if (!ripper_has_scan_event(p)) return;
5514 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5516 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5519 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5521 int saved_line = p->ruby_sourceline;
5522 const char *saved_tokp = p->lex.ptok;
5524 if (NIL_P(p->delayed.token)) return;
5525 p->ruby_sourceline = p->delayed.line;
5526 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5527 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5528 p->delayed.token = Qnil;
5529 p->ruby_sourceline = saved_line;
5530 p->lex.ptok = saved_tokp;
5532 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5533 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5536 #include "ruby/regex.h"
5537 #include "ruby/util.h"
5540 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5542 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5546 parser_is_identchar(struct parser_params *p)
5548 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5552 parser_isascii(struct parser_params *p)
5554 return ISASCII(*(p->lex.pcur-1));
5558 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5560 int column = 1, nonspc = 0, i;
5561 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5563 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5566 if (*ptr != ' ' && *ptr != '\t') {
5571 ptinfo->beg = loc->beg_pos;
5572 ptinfo->indent = column;
5573 ptinfo->nonspc = nonspc;
5577 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5581 if (!p->token_info_enabled) return;
5582 ptinfo = ALLOC(token_info);
5583 ptinfo->token = token;
5584 ptinfo->next = p->token_info;
5585 token_info_setup(ptinfo, p->lex.pbeg, loc);
5587 p->token_info = ptinfo;
5591 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5593 token_info *ptinfo_beg = p->token_info;
5595 if (!ptinfo_beg) return;
5596 p->token_info = ptinfo_beg->next;
5598 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5599 token_info_warn(p, token, ptinfo_beg, 1, loc);
5600 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5604 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5606 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5607 if (!p->token_info_enabled) return;
5608 if (!ptinfo_beg) return;
5609 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5610 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5611 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5612 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5613 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5614 rb_warn3L(ptinfo_end->beg.lineno,
5615 "mismatched indentations at '%s' with '%s' at %d",
5616 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5620 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5622 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5623 if (!MBCLEN_CHARFOUND_P(len)) {
5624 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5631 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5634 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5637 int lineno = p->ruby_sourceline;
5641 else if (yylloc->beg_pos.lineno == lineno) {
5642 str = p->lex.lastline;
5647 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5651 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5656 yylloc = RUBY_SET_YYLLOC(current);
5658 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5659 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5660 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5661 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5664 compile_error(p, "%s", msg);
5665 parser_show_error_line(p, yylloc);
5670 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5673 const int max_line_margin = 30;
5674 const char *ptr, *ptr_end, *pt, *pb;
5675 const char *pre = "", *post = "", *pend;
5676 const char *code = "", *caret = "";
5678 const char *const pbeg = RSTRING_PTR(str);
5683 if (!yylloc) return;
5684 pend = RSTRING_END(str);
5685 if (pend > pbeg && pend[-1] == '\n') {
5686 if (--pend > pbeg && pend[-1] == '\r') --pend;
5690 if (lineno == yylloc->end_pos.lineno &&
5691 (pend - pbeg) > yylloc->end_pos.column) {
5692 pt = pbeg + yylloc->end_pos.column;
5696 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5697 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5699 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5700 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5702 len = ptr_end - ptr;
5705 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5706 if (ptr > pbeg) pre = "...";
5708 if (ptr_end < pend) {
5709 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5710 if (ptr_end < pend) post = "...";
5714 if (lineno == yylloc->beg_pos.lineno) {
5715 pb += yylloc->beg_pos.column;
5716 if (pb > pt) pb = pt;
5718 if (pb < ptr) pb = ptr;
5719 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5722 if (RTEST(errbuf)) {
5723 mesg = rb_attr_get(errbuf, idMesg);
5724 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5725 rb_str_cat_cstr(mesg, "\n");
5728 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5730 if (!errbuf && rb_stderr_tty_p()) {
5731 #define CSI_BEGIN "\033["
5734 CSI_BEGIN""CSI_SGR"%s" /* pre */
5735 CSI_BEGIN"1"CSI_SGR"%.*s"
5736 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5737 CSI_BEGIN";1"CSI_SGR"%.*s"
5738 CSI_BEGIN""CSI_SGR"%s" /* post */
5741 (int)(pb - ptr), ptr,
5743 (int)(ptr_end - pt), pt,
5749 len = ptr_end - ptr;
5750 lim = pt < pend ? pt : pend;
5751 i = (int)(lim - ptr);
5752 buf = ALLOCA_N(char, i+2);
5757 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5763 memset(p2, '~', (lim - ptr));
5767 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5768 pre, (int)len, code, post,
5771 if (!errbuf) rb_write_error_str(mesg);
5775 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5777 const char *pcur = 0, *ptok = 0;
5779 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5780 p->ruby_sourceline == yylloc->end_pos.lineno) {
5783 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5784 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5786 dispatch1(parse_error, STR_NEW2(msg));
5796 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5799 #endif /* !RIPPER */
5803 vtable_size(const struct vtable *tbl)
5805 if (!DVARS_TERMINAL_P(tbl)) {
5814 static struct vtable *
5815 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5817 struct vtable *tbl = ALLOC(struct vtable);
5820 tbl->tbl = ALLOC_N(ID, tbl->capa);
5824 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5829 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5832 vtable_free_gen(struct parser_params *p, int line, const char *name,
5837 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5840 if (!DVARS_TERMINAL_P(tbl)) {
5842 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5844 ruby_sized_xfree(tbl, sizeof(tbl));
5847 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5850 vtable_add_gen(struct parser_params *p, int line, const char *name,
5851 struct vtable *tbl, ID id)
5855 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5856 line, name, (void *)tbl, rb_id2name(id));
5859 if (DVARS_TERMINAL_P(tbl)) {
5860 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5863 if (tbl->pos == tbl->capa) {
5864 tbl->capa = tbl->capa * 2;
5865 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5867 tbl->tbl[tbl->pos++] = id;
5869 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5873 vtable_pop_gen(struct parser_params *p, int line, const char *name,
5874 struct vtable *tbl, int n)
5877 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5878 line, name, (void *)tbl, n);
5881 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5886 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5890 vtable_included(const struct vtable * tbl, ID id)
5894 if (!DVARS_TERMINAL_P(tbl)) {
5895 for (i = 0; i < tbl->pos; i++) {
5896 if (tbl->tbl[i] == id) {
5904 static void parser_prepare(struct parser_params *p);
5907 static NODE *parser_append_options(struct parser_params *p, NODE *node);
5910 debug_lines(VALUE fname)
5913 CONST_ID(script_lines, "SCRIPT_LINES__");
5914 if (rb_const_defined_at(rb_cObject, script_lines)) {
5915 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5916 if (RB_TYPE_P(hash, T_HASH)) {
5917 VALUE lines = rb_ary_new();
5918 rb_hash_aset(hash, fname, lines);
5926 e_option_supplied(struct parser_params *p)
5928 return strcmp(p->ruby_sourcefile, "-e") == 0;
5932 yycompile0(VALUE arg)
5936 struct parser_params *p = (struct parser_params *)arg;
5939 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5940 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5941 if (p->debug_lines && p->ruby_sourceline > 0) {
5942 VALUE str = STR_NEW0();
5943 n = p->ruby_sourceline;
5945 rb_ary_push(p->debug_lines, str);
5949 if (!e_option_supplied(p)) {
5955 #define RUBY_DTRACE_PARSE_HOOK(name) \
5956 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5957 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5959 RUBY_DTRACE_PARSE_HOOK(BEGIN);
5961 RUBY_DTRACE_PARSE_HOOK(END);
5965 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
5966 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
5967 if (n || p->error_p) {
5968 VALUE mesg = p->error_buffer;
5970 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5972 rb_set_errinfo(mesg);
5975 tree = p->eval_tree;
5977 tree = NEW_NIL(&NULL_LOC);
5980 VALUE opt = p->compile_option;
5982 NODE *body = parser_append_options(p, tree->nd_body);
5983 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5984 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5985 prelude = block_append(p, p->eval_tree_begin, body);
5986 tree->nd_body = prelude;
5987 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
5989 p->ast->body.root = tree;
5990 p->ast->body.line_count = p->line_count;
5995 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
5999 p->ruby_sourcefile_string = Qnil;
6000 p->ruby_sourcefile = "(none)";
6003 p->ruby_sourcefile_string = rb_fstring(fname);
6004 p->ruby_sourcefile = StringValueCStr(fname);
6006 p->ruby_sourceline = line - 1;
6008 p->ast = ast = rb_ast_new();
6009 rb_suppress_tracing(yycompile0, (VALUE)p);
6011 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6015 #endif /* !RIPPER */
6017 static rb_encoding *
6018 must_be_ascii_compatible(VALUE s)
6020 rb_encoding *enc = rb_enc_get(s);
6021 if (!rb_enc_asciicompat(enc)) {
6022 rb_raise(rb_eArgError, "invalid source encoding");
6028 lex_get_str(struct parser_params *p, VALUE s)
6030 char *beg, *end, *start;
6033 beg = RSTRING_PTR(s);
6034 len = RSTRING_LEN(s);
6036 if (p->lex.gets_.ptr) {
6037 if (len == p->lex.gets_.ptr) return Qnil;
6038 beg += p->lex.gets_.ptr;
6039 len -= p->lex.gets_.ptr;
6041 end = memchr(beg, '\n', len);
6042 if (end) len = ++end - beg;
6043 p->lex.gets_.ptr += len;
6044 return rb_str_subseq(s, beg - start, len);
6048 lex_getline(struct parser_params *p)
6050 VALUE line = (*p->lex.gets)(p, p->lex.input);
6051 if (NIL_P(line)) return line;
6052 must_be_ascii_compatible(line);
6054 if (p->debug_lines) {
6055 rb_enc_associate(line, p->enc);
6056 rb_ary_push(p->debug_lines, line);
6063 static const rb_data_type_t parser_data_type;
6067 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6069 struct parser_params *p;
6071 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6073 p->lex.gets = lex_get_str;
6074 p->lex.gets_.ptr = 0;
6075 p->lex.input = rb_str_new_frozen(s);
6076 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6078 return yycompile(vparser, p, fname, line);
6082 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6084 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6088 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6090 must_be_ascii_compatible(s);
6091 return parser_compile_string(vparser, f, s, line);
6094 VALUE rb_io_gets_internal(VALUE io);
6097 lex_io_gets(struct parser_params *p, VALUE io)
6099 return rb_io_gets_internal(io);
6103 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6105 struct parser_params *p;
6107 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6109 p->lex.gets = lex_io_gets;
6110 p->lex.input = file;
6111 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6113 return yycompile(vparser, p, fname, start);
6117 lex_generic_gets(struct parser_params *p, VALUE input)
6119 return (*p->lex.gets_.call)(input, p->line_count);
6123 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6125 struct parser_params *p;
6127 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6129 p->lex.gets = lex_generic_gets;
6130 p->lex.gets_.call = lex_gets;
6131 p->lex.input = input;
6132 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6134 return yycompile(vparser, p, fname, start);
6136 #endif /* !RIPPER */
6138 #define STR_FUNC_ESCAPE 0x01
6139 #define STR_FUNC_EXPAND 0x02
6140 #define STR_FUNC_REGEXP 0x04
6141 #define STR_FUNC_QWORDS 0x08
6142 #define STR_FUNC_SYMBOL 0x10
6143 #define STR_FUNC_INDENT 0x20
6144 #define STR_FUNC_LABEL 0x40
6145 #define STR_FUNC_LIST 0x4000
6146 #define STR_FUNC_TERM 0x8000
6149 str_label = STR_FUNC_LABEL,
6151 str_dquote = (STR_FUNC_EXPAND),
6152 str_xquote = (STR_FUNC_EXPAND),
6153 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6154 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6155 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6156 str_ssym = (STR_FUNC_SYMBOL),
6157 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6161 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6165 str = rb_enc_str_new(ptr, len, enc);
6166 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6167 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6169 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6170 rb_enc_associate(str, rb_ascii8bit_encoding());
6177 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6178 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6179 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6180 #define peek(p,c) peek_n(p, (c), 0)
6181 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6182 #define peekc(p) peekc_n(p, 0)
6183 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6187 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6190 if (!has_delayed_token(p)) {
6191 p->delayed.token = rb_str_buf_new(end - tok);
6192 rb_enc_associate(p->delayed.token, p->enc);
6193 p->delayed.line = p->ruby_sourceline;
6194 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6196 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6201 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6205 nextline(struct parser_params *p)
6207 VALUE v = p->lex.nextline;
6208 p->lex.nextline = 0;
6213 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6217 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6225 else if (NIL_P(v)) {
6226 /* after here-document without terminator */
6229 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6230 if (p->heredoc_end > 0) {
6231 p->ruby_sourceline = p->heredoc_end;
6234 p->ruby_sourceline++;
6235 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6236 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6238 p->lex.prevline = p->lex.lastline;
6239 p->lex.lastline = v;
6244 parser_cr(struct parser_params *p, int c)
6246 if (peek(p, '\n')) {
6250 else if (!p->cr_seen) {
6252 /* carried over with p->lex.nextline for nextc() */
6253 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6259 nextc(struct parser_params *p)
6263 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6264 if (nextline(p)) return -1;
6266 c = (unsigned char)*p->lex.pcur++;
6267 if (UNLIKELY(c == '\r')) {
6268 c = parser_cr(p, c);
6275 pushback(struct parser_params *p, int c)
6277 if (c == -1) return;
6279 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6284 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6286 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6287 #define tok(p) (p)->tokenbuf
6288 #define toklen(p) (p)->tokidx
6291 looking_at_eol_p(struct parser_params *p)
6293 const char *ptr = p->lex.pcur;
6294 while (ptr < p->lex.pend) {
6295 int c = (unsigned char)*ptr++;
6296 int eol = (c == '\n' || c == '#');
6297 if (eol || !ISSPACE(c)) {
6305 newtok(struct parser_params *p)
6308 p->tokline = p->ruby_sourceline;
6311 p->tokenbuf = ALLOC_N(char, 60);
6313 if (p->toksiz > 4096) {
6315 REALLOC_N(p->tokenbuf, char, 60);
6321 tokspace(struct parser_params *p, int n)
6325 if (p->tokidx >= p->toksiz) {
6326 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6327 REALLOC_N(p->tokenbuf, char, p->toksiz);
6329 return &p->tokenbuf[p->tokidx-n];
6333 tokadd(struct parser_params *p, int c)
6335 p->tokenbuf[p->tokidx++] = (char)c;
6336 if (p->tokidx >= p->toksiz) {
6338 REALLOC_N(p->tokenbuf, char, p->toksiz);
6343 tok_hex(struct parser_params *p, size_t *numlen)
6347 c = scan_hex(p->lex.pcur, 2, numlen);
6349 yyerror0("invalid hex escape");
6353 p->lex.pcur += *numlen;
6357 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6360 escaped_control_code(int c)
6386 #define WARN_SPACE_CHAR(c, prefix) \
6387 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6390 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6391 int regexp_literal, int wide)
6394 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6395 literal_flush(p, p->lex.pcur);
6396 p->lex.pcur += numlen;
6397 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6398 yyerror0("invalid Unicode escape");
6399 return wide && numlen > 0;
6401 if (codepoint > 0x10ffff) {
6402 yyerror0("invalid Unicode codepoint (too large)");
6405 if ((codepoint & 0xfffff800) == 0xd800) {
6406 yyerror0("invalid Unicode codepoint");
6409 if (regexp_literal) {
6410 tokcopy(p, (int)numlen);
6412 else if (codepoint >= 0x80) {
6413 rb_encoding *utf8 = rb_utf8_encoding();
6414 if (*encp && utf8 != *encp) {
6415 YYLTYPE loc = RUBY_INIT_YYLLOC();
6416 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6417 parser_show_error_line(p, &loc);
6421 tokaddmbc(p, codepoint, *encp);
6424 tokadd(p, codepoint);
6429 /* return value is for ?\u3042 */
6431 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6432 int term, int symbol_literal, int regexp_literal)
6435 * If `term` is not -1, then we allow multiple codepoints in \u{}
6436 * upto `term` byte, otherwise we're parsing a character literal.
6437 * And then add the codepoints to the current token.
6439 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6441 const int open_brace = '{', close_brace = '}';
6443 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6445 if (peek(p, open_brace)) { /* handle \u{...} form */
6446 const char *second = NULL;
6447 int c, last = nextc(p);
6448 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6449 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6450 while (c != close_brace) {
6451 if (c == term) goto unterminated;
6452 if (second == multiple_codepoints)
6453 second = p->lex.pcur;
6454 if (regexp_literal) tokadd(p, last);
6455 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6458 while (ISSPACE(c = *p->lex.pcur)) {
6459 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6462 if (term == -1 && !second)
6463 second = multiple_codepoints;
6466 if (c != close_brace) {
6469 yyerror0("unterminated Unicode escape");
6472 if (second && second != multiple_codepoints) {
6473 const char *pcur = p->lex.pcur;
6474 p->lex.pcur = second;
6475 dispatch_scan_event(p, tSTRING_CONTENT);
6478 yyerror0(multiple_codepoints);
6482 if (regexp_literal) tokadd(p, close_brace);
6485 else { /* handle \uxxxx form */
6486 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6493 #define ESCAPE_CONTROL 1
6494 #define ESCAPE_META 2
6497 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6502 switch (c = nextc(p)) {
6503 case '\\': /* Backslash */
6506 case 'n': /* newline */
6509 case 't': /* horizontal tab */
6512 case 'r': /* carriage-return */
6515 case 'f': /* form-feed */
6518 case 'v': /* vertical tab */
6521 case 'a': /* alarm(bell) */
6524 case 'e': /* escape */
6527 case '0': case '1': case '2': case '3': /* octal constant */
6528 case '4': case '5': case '6': case '7':
6530 c = scan_oct(p->lex.pcur, 3, &numlen);
6531 p->lex.pcur += numlen;
6534 case 'x': /* hex constant */
6535 c = tok_hex(p, &numlen);
6536 if (numlen == 0) return 0;
6539 case 'b': /* backspace */
6542 case 's': /* space */
6546 if (flags & ESCAPE_META) goto eof;
6547 if ((c = nextc(p)) != '-') {
6550 if ((c = nextc(p)) == '\\') {
6551 if (peek(p, 'u')) goto eof;
6552 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6554 else if (c == -1 || !ISASCII(c)) goto eof;
6556 int c2 = escaped_control_code(c);
6558 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6559 WARN_SPACE_CHAR(c2, "\\M-");
6562 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6565 else if (ISCNTRL(c)) goto eof;
6566 return ((c & 0xff) | 0x80);
6570 if ((c = nextc(p)) != '-') {
6574 if (flags & ESCAPE_CONTROL) goto eof;
6575 if ((c = nextc(p))== '\\') {
6576 if (peek(p, 'u')) goto eof;
6577 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6581 else if (c == -1 || !ISASCII(c)) goto eof;
6583 int c2 = escaped_control_code(c);
6586 if (flags & ESCAPE_META) {
6587 WARN_SPACE_CHAR(c2, "\\M-");
6590 WARN_SPACE_CHAR(c2, "");
6594 if (flags & ESCAPE_META) {
6595 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6598 WARN_SPACE_CHAR(c2, "\\C-");
6602 else if (ISCNTRL(c)) goto eof;
6608 yyerror0("Invalid escape character syntax");
6618 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6620 int len = rb_enc_codelen(c, enc);
6621 rb_enc_mbcput(c, tokspace(p, len), enc);
6625 tokadd_escape(struct parser_params *p, rb_encoding **encp)
6632 switch (c = nextc(p)) {
6634 return 0; /* just ignore */
6636 case '0': case '1': case '2': case '3': /* octal constant */
6637 case '4': case '5': case '6': case '7':
6639 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6640 if (numlen == 0) goto eof;
6641 p->lex.pcur += numlen;
6642 tokcopy(p, (int)numlen + 1);
6646 case 'x': /* hex constant */
6648 tok_hex(p, &numlen);
6649 if (numlen == 0) return -1;
6650 tokcopy(p, (int)numlen + 2);
6655 if (flags & ESCAPE_META) goto eof;
6656 if ((c = nextc(p)) != '-') {
6661 flags |= ESCAPE_META;
6665 if (flags & ESCAPE_CONTROL) goto eof;
6666 if ((c = nextc(p)) != '-') {
6674 if (flags & ESCAPE_CONTROL) goto eof;
6676 flags |= ESCAPE_CONTROL;
6678 if ((c = nextc(p)) == '\\') {
6681 else if (c == -1) goto eof;
6687 yyerror0("Invalid escape character syntax");
6699 regx_options(struct parser_params *p)
6707 while (c = nextc(p), ISALPHA(c)) {
6709 options |= RE_OPTION_ONCE;
6711 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6713 if (kc != rb_ascii8bit_encindex()) kcode = c;
6727 YYLTYPE loc = RUBY_INIT_YYLLOC();
6729 compile_error(p, "unknown regexp option%s - %*s",
6730 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6731 parser_show_error_line(p, &loc);
6733 return options | RE_OPTION_ENCODING(kcode);
6737 tokadd_mbchar(struct parser_params *p, int c)
6739 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6740 if (len < 0) return -1;
6742 p->lex.pcur += --len;
6743 if (len > 0) tokcopy(p, len);
6748 simple_re_meta(int c)
6751 case '$': case '*': case '+': case '.':
6752 case '?': case '^': case '|':
6753 case ')': case ']': case '}': case '>':
6761 parser_update_heredoc_indent(struct parser_params *p, int c)
6763 if (p->heredoc_line_indent == -1) {
6764 if (c == '\n') p->heredoc_line_indent = 0;
6768 p->heredoc_line_indent++;
6771 else if (c == '\t') {
6772 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6773 p->heredoc_line_indent = w * TAB_WIDTH;
6776 else if (c != '\n') {
6777 if (p->heredoc_indent > p->heredoc_line_indent) {
6778 p->heredoc_indent = p->heredoc_line_indent;
6780 p->heredoc_line_indent = -1;
6787 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6789 YYLTYPE loc = RUBY_INIT_YYLLOC();
6790 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6791 compile_error(p, "%s mixed within %s source", n1, n2);
6792 parser_show_error_line(p, &loc);
6796 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6798 const char *pos = p->lex.pcur;
6800 parser_mixed_error(p, enc1, enc2);
6805 tokadd_string(struct parser_params *p,
6806 int func, int term, int paren, long *nest,
6807 rb_encoding **encp, rb_encoding **enc)
6812 #define mixed_error(enc1, enc2) \
6813 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6814 #define mixed_escape(beg, enc1, enc2) \
6815 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6817 while ((c = nextc(p)) != -1) {
6818 if (p->heredoc_indent > 0) {
6819 parser_update_heredoc_indent(p, c);
6822 if (paren && c == paren) {
6825 else if (c == term) {
6826 if (!nest || !*nest) {
6832 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6833 int c2 = *p->lex.pcur;
6834 if (c2 == '$' || c2 == '@' || c2 == '{') {
6839 else if (c == '\\') {
6840 literal_flush(p, p->lex.pcur - 1);
6844 if (func & STR_FUNC_QWORDS) break;
6845 if (func & STR_FUNC_EXPAND) {
6846 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6857 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6861 if ((func & STR_FUNC_EXPAND) == 0) {
6865 tokadd_utf8(p, enc, term,
6866 func & STR_FUNC_SYMBOL,
6867 func & STR_FUNC_REGEXP);
6871 if (c == -1) return -1;
6873 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6876 if (func & STR_FUNC_REGEXP) {
6877 if (c == term && !simple_re_meta(c)) {
6882 if ((c = tokadd_escape(p, enc)) < 0)
6884 if (*enc && *enc != *encp) {
6885 mixed_escape(p->lex.ptok+2, *enc, *encp);
6889 else if (func & STR_FUNC_EXPAND) {
6891 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6892 c = read_escape(p, 0, enc);
6894 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6895 /* ignore backslashed spaces in %w */
6897 else if (c != term && !(paren && c == paren)) {
6904 else if (!parser_isascii(p)) {
6909 else if (*enc != *encp) {
6910 mixed_error(*enc, *encp);
6913 if (tokadd_mbchar(p, c) == -1) return -1;
6916 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6924 else if (*enc != *encp) {
6925 mixed_error(*enc, *encp);
6932 if (*enc) *encp = *enc;
6936 static inline rb_strterm_t *
6937 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6939 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6942 /* imemo_parser_strterm for literal */
6943 #define NEW_STRTERM(func, term, paren) \
6944 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6948 flush_string_content(struct parser_params *p, rb_encoding *enc)
6950 VALUE content = yylval.val;
6951 if (!ripper_is_node_yylval(content))
6952 content = ripper_new_yylval(p, 0, 0, content);
6953 if (has_delayed_token(p)) {
6954 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6956 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6958 dispatch_delayed_token(p, tSTRING_CONTENT);
6959 p->lex.ptok = p->lex.pcur;
6960 RNODE(content)->nd_rval = yylval.val;
6962 dispatch_scan_event(p, tSTRING_CONTENT);
6963 if (yylval.val != content)
6964 RNODE(content)->nd_rval = yylval.val;
6965 yylval.val = content;
6968 #define flush_string_content(p, enc) ((void)(enc))
6971 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6972 /* this can be shared with ripper, since it's independent from struct
6975 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6976 #define SPECIAL_PUNCT(idx) ( \
6977 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6978 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6979 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6980 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6981 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6983 const unsigned int ruby_global_name_punct_bits[] = {
6989 #undef SPECIAL_PUNCT
6992 static enum yytokentype
6993 parser_peek_variable_name(struct parser_params *p)
6996 const char *ptr = p->lex.pcur;
6998 if (ptr + 1 >= p->lex.pend) return 0;
7002 if ((c = *ptr) == '-') {
7003 if (++ptr >= p->lex.pend) return 0;
7006 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7007 return tSTRING_DVAR;
7011 if ((c = *ptr) == '@') {
7012 if (++ptr >= p->lex.pend) return 0;
7018 p->command_start = TRUE;
7019 return tSTRING_DBEG;
7023 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7024 return tSTRING_DVAR;
7028 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7029 #define IS_END() IS_lex_state(EXPR_END_ANY)
7030 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7031 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7032 #define IS_LABEL_POSSIBLE() (\
7033 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7035 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7036 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7038 static inline enum yytokentype
7039 parser_string_term(struct parser_params *p, int func)
7042 if (func & STR_FUNC_REGEXP) {
7043 set_yylval_num(regx_options(p));
7044 dispatch_scan_event(p, tREGEXP_END);
7045 SET_LEX_STATE(EXPR_END);
7048 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7050 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7053 SET_LEX_STATE(EXPR_END);
7057 static enum yytokentype
7058 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7060 int func = (int)quote->u1.func;
7061 int term = (int)quote->u3.term;
7062 int paren = (int)quote->u2.paren;
7064 rb_encoding *enc = p->enc;
7065 rb_encoding *base_enc = 0;
7068 if (func & STR_FUNC_TERM) {
7069 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7070 SET_LEX_STATE(EXPR_END);
7072 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7075 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7076 do {c = nextc(p);} while (ISSPACE(c));
7079 if (func & STR_FUNC_LIST) {
7080 quote->u1.func &= ~STR_FUNC_LIST;
7083 if (c == term && !quote->u0.nest) {
7084 if (func & STR_FUNC_QWORDS) {
7085 quote->u1.func |= STR_FUNC_TERM;
7086 pushback(p, c); /* dispatch the term at tSTRING_END */
7087 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7090 return parser_string_term(p, func);
7094 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7098 if ((func & STR_FUNC_EXPAND) && c == '#') {
7099 int t = parser_peek_variable_name(p);
7105 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7106 &enc, &base_enc) == -1) {
7109 # define unterminated_literal(mesg) yyerror0(mesg)
7111 # define unterminated_literal(mesg) compile_error(p, mesg)
7113 literal_flush(p, p->lex.pcur);
7114 if (func & STR_FUNC_QWORDS) {
7115 /* no content to add, bailing out here */
7116 unterminated_literal("unterminated list meets end of file");
7120 if (func & STR_FUNC_REGEXP) {
7121 unterminated_literal("unterminated regexp meets end of file");
7124 unterminated_literal("unterminated string meets end of file");
7126 quote->u1.func |= STR_FUNC_TERM;
7131 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7132 set_yylval_str(lit);
7133 flush_string_content(p, enc);
7135 return tSTRING_CONTENT;
7138 static enum yytokentype
7139 heredoc_identifier(struct parser_params *p)
7142 * term_len is length of `<<"END"` except `END`,
7143 * in this case term_len is 4 (<, <, " and ").
7145 long len, offset = p->lex.pcur - p->lex.pbeg;
7146 int c = nextc(p), term, func = 0, quote = 0;
7147 enum yytokentype token = tSTRING_BEG;
7152 func = STR_FUNC_INDENT;
7155 else if (c == '~') {
7157 func = STR_FUNC_INDENT;
7163 func |= str_squote; goto quoted;
7165 func |= str_dquote; goto quoted;
7167 token = tXSTRING_BEG;
7168 func |= str_xquote; goto quoted;
7175 while ((c = nextc(p)) != term) {
7176 if (c == -1 || c == '\r' || c == '\n') {
7177 yyerror(NULL, p, "unterminated here document identifier");
7184 if (!parser_is_identchar(p)) {
7186 if (func & STR_FUNC_INDENT) {
7187 pushback(p, indent > 0 ? '~' : '-');
7193 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7194 if (n < 0) return 0;
7196 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7201 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7202 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7203 yyerror(NULL, p, "too long here document identifier");
7204 dispatch_scan_event(p, tHEREDOC_BEG);
7207 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7208 p->lex.strterm->flags |= STRTERM_HEREDOC;
7209 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7210 here->offset = offset;
7211 here->sourceline = p->ruby_sourceline;
7212 here->length = (int)len;
7213 here->quote = quote;
7217 p->heredoc_indent = indent;
7218 p->heredoc_line_indent = 0;
7223 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7228 line = here->lastline;
7229 p->lex.lastline = line;
7230 p->lex.pbeg = RSTRING_PTR(line);
7231 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7232 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7233 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7234 p->heredoc_end = p->ruby_sourceline;
7235 p->ruby_sourceline = (int)here->sourceline;
7236 if (p->eofp) p->lex.nextline = Qnil;
7241 dedent_string(VALUE string, int width)
7247 RSTRING_GETMEM(string, str, len);
7248 for (i = 0; i < len && col < width; i++) {
7249 if (str[i] == ' ') {
7252 else if (str[i] == '\t') {
7253 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7254 if (n > width) break;
7262 rb_str_modify(string);
7263 str = RSTRING_PTR(string);
7264 if (RSTRING_LEN(string) != len)
7265 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7266 MEMMOVE(str, str + i, char, len - i);
7267 rb_str_set_len(string, len - i);
7273 heredoc_dedent(struct parser_params *p, NODE *root)
7275 NODE *node, *str_node, *prev_node;
7276 int indent = p->heredoc_indent;
7279 if (indent <= 0) return root;
7280 p->heredoc_indent = 0;
7281 if (!root) return root;
7283 prev_node = node = str_node = root;
7284 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7287 VALUE lit = str_node->nd_lit;
7288 if (str_node->flags & NODE_FL_NEWLINE) {
7289 dedent_string(lit, indent);
7294 else if (!literal_concat0(p, prev_lit, lit)) {
7298 NODE *end = node->nd_end;
7299 node = prev_node->nd_next = node->nd_next;
7301 if (nd_type(prev_node) == NODE_DSTR)
7302 nd_set_type(prev_node, NODE_STR);
7310 while ((node = (prev_node = node)->nd_next) != 0) {
7312 if (nd_type(node) != NODE_LIST) break;
7313 if ((str_node = node->nd_head) != 0) {
7314 enum node_type type = nd_type(str_node);
7315 if (type == NODE_STR || type == NODE_DSTR) break;
7325 heredoc_dedent(struct parser_params *p, VALUE array)
7327 int indent = p->heredoc_indent;
7329 if (indent <= 0) return array;
7330 p->heredoc_indent = 0;
7331 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7337 * Ripper.dedent_string(input, width) -> Integer
7339 * USE OF RIPPER LIBRARY ONLY.
7341 * Strips up to +width+ leading whitespaces from +input+,
7342 * and returns the stripped column width.
7345 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7350 wid = NUM2UINT(width);
7351 col = dedent_string(input, wid);
7352 return INT2NUM(col);
7357 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7359 const char *ptr = p->lex.pbeg;
7363 while (*ptr && ISSPACE(*ptr)) ptr++;
7365 n = p->lex.pend - (ptr + len);
7366 if (n < 0) return FALSE;
7367 if (n > 0 && ptr[len] != '\n') {
7368 if (ptr[len] != '\r') return FALSE;
7369 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7371 return strncmp(eos, ptr, len) == 0;
7375 word_match_p(struct parser_params *p, const char *word, long len)
7377 if (strncmp(p->lex.pcur, word, len)) return 0;
7378 if (p->lex.pcur + len == p->lex.pend) return 1;
7379 int c = (unsigned char)p->lex.pcur[len];
7380 if (ISSPACE(c)) return 1;
7382 case '\0': case '\004': case '\032': return 1;
7387 #define NUM_SUFFIX_R (1<<0)
7388 #define NUM_SUFFIX_I (1<<1)
7389 #define NUM_SUFFIX_ALL 3
7392 number_literal_suffix(struct parser_params *p, int mask)
7395 const char *lastp = p->lex.pcur;
7397 while ((c = nextc(p)) != -1) {
7398 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7399 result |= (mask & NUM_SUFFIX_I);
7400 mask &= ~NUM_SUFFIX_I;
7401 /* r after i, rational of complex is disallowed */
7402 mask &= ~NUM_SUFFIX_R;
7405 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7406 result |= (mask & NUM_SUFFIX_R);
7407 mask &= ~NUM_SUFFIX_R;
7410 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7411 p->lex.pcur = lastp;
7412 literal_flush(p, p->lex.pcur);
7421 static enum yytokentype
7422 set_number_literal(struct parser_params *p, VALUE v,
7423 enum yytokentype type, int suffix)
7425 if (suffix & NUM_SUFFIX_I) {
7426 v = rb_complex_raw(INT2FIX(0), v);
7429 set_yylval_literal(v);
7430 SET_LEX_STATE(EXPR_END);
7434 static enum yytokentype
7435 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7437 enum yytokentype type = tINTEGER;
7438 if (suffix & NUM_SUFFIX_R) {
7439 v = rb_rational_raw1(v);
7442 return set_number_literal(p, v, type, suffix);
7447 dispatch_heredoc_end(struct parser_params *p)
7450 if (has_delayed_token(p))
7451 dispatch_delayed_token(p, tSTRING_CONTENT);
7452 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7453 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7459 #define dispatch_heredoc_end(p) ((void)0)
7462 static enum yytokentype
7463 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7465 int c, func, indent = 0;
7466 const char *eos, *ptr, *ptr_end;
7469 rb_encoding *enc = p->enc;
7470 rb_encoding *base_enc = 0;
7473 eos = RSTRING_PTR(here->lastline) + here->offset;
7475 indent = (func = here->func) & STR_FUNC_INDENT;
7477 if ((c = nextc(p)) == -1) {
7480 if (!has_delayed_token(p)) {
7481 dispatch_scan_event(p, tSTRING_CONTENT);
7484 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7485 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7486 int cr = ENC_CODERANGE_UNKNOWN;
7487 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7488 if (cr != ENC_CODERANGE_7BIT &&
7489 p->enc == rb_usascii_encoding() &&
7490 enc != rb_utf8_encoding()) {
7491 enc = rb_ascii8bit_encoding();
7494 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7496 dispatch_delayed_token(p, tSTRING_CONTENT);
7500 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7501 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7505 SET_LEX_STATE(EXPR_END);
7510 /* not beginning of line, cannot be the terminator */
7512 else if (p->heredoc_line_indent == -1) {
7513 /* `heredoc_line_indent == -1` means
7514 * - "after an interpolation in the same line", or
7515 * - "in a continuing line"
7517 p->heredoc_line_indent = 0;
7519 else if (whole_match_p(p, eos, len, indent)) {
7520 dispatch_heredoc_end(p);
7522 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7525 SET_LEX_STATE(EXPR_END);
7529 if (!(func & STR_FUNC_EXPAND)) {
7531 ptr = RSTRING_PTR(p->lex.lastline);
7532 ptr_end = p->lex.pend;
7533 if (ptr_end > ptr) {
7534 switch (ptr_end[-1]) {
7536 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7545 if (p->heredoc_indent > 0) {
7547 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7549 p->heredoc_line_indent = 0;
7553 rb_str_cat(str, ptr, ptr_end - ptr);
7555 str = STR_NEW(ptr, ptr_end - ptr);
7556 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7558 if (p->heredoc_indent > 0) {
7561 if (nextc(p) == -1) {
7567 } while (!whole_match_p(p, eos, len, indent));
7570 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7573 int t = parser_peek_variable_name(p);
7574 if (p->heredoc_line_indent != -1) {
7575 if (p->heredoc_indent > p->heredoc_line_indent) {
7576 p->heredoc_indent = p->heredoc_line_indent;
7578 p->heredoc_line_indent = -1;
7587 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7588 if (p->eofp) goto error;
7592 if (c == '\\') p->heredoc_line_indent = -1;
7594 str = STR_NEW3(tok(p), toklen(p), enc, func);
7596 set_yylval_str(str);
7598 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7600 flush_string_content(p, enc);
7601 return tSTRING_CONTENT;
7603 tokadd(p, nextc(p));
7604 if (p->heredoc_indent > 0) {
7608 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7609 if ((c = nextc(p)) == -1) goto error;
7610 } while (!whole_match_p(p, eos, len, indent));
7611 str = STR_NEW3(tok(p), toklen(p), enc, func);
7613 dispatch_heredoc_end(p);
7615 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7618 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7620 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7621 set_yylval_str(str);
7623 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7625 return tSTRING_CONTENT;
7631 arg_ambiguous(struct parser_params *p, char c)
7634 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7636 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7642 formal_argument(struct parser_params *p, ID lhs)
7644 switch (id_type(lhs)) {
7649 yyerror0("formal argument cannot be a constant");
7652 yyerror0("formal argument cannot be an instance variable");
7655 yyerror0("formal argument cannot be a global variable");
7658 yyerror0("formal argument cannot be a class variable");
7661 yyerror0("formal argument must be local variable");
7665 lhs = dispatch1(param_error, lhs);
7670 shadowing_lvar(p, lhs);
7675 lvar_defined(struct parser_params *p, ID id)
7677 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7680 /* emacsen -*- hack */
7682 parser_encode_length(struct parser_params *p, const char *name, long len)
7686 if (len > 5 && name[nlen = len - 5] == '-') {
7687 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7690 if (len > 4 && name[nlen = len - 4] == '-') {
7691 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7693 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7694 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7695 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7702 parser_set_encode(struct parser_params *p, const char *name)
7704 int idx = rb_enc_find_index(name);
7709 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7711 excargs[0] = rb_eArgError;
7712 excargs[2] = rb_make_backtrace();
7713 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7714 rb_exc_raise(rb_make_exception(3, excargs));
7716 enc = rb_enc_from_index(idx);
7717 if (!rb_enc_asciicompat(enc)) {
7718 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7723 if (p->debug_lines) {
7724 VALUE lines = p->debug_lines;
7725 long i, n = RARRAY_LEN(lines);
7726 for (i = 0; i < n; ++i) {
7727 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7734 comment_at_top(struct parser_params *p)
7736 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7737 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7738 while (ptr < ptr_end) {
7739 if (!ISSPACE(*ptr)) return 0;
7745 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7746 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7749 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7751 if (!comment_at_top(p)) {
7754 parser_set_encode(p, val);
7758 parser_get_bool(struct parser_params *p, const char *name, const char *val)
7762 if (strcasecmp(val, "true") == 0) {
7767 if (strcasecmp(val, "false") == 0) {
7772 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7777 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7779 int b = parser_get_bool(p, name, val);
7780 if (b >= 0) p->token_info_enabled = b;
7784 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7788 if (p->token_seen) {
7789 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7793 b = parser_get_bool(p, name, val);
7796 if (!p->compile_option)
7797 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7798 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7799 (b ? Qtrue : Qfalse));
7802 # if WARN_PAST_SCOPE
7804 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7806 int b = parser_get_bool(p, name, val);
7807 if (b >= 0) p->past_scope_enabled = b;
7811 struct magic_comment {
7813 rb_magic_comment_setter_t func;
7814 rb_magic_comment_length_t length;
7817 static const struct magic_comment magic_comments[] = {
7818 {"coding", magic_comment_encoding, parser_encode_length},
7819 {"encoding", magic_comment_encoding, parser_encode_length},
7820 {"frozen_string_literal", parser_set_compile_option_flag},
7821 {"warn_indent", parser_set_token_info},
7822 # if WARN_PAST_SCOPE
7823 {"warn_past_scope", parser_set_past_scope},
7828 magic_comment_marker(const char *str, long len)
7835 if (str[i-1] == '*' && str[i-2] == '-') {
7841 if (i + 1 >= len) return 0;
7842 if (str[i+1] != '-') {
7845 else if (str[i-1] != '-') {
7861 parser_magic_comment(struct parser_params *p, const char *str, long len)
7864 VALUE name = 0, val = 0;
7865 const char *beg, *end, *vbeg, *vend;
7866 #define str_copy(_s, _p, _n) ((_s) \
7867 ? (void)(rb_str_resize((_s), (_n)), \
7868 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7869 : (void)((_s) = STR_NEW((_p), (_n))))
7871 if (len <= 7) return FALSE;
7872 if (!!(beg = magic_comment_marker(str, len))) {
7873 if (!(end = magic_comment_marker(beg, str + len - beg)))
7877 len = end - beg - 3;
7880 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7882 const struct magic_comment *mc = magic_comments;
7887 for (; len > 0 && *str; str++, --len) {
7889 case '\'': case '"': case ':': case ';':
7892 if (!ISSPACE(*str)) break;
7894 for (beg = str; len > 0; str++, --len) {
7896 case '\'': case '"': case ':': case ';':
7899 if (ISSPACE(*str)) break;
7904 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7907 if (!indicator) return FALSE;
7911 do str++; while (--len > 0 && ISSPACE(*str));
7914 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7927 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7931 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7934 while (len > 0 && (ISSPACE(*str))) --len, str++;
7935 if (len) return FALSE;
7939 str_copy(name, beg, n);
7940 s = RSTRING_PTR(name);
7941 for (i = 0; i < n; ++i) {
7942 if (s[i] == '-') s[i] = '_';
7945 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7948 n = (*mc->length)(p, vbeg, n);
7950 str_copy(val, vbeg, n);
7951 (*mc->func)(p, mc->name, RSTRING_PTR(val));
7954 } while (++mc < magic_comments + numberof(magic_comments));
7956 str_copy(val, vbeg, vend - vbeg);
7957 dispatch2(magic_comment, name, val);
7965 set_file_encoding(struct parser_params *p, const char *str, const char *send)
7968 const char *beg = str;
7972 if (send - str <= 6) return;
7974 case 'C': case 'c': str += 6; continue;
7975 case 'O': case 'o': str += 5; continue;
7976 case 'D': case 'd': str += 4; continue;
7977 case 'I': case 'i': str += 3; continue;
7978 case 'N': case 'n': str += 2; continue;
7979 case 'G': case 'g': str += 1; continue;
7986 if (ISSPACE(*str)) break;
7989 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7993 if (++str >= send) return;
7994 } while (ISSPACE(*str));
7996 if (*str != '=' && *str != ':') return;
8001 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8002 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8003 parser_set_encode(p, RSTRING_PTR(s));
8004 rb_str_resize(s, 0);
8008 parser_prepare(struct parser_params *p)
8011 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8014 if (peek(p, '!')) p->has_shebang = 1;
8016 case 0xef: /* UTF-8 BOM marker */
8017 if (p->lex.pend - p->lex.pcur >= 2 &&
8018 (unsigned char)p->lex.pcur[0] == 0xbb &&
8019 (unsigned char)p->lex.pcur[1] == 0xbf) {
8020 p->enc = rb_utf8_encoding();
8022 p->lex.pbeg = p->lex.pcur;
8030 p->enc = rb_enc_get(p->lex.lastline);
8034 #define ambiguous_operator(tok, op, syn) ( \
8035 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8036 rb_warning0("even though it seems like "syn""))
8038 #define ambiguous_operator(tok, op, syn) \
8039 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8041 #define warn_balanced(tok, op, syn) ((void) \
8042 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8043 space_seen && !ISSPACE(c) && \
8044 (ambiguous_operator(tok, op, syn), 0)), \
8045 (enum yytokentype)(tok))
8048 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8051 char *point = &str[seen_point];
8052 size_t fraclen = len-seen_point-1;
8053 memmove(point, point+1, fraclen+1);
8054 v = rb_cstr_to_inum(str, 10, FALSE);
8055 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8058 static enum yytokentype
8059 no_digits(struct parser_params *p)
8061 yyerror0("numeric literal without digits");
8062 if (peek(p, '_')) nextc(p);
8063 /* dummy 0, for tUMINUS_NUM at numeric */
8064 return set_integer_literal(p, INT2FIX(0), 0);
8067 static enum yytokentype
8068 parse_numeric(struct parser_params *p, int c)
8070 int is_float, seen_point, seen_e, nondigit;
8073 is_float = seen_point = seen_e = nondigit = 0;
8074 SET_LEX_STATE(EXPR_END);
8076 if (c == '-' || c == '+') {
8081 int start = toklen(p);
8083 if (c == 'x' || c == 'X') {
8086 if (c != -1 && ISXDIGIT(c)) {
8089 if (nondigit) break;
8093 if (!ISXDIGIT(c)) break;
8096 } while ((c = nextc(p)) != -1);
8100 if (toklen(p) == start) {
8101 return no_digits(p);
8103 else if (nondigit) goto trailing_uc;
8104 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8105 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8107 if (c == 'b' || c == 'B') {
8110 if (c == '0' || c == '1') {
8113 if (nondigit) break;
8117 if (c != '0' && c != '1') break;
8120 } while ((c = nextc(p)) != -1);
8124 if (toklen(p) == start) {
8125 return no_digits(p);
8127 else if (nondigit) goto trailing_uc;
8128 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8129 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8131 if (c == 'd' || c == 'D') {
8134 if (c != -1 && ISDIGIT(c)) {
8137 if (nondigit) break;
8141 if (!ISDIGIT(c)) break;
8144 } while ((c = nextc(p)) != -1);
8148 if (toklen(p) == start) {
8149 return no_digits(p);
8151 else if (nondigit) goto trailing_uc;
8152 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8153 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8159 if (c == 'o' || c == 'O') {
8160 /* prefixed octal */
8162 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8163 return no_digits(p);
8166 if (c >= '0' && c <= '7') {
8171 if (nondigit) break;
8175 if (c < '0' || c > '9') break;
8176 if (c > '7') goto invalid_octal;
8179 } while ((c = nextc(p)) != -1);
8180 if (toklen(p) > start) {
8183 if (nondigit) goto trailing_uc;
8184 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8185 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8192 if (c > '7' && c <= '9') {
8194 yyerror0("Invalid octal digit");
8196 else if (c == '.' || c == 'e' || c == 'E') {
8201 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8202 return set_integer_literal(p, INT2FIX(0), suffix);
8208 case '0': case '1': case '2': case '3': case '4':
8209 case '5': case '6': case '7': case '8': case '9':
8215 if (nondigit) goto trailing_uc;
8216 if (seen_point || seen_e) {
8221 if (c0 == -1 || !ISDIGIT(c0)) {
8227 seen_point = toklen(p);
8246 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8251 tokadd(p, nondigit);
8255 nondigit = (c == '-' || c == '+') ? c : 0;
8258 case '_': /* `_' in number just ignored */
8259 if (nondigit) goto decode_num;
8273 literal_flush(p, p->lex.pcur - 1);
8274 YYLTYPE loc = RUBY_INIT_YYLLOC();
8275 compile_error(p, "trailing `%c' in number", nondigit);
8276 parser_show_error_line(p, &loc);
8280 enum yytokentype type = tFLOAT;
8283 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8284 if (suffix & NUM_SUFFIX_R) {
8286 v = parse_rational(p, tok(p), toklen(p), seen_point);
8289 double d = strtod(tok(p), 0);
8290 if (errno == ERANGE) {
8291 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8296 return set_number_literal(p, v, type, suffix);
8298 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8299 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8302 static enum yytokentype
8303 parse_qmark(struct parser_params *p, int space_seen)
8310 SET_LEX_STATE(EXPR_VALUE);
8315 compile_error(p, "incomplete character syntax");
8318 if (rb_enc_isspace(c, p->enc)) {
8320 int c2 = escaped_control_code(c);
8322 WARN_SPACE_CHAR(c2, "?");
8327 SET_LEX_STATE(EXPR_VALUE);
8332 if (!parser_isascii(p)) {
8333 if (tokadd_mbchar(p, c) == -1) return 0;
8335 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8336 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8338 const char *start = p->lex.pcur - 1, *ptr = start;
8340 int n = parser_precise_mbclen(p, ptr);
8341 if (n < 0) return -1;
8343 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8344 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8345 " a conditional operator, put a space after `?'",
8346 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8350 else if (c == '\\') {
8353 enc = rb_utf8_encoding();
8354 tokadd_utf8(p, &enc, -1, 0, 0);
8356 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8358 if (tokadd_mbchar(p, c) == -1) return 0;
8361 c = read_escape(p, 0, &enc);
8369 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8370 set_yylval_str(lit);
8371 SET_LEX_STATE(EXPR_END);
8375 static enum yytokentype
8376 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8379 const char *ptok = p->lex.pcur;
8387 if (c == -1 || !ISALNUM(c)) {
8393 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8394 yyerror0("unknown type of %string");
8398 if (c == -1 || term == -1) {
8399 compile_error(p, "unterminated quoted string meets end of file");
8403 if (term == '(') term = ')';
8404 else if (term == '[') term = ']';
8405 else if (term == '{') term = '}';
8406 else if (term == '<') term = '>';
8409 p->lex.ptok = ptok-1;
8412 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8416 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8420 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8424 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8428 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8429 return tSYMBOLS_BEG;
8432 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8433 return tQSYMBOLS_BEG;
8436 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8437 return tXSTRING_BEG;
8440 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8444 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8445 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8449 yyerror0("unknown type of %string");
8453 if ((c = nextc(p)) == '=') {
8455 SET_LEX_STATE(EXPR_BEG);
8458 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8461 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8463 return warn_balanced('%', "%%", "string literal");
8467 tokadd_ident(struct parser_params *p, int c)
8470 if (tokadd_mbchar(p, c) == -1) return -1;
8472 } while (parser_is_identchar(p));
8478 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8480 ID ident = TOK_INTERN();
8482 set_yylval_name(ident);
8488 parse_numvar(struct parser_params *p)
8492 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8493 const unsigned long nth_ref_max =
8494 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8495 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8496 * turned into a Fixnum, in compile.c */
8498 if (overflow || n > nth_ref_max) {
8499 /* compile_error()? */
8500 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8501 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8508 static enum yytokentype
8509 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8511 const char *ptr = p->lex.pcur;
8514 SET_LEX_STATE(EXPR_END);
8515 p->lex.ptok = ptr - 1; /* from '$' */
8519 case '_': /* $_: last read line string */
8521 if (parser_is_identchar(p)) {
8529 case '~': /* $~: match-data */
8530 case '*': /* $*: argv */
8531 case '$': /* $$: pid */
8532 case '?': /* $?: last status */
8533 case '!': /* $!: error string */
8534 case '@': /* $@: error position */
8535 case '/': /* $/: input record separator */
8536 case '\\': /* $\: output record separator */
8537 case ';': /* $;: field separator */
8538 case ',': /* $,: output field separator */
8539 case '.': /* $.: last read line number */
8540 case '=': /* $=: ignorecase */
8541 case ':': /* $:: load path */
8542 case '<': /* $<: reading filename */
8543 case '>': /* $>: default output handle */
8544 case '\"': /* $": already loaded files */
8553 if (parser_is_identchar(p)) {
8554 if (tokadd_mbchar(p, c) == -1) return 0;
8562 set_yylval_name(TOK_INTERN());
8565 case '&': /* $&: last match */
8566 case '`': /* $`: string before last match */
8567 case '\'': /* $': string after last match */
8568 case '+': /* $+: string matches last paren. */
8569 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8574 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8577 case '1': case '2': case '3':
8578 case '4': case '5': case '6':
8579 case '7': case '8': case '9':
8584 } while (c != -1 && ISDIGIT(c));
8586 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8588 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8592 if (!parser_is_identchar(p)) {
8593 YYLTYPE loc = RUBY_INIT_YYLLOC();
8594 if (c == -1 || ISSPACE(c)) {
8595 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8599 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8601 parser_show_error_line(p, &loc);
8602 set_yylval_noname();
8610 if (tokadd_ident(p, c)) return 0;
8611 SET_LEX_STATE(EXPR_END);
8612 tokenize_ident(p, last_state);
8618 parser_numbered_param(struct parser_params *p, int n)
8620 if (n < 0) return false;
8622 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8625 if (p->max_numparam == ORDINAL_PARAM) {
8626 compile_error(p, "ordinary parameter is defined");
8629 struct vtable *args = p->lvtbl->args;
8630 if (p->max_numparam < n) {
8631 p->max_numparam = n;
8633 while (n > args->pos) {
8634 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8640 static enum yytokentype
8641 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8643 const char *ptr = p->lex.pcur;
8644 enum yytokentype result = tIVAR;
8645 register int c = nextc(p);
8648 p->lex.ptok = ptr - 1; /* from '@' */
8656 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8657 if (c == -1 || !parser_is_identchar(p)) {
8659 RUBY_SET_YYLLOC(loc);
8660 if (result == tIVAR) {
8661 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8664 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8666 parser_show_error_line(p, &loc);
8667 set_yylval_noname();
8668 SET_LEX_STATE(EXPR_END);
8671 else if (ISDIGIT(c)) {
8673 RUBY_SET_YYLLOC(loc);
8674 if (result == tIVAR) {
8675 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8678 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8680 parser_show_error_line(p, &loc);
8681 set_yylval_noname();
8682 SET_LEX_STATE(EXPR_END);
8686 if (tokadd_ident(p, c)) return 0;
8687 tokenize_ident(p, last_state);
8691 static enum yytokentype
8692 parse_ident(struct parser_params *p, int c, int cmd_state)
8694 enum yytokentype result;
8695 int mb = ENC_CODERANGE_7BIT;
8696 const enum lex_state_e last_state = p->lex.state;
8700 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8701 if (tokadd_mbchar(p, c) == -1) return 0;
8703 } while (parser_is_identchar(p));
8704 if ((c == '!' || c == '?') && !peek(p, '=')) {
8708 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8709 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8710 result = tIDENTIFIER;
8714 result = tCONSTANT; /* assume provisionally */
8719 if (IS_LABEL_POSSIBLE()) {
8720 if (IS_LABEL_SUFFIX(0)) {
8721 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8723 set_yylval_name(TOK_INTERN());
8727 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8728 const struct kwtable *kw;
8730 /* See if it is a reserved word. */
8731 kw = rb_reserved_word(tok(p), toklen(p));
8733 enum lex_state_e state = p->lex.state;
8734 if (IS_lex_state_for(state, EXPR_FNAME)) {
8735 SET_LEX_STATE(EXPR_ENDFN);
8736 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8739 SET_LEX_STATE(kw->state);
8740 if (IS_lex_state(EXPR_BEG)) {
8741 p->command_start = TRUE;
8743 if (kw->id[0] == keyword_do) {
8744 if (lambda_beginning_p()) {
8745 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8746 return keyword_do_LAMBDA;
8748 if (COND_P()) return keyword_do_cond;
8749 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8750 return keyword_do_block;
8753 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8756 if (kw->id[0] != kw->id[1])
8757 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8763 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8765 SET_LEX_STATE(EXPR_CMDARG);
8768 SET_LEX_STATE(EXPR_ARG);
8771 else if (p->lex.state == EXPR_FNAME) {
8772 SET_LEX_STATE(EXPR_ENDFN);
8775 SET_LEX_STATE(EXPR_END);
8778 ident = tokenize_ident(p, last_state);
8779 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8780 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8781 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8782 lvar_defined(p, ident)) {
8783 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8788 static enum yytokentype
8789 parser_yylex(struct parser_params *p)
8795 enum lex_state_e last_state;
8796 int fallthru = FALSE;
8797 int token_seen = p->token_seen;
8799 if (p->lex.strterm) {
8800 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8801 return here_document(p, &p->lex.strterm->u.heredoc);
8805 return parse_string(p, &p->lex.strterm->u.literal);
8808 cmd_state = p->command_start;
8809 p->command_start = FALSE;
8810 p->token_seen = TRUE;
8812 last_state = p->lex.state;
8816 switch (c = nextc(p)) {
8817 case '\0': /* NUL */
8818 case '\004': /* ^D */
8819 case '\032': /* ^Z */
8820 case -1: /* end of script. */
8824 case ' ': case '\t': case '\f': case '\r':
8825 case '\13': /* '\v' */
8828 while ((c = nextc(p))) {
8830 case ' ': case '\t': case '\f': case '\r':
8831 case '\13': /* '\v' */
8839 dispatch_scan_event(p, tSP);
8843 case '#': /* it's a comment */
8844 p->token_seen = token_seen;
8845 /* no magic_comment in shebang line */
8846 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8847 if (comment_at_top(p)) {
8848 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8852 dispatch_scan_event(p, tCOMMENT);
8856 p->token_seen = token_seen;
8857 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8858 !IS_lex_state(EXPR_LABELED));
8859 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8861 dispatch_scan_event(p, tIGNORED_NL);
8864 if (!c && p->in_kwarg) {
8865 goto normal_newline;
8870 switch (c = nextc(p)) {
8871 case ' ': case '\t': case '\f': case '\r':
8872 case '\13': /* '\v' */
8877 if (space_seen) dispatch_scan_event(p, tSP);
8881 dispatch_delayed_token(p, tIGNORED_NL);
8882 if (peek(p, '.') == (c == '&')) {
8884 dispatch_scan_event(p, tSP);
8889 p->ruby_sourceline--;
8890 p->lex.nextline = p->lex.lastline;
8891 case -1: /* EOF no decrement*/
8893 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8894 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8895 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8896 pushback(p, 1); /* always pushback */
8897 p->lex.ptok = p->lex.pcur;
8901 p->lex.ptok = p->lex.pcur;
8904 goto normal_newline;
8908 p->command_start = TRUE;
8909 SET_LEX_STATE(EXPR_BEG);
8913 if ((c = nextc(p)) == '*') {
8914 if ((c = nextc(p)) == '=') {
8915 set_yylval_id(idPow);
8916 SET_LEX_STATE(EXPR_BEG);
8921 rb_warning0("`**' interpreted as argument prefix");
8924 else if (IS_BEG()) {
8928 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8934 SET_LEX_STATE(EXPR_BEG);
8939 rb_warning0("`*' interpreted as argument prefix");
8942 else if (IS_BEG()) {
8946 c = warn_balanced('*', "*", "argument prefix");
8949 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8954 if (IS_AFTER_OPERATOR()) {
8955 SET_LEX_STATE(EXPR_ARG);
8961 SET_LEX_STATE(EXPR_BEG);
8974 /* skip embedded rd document */
8975 if (word_match_p(p, "begin", 5)) {
8979 dispatch_scan_event(p, tEMBDOC_BEG);
8983 dispatch_scan_event(p, tEMBDOC);
8988 compile_error(p, "embedded document meets end of file");
8991 if (c == '=' && word_match_p(p, "end", 3)) {
8997 dispatch_scan_event(p, tEMBDOC_END);
9002 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9003 if ((c = nextc(p)) == '=') {
9004 if ((c = nextc(p)) == '=') {
9013 else if (c == '>') {
9022 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9024 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9025 int token = heredoc_identifier(p);
9026 if (token) return token < 0 ? 0 : token;
9028 if (IS_AFTER_OPERATOR()) {
9029 SET_LEX_STATE(EXPR_ARG);
9032 if (IS_lex_state(EXPR_CLASS))
9033 p->command_start = TRUE;
9034 SET_LEX_STATE(EXPR_BEG);
9037 if ((c = nextc(p)) == '>') {
9044 if ((c = nextc(p)) == '=') {
9045 set_yylval_id(idLTLT);
9046 SET_LEX_STATE(EXPR_BEG);
9050 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9056 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9057 if ((c = nextc(p)) == '=') {
9061 if ((c = nextc(p)) == '=') {
9062 set_yylval_id(idGTGT);
9063 SET_LEX_STATE(EXPR_BEG);
9073 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9074 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9075 p->lex.ptok = p->lex.pcur-1;
9079 if (IS_lex_state(EXPR_FNAME)) {
9080 SET_LEX_STATE(EXPR_ENDFN);
9083 if (IS_lex_state(EXPR_DOT)) {
9085 SET_LEX_STATE(EXPR_CMDARG);
9087 SET_LEX_STATE(EXPR_ARG);
9090 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9091 return tXSTRING_BEG;
9094 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9095 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9096 p->lex.ptok = p->lex.pcur-1;
9100 return parse_qmark(p, space_seen);
9103 if ((c = nextc(p)) == '&') {
9104 SET_LEX_STATE(EXPR_BEG);
9105 if ((c = nextc(p)) == '=') {
9106 set_yylval_id(idANDOP);
9107 SET_LEX_STATE(EXPR_BEG);
9113 else if (c == '=') {
9115 SET_LEX_STATE(EXPR_BEG);
9118 else if (c == '.') {
9119 set_yylval_id(idANDDOT);
9120 SET_LEX_STATE(EXPR_DOT);
9126 (c = peekc_n(p, 1)) == -1 ||
9127 !(c == '\'' || c == '"' ||
9128 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9129 rb_warning0("`&' interpreted as argument prefix");
9133 else if (IS_BEG()) {
9137 c = warn_balanced('&', "&", "argument prefix");
9139 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9143 if ((c = nextc(p)) == '|') {
9144 SET_LEX_STATE(EXPR_BEG);
9145 if ((c = nextc(p)) == '=') {
9146 set_yylval_id(idOROP);
9147 SET_LEX_STATE(EXPR_BEG);
9151 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9160 SET_LEX_STATE(EXPR_BEG);
9163 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9169 if (IS_AFTER_OPERATOR()) {
9170 SET_LEX_STATE(EXPR_ARG);
9179 SET_LEX_STATE(EXPR_BEG);
9182 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9183 SET_LEX_STATE(EXPR_BEG);
9185 if (c != -1 && ISDIGIT(c)) {
9186 return parse_numeric(p, '+');
9190 SET_LEX_STATE(EXPR_BEG);
9192 return warn_balanced('+', "+", "unary operator");
9196 if (IS_AFTER_OPERATOR()) {
9197 SET_LEX_STATE(EXPR_ARG);
9206 SET_LEX_STATE(EXPR_BEG);
9210 SET_LEX_STATE(EXPR_ENDFN);
9213 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9214 SET_LEX_STATE(EXPR_BEG);
9216 if (c != -1 && ISDIGIT(c)) {
9221 SET_LEX_STATE(EXPR_BEG);
9223 return warn_balanced('-', "-", "unary operator");
9226 int is_beg = IS_BEG();
9227 SET_LEX_STATE(EXPR_BEG);
9228 if ((c = nextc(p)) == '.') {
9229 if ((c = nextc(p)) == '.') {
9230 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9231 rb_warn0("... at EOL, should be parenthesized?");
9233 return is_beg ? tBDOT3 : tDOT3;
9236 return is_beg ? tBDOT2 : tDOT2;
9239 if (c != -1 && ISDIGIT(c)) {
9240 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9241 parse_numeric(p, '.');
9242 if (ISDIGIT(prev)) {
9243 yyerror0("unexpected fraction part after numeric literal");
9246 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9248 SET_LEX_STATE(EXPR_END);
9249 p->lex.ptok = p->lex.pcur;
9253 SET_LEX_STATE(EXPR_DOT);
9257 case '0': case '1': case '2': case '3': case '4':
9258 case '5': case '6': case '7': case '8': case '9':
9259 return parse_numeric(p, c);
9264 SET_LEX_STATE(EXPR_ENDFN);
9265 p->lex.paren_nest--;
9271 SET_LEX_STATE(EXPR_END);
9272 p->lex.paren_nest--;
9276 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9277 if (!p->lex.brace_nest--) return tSTRING_DEND;
9280 SET_LEX_STATE(EXPR_END);
9281 p->lex.paren_nest--;
9287 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9288 SET_LEX_STATE(EXPR_BEG);
9291 set_yylval_id(idCOLON2);
9292 SET_LEX_STATE(EXPR_DOT);
9295 if (IS_END() || ISSPACE(c) || c == '#') {
9297 c = warn_balanced(':', ":", "symbol literal");
9298 SET_LEX_STATE(EXPR_BEG);
9303 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9306 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9312 SET_LEX_STATE(EXPR_FNAME);
9317 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9320 if ((c = nextc(p)) == '=') {
9322 SET_LEX_STATE(EXPR_BEG);
9327 arg_ambiguous(p, '/');
9328 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9331 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9332 return warn_balanced('/', "/", "regexp literal");
9335 if ((c = nextc(p)) == '=') {
9337 SET_LEX_STATE(EXPR_BEG);
9340 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9345 SET_LEX_STATE(EXPR_BEG);
9346 p->command_start = TRUE;
9350 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9354 if (IS_AFTER_OPERATOR()) {
9355 if ((c = nextc(p)) != '@') {
9358 SET_LEX_STATE(EXPR_ARG);
9361 SET_LEX_STATE(EXPR_BEG);
9369 else if (!space_seen) {
9370 /* foo( ... ) => method call, no ambiguity */
9372 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9375 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9376 rb_warning0("parentheses after method name is interpreted as "
9377 "an argument list, not a decomposed argument");
9379 p->lex.paren_nest++;
9382 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9386 p->lex.paren_nest++;
9387 if (IS_AFTER_OPERATOR()) {
9388 if ((c = nextc(p)) == ']') {
9389 SET_LEX_STATE(EXPR_ARG);
9390 if ((c = nextc(p)) == '=') {
9397 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9400 else if (IS_BEG()) {
9403 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9406 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9412 ++p->lex.brace_nest;
9413 if (lambda_beginning_p())
9415 else if (IS_lex_state(EXPR_LABELED))
9416 c = tLBRACE; /* hash */
9417 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9418 c = '{'; /* block (primary) */
9419 else if (IS_lex_state(EXPR_ENDARG))
9420 c = tLBRACE_ARG; /* block (expr) */
9422 c = tLBRACE; /* hash */
9424 p->command_start = TRUE;
9425 SET_LEX_STATE(EXPR_BEG);
9428 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9430 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9439 dispatch_scan_event(p, tSP);
9440 goto retry; /* skip \\n */
9442 if (c == ' ') return tSP;
9443 if (ISSPACE(c)) return c;
9448 return parse_percent(p, space_seen, last_state);
9451 return parse_gvar(p, last_state);
9454 return parse_atmark(p, last_state);
9457 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9458 p->ruby__end__seen = 1;
9464 dispatch_scan_event(p, k__END__);
9472 if (!parser_is_identchar(p)) {
9473 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9482 return parse_ident(p, c, cmd_state);
9485 static enum yytokentype
9486 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9492 t = parser_yylex(p);
9494 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9495 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9497 RUBY_SET_YYLLOC(*yylloc);
9499 if (has_delayed_token(p))
9500 dispatch_delayed_token(p, t);
9502 dispatch_scan_event(p, t);
9507 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9510 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9512 NODE *n = rb_ast_newnode(p->ast, type);
9514 rb_node_init(n, type, a0, a1, a2);
9517 nd_set_node_id(n, parser_get_node_id(p));
9522 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9525 nd_set_line(nd, loc->beg_pos.lineno);
9530 static enum node_type
9531 nodetype(NODE *node) /* for debug */
9533 return (enum node_type)nd_type(node);
9537 nodeline(NODE *node)
9539 return nd_line(node);
9543 newline_node(NODE *node)
9546 node = remove_begin(node);
9547 node->flags |= NODE_FL_NEWLINE;
9553 fixpos(NODE *node, NODE *orig)
9557 nd_set_line(node, nd_line(orig));
9561 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9563 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9567 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9569 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9573 block_append(struct parser_params *p, NODE *head, NODE *tail)
9575 NODE *end, *h = head, *nd;
9577 if (tail == 0) return head;
9579 if (h == 0) return tail;
9580 switch (nd_type(h)) {
9587 parser_warning(p, h, "unused literal ignored");
9590 h = end = NEW_BLOCK(head, &head->nd_loc);
9600 switch (nd_type(nd)) {
9606 if (RTEST(ruby_verbose)) {
9607 parser_warning(p, tail, "statement not reached");
9615 if (nd_type(tail) != NODE_BLOCK) {
9616 tail = NEW_BLOCK(tail, &tail->nd_loc);
9617 tail->nd_end = tail;
9619 end->nd_next = tail;
9620 h->nd_end = tail->nd_end;
9621 nd_set_last_loc(head, nd_last_loc(tail));
9625 /* append item to the list */
9627 list_append(struct parser_params *p, NODE *list, NODE *item)
9631 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9632 if (list->nd_next) {
9633 last = list->nd_next->nd_end;
9640 last->nd_next = NEW_LIST(item, &item->nd_loc);
9641 list->nd_next->nd_end = last->nd_next;
9643 nd_set_last_loc(list, nd_last_loc(item));
9648 /* concat two lists */
9650 list_concat(NODE *head, NODE *tail)
9654 if (head->nd_next) {
9655 last = head->nd_next->nd_end;
9661 head->nd_alen += tail->nd_alen;
9662 last->nd_next = tail;
9663 if (tail->nd_next) {
9664 head->nd_next->nd_end = tail->nd_next->nd_end;
9667 head->nd_next->nd_end = tail;
9670 nd_set_last_loc(head, nd_last_loc(tail));
9676 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9678 if (NIL_P(tail)) return 1;
9679 if (!rb_enc_compatible(head, tail)) {
9680 compile_error(p, "string literal encodings differ (%s / %s)",
9681 rb_enc_name(rb_enc_get(head)),
9682 rb_enc_name(rb_enc_get(tail)));
9683 rb_str_resize(head, 0);
9684 rb_str_resize(tail, 0);
9687 rb_str_buf_append(head, tail);
9691 /* concat two string literals */
9693 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9695 enum node_type htype;
9699 if (!head) return tail;
9700 if (!tail) return head;
9702 htype = nd_type(head);
9703 if (htype == NODE_EVSTR) {
9704 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9705 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9706 head = list_append(p, node, head);
9709 if (p->heredoc_indent > 0) {
9712 nd_set_type(head, NODE_DSTR);
9714 return list_append(p, head, tail);
9719 switch (nd_type(tail)) {
9721 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9722 nd_type(headlast) == NODE_STR) {
9724 lit = headlast->nd_lit;
9729 if (htype == NODE_STR) {
9730 if (!literal_concat0(p, lit, tail->nd_lit)) {
9732 rb_discard_node(p, head);
9733 rb_discard_node(p, tail);
9736 rb_discard_node(p, tail);
9739 list_append(p, head, tail);
9744 if (htype == NODE_STR) {
9745 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9747 tail->nd_lit = head->nd_lit;
9748 rb_discard_node(p, head);
9751 else if (NIL_P(tail->nd_lit)) {
9753 head->nd_alen += tail->nd_alen - 1;
9754 head->nd_next->nd_end->nd_next = tail->nd_next;
9755 head->nd_next->nd_end = tail->nd_next->nd_end;
9756 rb_discard_node(p, tail);
9758 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9759 nd_type(headlast) == NODE_STR) {
9760 lit = headlast->nd_lit;
9761 if (!literal_concat0(p, lit, tail->nd_lit))
9763 tail->nd_lit = Qnil;
9767 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9772 if (htype == NODE_STR) {
9773 nd_set_type(head, NODE_DSTR);
9776 list_append(p, head, tail);
9783 evstr2dstr(struct parser_params *p, NODE *node)
9785 if (nd_type(node) == NODE_EVSTR) {
9786 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9787 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9788 node = list_append(p, dstr, node);
9794 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9799 switch (nd_type(node)) {
9800 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9804 return NEW_EVSTR(head, loc);
9808 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9809 const YYLTYPE *op_loc, const YYLTYPE *loc)
9814 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9815 nd_set_line(expr, op_loc->beg_pos.lineno);
9820 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9824 opcall = NEW_OPCALL(recv, id, 0, loc);
9825 nd_set_line(opcall, op_loc->beg_pos.lineno);
9830 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9832 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9833 nd_set_line(qcall, op_loc->beg_pos.lineno);
9838 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9841 if (block) block_dup_check(p, args, block);
9842 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9843 if (block) ret = method_add_block(p, ret, block, loc);
9848 #define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9850 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9853 int line = op_loc->beg_pos.lineno;
9857 if (node1 && (n = nd_once_body(node1)) != 0) {
9858 switch (nd_type(n)) {
9861 NODE *match = NEW_MATCH2(node1, node2, loc);
9862 nd_set_line(match, line);
9867 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9868 const VALUE lit = n->nd_lit;
9869 NODE *match = NEW_MATCH2(node1, node2, loc);
9870 match->nd_args = reg_named_capture_assign(p, lit, loc);
9871 nd_set_line(match, line);
9877 if (node2 && (n = nd_once_body(node2)) != 0) {
9880 switch (nd_type(n)) {
9882 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9885 match3 = NEW_MATCH3(node2, node1, loc);
9890 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9891 nd_set_line(n, line);
9895 # if WARN_PAST_SCOPE
9897 past_dvar_p(struct parser_params *p, ID id)
9899 struct vtable *past = p->lvtbl->past;
9901 if (vtable_included(past, id)) return 1;
9908 /* As Ripper#warn does not have arguments for the location, so the
9909 * following messages cannot be separated */
9910 #define WARN_LOCATION(type) do { \
9911 if (p->warn_location) { \
9913 VALUE file = rb_source_location(&line); \
9914 rb_warn3(type" in eval may not return location in binding;" \
9915 " use Binding#source_location instead\n" \
9916 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9917 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9922 numparam_nested_p(struct parser_params *p)
9924 struct local_vars *local = p->lvtbl;
9925 NODE *outer = local->numparam.outer;
9926 NODE *inner = local->numparam.inner;
9927 if (outer || inner) {
9928 NODE *used = outer ? outer : inner;
9929 compile_error(p, "numbered parameter is already used in\n"
9930 "%s:%d: %s block here",
9931 p->ruby_sourcefile, nd_line(used),
9932 outer ? "outer" : "inner");
9933 parser_show_error_line(p, &used->nd_loc);
9940 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9946 return NEW_SELF(loc);
9948 return NEW_NIL(loc);
9950 return NEW_TRUE(loc);
9952 return NEW_FALSE(loc);
9953 case keyword__FILE__:
9954 WARN_LOCATION("__FILE__");
9956 VALUE file = p->ruby_sourcefile_string;
9958 file = rb_str_new(0, 0);
9960 file = rb_str_dup(file);
9961 node = NEW_STR(file, loc);
9962 RB_OBJ_WRITTEN(p->ast, Qnil, file);
9965 case keyword__LINE__:
9966 WARN_LOCATION("__LINE__");
9967 return NEW_LIT(INT2FIX(p->tokline), loc);
9968 case keyword__ENCODING__:
9969 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
9970 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9974 switch (id_type(id)) {
9976 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
9977 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
9978 if (id == p->cur_arg) {
9979 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9982 if (vidp) *vidp |= LVAR_USED;
9983 node = NEW_DVAR(id, loc);
9986 if (local_id_ref(p, id, &vidp)) {
9987 if (id == p->cur_arg) {
9988 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9991 if (vidp) *vidp |= LVAR_USED;
9992 node = NEW_LVAR(id, loc);
9995 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
9996 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
9997 if (numparam_nested_p(p)) return 0;
9998 node = NEW_DVAR(id, loc);
9999 struct local_vars *local = p->lvtbl;
10000 if (!local->numparam.current) local->numparam.current = node;
10003 # if WARN_PAST_SCOPE
10004 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10005 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10008 /* method call without arguments */
10009 return NEW_VCALL(id, loc);
10011 return NEW_GVAR(id, loc);
10013 return NEW_IVAR(id, loc);
10015 return NEW_CONST(id, loc);
10017 return NEW_CVAR(id, loc);
10019 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10024 opt_arg_append(NODE *opt_list, NODE *opt)
10026 NODE *opts = opt_list;
10027 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10029 while (opts->nd_next) {
10030 opts = opts->nd_next;
10031 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10033 opts->nd_next = opt;
10039 kwd_append(NODE *kwlist, NODE *kw)
10042 NODE *kws = kwlist;
10043 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10044 while (kws->nd_next) {
10045 kws = kws->nd_next;
10046 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10054 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10056 return NEW_DEFINED(remove_begin_all(expr), loc);
10060 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10062 if (nd_type(symbol) == NODE_DSTR) {
10063 nd_set_type(symbol, NODE_DSYM);
10066 nd_set_type(symbol, NODE_LIT);
10067 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10069 return list_append(p, symbols, symbol);
10073 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10079 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10080 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10083 switch (nd_type(node)) {
10086 VALUE src = node->nd_lit;
10087 nd_set_type(node, NODE_LIT);
10088 nd_set_loc(node, loc);
10089 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10094 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10095 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10098 nd_set_type(node, NODE_DREGX);
10099 nd_set_loc(node, loc);
10100 node->nd_cflag = options & RE_OPTION_MASK;
10101 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10102 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10103 if (nd_type(list->nd_head) == NODE_STR) {
10104 VALUE tail = list->nd_head->nd_lit;
10105 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10106 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10107 if (!literal_concat0(p, lit, tail)) {
10108 return NEW_NIL(loc); /* dummy node on error */
10110 rb_str_resize(tail, 0);
10111 prev->nd_next = list->nd_next;
10112 rb_discard_node(p, list->nd_head);
10113 rb_discard_node(p, list);
10124 if (!node->nd_next) {
10125 VALUE src = node->nd_lit;
10126 nd_set_type(node, NODE_LIT);
10127 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10129 if (options & RE_OPTION_ONCE) {
10130 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10138 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10141 return NEW_KW_ARG(0, (k), loc);
10145 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10148 VALUE lit = STR_NEW0();
10149 NODE *xstr = NEW_XSTR(lit, loc);
10150 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10153 switch (nd_type(node)) {
10155 nd_set_type(node, NODE_XSTR);
10156 nd_set_loc(node, loc);
10159 nd_set_type(node, NODE_DXSTR);
10160 nd_set_loc(node, loc);
10163 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10170 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10174 if (!arg || !p->case_labels) return;
10176 lit = rb_node_case_when_optimizable_literal(arg);
10177 if (lit == Qundef) return;
10178 if (nd_type(arg) == NODE_STR) {
10179 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10182 if (NIL_P(p->case_labels)) {
10183 p->case_labels = rb_obj_hide(rb_hash_new());
10186 VALUE line = rb_hash_lookup(p->case_labels, lit);
10187 if (!NIL_P(line)) {
10188 rb_warning1("duplicated `when' clause with line %d is ignored",
10193 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10196 #else /* !RIPPER */
10198 id_is_var(struct parser_params *p, ID id)
10200 if (is_notop_id(id)) {
10201 switch (id & ID_SCOPE_MASK) {
10202 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10205 if (dyna_in_block(p)) {
10206 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10208 if (local_id(p, id)) return 1;
10209 /* method call without arguments */
10213 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10218 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10220 VALUE src = 0, err;
10222 if (ripper_is_node_yylval(re)) {
10223 src = RNODE(re)->nd_cval;
10224 re = RNODE(re)->nd_rval;
10226 if (ripper_is_node_yylval(opt)) {
10227 options = (int)RNODE(opt)->nd_tag;
10228 opt = RNODE(opt)->nd_rval;
10230 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10231 compile_error(p, "%"PRIsVALUE, err);
10233 return dispatch2(regexp_literal, re, opt);
10235 #endif /* !RIPPER */
10239 static const char rb_parser_lex_state_names[][8] = {
10240 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10241 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10242 "LABEL", "LABELED","FITEM",
10246 append_lex_state_name(enum lex_state_e state, VALUE buf)
10249 unsigned int mask = 1;
10250 static const char none[] = "NONE";
10252 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10253 if ((unsigned)state & mask) {
10255 rb_str_cat(buf, "|", 1);
10258 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10262 rb_str_cat(buf, none, sizeof(none)-1);
10268 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10270 VALUE mesg = p->debug_buffer;
10272 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10273 p->debug_buffer = Qnil;
10274 rb_io_puts(1, &mesg, out);
10276 if (!NIL_P(str) && RSTRING_LEN(str)) {
10277 rb_io_write(p->debug_output, str);
10282 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10283 enum lex_state_e to, int line)
10286 mesg = rb_str_new_cstr("lex_state: ");
10287 append_lex_state_name(from, mesg);
10288 rb_str_cat_cstr(mesg, " -> ");
10289 append_lex_state_name(to, mesg);
10290 rb_str_catf(mesg, " at line %d\n", line);
10291 flush_debug_buffer(p, p->debug_output, mesg);
10296 rb_parser_lex_state_name(enum lex_state_e state)
10298 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10302 append_bitstack_value(stack_type stack, VALUE mesg)
10305 rb_str_cat_cstr(mesg, "0");
10308 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10309 for (; mask && !(stack & mask); mask >>= 1) continue;
10310 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10315 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10316 const char *name, int line)
10318 VALUE mesg = rb_sprintf("%s: ", name);
10319 append_bitstack_value(stack, mesg);
10320 rb_str_catf(mesg, " at line %d\n", line);
10321 flush_debug_buffer(p, p->debug_output, mesg);
10325 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10328 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10331 rb_str_vcatf(mesg, fmt, ap);
10333 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10336 mesg = rb_str_new(0, 0);
10337 append_lex_state_name(p->lex.state, mesg);
10338 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10339 rb_str_resize(mesg, 0);
10340 append_bitstack_value(p->cond_stack, mesg);
10341 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10342 rb_str_resize(mesg, 0);
10343 append_bitstack_value(p->cmdarg_stack, mesg);
10344 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10345 if (p->debug_output == rb_stdout)
10346 p->debug_output = rb_stderr;
10351 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10353 int sourceline = here->sourceline;
10354 int beg_pos = (int)here->offset - here->quote
10355 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10356 int end_pos = (int)here->offset + here->length + here->quote;
10358 yylloc->beg_pos.lineno = sourceline;
10359 yylloc->beg_pos.column = beg_pos;
10360 yylloc->end_pos.lineno = sourceline;
10361 yylloc->end_pos.column = end_pos;
10366 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10368 yylloc->beg_pos.lineno = p->ruby_sourceline;
10369 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10370 yylloc->end_pos.lineno = p->ruby_sourceline;
10371 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10376 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10378 yylloc->beg_pos.lineno = p->ruby_sourceline;
10379 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10380 yylloc->end_pos.lineno = p->ruby_sourceline;
10381 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10384 #endif /* !RIPPER */
10387 parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10392 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10393 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10395 v = rb_id2str(valp->id);
10397 v = valp->node->nd_rval;
10399 rb_parser_printf(p, "%"PRIsVALUE, v);
10401 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10402 case tSTRING_CONTENT: case tCHAR:
10404 v = valp->node->nd_lit;
10408 rb_parser_printf(p, "%+"PRIsVALUE, v);
10412 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10414 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10419 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10421 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10430 assignable0(struct parser_params *p, ID id, const char **err)
10432 if (!id) return -1;
10435 *err = "Can't change the value of self";
10438 *err = "Can't assign to nil";
10441 *err = "Can't assign to true";
10443 case keyword_false:
10444 *err = "Can't assign to false";
10446 case keyword__FILE__:
10447 *err = "Can't assign to __FILE__";
10449 case keyword__LINE__:
10450 *err = "Can't assign to __LINE__";
10452 case keyword__ENCODING__:
10453 *err = "Can't assign to __ENCODING__";
10456 switch (id_type(id)) {
10458 if (dyna_in_block(p)) {
10459 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10460 compile_error(p, "Can't assign to numbered parameter _%d",
10461 NUMPARAM_ID_TO_IDX(id));
10464 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10465 if (dvar_defined(p, id)) return NODE_DASGN;
10466 if (local_id(p, id)) return NODE_LASGN;
10468 return NODE_DASGN_CURR;
10471 if (!local_id(p, id)) local_var(p, id);
10475 case ID_GLOBAL: return NODE_GASGN;
10476 case ID_INSTANCE: return NODE_IASGN;
10478 if (!p->in_def) return NODE_CDECL;
10479 *err = "dynamic constant assignment";
10481 case ID_CLASS: return NODE_CVASGN;
10483 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10490 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10492 const char *err = 0;
10493 int node_type = assignable0(p, id, &err);
10494 switch (node_type) {
10495 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10496 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10497 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10498 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10499 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10500 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10501 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10503 if (err) yyerror1(loc, err);
10504 return NEW_BEGIN(0, loc);
10508 assignable(struct parser_params *p, VALUE lhs)
10510 const char *err = 0;
10511 assignable0(p, get_id(lhs), &err);
10512 if (err) lhs = assign_error(p, lhs);
10518 is_private_local_id(ID name)
10521 if (name == idUScore) return 1;
10522 if (!is_local_id(name)) return 0;
10523 s = rb_id2str(name);
10525 return RSTRING_PTR(s)[0] == '_';
10529 shadowing_lvar_0(struct parser_params *p, ID name)
10531 if (is_private_local_id(name)) return 1;
10532 if (dyna_in_block(p)) {
10533 if (dvar_curr(p, name)) {
10534 yyerror0("duplicated argument name");
10536 else if (dvar_defined(p, name) || local_id(p, name)) {
10537 vtable_add(p->lvtbl->vars, name);
10538 if (p->lvtbl->used) {
10539 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10545 if (local_id(p, name)) {
10546 yyerror0("duplicated argument name");
10553 shadowing_lvar(struct parser_params *p, ID name)
10555 shadowing_lvar_0(p, name);
10560 new_bv(struct parser_params *p, ID name)
10563 if (!is_local_id(name)) {
10564 compile_error(p, "invalid local variable - %"PRIsVALUE,
10568 if (!shadowing_lvar_0(p, name)) return;
10574 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10576 return NEW_ATTRASGN(recv, tASET, idx, loc);
10580 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10582 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10583 compile_error(p, "both block arg and actual block given");
10588 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10590 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10591 return NEW_ATTRASGN(recv, id, 0, loc);
10595 rb_backref_error(struct parser_params *p, NODE *node)
10597 switch (nd_type(node)) {
10599 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10601 case NODE_BACK_REF:
10602 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10608 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10610 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10611 switch (nd_type(node1)) {
10613 return list_append(p, node1, node2);
10614 case NODE_BLOCK_PASS:
10615 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10616 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10618 case NODE_ARGSPUSH:
10619 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10620 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10621 nd_set_type(node1, NODE_ARGSCAT);
10624 if (nd_type(node1->nd_body) != NODE_LIST) break;
10625 node1->nd_body = list_append(p, node1->nd_body, node2);
10626 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10629 return NEW_ARGSPUSH(node1, node2, loc);
10633 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10635 if (!node2) return node1;
10636 switch (nd_type(node1)) {
10637 case NODE_BLOCK_PASS:
10638 if (node1->nd_head)
10639 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10641 node1->nd_head = NEW_LIST(node2, loc);
10643 case NODE_ARGSPUSH:
10644 if (nd_type(node2) != NODE_LIST) break;
10645 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10646 nd_set_type(node1, NODE_ARGSCAT);
10649 if (nd_type(node2) != NODE_LIST ||
10650 nd_type(node1->nd_body) != NODE_LIST) break;
10651 node1->nd_body = list_concat(node1->nd_body, node2);
10654 return NEW_ARGSCAT(node1, node2, loc);
10658 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10661 if ((n1 = splat_array(args)) != 0) {
10662 return list_append(p, n1, last_arg);
10664 return arg_append(p, args, last_arg, loc);
10668 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10671 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10672 return list_concat(n1, rest_arg);
10674 return arg_concat(p, args, rest_arg, loc);
10678 splat_array(NODE* node)
10680 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10681 if (nd_type(node) == NODE_LIST) return node;
10686 mark_lvar_used(struct parser_params *p, NODE *rhs)
10690 switch (nd_type(rhs)) {
10692 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10693 if (vidp) *vidp |= LVAR_USED;
10697 case NODE_DASGN_CURR:
10698 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10699 if (vidp) *vidp |= LVAR_USED;
10704 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10705 mark_lvar_used(p, rhs->nd_head);
10713 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10715 if (!lhs) return 0;
10717 switch (nd_type(lhs)) {
10722 case NODE_DASGN_CURR:
10726 lhs->nd_value = rhs;
10727 nd_set_loc(lhs, loc);
10730 case NODE_ATTRASGN:
10731 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10732 nd_set_loc(lhs, loc);
10736 /* should not happen */
10744 value_expr_check(struct parser_params *p, NODE *node)
10746 NODE *void_node = 0, *vn;
10749 rb_warning0("empty expression");
10752 switch (nd_type(node)) {
10758 return void_node ? void_node : node;
10761 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10762 compile_error(p, "unexpected node");
10765 if (node->nd_body->nd_body) {
10768 /* single line pattern matching */
10769 return void_node ? void_node : node;
10772 while (node->nd_next) {
10773 node = node->nd_next;
10775 node = node->nd_head;
10779 node = node->nd_body;
10784 if (!node->nd_body) {
10787 else if (!node->nd_else) {
10790 vn = value_expr_check(p, node->nd_body);
10791 if (!vn) return NULL;
10792 if (!void_node) void_node = vn;
10793 node = node->nd_else;
10798 node = node->nd_1st;
10803 case NODE_DASGN_CURR:
10805 mark_lvar_used(p, node);
10817 value_expr_gen(struct parser_params *p, NODE *node)
10819 NODE *void_node = value_expr_check(p, node);
10821 yyerror1(&void_node->nd_loc, "void value expression");
10822 /* or "control never reach"? */
10828 void_expr(struct parser_params *p, NODE *node)
10830 const char *useless = 0;
10832 if (!RTEST(ruby_verbose)) return;
10834 if (!node || !(node = nd_once_body(node))) return;
10835 switch (nd_type(node)) {
10837 switch (node->nd_mid) {
10856 useless = rb_id2name(node->nd_mid);
10867 case NODE_BACK_REF:
10868 useless = "a variable";
10871 useless = "a constant";
10877 useless = "a literal";
10902 useless = "defined?";
10907 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10912 void_stmts(struct parser_params *p, NODE *node)
10914 NODE *const n = node;
10915 if (!RTEST(ruby_verbose)) return n;
10916 if (!node) return n;
10917 if (nd_type(node) != NODE_BLOCK) return n;
10919 while (node->nd_next) {
10920 void_expr(p, node->nd_head);
10921 node = node->nd_next;
10927 remove_begin(NODE *node)
10929 NODE **n = &node, *n1 = node;
10930 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10931 *n = n1 = n1->nd_body;
10937 remove_begin_all(NODE *node)
10939 NODE **n = &node, *n1 = node;
10940 while (n1 && nd_type(n1) == NODE_BEGIN) {
10941 *n = n1 = n1->nd_body;
10947 reduce_nodes(struct parser_params *p, NODE **body)
10949 NODE *node = *body;
10952 *body = NEW_NIL(&NULL_LOC);
10955 #define subnodes(n1, n2) \
10956 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10957 (!node->n2) ? (body = &node->n1, 1) : \
10958 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10961 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10962 switch (nd_type(node)) {
10968 *body = node = node->nd_stts;
10969 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10972 *body = node = node->nd_body;
10973 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10976 body = &node->nd_end->nd_head;
10980 if (subnodes(nd_body, nd_else)) break;
10983 body = &node->nd_body;
10986 if (!subnodes(nd_body, nd_next)) goto end;
10989 if (!subnodes(nd_head, nd_resq)) goto end;
10992 if (node->nd_else) {
10993 body = &node->nd_resq;
10996 if (!subnodes(nd_head, nd_resq)) goto end;
11002 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11009 is_static_content(NODE *node)
11011 if (!node) return 1;
11012 switch (nd_type(node)) {
11014 if (!(node = node->nd_head)) break;
11017 if (!is_static_content(node->nd_head)) return 0;
11018 } while ((node = node->nd_next) != 0);
11033 assign_in_cond(struct parser_params *p, NODE *node)
11035 switch (nd_type(node)) {
11039 case NODE_DASGN_CURR:
11048 if (!node->nd_value) return 1;
11049 if (is_static_content(node->nd_value)) {
11050 /* reports always */
11051 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11062 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11064 case COND_IN_OP: break; \
11065 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11066 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11069 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11072 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11074 enum node_type type;
11076 if (node == 0) return 0;
11078 type = nd_type(node);
11080 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11081 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11082 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11084 return cond0(p, node, COND_IN_FF, loc);
11088 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11090 if (node == 0) return 0;
11091 if (!(node = nd_once_body(node))) return 0;
11092 assign_in_cond(p, node);
11094 switch (nd_type(node)) {
11098 SWITCH_BY_COND_TYPE(type, warn, "string ")
11102 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11104 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11108 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11109 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11114 node->nd_beg = range_op(p, node->nd_beg, loc);
11115 node->nd_end = range_op(p, node->nd_end, loc);
11116 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11117 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11121 SWITCH_BY_COND_TYPE(type, warning, "string ")
11125 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11126 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11127 nd_set_type(node, NODE_MATCH);
11129 else if (node->nd_lit == Qtrue ||
11130 node->nd_lit == Qfalse) {
11131 /* booleans are OK, e.g., while true */
11134 SWITCH_BY_COND_TYPE(type, warning, "")
11143 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11145 if (node == 0) return 0;
11146 return cond0(p, node, COND_IN_COND, loc);
11150 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11152 if (node == 0) return 0;
11153 return cond0(p, node, COND_IN_OP, loc);
11157 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11159 if (!cc) return right;
11160 cc = cond0(p, cc, COND_IN_COND, loc);
11161 return newline_node(NEW_IF(cc, left, right, loc));
11165 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11167 if (!cc) return right;
11168 cc = cond0(p, cc, COND_IN_COND, loc);
11169 return newline_node(NEW_UNLESS(cc, left, right, loc));
11173 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11174 const YYLTYPE *op_loc, const YYLTYPE *loc)
11176 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11179 if (left && (enum node_type)nd_type(left) == type) {
11180 NODE *node = left, *second;
11181 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11184 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11185 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11186 left->nd_loc.end_pos = loc->end_pos;
11189 op = NEW_NODE(type, left, right, 0, loc);
11190 nd_set_line(op, op_loc->beg_pos.lineno);
11195 no_blockarg(struct parser_params *p, NODE *node)
11197 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11198 compile_error(p, "block argument should not be given");
11203 ret_args(struct parser_params *p, NODE *node)
11206 no_blockarg(p, node);
11207 if (nd_type(node) == NODE_LIST) {
11208 if (node->nd_next == 0) {
11209 node = node->nd_head;
11212 nd_set_type(node, NODE_VALUES);
11220 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11222 if (node) no_blockarg(p, node);
11224 return NEW_YIELD(node, loc);
11228 negate_lit(struct parser_params *p, VALUE lit)
11230 if (FIXNUM_P(lit)) {
11231 return LONG2FIX(-FIX2LONG(lit));
11233 if (SPECIAL_CONST_P(lit)) {
11235 if (FLONUM_P(lit)) {
11236 return DBL2NUM(-RFLOAT_VALUE(lit));
11241 switch (BUILTIN_TYPE(lit)) {
11243 BIGNUM_NEGATE(lit);
11244 lit = rb_big_norm(lit);
11247 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11250 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11251 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11254 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11258 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11259 rb_builtin_class_name(lit));
11266 arg_blk_pass(NODE *node1, NODE *node2)
11269 if (!node1) return node2;
11270 node2->nd_head = node1;
11271 nd_set_first_lineno(node2, nd_first_lineno(node1));
11272 nd_set_first_column(node2, nd_first_column(node1));
11279 args_info_empty_p(struct rb_args_info *args)
11281 if (args->pre_args_num) return false;
11282 if (args->post_args_num) return false;
11283 if (args->rest_arg) return false;
11284 if (args->opt_args) return false;
11285 if (args->block_arg) return false;
11286 if (args->kw_args) return false;
11287 if (args->kw_rest_arg) return false;
11292 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11294 int saved_line = p->ruby_sourceline;
11295 struct rb_args_info *args = tail->nd_ainfo;
11297 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11298 args->pre_init = pre_args ? pre_args->nd_next : 0;
11300 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11301 args->post_init = post_args ? post_args->nd_next : 0;
11302 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11304 args->rest_arg = rest_arg;
11306 args->opt_args = opt_args;
11308 args->ruby2_keywords = rest_arg == idFWD_REST;
11310 p->ruby_sourceline = saved_line;
11311 nd_set_loc(tail, loc);
11317 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11319 int saved_line = p->ruby_sourceline;
11321 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11322 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11323 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11324 args->imemo = tmpbuf;
11325 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11326 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11327 if (p->error_p) return node;
11329 args->block_arg = block;
11330 args->kw_args = kw_args;
11334 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11335 * variable order: k1, kr1, k2, &b, internal_id, krest
11337 * variable order: kr1, k1, k2, internal_id, krest, &b
11339 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11340 struct vtable *vtargs = p->lvtbl->args;
11341 NODE *kwn = kw_args;
11343 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11344 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11346 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11348 --required_kw_vars;
11349 kwn = kwn->nd_next;
11352 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11353 ID vid = kwn->nd_body->nd_vid;
11354 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11355 *required_kw_vars++ = vid;
11362 arg_var(p, kw_bits);
11363 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11364 if (block) arg_var(p, block);
11366 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11367 args->kw_rest_arg->nd_cflag = kw_bits;
11369 else if (kw_rest_arg == idNil) {
11370 args->no_kwarg = 1;
11372 else if (kw_rest_arg) {
11373 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11376 p->ruby_sourceline = saved_line;
11381 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11383 if (max_numparam > NO_PARAM) {
11385 YYLTYPE loc = RUBY_INIT_YYLLOC();
11386 args = new_args_tail(p, 0, 0, 0, 0);
11387 nd_set_loc(args, &loc);
11389 args->nd_ainfo->pre_args_num = max_numparam;
11395 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11397 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11399 aryptn->nd_pconst = constant;
11402 NODE *pre_args = NEW_LIST(pre_arg, loc);
11403 if (apinfo->pre_args) {
11404 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11407 apinfo->pre_args = pre_args;
11414 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11416 int saved_line = p->ruby_sourceline;
11418 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11419 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11420 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11421 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11422 apinfo->imemo = tmpbuf;
11423 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11425 apinfo->pre_args = pre_args;
11429 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11432 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11436 apinfo->rest_arg = NULL;
11439 apinfo->post_args = post_args;
11441 p->ruby_sourceline = saved_line;
11446 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11448 hshptn->nd_pconst = constant;
11453 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11455 int saved_line = p->ruby_sourceline;
11456 NODE *node, *kw_rest_arg_node;
11458 if (kw_rest_arg == idNil) {
11459 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11461 else if (kw_rest_arg) {
11462 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11465 kw_rest_arg_node = NULL;
11468 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11470 p->ruby_sourceline = saved_line;
11475 new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11477 NODE *node = NEW_CASE3(val, pat, loc);
11479 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11480 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11485 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11490 return NEW_LIT(ID2SYM(idNULL), loc);
11493 switch (nd_type(node)) {
11495 nd_set_type(node, NODE_DSYM);
11496 nd_set_loc(node, loc);
11499 lit = node->nd_lit;
11500 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11501 nd_set_type(node, NODE_LIT);
11502 nd_set_loc(node, loc);
11505 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11512 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11514 NODE *node = (NODE *)v;
11515 NODE **result = (NODE **)h;
11517 node->nd_next->nd_end = node->nd_next;
11518 node->nd_next->nd_next = 0;
11520 list_concat(*result, node);
11523 return ST_CONTINUE;
11527 remove_duplicate_keys(struct parser_params *p, NODE *hash)
11529 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11531 rb_code_location_t loc = hash->nd_loc;
11532 while (hash && hash->nd_head && hash->nd_next) {
11533 NODE *head = hash->nd_head;
11534 NODE *value = hash->nd_next;
11535 NODE *next = value->nd_next;
11536 VALUE key = (VALUE)head;
11538 if (nd_type(head) == NODE_LIT &&
11539 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11540 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11541 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11542 head->nd_lit, nd_line(head));
11543 head = ((NODE *)data)->nd_next;
11544 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11547 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11551 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11552 st_free_table(literal_keys);
11554 if (!result) result = hash;
11555 else list_concat(result, hash);
11557 result->nd_loc = loc;
11562 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11564 if (hash) hash = remove_duplicate_keys(p, hash);
11565 return NEW_HASH(hash, loc);
11570 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11572 if (is_private_local_id(id)) {
11575 if (st_is_member(p->pvtbl, id)) {
11576 yyerror1(loc, "duplicated variable name");
11579 st_insert(p->pvtbl, (st_data_t)id, 0);
11584 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11587 p->pktbl = st_init_numtable();
11589 else if (st_is_member(p->pktbl, key)) {
11590 yyerror1(loc, "duplicated key name");
11593 st_insert(p->pktbl, (st_data_t)key, 0);
11598 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11600 return NEW_HASH(hash, loc);
11602 #endif /* !RIPPER */
11606 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11611 ID vid = lhs->nd_vid;
11612 YYLTYPE lhs_loc = lhs->nd_loc;
11614 lhs->nd_value = rhs;
11615 nd_set_loc(lhs, loc);
11616 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11617 if (is_notop_id(vid)) {
11618 switch (id_type(vid)) {
11622 asgn->nd_aid = vid;
11626 else if (op == tANDOP) {
11627 lhs->nd_value = rhs;
11628 nd_set_loc(lhs, loc);
11629 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11633 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11634 nd_set_loc(asgn, loc);
11638 asgn = NEW_BEGIN(0, loc);
11644 new_ary_op_assign(struct parser_params *p, NODE *ary,
11645 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11649 args = make_list(args, args_loc);
11650 if (nd_type(args) == NODE_BLOCK_PASS) {
11651 args = NEW_ARGSCAT(args, rhs, loc);
11654 args = arg_concat(p, args, rhs, loc);
11656 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11662 new_attr_op_assign(struct parser_params *p, NODE *lhs,
11663 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11667 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11673 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11678 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11681 asgn = NEW_BEGIN(0, loc);
11688 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11691 yyerror1(loc, "dynamic constant assignment");
11693 return NEW_CDECL(0, 0, (path), loc);
11697 const_decl(struct parser_params *p, VALUE path)
11700 path = dispatch1(assign_error, path);
11707 assign_error(struct parser_params *p, VALUE a)
11709 a = dispatch1(assign_error, a);
11715 var_field(struct parser_params *p, VALUE a)
11717 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11723 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11725 NODE *result = head;
11727 NODE *tmp = rescue_else ? rescue_else : rescue;
11728 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11730 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11731 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11733 else if (rescue_else) {
11734 result = block_append(p, result, rescue_else);
11737 result = NEW_ENSURE(result, ensure, loc);
11739 fixpos(result, head);
11745 warn_unused_var(struct parser_params *p, struct local_vars *local)
11749 if (!local->used) return;
11750 cnt = local->used->pos;
11751 if (cnt != local->vars->pos) {
11752 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11755 ID *v = local->vars->tbl;
11756 ID *u = local->used->tbl;
11757 for (int i = 0; i < cnt; ++i) {
11758 if (!v[i] || (u[i] & LVAR_USED)) continue;
11759 if (is_private_local_id(v[i])) continue;
11760 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11766 local_push(struct parser_params *p, int toplevel_scope)
11768 struct local_vars *local;
11769 int inherits_dvars = toplevel_scope && compile_for_eval;
11770 int warn_unused_vars = RTEST(ruby_verbose);
11772 local = ALLOC(struct local_vars);
11773 local->prev = p->lvtbl;
11774 local->args = vtable_alloc(0);
11775 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11777 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11778 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11779 local->numparam.outer = 0;
11780 local->numparam.inner = 0;
11781 local->numparam.current = 0;
11783 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11785 # if WARN_PAST_SCOPE
11794 local_pop(struct parser_params *p)
11796 struct local_vars *local = p->lvtbl->prev;
11797 if (p->lvtbl->used) {
11798 warn_unused_var(p, p->lvtbl);
11799 vtable_free(p->lvtbl->used);
11801 # if WARN_PAST_SCOPE
11802 while (p->lvtbl->past) {
11803 struct vtable *past = p->lvtbl->past;
11804 p->lvtbl->past = past->prev;
11808 vtable_free(p->lvtbl->args);
11809 vtable_free(p->lvtbl->vars);
11812 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11818 local_tbl(struct parser_params *p)
11820 int cnt_args = vtable_size(p->lvtbl->args);
11821 int cnt_vars = vtable_size(p->lvtbl->vars);
11822 int cnt = cnt_args + cnt_vars;
11826 if (cnt <= 0) return 0;
11827 buf = ALLOC_N(ID, cnt + 2);
11828 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11829 /* remove IDs duplicated to warn shadowing */
11830 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11831 ID id = p->lvtbl->vars->tbl[i];
11832 if (!vtable_included(p->lvtbl->args, id)) {
11837 REALLOC_N(buf, ID, (cnt = j) + 2);
11840 rb_ast_add_local_table(p->ast, buf);
11846 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11852 n = NEW_NODE(type, a0, a1, a2, loc);
11859 numparam_name(struct parser_params *p, ID id)
11861 if (!NUMPARAM_ID_P(id)) return;
11862 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11863 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11867 arg_var(struct parser_params *p, ID id)
11869 numparam_name(p, id);
11870 vtable_add(p->lvtbl->args, id);
11874 local_var(struct parser_params *p, ID id)
11876 numparam_name(p, id);
11877 vtable_add(p->lvtbl->vars, id);
11878 if (p->lvtbl->used) {
11879 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11884 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11886 struct vtable *vars, *args, *used;
11888 vars = p->lvtbl->vars;
11889 args = p->lvtbl->args;
11890 used = p->lvtbl->used;
11892 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11895 if (used) used = used->prev;
11898 if (vars && vars->prev == DVARS_INHERIT) {
11899 return rb_local_defined(id, p->parent_iseq);
11901 else if (vtable_included(args, id)) {
11905 int i = vtable_included(vars, id);
11906 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11912 local_id(struct parser_params *p, ID id)
11914 return local_id_ref(p, id, NULL);
11918 numparam_push(struct parser_params *p)
11921 struct local_vars *local = p->lvtbl;
11922 NODE *inner = local->numparam.inner;
11923 if (!local->numparam.outer) {
11924 local->numparam.outer = local->numparam.current;
11926 local->numparam.inner = 0;
11927 local->numparam.current = 0;
11935 numparam_pop(struct parser_params *p, NODE *prev_inner)
11938 struct local_vars *local = p->lvtbl;
11940 /* prefer first one */
11941 local->numparam.inner = prev_inner;
11943 else if (local->numparam.current) {
11944 /* current and inner are exclusive */
11945 local->numparam.inner = local->numparam.current;
11947 if (p->max_numparam > NO_PARAM) {
11948 /* current and outer are exclusive */
11949 local->numparam.current = local->numparam.outer;
11950 local->numparam.outer = 0;
11953 /* no numbered parameter */
11954 local->numparam.current = 0;
11959 static const struct vtable *
11960 dyna_push(struct parser_params *p)
11962 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11963 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11964 if (p->lvtbl->used) {
11965 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11967 return p->lvtbl->args;
11971 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11973 struct vtable *tmp = *vtblp;
11974 *vtblp = tmp->prev;
11975 # if WARN_PAST_SCOPE
11976 if (p->past_scope_enabled) {
11977 tmp->prev = p->lvtbl->past;
11978 p->lvtbl->past = tmp;
11986 dyna_pop_1(struct parser_params *p)
11988 struct vtable *tmp;
11990 if ((tmp = p->lvtbl->used) != 0) {
11991 warn_unused_var(p, p->lvtbl);
11992 p->lvtbl->used = p->lvtbl->used->prev;
11995 dyna_pop_vtable(p, &p->lvtbl->args);
11996 dyna_pop_vtable(p, &p->lvtbl->vars);
12000 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12002 while (p->lvtbl->args != lvargs) {
12004 if (!p->lvtbl->args) {
12005 struct local_vars *local = p->lvtbl->prev;
12006 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12014 dyna_in_block(struct parser_params *p)
12016 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12020 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12022 struct vtable *vars, *args, *used;
12025 args = p->lvtbl->args;
12026 vars = p->lvtbl->vars;
12027 used = p->lvtbl->used;
12029 while (!DVARS_TERMINAL_P(vars)) {
12030 if (vtable_included(args, id)) {
12033 if ((i = vtable_included(vars, id)) != 0) {
12034 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12039 if (!vidrefp) used = 0;
12040 if (used) used = used->prev;
12043 if (vars == DVARS_INHERIT) {
12044 return rb_dvar_defined(id, p->parent_iseq);
12051 dvar_defined(struct parser_params *p, ID id)
12053 return dvar_defined_ref(p, id, NULL);
12057 dvar_curr(struct parser_params *p, ID id)
12059 return (vtable_included(p->lvtbl->args, id) ||
12060 vtable_included(p->lvtbl->vars, id));
12064 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12067 "regexp encoding option '%c' differs from source encoding '%s'",
12068 c, rb_enc_name(rb_enc_get(str)));
12073 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12075 int c = RE_OPTION_ENCODING_IDX(options);
12079 rb_char_to_option_kcode(c, &opt, &idx);
12080 if (idx != ENCODING_GET(str) &&
12081 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12084 ENCODING_SET(str, idx);
12086 else if (RE_OPTION_ENCODING_NONE(options)) {
12087 if (!ENCODING_IS_ASCII8BIT(str) &&
12088 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12092 rb_enc_associate(str, rb_ascii8bit_encoding());
12094 else if (p->enc == rb_usascii_encoding()) {
12095 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12096 /* raise in re.c */
12097 rb_enc_associate(str, rb_usascii_encoding());
12100 rb_enc_associate(str, rb_ascii8bit_encoding());
12110 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12112 int c = rb_reg_fragment_setenc(p, str, options);
12113 if (c) reg_fragment_enc_error(p, str, c);
12117 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12120 reg_fragment_setenc(p, str, options);
12121 err = rb_reg_check_preprocess(str);
12123 err = rb_obj_as_string(err);
12124 compile_error(p, "%"PRIsVALUE, err);
12131 struct parser_params* parser;
12134 const YYLTYPE *loc;
12135 } reg_named_capture_assign_t;
12138 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12139 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12141 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12142 struct parser_params* p = arg->parser;
12143 rb_encoding *enc = arg->enc;
12144 long len = name_end - name;
12145 const char *s = (const char *)name;
12149 if (!len) return ST_CONTINUE;
12150 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len))
12151 return ST_CONTINUE;
12152 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12153 return ST_CONTINUE;
12155 var = intern_cstr(s, len, enc);
12156 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12157 succ = arg->succ_block;
12158 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12159 succ = block_append(p, succ, node);
12160 arg->succ_block = succ;
12161 return ST_CONTINUE;
12165 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12167 reg_named_capture_assign_t arg;
12170 arg.enc = rb_enc_get(regexp);
12171 arg.succ_block = 0;
12173 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12175 if (!arg.succ_block) return 0;
12176 return arg.succ_block->nd_next;
12180 parser_reg_compile(struct parser_params* p, VALUE str, int options)
12182 reg_fragment_setenc(p, str, options);
12183 return rb_parser_reg_compile(p, str, options);
12187 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12189 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12193 reg_compile(struct parser_params* p, VALUE str, int options)
12198 err = rb_errinfo();
12199 re = parser_reg_compile(p, str, options);
12201 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12202 rb_set_errinfo(err);
12203 compile_error(p, "%"PRIsVALUE, m);
12210 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12212 VALUE err = rb_errinfo();
12214 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12215 int c = rb_reg_fragment_setenc(p, str, options);
12216 if (c) reg_fragment_enc_error(p, str, c);
12217 re = rb_parser_reg_compile(p, str, options);
12219 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12220 rb_set_errinfo(err);
12228 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12230 struct parser_params *p;
12231 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12232 p->do_print = print;
12234 p->do_chomp = chomp;
12235 p->do_split = split;
12239 rb_parser_warn_location(VALUE vparser, int warn)
12241 struct parser_params *p;
12242 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12243 p->warn_location = warn;
12247 parser_append_options(struct parser_params *p, NODE *node)
12249 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12250 const YYLTYPE *const LOC = &default_location;
12253 NODE *print = NEW_FCALL(rb_intern("print"),
12254 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12256 node = block_append(p, node, print);
12261 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12262 NODE *split = NEW_GASGN(rb_intern("$F"),
12263 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12264 rb_intern("split"), args, LOC),
12266 node = block_append(p, split, node);
12269 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12270 rb_intern("chomp!"), 0, LOC);
12271 node = block_append(p, chomp, node);
12274 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12281 rb_init_parse(void)
12283 /* just to suppress unused-function warnings */
12289 internal_id(struct parser_params *p)
12291 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12292 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12294 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12296 #endif /* !RIPPER */
12299 parser_initialize(struct parser_params *p)
12301 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12302 p->command_start = TRUE;
12303 p->ruby_sourcefile_string = Qnil;
12304 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12307 p->delayed.token = Qnil;
12309 p->parsing_thread = Qnil;
12311 p->error_buffer = Qfalse;
12313 p->debug_buffer = Qnil;
12314 p->debug_output = rb_stdout;
12315 p->enc = rb_utf8_encoding();
12319 #define parser_mark ripper_parser_mark
12320 #define parser_free ripper_parser_free
12324 parser_mark(void *ptr)
12326 struct parser_params *p = (struct parser_params*)ptr;
12328 rb_gc_mark(p->lex.input);
12329 rb_gc_mark(p->lex.prevline);
12330 rb_gc_mark(p->lex.lastline);
12331 rb_gc_mark(p->lex.nextline);
12332 rb_gc_mark(p->ruby_sourcefile_string);
12333 rb_gc_mark((VALUE)p->lex.strterm);
12334 rb_gc_mark((VALUE)p->ast);
12335 rb_gc_mark(p->case_labels);
12337 rb_gc_mark(p->debug_lines);
12338 rb_gc_mark(p->compile_option);
12339 rb_gc_mark(p->error_buffer);
12341 rb_gc_mark(p->delayed.token);
12342 rb_gc_mark(p->value);
12343 rb_gc_mark(p->result);
12344 rb_gc_mark(p->parsing_thread);
12346 rb_gc_mark(p->debug_buffer);
12347 rb_gc_mark(p->debug_output);
12349 rb_gc_mark((VALUE)p->heap);
12354 parser_free(void *ptr)
12356 struct parser_params *p = (struct parser_params*)ptr;
12357 struct local_vars *local, *prev;
12360 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12362 for (local = p->lvtbl; local; local = prev) {
12363 if (local->vars) xfree(local->vars);
12364 prev = local->prev;
12368 token_info *ptinfo;
12369 while ((ptinfo = p->token_info) != 0) {
12370 p->token_info = ptinfo->next;
12378 parser_memsize(const void *ptr)
12380 struct parser_params *p = (struct parser_params*)ptr;
12381 struct local_vars *local;
12382 size_t size = sizeof(*p);
12385 for (local = p->lvtbl; local; local = local->prev) {
12386 size += sizeof(*local);
12387 if (local->vars) size += local->vars->capa * sizeof(ID);
12392 static const rb_data_type_t parser_data_type = {
12403 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12407 #undef rb_reserved_word
12409 const struct kwtable *
12410 rb_reserved_word(const char *str, unsigned int len)
12412 return reserved_word(str, len);
12416 rb_parser_new(void)
12418 struct parser_params *p;
12419 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12420 &parser_data_type, p);
12421 parser_initialize(p);
12426 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12428 struct parser_params *p;
12430 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12431 p->error_buffer = main ? Qfalse : Qnil;
12432 p->parent_iseq = base;
12438 #define rb_parser_end_seen_p ripper_parser_end_seen_p
12439 #define rb_parser_encoding ripper_parser_encoding
12440 #define rb_parser_get_yydebug ripper_parser_get_yydebug
12441 #define rb_parser_set_yydebug ripper_parser_set_yydebug
12442 #define rb_parser_get_debug_output ripper_parser_get_debug_output
12443 #define rb_parser_set_debug_output ripper_parser_set_debug_output
12444 static VALUE ripper_parser_end_seen_p(VALUE vparser);
12445 static VALUE ripper_parser_encoding(VALUE vparser);
12446 static VALUE ripper_parser_get_yydebug(VALUE self);
12447 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12448 static VALUE ripper_parser_get_debug_output(VALUE self);
12449 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12453 * ripper.error? -> Boolean
12455 * Return true if parsed source has errors.
12458 ripper_error_p(VALUE vparser)
12460 struct parser_params *p;
12462 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12463 return p->error_p ? Qtrue : Qfalse;
12469 * ripper.end_seen? -> Boolean
12471 * Return true if parsed source ended by +\_\_END\_\_+.
12474 rb_parser_end_seen_p(VALUE vparser)
12476 struct parser_params *p;
12478 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12479 return p->ruby__end__seen ? Qtrue : Qfalse;
12484 * ripper.encoding -> encoding
12486 * Return encoding of the source.
12489 rb_parser_encoding(VALUE vparser)
12491 struct parser_params *p;
12493 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12494 return rb_enc_from_encoding(p->enc);
12500 * ripper.yydebug -> true or false
12505 rb_parser_get_yydebug(VALUE self)
12507 struct parser_params *p;
12509 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12510 return p->debug ? Qtrue : Qfalse;
12516 * ripper.yydebug = flag
12521 rb_parser_set_yydebug(VALUE self, VALUE flag)
12523 struct parser_params *p;
12525 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12526 p->debug = RTEST(flag);
12532 * ripper.debug_output -> obj
12534 * Get debug output.
12537 rb_parser_get_debug_output(VALUE self)
12539 struct parser_params *p;
12541 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12542 return p->debug_output;
12547 * ripper.debug_output = obj
12549 * Set debug output.
12552 rb_parser_set_debug_output(VALUE self, VALUE output)
12554 struct parser_params *p;
12556 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12557 return p->debug_output = output;
12562 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12563 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12564 * potential memory leak */
12565 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12566 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12567 (new)->cnt = (cnt), (ptr))
12570 rb_parser_malloc(struct parser_params *p, size_t size)
12572 size_t cnt = HEAPCNT(1, size);
12573 rb_imemo_tmpbuf_t *n = NEWHEAP();
12574 void *ptr = xmalloc(size);
12576 return ADD2HEAP(n, cnt, ptr);
12580 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12582 size_t cnt = HEAPCNT(nelem, size);
12583 rb_imemo_tmpbuf_t *n = NEWHEAP();
12584 void *ptr = xcalloc(nelem, size);
12586 return ADD2HEAP(n, cnt, ptr);
12590 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12592 rb_imemo_tmpbuf_t *n;
12593 size_t cnt = HEAPCNT(1, size);
12595 if (ptr && (n = p->heap) != NULL) {
12597 if (n->ptr == ptr) {
12598 n->ptr = ptr = xrealloc(ptr, size);
12599 if (n->cnt) n->cnt = cnt;
12602 } while ((n = n->next) != NULL);
12605 ptr = xrealloc(ptr, size);
12606 return ADD2HEAP(n, cnt, ptr);
12610 rb_parser_free(struct parser_params *p, void *ptr)
12612 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12614 while ((n = *prev) != NULL) {
12615 if (n->ptr == ptr) {
12617 rb_gc_force_recycle((VALUE)n);
12627 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12630 VALUE mesg = p->debug_buffer;
12632 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12634 rb_str_vcatf(mesg, fmt, ap);
12636 if (RSTRING_END(mesg)[-1] == '\n') {
12637 rb_io_write(p->debug_output, mesg);
12638 p->debug_buffer = Qnil;
12643 parser_compile_error(struct parser_params *p, const char *fmt, ...)
12647 rb_io_flush(p->debug_output);
12651 rb_syntax_error_append(p->error_buffer,
12652 p->ruby_sourcefile_string,
12653 p->ruby_sourceline,
12654 rb_long2int(p->lex.pcur - p->lex.pbeg),
12660 count_char(const char *str, int c)
12663 while (str[n] == c) ++n;
12668 * strip enclosing double-quotes, same as the default yytnamerr except
12669 * for that single-quotes matching back-quotes do not stop stripping.
12671 * "\"`class' keyword\"" => "`class' keyword"
12673 RUBY_FUNC_EXPORTED size_t
12674 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12677 if (*yystr == '"') {
12678 size_t yyn = 0, bquote = 0;
12679 const char *yyp = yystr;
12685 bquote = count_char(yyp+1, '`') + 1;
12686 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12694 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12695 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12701 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12702 if (yyres) memcpy(yyres + yyn, yyp, 3);
12707 goto do_not_strip_quotes;
12710 goto do_not_strip_quotes;
12713 if (*++yyp != '\\')
12714 goto do_not_strip_quotes;
12715 /* Fall through. */
12730 do_not_strip_quotes: ;
12733 if (!yyres) return strlen(yystr);
12735 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12740 #ifdef RIPPER_DEBUG
12743 ripper_validate_object(VALUE self, VALUE x)
12745 if (x == Qfalse) return x;
12746 if (x == Qtrue) return x;
12747 if (x == Qnil) return x;
12749 rb_raise(rb_eArgError, "Qundef given");
12750 if (FIXNUM_P(x)) return x;
12751 if (SYMBOL_P(x)) return x;
12752 switch (BUILTIN_TYPE(x)) {
12762 if (nd_type((NODE *)x) != NODE_RIPPER) {
12763 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12765 x = ((NODE *)x)->nd_rval;
12768 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12769 (void *)x, rb_obj_classname(x));
12771 if (!RBASIC_CLASS(x)) {
12772 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12773 (void *)x, rb_builtin_type_name(TYPE(x)));
12779 #define validate(x) ((x) = get_value(x))
12782 ripper_dispatch0(struct parser_params *p, ID mid)
12784 return rb_funcall(p->value, mid, 0);
12788 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12791 return rb_funcall(p->value, mid, 1, a);
12795 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12799 return rb_funcall(p->value, mid, 2, a, b);
12803 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12808 return rb_funcall(p->value, mid, 3, a, b, c);
12812 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12818 return rb_funcall(p->value, mid, 4, a, b, c, d);
12822 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12829 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12833 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12842 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12846 ripper_get_id(VALUE v)
12849 if (!RB_TYPE_P(v, T_NODE)) return 0;
12851 if (nd_type(nd) != NODE_RIPPER) return 0;
12856 ripper_get_value(VALUE v)
12859 if (v == Qundef) return Qnil;
12860 if (!RB_TYPE_P(v, T_NODE)) return v;
12862 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12863 return nd->nd_rval;
12867 ripper_error(struct parser_params *p)
12873 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12878 va_start(args, fmt);
12879 str = rb_vsprintf(fmt, args);
12881 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12886 ripper_lex_get_generic(struct parser_params *p, VALUE src)
12888 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12889 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12890 rb_raise(rb_eTypeError,
12891 "gets returned %"PRIsVALUE" (expected String or nil)",
12892 rb_obj_class(line));
12898 ripper_lex_io_get(struct parser_params *p, VALUE src)
12900 return rb_io_gets(src);
12904 ripper_s_allocate(VALUE klass)
12906 struct parser_params *p;
12907 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12908 &parser_data_type, p);
12913 #define ripper_initialized_p(r) ((r)->lex.input != 0)
12917 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12919 * Create a new Ripper object.
12920 * _src_ must be a String, an IO, or an Object which has #gets method.
12922 * This method does not starts parsing.
12923 * See also Ripper#parse and Ripper.parse.
12926 ripper_initialize(int argc, VALUE *argv, VALUE self)
12928 struct parser_params *p;
12929 VALUE src, fname, lineno;
12931 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12932 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12933 if (RB_TYPE_P(src, T_FILE)) {
12934 p->lex.gets = ripper_lex_io_get;
12936 else if (rb_respond_to(src, id_gets)) {
12937 p->lex.gets = ripper_lex_get_generic;
12941 p->lex.gets = lex_get_str;
12943 p->lex.input = src;
12945 if (NIL_P(fname)) {
12946 fname = STR_NEW2("(ripper)");
12950 StringValueCStr(fname);
12951 fname = rb_str_new_frozen(fname);
12953 parser_initialize(p);
12955 p->ruby_sourcefile_string = fname;
12956 p->ruby_sourcefile = RSTRING_PTR(fname);
12957 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12963 ripper_parse0(VALUE parser_v)
12965 struct parser_params *p;
12967 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12969 p->ast = rb_ast_new();
12970 ripper_yyparse((void*)p);
12971 rb_ast_dispose(p->ast);
12977 ripper_ensure(VALUE parser_v)
12979 struct parser_params *p;
12981 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12982 p->parsing_thread = Qnil;
12990 * Start parsing and returns the value of the root action.
12993 ripper_parse(VALUE self)
12995 struct parser_params *p;
12997 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12998 if (!ripper_initialized_p(p)) {
12999 rb_raise(rb_eArgError, "method called for uninitialized object");
13001 if (!NIL_P(p->parsing_thread)) {
13002 if (p->parsing_thread == rb_thread_current())
13003 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13005 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13007 p->parsing_thread = rb_thread_current();
13008 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13015 * ripper.column -> Integer
13017 * Return column number of current parsing line.
13018 * This number starts from 0.
13021 ripper_column(VALUE self)
13023 struct parser_params *p;
13026 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13027 if (!ripper_initialized_p(p)) {
13028 rb_raise(rb_eArgError, "method called for uninitialized object");
13030 if (NIL_P(p->parsing_thread)) return Qnil;
13031 col = p->lex.ptok - p->lex.pbeg;
13032 return LONG2NUM(col);
13037 * ripper.filename -> String
13039 * Return current parsing filename.
13042 ripper_filename(VALUE self)
13044 struct parser_params *p;
13046 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13047 if (!ripper_initialized_p(p)) {
13048 rb_raise(rb_eArgError, "method called for uninitialized object");
13050 return p->ruby_sourcefile_string;
13055 * ripper.lineno -> Integer
13057 * Return line number of current parsing line.
13058 * This number starts from 1.
13061 ripper_lineno(VALUE self)
13063 struct parser_params *p;
13065 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13066 if (!ripper_initialized_p(p)) {
13067 rb_raise(rb_eArgError, "method called for uninitialized object");
13069 if (NIL_P(p->parsing_thread)) return Qnil;
13070 return INT2NUM(p->ruby_sourceline);
13075 * ripper.state -> Integer
13077 * Return scanner state of current token.
13080 ripper_state(VALUE self)
13082 struct parser_params *p;
13084 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13085 if (!ripper_initialized_p(p)) {
13086 rb_raise(rb_eArgError, "method called for uninitialized object");
13088 if (NIL_P(p->parsing_thread)) return Qnil;
13089 return INT2NUM(p->lex.state);
13094 * ripper.token -> String
13096 * Return the current token string.
13099 ripper_token(VALUE self)
13101 struct parser_params *p;
13104 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13105 if (!ripper_initialized_p(p)) {
13106 rb_raise(rb_eArgError, "method called for uninitialized object");
13108 if (NIL_P(p->parsing_thread)) return Qnil;
13109 pos = p->lex.ptok - p->lex.pbeg;
13110 len = p->lex.pcur - p->lex.ptok;
13111 return rb_str_subseq(p->lex.lastline, pos, len);
13114 #ifdef RIPPER_DEBUG
13117 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13120 if (obj == Qundef) {
13121 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13128 ripper_value(VALUE self, VALUE obj)
13130 return ULONG2NUM(obj);
13136 * Ripper.lex_state_name(integer) -> string
13138 * Returns a string representation of lex_state.
13141 ripper_lex_state_name(VALUE self, VALUE state)
13143 return rb_parser_lex_state_name(NUM2INT(state));
13149 ripper_init_eventids1();
13150 ripper_init_eventids2();
13151 id_warn = rb_intern_const("warn");
13152 id_warning = rb_intern_const("warning");
13153 id_gets = rb_intern_const("gets");
13154 id_assoc = rb_intern_const("=>");
13156 (void)yystpcpy; /* may not used in newer bison */
13162 InitVM_ripper(void)
13166 Ripper = rb_define_class("Ripper", rb_cObject);
13167 /* version of Ripper */
13168 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13169 rb_define_alloc_func(Ripper, ripper_s_allocate);
13170 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13171 rb_define_method(Ripper, "parse", ripper_parse, 0);
13172 rb_define_method(Ripper, "column", ripper_column, 0);
13173 rb_define_method(Ripper, "filename", ripper_filename, 0);
13174 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13175 rb_define_method(Ripper, "state", ripper_state, 0);
13176 rb_define_method(Ripper, "token", ripper_token, 0);
13177 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13178 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13179 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13180 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13181 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13182 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13183 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13184 #ifdef RIPPER_DEBUG
13185 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13186 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13187 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13190 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13191 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13193 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13195 /* ignore newline, +/- is a sign. */
13196 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13197 /* newline significant, +/- is an operator. */
13198 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13199 /* ditto, and unbound braces. */
13200 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13201 /* ditto, and unbound braces. */
13202 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13203 /* newline significant, +/- is an operator. */
13204 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13205 /* newline significant, +/- is an operator. */
13206 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13207 /* newline significant, +/- is an operator. */
13208 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13209 /* ignore newline, no reserved words. */
13210 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13211 /* right after `.' or `::', no reserved words. */
13212 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13213 /* immediate after `class', no here document. */
13214 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13215 /* flag bit, label is allowed. */
13216 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13217 /* flag bit, just after a label. */
13218 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13219 /* symbol literal as FNAME. */
13220 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13221 /* equals to +EXPR_BEG+ */
13222 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13223 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13224 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13225 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13226 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13227 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13228 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13229 /* equals to +0+ */
13230 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13232 ripper_init_eventids1_table(Ripper);
13233 ripper_init_eventids2_table(Ripper);
13236 /* Hack to let RDoc document SCRIPT_LINES__ */
13239 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13240 * after the assignment will be added as an Array of lines with the file
13243 rb_define_global_const("SCRIPT_LINES__", Qnil);
13247 #endif /* RIPPER */
13252 * c-file-style: "ruby"