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/internal/config.h"
32#include "internal/compile.h"
33#include "internal/compilers.h"
34#include "internal/complex.h"
35#include "internal/error.h"
36#include "internal/hash.h"
37#include "internal/imemo.h"
38#include "internal/io.h"
39#include "internal/numeric.h"
40#include "internal/parse.h"
41#include "internal/rational.h"
42#include "internal/re.h"
43#include "internal/symbol.h"
44#include "internal/thread.h"
45#include "internal/util.h"
46#include "internal/variable.h"
50#include "ruby/encoding.h"
51#include "ruby/regex.h"
55#include "ruby/ractor.h"
66 unsigned int in_defined: 1;
67 unsigned int in_kwarg: 1;
68 unsigned int in_def: 1;
69 unsigned int in_class: 1;
70 BITFIELD(enum shareability, shareable_constant_value, 2);
75#define NO_LEX_CTXT (struct lex_context){0}
77#define AREF(ary, i) RARRAY_AREF(ary, i)
79#ifndef WARN_PAST_SCOPE
80# define WARN_PAST_SCOPE 0
85#define yydebug (p->debug) /* disable the global variable definition */
87#define YYMALLOC(size) rb_parser_malloc(p, (size))
88#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
89#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
90#define YYFREE(ptr) rb_parser_free(p, (ptr))
91#define YYFPRINTF rb_parser_printf
92#define YY_LOCATION_PRINT(File, loc) \
93 rb_parser_printf(p, "%d.%d-%d.%d", \
94 (loc).beg_pos.lineno, (loc).beg_pos.column,\
95 (loc).end_pos.lineno, (loc).end_pos.column)
96#define YYLLOC_DEFAULT(Current, Rhs, N) \
100 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
101 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
105 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
106 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
110#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
111 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
112#define RUBY_SET_YYLLOC_OF_NONE(Current) \
113 rb_parser_set_location_of_none(p, &(Current))
114#define RUBY_SET_YYLLOC(Current) \
115 rb_parser_set_location(p, &(Current))
116#define RUBY_INIT_YYLLOC() \
118 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
119 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
123 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
124 EXPR_END_bit, /* newline significant, +/- is an operator. */
125 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
126 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
127 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
128 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
129 EXPR_MID_bit, /* newline significant, +/- is an operator. */
130 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
131 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
132 EXPR_CLASS_bit, /* immediate after `class', no here document. */
133 EXPR_LABEL_bit, /* flag bit, label is allowed. */
134 EXPR_LABELED_bit, /* flag bit, just after a label. */
135 EXPR_FITEM_bit, /* symbol literal as FNAME. */
138/* examine combinations */
140#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
154 EXPR_VALUE = EXPR_BEG,
155 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
156 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
157 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
160#define IS_lex_state_for(x, ls) ((x) & (ls))
161#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
162#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
163#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
165# define SET_LEX_STATE(ls) \
168 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
169 (enum lex_state_e)(ls)))
171typedef VALUE stack_type;
173static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
175# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
176# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
177# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
178# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
179# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
181/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
182 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
183#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
184#define COND_POP() BITSTACK_POP(cond_stack)
185#define COND_P() BITSTACK_SET_P(cond_stack)
186#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
188/* A flag to identify keyword_do_block; "do" keyword after command_call.
189 Example: `foo 1, 2 do`. */
190#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
191#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
192#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
193#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
209 struct local_vars *prev;
212 NODE *outer, *inner, *current;
223#define NUMPARAM_ID_P(id) numparam_id_p(id)
224#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
225#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
229 if (!is_local_id(id)) return 0;
230 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
231 return idx > 0 && idx <= NUMPARAM_MAX;
233static void numparam_name(struct parser_params *p, ID id);
235#define DVARS_INHERIT ((void*)1)
236#define DVARS_TOPSCOPE NULL
237#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
239typedef struct token_info {
241 rb_code_position_t beg;
244 struct token_info *next;
247typedef struct rb_strterm_struct rb_strterm_t;
250 Structure of Lexer Buffer:
252 lex.pbeg lex.ptok lex.pcur lex.pend
254 |------------+------------+------------|
258struct parser_params {
259 rb_imemo_tmpbuf_t *heap;
264 rb_strterm_t *strterm;
265 VALUE (*gets)(struct parser_params*,VALUE);
276 VALUE (*call)(VALUE, int);
278 enum lex_state_e state;
279 /* track the nest level of any parens "()[]{}" */
281 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
283 /* track the nest level of only braces "{}" */
286 stack_type cond_stack;
287 stack_type cmdarg_stack;
293 int heredoc_line_indent;
295 struct local_vars *lvtbl;
299 int ruby_sourceline; /* current line no. */
300 const char *ruby_sourcefile; /* current source file */
301 VALUE ruby_sourcefile_string;
303 token_info *token_info;
305 VALUE compile_option;
317 struct lex_context ctxt;
319 unsigned int command_start:1;
320 unsigned int eofp: 1;
321 unsigned int ruby__end__seen: 1;
322 unsigned int debug: 1;
323 unsigned int has_shebang: 1;
324 unsigned int token_seen: 1;
325 unsigned int token_info_enabled: 1;
327 unsigned int past_scope_enabled: 1;
329 unsigned int error_p: 1;
330 unsigned int cr_seen: 1;
335 unsigned int do_print: 1;
336 unsigned int do_loop: 1;
337 unsigned int do_chomp: 1;
338 unsigned int do_split: 1;
340 NODE *eval_tree_begin;
344 const struct rb_iseq_struct *parent_iseq;
356 VALUE parsing_thread;
360#define intern_cstr(n,l,en) rb_intern3(n,l,en)
362#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
363#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
364#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
365#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
366#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
369push_pvtbl(struct parser_params *p)
371 st_table *tbl = p->pvtbl;
372 p->pvtbl = st_init_numtable();
377pop_pvtbl(struct parser_params *p, st_table *tbl)
379 st_free_table(p->pvtbl);
384push_pktbl(struct parser_params *p)
386 st_table *tbl = p->pktbl;
392pop_pktbl(struct parser_params *p, st_table *tbl)
394 if (p->pktbl) st_free_table(p->pktbl);
398static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
399#define yyerror0(msg) parser_yyerror(p, NULL, (msg))
400#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
401#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
402#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
404static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
405static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
406static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
407static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
408static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
411#define compile_for_eval (0)
413#define compile_for_eval (p->parent_iseq != 0)
416#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
418#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
419#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
420#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
422#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
424static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
428rb_discard_node(struct parser_params *p, NODE *n)
430 rb_ast_delete_node(p->ast, n);
436add_mark_object(struct parser_params *p, VALUE obj)
438 if (!SPECIAL_CONST_P(obj)
439 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
441 rb_ast_add_mark_object(p->ast, obj);
446static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
449static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
450#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
452static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
455parser_get_node_id(struct parser_params *p)
457 int node_id = p->node_id;
464set_line_body(NODE *body, int line)
467 switch (nd_type(body)) {
470 nd_set_line(body, line);
474#define yyparse ruby_yyparse
476static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
477static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
478#define new_nil(loc) NEW_NIL(loc)
479static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
480static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
481static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
482static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
484static NODE *newline_node(NODE*);
485static void fixpos(NODE*,NODE*);
487static int value_expr_gen(struct parser_params*,NODE*);
488static void void_expr(struct parser_params*,NODE*);
489static NODE *remove_begin(NODE*);
490static NODE *remove_begin_all(NODE*);
491#define value_expr(node) value_expr_gen(p, (node))
492static NODE *void_stmts(struct parser_params*,NODE*);
493static void reduce_nodes(struct parser_params*,NODE**);
494static void block_dup_check(struct parser_params*,NODE*,NODE*);
496static NODE *block_append(struct parser_params*,NODE*,NODE*);
497static NODE *list_append(struct parser_params*,NODE*,NODE*);
498static NODE *list_concat(NODE*,NODE*);
499static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
500static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
501static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
502static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
503static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
504static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
505static NODE *evstr2dstr(struct parser_params*,NODE*);
506static NODE *splat_array(NODE*);
507static void mark_lvar_used(struct parser_params *p, NODE *rhs);
509static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
510static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
511static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
512static 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);
513static 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;}
515static bool args_info_empty_p(struct rb_args_info *args);
516static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
517static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
518static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
519static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
520static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
521static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
522static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
523static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
524static void warn_one_line_pattern_matching(struct parser_params *p, NODE *node, NODE *pattern, bool right_assign);
526static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
527static NODE *args_with_numbered(struct parser_params*,NODE*,int);
529static VALUE negate_lit(struct parser_params*, VALUE);
530static NODE *ret_args(struct parser_params*,NODE*);
531static NODE *arg_blk_pass(NODE*,NODE*);
532static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
533static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
535static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
536static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
538static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
539static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
541static void rb_backref_error(struct parser_params*,NODE*);
542static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
544static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
545static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
546static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
547static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
548static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
550static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
552static NODE *opt_arg_append(NODE*, NODE*);
553static NODE *kwd_append(NODE*, NODE*);
555static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
556static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
558static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
560static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
562#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
564static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
566static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
568static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
570static ID *local_tbl(struct parser_params*);
572static VALUE reg_compile(struct parser_params*, VALUE, int);
573static void reg_fragment_setenc(struct parser_params*, VALUE, int);
574static int reg_fragment_check(struct parser_params*, VALUE, int);
575static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
577static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
578static NODE *heredoc_dedent(struct parser_params*,NODE*);
580static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
582#define get_id(id) (id)
583#define get_value(val) (val)
584#define get_num(num) (num)
586#define NODE_RIPPER NODE_CDECL
587#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
589static inline int ripper_is_node_yylval(VALUE n);
592ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
594 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
595 add_mark_object(p, b);
596 add_mark_object(p, c);
597 return NEW_RIPPER(a, b, c, &NULL_LOC);
601ripper_is_node_yylval(VALUE n)
603 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
606#define value_expr(node) ((void)(node))
607#define remove_begin(node) (node)
608#define void_stmts(p,x) (x)
609#define rb_dvar_defined(id, base) 0
610#define rb_local_defined(id, base) 0
611static ID ripper_get_id(VALUE);
612#define get_id(id) ripper_get_id(id)
613static VALUE ripper_get_value(VALUE);
614#define get_value(val) ripper_get_value(val)
615#define get_num(num) (int)get_id(num)
616static VALUE assignable(struct parser_params*,VALUE);
617static int id_is_var(struct parser_params *p, ID id);
619#define method_cond(p,node,loc) (node)
620#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
621#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
622#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
623#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
625#define new_nil(loc) Qnil
627static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
629static VALUE const_decl(struct parser_params *p, VALUE path);
631static VALUE var_field(struct parser_params *p, VALUE a);
632static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
634static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
636static VALUE backref_error(struct parser_params*, NODE *, VALUE);
639/* forward declaration */
640typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
642RUBY_SYMBOL_EXPORT_BEGIN
643VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
644int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
645enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
646VALUE rb_parser_lex_state_name(enum lex_state_e state);
647void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
648PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
649YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
650YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
651YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
652RUBY_SYMBOL_EXPORT_END
654static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
655static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
657static ID formal_argument(struct parser_params*, ID);
659static ID formal_argument(struct parser_params*, VALUE);
661static ID shadowing_lvar(struct parser_params*,ID);
662static void new_bv(struct parser_params*,ID);
664static void local_push(struct parser_params*,int);
665static void local_pop(struct parser_params*);
666static void local_var(struct parser_params*, ID);
667static void arg_var(struct parser_params*, ID);
668static int local_id(struct parser_params *p, ID id);
669static int local_id_ref(struct parser_params*, ID, ID **);
671static ID internal_id(struct parser_params*);
672static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
673static NODE *new_args_forward_def(struct parser_params*, NODE*, const YYLTYPE*);
675static int check_forwarding_args(struct parser_params*);
676static void add_forwarding_args(struct parser_params *p);
678static const struct vtable *dyna_push(struct parser_params *);
679static void dyna_pop(struct parser_params*, const struct vtable *);
680static int dyna_in_block(struct parser_params*);
681#define dyna_var(p, id) local_var(p, id)
682static int dvar_defined(struct parser_params*, ID);
683static int dvar_defined_ref(struct parser_params*, ID, ID**);
684static int dvar_curr(struct parser_params*,ID);
686static int lvar_defined(struct parser_params*, ID);
688static NODE *numparam_push(struct parser_params *p);
689static void numparam_pop(struct parser_params *p, NODE *prev_inner);
692# define METHOD_NOT idNOT
694# define METHOD_NOT '!'
697#define idFWD_REST '*'
699#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
701#define idFWD_KWREST 0
703#define idFWD_BLOCK '&'
705#define RE_OPTION_ONCE (1<<16)
706#define RE_OPTION_ENCODING_SHIFT 8
707#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
708#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
709#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
710#define RE_OPTION_MASK 0xff
711#define RE_OPTION_ARG_ENCODING_NONE 32
713/* structs for managing terminator of string literal and heredocment */
714typedef struct rb_strterm_literal_struct {
721 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
725 long paren; /* '(' of `%q(...)` */
729 long term; /* ')' of `%q(...)` */
731} rb_strterm_literal_t;
733#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
735struct rb_strterm_heredoc_struct {
736 VALUE lastline; /* the string of line that contains `<<"END"` */
737 long offset; /* the column of END in `<<"END"` */
738 int sourceline; /* lineno of the line that contains `<<"END"` */
739 unsigned length /* the length of END in `<<"END"` */
740#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
741 : HERETERM_LENGTH_BITS
742# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
744# define HERETERM_LENGTH_MAX UINT_MAX
747#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
755STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
757#define STRTERM_HEREDOC IMEMO_FL_USER0
759struct rb_strterm_struct {
762 rb_strterm_literal_t literal;
763 rb_strterm_heredoc_t heredoc;
769rb_strterm_mark(VALUE obj)
771 rb_strterm_t *strterm = (rb_strterm_t*)obj;
772 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
773 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
774 rb_gc_mark(heredoc->lastline);
779#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
780size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
782#define TOKEN2ID(tok) ( \
783 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
784 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
785 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
786 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
787 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
788 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
789 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
791/****** Ripper *******/
794#define RIPPER_VERSION "0.1.0"
796static inline VALUE intern_sym(const char *name);
798#include "eventids1.c"
799#include "eventids2.c"
801static VALUE ripper_dispatch0(struct parser_params*,ID);
802static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
803static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
804static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
805static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
806static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
807static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
808static void ripper_error(struct parser_params *p);
810#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
811#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
812#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
813#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
814#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
815#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
816#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))
818#define yyparse ripper_yyparse
820#define ID2VAL(id) STATIC_ID2SYM(id)
821#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
822#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
824#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
825 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
827#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
830new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
832 NODE *t = (NODE *)tail;
833 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
834 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
838new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
840 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
841 add_mark_object(p, kw_args);
842 add_mark_object(p, kw_rest_arg);
843 add_mark_object(p, block);
848args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
854new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
856 NODE *t = (NODE *)aryptn;
857 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
859 if (!NIL_P(pre_arg)) {
860 if (!NIL_P(pre_args)) {
861 rb_ary_unshift(pre_args, pre_arg);
864 pre_args = rb_ary_new_from_args(1, pre_arg);
867 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
871new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
876 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
882 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
883 add_mark_object(p, pre_args);
884 add_mark_object(p, rest_arg);
885 add_mark_object(p, post_args);
890new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
892 NODE *t = (NODE *)fndptn;
893 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
895 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
899new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
903 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
904 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
906 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
907 add_mark_object(p, pre_rest_arg);
908 add_mark_object(p, args);
909 add_mark_object(p, post_rest_arg);
913#define new_hash(p,h,l) rb_ary_new_from_args(0)
916new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
922new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
924 NODE *t = (NODE *)hshptn;
925 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
926 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
930new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
934 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
939 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
941 add_mark_object(p, kw_args);
942 add_mark_object(p, kw_rest_arg);
946#define new_defined(p,expr,loc) dispatch1(defined, (expr))
948static VALUE heredoc_dedent(struct parser_params*,VALUE);
951#define ID2VAL(id) (id)
952#define TOKEN2VAL(t) ID2VAL(t)
953#define KWD2EID(t, v) keyword_##t
956set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
958 body = remove_begin(body);
959 reduce_nodes(p, &body);
960 n->nd_defn = NEW_SCOPE(args, body, loc);
962 nd_set_line(n->nd_defn, loc->end_pos.lineno);
963 set_line_body(body, loc->beg_pos.lineno);
968rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
969 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
971 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
972 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
973 loc.beg_pos = arg_loc->beg_pos;
974 return NEW_RESCUE(arg, rescue, 0, &loc);
980restore_defun(struct parser_params *p, NODE *name)
982 YYSTYPE c = {.val = name->nd_cval};
983 p->cur_arg = name->nd_vid;
984 p->ctxt.in_def = c.ctxt.in_def;
985 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
989endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
992 defn = defn->nd_defn;
994 ID mid = defn->nd_mid;
995 if (is_attrset_id(mid)) {
996 yyerror1(loc, "setter method cannot be defined in an endless method definition");
998 token_info_drop(p, "def", loc->beg_pos);
1004# define ifndef_ripper(x) (x)
1007# define Qnull Qundef
1008# define ifndef_ripper(x)
1011# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1012# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1013# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1014# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1015# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1016# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1017# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1018# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1019# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1020# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1021# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1022# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1023# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1024# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1025# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1026# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1027# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1028# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1029# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1030# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1032static ID id_warn, id_warning, id_gets, id_assoc;
1033# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1034# define WARN_S_L(s,l) STR_NEW(s,l)
1035# define WARN_S(s) STR_NEW2(s)
1036# define WARN_I(i) INT2NUM(i)
1037# define WARN_ID(i) rb_id2str(i)
1038# define WARN_IVAL(i) i
1039# define PRIsWARN "s"
1040# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1041# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1042# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1043# ifdef HAVE_VA_ARGS_MACRO
1044# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1046# define WARN_CALL rb_funcall
1048# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1049# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1050# ifdef HAVE_VA_ARGS_MACRO
1051# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1053# define WARNING_CALL rb_funcall
1055PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1056# define compile_error ripper_compile_error
1058# define WARN_S_L(s,l) s
1061# define WARN_ID(i) rb_id2name(i)
1062# define WARN_IVAL(i) NUM2INT(i)
1063# define PRIsWARN PRIsVALUE
1064# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1065# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1066# define WARN_CALL rb_compile_warn
1067# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1068# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1069# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1070# define WARNING_CALL rb_compile_warning
1071PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1072# define compile_error parser_compile_error
1075#define WARN_EOL(tok) \
1076 (looking_at_eol_p(p) ? \
1077 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1079static int looking_at_eol_p(struct parser_params *p);
1084%define parse.error verbose
1087 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1089 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1091} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1094 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1096 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1098} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1101 rb_parser_printf(p, "$%ld", $$->nd_nth);
1103 rb_parser_printf(p, "%"PRIsVALUE, $$);
1108 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1110 rb_parser_printf(p, "%"PRIsVALUE, $$);
1114%lex-param {struct parser_params *p}
1115%parse-param {struct parser_params *p}
1118 RUBY_SET_YYLLOC_OF_NONE(@$);
1127 const struct vtable *vars;
1128 struct rb_strterm_struct *strterm;
1129 struct lex_context ctxt;
1133 keyword_class "`class'"
1134 keyword_module "`module'"
1136 keyword_undef "`undef'"
1137 keyword_begin "`begin'"
1138 keyword_rescue "`rescue'"
1139 keyword_ensure "`ensure'"
1142 keyword_unless "`unless'"
1143 keyword_then "`then'"
1144 keyword_elsif "`elsif'"
1145 keyword_else "`else'"
1146 keyword_case "`case'"
1147 keyword_when "`when'"
1148 keyword_while "`while'"
1149 keyword_until "`until'"
1151 keyword_break "`break'"
1152 keyword_next "`next'"
1153 keyword_redo "`redo'"
1154 keyword_retry "`retry'"
1157 keyword_do_cond "`do' for condition"
1158 keyword_do_block "`do' for block"
1159 keyword_do_LAMBDA "`do' for lambda"
1160 keyword_return "`return'"
1161 keyword_yield "`yield'"
1162 keyword_super "`super'"
1163 keyword_self "`self'"
1165 keyword_true "`true'"
1166 keyword_false "`false'"
1170 modifier_if "`if' modifier"
1171 modifier_unless "`unless' modifier"
1172 modifier_while "`while' modifier"
1173 modifier_until "`until' modifier"
1174 modifier_rescue "`rescue' modifier"
1175 keyword_alias "`alias'"
1176 keyword_defined "`defined?'"
1177 keyword_BEGIN "`BEGIN'"
1179 keyword__LINE__ "`__LINE__'"
1180 keyword__FILE__ "`__FILE__'"
1181 keyword__ENCODING__ "`__ENCODING__'"
1183%token <val> tIDENTIFIER "local variable or method"
1184%token <val> tFID "method"
1185%token <val> tGVAR "global variable"
1186%token <val> tIVAR "instance variable"
1187%token <val> tCONSTANT "constant"
1188%token <val> tCVAR "class variable"
1189%token <val> tLABEL "label"
1190%token <val> tINTEGER "integer literal"
1191%token <val> tFLOAT "float literal"
1192%token <val> tRATIONAL "rational literal"
1193%token <val> tIMAGINARY "imaginary literal"
1194%token <val> tCHAR "char literal"
1195%token <val> tNTH_REF "numbered reference"
1196%token <val> tBACK_REF "back reference"
1197%token <val> tSTRING_CONTENT "literal content"
1198%token <val> tREGEXP_END
1200%type <val> singleton strings string string1 xstring regexp
1201%type <val> string_contents xstring_contents regexp_contents string_content
1202%type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1203%type <val> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1204%type <val> top_compstmt top_stmts top_stmt begin_block
1205%type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1206%type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1207%type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1208%type <val> args call_args opt_call_args
1209%type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1210%type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1211%type <val> command_rhs arg_rhs
1212%type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1213%type <val> f_block_optarg f_block_opt
1214%type <val> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1215%type <val> f_optarg f_marg f_marg_list f_margs f_rest_marg
1216%type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1217%type <val> block_param opt_block_param block_param_def f_opt
1218%type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1219%type <val> bv_decls opt_bv_decl bvar
1220%type <val> lambda f_larglist lambda_body brace_body do_body
1221%type <val> brace_block cmd_brace_block do_block lhs none fitem
1222%type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1223%type <val> p_case_body p_cases p_top_expr p_top_expr_body
1224%type <val> p_expr p_as p_alt p_expr_basic p_find
1225%type <val> p_args p_args_head p_args_tail p_args_post p_arg
1226%type <val> p_value p_primitive p_variable p_var_ref p_const
1227%type <val> p_kwargs p_kwarg p_kw
1228%type <val> keyword_variable user_variable sym operation operation2 operation3
1229%type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1230%type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1231%type <val> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1232%type <val> f_no_kwarg f_any_kwrest args_forward excessed_comma
1233 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1234%token END_OF_INPUT 0 "end-of-input"
1236/* escaped chars, should be ignored otherwise */
1237%token <val> '\\' "backslash"
1238%token tSP "escaped space"
1239%token <val> '\t' "escaped horizontal tab"
1240%token <val> '\f' "escaped form feed"
1241%token <val> '\r' "escaped carriage return"
1242%token <val> '\13' "escaped vertical tab"
1243%token tUPLUS 132 "unary+"
1244%token tUMINUS 133 "unary-"
1246%token tCMP 135 "<=>"
1248%token tEQQ 141 "==="
1252%token tANDOP 148 "&&"
1253%token tOROP 149 "||"
1254%token tMATCH 143 "=~"
1255%token tNMATCH 144 "!~"
1256%token tDOT2 128 ".."
1257%token tDOT3 129 "..."
1258%token tBDOT2 130 "(.."
1259%token tBDOT3 131 "(..."
1260%token tAREF 145 "[]"
1261%token tASET 146 "[]="
1262%token tLSHFT 136 "<<"
1263%token tRSHFT 137 ">>"
1264%token <val> tANDDOT 150 "&."
1265%token <val> tCOLON2 147 "::"
1266%token tCOLON3 ":: at EXPR_BEG"
1267%token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1270%token tLPAREN_ARG "( arg"
1274%token tLBRACE_ARG "{ arg"
1276%token tDSTAR "**arg"
1279%token tSYMBEG "symbol literal"
1280%token tSTRING_BEG "string literal"
1281%token tXSTRING_BEG "backtick literal"
1282%token tREGEXP_BEG "regexp literal"
1283%token tWORDS_BEG "word list"
1284%token tQWORDS_BEG "verbatim word list"
1285%token tSYMBOLS_BEG "symbol list"
1286%token tQSYMBOLS_BEG "verbatim symbol list"
1287%token tSTRING_END "terminator"
1288%token tSTRING_DEND "'}'"
1289%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1296%nonassoc tLBRACE_ARG
1298%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1299%left keyword_or keyword_and
1301%nonassoc keyword_defined
1303%left modifier_rescue
1305%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1308%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1309%left '>' tGEQ '<' tLEQ
1315%right tUMINUS_NUM tUMINUS
1317%right '!' '~' tUPLUS
1323 SET_LEX_STATE(EXPR_BEG);
1324 local_push(p, ifndef_ripper(1)+0);
1329 if ($2 && !compile_for_eval) {
1331 /* last expression should not be void */
1332 if (nd_type(node) == NODE_BLOCK) {
1333 while (node->nd_next) {
1334 node = node->nd_next;
1336 node = node->nd_head;
1338 node = remove_begin(node);
1341 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1343 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1348top_compstmt : top_stmts opt_terms
1350 $$ = void_stmts(p, $1);
1357 $$ = NEW_BEGIN(0, &@$);
1359 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1364 $$ = newline_node($1);
1366 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1368 | top_stmts terms top_stmt
1371 $$ = block_append(p, $1, newline_node($3));
1373 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1377 $$ = remove_begin($2);
1382 | keyword_BEGIN begin_block
1388begin_block : '{' top_compstmt '}'
1391 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1392 NEW_BEGIN($2, &@$));
1393 $$ = NEW_BEGIN(0, &@$);
1395 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1401 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1406 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1408 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1415 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1417 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=Qnil;v4=escape_Qundef($3);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1421compstmt : stmts opt_terms
1423 $$ = void_stmts(p, $1);
1430 $$ = NEW_BEGIN(0, &@$);
1432 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1437 $$ = newline_node($1);
1439 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1441 | stmts terms stmt_or_begin
1444 $$ = block_append(p, $1, newline_node($3));
1446 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1450 $$ = remove_begin($2);
1460 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1468stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1471 $$ = NEW_ALIAS($2, $4, &@$);
1473 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1475 | keyword_alias tGVAR tGVAR
1478 $$ = NEW_VALIAS($2, $3, &@$);
1480 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1482 | keyword_alias tGVAR tBACK_REF
1487 buf[1] = (char)$3->nd_nth;
1488 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1490 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1492 | keyword_alias tGVAR tNTH_REF
1494 static const char mesg[] = "can't make alias for the number variables";
1496 yyerror1(&@3, mesg);
1497 $$ = NEW_BEGIN(0, &@$);
1499 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$3;v3=dispatch2(alias_error,v1,v2);$$=v3;}ripper_error(p);
1501 | keyword_undef undef_list
1506 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1508 | stmt modifier_if expr_value
1511 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1514 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1516 | stmt modifier_unless expr_value
1519 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1522 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1524 | stmt modifier_while expr_value
1527 if ($1 && nd_type($1) == NODE_BEGIN) {
1528 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1531 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1534 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1536 | stmt modifier_until expr_value
1539 if ($1 && nd_type($1) == NODE_BEGIN) {
1540 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1543 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1546 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1548 | stmt modifier_rescue stmt
1552 YYLTYPE loc = code_loc_gen(&@2, &@3);
1553 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1554 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1556 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1558 | keyword_END '{' compstmt '}'
1560 if (p->ctxt.in_def) {
1561 rb_warn0("END in method; use at_exit");
1565 NODE *scope = NEW_NODE(
1566 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1567 $$ = NEW_POSTEXE(scope, &@$);
1570 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1573 | mlhs '=' lex_ctxt command_call
1577 $$ = node_assign(p, $1, $4, $3, &@$);
1579 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(massign,v1,v2);$$=v3;}
1581 | lhs '=' lex_ctxt mrhs
1585 $$ = node_assign(p, $1, $4, $3, &@$);
1587 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(assign,v1,v2);$$=v3;}
1589 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1592 YYLTYPE loc = code_loc_gen(&@5, &@6);
1594 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1596 {VALUE v1,v2,v3,v4,v5,v6;v1=$4;v2=$6;v3=dispatch2(rescue_mod,v1,v2);v4=$1;v5=v3;v6=dispatch2(massign,v4,v5);$$=v6;}
1598 | mlhs '=' lex_ctxt mrhs_arg
1601 $$ = node_assign(p, $1, $4, $3, &@$);
1603 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(massign,v1,v2);$$=v3;}
1608command_asgn : lhs '=' lex_ctxt command_rhs
1611 $$ = node_assign(p, $1, $4, $3, &@$);
1613 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(assign,v1,v2);$$=v3;}
1615 | var_lhs tOP_ASGN lex_ctxt command_rhs
1618 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1620 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$4;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1622 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1625 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1627 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$7;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1630 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1633 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1635 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1637 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1640 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1642 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1644 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1647 YYLTYPE loc = code_loc_gen(&@1, &@3);
1648 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1650 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1652 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1655 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1657 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1659 | backref tOP_ASGN lex_ctxt command_rhs
1662 rb_backref_error(p, $1);
1663 $$ = NEW_BEGIN(0, &@$);
1665 {VALUE v1,v2,v3;v1=var_field(p, $1);v2=$4;v3=dispatch2(assign,v1,v2);$$=backref_error(p, RNODE($1), v3);}ripper_error(p);
1669command_rhs : command_call %prec tOP_ASGN
1674 | command_call modifier_rescue stmt
1677 YYLTYPE loc = code_loc_gen(&@2, &@3);
1679 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1681 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1687 | expr keyword_and expr
1689 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1691 | expr keyword_or expr
1693 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1695 | keyword_not opt_nl expr
1697 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1701 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1706 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1707 p->command_start = FALSE;
1709 p->ctxt.in_kwarg = 1;
1711 {$<tbl>$ = push_pvtbl(p);}
1713 {pop_pvtbl(p, $<tbl>4);}
1715 p->ctxt.in_kwarg = $<ctxt>3.in_kwarg;
1717 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
1718 warn_one_line_pattern_matching(p, $$, $5, true);
1720 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$5;v2=Qnil;v3=Qnil;v4=dispatch3(in,v1,v2,v3);v5=$1;v6=v4;v7=dispatch2(case,v5,v6);$$=v7;}
1725 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1726 p->command_start = FALSE;
1728 p->ctxt.in_kwarg = 1;
1730 {$<tbl>$ = push_pvtbl(p);}
1732 {pop_pvtbl(p, $<tbl>4);}
1734 p->ctxt.in_kwarg = $<ctxt>3.in_kwarg;
1736 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
1737 warn_one_line_pattern_matching(p, $$, $5, false);
1739 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$5;v2=Qnil;v3=Qnil;v4=dispatch3(in,v1,v2,v3);v5=$1;v6=v4;v7=dispatch2(case,v5,v6);$$=v7;}
1741 | arg %prec tLBRACE_ARG
1746 ID fname = get_id($1);
1747 ID cur_arg = p->cur_arg;
1748 YYSTYPE c = {.ctxt = p->ctxt};
1749 numparam_name(p, fname);
1753 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
1756 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
1761defn_head : k_def def_name
1765 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
1770defs_head : k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} def_name
1772 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
1775 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
1777 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
1778 add_mark_object(p, ary);
1779 $<node>$->nd_rval = ary;
1791expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1797command_call : command
1801block_command : block_call
1802 | block_call call_op2 operation2 command_args
1805 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1807 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
1811cmd_brace_block : tLBRACE_ARG brace_body '}'
1815 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1816 nd_set_line($$, @1.end_pos.lineno);
1824 $$ = NEW_FCALL($1, 0, &@$);
1825 nd_set_line($$, p->tokline);
1831command : fcall command_args %prec tLOWEST
1835 nd_set_last_loc($1, @2.end_pos);
1838 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1840 | fcall command_args cmd_brace_block
1843 block_dup_check(p, $2, $3);
1845 $$ = method_add_block(p, $1, $3, &@$);
1847 nd_set_last_loc($1, @2.end_pos);
1849 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);v4=v3;v5=$3;v6=dispatch2(method_add_block,v4,v5);$$=v6;}
1851 | primary_value call_op operation2 command_args %prec tLOWEST
1854 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1856 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1858 | primary_value call_op operation2 command_args cmd_brace_block
1861 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1863 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1865 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1868 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1870 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1872 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1875 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1877 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1879 | keyword_super command_args
1882 $$ = NEW_SUPER($2, &@$);
1885 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1887 | keyword_yield command_args
1890 $$ = new_yield(p, $2, &@$);
1893 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1895 | k_return call_args
1898 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1900 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1902 | keyword_break call_args
1905 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1907 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1909 | keyword_next call_args
1912 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1914 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1919 | tLPAREN mlhs_inner rparen
1924 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1928mlhs_inner : mlhs_basic
1929 | tLPAREN mlhs_inner rparen
1932 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1934 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1938mlhs_basic : mlhs_head
1941 $$ = NEW_MASGN($1, 0, &@$);
1945 | mlhs_head mlhs_item
1948 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1950 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1952 | mlhs_head tSTAR mlhs_node
1955 $$ = NEW_MASGN($1, $3, &@$);
1957 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1959 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1962 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1964 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1969 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1971 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1973 | mlhs_head tSTAR ',' mlhs_post
1976 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1978 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$4;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1983 $$ = NEW_MASGN(0, $2, &@$);
1985 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1987 | tSTAR mlhs_node ',' mlhs_post
1990 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1992 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$4;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1997 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1999 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
2001 | tSTAR ',' mlhs_post
2004 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2006 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
2010mlhs_item : mlhs_node
2011 | tLPAREN mlhs_inner rparen
2016 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
2020mlhs_head : mlhs_item ','
2023 $$ = NEW_LIST($1, &@1);
2025 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
2027 | mlhs_head mlhs_item ','
2030 $$ = list_append(p, $1, $2);
2032 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
2036mlhs_post : mlhs_item
2039 $$ = NEW_LIST($1, &@$);
2041 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
2043 | mlhs_post ',' mlhs_item
2046 $$ = list_append(p, $1, $3);
2048 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
2052mlhs_node : user_variable
2055 $$ = assignable(p, $1, 0, &@$);
2057 $$=assignable(p, var_field(p, $1));
2062 $$ = assignable(p, $1, 0, &@$);
2064 $$=assignable(p, var_field(p, $1));
2066 | primary_value '[' opt_call_args rbracket
2069 $$ = aryset(p, $1, $3, &@$);
2071 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
2073 | primary_value call_op tIDENTIFIER
2075 if ($2 == tANDDOT) {
2076 yyerror1(&@2, "&. inside multiple assignment destination");
2079 $$ = attrset(p, $1, $2, $3, &@$);
2081 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2083 | primary_value tCOLON2 tIDENTIFIER
2086 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2088 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
2090 | primary_value call_op tCONSTANT
2092 if ($2 == tANDDOT) {
2093 yyerror1(&@2, "&. inside multiple assignment destination");
2096 $$ = attrset(p, $1, $2, $3, &@$);
2098 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2100 | primary_value tCOLON2 tCONSTANT
2103 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2105 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
2110 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2112 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
2117 rb_backref_error(p, $1);
2118 $$ = NEW_BEGIN(0, &@$);
2120 $$=backref_error(p, RNODE($1), var_field(p, $1));ripper_error(p);
2127 $$ = assignable(p, $1, 0, &@$);
2129 $$=assignable(p, var_field(p, $1));
2134 $$ = assignable(p, $1, 0, &@$);
2136 $$=assignable(p, var_field(p, $1));
2138 | primary_value '[' opt_call_args rbracket
2141 $$ = aryset(p, $1, $3, &@$);
2143 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
2145 | primary_value call_op tIDENTIFIER
2148 $$ = attrset(p, $1, $2, $3, &@$);
2150 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2152 | primary_value tCOLON2 tIDENTIFIER
2155 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2157 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2159 | primary_value call_op tCONSTANT
2162 $$ = attrset(p, $1, $2, $3, &@$);
2164 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
2166 | primary_value tCOLON2 tCONSTANT
2169 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2171 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
2176 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2178 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
2183 rb_backref_error(p, $1);
2184 $$ = NEW_BEGIN(0, &@$);
2186 $$=backref_error(p, RNODE($1), var_field(p, $1));ripper_error(p);
2192 static const char mesg[] = "class/module name must be CONSTANT";
2194 yyerror1(&@1, mesg);
2196 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(class_name_error,v1,v2);$$=v3;}ripper_error(p);
2201cpath : tCOLON3 cname
2204 $$ = NEW_COLON3($2, &@$);
2206 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2211 $$ = NEW_COLON2(0, $$, &@$);
2213 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2215 | primary_value tCOLON2 cname
2218 $$ = NEW_COLON2($1, $3, &@$);
2220 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2229 SET_LEX_STATE(EXPR_ENDFN);
2238 $$ = NEW_LIT(ID2SYM($1), &@$);
2240 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2248 $$ = NEW_UNDEF($1, &@$);
2250 $$=rb_ary_new3(1, get_value($1));
2252 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2255 NODE *undef = NEW_UNDEF($4, &@4);
2256 $$ = block_append(p, $1, undef);
2258 $$=rb_ary_push($1, get_value($4));
2262op : '|' { ifndef_ripper($$ = '|'); }
2263 | '^' { ifndef_ripper($$ = '^'); }
2264 | '&' { ifndef_ripper($$ = '&'); }
2265 | tCMP { ifndef_ripper($$ = tCMP); }
2266 | tEQ { ifndef_ripper($$ = tEQ); }
2267 | tEQQ { ifndef_ripper($$ = tEQQ); }
2268 | tMATCH { ifndef_ripper($$ = tMATCH); }
2269 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2270 | '>' { ifndef_ripper($$ = '>'); }
2271 | tGEQ { ifndef_ripper($$ = tGEQ); }
2272 | '<' { ifndef_ripper($$ = '<'); }
2273 | tLEQ { ifndef_ripper($$ = tLEQ); }
2274 | tNEQ { ifndef_ripper($$ = tNEQ); }
2275 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2276 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2277 | '+' { ifndef_ripper($$ = '+'); }
2278 | '-' { ifndef_ripper($$ = '-'); }
2279 | '*' { ifndef_ripper($$ = '*'); }
2280 | tSTAR { ifndef_ripper($$ = '*'); }
2281 | '/' { ifndef_ripper($$ = '/'); }
2282 | '%' { ifndef_ripper($$ = '%'); }
2283 | tPOW { ifndef_ripper($$ = tPOW); }
2284 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2285 | '!' { ifndef_ripper($$ = '!'); }
2286 | '~' { ifndef_ripper($$ = '~'); }
2287 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2288 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2289 | tAREF { ifndef_ripper($$ = tAREF); }
2290 | tASET { ifndef_ripper($$ = tASET); }
2291 | '`' { ifndef_ripper($$ = '`'); }
2294reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2295 | keyword_BEGIN | keyword_END
2296 | keyword_alias | keyword_and | keyword_begin
2297 | keyword_break | keyword_case | keyword_class | keyword_def
2298 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2299 | keyword_end | keyword_ensure | keyword_false
2300 | keyword_for | keyword_in | keyword_module | keyword_next
2301 | keyword_nil | keyword_not | keyword_or | keyword_redo
2302 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2303 | keyword_super | keyword_then | keyword_true | keyword_undef
2304 | keyword_when | keyword_yield | keyword_if | keyword_unless
2305 | keyword_while | keyword_until
2308arg : lhs '=' lex_ctxt arg_rhs
2311 $$ = node_assign(p, $1, $4, $3, &@$);
2313 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(assign,v1,v2);$$=v3;}
2315 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2318 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2320 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$4;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2322 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2325 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2327 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$7;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2329 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2332 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2334 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2336 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2339 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2341 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2343 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2346 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2348 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$6;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2350 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2353 YYLTYPE loc = code_loc_gen(&@1, &@3);
2354 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2356 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2358 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2361 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $5, $4, &@$);
2363 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=dispatch1(top_const_field,v1);v3=v2;v4=$3;v5=$5;v6=dispatch3(opassign,v3,v4,v5);$$=v6;}
2365 | backref tOP_ASGN lex_ctxt arg_rhs
2368 rb_backref_error(p, $1);
2369 $$ = NEW_BEGIN(0, &@$);
2371 {VALUE v1,v2,v3,v4;v1=var_field(p, $1);v2=$2;v3=$4;v4=dispatch3(opassign,v1,v2,v3);$$=backref_error(p, RNODE($1), v4);}ripper_error(p);
2378 $$ = NEW_DOT2($1, $3, &@$);
2380 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2387 $$ = NEW_DOT3($1, $3, &@$);
2389 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2395 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2397 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2403 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2405 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2411 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2413 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2419 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2421 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2425 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2429 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2433 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2437 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2441 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2445 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2447 | tUMINUS_NUM simple_numeric tPOW arg
2449 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2453 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2457 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2461 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2465 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2469 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2473 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2475 | rel_expr %prec tCMP
2478 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2482 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2486 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2490 $$ = match_op(p, $1, $3, &@2, &@$);
2494 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2498 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2502 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2506 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2510 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2514 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2518 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2520 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2522 p->ctxt.in_defined = 0;
2523 $$ = new_defined(p, $4, &@$);
2525 | arg '?' arg opt_nl ':' arg
2529 $$ = new_if(p, $1, $3, $6, &@$);
2532 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2534 | defn_head f_opt_paren_args '=' arg
2536 endless_method_name(p, $<node>1, &@1);
2537 restore_defun(p, $<node>1->nd_defn);
2539 $$ = set_defun_body(p, $1, $2, $4, &@$);
2541 {VALUE v1,v2,v3,v4;v1=get_value($1);v2=$2;v3=$4;v4=dispatch3(def,v1,v2,v3);$$=v4;}
2544 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2546 endless_method_name(p, $<node>1, &@1);
2547 restore_defun(p, $<node>1->nd_defn);
2549 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2550 $$ = set_defun_body(p, $1, $2, $4, &@$);
2552 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$4;v2=$6;v3=dispatch2(rescue_mod,v1,v2);v4=get_value($1);v5=$2;v6=v3;v7=dispatch3(def,v4,v5,v6);$$=v7;}
2555 | defs_head f_opt_paren_args '=' arg
2557 endless_method_name(p, $<node>1, &@1);
2558 restore_defun(p, $<node>1->nd_defn);
2560 $$ = set_defun_body(p, $1, $2, $4, &@$);
2564 {VALUE v1,v2,v3,v4,v5,v6;v1=AREF($1, 0);v2=AREF($1, 1);v3=AREF($1, 2);v4=$2;v5=$4;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
2567 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2569 endless_method_name(p, $<node>1, &@1);
2570 restore_defun(p, $<node>1->nd_defn);
2572 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2573 $$ = set_defun_body(p, $1, $2, $4, &@$);
2577 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=$4;v2=$6;v3=dispatch2(rescue_mod,v1,v2);v4=AREF($1, 0);v5=AREF($1, 1);v6=AREF($1, 2);v7=$2;v8=v3;v9=dispatch5(defs,v4,v5,v6,v7,v8);$$=v9;}
2586relop : '>' {$$ = '>';}
2592rel_expr : arg relop arg %prec '>'
2594 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2596 | rel_expr relop arg %prec '>'
2598 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2599 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2625 | args ',' assocs trailer
2628 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2630 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2635 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2637 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2641arg_rhs : arg %prec tOP_ASGN
2646 | arg modifier_rescue arg
2650 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2652 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2656paren_args : '(' opt_call_args rparen
2661 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2663 | '(' args ',' args_forward rparen
2665 if (!check_forwarding_args(p)) {
2670 $$ = new_args_forward_call(p, $2, &@4, &@$);
2672 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$4;v3=dispatch2(args_add,v1,v2);v4=v3;v5=dispatch1(arg_paren,v4);$$=v5;}
2675 | '(' args_forward rparen
2677 if (!check_forwarding_args(p)) {
2682 $$ = new_args_forward_call(p, 0, &@2, &@$);
2684 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2689opt_paren_args : none
2699 | args ',' assocs ','
2702 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2704 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2709 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2711 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2719 $$ = NEW_LIST($1, &@$);
2721 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2723 | args opt_block_arg
2726 $$ = arg_blk_pass($1, $2);
2728 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2730 | assocs opt_block_arg
2733 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2734 $$ = arg_blk_pass($$, $2);
2736 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);v7=v6;v8=$2;v9=dispatch2(args_add_block,v7,v8);$$=v9;}
2738 | args ',' assocs opt_block_arg
2741 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2742 $$ = arg_blk_pass($$, $4);
2744 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);v6=v5;v7=$4;v8=dispatch2(args_add_block,v6,v7);$$=v8;}
2747 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2751 /* If call_args starts with a open paren '(' or '[',
2752 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2753 * but the push must be done after CMDARG_PUSH(1).
2754 * So this code makes them consistent by first cancelling
2755 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2756 * and finally redoing CMDARG_PUSH(0).
2760 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2763 if (lookahead) CMDARG_POP();
2765 if (lookahead) CMDARG_PUSH(0);
2769 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2770 * but the push must be done after CMDARG_POP() in the parser.
2771 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2772 * CMDARG_POP() to pop 1 pushed by command_args,
2773 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2780 if (lookahead) CMDARG_POP();
2782 if (lookahead) CMDARG_PUSH(0);
2787block_arg : tAMPER arg_value
2790 $$ = NEW_BLOCK_PASS($2, &@$);
2796opt_block_arg : ',' block_arg
2809 $$ = NEW_LIST($1, &@$);
2811 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2816 $$ = NEW_SPLAT($2, &@$);
2818 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2820 | args ',' arg_value
2823 $$ = last_arg_append(p, $1, $3, &@$);
2825 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2827 | args ',' tSTAR arg_value
2830 $$ = rest_arg_append(p, $1, $4, &@$);
2832 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2840mrhs : args ',' arg_value
2843 $$ = last_arg_append(p, $1, $3, &@$);
2845 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$3;v5=dispatch2(mrhs_add,v3,v4);$$=v5;}
2847 | args ',' tSTAR arg_value
2850 $$ = rest_arg_append(p, $1, $4, &@$);
2852 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(mrhs_new_from_args,v1);v3=v2;v4=$4;v5=dispatch2(mrhs_add_star,v3,v4);$$=v5;}
2857 $$ = NEW_SPLAT($2, &@$);
2859 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2876 $$ = NEW_FCALL($1, 0, &@$);
2878 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);$$=v6;}
2889 set_line_body($3, @1.end_pos.lineno);
2890 $$ = NEW_BEGIN($3, &@$);
2891 nd_set_line($$, @1.end_pos.lineno);
2893 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2895 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2898 $$ = NEW_BEGIN(0, &@$);
2900 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2902 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2905 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2908 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2910 | tLPAREN compstmt ')'
2913 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2916 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2918 | primary_value tCOLON2 tCONSTANT
2921 $$ = NEW_COLON2($1, $3, &@$);
2923 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2928 $$ = NEW_COLON3($2, &@$);
2930 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2932 | tLBRACK aref_args ']'
2935 $$ = make_list($2, &@$);
2937 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2939 | tLBRACE assoc_list '}'
2942 $$ = new_hash(p, $2, &@$);
2943 $$->nd_brace = TRUE;
2945 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2950 $$ = NEW_RETURN(0, &@$);
2952 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2954 | keyword_yield '(' call_args rparen
2957 $$ = new_yield(p, $3, &@$);
2959 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2961 | keyword_yield '(' rparen
2964 $$ = NEW_YIELD(0, &@$);
2966 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2971 $$ = NEW_YIELD(0, &@$);
2973 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2975 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
2977 p->ctxt.in_defined = 0;
2978 $$ = new_defined(p, $5, &@$);
2980 | keyword_not '(' expr rparen
2982 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2984 | keyword_not '(' rparen
2986 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2991 $$ = method_add_block(p, $1, $2, &@$);
2993 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9;v1=$1;v2=dispatch1(fcall,v1);v3=dispatch0(args_new);v4=v2;v5=v3;v6=dispatch2(method_add_arg,v4,v5);v7=v6;v8=$2;v9=dispatch2(method_add_block,v7,v8);$$=v9;}
2996 | method_call brace_block
2999 block_dup_check(p, $1->nd_args, $2);
3000 $$ = method_add_block(p, $1, $2, &@$);
3002 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3005 | k_if expr_value then
3011 $$ = new_if(p, $2, $4, $5, &@$);
3014 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
3016 | k_unless expr_value then
3022 $$ = new_unless(p, $2, $4, $5, &@$);
3025 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
3027 | k_while expr_value_do
3032 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3035 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
3037 | k_until expr_value_do
3042 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3045 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
3047 | k_case expr_value opt_terms
3049 $<val>$ = p->case_labels;
3050 p->case_labels = Qnil;
3055 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3056 p->case_labels = $<val>4;
3058 $$ = NEW_CASE($2, $5, &@$);
3061 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
3065 $<val>$ = p->case_labels;
3071 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3072 p->case_labels = $<val>3;
3074 $$ = NEW_CASE2($4, &@$);
3076 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
3078 | k_case expr_value opt_terms
3083 $$ = NEW_CASE3($2, $4, &@$);
3085 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
3087 | k_for for_var keyword_in expr_value_do
3095 * e.each{|*x| a, b, c = x}
3099 * e.each{|x| a, = x}
3101 ID id = internal_id(p);
3102 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3103 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3104 ID *tbl = ALLOC_N(ID, 3);
3105 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
3106 rb_ast_add_local_table(p->ast, tbl);
3108 switch (nd_type($2)) {
3111 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
3112 $2->nd_value = internal_var;
3117 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3118 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3120 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3121 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3123 /* {|*internal_id| <m> = internal_id; ... } */
3124 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3125 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3126 $$ = NEW_FOR($4, scope, &@$);
3129 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
3131 | k_class cpath superclass
3133 if (p->ctxt.in_def) {
3134 YYLTYPE loc = code_loc_gen(&@1, &@2);
3135 yyerror1(&loc, "class definition in method body");
3137 p->ctxt.in_class = 1;
3144 $$ = NEW_CLASS($2, $5, $3, &@$);
3145 nd_set_line($$->nd_body, @6.end_pos.lineno);
3146 set_line_body($5, @3.end_pos.lineno);
3147 nd_set_line($$, @3.end_pos.lineno);
3149 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
3151 p->ctxt.in_class = $<ctxt>1.in_class;
3152 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3154 | k_class tLSHFT expr
3157 p->ctxt.in_class = 0;
3165 $$ = NEW_SCLASS($3, $6, &@$);
3166 nd_set_line($$->nd_body, @7.end_pos.lineno);
3167 set_line_body($6, nd_line($3));
3170 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
3172 p->ctxt.in_def = $<ctxt>1.in_def;
3173 p->ctxt.in_class = $<ctxt>1.in_class;
3174 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3178 if (p->ctxt.in_def) {
3179 YYLTYPE loc = code_loc_gen(&@1, &@2);
3180 yyerror1(&loc, "module definition in method body");
3182 p->ctxt.in_class = 1;
3189 $$ = NEW_MODULE($2, $4, &@$);
3190 nd_set_line($$->nd_body, @5.end_pos.lineno);
3191 set_line_body($4, @2.end_pos.lineno);
3192 nd_set_line($$, @2.end_pos.lineno);
3194 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
3196 p->ctxt.in_class = $<ctxt>1.in_class;
3197 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3204 restore_defun(p, $<node>1->nd_defn);
3206 $$ = set_defun_body(p, $1, $2, $3, &@$);
3208 {VALUE v1,v2,v3,v4;v1=get_value($1);v2=$2;v3=$3;v4=dispatch3(def,v1,v2,v3);$$=v4;}
3216 restore_defun(p, $<node>1->nd_defn);
3218 $$ = set_defun_body(p, $1, $2, $3, &@$);
3222 {VALUE v1,v2,v3,v4,v5,v6;v1=AREF($1, 0);v2=AREF($1, 1);v3=AREF($1, 2);v4=$2;v5=$3;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
3228 $$ = NEW_BREAK(0, &@$);
3230 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3235 $$ = NEW_NEXT(0, &@$);
3237 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3244 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3249 $$ = NEW_RETRY(&@$);
3251 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3255primary_value : primary
3262k_begin : keyword_begin
3264 token_info_push(p, "begin", &@$);
3271 token_info_push(p, "if", &@$);
3272 if (p->token_info && p->token_info->nonspc &&
3273 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3274 const char *tok = p->lex.ptok;
3275 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3276 beg += rb_strlen_lit("else");
3277 while (beg < tok && ISSPACE(*beg)) beg++;
3279 p->token_info->nonspc = 0;
3285k_unless : keyword_unless
3287 token_info_push(p, "unless", &@$);
3291k_while : keyword_while
3293 token_info_push(p, "while", &@$);
3297k_until : keyword_until
3299 token_info_push(p, "until", &@$);
3303k_case : keyword_case
3305 token_info_push(p, "case", &@$);
3311 token_info_push(p, "for", &@$);
3315k_class : keyword_class
3317 token_info_push(p, "class", &@$);
3322k_module : keyword_module
3324 token_info_push(p, "module", &@$);
3331 token_info_push(p, "def", &@$);
3337 token_info_push(p, "do", &@$);
3341k_do_block : keyword_do_block
3343 token_info_push(p, "do", &@$);
3347k_rescue : keyword_rescue
3349 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3353k_ensure : keyword_ensure
3355 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3359k_when : keyword_when
3361 token_info_warn(p, "when", p->token_info, 0, &@$);
3365k_else : keyword_else
3367 token_info *ptinfo_beg = p->token_info;
3368 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3369 token_info_warn(p, "else", p->token_info, same, &@$);
3372 e.next = ptinfo_beg->next;
3374 token_info_setup(&e, p->lex.pbeg, &@$);
3375 if (!e.nonspc) *ptinfo_beg = e;
3380k_elsif : keyword_elsif
3383 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3389 token_info_pop(p, "end", &@$);
3393k_return : keyword_return
3395 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3396 yyerror1(&@1, "Invalid return in class/module body");
3410 | k_elsif expr_value then
3415 $$ = new_if(p, $2, $4, $5, &@$);
3418 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3428 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3439 $$ = assignable(p, $1, 0, &@$);
3440 mark_lvar_used(p, $$);
3442 $$=assignable(p, $1);
3444 | tLPAREN f_margs rparen
3449 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3456 $$ = NEW_LIST($1, &@$);
3458 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3460 | f_marg_list ',' f_marg
3463 $$ = list_append(p, $1, $3);
3465 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3469f_margs : f_marg_list
3472 $$ = NEW_MASGN($1, 0, &@$);
3476 | f_marg_list ',' f_rest_marg
3479 $$ = NEW_MASGN($1, $3, &@$);
3481 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3483 | f_marg_list ',' f_rest_marg ',' f_marg_list
3486 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3488 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
3493 $$ = NEW_MASGN(0, $1, &@$);
3495 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3497 | f_rest_marg ',' f_marg_list
3500 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3502 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
3506f_rest_marg : tSTAR f_norm_arg
3509 $$ = assignable(p, $2, 0, &@$);
3510 mark_lvar_used(p, $$);
3512 $$=assignable(p, $2);
3517 $$ = NODE_SPECIAL_NO_NAME_REST;
3523f_any_kwrest : f_kwrest
3524 | f_no_kwarg {$$ = ID2VAL(idNil);}
3527block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3529 $$ = new_args_tail(p, $1, $3, $4, &@3);
3531 | f_block_kwarg opt_f_block_arg
3533 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3535 | f_any_kwrest opt_f_block_arg
3537 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3541 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3545opt_block_args_tail : ',' block_args_tail
3551 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3557 /* magic number for rest_id in iseq_set_arguments() */
3559 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3561 {VALUE v1;v1=dispatch0(excessed_comma);$$=v1;}
3565block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3567 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3569 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3571 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3573 | f_arg ',' f_block_optarg opt_block_args_tail
3575 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3577 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3579 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3581 | f_arg ',' f_rest_arg opt_block_args_tail
3583 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3585 | f_arg excessed_comma
3587 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3588 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3590 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3592 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3594 | f_arg opt_block_args_tail
3596 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3598 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3600 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3602 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3604 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3606 | f_block_optarg opt_block_args_tail
3608 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3610 | f_block_optarg ',' f_arg opt_block_args_tail
3612 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3614 | f_rest_arg opt_block_args_tail
3616 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3618 | f_rest_arg ',' f_arg opt_block_args_tail
3620 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3624 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3628opt_block_param : none
3631 p->command_start = TRUE;
3635block_param_def : '|' opt_bv_decl '|'
3638 p->max_numparam = ORDINAL_PARAM;
3642 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11;v1=Qnil;v2=Qnil;v3=Qnil;v4=Qnil;v5=Qnil;v6=Qnil;v7=Qnil;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=escape_Qundef($2);v11=dispatch2(block_var,v9,v10);$$=v11;}
3644 | '|' block_param opt_bv_decl '|'
3647 p->max_numparam = ORDINAL_PARAM;
3651 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3660 | opt_nl ';' bv_decls opt_nl
3670 {$$=rb_ary_new3(1, get_value($1));}
3672 {$$=rb_ary_push($1, get_value($3));}
3677 new_bv(p, get_id($1));
3688 token_info_push(p, "->", &@1);
3689 $<vars>1 = dyna_push(p);
3690 $<num>$ = p->lex.lpar_beg;
3691 p->lex.lpar_beg = p->lex.paren_nest;
3694 $<num>$ = p->max_numparam;
3695 p->max_numparam = 0;
3698 $<node>$ = numparam_push(p);
3706 int max_numparam = p->max_numparam;
3707 p->lex.lpar_beg = $<num>2;
3708 p->max_numparam = $<num>3;
3710 $5 = args_with_numbered(p, $5, max_numparam);
3713 YYLTYPE loc = code_loc_gen(&@5, &@7);
3714 $$ = NEW_LAMBDA($5, $7, &loc);
3715 nd_set_line($$->nd_body, @7.end_pos.lineno);
3716 nd_set_line($$, @5.end_pos.lineno);
3717 nd_set_first_loc($$, @1.beg_pos);
3720 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3721 numparam_pop(p, $<node>4);
3722 dyna_pop(p, $<vars>1);
3726f_larglist : '(' f_args opt_bv_decl ')'
3730 p->max_numparam = ORDINAL_PARAM;
3732 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3737 if (!args_info_empty_p($1->nd_ainfo))
3738 p->max_numparam = ORDINAL_PARAM;
3744lambda_body : tLAMBEG compstmt '}'
3746 token_info_pop(p, "}", &@3);
3749 | keyword_do_LAMBDA bodystmt k_end
3755do_block : k_do_block do_body k_end
3759 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3760 nd_set_line($$, @1.end_pos.lineno);
3765block_call : command do_block
3768 if (nd_type($1) == NODE_YIELD) {
3769 compile_error(p, "block given to yield");
3772 block_dup_check(p, $1->nd_args, $2);
3774 $$ = method_add_block(p, $1, $2, &@$);
3777 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3779 | block_call call_op2 operation2 opt_paren_args
3782 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3784 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3786 | block_call call_op2 operation2 opt_paren_args brace_block
3789 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3791 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=v7==Qundef ? v6 : dispatch2(method_add_block,v6,v7);$$=v8;}
3793 | block_call call_op2 operation2 command_args do_block
3796 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3798 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
3802method_call : fcall paren_args
3807 nd_set_last_loc($1, @2.end_pos);
3809 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3811 | primary_value call_op operation2 opt_paren_args
3814 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3815 nd_set_line($$, @3.end_pos.lineno);
3817 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=v6==Qundef ? v5 : dispatch2(method_add_arg,v5,v6);$$=v7;}
3819 | primary_value tCOLON2 operation2 paren_args
3822 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3823 nd_set_line($$, @3.end_pos.lineno);
3825 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3827 | primary_value tCOLON2 operation3
3830 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3832 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3834 | primary_value call_op paren_args
3837 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3838 nd_set_line($$, @2.end_pos.lineno);
3840 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3842 | primary_value tCOLON2 paren_args
3845 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3846 nd_set_line($$, @2.end_pos.lineno);
3848 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=ID2VAL(idCOLON2);v3=ID2VAL(idCall);v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$3;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
3850 | keyword_super paren_args
3853 $$ = NEW_SUPER($2, &@$);
3855 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3860 $$ = NEW_ZSUPER(&@$);
3862 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3864 | primary_value '[' opt_call_args rbracket
3867 if ($1 && nd_type($1) == NODE_SELF)
3868 $$ = NEW_FCALL(tAREF, $3, &@$);
3870 $$ = NEW_CALL($1, tAREF, $3, &@$);
3873 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3877brace_block : '{' brace_body '}'
3881 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3882 nd_set_line($$, @1.end_pos.lineno);
3885 | k_do do_body k_end
3889 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3890 nd_set_line($$, @1.end_pos.lineno);
3895brace_body : {$<vars>$ = dyna_push(p);}
3897 $<num>$ = p->max_numparam;
3898 p->max_numparam = 0;
3901 $<node>$ = numparam_push(p);
3903 opt_block_param compstmt
3905 int max_numparam = p->max_numparam;
3906 p->max_numparam = $<num>2;
3907 $4 = args_with_numbered(p, $4, max_numparam);
3909 $$ = NEW_ITER($4, $5, &@$);
3911 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3912 numparam_pop(p, $<node>3);
3913 dyna_pop(p, $<vars>1);
3917do_body : {$<vars>$ = dyna_push(p);}
3919 $<num>$ = p->max_numparam;
3920 p->max_numparam = 0;
3923 $<node>$ = numparam_push(p);
3926 opt_block_param bodystmt
3928 int max_numparam = p->max_numparam;
3929 p->max_numparam = $<num>2;
3930 $4 = args_with_numbered(p, $4, max_numparam);
3932 $$ = NEW_ITER($4, $5, &@$);
3934 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3936 numparam_pop(p, $<node>3);
3937 dyna_pop(p, $<vars>1);
3941case_args : arg_value
3944 check_literal_when(p, $1, &@1);
3945 $$ = NEW_LIST($1, &@$);
3947 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3952 $$ = NEW_SPLAT($2, &@$);
3954 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3956 | case_args ',' arg_value
3959 check_literal_when(p, $3, &@3);
3960 $$ = last_arg_append(p, $1, $3, &@$);
3962 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3964 | case_args ',' tSTAR arg_value
3967 $$ = rest_arg_append(p, $1, $4, &@$);
3969 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3973case_body : k_when case_args then
3978 $$ = NEW_WHEN($2, $4, $5, &@$);
3981 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3989p_case_body : keyword_in
3991 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3992 p->command_start = FALSE;
3994 p->ctxt.in_kwarg = 1;
3995 $<tbl>$ = push_pvtbl(p);
3998 $<tbl>$ = push_pktbl(p);
4002 pop_pktbl(p, $<tbl>3);
4003 pop_pvtbl(p, $<tbl>2);
4004 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4010 $$ = NEW_IN($4, $7, $8, &@$);
4012 {VALUE v1,v2,v3,v4;v1=$4;v2=$7;v3=escape_Qundef($8);v4=dispatch3(in,v1,v2,v3);$$=v4;}
4020p_top_expr : p_top_expr_body
4021 | p_top_expr_body modifier_if expr_value
4024 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
4027 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
4029 | p_top_expr_body modifier_unless expr_value
4032 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
4035 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
4039p_top_expr_body : p_expr
4042 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4043 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4047 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4049 nd_set_first_loc($$, @1.beg_pos);
4055 $$ = new_find_pattern(p, Qnone, $1, &@$);
4059 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4063 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4070p_as : p_expr tASSOC p_variable
4073 NODE *n = NEW_LIST($1, &@$);
4074 n = list_append(p, n, $3);
4075 $$ = new_hash(p, n, &@$);
4077 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
4082p_alt : p_alt '|' p_expr_basic
4085 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4087 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
4092p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4093p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4095p_expr_basic : p_value
4096 | p_const p_lparen p_args rparen
4098 pop_pktbl(p, $<tbl>2);
4099 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4101 nd_set_first_loc($$, @1.beg_pos);
4105 | p_const p_lparen p_find rparen
4107 pop_pktbl(p, $<tbl>2);
4108 $$ = new_find_pattern(p, $1, $3, &@$);
4110 nd_set_first_loc($$, @1.beg_pos);
4114 | p_const p_lparen p_kwargs rparen
4116 pop_pktbl(p, $<tbl>2);
4117 $$ = new_hash_pattern(p, $1, $3, &@$);
4119 nd_set_first_loc($$, @1.beg_pos);
4123 | p_const '(' rparen
4125 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4126 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4128 | p_const p_lbracket p_args rbracket
4130 pop_pktbl(p, $<tbl>2);
4131 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4133 nd_set_first_loc($$, @1.beg_pos);
4137 | p_const p_lbracket p_find rbracket
4139 pop_pktbl(p, $<tbl>2);
4140 $$ = new_find_pattern(p, $1, $3, &@$);
4142 nd_set_first_loc($$, @1.beg_pos);
4146 | p_const p_lbracket p_kwargs rbracket
4148 pop_pktbl(p, $<tbl>2);
4149 $$ = new_hash_pattern(p, $1, $3, &@$);
4151 nd_set_first_loc($$, @1.beg_pos);
4155 | p_const '[' rbracket
4157 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4158 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4160 | tLBRACK p_args rbracket
4162 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4164 | tLBRACK p_find rbracket
4166 $$ = new_find_pattern(p, Qnone, $2, &@$);
4170 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4171 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4175 $<tbl>$ = push_pktbl(p);
4177 p->ctxt.in_kwarg = 0;
4181 pop_pktbl(p, $<tbl>2);
4182 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4183 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4187 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4188 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4190 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4192 pop_pktbl(p, $<tbl>2);
4200 NODE *pre_args = NEW_LIST($1, &@$);
4201 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4203 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4208 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4213 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4215 VALUE pre_args = rb_ary_concat($1, get_value($2));
4216 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4219 | p_args_head tSTAR tIDENTIFIER
4221 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4223 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4225 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4229 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4231 | p_args_head tSTAR ',' p_args_post
4233 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4238p_args_head : p_arg ','
4242 | p_args_head p_arg ','
4245 $$ = list_concat($1, $2);
4247 $$=rb_ary_concat($1, get_value($2));
4253 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4255 | p_rest ',' p_args_post
4257 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4261p_find : p_rest ',' p_args_post ',' p_rest
4263 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4265 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
4266 rb_warn0L_experimental(nd_line($$), "Find pattern is experimental, and the behavior may change in future versions of Ruby!");
4271p_rest : tSTAR tIDENTIFIER
4282 | p_args_post ',' p_arg
4285 $$ = list_concat($1, $3);
4287 $$=rb_ary_concat($1, get_value($3));
4294 $$ = NEW_LIST($1, &@$);
4296 $$=rb_ary_new_from_args(1, get_value($1));
4300p_kwargs : p_kwarg ',' p_any_kwrest
4302 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4306 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4310 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4314 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4319 {$$=rb_ary_new_from_args(1, $1);}
4323 $$ = list_concat($1, $3);
4325 $$=rb_ary_push($1, $3);
4329p_kw : p_kw_label p_expr
4331 error_duplicate_pattern_key(p, get_id($1), &@1);
4333 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4335 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4339 error_duplicate_pattern_key(p, get_id($1), &@1);
4340 if ($1 && !is_local_id(get_id($1))) {
4341 yyerror1(&@1, "key must be valid as local variables");
4343 error_duplicate_pattern_variable(p, get_id($1), &@1);
4345 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4347 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4352 | tSTRING_BEG string_contents tLABEL_END
4354 YYLTYPE loc = code_loc_gen(&@1, &@3);
4356 if (!$2 || nd_type($2) == NODE_STR) {
4357 NODE *node = dsym_node(p, $2, &loc);
4358 $$ = SYM2ID(node->nd_lit);
4361 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4362 VALUE label = RNODE($2)->nd_cval;
4363 VALUE rval = RNODE($2)->nd_rval;
4364 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4365 RNODE($$)->nd_loc = loc;
4369 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4375p_kwrest : kwrest_mark tIDENTIFIER
4385p_kwnorest : kwrest_mark keyword_nil
4391p_any_kwrest : p_kwrest
4392 | p_kwnorest {$$ = ID2VAL(idNil);}
4395p_value : p_primitive
4396 | p_primitive tDOT2 p_primitive
4401 $$ = NEW_DOT2($1, $3, &@$);
4403 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4405 | p_primitive tDOT3 p_primitive
4410 $$ = NEW_DOT3($1, $3, &@$);
4412 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4418 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4420 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4426 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4428 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4433 | tBDOT2 p_primitive
4437 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4439 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4441 | tBDOT3 p_primitive
4445 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4447 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4451p_primitive : literal
4462 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4464 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4469p_variable : tIDENTIFIER
4472 error_duplicate_pattern_variable(p, $1, &@1);
4473 $$ = assignable(p, $1, 0, &@$);
4475 $$=assignable(p, var_field(p, $1));
4479p_var_ref : '^' tIDENTIFIER
4482 NODE *n = gettable(p, $2, &@$);
4483 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4484 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4488 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4492p_const : tCOLON3 cname
4495 $$ = NEW_COLON3($2, &@$);
4497 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4499 | p_const tCOLON2 cname
4502 $$ = NEW_COLON2($1, $3, &@$);
4504 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4509 $$ = gettable(p, $1, &@$);
4511 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4515opt_rescue : k_rescue exc_list exc_var then
4520 $$ = NEW_RESBODY($2,
4521 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4523 fixpos($$, $2?$2:$5);
4525 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(rescue,v1,v2,v3,v4);$$=v5;}
4533 $$ = NEW_LIST($1, &@$);
4535 $$=rb_ary_new3(1, get_value($1));
4540 if (!($$ = splat_array($1))) $$ = $1;
4554opt_ensure : k_ensure compstmt
4559 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4573 node = NEW_STR(STR_NEW0(), &@$);
4574 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4577 node = evstr2dstr(p, node);
4590 $$ = literal_concat(p, $1, $2, &@$);
4592 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4596string1 : tSTRING_BEG string_contents tSTRING_END
4599 $$ = heredoc_dedent(p, $2);
4600 if ($$) nd_set_loc($$, &@$);
4602 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4606xstring : tXSTRING_BEG xstring_contents tSTRING_END
4609 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4611 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4615regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4617 $$ = new_regexp(p, $2, $3, &@$);
4621words : tWORDS_BEG ' ' word_list tSTRING_END
4624 $$ = make_list($3, &@$);
4626 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4630word_list : /* none */
4635 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4637 | word_list word ' '
4640 $$ = list_append(p, $1, evstr2dstr(p, $2));
4642 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4646word : string_content
4647 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4648 | word string_content
4651 $$ = literal_concat(p, $1, $2, &@$);
4653 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4657symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4660 $$ = make_list($3, &@$);
4662 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4666symbol_list : /* none */
4671 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4673 | symbol_list word ' '
4676 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4678 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4682qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4685 $$ = make_list($3, &@$);
4687 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4691qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4694 $$ = make_list($3, &@$);
4696 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4700qword_list : /* none */
4705 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4707 | qword_list tSTRING_CONTENT ' '
4710 $$ = list_append(p, $1, $2);
4712 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4716qsym_list : /* none */
4721 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4723 | qsym_list tSTRING_CONTENT ' '
4726 $$ = symbol_append(p, $1, $2);
4728 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4732string_contents : /* none */
4737 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4740 $$ = ripper_new_yylval(p, 0, $$, 0);
4743 | string_contents string_content
4746 $$ = literal_concat(p, $1, $2, &@$);
4748 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4751 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4752 !RNODE($1)->nd_cval) {
4753 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4754 RNODE($1)->nd_rval = add_mark_object(p, $$);
4761xstring_contents: /* none */
4766 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4768 | xstring_contents string_content
4771 $$ = literal_concat(p, $1, $2, &@$);
4773 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4777regexp_contents: /* none */
4782 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4785 $$ = ripper_new_yylval(p, 0, $$, 0);
4788 | regexp_contents string_content
4791 NODE *head = $1, *tail = $2;
4799 switch (nd_type(head)) {
4801 nd_set_type(head, NODE_DSTR);
4806 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4809 $$ = list_append(p, head, tail);
4812 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4813 if (ripper_is_node_yylval(n1)) {
4814 s1 = RNODE(n1)->nd_cval;
4815 n1 = RNODE(n1)->nd_rval;
4817 if (ripper_is_node_yylval(n2)) {
4818 s2 = RNODE(n2)->nd_cval;
4819 n2 = RNODE(n2)->nd_rval;
4821 $$ = dispatch2(regexp_add, n1, n2);
4823 $$ = ripper_new_yylval(p, 0, $$, s2);
4829string_content : tSTRING_CONTENT
4830 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4833 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4834 $<strterm>$ = p->lex.strterm;
4836 SET_LEX_STATE(EXPR_BEG);
4840 p->lex.strterm = $<strterm>2;
4842 $$ = NEW_EVSTR($3, &@$);
4843 nd_set_line($$, @3.end_pos.lineno);
4845 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4853 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4854 $<strterm>$ = p->lex.strterm;
4858 $<num>$ = p->lex.state;
4859 SET_LEX_STATE(EXPR_BEG);
4862 $<num>$ = p->lex.brace_nest;
4863 p->lex.brace_nest = 0;
4866 $<num>$ = p->heredoc_indent;
4867 p->heredoc_indent = 0;
4869 compstmt tSTRING_DEND
4873 p->lex.strterm = $<strterm>3;
4874 SET_LEX_STATE($<num>4);
4875 p->lex.brace_nest = $<num>5;
4876 p->heredoc_indent = $<num>6;
4877 p->heredoc_line_indent = -1;
4879 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4880 $$ = new_evstr(p, $7, &@$);
4882 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4889 $$ = NEW_GVAR($1, &@$);
4891 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4896 $$ = NEW_IVAR($1, &@$);
4898 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4903 $$ = NEW_CVAR($1, &@$);
4905 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4916 SET_LEX_STATE(EXPR_END);
4918 $$ = NEW_LIT(ID2SYM($2), &@$);
4920 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4930dsym : tSYMBEG string_contents tSTRING_END
4932 SET_LEX_STATE(EXPR_END);
4934 $$ = dsym_node(p, $2, &@$);
4936 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4940numeric : simple_numeric
4941 | tUMINUS_NUM simple_numeric %prec tLOWEST
4945 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4947 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4951simple_numeric : tINTEGER
4957user_variable : tIDENTIFIER
4964keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4965 | keyword_self {$$ = KWD2EID(self, $1);}
4966 | keyword_true {$$ = KWD2EID(true, $1);}
4967 | keyword_false {$$ = KWD2EID(false, $1);}
4968 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4969 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4970 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4973var_ref : user_variable
4976 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4978 if (id_is_var(p, get_id($1))) {
4979 $$ = dispatch1(var_ref, $1);
4982 $$ = dispatch1(vcall, $1);
4989 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4991 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4995var_lhs : user_variable
4998 $$ = assignable(p, $1, 0, &@$);
5000 $$=assignable(p, var_field(p, $1));
5005 $$ = assignable(p, $1, 0, &@$);
5007 $$=assignable(p, var_field(p, $1));
5017 SET_LEX_STATE(EXPR_BEG);
5018 p->command_start = TRUE;
5033f_opt_paren_args: f_paren_args | none;
5035f_paren_args : '(' f_args rparen
5040 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
5041 SET_LEX_STATE(EXPR_BEG);
5042 p->command_start = TRUE;
5044 | '(' f_arg ',' args_forward rparen
5046 add_forwarding_args(p);
5048 $$ = new_args_forward_def(p, $2, &@$);
5050 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10;v1=$2;v2=Qnone;v3=$4;v4=Qnone;v5=Qnone;v6=Qnone;v7=Qnone;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=dispatch1(paren,v9);$$=v10;}
5051 SET_LEX_STATE(EXPR_BEG);
5052 p->command_start = TRUE;
5054 | '(' args_forward rparen
5056 add_forwarding_args(p);
5058 $$ = new_args_forward_def(p, 0, &@$);
5060 {VALUE v1,v2,v3,v4,v5,v6,v7,v8,v9,v10;v1=Qnone;v2=Qnone;v3=$2;v4=Qnone;v5=Qnone;v6=Qnone;v7=Qnone;v8=dispatch7(params,v1,v2,v3,v4,v5,v6,v7);v9=v8;v10=dispatch1(paren,v9);$$=v10;}
5061 SET_LEX_STATE(EXPR_BEG);
5062 p->command_start = TRUE;
5066f_arglist : f_paren_args
5069 p->ctxt.in_kwarg = 1;
5070 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5074 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5076 SET_LEX_STATE(EXPR_BEG);
5077 p->command_start = TRUE;
5081args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5083 $$ = new_args_tail(p, $1, $3, $4, &@3);
5085 | f_kwarg opt_f_block_arg
5087 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5089 | f_any_kwrest opt_f_block_arg
5091 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5095 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5099opt_args_tail : ',' args_tail
5105 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5109f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5111 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5113 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5115 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5117 | f_arg ',' f_optarg opt_args_tail
5119 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5121 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5123 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5125 | f_arg ',' f_rest_arg opt_args_tail
5127 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5129 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5131 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5133 | f_arg opt_args_tail
5135 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5137 | f_optarg ',' f_rest_arg opt_args_tail
5139 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5141 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5143 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5145 | f_optarg opt_args_tail
5147 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5149 | f_optarg ',' f_arg opt_args_tail
5151 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5153 | f_rest_arg opt_args_tail
5155 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5157 | f_rest_arg ',' f_arg opt_args_tail
5159 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5163 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5167 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5168 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5172args_forward : tBDOT3
5177 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
5181f_bad_arg : tCONSTANT
5183 static const char mesg[] = "formal argument cannot be a constant";
5185 yyerror1(&@1, mesg);
5188 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5192 static const char mesg[] = "formal argument cannot be an instance variable";
5194 yyerror1(&@1, mesg);
5197 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5201 static const char mesg[] = "formal argument cannot be a global variable";
5203 yyerror1(&@1, mesg);
5206 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5210 static const char mesg[] = "formal argument cannot be a class variable";
5212 yyerror1(&@1, mesg);
5215 {VALUE v1,v2,v3;v1=ERR_MESG();v2=$1;v3=dispatch2(param_error,v1,v2);$$=v3;}ripper_error(p);
5219f_norm_arg : f_bad_arg
5222 formal_argument(p, $1);
5223 p->max_numparam = ORDINAL_PARAM;
5228f_arg_asgn : f_norm_arg
5237f_arg_item : f_arg_asgn
5241 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5245 | tLPAREN f_margs rparen
5248 ID tid = internal_id(p);
5250 loc.beg_pos = @2.beg_pos;
5251 loc.end_pos = @2.beg_pos;
5253 if (dyna_in_block(p)) {
5254 $2->nd_value = NEW_DVAR(tid, &loc);
5257 $2->nd_value = NEW_LVAR(tid, &loc);
5259 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5262 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5267 {$$=rb_ary_new3(1, get_value($1));}
5268 | f_arg ',' f_arg_item
5273 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5274 rb_discard_node(p, $3);
5276 $$=rb_ary_push($1, get_value($3));
5283 arg_var(p, formal_argument(p, $1));
5284 p->cur_arg = get_id($1);
5285 p->max_numparam = ORDINAL_PARAM;
5290f_kw : f_label arg_value
5294 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5296 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5302 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5304 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5308f_block_kw : f_label primary_value
5311 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5313 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5318 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5320 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5324f_block_kwarg : f_block_kw
5329 $$=rb_ary_new3(1, get_value($1));
5331 | f_block_kwarg ',' f_block_kw
5334 $$ = kwd_append($1, $3);
5336 $$=rb_ary_push($1, get_value($3));
5346 $$=rb_ary_new3(1, get_value($1));
5351 $$ = kwd_append($1, $3);
5353 $$=rb_ary_push($1, get_value($3));
5361f_no_kwarg : kwrest_mark keyword_nil
5365 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5369f_kwrest : kwrest_mark tIDENTIFIER
5371 arg_var(p, shadowing_lvar(p, get_id($2)));
5375 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5380 $$ = internal_id(p);
5383 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5387f_opt : f_arg_asgn '=' arg_value
5391 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5393 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5397f_block_opt : f_arg_asgn '=' primary_value
5401 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5403 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5407f_block_optarg : f_block_opt
5412 $$=rb_ary_new3(1, get_value($1));
5414 | f_block_optarg ',' f_block_opt
5417 $$ = opt_arg_append($1, $3);
5419 $$=rb_ary_push($1, get_value($3));
5428 $$=rb_ary_new3(1, get_value($1));
5430 | f_optarg ',' f_opt
5433 $$ = opt_arg_append($1, $3);
5435 $$=rb_ary_push($1, get_value($3));
5443f_rest_arg : restarg_mark tIDENTIFIER
5445 arg_var(p, shadowing_lvar(p, get_id($2)));
5449 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5454 $$ = internal_id(p);
5457 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5465f_block_arg : blkarg_mark tIDENTIFIER
5467 arg_var(p, shadowing_lvar(p, get_id($2)));
5471 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5475opt_f_block_arg : ',' f_block_arg
5490 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5493 switch (nd_type($3)) {
5502 yyerror1(&@3, "can't define singleton method for literals");
5510 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5520 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5525 {$$=rb_ary_new3(1, get_value($1));}
5535 if (assocs->nd_head &&
5536 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5537 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5539 tail = tail->nd_next->nd_head->nd_head;
5541 assocs = list_concat(assocs, tail);
5545 $$=rb_ary_push($1, get_value($3));
5549assoc : arg_value tASSOC arg_value
5552 if (nd_type($1) == NODE_STR) {
5553 nd_set_type($1, NODE_LIT);
5554 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5556 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5558 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5563 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5565 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5567 | tSTRING_BEG string_contents tLABEL_END arg_value
5570 YYLTYPE loc = code_loc_gen(&@1, &@3);
5571 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5573 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5578 if (nd_type($2) == NODE_HASH &&
5579 !($2->nd_head && $2->nd_head->nd_alen)) {
5580 static VALUE empty_hash;
5582 empty_hash = rb_obj_freeze(rb_hash_new());
5583 rb_gc_register_mark_object(empty_hash);
5585 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5588 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5590 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5594operation : tIDENTIFIER
5599operation2 : tIDENTIFIER
5605operation3 : tIDENTIFIER
5622opt_terms : /* none */
5633rbracket : opt_nl ']'
5644term : ';' {yyerrok;token_flush(p);}
5645 | '\n' {token_flush(p);}
5649 | terms ';' {yyerrok;}
5661# define yylval (*p->lval)
5663static int regx_options(struct parser_params*);
5664static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5665static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5666static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5667static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5670# define set_yylval_node(x) { \
5672 rb_parser_set_location(p, &_cur_loc); \
5673 yylval.node = (x); \
5675# define set_yylval_str(x) \
5677 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5678 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5680# define set_yylval_literal(x) \
5682 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5683 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5685# define set_yylval_num(x) (yylval.num = (x))
5686# define set_yylval_id(x) (yylval.id = (x))
5687# define set_yylval_name(x) (yylval.id = (x))
5688# define yylval_id() (yylval.id)
5691ripper_yylval_id(struct parser_params *p, ID x)
5693 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5695# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5696# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5697# define set_yylval_id(x) (void)(x)
5698# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5699# define set_yylval_literal(x) add_mark_object(p, (x))
5700# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
5701# define yylval_id() yylval.id
5702# define _cur_loc NULL_LOC /* dummy */
5705#define set_yylval_noname() set_yylval_id(keyword_nil)
5708#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5709#define dispatch_scan_event(p, t) ((void)0)
5710#define dispatch_delayed_token(p, t) ((void)0)
5711#define has_delayed_token(p) (0)
5713#define literal_flush(p, ptr) ((void)(ptr))
5715#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5718intern_sym(const char *name)
5720 ID id = rb_intern_const(name);
5725ripper_has_scan_event(struct parser_params *p)
5727 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5728 return p->lex.pcur > p->lex.ptok;
5732ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5734 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5735 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5741ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5743 if (!ripper_has_scan_event(p)) return;
5744 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5746#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5749ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5751 int saved_line = p->ruby_sourceline;
5752 const char *saved_tokp = p->lex.ptok;
5754 if (NIL_P(p->delayed.token)) return;
5755 p->ruby_sourceline = p->delayed.line;
5756 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5757 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5758 p->delayed.token = Qnil;
5759 p->ruby_sourceline = saved_line;
5760 p->lex.ptok = saved_tokp;
5762#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5763#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5767is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5769 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5773parser_is_identchar(struct parser_params *p)
5775 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5779parser_isascii(struct parser_params *p)
5781 return ISASCII(*(p->lex.pcur-1));
5785token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5787 int column = 1, nonspc = 0, i;
5788 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5790 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5793 if (*ptr != ' ' && *ptr != '\t') {
5798 ptinfo->beg = loc->beg_pos;
5799 ptinfo->indent = column;
5800 ptinfo->nonspc = nonspc;
5804token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5808 if (!p->token_info_enabled) return;
5809 ptinfo = ALLOC(token_info);
5810 ptinfo->token = token;
5811 ptinfo->next = p->token_info;
5812 token_info_setup(ptinfo, p->lex.pbeg, loc);
5814 p->token_info = ptinfo;
5818token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5820 token_info *ptinfo_beg = p->token_info;
5822 if (!ptinfo_beg) return;
5823 p->token_info = ptinfo_beg->next;
5825 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5826 token_info_warn(p, token, ptinfo_beg, 1, loc);
5827 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5831token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
5833 token_info *ptinfo_beg = p->token_info;
5835 if (!ptinfo_beg) return;
5836 p->token_info = ptinfo_beg->next;
5838 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
5839 ptinfo_beg->beg.column != beg_pos.column ||
5840 strcmp(ptinfo_beg->token, token)) {
5841 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
5842 beg_pos.lineno, beg_pos.column, token,
5843 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
5847 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5851token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5853 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5854 if (!p->token_info_enabled) return;
5855 if (!ptinfo_beg) return;
5856 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5857 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5858 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5859 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5860 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5861 rb_warn3L(ptinfo_end->beg.lineno,
5862 "mismatched indentations at '%s' with '%s' at %d",
5863 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5867parser_precise_mbclen(struct parser_params *p, const char *ptr)
5869 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5870 if (!MBCLEN_CHARFOUND_P(len)) {
5871 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5878static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5881parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5884 int lineno = p->ruby_sourceline;
5888 else if (yylloc->beg_pos.lineno == lineno) {
5889 str = p->lex.lastline;
5894 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5898parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5903 yylloc = RUBY_SET_YYLLOC(current);
5905 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5906 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5907 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5908 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5911 compile_error(p, "%s", msg);
5912 parser_show_error_line(p, yylloc);
5917ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5920 const int max_line_margin = 30;
5921 const char *ptr, *ptr_end, *pt, *pb;
5922 const char *pre = "", *post = "", *pend;
5923 const char *code = "", *caret = "";
5925 const char *const pbeg = RSTRING_PTR(str);
5930 if (!yylloc) return;
5931 pend = RSTRING_END(str);
5932 if (pend > pbeg && pend[-1] == '\n') {
5933 if (--pend > pbeg && pend[-1] == '\r') --pend;
5937 if (lineno == yylloc->end_pos.lineno &&
5938 (pend - pbeg) > yylloc->end_pos.column) {
5939 pt = pbeg + yylloc->end_pos.column;
5943 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5944 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5946 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5947 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5949 len = ptr_end - ptr;
5952 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5953 if (ptr > pbeg) pre = "...";
5955 if (ptr_end < pend) {
5956 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5957 if (ptr_end < pend) post = "...";
5961 if (lineno == yylloc->beg_pos.lineno) {
5962 pb += yylloc->beg_pos.column;
5963 if (pb > pt) pb = pt;
5965 if (pb < ptr) pb = ptr;
5966 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5969 if (RTEST(errbuf)) {
5970 mesg = rb_attr_get(errbuf, idMesg);
5971 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5972 rb_str_cat_cstr(mesg, "\n");
5975 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5977 if (!errbuf && rb_stderr_tty_p()) {
5978#define CSI_BEGIN "\033["
5981 CSI_BEGIN""CSI_SGR"%s" /* pre */
5982 CSI_BEGIN"1"CSI_SGR"%.*s"
5983 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5984 CSI_BEGIN";1"CSI_SGR"%.*s"
5985 CSI_BEGIN""CSI_SGR"%s" /* post */
5988 (int)(pb - ptr), ptr,
5990 (int)(ptr_end - pt), pt,
5996 len = ptr_end - ptr;
5997 lim = pt < pend ? pt : pend;
5998 i = (int)(lim - ptr);
5999 buf = ALLOCA_N(char, i+2);
6004 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6010 memset(p2, '~', (lim - ptr));
6014 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6015 pre, (int)len, code, post,
6018 if (!errbuf) rb_write_error_str(mesg);
6022parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6024 const char *pcur = 0, *ptok = 0;
6026 p->ruby_sourceline == yylloc->beg_pos.lineno &&
6027 p->ruby_sourceline == yylloc->end_pos.lineno) {
6030 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6031 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6033 dispatch1(parse_error, STR_NEW2(msg));
6043parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6050vtable_size(const struct vtable *tbl)
6052 if (!DVARS_TERMINAL_P(tbl)) {
6061static struct vtable *
6062vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6064 struct vtable *tbl = ALLOC(struct vtable);
6067 tbl->tbl = ALLOC_N(ID, tbl->capa);
6071 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6076#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6079vtable_free_gen(struct parser_params *p, int line, const char *name,
6084 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6087 if (!DVARS_TERMINAL_P(tbl)) {
6089 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6091 ruby_sized_xfree(tbl, sizeof(*tbl));
6094#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6097vtable_add_gen(struct parser_params *p, int line, const char *name,
6098 struct vtable *tbl, ID id)
6102 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6103 line, name, (void *)tbl, rb_id2name(id));
6106 if (DVARS_TERMINAL_P(tbl)) {
6107 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6110 if (tbl->pos == tbl->capa) {
6111 tbl->capa = tbl->capa * 2;
6112 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6114 tbl->tbl[tbl->pos++] = id;
6116#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6120vtable_pop_gen(struct parser_params *p, int line, const char *name,
6121 struct vtable *tbl, int n)
6124 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6125 line, name, (void *)tbl, n);
6128 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6133#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6137vtable_included(const struct vtable * tbl, ID id)
6141 if (!DVARS_TERMINAL_P(tbl)) {
6142 for (i = 0; i < tbl->pos; i++) {
6143 if (tbl->tbl[i] == id) {
6151static void parser_prepare(struct parser_params *p);
6154static NODE *parser_append_options(struct parser_params *p, NODE *node);
6157debug_lines(VALUE fname)
6160 CONST_ID(script_lines, "SCRIPT_LINES__");
6161 if (rb_const_defined_at(rb_cObject, script_lines)) {
6162 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6163 if (RB_TYPE_P(hash, T_HASH)) {
6164 VALUE lines = rb_ary_new();
6165 rb_hash_aset(hash, fname, lines);
6173e_option_supplied(struct parser_params *p)
6175 return strcmp(p->ruby_sourcefile, "-e") == 0;
6179yycompile0(VALUE arg)
6183 struct parser_params *p = (struct parser_params *)arg;
6186 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6187 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6188 if (p->debug_lines && p->ruby_sourceline > 0) {
6189 VALUE str = rb_default_rs;
6190 n = p->ruby_sourceline;
6192 rb_ary_push(p->debug_lines, str);
6196 if (!e_option_supplied(p)) {
6202#define RUBY_DTRACE_PARSE_HOOK(name) \
6203 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6204 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6206 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6208 RUBY_DTRACE_PARSE_HOOK(END);
6212 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6213 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6214 if (n || p->error_p) {
6215 VALUE mesg = p->error_buffer;
6217 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6219 rb_set_errinfo(mesg);
6222 tree = p->eval_tree;
6224 tree = NEW_NIL(&NULL_LOC);
6227 VALUE opt = p->compile_option;
6229 NODE *body = parser_append_options(p, tree->nd_body);
6230 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6231 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6232 prelude = block_append(p, p->eval_tree_begin, body);
6233 tree->nd_body = prelude;
6234 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6236 p->ast->body.root = tree;
6237 p->ast->body.line_count = p->line_count;
6242yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6246 p->ruby_sourcefile_string = Qnil;
6247 p->ruby_sourcefile = "(none)";
6250 p->ruby_sourcefile_string = rb_fstring(fname);
6251 p->ruby_sourcefile = StringValueCStr(fname);
6253 p->ruby_sourceline = line - 1;
6255 p->ast = ast = rb_ast_new();
6256 rb_suppress_tracing(yycompile0, (VALUE)p);
6258 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6265must_be_ascii_compatible(VALUE s)
6267 rb_encoding *enc = rb_enc_get(s);
6268 if (!rb_enc_asciicompat(enc)) {
6269 rb_raise(rb_eArgError, "invalid source encoding");
6275lex_get_str(struct parser_params *p, VALUE s)
6277 char *beg, *end, *start;
6280 beg = RSTRING_PTR(s);
6281 len = RSTRING_LEN(s);
6283 if (p->lex.gets_.ptr) {
6284 if (len == p->lex.gets_.ptr) return Qnil;
6285 beg += p->lex.gets_.ptr;
6286 len -= p->lex.gets_.ptr;
6288 end = memchr(beg, '\n', len);
6289 if (end) len = ++end - beg;
6290 p->lex.gets_.ptr += len;
6291 return rb_str_subseq(s, beg - start, len);
6295lex_getline(struct parser_params *p)
6297 VALUE line = (*p->lex.gets)(p, p->lex.input);
6298 if (NIL_P(line)) return line;
6299 must_be_ascii_compatible(line);
6301 if (p->debug_lines) {
6302 rb_enc_associate(line, p->enc);
6303 rb_ary_push(p->debug_lines, line);
6310static const rb_data_type_t parser_data_type;
6314parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6316 struct parser_params *p;
6318 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6320 p->lex.gets = lex_get_str;
6321 p->lex.gets_.ptr = 0;
6322 p->lex.input = rb_str_new_frozen(s);
6323 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6325 return yycompile(vparser, p, fname, line);
6329rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6331 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6335rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6337 must_be_ascii_compatible(s);
6338 return parser_compile_string(vparser, f, s, line);
6341VALUE rb_io_gets_internal(VALUE io);
6344lex_io_gets(struct parser_params *p, VALUE io)
6346 return rb_io_gets_internal(io);
6350rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6352 struct parser_params *p;
6354 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6356 p->lex.gets = lex_io_gets;
6357 p->lex.input = file;
6358 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6360 return yycompile(vparser, p, fname, start);
6364lex_generic_gets(struct parser_params *p, VALUE input)
6366 return (*p->lex.gets_.call)(input, p->line_count);
6370rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6372 struct parser_params *p;
6374 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6376 p->lex.gets = lex_generic_gets;
6377 p->lex.gets_.call = lex_gets;
6378 p->lex.input = input;
6379 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6381 return yycompile(vparser, p, fname, start);
6385#define STR_FUNC_ESCAPE 0x01
6386#define STR_FUNC_EXPAND 0x02
6387#define STR_FUNC_REGEXP 0x04
6388#define STR_FUNC_QWORDS 0x08
6389#define STR_FUNC_SYMBOL 0x10
6390#define STR_FUNC_INDENT 0x20
6391#define STR_FUNC_LABEL 0x40
6392#define STR_FUNC_LIST 0x4000
6393#define STR_FUNC_TERM 0x8000
6396 str_label = STR_FUNC_LABEL,
6398 str_dquote = (STR_FUNC_EXPAND),
6399 str_xquote = (STR_FUNC_EXPAND),
6400 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6401 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6402 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6403 str_ssym = (STR_FUNC_SYMBOL),
6404 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6408parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6412 str = rb_enc_str_new(ptr, len, enc);
6413 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6414 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6416 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6417 rb_enc_associate(str, rb_ascii8bit_encoding());
6424#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6425#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6426#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6427#define peek(p,c) peek_n(p, (c), 0)
6428#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6429#define peekc(p) peekc_n(p, 0)
6430#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6434add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6437 if (!has_delayed_token(p)) {
6438 p->delayed.token = rb_str_buf_new(end - tok);
6439 rb_enc_associate(p->delayed.token, p->enc);
6440 p->delayed.line = p->ruby_sourceline;
6441 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6443 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6448#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6452nextline(struct parser_params *p)
6454 VALUE v = p->lex.nextline;
6455 p->lex.nextline = 0;
6460 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6464 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6472 else if (NIL_P(v)) {
6473 /* after here-document without terminator */
6476 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6477 if (p->heredoc_end > 0) {
6478 p->ruby_sourceline = p->heredoc_end;
6481 p->ruby_sourceline++;
6482 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6483 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6485 p->lex.prevline = p->lex.lastline;
6486 p->lex.lastline = v;
6491parser_cr(struct parser_params *p, int c)
6493 if (peek(p, '\n')) {
6501nextc(struct parser_params *p)
6505 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6506 if (nextline(p)) return -1;
6508 c = (unsigned char)*p->lex.pcur++;
6509 if (UNLIKELY(c == '\r')) {
6510 c = parser_cr(p, c);
6517pushback(struct parser_params *p, int c)
6519 if (c == -1) return;
6521 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6526#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6528#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6529#define tok(p) (p)->tokenbuf
6530#define toklen(p) (p)->tokidx
6533looking_at_eol_p(struct parser_params *p)
6535 const char *ptr = p->lex.pcur;
6536 while (ptr < p->lex.pend) {
6537 int c = (unsigned char)*ptr++;
6538 int eol = (c == '\n' || c == '#');
6539 if (eol || !ISSPACE(c)) {
6547newtok(struct parser_params *p)
6550 p->tokline = p->ruby_sourceline;
6553 p->tokenbuf = ALLOC_N(char, 60);
6555 if (p->toksiz > 4096) {
6557 REALLOC_N(p->tokenbuf, char, 60);
6563tokspace(struct parser_params *p, int n)
6567 if (p->tokidx >= p->toksiz) {
6568 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6569 REALLOC_N(p->tokenbuf, char, p->toksiz);
6571 return &p->tokenbuf[p->tokidx-n];
6575tokadd(struct parser_params *p, int c)
6577 p->tokenbuf[p->tokidx++] = (char)c;
6578 if (p->tokidx >= p->toksiz) {
6580 REALLOC_N(p->tokenbuf, char, p->toksiz);
6585tok_hex(struct parser_params *p, size_t *numlen)
6589 c = scan_hex(p->lex.pcur, 2, numlen);
6591 yyerror0("invalid hex escape");
6595 p->lex.pcur += *numlen;
6599#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6602escaped_control_code(int c)
6628#define WARN_SPACE_CHAR(c, prefix) \
6629 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6632tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6633 int regexp_literal, int wide)
6636 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6637 literal_flush(p, p->lex.pcur);
6638 p->lex.pcur += numlen;
6639 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6640 yyerror0("invalid Unicode escape");
6641 return wide && numlen > 0;
6643 if (codepoint > 0x10ffff) {
6644 yyerror0("invalid Unicode codepoint (too large)");
6647 if ((codepoint & 0xfffff800) == 0xd800) {
6648 yyerror0("invalid Unicode codepoint");
6651 if (regexp_literal) {
6652 tokcopy(p, (int)numlen);
6654 else if (codepoint >= 0x80) {
6655 rb_encoding *utf8 = rb_utf8_encoding();
6656 if (*encp && utf8 != *encp) {
6657 YYLTYPE loc = RUBY_INIT_YYLLOC();
6658 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6659 parser_show_error_line(p, &loc);
6663 tokaddmbc(p, codepoint, *encp);
6666 tokadd(p, codepoint);
6671/* return value is for ?\u3042 */
6673tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6674 int term, int symbol_literal, int regexp_literal)
6677 * If `term` is not -1, then we allow multiple codepoints in \u{}
6678 * upto `term` byte, otherwise we're parsing a character literal.
6679 * And then add the codepoints to the current token.
6681 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6683 const int open_brace = '{', close_brace = '}';
6685 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6687 if (peek(p, open_brace)) { /* handle \u{...} form */
6688 const char *second = NULL;
6689 int c, last = nextc(p);
6690 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6691 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6692 while (c != close_brace) {
6693 if (c == term) goto unterminated;
6694 if (second == multiple_codepoints)
6695 second = p->lex.pcur;
6696 if (regexp_literal) tokadd(p, last);
6697 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6700 while (ISSPACE(c = *p->lex.pcur)) {
6701 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6704 if (term == -1 && !second)
6705 second = multiple_codepoints;
6708 if (c != close_brace) {
6711 yyerror0("unterminated Unicode escape");
6714 if (second && second != multiple_codepoints) {
6715 const char *pcur = p->lex.pcur;
6716 p->lex.pcur = second;
6717 dispatch_scan_event(p, tSTRING_CONTENT);
6720 yyerror0(multiple_codepoints);
6724 if (regexp_literal) tokadd(p, close_brace);
6727 else { /* handle \uxxxx form */
6728 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6735#define ESCAPE_CONTROL 1
6736#define ESCAPE_META 2
6739read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6744 switch (c = nextc(p)) {
6745 case '\\': /* Backslash */
6748 case 'n': /* newline */
6751 case 't': /* horizontal tab */
6754 case 'r': /* carriage-return */
6757 case 'f': /* form-feed */
6760 case 'v': /* vertical tab */
6763 case 'a': /* alarm(bell) */
6766 case 'e': /* escape */
6769 case '0': case '1': case '2': case '3': /* octal constant */
6770 case '4': case '5': case '6': case '7':
6772 c = scan_oct(p->lex.pcur, 3, &numlen);
6773 p->lex.pcur += numlen;
6776 case 'x': /* hex constant */
6777 c = tok_hex(p, &numlen);
6778 if (numlen == 0) return 0;
6781 case 'b': /* backspace */
6784 case 's': /* space */
6788 if (flags & ESCAPE_META) goto eof;
6789 if ((c = nextc(p)) != '-') {
6792 if ((c = nextc(p)) == '\\') {
6798 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6800 else if (c == -1 || !ISASCII(c)) goto eof;
6802 int c2 = escaped_control_code(c);
6804 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6805 WARN_SPACE_CHAR(c2, "\\M-");
6808 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6811 else if (ISCNTRL(c)) goto eof;
6812 return ((c & 0xff) | 0x80);
6816 if ((c = nextc(p)) != '-') {
6820 if (flags & ESCAPE_CONTROL) goto eof;
6821 if ((c = nextc(p))== '\\') {
6827 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6831 else if (c == -1 || !ISASCII(c)) goto eof;
6833 int c2 = escaped_control_code(c);
6836 if (flags & ESCAPE_META) {
6837 WARN_SPACE_CHAR(c2, "\\M-");
6840 WARN_SPACE_CHAR(c2, "");
6844 if (flags & ESCAPE_META) {
6845 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6848 WARN_SPACE_CHAR(c2, "\\C-");
6852 else if (ISCNTRL(c)) goto eof;
6858 yyerror0("Invalid escape character syntax");
6868tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6870 int len = rb_enc_codelen(c, enc);
6871 rb_enc_mbcput(c, tokspace(p, len), enc);
6875tokadd_escape(struct parser_params *p, rb_encoding **encp)
6882 switch (c = nextc(p)) {
6884 return 0; /* just ignore */
6886 case '0': case '1': case '2': case '3': /* octal constant */
6887 case '4': case '5': case '6': case '7':
6889 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6890 if (numlen == 0) goto eof;
6891 p->lex.pcur += numlen;
6892 tokcopy(p, (int)numlen + 1);
6896 case 'x': /* hex constant */
6898 tok_hex(p, &numlen);
6899 if (numlen == 0) return -1;
6900 tokcopy(p, (int)numlen + 2);
6905 if (flags & ESCAPE_META) goto eof;
6906 if ((c = nextc(p)) != '-') {
6911 flags |= ESCAPE_META;
6915 if (flags & ESCAPE_CONTROL) goto eof;
6916 if ((c = nextc(p)) != '-') {
6924 if (flags & ESCAPE_CONTROL) goto eof;
6926 flags |= ESCAPE_CONTROL;
6928 if ((c = nextc(p)) == '\\') {
6931 else if (c == -1) goto eof;
6937 yyerror0("Invalid escape character syntax");
6949regx_options(struct parser_params *p)
6957 while (c = nextc(p), ISALPHA(c)) {
6959 options |= RE_OPTION_ONCE;
6961 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6963 if (kc != rb_ascii8bit_encindex()) kcode = c;
6977 YYLTYPE loc = RUBY_INIT_YYLLOC();
6979 compile_error(p, "unknown regexp option%s - %*s",
6980 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6981 parser_show_error_line(p, &loc);
6983 return options | RE_OPTION_ENCODING(kcode);
6987tokadd_mbchar(struct parser_params *p, int c)
6989 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6990 if (len < 0) return -1;
6992 p->lex.pcur += --len;
6993 if (len > 0) tokcopy(p, len);
6998simple_re_meta(int c)
7001 case '$': case '*': case '+': case '.':
7002 case '?': case '^': case '|':
7003 case ')': case ']': case '}': case '>':
7011parser_update_heredoc_indent(struct parser_params *p, int c)
7013 if (p->heredoc_line_indent == -1) {
7014 if (c == '\n') p->heredoc_line_indent = 0;
7018 p->heredoc_line_indent++;
7021 else if (c == '\t') {
7022 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7023 p->heredoc_line_indent = w * TAB_WIDTH;
7026 else if (c != '\n') {
7027 if (p->heredoc_indent > p->heredoc_line_indent) {
7028 p->heredoc_indent = p->heredoc_line_indent;
7030 p->heredoc_line_indent = -1;
7037parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7039 YYLTYPE loc = RUBY_INIT_YYLLOC();
7040 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7041 compile_error(p, "%s mixed within %s source", n1, n2);
7042 parser_show_error_line(p, &loc);
7046parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7048 const char *pos = p->lex.pcur;
7050 parser_mixed_error(p, enc1, enc2);
7055tokadd_string(struct parser_params *p,
7056 int func, int term, int paren, long *nest,
7057 rb_encoding **encp, rb_encoding **enc)
7062#define mixed_error(enc1, enc2) \
7063 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7064#define mixed_escape(beg, enc1, enc2) \
7065 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7067 while ((c = nextc(p)) != -1) {
7068 if (p->heredoc_indent > 0) {
7069 parser_update_heredoc_indent(p, c);
7072 if (paren && c == paren) {
7075 else if (c == term) {
7076 if (!nest || !*nest) {
7082 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7083 int c2 = *p->lex.pcur;
7084 if (c2 == '$' || c2 == '@' || c2 == '{') {
7089 else if (c == '\\') {
7090 literal_flush(p, p->lex.pcur - 1);
7094 if (func & STR_FUNC_QWORDS) break;
7095 if (func & STR_FUNC_EXPAND) {
7096 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7107 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7111 if ((func & STR_FUNC_EXPAND) == 0) {
7115 tokadd_utf8(p, enc, term,
7116 func & STR_FUNC_SYMBOL,
7117 func & STR_FUNC_REGEXP);
7121 if (c == -1) return -1;
7123 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7126 if (func & STR_FUNC_REGEXP) {
7127 if (c == term && !simple_re_meta(c)) {
7132 if ((c = tokadd_escape(p, enc)) < 0)
7134 if (*enc && *enc != *encp) {
7135 mixed_escape(p->lex.ptok+2, *enc, *encp);
7139 else if (func & STR_FUNC_EXPAND) {
7141 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7142 c = read_escape(p, 0, enc);
7144 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7145 /* ignore backslashed spaces in %w */
7147 else if (c != term && !(paren && c == paren)) {
7154 else if (!parser_isascii(p)) {
7159 else if (*enc != *encp) {
7160 mixed_error(*enc, *encp);
7163 if (tokadd_mbchar(p, c) == -1) return -1;
7166 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7174 else if (*enc != *encp) {
7175 mixed_error(*enc, *encp);
7182 if (*enc) *encp = *enc;
7186static inline rb_strterm_t *
7187new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7189 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7192/* imemo_parser_strterm for literal */
7193#define NEW_STRTERM(func, term, paren) \
7194 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7198flush_string_content(struct parser_params *p, rb_encoding *enc)
7200 VALUE content = yylval.val;
7201 if (!ripper_is_node_yylval(content))
7202 content = ripper_new_yylval(p, 0, 0, content);
7203 if (has_delayed_token(p)) {
7204 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7206 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7208 dispatch_delayed_token(p, tSTRING_CONTENT);
7209 p->lex.ptok = p->lex.pcur;
7210 RNODE(content)->nd_rval = yylval.val;
7212 dispatch_scan_event(p, tSTRING_CONTENT);
7213 if (yylval.val != content)
7214 RNODE(content)->nd_rval = yylval.val;
7215 yylval.val = content;
7218#define flush_string_content(p, enc) ((void)(enc))
7221RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7222/* this can be shared with ripper, since it's independent from struct
7225#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7226#define SPECIAL_PUNCT(idx) ( \
7227 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7228 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7229 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7230 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7231 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7233const unsigned int ruby_global_name_punct_bits[] = {
7242static enum yytokentype
7243parser_peek_variable_name(struct parser_params *p)
7246 const char *ptr = p->lex.pcur;
7248 if (ptr + 1 >= p->lex.pend) return 0;
7252 if ((c = *ptr) == '-') {
7253 if (++ptr >= p->lex.pend) return 0;
7256 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7257 return tSTRING_DVAR;
7261 if ((c = *ptr) == '@') {
7262 if (++ptr >= p->lex.pend) return 0;
7268 p->command_start = TRUE;
7269 return tSTRING_DBEG;
7273 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7274 return tSTRING_DVAR;
7278#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7279#define IS_END() IS_lex_state(EXPR_END_ANY)
7280#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7281#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7282#define IS_LABEL_POSSIBLE() (\
7283 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7285#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7286#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7288static inline enum yytokentype
7289parser_string_term(struct parser_params *p, int func)
7292 if (func & STR_FUNC_REGEXP) {
7293 set_yylval_num(regx_options(p));
7294 dispatch_scan_event(p, tREGEXP_END);
7295 SET_LEX_STATE(EXPR_END);
7298 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7300 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7303 SET_LEX_STATE(EXPR_END);
7307static enum yytokentype
7308parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7310 int func = (int)quote->u1.func;
7311 int term = (int)quote->u3.term;
7312 int paren = (int)quote->u2.paren;
7314 rb_encoding *enc = p->enc;
7315 rb_encoding *base_enc = 0;
7318 if (func & STR_FUNC_TERM) {
7319 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7320 SET_LEX_STATE(EXPR_END);
7322 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7325 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7326 do {c = nextc(p);} while (ISSPACE(c));
7329 if (func & STR_FUNC_LIST) {
7330 quote->u1.func &= ~STR_FUNC_LIST;
7333 if (c == term && !quote->u0.nest) {
7334 if (func & STR_FUNC_QWORDS) {
7335 quote->u1.func |= STR_FUNC_TERM;
7336 pushback(p, c); /* dispatch the term at tSTRING_END */
7337 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7340 return parser_string_term(p, func);
7344 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7348 if ((func & STR_FUNC_EXPAND) && c == '#') {
7349 int t = parser_peek_variable_name(p);
7355 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7356 &enc, &base_enc) == -1) {
7359# define unterminated_literal(mesg) yyerror0(mesg)
7361# define unterminated_literal(mesg) compile_error(p, mesg)
7363 literal_flush(p, p->lex.pcur);
7364 if (func & STR_FUNC_QWORDS) {
7365 /* no content to add, bailing out here */
7366 unterminated_literal("unterminated list meets end of file");
7370 if (func & STR_FUNC_REGEXP) {
7371 unterminated_literal("unterminated regexp meets end of file");
7374 unterminated_literal("unterminated string meets end of file");
7376 quote->u1.func |= STR_FUNC_TERM;
7381 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7382 set_yylval_str(lit);
7383 flush_string_content(p, enc);
7385 return tSTRING_CONTENT;
7388static enum yytokentype
7389heredoc_identifier(struct parser_params *p)
7392 * term_len is length of `<<"END"` except `END`,
7393 * in this case term_len is 4 (<, <, " and ").
7395 long len, offset = p->lex.pcur - p->lex.pbeg;
7396 int c = nextc(p), term, func = 0, quote = 0;
7397 enum yytokentype token = tSTRING_BEG;
7402 func = STR_FUNC_INDENT;
7405 else if (c == '~') {
7407 func = STR_FUNC_INDENT;
7413 func |= str_squote; goto quoted;
7415 func |= str_dquote; goto quoted;
7417 token = tXSTRING_BEG;
7418 func |= str_xquote; goto quoted;
7425 while ((c = nextc(p)) != term) {
7426 if (c == -1 || c == '\r' || c == '\n') {
7427 yyerror(NULL, p, "unterminated here document identifier");
7434 if (!parser_is_identchar(p)) {
7436 if (func & STR_FUNC_INDENT) {
7437 pushback(p, indent > 0 ? '~' : '-');
7443 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7444 if (n < 0) return 0;
7446 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7451 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7452 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7453 yyerror(NULL, p, "too long here document identifier");
7454 dispatch_scan_event(p, tHEREDOC_BEG);
7457 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7458 p->lex.strterm->flags |= STRTERM_HEREDOC;
7459 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7460 here->offset = offset;
7461 here->sourceline = p->ruby_sourceline;
7462 here->length = (int)len;
7463 here->quote = quote;
7467 p->heredoc_indent = indent;
7468 p->heredoc_line_indent = 0;
7473heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7478 line = here->lastline;
7479 p->lex.lastline = line;
7480 p->lex.pbeg = RSTRING_PTR(line);
7481 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7482 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7483 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7484 p->heredoc_end = p->ruby_sourceline;
7485 p->ruby_sourceline = (int)here->sourceline;
7486 if (p->eofp) p->lex.nextline = Qnil;
7491dedent_string(VALUE string, int width)
7497 RSTRING_GETMEM(string, str, len);
7498 for (i = 0; i < len && col < width; i++) {
7499 if (str[i] == ' ') {
7502 else if (str[i] == '\t') {
7503 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7504 if (n > width) break;
7512 rb_str_modify(string);
7513 str = RSTRING_PTR(string);
7514 if (RSTRING_LEN(string) != len)
7515 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7516 MEMMOVE(str, str + i, char, len - i);
7517 rb_str_set_len(string, len - i);
7523heredoc_dedent(struct parser_params *p, NODE *root)
7525 NODE *node, *str_node, *prev_node;
7526 int indent = p->heredoc_indent;
7529 if (indent <= 0) return root;
7530 p->heredoc_indent = 0;
7531 if (!root) return root;
7533 prev_node = node = str_node = root;
7534 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7537 VALUE lit = str_node->nd_lit;
7538 if (str_node->flags & NODE_FL_NEWLINE) {
7539 dedent_string(lit, indent);
7544 else if (!literal_concat0(p, prev_lit, lit)) {
7548 NODE *end = node->nd_end;
7549 node = prev_node->nd_next = node->nd_next;
7551 if (nd_type(prev_node) == NODE_DSTR)
7552 nd_set_type(prev_node, NODE_STR);
7560 while ((node = (prev_node = node)->nd_next) != 0) {
7562 if (nd_type(node) != NODE_LIST) break;
7563 if ((str_node = node->nd_head) != 0) {
7564 enum node_type type = nd_type(str_node);
7565 if (type == NODE_STR || type == NODE_DSTR) break;
7575heredoc_dedent(struct parser_params *p, VALUE array)
7577 int indent = p->heredoc_indent;
7579 if (indent <= 0) return array;
7580 p->heredoc_indent = 0;
7581 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7587 * Ripper.dedent_string(input, width) -> Integer
7589 * USE OF RIPPER LIBRARY ONLY.
7591 * Strips up to +width+ leading whitespaces from +input+,
7592 * and returns the stripped column width.
7595parser_dedent_string(VALUE self, VALUE input, VALUE width)
7600 wid = NUM2UINT(width);
7601 col = dedent_string(input, wid);
7602 return INT2NUM(col);
7607whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7609 const char *ptr = p->lex.pbeg;
7613 while (*ptr && ISSPACE(*ptr)) ptr++;
7615 n = p->lex.pend - (ptr + len);
7616 if (n < 0) return FALSE;
7617 if (n > 0 && ptr[len] != '\n') {
7618 if (ptr[len] != '\r') return FALSE;
7619 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7621 return strncmp(eos, ptr, len) == 0;
7625word_match_p(struct parser_params *p, const char *word, long len)
7627 if (strncmp(p->lex.pcur, word, len)) return 0;
7628 if (p->lex.pcur + len == p->lex.pend) return 1;
7629 int c = (unsigned char)p->lex.pcur[len];
7630 if (ISSPACE(c)) return 1;
7632 case '\0': case '\004': case '\032': return 1;
7637#define NUM_SUFFIX_R (1<<0)
7638#define NUM_SUFFIX_I (1<<1)
7639#define NUM_SUFFIX_ALL 3
7642number_literal_suffix(struct parser_params *p, int mask)
7645 const char *lastp = p->lex.pcur;
7647 while ((c = nextc(p)) != -1) {
7648 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7649 result |= (mask & NUM_SUFFIX_I);
7650 mask &= ~NUM_SUFFIX_I;
7651 /* r after i, rational of complex is disallowed */
7652 mask &= ~NUM_SUFFIX_R;
7655 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7656 result |= (mask & NUM_SUFFIX_R);
7657 mask &= ~NUM_SUFFIX_R;
7660 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7661 p->lex.pcur = lastp;
7662 literal_flush(p, p->lex.pcur);
7671static enum yytokentype
7672set_number_literal(struct parser_params *p, VALUE v,
7673 enum yytokentype type, int suffix)
7675 if (suffix & NUM_SUFFIX_I) {
7676 v = rb_complex_raw(INT2FIX(0), v);
7679 set_yylval_literal(v);
7680 SET_LEX_STATE(EXPR_END);
7684static enum yytokentype
7685set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7687 enum yytokentype type = tINTEGER;
7688 if (suffix & NUM_SUFFIX_R) {
7689 v = rb_rational_raw1(v);
7692 return set_number_literal(p, v, type, suffix);
7697dispatch_heredoc_end(struct parser_params *p)
7700 if (has_delayed_token(p))
7701 dispatch_delayed_token(p, tSTRING_CONTENT);
7702 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7703 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7709#define dispatch_heredoc_end(p) ((void)0)
7712static enum yytokentype
7713here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7715 int c, func, indent = 0;
7716 const char *eos, *ptr, *ptr_end;
7719 rb_encoding *enc = p->enc;
7720 rb_encoding *base_enc = 0;
7723 eos = RSTRING_PTR(here->lastline) + here->offset;
7725 indent = (func = here->func) & STR_FUNC_INDENT;
7727 if ((c = nextc(p)) == -1) {
7730 if (!has_delayed_token(p)) {
7731 dispatch_scan_event(p, tSTRING_CONTENT);
7734 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7735 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7736 int cr = ENC_CODERANGE_UNKNOWN;
7737 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7738 if (cr != ENC_CODERANGE_7BIT &&
7739 p->enc == rb_usascii_encoding() &&
7740 enc != rb_utf8_encoding()) {
7741 enc = rb_ascii8bit_encoding();
7744 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7746 dispatch_delayed_token(p, tSTRING_CONTENT);
7750 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7751 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7755 SET_LEX_STATE(EXPR_END);
7760 /* not beginning of line, cannot be the terminator */
7762 else if (p->heredoc_line_indent == -1) {
7763 /* `heredoc_line_indent == -1` means
7764 * - "after an interpolation in the same line", or
7765 * - "in a continuing line"
7767 p->heredoc_line_indent = 0;
7769 else if (whole_match_p(p, eos, len, indent)) {
7770 dispatch_heredoc_end(p);
7772 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7775 SET_LEX_STATE(EXPR_END);
7779 if (!(func & STR_FUNC_EXPAND)) {
7781 ptr = RSTRING_PTR(p->lex.lastline);
7782 ptr_end = p->lex.pend;
7783 if (ptr_end > ptr) {
7784 switch (ptr_end[-1]) {
7786 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7795 if (p->heredoc_indent > 0) {
7797 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7799 p->heredoc_line_indent = 0;
7803 rb_str_cat(str, ptr, ptr_end - ptr);
7805 str = STR_NEW(ptr, ptr_end - ptr);
7806 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7808 if (p->heredoc_indent > 0) {
7811 if (nextc(p) == -1) {
7817 } while (!whole_match_p(p, eos, len, indent));
7820 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7823 int t = parser_peek_variable_name(p);
7824 if (p->heredoc_line_indent != -1) {
7825 if (p->heredoc_indent > p->heredoc_line_indent) {
7826 p->heredoc_indent = p->heredoc_line_indent;
7828 p->heredoc_line_indent = -1;
7837 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7838 if (p->eofp) goto error;
7842 if (c == '\\') p->heredoc_line_indent = -1;
7844 str = STR_NEW3(tok(p), toklen(p), enc, func);
7846 set_yylval_str(str);
7848 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7850 flush_string_content(p, enc);
7851 return tSTRING_CONTENT;
7853 tokadd(p, nextc(p));
7854 if (p->heredoc_indent > 0) {
7858 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7859 if ((c = nextc(p)) == -1) goto error;
7860 } while (!whole_match_p(p, eos, len, indent));
7861 str = STR_NEW3(tok(p), toklen(p), enc, func);
7863 dispatch_heredoc_end(p);
7865 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7868 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7870 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7871 set_yylval_str(str);
7873 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7875 return tSTRING_CONTENT;
7881arg_ambiguous(struct parser_params *p, char c)
7885 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
7888 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7891 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7898formal_argument(struct parser_params *p, ID lhs)
7900formal_argument(struct parser_params *p, VALUE lhs)
7903 switch (id_type(get_id(lhs))) {
7907# define ERR(mesg) yyerror0(mesg)
7909# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
7912 ERR("formal argument cannot be a constant");
7915 ERR("formal argument cannot be an instance variable");
7918 ERR("formal argument cannot be a global variable");
7921 ERR("formal argument cannot be a class variable");
7924 ERR("formal argument must be local variable");
7928 shadowing_lvar(p, lhs);
7933lvar_defined(struct parser_params *p, ID id)
7935 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7938/* emacsen -*- hack */
7940parser_encode_length(struct parser_params *p, const char *name, long len)
7944 if (len > 5 && name[nlen = len - 5] == '-') {
7945 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7948 if (len > 4 && name[nlen = len - 4] == '-') {
7949 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7951 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7952 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7953 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7960parser_set_encode(struct parser_params *p, const char *name)
7962 int idx = rb_enc_find_index(name);
7967 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7969 excargs[0] = rb_eArgError;
7970 excargs[2] = rb_make_backtrace();
7971 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7972 rb_exc_raise(rb_make_exception(3, excargs));
7974 enc = rb_enc_from_index(idx);
7975 if (!rb_enc_asciicompat(enc)) {
7976 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7981 if (p->debug_lines) {
7982 VALUE lines = p->debug_lines;
7983 long i, n = RARRAY_LEN(lines);
7984 for (i = 0; i < n; ++i) {
7985 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7992comment_at_top(struct parser_params *p)
7994 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7995 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7996 while (ptr < ptr_end) {
7997 if (!ISSPACE(*ptr)) return 0;
8003typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8004typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8006static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8009magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8011 if (!comment_at_top(p)) {
8014 parser_set_encode(p, val);
8018parser_get_bool(struct parser_params *p, const char *name, const char *val)
8022 if (STRCASECMP(val, "true") == 0) {
8027 if (STRCASECMP(val, "false") == 0) {
8032 return parser_invalid_pragma_value(p, name, val);
8036parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8038 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8043parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8045 int b = parser_get_bool(p, name, val);
8046 if (b >= 0) p->token_info_enabled = b;
8050parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8054 if (p->token_seen) {
8055 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8059 b = parser_get_bool(p, name, val);
8062 if (!p->compile_option)
8063 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8064 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8065 (b ? Qtrue : Qfalse));
8069parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8071 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8072 if (*s == ' ' || *s == '\t') continue;
8073 if (*s == '#') break;
8074 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8080 if (STRCASECMP(val, "none") == 0) {
8081 p->ctxt.shareable_constant_value = shareable_none;
8086 if (STRCASECMP(val, "literal") == 0) {
8087 p->ctxt.shareable_constant_value = shareable_literal;
8092 if (STRCASECMP(val, "experimental_copy") == 0) {
8093 p->ctxt.shareable_constant_value = shareable_copy;
8096 if (STRCASECMP(val, "experimental_everything") == 0) {
8097 p->ctxt.shareable_constant_value = shareable_everything;
8102 parser_invalid_pragma_value(p, name, val);
8107parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8109 int b = parser_get_bool(p, name, val);
8110 if (b >= 0) p->past_scope_enabled = b;
8114struct magic_comment {
8116 rb_magic_comment_setter_t func;
8117 rb_magic_comment_length_t length;
8120static const struct magic_comment magic_comments[] = {
8121 {"coding", magic_comment_encoding, parser_encode_length},
8122 {"encoding", magic_comment_encoding, parser_encode_length},
8123 {"frozen_string_literal", parser_set_compile_option_flag},
8124 {"shareable_constant_value", parser_set_shareable_constant_value},
8125 {"warn_indent", parser_set_token_info},
8127 {"warn_past_scope", parser_set_past_scope},
8132magic_comment_marker(const char *str, long len)
8139 if (str[i-1] == '*' && str[i-2] == '-') {
8145 if (i + 1 >= len) return 0;
8146 if (str[i+1] != '-') {
8149 else if (str[i-1] != '-') {
8165parser_magic_comment(struct parser_params *p, const char *str, long len)
8168 VALUE name = 0, val = 0;
8169 const char *beg, *end, *vbeg, *vend;
8170#define str_copy(_s, _p, _n) ((_s) \
8171 ? (void)(rb_str_resize((_s), (_n)), \
8172 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8173 : (void)((_s) = STR_NEW((_p), (_n))))
8175 if (len <= 7) return FALSE;
8176 if (!!(beg = magic_comment_marker(str, len))) {
8177 if (!(end = magic_comment_marker(beg, str + len - beg)))
8181 len = end - beg - 3;
8184 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8186 const struct magic_comment *mc = magic_comments;
8191 for (; len > 0 && *str; str++, --len) {
8193 case '\'': case '"': case ':': case ';':
8196 if (!ISSPACE(*str)) break;
8198 for (beg = str; len > 0; str++, --len) {
8200 case '\'': case '"': case ':': case ';':
8203 if (ISSPACE(*str)) break;
8208 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8211 if (!indicator) return FALSE;
8215 do str++; while (--len > 0 && ISSPACE(*str));
8218 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8231 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8235 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8238 while (len > 0 && (ISSPACE(*str))) --len, str++;
8239 if (len) return FALSE;
8243 str_copy(name, beg, n);
8244 s = RSTRING_PTR(name);
8245 for (i = 0; i < n; ++i) {
8246 if (s[i] == '-') s[i] = '_';
8249 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8252 n = (*mc->length)(p, vbeg, n);
8254 str_copy(val, vbeg, n);
8255 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8258 } while (++mc < magic_comments + numberof(magic_comments));
8260 str_copy(val, vbeg, vend - vbeg);
8261 dispatch2(magic_comment, name, val);
8269set_file_encoding(struct parser_params *p, const char *str, const char *send)
8272 const char *beg = str;
8276 if (send - str <= 6) return;
8278 case 'C': case 'c': str += 6; continue;
8279 case 'O': case 'o': str += 5; continue;
8280 case 'D': case 'd': str += 4; continue;
8281 case 'I': case 'i': str += 3; continue;
8282 case 'N': case 'n': str += 2; continue;
8283 case 'G': case 'g': str += 1; continue;
8290 if (ISSPACE(*str)) break;
8293 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8297 if (++str >= send) return;
8298 } while (ISSPACE(*str));
8300 if (*str != '=' && *str != ':') return;
8305 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8306 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8307 parser_set_encode(p, RSTRING_PTR(s));
8308 rb_str_resize(s, 0);
8312parser_prepare(struct parser_params *p)
8315 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8318 if (peek(p, '!')) p->has_shebang = 1;
8320 case 0xef: /* UTF-8 BOM marker */
8321 if (p->lex.pend - p->lex.pcur >= 2 &&
8322 (unsigned char)p->lex.pcur[0] == 0xbb &&
8323 (unsigned char)p->lex.pcur[1] == 0xbf) {
8324 p->enc = rb_utf8_encoding();
8326 p->lex.pbeg = p->lex.pcur;
8334 p->enc = rb_enc_get(p->lex.lastline);
8338#define ambiguous_operator(tok, op, syn) ( \
8339 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8340 rb_warning0("even though it seems like "syn""))
8342#define ambiguous_operator(tok, op, syn) \
8343 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8345#define warn_balanced(tok, op, syn) ((void) \
8346 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8347 space_seen && !ISSPACE(c) && \
8348 (ambiguous_operator(tok, op, syn), 0)), \
8349 (enum yytokentype)(tok))
8352parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8355 char *point = &str[seen_point];
8356 size_t fraclen = len-seen_point-1;
8357 memmove(point, point+1, fraclen+1);
8358 v = rb_cstr_to_inum(str, 10, FALSE);
8359 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8362static enum yytokentype
8363no_digits(struct parser_params *p)
8365 yyerror0("numeric literal without digits");
8366 if (peek(p, '_')) nextc(p);
8367 /* dummy 0, for tUMINUS_NUM at numeric */
8368 return set_integer_literal(p, INT2FIX(0), 0);
8371static enum yytokentype
8372parse_numeric(struct parser_params *p, int c)
8374 int is_float, seen_point, seen_e, nondigit;
8377 is_float = seen_point = seen_e = nondigit = 0;
8378 SET_LEX_STATE(EXPR_END);
8380 if (c == '-' || c == '+') {
8385 int start = toklen(p);
8387 if (c == 'x' || c == 'X') {
8390 if (c != -1 && ISXDIGIT(c)) {
8393 if (nondigit) break;
8397 if (!ISXDIGIT(c)) break;
8400 } while ((c = nextc(p)) != -1);
8404 if (toklen(p) == start) {
8405 return no_digits(p);
8407 else if (nondigit) goto trailing_uc;
8408 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8409 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8411 if (c == 'b' || c == 'B') {
8414 if (c == '0' || c == '1') {
8417 if (nondigit) break;
8421 if (c != '0' && c != '1') break;
8424 } while ((c = nextc(p)) != -1);
8428 if (toklen(p) == start) {
8429 return no_digits(p);
8431 else if (nondigit) goto trailing_uc;
8432 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8433 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8435 if (c == 'd' || c == 'D') {
8438 if (c != -1 && ISDIGIT(c)) {
8441 if (nondigit) break;
8445 if (!ISDIGIT(c)) break;
8448 } while ((c = nextc(p)) != -1);
8452 if (toklen(p) == start) {
8453 return no_digits(p);
8455 else if (nondigit) goto trailing_uc;
8456 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8457 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8463 if (c == 'o' || c == 'O') {
8464 /* prefixed octal */
8466 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8467 return no_digits(p);
8470 if (c >= '0' && c <= '7') {
8475 if (nondigit) break;
8479 if (c < '0' || c > '9') break;
8480 if (c > '7') goto invalid_octal;
8483 } while ((c = nextc(p)) != -1);
8484 if (toklen(p) > start) {
8487 if (nondigit) goto trailing_uc;
8488 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8489 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8496 if (c > '7' && c <= '9') {
8498 yyerror0("Invalid octal digit");
8500 else if (c == '.' || c == 'e' || c == 'E') {
8505 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8506 return set_integer_literal(p, INT2FIX(0), suffix);
8512 case '0': case '1': case '2': case '3': case '4':
8513 case '5': case '6': case '7': case '8': case '9':
8519 if (nondigit) goto trailing_uc;
8520 if (seen_point || seen_e) {
8525 if (c0 == -1 || !ISDIGIT(c0)) {
8531 seen_point = toklen(p);
8550 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8555 tokadd(p, nondigit);
8559 nondigit = (c == '-' || c == '+') ? c : 0;
8562 case '_': /* `_' in number just ignored */
8563 if (nondigit) goto decode_num;
8577 literal_flush(p, p->lex.pcur - 1);
8578 YYLTYPE loc = RUBY_INIT_YYLLOC();
8579 compile_error(p, "trailing `%c' in number", nondigit);
8580 parser_show_error_line(p, &loc);
8584 enum yytokentype type = tFLOAT;
8587 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8588 if (suffix & NUM_SUFFIX_R) {
8590 v = parse_rational(p, tok(p), toklen(p), seen_point);
8593 double d = strtod(tok(p), 0);
8594 if (errno == ERANGE) {
8595 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8600 return set_number_literal(p, v, type, suffix);
8602 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8603 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8606static enum yytokentype
8607parse_qmark(struct parser_params *p, int space_seen)
8614 SET_LEX_STATE(EXPR_VALUE);
8619 compile_error(p, "incomplete character syntax");
8622 if (rb_enc_isspace(c, p->enc)) {
8624 int c2 = escaped_control_code(c);
8626 WARN_SPACE_CHAR(c2, "?");
8631 SET_LEX_STATE(EXPR_VALUE);
8636 if (!parser_isascii(p)) {
8637 if (tokadd_mbchar(p, c) == -1) return 0;
8639 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8640 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8642 const char *start = p->lex.pcur - 1, *ptr = start;
8644 int n = parser_precise_mbclen(p, ptr);
8645 if (n < 0) return -1;
8647 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8648 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8649 " a conditional operator, put a space after `?'",
8650 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8654 else if (c == '\\') {
8657 enc = rb_utf8_encoding();
8658 tokadd_utf8(p, &enc, -1, 0, 0);
8660 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8662 if (tokadd_mbchar(p, c) == -1) return 0;
8665 c = read_escape(p, 0, &enc);
8673 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8674 set_yylval_str(lit);
8675 SET_LEX_STATE(EXPR_END);
8679static enum yytokentype
8680parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8683 const char *ptok = p->lex.pcur;
8691 if (c == -1 || !ISALNUM(c)) {
8693 if (!ISASCII(c)) goto unknown;
8698 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8701 c = parser_precise_mbclen(p, p->lex.pcur);
8702 if (c < 0) return 0;
8704 yyerror0("unknown type of %string");
8709 compile_error(p, "unterminated quoted string meets end of file");
8713 if (term == '(') term = ')';
8714 else if (term == '[') term = ']';
8715 else if (term == '{') term = '}';
8716 else if (term == '<') term = '>';
8719 p->lex.ptok = ptok-1;
8722 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8726 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8730 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8734 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8738 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8739 return tSYMBOLS_BEG;
8742 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8743 return tQSYMBOLS_BEG;
8746 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8747 return tXSTRING_BEG;
8750 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8754 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8755 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8759 yyerror0("unknown type of %string");
8763 if ((c = nextc(p)) == '=') {
8765 SET_LEX_STATE(EXPR_BEG);
8768 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8771 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8773 return warn_balanced('%', "%%", "string literal");
8777tokadd_ident(struct parser_params *p, int c)
8780 if (tokadd_mbchar(p, c) == -1) return -1;
8782 } while (parser_is_identchar(p));
8788tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8790 ID ident = TOK_INTERN();
8792 set_yylval_name(ident);
8798parse_numvar(struct parser_params *p)
8802 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8803 const unsigned long nth_ref_max =
8804 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8805 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8806 * turned into a Fixnum, in compile.c */
8808 if (overflow || n > nth_ref_max) {
8809 /* compile_error()? */
8810 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8811 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8818static enum yytokentype
8819parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8821 const char *ptr = p->lex.pcur;
8824 SET_LEX_STATE(EXPR_END);
8825 p->lex.ptok = ptr - 1; /* from '$' */
8829 case '_': /* $_: last read line string */
8831 if (parser_is_identchar(p)) {
8839 case '~': /* $~: match-data */
8840 case '*': /* $*: argv */
8841 case '$': /* $$: pid */
8842 case '?': /* $?: last status */
8843 case '!': /* $!: error string */
8844 case '@': /* $@: error position */
8845 case '/': /* $/: input record separator */
8846 case '\\': /* $\: output record separator */
8847 case ';': /* $;: field separator */
8848 case ',': /* $,: output field separator */
8849 case '.': /* $.: last read line number */
8850 case '=': /* $=: ignorecase */
8851 case ':': /* $:: load path */
8852 case '<': /* $<: reading filename */
8853 case '>': /* $>: default output handle */
8854 case '\"': /* $": already loaded files */
8863 if (parser_is_identchar(p)) {
8864 if (tokadd_mbchar(p, c) == -1) return 0;
8872 set_yylval_name(TOK_INTERN());
8875 case '&': /* $&: last match */
8876 case '`': /* $`: string before last match */
8877 case '\'': /* $': string after last match */
8878 case '+': /* $+: string matches last paren. */
8879 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8884 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8887 case '1': case '2': case '3':
8888 case '4': case '5': case '6':
8889 case '7': case '8': case '9':
8894 } while (c != -1 && ISDIGIT(c));
8896 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8898 c = parse_numvar(p);
8899 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
8903 if (!parser_is_identchar(p)) {
8904 YYLTYPE loc = RUBY_INIT_YYLLOC();
8905 if (c == -1 || ISSPACE(c)) {
8906 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8910 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8912 parser_show_error_line(p, &loc);
8913 set_yylval_noname();
8921 if (tokadd_ident(p, c)) return 0;
8922 SET_LEX_STATE(EXPR_END);
8923 tokenize_ident(p, last_state);
8929parser_numbered_param(struct parser_params *p, int n)
8931 if (n < 0) return false;
8933 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8936 if (p->max_numparam == ORDINAL_PARAM) {
8937 compile_error(p, "ordinary parameter is defined");
8940 struct vtable *args = p->lvtbl->args;
8941 if (p->max_numparam < n) {
8942 p->max_numparam = n;
8944 while (n > args->pos) {
8945 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8951static enum yytokentype
8952parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8954 const char *ptr = p->lex.pcur;
8955 enum yytokentype result = tIVAR;
8956 register int c = nextc(p);
8959 p->lex.ptok = ptr - 1; /* from '@' */
8967 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8968 if (c == -1 || !parser_is_identchar(p)) {
8970 RUBY_SET_YYLLOC(loc);
8971 if (result == tIVAR) {
8972 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8975 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8977 parser_show_error_line(p, &loc);
8978 set_yylval_noname();
8979 SET_LEX_STATE(EXPR_END);
8982 else if (ISDIGIT(c)) {
8984 RUBY_SET_YYLLOC(loc);
8985 if (result == tIVAR) {
8986 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8989 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8991 parser_show_error_line(p, &loc);
8992 set_yylval_noname();
8993 SET_LEX_STATE(EXPR_END);
8997 if (tokadd_ident(p, c)) return 0;
8998 tokenize_ident(p, last_state);
9002static enum yytokentype
9003parse_ident(struct parser_params *p, int c, int cmd_state)
9005 enum yytokentype result;
9006 int mb = ENC_CODERANGE_7BIT;
9007 const enum lex_state_e last_state = p->lex.state;
9011 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9012 if (tokadd_mbchar(p, c) == -1) return 0;
9014 } while (parser_is_identchar(p));
9015 if ((c == '!' || c == '?') && !peek(p, '=')) {
9019 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9020 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9021 result = tIDENTIFIER;
9025 result = tCONSTANT; /* assume provisionally */
9030 if (IS_LABEL_POSSIBLE()) {
9031 if (IS_LABEL_SUFFIX(0)) {
9032 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9034 set_yylval_name(TOK_INTERN());
9038 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9039 const struct kwtable *kw;
9041 /* See if it is a reserved word. */
9042 kw = rb_reserved_word(tok(p), toklen(p));
9044 enum lex_state_e state = p->lex.state;
9045 if (IS_lex_state_for(state, EXPR_FNAME)) {
9046 SET_LEX_STATE(EXPR_ENDFN);
9047 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9050 SET_LEX_STATE(kw->state);
9051 if (IS_lex_state(EXPR_BEG)) {
9052 p->command_start = TRUE;
9054 if (kw->id[0] == keyword_do) {
9055 if (lambda_beginning_p()) {
9056 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9057 return keyword_do_LAMBDA;
9059 if (COND_P()) return keyword_do_cond;
9060 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9061 return keyword_do_block;
9064 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9067 if (kw->id[0] != kw->id[1])
9068 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9074 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9076 SET_LEX_STATE(EXPR_CMDARG);
9079 SET_LEX_STATE(EXPR_ARG);
9082 else if (p->lex.state == EXPR_FNAME) {
9083 SET_LEX_STATE(EXPR_ENDFN);
9086 SET_LEX_STATE(EXPR_END);
9089 ident = tokenize_ident(p, last_state);
9090 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9091 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9092 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9093 lvar_defined(p, ident)) {
9094 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9099static enum yytokentype
9100parser_yylex(struct parser_params *p)
9106 enum lex_state_e last_state;
9107 int fallthru = FALSE;
9108 int token_seen = p->token_seen;
9110 if (p->lex.strterm) {
9111 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9112 return here_document(p, &p->lex.strterm->u.heredoc);
9116 return parse_string(p, &p->lex.strterm->u.literal);
9119 cmd_state = p->command_start;
9120 p->command_start = FALSE;
9121 p->token_seen = TRUE;
9123 last_state = p->lex.state;
9127 switch (c = nextc(p)) {
9128 case '\0': /* NUL */
9129 case '\004': /* ^D */
9130 case '\032': /* ^Z */
9131 case -1: /* end of script. */
9138 /* carried over with p->lex.nextline for nextc() */
9139 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9142 case ' ': case '\t': case '\f':
9143 case '\13': /* '\v' */
9146 while ((c = nextc(p))) {
9148 case ' ': case '\t': case '\f': case '\r':
9149 case '\13': /* '\v' */
9157 dispatch_scan_event(p, tSP);
9161 case '#': /* it's a comment */
9162 p->token_seen = token_seen;
9163 /* no magic_comment in shebang line */
9164 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9165 if (comment_at_top(p)) {
9166 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9170 dispatch_scan_event(p, tCOMMENT);
9174 p->token_seen = token_seen;
9175 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9176 !IS_lex_state(EXPR_LABELED));
9177 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9179 dispatch_scan_event(p, tIGNORED_NL);
9182 if (!c && p->ctxt.in_kwarg) {
9183 goto normal_newline;
9188 switch (c = nextc(p)) {
9189 case ' ': case '\t': case '\f': case '\r':
9190 case '\13': /* '\v' */
9195 if (space_seen) dispatch_scan_event(p, tSP);
9199 dispatch_delayed_token(p, tIGNORED_NL);
9200 if (peek(p, '.') == (c == '&')) {
9202 dispatch_scan_event(p, tSP);
9207 p->ruby_sourceline--;
9208 p->lex.nextline = p->lex.lastline;
9209 case -1: /* EOF no decrement*/
9211 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9212 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9213 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9214 pushback(p, 1); /* always pushback */
9215 p->lex.ptok = p->lex.pcur;
9219 p->lex.ptok = p->lex.pcur;
9222 goto normal_newline;
9226 p->command_start = TRUE;
9227 SET_LEX_STATE(EXPR_BEG);
9231 if ((c = nextc(p)) == '*') {
9232 if ((c = nextc(p)) == '=') {
9233 set_yylval_id(idPow);
9234 SET_LEX_STATE(EXPR_BEG);
9239 rb_warning0("`**' interpreted as argument prefix");
9242 else if (IS_BEG()) {
9246 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9252 SET_LEX_STATE(EXPR_BEG);
9257 rb_warning0("`*' interpreted as argument prefix");
9260 else if (IS_BEG()) {
9264 c = warn_balanced('*', "*", "argument prefix");
9267 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9272 if (IS_AFTER_OPERATOR()) {
9273 SET_LEX_STATE(EXPR_ARG);
9279 SET_LEX_STATE(EXPR_BEG);
9292 /* skip embedded rd document */
9293 if (word_match_p(p, "begin", 5)) {
9297 dispatch_scan_event(p, tEMBDOC_BEG);
9301 dispatch_scan_event(p, tEMBDOC);
9306 compile_error(p, "embedded document meets end of file");
9309 if (c == '=' && word_match_p(p, "end", 3)) {
9315 dispatch_scan_event(p, tEMBDOC_END);
9320 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9321 if ((c = nextc(p)) == '=') {
9322 if ((c = nextc(p)) == '=') {
9331 else if (c == '>') {
9340 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9342 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9343 int token = heredoc_identifier(p);
9344 if (token) return token < 0 ? 0 : token;
9346 if (IS_AFTER_OPERATOR()) {
9347 SET_LEX_STATE(EXPR_ARG);
9350 if (IS_lex_state(EXPR_CLASS))
9351 p->command_start = TRUE;
9352 SET_LEX_STATE(EXPR_BEG);
9355 if ((c = nextc(p)) == '>') {
9362 if ((c = nextc(p)) == '=') {
9363 set_yylval_id(idLTLT);
9364 SET_LEX_STATE(EXPR_BEG);
9368 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9374 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9375 if ((c = nextc(p)) == '=') {
9379 if ((c = nextc(p)) == '=') {
9380 set_yylval_id(idGTGT);
9381 SET_LEX_STATE(EXPR_BEG);
9391 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9392 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9393 p->lex.ptok = p->lex.pcur-1;
9397 if (IS_lex_state(EXPR_FNAME)) {
9398 SET_LEX_STATE(EXPR_ENDFN);
9401 if (IS_lex_state(EXPR_DOT)) {
9403 SET_LEX_STATE(EXPR_CMDARG);
9405 SET_LEX_STATE(EXPR_ARG);
9408 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9409 return tXSTRING_BEG;
9412 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9413 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9414 p->lex.ptok = p->lex.pcur-1;
9418 return parse_qmark(p, space_seen);
9421 if ((c = nextc(p)) == '&') {
9422 SET_LEX_STATE(EXPR_BEG);
9423 if ((c = nextc(p)) == '=') {
9424 set_yylval_id(idANDOP);
9425 SET_LEX_STATE(EXPR_BEG);
9431 else if (c == '=') {
9433 SET_LEX_STATE(EXPR_BEG);
9436 else if (c == '.') {
9437 set_yylval_id(idANDDOT);
9438 SET_LEX_STATE(EXPR_DOT);
9444 (c = peekc_n(p, 1)) == -1 ||
9445 !(c == '\'' || c == '"' ||
9446 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9447 rb_warning0("`&' interpreted as argument prefix");
9451 else if (IS_BEG()) {
9455 c = warn_balanced('&', "&", "argument prefix");
9457 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9461 if ((c = nextc(p)) == '|') {
9462 SET_LEX_STATE(EXPR_BEG);
9463 if ((c = nextc(p)) == '=') {
9464 set_yylval_id(idOROP);
9465 SET_LEX_STATE(EXPR_BEG);
9469 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9478 SET_LEX_STATE(EXPR_BEG);
9481 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9487 if (IS_AFTER_OPERATOR()) {
9488 SET_LEX_STATE(EXPR_ARG);
9497 SET_LEX_STATE(EXPR_BEG);
9500 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9501 SET_LEX_STATE(EXPR_BEG);
9503 if (c != -1 && ISDIGIT(c)) {
9504 return parse_numeric(p, '+');
9508 SET_LEX_STATE(EXPR_BEG);
9510 return warn_balanced('+', "+", "unary operator");
9514 if (IS_AFTER_OPERATOR()) {
9515 SET_LEX_STATE(EXPR_ARG);
9524 SET_LEX_STATE(EXPR_BEG);
9528 SET_LEX_STATE(EXPR_ENDFN);
9531 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9532 SET_LEX_STATE(EXPR_BEG);
9534 if (c != -1 && ISDIGIT(c)) {
9539 SET_LEX_STATE(EXPR_BEG);
9541 return warn_balanced('-', "-", "unary operator");
9544 int is_beg = IS_BEG();
9545 SET_LEX_STATE(EXPR_BEG);
9546 if ((c = nextc(p)) == '.') {
9547 if ((c = nextc(p)) == '.') {
9548 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9549 rb_warn0("... at EOL, should be parenthesized?");
9551 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9552 if (IS_lex_state_for(last_state, EXPR_LABEL))
9555 return is_beg ? tBDOT3 : tDOT3;
9558 return is_beg ? tBDOT2 : tDOT2;
9561 if (c != -1 && ISDIGIT(c)) {
9562 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9563 parse_numeric(p, '.');
9564 if (ISDIGIT(prev)) {
9565 yyerror0("unexpected fraction part after numeric literal");
9568 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9570 SET_LEX_STATE(EXPR_END);
9571 p->lex.ptok = p->lex.pcur;
9575 SET_LEX_STATE(EXPR_DOT);
9579 case '0': case '1': case '2': case '3': case '4':
9580 case '5': case '6': case '7': case '8': case '9':
9581 return parse_numeric(p, c);
9586 SET_LEX_STATE(EXPR_ENDFN);
9587 p->lex.paren_nest--;
9593 SET_LEX_STATE(EXPR_END);
9594 p->lex.paren_nest--;
9598 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9599 if (!p->lex.brace_nest--) return tSTRING_DEND;
9602 SET_LEX_STATE(EXPR_END);
9603 p->lex.paren_nest--;
9609 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9610 SET_LEX_STATE(EXPR_BEG);
9613 set_yylval_id(idCOLON2);
9614 SET_LEX_STATE(EXPR_DOT);
9617 if (IS_END() || ISSPACE(c) || c == '#') {
9619 c = warn_balanced(':', ":", "symbol literal");
9620 SET_LEX_STATE(EXPR_BEG);
9625 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9628 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9634 SET_LEX_STATE(EXPR_FNAME);
9639 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9642 if ((c = nextc(p)) == '=') {
9644 SET_LEX_STATE(EXPR_BEG);
9649 arg_ambiguous(p, '/');
9650 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9653 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9654 return warn_balanced('/', "/", "regexp literal");
9657 if ((c = nextc(p)) == '=') {
9659 SET_LEX_STATE(EXPR_BEG);
9662 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9667 SET_LEX_STATE(EXPR_BEG);
9668 p->command_start = TRUE;
9672 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9676 if (IS_AFTER_OPERATOR()) {
9677 if ((c = nextc(p)) != '@') {
9680 SET_LEX_STATE(EXPR_ARG);
9683 SET_LEX_STATE(EXPR_BEG);
9691 else if (!space_seen) {
9692 /* foo( ... ) => method call, no ambiguity */
9694 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9697 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9698 rb_warning0("parentheses after method name is interpreted as "
9699 "an argument list, not a decomposed argument");
9701 p->lex.paren_nest++;
9704 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9708 p->lex.paren_nest++;
9709 if (IS_AFTER_OPERATOR()) {
9710 if ((c = nextc(p)) == ']') {
9711 p->lex.paren_nest--;
9712 SET_LEX_STATE(EXPR_ARG);
9713 if ((c = nextc(p)) == '=') {
9720 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9723 else if (IS_BEG()) {
9726 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9729 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9735 ++p->lex.brace_nest;
9736 if (lambda_beginning_p())
9738 else if (IS_lex_state(EXPR_LABELED))
9739 c = tLBRACE; /* hash */
9740 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9741 c = '{'; /* block (primary) */
9742 else if (IS_lex_state(EXPR_ENDARG))
9743 c = tLBRACE_ARG; /* block (expr) */
9745 c = tLBRACE; /* hash */
9747 p->command_start = TRUE;
9748 SET_LEX_STATE(EXPR_BEG);
9751 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9753 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9762 dispatch_scan_event(p, tSP);
9763 goto retry; /* skip \\n */
9765 if (c == ' ') return tSP;
9766 if (ISSPACE(c)) return c;
9771 return parse_percent(p, space_seen, last_state);
9774 return parse_gvar(p, last_state);
9777 return parse_atmark(p, last_state);
9780 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9781 p->ruby__end__seen = 1;
9787 dispatch_scan_event(p, k__END__);
9795 if (!parser_is_identchar(p)) {
9796 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9805 return parse_ident(p, c, cmd_state);
9808static enum yytokentype
9809yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9815 t = parser_yylex(p);
9817 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9818 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9820 RUBY_SET_YYLLOC(*yylloc);
9822 if (has_delayed_token(p))
9823 dispatch_delayed_token(p, t);
9825 dispatch_scan_event(p, t);
9830#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9833node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9835 NODE *n = rb_ast_newnode(p->ast, type);
9837 rb_node_init(n, type, a0, a1, a2);
9840 nd_set_node_id(n, parser_get_node_id(p));
9845nd_set_loc(NODE *nd, const YYLTYPE *loc)
9848 nd_set_line(nd, loc->beg_pos.lineno);
9853static enum node_type
9854nodetype(NODE *node) /* for debug */
9856 return (enum node_type)nd_type(node);
9862 return nd_line(node);
9866newline_node(NODE *node)
9869 node = remove_begin(node);
9870 node->flags |= NODE_FL_NEWLINE;
9876fixpos(NODE *node, NODE *orig)
9880 nd_set_line(node, nd_line(orig));
9884parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9886 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9890parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9892 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9896block_append(struct parser_params *p, NODE *head, NODE *tail)
9898 NODE *end, *h = head, *nd;
9900 if (tail == 0) return head;
9902 if (h == 0) return tail;
9903 switch (nd_type(h)) {
9910 parser_warning(p, h, "unused literal ignored");
9913 h = end = NEW_BLOCK(head, &head->nd_loc);
9923 switch (nd_type(nd)) {
9929 if (RTEST(ruby_verbose)) {
9930 parser_warning(p, tail, "statement not reached");
9938 if (nd_type(tail) != NODE_BLOCK) {
9939 tail = NEW_BLOCK(tail, &tail->nd_loc);
9940 tail->nd_end = tail;
9942 end->nd_next = tail;
9943 h->nd_end = tail->nd_end;
9944 nd_set_last_loc(head, nd_last_loc(tail));
9948/* append item to the list */
9950list_append(struct parser_params *p, NODE *list, NODE *item)
9954 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9955 if (list->nd_next) {
9956 last = list->nd_next->nd_end;
9963 last->nd_next = NEW_LIST(item, &item->nd_loc);
9964 list->nd_next->nd_end = last->nd_next;
9966 nd_set_last_loc(list, nd_last_loc(item));
9971/* concat two lists */
9973list_concat(NODE *head, NODE *tail)
9977 if (head->nd_next) {
9978 last = head->nd_next->nd_end;
9984 head->nd_alen += tail->nd_alen;
9985 last->nd_next = tail;
9986 if (tail->nd_next) {
9987 head->nd_next->nd_end = tail->nd_next->nd_end;
9990 head->nd_next->nd_end = tail;
9993 nd_set_last_loc(head, nd_last_loc(tail));
9999literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10001 if (NIL_P(tail)) return 1;
10002 if (!rb_enc_compatible(head, tail)) {
10003 compile_error(p, "string literal encodings differ (%s / %s)",
10004 rb_enc_name(rb_enc_get(head)),
10005 rb_enc_name(rb_enc_get(tail)));
10006 rb_str_resize(head, 0);
10007 rb_str_resize(tail, 0);
10010 rb_str_buf_append(head, tail);
10015string_literal_head(enum node_type htype, NODE *head)
10017 if (htype != NODE_DSTR) return Qfalse;
10018 if (head->nd_next) {
10019 head = head->nd_next->nd_end->nd_head;
10020 if (!head || nd_type(head) != NODE_STR) return Qfalse;
10022 const VALUE lit = head->nd_lit;
10023 ASSUME(lit != Qfalse);
10027/* concat two string literals */
10029literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10031 enum node_type htype;
10034 if (!head) return tail;
10035 if (!tail) return head;
10037 htype = nd_type(head);
10038 if (htype == NODE_EVSTR) {
10039 head = new_dstr(p, head, loc);
10042 if (p->heredoc_indent > 0) {
10045 nd_set_type(head, NODE_DSTR);
10047 return list_append(p, head, tail);
10052 switch (nd_type(tail)) {
10054 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10058 lit = head->nd_lit;
10060 if (htype == NODE_STR) {
10061 if (!literal_concat0(p, lit, tail->nd_lit)) {
10063 rb_discard_node(p, head);
10064 rb_discard_node(p, tail);
10067 rb_discard_node(p, tail);
10070 list_append(p, head, tail);
10075 if (htype == NODE_STR) {
10076 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10078 tail->nd_lit = head->nd_lit;
10079 rb_discard_node(p, head);
10082 else if (NIL_P(tail->nd_lit)) {
10084 head->nd_alen += tail->nd_alen - 1;
10085 if (!head->nd_next) {
10086 head->nd_next = tail->nd_next;
10088 else if (tail->nd_next) {
10089 head->nd_next->nd_end->nd_next = tail->nd_next;
10090 head->nd_next->nd_end = tail->nd_next->nd_end;
10092 rb_discard_node(p, tail);
10094 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10095 if (!literal_concat0(p, lit, tail->nd_lit))
10097 tail->nd_lit = Qnil;
10101 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10106 if (htype == NODE_STR) {
10107 nd_set_type(head, NODE_DSTR);
10110 list_append(p, head, tail);
10117evstr2dstr(struct parser_params *p, NODE *node)
10119 if (nd_type(node) == NODE_EVSTR) {
10120 node = new_dstr(p, node, &node->nd_loc);
10126new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10131 switch (nd_type(node)) {
10133 nd_set_type(node, NODE_DSTR);
10134 case NODE_DSTR: case NODE_EVSTR:
10138 return NEW_EVSTR(head, loc);
10142new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10144 VALUE lit = STR_NEW0();
10145 NODE *dstr = NEW_DSTR(lit, loc);
10146 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10147 return list_append(p, dstr, node);
10151call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10152 const YYLTYPE *op_loc, const YYLTYPE *loc)
10157 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10158 nd_set_line(expr, op_loc->beg_pos.lineno);
10163call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10167 opcall = NEW_OPCALL(recv, id, 0, loc);
10168 nd_set_line(opcall, op_loc->beg_pos.lineno);
10173new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10175 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10176 nd_set_line(qcall, op_loc->beg_pos.lineno);
10181new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10184 if (block) block_dup_check(p, args, block);
10185 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10186 if (block) ret = method_add_block(p, ret, block, loc);
10191#define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
10193match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10196 int line = op_loc->beg_pos.lineno;
10200 if (node1 && (n = nd_once_body(node1)) != 0) {
10201 switch (nd_type(n)) {
10204 NODE *match = NEW_MATCH2(node1, node2, loc);
10205 nd_set_line(match, line);
10210 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10211 const VALUE lit = n->nd_lit;
10212 NODE *match = NEW_MATCH2(node1, node2, loc);
10213 match->nd_args = reg_named_capture_assign(p, lit, loc);
10214 nd_set_line(match, line);
10220 if (node2 && (n = nd_once_body(node2)) != 0) {
10223 switch (nd_type(n)) {
10225 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10228 match3 = NEW_MATCH3(node2, node1, loc);
10233 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10234 nd_set_line(n, line);
10238# if WARN_PAST_SCOPE
10240past_dvar_p(struct parser_params *p, ID id)
10242 struct vtable *past = p->lvtbl->past;
10244 if (vtable_included(past, id)) return 1;
10252numparam_nested_p(struct parser_params *p)
10254 struct local_vars *local = p->lvtbl;
10255 NODE *outer = local->numparam.outer;
10256 NODE *inner = local->numparam.inner;
10257 if (outer || inner) {
10258 NODE *used = outer ? outer : inner;
10259 compile_error(p, "numbered parameter is already used in\n"
10260 "%s:%d: %s block here",
10261 p->ruby_sourcefile, nd_line(used),
10262 outer ? "outer" : "inner");
10263 parser_show_error_line(p, &used->nd_loc);
10270gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10276 return NEW_SELF(loc);
10278 return NEW_NIL(loc);
10280 return NEW_TRUE(loc);
10281 case keyword_false:
10282 return NEW_FALSE(loc);
10283 case keyword__FILE__:
10285 VALUE file = p->ruby_sourcefile_string;
10287 file = rb_str_new(0, 0);
10289 file = rb_str_dup(file);
10290 node = NEW_STR(file, loc);
10291 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10294 case keyword__LINE__:
10295 return NEW_LIT(INT2FIX(p->tokline), loc);
10296 case keyword__ENCODING__:
10297 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10298 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10302 switch (id_type(id)) {
10304 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10305 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10306 if (id == p->cur_arg) {
10307 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10310 if (vidp) *vidp |= LVAR_USED;
10311 node = NEW_DVAR(id, loc);
10314 if (local_id_ref(p, id, &vidp)) {
10315 if (id == p->cur_arg) {
10316 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10319 if (vidp) *vidp |= LVAR_USED;
10320 node = NEW_LVAR(id, loc);
10323 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10324 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10325 if (numparam_nested_p(p)) return 0;
10326 node = NEW_DVAR(id, loc);
10327 struct local_vars *local = p->lvtbl;
10328 if (!local->numparam.current) local->numparam.current = node;
10331# if WARN_PAST_SCOPE
10332 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10333 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10336 /* method call without arguments */
10337 return NEW_VCALL(id, loc);
10339 return NEW_GVAR(id, loc);
10341 return NEW_IVAR(id, loc);
10343 return NEW_CONST(id, loc);
10345 return NEW_CVAR(id, loc);
10347 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10352opt_arg_append(NODE *opt_list, NODE *opt)
10354 NODE *opts = opt_list;
10355 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10357 while (opts->nd_next) {
10358 opts = opts->nd_next;
10359 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10361 opts->nd_next = opt;
10367kwd_append(NODE *kwlist, NODE *kw)
10370 NODE *kws = kwlist;
10371 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10372 while (kws->nd_next) {
10373 kws = kws->nd_next;
10374 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10382new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10384 return NEW_DEFINED(remove_begin_all(expr), loc);
10388symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10390 enum node_type type = nd_type(symbol);
10393 nd_set_type(symbol, NODE_DSYM);
10396 nd_set_type(symbol, NODE_LIT);
10397 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10400 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10402 return list_append(p, symbols, symbol);
10406new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10412 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10413 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10416 switch (nd_type(node)) {
10419 VALUE src = node->nd_lit;
10420 nd_set_type(node, NODE_LIT);
10421 nd_set_loc(node, loc);
10422 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10427 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10428 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10431 nd_set_type(node, NODE_DREGX);
10432 nd_set_loc(node, loc);
10433 node->nd_cflag = options & RE_OPTION_MASK;
10434 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10435 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10436 NODE *frag = list->nd_head;
10437 enum node_type type = nd_type(frag);
10438 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10439 VALUE tail = frag->nd_lit;
10440 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10441 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10442 if (!literal_concat0(p, lit, tail)) {
10443 return NEW_NIL(loc); /* dummy node on error */
10445 rb_str_resize(tail, 0);
10446 prev->nd_next = list->nd_next;
10447 rb_discard_node(p, list->nd_head);
10448 rb_discard_node(p, list);
10459 if (!node->nd_next) {
10460 VALUE src = node->nd_lit;
10461 nd_set_type(node, NODE_LIT);
10462 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10464 if (options & RE_OPTION_ONCE) {
10465 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10473new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10476 return NEW_KW_ARG(0, (k), loc);
10480new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10483 VALUE lit = STR_NEW0();
10484 NODE *xstr = NEW_XSTR(lit, loc);
10485 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10488 switch (nd_type(node)) {
10490 nd_set_type(node, NODE_XSTR);
10491 nd_set_loc(node, loc);
10494 nd_set_type(node, NODE_DXSTR);
10495 nd_set_loc(node, loc);
10498 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10505check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10509 if (!arg || !p->case_labels) return;
10511 lit = rb_node_case_when_optimizable_literal(arg);
10512 if (lit == Qundef) return;
10513 if (nd_type(arg) == NODE_STR) {
10514 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10517 if (NIL_P(p->case_labels)) {
10518 p->case_labels = rb_obj_hide(rb_hash_new());
10521 VALUE line = rb_hash_lookup(p->case_labels, lit);
10522 if (!NIL_P(line)) {
10523 rb_warning1("duplicated `when' clause with line %d is ignored",
10528 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10533id_is_var(struct parser_params *p, ID id)
10535 if (is_notop_id(id)) {
10536 switch (id & ID_SCOPE_MASK) {
10537 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10540 if (dyna_in_block(p)) {
10541 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10543 if (local_id(p, id)) return 1;
10544 /* method call without arguments */
10548 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10553new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10555 VALUE src = 0, err;
10557 if (ripper_is_node_yylval(re)) {
10558 src = RNODE(re)->nd_cval;
10559 re = RNODE(re)->nd_rval;
10561 if (ripper_is_node_yylval(opt)) {
10562 options = (int)RNODE(opt)->nd_tag;
10563 opt = RNODE(opt)->nd_rval;
10565 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10566 compile_error(p, "%"PRIsVALUE, err);
10568 return dispatch2(regexp_literal, re, opt);
10570#endif /* !RIPPER */
10574static const char rb_parser_lex_state_names[][8] = {
10575 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10576 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10577 "LABEL", "LABELED","FITEM",
10581append_lex_state_name(enum lex_state_e state, VALUE buf)
10584 unsigned int mask = 1;
10585 static const char none[] = "NONE";
10587 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10588 if ((unsigned)state & mask) {
10590 rb_str_cat(buf, "|", 1);
10593 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10597 rb_str_cat(buf, none, sizeof(none)-1);
10603flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10605 VALUE mesg = p->debug_buffer;
10607 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10608 p->debug_buffer = Qnil;
10609 rb_io_puts(1, &mesg, out);
10611 if (!NIL_P(str) && RSTRING_LEN(str)) {
10612 rb_io_write(p->debug_output, str);
10617rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10618 enum lex_state_e to, int line)
10621 mesg = rb_str_new_cstr("lex_state: ");
10622 append_lex_state_name(from, mesg);
10623 rb_str_cat_cstr(mesg, " -> ");
10624 append_lex_state_name(to, mesg);
10625 rb_str_catf(mesg, " at line %d\n", line);
10626 flush_debug_buffer(p, p->debug_output, mesg);
10631rb_parser_lex_state_name(enum lex_state_e state)
10633 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10637append_bitstack_value(stack_type stack, VALUE mesg)
10640 rb_str_cat_cstr(mesg, "0");
10643 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10644 for (; mask && !(stack & mask); mask >>= 1) continue;
10645 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10650rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10651 const char *name, int line)
10653 VALUE mesg = rb_sprintf("%s: ", name);
10654 append_bitstack_value(stack, mesg);
10655 rb_str_catf(mesg, " at line %d\n", line);
10656 flush_debug_buffer(p, p->debug_output, mesg);
10660rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10663 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10666 rb_str_vcatf(mesg, fmt, ap);
10668 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10671 mesg = rb_str_new(0, 0);
10672 append_lex_state_name(p->lex.state, mesg);
10673 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10674 rb_str_resize(mesg, 0);
10675 append_bitstack_value(p->cond_stack, mesg);
10676 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10677 rb_str_resize(mesg, 0);
10678 append_bitstack_value(p->cmdarg_stack, mesg);
10679 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10680 if (p->debug_output == rb_ractor_stdout())
10681 p->debug_output = rb_ractor_stderr();
10686rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10688 int sourceline = here->sourceline;
10689 int beg_pos = (int)here->offset - here->quote
10690 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10691 int end_pos = (int)here->offset + here->length + here->quote;
10693 yylloc->beg_pos.lineno = sourceline;
10694 yylloc->beg_pos.column = beg_pos;
10695 yylloc->end_pos.lineno = sourceline;
10696 yylloc->end_pos.column = end_pos;
10701rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10703 yylloc->beg_pos.lineno = p->ruby_sourceline;
10704 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10705 yylloc->end_pos.lineno = p->ruby_sourceline;
10706 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10711rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10713 yylloc->beg_pos.lineno = p->ruby_sourceline;
10714 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10715 yylloc->end_pos.lineno = p->ruby_sourceline;
10716 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10719#endif /* !RIPPER */
10722parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10727 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10728 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10730 v = rb_id2str(valp->id);
10732 v = valp->node->nd_rval;
10734 rb_parser_printf(p, "%"PRIsVALUE, v);
10736 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10737 case tSTRING_CONTENT: case tCHAR:
10739 v = valp->node->nd_lit;
10743 rb_parser_printf(p, "%+"PRIsVALUE, v);
10747 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10749 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10754 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10756 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10765assignable0(struct parser_params *p, ID id, const char **err)
10767 if (!id) return -1;
10770 *err = "Can't change the value of self";
10773 *err = "Can't assign to nil";
10776 *err = "Can't assign to true";
10778 case keyword_false:
10779 *err = "Can't assign to false";
10781 case keyword__FILE__:
10782 *err = "Can't assign to __FILE__";
10784 case keyword__LINE__:
10785 *err = "Can't assign to __LINE__";
10787 case keyword__ENCODING__:
10788 *err = "Can't assign to __ENCODING__";
10791 switch (id_type(id)) {
10793 if (dyna_in_block(p)) {
10794 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10795 compile_error(p, "Can't assign to numbered parameter _%d",
10796 NUMPARAM_ID_TO_IDX(id));
10799 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10800 if (dvar_defined(p, id)) return NODE_DASGN;
10801 if (local_id(p, id)) return NODE_LASGN;
10803 return NODE_DASGN_CURR;
10806 if (!local_id(p, id)) local_var(p, id);
10810 case ID_GLOBAL: return NODE_GASGN;
10811 case ID_INSTANCE: return NODE_IASGN;
10813 if (!p->ctxt.in_def) return NODE_CDECL;
10814 *err = "dynamic constant assignment";
10816 case ID_CLASS: return NODE_CVASGN;
10818 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10825assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10827 const char *err = 0;
10828 int node_type = assignable0(p, id, &err);
10829 switch (node_type) {
10830 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10831 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10832 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10833 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10834 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10835 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10836 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10838 if (err) yyerror1(loc, err);
10839 return NEW_BEGIN(0, loc);
10843assignable(struct parser_params *p, VALUE lhs)
10845 const char *err = 0;
10846 assignable0(p, get_id(lhs), &err);
10847 if (err) lhs = assign_error(p, err, lhs);
10853is_private_local_id(ID name)
10856 if (name == idUScore) return 1;
10857 if (!is_local_id(name)) return 0;
10858 s = rb_id2str(name);
10860 return RSTRING_PTR(s)[0] == '_';
10864shadowing_lvar_0(struct parser_params *p, ID name)
10866 if (is_private_local_id(name)) return 1;
10867 if (dyna_in_block(p)) {
10868 if (dvar_curr(p, name)) {
10869 yyerror0("duplicated argument name");
10871 else if (dvar_defined(p, name) || local_id(p, name)) {
10872 vtable_add(p->lvtbl->vars, name);
10873 if (p->lvtbl->used) {
10874 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10880 if (local_id(p, name)) {
10881 yyerror0("duplicated argument name");
10888shadowing_lvar(struct parser_params *p, ID name)
10890 shadowing_lvar_0(p, name);
10895new_bv(struct parser_params *p, ID name)
10898 if (!is_local_id(name)) {
10899 compile_error(p, "invalid local variable - %"PRIsVALUE,
10903 if (!shadowing_lvar_0(p, name)) return;
10909aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10911 return NEW_ATTRASGN(recv, tASET, idx, loc);
10915block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10917 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10918 compile_error(p, "both block arg and actual block given");
10923attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10925 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10926 return NEW_ATTRASGN(recv, id, 0, loc);
10930rb_backref_error(struct parser_params *p, NODE *node)
10932 switch (nd_type(node)) {
10934 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10936 case NODE_BACK_REF:
10937 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10943backref_error(struct parser_params *p, NODE *ref, VALUE expr)
10945 VALUE mesg = rb_str_new_cstr("Can't set variable ");
10946 rb_str_append(mesg, ref->nd_cval);
10947 return dispatch2(assign_error, mesg, expr);
10953arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10955 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10956 switch (nd_type(node1)) {
10958 return list_append(p, node1, node2);
10959 case NODE_BLOCK_PASS:
10960 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10961 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10963 case NODE_ARGSPUSH:
10964 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10965 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10966 nd_set_type(node1, NODE_ARGSCAT);
10969 if (nd_type(node1->nd_body) != NODE_LIST) break;
10970 node1->nd_body = list_append(p, node1->nd_body, node2);
10971 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10974 return NEW_ARGSPUSH(node1, node2, loc);
10978arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10980 if (!node2) return node1;
10981 switch (nd_type(node1)) {
10982 case NODE_BLOCK_PASS:
10983 if (node1->nd_head)
10984 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10986 node1->nd_head = NEW_LIST(node2, loc);
10988 case NODE_ARGSPUSH:
10989 if (nd_type(node2) != NODE_LIST) break;
10990 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10991 nd_set_type(node1, NODE_ARGSCAT);
10994 if (nd_type(node2) != NODE_LIST ||
10995 nd_type(node1->nd_body) != NODE_LIST) break;
10996 node1->nd_body = list_concat(node1->nd_body, node2);
10999 return NEW_ARGSCAT(node1, node2, loc);
11003last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11006 if ((n1 = splat_array(args)) != 0) {
11007 return list_append(p, n1, last_arg);
11009 return arg_append(p, args, last_arg, loc);
11013rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11016 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
11017 return list_concat(n1, rest_arg);
11019 return arg_concat(p, args, rest_arg, loc);
11023splat_array(NODE* node)
11025 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
11026 if (nd_type(node) == NODE_LIST) return node;
11031mark_lvar_used(struct parser_params *p, NODE *rhs)
11035 switch (nd_type(rhs)) {
11037 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11038 if (vidp) *vidp |= LVAR_USED;
11042 case NODE_DASGN_CURR:
11043 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11044 if (vidp) *vidp |= LVAR_USED;
11049 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11050 mark_lvar_used(p, rhs->nd_head);
11058const_decl_path(struct parser_params *p, NODE **dest)
11061 if (nd_type(n) != NODE_CALL) {
11062 const YYLTYPE *loc = &n->nd_loc;
11065 path = rb_id2str(n->nd_vid);
11069 path = rb_ary_new();
11070 for (; n && nd_type(n) == NODE_COLON2; n = n->nd_head) {
11071 rb_ary_push(path, rb_id2str(n->nd_mid));
11073 if (n && nd_type(n) == NODE_CONST) {
11075 rb_ary_push(path, rb_id2str(n->nd_vid));
11077 else if (n && nd_type(n) == NODE_COLON3) {
11079 rb_ary_push(path, rb_str_new(0, 0));
11082 // expression::Name
11083 rb_ary_push(path, rb_str_new_cstr("..."));
11085 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11086 path = rb_fstring(path);
11088 *dest = n = NEW_LIT(path, loc);
11093extern VALUE rb_mRubyVMFrozenCore;
11096make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11098 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11101 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11102 NEW_LIST(value, loc), loc);
11105 return NEW_CALL(fcore, rb_intern("make_shareable"),
11106 NEW_LIST(value, loc), loc);
11111ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11113 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11114 NODE *args = NEW_LIST(value, loc);
11115 args = list_append(p, args, const_decl_path(p, dest));
11116 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11119static int is_static_content(NODE *node);
11122shareable_literal_value(NODE *node)
11124 if (!node) return Qnil;
11125 enum node_type type = nd_type(node);
11134 return node->nd_lit;
11140#ifndef SHAREABLE_BARE_EXPRESSION
11141#define SHAREABLE_BARE_EXPRESSION 1
11145shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11146 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11148# define shareable_literal_constant_next(n) \
11149 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11152 if (!value) return 0;
11153 enum node_type type = nd_type(value);
11162 if (shareable == shareable_literal) {
11163 value = NEW_CALL(value, idUMinus, 0, loc);
11168 lit = rb_fstring(value->nd_lit);
11169 nd_set_type(value, NODE_LIT);
11170 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11174 lit = rb_ary_new();
11175 OBJ_FREEZE_RAW(lit);
11176 return NEW_LIT(lit, loc);
11179 lit = rb_ary_new();
11180 for (NODE *n = value; n; n = n->nd_next) {
11181 NODE *elt = n->nd_head;
11183 elt = shareable_literal_constant_next(elt);
11187 else if (RTEST(lit)) {
11193 VALUE e = shareable_literal_value(elt);
11195 rb_ary_push(lit, e);
11199 lit = Qnil; /* make shareable at runtime */
11206 if (!value->nd_brace) return 0;
11207 lit = rb_hash_new();
11208 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11209 NODE *key = n->nd_head;
11210 NODE *val = n->nd_next->nd_head;
11212 key = shareable_literal_constant_next(key);
11216 else if (RTEST(lit)) {
11217 rb_hash_clear(lit);
11222 val = shareable_literal_constant_next(val);
11224 n->nd_next->nd_head = val;
11226 else if (RTEST(lit)) {
11227 rb_hash_clear(lit);
11232 VALUE k = shareable_literal_value(key);
11233 VALUE v = shareable_literal_value(val);
11234 if (k != Qundef && v != Qundef) {
11235 rb_hash_aset(lit, k, v);
11238 rb_hash_clear(lit);
11239 lit = Qnil; /* make shareable at runtime */
11246 if (shareable == shareable_literal &&
11247 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11248 return ensure_shareable_node(p, dest, value, loc);
11253 /* Array or Hash */
11254 if (!lit) return 0;
11256 // if shareable_literal, all elements should have been ensured
11258 value = make_shareable_node(p, value, false, loc);
11261 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11265# undef shareable_literal_constant_next
11269shareable_constant_value(struct parser_params *p, enum shareability shareable,
11270 NODE *lhs, NODE *value, const YYLTYPE *loc)
11272 if (!value) return 0;
11273 switch (shareable) {
11274 case shareable_none:
11277 case shareable_literal:
11279 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11280 if (lit) return lit;
11285 case shareable_copy:
11286 case shareable_everything:
11288 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11289 if (lit) return lit;
11290 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11295 UNREACHABLE_RETURN(0);
11300node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11302 if (!lhs) return 0;
11304 switch (nd_type(lhs)) {
11306 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11313 case NODE_DASGN_CURR:
11316 lhs->nd_value = rhs;
11317 nd_set_loc(lhs, loc);
11320 case NODE_ATTRASGN:
11321 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11322 nd_set_loc(lhs, loc);
11326 /* should not happen */
11334value_expr_check(struct parser_params *p, NODE *node)
11336 NODE *void_node = 0, *vn;
11339 rb_warning0("empty expression");
11342 switch (nd_type(node)) {
11348 return void_node ? void_node : node;
11351 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
11352 compile_error(p, "unexpected node");
11355 if (node->nd_body->nd_body) {
11358 /* single line pattern matching */
11359 return void_node ? void_node : node;
11362 while (node->nd_next) {
11363 node = node->nd_next;
11365 node = node->nd_head;
11369 node = node->nd_body;
11374 if (!node->nd_body) {
11377 else if (!node->nd_else) {
11380 vn = value_expr_check(p, node->nd_body);
11381 if (!vn) return NULL;
11382 if (!void_node) void_node = vn;
11383 node = node->nd_else;
11388 node = node->nd_1st;
11393 case NODE_DASGN_CURR:
11395 mark_lvar_used(p, node);
11407value_expr_gen(struct parser_params *p, NODE *node)
11409 NODE *void_node = value_expr_check(p, node);
11411 yyerror1(&void_node->nd_loc, "void value expression");
11412 /* or "control never reach"? */
11418void_expr(struct parser_params *p, NODE *node)
11420 const char *useless = 0;
11422 if (!RTEST(ruby_verbose)) return;
11424 if (!node || !(node = nd_once_body(node))) return;
11425 switch (nd_type(node)) {
11427 switch (node->nd_mid) {
11446 useless = rb_id2name(node->nd_mid);
11457 case NODE_BACK_REF:
11458 useless = "a variable";
11461 useless = "a constant";
11467 useless = "a literal";
11492 useless = "defined?";
11497 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11502void_stmts(struct parser_params *p, NODE *node)
11504 NODE *const n = node;
11505 if (!RTEST(ruby_verbose)) return n;
11506 if (!node) return n;
11507 if (nd_type(node) != NODE_BLOCK) return n;
11509 while (node->nd_next) {
11510 void_expr(p, node->nd_head);
11511 node = node->nd_next;
11517remove_begin(NODE *node)
11519 NODE **n = &node, *n1 = node;
11520 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
11521 *n = n1 = n1->nd_body;
11527remove_begin_all(NODE *node)
11529 NODE **n = &node, *n1 = node;
11530 while (n1 && nd_type(n1) == NODE_BEGIN) {
11531 *n = n1 = n1->nd_body;
11537reduce_nodes(struct parser_params *p, NODE **body)
11539 NODE *node = *body;
11542 *body = NEW_NIL(&NULL_LOC);
11545#define subnodes(n1, n2) \
11546 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11547 (!node->n2) ? (body = &node->n1, 1) : \
11548 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11551 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11552 switch (nd_type(node)) {
11558 *body = node = node->nd_stts;
11559 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11562 *body = node = node->nd_body;
11563 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11566 body = &node->nd_end->nd_head;
11570 if (subnodes(nd_body, nd_else)) break;
11573 body = &node->nd_body;
11576 if (!subnodes(nd_body, nd_next)) goto end;
11579 if (!subnodes(nd_head, nd_resq)) goto end;
11582 if (node->nd_else) {
11583 body = &node->nd_resq;
11586 if (!subnodes(nd_head, nd_resq)) goto end;
11592 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11599is_static_content(NODE *node)
11601 if (!node) return 1;
11602 switch (nd_type(node)) {
11604 if (!(node = node->nd_head)) break;
11607 if (!is_static_content(node->nd_head)) return 0;
11608 } while ((node = node->nd_next) != 0);
11623assign_in_cond(struct parser_params *p, NODE *node)
11625 switch (nd_type(node)) {
11629 case NODE_DASGN_CURR:
11638 if (!node->nd_value) return 1;
11639 if (is_static_content(node->nd_value)) {
11640 /* reports always */
11641 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11652#define SWITCH_BY_COND_TYPE(t, w, arg) \
11654 case COND_IN_OP: break; \
11655 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11656 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11659static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11662range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11664 enum node_type type;
11666 if (node == 0) return 0;
11668 type = nd_type(node);
11670 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11671 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11672 ID lineno = rb_intern("$.");
11673 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11675 return cond0(p, node, COND_IN_FF, loc);
11679cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11681 if (node == 0) return 0;
11682 if (!(node = nd_once_body(node))) return 0;
11683 assign_in_cond(p, node);
11685 switch (nd_type(node)) {
11689 SWITCH_BY_COND_TYPE(type, warn, "string ")
11693 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11695 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11699 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11700 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11705 node->nd_beg = range_op(p, node->nd_beg, loc);
11706 node->nd_end = range_op(p, node->nd_end, loc);
11707 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11708 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11712 SWITCH_BY_COND_TYPE(type, warning, "string ")
11716 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11717 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11718 nd_set_type(node, NODE_MATCH);
11720 else if (node->nd_lit == Qtrue ||
11721 node->nd_lit == Qfalse) {
11722 /* booleans are OK, e.g., while true */
11725 SWITCH_BY_COND_TYPE(type, warning, "")
11734cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11736 if (node == 0) return 0;
11737 return cond0(p, node, COND_IN_COND, loc);
11741method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11743 if (node == 0) return 0;
11744 return cond0(p, node, COND_IN_OP, loc);
11748new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11750 YYLTYPE loc = {*pos, *pos};
11751 return NEW_NIL(&loc);
11755new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11757 if (!cc) return right;
11758 cc = cond0(p, cc, COND_IN_COND, loc);
11759 return newline_node(NEW_IF(cc, left, right, loc));
11763new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11765 if (!cc) return right;
11766 cc = cond0(p, cc, COND_IN_COND, loc);
11767 return newline_node(NEW_UNLESS(cc, left, right, loc));
11771logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11772 const YYLTYPE *op_loc, const YYLTYPE *loc)
11774 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11777 if (left && (enum node_type)nd_type(left) == type) {
11778 NODE *node = left, *second;
11779 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11782 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11783 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11784 left->nd_loc.end_pos = loc->end_pos;
11787 op = NEW_NODE(type, left, right, 0, loc);
11788 nd_set_line(op, op_loc->beg_pos.lineno);
11793no_blockarg(struct parser_params *p, NODE *node)
11795 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11796 compile_error(p, "block argument should not be given");
11801ret_args(struct parser_params *p, NODE *node)
11804 no_blockarg(p, node);
11805 if (nd_type(node) == NODE_LIST) {
11806 if (node->nd_next == 0) {
11807 node = node->nd_head;
11810 nd_set_type(node, NODE_VALUES);
11818new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11820 if (node) no_blockarg(p, node);
11822 return NEW_YIELD(node, loc);
11826negate_lit(struct parser_params *p, VALUE lit)
11828 if (FIXNUM_P(lit)) {
11829 return LONG2FIX(-FIX2LONG(lit));
11831 if (SPECIAL_CONST_P(lit)) {
11833 if (FLONUM_P(lit)) {
11834 return DBL2NUM(-RFLOAT_VALUE(lit));
11839 switch (BUILTIN_TYPE(lit)) {
11841 BIGNUM_NEGATE(lit);
11842 lit = rb_big_norm(lit);
11845 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11848 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11849 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11852 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11856 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11857 rb_builtin_class_name(lit));
11864arg_blk_pass(NODE *node1, NODE *node2)
11867 if (!node1) return node2;
11868 node2->nd_head = node1;
11869 nd_set_first_lineno(node2, nd_first_lineno(node1));
11870 nd_set_first_column(node2, nd_first_column(node1));
11877args_info_empty_p(struct rb_args_info *args)
11879 if (args->pre_args_num) return false;
11880 if (args->post_args_num) return false;
11881 if (args->rest_arg) return false;
11882 if (args->opt_args) return false;
11883 if (args->block_arg) return false;
11884 if (args->kw_args) return false;
11885 if (args->kw_rest_arg) return false;
11890new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11892 int saved_line = p->ruby_sourceline;
11893 struct rb_args_info *args = tail->nd_ainfo;
11895 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11896 args->pre_init = pre_args ? pre_args->nd_next : 0;
11898 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11899 args->post_init = post_args ? post_args->nd_next : 0;
11900 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11902 args->rest_arg = rest_arg;
11904 args->opt_args = opt_args;
11906 args->ruby2_keywords = rest_arg == idFWD_REST;
11908 p->ruby_sourceline = saved_line;
11909 nd_set_loc(tail, loc);
11915new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
11917 int saved_line = p->ruby_sourceline;
11919 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11920 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11921 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11922 args->imemo = tmpbuf;
11923 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11924 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11925 if (p->error_p) return node;
11927 args->block_arg = block;
11928 args->kw_args = kw_args;
11932 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11933 * variable order: k1, kr1, k2, &b, internal_id, krest
11935 * variable order: kr1, k1, k2, internal_id, krest, &b
11937 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11938 struct vtable *vtargs = p->lvtbl->args;
11939 NODE *kwn = kw_args;
11941 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11942 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11944 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11946 --required_kw_vars;
11947 kwn = kwn->nd_next;
11950 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11951 ID vid = kwn->nd_body->nd_vid;
11952 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11953 *required_kw_vars++ = vid;
11960 arg_var(p, kw_bits);
11961 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11962 if (block) arg_var(p, block);
11964 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
11965 args->kw_rest_arg->nd_cflag = kw_bits;
11967 else if (kw_rest_arg == idNil) {
11968 args->no_kwarg = 1;
11970 else if (kw_rest_arg) {
11971 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
11974 p->ruby_sourceline = saved_line;
11979args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11981 if (max_numparam > NO_PARAM) {
11983 YYLTYPE loc = RUBY_INIT_YYLLOC();
11984 args = new_args_tail(p, 0, 0, 0, 0);
11985 nd_set_loc(args, &loc);
11987 args->nd_ainfo->pre_args_num = max_numparam;
11993new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11995 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11997 aryptn->nd_pconst = constant;
12000 NODE *pre_args = NEW_LIST(pre_arg, loc);
12001 if (apinfo->pre_args) {
12002 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12005 apinfo->pre_args = pre_args;
12012new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12014 int saved_line = p->ruby_sourceline;
12016 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12017 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12018 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12019 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12020 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12022 apinfo->pre_args = pre_args;
12026 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12029 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12033 apinfo->rest_arg = NULL;
12036 apinfo->post_args = post_args;
12038 p->ruby_sourceline = saved_line;
12043new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12045 fndptn->nd_pconst = constant;
12051new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12053 int saved_line = p->ruby_sourceline;
12055 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12056 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12057 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12058 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12059 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12061 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12062 fpinfo->args = args;
12063 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12065 p->ruby_sourceline = saved_line;
12070new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12072 hshptn->nd_pconst = constant;
12077new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12079 int saved_line = p->ruby_sourceline;
12080 NODE *node, *kw_rest_arg_node;
12082 if (kw_rest_arg == idNil) {
12083 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12085 else if (kw_rest_arg) {
12086 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12089 kw_rest_arg_node = NULL;
12092 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12094 p->ruby_sourceline = saved_line;
12099warn_one_line_pattern_matching(struct parser_params *p, NODE *node, NODE *pattern, bool right_assign)
12101 enum node_type type;
12102 type = nd_type(pattern);
12104 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL) &&
12105 !(right_assign && (type == NODE_LASGN || type == NODE_DASGN || type == NODE_DASGN_CURR)))
12106 rb_warn0L_experimental(nd_line(node), "One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!");
12110dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12115 return NEW_LIT(ID2SYM(idNULL), loc);
12118 switch (nd_type(node)) {
12120 nd_set_type(node, NODE_DSYM);
12121 nd_set_loc(node, loc);
12124 lit = node->nd_lit;
12125 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12126 nd_set_type(node, NODE_LIT);
12127 nd_set_loc(node, loc);
12130 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12137append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12139 NODE *node = (NODE *)v;
12140 NODE **result = (NODE **)h;
12142 node->nd_next->nd_end = node->nd_next;
12143 node->nd_next->nd_next = 0;
12145 list_concat(*result, node);
12148 return ST_CONTINUE;
12152remove_duplicate_keys(struct parser_params *p, NODE *hash)
12154 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
12156 rb_code_location_t loc = hash->nd_loc;
12157 while (hash && hash->nd_head && hash->nd_next) {
12158 NODE *head = hash->nd_head;
12159 NODE *value = hash->nd_next;
12160 NODE *next = value->nd_next;
12161 VALUE key = (VALUE)head;
12163 if (nd_type(head) == NODE_LIT &&
12164 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
12165 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12166 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12167 head->nd_lit, nd_line(head));
12168 head = ((NODE *)data)->nd_next;
12169 head->nd_head = block_append(p, head->nd_head, value->nd_head);
12172 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12176 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12177 st_free_table(literal_keys);
12179 if (!result) result = hash;
12180 else list_concat(result, hash);
12182 result->nd_loc = loc;
12187new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12189 if (hash) hash = remove_duplicate_keys(p, hash);
12190 return NEW_HASH(hash, loc);
12195error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12197 if (is_private_local_id(id)) {
12200 if (st_is_member(p->pvtbl, id)) {
12201 yyerror1(loc, "duplicated variable name");
12204 st_insert(p->pvtbl, (st_data_t)id, 0);
12209error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12212 p->pktbl = st_init_numtable();
12214 else if (st_is_member(p->pktbl, key)) {
12215 yyerror1(loc, "duplicated key name");
12218 st_insert(p->pktbl, (st_data_t)key, 0);
12223new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12225 return NEW_HASH(hash, loc);
12227#endif /* !RIPPER */
12231new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12236 ID vid = lhs->nd_vid;
12237 YYLTYPE lhs_loc = lhs->nd_loc;
12238 int shareable = ctxt.shareable_constant_value;
12240 switch (nd_type(lhs)) {
12251 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12252 lhs->nd_value = rhs;
12253 nd_set_loc(lhs, loc);
12254 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12255 if (is_notop_id(vid)) {
12256 switch (id_type(vid)) {
12260 asgn->nd_aid = vid;
12264 else if (op == tANDOP) {
12266 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12268 lhs->nd_value = rhs;
12269 nd_set_loc(lhs, loc);
12270 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12274 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12276 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12278 asgn->nd_value = rhs;
12279 nd_set_loc(asgn, loc);
12283 asgn = NEW_BEGIN(0, loc);
12289new_ary_op_assign(struct parser_params *p, NODE *ary,
12290 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12294 args = make_list(args, args_loc);
12295 if (nd_type(args) == NODE_BLOCK_PASS) {
12296 args = NEW_ARGSCAT(args, rhs, loc);
12299 args = arg_concat(p, args, rhs, loc);
12301 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12307new_attr_op_assign(struct parser_params *p, NODE *lhs,
12308 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12312 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12318new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12323 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12324 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12327 asgn = NEW_BEGIN(0, loc);
12334const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12336 if (p->ctxt.in_def) {
12337 yyerror1(loc, "dynamic constant assignment");
12339 return NEW_CDECL(0, 0, (path), loc);
12343const_decl(struct parser_params *p, VALUE path)
12345 if (p->ctxt.in_def) {
12346 path = assign_error(p, "dynamic constant assignment", path);
12352assign_error(struct parser_params *p, const char *mesg, VALUE a)
12354 a = dispatch2(assign_error, ERR_MESG(), a);
12360var_field(struct parser_params *p, VALUE a)
12362 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12368new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12370 NODE *result = head;
12372 NODE *tmp = rescue_else ? rescue_else : rescue;
12373 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12375 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12376 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12378 else if (rescue_else) {
12379 result = block_append(p, result, rescue_else);
12382 result = NEW_ENSURE(result, ensure, loc);
12384 fixpos(result, head);
12390warn_unused_var(struct parser_params *p, struct local_vars *local)
12394 if (!local->used) return;
12395 cnt = local->used->pos;
12396 if (cnt != local->vars->pos) {
12397 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12400 ID *v = local->vars->tbl;
12401 ID *u = local->used->tbl;
12402 for (int i = 0; i < cnt; ++i) {
12403 if (!v[i] || (u[i] & LVAR_USED)) continue;
12404 if (is_private_local_id(v[i])) continue;
12405 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12411local_push(struct parser_params *p, int toplevel_scope)
12413 struct local_vars *local;
12414 int inherits_dvars = toplevel_scope && compile_for_eval;
12415 int warn_unused_vars = RTEST(ruby_verbose);
12417 local = ALLOC(struct local_vars);
12418 local->prev = p->lvtbl;
12419 local->args = vtable_alloc(0);
12420 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12422 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12423 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12424 local->numparam.outer = 0;
12425 local->numparam.inner = 0;
12426 local->numparam.current = 0;
12428 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12430# if WARN_PAST_SCOPE
12439local_pop(struct parser_params *p)
12441 struct local_vars *local = p->lvtbl->prev;
12442 if (p->lvtbl->used) {
12443 warn_unused_var(p, p->lvtbl);
12444 vtable_free(p->lvtbl->used);
12446# if WARN_PAST_SCOPE
12447 while (p->lvtbl->past) {
12448 struct vtable *past = p->lvtbl->past;
12449 p->lvtbl->past = past->prev;
12453 vtable_free(p->lvtbl->args);
12454 vtable_free(p->lvtbl->vars);
12457 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12463local_tbl(struct parser_params *p)
12465 int cnt_args = vtable_size(p->lvtbl->args);
12466 int cnt_vars = vtable_size(p->lvtbl->vars);
12467 int cnt = cnt_args + cnt_vars;
12471 if (cnt <= 0) return 0;
12472 buf = ALLOC_N(ID, cnt + 2);
12473 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
12474 /* remove IDs duplicated to warn shadowing */
12475 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
12476 ID id = p->lvtbl->vars->tbl[i];
12477 if (!vtable_included(p->lvtbl->args, id)) {
12482 REALLOC_N(buf, ID, (cnt = j) + 2);
12485 rb_ast_add_local_table(p->ast, buf);
12491node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12497 n = NEW_NODE(type, a0, a1, a2, loc);
12504numparam_name(struct parser_params *p, ID id)
12506 if (!NUMPARAM_ID_P(id)) return;
12507 compile_error(p, "_%d is reserved for numbered parameter",
12508 NUMPARAM_ID_TO_IDX(id));
12512arg_var(struct parser_params *p, ID id)
12514 numparam_name(p, id);
12515 vtable_add(p->lvtbl->args, id);
12519local_var(struct parser_params *p, ID id)
12521 numparam_name(p, id);
12522 vtable_add(p->lvtbl->vars, id);
12523 if (p->lvtbl->used) {
12524 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12529local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12531 struct vtable *vars, *args, *used;
12533 vars = p->lvtbl->vars;
12534 args = p->lvtbl->args;
12535 used = p->lvtbl->used;
12537 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12540 if (used) used = used->prev;
12543 if (vars && vars->prev == DVARS_INHERIT) {
12544 return rb_local_defined(id, p->parent_iseq);
12546 else if (vtable_included(args, id)) {
12550 int i = vtable_included(vars, id);
12551 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12557local_id(struct parser_params *p, ID id)
12559 return local_id_ref(p, id, NULL);
12563check_forwarding_args(struct parser_params *p)
12565 if (local_id(p, idFWD_REST) &&
12567 local_id(p, idFWD_KWREST) &&
12569 local_id(p, idFWD_BLOCK)) return TRUE;
12570 compile_error(p, "unexpected ...");
12575add_forwarding_args(struct parser_params *p)
12577 arg_var(p, idFWD_REST);
12579 arg_var(p, idFWD_KWREST);
12581 arg_var(p, idFWD_BLOCK);
12586new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12588 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12590 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12592 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12593 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12595 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12597 return arg_blk_pass(args, block);
12601new_args_forward_def(struct parser_params *p, NODE *leading, const YYLTYPE *loc)
12603 NODE *n = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, loc);
12604 return new_args(p, leading, Qnone, idFWD_REST, Qnone, n, loc);
12609numparam_push(struct parser_params *p)
12612 struct local_vars *local = p->lvtbl;
12613 NODE *inner = local->numparam.inner;
12614 if (!local->numparam.outer) {
12615 local->numparam.outer = local->numparam.current;
12617 local->numparam.inner = 0;
12618 local->numparam.current = 0;
12626numparam_pop(struct parser_params *p, NODE *prev_inner)
12629 struct local_vars *local = p->lvtbl;
12631 /* prefer first one */
12632 local->numparam.inner = prev_inner;
12634 else if (local->numparam.current) {
12635 /* current and inner are exclusive */
12636 local->numparam.inner = local->numparam.current;
12638 if (p->max_numparam > NO_PARAM) {
12639 /* current and outer are exclusive */
12640 local->numparam.current = local->numparam.outer;
12641 local->numparam.outer = 0;
12644 /* no numbered parameter */
12645 local->numparam.current = 0;
12650static const struct vtable *
12651dyna_push(struct parser_params *p)
12653 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12654 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12655 if (p->lvtbl->used) {
12656 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12658 return p->lvtbl->args;
12662dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12664 struct vtable *tmp = *vtblp;
12665 *vtblp = tmp->prev;
12666# if WARN_PAST_SCOPE
12667 if (p->past_scope_enabled) {
12668 tmp->prev = p->lvtbl->past;
12669 p->lvtbl->past = tmp;
12677dyna_pop_1(struct parser_params *p)
12679 struct vtable *tmp;
12681 if ((tmp = p->lvtbl->used) != 0) {
12682 warn_unused_var(p, p->lvtbl);
12683 p->lvtbl->used = p->lvtbl->used->prev;
12686 dyna_pop_vtable(p, &p->lvtbl->args);
12687 dyna_pop_vtable(p, &p->lvtbl->vars);
12691dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12693 while (p->lvtbl->args != lvargs) {
12695 if (!p->lvtbl->args) {
12696 struct local_vars *local = p->lvtbl->prev;
12697 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12705dyna_in_block(struct parser_params *p)
12707 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12711dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12713 struct vtable *vars, *args, *used;
12716 args = p->lvtbl->args;
12717 vars = p->lvtbl->vars;
12718 used = p->lvtbl->used;
12720 while (!DVARS_TERMINAL_P(vars)) {
12721 if (vtable_included(args, id)) {
12724 if ((i = vtable_included(vars, id)) != 0) {
12725 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12730 if (!vidrefp) used = 0;
12731 if (used) used = used->prev;
12734 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12735 return rb_dvar_defined(id, p->parent_iseq);
12742dvar_defined(struct parser_params *p, ID id)
12744 return dvar_defined_ref(p, id, NULL);
12748dvar_curr(struct parser_params *p, ID id)
12750 return (vtable_included(p->lvtbl->args, id) ||
12751 vtable_included(p->lvtbl->vars, id));
12755reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12758 "regexp encoding option '%c' differs from source encoding '%s'",
12759 c, rb_enc_name(rb_enc_get(str)));
12764rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12766 int c = RE_OPTION_ENCODING_IDX(options);
12770 rb_char_to_option_kcode(c, &opt, &idx);
12771 if (idx != ENCODING_GET(str) &&
12772 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12775 ENCODING_SET(str, idx);
12777 else if (RE_OPTION_ENCODING_NONE(options)) {
12778 if (!ENCODING_IS_ASCII8BIT(str) &&
12779 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12783 rb_enc_associate(str, rb_ascii8bit_encoding());
12785 else if (p->enc == rb_usascii_encoding()) {
12786 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12787 /* raise in re.c */
12788 rb_enc_associate(str, rb_usascii_encoding());
12791 rb_enc_associate(str, rb_ascii8bit_encoding());
12801reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12803 int c = rb_reg_fragment_setenc(p, str, options);
12804 if (c) reg_fragment_enc_error(p, str, c);
12808reg_fragment_check(struct parser_params* p, VALUE str, int options)
12811 reg_fragment_setenc(p, str, options);
12812 err = rb_reg_check_preprocess(str);
12814 err = rb_obj_as_string(err);
12815 compile_error(p, "%"PRIsVALUE, err);
12822 struct parser_params* parser;
12825 const YYLTYPE *loc;
12826} reg_named_capture_assign_t;
12829reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12830 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12832 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12833 struct parser_params* p = arg->parser;
12834 rb_encoding *enc = arg->enc;
12835 long len = name_end - name;
12836 const char *s = (const char *)name;
12840 if (!len) return ST_CONTINUE;
12841 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12842 return ST_CONTINUE;
12844 var = intern_cstr(s, len, enc);
12845 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12846 if (!lvar_defined(p, var)) return ST_CONTINUE;
12848 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
12849 succ = arg->succ_block;
12850 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12851 succ = block_append(p, succ, node);
12852 arg->succ_block = succ;
12853 return ST_CONTINUE;
12857reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12859 reg_named_capture_assign_t arg;
12862 arg.enc = rb_enc_get(regexp);
12863 arg.succ_block = 0;
12865 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12867 if (!arg.succ_block) return 0;
12868 return arg.succ_block->nd_next;
12872parser_reg_compile(struct parser_params* p, VALUE str, int options)
12874 reg_fragment_setenc(p, str, options);
12875 return rb_parser_reg_compile(p, str, options);
12879rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12881 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12885reg_compile(struct parser_params* p, VALUE str, int options)
12890 err = rb_errinfo();
12891 re = parser_reg_compile(p, str, options);
12893 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12894 rb_set_errinfo(err);
12895 compile_error(p, "%"PRIsVALUE, m);
12902parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12904 VALUE err = rb_errinfo();
12906 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12907 int c = rb_reg_fragment_setenc(p, str, options);
12908 if (c) reg_fragment_enc_error(p, str, c);
12909 re = rb_parser_reg_compile(p, str, options);
12911 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12912 rb_set_errinfo(err);
12920rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12922 struct parser_params *p;
12923 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12924 p->do_print = print;
12926 p->do_chomp = chomp;
12927 p->do_split = split;
12931parser_append_options(struct parser_params *p, NODE *node)
12933 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12934 const YYLTYPE *const LOC = &default_location;
12937 NODE *print = NEW_FCALL(rb_intern("print"),
12938 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12940 node = block_append(p, node, print);
12945 ID ifs = rb_intern("$;");
12946 ID fields = rb_intern("$F");
12947 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
12948 NODE *split = NEW_GASGN(fields,
12949 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12950 rb_intern("split"), args, LOC),
12952 node = block_append(p, split, node);
12955 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12956 rb_intern("chomp!"), 0, LOC);
12957 node = block_append(p, chomp, node);
12960 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12969 /* just to suppress unused-function warnings */
12975internal_id(struct parser_params *p)
12977 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12978 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12980 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12982#endif /* !RIPPER */
12985parser_initialize(struct parser_params *p)
12987 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12988 p->command_start = TRUE;
12989 p->ruby_sourcefile_string = Qnil;
12990 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12993 p->delayed.token = Qnil;
12995 p->parsing_thread = Qnil;
12997 p->error_buffer = Qfalse;
12999 p->debug_buffer = Qnil;
13000 p->debug_output = rb_ractor_stdout();
13001 p->enc = rb_utf8_encoding();
13005#define parser_mark ripper_parser_mark
13006#define parser_free ripper_parser_free
13010parser_mark(void *ptr)
13012 struct parser_params *p = (struct parser_params*)ptr;
13014 rb_gc_mark(p->lex.input);
13015 rb_gc_mark(p->lex.prevline);
13016 rb_gc_mark(p->lex.lastline);
13017 rb_gc_mark(p->lex.nextline);
13018 rb_gc_mark(p->ruby_sourcefile_string);
13019 rb_gc_mark((VALUE)p->lex.strterm);
13020 rb_gc_mark((VALUE)p->ast);
13021 rb_gc_mark(p->case_labels);
13023 rb_gc_mark(p->debug_lines);
13024 rb_gc_mark(p->compile_option);
13025 rb_gc_mark(p->error_buffer);
13027 rb_gc_mark(p->delayed.token);
13028 rb_gc_mark(p->value);
13029 rb_gc_mark(p->result);
13030 rb_gc_mark(p->parsing_thread);
13032 rb_gc_mark(p->debug_buffer);
13033 rb_gc_mark(p->debug_output);
13035 rb_gc_mark((VALUE)p->heap);
13040parser_free(void *ptr)
13042 struct parser_params *p = (struct parser_params*)ptr;
13043 struct local_vars *local, *prev;
13046 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13048 for (local = p->lvtbl; local; local = prev) {
13049 if (local->vars) xfree(local->vars);
13050 prev = local->prev;
13054 token_info *ptinfo;
13055 while ((ptinfo = p->token_info) != 0) {
13056 p->token_info = ptinfo->next;
13064parser_memsize(const void *ptr)
13066 struct parser_params *p = (struct parser_params*)ptr;
13067 struct local_vars *local;
13068 size_t size = sizeof(*p);
13071 for (local = p->lvtbl; local; local = local->prev) {
13072 size += sizeof(*local);
13073 if (local->vars) size += local->vars->capa * sizeof(ID);
13078static const rb_data_type_t parser_data_type = {
13089 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13093#undef rb_reserved_word
13095const struct kwtable *
13096rb_reserved_word(const char *str, unsigned int len)
13098 return reserved_word(str, len);
13104 struct parser_params *p;
13105 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13106 &parser_data_type, p);
13107 parser_initialize(p);
13112rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13114 struct parser_params *p;
13116 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13117 p->error_buffer = main ? Qfalse : Qnil;
13118 p->parent_iseq = base;
13124#define rb_parser_end_seen_p ripper_parser_end_seen_p
13125#define rb_parser_encoding ripper_parser_encoding
13126#define rb_parser_get_yydebug ripper_parser_get_yydebug
13127#define rb_parser_set_yydebug ripper_parser_set_yydebug
13128#define rb_parser_get_debug_output ripper_parser_get_debug_output
13129#define rb_parser_set_debug_output ripper_parser_set_debug_output
13130static VALUE ripper_parser_end_seen_p(VALUE vparser);
13131static VALUE ripper_parser_encoding(VALUE vparser);
13132static VALUE ripper_parser_get_yydebug(VALUE self);
13133static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13134static VALUE ripper_parser_get_debug_output(VALUE self);
13135static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13139 * ripper.error? -> Boolean
13141 * Return true if parsed source has errors.
13144ripper_error_p(VALUE vparser)
13146 struct parser_params *p;
13148 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13149 return p->error_p ? Qtrue : Qfalse;
13155 * ripper.end_seen? -> Boolean
13157 * Return true if parsed source ended by +\_\_END\_\_+.
13160rb_parser_end_seen_p(VALUE vparser)
13162 struct parser_params *p;
13164 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13165 return p->ruby__end__seen ? Qtrue : Qfalse;
13170 * ripper.encoding -> encoding
13172 * Return encoding of the source.
13175rb_parser_encoding(VALUE vparser)
13177 struct parser_params *p;
13179 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13180 return rb_enc_from_encoding(p->enc);
13186 * ripper.yydebug -> true or false
13191rb_parser_get_yydebug(VALUE self)
13193 struct parser_params *p;
13195 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13196 return p->debug ? Qtrue : Qfalse;
13202 * ripper.yydebug = flag
13207rb_parser_set_yydebug(VALUE self, VALUE flag)
13209 struct parser_params *p;
13211 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13212 p->debug = RTEST(flag);
13218 * ripper.debug_output -> obj
13220 * Get debug output.
13223rb_parser_get_debug_output(VALUE self)
13225 struct parser_params *p;
13227 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13228 return p->debug_output;
13233 * ripper.debug_output = obj
13235 * Set debug output.
13238rb_parser_set_debug_output(VALUE self, VALUE output)
13240 struct parser_params *p;
13242 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13243 return p->debug_output = output;
13248#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13249/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13250 * potential memory leak */
13251#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13252#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13253 (new)->cnt = (cnt), (ptr))
13256rb_parser_malloc(struct parser_params *p, size_t size)
13258 size_t cnt = HEAPCNT(1, size);
13259 rb_imemo_tmpbuf_t *n = NEWHEAP();
13260 void *ptr = xmalloc(size);
13262 return ADD2HEAP(n, cnt, ptr);
13266rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13268 size_t cnt = HEAPCNT(nelem, size);
13269 rb_imemo_tmpbuf_t *n = NEWHEAP();
13270 void *ptr = xcalloc(nelem, size);
13272 return ADD2HEAP(n, cnt, ptr);
13276rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13278 rb_imemo_tmpbuf_t *n;
13279 size_t cnt = HEAPCNT(1, size);
13281 if (ptr && (n = p->heap) != NULL) {
13283 if (n->ptr == ptr) {
13284 n->ptr = ptr = xrealloc(ptr, size);
13285 if (n->cnt) n->cnt = cnt;
13288 } while ((n = n->next) != NULL);
13291 ptr = xrealloc(ptr, size);
13292 return ADD2HEAP(n, cnt, ptr);
13296rb_parser_free(struct parser_params *p, void *ptr)
13298 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13300 while ((n = *prev) != NULL) {
13301 if (n->ptr == ptr) {
13303 rb_gc_force_recycle((VALUE)n);
13313rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13316 VALUE mesg = p->debug_buffer;
13318 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13320 rb_str_vcatf(mesg, fmt, ap);
13322 if (RSTRING_END(mesg)[-1] == '\n') {
13323 rb_io_write(p->debug_output, mesg);
13324 p->debug_buffer = Qnil;
13329parser_compile_error(struct parser_params *p, const char *fmt, ...)
13333 rb_io_flush(p->debug_output);
13337 rb_syntax_error_append(p->error_buffer,
13338 p->ruby_sourcefile_string,
13339 p->ruby_sourceline,
13340 rb_long2int(p->lex.pcur - p->lex.pbeg),
13346count_char(const char *str, int c)
13349 while (str[n] == c) ++n;
13354 * strip enclosing double-quotes, same as the default yytnamerr except
13355 * for that single-quotes matching back-quotes do not stop stripping.
13357 * "\"`class' keyword\"" => "`class' keyword"
13359RUBY_FUNC_EXPORTED size_t
13360rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13362 if (*yystr == '"') {
13363 size_t yyn = 0, bquote = 0;
13364 const char *yyp = yystr;
13370 bquote = count_char(yyp+1, '`') + 1;
13371 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13379 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13380 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13386 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13387 if (yyres) memcpy(yyres + yyn, yyp, 3);
13392 goto do_not_strip_quotes;
13395 goto do_not_strip_quotes;
13398 if (*++yyp != '\\')
13399 goto do_not_strip_quotes;
13400 /* Fall through. */
13415 do_not_strip_quotes: ;
13418 if (!yyres) return strlen(yystr);
13420 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13428ripper_validate_object(VALUE self, VALUE x)
13430 if (x == Qfalse) return x;
13431 if (x == Qtrue) return x;
13432 if (x == Qnil) return x;
13434 rb_raise(rb_eArgError, "Qundef given");
13435 if (FIXNUM_P(x)) return x;
13436 if (SYMBOL_P(x)) return x;
13437 switch (BUILTIN_TYPE(x)) {
13447 if (nd_type((NODE *)x) != NODE_RIPPER) {
13448 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13450 x = ((NODE *)x)->nd_rval;
13453 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13454 (void *)x, rb_obj_classname(x));
13456 if (!RBASIC_CLASS(x)) {
13457 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13458 (void *)x, rb_builtin_type_name(TYPE(x)));
13464#define validate(x) ((x) = get_value(x))
13467ripper_dispatch0(struct parser_params *p, ID mid)
13469 return rb_funcall(p->value, mid, 0);
13473ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13476 return rb_funcall(p->value, mid, 1, a);
13480ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13484 return rb_funcall(p->value, mid, 2, a, b);
13488ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13493 return rb_funcall(p->value, mid, 3, a, b, c);
13497ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13503 return rb_funcall(p->value, mid, 4, a, b, c, d);
13507ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13514 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13518ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13527 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13531ripper_get_id(VALUE v)
13534 if (!RB_TYPE_P(v, T_NODE)) return 0;
13536 if (nd_type(nd) != NODE_RIPPER) return 0;
13541ripper_get_value(VALUE v)
13544 if (v == Qundef) return Qnil;
13545 if (!RB_TYPE_P(v, T_NODE)) return v;
13547 if (nd_type(nd) != NODE_RIPPER) return Qnil;
13548 return nd->nd_rval;
13552ripper_error(struct parser_params *p)
13558ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13563 va_start(args, fmt);
13564 str = rb_vsprintf(fmt, args);
13566 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13571ripper_lex_get_generic(struct parser_params *p, VALUE src)
13573 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13574 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13575 rb_raise(rb_eTypeError,
13576 "gets returned %"PRIsVALUE" (expected String or nil)",
13577 rb_obj_class(line));
13583ripper_lex_io_get(struct parser_params *p, VALUE src)
13585 return rb_io_gets(src);
13589ripper_s_allocate(VALUE klass)
13591 struct parser_params *p;
13592 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13593 &parser_data_type, p);
13598#define ripper_initialized_p(r) ((r)->lex.input != 0)
13602 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13604 * Create a new Ripper object.
13605 * _src_ must be a String, an IO, or an Object which has #gets method.
13607 * This method does not starts parsing.
13608 * See also Ripper#parse and Ripper.parse.
13611ripper_initialize(int argc, VALUE *argv, VALUE self)
13613 struct parser_params *p;
13614 VALUE src, fname, lineno;
13616 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13617 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13618 if (RB_TYPE_P(src, T_FILE)) {
13619 p->lex.gets = ripper_lex_io_get;
13621 else if (rb_respond_to(src, id_gets)) {
13622 p->lex.gets = ripper_lex_get_generic;
13626 p->lex.gets = lex_get_str;
13628 p->lex.input = src;
13630 if (NIL_P(fname)) {
13631 fname = STR_NEW2("(ripper)");
13635 StringValueCStr(fname);
13636 fname = rb_str_new_frozen(fname);
13638 parser_initialize(p);
13640 p->ruby_sourcefile_string = fname;
13641 p->ruby_sourcefile = RSTRING_PTR(fname);
13642 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13648ripper_parse0(VALUE parser_v)
13650 struct parser_params *p;
13652 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13654 p->ast = rb_ast_new();
13655 ripper_yyparse((void*)p);
13656 rb_ast_dispose(p->ast);
13662ripper_ensure(VALUE parser_v)
13664 struct parser_params *p;
13666 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13667 p->parsing_thread = Qnil;
13675 * Start parsing and returns the value of the root action.
13678ripper_parse(VALUE self)
13680 struct parser_params *p;
13682 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13683 if (!ripper_initialized_p(p)) {
13684 rb_raise(rb_eArgError, "method called for uninitialized object");
13686 if (!NIL_P(p->parsing_thread)) {
13687 if (p->parsing_thread == rb_thread_current())
13688 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13690 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13692 p->parsing_thread = rb_thread_current();
13693 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13700 * ripper.column -> Integer
13702 * Return column number of current parsing line.
13703 * This number starts from 0.
13706ripper_column(VALUE self)
13708 struct parser_params *p;
13711 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13712 if (!ripper_initialized_p(p)) {
13713 rb_raise(rb_eArgError, "method called for uninitialized object");
13715 if (NIL_P(p->parsing_thread)) return Qnil;
13716 col = p->lex.ptok - p->lex.pbeg;
13717 return LONG2NUM(col);
13722 * ripper.filename -> String
13724 * Return current parsing filename.
13727ripper_filename(VALUE self)
13729 struct parser_params *p;
13731 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13732 if (!ripper_initialized_p(p)) {
13733 rb_raise(rb_eArgError, "method called for uninitialized object");
13735 return p->ruby_sourcefile_string;
13740 * ripper.lineno -> Integer
13742 * Return line number of current parsing line.
13743 * This number starts from 1.
13746ripper_lineno(VALUE self)
13748 struct parser_params *p;
13750 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13751 if (!ripper_initialized_p(p)) {
13752 rb_raise(rb_eArgError, "method called for uninitialized object");
13754 if (NIL_P(p->parsing_thread)) return Qnil;
13755 return INT2NUM(p->ruby_sourceline);
13760 * ripper.state -> Integer
13762 * Return scanner state of current token.
13765ripper_state(VALUE self)
13767 struct parser_params *p;
13769 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13770 if (!ripper_initialized_p(p)) {
13771 rb_raise(rb_eArgError, "method called for uninitialized object");
13773 if (NIL_P(p->parsing_thread)) return Qnil;
13774 return INT2NUM(p->lex.state);
13779 * ripper.token -> String
13781 * Return the current token string.
13784ripper_token(VALUE self)
13786 struct parser_params *p;
13789 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13790 if (!ripper_initialized_p(p)) {
13791 rb_raise(rb_eArgError, "method called for uninitialized object");
13793 if (NIL_P(p->parsing_thread)) return Qnil;
13794 pos = p->lex.ptok - p->lex.pbeg;
13795 len = p->lex.pcur - p->lex.ptok;
13796 return rb_str_subseq(p->lex.lastline, pos, len);
13802ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13805 if (obj == Qundef) {
13806 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13813ripper_value(VALUE self, VALUE obj)
13815 return ULONG2NUM(obj);
13821 * Ripper.lex_state_name(integer) -> string
13823 * Returns a string representation of lex_state.
13826ripper_lex_state_name(VALUE self, VALUE state)
13828 return rb_parser_lex_state_name(NUM2INT(state));
13834 ripper_init_eventids1();
13835 ripper_init_eventids2();
13836 id_warn = rb_intern_const("warn");
13837 id_warning = rb_intern_const("warning");
13838 id_gets = rb_intern_const("gets");
13839 id_assoc = rb_intern_const("=>");
13841 (void)yystpcpy; /* may not used in newer bison */
13851 Ripper = rb_define_class("Ripper", rb_cObject);
13852 /* version of Ripper */
13853 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13854 rb_define_alloc_func(Ripper, ripper_s_allocate);
13855 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13856 rb_define_method(Ripper, "parse", ripper_parse, 0);
13857 rb_define_method(Ripper, "column", ripper_column, 0);
13858 rb_define_method(Ripper, "filename", ripper_filename, 0);
13859 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13860 rb_define_method(Ripper, "state", ripper_state, 0);
13861 rb_define_method(Ripper, "token", ripper_token, 0);
13862 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13863 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13864 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13865 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13866 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13867 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13868 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13870 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13871 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13872 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13875 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13876 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13878 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13880 /* ignore newline, +/- is a sign. */
13881 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13882 /* newline significant, +/- is an operator. */
13883 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13884 /* ditto, and unbound braces. */
13885 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13886 /* ditto, and unbound braces. */
13887 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13888 /* newline significant, +/- is an operator. */
13889 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13890 /* newline significant, +/- is an operator. */
13891 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13892 /* newline significant, +/- is an operator. */
13893 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13894 /* ignore newline, no reserved words. */
13895 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13896 /* right after `.' or `::', no reserved words. */
13897 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13898 /* immediate after `class', no here document. */
13899 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13900 /* flag bit, label is allowed. */
13901 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13902 /* flag bit, just after a label. */
13903 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13904 /* symbol literal as FNAME. */
13905 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13906 /* equals to +EXPR_BEG+ */
13907 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13908 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13909 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13910 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13911 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13912 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13913 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13914 /* equals to +0+ */
13915 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13917 ripper_init_eventids1_table(Ripper);
13918 ripper_init_eventids2_table(Ripper);
13921 /* Hack to let RDoc document SCRIPT_LINES__ */
13924 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13925 * after the assignment will be added as an Array of lines with the file
13928 rb_define_global_const("SCRIPT_LINES__", Qnil);
13937 * c-file-style: "ruby"