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 <id> tIDENTIFIER "local variable or method"
1040 %token <id> tFID "method"
1041 %token <id> tGVAR "global variable"
1042 %token <id> tIVAR "instance variable"
1043 %token <id> tCONSTANT "constant"
1044 %token <id> tCVAR "class variable"
1046 %token <node> tINTEGER "integer literal"
1047 %token <node> tFLOAT "float literal"
1048 %token <node> tRATIONAL "rational literal"
1049 %token <node> tIMAGINARY "imaginary literal"
1050 %token <node> tCHAR "char literal"
1051 %token <node> tNTH_REF "numbered reference"
1052 %token <node> tBACK_REF "back reference"
1053 %token <node> tSTRING_CONTENT "literal content"
1054 %token <num> tREGEXP_END
1056 %type <node> singleton strings string string1 xstring regexp
1057 %type <node> string_contents xstring_contents regexp_contents string_content
1058 %type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1059 %type <node> literal numeric simple_numeric ssym dsym symbol cpath
1060 %type <node> top_compstmt top_stmts top_stmt begin_block
1061 %type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1062 %type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1063 %type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1064 %type <node> args call_args opt_call_args
1065 %type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1066 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1067 %type <node> command_rhs arg_rhs
1068 %type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1069 %type <node> f_block_optarg f_block_opt
1070 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1071 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1072 %type <node> block_param opt_block_param block_param_def f_opt
1073 %type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1074 %type <node> bv_decls opt_bv_decl bvar
1075 %type <node> lambda f_larglist lambda_body brace_body do_body
1076 %type <node> brace_block cmd_brace_block do_block lhs none fitem
1077 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1078 %type <node> p_case_body p_cases p_top_expr p_top_expr_body
1079 %type <node> p_expr p_as p_alt p_expr_basic
1080 %type <node> p_args p_args_head p_args_tail p_args_post p_arg
1081 %type <node> p_value p_primitive p_variable p_var_ref p_const
1082 %type <node> p_kwargs p_kwarg p_kw
1083 %type <id> keyword_variable user_variable sym operation operation2 operation3
1084 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1085 %type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1086 %type <id> p_kwrest p_kwnorest p_kw_label
1087 %type <id> f_no_kwarg args_forward
1088 %token END_OF_INPUT 0 "end-of-input"
1090 /* escaped chars, should be ignored otherwise */
1091 %token <id> '\\' "backslash"
1092 %token tSP "escaped space"
1093 %token <id> '\t' "escaped horizontal tab"
1094 %token <id> '\f' "escaped form feed"
1095 %token <id> '\r' "escaped carriage return"
1096 %token <id> '\13' "escaped vertical tab"
1097 %token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1098 %token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1099 %token tPOW RUBY_TOKEN(POW) "**"
1100 %token tCMP RUBY_TOKEN(CMP) "<=>"
1101 %token tEQ RUBY_TOKEN(EQ) "=="
1102 %token tEQQ RUBY_TOKEN(EQQ) "==="
1103 %token tNEQ RUBY_TOKEN(NEQ) "!="
1104 %token tGEQ RUBY_TOKEN(GEQ) ">="
1105 %token tLEQ RUBY_TOKEN(LEQ) "<="
1106 %token tANDOP RUBY_TOKEN(ANDOP) "&&"
1107 %token tOROP RUBY_TOKEN(OROP) "||"
1108 %token tMATCH RUBY_TOKEN(MATCH) "=~"
1109 %token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1110 %token tDOT2 RUBY_TOKEN(DOT2) ".."
1111 %token tDOT3 RUBY_TOKEN(DOT3) "..."
1112 %token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1113 %token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1114 %token tAREF RUBY_TOKEN(AREF) "[]"
1115 %token tASET RUBY_TOKEN(ASET) "[]="
1116 %token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1117 %token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1118 %token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1119 %token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1120 %token tCOLON3 ":: at EXPR_BEG"
1121 %token <id> 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 /*% ripper[final]: program!($2) %*/
1202 top_compstmt : top_stmts opt_terms
1204 $$ = void_stmts(p, $1);
1211 $$ = NEW_BEGIN(0, &@$);
1213 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1218 $$ = newline_node($1);
1220 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1222 | top_stmts terms top_stmt
1225 $$ = block_append(p, $1, newline_node($3));
1227 /*% ripper: stmts_add!($1, $3) %*/
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 /*% ripper: BEGIN!($2) %*/
1255 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1260 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1262 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1269 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1271 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1275 compstmt : stmts opt_terms
1277 $$ = void_stmts(p, $1);
1284 $$ = NEW_BEGIN(0, &@$);
1286 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1291 $$ = newline_node($1);
1293 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1295 | stmts terms stmt_or_begin
1298 $$ = block_append(p, $1, newline_node($3));
1300 /*% ripper: stmts_add!($1, $3) %*/
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 /*% ripper: alias!($2, $4) %*/
1329 | keyword_alias tGVAR tGVAR
1332 $$ = NEW_VALIAS($2, $3, &@$);
1334 /*% ripper: var_alias!($2, $3) %*/
1336 | keyword_alias tGVAR tBACK_REF
1341 buf[1] = (char)$3->nd_nth;
1342 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1344 /*% ripper: var_alias!($2, $3) %*/
1346 | keyword_alias tGVAR tNTH_REF
1349 yyerror1(&@3, "can't make alias for the number variables");
1350 $$ = NEW_BEGIN(0, &@$);
1352 /*% ripper[error]: alias_error!(var_alias!($2, $3)) %*/
1354 | keyword_undef undef_list
1359 /*% ripper: undef!($2) %*/
1361 | stmt modifier_if expr_value
1364 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1367 /*% ripper: if_mod!($3, $1) %*/
1369 | stmt modifier_unless expr_value
1372 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1375 /*% ripper: unless_mod!($3, $1) %*/
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 /*% ripper: while_mod!($3, $1) %*/
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 /*% ripper: until_mod!($3, $1) %*/
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 /*% ripper: rescue_mod!($1, $3) %*/
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 /*% ripper: END!($3) %*/
1426 | mlhs '=' command_call
1430 $$ = node_assign(p, $1, $3, &@$);
1432 /*% ripper: massign!($1, $3) %*/
1438 $$ = node_assign(p, $1, $3, &@$);
1440 /*% ripper: assign!($1, $3) %*/
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 /*% ripper: massign!($1, rescue_mod!($3, $5)) %*/
1454 $$ = node_assign(p, $1, $3, &@$);
1456 /*% ripper: massign!($1, $3) %*/
1461 command_asgn : lhs '=' command_rhs
1464 $$ = node_assign(p, $1, $3, &@$);
1466 /*% ripper: assign!($1, $3) %*/
1468 | var_lhs tOP_ASGN command_rhs
1471 $$ = new_op_assign(p, $1, $2, $3, &@$);
1473 /*% ripper: opassign!($1, $2, $3) %*/
1475 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1478 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1480 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $6) %*/
1483 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1486 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1488 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
1490 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1493 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1495 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
1497 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1500 YYLTYPE loc = code_loc_gen(&@1, &@3);
1501 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1503 /*% ripper: opassign!(const_path_field!($1, $3), $4, $5) %*/
1505 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1508 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1510 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $5) %*/
1512 | backref tOP_ASGN command_rhs
1515 rb_backref_error(p, $1);
1516 $$ = NEW_BEGIN(0, &@$);
1518 /*% ripper[error]: assign_error!(assign!(var_field(p, $1), $3)) %*/
1522 command_rhs : command_call %prec tOP_ASGN
1527 | command_call modifier_rescue stmt
1530 YYLTYPE loc = code_loc_gen(&@2, &@3);
1532 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1534 /*% ripper: rescue_mod!($1, $3) %*/
1540 | expr keyword_and expr
1542 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1544 | expr keyword_or expr
1546 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1548 | keyword_not opt_nl expr
1550 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1554 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1559 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1560 p->command_start = FALSE;
1561 $<num>$ = p->in_kwarg;
1564 {$<tbl>$ = push_pvtbl(p);}
1566 {pop_pvtbl(p, $<tbl>4);}
1568 p->in_kwarg = !!$<num>3;
1570 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1572 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1574 | arg %prec tLBRACE_ARG
1584 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1590 command_call : command
1594 block_command : block_call
1595 | block_call call_op2 operation2 command_args
1598 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1600 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
1604 cmd_brace_block : tLBRACE_ARG brace_body '}'
1608 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1609 nd_set_line($$, @1.end_pos.lineno);
1617 $$ = NEW_FCALL($1, 0, &@$);
1618 nd_set_line($$, p->tokline);
1624 command : fcall command_args %prec tLOWEST
1628 nd_set_last_loc($1, @2.end_pos);
1631 /*% ripper: command!($1, $2) %*/
1633 | fcall command_args cmd_brace_block
1636 block_dup_check(p, $2, $3);
1638 $$ = method_add_block(p, $1, $3, &@$);
1640 nd_set_last_loc($1, @2.end_pos);
1642 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
1644 | primary_value call_op operation2 command_args %prec tLOWEST
1647 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1649 /*% ripper: command_call!($1, $2, $3, $4) %*/
1651 | primary_value call_op operation2 command_args cmd_brace_block
1654 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1656 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
1658 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1661 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1663 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
1665 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1668 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1670 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
1672 | keyword_super command_args
1675 $$ = NEW_SUPER($2, &@$);
1678 /*% ripper: super!($2) %*/
1680 | keyword_yield command_args
1683 $$ = new_yield(p, $2, &@$);
1686 /*% ripper: yield!($2) %*/
1688 | k_return call_args
1691 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1693 /*% ripper: return!($2) %*/
1695 | keyword_break call_args
1698 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1700 /*% ripper: break!($2) %*/
1702 | keyword_next call_args
1705 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1707 /*% ripper: next!($2) %*/
1712 | tLPAREN mlhs_inner rparen
1717 /*% ripper: mlhs_paren!($2) %*/
1721 mlhs_inner : mlhs_basic
1722 | tLPAREN mlhs_inner rparen
1725 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1727 /*% ripper: mlhs_paren!($2) %*/
1731 mlhs_basic : mlhs_head
1734 $$ = NEW_MASGN($1, 0, &@$);
1738 | mlhs_head mlhs_item
1741 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1743 /*% ripper: mlhs_add!($1, $2) %*/
1745 | mlhs_head tSTAR mlhs_node
1748 $$ = NEW_MASGN($1, $3, &@$);
1750 /*% ripper: mlhs_add_star!($1, $3) %*/
1752 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1755 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1757 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
1762 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1764 /*% ripper: mlhs_add_star!($1, Qnil) %*/
1766 | mlhs_head tSTAR ',' mlhs_post
1769 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1771 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
1776 $$ = NEW_MASGN(0, $2, &@$);
1778 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
1780 | tSTAR mlhs_node ',' mlhs_post
1783 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1785 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
1790 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1792 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
1794 | tSTAR ',' mlhs_post
1797 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1799 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
1803 mlhs_item : mlhs_node
1804 | tLPAREN mlhs_inner rparen
1809 /*% ripper: mlhs_paren!($2) %*/
1813 mlhs_head : mlhs_item ','
1816 $$ = NEW_LIST($1, &@1);
1818 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
1820 | mlhs_head mlhs_item ','
1823 $$ = list_append(p, $1, $2);
1825 /*% ripper: mlhs_add!($1, $2) %*/
1829 mlhs_post : mlhs_item
1832 $$ = NEW_LIST($1, &@$);
1834 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
1836 | mlhs_post ',' mlhs_item
1839 $$ = list_append(p, $1, $3);
1841 /*% ripper: mlhs_add!($1, $3) %*/
1845 mlhs_node : user_variable
1848 $$ = assignable(p, $1, 0, &@$);
1850 /*% ripper: assignable(p, var_field(p, $1)) %*/
1855 $$ = assignable(p, $1, 0, &@$);
1857 /*% ripper: assignable(p, var_field(p, $1)) %*/
1859 | primary_value '[' opt_call_args rbracket
1862 $$ = aryset(p, $1, $3, &@$);
1864 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
1866 | primary_value call_op tIDENTIFIER
1868 if ($2 == tANDDOT) {
1869 yyerror1(&@2, "&. inside multiple assignment destination");
1872 $$ = attrset(p, $1, $2, $3, &@$);
1874 /*% ripper: field!($1, $2, $3) %*/
1876 | primary_value tCOLON2 tIDENTIFIER
1879 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1881 /*% ripper: const_path_field!($1, $3) %*/
1883 | primary_value call_op tCONSTANT
1885 if ($2 == tANDDOT) {
1886 yyerror1(&@2, "&. inside multiple assignment destination");
1889 $$ = attrset(p, $1, $2, $3, &@$);
1891 /*% ripper: field!($1, $2, $3) %*/
1893 | primary_value tCOLON2 tCONSTANT
1896 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1898 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
1903 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1905 /*% ripper: const_decl(p, top_const_field!($2)) %*/
1910 rb_backref_error(p, $1);
1911 $$ = NEW_BEGIN(0, &@$);
1913 /*% ripper[error]: assign_error!(var_field(p, $1)) %*/
1920 $$ = assignable(p, $1, 0, &@$);
1922 /*% ripper: assignable(p, var_field(p, $1)) %*/
1927 $$ = assignable(p, $1, 0, &@$);
1929 /*% ripper: assignable(p, var_field(p, $1)) %*/
1931 | primary_value '[' opt_call_args rbracket
1934 $$ = aryset(p, $1, $3, &@$);
1936 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
1938 | primary_value call_op tIDENTIFIER
1941 $$ = attrset(p, $1, $2, $3, &@$);
1943 /*% ripper: field!($1, $2, $3) %*/
1945 | primary_value tCOLON2 tIDENTIFIER
1948 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1950 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
1952 | primary_value call_op tCONSTANT
1955 $$ = attrset(p, $1, $2, $3, &@$);
1957 /*% ripper: field!($1, $2, $3) %*/
1959 | primary_value tCOLON2 tCONSTANT
1962 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1964 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
1969 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1971 /*% ripper: const_decl(p, top_const_field!($2)) %*/
1976 rb_backref_error(p, $1);
1977 $$ = NEW_BEGIN(0, &@$);
1979 /*% ripper[error]: assign_error!(var_field(p, $1)) %*/
1986 yyerror1(&@1, "class/module name must be CONSTANT");
1988 /*% ripper[error]: class_name_error!($1) %*/
1993 cpath : tCOLON3 cname
1996 $$ = NEW_COLON3($2, &@$);
1998 /*% ripper: top_const_ref!($2) %*/
2003 $$ = NEW_COLON2(0, $$, &@$);
2005 /*% ripper: const_ref!($1) %*/
2007 | primary_value tCOLON2 cname
2010 $$ = NEW_COLON2($1, $3, &@$);
2012 /*% ripper: const_path_ref!($1, $3) %*/
2021 SET_LEX_STATE(EXPR_ENDFN);
2026 SET_LEX_STATE(EXPR_ENDFN);
2034 $$ = NEW_LIT(ID2SYM($1), &@$);
2036 /*% ripper: symbol_literal!($1) %*/
2044 $$ = NEW_UNDEF($1, &@$);
2046 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2048 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2051 NODE *undef = NEW_UNDEF($4, &@4);
2052 $$ = block_append(p, $1, undef);
2054 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2058 op : '|' { ifndef_ripper($$ = '|'); }
2059 | '^' { ifndef_ripper($$ = '^'); }
2060 | '&' { ifndef_ripper($$ = '&'); }
2061 | tCMP { ifndef_ripper($$ = tCMP); }
2062 | tEQ { ifndef_ripper($$ = tEQ); }
2063 | tEQQ { ifndef_ripper($$ = tEQQ); }
2064 | tMATCH { ifndef_ripper($$ = tMATCH); }
2065 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2066 | '>' { ifndef_ripper($$ = '>'); }
2067 | tGEQ { ifndef_ripper($$ = tGEQ); }
2068 | '<' { ifndef_ripper($$ = '<'); }
2069 | tLEQ { ifndef_ripper($$ = tLEQ); }
2070 | tNEQ { ifndef_ripper($$ = tNEQ); }
2071 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2072 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2073 | '+' { ifndef_ripper($$ = '+'); }
2074 | '-' { ifndef_ripper($$ = '-'); }
2075 | '*' { ifndef_ripper($$ = '*'); }
2076 | tSTAR { ifndef_ripper($$ = '*'); }
2077 | '/' { ifndef_ripper($$ = '/'); }
2078 | '%' { ifndef_ripper($$ = '%'); }
2079 | tPOW { ifndef_ripper($$ = tPOW); }
2080 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2081 | '!' { ifndef_ripper($$ = '!'); }
2082 | '~' { ifndef_ripper($$ = '~'); }
2083 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2084 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2085 | tAREF { ifndef_ripper($$ = tAREF); }
2086 | tASET { ifndef_ripper($$ = tASET); }
2087 | '`' { ifndef_ripper($$ = '`'); }
2090 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2091 | keyword_BEGIN | keyword_END
2092 | keyword_alias | keyword_and | keyword_begin
2093 | keyword_break | keyword_case | keyword_class | keyword_def
2094 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2095 | keyword_end | keyword_ensure | keyword_false
2096 | keyword_for | keyword_in | keyword_module | keyword_next
2097 | keyword_nil | keyword_not | keyword_or | keyword_redo
2098 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2099 | keyword_super | keyword_then | keyword_true | keyword_undef
2100 | keyword_when | keyword_yield | keyword_if | keyword_unless
2101 | keyword_while | keyword_until
2104 arg : lhs '=' arg_rhs
2107 $$ = node_assign(p, $1, $3, &@$);
2109 /*% ripper: assign!($1, $3) %*/
2111 | var_lhs tOP_ASGN arg_rhs
2114 $$ = new_op_assign(p, $1, $2, $3, &@$);
2116 /*% ripper: opassign!($1, $2, $3) %*/
2118 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2122 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2124 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $6) %*/
2126 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2130 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2132 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
2134 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2138 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2140 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
2142 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2146 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2148 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $5) %*/
2150 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2153 YYLTYPE loc = code_loc_gen(&@1, &@3);
2154 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2156 /*% ripper: opassign!(const_path_field!($1, $3), $4, $5) %*/
2158 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2161 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2163 /*% ripper: opassign!(top_const_field!($2), $3, $4) %*/
2165 | backref tOP_ASGN arg_rhs
2168 rb_backref_error(p, $1);
2169 $$ = NEW_BEGIN(0, &@$);
2171 /*% ripper[error]: assign_error!(opassign!(var_field(p, $1), $2, $3)) %*/
2178 $$ = NEW_DOT2($1, $3, &@$);
2180 /*% ripper: dot2!($1, $3) %*/
2187 $$ = NEW_DOT3($1, $3, &@$);
2189 /*% ripper: dot3!($1, $3) %*/
2195 loc.beg_pos = @2.end_pos;
2196 loc.end_pos = @2.end_pos;
2199 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2201 /*% ripper: dot2!($1, Qnil) %*/
2207 loc.beg_pos = @2.end_pos;
2208 loc.end_pos = @2.end_pos;
2211 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2213 /*% ripper: dot3!($1, Qnil) %*/
2219 loc.beg_pos = @1.beg_pos;
2220 loc.end_pos = @1.beg_pos;
2223 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2225 /*% ripper: dot2!(Qnil, $2) %*/
2231 loc.beg_pos = @1.beg_pos;
2232 loc.end_pos = @1.beg_pos;
2235 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2237 /*% ripper: dot3!(Qnil, $2) %*/
2241 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2245 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2249 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2253 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2257 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2261 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2263 | tUMINUS_NUM simple_numeric tPOW arg
2265 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2269 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2273 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2277 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2281 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2285 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2289 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2291 | rel_expr %prec tCMP
2294 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2298 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2302 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2306 $$ = match_op(p, $1, $3, &@2, &@$);
2310 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2314 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2318 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2322 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2326 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2330 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2334 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2336 | keyword_defined opt_nl {p->in_defined = 1;} arg
2339 $$ = new_defined(p, $4, &@$);
2341 | arg '?' arg opt_nl ':' arg
2345 $$ = new_if(p, $1, $3, $6, &@$);
2348 /*% ripper: ifop!($1, $3, $6) %*/
2356 relop : '>' {$$ = '>';}
2362 rel_expr : arg relop arg %prec '>'
2364 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2366 | rel_expr relop arg %prec '>'
2368 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2369 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2385 | args ',' assocs trailer
2388 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2390 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2395 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2397 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2401 arg_rhs : arg %prec tOP_ASGN
2406 | arg modifier_rescue arg
2409 YYLTYPE loc = code_loc_gen(&@2, &@3);
2411 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2413 /*% ripper: rescue_mod!($1, $3) %*/
2417 paren_args : '(' opt_call_args rparen
2422 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2424 | '(' args_forward rparen
2426 if (!local_id(p, idFWD_REST) ||
2428 !local_id(p, idFWD_KWREST) ||
2430 !local_id(p, idFWD_BLOCK)) {
2431 compile_error(p, "unexpected ...");
2436 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2438 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2440 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2442 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2446 $$ = arg_blk_pass($$, block);
2448 /*% ripper: arg_paren!($2) %*/
2453 opt_paren_args : none
2457 opt_call_args : none
2463 | args ',' assocs ','
2466 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2468 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2473 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2475 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2483 $$ = NEW_LIST($1, &@$);
2485 /*% ripper: args_add!(args_new!, $1) %*/
2487 | args opt_block_arg
2490 $$ = arg_blk_pass($1, $2);
2492 /*% ripper: args_add_block!($1, $2) %*/
2494 | assocs opt_block_arg
2497 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2498 $$ = arg_blk_pass($$, $2);
2500 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
2502 | args ',' assocs opt_block_arg
2505 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2506 $$ = arg_blk_pass($$, $4);
2508 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
2511 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
2515 /* If call_args starts with a open paren '(' or '[',
2516 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2517 * but the push must be done after CMDARG_PUSH(1).
2518 * So this code makes them consistent by first cancelling
2519 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2520 * and finally redoing CMDARG_PUSH(0).
2524 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2527 if (lookahead) CMDARG_POP();
2529 if (lookahead) CMDARG_PUSH(0);
2533 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2534 * but the push must be done after CMDARG_POP() in the parser.
2535 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2536 * CMDARG_POP() to pop 1 pushed by command_args,
2537 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2544 if (lookahead) CMDARG_POP();
2546 if (lookahead) CMDARG_PUSH(0);
2551 block_arg : tAMPER arg_value
2554 $$ = NEW_BLOCK_PASS($2, &@$);
2560 opt_block_arg : ',' block_arg
2573 $$ = NEW_LIST($1, &@$);
2575 /*% ripper: args_add!(args_new!, $1) %*/
2580 $$ = NEW_SPLAT($2, &@$);
2582 /*% ripper: args_add_star!(args_new!, $2) %*/
2584 | args ',' arg_value
2587 $$ = last_arg_append(p, $1, $3, &@$);
2589 /*% ripper: args_add!($1, $3) %*/
2591 | args ',' tSTAR arg_value
2594 $$ = rest_arg_append(p, $1, $4, &@$);
2596 /*% ripper: args_add_star!($1, $4) %*/
2604 mrhs : args ',' arg_value
2607 $$ = last_arg_append(p, $1, $3, &@$);
2609 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
2611 | args ',' tSTAR arg_value
2614 $$ = rest_arg_append(p, $1, $4, &@$);
2616 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
2621 $$ = NEW_SPLAT($2, &@$);
2623 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
2640 $$ = NEW_FCALL($1, 0, &@$);
2642 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
2653 set_line_body($3, @1.end_pos.lineno);
2654 $$ = NEW_BEGIN($3, &@$);
2655 nd_set_line($$, @1.end_pos.lineno);
2657 /*% ripper: begin!($3) %*/
2659 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2662 $$ = NEW_BEGIN(0, &@$);
2664 /*% ripper: paren!(0) %*/
2666 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2669 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2672 /*% ripper: paren!($2) %*/
2674 | tLPAREN compstmt ')'
2677 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2680 /*% ripper: paren!($2) %*/
2682 | primary_value tCOLON2 tCONSTANT
2685 $$ = NEW_COLON2($1, $3, &@$);
2687 /*% ripper: const_path_ref!($1, $3) %*/
2692 $$ = NEW_COLON3($2, &@$);
2694 /*% ripper: top_const_ref!($2) %*/
2696 | tLBRACK aref_args ']'
2699 $$ = make_list($2, &@$);
2701 /*% ripper: array!(escape_Qundef($2)) %*/
2703 | tLBRACE assoc_list '}'
2706 $$ = new_hash(p, $2, &@$);
2707 $$->nd_brace = TRUE;
2709 /*% ripper: hash!(escape_Qundef($2)) %*/
2714 $$ = NEW_RETURN(0, &@$);
2716 /*% ripper: return0! %*/
2718 | keyword_yield '(' call_args rparen
2721 $$ = new_yield(p, $3, &@$);
2723 /*% ripper: yield!(paren!($3)) %*/
2725 | keyword_yield '(' rparen
2728 $$ = NEW_YIELD(0, &@$);
2730 /*% ripper: yield!(paren!(args_new!)) %*/
2735 $$ = NEW_YIELD(0, &@$);
2737 /*% ripper: yield0! %*/
2739 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2742 $$ = new_defined(p, $5, &@$);
2744 | keyword_not '(' expr rparen
2746 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2748 | keyword_not '(' rparen
2750 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2755 $$ = method_add_block(p, $1, $2, &@$);
2757 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
2760 | method_call brace_block
2763 block_dup_check(p, $1->nd_args, $2);
2764 $$ = method_add_block(p, $1, $2, &@$);
2766 /*% ripper: method_add_block!($1, $2) %*/
2770 token_info_push(p, "->", &@1);
2776 nd_set_first_loc($$, @1.beg_pos);
2779 | k_if expr_value then
2785 $$ = new_if(p, $2, $4, $5, &@$);
2788 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
2790 | k_unless expr_value then
2796 $$ = new_unless(p, $2, $4, $5, &@$);
2799 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
2801 | k_while expr_value_do
2806 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2809 /*% ripper: while!($2, $3) %*/
2811 | k_until expr_value_do
2816 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2819 /*% ripper: until!($2, $3) %*/
2821 | k_case expr_value opt_terms
2823 $<val>$ = p->case_labels;
2824 p->case_labels = Qnil;
2829 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2830 p->case_labels = $<val>4;
2832 $$ = NEW_CASE($2, $5, &@$);
2835 /*% ripper: case!($2, $5) %*/
2839 $<val>$ = p->case_labels;
2845 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2846 p->case_labels = $<val>3;
2848 $$ = NEW_CASE2($4, &@$);
2850 /*% ripper: case!(Qnil, $4) %*/
2852 | k_case expr_value opt_terms
2857 $$ = new_case3(p, $2, $4, &@$);
2859 /*% ripper: case!($2, $4) %*/
2861 | k_for for_var keyword_in expr_value_do
2869 * e.each{|*x| a, b, c = x}
2873 * e.each{|x| a, = x}
2875 ID id = internal_id(p);
2876 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2877 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2878 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2879 ID *tbl = ALLOC_N(ID, 3);
2880 rb_imemo_tmpbuf_set_ptr(tmpbuf, tbl);
2881 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2884 switch (nd_type($2)) {
2887 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2888 $2->nd_value = internal_var;
2893 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2894 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2896 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2897 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2899 /* {|*internal_id| <m> = internal_id; ... } */
2900 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2901 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2902 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
2903 $$ = NEW_FOR($4, scope, &@$);
2906 /*% ripper: for!($2, $4, $5) %*/
2908 | k_class cpath superclass
2911 YYLTYPE loc = code_loc_gen(&@1, &@2);
2912 yyerror1(&loc, "class definition in method body");
2914 $<num>1 = p->in_class;
2922 $$ = NEW_CLASS($2, $5, $3, &@$);
2923 nd_set_line($$->nd_body, @6.end_pos.lineno);
2924 set_line_body($5, @3.end_pos.lineno);
2925 nd_set_line($$, @3.end_pos.lineno);
2927 /*% ripper: class!($2, $3, $5) %*/
2929 p->in_class = $<num>1 & 1;
2931 | k_class tLSHFT expr
2933 $<num>$ = (p->in_class << 1) | p->in_def;
2943 $$ = NEW_SCLASS($3, $6, &@$);
2944 nd_set_line($$->nd_body, @7.end_pos.lineno);
2945 set_line_body($6, nd_line($3));
2948 /*% ripper: sclass!($3, $6) %*/
2950 p->in_def = $<num>4 & 1;
2951 p->in_class = ($<num>4 >> 1) & 1;
2956 YYLTYPE loc = code_loc_gen(&@1, &@2);
2957 yyerror1(&loc, "module definition in method body");
2959 $<num>1 = p->in_class;
2967 $$ = NEW_MODULE($2, $4, &@$);
2968 nd_set_line($$->nd_body, @5.end_pos.lineno);
2969 set_line_body($4, @2.end_pos.lineno);
2970 nd_set_line($$, @2.end_pos.lineno);
2972 /*% ripper: module!($2, $4) %*/
2974 p->in_class = $<num>1 & 1;
2978 numparam_name(p, get_id($2));
2980 $<id>$ = p->cur_arg;
2984 $<num>$ = p->in_def;
2992 NODE *body = remove_begin($6);
2993 reduce_nodes(p, &body);
2994 $$ = NEW_DEFN($2, $5, body, &@$);
2995 nd_set_line($$->nd_defn, @7.end_pos.lineno);
2996 set_line_body(body, @1.beg_pos.lineno);
2998 /*% ripper: def!($2, $5, $6) %*/
3000 p->in_def = $<num>4 & 1;
3001 p->cur_arg = $<id>3;
3003 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3005 numparam_name(p, get_id($5));
3006 $<num>4 = p->in_def;
3008 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3010 $<id>$ = p->cur_arg;
3018 NODE *body = remove_begin($8);
3019 reduce_nodes(p, &body);
3020 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3021 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3022 set_line_body(body, @1.beg_pos.lineno);
3024 /*% ripper: defs!($2, $3, $5, $7, $8) %*/
3026 p->in_def = $<num>4 & 1;
3027 p->cur_arg = $<id>6;
3032 $$ = NEW_BREAK(0, &@$);
3034 /*% ripper: break!(args_new!) %*/
3039 $$ = NEW_NEXT(0, &@$);
3041 /*% ripper: next!(args_new!) %*/
3048 /*% ripper: redo! %*/
3053 $$ = NEW_RETRY(&@$);
3055 /*% ripper: retry! %*/
3059 primary_value : primary
3066 k_begin : keyword_begin
3068 token_info_push(p, "begin", &@$);
3075 token_info_push(p, "if", &@$);
3076 if (p->token_info && p->token_info->nonspc &&
3077 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3078 const char *tok = p->lex.ptok;
3079 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3080 beg += rb_strlen_lit("else");
3081 while (beg < tok && ISSPACE(*beg)) beg++;
3083 p->token_info->nonspc = 0;
3089 k_unless : keyword_unless
3091 token_info_push(p, "unless", &@$);
3095 k_while : keyword_while
3097 token_info_push(p, "while", &@$);
3101 k_until : keyword_until
3103 token_info_push(p, "until", &@$);
3107 k_case : keyword_case
3109 token_info_push(p, "case", &@$);
3115 token_info_push(p, "for", &@$);
3119 k_class : keyword_class
3121 token_info_push(p, "class", &@$);
3125 k_module : keyword_module
3127 token_info_push(p, "module", &@$);
3133 token_info_push(p, "def", &@$);
3139 token_info_push(p, "do", &@$);
3143 k_do_block : keyword_do_block
3145 token_info_push(p, "do", &@$);
3149 k_rescue : keyword_rescue
3151 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3155 k_ensure : keyword_ensure
3157 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3161 k_when : keyword_when
3163 token_info_warn(p, "when", p->token_info, 0, &@$);
3167 k_else : keyword_else
3169 token_info *ptinfo_beg = p->token_info;
3170 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3171 token_info_warn(p, "else", p->token_info, same, &@$);
3174 e.next = ptinfo_beg->next;
3176 token_info_setup(&e, p->lex.pbeg, &@$);
3177 if (!e.nonspc) *ptinfo_beg = e;
3182 k_elsif : keyword_elsif
3185 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3191 token_info_pop(p, "end", &@$);
3195 k_return : keyword_return
3197 if (p->in_class && !p->in_def && !dyna_in_block(p))
3198 yyerror1(&@1, "Invalid return in class/module body");
3212 | k_elsif expr_value then
3217 $$ = new_if(p, $2, $4, $5, &@$);
3220 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3230 /*% ripper: else!($2) %*/
3241 $$ = assignable(p, $1, 0, &@$);
3242 mark_lvar_used(p, $$);
3244 /*% ripper: assignable(p, $1) %*/
3246 | tLPAREN f_margs rparen
3251 /*% ripper: mlhs_paren!($2) %*/
3255 f_marg_list : f_marg
3258 $$ = NEW_LIST($1, &@$);
3260 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3262 | f_marg_list ',' f_marg
3265 $$ = list_append(p, $1, $3);
3267 /*% ripper: mlhs_add!($1, $3) %*/
3271 f_margs : f_marg_list
3274 $$ = NEW_MASGN($1, 0, &@$);
3278 | f_marg_list ',' f_rest_marg
3281 $$ = NEW_MASGN($1, $3, &@$);
3283 /*% ripper: mlhs_add_star!($1, $3) %*/
3285 | f_marg_list ',' f_rest_marg ',' f_marg_list
3288 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3290 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3295 $$ = NEW_MASGN(0, $1, &@$);
3297 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3299 | f_rest_marg ',' f_marg_list
3302 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3304 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3308 f_rest_marg : tSTAR f_norm_arg
3311 $$ = assignable(p, $2, 0, &@$);
3312 mark_lvar_used(p, $$);
3314 /*% ripper: assignable(p, $2) %*/
3319 $$ = NODE_SPECIAL_NO_NAME_REST;
3321 /*% ripper: Qnil %*/
3325 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3327 $$ = new_args_tail(p, $1, $3, $4, &@3);
3329 | f_block_kwarg opt_f_block_arg
3331 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3333 | f_kwrest opt_f_block_arg
3335 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3337 | f_no_kwarg opt_f_block_arg
3339 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3343 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3347 opt_block_args_tail : ',' block_args_tail
3353 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3357 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3359 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3361 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3363 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3365 | f_arg ',' f_block_optarg opt_block_args_tail
3367 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3369 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3371 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3373 | f_arg ',' f_rest_arg opt_block_args_tail
3375 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3380 /* magic number for rest_id in iseq_set_arguments() */
3381 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3383 /*% ripper: new_args(p, $1, Qnone, excessed_comma!, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL) %*/
3385 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3387 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3389 | f_arg opt_block_args_tail
3391 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3393 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3395 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3397 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3399 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3401 | f_block_optarg opt_block_args_tail
3403 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3405 | f_block_optarg ',' f_arg opt_block_args_tail
3407 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3409 | f_rest_arg opt_block_args_tail
3411 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3413 | f_rest_arg ',' f_arg opt_block_args_tail
3415 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3419 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3423 opt_block_param : none
3426 p->command_start = TRUE;
3430 block_param_def : '|' opt_bv_decl '|'
3433 p->max_numparam = ORDINAL_PARAM;
3437 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
3439 | '|' block_param opt_bv_decl '|'
3442 p->max_numparam = ORDINAL_PARAM;
3446 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
3451 opt_bv_decl : opt_nl
3455 | opt_nl ';' bv_decls opt_nl
3465 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
3467 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
3472 new_bv(p, get_id($1));
3473 /*% ripper: get_value($1) %*/
3482 $<vars>$ = dyna_push(p);
3485 $<num>$ = p->lex.lpar_beg;
3486 p->lex.lpar_beg = p->lex.paren_nest;
3489 $<num>$ = p->max_numparam;
3490 p->max_numparam = 0;
3493 $<node>$ = numparam_push(p);
3501 int max_numparam = p->max_numparam;
3502 p->lex.lpar_beg = $<num>2;
3503 p->max_numparam = $<num>3;
3505 $5 = args_with_numbered(p, $5, max_numparam);
3508 YYLTYPE loc = code_loc_gen(&@5, &@7);
3509 $$ = NEW_LAMBDA($5, $7, &loc);
3510 nd_set_line($$->nd_body, @7.end_pos.lineno);
3511 nd_set_line($$, @5.end_pos.lineno);
3514 /*% ripper: lambda!($5, $7) %*/
3515 numparam_pop(p, $<node>4);
3516 dyna_pop(p, $<vars>1);
3520 f_larglist : '(' f_args opt_bv_decl ')'
3524 p->max_numparam = ORDINAL_PARAM;
3526 /*% ripper: paren!($2) %*/
3531 if (!args_info_empty_p($1->nd_ainfo))
3532 p->max_numparam = ORDINAL_PARAM;
3538 lambda_body : tLAMBEG compstmt '}'
3540 token_info_pop(p, "}", &@3);
3543 | keyword_do_LAMBDA bodystmt k_end
3549 do_block : k_do_block do_body k_end
3553 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3554 nd_set_line($$, @1.end_pos.lineno);
3559 block_call : command do_block
3562 if (nd_type($1) == NODE_YIELD) {
3563 compile_error(p, "block given to yield");
3566 block_dup_check(p, $1->nd_args, $2);
3568 $$ = method_add_block(p, $1, $2, &@$);
3571 /*% ripper: method_add_block!($1, $2) %*/
3573 | block_call call_op2 operation2 opt_paren_args
3576 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3578 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3580 | block_call call_op2 operation2 opt_paren_args brace_block
3583 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3585 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
3587 | block_call call_op2 operation2 command_args do_block
3590 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3592 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
3596 method_call : fcall paren_args
3601 nd_set_last_loc($1, @2.end_pos);
3603 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
3605 | primary_value call_op operation2 opt_paren_args
3608 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3609 nd_set_line($$, @3.end_pos.lineno);
3611 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3613 | primary_value tCOLON2 operation2 paren_args
3616 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3617 nd_set_line($$, @3.end_pos.lineno);
3619 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
3621 | primary_value tCOLON2 operation3
3624 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3626 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
3628 | primary_value call_op paren_args
3631 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3632 nd_set_line($$, @2.end_pos.lineno);
3634 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
3636 | primary_value tCOLON2 paren_args
3639 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3640 nd_set_line($$, @2.end_pos.lineno);
3642 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
3644 | keyword_super paren_args
3647 $$ = NEW_SUPER($2, &@$);
3649 /*% ripper: super!($2) %*/
3654 $$ = NEW_ZSUPER(&@$);
3656 /*% ripper: zsuper! %*/
3658 | primary_value '[' opt_call_args rbracket
3661 if ($1 && nd_type($1) == NODE_SELF)
3662 $$ = NEW_FCALL(tAREF, $3, &@$);
3664 $$ = NEW_CALL($1, tAREF, $3, &@$);
3667 /*% ripper: aref!($1, escape_Qundef($3)) %*/
3671 brace_block : '{' brace_body '}'
3675 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3676 nd_set_line($$, @1.end_pos.lineno);
3679 | k_do do_body k_end
3683 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3684 nd_set_line($$, @1.end_pos.lineno);
3689 brace_body : {$<vars>$ = dyna_push(p);}
3691 $<num>$ = p->max_numparam;
3692 p->max_numparam = 0;
3695 $<node>$ = numparam_push(p);
3697 opt_block_param compstmt
3699 int max_numparam = p->max_numparam;
3700 p->max_numparam = $<num>2;
3701 $4 = args_with_numbered(p, $4, max_numparam);
3703 $$ = NEW_ITER($4, $5, &@$);
3705 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
3706 numparam_pop(p, $<node>3);
3707 dyna_pop(p, $<vars>1);
3711 do_body : {$<vars>$ = dyna_push(p);}
3713 $<num>$ = p->max_numparam;
3714 p->max_numparam = 0;
3717 $<node>$ = numparam_push(p);
3720 opt_block_param bodystmt
3722 int max_numparam = p->max_numparam;
3723 p->max_numparam = $<num>2;
3724 $4 = args_with_numbered(p, $4, max_numparam);
3726 $$ = NEW_ITER($4, $5, &@$);
3728 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
3730 numparam_pop(p, $<node>3);
3731 dyna_pop(p, $<vars>1);
3735 case_args : arg_value
3738 check_literal_when(p, $1, &@1);
3739 $$ = NEW_LIST($1, &@$);
3741 /*% ripper: args_add!(args_new!, $1) %*/
3746 $$ = NEW_SPLAT($2, &@$);
3748 /*% ripper: args_add_star!(args_new!, $2) %*/
3750 | case_args ',' arg_value
3753 check_literal_when(p, $3, &@3);
3754 $$ = last_arg_append(p, $1, $3, &@$);
3756 /*% ripper: args_add!($1, $3) %*/
3758 | case_args ',' tSTAR arg_value
3761 $$ = rest_arg_append(p, $1, $4, &@$);
3763 /*% ripper: args_add_star!($1, $4) %*/
3767 case_body : k_when case_args then
3772 $$ = NEW_WHEN($2, $4, $5, &@$);
3775 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
3783 p_case_body : keyword_in
3785 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3786 p->command_start = FALSE;
3787 $<num>$ = p->in_kwarg;
3790 {$<tbl>$ = push_pvtbl(p);}
3791 {$<tbl>$ = push_pktbl(p);}
3793 {pop_pktbl(p, $<tbl>4);}
3794 {pop_pvtbl(p, $<tbl>3);}
3796 p->in_kwarg = !!$<num>2;
3802 $$ = NEW_IN($5, $10, $11, &@$);
3804 /*% ripper: in!($5, $10, escape_Qundef($11)) %*/
3812 p_top_expr : p_top_expr_body
3813 | p_top_expr_body modifier_if expr_value
3816 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3819 /*% ripper: if_mod!($3, $1) %*/
3821 | p_top_expr_body modifier_unless expr_value
3824 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3827 /*% ripper: unless_mod!($3, $1) %*/
3831 p_top_expr_body : p_expr
3834 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3835 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3839 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3841 nd_set_first_loc($$, @1.beg_pos);
3847 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3851 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3858 p_as : p_expr tASSOC p_variable
3861 NODE *n = NEW_LIST($1, &@$);
3862 n = list_append(p, n, $3);
3863 $$ = new_hash(p, n, &@$);
3865 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
3870 p_alt : p_alt '|' p_expr_basic
3873 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3875 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
3880 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3881 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3883 p_expr_basic : p_value
3884 | p_const p_lparen p_args rparen
3886 pop_pktbl(p, $<tbl>2);
3887 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3889 nd_set_first_loc($$, @1.beg_pos);
3893 | p_const p_lparen p_kwargs rparen
3895 pop_pktbl(p, $<tbl>2);
3896 $$ = new_hash_pattern(p, $1, $3, &@$);
3898 nd_set_first_loc($$, @1.beg_pos);
3902 | p_const '(' rparen
3904 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3905 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3907 | p_const p_lbracket p_args rbracket
3909 pop_pktbl(p, $<tbl>2);
3910 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3912 nd_set_first_loc($$, @1.beg_pos);
3916 | p_const p_lbracket p_kwargs rbracket
3918 pop_pktbl(p, $<tbl>2);
3919 $$ = new_hash_pattern(p, $1, $3, &@$);
3921 nd_set_first_loc($$, @1.beg_pos);
3925 | p_const '[' rbracket
3927 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3928 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3930 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3932 pop_pktbl(p, $<tbl>2);
3933 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3937 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3938 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3942 $<tbl>$ = push_pktbl(p);
3943 $<num>1 = p->in_kwarg;
3948 pop_pktbl(p, $<tbl>2);
3949 p->in_kwarg = $<num>1;
3950 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3954 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3955 $$ = new_hash_pattern(p, Qnone, $$, &@$);
3957 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3959 pop_pktbl(p, $<tbl>2);
3967 NODE *pre_args = NEW_LIST($1, &@$);
3968 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3970 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3975 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3980 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
3982 VALUE pre_args = rb_ary_concat($1, get_value($2));
3983 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3986 | p_args_head tSTAR tIDENTIFIER
3988 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
3990 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
3992 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
3996 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3998 | p_args_head tSTAR ',' p_args_post
4000 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4005 p_args_head : p_arg ','
4009 | p_args_head p_arg ','
4012 $$ = list_concat($1, $2);
4014 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4018 p_args_tail : tSTAR tIDENTIFIER
4020 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4022 | tSTAR tIDENTIFIER ',' p_args_post
4024 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4028 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4030 | tSTAR ',' p_args_post
4032 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4037 | p_args_post ',' p_arg
4040 $$ = list_concat($1, $3);
4042 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4049 $$ = NEW_LIST($1, &@$);
4051 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4055 p_kwargs : p_kwarg ',' p_kwrest
4057 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4061 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4065 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4069 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4071 | p_kwarg ',' p_kwnorest
4073 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4077 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4082 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4086 $$ = list_concat($1, $3);
4088 /*% ripper: rb_ary_push($1, $3) %*/
4092 p_kw : p_kw_label p_expr
4094 error_duplicate_pattern_key(p, get_id($1), &@1);
4096 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4098 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4102 error_duplicate_pattern_key(p, get_id($1), &@1);
4103 if ($1 && !is_local_id(get_id($1))) {
4104 yyerror1(&@1, "key must be valid as local variables");
4106 error_duplicate_pattern_variable(p, get_id($1), &@1);
4108 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4110 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4115 | tSTRING_BEG string_contents tLABEL_END
4117 YYLTYPE loc = code_loc_gen(&@1, &@3);
4119 if (!$2 || nd_type($2) == NODE_STR) {
4120 NODE *node = dsym_node(p, $2, &loc);
4121 $$ = SYM2ID(node->nd_lit);
4124 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4125 VALUE label = RNODE($2)->nd_cval;
4126 VALUE rval = RNODE($2)->nd_rval;
4127 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4128 RNODE($$)->nd_loc = loc;
4132 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4138 p_kwrest : kwrest_mark tIDENTIFIER
4148 p_kwnorest : kwrest_mark keyword_nil
4154 p_value : p_primitive
4155 | p_primitive tDOT2 p_primitive
4160 $$ = NEW_DOT2($1, $3, &@$);
4162 /*% ripper: dot2!($1, $3) %*/
4164 | p_primitive tDOT3 p_primitive
4169 $$ = NEW_DOT3($1, $3, &@$);
4171 /*% ripper: dot3!($1, $3) %*/
4177 loc.beg_pos = @2.end_pos;
4178 loc.end_pos = @2.end_pos;
4181 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4183 /*% ripper: dot2!($1, Qnil) %*/
4189 loc.beg_pos = @2.end_pos;
4190 loc.end_pos = @2.end_pos;
4193 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4195 /*% ripper: dot3!($1, Qnil) %*/
4200 | tBDOT2 p_primitive
4204 loc.beg_pos = @1.beg_pos;
4205 loc.end_pos = @1.beg_pos;
4208 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4210 /*% ripper: dot2!(Qnil, $2) %*/
4212 | tBDOT3 p_primitive
4216 loc.beg_pos = @1.beg_pos;
4217 loc.end_pos = @1.beg_pos;
4220 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4222 /*% ripper: dot3!(Qnil, $2) %*/
4226 p_primitive : literal
4237 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4239 /*% ripper: var_ref!($1) %*/
4243 token_info_push(p, "->", &@1);
4249 nd_set_first_loc($$, @1.beg_pos);
4254 p_variable : tIDENTIFIER
4257 error_duplicate_pattern_variable(p, $1, &@1);
4258 $$ = assignable(p, $1, 0, &@$);
4260 /*% ripper: assignable(p, var_field(p, $1)) %*/
4264 p_var_ref : '^' tIDENTIFIER
4267 NODE *n = gettable(p, $2, &@$);
4268 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4269 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4273 /*% ripper: var_ref!($2) %*/
4277 p_const : tCOLON3 cname
4280 $$ = NEW_COLON3($2, &@$);
4282 /*% ripper: top_const_ref!($2) %*/
4284 | p_const tCOLON2 cname
4287 $$ = NEW_COLON2($1, $3, &@$);
4289 /*% ripper: const_path_ref!($1, $3) %*/
4294 $$ = gettable(p, $1, &@$);
4296 /*% ripper: var_ref!($1) %*/
4300 opt_rescue : k_rescue exc_list exc_var then
4305 $$ = NEW_RESBODY($2,
4306 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4308 fixpos($$, $2?$2:$5);
4310 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4315 exc_list : arg_value
4318 $$ = NEW_LIST($1, &@$);
4320 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4325 if (!($$ = splat_array($1))) $$ = $1;
4332 exc_var : tASSOC lhs
4339 opt_ensure : k_ensure compstmt
4344 /*% ripper: ensure!($2) %*/
4358 node = NEW_STR(STR_NEW0(), &@$);
4359 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4362 node = evstr2dstr(p, node);
4375 $$ = literal_concat(p, $1, $2, &@$);
4377 /*% ripper: string_concat!($1, $2) %*/
4381 string1 : tSTRING_BEG string_contents tSTRING_END
4384 $$ = heredoc_dedent(p, $2);
4385 if ($$) nd_set_loc($$, &@$);
4387 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4391 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4394 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4396 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4400 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4402 $$ = new_regexp(p, $2, $3, &@$);
4406 words : tWORDS_BEG ' ' word_list tSTRING_END
4409 $$ = make_list($3, &@$);
4411 /*% ripper: array!($3) %*/
4415 word_list : /* none */
4420 /*% ripper: words_new! %*/
4422 | word_list word ' '
4425 $$ = list_append(p, $1, evstr2dstr(p, $2));
4427 /*% ripper: words_add!($1, $2) %*/
4431 word : string_content
4432 /*% ripper[brace]: word_add!(word_new!, $1) %*/
4433 | word string_content
4436 $$ = literal_concat(p, $1, $2, &@$);
4438 /*% ripper: word_add!($1, $2) %*/
4442 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4445 $$ = make_list($3, &@$);
4447 /*% ripper: array!($3) %*/
4451 symbol_list : /* none */
4456 /*% ripper: symbols_new! %*/
4458 | symbol_list word ' '
4461 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4463 /*% ripper: symbols_add!($1, $2) %*/
4467 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4470 $$ = make_list($3, &@$);
4472 /*% ripper: array!($3) %*/
4476 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4479 $$ = make_list($3, &@$);
4481 /*% ripper: array!($3) %*/
4485 qword_list : /* none */
4490 /*% ripper: qwords_new! %*/
4492 | qword_list tSTRING_CONTENT ' '
4495 $$ = list_append(p, $1, $2);
4497 /*% ripper: qwords_add!($1, $2) %*/
4501 qsym_list : /* none */
4506 /*% ripper: qsymbols_new! %*/
4508 | qsym_list tSTRING_CONTENT ' '
4511 $$ = symbol_append(p, $1, $2);
4513 /*% ripper: qsymbols_add!($1, $2) %*/
4517 string_contents : /* none */
4522 /*% ripper: string_content! %*/
4525 $$ = ripper_new_yylval(p, 0, $$, 0);
4528 | string_contents string_content
4531 $$ = literal_concat(p, $1, $2, &@$);
4533 /*% ripper: string_add!($1, $2) %*/
4536 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4537 !RNODE($1)->nd_cval) {
4538 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4539 RNODE($1)->nd_rval = add_mark_object(p, $$);
4546 xstring_contents: /* none */
4551 /*% ripper: xstring_new! %*/
4553 | xstring_contents string_content
4556 $$ = literal_concat(p, $1, $2, &@$);
4558 /*% ripper: xstring_add!($1, $2) %*/
4562 regexp_contents: /* none */
4567 /*% ripper: regexp_new! %*/
4570 $$ = ripper_new_yylval(p, 0, $$, 0);
4573 | regexp_contents string_content
4576 NODE *head = $1, *tail = $2;
4584 switch (nd_type(head)) {
4586 nd_set_type(head, NODE_DSTR);
4591 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4594 $$ = list_append(p, head, tail);
4597 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4598 if (ripper_is_node_yylval(n1)) {
4599 s1 = RNODE(n1)->nd_cval;
4600 n1 = RNODE(n1)->nd_rval;
4602 if (ripper_is_node_yylval(n2)) {
4603 s2 = RNODE(n2)->nd_cval;
4604 n2 = RNODE(n2)->nd_rval;
4606 $$ = dispatch2(regexp_add, n1, n2);
4608 $$ = ripper_new_yylval(p, 0, $$, s2);
4614 string_content : tSTRING_CONTENT
4615 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
4618 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4619 $<strterm>$ = p->lex.strterm;
4621 SET_LEX_STATE(EXPR_BEG);
4625 p->lex.strterm = $<strterm>2;
4627 $$ = NEW_EVSTR($3, &@$);
4628 nd_set_line($$, @3.end_pos.lineno);
4630 /*% ripper: string_dvar!($3) %*/
4638 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4639 $<strterm>$ = p->lex.strterm;
4643 $<num>$ = p->lex.state;
4644 SET_LEX_STATE(EXPR_BEG);
4647 $<num>$ = p->lex.brace_nest;
4648 p->lex.brace_nest = 0;
4651 $<num>$ = p->heredoc_indent;
4652 p->heredoc_indent = 0;
4654 compstmt tSTRING_DEND
4658 p->lex.strterm = $<strterm>3;
4659 SET_LEX_STATE($<num>4);
4660 p->lex.brace_nest = $<num>5;
4661 p->heredoc_indent = $<num>6;
4662 p->heredoc_line_indent = -1;
4664 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4665 $$ = new_evstr(p, $7, &@$);
4667 /*% ripper: string_embexpr!($7) %*/
4674 $$ = NEW_GVAR($1, &@$);
4676 /*% ripper: var_ref!($1) %*/
4681 $$ = NEW_IVAR($1, &@$);
4683 /*% ripper: var_ref!($1) %*/
4688 $$ = NEW_CVAR($1, &@$);
4690 /*% ripper: var_ref!($1) %*/
4701 SET_LEX_STATE(EXPR_END);
4703 $$ = NEW_LIT(ID2SYM($2), &@$);
4705 /*% ripper: symbol_literal!(symbol!($2)) %*/
4715 dsym : tSYMBEG string_contents tSTRING_END
4717 SET_LEX_STATE(EXPR_END);
4719 $$ = dsym_node(p, $2, &@$);
4721 /*% ripper: dyna_symbol!($2) %*/
4725 numeric : simple_numeric
4726 | tUMINUS_NUM simple_numeric %prec tLOWEST
4730 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4732 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
4736 simple_numeric : tINTEGER
4742 user_variable : tIDENTIFIER
4749 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4750 | keyword_self {$$ = KWD2EID(self, $1);}
4751 | keyword_true {$$ = KWD2EID(true, $1);}
4752 | keyword_false {$$ = KWD2EID(false, $1);}
4753 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4754 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4755 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4758 var_ref : user_variable
4761 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4763 if (id_is_var(p, get_id($1))) {
4764 $$ = dispatch1(var_ref, $1);
4767 $$ = dispatch1(vcall, $1);
4774 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4776 /*% ripper: var_ref!($1) %*/
4780 var_lhs : user_variable
4783 $$ = assignable(p, $1, 0, &@$);
4785 /*% ripper: assignable(p, var_field(p, $1)) %*/
4790 $$ = assignable(p, $1, 0, &@$);
4792 /*% ripper: assignable(p, var_field(p, $1)) %*/
4802 SET_LEX_STATE(EXPR_BEG);
4803 p->command_start = TRUE;
4814 /*% ripper: Qnil %*/
4818 f_arglist : '(' f_args rparen
4823 /*% ripper: paren!($2) %*/
4824 SET_LEX_STATE(EXPR_BEG);
4825 p->command_start = TRUE;
4827 | '(' args_forward rparen
4829 arg_var(p, idFWD_REST);
4831 arg_var(p, idFWD_KWREST);
4833 arg_var(p, idFWD_BLOCK);
4835 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4836 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4838 /*% ripper: paren!(params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone)) %*/
4839 SET_LEX_STATE(EXPR_BEG);
4840 p->command_start = TRUE;
4843 $<num>$ = p->in_kwarg;
4845 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4849 p->in_kwarg = !!$<num>1;
4851 SET_LEX_STATE(EXPR_BEG);
4852 p->command_start = TRUE;
4856 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4858 $$ = new_args_tail(p, $1, $3, $4, &@3);
4860 | f_kwarg opt_f_block_arg
4862 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4864 | f_kwrest opt_f_block_arg
4866 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4868 | f_no_kwarg opt_f_block_arg
4870 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4874 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4878 opt_args_tail : ',' args_tail
4884 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4888 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4890 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4892 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4894 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4896 | f_arg ',' f_optarg opt_args_tail
4898 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4900 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4902 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4904 | f_arg ',' f_rest_arg opt_args_tail
4906 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4908 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4910 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4912 | f_arg opt_args_tail
4914 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4916 | f_optarg ',' f_rest_arg opt_args_tail
4918 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4920 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4922 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4924 | f_optarg opt_args_tail
4926 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4928 | f_optarg ',' f_arg opt_args_tail
4930 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4932 | f_rest_arg opt_args_tail
4934 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4936 | f_rest_arg ',' f_arg opt_args_tail
4938 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4942 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4946 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4947 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4951 args_forward : tBDOT3
4956 /*% ripper: args_forward! %*/
4960 f_bad_arg : tCONSTANT
4963 yyerror1(&@1, "formal argument cannot be a constant");
4966 /*% ripper[error]: param_error!($1) %*/
4971 yyerror1(&@1, "formal argument cannot be an instance variable");
4974 /*% ripper[error]: param_error!($1) %*/
4979 yyerror1(&@1, "formal argument cannot be a global variable");
4982 /*% ripper[error]: param_error!($1) %*/
4987 yyerror1(&@1, "formal argument cannot be a class variable");
4990 /*% ripper[error]: param_error!($1) %*/
4994 f_norm_arg : f_bad_arg
4997 formal_argument(p, get_id($1));
4998 p->max_numparam = ORDINAL_PARAM;
5003 f_arg_asgn : f_norm_arg
5012 f_arg_item : f_arg_asgn
5016 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5018 /*% ripper: get_value($1) %*/
5020 | tLPAREN f_margs rparen
5023 ID tid = internal_id(p);
5025 loc.beg_pos = @2.beg_pos;
5026 loc.end_pos = @2.beg_pos;
5028 if (dyna_in_block(p)) {
5029 $2->nd_value = NEW_DVAR(tid, &loc);
5032 $2->nd_value = NEW_LVAR(tid, &loc);
5034 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5037 /*% ripper: mlhs_paren!($2) %*/
5042 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5043 | f_arg ',' f_arg_item
5048 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5049 rb_discard_node(p, $3);
5051 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5059 arg_var(p, formal_argument(p, id));
5061 p->max_numparam = ORDINAL_PARAM;
5066 f_kw : f_label arg_value
5070 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5072 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5078 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5080 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5084 f_block_kw : f_label primary_value
5087 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5089 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5094 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5096 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5100 f_block_kwarg : f_block_kw
5105 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5107 | f_block_kwarg ',' f_block_kw
5110 $$ = kwd_append($1, $3);
5112 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5122 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5127 $$ = kwd_append($1, $3);
5129 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5137 f_no_kwarg : kwrest_mark keyword_nil
5141 /*% ripper: nokw_param!(Qnil) %*/
5145 f_kwrest : kwrest_mark tIDENTIFIER
5147 arg_var(p, shadowing_lvar(p, get_id($2)));
5151 /*% ripper: kwrest_param!($2) %*/
5156 $$ = internal_id(p);
5159 /*% ripper: kwrest_param!(Qnil) %*/
5163 f_opt : f_arg_asgn '=' arg_value
5167 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5169 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5173 f_block_opt : f_arg_asgn '=' primary_value
5177 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5179 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5183 f_block_optarg : f_block_opt
5188 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5190 | f_block_optarg ',' f_block_opt
5193 $$ = opt_arg_append($1, $3);
5195 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5204 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5206 | f_optarg ',' f_opt
5209 $$ = opt_arg_append($1, $3);
5211 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5219 f_rest_arg : restarg_mark tIDENTIFIER
5221 arg_var(p, shadowing_lvar(p, get_id($2)));
5225 /*% ripper: rest_param!($2) %*/
5230 $$ = internal_id(p);
5233 /*% ripper: rest_param!(Qnil) %*/
5241 f_block_arg : blkarg_mark tIDENTIFIER
5243 arg_var(p, shadowing_lvar(p, get_id($2)));
5247 /*% ripper: blockarg!($2) %*/
5251 opt_f_block_arg : ',' f_block_arg
5266 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5269 switch (nd_type($3)) {
5278 yyerror1(&@3, "can't define singleton method for literals");
5286 /*% ripper: paren!($3) %*/
5296 /*% ripper: assoclist_from_args!($1) %*/
5301 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5311 if (assocs->nd_head &&
5312 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5313 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5315 tail = tail->nd_next->nd_head->nd_head;
5317 assocs = list_concat(assocs, tail);
5321 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5325 assoc : arg_value tASSOC arg_value
5328 if (nd_type($1) == NODE_STR) {
5329 nd_set_type($1, NODE_LIT);
5330 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5332 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5334 /*% ripper: assoc_new!($1, $3) %*/
5339 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5341 /*% ripper: assoc_new!($1, $2) %*/
5343 | tSTRING_BEG string_contents tLABEL_END arg_value
5346 YYLTYPE loc = code_loc_gen(&@1, &@3);
5347 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5349 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
5354 if (nd_type($2) == NODE_HASH &&
5355 !($2->nd_head && $2->nd_head->nd_alen)) {
5356 static VALUE empty_hash;
5358 empty_hash = rb_obj_freeze(rb_hash_new());
5359 rb_gc_register_mark_object(empty_hash);
5361 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5364 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5366 /*% ripper: assoc_splat!($2) %*/
5370 operation : tIDENTIFIER
5375 operation2 : tIDENTIFIER
5381 operation3 : tIDENTIFIER
5398 opt_terms : /* none */
5409 rbracket : opt_nl ']'
5415 trailer : /* none */
5420 term : ';' {yyerrok;token_flush(p);}
5421 | '\n' {token_flush(p);}
5425 | terms ';' {yyerrok;}
5437 # define yylval (*p->lval)
5439 static int regx_options(struct parser_params*);
5440 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5441 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5442 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5443 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5446 # define set_yylval_node(x) { \
5448 rb_parser_set_location(p, &_cur_loc); \
5449 yylval.node = (x); \
5451 # define set_yylval_str(x) \
5453 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5454 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5456 # define set_yylval_literal(x) \
5458 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5459 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5461 # define set_yylval_num(x) (yylval.num = (x))
5462 # define set_yylval_id(x) (yylval.id = (x))
5463 # define set_yylval_name(x) (yylval.id = (x))
5464 # define yylval_id() (yylval.id)
5467 ripper_yylval_id(struct parser_params *p, ID x)
5469 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5471 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5472 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5473 # define set_yylval_id(x) (void)(x)
5474 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5475 # define set_yylval_literal(x) add_mark_object(p, (x))
5476 # define set_yylval_node(x) (void)(x)
5477 # define yylval_id() yylval.id
5478 # define _cur_loc NULL_LOC /* dummy */
5481 #define set_yylval_noname() set_yylval_id(keyword_nil)
5484 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5485 #define dispatch_scan_event(p, t) ((void)0)
5486 #define dispatch_delayed_token(p, t) ((void)0)
5487 #define has_delayed_token(p) (0)
5489 #define literal_flush(p, ptr) ((void)(ptr))
5491 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5494 intern_sym(const char *name)
5496 ID id = rb_intern_const(name);
5501 ripper_has_scan_event(struct parser_params *p)
5503 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5504 return p->lex.pcur > p->lex.ptok;
5508 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5510 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5511 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5517 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5519 if (!ripper_has_scan_event(p)) return;
5520 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5522 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5525 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5527 int saved_line = p->ruby_sourceline;
5528 const char *saved_tokp = p->lex.ptok;
5530 if (NIL_P(p->delayed.token)) return;
5531 p->ruby_sourceline = p->delayed.line;
5532 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5533 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5534 p->delayed.token = Qnil;
5535 p->ruby_sourceline = saved_line;
5536 p->lex.ptok = saved_tokp;
5538 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5539 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5542 #include "ruby/regex.h"
5543 #include "ruby/util.h"
5546 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5548 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5552 parser_is_identchar(struct parser_params *p)
5554 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5558 parser_isascii(struct parser_params *p)
5560 return ISASCII(*(p->lex.pcur-1));
5564 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5566 int column = 1, nonspc = 0, i;
5567 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5569 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5572 if (*ptr != ' ' && *ptr != '\t') {
5577 ptinfo->beg = loc->beg_pos;
5578 ptinfo->indent = column;
5579 ptinfo->nonspc = nonspc;
5583 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5587 if (!p->token_info_enabled) return;
5588 ptinfo = ALLOC(token_info);
5589 ptinfo->token = token;
5590 ptinfo->next = p->token_info;
5591 token_info_setup(ptinfo, p->lex.pbeg, loc);
5593 p->token_info = ptinfo;
5597 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5599 token_info *ptinfo_beg = p->token_info;
5601 if (!ptinfo_beg) return;
5602 p->token_info = ptinfo_beg->next;
5604 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5605 token_info_warn(p, token, ptinfo_beg, 1, loc);
5606 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5610 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5612 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5613 if (!p->token_info_enabled) return;
5614 if (!ptinfo_beg) return;
5615 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5616 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5617 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5618 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5619 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5620 rb_warn3L(ptinfo_end->beg.lineno,
5621 "mismatched indentations at '%s' with '%s' at %d",
5622 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5626 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5628 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5629 if (!MBCLEN_CHARFOUND_P(len)) {
5630 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5637 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5640 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5643 int lineno = p->ruby_sourceline;
5647 else if (yylloc->beg_pos.lineno == lineno) {
5648 str = p->lex.lastline;
5653 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5657 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5662 yylloc = RUBY_SET_YYLLOC(current);
5664 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5665 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5666 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5667 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5670 compile_error(p, "%s", msg);
5671 parser_show_error_line(p, yylloc);
5676 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5679 const int max_line_margin = 30;
5680 const char *ptr, *ptr_end, *pt, *pb;
5681 const char *pre = "", *post = "", *pend;
5682 const char *code = "", *caret = "";
5684 const char *const pbeg = RSTRING_PTR(str);
5689 if (!yylloc) return;
5690 pend = RSTRING_END(str);
5691 if (pend > pbeg && pend[-1] == '\n') {
5692 if (--pend > pbeg && pend[-1] == '\r') --pend;
5696 if (lineno == yylloc->end_pos.lineno &&
5697 (pend - pbeg) > yylloc->end_pos.column) {
5698 pt = pbeg + yylloc->end_pos.column;
5702 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5703 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5705 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5706 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5708 len = ptr_end - ptr;
5711 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5712 if (ptr > pbeg) pre = "...";
5714 if (ptr_end < pend) {
5715 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5716 if (ptr_end < pend) post = "...";
5720 if (lineno == yylloc->beg_pos.lineno) {
5721 pb += yylloc->beg_pos.column;
5722 if (pb > pt) pb = pt;
5724 if (pb < ptr) pb = ptr;
5725 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5728 if (RTEST(errbuf)) {
5729 mesg = rb_attr_get(errbuf, idMesg);
5730 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5731 rb_str_cat_cstr(mesg, "\n");
5734 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5736 if (!errbuf && rb_stderr_tty_p()) {
5737 #define CSI_BEGIN "\033["
5740 CSI_BEGIN""CSI_SGR"%s" /* pre */
5741 CSI_BEGIN"1"CSI_SGR"%.*s"
5742 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5743 CSI_BEGIN";1"CSI_SGR"%.*s"
5744 CSI_BEGIN""CSI_SGR"%s" /* post */
5747 (int)(pb - ptr), ptr,
5749 (int)(ptr_end - pt), pt,
5755 len = ptr_end - ptr;
5756 lim = pt < pend ? pt : pend;
5757 i = (int)(lim - ptr);
5758 buf = ALLOCA_N(char, i+2);
5763 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5769 memset(p2, '~', (lim - ptr));
5773 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5774 pre, (int)len, code, post,
5777 if (!errbuf) rb_write_error_str(mesg);
5781 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5783 const char *pcur = 0, *ptok = 0;
5785 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5786 p->ruby_sourceline == yylloc->end_pos.lineno) {
5789 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5790 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5792 dispatch1(parse_error, STR_NEW2(msg));
5802 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5805 #endif /* !RIPPER */
5809 vtable_size(const struct vtable *tbl)
5811 if (!DVARS_TERMINAL_P(tbl)) {
5820 static struct vtable *
5821 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5823 struct vtable *tbl = ALLOC(struct vtable);
5826 tbl->tbl = ALLOC_N(ID, tbl->capa);
5830 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5835 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5838 vtable_free_gen(struct parser_params *p, int line, const char *name,
5843 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5846 if (!DVARS_TERMINAL_P(tbl)) {
5848 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5850 ruby_sized_xfree(tbl, sizeof(tbl));
5853 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5856 vtable_add_gen(struct parser_params *p, int line, const char *name,
5857 struct vtable *tbl, ID id)
5861 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5862 line, name, (void *)tbl, rb_id2name(id));
5865 if (DVARS_TERMINAL_P(tbl)) {
5866 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5869 if (tbl->pos == tbl->capa) {
5870 tbl->capa = tbl->capa * 2;
5871 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5873 tbl->tbl[tbl->pos++] = id;
5875 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5879 vtable_pop_gen(struct parser_params *p, int line, const char *name,
5880 struct vtable *tbl, int n)
5883 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5884 line, name, (void *)tbl, n);
5887 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5892 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5896 vtable_included(const struct vtable * tbl, ID id)
5900 if (!DVARS_TERMINAL_P(tbl)) {
5901 for (i = 0; i < tbl->pos; i++) {
5902 if (tbl->tbl[i] == id) {
5910 static void parser_prepare(struct parser_params *p);
5913 static NODE *parser_append_options(struct parser_params *p, NODE *node);
5916 debug_lines(VALUE fname)
5919 CONST_ID(script_lines, "SCRIPT_LINES__");
5920 if (rb_const_defined_at(rb_cObject, script_lines)) {
5921 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5922 if (RB_TYPE_P(hash, T_HASH)) {
5923 VALUE lines = rb_ary_new();
5924 rb_hash_aset(hash, fname, lines);
5932 e_option_supplied(struct parser_params *p)
5934 return strcmp(p->ruby_sourcefile, "-e") == 0;
5938 yycompile0(VALUE arg)
5942 struct parser_params *p = (struct parser_params *)arg;
5945 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5946 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5947 if (p->debug_lines && p->ruby_sourceline > 0) {
5948 VALUE str = STR_NEW0();
5949 n = p->ruby_sourceline;
5951 rb_ary_push(p->debug_lines, str);
5955 if (!e_option_supplied(p)) {
5961 #define RUBY_DTRACE_PARSE_HOOK(name) \
5962 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5963 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5965 RUBY_DTRACE_PARSE_HOOK(BEGIN);
5967 RUBY_DTRACE_PARSE_HOOK(END);
5971 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
5972 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
5973 if (n || p->error_p) {
5974 VALUE mesg = p->error_buffer;
5976 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5978 rb_set_errinfo(mesg);
5981 tree = p->eval_tree;
5983 tree = NEW_NIL(&NULL_LOC);
5986 VALUE opt = p->compile_option;
5988 NODE *body = parser_append_options(p, tree->nd_body);
5989 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5990 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5991 prelude = block_append(p, p->eval_tree_begin, body);
5992 tree->nd_body = prelude;
5993 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
5995 p->ast->body.root = tree;
5996 p->ast->body.line_count = p->line_count;
6001 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6005 p->ruby_sourcefile_string = Qnil;
6006 p->ruby_sourcefile = "(none)";
6009 p->ruby_sourcefile_string = rb_fstring(fname);
6010 p->ruby_sourcefile = StringValueCStr(fname);
6012 p->ruby_sourceline = line - 1;
6014 p->ast = ast = rb_ast_new();
6015 rb_suppress_tracing(yycompile0, (VALUE)p);
6017 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6021 #endif /* !RIPPER */
6023 static rb_encoding *
6024 must_be_ascii_compatible(VALUE s)
6026 rb_encoding *enc = rb_enc_get(s);
6027 if (!rb_enc_asciicompat(enc)) {
6028 rb_raise(rb_eArgError, "invalid source encoding");
6034 lex_get_str(struct parser_params *p, VALUE s)
6036 char *beg, *end, *start;
6039 beg = RSTRING_PTR(s);
6040 len = RSTRING_LEN(s);
6042 if (p->lex.gets_.ptr) {
6043 if (len == p->lex.gets_.ptr) return Qnil;
6044 beg += p->lex.gets_.ptr;
6045 len -= p->lex.gets_.ptr;
6047 end = memchr(beg, '\n', len);
6048 if (end) len = ++end - beg;
6049 p->lex.gets_.ptr += len;
6050 return rb_str_subseq(s, beg - start, len);
6054 lex_getline(struct parser_params *p)
6056 VALUE line = (*p->lex.gets)(p, p->lex.input);
6057 if (NIL_P(line)) return line;
6058 must_be_ascii_compatible(line);
6060 if (p->debug_lines) {
6061 rb_enc_associate(line, p->enc);
6062 rb_ary_push(p->debug_lines, line);
6069 static const rb_data_type_t parser_data_type;
6073 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6075 struct parser_params *p;
6077 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6079 p->lex.gets = lex_get_str;
6080 p->lex.gets_.ptr = 0;
6081 p->lex.input = rb_str_new_frozen(s);
6082 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6084 return yycompile(vparser, p, fname, line);
6088 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6090 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6094 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6096 must_be_ascii_compatible(s);
6097 return parser_compile_string(vparser, f, s, line);
6100 VALUE rb_io_gets_internal(VALUE io);
6103 lex_io_gets(struct parser_params *p, VALUE io)
6105 return rb_io_gets_internal(io);
6109 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6111 struct parser_params *p;
6113 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6115 p->lex.gets = lex_io_gets;
6116 p->lex.input = file;
6117 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6119 return yycompile(vparser, p, fname, start);
6123 lex_generic_gets(struct parser_params *p, VALUE input)
6125 return (*p->lex.gets_.call)(input, p->line_count);
6129 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6131 struct parser_params *p;
6133 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6135 p->lex.gets = lex_generic_gets;
6136 p->lex.gets_.call = lex_gets;
6137 p->lex.input = input;
6138 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6140 return yycompile(vparser, p, fname, start);
6142 #endif /* !RIPPER */
6144 #define STR_FUNC_ESCAPE 0x01
6145 #define STR_FUNC_EXPAND 0x02
6146 #define STR_FUNC_REGEXP 0x04
6147 #define STR_FUNC_QWORDS 0x08
6148 #define STR_FUNC_SYMBOL 0x10
6149 #define STR_FUNC_INDENT 0x20
6150 #define STR_FUNC_LABEL 0x40
6151 #define STR_FUNC_LIST 0x4000
6152 #define STR_FUNC_TERM 0x8000
6155 str_label = STR_FUNC_LABEL,
6157 str_dquote = (STR_FUNC_EXPAND),
6158 str_xquote = (STR_FUNC_EXPAND),
6159 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6160 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6161 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6162 str_ssym = (STR_FUNC_SYMBOL),
6163 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6167 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6171 str = rb_enc_str_new(ptr, len, enc);
6172 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6173 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6175 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6176 rb_enc_associate(str, rb_ascii8bit_encoding());
6183 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6184 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6185 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6186 #define peek(p,c) peek_n(p, (c), 0)
6187 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6188 #define peekc(p) peekc_n(p, 0)
6189 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6193 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6196 if (!has_delayed_token(p)) {
6197 p->delayed.token = rb_str_buf_new(end - tok);
6198 rb_enc_associate(p->delayed.token, p->enc);
6199 p->delayed.line = p->ruby_sourceline;
6200 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6202 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6207 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6211 nextline(struct parser_params *p)
6213 VALUE v = p->lex.nextline;
6214 p->lex.nextline = 0;
6219 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6223 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6231 else if (NIL_P(v)) {
6232 /* after here-document without terminator */
6235 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6236 if (p->heredoc_end > 0) {
6237 p->ruby_sourceline = p->heredoc_end;
6240 p->ruby_sourceline++;
6241 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6242 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6244 p->lex.prevline = p->lex.lastline;
6245 p->lex.lastline = v;
6250 parser_cr(struct parser_params *p, int c)
6252 if (peek(p, '\n')) {
6256 else if (!p->cr_seen) {
6258 /* carried over with p->lex.nextline for nextc() */
6259 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6265 nextc(struct parser_params *p)
6269 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6270 if (nextline(p)) return -1;
6272 c = (unsigned char)*p->lex.pcur++;
6273 if (UNLIKELY(c == '\r')) {
6274 c = parser_cr(p, c);
6281 pushback(struct parser_params *p, int c)
6283 if (c == -1) return;
6285 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6290 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6292 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6293 #define tok(p) (p)->tokenbuf
6294 #define toklen(p) (p)->tokidx
6297 looking_at_eol_p(struct parser_params *p)
6299 const char *ptr = p->lex.pcur;
6300 while (ptr < p->lex.pend) {
6301 int c = (unsigned char)*ptr++;
6302 int eol = (c == '\n' || c == '#');
6303 if (eol || !ISSPACE(c)) {
6311 newtok(struct parser_params *p)
6314 p->tokline = p->ruby_sourceline;
6317 p->tokenbuf = ALLOC_N(char, 60);
6319 if (p->toksiz > 4096) {
6321 REALLOC_N(p->tokenbuf, char, 60);
6327 tokspace(struct parser_params *p, int n)
6331 if (p->tokidx >= p->toksiz) {
6332 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6333 REALLOC_N(p->tokenbuf, char, p->toksiz);
6335 return &p->tokenbuf[p->tokidx-n];
6339 tokadd(struct parser_params *p, int c)
6341 p->tokenbuf[p->tokidx++] = (char)c;
6342 if (p->tokidx >= p->toksiz) {
6344 REALLOC_N(p->tokenbuf, char, p->toksiz);
6349 tok_hex(struct parser_params *p, size_t *numlen)
6353 c = scan_hex(p->lex.pcur, 2, numlen);
6355 yyerror0("invalid hex escape");
6359 p->lex.pcur += *numlen;
6363 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6366 escaped_control_code(int c)
6392 #define WARN_SPACE_CHAR(c, prefix) \
6393 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6396 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6397 int regexp_literal, int wide)
6400 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6401 literal_flush(p, p->lex.pcur);
6402 p->lex.pcur += numlen;
6403 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6404 yyerror0("invalid Unicode escape");
6405 return wide && numlen > 0;
6407 if (codepoint > 0x10ffff) {
6408 yyerror0("invalid Unicode codepoint (too large)");
6411 if ((codepoint & 0xfffff800) == 0xd800) {
6412 yyerror0("invalid Unicode codepoint");
6415 if (regexp_literal) {
6416 tokcopy(p, (int)numlen);
6418 else if (codepoint >= 0x80) {
6419 rb_encoding *utf8 = rb_utf8_encoding();
6420 if (*encp && utf8 != *encp) {
6421 YYLTYPE loc = RUBY_INIT_YYLLOC();
6422 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6423 parser_show_error_line(p, &loc);
6427 tokaddmbc(p, codepoint, *encp);
6430 tokadd(p, codepoint);
6435 /* return value is for ?\u3042 */
6437 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6438 int term, int symbol_literal, int regexp_literal)
6441 * If `term` is not -1, then we allow multiple codepoints in \u{}
6442 * upto `term` byte, otherwise we're parsing a character literal.
6443 * And then add the codepoints to the current token.
6445 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6447 const int open_brace = '{', close_brace = '}';
6449 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6451 if (peek(p, open_brace)) { /* handle \u{...} form */
6452 const char *second = NULL;
6453 int c, last = nextc(p);
6454 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6455 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6456 while (c != close_brace) {
6457 if (c == term) goto unterminated;
6458 if (second == multiple_codepoints)
6459 second = p->lex.pcur;
6460 if (regexp_literal) tokadd(p, last);
6461 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6464 while (ISSPACE(c = *p->lex.pcur)) {
6465 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6468 if (term == -1 && !second)
6469 second = multiple_codepoints;
6472 if (c != close_brace) {
6475 yyerror0("unterminated Unicode escape");
6478 if (second && second != multiple_codepoints) {
6479 const char *pcur = p->lex.pcur;
6480 p->lex.pcur = second;
6481 dispatch_scan_event(p, tSTRING_CONTENT);
6484 yyerror0(multiple_codepoints);
6488 if (regexp_literal) tokadd(p, close_brace);
6491 else { /* handle \uxxxx form */
6492 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6499 #define ESCAPE_CONTROL 1
6500 #define ESCAPE_META 2
6503 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6508 switch (c = nextc(p)) {
6509 case '\\': /* Backslash */
6512 case 'n': /* newline */
6515 case 't': /* horizontal tab */
6518 case 'r': /* carriage-return */
6521 case 'f': /* form-feed */
6524 case 'v': /* vertical tab */
6527 case 'a': /* alarm(bell) */
6530 case 'e': /* escape */
6533 case '0': case '1': case '2': case '3': /* octal constant */
6534 case '4': case '5': case '6': case '7':
6536 c = scan_oct(p->lex.pcur, 3, &numlen);
6537 p->lex.pcur += numlen;
6540 case 'x': /* hex constant */
6541 c = tok_hex(p, &numlen);
6542 if (numlen == 0) return 0;
6545 case 'b': /* backspace */
6548 case 's': /* space */
6552 if (flags & ESCAPE_META) goto eof;
6553 if ((c = nextc(p)) != '-') {
6556 if ((c = nextc(p)) == '\\') {
6557 if (peek(p, 'u')) goto eof;
6558 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6560 else if (c == -1 || !ISASCII(c)) goto eof;
6562 int c2 = escaped_control_code(c);
6564 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6565 WARN_SPACE_CHAR(c2, "\\M-");
6568 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6571 else if (ISCNTRL(c)) goto eof;
6572 return ((c & 0xff) | 0x80);
6576 if ((c = nextc(p)) != '-') {
6580 if (flags & ESCAPE_CONTROL) goto eof;
6581 if ((c = nextc(p))== '\\') {
6582 if (peek(p, 'u')) goto eof;
6583 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6587 else if (c == -1 || !ISASCII(c)) goto eof;
6589 int c2 = escaped_control_code(c);
6592 if (flags & ESCAPE_META) {
6593 WARN_SPACE_CHAR(c2, "\\M-");
6596 WARN_SPACE_CHAR(c2, "");
6600 if (flags & ESCAPE_META) {
6601 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6604 WARN_SPACE_CHAR(c2, "\\C-");
6608 else if (ISCNTRL(c)) goto eof;
6614 yyerror0("Invalid escape character syntax");
6624 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6626 int len = rb_enc_codelen(c, enc);
6627 rb_enc_mbcput(c, tokspace(p, len), enc);
6631 tokadd_escape(struct parser_params *p, rb_encoding **encp)
6638 switch (c = nextc(p)) {
6640 return 0; /* just ignore */
6642 case '0': case '1': case '2': case '3': /* octal constant */
6643 case '4': case '5': case '6': case '7':
6645 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6646 if (numlen == 0) goto eof;
6647 p->lex.pcur += numlen;
6648 tokcopy(p, (int)numlen + 1);
6652 case 'x': /* hex constant */
6654 tok_hex(p, &numlen);
6655 if (numlen == 0) return -1;
6656 tokcopy(p, (int)numlen + 2);
6661 if (flags & ESCAPE_META) goto eof;
6662 if ((c = nextc(p)) != '-') {
6667 flags |= ESCAPE_META;
6671 if (flags & ESCAPE_CONTROL) goto eof;
6672 if ((c = nextc(p)) != '-') {
6680 if (flags & ESCAPE_CONTROL) goto eof;
6682 flags |= ESCAPE_CONTROL;
6684 if ((c = nextc(p)) == '\\') {
6687 else if (c == -1) goto eof;
6693 yyerror0("Invalid escape character syntax");
6705 regx_options(struct parser_params *p)
6713 while (c = nextc(p), ISALPHA(c)) {
6715 options |= RE_OPTION_ONCE;
6717 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6719 if (kc != rb_ascii8bit_encindex()) kcode = c;
6733 YYLTYPE loc = RUBY_INIT_YYLLOC();
6735 compile_error(p, "unknown regexp option%s - %*s",
6736 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6737 parser_show_error_line(p, &loc);
6739 return options | RE_OPTION_ENCODING(kcode);
6743 tokadd_mbchar(struct parser_params *p, int c)
6745 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6746 if (len < 0) return -1;
6748 p->lex.pcur += --len;
6749 if (len > 0) tokcopy(p, len);
6754 simple_re_meta(int c)
6757 case '$': case '*': case '+': case '.':
6758 case '?': case '^': case '|':
6759 case ')': case ']': case '}': case '>':
6767 parser_update_heredoc_indent(struct parser_params *p, int c)
6769 if (p->heredoc_line_indent == -1) {
6770 if (c == '\n') p->heredoc_line_indent = 0;
6774 p->heredoc_line_indent++;
6777 else if (c == '\t') {
6778 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6779 p->heredoc_line_indent = w * TAB_WIDTH;
6782 else if (c != '\n') {
6783 if (p->heredoc_indent > p->heredoc_line_indent) {
6784 p->heredoc_indent = p->heredoc_line_indent;
6786 p->heredoc_line_indent = -1;
6793 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6795 YYLTYPE loc = RUBY_INIT_YYLLOC();
6796 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6797 compile_error(p, "%s mixed within %s source", n1, n2);
6798 parser_show_error_line(p, &loc);
6802 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6804 const char *pos = p->lex.pcur;
6806 parser_mixed_error(p, enc1, enc2);
6811 tokadd_string(struct parser_params *p,
6812 int func, int term, int paren, long *nest,
6813 rb_encoding **encp, rb_encoding **enc)
6818 #define mixed_error(enc1, enc2) \
6819 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6820 #define mixed_escape(beg, enc1, enc2) \
6821 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6823 while ((c = nextc(p)) != -1) {
6824 if (p->heredoc_indent > 0) {
6825 parser_update_heredoc_indent(p, c);
6828 if (paren && c == paren) {
6831 else if (c == term) {
6832 if (!nest || !*nest) {
6838 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6839 int c2 = *p->lex.pcur;
6840 if (c2 == '$' || c2 == '@' || c2 == '{') {
6845 else if (c == '\\') {
6846 literal_flush(p, p->lex.pcur - 1);
6850 if (func & STR_FUNC_QWORDS) break;
6851 if (func & STR_FUNC_EXPAND) {
6852 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6863 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6867 if ((func & STR_FUNC_EXPAND) == 0) {
6871 tokadd_utf8(p, enc, term,
6872 func & STR_FUNC_SYMBOL,
6873 func & STR_FUNC_REGEXP);
6877 if (c == -1) return -1;
6879 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6882 if (func & STR_FUNC_REGEXP) {
6883 if (c == term && !simple_re_meta(c)) {
6888 if ((c = tokadd_escape(p, enc)) < 0)
6890 if (*enc && *enc != *encp) {
6891 mixed_escape(p->lex.ptok+2, *enc, *encp);
6895 else if (func & STR_FUNC_EXPAND) {
6897 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6898 c = read_escape(p, 0, enc);
6900 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6901 /* ignore backslashed spaces in %w */
6903 else if (c != term && !(paren && c == paren)) {
6910 else if (!parser_isascii(p)) {
6915 else if (*enc != *encp) {
6916 mixed_error(*enc, *encp);
6919 if (tokadd_mbchar(p, c) == -1) return -1;
6922 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6930 else if (*enc != *encp) {
6931 mixed_error(*enc, *encp);
6938 if (*enc) *encp = *enc;
6942 static inline rb_strterm_t *
6943 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6945 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6948 /* imemo_parser_strterm for literal */
6949 #define NEW_STRTERM(func, term, paren) \
6950 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6954 flush_string_content(struct parser_params *p, rb_encoding *enc)
6956 VALUE content = yylval.val;
6957 if (!ripper_is_node_yylval(content))
6958 content = ripper_new_yylval(p, 0, 0, content);
6959 if (has_delayed_token(p)) {
6960 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6962 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6964 dispatch_delayed_token(p, tSTRING_CONTENT);
6965 p->lex.ptok = p->lex.pcur;
6966 RNODE(content)->nd_rval = yylval.val;
6968 dispatch_scan_event(p, tSTRING_CONTENT);
6969 if (yylval.val != content)
6970 RNODE(content)->nd_rval = yylval.val;
6971 yylval.val = content;
6974 #define flush_string_content(p, enc) ((void)(enc))
6977 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6978 /* this can be shared with ripper, since it's independent from struct
6981 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6982 #define SPECIAL_PUNCT(idx) ( \
6983 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6984 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6985 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6986 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6987 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6989 const unsigned int ruby_global_name_punct_bits[] = {
6995 #undef SPECIAL_PUNCT
6998 static enum yytokentype
6999 parser_peek_variable_name(struct parser_params *p)
7002 const char *ptr = p->lex.pcur;
7004 if (ptr + 1 >= p->lex.pend) return 0;
7008 if ((c = *ptr) == '-') {
7009 if (++ptr >= p->lex.pend) return 0;
7012 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7013 return tSTRING_DVAR;
7017 if ((c = *ptr) == '@') {
7018 if (++ptr >= p->lex.pend) return 0;
7024 p->command_start = TRUE;
7025 return tSTRING_DBEG;
7029 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7030 return tSTRING_DVAR;
7034 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7035 #define IS_END() IS_lex_state(EXPR_END_ANY)
7036 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7037 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7038 #define IS_LABEL_POSSIBLE() (\
7039 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7041 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7042 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7044 static inline enum yytokentype
7045 parser_string_term(struct parser_params *p, int func)
7048 if (func & STR_FUNC_REGEXP) {
7049 set_yylval_num(regx_options(p));
7050 dispatch_scan_event(p, tREGEXP_END);
7051 SET_LEX_STATE(EXPR_END);
7054 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7056 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7059 SET_LEX_STATE(EXPR_END);
7063 static enum yytokentype
7064 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7066 int func = (int)quote->u1.func;
7067 int term = (int)quote->u3.term;
7068 int paren = (int)quote->u2.paren;
7070 rb_encoding *enc = p->enc;
7071 rb_encoding *base_enc = 0;
7074 if (func & STR_FUNC_TERM) {
7075 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7076 SET_LEX_STATE(EXPR_END);
7078 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7081 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7082 do {c = nextc(p);} while (ISSPACE(c));
7085 if (func & STR_FUNC_LIST) {
7086 quote->u1.func &= ~STR_FUNC_LIST;
7089 if (c == term && !quote->u0.nest) {
7090 if (func & STR_FUNC_QWORDS) {
7091 quote->u1.func |= STR_FUNC_TERM;
7092 pushback(p, c); /* dispatch the term at tSTRING_END */
7093 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7096 return parser_string_term(p, func);
7100 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7104 if ((func & STR_FUNC_EXPAND) && c == '#') {
7105 int t = parser_peek_variable_name(p);
7111 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7112 &enc, &base_enc) == -1) {
7115 # define unterminated_literal(mesg) yyerror0(mesg)
7117 # define unterminated_literal(mesg) compile_error(p, mesg)
7119 literal_flush(p, p->lex.pcur);
7120 if (func & STR_FUNC_QWORDS) {
7121 /* no content to add, bailing out here */
7122 unterminated_literal("unterminated list meets end of file");
7126 if (func & STR_FUNC_REGEXP) {
7127 unterminated_literal("unterminated regexp meets end of file");
7130 unterminated_literal("unterminated string meets end of file");
7132 quote->u1.func |= STR_FUNC_TERM;
7137 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7138 set_yylval_str(lit);
7139 flush_string_content(p, enc);
7141 return tSTRING_CONTENT;
7144 static enum yytokentype
7145 heredoc_identifier(struct parser_params *p)
7148 * term_len is length of `<<"END"` except `END`,
7149 * in this case term_len is 4 (<, <, " and ").
7151 long len, offset = p->lex.pcur - p->lex.pbeg;
7152 int c = nextc(p), term, func = 0, quote = 0;
7153 enum yytokentype token = tSTRING_BEG;
7158 func = STR_FUNC_INDENT;
7161 else if (c == '~') {
7163 func = STR_FUNC_INDENT;
7169 func |= str_squote; goto quoted;
7171 func |= str_dquote; goto quoted;
7173 token = tXSTRING_BEG;
7174 func |= str_xquote; goto quoted;
7181 while ((c = nextc(p)) != term) {
7182 if (c == -1 || c == '\r' || c == '\n') {
7183 yyerror(NULL, p, "unterminated here document identifier");
7190 if (!parser_is_identchar(p)) {
7192 if (func & STR_FUNC_INDENT) {
7193 pushback(p, indent > 0 ? '~' : '-');
7199 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7200 if (n < 0) return 0;
7202 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7207 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7208 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7209 yyerror(NULL, p, "too long here document identifier");
7210 dispatch_scan_event(p, tHEREDOC_BEG);
7213 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7214 p->lex.strterm->flags |= STRTERM_HEREDOC;
7215 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7216 here->offset = offset;
7217 here->sourceline = p->ruby_sourceline;
7218 here->length = (int)len;
7219 here->quote = quote;
7223 p->heredoc_indent = indent;
7224 p->heredoc_line_indent = 0;
7229 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7234 line = here->lastline;
7235 p->lex.lastline = line;
7236 p->lex.pbeg = RSTRING_PTR(line);
7237 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7238 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7239 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7240 p->heredoc_end = p->ruby_sourceline;
7241 p->ruby_sourceline = (int)here->sourceline;
7242 if (p->eofp) p->lex.nextline = Qnil;
7247 dedent_string(VALUE string, int width)
7253 RSTRING_GETMEM(string, str, len);
7254 for (i = 0; i < len && col < width; i++) {
7255 if (str[i] == ' ') {
7258 else if (str[i] == '\t') {
7259 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7260 if (n > width) break;
7268 rb_str_modify(string);
7269 str = RSTRING_PTR(string);
7270 if (RSTRING_LEN(string) != len)
7271 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7272 MEMMOVE(str, str + i, char, len - i);
7273 rb_str_set_len(string, len - i);
7279 heredoc_dedent(struct parser_params *p, NODE *root)
7281 NODE *node, *str_node, *prev_node;
7282 int indent = p->heredoc_indent;
7285 if (indent <= 0) return root;
7286 p->heredoc_indent = 0;
7287 if (!root) return root;
7289 prev_node = node = str_node = root;
7290 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7293 VALUE lit = str_node->nd_lit;
7294 if (str_node->flags & NODE_FL_NEWLINE) {
7295 dedent_string(lit, indent);
7300 else if (!literal_concat0(p, prev_lit, lit)) {
7304 NODE *end = node->nd_end;
7305 node = prev_node->nd_next = node->nd_next;
7307 if (nd_type(prev_node) == NODE_DSTR)
7308 nd_set_type(prev_node, NODE_STR);
7316 while ((node = (prev_node = node)->nd_next) != 0) {
7318 if (nd_type(node) != NODE_LIST) break;
7319 if ((str_node = node->nd_head) != 0) {
7320 enum node_type type = nd_type(str_node);
7321 if (type == NODE_STR || type == NODE_DSTR) break;
7331 heredoc_dedent(struct parser_params *p, VALUE array)
7333 int indent = p->heredoc_indent;
7335 if (indent <= 0) return array;
7336 p->heredoc_indent = 0;
7337 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7343 * Ripper.dedent_string(input, width) -> Integer
7345 * USE OF RIPPER LIBRARY ONLY.
7347 * Strips up to +width+ leading whitespaces from +input+,
7348 * and returns the stripped column width.
7351 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7356 wid = NUM2UINT(width);
7357 col = dedent_string(input, wid);
7358 return INT2NUM(col);
7363 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7365 const char *ptr = p->lex.pbeg;
7369 while (*ptr && ISSPACE(*ptr)) ptr++;
7371 n = p->lex.pend - (ptr + len);
7372 if (n < 0) return FALSE;
7373 if (n > 0 && ptr[len] != '\n') {
7374 if (ptr[len] != '\r') return FALSE;
7375 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7377 return strncmp(eos, ptr, len) == 0;
7381 word_match_p(struct parser_params *p, const char *word, long len)
7383 if (strncmp(p->lex.pcur, word, len)) return 0;
7384 if (p->lex.pcur + len == p->lex.pend) return 1;
7385 int c = (unsigned char)p->lex.pcur[len];
7386 if (ISSPACE(c)) return 1;
7388 case '\0': case '\004': case '\032': return 1;
7393 #define NUM_SUFFIX_R (1<<0)
7394 #define NUM_SUFFIX_I (1<<1)
7395 #define NUM_SUFFIX_ALL 3
7398 number_literal_suffix(struct parser_params *p, int mask)
7401 const char *lastp = p->lex.pcur;
7403 while ((c = nextc(p)) != -1) {
7404 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7405 result |= (mask & NUM_SUFFIX_I);
7406 mask &= ~NUM_SUFFIX_I;
7407 /* r after i, rational of complex is disallowed */
7408 mask &= ~NUM_SUFFIX_R;
7411 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7412 result |= (mask & NUM_SUFFIX_R);
7413 mask &= ~NUM_SUFFIX_R;
7416 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7417 p->lex.pcur = lastp;
7418 literal_flush(p, p->lex.pcur);
7427 static enum yytokentype
7428 set_number_literal(struct parser_params *p, VALUE v,
7429 enum yytokentype type, int suffix)
7431 if (suffix & NUM_SUFFIX_I) {
7432 v = rb_complex_raw(INT2FIX(0), v);
7435 set_yylval_literal(v);
7436 SET_LEX_STATE(EXPR_END);
7440 static enum yytokentype
7441 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7443 enum yytokentype type = tINTEGER;
7444 if (suffix & NUM_SUFFIX_R) {
7445 v = rb_rational_raw1(v);
7448 return set_number_literal(p, v, type, suffix);
7453 dispatch_heredoc_end(struct parser_params *p)
7456 if (has_delayed_token(p))
7457 dispatch_delayed_token(p, tSTRING_CONTENT);
7458 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7459 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7465 #define dispatch_heredoc_end(p) ((void)0)
7468 static enum yytokentype
7469 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7471 int c, func, indent = 0;
7472 const char *eos, *ptr, *ptr_end;
7475 rb_encoding *enc = p->enc;
7476 rb_encoding *base_enc = 0;
7479 eos = RSTRING_PTR(here->lastline) + here->offset;
7481 indent = (func = here->func) & STR_FUNC_INDENT;
7483 if ((c = nextc(p)) == -1) {
7486 if (!has_delayed_token(p)) {
7487 dispatch_scan_event(p, tSTRING_CONTENT);
7490 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7491 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7492 int cr = ENC_CODERANGE_UNKNOWN;
7493 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7494 if (cr != ENC_CODERANGE_7BIT &&
7495 p->enc == rb_usascii_encoding() &&
7496 enc != rb_utf8_encoding()) {
7497 enc = rb_ascii8bit_encoding();
7500 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7502 dispatch_delayed_token(p, tSTRING_CONTENT);
7506 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7507 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7511 SET_LEX_STATE(EXPR_END);
7516 /* not beginning of line, cannot be the terminator */
7518 else if (p->heredoc_line_indent == -1) {
7519 /* `heredoc_line_indent == -1` means
7520 * - "after an interpolation in the same line", or
7521 * - "in a continuing line"
7523 p->heredoc_line_indent = 0;
7525 else if (whole_match_p(p, eos, len, indent)) {
7526 dispatch_heredoc_end(p);
7528 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7531 SET_LEX_STATE(EXPR_END);
7535 if (!(func & STR_FUNC_EXPAND)) {
7537 ptr = RSTRING_PTR(p->lex.lastline);
7538 ptr_end = p->lex.pend;
7539 if (ptr_end > ptr) {
7540 switch (ptr_end[-1]) {
7542 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7551 if (p->heredoc_indent > 0) {
7553 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7555 p->heredoc_line_indent = 0;
7559 rb_str_cat(str, ptr, ptr_end - ptr);
7561 str = STR_NEW(ptr, ptr_end - ptr);
7562 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7564 if (p->heredoc_indent > 0) {
7567 if (nextc(p) == -1) {
7573 } while (!whole_match_p(p, eos, len, indent));
7576 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7579 int t = parser_peek_variable_name(p);
7580 if (p->heredoc_line_indent != -1) {
7581 if (p->heredoc_indent > p->heredoc_line_indent) {
7582 p->heredoc_indent = p->heredoc_line_indent;
7584 p->heredoc_line_indent = -1;
7593 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7594 if (p->eofp) goto error;
7598 if (c == '\\') p->heredoc_line_indent = -1;
7600 str = STR_NEW3(tok(p), toklen(p), enc, func);
7602 set_yylval_str(str);
7604 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7606 flush_string_content(p, enc);
7607 return tSTRING_CONTENT;
7609 tokadd(p, nextc(p));
7610 if (p->heredoc_indent > 0) {
7614 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7615 if ((c = nextc(p)) == -1) goto error;
7616 } while (!whole_match_p(p, eos, len, indent));
7617 str = STR_NEW3(tok(p), toklen(p), enc, func);
7619 dispatch_heredoc_end(p);
7621 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7624 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7626 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7627 set_yylval_str(str);
7629 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7631 return tSTRING_CONTENT;
7637 arg_ambiguous(struct parser_params *p, char c)
7640 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7642 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7648 formal_argument(struct parser_params *p, ID lhs)
7650 switch (id_type(lhs)) {
7655 yyerror0("formal argument cannot be a constant");
7658 yyerror0("formal argument cannot be an instance variable");
7661 yyerror0("formal argument cannot be a global variable");
7664 yyerror0("formal argument cannot be a class variable");
7667 yyerror0("formal argument must be local variable");
7671 lhs = dispatch1(param_error, lhs);
7676 shadowing_lvar(p, lhs);
7681 lvar_defined(struct parser_params *p, ID id)
7683 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7686 /* emacsen -*- hack */
7688 parser_encode_length(struct parser_params *p, const char *name, long len)
7692 if (len > 5 && name[nlen = len - 5] == '-') {
7693 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7696 if (len > 4 && name[nlen = len - 4] == '-') {
7697 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7699 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7700 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7701 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7708 parser_set_encode(struct parser_params *p, const char *name)
7710 int idx = rb_enc_find_index(name);
7715 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7717 excargs[0] = rb_eArgError;
7718 excargs[2] = rb_make_backtrace();
7719 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7720 rb_exc_raise(rb_make_exception(3, excargs));
7722 enc = rb_enc_from_index(idx);
7723 if (!rb_enc_asciicompat(enc)) {
7724 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7729 if (p->debug_lines) {
7730 VALUE lines = p->debug_lines;
7731 long i, n = RARRAY_LEN(lines);
7732 for (i = 0; i < n; ++i) {
7733 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7740 comment_at_top(struct parser_params *p)
7742 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7743 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7744 while (ptr < ptr_end) {
7745 if (!ISSPACE(*ptr)) return 0;
7751 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7752 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7755 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7757 if (!comment_at_top(p)) {
7760 parser_set_encode(p, val);
7764 parser_get_bool(struct parser_params *p, const char *name, const char *val)
7768 if (strcasecmp(val, "true") == 0) {
7773 if (strcasecmp(val, "false") == 0) {
7778 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7783 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7785 int b = parser_get_bool(p, name, val);
7786 if (b >= 0) p->token_info_enabled = b;
7790 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7794 if (p->token_seen) {
7795 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7799 b = parser_get_bool(p, name, val);
7802 if (!p->compile_option)
7803 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7804 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7805 (b ? Qtrue : Qfalse));
7808 # if WARN_PAST_SCOPE
7810 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7812 int b = parser_get_bool(p, name, val);
7813 if (b >= 0) p->past_scope_enabled = b;
7817 struct magic_comment {
7819 rb_magic_comment_setter_t func;
7820 rb_magic_comment_length_t length;
7823 static const struct magic_comment magic_comments[] = {
7824 {"coding", magic_comment_encoding, parser_encode_length},
7825 {"encoding", magic_comment_encoding, parser_encode_length},
7826 {"frozen_string_literal", parser_set_compile_option_flag},
7827 {"warn_indent", parser_set_token_info},
7828 # if WARN_PAST_SCOPE
7829 {"warn_past_scope", parser_set_past_scope},
7834 magic_comment_marker(const char *str, long len)
7841 if (str[i-1] == '*' && str[i-2] == '-') {
7847 if (i + 1 >= len) return 0;
7848 if (str[i+1] != '-') {
7851 else if (str[i-1] != '-') {
7867 parser_magic_comment(struct parser_params *p, const char *str, long len)
7870 VALUE name = 0, val = 0;
7871 const char *beg, *end, *vbeg, *vend;
7872 #define str_copy(_s, _p, _n) ((_s) \
7873 ? (void)(rb_str_resize((_s), (_n)), \
7874 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7875 : (void)((_s) = STR_NEW((_p), (_n))))
7877 if (len <= 7) return FALSE;
7878 if (!!(beg = magic_comment_marker(str, len))) {
7879 if (!(end = magic_comment_marker(beg, str + len - beg)))
7883 len = end - beg - 3;
7886 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7888 const struct magic_comment *mc = magic_comments;
7893 for (; len > 0 && *str; str++, --len) {
7895 case '\'': case '"': case ':': case ';':
7898 if (!ISSPACE(*str)) break;
7900 for (beg = str; len > 0; str++, --len) {
7902 case '\'': case '"': case ':': case ';':
7905 if (ISSPACE(*str)) break;
7910 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7913 if (!indicator) return FALSE;
7917 do str++; while (--len > 0 && ISSPACE(*str));
7920 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7933 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7937 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7940 while (len > 0 && (ISSPACE(*str))) --len, str++;
7941 if (len) return FALSE;
7945 str_copy(name, beg, n);
7946 s = RSTRING_PTR(name);
7947 for (i = 0; i < n; ++i) {
7948 if (s[i] == '-') s[i] = '_';
7951 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7954 n = (*mc->length)(p, vbeg, n);
7956 str_copy(val, vbeg, n);
7957 (*mc->func)(p, mc->name, RSTRING_PTR(val));
7960 } while (++mc < magic_comments + numberof(magic_comments));
7962 str_copy(val, vbeg, vend - vbeg);
7963 dispatch2(magic_comment, name, val);
7971 set_file_encoding(struct parser_params *p, const char *str, const char *send)
7974 const char *beg = str;
7978 if (send - str <= 6) return;
7980 case 'C': case 'c': str += 6; continue;
7981 case 'O': case 'o': str += 5; continue;
7982 case 'D': case 'd': str += 4; continue;
7983 case 'I': case 'i': str += 3; continue;
7984 case 'N': case 'n': str += 2; continue;
7985 case 'G': case 'g': str += 1; continue;
7992 if (ISSPACE(*str)) break;
7995 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7999 if (++str >= send) return;
8000 } while (ISSPACE(*str));
8002 if (*str != '=' && *str != ':') return;
8007 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8008 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8009 parser_set_encode(p, RSTRING_PTR(s));
8010 rb_str_resize(s, 0);
8014 parser_prepare(struct parser_params *p)
8017 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8020 if (peek(p, '!')) p->has_shebang = 1;
8022 case 0xef: /* UTF-8 BOM marker */
8023 if (p->lex.pend - p->lex.pcur >= 2 &&
8024 (unsigned char)p->lex.pcur[0] == 0xbb &&
8025 (unsigned char)p->lex.pcur[1] == 0xbf) {
8026 p->enc = rb_utf8_encoding();
8028 p->lex.pbeg = p->lex.pcur;
8036 p->enc = rb_enc_get(p->lex.lastline);
8040 #define ambiguous_operator(tok, op, syn) ( \
8041 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8042 rb_warning0("even though it seems like "syn""))
8044 #define ambiguous_operator(tok, op, syn) \
8045 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8047 #define warn_balanced(tok, op, syn) ((void) \
8048 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8049 space_seen && !ISSPACE(c) && \
8050 (ambiguous_operator(tok, op, syn), 0)), \
8051 (enum yytokentype)(tok))
8054 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8057 char *point = &str[seen_point];
8058 size_t fraclen = len-seen_point-1;
8059 memmove(point, point+1, fraclen+1);
8060 v = rb_cstr_to_inum(str, 10, FALSE);
8061 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8064 static enum yytokentype
8065 no_digits(struct parser_params *p)
8067 yyerror0("numeric literal without digits");
8068 if (peek(p, '_')) nextc(p);
8069 /* dummy 0, for tUMINUS_NUM at numeric */
8070 return set_integer_literal(p, INT2FIX(0), 0);
8073 static enum yytokentype
8074 parse_numeric(struct parser_params *p, int c)
8076 int is_float, seen_point, seen_e, nondigit;
8079 is_float = seen_point = seen_e = nondigit = 0;
8080 SET_LEX_STATE(EXPR_END);
8082 if (c == '-' || c == '+') {
8087 int start = toklen(p);
8089 if (c == 'x' || c == 'X') {
8092 if (c != -1 && ISXDIGIT(c)) {
8095 if (nondigit) break;
8099 if (!ISXDIGIT(c)) break;
8102 } while ((c = nextc(p)) != -1);
8106 if (toklen(p) == start) {
8107 return no_digits(p);
8109 else if (nondigit) goto trailing_uc;
8110 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8111 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8113 if (c == 'b' || c == 'B') {
8116 if (c == '0' || c == '1') {
8119 if (nondigit) break;
8123 if (c != '0' && c != '1') break;
8126 } while ((c = nextc(p)) != -1);
8130 if (toklen(p) == start) {
8131 return no_digits(p);
8133 else if (nondigit) goto trailing_uc;
8134 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8135 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8137 if (c == 'd' || c == 'D') {
8140 if (c != -1 && ISDIGIT(c)) {
8143 if (nondigit) break;
8147 if (!ISDIGIT(c)) break;
8150 } while ((c = nextc(p)) != -1);
8154 if (toklen(p) == start) {
8155 return no_digits(p);
8157 else if (nondigit) goto trailing_uc;
8158 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8159 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8165 if (c == 'o' || c == 'O') {
8166 /* prefixed octal */
8168 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8169 return no_digits(p);
8172 if (c >= '0' && c <= '7') {
8177 if (nondigit) break;
8181 if (c < '0' || c > '9') break;
8182 if (c > '7') goto invalid_octal;
8185 } while ((c = nextc(p)) != -1);
8186 if (toklen(p) > start) {
8189 if (nondigit) goto trailing_uc;
8190 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8191 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8198 if (c > '7' && c <= '9') {
8200 yyerror0("Invalid octal digit");
8202 else if (c == '.' || c == 'e' || c == 'E') {
8207 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8208 return set_integer_literal(p, INT2FIX(0), suffix);
8214 case '0': case '1': case '2': case '3': case '4':
8215 case '5': case '6': case '7': case '8': case '9':
8221 if (nondigit) goto trailing_uc;
8222 if (seen_point || seen_e) {
8227 if (c0 == -1 || !ISDIGIT(c0)) {
8233 seen_point = toklen(p);
8252 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8257 tokadd(p, nondigit);
8261 nondigit = (c == '-' || c == '+') ? c : 0;
8264 case '_': /* `_' in number just ignored */
8265 if (nondigit) goto decode_num;
8279 literal_flush(p, p->lex.pcur - 1);
8280 YYLTYPE loc = RUBY_INIT_YYLLOC();
8281 compile_error(p, "trailing `%c' in number", nondigit);
8282 parser_show_error_line(p, &loc);
8286 enum yytokentype type = tFLOAT;
8289 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8290 if (suffix & NUM_SUFFIX_R) {
8292 v = parse_rational(p, tok(p), toklen(p), seen_point);
8295 double d = strtod(tok(p), 0);
8296 if (errno == ERANGE) {
8297 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8302 return set_number_literal(p, v, type, suffix);
8304 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8305 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8308 static enum yytokentype
8309 parse_qmark(struct parser_params *p, int space_seen)
8316 SET_LEX_STATE(EXPR_VALUE);
8321 compile_error(p, "incomplete character syntax");
8324 if (rb_enc_isspace(c, p->enc)) {
8326 int c2 = escaped_control_code(c);
8328 WARN_SPACE_CHAR(c2, "?");
8333 SET_LEX_STATE(EXPR_VALUE);
8338 if (!parser_isascii(p)) {
8339 if (tokadd_mbchar(p, c) == -1) return 0;
8341 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8342 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8344 const char *start = p->lex.pcur - 1, *ptr = start;
8346 int n = parser_precise_mbclen(p, ptr);
8347 if (n < 0) return -1;
8349 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8350 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8351 " a conditional operator, put a space after `?'",
8352 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8356 else if (c == '\\') {
8359 enc = rb_utf8_encoding();
8360 tokadd_utf8(p, &enc, -1, 0, 0);
8362 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8364 if (tokadd_mbchar(p, c) == -1) return 0;
8367 c = read_escape(p, 0, &enc);
8375 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8376 set_yylval_str(lit);
8377 SET_LEX_STATE(EXPR_END);
8381 static enum yytokentype
8382 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8385 const char *ptok = p->lex.pcur;
8393 if (c == -1 || !ISALNUM(c)) {
8399 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8400 yyerror0("unknown type of %string");
8404 if (c == -1 || term == -1) {
8405 compile_error(p, "unterminated quoted string meets end of file");
8409 if (term == '(') term = ')';
8410 else if (term == '[') term = ']';
8411 else if (term == '{') term = '}';
8412 else if (term == '<') term = '>';
8415 p->lex.ptok = ptok-1;
8418 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8422 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8426 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8430 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8434 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8435 return tSYMBOLS_BEG;
8438 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8439 return tQSYMBOLS_BEG;
8442 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8443 return tXSTRING_BEG;
8446 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8450 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8451 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8455 yyerror0("unknown type of %string");
8459 if ((c = nextc(p)) == '=') {
8461 SET_LEX_STATE(EXPR_BEG);
8464 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8467 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8469 return warn_balanced('%', "%%", "string literal");
8473 tokadd_ident(struct parser_params *p, int c)
8476 if (tokadd_mbchar(p, c) == -1) return -1;
8478 } while (parser_is_identchar(p));
8484 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8486 ID ident = TOK_INTERN();
8488 set_yylval_name(ident);
8494 parse_numvar(struct parser_params *p)
8498 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8499 const unsigned long nth_ref_max =
8500 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8501 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8502 * turned into a Fixnum, in compile.c */
8504 if (overflow || n > nth_ref_max) {
8505 /* compile_error()? */
8506 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8507 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8514 static enum yytokentype
8515 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8517 const char *ptr = p->lex.pcur;
8520 SET_LEX_STATE(EXPR_END);
8521 p->lex.ptok = ptr - 1; /* from '$' */
8525 case '_': /* $_: last read line string */
8527 if (parser_is_identchar(p)) {
8535 case '~': /* $~: match-data */
8536 case '*': /* $*: argv */
8537 case '$': /* $$: pid */
8538 case '?': /* $?: last status */
8539 case '!': /* $!: error string */
8540 case '@': /* $@: error position */
8541 case '/': /* $/: input record separator */
8542 case '\\': /* $\: output record separator */
8543 case ';': /* $;: field separator */
8544 case ',': /* $,: output field separator */
8545 case '.': /* $.: last read line number */
8546 case '=': /* $=: ignorecase */
8547 case ':': /* $:: load path */
8548 case '<': /* $<: reading filename */
8549 case '>': /* $>: default output handle */
8550 case '\"': /* $": already loaded files */
8559 if (parser_is_identchar(p)) {
8560 if (tokadd_mbchar(p, c) == -1) return 0;
8568 set_yylval_name(TOK_INTERN());
8571 case '&': /* $&: last match */
8572 case '`': /* $`: string before last match */
8573 case '\'': /* $': string after last match */
8574 case '+': /* $+: string matches last paren. */
8575 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8580 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8583 case '1': case '2': case '3':
8584 case '4': case '5': case '6':
8585 case '7': case '8': case '9':
8590 } while (c != -1 && ISDIGIT(c));
8592 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8594 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8598 if (!parser_is_identchar(p)) {
8599 YYLTYPE loc = RUBY_INIT_YYLLOC();
8600 if (c == -1 || ISSPACE(c)) {
8601 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8605 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8607 parser_show_error_line(p, &loc);
8608 set_yylval_noname();
8616 if (tokadd_ident(p, c)) return 0;
8617 SET_LEX_STATE(EXPR_END);
8618 tokenize_ident(p, last_state);
8624 parser_numbered_param(struct parser_params *p, int n)
8626 if (n < 0) return false;
8628 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8631 if (p->max_numparam == ORDINAL_PARAM) {
8632 compile_error(p, "ordinary parameter is defined");
8635 struct vtable *args = p->lvtbl->args;
8636 if (p->max_numparam < n) {
8637 p->max_numparam = n;
8639 while (n > args->pos) {
8640 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8646 static enum yytokentype
8647 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8649 const char *ptr = p->lex.pcur;
8650 enum yytokentype result = tIVAR;
8651 register int c = nextc(p);
8654 p->lex.ptok = ptr - 1; /* from '@' */
8662 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8663 if (c == -1 || !parser_is_identchar(p)) {
8665 RUBY_SET_YYLLOC(loc);
8666 if (result == tIVAR) {
8667 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8670 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8672 parser_show_error_line(p, &loc);
8673 set_yylval_noname();
8674 SET_LEX_STATE(EXPR_END);
8677 else if (ISDIGIT(c)) {
8679 RUBY_SET_YYLLOC(loc);
8680 if (result == tIVAR) {
8681 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8684 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8686 parser_show_error_line(p, &loc);
8687 set_yylval_noname();
8688 SET_LEX_STATE(EXPR_END);
8692 if (tokadd_ident(p, c)) return 0;
8693 tokenize_ident(p, last_state);
8697 static enum yytokentype
8698 parse_ident(struct parser_params *p, int c, int cmd_state)
8700 enum yytokentype result;
8701 int mb = ENC_CODERANGE_7BIT;
8702 const enum lex_state_e last_state = p->lex.state;
8706 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8707 if (tokadd_mbchar(p, c) == -1) return 0;
8709 } while (parser_is_identchar(p));
8710 if ((c == '!' || c == '?') && !peek(p, '=')) {
8714 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8715 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8716 result = tIDENTIFIER;
8720 result = tCONSTANT; /* assume provisionally */
8725 if (IS_LABEL_POSSIBLE()) {
8726 if (IS_LABEL_SUFFIX(0)) {
8727 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8729 set_yylval_name(TOK_INTERN());
8733 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8734 const struct kwtable *kw;
8736 /* See if it is a reserved word. */
8737 kw = rb_reserved_word(tok(p), toklen(p));
8739 enum lex_state_e state = p->lex.state;
8740 SET_LEX_STATE(kw->state);
8741 if (IS_lex_state_for(state, EXPR_FNAME)) {
8742 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8745 if (IS_lex_state(EXPR_BEG)) {
8746 p->command_start = TRUE;
8748 if (kw->id[0] == keyword_do) {
8749 if (lambda_beginning_p()) {
8750 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8751 return keyword_do_LAMBDA;
8753 if (COND_P()) return keyword_do_cond;
8754 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8755 return keyword_do_block;
8758 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8761 if (kw->id[0] != kw->id[1])
8762 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8768 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8770 SET_LEX_STATE(EXPR_CMDARG);
8773 SET_LEX_STATE(EXPR_ARG);
8776 else if (p->lex.state == EXPR_FNAME) {
8777 SET_LEX_STATE(EXPR_ENDFN);
8780 SET_LEX_STATE(EXPR_END);
8783 ident = tokenize_ident(p, last_state);
8784 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8785 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8786 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8787 lvar_defined(p, ident)) {
8788 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8793 static enum yytokentype
8794 parser_yylex(struct parser_params *p)
8800 enum lex_state_e last_state;
8801 int fallthru = FALSE;
8802 int token_seen = p->token_seen;
8804 if (p->lex.strterm) {
8805 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8806 return here_document(p, &p->lex.strterm->u.heredoc);
8810 return parse_string(p, &p->lex.strterm->u.literal);
8813 cmd_state = p->command_start;
8814 p->command_start = FALSE;
8815 p->token_seen = TRUE;
8817 last_state = p->lex.state;
8821 switch (c = nextc(p)) {
8822 case '\0': /* NUL */
8823 case '\004': /* ^D */
8824 case '\032': /* ^Z */
8825 case -1: /* end of script. */
8829 case ' ': case '\t': case '\f': case '\r':
8830 case '\13': /* '\v' */
8833 while ((c = nextc(p))) {
8835 case ' ': case '\t': case '\f': case '\r':
8836 case '\13': /* '\v' */
8844 dispatch_scan_event(p, tSP);
8848 case '#': /* it's a comment */
8849 p->token_seen = token_seen;
8850 /* no magic_comment in shebang line */
8851 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8852 if (comment_at_top(p)) {
8853 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8857 dispatch_scan_event(p, tCOMMENT);
8861 p->token_seen = token_seen;
8862 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8863 !IS_lex_state(EXPR_LABELED));
8864 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8866 dispatch_scan_event(p, tIGNORED_NL);
8869 if (!c && p->in_kwarg) {
8870 goto normal_newline;
8875 switch (c = nextc(p)) {
8876 case ' ': case '\t': case '\f': case '\r':
8877 case '\13': /* '\v' */
8882 if (space_seen) dispatch_scan_event(p, tSP);
8886 dispatch_delayed_token(p, tIGNORED_NL);
8887 if (peek(p, '.') == (c == '&')) {
8889 dispatch_scan_event(p, tSP);
8894 p->ruby_sourceline--;
8895 p->lex.nextline = p->lex.lastline;
8896 case -1: /* EOF no decrement*/
8898 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8899 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8900 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8901 pushback(p, 1); /* always pushback */
8902 p->lex.ptok = p->lex.pcur;
8906 p->lex.ptok = p->lex.pcur;
8909 goto normal_newline;
8913 p->command_start = TRUE;
8914 SET_LEX_STATE(EXPR_BEG);
8918 if ((c = nextc(p)) == '*') {
8919 if ((c = nextc(p)) == '=') {
8920 set_yylval_id(idPow);
8921 SET_LEX_STATE(EXPR_BEG);
8926 rb_warning0("`**' interpreted as argument prefix");
8929 else if (IS_BEG()) {
8933 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8939 SET_LEX_STATE(EXPR_BEG);
8944 rb_warning0("`*' interpreted as argument prefix");
8947 else if (IS_BEG()) {
8951 c = warn_balanced('*', "*", "argument prefix");
8954 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8959 if (IS_AFTER_OPERATOR()) {
8960 SET_LEX_STATE(EXPR_ARG);
8966 SET_LEX_STATE(EXPR_BEG);
8979 /* skip embedded rd document */
8980 if (word_match_p(p, "begin", 5)) {
8984 dispatch_scan_event(p, tEMBDOC_BEG);
8988 dispatch_scan_event(p, tEMBDOC);
8993 compile_error(p, "embedded document meets end of file");
8996 if (c == '=' && word_match_p(p, "end", 3)) {
9002 dispatch_scan_event(p, tEMBDOC_END);
9007 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9008 if ((c = nextc(p)) == '=') {
9009 if ((c = nextc(p)) == '=') {
9018 else if (c == '>') {
9027 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9029 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9030 int token = heredoc_identifier(p);
9031 if (token) return token < 0 ? 0 : token;
9033 if (IS_AFTER_OPERATOR()) {
9034 SET_LEX_STATE(EXPR_ARG);
9037 if (IS_lex_state(EXPR_CLASS))
9038 p->command_start = TRUE;
9039 SET_LEX_STATE(EXPR_BEG);
9042 if ((c = nextc(p)) == '>') {
9049 if ((c = nextc(p)) == '=') {
9050 set_yylval_id(idLTLT);
9051 SET_LEX_STATE(EXPR_BEG);
9055 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9061 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9062 if ((c = nextc(p)) == '=') {
9066 if ((c = nextc(p)) == '=') {
9067 set_yylval_id(idGTGT);
9068 SET_LEX_STATE(EXPR_BEG);
9078 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9079 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9080 p->lex.ptok = p->lex.pcur-1;
9084 if (IS_lex_state(EXPR_FNAME)) {
9085 SET_LEX_STATE(EXPR_ENDFN);
9088 if (IS_lex_state(EXPR_DOT)) {
9090 SET_LEX_STATE(EXPR_CMDARG);
9092 SET_LEX_STATE(EXPR_ARG);
9095 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9096 return tXSTRING_BEG;
9099 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9100 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9101 p->lex.ptok = p->lex.pcur-1;
9105 return parse_qmark(p, space_seen);
9108 if ((c = nextc(p)) == '&') {
9109 SET_LEX_STATE(EXPR_BEG);
9110 if ((c = nextc(p)) == '=') {
9111 set_yylval_id(idANDOP);
9112 SET_LEX_STATE(EXPR_BEG);
9118 else if (c == '=') {
9120 SET_LEX_STATE(EXPR_BEG);
9123 else if (c == '.') {
9124 set_yylval_id(idANDDOT);
9125 SET_LEX_STATE(EXPR_DOT);
9131 (c = peekc_n(p, 1)) == -1 ||
9132 !(c == '\'' || c == '"' ||
9133 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9134 rb_warning0("`&' interpreted as argument prefix");
9138 else if (IS_BEG()) {
9142 c = warn_balanced('&', "&", "argument prefix");
9144 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9148 if ((c = nextc(p)) == '|') {
9149 SET_LEX_STATE(EXPR_BEG);
9150 if ((c = nextc(p)) == '=') {
9151 set_yylval_id(idOROP);
9152 SET_LEX_STATE(EXPR_BEG);
9156 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9165 SET_LEX_STATE(EXPR_BEG);
9168 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9174 if (IS_AFTER_OPERATOR()) {
9175 SET_LEX_STATE(EXPR_ARG);
9184 SET_LEX_STATE(EXPR_BEG);
9187 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9188 SET_LEX_STATE(EXPR_BEG);
9190 if (c != -1 && ISDIGIT(c)) {
9191 return parse_numeric(p, '+');
9195 SET_LEX_STATE(EXPR_BEG);
9197 return warn_balanced('+', "+", "unary operator");
9201 if (IS_AFTER_OPERATOR()) {
9202 SET_LEX_STATE(EXPR_ARG);
9211 SET_LEX_STATE(EXPR_BEG);
9215 SET_LEX_STATE(EXPR_ENDFN);
9218 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9219 SET_LEX_STATE(EXPR_BEG);
9221 if (c != -1 && ISDIGIT(c)) {
9226 SET_LEX_STATE(EXPR_BEG);
9228 return warn_balanced('-', "-", "unary operator");
9231 int is_beg = IS_BEG();
9232 SET_LEX_STATE(EXPR_BEG);
9233 if ((c = nextc(p)) == '.') {
9234 if ((c = nextc(p)) == '.') {
9235 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9236 rb_warn0("... at EOL, should be parenthesized?");
9238 return is_beg ? tBDOT3 : tDOT3;
9241 return is_beg ? tBDOT2 : tDOT2;
9244 if (c != -1 && ISDIGIT(c)) {
9245 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9246 parse_numeric(p, '.');
9247 if (ISDIGIT(prev)) {
9248 yyerror0("unexpected fraction part after numeric literal");
9251 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9253 SET_LEX_STATE(EXPR_END);
9254 p->lex.ptok = p->lex.pcur;
9258 SET_LEX_STATE(EXPR_DOT);
9262 case '0': case '1': case '2': case '3': case '4':
9263 case '5': case '6': case '7': case '8': case '9':
9264 return parse_numeric(p, c);
9269 SET_LEX_STATE(EXPR_ENDFN);
9270 p->lex.paren_nest--;
9276 SET_LEX_STATE(EXPR_END);
9277 p->lex.paren_nest--;
9281 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9282 if (!p->lex.brace_nest--) return tSTRING_DEND;
9285 SET_LEX_STATE(EXPR_END);
9286 p->lex.paren_nest--;
9292 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9293 SET_LEX_STATE(EXPR_BEG);
9296 set_yylval_id(idCOLON2);
9297 SET_LEX_STATE(EXPR_DOT);
9300 if (IS_END() || ISSPACE(c) || c == '#') {
9302 c = warn_balanced(':', ":", "symbol literal");
9303 SET_LEX_STATE(EXPR_BEG);
9308 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9311 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9317 SET_LEX_STATE(EXPR_FNAME);
9322 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9325 if ((c = nextc(p)) == '=') {
9327 SET_LEX_STATE(EXPR_BEG);
9332 arg_ambiguous(p, '/');
9333 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9336 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9337 return warn_balanced('/', "/", "regexp literal");
9340 if ((c = nextc(p)) == '=') {
9342 SET_LEX_STATE(EXPR_BEG);
9345 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9350 SET_LEX_STATE(EXPR_BEG);
9351 p->command_start = TRUE;
9355 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9359 if (IS_AFTER_OPERATOR()) {
9360 if ((c = nextc(p)) != '@') {
9363 SET_LEX_STATE(EXPR_ARG);
9366 SET_LEX_STATE(EXPR_BEG);
9374 else if (!space_seen) {
9375 /* foo( ... ) => method call, no ambiguity */
9377 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9380 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9381 rb_warning0("parentheses after method name is interpreted as "
9382 "an argument list, not a decomposed argument");
9384 p->lex.paren_nest++;
9387 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9391 p->lex.paren_nest++;
9392 if (IS_AFTER_OPERATOR()) {
9393 if ((c = nextc(p)) == ']') {
9394 SET_LEX_STATE(EXPR_ARG);
9395 if ((c = nextc(p)) == '=') {
9402 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9405 else if (IS_BEG()) {
9408 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9411 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9417 ++p->lex.brace_nest;
9418 if (lambda_beginning_p())
9420 else if (IS_lex_state(EXPR_LABELED))
9421 c = tLBRACE; /* hash */
9422 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9423 c = '{'; /* block (primary) */
9424 else if (IS_lex_state(EXPR_ENDARG))
9425 c = tLBRACE_ARG; /* block (expr) */
9427 c = tLBRACE; /* hash */
9429 p->command_start = TRUE;
9430 SET_LEX_STATE(EXPR_BEG);
9433 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9435 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9444 dispatch_scan_event(p, tSP);
9445 goto retry; /* skip \\n */
9447 if (c == ' ') return tSP;
9448 if (ISSPACE(c)) return c;
9453 return parse_percent(p, space_seen, last_state);
9456 return parse_gvar(p, last_state);
9459 return parse_atmark(p, last_state);
9462 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9463 p->ruby__end__seen = 1;
9469 dispatch_scan_event(p, k__END__);
9477 if (!parser_is_identchar(p)) {
9478 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9487 return parse_ident(p, c, cmd_state);
9490 static enum yytokentype
9491 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9497 t = parser_yylex(p);
9498 if (has_delayed_token(p))
9499 dispatch_delayed_token(p, t);
9501 dispatch_scan_event(p, t);
9503 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9504 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9506 RUBY_SET_YYLLOC(*yylloc);
9511 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9514 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9516 NODE *n = rb_ast_newnode(p->ast, type);
9518 rb_node_init(n, type, a0, a1, a2);
9521 nd_set_node_id(n, parser_get_node_id(p));
9526 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9529 nd_set_line(nd, loc->beg_pos.lineno);
9534 static enum node_type
9535 nodetype(NODE *node) /* for debug */
9537 return (enum node_type)nd_type(node);
9541 nodeline(NODE *node)
9543 return nd_line(node);
9547 newline_node(NODE *node)
9550 node = remove_begin(node);
9551 node->flags |= NODE_FL_NEWLINE;
9557 fixpos(NODE *node, NODE *orig)
9561 nd_set_line(node, nd_line(orig));
9565 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9567 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9571 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9573 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9577 block_append(struct parser_params *p, NODE *head, NODE *tail)
9579 NODE *end, *h = head, *nd;
9581 if (tail == 0) return head;
9583 if (h == 0) return tail;
9584 switch (nd_type(h)) {
9591 parser_warning(p, h, "unused literal ignored");
9594 h = end = NEW_BLOCK(head, &head->nd_loc);
9604 switch (nd_type(nd)) {
9610 if (RTEST(ruby_verbose)) {
9611 parser_warning(p, tail, "statement not reached");
9619 if (nd_type(tail) != NODE_BLOCK) {
9620 tail = NEW_BLOCK(tail, &tail->nd_loc);
9621 tail->nd_end = tail;
9623 end->nd_next = tail;
9624 h->nd_end = tail->nd_end;
9625 nd_set_last_loc(head, nd_last_loc(tail));
9629 /* append item to the list */
9631 list_append(struct parser_params *p, NODE *list, NODE *item)
9635 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9636 if (list->nd_next) {
9637 last = list->nd_next->nd_end;
9644 last->nd_next = NEW_LIST(item, &item->nd_loc);
9645 list->nd_next->nd_end = last->nd_next;
9647 nd_set_last_loc(list, nd_last_loc(item));
9652 /* concat two lists */
9654 list_concat(NODE *head, NODE *tail)
9658 if (head->nd_next) {
9659 last = head->nd_next->nd_end;
9665 head->nd_alen += tail->nd_alen;
9666 last->nd_next = tail;
9667 if (tail->nd_next) {
9668 head->nd_next->nd_end = tail->nd_next->nd_end;
9671 head->nd_next->nd_end = tail;
9674 nd_set_last_loc(head, nd_last_loc(tail));
9680 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9682 if (NIL_P(tail)) return 1;
9683 if (!rb_enc_compatible(head, tail)) {
9684 compile_error(p, "string literal encodings differ (%s / %s)",
9685 rb_enc_name(rb_enc_get(head)),
9686 rb_enc_name(rb_enc_get(tail)));
9687 rb_str_resize(head, 0);
9688 rb_str_resize(tail, 0);
9691 rb_str_buf_append(head, tail);
9695 /* concat two string literals */
9697 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9699 enum node_type htype;
9703 if (!head) return tail;
9704 if (!tail) return head;
9706 htype = nd_type(head);
9707 if (htype == NODE_EVSTR) {
9708 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9709 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9710 head = list_append(p, node, head);
9713 if (p->heredoc_indent > 0) {
9716 nd_set_type(head, NODE_DSTR);
9718 return list_append(p, head, tail);
9723 switch (nd_type(tail)) {
9725 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9726 nd_type(headlast) == NODE_STR) {
9728 lit = headlast->nd_lit;
9733 if (htype == NODE_STR) {
9734 if (!literal_concat0(p, lit, tail->nd_lit)) {
9736 rb_discard_node(p, head);
9737 rb_discard_node(p, tail);
9740 rb_discard_node(p, tail);
9743 list_append(p, head, tail);
9748 if (htype == NODE_STR) {
9749 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9751 tail->nd_lit = head->nd_lit;
9752 rb_discard_node(p, head);
9755 else if (NIL_P(tail->nd_lit)) {
9757 head->nd_alen += tail->nd_alen - 1;
9758 head->nd_next->nd_end->nd_next = tail->nd_next;
9759 head->nd_next->nd_end = tail->nd_next->nd_end;
9760 rb_discard_node(p, tail);
9762 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9763 nd_type(headlast) == NODE_STR) {
9764 lit = headlast->nd_lit;
9765 if (!literal_concat0(p, lit, tail->nd_lit))
9767 tail->nd_lit = Qnil;
9771 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9776 if (htype == NODE_STR) {
9777 nd_set_type(head, NODE_DSTR);
9780 list_append(p, head, tail);
9787 evstr2dstr(struct parser_params *p, NODE *node)
9789 if (nd_type(node) == NODE_EVSTR) {
9790 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9791 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9792 node = list_append(p, dstr, node);
9798 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9803 switch (nd_type(node)) {
9804 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9808 return NEW_EVSTR(head, loc);
9812 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9813 const YYLTYPE *op_loc, const YYLTYPE *loc)
9818 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9819 nd_set_line(expr, op_loc->beg_pos.lineno);
9824 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9828 opcall = NEW_OPCALL(recv, id, 0, loc);
9829 nd_set_line(opcall, op_loc->beg_pos.lineno);
9834 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9836 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9837 nd_set_line(qcall, op_loc->beg_pos.lineno);
9842 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9845 if (block) block_dup_check(p, args, block);
9846 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9847 if (block) ret = method_add_block(p, ret, block, loc);
9852 #define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9854 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9857 int line = op_loc->beg_pos.lineno;
9861 if (node1 && (n = nd_once_body(node1)) != 0) {
9862 switch (nd_type(n)) {
9865 NODE *match = NEW_MATCH2(node1, node2, loc);
9866 nd_set_line(match, line);
9871 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9872 const VALUE lit = n->nd_lit;
9873 NODE *match = NEW_MATCH2(node1, node2, loc);
9874 match->nd_args = reg_named_capture_assign(p, lit, loc);
9875 nd_set_line(match, line);
9881 if (node2 && (n = nd_once_body(node2)) != 0) {
9884 switch (nd_type(n)) {
9886 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9889 match3 = NEW_MATCH3(node2, node1, loc);
9894 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9895 nd_set_line(n, line);
9899 # if WARN_PAST_SCOPE
9901 past_dvar_p(struct parser_params *p, ID id)
9903 struct vtable *past = p->lvtbl->past;
9905 if (vtable_included(past, id)) return 1;
9912 /* As Ripper#warn does not have arguments for the location, so the
9913 * following messages cannot be separated */
9914 #define WARN_LOCATION(type) do { \
9915 if (p->warn_location) { \
9917 VALUE file = rb_source_location(&line); \
9918 rb_warn3(type" in eval may not return location in binding;" \
9919 " use Binding#source_location instead\n" \
9920 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9921 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9926 numparam_nested_p(struct parser_params *p)
9928 struct local_vars *local = p->lvtbl;
9929 NODE *outer = local->numparam.outer;
9930 NODE *inner = local->numparam.inner;
9931 if (outer || inner) {
9932 NODE *used = outer ? outer : inner;
9933 compile_error(p, "numbered parameter is already used in\n"
9934 "%s:%d: %s block here",
9935 p->ruby_sourcefile, nd_line(used),
9936 outer ? "outer" : "inner");
9937 parser_show_error_line(p, &used->nd_loc);
9944 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9950 return NEW_SELF(loc);
9952 return NEW_NIL(loc);
9954 return NEW_TRUE(loc);
9956 return NEW_FALSE(loc);
9957 case keyword__FILE__:
9958 WARN_LOCATION("__FILE__");
9960 VALUE file = p->ruby_sourcefile_string;
9962 file = rb_str_new(0, 0);
9964 file = rb_str_dup(file);
9965 node = NEW_STR(file, loc);
9966 RB_OBJ_WRITTEN(p->ast, Qnil, file);
9969 case keyword__LINE__:
9970 WARN_LOCATION("__LINE__");
9971 return NEW_LIT(INT2FIX(p->tokline), loc);
9972 case keyword__ENCODING__:
9973 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
9974 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9978 switch (id_type(id)) {
9980 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
9981 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
9982 if (id == p->cur_arg) {
9983 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9986 if (vidp) *vidp |= LVAR_USED;
9987 node = NEW_DVAR(id, loc);
9990 if (local_id_ref(p, id, &vidp)) {
9991 if (id == p->cur_arg) {
9992 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9995 if (vidp) *vidp |= LVAR_USED;
9996 node = NEW_LVAR(id, loc);
9999 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10000 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10001 if (numparam_nested_p(p)) return 0;
10002 node = NEW_DVAR(id, loc);
10003 struct local_vars *local = p->lvtbl;
10004 if (!local->numparam.current) local->numparam.current = node;
10007 # if WARN_PAST_SCOPE
10008 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10009 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10012 /* method call without arguments */
10013 return NEW_VCALL(id, loc);
10015 return NEW_GVAR(id, loc);
10017 return NEW_IVAR(id, loc);
10019 return NEW_CONST(id, loc);
10021 return NEW_CVAR(id, loc);
10023 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10028 opt_arg_append(NODE *opt_list, NODE *opt)
10030 NODE *opts = opt_list;
10031 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10033 while (opts->nd_next) {
10034 opts = opts->nd_next;
10035 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10037 opts->nd_next = opt;
10043 kwd_append(NODE *kwlist, NODE *kw)
10046 NODE *kws = kwlist;
10047 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10048 while (kws->nd_next) {
10049 kws = kws->nd_next;
10050 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10058 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10060 return NEW_DEFINED(remove_begin_all(expr), loc);
10064 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10066 if (nd_type(symbol) == NODE_DSTR) {
10067 nd_set_type(symbol, NODE_DSYM);
10070 nd_set_type(symbol, NODE_LIT);
10071 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10073 return list_append(p, symbols, symbol);
10077 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10083 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10084 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10087 switch (nd_type(node)) {
10090 VALUE src = node->nd_lit;
10091 nd_set_type(node, NODE_LIT);
10092 nd_set_loc(node, loc);
10093 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10098 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10099 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10102 nd_set_type(node, NODE_DREGX);
10103 nd_set_loc(node, loc);
10104 node->nd_cflag = options & RE_OPTION_MASK;
10105 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10106 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10107 if (nd_type(list->nd_head) == NODE_STR) {
10108 VALUE tail = list->nd_head->nd_lit;
10109 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10110 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10111 if (!literal_concat0(p, lit, tail)) {
10112 return NEW_NIL(loc); /* dummy node on error */
10114 rb_str_resize(tail, 0);
10115 prev->nd_next = list->nd_next;
10116 rb_discard_node(p, list->nd_head);
10117 rb_discard_node(p, list);
10128 if (!node->nd_next) {
10129 VALUE src = node->nd_lit;
10130 nd_set_type(node, NODE_LIT);
10131 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10133 if (options & RE_OPTION_ONCE) {
10134 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10142 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10145 return NEW_KW_ARG(0, (k), loc);
10149 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10152 VALUE lit = STR_NEW0();
10153 NODE *xstr = NEW_XSTR(lit, loc);
10154 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10157 switch (nd_type(node)) {
10159 nd_set_type(node, NODE_XSTR);
10160 nd_set_loc(node, loc);
10163 nd_set_type(node, NODE_DXSTR);
10164 nd_set_loc(node, loc);
10167 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10174 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10178 if (!arg || !p->case_labels) return;
10180 lit = rb_node_case_when_optimizable_literal(arg);
10181 if (lit == Qundef) return;
10182 if (nd_type(arg) == NODE_STR) {
10183 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10186 if (NIL_P(p->case_labels)) {
10187 p->case_labels = rb_obj_hide(rb_hash_new());
10190 VALUE line = rb_hash_lookup(p->case_labels, lit);
10191 if (!NIL_P(line)) {
10192 rb_warning1("duplicated `when' clause with line %d is ignored",
10197 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10200 #else /* !RIPPER */
10202 id_is_var(struct parser_params *p, ID id)
10204 if (is_notop_id(id)) {
10205 switch (id & ID_SCOPE_MASK) {
10206 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10209 if (dyna_in_block(p)) {
10210 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10212 if (local_id(p, id)) return 1;
10213 /* method call without arguments */
10217 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10222 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10224 VALUE src = 0, err;
10226 if (ripper_is_node_yylval(re)) {
10227 src = RNODE(re)->nd_cval;
10228 re = RNODE(re)->nd_rval;
10230 if (ripper_is_node_yylval(opt)) {
10231 options = (int)RNODE(opt)->nd_tag;
10232 opt = RNODE(opt)->nd_rval;
10234 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10235 compile_error(p, "%"PRIsVALUE, err);
10237 return dispatch2(regexp_literal, re, opt);
10239 #endif /* !RIPPER */
10243 static const char rb_parser_lex_state_names[][8] = {
10244 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10245 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10246 "LABEL", "LABELED","FITEM",
10250 append_lex_state_name(enum lex_state_e state, VALUE buf)
10253 unsigned int mask = 1;
10254 static const char none[] = "NONE";
10256 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10257 if ((unsigned)state & mask) {
10259 rb_str_cat(buf, "|", 1);
10262 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10266 rb_str_cat(buf, none, sizeof(none)-1);
10272 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10274 VALUE mesg = p->debug_buffer;
10276 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10277 p->debug_buffer = Qnil;
10278 rb_io_puts(1, &mesg, out);
10280 if (!NIL_P(str) && RSTRING_LEN(str)) {
10281 rb_io_write(p->debug_output, str);
10286 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10287 enum lex_state_e to, int line)
10290 mesg = rb_str_new_cstr("lex_state: ");
10291 append_lex_state_name(from, mesg);
10292 rb_str_cat_cstr(mesg, " -> ");
10293 append_lex_state_name(to, mesg);
10294 rb_str_catf(mesg, " at line %d\n", line);
10295 flush_debug_buffer(p, p->debug_output, mesg);
10300 rb_parser_lex_state_name(enum lex_state_e state)
10302 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10306 append_bitstack_value(stack_type stack, VALUE mesg)
10309 rb_str_cat_cstr(mesg, "0");
10312 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10313 for (; mask && !(stack & mask); mask >>= 1) continue;
10314 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10319 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10320 const char *name, int line)
10322 VALUE mesg = rb_sprintf("%s: ", name);
10323 append_bitstack_value(stack, mesg);
10324 rb_str_catf(mesg, " at line %d\n", line);
10325 flush_debug_buffer(p, p->debug_output, mesg);
10329 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10332 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10335 rb_str_vcatf(mesg, fmt, ap);
10337 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10340 mesg = rb_str_new(0, 0);
10341 append_lex_state_name(p->lex.state, mesg);
10342 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10343 rb_str_resize(mesg, 0);
10344 append_bitstack_value(p->cond_stack, mesg);
10345 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10346 rb_str_resize(mesg, 0);
10347 append_bitstack_value(p->cmdarg_stack, mesg);
10348 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10349 if (p->debug_output == rb_stdout)
10350 p->debug_output = rb_stderr;
10355 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10357 int sourceline = here->sourceline;
10358 int beg_pos = (int)here->offset - here->quote
10359 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10360 int end_pos = (int)here->offset + here->length + here->quote;
10362 yylloc->beg_pos.lineno = sourceline;
10363 yylloc->beg_pos.column = beg_pos;
10364 yylloc->end_pos.lineno = sourceline;
10365 yylloc->end_pos.column = end_pos;
10370 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10372 yylloc->beg_pos.lineno = p->ruby_sourceline;
10373 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10374 yylloc->end_pos.lineno = p->ruby_sourceline;
10375 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10380 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10382 yylloc->beg_pos.lineno = p->ruby_sourceline;
10383 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10384 yylloc->end_pos.lineno = p->ruby_sourceline;
10385 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10388 #endif /* !RIPPER */
10391 parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10396 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10397 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10399 v = rb_id2str(valp->id);
10401 v = valp->node->nd_rval;
10403 rb_parser_printf(p, "%"PRIsVALUE, v);
10405 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10406 case tSTRING_CONTENT: case tCHAR:
10408 v = valp->node->nd_lit;
10412 rb_parser_printf(p, "%+"PRIsVALUE, v);
10416 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10418 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10423 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10425 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10434 assignable0(struct parser_params *p, ID id, const char **err)
10436 if (!id) return -1;
10439 *err = "Can't change the value of self";
10442 *err = "Can't assign to nil";
10445 *err = "Can't assign to true";
10447 case keyword_false:
10448 *err = "Can't assign to false";
10450 case keyword__FILE__:
10451 *err = "Can't assign to __FILE__";
10453 case keyword__LINE__:
10454 *err = "Can't assign to __LINE__";
10456 case keyword__ENCODING__:
10457 *err = "Can't assign to __ENCODING__";
10460 switch (id_type(id)) {
10462 if (dyna_in_block(p)) {
10463 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10464 compile_error(p, "Can't assign to numbered parameter _%d",
10465 NUMPARAM_ID_TO_IDX(id));
10468 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10469 if (dvar_defined(p, id)) return NODE_DASGN;
10470 if (local_id(p, id)) return NODE_LASGN;
10472 return NODE_DASGN_CURR;
10475 if (!local_id(p, id)) local_var(p, id);
10479 case ID_GLOBAL: return NODE_GASGN;
10480 case ID_INSTANCE: return NODE_IASGN;
10482 if (!p->in_def) return NODE_CDECL;
10483 *err = "dynamic constant assignment";
10485 case ID_CLASS: return NODE_CVASGN;
10487 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10494 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10496 const char *err = 0;
10497 int node_type = assignable0(p, id, &err);
10498 switch (node_type) {
10499 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10500 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10501 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10502 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10503 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10504 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10505 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10507 if (err) yyerror1(loc, err);
10508 return NEW_BEGIN(0, loc);
10512 assignable(struct parser_params *p, VALUE lhs)
10514 const char *err = 0;
10515 assignable0(p, get_id(lhs), &err);
10516 if (err) lhs = assign_error(p, lhs);
10522 is_private_local_id(ID name)
10525 if (name == idUScore) return 1;
10526 if (!is_local_id(name)) return 0;
10527 s = rb_id2str(name);
10529 return RSTRING_PTR(s)[0] == '_';
10533 shadowing_lvar_0(struct parser_params *p, ID name)
10535 if (is_private_local_id(name)) return 1;
10536 if (dyna_in_block(p)) {
10537 if (dvar_curr(p, name)) {
10538 yyerror0("duplicated argument name");
10540 else if (dvar_defined(p, name) || local_id(p, name)) {
10541 vtable_add(p->lvtbl->vars, name);
10542 if (p->lvtbl->used) {
10543 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10549 if (local_id(p, name)) {
10550 yyerror0("duplicated argument name");
10557 shadowing_lvar(struct parser_params *p, ID name)
10559 shadowing_lvar_0(p, name);
10564 new_bv(struct parser_params *p, ID name)
10567 if (!is_local_id(name)) {
10568 compile_error(p, "invalid local variable - %"PRIsVALUE,
10572 if (!shadowing_lvar_0(p, name)) return;
10578 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10580 return NEW_ATTRASGN(recv, tASET, idx, loc);
10584 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10586 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10587 compile_error(p, "both block arg and actual block given");
10592 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10594 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10595 return NEW_ATTRASGN(recv, id, 0, loc);
10599 rb_backref_error(struct parser_params *p, NODE *node)
10601 switch (nd_type(node)) {
10603 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10605 case NODE_BACK_REF:
10606 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10612 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10614 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10615 switch (nd_type(node1)) {
10617 return list_append(p, node1, node2);
10618 case NODE_BLOCK_PASS:
10619 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10620 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10622 case NODE_ARGSPUSH:
10623 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10624 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10625 nd_set_type(node1, NODE_ARGSCAT);
10628 if (nd_type(node1->nd_body) != NODE_LIST) break;
10629 node1->nd_body = list_append(p, node1->nd_body, node2);
10630 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10633 return NEW_ARGSPUSH(node1, node2, loc);
10637 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10639 if (!node2) return node1;
10640 switch (nd_type(node1)) {
10641 case NODE_BLOCK_PASS:
10642 if (node1->nd_head)
10643 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10645 node1->nd_head = NEW_LIST(node2, loc);
10647 case NODE_ARGSPUSH:
10648 if (nd_type(node2) != NODE_LIST) break;
10649 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10650 nd_set_type(node1, NODE_ARGSCAT);
10653 if (nd_type(node2) != NODE_LIST ||
10654 nd_type(node1->nd_body) != NODE_LIST) break;
10655 node1->nd_body = list_concat(node1->nd_body, node2);
10658 return NEW_ARGSCAT(node1, node2, loc);
10662 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10665 if ((n1 = splat_array(args)) != 0) {
10666 return list_append(p, n1, last_arg);
10668 return arg_append(p, args, last_arg, loc);
10672 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10675 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10676 return list_concat(n1, rest_arg);
10678 return arg_concat(p, args, rest_arg, loc);
10682 splat_array(NODE* node)
10684 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10685 if (nd_type(node) == NODE_LIST) return node;
10690 mark_lvar_used(struct parser_params *p, NODE *rhs)
10694 switch (nd_type(rhs)) {
10696 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10697 if (vidp) *vidp |= LVAR_USED;
10701 case NODE_DASGN_CURR:
10702 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10703 if (vidp) *vidp |= LVAR_USED;
10708 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10709 mark_lvar_used(p, rhs->nd_head);
10717 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10719 if (!lhs) return 0;
10721 switch (nd_type(lhs)) {
10726 case NODE_DASGN_CURR:
10730 lhs->nd_value = rhs;
10731 nd_set_loc(lhs, loc);
10734 case NODE_ATTRASGN:
10735 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10736 nd_set_loc(lhs, loc);
10740 /* should not happen */
10748 value_expr_check(struct parser_params *p, NODE *node)
10750 NODE *void_node = 0, *vn;
10753 rb_warning0("empty expression");
10756 switch (nd_type(node)) {
10762 return void_node ? void_node : node;
10765 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10766 compile_error(p, "unexpected node");
10769 if (node->nd_body->nd_body) {
10772 /* single line pattern matching */
10773 return void_node ? void_node : node;
10776 while (node->nd_next) {
10777 node = node->nd_next;
10779 node = node->nd_head;
10783 node = node->nd_body;
10788 if (!node->nd_body) {
10791 else if (!node->nd_else) {
10794 vn = value_expr_check(p, node->nd_body);
10795 if (!vn) return NULL;
10796 if (!void_node) void_node = vn;
10797 node = node->nd_else;
10802 node = node->nd_1st;
10807 case NODE_DASGN_CURR:
10809 mark_lvar_used(p, node);
10821 value_expr_gen(struct parser_params *p, NODE *node)
10823 NODE *void_node = value_expr_check(p, node);
10825 yyerror1(&void_node->nd_loc, "void value expression");
10826 /* or "control never reach"? */
10832 void_expr(struct parser_params *p, NODE *node)
10834 const char *useless = 0;
10836 if (!RTEST(ruby_verbose)) return;
10838 if (!node || !(node = nd_once_body(node))) return;
10839 switch (nd_type(node)) {
10841 switch (node->nd_mid) {
10860 useless = rb_id2name(node->nd_mid);
10871 case NODE_BACK_REF:
10872 useless = "a variable";
10875 useless = "a constant";
10881 useless = "a literal";
10906 useless = "defined?";
10911 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10916 void_stmts(struct parser_params *p, NODE *node)
10918 NODE *const n = node;
10919 if (!RTEST(ruby_verbose)) return n;
10920 if (!node) return n;
10921 if (nd_type(node) != NODE_BLOCK) return n;
10923 while (node->nd_next) {
10924 void_expr(p, node->nd_head);
10925 node = node->nd_next;
10931 remove_begin(NODE *node)
10933 NODE **n = &node, *n1 = node;
10934 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10935 *n = n1 = n1->nd_body;
10941 remove_begin_all(NODE *node)
10943 NODE **n = &node, *n1 = node;
10944 while (n1 && nd_type(n1) == NODE_BEGIN) {
10945 *n = n1 = n1->nd_body;
10951 reduce_nodes(struct parser_params *p, NODE **body)
10953 NODE *node = *body;
10956 *body = NEW_NIL(&NULL_LOC);
10959 #define subnodes(n1, n2) \
10960 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10961 (!node->n2) ? (body = &node->n1, 1) : \
10962 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10965 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10966 switch (nd_type(node)) {
10972 *body = node = node->nd_stts;
10973 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10976 *body = node = node->nd_body;
10977 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10980 body = &node->nd_end->nd_head;
10984 if (subnodes(nd_body, nd_else)) break;
10987 body = &node->nd_body;
10990 if (!subnodes(nd_body, nd_next)) goto end;
10993 if (!subnodes(nd_head, nd_resq)) goto end;
10996 if (node->nd_else) {
10997 body = &node->nd_resq;
11000 if (!subnodes(nd_head, nd_resq)) goto end;
11006 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11013 is_static_content(NODE *node)
11015 if (!node) return 1;
11016 switch (nd_type(node)) {
11018 if (!(node = node->nd_head)) break;
11021 if (!is_static_content(node->nd_head)) return 0;
11022 } while ((node = node->nd_next) != 0);
11037 assign_in_cond(struct parser_params *p, NODE *node)
11039 switch (nd_type(node)) {
11043 case NODE_DASGN_CURR:
11052 if (!node->nd_value) return 1;
11053 if (is_static_content(node->nd_value)) {
11054 /* reports always */
11055 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11066 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11068 case COND_IN_OP: break; \
11069 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11070 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11073 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11076 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11078 enum node_type type;
11080 if (node == 0) return 0;
11082 type = nd_type(node);
11084 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11085 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11086 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11088 return cond0(p, node, COND_IN_FF, loc);
11092 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11094 if (node == 0) return 0;
11095 if (!(node = nd_once_body(node))) return 0;
11096 assign_in_cond(p, node);
11098 switch (nd_type(node)) {
11102 SWITCH_BY_COND_TYPE(type, warn, "string ")
11106 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11108 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11112 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11113 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11118 node->nd_beg = range_op(p, node->nd_beg, loc);
11119 node->nd_end = range_op(p, node->nd_end, loc);
11120 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11121 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11125 SWITCH_BY_COND_TYPE(type, warning, "string ")
11129 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11130 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11131 nd_set_type(node, NODE_MATCH);
11133 else if (node->nd_lit == Qtrue ||
11134 node->nd_lit == Qfalse) {
11135 /* booleans are OK, e.g., while true */
11138 SWITCH_BY_COND_TYPE(type, warning, "")
11147 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11149 if (node == 0) return 0;
11150 return cond0(p, node, COND_IN_COND, loc);
11154 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11156 if (node == 0) return 0;
11157 return cond0(p, node, COND_IN_OP, loc);
11161 new_if(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_IF(cc, left, right, loc));
11169 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11171 if (!cc) return right;
11172 cc = cond0(p, cc, COND_IN_COND, loc);
11173 return newline_node(NEW_UNLESS(cc, left, right, loc));
11177 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11178 const YYLTYPE *op_loc, const YYLTYPE *loc)
11180 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11183 if (left && (enum node_type)nd_type(left) == type) {
11184 NODE *node = left, *second;
11185 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11188 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11189 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11190 left->nd_loc.end_pos = loc->end_pos;
11193 op = NEW_NODE(type, left, right, 0, loc);
11194 nd_set_line(op, op_loc->beg_pos.lineno);
11199 no_blockarg(struct parser_params *p, NODE *node)
11201 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11202 compile_error(p, "block argument should not be given");
11207 ret_args(struct parser_params *p, NODE *node)
11210 no_blockarg(p, node);
11211 if (nd_type(node) == NODE_LIST) {
11212 if (node->nd_next == 0) {
11213 node = node->nd_head;
11216 nd_set_type(node, NODE_VALUES);
11224 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11226 if (node) no_blockarg(p, node);
11228 return NEW_YIELD(node, loc);
11232 negate_lit(struct parser_params *p, VALUE lit)
11234 if (FIXNUM_P(lit)) {
11235 return LONG2FIX(-FIX2LONG(lit));
11237 if (SPECIAL_CONST_P(lit)) {
11239 if (FLONUM_P(lit)) {
11240 return DBL2NUM(-RFLOAT_VALUE(lit));
11245 switch (BUILTIN_TYPE(lit)) {
11247 BIGNUM_NEGATE(lit);
11248 lit = rb_big_norm(lit);
11251 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11254 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11255 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11258 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11262 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11263 rb_builtin_class_name(lit));
11270 arg_blk_pass(NODE *node1, NODE *node2)
11273 if (!node1) return node2;
11274 node2->nd_head = node1;
11275 nd_set_first_lineno(node2, nd_first_lineno(node1));
11276 nd_set_first_column(node2, nd_first_column(node1));
11283 args_info_empty_p(struct rb_args_info *args)
11285 if (args->pre_args_num) return false;
11286 if (args->post_args_num) return false;
11287 if (args->rest_arg) return false;
11288 if (args->opt_args) return false;
11289 if (args->block_arg) return false;
11290 if (args->kw_args) return false;
11291 if (args->kw_rest_arg) return false;
11296 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11298 int saved_line = p->ruby_sourceline;
11299 struct rb_args_info *args = tail->nd_ainfo;
11301 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11302 args->pre_init = pre_args ? pre_args->nd_next : 0;
11304 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11305 args->post_init = post_args ? post_args->nd_next : 0;
11306 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11308 args->rest_arg = rest_arg;
11310 args->opt_args = opt_args;
11312 args->ruby2_keywords = rest_arg == idFWD_REST;
11314 p->ruby_sourceline = saved_line;
11315 nd_set_loc(tail, loc);
11321 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11323 int saved_line = p->ruby_sourceline;
11325 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11326 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11327 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11328 args->imemo = tmpbuf;
11329 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11330 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11331 if (p->error_p) return node;
11333 args->block_arg = block;
11334 args->kw_args = kw_args;
11338 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11339 * variable order: k1, kr1, k2, &b, internal_id, krest
11341 * variable order: kr1, k1, k2, internal_id, krest, &b
11343 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11344 struct vtable *vtargs = p->lvtbl->args;
11345 NODE *kwn = kw_args;
11347 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11348 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11350 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11352 --required_kw_vars;
11353 kwn = kwn->nd_next;
11356 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11357 ID vid = kwn->nd_body->nd_vid;
11358 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11359 *required_kw_vars++ = vid;
11366 arg_var(p, kw_bits);
11367 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11368 if (block) arg_var(p, block);
11370 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11371 args->kw_rest_arg->nd_cflag = kw_bits;
11373 else if (kw_rest_arg == idNil) {
11374 args->no_kwarg = 1;
11376 else if (kw_rest_arg) {
11377 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11380 p->ruby_sourceline = saved_line;
11385 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11387 if (max_numparam > NO_PARAM) {
11389 YYLTYPE loc = RUBY_INIT_YYLLOC();
11390 args = new_args_tail(p, 0, 0, 0, 0);
11391 nd_set_loc(args, &loc);
11393 args->nd_ainfo->pre_args_num = max_numparam;
11399 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11401 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11403 aryptn->nd_pconst = constant;
11406 NODE *pre_args = NEW_LIST(pre_arg, loc);
11407 if (apinfo->pre_args) {
11408 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11411 apinfo->pre_args = pre_args;
11418 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11420 int saved_line = p->ruby_sourceline;
11422 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11423 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11424 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11425 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11426 apinfo->imemo = tmpbuf;
11427 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11429 apinfo->pre_args = pre_args;
11433 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11436 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11440 apinfo->rest_arg = NULL;
11443 apinfo->post_args = post_args;
11445 p->ruby_sourceline = saved_line;
11450 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11452 hshptn->nd_pconst = constant;
11457 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11459 int saved_line = p->ruby_sourceline;
11460 NODE *node, *kw_rest_arg_node;
11462 if (kw_rest_arg == idNil) {
11463 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11465 else if (kw_rest_arg) {
11466 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11469 kw_rest_arg_node = NULL;
11472 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11474 p->ruby_sourceline = saved_line;
11479 new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11481 NODE *node = NEW_CASE3(val, pat, loc);
11483 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11484 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11489 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11494 return NEW_LIT(ID2SYM(idNULL), loc);
11497 switch (nd_type(node)) {
11499 nd_set_type(node, NODE_DSYM);
11500 nd_set_loc(node, loc);
11503 lit = node->nd_lit;
11504 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11505 nd_set_type(node, NODE_LIT);
11506 nd_set_loc(node, loc);
11509 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11516 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11518 NODE *node = (NODE *)v;
11519 NODE **result = (NODE **)h;
11521 node->nd_next->nd_end = node->nd_next;
11522 node->nd_next->nd_next = 0;
11524 list_concat(*result, node);
11527 return ST_CONTINUE;
11531 remove_duplicate_keys(struct parser_params *p, NODE *hash)
11533 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11535 rb_code_location_t loc = hash->nd_loc;
11536 while (hash && hash->nd_head && hash->nd_next) {
11537 NODE *head = hash->nd_head;
11538 NODE *value = hash->nd_next;
11539 NODE *next = value->nd_next;
11540 VALUE key = (VALUE)head;
11542 if (nd_type(head) == NODE_LIT &&
11543 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11544 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11545 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11546 head->nd_lit, nd_line(head));
11547 head = ((NODE *)data)->nd_next;
11548 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11551 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11555 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11556 st_free_table(literal_keys);
11558 if (!result) result = hash;
11559 else list_concat(result, hash);
11561 result->nd_loc = loc;
11566 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11568 if (hash) hash = remove_duplicate_keys(p, hash);
11569 return NEW_HASH(hash, loc);
11574 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11576 if (is_private_local_id(id)) {
11579 if (st_is_member(p->pvtbl, id)) {
11580 yyerror1(loc, "duplicated variable name");
11583 st_insert(p->pvtbl, (st_data_t)id, 0);
11588 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11591 p->pktbl = st_init_numtable();
11593 else if (st_is_member(p->pktbl, key)) {
11594 yyerror1(loc, "duplicated key name");
11597 st_insert(p->pktbl, (st_data_t)key, 0);
11602 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11604 return NEW_HASH(hash, loc);
11606 #endif /* !RIPPER */
11610 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11615 ID vid = lhs->nd_vid;
11616 YYLTYPE lhs_loc = lhs->nd_loc;
11618 lhs->nd_value = rhs;
11619 nd_set_loc(lhs, loc);
11620 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11621 if (is_notop_id(vid)) {
11622 switch (id_type(vid)) {
11626 asgn->nd_aid = vid;
11630 else if (op == tANDOP) {
11631 lhs->nd_value = rhs;
11632 nd_set_loc(lhs, loc);
11633 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11637 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11638 nd_set_loc(asgn, loc);
11642 asgn = NEW_BEGIN(0, loc);
11648 new_ary_op_assign(struct parser_params *p, NODE *ary,
11649 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11653 args = make_list(args, args_loc);
11654 if (nd_type(args) == NODE_BLOCK_PASS) {
11655 args = NEW_ARGSCAT(args, rhs, loc);
11658 args = arg_concat(p, args, rhs, loc);
11660 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11666 new_attr_op_assign(struct parser_params *p, NODE *lhs,
11667 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11671 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11677 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11682 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11685 asgn = NEW_BEGIN(0, loc);
11692 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11695 yyerror1(loc, "dynamic constant assignment");
11697 return NEW_CDECL(0, 0, (path), loc);
11701 const_decl(struct parser_params *p, VALUE path)
11704 path = dispatch1(assign_error, path);
11711 assign_error(struct parser_params *p, VALUE a)
11713 a = dispatch1(assign_error, a);
11719 var_field(struct parser_params *p, VALUE a)
11721 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11727 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11729 NODE *result = head;
11731 NODE *tmp = rescue_else ? rescue_else : rescue;
11732 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11734 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11735 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11737 else if (rescue_else) {
11738 result = block_append(p, result, rescue_else);
11741 result = NEW_ENSURE(result, ensure, loc);
11743 fixpos(result, head);
11749 warn_unused_var(struct parser_params *p, struct local_vars *local)
11753 if (!local->used) return;
11754 cnt = local->used->pos;
11755 if (cnt != local->vars->pos) {
11756 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11759 ID *v = local->vars->tbl;
11760 ID *u = local->used->tbl;
11761 for (int i = 0; i < cnt; ++i) {
11762 if (!v[i] || (u[i] & LVAR_USED)) continue;
11763 if (is_private_local_id(v[i])) continue;
11764 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11770 local_push(struct parser_params *p, int toplevel_scope)
11772 struct local_vars *local;
11773 int inherits_dvars = toplevel_scope && compile_for_eval;
11774 int warn_unused_vars = RTEST(ruby_verbose);
11776 local = ALLOC(struct local_vars);
11777 local->prev = p->lvtbl;
11778 local->args = vtable_alloc(0);
11779 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11781 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11782 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11783 local->numparam.outer = 0;
11784 local->numparam.inner = 0;
11785 local->numparam.current = 0;
11787 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11789 # if WARN_PAST_SCOPE
11798 local_pop(struct parser_params *p)
11800 struct local_vars *local = p->lvtbl->prev;
11801 if (p->lvtbl->used) {
11802 warn_unused_var(p, p->lvtbl);
11803 vtable_free(p->lvtbl->used);
11805 # if WARN_PAST_SCOPE
11806 while (p->lvtbl->past) {
11807 struct vtable *past = p->lvtbl->past;
11808 p->lvtbl->past = past->prev;
11812 vtable_free(p->lvtbl->args);
11813 vtable_free(p->lvtbl->vars);
11816 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11822 local_tbl(struct parser_params *p)
11824 int cnt_args = vtable_size(p->lvtbl->args);
11825 int cnt_vars = vtable_size(p->lvtbl->vars);
11826 int cnt = cnt_args + cnt_vars;
11831 if (cnt <= 0) return 0;
11832 tbl = rb_imemo_tmpbuf_auto_free_pointer();
11833 buf = ALLOC_N(ID, cnt + 2);
11834 rb_imemo_tmpbuf_set_ptr(tbl, buf);
11835 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11836 /* remove IDs duplicated to warn shadowing */
11837 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11838 ID id = p->lvtbl->vars->tbl[i];
11839 if (!vtable_included(p->lvtbl->args, id)) {
11844 REALLOC_N(buf, ID, (cnt = j) + 2);
11845 rb_imemo_tmpbuf_set_ptr(tbl, buf);
11848 buf[cnt + 1] = (ID)tbl;
11849 RB_OBJ_WRITTEN(p->ast, Qnil, tbl);
11855 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11861 n = NEW_NODE(type, a0, a1, a2, loc);
11868 numparam_name(struct parser_params *p, ID id)
11870 if (!NUMPARAM_ID_P(id)) return;
11871 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11872 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11876 arg_var(struct parser_params *p, ID id)
11878 numparam_name(p, id);
11879 vtable_add(p->lvtbl->args, id);
11883 local_var(struct parser_params *p, ID id)
11885 numparam_name(p, id);
11886 vtable_add(p->lvtbl->vars, id);
11887 if (p->lvtbl->used) {
11888 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11893 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11895 struct vtable *vars, *args, *used;
11897 vars = p->lvtbl->vars;
11898 args = p->lvtbl->args;
11899 used = p->lvtbl->used;
11901 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11904 if (used) used = used->prev;
11907 if (vars && vars->prev == DVARS_INHERIT) {
11908 return rb_local_defined(id, p->parent_iseq);
11910 else if (vtable_included(args, id)) {
11914 int i = vtable_included(vars, id);
11915 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11921 local_id(struct parser_params *p, ID id)
11923 return local_id_ref(p, id, NULL);
11927 numparam_push(struct parser_params *p)
11930 struct local_vars *local = p->lvtbl;
11931 NODE *inner = local->numparam.inner;
11932 if (!local->numparam.outer) {
11933 local->numparam.outer = local->numparam.current;
11935 local->numparam.inner = 0;
11936 local->numparam.current = 0;
11944 numparam_pop(struct parser_params *p, NODE *prev_inner)
11947 struct local_vars *local = p->lvtbl;
11949 /* prefer first one */
11950 local->numparam.inner = prev_inner;
11952 else if (local->numparam.current) {
11953 /* current and inner are exclusive */
11954 local->numparam.inner = local->numparam.current;
11956 if (p->max_numparam > NO_PARAM) {
11957 /* current and outer are exclusive */
11958 local->numparam.current = local->numparam.outer;
11959 local->numparam.outer = 0;
11962 /* no numbered parameter */
11963 local->numparam.current = 0;
11968 static const struct vtable *
11969 dyna_push(struct parser_params *p)
11971 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11972 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11973 if (p->lvtbl->used) {
11974 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11976 return p->lvtbl->args;
11980 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11982 struct vtable *tmp = *vtblp;
11983 *vtblp = tmp->prev;
11984 # if WARN_PAST_SCOPE
11985 if (p->past_scope_enabled) {
11986 tmp->prev = p->lvtbl->past;
11987 p->lvtbl->past = tmp;
11995 dyna_pop_1(struct parser_params *p)
11997 struct vtable *tmp;
11999 if ((tmp = p->lvtbl->used) != 0) {
12000 warn_unused_var(p, p->lvtbl);
12001 p->lvtbl->used = p->lvtbl->used->prev;
12004 dyna_pop_vtable(p, &p->lvtbl->args);
12005 dyna_pop_vtable(p, &p->lvtbl->vars);
12009 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12011 while (p->lvtbl->args != lvargs) {
12013 if (!p->lvtbl->args) {
12014 struct local_vars *local = p->lvtbl->prev;
12015 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12023 dyna_in_block(struct parser_params *p)
12025 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12029 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12031 struct vtable *vars, *args, *used;
12034 args = p->lvtbl->args;
12035 vars = p->lvtbl->vars;
12036 used = p->lvtbl->used;
12038 while (!DVARS_TERMINAL_P(vars)) {
12039 if (vtable_included(args, id)) {
12042 if ((i = vtable_included(vars, id)) != 0) {
12043 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12048 if (!vidrefp) used = 0;
12049 if (used) used = used->prev;
12052 if (vars == DVARS_INHERIT) {
12053 return rb_dvar_defined(id, p->parent_iseq);
12060 dvar_defined(struct parser_params *p, ID id)
12062 return dvar_defined_ref(p, id, NULL);
12066 dvar_curr(struct parser_params *p, ID id)
12068 return (vtable_included(p->lvtbl->args, id) ||
12069 vtable_included(p->lvtbl->vars, id));
12073 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12076 "regexp encoding option '%c' differs from source encoding '%s'",
12077 c, rb_enc_name(rb_enc_get(str)));
12082 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12084 int c = RE_OPTION_ENCODING_IDX(options);
12088 rb_char_to_option_kcode(c, &opt, &idx);
12089 if (idx != ENCODING_GET(str) &&
12090 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12093 ENCODING_SET(str, idx);
12095 else if (RE_OPTION_ENCODING_NONE(options)) {
12096 if (!ENCODING_IS_ASCII8BIT(str) &&
12097 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12101 rb_enc_associate(str, rb_ascii8bit_encoding());
12103 else if (p->enc == rb_usascii_encoding()) {
12104 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12105 /* raise in re.c */
12106 rb_enc_associate(str, rb_usascii_encoding());
12109 rb_enc_associate(str, rb_ascii8bit_encoding());
12119 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12121 int c = rb_reg_fragment_setenc(p, str, options);
12122 if (c) reg_fragment_enc_error(p, str, c);
12126 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12129 reg_fragment_setenc(p, str, options);
12130 err = rb_reg_check_preprocess(str);
12132 err = rb_obj_as_string(err);
12133 compile_error(p, "%"PRIsVALUE, err);
12140 struct parser_params* parser;
12143 const YYLTYPE *loc;
12144 } reg_named_capture_assign_t;
12147 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12148 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12150 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12151 struct parser_params* p = arg->parser;
12152 rb_encoding *enc = arg->enc;
12153 long len = name_end - name;
12154 const char *s = (const char *)name;
12158 if (!len) return ST_CONTINUE;
12159 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len))
12160 return ST_CONTINUE;
12161 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12162 return ST_CONTINUE;
12164 var = intern_cstr(s, len, enc);
12165 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12166 succ = arg->succ_block;
12167 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12168 succ = block_append(p, succ, node);
12169 arg->succ_block = succ;
12170 return ST_CONTINUE;
12174 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12176 reg_named_capture_assign_t arg;
12179 arg.enc = rb_enc_get(regexp);
12180 arg.succ_block = 0;
12182 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12184 if (!arg.succ_block) return 0;
12185 return arg.succ_block->nd_next;
12189 parser_reg_compile(struct parser_params* p, VALUE str, int options)
12191 reg_fragment_setenc(p, str, options);
12192 return rb_parser_reg_compile(p, str, options);
12196 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12198 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12202 reg_compile(struct parser_params* p, VALUE str, int options)
12207 err = rb_errinfo();
12208 re = parser_reg_compile(p, str, options);
12210 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12211 rb_set_errinfo(err);
12212 compile_error(p, "%"PRIsVALUE, m);
12219 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12221 VALUE err = rb_errinfo();
12223 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12224 int c = rb_reg_fragment_setenc(p, str, options);
12225 if (c) reg_fragment_enc_error(p, str, c);
12226 re = rb_parser_reg_compile(p, str, options);
12228 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12229 rb_set_errinfo(err);
12237 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12239 struct parser_params *p;
12240 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12241 p->do_print = print;
12243 p->do_chomp = chomp;
12244 p->do_split = split;
12248 rb_parser_warn_location(VALUE vparser, int warn)
12250 struct parser_params *p;
12251 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12252 p->warn_location = warn;
12256 parser_append_options(struct parser_params *p, NODE *node)
12258 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12259 const YYLTYPE *const LOC = &default_location;
12262 NODE *print = NEW_FCALL(rb_intern("print"),
12263 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12265 node = block_append(p, node, print);
12270 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12271 NODE *split = NEW_GASGN(rb_intern("$F"),
12272 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12273 rb_intern("split"), args, LOC),
12275 node = block_append(p, split, node);
12278 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12279 rb_intern("chomp!"), 0, LOC);
12280 node = block_append(p, chomp, node);
12283 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12290 rb_init_parse(void)
12292 /* just to suppress unused-function warnings */
12298 internal_id(struct parser_params *p)
12300 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12301 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12303 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12305 #endif /* !RIPPER */
12308 parser_initialize(struct parser_params *p)
12310 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12311 p->command_start = TRUE;
12312 p->ruby_sourcefile_string = Qnil;
12313 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12316 p->delayed.token = Qnil;
12318 p->parsing_thread = Qnil;
12320 p->error_buffer = Qfalse;
12322 p->debug_buffer = Qnil;
12323 p->debug_output = rb_stdout;
12324 p->enc = rb_utf8_encoding();
12328 #define parser_mark ripper_parser_mark
12329 #define parser_free ripper_parser_free
12333 parser_mark(void *ptr)
12335 struct parser_params *p = (struct parser_params*)ptr;
12337 rb_gc_mark(p->lex.input);
12338 rb_gc_mark(p->lex.prevline);
12339 rb_gc_mark(p->lex.lastline);
12340 rb_gc_mark(p->lex.nextline);
12341 rb_gc_mark(p->ruby_sourcefile_string);
12342 rb_gc_mark((VALUE)p->lex.strterm);
12343 rb_gc_mark((VALUE)p->ast);
12344 rb_gc_mark(p->case_labels);
12346 rb_gc_mark(p->debug_lines);
12347 rb_gc_mark(p->compile_option);
12348 rb_gc_mark(p->error_buffer);
12350 rb_gc_mark(p->delayed.token);
12351 rb_gc_mark(p->value);
12352 rb_gc_mark(p->result);
12353 rb_gc_mark(p->parsing_thread);
12355 rb_gc_mark(p->debug_buffer);
12356 rb_gc_mark(p->debug_output);
12358 rb_gc_mark((VALUE)p->heap);
12363 parser_free(void *ptr)
12365 struct parser_params *p = (struct parser_params*)ptr;
12366 struct local_vars *local, *prev;
12369 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12371 for (local = p->lvtbl; local; local = prev) {
12372 if (local->vars) xfree(local->vars);
12373 prev = local->prev;
12377 token_info *ptinfo;
12378 while ((ptinfo = p->token_info) != 0) {
12379 p->token_info = ptinfo->next;
12387 parser_memsize(const void *ptr)
12389 struct parser_params *p = (struct parser_params*)ptr;
12390 struct local_vars *local;
12391 size_t size = sizeof(*p);
12394 for (local = p->lvtbl; local; local = local->prev) {
12395 size += sizeof(*local);
12396 if (local->vars) size += local->vars->capa * sizeof(ID);
12401 static const rb_data_type_t parser_data_type = {
12412 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12416 #undef rb_reserved_word
12418 const struct kwtable *
12419 rb_reserved_word(const char *str, unsigned int len)
12421 return reserved_word(str, len);
12425 rb_parser_new(void)
12427 struct parser_params *p;
12428 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12429 &parser_data_type, p);
12430 parser_initialize(p);
12435 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12437 struct parser_params *p;
12439 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12440 p->error_buffer = main ? Qfalse : Qnil;
12441 p->parent_iseq = base;
12447 #define rb_parser_end_seen_p ripper_parser_end_seen_p
12448 #define rb_parser_encoding ripper_parser_encoding
12449 #define rb_parser_get_yydebug ripper_parser_get_yydebug
12450 #define rb_parser_set_yydebug ripper_parser_set_yydebug
12451 #define rb_parser_get_debug_output ripper_parser_get_debug_output
12452 #define rb_parser_set_debug_output ripper_parser_set_debug_output
12453 static VALUE ripper_parser_end_seen_p(VALUE vparser);
12454 static VALUE ripper_parser_encoding(VALUE vparser);
12455 static VALUE ripper_parser_get_yydebug(VALUE self);
12456 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12457 static VALUE ripper_parser_get_debug_output(VALUE self);
12458 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12462 * ripper.error? -> Boolean
12464 * Return true if parsed source has errors.
12467 ripper_error_p(VALUE vparser)
12469 struct parser_params *p;
12471 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12472 return p->error_p ? Qtrue : Qfalse;
12478 * ripper.end_seen? -> Boolean
12480 * Return true if parsed source ended by +\_\_END\_\_+.
12483 rb_parser_end_seen_p(VALUE vparser)
12485 struct parser_params *p;
12487 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12488 return p->ruby__end__seen ? Qtrue : Qfalse;
12493 * ripper.encoding -> encoding
12495 * Return encoding of the source.
12498 rb_parser_encoding(VALUE vparser)
12500 struct parser_params *p;
12502 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12503 return rb_enc_from_encoding(p->enc);
12509 * ripper.yydebug -> true or false
12514 rb_parser_get_yydebug(VALUE self)
12516 struct parser_params *p;
12518 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12519 return p->debug ? Qtrue : Qfalse;
12525 * ripper.yydebug = flag
12530 rb_parser_set_yydebug(VALUE self, VALUE flag)
12532 struct parser_params *p;
12534 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12535 p->debug = RTEST(flag);
12541 * ripper.debug_output -> obj
12543 * Get debug output.
12546 rb_parser_get_debug_output(VALUE self)
12548 struct parser_params *p;
12550 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12551 return p->debug_output;
12556 * ripper.debug_output = obj
12558 * Set debug output.
12561 rb_parser_set_debug_output(VALUE self, VALUE output)
12563 struct parser_params *p;
12565 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12566 return p->debug_output = output;
12571 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12572 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12573 * potential memory leak */
12574 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12575 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12576 (new)->cnt = (cnt), (ptr))
12579 rb_parser_malloc(struct parser_params *p, size_t size)
12581 size_t cnt = HEAPCNT(1, size);
12582 rb_imemo_tmpbuf_t *n = NEWHEAP();
12583 void *ptr = xmalloc(size);
12585 return ADD2HEAP(n, cnt, ptr);
12589 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12591 size_t cnt = HEAPCNT(nelem, size);
12592 rb_imemo_tmpbuf_t *n = NEWHEAP();
12593 void *ptr = xcalloc(nelem, size);
12595 return ADD2HEAP(n, cnt, ptr);
12599 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12601 rb_imemo_tmpbuf_t *n;
12602 size_t cnt = HEAPCNT(1, size);
12604 if (ptr && (n = p->heap) != NULL) {
12606 if (n->ptr == ptr) {
12607 n->ptr = ptr = xrealloc(ptr, size);
12608 if (n->cnt) n->cnt = cnt;
12611 } while ((n = n->next) != NULL);
12614 ptr = xrealloc(ptr, size);
12615 return ADD2HEAP(n, cnt, ptr);
12619 rb_parser_free(struct parser_params *p, void *ptr)
12621 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12623 while ((n = *prev) != NULL) {
12624 if (n->ptr == ptr) {
12626 rb_gc_force_recycle((VALUE)n);
12636 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12639 VALUE mesg = p->debug_buffer;
12641 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12643 rb_str_vcatf(mesg, fmt, ap);
12645 if (RSTRING_END(mesg)[-1] == '\n') {
12646 rb_io_write(p->debug_output, mesg);
12647 p->debug_buffer = Qnil;
12652 parser_compile_error(struct parser_params *p, const char *fmt, ...)
12656 rb_io_flush(p->debug_output);
12660 rb_syntax_error_append(p->error_buffer,
12661 p->ruby_sourcefile_string,
12662 p->ruby_sourceline,
12663 rb_long2int(p->lex.pcur - p->lex.pbeg),
12669 count_char(const char *str, int c)
12672 while (str[n] == c) ++n;
12677 * strip enclosing double-quotes, same as the default yytnamerr except
12678 * for that single-quotes matching back-quotes do not stop stripping.
12680 * "\"`class' keyword\"" => "`class' keyword"
12682 RUBY_FUNC_EXPORTED size_t
12683 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12686 if (*yystr == '"') {
12687 size_t yyn = 0, bquote = 0;
12688 const char *yyp = yystr;
12694 bquote = count_char(yyp+1, '`') + 1;
12695 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12703 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12704 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12710 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12711 if (yyres) memcpy(yyres + yyn, yyp, 3);
12716 goto do_not_strip_quotes;
12719 goto do_not_strip_quotes;
12722 if (*++yyp != '\\')
12723 goto do_not_strip_quotes;
12724 /* Fall through. */
12739 do_not_strip_quotes: ;
12742 if (!yyres) return strlen(yystr);
12744 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12749 #ifdef RIPPER_DEBUG
12752 ripper_validate_object(VALUE self, VALUE x)
12754 if (x == Qfalse) return x;
12755 if (x == Qtrue) return x;
12756 if (x == Qnil) return x;
12758 rb_raise(rb_eArgError, "Qundef given");
12759 if (FIXNUM_P(x)) return x;
12760 if (SYMBOL_P(x)) return x;
12761 switch (BUILTIN_TYPE(x)) {
12771 if (nd_type((NODE *)x) != NODE_RIPPER) {
12772 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12774 x = ((NODE *)x)->nd_rval;
12777 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12778 (void *)x, rb_obj_classname(x));
12780 if (!RBASIC_CLASS(x)) {
12781 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12782 (void *)x, rb_builtin_type_name(TYPE(x)));
12788 #define validate(x) ((x) = get_value(x))
12791 ripper_dispatch0(struct parser_params *p, ID mid)
12793 return rb_funcall(p->value, mid, 0);
12797 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12800 return rb_funcall(p->value, mid, 1, a);
12804 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12808 return rb_funcall(p->value, mid, 2, a, b);
12812 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12817 return rb_funcall(p->value, mid, 3, a, b, c);
12821 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12827 return rb_funcall(p->value, mid, 4, a, b, c, d);
12831 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12838 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12842 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12851 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12855 ripper_get_id(VALUE v)
12858 if (!RB_TYPE_P(v, T_NODE)) return 0;
12860 if (nd_type(nd) != NODE_RIPPER) return 0;
12865 ripper_get_value(VALUE v)
12868 if (v == Qundef) return Qnil;
12869 if (!RB_TYPE_P(v, T_NODE)) return v;
12871 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12872 return nd->nd_rval;
12876 ripper_error(struct parser_params *p)
12882 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12887 va_start(args, fmt);
12888 str = rb_vsprintf(fmt, args);
12890 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12895 ripper_lex_get_generic(struct parser_params *p, VALUE src)
12897 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12898 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12899 rb_raise(rb_eTypeError,
12900 "gets returned %"PRIsVALUE" (expected String or nil)",
12901 rb_obj_class(line));
12907 ripper_lex_io_get(struct parser_params *p, VALUE src)
12909 return rb_io_gets(src);
12913 ripper_s_allocate(VALUE klass)
12915 struct parser_params *p;
12916 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12917 &parser_data_type, p);
12922 #define ripper_initialized_p(r) ((r)->lex.input != 0)
12926 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12928 * Create a new Ripper object.
12929 * _src_ must be a String, an IO, or an Object which has #gets method.
12931 * This method does not starts parsing.
12932 * See also Ripper#parse and Ripper.parse.
12935 ripper_initialize(int argc, VALUE *argv, VALUE self)
12937 struct parser_params *p;
12938 VALUE src, fname, lineno;
12940 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12941 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12942 if (RB_TYPE_P(src, T_FILE)) {
12943 p->lex.gets = ripper_lex_io_get;
12945 else if (rb_respond_to(src, id_gets)) {
12946 p->lex.gets = ripper_lex_get_generic;
12950 p->lex.gets = lex_get_str;
12952 p->lex.input = src;
12954 if (NIL_P(fname)) {
12955 fname = STR_NEW2("(ripper)");
12959 StringValueCStr(fname);
12960 fname = rb_str_new_frozen(fname);
12962 parser_initialize(p);
12964 p->ruby_sourcefile_string = fname;
12965 p->ruby_sourcefile = RSTRING_PTR(fname);
12966 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12972 ripper_parse0(VALUE parser_v)
12974 struct parser_params *p;
12976 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12978 p->ast = rb_ast_new();
12979 ripper_yyparse((void*)p);
12980 rb_ast_dispose(p->ast);
12986 ripper_ensure(VALUE parser_v)
12988 struct parser_params *p;
12990 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12991 p->parsing_thread = Qnil;
12999 * Start parsing and returns the value of the root action.
13002 ripper_parse(VALUE self)
13004 struct parser_params *p;
13006 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13007 if (!ripper_initialized_p(p)) {
13008 rb_raise(rb_eArgError, "method called for uninitialized object");
13010 if (!NIL_P(p->parsing_thread)) {
13011 if (p->parsing_thread == rb_thread_current())
13012 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13014 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13016 p->parsing_thread = rb_thread_current();
13017 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13024 * ripper.column -> Integer
13026 * Return column number of current parsing line.
13027 * This number starts from 0.
13030 ripper_column(VALUE self)
13032 struct parser_params *p;
13035 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13036 if (!ripper_initialized_p(p)) {
13037 rb_raise(rb_eArgError, "method called for uninitialized object");
13039 if (NIL_P(p->parsing_thread)) return Qnil;
13040 col = p->lex.ptok - p->lex.pbeg;
13041 return LONG2NUM(col);
13046 * ripper.filename -> String
13048 * Return current parsing filename.
13051 ripper_filename(VALUE self)
13053 struct parser_params *p;
13055 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13056 if (!ripper_initialized_p(p)) {
13057 rb_raise(rb_eArgError, "method called for uninitialized object");
13059 return p->ruby_sourcefile_string;
13064 * ripper.lineno -> Integer
13066 * Return line number of current parsing line.
13067 * This number starts from 1.
13070 ripper_lineno(VALUE self)
13072 struct parser_params *p;
13074 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13075 if (!ripper_initialized_p(p)) {
13076 rb_raise(rb_eArgError, "method called for uninitialized object");
13078 if (NIL_P(p->parsing_thread)) return Qnil;
13079 return INT2NUM(p->ruby_sourceline);
13084 * ripper.state -> Integer
13086 * Return scanner state of current token.
13089 ripper_state(VALUE self)
13091 struct parser_params *p;
13093 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13094 if (!ripper_initialized_p(p)) {
13095 rb_raise(rb_eArgError, "method called for uninitialized object");
13097 if (NIL_P(p->parsing_thread)) return Qnil;
13098 return INT2NUM(p->lex.state);
13103 * ripper.token -> String
13105 * Return the current token string.
13108 ripper_token(VALUE self)
13110 struct parser_params *p;
13113 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13114 if (!ripper_initialized_p(p)) {
13115 rb_raise(rb_eArgError, "method called for uninitialized object");
13117 if (NIL_P(p->parsing_thread)) return Qnil;
13118 pos = p->lex.ptok - p->lex.pbeg;
13119 len = p->lex.pcur - p->lex.ptok;
13120 return rb_str_subseq(p->lex.lastline, pos, len);
13123 #ifdef RIPPER_DEBUG
13126 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13129 if (obj == Qundef) {
13130 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13137 ripper_value(VALUE self, VALUE obj)
13139 return ULONG2NUM(obj);
13145 * Ripper.lex_state_name(integer) -> string
13147 * Returns a string representation of lex_state.
13150 ripper_lex_state_name(VALUE self, VALUE state)
13152 return rb_parser_lex_state_name(NUM2INT(state));
13158 ripper_init_eventids1();
13159 ripper_init_eventids2();
13160 id_warn = rb_intern_const("warn");
13161 id_warning = rb_intern_const("warning");
13162 id_gets = rb_intern_const("gets");
13163 id_assoc = rb_intern_const("=>");
13165 (void)yystpcpy; /* may not used in newer bison */
13171 InitVM_ripper(void)
13175 Ripper = rb_define_class("Ripper", rb_cObject);
13176 /* version of Ripper */
13177 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13178 rb_define_alloc_func(Ripper, ripper_s_allocate);
13179 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13180 rb_define_method(Ripper, "parse", ripper_parse, 0);
13181 rb_define_method(Ripper, "column", ripper_column, 0);
13182 rb_define_method(Ripper, "filename", ripper_filename, 0);
13183 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13184 rb_define_method(Ripper, "state", ripper_state, 0);
13185 rb_define_method(Ripper, "token", ripper_token, 0);
13186 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13187 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13188 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13189 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13190 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13191 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13192 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13193 #ifdef RIPPER_DEBUG
13194 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13195 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13196 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13199 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13200 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13202 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13204 <% @exprs.each do |expr, desc| -%>
13206 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
13208 ripper_init_eventids1_table(Ripper);
13209 ripper_init_eventids2_table(Ripper);
13212 /* Hack to let RDoc document SCRIPT_LINES__ */
13215 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13216 * after the assignment will be added as an Array of lines with the file
13219 rb_define_global_const("SCRIPT_LINES__", Qnil);
13223 #endif /* RIPPER */
13228 * c-file-style: "ruby"