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 %lex-param {struct parser_params *p}
972 %parse-param {struct parser_params *p}
975 RUBY_SET_YYLLOC_OF_NONE(@$);
984 const struct vtable *vars;
985 struct rb_strterm_struct *strterm;
989 keyword_class "`class'"
990 keyword_module "`module'"
992 keyword_undef "`undef'"
993 keyword_begin "`begin'"
994 keyword_rescue "`rescue'"
995 keyword_ensure "`ensure'"
998 keyword_unless "`unless'"
999 keyword_then "`then'"
1000 keyword_elsif "`elsif'"
1001 keyword_else "`else'"
1002 keyword_case "`case'"
1003 keyword_when "`when'"
1004 keyword_while "`while'"
1005 keyword_until "`until'"
1007 keyword_break "`break'"
1008 keyword_next "`next'"
1009 keyword_redo "`redo'"
1010 keyword_retry "`retry'"
1013 keyword_do_cond "`do' for condition"
1014 keyword_do_block "`do' for block"
1015 keyword_do_LAMBDA "`do' for lambda"
1016 keyword_return "`return'"
1017 keyword_yield "`yield'"
1018 keyword_super "`super'"
1019 keyword_self "`self'"
1021 keyword_true "`true'"
1022 keyword_false "`false'"
1026 modifier_if "`if' modifier"
1027 modifier_unless "`unless' modifier"
1028 modifier_while "`while' modifier"
1029 modifier_until "`until' modifier"
1030 modifier_rescue "`rescue' modifier"
1031 keyword_alias "`alias'"
1032 keyword_defined "`defined?'"
1033 keyword_BEGIN "`BEGIN'"
1035 keyword__LINE__ "`__LINE__'"
1036 keyword__FILE__ "`__FILE__'"
1037 keyword__ENCODING__ "`__ENCODING__'"
1039 %token <val> tIDENTIFIER "local variable or method"
1040 %token <val> tFID "method"
1041 %token <val> tGVAR "global variable"
1042 %token <val> tIVAR "instance variable"
1043 %token <val> tCONSTANT "constant"
1044 %token <val> tCVAR "class variable"
1046 %token <val> tINTEGER "integer literal"
1047 %token <val> tFLOAT "float literal"
1048 %token <val> tRATIONAL "rational literal"
1049 %token <val> tIMAGINARY "imaginary literal"
1050 %token <val> tCHAR "char literal"
1051 %token <val> tNTH_REF "numbered reference"
1052 %token <val> tBACK_REF "back reference"
1053 %token <val> tSTRING_CONTENT "literal content"
1054 %token <val> tREGEXP_END
1056 %type <val> singleton strings string string1 xstring regexp
1057 %type <val> string_contents xstring_contents regexp_contents string_content
1058 %type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1059 %type <val> literal numeric simple_numeric ssym dsym symbol cpath
1060 %type <val> top_compstmt top_stmts top_stmt begin_block
1061 %type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1062 %type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1063 %type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1064 %type <val> args call_args opt_call_args
1065 %type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1066 %type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1067 %type <val> command_rhs arg_rhs
1068 %type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1069 %type <val> f_block_optarg f_block_opt
1070 %type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1071 %type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1072 %type <val> block_param opt_block_param block_param_def f_opt
1073 %type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1074 %type <val> bv_decls opt_bv_decl bvar
1075 %type <val> lambda f_larglist lambda_body brace_body do_body
1076 %type <val> brace_block cmd_brace_block do_block lhs none fitem
1077 %type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1078 %type <val> p_case_body p_cases p_top_expr p_top_expr_body
1079 %type <val> p_expr p_as p_alt p_expr_basic
1080 %type <val> p_args p_args_head p_args_tail p_args_post p_arg
1081 %type <val> p_value p_primitive p_variable p_var_ref p_const
1082 %type <val> p_kwargs p_kwarg p_kw
1083 %type <val> keyword_variable user_variable sym operation operation2 operation3
1084 %type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1085 %type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1086 %type <val> p_kwrest p_kwnorest p_kw_label
1087 %type <val> f_no_kwarg args_forward
1088 %token END_OF_INPUT 0 "end-of-input"
1090 /* escaped chars, should be ignored otherwise */
1091 %token <val> '\\' "backslash"
1092 %token tSP "escaped space"
1093 %token <val> '\t' "escaped horizontal tab"
1094 %token <val> '\f' "escaped form feed"
1095 %token <val> '\r' "escaped carriage return"
1096 %token <val> '\13' "escaped vertical tab"
1097 %token tUPLUS 132 "unary+"
1098 %token tUMINUS 133 "unary-"
1099 %token tPOW 134 "**"
1100 %token tCMP 135 "<=>"
1102 %token tEQQ 141 "==="
1103 %token tNEQ 142 "!="
1104 %token tGEQ 139 ">="
1105 %token tLEQ 138 "<="
1106 %token tANDOP 148 "&&"
1107 %token tOROP 149 "||"
1108 %token tMATCH 143 "=~"
1109 %token tNMATCH 144 "!~"
1110 %token tDOT2 128 ".."
1111 %token tDOT3 129 "..."
1112 %token tBDOT2 130 "(.."
1113 %token tBDOT3 131 "(..."
1114 %token tAREF 145 "[]"
1115 %token tASET 146 "[]="
1116 %token tLSHFT 136 "<<"
1117 %token tRSHFT 137 ">>"
1118 %token <val> tANDDOT 150 "&."
1119 %token <val> tCOLON2 147 "::"
1120 %token tCOLON3 ":: at EXPR_BEG"
1121 %token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1124 %token tLPAREN_ARG "( arg"
1128 %token tLBRACE_ARG "{ arg"
1130 %token tDSTAR "**arg"
1133 %token tSYMBEG "symbol literal"
1134 %token tSTRING_BEG "string literal"
1135 %token tXSTRING_BEG "backtick literal"
1136 %token tREGEXP_BEG "regexp literal"
1137 %token tWORDS_BEG "word list"
1138 %token tQWORDS_BEG "verbatim word list"
1139 %token tSYMBOLS_BEG "symbol list"
1140 %token tQSYMBOLS_BEG "verbatim symbol list"
1141 %token tSTRING_END "terminator"
1142 %token tSTRING_DEND "'}'"
1143 %token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1150 %nonassoc tLBRACE_ARG
1152 %nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1153 %left keyword_or keyword_and
1155 %nonassoc keyword_defined
1157 %left modifier_rescue
1159 %nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1162 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1163 %left '>' tGEQ '<' tLEQ
1169 %right tUMINUS_NUM tUMINUS
1171 %right '!' '~' tUPLUS
1177 SET_LEX_STATE(EXPR_BEG);
1178 local_push(p, ifndef_ripper(1)+0);
1183 if ($2 && !compile_for_eval) {
1185 /* last expression should not be void */
1186 if (nd_type(node) == NODE_BLOCK) {
1187 while (node->nd_next) {
1188 node = node->nd_next;
1190 node = node->nd_head;
1192 node = remove_begin(node);
1195 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1197 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1202 top_compstmt : top_stmts opt_terms
1204 $$ = void_stmts(p, $1);
1211 $$ = NEW_BEGIN(0, &@$);
1213 {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;}
1218 $$ = newline_node($1);
1220 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1222 | top_stmts terms top_stmt
1225 $$ = block_append(p, $1, newline_node($3));
1227 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1231 $$ = remove_begin($2);
1236 | keyword_BEGIN begin_block
1242 begin_block : '{' top_compstmt '}'
1245 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1246 NEW_BEGIN($2, &@$));
1247 $$ = NEW_BEGIN(0, &@$);
1249 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1255 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1260 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1262 {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;}
1269 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1271 {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;}
1275 compstmt : stmts opt_terms
1277 $$ = void_stmts(p, $1);
1284 $$ = NEW_BEGIN(0, &@$);
1286 {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;}
1291 $$ = newline_node($1);
1293 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1295 | stmts terms stmt_or_begin
1298 $$ = block_append(p, $1, newline_node($3));
1300 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1304 $$ = remove_begin($2);
1308 stmt_or_begin : stmt
1314 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1322 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1325 $$ = NEW_ALIAS($2, $4, &@$);
1327 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1329 | keyword_alias tGVAR tGVAR
1332 $$ = NEW_VALIAS($2, $3, &@$);
1334 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1336 | keyword_alias tGVAR tBACK_REF
1341 buf[1] = (char)$3->nd_nth;
1342 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1344 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1346 | keyword_alias tGVAR tNTH_REF
1349 yyerror1(&@3, "can't make alias for the number variables");
1350 $$ = NEW_BEGIN(0, &@$);
1352 {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);
1354 | keyword_undef undef_list
1359 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1361 | stmt modifier_if expr_value
1364 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1367 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1369 | stmt modifier_unless expr_value
1372 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1375 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1377 | stmt modifier_while expr_value
1380 if ($1 && nd_type($1) == NODE_BEGIN) {
1381 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1384 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1387 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1389 | stmt modifier_until expr_value
1392 if ($1 && nd_type($1) == NODE_BEGIN) {
1393 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1396 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1399 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1401 | stmt modifier_rescue stmt
1405 YYLTYPE loc = code_loc_gen(&@2, &@3);
1406 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1407 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1409 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1411 | keyword_END '{' compstmt '}'
1414 rb_warn0("END in method; use at_exit");
1418 NODE *scope = NEW_NODE(
1419 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1420 $$ = NEW_POSTEXE(scope, &@$);
1423 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1426 | mlhs '=' command_call
1430 $$ = node_assign(p, $1, $3, &@$);
1432 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1438 $$ = node_assign(p, $1, $3, &@$);
1440 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1442 | mlhs '=' mrhs_arg modifier_rescue stmt
1445 YYLTYPE loc = code_loc_gen(&@4, &@5);
1447 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1449 {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;}
1454 $$ = node_assign(p, $1, $3, &@$);
1456 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1461 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
1473 $$ = new_op_assign(p, $1, $2, $3, &@$);
1475 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1477 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1481 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1483 {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;}
1486 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1490 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1492 {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;}
1494 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1498 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1500 {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;}
1502 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1505 YYLTYPE loc = code_loc_gen(&@1, &@3);
1506 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1508 {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;}
1510 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1514 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1516 {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;}
1518 | backref tOP_ASGN command_rhs
1521 rb_backref_error(p, $1);
1522 $$ = NEW_BEGIN(0, &@$);
1524 {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);
1528 command_rhs : command_call %prec tOP_ASGN
1533 | command_call modifier_rescue stmt
1536 YYLTYPE loc = code_loc_gen(&@2, &@3);
1538 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1540 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1546 | expr keyword_and expr
1548 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1550 | expr keyword_or expr
1552 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1554 | keyword_not opt_nl expr
1556 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1560 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1565 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1566 p->command_start = FALSE;
1567 $<num>$ = p->in_kwarg;
1570 {$<tbl>$ = push_pvtbl(p);}
1572 {pop_pvtbl(p, $<tbl>4);}
1574 p->in_kwarg = !!$<num>3;
1576 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1578 {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;}
1580 | arg %prec tLBRACE_ARG
1590 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1596 command_call : command
1600 block_command : block_call
1601 | block_call call_op2 operation2 command_args
1604 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1606 {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;}
1610 cmd_brace_block : tLBRACE_ARG brace_body '}'
1614 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1615 nd_set_line($$, @1.end_pos.lineno);
1623 $$ = NEW_FCALL($1, 0, &@$);
1624 nd_set_line($$, p->tokline);
1630 command : fcall command_args %prec tLOWEST
1634 nd_set_last_loc($1, @2.end_pos);
1637 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1639 | fcall command_args cmd_brace_block
1642 block_dup_check(p, $2, $3);
1644 $$ = method_add_block(p, $1, $3, &@$);
1646 nd_set_last_loc($1, @2.end_pos);
1648 {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;}
1650 | primary_value call_op operation2 command_args %prec tLOWEST
1653 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1655 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1657 | primary_value call_op operation2 command_args cmd_brace_block
1660 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1662 {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;}
1664 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1667 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1669 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1671 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1674 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1676 {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;}
1678 | keyword_super command_args
1681 $$ = NEW_SUPER($2, &@$);
1684 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1686 | keyword_yield command_args
1689 $$ = new_yield(p, $2, &@$);
1692 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1694 | k_return call_args
1697 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1699 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1701 | keyword_break call_args
1704 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1706 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1708 | keyword_next call_args
1711 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1713 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1718 | tLPAREN mlhs_inner rparen
1723 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1727 mlhs_inner : mlhs_basic
1728 | tLPAREN mlhs_inner rparen
1731 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1733 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1737 mlhs_basic : mlhs_head
1740 $$ = NEW_MASGN($1, 0, &@$);
1744 | mlhs_head mlhs_item
1747 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1749 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1751 | mlhs_head tSTAR mlhs_node
1754 $$ = NEW_MASGN($1, $3, &@$);
1756 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1758 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1761 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1763 {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;}
1768 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1770 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1772 | mlhs_head tSTAR ',' mlhs_post
1775 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1777 {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;}
1782 $$ = NEW_MASGN(0, $2, &@$);
1784 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1786 | tSTAR mlhs_node ',' mlhs_post
1789 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1791 {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;}
1796 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1798 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1800 | tSTAR ',' mlhs_post
1803 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1805 {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;}
1809 mlhs_item : mlhs_node
1810 | tLPAREN mlhs_inner rparen
1815 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1819 mlhs_head : mlhs_item ','
1822 $$ = NEW_LIST($1, &@1);
1824 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1826 | mlhs_head mlhs_item ','
1829 $$ = list_append(p, $1, $2);
1831 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1835 mlhs_post : mlhs_item
1838 $$ = NEW_LIST($1, &@$);
1840 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1842 | mlhs_post ',' mlhs_item
1845 $$ = list_append(p, $1, $3);
1847 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1851 mlhs_node : user_variable
1854 $$ = assignable(p, $1, 0, &@$);
1856 $$=assignable(p, var_field(p, $1));
1861 $$ = assignable(p, $1, 0, &@$);
1863 $$=assignable(p, var_field(p, $1));
1865 | primary_value '[' opt_call_args rbracket
1868 $$ = aryset(p, $1, $3, &@$);
1870 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1872 | primary_value call_op tIDENTIFIER
1874 if ($2 == tANDDOT) {
1875 yyerror1(&@2, "&. inside multiple assignment destination");
1878 $$ = attrset(p, $1, $2, $3, &@$);
1880 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1882 | primary_value tCOLON2 tIDENTIFIER
1885 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1887 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1889 | primary_value call_op tCONSTANT
1891 if ($2 == tANDDOT) {
1892 yyerror1(&@2, "&. inside multiple assignment destination");
1895 $$ = attrset(p, $1, $2, $3, &@$);
1897 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1899 | primary_value tCOLON2 tCONSTANT
1902 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1904 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1909 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1911 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1916 rb_backref_error(p, $1);
1917 $$ = NEW_BEGIN(0, &@$);
1919 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1926 $$ = assignable(p, $1, 0, &@$);
1928 $$=assignable(p, var_field(p, $1));
1933 $$ = assignable(p, $1, 0, &@$);
1935 $$=assignable(p, var_field(p, $1));
1937 | primary_value '[' opt_call_args rbracket
1940 $$ = aryset(p, $1, $3, &@$);
1942 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1944 | primary_value call_op tIDENTIFIER
1947 $$ = attrset(p, $1, $2, $3, &@$);
1949 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1951 | primary_value tCOLON2 tIDENTIFIER
1954 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1956 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1958 | primary_value call_op tCONSTANT
1961 $$ = attrset(p, $1, $2, $3, &@$);
1963 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1965 | primary_value tCOLON2 tCONSTANT
1968 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1970 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1975 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1977 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1982 rb_backref_error(p, $1);
1983 $$ = NEW_BEGIN(0, &@$);
1985 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1992 yyerror1(&@1, "class/module name must be CONSTANT");
1994 {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
1999 cpath : tCOLON3 cname
2002 $$ = NEW_COLON3($2, &@$);
2004 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2009 $$ = NEW_COLON2(0, $$, &@$);
2011 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2013 | primary_value tCOLON2 cname
2016 $$ = NEW_COLON2($1, $3, &@$);
2018 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2027 SET_LEX_STATE(EXPR_ENDFN);
2032 SET_LEX_STATE(EXPR_ENDFN);
2040 $$ = NEW_LIT(ID2SYM($1), &@$);
2042 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2050 $$ = NEW_UNDEF($1, &@$);
2052 $$=rb_ary_new3(1, get_value($1));
2054 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2057 NODE *undef = NEW_UNDEF($4, &@4);
2058 $$ = block_append(p, $1, undef);
2060 $$=rb_ary_push($1, get_value($4));
2064 op : '|' { ifndef_ripper($$ = '|'); }
2065 | '^' { ifndef_ripper($$ = '^'); }
2066 | '&' { ifndef_ripper($$ = '&'); }
2067 | tCMP { ifndef_ripper($$ = tCMP); }
2068 | tEQ { ifndef_ripper($$ = tEQ); }
2069 | tEQQ { ifndef_ripper($$ = tEQQ); }
2070 | tMATCH { ifndef_ripper($$ = tMATCH); }
2071 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2072 | '>' { ifndef_ripper($$ = '>'); }
2073 | tGEQ { ifndef_ripper($$ = tGEQ); }
2074 | '<' { ifndef_ripper($$ = '<'); }
2075 | tLEQ { ifndef_ripper($$ = tLEQ); }
2076 | tNEQ { ifndef_ripper($$ = tNEQ); }
2077 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2078 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2079 | '+' { ifndef_ripper($$ = '+'); }
2080 | '-' { ifndef_ripper($$ = '-'); }
2081 | '*' { ifndef_ripper($$ = '*'); }
2082 | tSTAR { ifndef_ripper($$ = '*'); }
2083 | '/' { ifndef_ripper($$ = '/'); }
2084 | '%' { ifndef_ripper($$ = '%'); }
2085 | tPOW { ifndef_ripper($$ = tPOW); }
2086 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2087 | '!' { ifndef_ripper($$ = '!'); }
2088 | '~' { ifndef_ripper($$ = '~'); }
2089 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2090 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2091 | tAREF { ifndef_ripper($$ = tAREF); }
2092 | tASET { ifndef_ripper($$ = tASET); }
2093 | '`' { ifndef_ripper($$ = '`'); }
2096 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2097 | keyword_BEGIN | keyword_END
2098 | keyword_alias | keyword_and | keyword_begin
2099 | keyword_break | keyword_case | keyword_class | keyword_def
2100 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2101 | keyword_end | keyword_ensure | keyword_false
2102 | keyword_for | keyword_in | keyword_module | keyword_next
2103 | keyword_nil | keyword_not | keyword_or | keyword_redo
2104 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2105 | keyword_super | keyword_then | keyword_true | keyword_undef
2106 | keyword_when | keyword_yield | keyword_if | keyword_unless
2107 | keyword_while | keyword_until
2110 arg : lhs '=' arg_rhs
2113 $$ = node_assign(p, $1, $3, &@$);
2115 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2117 | var_lhs tOP_ASGN arg_rhs
2120 $$ = new_op_assign(p, $1, $2, $3, &@$);
2122 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2124 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2128 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2130 {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;}
2132 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2136 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2138 {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;}
2140 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2144 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2146 {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;}
2148 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2152 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2154 {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;}
2156 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2159 YYLTYPE loc = code_loc_gen(&@1, &@3);
2160 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2162 {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;}
2164 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2167 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2169 {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;}
2171 | backref tOP_ASGN arg_rhs
2174 rb_backref_error(p, $1);
2175 $$ = NEW_BEGIN(0, &@$);
2177 {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);
2184 $$ = NEW_DOT2($1, $3, &@$);
2186 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2193 $$ = NEW_DOT3($1, $3, &@$);
2195 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2201 loc.beg_pos = @2.end_pos;
2202 loc.end_pos = @2.end_pos;
2205 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2207 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2213 loc.beg_pos = @2.end_pos;
2214 loc.end_pos = @2.end_pos;
2217 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2219 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2225 loc.beg_pos = @1.beg_pos;
2226 loc.end_pos = @1.beg_pos;
2229 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2231 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2237 loc.beg_pos = @1.beg_pos;
2238 loc.end_pos = @1.beg_pos;
2241 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2243 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2247 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2251 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2255 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2259 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2263 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2267 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2269 | tUMINUS_NUM simple_numeric tPOW arg
2271 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2275 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2279 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2283 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2287 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2291 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2295 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2297 | rel_expr %prec tCMP
2300 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2304 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2308 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2312 $$ = match_op(p, $1, $3, &@2, &@$);
2316 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2320 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2324 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2328 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2332 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2336 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2340 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2342 | keyword_defined opt_nl {p->in_defined = 1;} arg
2345 $$ = new_defined(p, $4, &@$);
2347 | arg '?' arg opt_nl ':' arg
2351 $$ = new_if(p, $1, $3, $6, &@$);
2354 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2362 relop : '>' {$$ = '>';}
2368 rel_expr : arg relop arg %prec '>'
2370 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2372 | rel_expr relop arg %prec '>'
2374 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2375 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2391 | args ',' assocs trailer
2394 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2396 {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;}
2401 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2403 {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;}
2407 arg_rhs : arg %prec tOP_ASGN
2412 | arg modifier_rescue arg
2415 YYLTYPE loc = code_loc_gen(&@2, &@3);
2417 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2419 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2423 paren_args : '(' opt_call_args rparen
2428 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2430 | '(' args_forward rparen
2432 if (!local_id(p, idFWD_REST) ||
2434 !local_id(p, idFWD_KWREST) ||
2436 !local_id(p, idFWD_BLOCK)) {
2437 compile_error(p, "unexpected ...");
2442 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2444 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2446 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2448 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2452 $$ = arg_blk_pass($$, block);
2454 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2459 opt_paren_args : none
2463 opt_call_args : none
2469 | args ',' assocs ','
2472 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2474 {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;}
2479 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2481 {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;}
2489 $$ = NEW_LIST($1, &@$);
2491 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2493 | args opt_block_arg
2496 $$ = arg_blk_pass($1, $2);
2498 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2500 | assocs opt_block_arg
2503 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2504 $$ = arg_blk_pass($$, $2);
2506 {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;}
2508 | args ',' assocs opt_block_arg
2511 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2512 $$ = arg_blk_pass($$, $4);
2514 {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;}
2517 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2521 /* If call_args starts with a open paren '(' or '[',
2522 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2523 * but the push must be done after CMDARG_PUSH(1).
2524 * So this code makes them consistent by first cancelling
2525 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2526 * and finally redoing CMDARG_PUSH(0).
2530 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2533 if (lookahead) CMDARG_POP();
2535 if (lookahead) CMDARG_PUSH(0);
2539 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2540 * but the push must be done after CMDARG_POP() in the parser.
2541 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2542 * CMDARG_POP() to pop 1 pushed by command_args,
2543 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2550 if (lookahead) CMDARG_POP();
2552 if (lookahead) CMDARG_PUSH(0);
2557 block_arg : tAMPER arg_value
2560 $$ = NEW_BLOCK_PASS($2, &@$);
2566 opt_block_arg : ',' block_arg
2579 $$ = NEW_LIST($1, &@$);
2581 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2586 $$ = NEW_SPLAT($2, &@$);
2588 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2590 | args ',' arg_value
2593 $$ = last_arg_append(p, $1, $3, &@$);
2595 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2597 | args ',' tSTAR arg_value
2600 $$ = rest_arg_append(p, $1, $4, &@$);
2602 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2610 mrhs : args ',' arg_value
2613 $$ = last_arg_append(p, $1, $3, &@$);
2615 {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;}
2617 | args ',' tSTAR arg_value
2620 $$ = rest_arg_append(p, $1, $4, &@$);
2622 {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;}
2627 $$ = NEW_SPLAT($2, &@$);
2629 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2646 $$ = NEW_FCALL($1, 0, &@$);
2648 {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;}
2659 set_line_body($3, @1.end_pos.lineno);
2660 $$ = NEW_BEGIN($3, &@$);
2661 nd_set_line($$, @1.end_pos.lineno);
2663 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2665 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2668 $$ = NEW_BEGIN(0, &@$);
2670 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2672 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2675 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2678 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2680 | tLPAREN compstmt ')'
2683 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2686 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2688 | primary_value tCOLON2 tCONSTANT
2691 $$ = NEW_COLON2($1, $3, &@$);
2693 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2698 $$ = NEW_COLON3($2, &@$);
2700 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2702 | tLBRACK aref_args ']'
2705 $$ = make_list($2, &@$);
2707 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2709 | tLBRACE assoc_list '}'
2712 $$ = new_hash(p, $2, &@$);
2713 $$->nd_brace = TRUE;
2715 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2720 $$ = NEW_RETURN(0, &@$);
2722 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2724 | keyword_yield '(' call_args rparen
2727 $$ = new_yield(p, $3, &@$);
2729 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2731 | keyword_yield '(' rparen
2734 $$ = NEW_YIELD(0, &@$);
2736 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2741 $$ = NEW_YIELD(0, &@$);
2743 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2745 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2748 $$ = new_defined(p, $5, &@$);
2750 | keyword_not '(' expr rparen
2752 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2754 | keyword_not '(' rparen
2756 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2761 $$ = method_add_block(p, $1, $2, &@$);
2763 {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;}
2766 | method_call brace_block
2769 block_dup_check(p, $1->nd_args, $2);
2770 $$ = method_add_block(p, $1, $2, &@$);
2772 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2776 token_info_push(p, "->", &@1);
2782 nd_set_first_loc($$, @1.beg_pos);
2785 | k_if expr_value then
2791 $$ = new_if(p, $2, $4, $5, &@$);
2794 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2796 | k_unless expr_value then
2802 $$ = new_unless(p, $2, $4, $5, &@$);
2805 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2807 | k_while expr_value_do
2812 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2815 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2817 | k_until expr_value_do
2822 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2825 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2827 | k_case expr_value opt_terms
2829 $<val>$ = p->case_labels;
2830 p->case_labels = Qnil;
2835 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2836 p->case_labels = $<val>4;
2838 $$ = NEW_CASE($2, $5, &@$);
2841 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2845 $<val>$ = p->case_labels;
2851 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2852 p->case_labels = $<val>3;
2854 $$ = NEW_CASE2($4, &@$);
2856 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2858 | k_case expr_value opt_terms
2863 $$ = new_case3(p, $2, $4, &@$);
2865 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2867 | k_for for_var keyword_in expr_value_do
2875 * e.each{|*x| a, b, c = x}
2879 * e.each{|x| a, = x}
2881 ID id = internal_id(p);
2882 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2883 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2884 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2885 ID *tbl = ALLOC_N(ID, 3);
2886 rb_imemo_tmpbuf_set_ptr(tmpbuf, tbl);
2887 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2890 switch (nd_type($2)) {
2893 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2894 $2->nd_value = internal_var;
2899 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2900 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2902 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2903 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2905 /* {|*internal_id| <m> = internal_id; ... } */
2906 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2907 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2908 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
2909 $$ = NEW_FOR($4, scope, &@$);
2912 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2914 | k_class cpath superclass
2917 YYLTYPE loc = code_loc_gen(&@1, &@2);
2918 yyerror1(&loc, "class definition in method body");
2920 $<num>1 = p->in_class;
2928 $$ = NEW_CLASS($2, $5, $3, &@$);
2929 nd_set_line($$->nd_body, @6.end_pos.lineno);
2930 set_line_body($5, @3.end_pos.lineno);
2931 nd_set_line($$, @3.end_pos.lineno);
2933 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2935 p->in_class = $<num>1 & 1;
2937 | k_class tLSHFT expr
2939 $<num>$ = (p->in_class << 1) | p->in_def;
2949 $$ = NEW_SCLASS($3, $6, &@$);
2950 nd_set_line($$->nd_body, @7.end_pos.lineno);
2951 set_line_body($6, nd_line($3));
2954 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2956 p->in_def = $<num>4 & 1;
2957 p->in_class = ($<num>4 >> 1) & 1;
2962 YYLTYPE loc = code_loc_gen(&@1, &@2);
2963 yyerror1(&loc, "module definition in method body");
2965 $<num>1 = p->in_class;
2973 $$ = NEW_MODULE($2, $4, &@$);
2974 nd_set_line($$->nd_body, @5.end_pos.lineno);
2975 set_line_body($4, @2.end_pos.lineno);
2976 nd_set_line($$, @2.end_pos.lineno);
2978 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
2980 p->in_class = $<num>1 & 1;
2984 numparam_name(p, get_id($2));
2986 $<id>$ = p->cur_arg;
2990 $<num>$ = p->in_def;
2998 NODE *body = remove_begin($6);
2999 reduce_nodes(p, &body);
3000 $$ = NEW_DEFN($2, $5, body, &@$);
3001 nd_set_line($$->nd_defn, @7.end_pos.lineno);
3002 set_line_body(body, @1.beg_pos.lineno);
3004 {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
3006 p->in_def = $<num>4 & 1;
3007 p->cur_arg = $<id>3;
3009 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3011 numparam_name(p, get_id($5));
3012 $<num>4 = p->in_def;
3014 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3016 $<id>$ = p->cur_arg;
3024 NODE *body = remove_begin($8);
3025 reduce_nodes(p, &body);
3026 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3027 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3028 set_line_body(body, @1.beg_pos.lineno);
3030 {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;}
3032 p->in_def = $<num>4 & 1;
3033 p->cur_arg = $<id>6;
3038 $$ = NEW_BREAK(0, &@$);
3040 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3045 $$ = NEW_NEXT(0, &@$);
3047 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3054 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3059 $$ = NEW_RETRY(&@$);
3061 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3065 primary_value : primary
3072 k_begin : keyword_begin
3074 token_info_push(p, "begin", &@$);
3081 token_info_push(p, "if", &@$);
3082 if (p->token_info && p->token_info->nonspc &&
3083 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3084 const char *tok = p->lex.ptok;
3085 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3086 beg += rb_strlen_lit("else");
3087 while (beg < tok && ISSPACE(*beg)) beg++;
3089 p->token_info->nonspc = 0;
3095 k_unless : keyword_unless
3097 token_info_push(p, "unless", &@$);
3101 k_while : keyword_while
3103 token_info_push(p, "while", &@$);
3107 k_until : keyword_until
3109 token_info_push(p, "until", &@$);
3113 k_case : keyword_case
3115 token_info_push(p, "case", &@$);
3121 token_info_push(p, "for", &@$);
3125 k_class : keyword_class
3127 token_info_push(p, "class", &@$);
3131 k_module : keyword_module
3133 token_info_push(p, "module", &@$);
3139 token_info_push(p, "def", &@$);
3145 token_info_push(p, "do", &@$);
3149 k_do_block : keyword_do_block
3151 token_info_push(p, "do", &@$);
3155 k_rescue : keyword_rescue
3157 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3161 k_ensure : keyword_ensure
3163 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3167 k_when : keyword_when
3169 token_info_warn(p, "when", p->token_info, 0, &@$);
3173 k_else : keyword_else
3175 token_info *ptinfo_beg = p->token_info;
3176 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3177 token_info_warn(p, "else", p->token_info, same, &@$);
3180 e.next = ptinfo_beg->next;
3182 token_info_setup(&e, p->lex.pbeg, &@$);
3183 if (!e.nonspc) *ptinfo_beg = e;
3188 k_elsif : keyword_elsif
3191 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3197 token_info_pop(p, "end", &@$);
3201 k_return : keyword_return
3203 if (p->in_class && !p->in_def && !dyna_in_block(p))
3204 yyerror1(&@1, "Invalid return in class/module body");
3218 | k_elsif expr_value then
3223 $$ = new_if(p, $2, $4, $5, &@$);
3226 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3236 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3247 $$ = assignable(p, $1, 0, &@$);
3248 mark_lvar_used(p, $$);
3250 $$=assignable(p, $1);
3252 | tLPAREN f_margs rparen
3257 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3261 f_marg_list : f_marg
3264 $$ = NEW_LIST($1, &@$);
3266 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3268 | f_marg_list ',' f_marg
3271 $$ = list_append(p, $1, $3);
3273 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3277 f_margs : f_marg_list
3280 $$ = NEW_MASGN($1, 0, &@$);
3284 | f_marg_list ',' f_rest_marg
3287 $$ = NEW_MASGN($1, $3, &@$);
3289 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3291 | f_marg_list ',' f_rest_marg ',' f_marg_list
3294 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3296 {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;}
3301 $$ = NEW_MASGN(0, $1, &@$);
3303 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3305 | f_rest_marg ',' f_marg_list
3308 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3310 {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;}
3314 f_rest_marg : tSTAR f_norm_arg
3317 $$ = assignable(p, $2, 0, &@$);
3318 mark_lvar_used(p, $$);
3320 $$=assignable(p, $2);
3325 $$ = NODE_SPECIAL_NO_NAME_REST;
3331 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3333 $$ = new_args_tail(p, $1, $3, $4, &@3);
3335 | f_block_kwarg opt_f_block_arg
3337 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3339 | f_kwrest opt_f_block_arg
3341 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3343 | f_no_kwarg opt_f_block_arg
3345 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3349 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3353 opt_block_args_tail : ',' block_args_tail
3359 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3363 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3365 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3367 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3369 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3371 | f_arg ',' f_block_optarg opt_block_args_tail
3373 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3375 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3377 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3379 | f_arg ',' f_rest_arg opt_block_args_tail
3381 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3386 /* magic number for rest_id in iseq_set_arguments() */
3387 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3389 {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3391 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3393 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3395 | f_arg opt_block_args_tail
3397 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3399 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3401 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3403 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3405 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3407 | f_block_optarg opt_block_args_tail
3409 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3411 | f_block_optarg ',' f_arg opt_block_args_tail
3413 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3415 | f_rest_arg opt_block_args_tail
3417 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3419 | f_rest_arg ',' f_arg opt_block_args_tail
3421 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3425 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3429 opt_block_param : none
3432 p->command_start = TRUE;
3436 block_param_def : '|' opt_bv_decl '|'
3439 p->max_numparam = ORDINAL_PARAM;
3443 {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;}
3445 | '|' block_param opt_bv_decl '|'
3448 p->max_numparam = ORDINAL_PARAM;
3452 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3457 opt_bv_decl : opt_nl
3461 | opt_nl ';' bv_decls opt_nl
3471 {$$=rb_ary_new3(1, get_value($1));}
3473 {$$=rb_ary_push($1, get_value($3));}
3478 new_bv(p, get_id($1));
3488 $<vars>$ = dyna_push(p);
3491 $<num>$ = p->lex.lpar_beg;
3492 p->lex.lpar_beg = p->lex.paren_nest;
3495 $<num>$ = p->max_numparam;
3496 p->max_numparam = 0;
3499 $<node>$ = numparam_push(p);
3507 int max_numparam = p->max_numparam;
3508 p->lex.lpar_beg = $<num>2;
3509 p->max_numparam = $<num>3;
3511 $5 = args_with_numbered(p, $5, max_numparam);
3514 YYLTYPE loc = code_loc_gen(&@5, &@7);
3515 $$ = NEW_LAMBDA($5, $7, &loc);
3516 nd_set_line($$->nd_body, @7.end_pos.lineno);
3517 nd_set_line($$, @5.end_pos.lineno);
3520 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3521 numparam_pop(p, $<node>4);
3522 dyna_pop(p, $<vars>1);
3526 f_larglist : '(' f_args opt_bv_decl ')'
3530 p->max_numparam = ORDINAL_PARAM;
3532 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3537 if (!args_info_empty_p($1->nd_ainfo))
3538 p->max_numparam = ORDINAL_PARAM;
3544 lambda_body : tLAMBEG compstmt '}'
3546 token_info_pop(p, "}", &@3);
3549 | keyword_do_LAMBDA bodystmt k_end
3555 do_block : k_do_block do_body k_end
3559 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3560 nd_set_line($$, @1.end_pos.lineno);
3565 block_call : command do_block
3568 if (nd_type($1) == NODE_YIELD) {
3569 compile_error(p, "block given to yield");
3572 block_dup_check(p, $1->nd_args, $2);
3574 $$ = method_add_block(p, $1, $2, &@$);
3577 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3579 | block_call call_op2 operation2 opt_paren_args
3582 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3584 {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;}
3586 | block_call call_op2 operation2 opt_paren_args brace_block
3589 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3591 {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;}
3593 | block_call call_op2 operation2 command_args do_block
3596 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3598 {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;}
3602 method_call : fcall paren_args
3607 nd_set_last_loc($1, @2.end_pos);
3609 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3611 | primary_value call_op operation2 opt_paren_args
3614 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3615 nd_set_line($$, @3.end_pos.lineno);
3617 {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;}
3619 | primary_value tCOLON2 operation2 paren_args
3622 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3623 nd_set_line($$, @3.end_pos.lineno);
3625 {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;}
3627 | primary_value tCOLON2 operation3
3630 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3632 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3634 | primary_value call_op paren_args
3637 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3638 nd_set_line($$, @2.end_pos.lineno);
3640 {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;}
3642 | primary_value tCOLON2 paren_args
3645 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3646 nd_set_line($$, @2.end_pos.lineno);
3648 {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;}
3650 | keyword_super paren_args
3653 $$ = NEW_SUPER($2, &@$);
3655 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3660 $$ = NEW_ZSUPER(&@$);
3662 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3664 | primary_value '[' opt_call_args rbracket
3667 if ($1 && nd_type($1) == NODE_SELF)
3668 $$ = NEW_FCALL(tAREF, $3, &@$);
3670 $$ = NEW_CALL($1, tAREF, $3, &@$);
3673 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3677 brace_block : '{' brace_body '}'
3681 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3682 nd_set_line($$, @1.end_pos.lineno);
3685 | k_do do_body k_end
3689 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3690 nd_set_line($$, @1.end_pos.lineno);
3695 brace_body : {$<vars>$ = dyna_push(p);}
3697 $<num>$ = p->max_numparam;
3698 p->max_numparam = 0;
3701 $<node>$ = numparam_push(p);
3703 opt_block_param compstmt
3705 int max_numparam = p->max_numparam;
3706 p->max_numparam = $<num>2;
3707 $4 = args_with_numbered(p, $4, max_numparam);
3709 $$ = NEW_ITER($4, $5, &@$);
3711 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3712 numparam_pop(p, $<node>3);
3713 dyna_pop(p, $<vars>1);
3717 do_body : {$<vars>$ = dyna_push(p);}
3719 $<num>$ = p->max_numparam;
3720 p->max_numparam = 0;
3723 $<node>$ = numparam_push(p);
3726 opt_block_param bodystmt
3728 int max_numparam = p->max_numparam;
3729 p->max_numparam = $<num>2;
3730 $4 = args_with_numbered(p, $4, max_numparam);
3732 $$ = NEW_ITER($4, $5, &@$);
3734 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3736 numparam_pop(p, $<node>3);
3737 dyna_pop(p, $<vars>1);
3741 case_args : arg_value
3744 check_literal_when(p, $1, &@1);
3745 $$ = NEW_LIST($1, &@$);
3747 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3752 $$ = NEW_SPLAT($2, &@$);
3754 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3756 | case_args ',' arg_value
3759 check_literal_when(p, $3, &@3);
3760 $$ = last_arg_append(p, $1, $3, &@$);
3762 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3764 | case_args ',' tSTAR arg_value
3767 $$ = rest_arg_append(p, $1, $4, &@$);
3769 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3773 case_body : k_when case_args then
3778 $$ = NEW_WHEN($2, $4, $5, &@$);
3781 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3789 p_case_body : keyword_in
3791 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3792 p->command_start = FALSE;
3793 $<num>$ = p->in_kwarg;
3796 {$<tbl>$ = push_pvtbl(p);}
3797 {$<tbl>$ = push_pktbl(p);}
3799 {pop_pktbl(p, $<tbl>4);}
3800 {pop_pvtbl(p, $<tbl>3);}
3802 p->in_kwarg = !!$<num>2;
3808 $$ = NEW_IN($5, $10, $11, &@$);
3810 {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3818 p_top_expr : p_top_expr_body
3819 | p_top_expr_body modifier_if expr_value
3822 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3825 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3827 | p_top_expr_body modifier_unless expr_value
3830 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3833 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3837 p_top_expr_body : p_expr
3840 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3841 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3845 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3847 nd_set_first_loc($$, @1.beg_pos);
3853 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3857 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3864 p_as : p_expr tASSOC p_variable
3867 NODE *n = NEW_LIST($1, &@$);
3868 n = list_append(p, n, $3);
3869 $$ = new_hash(p, n, &@$);
3871 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3876 p_alt : p_alt '|' p_expr_basic
3879 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3881 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3886 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3887 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3889 p_expr_basic : p_value
3890 | p_const p_lparen p_args rparen
3892 pop_pktbl(p, $<tbl>2);
3893 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3895 nd_set_first_loc($$, @1.beg_pos);
3899 | p_const p_lparen p_kwargs rparen
3901 pop_pktbl(p, $<tbl>2);
3902 $$ = new_hash_pattern(p, $1, $3, &@$);
3904 nd_set_first_loc($$, @1.beg_pos);
3908 | p_const '(' rparen
3910 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3911 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3913 | p_const p_lbracket p_args rbracket
3915 pop_pktbl(p, $<tbl>2);
3916 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3918 nd_set_first_loc($$, @1.beg_pos);
3922 | p_const p_lbracket p_kwargs rbracket
3924 pop_pktbl(p, $<tbl>2);
3925 $$ = new_hash_pattern(p, $1, $3, &@$);
3927 nd_set_first_loc($$, @1.beg_pos);
3931 | p_const '[' rbracket
3933 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3934 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3936 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3938 pop_pktbl(p, $<tbl>2);
3939 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3943 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3944 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3946 | tLBRACE {$<tbl>$ = push_pktbl(p);} p_kwargs '}'
3948 pop_pktbl(p, $<tbl>2);
3949 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3953 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3954 $$ = new_hash_pattern(p, Qnone, $$, &@$);
3956 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3958 pop_pktbl(p, $<tbl>2);
3966 NODE *pre_args = NEW_LIST($1, &@$);
3967 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3969 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3974 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3979 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
3981 VALUE pre_args = rb_ary_concat($1, get_value($2));
3982 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3985 | p_args_head tSTAR tIDENTIFIER
3987 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
3989 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
3991 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
3995 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3997 | p_args_head tSTAR ',' p_args_post
3999 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4004 p_args_head : p_arg ','
4008 | p_args_head p_arg ','
4011 $$ = list_concat($1, $2);
4013 $$=rb_ary_concat($1, get_value($2));
4017 p_args_tail : tSTAR tIDENTIFIER
4019 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4021 | tSTAR tIDENTIFIER ',' p_args_post
4023 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4027 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4029 | tSTAR ',' p_args_post
4031 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4036 | p_args_post ',' p_arg
4039 $$ = list_concat($1, $3);
4041 $$=rb_ary_concat($1, get_value($3));
4048 $$ = NEW_LIST($1, &@$);
4050 $$=rb_ary_new_from_args(1, get_value($1));
4054 p_kwargs : p_kwarg ',' p_kwrest
4056 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4060 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4064 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4066 | p_kwarg ',' p_kwnorest
4068 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4072 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4077 {$$=rb_ary_new_from_args(1, $1);}
4081 $$ = list_concat($1, $3);
4083 $$=rb_ary_push($1, $3);
4087 p_kw : p_kw_label p_expr
4089 error_duplicate_pattern_key(p, get_id($1), &@1);
4091 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4093 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4097 error_duplicate_pattern_key(p, get_id($1), &@1);
4098 if ($1 && !is_local_id(get_id($1))) {
4099 yyerror1(&@1, "key must be valid as local variables");
4101 error_duplicate_pattern_variable(p, get_id($1), &@1);
4103 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4105 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4110 | tSTRING_BEG string_contents tLABEL_END
4112 YYLTYPE loc = code_loc_gen(&@1, &@3);
4114 if (!$2 || nd_type($2) == NODE_STR) {
4115 NODE *node = dsym_node(p, $2, &loc);
4116 $$ = SYM2ID(node->nd_lit);
4119 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4120 VALUE label = RNODE($2)->nd_cval;
4121 VALUE rval = RNODE($2)->nd_rval;
4122 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4123 RNODE($$)->nd_loc = loc;
4127 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4133 p_kwrest : kwrest_mark tIDENTIFIER
4143 p_kwnorest : kwrest_mark keyword_nil
4149 p_value : p_primitive
4150 | p_primitive tDOT2 p_primitive
4155 $$ = NEW_DOT2($1, $3, &@$);
4157 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4159 | p_primitive tDOT3 p_primitive
4164 $$ = NEW_DOT3($1, $3, &@$);
4166 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4172 loc.beg_pos = @2.end_pos;
4173 loc.end_pos = @2.end_pos;
4176 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4178 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4184 loc.beg_pos = @2.end_pos;
4185 loc.end_pos = @2.end_pos;
4188 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4190 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4195 | tBDOT2 p_primitive
4199 loc.beg_pos = @1.beg_pos;
4200 loc.end_pos = @1.beg_pos;
4203 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4205 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4207 | tBDOT3 p_primitive
4211 loc.beg_pos = @1.beg_pos;
4212 loc.end_pos = @1.beg_pos;
4215 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4217 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4221 p_primitive : literal
4232 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4234 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4238 token_info_push(p, "->", &@1);
4244 nd_set_first_loc($$, @1.beg_pos);
4249 p_variable : tIDENTIFIER
4252 error_duplicate_pattern_variable(p, $1, &@1);
4253 $$ = assignable(p, $1, 0, &@$);
4255 $$=assignable(p, var_field(p, $1));
4259 p_var_ref : '^' tIDENTIFIER
4262 NODE *n = gettable(p, $2, &@$);
4263 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4264 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4268 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4272 p_const : tCOLON3 cname
4275 $$ = NEW_COLON3($2, &@$);
4277 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4279 | p_const tCOLON2 cname
4282 $$ = NEW_COLON2($1, $3, &@$);
4284 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4289 $$ = gettable(p, $1, &@$);
4291 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4295 opt_rescue : k_rescue exc_list exc_var then
4300 $$ = NEW_RESBODY($2,
4301 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4303 fixpos($$, $2?$2:$5);
4305 {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;}
4310 exc_list : arg_value
4313 $$ = NEW_LIST($1, &@$);
4315 $$=rb_ary_new3(1, get_value($1));
4320 if (!($$ = splat_array($1))) $$ = $1;
4327 exc_var : tASSOC lhs
4334 opt_ensure : k_ensure compstmt
4339 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4353 node = NEW_STR(STR_NEW0(), &@$);
4354 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4357 node = evstr2dstr(p, node);
4370 $$ = literal_concat(p, $1, $2, &@$);
4372 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4376 string1 : tSTRING_BEG string_contents tSTRING_END
4379 $$ = heredoc_dedent(p, $2);
4380 if ($$) nd_set_loc($$, &@$);
4382 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4386 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4389 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4391 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4395 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4397 $$ = new_regexp(p, $2, $3, &@$);
4401 words : tWORDS_BEG ' ' word_list tSTRING_END
4404 $$ = make_list($3, &@$);
4406 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4410 word_list : /* none */
4415 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4417 | word_list word ' '
4420 $$ = list_append(p, $1, evstr2dstr(p, $2));
4422 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4426 word : string_content
4427 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4428 | word string_content
4431 $$ = literal_concat(p, $1, $2, &@$);
4433 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4437 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4440 $$ = make_list($3, &@$);
4442 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4446 symbol_list : /* none */
4451 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4453 | symbol_list word ' '
4456 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4458 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4462 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4465 $$ = make_list($3, &@$);
4467 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4471 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4474 $$ = make_list($3, &@$);
4476 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4480 qword_list : /* none */
4485 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4487 | qword_list tSTRING_CONTENT ' '
4490 $$ = list_append(p, $1, $2);
4492 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4496 qsym_list : /* none */
4501 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4503 | qsym_list tSTRING_CONTENT ' '
4506 $$ = symbol_append(p, $1, $2);
4508 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4512 string_contents : /* none */
4517 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4520 $$ = ripper_new_yylval(p, 0, $$, 0);
4523 | string_contents string_content
4526 $$ = literal_concat(p, $1, $2, &@$);
4528 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4531 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4532 !RNODE($1)->nd_cval) {
4533 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4534 RNODE($1)->nd_rval = add_mark_object(p, $$);
4541 xstring_contents: /* none */
4546 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4548 | xstring_contents string_content
4551 $$ = literal_concat(p, $1, $2, &@$);
4553 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4557 regexp_contents: /* none */
4562 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4565 $$ = ripper_new_yylval(p, 0, $$, 0);
4568 | regexp_contents string_content
4571 NODE *head = $1, *tail = $2;
4579 switch (nd_type(head)) {
4581 nd_set_type(head, NODE_DSTR);
4586 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4589 $$ = list_append(p, head, tail);
4592 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4593 if (ripper_is_node_yylval(n1)) {
4594 s1 = RNODE(n1)->nd_cval;
4595 n1 = RNODE(n1)->nd_rval;
4597 if (ripper_is_node_yylval(n2)) {
4598 s2 = RNODE(n2)->nd_cval;
4599 n2 = RNODE(n2)->nd_rval;
4601 $$ = dispatch2(regexp_add, n1, n2);
4603 $$ = ripper_new_yylval(p, 0, $$, s2);
4609 string_content : tSTRING_CONTENT
4610 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4613 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4614 $<strterm>$ = p->lex.strterm;
4616 SET_LEX_STATE(EXPR_BEG);
4620 p->lex.strterm = $<strterm>2;
4622 $$ = NEW_EVSTR($3, &@$);
4623 nd_set_line($$, @3.end_pos.lineno);
4625 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4633 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4634 $<strterm>$ = p->lex.strterm;
4638 $<num>$ = p->lex.state;
4639 SET_LEX_STATE(EXPR_BEG);
4642 $<num>$ = p->lex.brace_nest;
4643 p->lex.brace_nest = 0;
4646 $<num>$ = p->heredoc_indent;
4647 p->heredoc_indent = 0;
4649 compstmt tSTRING_DEND
4653 p->lex.strterm = $<strterm>3;
4654 SET_LEX_STATE($<num>4);
4655 p->lex.brace_nest = $<num>5;
4656 p->heredoc_indent = $<num>6;
4657 p->heredoc_line_indent = -1;
4659 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4660 $$ = new_evstr(p, $7, &@$);
4662 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4669 $$ = NEW_GVAR($1, &@$);
4671 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4676 $$ = NEW_IVAR($1, &@$);
4678 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4683 $$ = NEW_CVAR($1, &@$);
4685 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4696 SET_LEX_STATE(EXPR_END);
4698 $$ = NEW_LIT(ID2SYM($2), &@$);
4700 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4710 dsym : tSYMBEG string_contents tSTRING_END
4712 SET_LEX_STATE(EXPR_END);
4714 $$ = dsym_node(p, $2, &@$);
4716 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4720 numeric : simple_numeric
4721 | tUMINUS_NUM simple_numeric %prec tLOWEST
4725 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4727 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4731 simple_numeric : tINTEGER
4737 user_variable : tIDENTIFIER
4744 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4745 | keyword_self {$$ = KWD2EID(self, $1);}
4746 | keyword_true {$$ = KWD2EID(true, $1);}
4747 | keyword_false {$$ = KWD2EID(false, $1);}
4748 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4749 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4750 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4753 var_ref : user_variable
4756 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4758 if (id_is_var(p, get_id($1))) {
4759 $$ = dispatch1(var_ref, $1);
4762 $$ = dispatch1(vcall, $1);
4769 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4771 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4775 var_lhs : user_variable
4778 $$ = assignable(p, $1, 0, &@$);
4780 $$=assignable(p, var_field(p, $1));
4785 $$ = assignable(p, $1, 0, &@$);
4787 $$=assignable(p, var_field(p, $1));
4797 SET_LEX_STATE(EXPR_BEG);
4798 p->command_start = TRUE;
4813 f_arglist : '(' f_args rparen
4818 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4819 SET_LEX_STATE(EXPR_BEG);
4820 p->command_start = TRUE;
4822 | '(' args_forward rparen
4824 arg_var(p, idFWD_REST);
4826 arg_var(p, idFWD_KWREST);
4828 arg_var(p, idFWD_BLOCK);
4830 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4831 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4833 {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4834 SET_LEX_STATE(EXPR_BEG);
4835 p->command_start = TRUE;
4838 $<num>$ = p->in_kwarg;
4840 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4844 p->in_kwarg = !!$<num>1;
4846 SET_LEX_STATE(EXPR_BEG);
4847 p->command_start = TRUE;
4851 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4853 $$ = new_args_tail(p, $1, $3, $4, &@3);
4855 | f_kwarg opt_f_block_arg
4857 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4859 | f_kwrest opt_f_block_arg
4861 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4863 | f_no_kwarg opt_f_block_arg
4865 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4869 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4873 opt_args_tail : ',' args_tail
4879 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4883 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4885 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4887 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4889 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4891 | f_arg ',' f_optarg opt_args_tail
4893 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4895 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4897 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4899 | f_arg ',' f_rest_arg opt_args_tail
4901 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4903 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4905 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4907 | f_arg opt_args_tail
4909 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4911 | f_optarg ',' f_rest_arg opt_args_tail
4913 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4915 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4917 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4919 | f_optarg opt_args_tail
4921 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4923 | f_optarg ',' f_arg opt_args_tail
4925 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4927 | f_rest_arg opt_args_tail
4929 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4931 | f_rest_arg ',' f_arg opt_args_tail
4933 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4937 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4941 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4942 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4946 args_forward : tBDOT3
4951 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
4955 f_bad_arg : tCONSTANT
4958 yyerror1(&@1, "formal argument cannot be a constant");
4961 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4966 yyerror1(&@1, "formal argument cannot be an instance variable");
4969 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4974 yyerror1(&@1, "formal argument cannot be a global variable");
4977 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4982 yyerror1(&@1, "formal argument cannot be a class variable");
4985 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
4989 f_norm_arg : f_bad_arg
4992 formal_argument(p, get_id($1));
4993 p->max_numparam = ORDINAL_PARAM;
4998 f_arg_asgn : f_norm_arg
5007 f_arg_item : f_arg_asgn
5011 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5015 | tLPAREN f_margs rparen
5018 ID tid = internal_id(p);
5020 loc.beg_pos = @2.beg_pos;
5021 loc.end_pos = @2.beg_pos;
5023 if (dyna_in_block(p)) {
5024 $2->nd_value = NEW_DVAR(tid, &loc);
5027 $2->nd_value = NEW_LVAR(tid, &loc);
5029 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5032 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5037 {$$=rb_ary_new3(1, get_value($1));}
5038 | f_arg ',' f_arg_item
5043 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5044 rb_discard_node(p, $3);
5046 $$=rb_ary_push($1, get_value($3));
5054 arg_var(p, formal_argument(p, id));
5056 p->max_numparam = ORDINAL_PARAM;
5061 f_kw : f_label arg_value
5065 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5067 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5073 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5075 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5079 f_block_kw : f_label primary_value
5082 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5084 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5089 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5091 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5095 f_block_kwarg : f_block_kw
5100 $$=rb_ary_new3(1, get_value($1));
5102 | f_block_kwarg ',' f_block_kw
5105 $$ = kwd_append($1, $3);
5107 $$=rb_ary_push($1, get_value($3));
5117 $$=rb_ary_new3(1, get_value($1));
5122 $$ = kwd_append($1, $3);
5124 $$=rb_ary_push($1, get_value($3));
5132 f_no_kwarg : kwrest_mark keyword_nil
5136 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5140 f_kwrest : kwrest_mark tIDENTIFIER
5142 arg_var(p, shadowing_lvar(p, get_id($2)));
5146 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5151 $$ = internal_id(p);
5154 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5158 f_opt : f_arg_asgn '=' arg_value
5162 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5164 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5168 f_block_opt : f_arg_asgn '=' primary_value
5172 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5174 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5178 f_block_optarg : f_block_opt
5183 $$=rb_ary_new3(1, get_value($1));
5185 | f_block_optarg ',' f_block_opt
5188 $$ = opt_arg_append($1, $3);
5190 $$=rb_ary_push($1, get_value($3));
5199 $$=rb_ary_new3(1, get_value($1));
5201 | f_optarg ',' f_opt
5204 $$ = opt_arg_append($1, $3);
5206 $$=rb_ary_push($1, get_value($3));
5214 f_rest_arg : restarg_mark tIDENTIFIER
5216 arg_var(p, shadowing_lvar(p, get_id($2)));
5220 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5225 $$ = internal_id(p);
5228 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5236 f_block_arg : blkarg_mark tIDENTIFIER
5238 arg_var(p, shadowing_lvar(p, get_id($2)));
5242 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5246 opt_f_block_arg : ',' f_block_arg
5261 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5264 switch (nd_type($3)) {
5273 yyerror1(&@3, "can't define singleton method for literals");
5281 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5291 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5296 {$$=rb_ary_new3(1, get_value($1));}
5306 if (assocs->nd_head &&
5307 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5308 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5310 tail = tail->nd_next->nd_head->nd_head;
5312 assocs = list_concat(assocs, tail);
5316 $$=rb_ary_push($1, get_value($3));
5320 assoc : arg_value tASSOC arg_value
5323 if (nd_type($1) == NODE_STR) {
5324 nd_set_type($1, NODE_LIT);
5325 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5327 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5329 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5334 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5336 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5338 | tSTRING_BEG string_contents tLABEL_END arg_value
5341 YYLTYPE loc = code_loc_gen(&@1, &@3);
5342 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5344 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5349 if (nd_type($2) == NODE_HASH &&
5350 !($2->nd_head && $2->nd_head->nd_alen)) {
5351 static VALUE empty_hash;
5353 empty_hash = rb_obj_freeze(rb_hash_new());
5354 rb_gc_register_mark_object(empty_hash);
5356 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5359 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5361 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5365 operation : tIDENTIFIER
5370 operation2 : tIDENTIFIER
5376 operation3 : tIDENTIFIER
5393 opt_terms : /* none */
5404 rbracket : opt_nl ']'
5407 trailer : /* none */
5412 term : ';' {yyerrok;token_flush(p);}
5413 | '\n' {token_flush(p);}
5417 | terms ';' {yyerrok;}
5429 # define yylval (*p->lval)
5431 static int regx_options(struct parser_params*);
5432 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5433 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5434 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5435 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5438 # define set_yylval_node(x) { \
5440 rb_parser_set_location(p, &_cur_loc); \
5441 yylval.node = (x); \
5443 # define set_yylval_str(x) \
5445 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5446 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5448 # define set_yylval_literal(x) \
5450 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5451 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5453 # define set_yylval_num(x) (yylval.num = (x))
5454 # define set_yylval_id(x) (yylval.id = (x))
5455 # define set_yylval_name(x) (yylval.id = (x))
5456 # define yylval_id() (yylval.id)
5459 ripper_yylval_id(struct parser_params *p, ID x)
5461 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5463 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5464 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5465 # define set_yylval_id(x) (void)(x)
5466 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5467 # define set_yylval_literal(x) add_mark_object(p, (x))
5468 # define set_yylval_node(x) (void)(x)
5469 # define yylval_id() yylval.id
5470 # define _cur_loc NULL_LOC /* dummy */
5473 #define set_yylval_noname() set_yylval_id(keyword_nil)
5476 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5477 #define dispatch_scan_event(p, t) ((void)0)
5478 #define dispatch_delayed_token(p, t) ((void)0)
5479 #define has_delayed_token(p) (0)
5481 #define literal_flush(p, ptr) ((void)(ptr))
5483 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5486 intern_sym(const char *name)
5488 ID id = rb_intern_const(name);
5493 ripper_has_scan_event(struct parser_params *p)
5495 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5496 return p->lex.pcur > p->lex.ptok;
5500 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5502 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5503 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5509 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5511 if (!ripper_has_scan_event(p)) return;
5512 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5514 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5517 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5519 int saved_line = p->ruby_sourceline;
5520 const char *saved_tokp = p->lex.ptok;
5522 if (NIL_P(p->delayed.token)) return;
5523 p->ruby_sourceline = p->delayed.line;
5524 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5525 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5526 p->delayed.token = Qnil;
5527 p->ruby_sourceline = saved_line;
5528 p->lex.ptok = saved_tokp;
5530 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5531 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5534 #include "ruby/regex.h"
5535 #include "ruby/util.h"
5538 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5540 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5544 parser_is_identchar(struct parser_params *p)
5546 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5550 parser_isascii(struct parser_params *p)
5552 return ISASCII(*(p->lex.pcur-1));
5556 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5558 int column = 1, nonspc = 0, i;
5559 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5561 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5564 if (*ptr != ' ' && *ptr != '\t') {
5569 ptinfo->beg = loc->beg_pos;
5570 ptinfo->indent = column;
5571 ptinfo->nonspc = nonspc;
5575 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5579 if (!p->token_info_enabled) return;
5580 ptinfo = ALLOC(token_info);
5581 ptinfo->token = token;
5582 ptinfo->next = p->token_info;
5583 token_info_setup(ptinfo, p->lex.pbeg, loc);
5585 p->token_info = ptinfo;
5589 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5591 token_info *ptinfo_beg = p->token_info;
5593 if (!ptinfo_beg) return;
5594 p->token_info = ptinfo_beg->next;
5596 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5597 token_info_warn(p, token, ptinfo_beg, 1, loc);
5598 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5602 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5604 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5605 if (!p->token_info_enabled) return;
5606 if (!ptinfo_beg) return;
5607 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5608 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5609 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5610 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5611 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5612 rb_warn3L(ptinfo_end->beg.lineno,
5613 "mismatched indentations at '%s' with '%s' at %d",
5614 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5618 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5620 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5621 if (!MBCLEN_CHARFOUND_P(len)) {
5622 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5629 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5632 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5635 int lineno = p->ruby_sourceline;
5639 else if (yylloc->beg_pos.lineno == lineno) {
5640 str = p->lex.lastline;
5645 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5649 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5654 yylloc = RUBY_SET_YYLLOC(current);
5656 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5657 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5658 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5659 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5662 compile_error(p, "%s", msg);
5663 parser_show_error_line(p, yylloc);
5668 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5671 const int max_line_margin = 30;
5672 const char *ptr, *ptr_end, *pt, *pb;
5673 const char *pre = "", *post = "", *pend;
5674 const char *code = "", *caret = "";
5676 const char *const pbeg = RSTRING_PTR(str);
5681 if (!yylloc) return;
5682 pend = RSTRING_END(str);
5683 if (pend > pbeg && pend[-1] == '\n') {
5684 if (--pend > pbeg && pend[-1] == '\r') --pend;
5688 if (lineno == yylloc->end_pos.lineno &&
5689 (pend - pbeg) > yylloc->end_pos.column) {
5690 pt = pbeg + yylloc->end_pos.column;
5694 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5695 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5697 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5698 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5700 len = ptr_end - ptr;
5703 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5704 if (ptr > pbeg) pre = "...";
5706 if (ptr_end < pend) {
5707 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5708 if (ptr_end < pend) post = "...";
5712 if (lineno == yylloc->beg_pos.lineno) {
5713 pb += yylloc->beg_pos.column;
5714 if (pb > pt) pb = pt;
5716 if (pb < ptr) pb = ptr;
5717 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5720 if (RTEST(errbuf)) {
5721 mesg = rb_attr_get(errbuf, idMesg);
5722 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5723 rb_str_cat_cstr(mesg, "\n");
5726 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5728 if (!errbuf && rb_stderr_tty_p()) {
5729 #define CSI_BEGIN "\033["
5732 CSI_BEGIN""CSI_SGR"%s" /* pre */
5733 CSI_BEGIN"1"CSI_SGR"%.*s"
5734 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5735 CSI_BEGIN";1"CSI_SGR"%.*s"
5736 CSI_BEGIN""CSI_SGR"%s" /* post */
5739 (int)(pb - ptr), ptr,
5741 (int)(ptr_end - pt), pt,
5747 len = ptr_end - ptr;
5748 lim = pt < pend ? pt : pend;
5749 i = (int)(lim - ptr);
5750 buf = ALLOCA_N(char, i+2);
5755 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5761 memset(p2, '~', (lim - ptr));
5765 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5766 pre, (int)len, code, post,
5769 if (!errbuf) rb_write_error_str(mesg);
5773 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5775 const char *pcur = 0, *ptok = 0;
5777 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5778 p->ruby_sourceline == yylloc->end_pos.lineno) {
5781 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5782 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5784 dispatch1(parse_error, STR_NEW2(msg));
5794 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5797 #endif /* !RIPPER */
5801 vtable_size(const struct vtable *tbl)
5803 if (!DVARS_TERMINAL_P(tbl)) {
5812 static struct vtable *
5813 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5815 struct vtable *tbl = ALLOC(struct vtable);
5818 tbl->tbl = ALLOC_N(ID, tbl->capa);
5822 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5827 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5830 vtable_free_gen(struct parser_params *p, int line, const char *name,
5835 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5838 if (!DVARS_TERMINAL_P(tbl)) {
5840 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5842 ruby_sized_xfree(tbl, sizeof(tbl));
5845 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5848 vtable_add_gen(struct parser_params *p, int line, const char *name,
5849 struct vtable *tbl, ID id)
5853 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5854 line, name, (void *)tbl, rb_id2name(id));
5857 if (DVARS_TERMINAL_P(tbl)) {
5858 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5861 if (tbl->pos == tbl->capa) {
5862 tbl->capa = tbl->capa * 2;
5863 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5865 tbl->tbl[tbl->pos++] = id;
5867 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5871 vtable_pop_gen(struct parser_params *p, int line, const char *name,
5872 struct vtable *tbl, int n)
5875 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5876 line, name, (void *)tbl, n);
5879 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5884 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5888 vtable_included(const struct vtable * tbl, ID id)
5892 if (!DVARS_TERMINAL_P(tbl)) {
5893 for (i = 0; i < tbl->pos; i++) {
5894 if (tbl->tbl[i] == id) {
5902 static void parser_prepare(struct parser_params *p);
5905 static NODE *parser_append_options(struct parser_params *p, NODE *node);
5908 debug_lines(VALUE fname)
5911 CONST_ID(script_lines, "SCRIPT_LINES__");
5912 if (rb_const_defined_at(rb_cObject, script_lines)) {
5913 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5914 if (RB_TYPE_P(hash, T_HASH)) {
5915 VALUE lines = rb_ary_new();
5916 rb_hash_aset(hash, fname, lines);
5924 e_option_supplied(struct parser_params *p)
5926 return strcmp(p->ruby_sourcefile, "-e") == 0;
5930 yycompile0(VALUE arg)
5934 struct parser_params *p = (struct parser_params *)arg;
5937 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5938 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5939 if (p->debug_lines && p->ruby_sourceline > 0) {
5940 VALUE str = STR_NEW0();
5941 n = p->ruby_sourceline;
5943 rb_ary_push(p->debug_lines, str);
5947 if (!e_option_supplied(p)) {
5953 #define RUBY_DTRACE_PARSE_HOOK(name) \
5954 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5955 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5957 RUBY_DTRACE_PARSE_HOOK(BEGIN);
5959 RUBY_DTRACE_PARSE_HOOK(END);
5963 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
5964 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
5965 if (n || p->error_p) {
5966 VALUE mesg = p->error_buffer;
5968 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5970 rb_set_errinfo(mesg);
5973 tree = p->eval_tree;
5975 tree = NEW_NIL(&NULL_LOC);
5978 VALUE opt = p->compile_option;
5980 NODE *body = parser_append_options(p, tree->nd_body);
5981 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5982 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5983 prelude = block_append(p, p->eval_tree_begin, body);
5984 tree->nd_body = prelude;
5985 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
5987 p->ast->body.root = tree;
5988 p->ast->body.line_count = p->line_count;
5993 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
5997 p->ruby_sourcefile_string = Qnil;
5998 p->ruby_sourcefile = "(none)";
6001 p->ruby_sourcefile_string = rb_fstring(fname);
6002 p->ruby_sourcefile = StringValueCStr(fname);
6004 p->ruby_sourceline = line - 1;
6006 p->ast = ast = rb_ast_new();
6007 rb_suppress_tracing(yycompile0, (VALUE)p);
6009 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6013 #endif /* !RIPPER */
6015 static rb_encoding *
6016 must_be_ascii_compatible(VALUE s)
6018 rb_encoding *enc = rb_enc_get(s);
6019 if (!rb_enc_asciicompat(enc)) {
6020 rb_raise(rb_eArgError, "invalid source encoding");
6026 lex_get_str(struct parser_params *p, VALUE s)
6028 char *beg, *end, *start;
6031 beg = RSTRING_PTR(s);
6032 len = RSTRING_LEN(s);
6034 if (p->lex.gets_.ptr) {
6035 if (len == p->lex.gets_.ptr) return Qnil;
6036 beg += p->lex.gets_.ptr;
6037 len -= p->lex.gets_.ptr;
6039 end = memchr(beg, '\n', len);
6040 if (end) len = ++end - beg;
6041 p->lex.gets_.ptr += len;
6042 return rb_str_subseq(s, beg - start, len);
6046 lex_getline(struct parser_params *p)
6048 VALUE line = (*p->lex.gets)(p, p->lex.input);
6049 if (NIL_P(line)) return line;
6050 must_be_ascii_compatible(line);
6052 if (p->debug_lines) {
6053 rb_enc_associate(line, p->enc);
6054 rb_ary_push(p->debug_lines, line);
6061 static const rb_data_type_t parser_data_type;
6065 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6067 struct parser_params *p;
6069 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6071 p->lex.gets = lex_get_str;
6072 p->lex.gets_.ptr = 0;
6073 p->lex.input = rb_str_new_frozen(s);
6074 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6076 return yycompile(vparser, p, fname, line);
6080 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6082 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6086 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6088 must_be_ascii_compatible(s);
6089 return parser_compile_string(vparser, f, s, line);
6092 VALUE rb_io_gets_internal(VALUE io);
6095 lex_io_gets(struct parser_params *p, VALUE io)
6097 return rb_io_gets_internal(io);
6101 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6103 struct parser_params *p;
6105 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6107 p->lex.gets = lex_io_gets;
6108 p->lex.input = file;
6109 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6111 return yycompile(vparser, p, fname, start);
6115 lex_generic_gets(struct parser_params *p, VALUE input)
6117 return (*p->lex.gets_.call)(input, p->line_count);
6121 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6123 struct parser_params *p;
6125 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6127 p->lex.gets = lex_generic_gets;
6128 p->lex.gets_.call = lex_gets;
6129 p->lex.input = input;
6130 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6132 return yycompile(vparser, p, fname, start);
6134 #endif /* !RIPPER */
6136 #define STR_FUNC_ESCAPE 0x01
6137 #define STR_FUNC_EXPAND 0x02
6138 #define STR_FUNC_REGEXP 0x04
6139 #define STR_FUNC_QWORDS 0x08
6140 #define STR_FUNC_SYMBOL 0x10
6141 #define STR_FUNC_INDENT 0x20
6142 #define STR_FUNC_LABEL 0x40
6143 #define STR_FUNC_LIST 0x4000
6144 #define STR_FUNC_TERM 0x8000
6147 str_label = STR_FUNC_LABEL,
6149 str_dquote = (STR_FUNC_EXPAND),
6150 str_xquote = (STR_FUNC_EXPAND),
6151 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6152 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6153 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6154 str_ssym = (STR_FUNC_SYMBOL),
6155 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6159 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6163 str = rb_enc_str_new(ptr, len, enc);
6164 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6165 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6167 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6168 rb_enc_associate(str, rb_ascii8bit_encoding());
6175 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6176 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6177 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6178 #define peek(p,c) peek_n(p, (c), 0)
6179 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6180 #define peekc(p) peekc_n(p, 0)
6181 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6185 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6188 if (!has_delayed_token(p)) {
6189 p->delayed.token = rb_str_buf_new(end - tok);
6190 rb_enc_associate(p->delayed.token, p->enc);
6191 p->delayed.line = p->ruby_sourceline;
6192 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6194 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6199 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6203 nextline(struct parser_params *p)
6205 VALUE v = p->lex.nextline;
6206 p->lex.nextline = 0;
6211 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6215 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6223 else if (NIL_P(v)) {
6224 /* after here-document without terminator */
6227 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6228 if (p->heredoc_end > 0) {
6229 p->ruby_sourceline = p->heredoc_end;
6232 p->ruby_sourceline++;
6233 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6234 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6236 p->lex.prevline = p->lex.lastline;
6237 p->lex.lastline = v;
6242 parser_cr(struct parser_params *p, int c)
6244 if (peek(p, '\n')) {
6248 else if (!p->cr_seen) {
6250 /* carried over with p->lex.nextline for nextc() */
6251 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6257 nextc(struct parser_params *p)
6261 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6262 if (nextline(p)) return -1;
6264 c = (unsigned char)*p->lex.pcur++;
6265 if (UNLIKELY(c == '\r')) {
6266 c = parser_cr(p, c);
6273 pushback(struct parser_params *p, int c)
6275 if (c == -1) return;
6277 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6282 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6284 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6285 #define tok(p) (p)->tokenbuf
6286 #define toklen(p) (p)->tokidx
6289 looking_at_eol_p(struct parser_params *p)
6291 const char *ptr = p->lex.pcur;
6292 while (ptr < p->lex.pend) {
6293 int c = (unsigned char)*ptr++;
6294 int eol = (c == '\n' || c == '#');
6295 if (eol || !ISSPACE(c)) {
6303 newtok(struct parser_params *p)
6306 p->tokline = p->ruby_sourceline;
6309 p->tokenbuf = ALLOC_N(char, 60);
6311 if (p->toksiz > 4096) {
6313 REALLOC_N(p->tokenbuf, char, 60);
6319 tokspace(struct parser_params *p, int n)
6323 if (p->tokidx >= p->toksiz) {
6324 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6325 REALLOC_N(p->tokenbuf, char, p->toksiz);
6327 return &p->tokenbuf[p->tokidx-n];
6331 tokadd(struct parser_params *p, int c)
6333 p->tokenbuf[p->tokidx++] = (char)c;
6334 if (p->tokidx >= p->toksiz) {
6336 REALLOC_N(p->tokenbuf, char, p->toksiz);
6341 tok_hex(struct parser_params *p, size_t *numlen)
6345 c = scan_hex(p->lex.pcur, 2, numlen);
6347 yyerror0("invalid hex escape");
6351 p->lex.pcur += *numlen;
6355 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6358 escaped_control_code(int c)
6384 #define WARN_SPACE_CHAR(c, prefix) \
6385 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6388 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6389 int regexp_literal, int wide)
6392 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6393 literal_flush(p, p->lex.pcur);
6394 p->lex.pcur += numlen;
6395 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6396 yyerror0("invalid Unicode escape");
6397 return wide && numlen > 0;
6399 if (codepoint > 0x10ffff) {
6400 yyerror0("invalid Unicode codepoint (too large)");
6403 if ((codepoint & 0xfffff800) == 0xd800) {
6404 yyerror0("invalid Unicode codepoint");
6407 if (regexp_literal) {
6408 tokcopy(p, (int)numlen);
6410 else if (codepoint >= 0x80) {
6411 rb_encoding *utf8 = rb_utf8_encoding();
6412 if (*encp && utf8 != *encp) {
6413 YYLTYPE loc = RUBY_INIT_YYLLOC();
6414 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6415 parser_show_error_line(p, &loc);
6419 tokaddmbc(p, codepoint, *encp);
6422 tokadd(p, codepoint);
6427 /* return value is for ?\u3042 */
6429 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6430 int term, int symbol_literal, int regexp_literal)
6433 * If `term` is not -1, then we allow multiple codepoints in \u{}
6434 * upto `term` byte, otherwise we're parsing a character literal.
6435 * And then add the codepoints to the current token.
6437 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6439 const int open_brace = '{', close_brace = '}';
6441 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6443 if (peek(p, open_brace)) { /* handle \u{...} form */
6444 const char *second = NULL;
6445 int c, last = nextc(p);
6446 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6447 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6448 while (c != close_brace) {
6449 if (c == term) goto unterminated;
6450 if (second == multiple_codepoints)
6451 second = p->lex.pcur;
6452 if (regexp_literal) tokadd(p, last);
6453 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6456 while (ISSPACE(c = *p->lex.pcur)) {
6457 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6460 if (term == -1 && !second)
6461 second = multiple_codepoints;
6464 if (c != close_brace) {
6467 yyerror0("unterminated Unicode escape");
6470 if (second && second != multiple_codepoints) {
6471 const char *pcur = p->lex.pcur;
6472 p->lex.pcur = second;
6473 dispatch_scan_event(p, tSTRING_CONTENT);
6476 yyerror0(multiple_codepoints);
6480 if (regexp_literal) tokadd(p, close_brace);
6483 else { /* handle \uxxxx form */
6484 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6491 #define ESCAPE_CONTROL 1
6492 #define ESCAPE_META 2
6495 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6500 switch (c = nextc(p)) {
6501 case '\\': /* Backslash */
6504 case 'n': /* newline */
6507 case 't': /* horizontal tab */
6510 case 'r': /* carriage-return */
6513 case 'f': /* form-feed */
6516 case 'v': /* vertical tab */
6519 case 'a': /* alarm(bell) */
6522 case 'e': /* escape */
6525 case '0': case '1': case '2': case '3': /* octal constant */
6526 case '4': case '5': case '6': case '7':
6528 c = scan_oct(p->lex.pcur, 3, &numlen);
6529 p->lex.pcur += numlen;
6532 case 'x': /* hex constant */
6533 c = tok_hex(p, &numlen);
6534 if (numlen == 0) return 0;
6537 case 'b': /* backspace */
6540 case 's': /* space */
6544 if (flags & ESCAPE_META) goto eof;
6545 if ((c = nextc(p)) != '-') {
6548 if ((c = nextc(p)) == '\\') {
6549 if (peek(p, 'u')) goto eof;
6550 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6552 else if (c == -1 || !ISASCII(c)) goto eof;
6554 int c2 = escaped_control_code(c);
6556 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6557 WARN_SPACE_CHAR(c2, "\\M-");
6560 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6563 else if (ISCNTRL(c)) goto eof;
6564 return ((c & 0xff) | 0x80);
6568 if ((c = nextc(p)) != '-') {
6572 if (flags & ESCAPE_CONTROL) goto eof;
6573 if ((c = nextc(p))== '\\') {
6574 if (peek(p, 'u')) goto eof;
6575 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6579 else if (c == -1 || !ISASCII(c)) goto eof;
6581 int c2 = escaped_control_code(c);
6584 if (flags & ESCAPE_META) {
6585 WARN_SPACE_CHAR(c2, "\\M-");
6588 WARN_SPACE_CHAR(c2, "");
6592 if (flags & ESCAPE_META) {
6593 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6596 WARN_SPACE_CHAR(c2, "\\C-");
6600 else if (ISCNTRL(c)) goto eof;
6606 yyerror0("Invalid escape character syntax");
6616 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6618 int len = rb_enc_codelen(c, enc);
6619 rb_enc_mbcput(c, tokspace(p, len), enc);
6623 tokadd_escape(struct parser_params *p, rb_encoding **encp)
6630 switch (c = nextc(p)) {
6632 return 0; /* just ignore */
6634 case '0': case '1': case '2': case '3': /* octal constant */
6635 case '4': case '5': case '6': case '7':
6637 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6638 if (numlen == 0) goto eof;
6639 p->lex.pcur += numlen;
6640 tokcopy(p, (int)numlen + 1);
6644 case 'x': /* hex constant */
6646 tok_hex(p, &numlen);
6647 if (numlen == 0) return -1;
6648 tokcopy(p, (int)numlen + 2);
6653 if (flags & ESCAPE_META) goto eof;
6654 if ((c = nextc(p)) != '-') {
6659 flags |= ESCAPE_META;
6663 if (flags & ESCAPE_CONTROL) goto eof;
6664 if ((c = nextc(p)) != '-') {
6672 if (flags & ESCAPE_CONTROL) goto eof;
6674 flags |= ESCAPE_CONTROL;
6676 if ((c = nextc(p)) == '\\') {
6679 else if (c == -1) goto eof;
6685 yyerror0("Invalid escape character syntax");
6697 regx_options(struct parser_params *p)
6705 while (c = nextc(p), ISALPHA(c)) {
6707 options |= RE_OPTION_ONCE;
6709 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6711 if (kc != rb_ascii8bit_encindex()) kcode = c;
6725 YYLTYPE loc = RUBY_INIT_YYLLOC();
6727 compile_error(p, "unknown regexp option%s - %*s",
6728 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6729 parser_show_error_line(p, &loc);
6731 return options | RE_OPTION_ENCODING(kcode);
6735 tokadd_mbchar(struct parser_params *p, int c)
6737 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6738 if (len < 0) return -1;
6740 p->lex.pcur += --len;
6741 if (len > 0) tokcopy(p, len);
6746 simple_re_meta(int c)
6749 case '$': case '*': case '+': case '.':
6750 case '?': case '^': case '|':
6751 case ')': case ']': case '}': case '>':
6759 parser_update_heredoc_indent(struct parser_params *p, int c)
6761 if (p->heredoc_line_indent == -1) {
6762 if (c == '\n') p->heredoc_line_indent = 0;
6766 p->heredoc_line_indent++;
6769 else if (c == '\t') {
6770 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6771 p->heredoc_line_indent = w * TAB_WIDTH;
6774 else if (c != '\n') {
6775 if (p->heredoc_indent > p->heredoc_line_indent) {
6776 p->heredoc_indent = p->heredoc_line_indent;
6778 p->heredoc_line_indent = -1;
6785 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6787 YYLTYPE loc = RUBY_INIT_YYLLOC();
6788 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6789 compile_error(p, "%s mixed within %s source", n1, n2);
6790 parser_show_error_line(p, &loc);
6794 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6796 const char *pos = p->lex.pcur;
6798 parser_mixed_error(p, enc1, enc2);
6803 tokadd_string(struct parser_params *p,
6804 int func, int term, int paren, long *nest,
6805 rb_encoding **encp, rb_encoding **enc)
6810 #define mixed_error(enc1, enc2) \
6811 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6812 #define mixed_escape(beg, enc1, enc2) \
6813 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6815 while ((c = nextc(p)) != -1) {
6816 if (p->heredoc_indent > 0) {
6817 parser_update_heredoc_indent(p, c);
6820 if (paren && c == paren) {
6823 else if (c == term) {
6824 if (!nest || !*nest) {
6830 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6831 int c2 = *p->lex.pcur;
6832 if (c2 == '$' || c2 == '@' || c2 == '{') {
6837 else if (c == '\\') {
6838 literal_flush(p, p->lex.pcur - 1);
6842 if (func & STR_FUNC_QWORDS) break;
6843 if (func & STR_FUNC_EXPAND) {
6844 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6855 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6859 if ((func & STR_FUNC_EXPAND) == 0) {
6863 tokadd_utf8(p, enc, term,
6864 func & STR_FUNC_SYMBOL,
6865 func & STR_FUNC_REGEXP);
6869 if (c == -1) return -1;
6871 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6874 if (func & STR_FUNC_REGEXP) {
6875 if (c == term && !simple_re_meta(c)) {
6880 if ((c = tokadd_escape(p, enc)) < 0)
6882 if (*enc && *enc != *encp) {
6883 mixed_escape(p->lex.ptok+2, *enc, *encp);
6887 else if (func & STR_FUNC_EXPAND) {
6889 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6890 c = read_escape(p, 0, enc);
6892 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6893 /* ignore backslashed spaces in %w */
6895 else if (c != term && !(paren && c == paren)) {
6902 else if (!parser_isascii(p)) {
6907 else if (*enc != *encp) {
6908 mixed_error(*enc, *encp);
6911 if (tokadd_mbchar(p, c) == -1) return -1;
6914 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6922 else if (*enc != *encp) {
6923 mixed_error(*enc, *encp);
6930 if (*enc) *encp = *enc;
6934 static inline rb_strterm_t *
6935 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6937 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6940 /* imemo_parser_strterm for literal */
6941 #define NEW_STRTERM(func, term, paren) \
6942 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6946 flush_string_content(struct parser_params *p, rb_encoding *enc)
6948 VALUE content = yylval.val;
6949 if (!ripper_is_node_yylval(content))
6950 content = ripper_new_yylval(p, 0, 0, content);
6951 if (has_delayed_token(p)) {
6952 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6954 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6956 dispatch_delayed_token(p, tSTRING_CONTENT);
6957 p->lex.ptok = p->lex.pcur;
6958 RNODE(content)->nd_rval = yylval.val;
6960 dispatch_scan_event(p, tSTRING_CONTENT);
6961 if (yylval.val != content)
6962 RNODE(content)->nd_rval = yylval.val;
6963 yylval.val = content;
6966 #define flush_string_content(p, enc) ((void)(enc))
6969 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6970 /* this can be shared with ripper, since it's independent from struct
6973 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6974 #define SPECIAL_PUNCT(idx) ( \
6975 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6976 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', 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) | \
6981 const unsigned int ruby_global_name_punct_bits[] = {
6987 #undef SPECIAL_PUNCT
6990 static enum yytokentype
6991 parser_peek_variable_name(struct parser_params *p)
6994 const char *ptr = p->lex.pcur;
6996 if (ptr + 1 >= p->lex.pend) return 0;
7000 if ((c = *ptr) == '-') {
7001 if (++ptr >= p->lex.pend) return 0;
7004 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7005 return tSTRING_DVAR;
7009 if ((c = *ptr) == '@') {
7010 if (++ptr >= p->lex.pend) return 0;
7016 p->command_start = TRUE;
7017 return tSTRING_DBEG;
7021 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7022 return tSTRING_DVAR;
7026 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7027 #define IS_END() IS_lex_state(EXPR_END_ANY)
7028 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7029 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7030 #define IS_LABEL_POSSIBLE() (\
7031 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7033 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7034 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7036 static inline enum yytokentype
7037 parser_string_term(struct parser_params *p, int func)
7040 if (func & STR_FUNC_REGEXP) {
7041 set_yylval_num(regx_options(p));
7042 dispatch_scan_event(p, tREGEXP_END);
7043 SET_LEX_STATE(EXPR_END);
7046 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7048 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7051 SET_LEX_STATE(EXPR_END);
7055 static enum yytokentype
7056 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7058 int func = (int)quote->u1.func;
7059 int term = (int)quote->u3.term;
7060 int paren = (int)quote->u2.paren;
7062 rb_encoding *enc = p->enc;
7063 rb_encoding *base_enc = 0;
7066 if (func & STR_FUNC_TERM) {
7067 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7068 SET_LEX_STATE(EXPR_END);
7070 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7073 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7074 do {c = nextc(p);} while (ISSPACE(c));
7077 if (func & STR_FUNC_LIST) {
7078 quote->u1.func &= ~STR_FUNC_LIST;
7081 if (c == term && !quote->u0.nest) {
7082 if (func & STR_FUNC_QWORDS) {
7083 quote->u1.func |= STR_FUNC_TERM;
7084 pushback(p, c); /* dispatch the term at tSTRING_END */
7085 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7088 return parser_string_term(p, func);
7092 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7096 if ((func & STR_FUNC_EXPAND) && c == '#') {
7097 int t = parser_peek_variable_name(p);
7103 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7104 &enc, &base_enc) == -1) {
7107 # define unterminated_literal(mesg) yyerror0(mesg)
7109 # define unterminated_literal(mesg) compile_error(p, mesg)
7111 literal_flush(p, p->lex.pcur);
7112 if (func & STR_FUNC_QWORDS) {
7113 /* no content to add, bailing out here */
7114 unterminated_literal("unterminated list meets end of file");
7118 if (func & STR_FUNC_REGEXP) {
7119 unterminated_literal("unterminated regexp meets end of file");
7122 unterminated_literal("unterminated string meets end of file");
7124 quote->u1.func |= STR_FUNC_TERM;
7129 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7130 set_yylval_str(lit);
7131 flush_string_content(p, enc);
7133 return tSTRING_CONTENT;
7136 static enum yytokentype
7137 heredoc_identifier(struct parser_params *p)
7140 * term_len is length of `<<"END"` except `END`,
7141 * in this case term_len is 4 (<, <, " and ").
7143 long len, offset = p->lex.pcur - p->lex.pbeg;
7144 int c = nextc(p), term, func = 0, quote = 0;
7145 enum yytokentype token = tSTRING_BEG;
7150 func = STR_FUNC_INDENT;
7153 else if (c == '~') {
7155 func = STR_FUNC_INDENT;
7161 func |= str_squote; goto quoted;
7163 func |= str_dquote; goto quoted;
7165 token = tXSTRING_BEG;
7166 func |= str_xquote; goto quoted;
7173 while ((c = nextc(p)) != term) {
7174 if (c == -1 || c == '\r' || c == '\n') {
7175 yyerror(NULL, p, "unterminated here document identifier");
7182 if (!parser_is_identchar(p)) {
7184 if (func & STR_FUNC_INDENT) {
7185 pushback(p, indent > 0 ? '~' : '-');
7191 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7192 if (n < 0) return 0;
7194 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7199 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7200 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7201 yyerror(NULL, p, "too long here document identifier");
7202 dispatch_scan_event(p, tHEREDOC_BEG);
7205 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7206 p->lex.strterm->flags |= STRTERM_HEREDOC;
7207 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7208 here->offset = offset;
7209 here->sourceline = p->ruby_sourceline;
7210 here->length = (int)len;
7211 here->quote = quote;
7215 p->heredoc_indent = indent;
7216 p->heredoc_line_indent = 0;
7221 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7226 line = here->lastline;
7227 p->lex.lastline = line;
7228 p->lex.pbeg = RSTRING_PTR(line);
7229 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7230 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7231 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7232 p->heredoc_end = p->ruby_sourceline;
7233 p->ruby_sourceline = (int)here->sourceline;
7234 if (p->eofp) p->lex.nextline = Qnil;
7239 dedent_string(VALUE string, int width)
7245 RSTRING_GETMEM(string, str, len);
7246 for (i = 0; i < len && col < width; i++) {
7247 if (str[i] == ' ') {
7250 else if (str[i] == '\t') {
7251 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7252 if (n > width) break;
7260 rb_str_modify(string);
7261 str = RSTRING_PTR(string);
7262 if (RSTRING_LEN(string) != len)
7263 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7264 MEMMOVE(str, str + i, char, len - i);
7265 rb_str_set_len(string, len - i);
7271 heredoc_dedent(struct parser_params *p, NODE *root)
7273 NODE *node, *str_node, *prev_node;
7274 int indent = p->heredoc_indent;
7277 if (indent <= 0) return root;
7278 p->heredoc_indent = 0;
7279 if (!root) return root;
7281 prev_node = node = str_node = root;
7282 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7285 VALUE lit = str_node->nd_lit;
7286 if (str_node->flags & NODE_FL_NEWLINE) {
7287 dedent_string(lit, indent);
7292 else if (!literal_concat0(p, prev_lit, lit)) {
7296 NODE *end = node->nd_end;
7297 node = prev_node->nd_next = node->nd_next;
7299 if (nd_type(prev_node) == NODE_DSTR)
7300 nd_set_type(prev_node, NODE_STR);
7308 while ((node = (prev_node = node)->nd_next) != 0) {
7310 if (nd_type(node) != NODE_LIST) break;
7311 if ((str_node = node->nd_head) != 0) {
7312 enum node_type type = nd_type(str_node);
7313 if (type == NODE_STR || type == NODE_DSTR) break;
7323 heredoc_dedent(struct parser_params *p, VALUE array)
7325 int indent = p->heredoc_indent;
7327 if (indent <= 0) return array;
7328 p->heredoc_indent = 0;
7329 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7335 * Ripper.dedent_string(input, width) -> Integer
7337 * USE OF RIPPER LIBRARY ONLY.
7339 * Strips up to +width+ leading whitespaces from +input+,
7340 * and returns the stripped column width.
7343 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7348 wid = NUM2UINT(width);
7349 col = dedent_string(input, wid);
7350 return INT2NUM(col);
7355 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7357 const char *ptr = p->lex.pbeg;
7361 while (*ptr && ISSPACE(*ptr)) ptr++;
7363 n = p->lex.pend - (ptr + len);
7364 if (n < 0) return FALSE;
7365 if (n > 0 && ptr[len] != '\n') {
7366 if (ptr[len] != '\r') return FALSE;
7367 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7369 return strncmp(eos, ptr, len) == 0;
7373 word_match_p(struct parser_params *p, const char *word, long len)
7375 if (strncmp(p->lex.pcur, word, len)) return 0;
7376 if (p->lex.pcur + len == p->lex.pend) return 1;
7377 int c = (unsigned char)p->lex.pcur[len];
7378 if (ISSPACE(c)) return 1;
7380 case '\0': case '\004': case '\032': return 1;
7385 #define NUM_SUFFIX_R (1<<0)
7386 #define NUM_SUFFIX_I (1<<1)
7387 #define NUM_SUFFIX_ALL 3
7390 number_literal_suffix(struct parser_params *p, int mask)
7393 const char *lastp = p->lex.pcur;
7395 while ((c = nextc(p)) != -1) {
7396 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7397 result |= (mask & NUM_SUFFIX_I);
7398 mask &= ~NUM_SUFFIX_I;
7399 /* r after i, rational of complex is disallowed */
7400 mask &= ~NUM_SUFFIX_R;
7403 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7404 result |= (mask & NUM_SUFFIX_R);
7405 mask &= ~NUM_SUFFIX_R;
7408 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7409 p->lex.pcur = lastp;
7410 literal_flush(p, p->lex.pcur);
7419 static enum yytokentype
7420 set_number_literal(struct parser_params *p, VALUE v,
7421 enum yytokentype type, int suffix)
7423 if (suffix & NUM_SUFFIX_I) {
7424 v = rb_complex_raw(INT2FIX(0), v);
7427 set_yylval_literal(v);
7428 SET_LEX_STATE(EXPR_END);
7432 static enum yytokentype
7433 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7435 enum yytokentype type = tINTEGER;
7436 if (suffix & NUM_SUFFIX_R) {
7437 v = rb_rational_raw1(v);
7440 return set_number_literal(p, v, type, suffix);
7445 dispatch_heredoc_end(struct parser_params *p)
7448 if (has_delayed_token(p))
7449 dispatch_delayed_token(p, tSTRING_CONTENT);
7450 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7451 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7457 #define dispatch_heredoc_end(p) ((void)0)
7460 static enum yytokentype
7461 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7463 int c, func, indent = 0;
7464 const char *eos, *ptr, *ptr_end;
7467 rb_encoding *enc = p->enc;
7468 rb_encoding *base_enc = 0;
7471 eos = RSTRING_PTR(here->lastline) + here->offset;
7473 indent = (func = here->func) & STR_FUNC_INDENT;
7475 if ((c = nextc(p)) == -1) {
7478 if (!has_delayed_token(p)) {
7479 dispatch_scan_event(p, tSTRING_CONTENT);
7482 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7483 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7484 int cr = ENC_CODERANGE_UNKNOWN;
7485 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7486 if (cr != ENC_CODERANGE_7BIT &&
7487 p->enc == rb_usascii_encoding() &&
7488 enc != rb_utf8_encoding()) {
7489 enc = rb_ascii8bit_encoding();
7492 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7494 dispatch_delayed_token(p, tSTRING_CONTENT);
7498 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7499 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7503 SET_LEX_STATE(EXPR_END);
7508 /* not beginning of line, cannot be the terminator */
7510 else if (p->heredoc_line_indent == -1) {
7511 /* `heredoc_line_indent == -1` means
7512 * - "after an interpolation in the same line", or
7513 * - "in a continuing line"
7515 p->heredoc_line_indent = 0;
7517 else if (whole_match_p(p, eos, len, indent)) {
7518 dispatch_heredoc_end(p);
7520 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7523 SET_LEX_STATE(EXPR_END);
7527 if (!(func & STR_FUNC_EXPAND)) {
7529 ptr = RSTRING_PTR(p->lex.lastline);
7530 ptr_end = p->lex.pend;
7531 if (ptr_end > ptr) {
7532 switch (ptr_end[-1]) {
7534 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7543 if (p->heredoc_indent > 0) {
7545 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7547 p->heredoc_line_indent = 0;
7551 rb_str_cat(str, ptr, ptr_end - ptr);
7553 str = STR_NEW(ptr, ptr_end - ptr);
7554 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7556 if (p->heredoc_indent > 0) {
7559 if (nextc(p) == -1) {
7565 } while (!whole_match_p(p, eos, len, indent));
7568 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7571 int t = parser_peek_variable_name(p);
7572 if (p->heredoc_line_indent != -1) {
7573 if (p->heredoc_indent > p->heredoc_line_indent) {
7574 p->heredoc_indent = p->heredoc_line_indent;
7576 p->heredoc_line_indent = -1;
7585 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7586 if (p->eofp) goto error;
7590 if (c == '\\') p->heredoc_line_indent = -1;
7592 str = STR_NEW3(tok(p), toklen(p), enc, func);
7594 set_yylval_str(str);
7596 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7598 flush_string_content(p, enc);
7599 return tSTRING_CONTENT;
7601 tokadd(p, nextc(p));
7602 if (p->heredoc_indent > 0) {
7606 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7607 if ((c = nextc(p)) == -1) goto error;
7608 } while (!whole_match_p(p, eos, len, indent));
7609 str = STR_NEW3(tok(p), toklen(p), enc, func);
7611 dispatch_heredoc_end(p);
7613 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7616 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7618 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7619 set_yylval_str(str);
7621 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7623 return tSTRING_CONTENT;
7629 arg_ambiguous(struct parser_params *p, char c)
7632 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7634 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7640 formal_argument(struct parser_params *p, ID lhs)
7642 switch (id_type(lhs)) {
7647 yyerror0("formal argument cannot be a constant");
7650 yyerror0("formal argument cannot be an instance variable");
7653 yyerror0("formal argument cannot be a global variable");
7656 yyerror0("formal argument cannot be a class variable");
7659 yyerror0("formal argument must be local variable");
7663 lhs = dispatch1(param_error, lhs);
7668 shadowing_lvar(p, lhs);
7673 lvar_defined(struct parser_params *p, ID id)
7675 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7678 /* emacsen -*- hack */
7680 parser_encode_length(struct parser_params *p, const char *name, long len)
7684 if (len > 5 && name[nlen = len - 5] == '-') {
7685 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7688 if (len > 4 && name[nlen = len - 4] == '-') {
7689 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7691 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7692 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7693 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7700 parser_set_encode(struct parser_params *p, const char *name)
7702 int idx = rb_enc_find_index(name);
7707 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7709 excargs[0] = rb_eArgError;
7710 excargs[2] = rb_make_backtrace();
7711 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7712 rb_exc_raise(rb_make_exception(3, excargs));
7714 enc = rb_enc_from_index(idx);
7715 if (!rb_enc_asciicompat(enc)) {
7716 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7721 if (p->debug_lines) {
7722 VALUE lines = p->debug_lines;
7723 long i, n = RARRAY_LEN(lines);
7724 for (i = 0; i < n; ++i) {
7725 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7732 comment_at_top(struct parser_params *p)
7734 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7735 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7736 while (ptr < ptr_end) {
7737 if (!ISSPACE(*ptr)) return 0;
7743 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7744 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7747 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7749 if (!comment_at_top(p)) {
7752 parser_set_encode(p, val);
7756 parser_get_bool(struct parser_params *p, const char *name, const char *val)
7760 if (strcasecmp(val, "true") == 0) {
7765 if (strcasecmp(val, "false") == 0) {
7770 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7775 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7777 int b = parser_get_bool(p, name, val);
7778 if (b >= 0) p->token_info_enabled = b;
7782 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7786 if (p->token_seen) {
7787 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7791 b = parser_get_bool(p, name, val);
7794 if (!p->compile_option)
7795 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7796 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7797 (b ? Qtrue : Qfalse));
7800 # if WARN_PAST_SCOPE
7802 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7804 int b = parser_get_bool(p, name, val);
7805 if (b >= 0) p->past_scope_enabled = b;
7809 struct magic_comment {
7811 rb_magic_comment_setter_t func;
7812 rb_magic_comment_length_t length;
7815 static const struct magic_comment magic_comments[] = {
7816 {"coding", magic_comment_encoding, parser_encode_length},
7817 {"encoding", magic_comment_encoding, parser_encode_length},
7818 {"frozen_string_literal", parser_set_compile_option_flag},
7819 {"warn_indent", parser_set_token_info},
7820 # if WARN_PAST_SCOPE
7821 {"warn_past_scope", parser_set_past_scope},
7826 magic_comment_marker(const char *str, long len)
7833 if (str[i-1] == '*' && str[i-2] == '-') {
7839 if (i + 1 >= len) return 0;
7840 if (str[i+1] != '-') {
7843 else if (str[i-1] != '-') {
7859 parser_magic_comment(struct parser_params *p, const char *str, long len)
7862 VALUE name = 0, val = 0;
7863 const char *beg, *end, *vbeg, *vend;
7864 #define str_copy(_s, _p, _n) ((_s) \
7865 ? (void)(rb_str_resize((_s), (_n)), \
7866 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7867 : (void)((_s) = STR_NEW((_p), (_n))))
7869 if (len <= 7) return FALSE;
7870 if (!!(beg = magic_comment_marker(str, len))) {
7871 if (!(end = magic_comment_marker(beg, str + len - beg)))
7875 len = end - beg - 3;
7878 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7880 const struct magic_comment *mc = magic_comments;
7885 for (; len > 0 && *str; str++, --len) {
7887 case '\'': case '"': case ':': case ';':
7890 if (!ISSPACE(*str)) break;
7892 for (beg = str; len > 0; str++, --len) {
7894 case '\'': case '"': case ':': case ';':
7897 if (ISSPACE(*str)) break;
7902 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7905 if (!indicator) return FALSE;
7909 do str++; while (--len > 0 && ISSPACE(*str));
7912 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7925 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7929 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7932 while (len > 0 && (ISSPACE(*str))) --len, str++;
7933 if (len) return FALSE;
7937 str_copy(name, beg, n);
7938 s = RSTRING_PTR(name);
7939 for (i = 0; i < n; ++i) {
7940 if (s[i] == '-') s[i] = '_';
7943 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7946 n = (*mc->length)(p, vbeg, n);
7948 str_copy(val, vbeg, n);
7949 (*mc->func)(p, mc->name, RSTRING_PTR(val));
7952 } while (++mc < magic_comments + numberof(magic_comments));
7954 str_copy(val, vbeg, vend - vbeg);
7955 dispatch2(magic_comment, name, val);
7963 set_file_encoding(struct parser_params *p, const char *str, const char *send)
7966 const char *beg = str;
7970 if (send - str <= 6) return;
7972 case 'C': case 'c': str += 6; continue;
7973 case 'O': case 'o': str += 5; continue;
7974 case 'D': case 'd': str += 4; continue;
7975 case 'I': case 'i': str += 3; continue;
7976 case 'N': case 'n': str += 2; continue;
7977 case 'G': case 'g': str += 1; continue;
7984 if (ISSPACE(*str)) break;
7987 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7991 if (++str >= send) return;
7992 } while (ISSPACE(*str));
7994 if (*str != '=' && *str != ':') return;
7999 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8000 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8001 parser_set_encode(p, RSTRING_PTR(s));
8002 rb_str_resize(s, 0);
8006 parser_prepare(struct parser_params *p)
8009 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8012 if (peek(p, '!')) p->has_shebang = 1;
8014 case 0xef: /* UTF-8 BOM marker */
8015 if (p->lex.pend - p->lex.pcur >= 2 &&
8016 (unsigned char)p->lex.pcur[0] == 0xbb &&
8017 (unsigned char)p->lex.pcur[1] == 0xbf) {
8018 p->enc = rb_utf8_encoding();
8020 p->lex.pbeg = p->lex.pcur;
8028 p->enc = rb_enc_get(p->lex.lastline);
8032 #define ambiguous_operator(tok, op, syn) ( \
8033 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8034 rb_warning0("even though it seems like "syn""))
8036 #define ambiguous_operator(tok, op, syn) \
8037 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8039 #define warn_balanced(tok, op, syn) ((void) \
8040 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8041 space_seen && !ISSPACE(c) && \
8042 (ambiguous_operator(tok, op, syn), 0)), \
8043 (enum yytokentype)(tok))
8046 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8049 char *point = &str[seen_point];
8050 size_t fraclen = len-seen_point-1;
8051 memmove(point, point+1, fraclen+1);
8052 v = rb_cstr_to_inum(str, 10, FALSE);
8053 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8056 static enum yytokentype
8057 no_digits(struct parser_params *p)
8059 yyerror0("numeric literal without digits");
8060 if (peek(p, '_')) nextc(p);
8061 /* dummy 0, for tUMINUS_NUM at numeric */
8062 return set_integer_literal(p, INT2FIX(0), 0);
8065 static enum yytokentype
8066 parse_numeric(struct parser_params *p, int c)
8068 int is_float, seen_point, seen_e, nondigit;
8071 is_float = seen_point = seen_e = nondigit = 0;
8072 SET_LEX_STATE(EXPR_END);
8074 if (c == '-' || c == '+') {
8079 int start = toklen(p);
8081 if (c == 'x' || c == 'X') {
8084 if (c != -1 && ISXDIGIT(c)) {
8087 if (nondigit) break;
8091 if (!ISXDIGIT(c)) break;
8094 } while ((c = nextc(p)) != -1);
8098 if (toklen(p) == start) {
8099 return no_digits(p);
8101 else if (nondigit) goto trailing_uc;
8102 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8103 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8105 if (c == 'b' || c == 'B') {
8108 if (c == '0' || c == '1') {
8111 if (nondigit) break;
8115 if (c != '0' && c != '1') break;
8118 } while ((c = nextc(p)) != -1);
8122 if (toklen(p) == start) {
8123 return no_digits(p);
8125 else if (nondigit) goto trailing_uc;
8126 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8127 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8129 if (c == 'd' || c == 'D') {
8132 if (c != -1 && ISDIGIT(c)) {
8135 if (nondigit) break;
8139 if (!ISDIGIT(c)) break;
8142 } while ((c = nextc(p)) != -1);
8146 if (toklen(p) == start) {
8147 return no_digits(p);
8149 else if (nondigit) goto trailing_uc;
8150 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8151 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8157 if (c == 'o' || c == 'O') {
8158 /* prefixed octal */
8160 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8161 return no_digits(p);
8164 if (c >= '0' && c <= '7') {
8169 if (nondigit) break;
8173 if (c < '0' || c > '9') break;
8174 if (c > '7') goto invalid_octal;
8177 } while ((c = nextc(p)) != -1);
8178 if (toklen(p) > start) {
8181 if (nondigit) goto trailing_uc;
8182 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8183 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8190 if (c > '7' && c <= '9') {
8192 yyerror0("Invalid octal digit");
8194 else if (c == '.' || c == 'e' || c == 'E') {
8199 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8200 return set_integer_literal(p, INT2FIX(0), suffix);
8206 case '0': case '1': case '2': case '3': case '4':
8207 case '5': case '6': case '7': case '8': case '9':
8213 if (nondigit) goto trailing_uc;
8214 if (seen_point || seen_e) {
8219 if (c0 == -1 || !ISDIGIT(c0)) {
8225 seen_point = toklen(p);
8244 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8249 tokadd(p, nondigit);
8253 nondigit = (c == '-' || c == '+') ? c : 0;
8256 case '_': /* `_' in number just ignored */
8257 if (nondigit) goto decode_num;
8271 literal_flush(p, p->lex.pcur - 1);
8272 YYLTYPE loc = RUBY_INIT_YYLLOC();
8273 compile_error(p, "trailing `%c' in number", nondigit);
8274 parser_show_error_line(p, &loc);
8278 enum yytokentype type = tFLOAT;
8281 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8282 if (suffix & NUM_SUFFIX_R) {
8284 v = parse_rational(p, tok(p), toklen(p), seen_point);
8287 double d = strtod(tok(p), 0);
8288 if (errno == ERANGE) {
8289 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8294 return set_number_literal(p, v, type, suffix);
8296 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8297 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8300 static enum yytokentype
8301 parse_qmark(struct parser_params *p, int space_seen)
8308 SET_LEX_STATE(EXPR_VALUE);
8313 compile_error(p, "incomplete character syntax");
8316 if (rb_enc_isspace(c, p->enc)) {
8318 int c2 = escaped_control_code(c);
8320 WARN_SPACE_CHAR(c2, "?");
8325 SET_LEX_STATE(EXPR_VALUE);
8330 if (!parser_isascii(p)) {
8331 if (tokadd_mbchar(p, c) == -1) return 0;
8333 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8334 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8336 const char *start = p->lex.pcur - 1, *ptr = start;
8338 int n = parser_precise_mbclen(p, ptr);
8339 if (n < 0) return -1;
8341 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8342 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8343 " a conditional operator, put a space after `?'",
8344 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8348 else if (c == '\\') {
8351 enc = rb_utf8_encoding();
8352 tokadd_utf8(p, &enc, -1, 0, 0);
8354 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8356 if (tokadd_mbchar(p, c) == -1) return 0;
8359 c = read_escape(p, 0, &enc);
8367 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8368 set_yylval_str(lit);
8369 SET_LEX_STATE(EXPR_END);
8373 static enum yytokentype
8374 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8377 const char *ptok = p->lex.pcur;
8385 if (c == -1 || !ISALNUM(c)) {
8391 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8392 yyerror0("unknown type of %string");
8396 if (c == -1 || term == -1) {
8397 compile_error(p, "unterminated quoted string meets end of file");
8401 if (term == '(') term = ')';
8402 else if (term == '[') term = ']';
8403 else if (term == '{') term = '}';
8404 else if (term == '<') term = '>';
8407 p->lex.ptok = ptok-1;
8410 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8414 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8418 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8422 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8426 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8427 return tSYMBOLS_BEG;
8430 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8431 return tQSYMBOLS_BEG;
8434 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8435 return tXSTRING_BEG;
8438 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8442 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8443 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8447 yyerror0("unknown type of %string");
8451 if ((c = nextc(p)) == '=') {
8453 SET_LEX_STATE(EXPR_BEG);
8456 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8459 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8461 return warn_balanced('%', "%%", "string literal");
8465 tokadd_ident(struct parser_params *p, int c)
8468 if (tokadd_mbchar(p, c) == -1) return -1;
8470 } while (parser_is_identchar(p));
8476 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8478 ID ident = TOK_INTERN();
8480 set_yylval_name(ident);
8486 parse_numvar(struct parser_params *p)
8490 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8491 const unsigned long nth_ref_max =
8492 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8493 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8494 * turned into a Fixnum, in compile.c */
8496 if (overflow || n > nth_ref_max) {
8497 /* compile_error()? */
8498 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8499 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8506 static enum yytokentype
8507 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8509 const char *ptr = p->lex.pcur;
8512 SET_LEX_STATE(EXPR_END);
8513 p->lex.ptok = ptr - 1; /* from '$' */
8517 case '_': /* $_: last read line string */
8519 if (parser_is_identchar(p)) {
8527 case '~': /* $~: match-data */
8528 case '*': /* $*: argv */
8529 case '$': /* $$: pid */
8530 case '?': /* $?: last status */
8531 case '!': /* $!: error string */
8532 case '@': /* $@: error position */
8533 case '/': /* $/: input record separator */
8534 case '\\': /* $\: output record separator */
8535 case ';': /* $;: field separator */
8536 case ',': /* $,: output field separator */
8537 case '.': /* $.: last read line number */
8538 case '=': /* $=: ignorecase */
8539 case ':': /* $:: load path */
8540 case '<': /* $<: reading filename */
8541 case '>': /* $>: default output handle */
8542 case '\"': /* $": already loaded files */
8551 if (parser_is_identchar(p)) {
8552 if (tokadd_mbchar(p, c) == -1) return 0;
8560 set_yylval_name(TOK_INTERN());
8563 case '&': /* $&: last match */
8564 case '`': /* $`: string before last match */
8565 case '\'': /* $': string after last match */
8566 case '+': /* $+: string matches last paren. */
8567 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8572 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8575 case '1': case '2': case '3':
8576 case '4': case '5': case '6':
8577 case '7': case '8': case '9':
8582 } while (c != -1 && ISDIGIT(c));
8584 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8586 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8590 if (!parser_is_identchar(p)) {
8591 YYLTYPE loc = RUBY_INIT_YYLLOC();
8592 if (c == -1 || ISSPACE(c)) {
8593 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8597 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8599 parser_show_error_line(p, &loc);
8600 set_yylval_noname();
8608 if (tokadd_ident(p, c)) return 0;
8609 SET_LEX_STATE(EXPR_END);
8610 tokenize_ident(p, last_state);
8616 parser_numbered_param(struct parser_params *p, int n)
8618 if (n < 0) return false;
8620 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8623 if (p->max_numparam == ORDINAL_PARAM) {
8624 compile_error(p, "ordinary parameter is defined");
8627 struct vtable *args = p->lvtbl->args;
8628 if (p->max_numparam < n) {
8629 p->max_numparam = n;
8631 while (n > args->pos) {
8632 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8638 static enum yytokentype
8639 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8641 const char *ptr = p->lex.pcur;
8642 enum yytokentype result = tIVAR;
8643 register int c = nextc(p);
8646 p->lex.ptok = ptr - 1; /* from '@' */
8654 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8655 if (c == -1 || !parser_is_identchar(p)) {
8657 RUBY_SET_YYLLOC(loc);
8658 if (result == tIVAR) {
8659 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8662 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8664 parser_show_error_line(p, &loc);
8665 set_yylval_noname();
8666 SET_LEX_STATE(EXPR_END);
8669 else if (ISDIGIT(c)) {
8671 RUBY_SET_YYLLOC(loc);
8672 if (result == tIVAR) {
8673 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8676 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8678 parser_show_error_line(p, &loc);
8679 set_yylval_noname();
8680 SET_LEX_STATE(EXPR_END);
8684 if (tokadd_ident(p, c)) return 0;
8685 tokenize_ident(p, last_state);
8689 static enum yytokentype
8690 parse_ident(struct parser_params *p, int c, int cmd_state)
8692 enum yytokentype result;
8693 int mb = ENC_CODERANGE_7BIT;
8694 const enum lex_state_e last_state = p->lex.state;
8698 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8699 if (tokadd_mbchar(p, c) == -1) return 0;
8701 } while (parser_is_identchar(p));
8702 if ((c == '!' || c == '?') && !peek(p, '=')) {
8706 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8707 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8708 result = tIDENTIFIER;
8712 result = tCONSTANT; /* assume provisionally */
8717 if (IS_LABEL_POSSIBLE()) {
8718 if (IS_LABEL_SUFFIX(0)) {
8719 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8721 set_yylval_name(TOK_INTERN());
8725 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8726 const struct kwtable *kw;
8728 /* See if it is a reserved word. */
8729 kw = rb_reserved_word(tok(p), toklen(p));
8731 enum lex_state_e state = p->lex.state;
8732 SET_LEX_STATE(kw->state);
8733 if (IS_lex_state_for(state, EXPR_FNAME)) {
8734 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8737 if (IS_lex_state(EXPR_BEG)) {
8738 p->command_start = TRUE;
8740 if (kw->id[0] == keyword_do) {
8741 if (lambda_beginning_p()) {
8742 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8743 return keyword_do_LAMBDA;
8745 if (COND_P()) return keyword_do_cond;
8746 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8747 return keyword_do_block;
8750 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8753 if (kw->id[0] != kw->id[1])
8754 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8760 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8762 SET_LEX_STATE(EXPR_CMDARG);
8765 SET_LEX_STATE(EXPR_ARG);
8768 else if (p->lex.state == EXPR_FNAME) {
8769 SET_LEX_STATE(EXPR_ENDFN);
8772 SET_LEX_STATE(EXPR_END);
8775 ident = tokenize_ident(p, last_state);
8776 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8777 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8778 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8779 lvar_defined(p, ident)) {
8780 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8785 static enum yytokentype
8786 parser_yylex(struct parser_params *p)
8792 enum lex_state_e last_state;
8793 int fallthru = FALSE;
8794 int token_seen = p->token_seen;
8796 if (p->lex.strterm) {
8797 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8798 return here_document(p, &p->lex.strterm->u.heredoc);
8802 return parse_string(p, &p->lex.strterm->u.literal);
8805 cmd_state = p->command_start;
8806 p->command_start = FALSE;
8807 p->token_seen = TRUE;
8809 last_state = p->lex.state;
8813 switch (c = nextc(p)) {
8814 case '\0': /* NUL */
8815 case '\004': /* ^D */
8816 case '\032': /* ^Z */
8817 case -1: /* end of script. */
8821 case ' ': case '\t': case '\f': case '\r':
8822 case '\13': /* '\v' */
8825 while ((c = nextc(p))) {
8827 case ' ': case '\t': case '\f': case '\r':
8828 case '\13': /* '\v' */
8836 dispatch_scan_event(p, tSP);
8840 case '#': /* it's a comment */
8841 p->token_seen = token_seen;
8842 /* no magic_comment in shebang line */
8843 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8844 if (comment_at_top(p)) {
8845 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8849 dispatch_scan_event(p, tCOMMENT);
8853 p->token_seen = token_seen;
8854 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8855 !IS_lex_state(EXPR_LABELED));
8856 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8858 dispatch_scan_event(p, tIGNORED_NL);
8861 if (!c && p->in_kwarg) {
8862 goto normal_newline;
8867 switch (c = nextc(p)) {
8868 case ' ': case '\t': case '\f': case '\r':
8869 case '\13': /* '\v' */
8874 if (space_seen) dispatch_scan_event(p, tSP);
8878 dispatch_delayed_token(p, tIGNORED_NL);
8879 if (peek(p, '.') == (c == '&')) {
8881 dispatch_scan_event(p, tSP);
8886 p->ruby_sourceline--;
8887 p->lex.nextline = p->lex.lastline;
8888 case -1: /* EOF no decrement*/
8890 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8891 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8892 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8893 pushback(p, 1); /* always pushback */
8894 p->lex.ptok = p->lex.pcur;
8898 p->lex.ptok = p->lex.pcur;
8901 goto normal_newline;
8905 p->command_start = TRUE;
8906 SET_LEX_STATE(EXPR_BEG);
8910 if ((c = nextc(p)) == '*') {
8911 if ((c = nextc(p)) == '=') {
8912 set_yylval_id(idPow);
8913 SET_LEX_STATE(EXPR_BEG);
8918 rb_warning0("`**' interpreted as argument prefix");
8921 else if (IS_BEG()) {
8925 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8931 SET_LEX_STATE(EXPR_BEG);
8936 rb_warning0("`*' interpreted as argument prefix");
8939 else if (IS_BEG()) {
8943 c = warn_balanced('*', "*", "argument prefix");
8946 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8951 if (IS_AFTER_OPERATOR()) {
8952 SET_LEX_STATE(EXPR_ARG);
8958 SET_LEX_STATE(EXPR_BEG);
8971 /* skip embedded rd document */
8972 if (word_match_p(p, "begin", 5)) {
8976 dispatch_scan_event(p, tEMBDOC_BEG);
8980 dispatch_scan_event(p, tEMBDOC);
8985 compile_error(p, "embedded document meets end of file");
8988 if (c == '=' && word_match_p(p, "end", 3)) {
8994 dispatch_scan_event(p, tEMBDOC_END);
8999 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9000 if ((c = nextc(p)) == '=') {
9001 if ((c = nextc(p)) == '=') {
9010 else if (c == '>') {
9019 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9021 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9022 int token = heredoc_identifier(p);
9023 if (token) return token < 0 ? 0 : token;
9025 if (IS_AFTER_OPERATOR()) {
9026 SET_LEX_STATE(EXPR_ARG);
9029 if (IS_lex_state(EXPR_CLASS))
9030 p->command_start = TRUE;
9031 SET_LEX_STATE(EXPR_BEG);
9034 if ((c = nextc(p)) == '>') {
9041 if ((c = nextc(p)) == '=') {
9042 set_yylval_id(idLTLT);
9043 SET_LEX_STATE(EXPR_BEG);
9047 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9053 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9054 if ((c = nextc(p)) == '=') {
9058 if ((c = nextc(p)) == '=') {
9059 set_yylval_id(idGTGT);
9060 SET_LEX_STATE(EXPR_BEG);
9070 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9071 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9072 p->lex.ptok = p->lex.pcur-1;
9076 if (IS_lex_state(EXPR_FNAME)) {
9077 SET_LEX_STATE(EXPR_ENDFN);
9080 if (IS_lex_state(EXPR_DOT)) {
9082 SET_LEX_STATE(EXPR_CMDARG);
9084 SET_LEX_STATE(EXPR_ARG);
9087 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9088 return tXSTRING_BEG;
9091 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9092 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9093 p->lex.ptok = p->lex.pcur-1;
9097 return parse_qmark(p, space_seen);
9100 if ((c = nextc(p)) == '&') {
9101 SET_LEX_STATE(EXPR_BEG);
9102 if ((c = nextc(p)) == '=') {
9103 set_yylval_id(idANDOP);
9104 SET_LEX_STATE(EXPR_BEG);
9110 else if (c == '=') {
9112 SET_LEX_STATE(EXPR_BEG);
9115 else if (c == '.') {
9116 set_yylval_id(idANDDOT);
9117 SET_LEX_STATE(EXPR_DOT);
9123 (c = peekc_n(p, 1)) == -1 ||
9124 !(c == '\'' || c == '"' ||
9125 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9126 rb_warning0("`&' interpreted as argument prefix");
9130 else if (IS_BEG()) {
9134 c = warn_balanced('&', "&", "argument prefix");
9136 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9140 if ((c = nextc(p)) == '|') {
9141 SET_LEX_STATE(EXPR_BEG);
9142 if ((c = nextc(p)) == '=') {
9143 set_yylval_id(idOROP);
9144 SET_LEX_STATE(EXPR_BEG);
9148 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9157 SET_LEX_STATE(EXPR_BEG);
9160 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9166 if (IS_AFTER_OPERATOR()) {
9167 SET_LEX_STATE(EXPR_ARG);
9176 SET_LEX_STATE(EXPR_BEG);
9179 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9180 SET_LEX_STATE(EXPR_BEG);
9182 if (c != -1 && ISDIGIT(c)) {
9183 return parse_numeric(p, '+');
9187 SET_LEX_STATE(EXPR_BEG);
9189 return warn_balanced('+', "+", "unary operator");
9193 if (IS_AFTER_OPERATOR()) {
9194 SET_LEX_STATE(EXPR_ARG);
9203 SET_LEX_STATE(EXPR_BEG);
9207 SET_LEX_STATE(EXPR_ENDFN);
9210 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9211 SET_LEX_STATE(EXPR_BEG);
9213 if (c != -1 && ISDIGIT(c)) {
9218 SET_LEX_STATE(EXPR_BEG);
9220 return warn_balanced('-', "-", "unary operator");
9223 int is_beg = IS_BEG();
9224 SET_LEX_STATE(EXPR_BEG);
9225 if ((c = nextc(p)) == '.') {
9226 if ((c = nextc(p)) == '.') {
9227 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9228 rb_warn0("... at EOL, should be parenthesized?");
9230 return is_beg ? tBDOT3 : tDOT3;
9233 return is_beg ? tBDOT2 : tDOT2;
9236 if (c != -1 && ISDIGIT(c)) {
9237 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9238 parse_numeric(p, '.');
9239 if (ISDIGIT(prev)) {
9240 yyerror0("unexpected fraction part after numeric literal");
9243 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9245 SET_LEX_STATE(EXPR_END);
9246 p->lex.ptok = p->lex.pcur;
9250 SET_LEX_STATE(EXPR_DOT);
9254 case '0': case '1': case '2': case '3': case '4':
9255 case '5': case '6': case '7': case '8': case '9':
9256 return parse_numeric(p, c);
9261 SET_LEX_STATE(EXPR_ENDFN);
9262 p->lex.paren_nest--;
9268 SET_LEX_STATE(EXPR_END);
9269 p->lex.paren_nest--;
9273 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9274 if (!p->lex.brace_nest--) return tSTRING_DEND;
9277 SET_LEX_STATE(EXPR_END);
9278 p->lex.paren_nest--;
9284 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9285 SET_LEX_STATE(EXPR_BEG);
9288 set_yylval_id(idCOLON2);
9289 SET_LEX_STATE(EXPR_DOT);
9292 if (IS_END() || ISSPACE(c) || c == '#') {
9294 c = warn_balanced(':', ":", "symbol literal");
9295 SET_LEX_STATE(EXPR_BEG);
9300 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9303 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9309 SET_LEX_STATE(EXPR_FNAME);
9314 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9317 if ((c = nextc(p)) == '=') {
9319 SET_LEX_STATE(EXPR_BEG);
9324 arg_ambiguous(p, '/');
9325 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9328 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9329 return warn_balanced('/', "/", "regexp literal");
9332 if ((c = nextc(p)) == '=') {
9334 SET_LEX_STATE(EXPR_BEG);
9337 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9342 SET_LEX_STATE(EXPR_BEG);
9343 p->command_start = TRUE;
9347 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9351 if (IS_AFTER_OPERATOR()) {
9352 if ((c = nextc(p)) != '@') {
9355 SET_LEX_STATE(EXPR_ARG);
9358 SET_LEX_STATE(EXPR_BEG);
9366 else if (!space_seen) {
9367 /* foo( ... ) => method call, no ambiguity */
9369 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9372 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9373 rb_warning0("parentheses after method name is interpreted as "
9374 "an argument list, not a decomposed argument");
9376 p->lex.paren_nest++;
9379 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9383 p->lex.paren_nest++;
9384 if (IS_AFTER_OPERATOR()) {
9385 if ((c = nextc(p)) == ']') {
9386 SET_LEX_STATE(EXPR_ARG);
9387 if ((c = nextc(p)) == '=') {
9394 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9397 else if (IS_BEG()) {
9400 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9403 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9409 ++p->lex.brace_nest;
9410 if (lambda_beginning_p())
9412 else if (IS_lex_state(EXPR_LABELED))
9413 c = tLBRACE; /* hash */
9414 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9415 c = '{'; /* block (primary) */
9416 else if (IS_lex_state(EXPR_ENDARG))
9417 c = tLBRACE_ARG; /* block (expr) */
9419 c = tLBRACE; /* hash */
9421 p->command_start = TRUE;
9422 SET_LEX_STATE(EXPR_BEG);
9425 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9427 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9436 dispatch_scan_event(p, tSP);
9437 goto retry; /* skip \\n */
9439 if (c == ' ') return tSP;
9440 if (ISSPACE(c)) return c;
9445 return parse_percent(p, space_seen, last_state);
9448 return parse_gvar(p, last_state);
9451 return parse_atmark(p, last_state);
9454 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9455 p->ruby__end__seen = 1;
9461 dispatch_scan_event(p, k__END__);
9469 if (!parser_is_identchar(p)) {
9470 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9479 return parse_ident(p, c, cmd_state);
9482 static enum yytokentype
9483 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9489 t = parser_yylex(p);
9490 if (has_delayed_token(p))
9491 dispatch_delayed_token(p, t);
9493 dispatch_scan_event(p, t);
9495 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9496 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9498 RUBY_SET_YYLLOC(*yylloc);
9503 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9506 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9508 NODE *n = rb_ast_newnode(p->ast, type);
9510 rb_node_init(n, type, a0, a1, a2);
9513 nd_set_node_id(n, parser_get_node_id(p));
9518 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9521 nd_set_line(nd, loc->beg_pos.lineno);
9526 static enum node_type
9527 nodetype(NODE *node) /* for debug */
9529 return (enum node_type)nd_type(node);
9533 nodeline(NODE *node)
9535 return nd_line(node);
9539 newline_node(NODE *node)
9542 node = remove_begin(node);
9543 node->flags |= NODE_FL_NEWLINE;
9549 fixpos(NODE *node, NODE *orig)
9553 nd_set_line(node, nd_line(orig));
9557 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9559 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9563 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9565 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9569 block_append(struct parser_params *p, NODE *head, NODE *tail)
9571 NODE *end, *h = head, *nd;
9573 if (tail == 0) return head;
9575 if (h == 0) return tail;
9576 switch (nd_type(h)) {
9583 parser_warning(p, h, "unused literal ignored");
9586 h = end = NEW_BLOCK(head, &head->nd_loc);
9596 switch (nd_type(nd)) {
9602 if (RTEST(ruby_verbose)) {
9603 parser_warning(p, tail, "statement not reached");
9611 if (nd_type(tail) != NODE_BLOCK) {
9612 tail = NEW_BLOCK(tail, &tail->nd_loc);
9613 tail->nd_end = tail;
9615 end->nd_next = tail;
9616 h->nd_end = tail->nd_end;
9617 nd_set_last_loc(head, nd_last_loc(tail));
9621 /* append item to the list */
9623 list_append(struct parser_params *p, NODE *list, NODE *item)
9627 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9628 if (list->nd_next) {
9629 last = list->nd_next->nd_end;
9636 last->nd_next = NEW_LIST(item, &item->nd_loc);
9637 list->nd_next->nd_end = last->nd_next;
9639 nd_set_last_loc(list, nd_last_loc(item));
9644 /* concat two lists */
9646 list_concat(NODE *head, NODE *tail)
9650 if (head->nd_next) {
9651 last = head->nd_next->nd_end;
9657 head->nd_alen += tail->nd_alen;
9658 last->nd_next = tail;
9659 if (tail->nd_next) {
9660 head->nd_next->nd_end = tail->nd_next->nd_end;
9663 head->nd_next->nd_end = tail;
9666 nd_set_last_loc(head, nd_last_loc(tail));
9672 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9674 if (NIL_P(tail)) return 1;
9675 if (!rb_enc_compatible(head, tail)) {
9676 compile_error(p, "string literal encodings differ (%s / %s)",
9677 rb_enc_name(rb_enc_get(head)),
9678 rb_enc_name(rb_enc_get(tail)));
9679 rb_str_resize(head, 0);
9680 rb_str_resize(tail, 0);
9683 rb_str_buf_append(head, tail);
9687 /* concat two string literals */
9689 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9691 enum node_type htype;
9695 if (!head) return tail;
9696 if (!tail) return head;
9698 htype = nd_type(head);
9699 if (htype == NODE_EVSTR) {
9700 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9701 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9702 head = list_append(p, node, head);
9705 if (p->heredoc_indent > 0) {
9708 nd_set_type(head, NODE_DSTR);
9710 return list_append(p, head, tail);
9715 switch (nd_type(tail)) {
9717 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9718 nd_type(headlast) == NODE_STR) {
9720 lit = headlast->nd_lit;
9725 if (htype == NODE_STR) {
9726 if (!literal_concat0(p, lit, tail->nd_lit)) {
9728 rb_discard_node(p, head);
9729 rb_discard_node(p, tail);
9732 rb_discard_node(p, tail);
9735 list_append(p, head, tail);
9740 if (htype == NODE_STR) {
9741 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9743 tail->nd_lit = head->nd_lit;
9744 rb_discard_node(p, head);
9747 else if (NIL_P(tail->nd_lit)) {
9749 head->nd_alen += tail->nd_alen - 1;
9750 head->nd_next->nd_end->nd_next = tail->nd_next;
9751 head->nd_next->nd_end = tail->nd_next->nd_end;
9752 rb_discard_node(p, tail);
9754 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9755 nd_type(headlast) == NODE_STR) {
9756 lit = headlast->nd_lit;
9757 if (!literal_concat0(p, lit, tail->nd_lit))
9759 tail->nd_lit = Qnil;
9763 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9768 if (htype == NODE_STR) {
9769 nd_set_type(head, NODE_DSTR);
9772 list_append(p, head, tail);
9779 evstr2dstr(struct parser_params *p, NODE *node)
9781 if (nd_type(node) == NODE_EVSTR) {
9782 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9783 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9784 node = list_append(p, dstr, node);
9790 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9795 switch (nd_type(node)) {
9796 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9800 return NEW_EVSTR(head, loc);
9804 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9805 const YYLTYPE *op_loc, const YYLTYPE *loc)
9810 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9811 nd_set_line(expr, op_loc->beg_pos.lineno);
9816 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9820 opcall = NEW_OPCALL(recv, id, 0, loc);
9821 nd_set_line(opcall, op_loc->beg_pos.lineno);
9826 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9828 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9829 nd_set_line(qcall, op_loc->beg_pos.lineno);
9834 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9837 if (block) block_dup_check(p, args, block);
9838 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9839 if (block) ret = method_add_block(p, ret, block, loc);
9844 #define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9846 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9849 int line = op_loc->beg_pos.lineno;
9853 if (node1 && (n = nd_once_body(node1)) != 0) {
9854 switch (nd_type(n)) {
9857 NODE *match = NEW_MATCH2(node1, node2, loc);
9858 nd_set_line(match, line);
9863 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9864 const VALUE lit = n->nd_lit;
9865 NODE *match = NEW_MATCH2(node1, node2, loc);
9866 match->nd_args = reg_named_capture_assign(p, lit, loc);
9867 nd_set_line(match, line);
9873 if (node2 && (n = nd_once_body(node2)) != 0) {
9876 switch (nd_type(n)) {
9878 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9881 match3 = NEW_MATCH3(node2, node1, loc);
9886 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9887 nd_set_line(n, line);
9891 # if WARN_PAST_SCOPE
9893 past_dvar_p(struct parser_params *p, ID id)
9895 struct vtable *past = p->lvtbl->past;
9897 if (vtable_included(past, id)) return 1;
9904 /* As Ripper#warn does not have arguments for the location, so the
9905 * following messages cannot be separated */
9906 #define WARN_LOCATION(type) do { \
9907 if (p->warn_location) { \
9909 VALUE file = rb_source_location(&line); \
9910 rb_warn3(type" in eval may not return location in binding;" \
9911 " use Binding#source_location instead\n" \
9912 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9913 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9918 numparam_nested_p(struct parser_params *p)
9920 struct local_vars *local = p->lvtbl;
9921 NODE *outer = local->numparam.outer;
9922 NODE *inner = local->numparam.inner;
9923 if (outer || inner) {
9924 NODE *used = outer ? outer : inner;
9925 compile_error(p, "numbered parameter is already used in\n"
9926 "%s:%d: %s block here",
9927 p->ruby_sourcefile, nd_line(used),
9928 outer ? "outer" : "inner");
9929 parser_show_error_line(p, &used->nd_loc);
9936 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9942 return NEW_SELF(loc);
9944 return NEW_NIL(loc);
9946 return NEW_TRUE(loc);
9948 return NEW_FALSE(loc);
9949 case keyword__FILE__:
9950 WARN_LOCATION("__FILE__");
9952 VALUE file = p->ruby_sourcefile_string;
9954 file = rb_str_new(0, 0);
9956 file = rb_str_dup(file);
9957 node = NEW_STR(file, loc);
9958 RB_OBJ_WRITTEN(p->ast, Qnil, file);
9961 case keyword__LINE__:
9962 WARN_LOCATION("__LINE__");
9963 return NEW_LIT(INT2FIX(p->tokline), loc);
9964 case keyword__ENCODING__:
9965 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
9966 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9970 switch (id_type(id)) {
9972 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
9973 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
9974 if (id == p->cur_arg) {
9975 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9978 if (vidp) *vidp |= LVAR_USED;
9979 node = NEW_DVAR(id, loc);
9982 if (local_id_ref(p, id, &vidp)) {
9983 if (id == p->cur_arg) {
9984 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9987 if (vidp) *vidp |= LVAR_USED;
9988 node = NEW_LVAR(id, loc);
9991 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
9992 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
9993 if (numparam_nested_p(p)) return 0;
9994 node = NEW_DVAR(id, loc);
9995 struct local_vars *local = p->lvtbl;
9996 if (!local->numparam.current) local->numparam.current = node;
9999 # if WARN_PAST_SCOPE
10000 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10001 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10004 /* method call without arguments */
10005 return NEW_VCALL(id, loc);
10007 return NEW_GVAR(id, loc);
10009 return NEW_IVAR(id, loc);
10011 return NEW_CONST(id, loc);
10013 return NEW_CVAR(id, loc);
10015 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10020 opt_arg_append(NODE *opt_list, NODE *opt)
10022 NODE *opts = opt_list;
10023 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10025 while (opts->nd_next) {
10026 opts = opts->nd_next;
10027 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10029 opts->nd_next = opt;
10035 kwd_append(NODE *kwlist, NODE *kw)
10038 NODE *kws = kwlist;
10039 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10040 while (kws->nd_next) {
10041 kws = kws->nd_next;
10042 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10050 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10052 return NEW_DEFINED(remove_begin_all(expr), loc);
10056 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10058 if (nd_type(symbol) == NODE_DSTR) {
10059 nd_set_type(symbol, NODE_DSYM);
10062 nd_set_type(symbol, NODE_LIT);
10063 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10065 return list_append(p, symbols, symbol);
10069 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10075 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10076 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10079 switch (nd_type(node)) {
10082 VALUE src = node->nd_lit;
10083 nd_set_type(node, NODE_LIT);
10084 nd_set_loc(node, loc);
10085 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10090 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10091 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10094 nd_set_type(node, NODE_DREGX);
10095 nd_set_loc(node, loc);
10096 node->nd_cflag = options & RE_OPTION_MASK;
10097 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10098 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10099 if (nd_type(list->nd_head) == NODE_STR) {
10100 VALUE tail = list->nd_head->nd_lit;
10101 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10102 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10103 if (!literal_concat0(p, lit, tail)) {
10104 return NEW_NIL(loc); /* dummy node on error */
10106 rb_str_resize(tail, 0);
10107 prev->nd_next = list->nd_next;
10108 rb_discard_node(p, list->nd_head);
10109 rb_discard_node(p, list);
10120 if (!node->nd_next) {
10121 VALUE src = node->nd_lit;
10122 nd_set_type(node, NODE_LIT);
10123 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10125 if (options & RE_OPTION_ONCE) {
10126 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10134 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10137 return NEW_KW_ARG(0, (k), loc);
10141 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10144 VALUE lit = STR_NEW0();
10145 NODE *xstr = NEW_XSTR(lit, loc);
10146 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10149 switch (nd_type(node)) {
10151 nd_set_type(node, NODE_XSTR);
10152 nd_set_loc(node, loc);
10155 nd_set_type(node, NODE_DXSTR);
10156 nd_set_loc(node, loc);
10159 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10166 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10170 if (!arg || !p->case_labels) return;
10172 lit = rb_node_case_when_optimizable_literal(arg);
10173 if (lit == Qundef) return;
10174 if (nd_type(arg) == NODE_STR) {
10175 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10178 if (NIL_P(p->case_labels)) {
10179 p->case_labels = rb_obj_hide(rb_hash_new());
10182 VALUE line = rb_hash_lookup(p->case_labels, lit);
10183 if (!NIL_P(line)) {
10184 rb_warning1("duplicated `when' clause with line %d is ignored",
10189 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10192 #else /* !RIPPER */
10194 id_is_var(struct parser_params *p, ID id)
10196 if (is_notop_id(id)) {
10197 switch (id & ID_SCOPE_MASK) {
10198 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10201 if (dyna_in_block(p)) {
10202 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10204 if (local_id(p, id)) return 1;
10205 /* method call without arguments */
10209 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10214 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10216 VALUE src = 0, err;
10218 if (ripper_is_node_yylval(re)) {
10219 src = RNODE(re)->nd_cval;
10220 re = RNODE(re)->nd_rval;
10222 if (ripper_is_node_yylval(opt)) {
10223 options = (int)RNODE(opt)->nd_tag;
10224 opt = RNODE(opt)->nd_rval;
10226 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10227 compile_error(p, "%"PRIsVALUE, err);
10229 return dispatch2(regexp_literal, re, opt);
10231 #endif /* !RIPPER */
10235 static const char rb_parser_lex_state_names[][8] = {
10236 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10237 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10238 "LABEL", "LABELED","FITEM",
10242 append_lex_state_name(enum lex_state_e state, VALUE buf)
10245 unsigned int mask = 1;
10246 static const char none[] = "NONE";
10248 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10249 if ((unsigned)state & mask) {
10251 rb_str_cat(buf, "|", 1);
10254 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10258 rb_str_cat(buf, none, sizeof(none)-1);
10264 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10266 VALUE mesg = p->debug_buffer;
10268 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10269 p->debug_buffer = Qnil;
10270 rb_io_puts(1, &mesg, out);
10272 if (!NIL_P(str) && RSTRING_LEN(str)) {
10273 rb_io_write(p->debug_output, str);
10278 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10279 enum lex_state_e to, int line)
10282 mesg = rb_str_new_cstr("lex_state: ");
10283 append_lex_state_name(from, mesg);
10284 rb_str_cat_cstr(mesg, " -> ");
10285 append_lex_state_name(to, mesg);
10286 rb_str_catf(mesg, " at line %d\n", line);
10287 flush_debug_buffer(p, p->debug_output, mesg);
10292 rb_parser_lex_state_name(enum lex_state_e state)
10294 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10298 append_bitstack_value(stack_type stack, VALUE mesg)
10301 rb_str_cat_cstr(mesg, "0");
10304 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10305 for (; mask && !(stack & mask); mask >>= 1) continue;
10306 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10311 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10312 const char *name, int line)
10314 VALUE mesg = rb_sprintf("%s: ", name);
10315 append_bitstack_value(stack, mesg);
10316 rb_str_catf(mesg, " at line %d\n", line);
10317 flush_debug_buffer(p, p->debug_output, mesg);
10321 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10324 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10327 rb_str_vcatf(mesg, fmt, ap);
10329 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10332 mesg = rb_str_new(0, 0);
10333 append_lex_state_name(p->lex.state, mesg);
10334 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10335 rb_str_resize(mesg, 0);
10336 append_bitstack_value(p->cond_stack, mesg);
10337 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10338 rb_str_resize(mesg, 0);
10339 append_bitstack_value(p->cmdarg_stack, mesg);
10340 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10341 if (p->debug_output == rb_stdout)
10342 p->debug_output = rb_stderr;
10347 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10349 int sourceline = here->sourceline;
10350 int beg_pos = (int)here->offset - here->quote
10351 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10352 int end_pos = (int)here->offset + here->length + here->quote;
10354 yylloc->beg_pos.lineno = sourceline;
10355 yylloc->beg_pos.column = beg_pos;
10356 yylloc->end_pos.lineno = sourceline;
10357 yylloc->end_pos.column = end_pos;
10362 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10364 yylloc->beg_pos.lineno = p->ruby_sourceline;
10365 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10366 yylloc->end_pos.lineno = p->ruby_sourceline;
10367 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10372 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10374 yylloc->beg_pos.lineno = p->ruby_sourceline;
10375 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10376 yylloc->end_pos.lineno = p->ruby_sourceline;
10377 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10380 #endif /* !RIPPER */
10383 parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10388 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10389 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10391 v = rb_id2str(valp->id);
10393 v = valp->node->nd_rval;
10395 rb_parser_printf(p, "%"PRIsVALUE, v);
10397 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10398 case tSTRING_CONTENT: case tCHAR:
10400 v = valp->node->nd_lit;
10404 rb_parser_printf(p, "%+"PRIsVALUE, v);
10408 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10410 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10415 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10417 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10426 assignable0(struct parser_params *p, ID id, const char **err)
10428 if (!id) return -1;
10431 *err = "Can't change the value of self";
10434 *err = "Can't assign to nil";
10437 *err = "Can't assign to true";
10439 case keyword_false:
10440 *err = "Can't assign to false";
10442 case keyword__FILE__:
10443 *err = "Can't assign to __FILE__";
10445 case keyword__LINE__:
10446 *err = "Can't assign to __LINE__";
10448 case keyword__ENCODING__:
10449 *err = "Can't assign to __ENCODING__";
10452 switch (id_type(id)) {
10454 if (dyna_in_block(p)) {
10455 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10456 compile_error(p, "Can't assign to numbered parameter _%d",
10457 NUMPARAM_ID_TO_IDX(id));
10460 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10461 if (dvar_defined(p, id)) return NODE_DASGN;
10462 if (local_id(p, id)) return NODE_LASGN;
10464 return NODE_DASGN_CURR;
10467 if (!local_id(p, id)) local_var(p, id);
10471 case ID_GLOBAL: return NODE_GASGN;
10472 case ID_INSTANCE: return NODE_IASGN;
10474 if (!p->in_def) return NODE_CDECL;
10475 *err = "dynamic constant assignment";
10477 case ID_CLASS: return NODE_CVASGN;
10479 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10486 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10488 const char *err = 0;
10489 int node_type = assignable0(p, id, &err);
10490 switch (node_type) {
10491 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10492 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10493 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10494 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10495 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10496 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10497 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10499 if (err) yyerror1(loc, err);
10500 return NEW_BEGIN(0, loc);
10504 assignable(struct parser_params *p, VALUE lhs)
10506 const char *err = 0;
10507 assignable0(p, get_id(lhs), &err);
10508 if (err) lhs = assign_error(p, lhs);
10514 is_private_local_id(ID name)
10517 if (name == idUScore) return 1;
10518 if (!is_local_id(name)) return 0;
10519 s = rb_id2str(name);
10521 return RSTRING_PTR(s)[0] == '_';
10525 shadowing_lvar_0(struct parser_params *p, ID name)
10527 if (is_private_local_id(name)) return 1;
10528 if (dyna_in_block(p)) {
10529 if (dvar_curr(p, name)) {
10530 yyerror0("duplicated argument name");
10532 else if (dvar_defined(p, name) || local_id(p, name)) {
10533 vtable_add(p->lvtbl->vars, name);
10534 if (p->lvtbl->used) {
10535 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10541 if (local_id(p, name)) {
10542 yyerror0("duplicated argument name");
10549 shadowing_lvar(struct parser_params *p, ID name)
10551 shadowing_lvar_0(p, name);
10556 new_bv(struct parser_params *p, ID name)
10559 if (!is_local_id(name)) {
10560 compile_error(p, "invalid local variable - %"PRIsVALUE,
10564 if (!shadowing_lvar_0(p, name)) return;
10570 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10572 return NEW_ATTRASGN(recv, tASET, idx, loc);
10576 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10578 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10579 compile_error(p, "both block arg and actual block given");
10584 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10586 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10587 return NEW_ATTRASGN(recv, id, 0, loc);
10591 rb_backref_error(struct parser_params *p, NODE *node)
10593 switch (nd_type(node)) {
10595 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10597 case NODE_BACK_REF:
10598 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10604 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10606 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10607 switch (nd_type(node1)) {
10609 return list_append(p, node1, node2);
10610 case NODE_BLOCK_PASS:
10611 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10612 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10614 case NODE_ARGSPUSH:
10615 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10616 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10617 nd_set_type(node1, NODE_ARGSCAT);
10620 if (nd_type(node1->nd_body) != NODE_LIST) break;
10621 node1->nd_body = list_append(p, node1->nd_body, node2);
10622 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10625 return NEW_ARGSPUSH(node1, node2, loc);
10629 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10631 if (!node2) return node1;
10632 switch (nd_type(node1)) {
10633 case NODE_BLOCK_PASS:
10634 if (node1->nd_head)
10635 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10637 node1->nd_head = NEW_LIST(node2, loc);
10639 case NODE_ARGSPUSH:
10640 if (nd_type(node2) != NODE_LIST) break;
10641 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10642 nd_set_type(node1, NODE_ARGSCAT);
10645 if (nd_type(node2) != NODE_LIST ||
10646 nd_type(node1->nd_body) != NODE_LIST) break;
10647 node1->nd_body = list_concat(node1->nd_body, node2);
10650 return NEW_ARGSCAT(node1, node2, loc);
10654 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10657 if ((n1 = splat_array(args)) != 0) {
10658 return list_append(p, n1, last_arg);
10660 return arg_append(p, args, last_arg, loc);
10664 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10667 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10668 return list_concat(n1, rest_arg);
10670 return arg_concat(p, args, rest_arg, loc);
10674 splat_array(NODE* node)
10676 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10677 if (nd_type(node) == NODE_LIST) return node;
10682 mark_lvar_used(struct parser_params *p, NODE *rhs)
10686 switch (nd_type(rhs)) {
10688 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10689 if (vidp) *vidp |= LVAR_USED;
10693 case NODE_DASGN_CURR:
10694 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10695 if (vidp) *vidp |= LVAR_USED;
10700 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10701 mark_lvar_used(p, rhs->nd_head);
10709 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10711 if (!lhs) return 0;
10713 switch (nd_type(lhs)) {
10718 case NODE_DASGN_CURR:
10722 lhs->nd_value = rhs;
10723 nd_set_loc(lhs, loc);
10726 case NODE_ATTRASGN:
10727 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10728 nd_set_loc(lhs, loc);
10732 /* should not happen */
10740 value_expr_check(struct parser_params *p, NODE *node)
10742 NODE *void_node = 0, *vn;
10745 rb_warning0("empty expression");
10748 switch (nd_type(node)) {
10754 return void_node ? void_node : node;
10757 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10758 compile_error(p, "unexpected node");
10761 if (node->nd_body->nd_body) {
10764 /* single line pattern matching */
10765 return void_node ? void_node : node;
10768 while (node->nd_next) {
10769 node = node->nd_next;
10771 node = node->nd_head;
10775 node = node->nd_body;
10780 if (!node->nd_body) {
10783 else if (!node->nd_else) {
10786 vn = value_expr_check(p, node->nd_body);
10787 if (!vn) return NULL;
10788 if (!void_node) void_node = vn;
10789 node = node->nd_else;
10794 node = node->nd_1st;
10799 case NODE_DASGN_CURR:
10801 mark_lvar_used(p, node);
10813 value_expr_gen(struct parser_params *p, NODE *node)
10815 NODE *void_node = value_expr_check(p, node);
10817 yyerror1(&void_node->nd_loc, "void value expression");
10818 /* or "control never reach"? */
10824 void_expr(struct parser_params *p, NODE *node)
10826 const char *useless = 0;
10828 if (!RTEST(ruby_verbose)) return;
10830 if (!node || !(node = nd_once_body(node))) return;
10831 switch (nd_type(node)) {
10833 switch (node->nd_mid) {
10852 useless = rb_id2name(node->nd_mid);
10863 case NODE_BACK_REF:
10864 useless = "a variable";
10867 useless = "a constant";
10873 useless = "a literal";
10898 useless = "defined?";
10903 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10908 void_stmts(struct parser_params *p, NODE *node)
10910 NODE *const n = node;
10911 if (!RTEST(ruby_verbose)) return n;
10912 if (!node) return n;
10913 if (nd_type(node) != NODE_BLOCK) return n;
10915 while (node->nd_next) {
10916 void_expr(p, node->nd_head);
10917 node = node->nd_next;
10923 remove_begin(NODE *node)
10925 NODE **n = &node, *n1 = node;
10926 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10927 *n = n1 = n1->nd_body;
10933 remove_begin_all(NODE *node)
10935 NODE **n = &node, *n1 = node;
10936 while (n1 && nd_type(n1) == NODE_BEGIN) {
10937 *n = n1 = n1->nd_body;
10943 reduce_nodes(struct parser_params *p, NODE **body)
10945 NODE *node = *body;
10948 *body = NEW_NIL(&NULL_LOC);
10951 #define subnodes(n1, n2) \
10952 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10953 (!node->n2) ? (body = &node->n1, 1) : \
10954 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10957 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10958 switch (nd_type(node)) {
10964 *body = node = node->nd_stts;
10965 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10968 *body = node = node->nd_body;
10969 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10972 body = &node->nd_end->nd_head;
10976 if (subnodes(nd_body, nd_else)) break;
10979 body = &node->nd_body;
10982 if (!subnodes(nd_body, nd_next)) goto end;
10985 if (!subnodes(nd_head, nd_resq)) goto end;
10988 if (node->nd_else) {
10989 body = &node->nd_resq;
10992 if (!subnodes(nd_head, nd_resq)) goto end;
10998 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11005 is_static_content(NODE *node)
11007 if (!node) return 1;
11008 switch (nd_type(node)) {
11010 if (!(node = node->nd_head)) break;
11013 if (!is_static_content(node->nd_head)) return 0;
11014 } while ((node = node->nd_next) != 0);
11029 assign_in_cond(struct parser_params *p, NODE *node)
11031 switch (nd_type(node)) {
11035 case NODE_DASGN_CURR:
11044 if (!node->nd_value) return 1;
11045 if (is_static_content(node->nd_value)) {
11046 /* reports always */
11047 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11058 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11060 case COND_IN_OP: break; \
11061 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11062 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11065 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11068 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11070 enum node_type type;
11072 if (node == 0) return 0;
11074 type = nd_type(node);
11076 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11077 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11078 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11080 return cond0(p, node, COND_IN_FF, loc);
11084 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11086 if (node == 0) return 0;
11087 if (!(node = nd_once_body(node))) return 0;
11088 assign_in_cond(p, node);
11090 switch (nd_type(node)) {
11094 SWITCH_BY_COND_TYPE(type, warn, "string ")
11098 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11100 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11104 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11105 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11110 node->nd_beg = range_op(p, node->nd_beg, loc);
11111 node->nd_end = range_op(p, node->nd_end, loc);
11112 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11113 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11117 SWITCH_BY_COND_TYPE(type, warning, "string ")
11121 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11122 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11123 nd_set_type(node, NODE_MATCH);
11125 else if (node->nd_lit == Qtrue ||
11126 node->nd_lit == Qfalse) {
11127 /* booleans are OK, e.g., while true */
11130 SWITCH_BY_COND_TYPE(type, warning, "")
11139 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11141 if (node == 0) return 0;
11142 return cond0(p, node, COND_IN_COND, loc);
11146 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11148 if (node == 0) return 0;
11149 return cond0(p, node, COND_IN_OP, loc);
11153 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11155 if (!cc) return right;
11156 cc = cond0(p, cc, COND_IN_COND, loc);
11157 return newline_node(NEW_IF(cc, left, right, loc));
11161 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11163 if (!cc) return right;
11164 cc = cond0(p, cc, COND_IN_COND, loc);
11165 return newline_node(NEW_UNLESS(cc, left, right, loc));
11169 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11170 const YYLTYPE *op_loc, const YYLTYPE *loc)
11172 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11175 if (left && (enum node_type)nd_type(left) == type) {
11176 NODE *node = left, *second;
11177 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11180 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11181 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11182 left->nd_loc.end_pos = loc->end_pos;
11185 op = NEW_NODE(type, left, right, 0, loc);
11186 nd_set_line(op, op_loc->beg_pos.lineno);
11191 no_blockarg(struct parser_params *p, NODE *node)
11193 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11194 compile_error(p, "block argument should not be given");
11199 ret_args(struct parser_params *p, NODE *node)
11202 no_blockarg(p, node);
11203 if (nd_type(node) == NODE_LIST) {
11204 if (node->nd_next == 0) {
11205 node = node->nd_head;
11208 nd_set_type(node, NODE_VALUES);
11216 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11218 if (node) no_blockarg(p, node);
11220 return NEW_YIELD(node, loc);
11224 negate_lit(struct parser_params *p, VALUE lit)
11226 if (FIXNUM_P(lit)) {
11227 return LONG2FIX(-FIX2LONG(lit));
11229 if (SPECIAL_CONST_P(lit)) {
11231 if (FLONUM_P(lit)) {
11232 return DBL2NUM(-RFLOAT_VALUE(lit));
11237 switch (BUILTIN_TYPE(lit)) {
11239 BIGNUM_NEGATE(lit);
11240 lit = rb_big_norm(lit);
11243 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11246 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11247 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11250 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11254 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11255 rb_builtin_class_name(lit));
11262 arg_blk_pass(NODE *node1, NODE *node2)
11265 if (!node1) return node2;
11266 node2->nd_head = node1;
11267 nd_set_first_lineno(node2, nd_first_lineno(node1));
11268 nd_set_first_column(node2, nd_first_column(node1));
11275 args_info_empty_p(struct rb_args_info *args)
11277 if (args->pre_args_num) return false;
11278 if (args->post_args_num) return false;
11279 if (args->rest_arg) return false;
11280 if (args->opt_args) return false;
11281 if (args->block_arg) return false;
11282 if (args->kw_args) return false;
11283 if (args->kw_rest_arg) return false;
11288 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11290 int saved_line = p->ruby_sourceline;
11291 struct rb_args_info *args = tail->nd_ainfo;
11293 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11294 args->pre_init = pre_args ? pre_args->nd_next : 0;
11296 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11297 args->post_init = post_args ? post_args->nd_next : 0;
11298 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11300 args->rest_arg = rest_arg;
11302 args->opt_args = opt_args;
11304 args->ruby2_keywords = rest_arg == idFWD_REST;
11306 p->ruby_sourceline = saved_line;
11307 nd_set_loc(tail, loc);
11313 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11315 int saved_line = p->ruby_sourceline;
11317 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11318 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11319 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11320 args->imemo = tmpbuf;
11321 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11322 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11323 if (p->error_p) return node;
11325 args->block_arg = block;
11326 args->kw_args = kw_args;
11330 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11331 * variable order: k1, kr1, k2, &b, internal_id, krest
11333 * variable order: kr1, k1, k2, internal_id, krest, &b
11335 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11336 struct vtable *vtargs = p->lvtbl->args;
11337 NODE *kwn = kw_args;
11339 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11340 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11342 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11344 --required_kw_vars;
11345 kwn = kwn->nd_next;
11348 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11349 ID vid = kwn->nd_body->nd_vid;
11350 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11351 *required_kw_vars++ = vid;
11358 arg_var(p, kw_bits);
11359 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11360 if (block) arg_var(p, block);
11362 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11363 args->kw_rest_arg->nd_cflag = kw_bits;
11365 else if (kw_rest_arg == idNil) {
11366 args->no_kwarg = 1;
11368 else if (kw_rest_arg) {
11369 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11372 p->ruby_sourceline = saved_line;
11377 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11379 if (max_numparam > NO_PARAM) {
11380 if (!args) args = new_args_tail(p, 0, 0, 0, 0);
11381 args->nd_ainfo->pre_args_num = max_numparam;
11387 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11389 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11391 aryptn->nd_pconst = constant;
11394 NODE *pre_args = NEW_LIST(pre_arg, loc);
11395 if (apinfo->pre_args) {
11396 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11399 apinfo->pre_args = pre_args;
11406 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11408 int saved_line = p->ruby_sourceline;
11410 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11411 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11412 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11413 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11414 apinfo->imemo = tmpbuf;
11415 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11417 apinfo->pre_args = pre_args;
11421 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11424 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11428 apinfo->rest_arg = NULL;
11431 apinfo->post_args = post_args;
11433 p->ruby_sourceline = saved_line;
11438 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11440 hshptn->nd_pconst = constant;
11445 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11447 int saved_line = p->ruby_sourceline;
11448 NODE *node, *kw_rest_arg_node;
11450 if (kw_rest_arg == idNil) {
11451 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11453 else if (kw_rest_arg) {
11454 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11457 kw_rest_arg_node = NULL;
11460 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11462 p->ruby_sourceline = saved_line;
11467 new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11469 NODE *node = NEW_CASE3(val, pat, loc);
11471 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11472 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11477 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11482 return NEW_LIT(ID2SYM(idNULL), loc);
11485 switch (nd_type(node)) {
11487 nd_set_type(node, NODE_DSYM);
11488 nd_set_loc(node, loc);
11491 lit = node->nd_lit;
11492 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11493 nd_set_type(node, NODE_LIT);
11494 nd_set_loc(node, loc);
11497 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11504 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11506 NODE *node = (NODE *)v;
11507 NODE **result = (NODE **)h;
11509 node->nd_next->nd_end = node->nd_next;
11510 node->nd_next->nd_next = 0;
11512 list_concat(*result, node);
11515 return ST_CONTINUE;
11519 remove_duplicate_keys(struct parser_params *p, NODE *hash)
11521 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11523 rb_code_location_t loc = hash->nd_loc;
11524 while (hash && hash->nd_head && hash->nd_next) {
11525 NODE *head = hash->nd_head;
11526 NODE *value = hash->nd_next;
11527 NODE *next = value->nd_next;
11528 VALUE key = (VALUE)head;
11530 if (nd_type(head) == NODE_LIT &&
11531 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11532 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11533 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11534 head->nd_lit, nd_line(head));
11535 head = ((NODE *)data)->nd_next;
11536 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11539 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11543 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11544 st_free_table(literal_keys);
11546 if (!result) result = hash;
11547 else list_concat(result, hash);
11549 result->nd_loc = loc;
11554 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11556 if (hash) hash = remove_duplicate_keys(p, hash);
11557 return NEW_HASH(hash, loc);
11562 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11564 if (is_private_local_id(id)) {
11567 if (st_is_member(p->pvtbl, id)) {
11568 yyerror1(loc, "duplicated variable name");
11571 st_insert(p->pvtbl, (st_data_t)id, 0);
11576 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11579 p->pktbl = st_init_numtable();
11581 else if (st_is_member(p->pktbl, key)) {
11582 yyerror1(loc, "duplicated key name");
11585 st_insert(p->pktbl, (st_data_t)key, 0);
11590 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11592 return NEW_HASH(hash, loc);
11594 #endif /* !RIPPER */
11598 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11603 ID vid = lhs->nd_vid;
11604 YYLTYPE lhs_loc = lhs->nd_loc;
11606 lhs->nd_value = rhs;
11607 nd_set_loc(lhs, loc);
11608 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11609 if (is_notop_id(vid)) {
11610 switch (id_type(vid)) {
11614 asgn->nd_aid = vid;
11618 else if (op == tANDOP) {
11619 lhs->nd_value = rhs;
11620 nd_set_loc(lhs, loc);
11621 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11625 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11626 nd_set_loc(asgn, loc);
11630 asgn = NEW_BEGIN(0, loc);
11636 new_ary_op_assign(struct parser_params *p, NODE *ary,
11637 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11641 args = make_list(args, args_loc);
11642 if (nd_type(args) == NODE_BLOCK_PASS) {
11643 args = NEW_ARGSCAT(args, rhs, loc);
11646 args = arg_concat(p, args, rhs, loc);
11648 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11654 new_attr_op_assign(struct parser_params *p, NODE *lhs,
11655 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11659 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11665 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11670 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11673 asgn = NEW_BEGIN(0, loc);
11680 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11683 yyerror1(loc, "dynamic constant assignment");
11685 return NEW_CDECL(0, 0, (path), loc);
11689 const_decl(struct parser_params *p, VALUE path)
11692 path = dispatch1(assign_error, path);
11699 assign_error(struct parser_params *p, VALUE a)
11701 a = dispatch1(assign_error, a);
11707 var_field(struct parser_params *p, VALUE a)
11709 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11715 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11717 NODE *result = head;
11719 NODE *tmp = rescue_else ? rescue_else : rescue;
11720 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11722 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11723 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11725 else if (rescue_else) {
11726 result = block_append(p, result, rescue_else);
11729 result = NEW_ENSURE(result, ensure, loc);
11731 fixpos(result, head);
11737 warn_unused_var(struct parser_params *p, struct local_vars *local)
11741 if (!local->used) return;
11742 cnt = local->used->pos;
11743 if (cnt != local->vars->pos) {
11744 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11747 ID *v = local->vars->tbl;
11748 ID *u = local->used->tbl;
11749 for (int i = 0; i < cnt; ++i) {
11750 if (!v[i] || (u[i] & LVAR_USED)) continue;
11751 if (is_private_local_id(v[i])) continue;
11752 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11758 local_push(struct parser_params *p, int toplevel_scope)
11760 struct local_vars *local;
11761 int inherits_dvars = toplevel_scope && compile_for_eval;
11762 int warn_unused_vars = RTEST(ruby_verbose);
11764 local = ALLOC(struct local_vars);
11765 local->prev = p->lvtbl;
11766 local->args = vtable_alloc(0);
11767 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11769 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11770 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11771 local->numparam.outer = 0;
11772 local->numparam.inner = 0;
11773 local->numparam.current = 0;
11775 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11777 # if WARN_PAST_SCOPE
11786 local_pop(struct parser_params *p)
11788 struct local_vars *local = p->lvtbl->prev;
11789 if (p->lvtbl->used) {
11790 warn_unused_var(p, p->lvtbl);
11791 vtable_free(p->lvtbl->used);
11793 # if WARN_PAST_SCOPE
11794 while (p->lvtbl->past) {
11795 struct vtable *past = p->lvtbl->past;
11796 p->lvtbl->past = past->prev;
11800 vtable_free(p->lvtbl->args);
11801 vtable_free(p->lvtbl->vars);
11804 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11810 local_tbl(struct parser_params *p)
11812 int cnt_args = vtable_size(p->lvtbl->args);
11813 int cnt_vars = vtable_size(p->lvtbl->vars);
11814 int cnt = cnt_args + cnt_vars;
11819 if (cnt <= 0) return 0;
11820 tbl = rb_imemo_tmpbuf_auto_free_pointer();
11821 buf = ALLOC_N(ID, cnt + 2);
11822 rb_imemo_tmpbuf_set_ptr(tbl, buf);
11823 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11824 /* remove IDs duplicated to warn shadowing */
11825 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11826 ID id = p->lvtbl->vars->tbl[i];
11827 if (!vtable_included(p->lvtbl->args, id)) {
11832 REALLOC_N(buf, ID, (cnt = j) + 2);
11833 rb_imemo_tmpbuf_set_ptr(tbl, buf);
11836 buf[cnt + 1] = (ID)tbl;
11837 RB_OBJ_WRITTEN(p->ast, Qnil, tbl);
11843 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11849 n = NEW_NODE(type, a0, a1, a2, loc);
11856 numparam_name(struct parser_params *p, ID id)
11858 if (!NUMPARAM_ID_P(id)) return;
11859 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11860 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11864 arg_var(struct parser_params *p, ID id)
11866 numparam_name(p, id);
11867 vtable_add(p->lvtbl->args, id);
11871 local_var(struct parser_params *p, ID id)
11873 numparam_name(p, id);
11874 vtable_add(p->lvtbl->vars, id);
11875 if (p->lvtbl->used) {
11876 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11881 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11883 struct vtable *vars, *args, *used;
11885 vars = p->lvtbl->vars;
11886 args = p->lvtbl->args;
11887 used = p->lvtbl->used;
11889 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11892 if (used) used = used->prev;
11895 if (vars && vars->prev == DVARS_INHERIT) {
11896 return rb_local_defined(id, p->parent_iseq);
11898 else if (vtable_included(args, id)) {
11902 int i = vtable_included(vars, id);
11903 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11909 local_id(struct parser_params *p, ID id)
11911 return local_id_ref(p, id, NULL);
11915 numparam_push(struct parser_params *p)
11918 struct local_vars *local = p->lvtbl;
11919 NODE *inner = local->numparam.inner;
11920 if (!local->numparam.outer) {
11921 local->numparam.outer = local->numparam.current;
11923 local->numparam.inner = 0;
11924 local->numparam.current = 0;
11932 numparam_pop(struct parser_params *p, NODE *prev_inner)
11935 struct local_vars *local = p->lvtbl;
11937 /* prefer first one */
11938 local->numparam.inner = prev_inner;
11940 else if (local->numparam.current) {
11941 /* current and inner are exclusive */
11942 local->numparam.inner = local->numparam.current;
11944 if (p->max_numparam > NO_PARAM) {
11945 /* current and outer are exclusive */
11946 local->numparam.current = local->numparam.outer;
11947 local->numparam.outer = 0;
11950 /* no numbered parameter */
11951 local->numparam.current = 0;
11956 static const struct vtable *
11957 dyna_push(struct parser_params *p)
11959 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11960 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11961 if (p->lvtbl->used) {
11962 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11964 return p->lvtbl->args;
11968 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11970 struct vtable *tmp = *vtblp;
11971 *vtblp = tmp->prev;
11972 # if WARN_PAST_SCOPE
11973 if (p->past_scope_enabled) {
11974 tmp->prev = p->lvtbl->past;
11975 p->lvtbl->past = tmp;
11983 dyna_pop_1(struct parser_params *p)
11985 struct vtable *tmp;
11987 if ((tmp = p->lvtbl->used) != 0) {
11988 warn_unused_var(p, p->lvtbl);
11989 p->lvtbl->used = p->lvtbl->used->prev;
11992 dyna_pop_vtable(p, &p->lvtbl->args);
11993 dyna_pop_vtable(p, &p->lvtbl->vars);
11997 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
11999 while (p->lvtbl->args != lvargs) {
12001 if (!p->lvtbl->args) {
12002 struct local_vars *local = p->lvtbl->prev;
12003 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12011 dyna_in_block(struct parser_params *p)
12013 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12017 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12019 struct vtable *vars, *args, *used;
12022 args = p->lvtbl->args;
12023 vars = p->lvtbl->vars;
12024 used = p->lvtbl->used;
12026 while (!DVARS_TERMINAL_P(vars)) {
12027 if (vtable_included(args, id)) {
12030 if ((i = vtable_included(vars, id)) != 0) {
12031 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12036 if (!vidrefp) used = 0;
12037 if (used) used = used->prev;
12040 if (vars == DVARS_INHERIT) {
12041 return rb_dvar_defined(id, p->parent_iseq);
12048 dvar_defined(struct parser_params *p, ID id)
12050 return dvar_defined_ref(p, id, NULL);
12054 dvar_curr(struct parser_params *p, ID id)
12056 return (vtable_included(p->lvtbl->args, id) ||
12057 vtable_included(p->lvtbl->vars, id));
12061 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12064 "regexp encoding option '%c' differs from source encoding '%s'",
12065 c, rb_enc_name(rb_enc_get(str)));
12070 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12072 int c = RE_OPTION_ENCODING_IDX(options);
12076 rb_char_to_option_kcode(c, &opt, &idx);
12077 if (idx != ENCODING_GET(str) &&
12078 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12081 ENCODING_SET(str, idx);
12083 else if (RE_OPTION_ENCODING_NONE(options)) {
12084 if (!ENCODING_IS_ASCII8BIT(str) &&
12085 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12089 rb_enc_associate(str, rb_ascii8bit_encoding());
12091 else if (p->enc == rb_usascii_encoding()) {
12092 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12093 /* raise in re.c */
12094 rb_enc_associate(str, rb_usascii_encoding());
12097 rb_enc_associate(str, rb_ascii8bit_encoding());
12107 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12109 int c = rb_reg_fragment_setenc(p, str, options);
12110 if (c) reg_fragment_enc_error(p, str, c);
12114 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12117 reg_fragment_setenc(p, str, options);
12118 err = rb_reg_check_preprocess(str);
12120 err = rb_obj_as_string(err);
12121 compile_error(p, "%"PRIsVALUE, err);
12128 struct parser_params* parser;
12131 const YYLTYPE *loc;
12132 } reg_named_capture_assign_t;
12135 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12136 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12138 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12139 struct parser_params* p = arg->parser;
12140 rb_encoding *enc = arg->enc;
12141 long len = name_end - name;
12142 const char *s = (const char *)name;
12146 if (!len) return ST_CONTINUE;
12147 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len))
12148 return ST_CONTINUE;
12149 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12150 return ST_CONTINUE;
12152 var = intern_cstr(s, len, enc);
12153 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12154 succ = arg->succ_block;
12155 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12156 succ = block_append(p, succ, node);
12157 arg->succ_block = succ;
12158 return ST_CONTINUE;
12162 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12164 reg_named_capture_assign_t arg;
12167 arg.enc = rb_enc_get(regexp);
12168 arg.succ_block = 0;
12170 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12172 if (!arg.succ_block) return 0;
12173 return arg.succ_block->nd_next;
12177 parser_reg_compile(struct parser_params* p, VALUE str, int options)
12179 reg_fragment_setenc(p, str, options);
12180 return rb_parser_reg_compile(p, str, options);
12184 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12186 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12190 reg_compile(struct parser_params* p, VALUE str, int options)
12195 err = rb_errinfo();
12196 re = parser_reg_compile(p, str, options);
12198 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12199 rb_set_errinfo(err);
12200 compile_error(p, "%"PRIsVALUE, m);
12207 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12209 VALUE err = rb_errinfo();
12211 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12212 int c = rb_reg_fragment_setenc(p, str, options);
12213 if (c) reg_fragment_enc_error(p, str, c);
12214 re = rb_parser_reg_compile(p, str, options);
12216 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12217 rb_set_errinfo(err);
12225 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12227 struct parser_params *p;
12228 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12229 p->do_print = print;
12231 p->do_chomp = chomp;
12232 p->do_split = split;
12236 rb_parser_warn_location(VALUE vparser, int warn)
12238 struct parser_params *p;
12239 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12240 p->warn_location = warn;
12244 parser_append_options(struct parser_params *p, NODE *node)
12246 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12247 const YYLTYPE *const LOC = &default_location;
12250 NODE *print = NEW_FCALL(rb_intern("print"),
12251 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12253 node = block_append(p, node, print);
12258 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12259 NODE *split = NEW_GASGN(rb_intern("$F"),
12260 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12261 rb_intern("split"), args, LOC),
12263 node = block_append(p, split, node);
12266 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12267 rb_intern("chomp!"), 0, LOC);
12268 node = block_append(p, chomp, node);
12271 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12278 rb_init_parse(void)
12280 /* just to suppress unused-function warnings */
12286 internal_id(struct parser_params *p)
12288 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12289 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12291 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12293 #endif /* !RIPPER */
12296 parser_initialize(struct parser_params *p)
12298 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12299 p->command_start = TRUE;
12300 p->ruby_sourcefile_string = Qnil;
12301 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12304 p->delayed.token = Qnil;
12306 p->parsing_thread = Qnil;
12308 p->error_buffer = Qfalse;
12310 p->debug_buffer = Qnil;
12311 p->debug_output = rb_stdout;
12312 p->enc = rb_utf8_encoding();
12316 #define parser_mark ripper_parser_mark
12317 #define parser_free ripper_parser_free
12321 parser_mark(void *ptr)
12323 struct parser_params *p = (struct parser_params*)ptr;
12325 rb_gc_mark(p->lex.input);
12326 rb_gc_mark(p->lex.prevline);
12327 rb_gc_mark(p->lex.lastline);
12328 rb_gc_mark(p->lex.nextline);
12329 rb_gc_mark(p->ruby_sourcefile_string);
12330 rb_gc_mark((VALUE)p->lex.strterm);
12331 rb_gc_mark((VALUE)p->ast);
12332 rb_gc_mark(p->case_labels);
12334 rb_gc_mark(p->debug_lines);
12335 rb_gc_mark(p->compile_option);
12336 rb_gc_mark(p->error_buffer);
12338 rb_gc_mark(p->delayed.token);
12339 rb_gc_mark(p->value);
12340 rb_gc_mark(p->result);
12341 rb_gc_mark(p->parsing_thread);
12343 rb_gc_mark(p->debug_buffer);
12344 rb_gc_mark(p->debug_output);
12346 rb_gc_mark((VALUE)p->heap);
12351 parser_free(void *ptr)
12353 struct parser_params *p = (struct parser_params*)ptr;
12354 struct local_vars *local, *prev;
12357 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12359 for (local = p->lvtbl; local; local = prev) {
12360 if (local->vars) xfree(local->vars);
12361 prev = local->prev;
12365 token_info *ptinfo;
12366 while ((ptinfo = p->token_info) != 0) {
12367 p->token_info = ptinfo->next;
12375 parser_memsize(const void *ptr)
12377 struct parser_params *p = (struct parser_params*)ptr;
12378 struct local_vars *local;
12379 size_t size = sizeof(*p);
12382 for (local = p->lvtbl; local; local = local->prev) {
12383 size += sizeof(*local);
12384 if (local->vars) size += local->vars->capa * sizeof(ID);
12389 static const rb_data_type_t parser_data_type = {
12400 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12404 #undef rb_reserved_word
12406 const struct kwtable *
12407 rb_reserved_word(const char *str, unsigned int len)
12409 return reserved_word(str, len);
12413 rb_parser_new(void)
12415 struct parser_params *p;
12416 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12417 &parser_data_type, p);
12418 parser_initialize(p);
12423 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12425 struct parser_params *p;
12427 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12428 p->error_buffer = main ? Qfalse : Qnil;
12429 p->parent_iseq = base;
12435 #define rb_parser_end_seen_p ripper_parser_end_seen_p
12436 #define rb_parser_encoding ripper_parser_encoding
12437 #define rb_parser_get_yydebug ripper_parser_get_yydebug
12438 #define rb_parser_set_yydebug ripper_parser_set_yydebug
12439 #define rb_parser_get_debug_output ripper_parser_get_debug_output
12440 #define rb_parser_set_debug_output ripper_parser_set_debug_output
12441 static VALUE ripper_parser_end_seen_p(VALUE vparser);
12442 static VALUE ripper_parser_encoding(VALUE vparser);
12443 static VALUE ripper_parser_get_yydebug(VALUE self);
12444 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12445 static VALUE ripper_parser_get_debug_output(VALUE self);
12446 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12450 * ripper.error? -> Boolean
12452 * Return true if parsed source has errors.
12455 ripper_error_p(VALUE vparser)
12457 struct parser_params *p;
12459 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12460 return p->error_p ? Qtrue : Qfalse;
12466 * ripper.end_seen? -> Boolean
12468 * Return true if parsed source ended by +\_\_END\_\_+.
12471 rb_parser_end_seen_p(VALUE vparser)
12473 struct parser_params *p;
12475 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12476 return p->ruby__end__seen ? Qtrue : Qfalse;
12481 * ripper.encoding -> encoding
12483 * Return encoding of the source.
12486 rb_parser_encoding(VALUE vparser)
12488 struct parser_params *p;
12490 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12491 return rb_enc_from_encoding(p->enc);
12497 * ripper.yydebug -> true or false
12502 rb_parser_get_yydebug(VALUE self)
12504 struct parser_params *p;
12506 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12507 return p->debug ? Qtrue : Qfalse;
12513 * ripper.yydebug = flag
12518 rb_parser_set_yydebug(VALUE self, VALUE flag)
12520 struct parser_params *p;
12522 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12523 p->debug = RTEST(flag);
12529 * ripper.debug_output -> obj
12531 * Get debug output.
12534 rb_parser_get_debug_output(VALUE self)
12536 struct parser_params *p;
12538 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12539 return p->debug_output;
12544 * ripper.debug_output = obj
12546 * Set debug output.
12549 rb_parser_set_debug_output(VALUE self, VALUE output)
12551 struct parser_params *p;
12553 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12554 return p->debug_output = output;
12559 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12560 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12561 * potential memory leak */
12562 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12563 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12564 (new)->cnt = (cnt), (ptr))
12567 rb_parser_malloc(struct parser_params *p, size_t size)
12569 size_t cnt = HEAPCNT(1, size);
12570 rb_imemo_tmpbuf_t *n = NEWHEAP();
12571 void *ptr = xmalloc(size);
12573 return ADD2HEAP(n, cnt, ptr);
12577 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12579 size_t cnt = HEAPCNT(nelem, size);
12580 rb_imemo_tmpbuf_t *n = NEWHEAP();
12581 void *ptr = xcalloc(nelem, size);
12583 return ADD2HEAP(n, cnt, ptr);
12587 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12589 rb_imemo_tmpbuf_t *n;
12590 size_t cnt = HEAPCNT(1, size);
12592 if (ptr && (n = p->heap) != NULL) {
12594 if (n->ptr == ptr) {
12595 n->ptr = ptr = xrealloc(ptr, size);
12596 if (n->cnt) n->cnt = cnt;
12599 } while ((n = n->next) != NULL);
12602 ptr = xrealloc(ptr, size);
12603 return ADD2HEAP(n, cnt, ptr);
12607 rb_parser_free(struct parser_params *p, void *ptr)
12609 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12611 while ((n = *prev) != NULL) {
12612 if (n->ptr == ptr) {
12614 rb_gc_force_recycle((VALUE)n);
12624 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12627 VALUE mesg = p->debug_buffer;
12629 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12631 rb_str_vcatf(mesg, fmt, ap);
12633 if (RSTRING_END(mesg)[-1] == '\n') {
12634 rb_io_write(p->debug_output, mesg);
12635 p->debug_buffer = Qnil;
12640 parser_compile_error(struct parser_params *p, const char *fmt, ...)
12644 rb_io_flush(p->debug_output);
12648 rb_syntax_error_append(p->error_buffer,
12649 p->ruby_sourcefile_string,
12650 p->ruby_sourceline,
12651 rb_long2int(p->lex.pcur - p->lex.pbeg),
12657 count_char(const char *str, int c)
12660 while (str[n] == c) ++n;
12665 * strip enclosing double-quotes, same as the default yytnamerr except
12666 * for that single-quotes matching back-quotes do not stop stripping.
12668 * "\"`class' keyword\"" => "`class' keyword"
12670 RUBY_FUNC_EXPORTED size_t
12671 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12674 if (*yystr == '"') {
12675 size_t yyn = 0, bquote = 0;
12676 const char *yyp = yystr;
12682 bquote = count_char(yyp+1, '`') + 1;
12683 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12691 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12692 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12698 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12699 if (yyres) memcpy(yyres + yyn, yyp, 3);
12704 goto do_not_strip_quotes;
12707 goto do_not_strip_quotes;
12710 if (*++yyp != '\\')
12711 goto do_not_strip_quotes;
12712 /* Fall through. */
12727 do_not_strip_quotes: ;
12730 if (!yyres) return strlen(yystr);
12732 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12737 #ifdef RIPPER_DEBUG
12740 ripper_validate_object(VALUE self, VALUE x)
12742 if (x == Qfalse) return x;
12743 if (x == Qtrue) return x;
12744 if (x == Qnil) return x;
12746 rb_raise(rb_eArgError, "Qundef given");
12747 if (FIXNUM_P(x)) return x;
12748 if (SYMBOL_P(x)) return x;
12749 switch (BUILTIN_TYPE(x)) {
12759 if (nd_type((NODE *)x) != NODE_RIPPER) {
12760 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12762 x = ((NODE *)x)->nd_rval;
12765 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12766 (void *)x, rb_obj_classname(x));
12768 if (!RBASIC_CLASS(x)) {
12769 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12770 (void *)x, rb_builtin_type_name(TYPE(x)));
12776 #define validate(x) ((x) = get_value(x))
12779 ripper_dispatch0(struct parser_params *p, ID mid)
12781 return rb_funcall(p->value, mid, 0);
12785 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12788 return rb_funcall(p->value, mid, 1, a);
12792 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12796 return rb_funcall(p->value, mid, 2, a, b);
12800 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12805 return rb_funcall(p->value, mid, 3, a, b, c);
12809 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12815 return rb_funcall(p->value, mid, 4, a, b, c, d);
12819 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12826 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12830 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12839 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12843 ripper_get_id(VALUE v)
12846 if (!RB_TYPE_P(v, T_NODE)) return 0;
12848 if (nd_type(nd) != NODE_RIPPER) return 0;
12853 ripper_get_value(VALUE v)
12856 if (v == Qundef) return Qnil;
12857 if (!RB_TYPE_P(v, T_NODE)) return v;
12859 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12860 return nd->nd_rval;
12864 ripper_error(struct parser_params *p)
12870 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12875 va_start(args, fmt);
12876 str = rb_vsprintf(fmt, args);
12878 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12883 ripper_lex_get_generic(struct parser_params *p, VALUE src)
12885 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12886 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12887 rb_raise(rb_eTypeError,
12888 "gets returned %"PRIsVALUE" (expected String or nil)",
12889 rb_obj_class(line));
12895 ripper_lex_io_get(struct parser_params *p, VALUE src)
12897 return rb_io_gets(src);
12901 ripper_s_allocate(VALUE klass)
12903 struct parser_params *p;
12904 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12905 &parser_data_type, p);
12910 #define ripper_initialized_p(r) ((r)->lex.input != 0)
12914 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12916 * Create a new Ripper object.
12917 * _src_ must be a String, an IO, or an Object which has #gets method.
12919 * This method does not starts parsing.
12920 * See also Ripper#parse and Ripper.parse.
12923 ripper_initialize(int argc, VALUE *argv, VALUE self)
12925 struct parser_params *p;
12926 VALUE src, fname, lineno;
12928 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12929 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12930 if (RB_TYPE_P(src, T_FILE)) {
12931 p->lex.gets = ripper_lex_io_get;
12933 else if (rb_respond_to(src, id_gets)) {
12934 p->lex.gets = ripper_lex_get_generic;
12938 p->lex.gets = lex_get_str;
12940 p->lex.input = src;
12942 if (NIL_P(fname)) {
12943 fname = STR_NEW2("(ripper)");
12947 StringValueCStr(fname);
12948 fname = rb_str_new_frozen(fname);
12950 parser_initialize(p);
12952 p->ruby_sourcefile_string = fname;
12953 p->ruby_sourcefile = RSTRING_PTR(fname);
12954 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12960 ripper_parse0(VALUE parser_v)
12962 struct parser_params *p;
12964 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12966 p->ast = rb_ast_new();
12967 ripper_yyparse((void*)p);
12968 rb_ast_dispose(p->ast);
12974 ripper_ensure(VALUE parser_v)
12976 struct parser_params *p;
12978 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12979 p->parsing_thread = Qnil;
12987 * Start parsing and returns the value of the root action.
12990 ripper_parse(VALUE self)
12992 struct parser_params *p;
12994 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12995 if (!ripper_initialized_p(p)) {
12996 rb_raise(rb_eArgError, "method called for uninitialized object");
12998 if (!NIL_P(p->parsing_thread)) {
12999 if (p->parsing_thread == rb_thread_current())
13000 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13002 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13004 p->parsing_thread = rb_thread_current();
13005 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13012 * ripper.column -> Integer
13014 * Return column number of current parsing line.
13015 * This number starts from 0.
13018 ripper_column(VALUE self)
13020 struct parser_params *p;
13023 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13024 if (!ripper_initialized_p(p)) {
13025 rb_raise(rb_eArgError, "method called for uninitialized object");
13027 if (NIL_P(p->parsing_thread)) return Qnil;
13028 col = p->lex.ptok - p->lex.pbeg;
13029 return LONG2NUM(col);
13034 * ripper.filename -> String
13036 * Return current parsing filename.
13039 ripper_filename(VALUE self)
13041 struct parser_params *p;
13043 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13044 if (!ripper_initialized_p(p)) {
13045 rb_raise(rb_eArgError, "method called for uninitialized object");
13047 return p->ruby_sourcefile_string;
13052 * ripper.lineno -> Integer
13054 * Return line number of current parsing line.
13055 * This number starts from 1.
13058 ripper_lineno(VALUE self)
13060 struct parser_params *p;
13062 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13063 if (!ripper_initialized_p(p)) {
13064 rb_raise(rb_eArgError, "method called for uninitialized object");
13066 if (NIL_P(p->parsing_thread)) return Qnil;
13067 return INT2NUM(p->ruby_sourceline);
13072 * ripper.state -> Integer
13074 * Return scanner state of current token.
13077 ripper_state(VALUE self)
13079 struct parser_params *p;
13081 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13082 if (!ripper_initialized_p(p)) {
13083 rb_raise(rb_eArgError, "method called for uninitialized object");
13085 if (NIL_P(p->parsing_thread)) return Qnil;
13086 return INT2NUM(p->lex.state);
13091 * ripper.token -> String
13093 * Return the current token string.
13096 ripper_token(VALUE self)
13098 struct parser_params *p;
13101 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13102 if (!ripper_initialized_p(p)) {
13103 rb_raise(rb_eArgError, "method called for uninitialized object");
13105 if (NIL_P(p->parsing_thread)) return Qnil;
13106 pos = p->lex.ptok - p->lex.pbeg;
13107 len = p->lex.pcur - p->lex.ptok;
13108 return rb_str_subseq(p->lex.lastline, pos, len);
13111 #ifdef RIPPER_DEBUG
13114 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13117 if (obj == Qundef) {
13118 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13125 ripper_value(VALUE self, VALUE obj)
13127 return ULONG2NUM(obj);
13133 * Ripper.lex_state_name(integer) -> string
13135 * Returns a string representation of lex_state.
13138 ripper_lex_state_name(VALUE self, VALUE state)
13140 return rb_parser_lex_state_name(NUM2INT(state));
13146 ripper_init_eventids1();
13147 ripper_init_eventids2();
13148 id_warn = rb_intern_const("warn");
13149 id_warning = rb_intern_const("warning");
13150 id_gets = rb_intern_const("gets");
13151 id_assoc = rb_intern_const("=>");
13153 (void)yystpcpy; /* may not used in newer bison */
13159 InitVM_ripper(void)
13163 Ripper = rb_define_class("Ripper", rb_cObject);
13164 /* version of Ripper */
13165 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13166 rb_define_alloc_func(Ripper, ripper_s_allocate);
13167 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13168 rb_define_method(Ripper, "parse", ripper_parse, 0);
13169 rb_define_method(Ripper, "column", ripper_column, 0);
13170 rb_define_method(Ripper, "filename", ripper_filename, 0);
13171 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13172 rb_define_method(Ripper, "state", ripper_state, 0);
13173 rb_define_method(Ripper, "token", ripper_token, 0);
13174 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13175 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13176 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13177 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13178 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13179 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13180 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13181 #ifdef RIPPER_DEBUG
13182 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13183 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13184 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13187 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13188 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13190 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13192 /* ignore newline, +/- is a sign. */
13193 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13194 /* newline significant, +/- is an operator. */
13195 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13196 /* ditto, and unbound braces. */
13197 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13198 /* ditto, and unbound braces. */
13199 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13200 /* newline significant, +/- is an operator. */
13201 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13202 /* newline significant, +/- is an operator. */
13203 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13204 /* newline significant, +/- is an operator. */
13205 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13206 /* ignore newline, no reserved words. */
13207 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13208 /* right after `.' or `::', no reserved words. */
13209 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13210 /* immediate after `class', no here document. */
13211 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13212 /* flag bit, label is allowed. */
13213 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13214 /* flag bit, just after a label. */
13215 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13216 /* symbol literal as FNAME. */
13217 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13218 /* equals to +EXPR_BEG+ */
13219 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13220 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13221 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13222 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13223 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13224 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13225 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13226 /* equals to +0+ */
13227 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13229 ripper_init_eventids1_table(Ripper);
13230 ripper_init_eventids2_table(Ripper);
13233 /* Hack to let RDoc document SCRIPT_LINES__ */
13236 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13237 * after the assignment will be added as an Array of lines with the file
13240 rb_define_global_const("SCRIPT_LINES__", Qnil);
13244 #endif /* RIPPER */
13249 * c-file-style: "ruby"