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
25#include "ruby/encoding.h"
36#ifndef WARN_PAST_SCOPE
37# define WARN_PAST_SCOPE 0
42#define yydebug (p->debug) /* disable the global variable definition */
44#define YYMALLOC(size) rb_parser_malloc(p, (size))
45#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
46#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
47#define YYFREE(ptr) rb_parser_free(p, (ptr))
48#define YYFPRINTF rb_parser_printf
49#define YYPRINT(out, tok, val) parser_token_value_print(p, (tok), &(val))
50#define YY_LOCATION_PRINT(File, loc) \
51 rb_parser_printf(p, "%d.%d-%d.%d", \
52 (loc).beg_pos.lineno, (loc).beg_pos.column,\
53 (loc).end_pos.lineno, (loc).end_pos.column)
54#define YYLLOC_DEFAULT(Current, Rhs, N) \
58 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
59 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
63 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
64 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
68#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
69 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
70#define RUBY_SET_YYLLOC_OF_NONE(Current) \
71 rb_parser_set_location_of_none(p, &(Current))
72#define RUBY_SET_YYLLOC(Current) \
73 rb_parser_set_location(p, &(Current))
74#define RUBY_INIT_YYLLOC() \
76 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
77 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
81 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
82 EXPR_END_bit, /* newline significant, +/- is an operator. */
83 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
84 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
85 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
86 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
87 EXPR_MID_bit, /* newline significant, +/- is an operator. */
88 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
89 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
90 EXPR_CLASS_bit, /* immediate after `class', no here document. */
91 EXPR_LABEL_bit, /* flag bit, label is allowed. */
92 EXPR_LABELED_bit, /* flag bit, just after a label. */
93 EXPR_FITEM_bit, /* symbol literal as FNAME. */
96/* examine combinations */
98#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
112 EXPR_VALUE = EXPR_BEG,
113 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
114 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
115 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
118#define IS_lex_state_for(x, ls) ((x) & (ls))
119#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
120#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
121#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
123# define SET_LEX_STATE(ls) \
126 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
127 (enum lex_state_e)(ls)))
129typedef VALUE stack_type;
131static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
133# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
134# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
135# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
136# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
137# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
139/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
140 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
141#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
142#define COND_POP() BITSTACK_POP(cond_stack)
143#define COND_P() BITSTACK_SET_P(cond_stack)
144#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
146/* A flag to identify keyword_do_block; "do" keyword after command_call.
147 Example: `foo 1, 2 do`. */
148#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
149#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
150#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
151#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
167 struct local_vars *prev;
170 NODE *outer, *inner, *current;
181#define NUMPARAM_ID_P(id) numparam_id_p(id)
182#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
183#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
187 if (!is_local_id(id)) return 0;
188 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
189 return idx > 0 && idx <= NUMPARAM_MAX;
191static void numparam_name(struct parser_params *p, ID id);
193#define DVARS_INHERIT ((void*)1)
194#define DVARS_TOPSCOPE NULL
195#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
197typedef struct token_info {
199 rb_code_position_t beg;
202 struct token_info *next;
205typedef struct rb_strterm_struct rb_strterm_t;
208 Structure of Lexer Buffer:
210 lex.pbeg lex.ptok lex.pcur lex.pend
212 |------------+------------+------------|
216struct parser_params {
217 rb_imemo_tmpbuf_t *heap;
222 rb_strterm_t *strterm;
223 VALUE (*gets)(struct parser_params*,VALUE);
234 VALUE (*call)(VALUE, int);
236 enum lex_state_e state;
237 /* track the nest level of any parens "()[]{}" */
239 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
241 /* track the nest level of only braces "{}" */
244 stack_type cond_stack;
245 stack_type cmdarg_stack;
251 int heredoc_line_indent;
253 struct local_vars *lvtbl;
257 int ruby_sourceline; /* current line no. */
258 const char *ruby_sourcefile; /* current source file */
259 VALUE ruby_sourcefile_string;
261 token_info *token_info;
263 VALUE compile_option;
275 unsigned int command_start:1;
276 unsigned int eofp: 1;
277 unsigned int ruby__end__seen: 1;
278 unsigned int debug: 1;
279 unsigned int has_shebang: 1;
280 unsigned int in_defined: 1;
281 unsigned int in_kwarg: 1;
282 unsigned int in_def: 1;
283 unsigned int in_class: 1;
284 unsigned int token_seen: 1;
285 unsigned int token_info_enabled: 1;
287 unsigned int past_scope_enabled: 1;
289 unsigned int error_p: 1;
290 unsigned int cr_seen: 1;
295 unsigned int do_print: 1;
296 unsigned int do_loop: 1;
297 unsigned int do_chomp: 1;
298 unsigned int do_split: 1;
299 unsigned int warn_location: 1;
301 NODE *eval_tree_begin;
305 const struct rb_iseq_struct *parent_iseq;
317 VALUE parsing_thread;
321#define intern_cstr(n,l,en) rb_intern3(n,l,en)
323#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
324#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
325#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
326#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
327#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
330push_pvtbl(struct parser_params *p)
332 st_table *tbl = p->pvtbl;
333 p->pvtbl = st_init_numtable();
338pop_pvtbl(struct parser_params *p, st_table *tbl)
340 st_free_table(p->pvtbl);
345push_pktbl(struct parser_params *p)
347 st_table *tbl = p->pktbl;
353pop_pktbl(struct parser_params *p, st_table *tbl)
355 if (p->pktbl) st_free_table(p->pktbl);
359static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
360#define yyerror0(msg) parser_yyerror(p, NULL, (msg))
361#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
362#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
363#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
366#define compile_for_eval (0)
368#define compile_for_eval (p->parent_iseq != 0)
371#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
373#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
374#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
375#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
377#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
379static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
383rb_discard_node(struct parser_params *p, NODE *n)
385 rb_ast_delete_node(p->ast, n);
391add_mark_object(struct parser_params *p, VALUE obj)
393 if (!SPECIAL_CONST_P(obj)
394 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
396 rb_ast_add_mark_object(p->ast, obj);
401static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
404static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
405#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
407static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
410parser_get_node_id(struct parser_params *p)
412 int node_id = p->node_id;
419set_line_body(NODE *body, int line)
422 switch (nd_type(body)) {
425 nd_set_line(body, line);
429#define yyparse ruby_yyparse
431static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
433#define new_nil(loc) NEW_NIL(loc)
434static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
436static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
438static NODE *newline_node(NODE*);
439static void fixpos(NODE*,NODE*);
441static int value_expr_gen(struct parser_params*,NODE*);
442static void void_expr(struct parser_params*,NODE*);
443static NODE *remove_begin(NODE*);
444static NODE *remove_begin_all(NODE*);
445#define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
446static NODE *void_stmts(struct parser_params*,NODE*);
447static void reduce_nodes(struct parser_params*,NODE**);
448static void block_dup_check(struct parser_params*,NODE*,NODE*);
450static NODE *block_append(struct parser_params*,NODE*,NODE*);
451static NODE *list_append(struct parser_params*,NODE*,NODE*);
452static NODE *list_concat(NODE*,NODE*);
453static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
454static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
455static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
456static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
457static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
458static NODE *evstr2dstr(struct parser_params*,NODE*);
459static NODE *splat_array(NODE*);
460static void mark_lvar_used(struct parser_params *p, NODE *rhs);
462static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
463static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
464static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
465static 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);
466static 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;}
468static bool args_info_empty_p(struct rb_args_info *args);
469static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
470static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
471static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
472static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
473static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
474static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
475static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
477static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
478static NODE *args_with_numbered(struct parser_params*,NODE*,int);
480static VALUE negate_lit(struct parser_params*, VALUE);
481static NODE *ret_args(struct parser_params*,NODE*);
482static NODE *arg_blk_pass(NODE*,NODE*);
483static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
484static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
486static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
487static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
489static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
490static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
492static void rb_backref_error(struct parser_params*,NODE*);
493static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
495static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
496static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
497static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
498static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
499static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
501static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
503static NODE *opt_arg_append(NODE*, NODE*);
504static NODE *kwd_append(NODE*, NODE*);
506static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
507static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
509static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
511static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
513#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
515static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
517static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
519static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
521static ID *local_tbl(struct parser_params*);
523static VALUE reg_compile(struct parser_params*, VALUE, int);
524static void reg_fragment_setenc(struct parser_params*, VALUE, int);
525static int reg_fragment_check(struct parser_params*, VALUE, int);
526static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
528static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
529static NODE *heredoc_dedent(struct parser_params*,NODE*);
531static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
533#define get_id(id) (id)
534#define get_value(val) (val)
535#define get_num(num) (num)
537#define NODE_RIPPER NODE_CDECL
539static inline int ripper_is_node_yylval(VALUE n);
542ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
544 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
545 add_mark_object(p, b);
546 add_mark_object(p, c);
547 return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
551ripper_is_node_yylval(VALUE n)
553 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
556#define value_expr(node) ((void)(node))
557#define remove_begin(node) (node)
558#define void_stmts(p,x) (x)
559#define rb_dvar_defined(id, base) 0
560#define rb_local_defined(id, base) 0
561static ID ripper_get_id(VALUE);
562#define get_id(id) ripper_get_id(id)
563static VALUE ripper_get_value(VALUE);
564#define get_value(val) ripper_get_value(val)
565#define get_num(num) (int)get_id(num)
566static VALUE assignable(struct parser_params*,VALUE);
567static int id_is_var(struct parser_params *p, ID id);
569#define method_cond(p,node,loc) (node)
570#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
571#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
572#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
573#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
575#define new_nil(loc) Qnil
577static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
579static VALUE const_decl(struct parser_params *p, VALUE path);
581static VALUE var_field(struct parser_params *p, VALUE a);
582static VALUE assign_error(struct parser_params *p, VALUE a);
584static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
588/* forward declaration */
589typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
591RUBY_SYMBOL_EXPORT_BEGIN
592VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
593int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
594enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
595VALUE rb_parser_lex_state_name(enum lex_state_e state);
596void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
597PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
598YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
599YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
600YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
601RUBY_SYMBOL_EXPORT_END
603static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
604static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
605static void parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp);
606static ID formal_argument(struct parser_params*, ID);
607static ID shadowing_lvar(struct parser_params*,ID);
608static void new_bv(struct parser_params*,ID);
610static void local_push(struct parser_params*,int);
611static void local_pop(struct parser_params*);
612static void local_var(struct parser_params*, ID);
613static void arg_var(struct parser_params*, ID);
614static int local_id(struct parser_params *p, ID id);
615static int local_id_ref(struct parser_params*, ID, ID **);
617static ID internal_id(struct parser_params*);
620static const struct vtable *dyna_push(struct parser_params *);
621static void dyna_pop(struct parser_params*, const struct vtable *);
622static int dyna_in_block(struct parser_params*);
623#define dyna_var(p, id) local_var(p, id)
624static int dvar_defined(struct parser_params*, ID);
625static int dvar_defined_ref(struct parser_params*, ID, ID**);
626static int dvar_curr(struct parser_params*,ID);
628static int lvar_defined(struct parser_params*, ID);
630static NODE *numparam_push(struct parser_params *p);
631static void numparam_pop(struct parser_params *p, NODE *prev_inner);
634# define METHOD_NOT idNOT
636# define METHOD_NOT '!'
639#define idFWD_REST '*'
641#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
643#define idFWD_KWREST 0
645#define idFWD_BLOCK '&'
647#define RE_OPTION_ONCE (1<<16)
648#define RE_OPTION_ENCODING_SHIFT 8
649#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
650#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
651#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
652#define RE_OPTION_MASK 0xff
653#define RE_OPTION_ARG_ENCODING_NONE 32
655/* structs for managing terminator of string literal and heredocment */
656typedef struct rb_strterm_literal_struct {
663 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
667 long paren; /* '(' of `%q(...)` */
671 long term; /* ')' of `%q(...)` */
673} rb_strterm_literal_t;
675#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
677struct rb_strterm_heredoc_struct {
678 VALUE lastline; /* the string of line that contains `<<"END"` */
679 long offset; /* the column of END in `<<"END"` */
680 int sourceline; /* lineno of the line that contains `<<"END"` */
681 unsigned length /* the length of END in `<<"END"` */
682#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
683 : HERETERM_LENGTH_BITS
684# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
686# define HERETERM_LENGTH_MAX UINT_MAX
689#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
697STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
699#define STRTERM_HEREDOC IMEMO_FL_USER0
701struct rb_strterm_struct {
704 rb_strterm_literal_t literal;
705 rb_strterm_heredoc_t heredoc;
711rb_strterm_mark(VALUE obj)
713 rb_strterm_t *strterm = (rb_strterm_t*)obj;
714 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
715 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
716 rb_gc_mark(heredoc->lastline);
721#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
722size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
724#define TOKEN2ID(tok) ( \
725 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
726 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
727 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
728 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
729 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
730 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
731 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
733/****** Ripper *******/
736#define RIPPER_VERSION "0.1.0"
738static inline VALUE intern_sym(const char *name);
740#include "eventids1.c"
741#include "eventids2.c"
743static VALUE ripper_dispatch0(struct parser_params*,ID);
744static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
745static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
746static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
747static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
748static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
749static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
750static void ripper_error(struct parser_params *p);
752#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
753#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
754#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
755#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
756#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
757#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
758#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
760#define yyparse ripper_yyparse
762#define ID2VAL(id) STATIC_ID2SYM(id)
763#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
764#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
766#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
767 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
769#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
772new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
774 NODE *t = (NODE *)tail;
775 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
776 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
780new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
782 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
783 add_mark_object(p, kw_args);
784 add_mark_object(p, kw_rest_arg);
785 add_mark_object(p, block);
790args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
796new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
798 NODE *t = (NODE *)aryptn;
799 struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
800 VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
803 pre_args = rb_ary_entry(apinfo->imemo, 0);
804 rest_arg = rb_ary_entry(apinfo->imemo, 1);
805 post_args = rb_ary_entry(apinfo->imemo, 2);
808 if (!NIL_P(pre_arg)) {
809 if (!NIL_P(pre_args)) {
810 rb_ary_unshift(pre_args, pre_arg);
813 pre_args = rb_ary_new_from_args(1, pre_arg);
816 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
820new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
823 struct rb_ary_pattern_info *apinfo;
826 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
832 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
833 apinfo = ZALLOC(struct rb_ary_pattern_info);
834 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
835 apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
837 t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
838 RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
843#define new_hash(p,h,l) rb_ary_new_from_args(0)
846new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
852new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
854 NODE *t = (NODE *)hshptn;
855 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
856 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
860new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
864 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
869 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
871 add_mark_object(p, kw_args);
872 add_mark_object(p, kw_rest_arg);
876#define new_defined(p,expr,loc) dispatch1(defined, (expr))
878static VALUE heredoc_dedent(struct parser_params*,VALUE);
881#define ID2VAL(id) (id)
882#define TOKEN2VAL(t) ID2VAL(t)
883#define KWD2EID(t, v) keyword_##t
889# define ifndef_ripper(x) (x)
893# define ifndef_ripper(x)
896# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
897# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
898# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
899# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
900# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
901# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
902# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
903# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
904# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
905# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
906# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
907# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
908# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
909# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
910# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
911# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
912# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
913# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
914# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
915# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
917static ID id_warn, id_warning, id_gets, id_assoc;
918# define WARN_S_L(s,l) STR_NEW(s,l)
919# define WARN_S(s) STR_NEW2(s)
920# define WARN_I(i) INT2NUM(i)
921# define WARN_ID(i) rb_id2str(i)
922# define WARN_IVAL(i) i
924# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
925# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
926# ifdef HAVE_VA_ARGS_MACRO
927# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
929# define WARN_CALL rb_funcall
931# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
932# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
933# ifdef HAVE_VA_ARGS_MACRO
934# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
936# define WARNING_CALL rb_funcall
938PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
939# define compile_error ripper_compile_error
941# define WARN_S_L(s,l) s
944# define WARN_ID(i) rb_id2name(i)
945# define WARN_IVAL(i) NUM2INT(i)
946# define PRIsWARN PRIsVALUE
947# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
948# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
949# define WARN_CALL rb_compile_warn
950# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
951# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
952# define WARNING_CALL rb_compile_warning
953PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
954# define compile_error parser_compile_error
957static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
958static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
959static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
960static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
962#define WARN_EOL(tok) \
963 (looking_at_eol_p(p) ? \
964 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
966static int looking_at_eol_p(struct parser_params *p);
971%define parse.error verbose
972%lex-param {struct parser_params *p}
973%parse-param {struct parser_params *p}
976 RUBY_SET_YYLLOC_OF_NONE(@$);
985 const struct vtable *vars;
986 struct rb_strterm_struct *strterm;
990 keyword_class "`class'"
991 keyword_module "`module'"
993 keyword_undef "`undef'"
994 keyword_begin "`begin'"
995 keyword_rescue "`rescue'"
996 keyword_ensure "`ensure'"
999 keyword_unless "`unless'"
1000 keyword_then "`then'"
1001 keyword_elsif "`elsif'"
1002 keyword_else "`else'"
1003 keyword_case "`case'"
1004 keyword_when "`when'"
1005 keyword_while "`while'"
1006 keyword_until "`until'"
1008 keyword_break "`break'"
1009 keyword_next "`next'"
1010 keyword_redo "`redo'"
1011 keyword_retry "`retry'"
1014 keyword_do_cond "`do' for condition"
1015 keyword_do_block "`do' for block"
1016 keyword_do_LAMBDA "`do' for lambda"
1017 keyword_return "`return'"
1018 keyword_yield "`yield'"
1019 keyword_super "`super'"
1020 keyword_self "`self'"
1022 keyword_true "`true'"
1023 keyword_false "`false'"
1027 modifier_if "`if' modifier"
1028 modifier_unless "`unless' modifier"
1029 modifier_while "`while' modifier"
1030 modifier_until "`until' modifier"
1031 modifier_rescue "`rescue' modifier"
1032 keyword_alias "`alias'"
1033 keyword_defined "`defined?'"
1034 keyword_BEGIN "`BEGIN'"
1036 keyword__LINE__ "`__LINE__'"
1037 keyword__FILE__ "`__FILE__'"
1038 keyword__ENCODING__ "`__ENCODING__'"
1040%token <val> tIDENTIFIER "local variable or method"
1041%token <val> tFID "method"
1042%token <val> tGVAR "global variable"
1043%token <val> tIVAR "instance variable"
1044%token <val> tCONSTANT "constant"
1045%token <val> tCVAR "class variable"
1047%token <val> tINTEGER "integer literal"
1048%token <val> tFLOAT "float literal"
1049%token <val> tRATIONAL "rational literal"
1050%token <val> tIMAGINARY "imaginary literal"
1051%token <val> tCHAR "char literal"
1052%token <val> tNTH_REF "numbered reference"
1053%token <val> tBACK_REF "back reference"
1054%token <val> tSTRING_CONTENT "literal content"
1055%token <val> tREGEXP_END
1057%type <val> singleton strings string string1 xstring regexp
1058%type <val> string_contents xstring_contents regexp_contents string_content
1059%type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1060%type <val> literal numeric simple_numeric ssym dsym symbol cpath
1061%type <val> top_compstmt top_stmts top_stmt begin_block
1062%type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1063%type <val> expr_value expr_value_do arg_value primary_value fcall rel_expr
1064%type <val> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1065%type <val> args call_args opt_call_args
1066%type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1067%type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1068%type <val> command_rhs arg_rhs
1069%type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
1070%type <val> f_block_optarg f_block_opt
1071%type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1072%type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
1073%type <val> block_param opt_block_param block_param_def f_opt
1074%type <val> f_kwarg f_kw f_block_kwarg f_block_kw
1075%type <val> bv_decls opt_bv_decl bvar
1076%type <val> lambda f_larglist lambda_body brace_body do_body
1077%type <val> brace_block cmd_brace_block do_block lhs none fitem
1078%type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1079%type <val> p_case_body p_cases p_top_expr p_top_expr_body
1080%type <val> p_expr p_as p_alt p_expr_basic
1081%type <val> p_args p_args_head p_args_tail p_args_post p_arg
1082%type <val> p_value p_primitive p_variable p_var_ref p_const
1083%type <val> p_kwargs p_kwarg p_kw
1084%type <val> keyword_variable user_variable sym operation operation2 operation3
1085%type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1086%type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1087%type <val> p_kwrest p_kwnorest p_kw_label
1088%type <val> f_no_kwarg args_forward
1089%token END_OF_INPUT 0 "end-of-input"
1091/* escaped chars, should be ignored otherwise */
1092%token <val> '\\' "backslash"
1093%token tSP "escaped space"
1094%token <val> '\t' "escaped horizontal tab"
1095%token <val> '\f' "escaped form feed"
1096%token <val> '\r' "escaped carriage return"
1097%token <val> '\13' "escaped vertical tab"
1098%token tUPLUS 132 "unary+"
1099%token tUMINUS 133 "unary-"
1101%token tCMP 135 "<=>"
1103%token tEQQ 141 "==="
1107%token tANDOP 148 "&&"
1108%token tOROP 149 "||"
1109%token tMATCH 143 "=~"
1110%token tNMATCH 144 "!~"
1111%token tDOT2 128 ".."
1112%token tDOT3 129 "..."
1113%token tBDOT2 130 "(.."
1114%token tBDOT3 131 "(..."
1115%token tAREF 145 "[]"
1116%token tASET 146 "[]="
1117%token tLSHFT 136 "<<"
1118%token tRSHFT 137 ">>"
1119%token <val> tANDDOT 150 "&."
1120%token <val> tCOLON2 147 "::"
1121%token tCOLON3 ":: at EXPR_BEG"
1122%token <val> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1125%token tLPAREN_ARG "( arg"
1129%token tLBRACE_ARG "{ arg"
1131%token tDSTAR "**arg"
1134%token tSYMBEG "symbol literal"
1135%token tSTRING_BEG "string literal"
1136%token tXSTRING_BEG "backtick literal"
1137%token tREGEXP_BEG "regexp literal"
1138%token tWORDS_BEG "word list"
1139%token tQWORDS_BEG "verbatim word list"
1140%token tSYMBOLS_BEG "symbol list"
1141%token tQSYMBOLS_BEG "verbatim symbol list"
1142%token tSTRING_END "terminator"
1143%token tSTRING_DEND "'}'"
1144%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1151%nonassoc tLBRACE_ARG
1153%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1154%left keyword_or keyword_and
1156%nonassoc keyword_defined
1158%left modifier_rescue
1160%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1163%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1164%left '>' tGEQ '<' tLEQ
1170%right tUMINUS_NUM tUMINUS
1172%right '!' '~' tUPLUS
1178 SET_LEX_STATE(EXPR_BEG);
1179 local_push(p, ifndef_ripper(1)+0);
1184 if ($2 && !compile_for_eval) {
1186 /* last expression should not be void */
1187 if (nd_type(node) == NODE_BLOCK) {
1188 while (node->nd_next) {
1189 node = node->nd_next;
1191 node = node->nd_head;
1193 node = remove_begin(node);
1196 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1198 {VALUE v1,v2;v1=$2;v2=dispatch1(program,v1);p->result=v2;}
1203top_compstmt : top_stmts opt_terms
1205 $$ = void_stmts(p, $1);
1212 $$ = NEW_BEGIN(0, &@$);
1214 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1219 $$ = newline_node($1);
1221 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1223 | top_stmts terms top_stmt
1226 $$ = block_append(p, $1, newline_node($3));
1228 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1232 $$ = remove_begin($2);
1237 | keyword_BEGIN begin_block
1243begin_block : '{' top_compstmt '}'
1246 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1247 NEW_BEGIN($2, &@$));
1248 $$ = NEW_BEGIN(0, &@$);
1250 {VALUE v1,v2;v1=$2;v2=dispatch1(BEGIN,v1);$$=v2;}
1256 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1261 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1263 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=escape_Qundef($5);v4=escape_Qundef($6);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1270 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1272 {VALUE v1,v2,v3,v4,v5;v1=escape_Qundef($1);v2=escape_Qundef($2);v3=Qnil;v4=escape_Qundef($3);v5=dispatch4(bodystmt,v1,v2,v3,v4);$$=v5;}
1276compstmt : stmts opt_terms
1278 $$ = void_stmts(p, $1);
1285 $$ = NEW_BEGIN(0, &@$);
1287 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(stmts_new);v2=dispatch0(void_stmt);v3=v1;v4=v2;v5=dispatch2(stmts_add,v3,v4);$$=v5;}
1292 $$ = newline_node($1);
1294 {VALUE v1,v2,v3,v4;v1=dispatch0(stmts_new);v2=v1;v3=$1;v4=dispatch2(stmts_add,v2,v3);$$=v4;}
1296 | stmts terms stmt_or_begin
1299 $$ = block_append(p, $1, newline_node($3));
1301 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(stmts_add,v1,v2);$$=v3;}
1305 $$ = remove_begin($2);
1315 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1323stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1326 $$ = NEW_ALIAS($2, $4, &@$);
1328 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(alias,v1,v2);$$=v3;}
1330 | keyword_alias tGVAR tGVAR
1333 $$ = NEW_VALIAS($2, $3, &@$);
1335 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1337 | keyword_alias tGVAR tBACK_REF
1342 buf[1] = (char)$3->nd_nth;
1343 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1345 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);$$=v3;}
1347 | keyword_alias tGVAR tNTH_REF
1350 yyerror1(&@3, "can't make alias for the number variables");
1351 $$ = NEW_BEGIN(0, &@$);
1353 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$3;v3=dispatch2(var_alias,v1,v2);v4=v3;v5=dispatch1(alias_error,v4);$$=v5;}ripper_error(p);
1355 | keyword_undef undef_list
1360 {VALUE v1,v2;v1=$2;v2=dispatch1(undef,v1);$$=v2;}
1362 | stmt modifier_if expr_value
1365 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1368 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
1370 | stmt modifier_unless expr_value
1373 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1376 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
1378 | stmt modifier_while expr_value
1381 if ($1 && nd_type($1) == NODE_BEGIN) {
1382 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1385 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1388 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(while_mod,v1,v2);$$=v3;}
1390 | stmt modifier_until expr_value
1393 if ($1 && nd_type($1) == NODE_BEGIN) {
1394 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1397 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1400 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(until_mod,v1,v2);$$=v3;}
1402 | stmt modifier_rescue stmt
1406 YYLTYPE loc = code_loc_gen(&@2, &@3);
1407 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1408 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1410 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1412 | keyword_END '{' compstmt '}'
1415 rb_warn0("END in method; use at_exit");
1419 NODE *scope = NEW_NODE(
1420 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1421 $$ = NEW_POSTEXE(scope, &@$);
1424 {VALUE v1,v2;v1=$3;v2=dispatch1(END,v1);$$=v2;}
1427 | mlhs '=' command_call
1431 $$ = node_assign(p, $1, $3, &@$);
1433 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1439 $$ = node_assign(p, $1, $3, &@$);
1441 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1443 | mlhs '=' mrhs_arg modifier_rescue stmt
1446 YYLTYPE loc = code_loc_gen(&@4, &@5);
1448 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1450 {VALUE v1,v2,v3,v4,v5,v6;v1=$3;v2=$5;v3=dispatch2(rescue_mod,v1,v2);v4=$1;v5=v3;v6=dispatch2(massign,v4,v5);$$=v6;}
1455 $$ = node_assign(p, $1, $3, &@$);
1457 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(massign,v1,v2);$$=v3;}
1462command_asgn : lhs '=' command_rhs
1465 $$ = node_assign(p, $1, $3, &@$);
1467 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
1469 | var_lhs tOP_ASGN command_rhs
1472 $$ = new_op_assign(p, $1, $2, $3, &@$);
1474 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
1476 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1479 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1481 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1484 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1487 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1489 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1491 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1494 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1496 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1498 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1501 YYLTYPE loc = code_loc_gen(&@1, &@3);
1502 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1504 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
1506 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1509 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1511 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
1513 | backref tOP_ASGN command_rhs
1516 rb_backref_error(p, $1);
1517 $$ = NEW_BEGIN(0, &@$);
1519 {VALUE v1,v2,v3,v4,v5;v1=var_field(p, $1);v2=$3;v3=dispatch2(assign,v1,v2);v4=v3;v5=dispatch1(assign_error,v4);$$=v5;}ripper_error(p);
1523command_rhs : command_call %prec tOP_ASGN
1528 | command_call modifier_rescue stmt
1531 YYLTYPE loc = code_loc_gen(&@2, &@3);
1533 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1535 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
1541 | expr keyword_and expr
1543 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1545 | expr keyword_or expr
1547 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1549 | keyword_not opt_nl expr
1551 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1555 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1560 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1561 p->command_start = FALSE;
1562 $<num>$ = p->in_kwarg;
1565 {$<tbl>$ = push_pvtbl(p);}
1567 {pop_pvtbl(p, $<tbl>4);}
1569 p->in_kwarg = !!$<num>3;
1571 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1573 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$5;v2=Qnil;v3=Qnil;v4=dispatch3(in,v1,v2,v3);v5=$1;v6=v4;v7=dispatch2(case,v5,v6);$$=v7;}
1575 | arg %prec tLBRACE_ARG
1585expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1591command_call : command
1595block_command : block_call
1596 | block_call call_op2 operation2 command_args
1599 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1601 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$2;v3=$3;v4=dispatch3(call,v1,v2,v3);v5=v4;v6=$4;v7=dispatch2(method_add_arg,v5,v6);$$=v7;}
1605cmd_brace_block : tLBRACE_ARG brace_body '}'
1609 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1610 nd_set_line($$, @1.end_pos.lineno);
1618 $$ = NEW_FCALL($1, 0, &@$);
1619 nd_set_line($$, p->tokline);
1625command : fcall command_args %prec tLOWEST
1629 nd_set_last_loc($1, @2.end_pos);
1632 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);$$=v3;}
1634 | fcall command_args cmd_brace_block
1637 block_dup_check(p, $2, $3);
1639 $$ = method_add_block(p, $1, $3, &@$);
1641 nd_set_last_loc($1, @2.end_pos);
1643 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$2;v3=dispatch2(command,v1,v2);v4=v3;v5=$3;v6=dispatch2(method_add_block,v4,v5);$$=v6;}
1645 | primary_value call_op operation2 command_args %prec tLOWEST
1648 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1650 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1652 | primary_value call_op operation2 command_args cmd_brace_block
1655 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1657 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1659 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1662 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1664 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);$$=v5;}
1666 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1669 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1671 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=$4;v5=dispatch4(command_call,v1,v2,v3,v4);v6=v5;v7=$5;v8=dispatch2(method_add_block,v6,v7);$$=v8;}
1673 | keyword_super command_args
1676 $$ = NEW_SUPER($2, &@$);
1679 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
1681 | keyword_yield command_args
1684 $$ = new_yield(p, $2, &@$);
1687 {VALUE v1,v2;v1=$2;v2=dispatch1(yield,v1);$$=v2;}
1689 | k_return call_args
1692 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1694 {VALUE v1,v2;v1=$2;v2=dispatch1(return,v1);$$=v2;}
1696 | keyword_break call_args
1699 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1701 {VALUE v1,v2;v1=$2;v2=dispatch1(break,v1);$$=v2;}
1703 | keyword_next call_args
1706 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1708 {VALUE v1,v2;v1=$2;v2=dispatch1(next,v1);$$=v2;}
1713 | tLPAREN mlhs_inner rparen
1718 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1722mlhs_inner : mlhs_basic
1723 | tLPAREN mlhs_inner rparen
1726 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1728 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1732mlhs_basic : mlhs_head
1735 $$ = NEW_MASGN($1, 0, &@$);
1739 | mlhs_head mlhs_item
1742 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1744 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1746 | mlhs_head tSTAR mlhs_node
1749 $$ = NEW_MASGN($1, $3, &@$);
1751 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1753 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1756 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1758 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$5;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1763 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1765 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
1767 | mlhs_head tSTAR ',' mlhs_post
1770 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1772 {VALUE v1,v2,v3,v4,v5,v6;v1=$1;v2=Qnil;v3=dispatch2(mlhs_add_star,v1,v2);v4=v3;v5=$4;v6=dispatch2(mlhs_add_post,v4,v5);$$=v6;}
1777 $$ = NEW_MASGN(0, $2, &@$);
1779 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1781 | tSTAR mlhs_node ',' mlhs_post
1784 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1786 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=$2;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$4;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1791 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1793 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
1795 | tSTAR ',' mlhs_post
1798 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1800 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=dispatch0(mlhs_new);v2=v1;v3=Qnil;v4=dispatch2(mlhs_add_star,v2,v3);v5=v4;v6=$3;v7=dispatch2(mlhs_add_post,v5,v6);$$=v7;}
1804mlhs_item : mlhs_node
1805 | tLPAREN mlhs_inner rparen
1810 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
1814mlhs_head : mlhs_item ','
1817 $$ = NEW_LIST($1, &@1);
1819 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1821 | mlhs_head mlhs_item ','
1824 $$ = list_append(p, $1, $2);
1826 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1830mlhs_post : mlhs_item
1833 $$ = NEW_LIST($1, &@$);
1835 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
1837 | mlhs_post ',' mlhs_item
1840 $$ = list_append(p, $1, $3);
1842 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
1846mlhs_node : user_variable
1849 $$ = assignable(p, $1, 0, &@$);
1851 $$=assignable(p, var_field(p, $1));
1856 $$ = assignable(p, $1, 0, &@$);
1858 $$=assignable(p, var_field(p, $1));
1860 | primary_value '[' opt_call_args rbracket
1863 $$ = aryset(p, $1, $3, &@$);
1865 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1867 | primary_value call_op tIDENTIFIER
1869 if ($2 == tANDDOT) {
1870 yyerror1(&@2, "&. inside multiple assignment destination");
1873 $$ = attrset(p, $1, $2, $3, &@$);
1875 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1877 | primary_value tCOLON2 tIDENTIFIER
1880 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1882 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=v3;}
1884 | primary_value call_op tCONSTANT
1886 if ($2 == tANDDOT) {
1887 yyerror1(&@2, "&. inside multiple assignment destination");
1890 $$ = attrset(p, $1, $2, $3, &@$);
1892 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1894 | primary_value tCOLON2 tCONSTANT
1897 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1899 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1904 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1906 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1911 rb_backref_error(p, $1);
1912 $$ = NEW_BEGIN(0, &@$);
1914 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1921 $$ = assignable(p, $1, 0, &@$);
1923 $$=assignable(p, var_field(p, $1));
1928 $$ = assignable(p, $1, 0, &@$);
1930 $$=assignable(p, var_field(p, $1));
1932 | primary_value '[' opt_call_args rbracket
1935 $$ = aryset(p, $1, $3, &@$);
1937 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);$$=v3;}
1939 | primary_value call_op tIDENTIFIER
1942 $$ = attrset(p, $1, $2, $3, &@$);
1944 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1946 | primary_value tCOLON2 tIDENTIFIER
1949 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1951 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1953 | primary_value call_op tCONSTANT
1956 $$ = attrset(p, $1, $2, $3, &@$);
1958 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);$$=v4;}
1960 | primary_value tCOLON2 tCONSTANT
1963 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1965 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);$$=const_decl(p, v3);}
1970 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1972 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_field,v1);$$=const_decl(p, v2);}
1977 rb_backref_error(p, $1);
1978 $$ = NEW_BEGIN(0, &@$);
1980 {VALUE v1,v2;v1=var_field(p, $1);v2=dispatch1(assign_error,v1);$$=v2;}ripper_error(p);
1987 yyerror1(&@1, "class/module name must be CONSTANT");
1989 {VALUE v1,v2;v1=$1;v2=dispatch1(class_name_error,v1);$$=v2;}ripper_error(p);
1994cpath : tCOLON3 cname
1997 $$ = NEW_COLON3($2, &@$);
1999 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2004 $$ = NEW_COLON2(0, $$, &@$);
2006 {VALUE v1,v2;v1=$1;v2=dispatch1(const_ref,v1);$$=v2;}
2008 | primary_value tCOLON2 cname
2011 $$ = NEW_COLON2($1, $3, &@$);
2013 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2022 SET_LEX_STATE(EXPR_ENDFN);
2031 $$ = NEW_LIT(ID2SYM($1), &@$);
2033 {VALUE v1,v2;v1=$1;v2=dispatch1(symbol_literal,v1);$$=v2;}
2041 $$ = NEW_UNDEF($1, &@$);
2043 $$=rb_ary_new3(1, get_value($1));
2045 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2048 NODE *undef = NEW_UNDEF($4, &@4);
2049 $$ = block_append(p, $1, undef);
2051 $$=rb_ary_push($1, get_value($4));
2055op : '|' { ifndef_ripper($$ = '|'); }
2056 | '^' { ifndef_ripper($$ = '^'); }
2057 | '&' { ifndef_ripper($$ = '&'); }
2058 | tCMP { ifndef_ripper($$ = tCMP); }
2059 | tEQ { ifndef_ripper($$ = tEQ); }
2060 | tEQQ { ifndef_ripper($$ = tEQQ); }
2061 | tMATCH { ifndef_ripper($$ = tMATCH); }
2062 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2063 | '>' { ifndef_ripper($$ = '>'); }
2064 | tGEQ { ifndef_ripper($$ = tGEQ); }
2065 | '<' { ifndef_ripper($$ = '<'); }
2066 | tLEQ { ifndef_ripper($$ = tLEQ); }
2067 | tNEQ { ifndef_ripper($$ = tNEQ); }
2068 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2069 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2070 | '+' { ifndef_ripper($$ = '+'); }
2071 | '-' { ifndef_ripper($$ = '-'); }
2072 | '*' { ifndef_ripper($$ = '*'); }
2073 | tSTAR { ifndef_ripper($$ = '*'); }
2074 | '/' { ifndef_ripper($$ = '/'); }
2075 | '%' { ifndef_ripper($$ = '%'); }
2076 | tPOW { ifndef_ripper($$ = tPOW); }
2077 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2078 | '!' { ifndef_ripper($$ = '!'); }
2079 | '~' { ifndef_ripper($$ = '~'); }
2080 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2081 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2082 | tAREF { ifndef_ripper($$ = tAREF); }
2083 | tASET { ifndef_ripper($$ = tASET); }
2084 | '`' { ifndef_ripper($$ = '`'); }
2087reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2088 | keyword_BEGIN | keyword_END
2089 | keyword_alias | keyword_and | keyword_begin
2090 | keyword_break | keyword_case | keyword_class | keyword_def
2091 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2092 | keyword_end | keyword_ensure | keyword_false
2093 | keyword_for | keyword_in | keyword_module | keyword_next
2094 | keyword_nil | keyword_not | keyword_or | keyword_redo
2095 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2096 | keyword_super | keyword_then | keyword_true | keyword_undef
2097 | keyword_when | keyword_yield | keyword_if | keyword_unless
2098 | keyword_while | keyword_until
2101arg : lhs '=' arg_rhs
2104 $$ = node_assign(p, $1, $3, &@$);
2106 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assign,v1,v2);$$=v3;}
2108 | var_lhs tOP_ASGN arg_rhs
2111 $$ = new_op_assign(p, $1, $2, $3, &@$);
2113 {VALUE v1,v2,v3,v4;v1=$1;v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);$$=v4;}
2115 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2119 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2121 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref_field,v1,v2);v4=v3;v5=$5;v6=$6;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2123 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2127 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2129 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2131 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2135 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2137 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=$2;v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2139 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2143 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2145 {VALUE v1,v2,v3,v4,v5,v6,v7,v8;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(field,v1,v2,v3);v5=v4;v6=$4;v7=$5;v8=dispatch3(opassign,v5,v6,v7);$$=v8;}
2147 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2150 YYLTYPE loc = code_loc_gen(&@1, &@3);
2151 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2153 {VALUE v1,v2,v3,v4,v5,v6,v7;v1=$1;v2=$3;v3=dispatch2(const_path_field,v1,v2);v4=v3;v5=$4;v6=$5;v7=dispatch3(opassign,v4,v5,v6);$$=v7;}
2155 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2158 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2160 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=dispatch1(top_const_field,v1);v3=v2;v4=$3;v5=$4;v6=dispatch3(opassign,v3,v4,v5);$$=v6;}
2162 | backref tOP_ASGN arg_rhs
2165 rb_backref_error(p, $1);
2166 $$ = NEW_BEGIN(0, &@$);
2168 {VALUE v1,v2,v3,v4,v5,v6;v1=var_field(p, $1);v2=$2;v3=$3;v4=dispatch3(opassign,v1,v2,v3);v5=v4;v6=dispatch1(assign_error,v5);$$=v6;}ripper_error(p);
2175 $$ = NEW_DOT2($1, $3, &@$);
2177 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
2184 $$ = NEW_DOT3($1, $3, &@$);
2186 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
2192 loc.beg_pos = @2.end_pos;
2193 loc.end_pos = @2.end_pos;
2196 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2198 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
2204 loc.beg_pos = @2.end_pos;
2205 loc.end_pos = @2.end_pos;
2208 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2210 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
2216 loc.beg_pos = @1.beg_pos;
2217 loc.end_pos = @1.beg_pos;
2220 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2222 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
2228 loc.beg_pos = @1.beg_pos;
2229 loc.end_pos = @1.beg_pos;
2232 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2234 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
2238 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2242 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2246 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2250 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2254 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2258 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2260 | tUMINUS_NUM simple_numeric tPOW arg
2262 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2266 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2270 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2274 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2278 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2282 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2286 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2288 | rel_expr %prec tCMP
2291 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2295 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2299 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2303 $$ = match_op(p, $1, $3, &@2, &@$);
2307 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2311 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2315 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2319 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2323 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2327 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2331 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2333 | keyword_defined opt_nl {p->in_defined = 1;} arg
2336 $$ = new_defined(p, $4, &@$);
2338 | arg '?' arg opt_nl ':' arg
2342 $$ = new_if(p, $1, $3, $6, &@$);
2345 {VALUE v1,v2,v3,v4;v1=$1;v2=$3;v3=$6;v4=dispatch3(ifop,v1,v2,v3);$$=v4;}
2353relop : '>' {$$ = '>';}
2359rel_expr : arg relop arg %prec '>'
2361 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2363 | rel_expr relop arg %prec '>'
2365 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2366 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2382 | args ',' assocs trailer
2385 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2387 {VALUE v1,v2,v3,v4,v5;v1=$3;v2=dispatch1(bare_assoc_hash,v1);v3=$1;v4=v2;v5=dispatch2(args_add,v3,v4);$$=v5;}
2392 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2394 {VALUE v1,v2,v3,v4,v5,v6;v1=dispatch0(args_new);v2=$1;v3=dispatch1(bare_assoc_hash,v2);v4=v1;v5=v3;v6=dispatch2(args_add,v4,v5);$$=v6;}
2398arg_rhs : arg %prec tOP_ASGN
2403 | arg modifier_rescue arg
2406 YYLTYPE loc = code_loc_gen(&@2, &@3);
2408 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2410 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(rescue_mod,v1,v2);$$=v3;}
2414paren_args : '(' opt_call_args rparen
2419 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(arg_paren,v1);$$=v2;}
2421 | '(' args ',' args_forward rparen
2423 if (!local_id(p, idFWD_REST) ||
2425 !local_id(p, idFWD_KWREST) ||
2427 !local_id(p, idFWD_BLOCK)) {
2428 compile_error(p, "unexpected ...");
2433 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@4), &@4);
2435 NODE *kwrest = list_append(p, NEW_LIST(0, &@4), NEW_LVAR(idFWD_KWREST, &@4));
2437 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@4), &@4);
2438 $$ = rest_arg_append(p, $2, splat, &@$);
2440 $$ = arg_append(p, $$, new_hash(p, kwrest, &@4), &@4);
2442 $$ = arg_blk_pass($$, block);
2444 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=$4;v3=dispatch2(args_add,v1,v2);v4=v3;v5=dispatch1(arg_paren,v4);$$=v5;}
2447 | '(' args_forward rparen
2449 if (!local_id(p, idFWD_REST) ||
2451 !local_id(p, idFWD_KWREST) ||
2453 !local_id(p, idFWD_BLOCK)) {
2454 compile_error(p, "unexpected ...");
2459 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2461 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2463 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2465 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2469 $$ = arg_blk_pass($$, block);
2471 {VALUE v1,v2;v1=$2;v2=dispatch1(arg_paren,v1);$$=v2;}
2476opt_paren_args : none
2486 | args ',' assocs ','
2489 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2491 {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;}
2496 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2498 {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;}
2506 $$ = NEW_LIST($1, &@$);
2508 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2510 | args opt_block_arg
2513 $$ = arg_blk_pass($1, $2);
2515 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(args_add_block,v1,v2);$$=v3;}
2517 | assocs opt_block_arg
2520 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2521 $$ = arg_blk_pass($$, $2);
2523 {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;}
2525 | args ',' assocs opt_block_arg
2528 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2529 $$ = arg_blk_pass($$, $4);
2531 {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;}
2534 {{VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add_block,v2,v3);$$=v4;}}
2538 /* If call_args starts with a open paren '(' or '[',
2539 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2540 * but the push must be done after CMDARG_PUSH(1).
2541 * So this code makes them consistent by first cancelling
2542 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2543 * and finally redoing CMDARG_PUSH(0).
2547 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2550 if (lookahead) CMDARG_POP();
2552 if (lookahead) CMDARG_PUSH(0);
2556 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2557 * but the push must be done after CMDARG_POP() in the parser.
2558 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2559 * CMDARG_POP() to pop 1 pushed by command_args,
2560 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2567 if (lookahead) CMDARG_POP();
2569 if (lookahead) CMDARG_PUSH(0);
2574block_arg : tAMPER arg_value
2577 $$ = NEW_BLOCK_PASS($2, &@$);
2583opt_block_arg : ',' block_arg
2596 $$ = NEW_LIST($1, &@$);
2598 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
2603 $$ = NEW_SPLAT($2, &@$);
2605 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
2607 | args ',' arg_value
2610 $$ = last_arg_append(p, $1, $3, &@$);
2612 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
2614 | args ',' tSTAR arg_value
2617 $$ = rest_arg_append(p, $1, $4, &@$);
2619 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
2627mrhs : args ',' arg_value
2630 $$ = last_arg_append(p, $1, $3, &@$);
2632 {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;}
2634 | args ',' tSTAR arg_value
2637 $$ = rest_arg_append(p, $1, $4, &@$);
2639 {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;}
2644 $$ = NEW_SPLAT($2, &@$);
2646 {VALUE v1,v2,v3,v4;v1=dispatch0(mrhs_new);v2=v1;v3=$2;v4=dispatch2(mrhs_add_star,v2,v3);$$=v4;}
2663 $$ = NEW_FCALL($1, 0, &@$);
2665 {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;}
2676 set_line_body($3, @1.end_pos.lineno);
2677 $$ = NEW_BEGIN($3, &@$);
2678 nd_set_line($$, @1.end_pos.lineno);
2680 {VALUE v1,v2;v1=$3;v2=dispatch1(begin,v1);$$=v2;}
2682 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2685 $$ = NEW_BEGIN(0, &@$);
2687 {VALUE v1,v2;v1=0;v2=dispatch1(paren,v1);$$=v2;}
2689 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2692 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2695 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2697 | tLPAREN compstmt ')'
2700 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2703 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
2705 | primary_value tCOLON2 tCONSTANT
2708 $$ = NEW_COLON2($1, $3, &@$);
2710 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
2715 $$ = NEW_COLON3($2, &@$);
2717 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
2719 | tLBRACK aref_args ']'
2722 $$ = make_list($2, &@$);
2724 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(array,v1);$$=v2;}
2726 | tLBRACE assoc_list '}'
2729 $$ = new_hash(p, $2, &@$);
2730 $$->nd_brace = TRUE;
2732 {VALUE v1,v2;v1=escape_Qundef($2);v2=dispatch1(hash,v1);$$=v2;}
2737 $$ = NEW_RETURN(0, &@$);
2739 {VALUE v1;v1=dispatch0(return0);$$=v1;}
2741 | keyword_yield '(' call_args rparen
2744 $$ = new_yield(p, $3, &@$);
2746 {VALUE v1,v2,v3,v4;v1=$3;v2=dispatch1(paren,v1);v3=v2;v4=dispatch1(yield,v3);$$=v4;}
2748 | keyword_yield '(' rparen
2751 $$ = NEW_YIELD(0, &@$);
2753 {VALUE v1,v2,v3,v4,v5;v1=dispatch0(args_new);v2=v1;v3=dispatch1(paren,v2);v4=v3;v5=dispatch1(yield,v4);$$=v5;}
2758 $$ = NEW_YIELD(0, &@$);
2760 {VALUE v1;v1=dispatch0(yield0);$$=v1;}
2762 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2765 $$ = new_defined(p, $5, &@$);
2767 | keyword_not '(' expr rparen
2769 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2771 | keyword_not '(' rparen
2773 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2778 $$ = method_add_block(p, $1, $2, &@$);
2780 {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;}
2783 | method_call brace_block
2786 block_dup_check(p, $1->nd_args, $2);
2787 $$ = method_add_block(p, $1, $2, &@$);
2789 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
2793 token_info_push(p, "->", &@1);
2799 nd_set_first_loc($$, @1.beg_pos);
2802 | k_if expr_value then
2808 $$ = new_if(p, $2, $4, $5, &@$);
2811 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(if,v1,v2,v3);$$=v4;}
2813 | k_unless expr_value then
2819 $$ = new_unless(p, $2, $4, $5, &@$);
2822 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(unless,v1,v2,v3);$$=v4;}
2824 | k_while expr_value_do
2829 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2832 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(while,v1,v2);$$=v3;}
2834 | k_until expr_value_do
2839 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2842 {VALUE v1,v2,v3;v1=$2;v2=$3;v3=dispatch2(until,v1,v2);$$=v3;}
2844 | k_case expr_value opt_terms
2846 $<val>$ = p->case_labels;
2847 p->case_labels = Qnil;
2852 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2853 p->case_labels = $<val>4;
2855 $$ = NEW_CASE($2, $5, &@$);
2858 {VALUE v1,v2,v3;v1=$2;v2=$5;v3=dispatch2(case,v1,v2);$$=v3;}
2862 $<val>$ = p->case_labels;
2868 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2869 p->case_labels = $<val>3;
2871 $$ = NEW_CASE2($4, &@$);
2873 {VALUE v1,v2,v3;v1=Qnil;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2875 | k_case expr_value opt_terms
2880 $$ = new_case3(p, $2, $4, &@$);
2882 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(case,v1,v2);$$=v3;}
2884 | k_for for_var keyword_in expr_value_do
2892 * e.each{|*x| a, b, c = x}
2896 * e.each{|x| a, = x}
2898 ID id = internal_id(p);
2899 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2900 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2901 ID *tbl = ALLOC_N(ID, 3);
2902 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2903 rb_ast_add_local_table(p->ast, tbl);
2905 switch (nd_type($2)) {
2908 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2909 $2->nd_value = internal_var;
2914 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2915 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2917 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2918 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2920 /* {|*internal_id| <m> = internal_id; ... } */
2921 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2922 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2923 $$ = NEW_FOR($4, scope, &@$);
2926 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=$5;v4=dispatch3(for,v1,v2,v3);$$=v4;}
2928 | k_class cpath superclass
2931 YYLTYPE loc = code_loc_gen(&@1, &@2);
2932 yyerror1(&loc, "class definition in method body");
2934 $<num>1 = p->in_class;
2942 $$ = NEW_CLASS($2, $5, $3, &@$);
2943 nd_set_line($$->nd_body, @6.end_pos.lineno);
2944 set_line_body($5, @3.end_pos.lineno);
2945 nd_set_line($$, @3.end_pos.lineno);
2947 {VALUE v1,v2,v3,v4;v1=$2;v2=$3;v3=$5;v4=dispatch3(class,v1,v2,v3);$$=v4;}
2949 p->in_class = $<num>1 & 1;
2951 | k_class tLSHFT expr
2953 $<num>$ = (p->in_class << 1) | p->in_def;
2963 $$ = NEW_SCLASS($3, $6, &@$);
2964 nd_set_line($$->nd_body, @7.end_pos.lineno);
2965 set_line_body($6, nd_line($3));
2968 {VALUE v1,v2,v3;v1=$3;v2=$6;v3=dispatch2(sclass,v1,v2);$$=v3;}
2970 p->in_def = $<num>4 & 1;
2971 p->in_class = ($<num>4 >> 1) & 1;
2976 YYLTYPE loc = code_loc_gen(&@1, &@2);
2977 yyerror1(&loc, "module definition in method body");
2979 $<num>1 = p->in_class;
2987 $$ = NEW_MODULE($2, $4, &@$);
2988 nd_set_line($$->nd_body, @5.end_pos.lineno);
2989 set_line_body($4, @2.end_pos.lineno);
2990 nd_set_line($$, @2.end_pos.lineno);
2992 {VALUE v1,v2,v3;v1=$2;v2=$4;v3=dispatch2(module,v1,v2);$$=v3;}
2994 p->in_class = $<num>1 & 1;
2998 numparam_name(p, get_id($2));
3000 $<id>$ = p->cur_arg;
3004 $<num>$ = p->in_def;
3012 NODE *body = remove_begin($6);
3013 reduce_nodes(p, &body);
3014 $$ = NEW_DEFN($2, $5, body, &@$);
3015 nd_set_line($$->nd_defn, @7.end_pos.lineno);
3016 set_line_body(body, @1.beg_pos.lineno);
3018 {VALUE v1,v2,v3,v4;v1=$2;v2=$5;v3=$6;v4=dispatch3(def,v1,v2,v3);$$=v4;}
3020 p->in_def = $<num>4 & 1;
3021 p->cur_arg = $<id>3;
3023 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3025 numparam_name(p, get_id($5));
3026 $<num>4 = p->in_def;
3028 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3030 $<id>$ = p->cur_arg;
3038 NODE *body = remove_begin($8);
3039 reduce_nodes(p, &body);
3040 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3041 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3042 set_line_body(body, @1.beg_pos.lineno);
3044 {VALUE v1,v2,v3,v4,v5,v6;v1=$2;v2=$3;v3=$5;v4=$7;v5=$8;v6=dispatch5(defs,v1,v2,v3,v4,v5);$$=v6;}
3046 p->in_def = $<num>4 & 1;
3047 p->cur_arg = $<id>6;
3052 $$ = NEW_BREAK(0, &@$);
3054 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(break,v2);$$=v3;}
3059 $$ = NEW_NEXT(0, &@$);
3061 {VALUE v1,v2,v3;v1=dispatch0(args_new);v2=v1;v3=dispatch1(next,v2);$$=v3;}
3068 {VALUE v1;v1=dispatch0(redo);$$=v1;}
3073 $$ = NEW_RETRY(&@$);
3075 {VALUE v1;v1=dispatch0(retry);$$=v1;}
3079primary_value : primary
3086k_begin : keyword_begin
3088 token_info_push(p, "begin", &@$);
3095 token_info_push(p, "if", &@$);
3096 if (p->token_info && p->token_info->nonspc &&
3097 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3098 const char *tok = p->lex.ptok;
3099 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3100 beg += rb_strlen_lit("else");
3101 while (beg < tok && ISSPACE(*beg)) beg++;
3103 p->token_info->nonspc = 0;
3109k_unless : keyword_unless
3111 token_info_push(p, "unless", &@$);
3115k_while : keyword_while
3117 token_info_push(p, "while", &@$);
3121k_until : keyword_until
3123 token_info_push(p, "until", &@$);
3127k_case : keyword_case
3129 token_info_push(p, "case", &@$);
3135 token_info_push(p, "for", &@$);
3139k_class : keyword_class
3141 token_info_push(p, "class", &@$);
3145k_module : keyword_module
3147 token_info_push(p, "module", &@$);
3153 token_info_push(p, "def", &@$);
3159 token_info_push(p, "do", &@$);
3163k_do_block : keyword_do_block
3165 token_info_push(p, "do", &@$);
3169k_rescue : keyword_rescue
3171 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3175k_ensure : keyword_ensure
3177 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3181k_when : keyword_when
3183 token_info_warn(p, "when", p->token_info, 0, &@$);
3187k_else : keyword_else
3189 token_info *ptinfo_beg = p->token_info;
3190 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3191 token_info_warn(p, "else", p->token_info, same, &@$);
3194 e.next = ptinfo_beg->next;
3196 token_info_setup(&e, p->lex.pbeg, &@$);
3197 if (!e.nonspc) *ptinfo_beg = e;
3202k_elsif : keyword_elsif
3205 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3211 token_info_pop(p, "end", &@$);
3215k_return : keyword_return
3217 if (p->in_class && !p->in_def && !dyna_in_block(p))
3218 yyerror1(&@1, "Invalid return in class/module body");
3232 | k_elsif expr_value then
3237 $$ = new_if(p, $2, $4, $5, &@$);
3240 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(elsif,v1,v2,v3);$$=v4;}
3250 {VALUE v1,v2;v1=$2;v2=dispatch1(else,v1);$$=v2;}
3261 $$ = assignable(p, $1, 0, &@$);
3262 mark_lvar_used(p, $$);
3264 $$=assignable(p, $1);
3266 | tLPAREN f_margs rparen
3271 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
3278 $$ = NEW_LIST($1, &@$);
3280 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add,v2,v3);$$=v4;}
3282 | f_marg_list ',' f_marg
3285 $$ = list_append(p, $1, $3);
3287 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add,v1,v2);$$=v3;}
3291f_margs : f_marg_list
3294 $$ = NEW_MASGN($1, 0, &@$);
3298 | f_marg_list ',' f_rest_marg
3301 $$ = NEW_MASGN($1, $3, &@$);
3303 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(mlhs_add_star,v1,v2);$$=v3;}
3305 | f_marg_list ',' f_rest_marg ',' f_marg_list
3308 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3310 {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;}
3315 $$ = NEW_MASGN(0, $1, &@$);
3317 {VALUE v1,v2,v3,v4;v1=dispatch0(mlhs_new);v2=v1;v3=$1;v4=dispatch2(mlhs_add_star,v2,v3);$$=v4;}
3319 | f_rest_marg ',' f_marg_list
3322 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3324 {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;}
3328f_rest_marg : tSTAR f_norm_arg
3331 $$ = assignable(p, $2, 0, &@$);
3332 mark_lvar_used(p, $$);
3334 $$=assignable(p, $2);
3339 $$ = NODE_SPECIAL_NO_NAME_REST;
3345block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3347 $$ = new_args_tail(p, $1, $3, $4, &@3);
3349 | f_block_kwarg opt_f_block_arg
3351 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3353 | f_kwrest opt_f_block_arg
3355 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3357 | f_no_kwarg opt_f_block_arg
3359 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3363 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3367opt_block_args_tail : ',' block_args_tail
3373 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3377block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3379 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3381 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3383 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3385 | f_arg ',' f_block_optarg opt_block_args_tail
3387 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3389 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3391 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3393 | f_arg ',' f_rest_arg opt_block_args_tail
3395 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3400 /* magic number for rest_id in iseq_set_arguments() */
3401 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3403 {VALUE v1;v1=dispatch0(excessed_comma);$$=new_args(p, $1, Qnone, v1, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL);}
3405 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3407 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3409 | f_arg opt_block_args_tail
3411 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3413 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3415 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3417 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3419 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3421 | f_block_optarg opt_block_args_tail
3423 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3425 | f_block_optarg ',' f_arg opt_block_args_tail
3427 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3429 | f_rest_arg opt_block_args_tail
3431 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3433 | f_rest_arg ',' f_arg opt_block_args_tail
3435 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3439 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3443opt_block_param : none
3446 p->command_start = TRUE;
3450block_param_def : '|' opt_bv_decl '|'
3453 p->max_numparam = ORDINAL_PARAM;
3457 {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;}
3459 | '|' block_param opt_bv_decl '|'
3462 p->max_numparam = ORDINAL_PARAM;
3466 {VALUE v1,v2,v3;v1=escape_Qundef($2);v2=escape_Qundef($3);v3=dispatch2(block_var,v1,v2);$$=v3;}
3475 | opt_nl ';' bv_decls opt_nl
3485 {$$=rb_ary_new3(1, get_value($1));}
3487 {$$=rb_ary_push($1, get_value($3));}
3492 new_bv(p, get_id($1));
3502 $<vars>$ = dyna_push(p);
3505 $<num>$ = p->lex.lpar_beg;
3506 p->lex.lpar_beg = p->lex.paren_nest;
3509 $<num>$ = p->max_numparam;
3510 p->max_numparam = 0;
3513 $<node>$ = numparam_push(p);
3521 int max_numparam = p->max_numparam;
3522 p->lex.lpar_beg = $<num>2;
3523 p->max_numparam = $<num>3;
3525 $5 = args_with_numbered(p, $5, max_numparam);
3528 YYLTYPE loc = code_loc_gen(&@5, &@7);
3529 $$ = NEW_LAMBDA($5, $7, &loc);
3530 nd_set_line($$->nd_body, @7.end_pos.lineno);
3531 nd_set_line($$, @5.end_pos.lineno);
3534 {VALUE v1,v2,v3;v1=$5;v2=$7;v3=dispatch2(lambda,v1,v2);$$=v3;}
3535 numparam_pop(p, $<node>4);
3536 dyna_pop(p, $<vars>1);
3540f_larglist : '(' f_args opt_bv_decl ')'
3544 p->max_numparam = ORDINAL_PARAM;
3546 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
3551 if (!args_info_empty_p($1->nd_ainfo))
3552 p->max_numparam = ORDINAL_PARAM;
3558lambda_body : tLAMBEG compstmt '}'
3560 token_info_pop(p, "}", &@3);
3563 | keyword_do_LAMBDA bodystmt k_end
3569do_block : k_do_block do_body k_end
3573 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3574 nd_set_line($$, @1.end_pos.lineno);
3579block_call : command do_block
3582 if (nd_type($1) == NODE_YIELD) {
3583 compile_error(p, "block given to yield");
3586 block_dup_check(p, $1->nd_args, $2);
3588 $$ = method_add_block(p, $1, $2, &@$);
3591 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(method_add_block,v1,v2);$$=v3;}
3593 | block_call call_op2 operation2 opt_paren_args
3596 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3598 {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;}
3600 | block_call call_op2 operation2 opt_paren_args brace_block
3603 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3605 {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;}
3607 | block_call call_op2 operation2 command_args do_block
3610 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3612 {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;}
3616method_call : fcall paren_args
3621 nd_set_last_loc($1, @2.end_pos);
3623 {VALUE v1,v2,v3,v4,v5;v1=$1;v2=dispatch1(fcall,v1);v3=v2;v4=$2;v5=dispatch2(method_add_arg,v3,v4);$$=v5;}
3625 | primary_value call_op operation2 opt_paren_args
3628 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3629 nd_set_line($$, @3.end_pos.lineno);
3631 {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;}
3633 | primary_value tCOLON2 operation2 paren_args
3636 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3637 nd_set_line($$, @3.end_pos.lineno);
3639 {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;}
3641 | primary_value tCOLON2 operation3
3644 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3646 {VALUE v1,v2,v3,v4;v1=$1;v2=ID2VAL(idCOLON2);v3=$3;v4=dispatch3(call,v1,v2,v3);$$=v4;}
3648 | primary_value call_op paren_args
3651 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3652 nd_set_line($$, @2.end_pos.lineno);
3654 {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;}
3656 | primary_value tCOLON2 paren_args
3659 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3660 nd_set_line($$, @2.end_pos.lineno);
3662 {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;}
3664 | keyword_super paren_args
3667 $$ = NEW_SUPER($2, &@$);
3669 {VALUE v1,v2;v1=$2;v2=dispatch1(super,v1);$$=v2;}
3674 $$ = NEW_ZSUPER(&@$);
3676 {VALUE v1;v1=dispatch0(zsuper);$$=v1;}
3678 | primary_value '[' opt_call_args rbracket
3681 if ($1 && nd_type($1) == NODE_SELF)
3682 $$ = NEW_FCALL(tAREF, $3, &@$);
3684 $$ = NEW_CALL($1, tAREF, $3, &@$);
3687 {VALUE v1,v2,v3;v1=$1;v2=escape_Qundef($3);v3=dispatch2(aref,v1,v2);$$=v3;}
3691brace_block : '{' brace_body '}'
3695 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3696 nd_set_line($$, @1.end_pos.lineno);
3699 | k_do do_body k_end
3703 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3704 nd_set_line($$, @1.end_pos.lineno);
3709brace_body : {$<vars>$ = dyna_push(p);}
3711 $<num>$ = p->max_numparam;
3712 p->max_numparam = 0;
3715 $<node>$ = numparam_push(p);
3717 opt_block_param compstmt
3719 int max_numparam = p->max_numparam;
3720 p->max_numparam = $<num>2;
3721 $4 = args_with_numbered(p, $4, max_numparam);
3723 $$ = NEW_ITER($4, $5, &@$);
3725 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(brace_block,v1,v2);$$=v3;}
3726 numparam_pop(p, $<node>3);
3727 dyna_pop(p, $<vars>1);
3731do_body : {$<vars>$ = dyna_push(p);}
3733 $<num>$ = p->max_numparam;
3734 p->max_numparam = 0;
3737 $<node>$ = numparam_push(p);
3740 opt_block_param bodystmt
3742 int max_numparam = p->max_numparam;
3743 p->max_numparam = $<num>2;
3744 $4 = args_with_numbered(p, $4, max_numparam);
3746 $$ = NEW_ITER($4, $5, &@$);
3748 {VALUE v1,v2,v3;v1=escape_Qundef($4);v2=$5;v3=dispatch2(do_block,v1,v2);$$=v3;}
3750 numparam_pop(p, $<node>3);
3751 dyna_pop(p, $<vars>1);
3755case_args : arg_value
3758 check_literal_when(p, $1, &@1);
3759 $$ = NEW_LIST($1, &@$);
3761 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$1;v4=dispatch2(args_add,v2,v3);$$=v4;}
3766 $$ = NEW_SPLAT($2, &@$);
3768 {VALUE v1,v2,v3,v4;v1=dispatch0(args_new);v2=v1;v3=$2;v4=dispatch2(args_add_star,v2,v3);$$=v4;}
3770 | case_args ',' arg_value
3773 check_literal_when(p, $3, &@3);
3774 $$ = last_arg_append(p, $1, $3, &@$);
3776 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(args_add,v1,v2);$$=v3;}
3778 | case_args ',' tSTAR arg_value
3781 $$ = rest_arg_append(p, $1, $4, &@$);
3783 {VALUE v1,v2,v3;v1=$1;v2=$4;v3=dispatch2(args_add_star,v1,v2);$$=v3;}
3787case_body : k_when case_args then
3792 $$ = NEW_WHEN($2, $4, $5, &@$);
3795 {VALUE v1,v2,v3,v4;v1=$2;v2=$4;v3=escape_Qundef($5);v4=dispatch3(when,v1,v2,v3);$$=v4;}
3803p_case_body : keyword_in
3805 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3806 p->command_start = FALSE;
3807 $<num>$ = p->in_kwarg;
3810 {$<tbl>$ = push_pvtbl(p);}
3811 {$<tbl>$ = push_pktbl(p);}
3813 {pop_pktbl(p, $<tbl>4);}
3814 {pop_pvtbl(p, $<tbl>3);}
3816 p->in_kwarg = !!$<num>2;
3822 $$ = NEW_IN($5, $10, $11, &@$);
3824 {VALUE v1,v2,v3,v4;v1=$5;v2=$10;v3=escape_Qundef($11);v4=dispatch3(in,v1,v2,v3);$$=v4;}
3832p_top_expr : p_top_expr_body
3833 | p_top_expr_body modifier_if expr_value
3836 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3839 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(if_mod,v1,v2);$$=v3;}
3841 | p_top_expr_body modifier_unless expr_value
3844 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3847 {VALUE v1,v2,v3;v1=$3;v2=$1;v3=dispatch2(unless_mod,v1,v2);$$=v3;}
3851p_top_expr_body : p_expr
3854 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3855 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3859 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3861 nd_set_first_loc($$, @1.beg_pos);
3867 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3871 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3878p_as : p_expr tASSOC p_variable
3881 NODE *n = NEW_LIST($1, &@$);
3882 n = list_append(p, n, $3);
3883 $$ = new_hash(p, n, &@$);
3885 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(id_assoc);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3890p_alt : p_alt '|' p_expr_basic
3893 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3895 {VALUE v1,v2,v3,v4;v1=$1;v2=STATIC_ID2SYM(idOr);v3=$3;v4=dispatch3(binary,v1,v2,v3);$$=v4;}
3900p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3901p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3903p_expr_basic : p_value
3904 | p_const p_lparen p_args rparen
3906 pop_pktbl(p, $<tbl>2);
3907 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3909 nd_set_first_loc($$, @1.beg_pos);
3913 | p_const p_lparen p_kwargs rparen
3915 pop_pktbl(p, $<tbl>2);
3916 $$ = new_hash_pattern(p, $1, $3, &@$);
3918 nd_set_first_loc($$, @1.beg_pos);
3922 | p_const '(' rparen
3924 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3925 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3927 | p_const p_lbracket p_args rbracket
3929 pop_pktbl(p, $<tbl>2);
3930 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3932 nd_set_first_loc($$, @1.beg_pos);
3936 | p_const p_lbracket p_kwargs rbracket
3938 pop_pktbl(p, $<tbl>2);
3939 $$ = new_hash_pattern(p, $1, $3, &@$);
3941 nd_set_first_loc($$, @1.beg_pos);
3945 | p_const '[' rbracket
3947 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3948 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3950 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3952 pop_pktbl(p, $<tbl>2);
3953 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3957 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3958 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3962 $<tbl>$ = push_pktbl(p);
3963 $<num>1 = p->in_kwarg;
3968 pop_pktbl(p, $<tbl>2);
3969 p->in_kwarg = $<num>1;
3970 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3974 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3975 $$ = new_hash_pattern(p, Qnone, $$, &@$);
3977 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3979 pop_pktbl(p, $<tbl>2);
3987 NODE *pre_args = NEW_LIST($1, &@$);
3988 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3990 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3995 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4000 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4002 VALUE pre_args = rb_ary_concat($1, get_value($2));
4003 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4006 | p_args_head tSTAR tIDENTIFIER
4008 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4010 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4012 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4016 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4018 | p_args_head tSTAR ',' p_args_post
4020 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4025p_args_head : p_arg ','
4029 | p_args_head p_arg ','
4032 $$ = list_concat($1, $2);
4034 $$=rb_ary_concat($1, get_value($2));
4038p_args_tail : tSTAR tIDENTIFIER
4040 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4042 | tSTAR tIDENTIFIER ',' p_args_post
4044 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4048 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4050 | tSTAR ',' p_args_post
4052 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4057 | p_args_post ',' p_arg
4060 $$ = list_concat($1, $3);
4062 $$=rb_ary_concat($1, get_value($3));
4069 $$ = NEW_LIST($1, &@$);
4071 $$=rb_ary_new_from_args(1, get_value($1));
4075p_kwargs : p_kwarg ',' p_kwrest
4077 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4081 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4085 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4089 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4091 | p_kwarg ',' p_kwnorest
4093 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4097 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4102 {$$=rb_ary_new_from_args(1, $1);}
4106 $$ = list_concat($1, $3);
4108 $$=rb_ary_push($1, $3);
4112p_kw : p_kw_label p_expr
4114 error_duplicate_pattern_key(p, get_id($1), &@1);
4116 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4118 $$=rb_ary_new_from_args(2, get_value($1), get_value($2));
4122 error_duplicate_pattern_key(p, get_id($1), &@1);
4123 if ($1 && !is_local_id(get_id($1))) {
4124 yyerror1(&@1, "key must be valid as local variables");
4126 error_duplicate_pattern_variable(p, get_id($1), &@1);
4128 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4130 $$=rb_ary_new_from_args(2, get_value($1), Qnil);
4135 | tSTRING_BEG string_contents tLABEL_END
4137 YYLTYPE loc = code_loc_gen(&@1, &@3);
4139 if (!$2 || nd_type($2) == NODE_STR) {
4140 NODE *node = dsym_node(p, $2, &loc);
4141 $$ = SYM2ID(node->nd_lit);
4144 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4145 VALUE label = RNODE($2)->nd_cval;
4146 VALUE rval = RNODE($2)->nd_rval;
4147 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4148 RNODE($$)->nd_loc = loc;
4152 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4158p_kwrest : kwrest_mark tIDENTIFIER
4168p_kwnorest : kwrest_mark keyword_nil
4174p_value : p_primitive
4175 | p_primitive tDOT2 p_primitive
4180 $$ = NEW_DOT2($1, $3, &@$);
4182 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot2,v1,v2);$$=v3;}
4184 | p_primitive tDOT3 p_primitive
4189 $$ = NEW_DOT3($1, $3, &@$);
4191 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(dot3,v1,v2);$$=v3;}
4197 loc.beg_pos = @2.end_pos;
4198 loc.end_pos = @2.end_pos;
4201 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4203 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot2,v1,v2);$$=v3;}
4209 loc.beg_pos = @2.end_pos;
4210 loc.end_pos = @2.end_pos;
4213 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4215 {VALUE v1,v2,v3;v1=$1;v2=Qnil;v3=dispatch2(dot3,v1,v2);$$=v3;}
4220 | tBDOT2 p_primitive
4224 loc.beg_pos = @1.beg_pos;
4225 loc.end_pos = @1.beg_pos;
4228 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4230 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot2,v1,v2);$$=v3;}
4232 | tBDOT3 p_primitive
4236 loc.beg_pos = @1.beg_pos;
4237 loc.end_pos = @1.beg_pos;
4240 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4242 {VALUE v1,v2,v3;v1=Qnil;v2=$2;v3=dispatch2(dot3,v1,v2);$$=v3;}
4246p_primitive : literal
4257 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4259 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4263 token_info_push(p, "->", &@1);
4269 nd_set_first_loc($$, @1.beg_pos);
4274p_variable : tIDENTIFIER
4277 error_duplicate_pattern_variable(p, $1, &@1);
4278 $$ = assignable(p, $1, 0, &@$);
4280 $$=assignable(p, var_field(p, $1));
4284p_var_ref : '^' tIDENTIFIER
4287 NODE *n = gettable(p, $2, &@$);
4288 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4289 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4293 {VALUE v1,v2;v1=$2;v2=dispatch1(var_ref,v1);$$=v2;}
4297p_const : tCOLON3 cname
4300 $$ = NEW_COLON3($2, &@$);
4302 {VALUE v1,v2;v1=$2;v2=dispatch1(top_const_ref,v1);$$=v2;}
4304 | p_const tCOLON2 cname
4307 $$ = NEW_COLON2($1, $3, &@$);
4309 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(const_path_ref,v1,v2);$$=v3;}
4314 $$ = gettable(p, $1, &@$);
4316 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4320opt_rescue : k_rescue exc_list exc_var then
4325 $$ = NEW_RESBODY($2,
4326 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4328 fixpos($$, $2?$2:$5);
4330 {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;}
4338 $$ = NEW_LIST($1, &@$);
4340 $$=rb_ary_new3(1, get_value($1));
4345 if (!($$ = splat_array($1))) $$ = $1;
4359opt_ensure : k_ensure compstmt
4364 {VALUE v1,v2;v1=$2;v2=dispatch1(ensure,v1);$$=v2;}
4378 node = NEW_STR(STR_NEW0(), &@$);
4379 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4382 node = evstr2dstr(p, node);
4395 $$ = literal_concat(p, $1, $2, &@$);
4397 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_concat,v1,v2);$$=v3;}
4401string1 : tSTRING_BEG string_contents tSTRING_END
4404 $$ = heredoc_dedent(p, $2);
4405 if ($$) nd_set_loc($$, &@$);
4407 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(string_literal,v1);$$=v2;}
4411xstring : tXSTRING_BEG xstring_contents tSTRING_END
4414 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4416 {VALUE v1,v2;v1=heredoc_dedent(p, $2);v2=dispatch1(xstring_literal,v1);$$=v2;}
4420regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4422 $$ = new_regexp(p, $2, $3, &@$);
4426words : tWORDS_BEG ' ' word_list tSTRING_END
4429 $$ = make_list($3, &@$);
4431 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4435word_list : /* none */
4440 {VALUE v1;v1=dispatch0(words_new);$$=v1;}
4442 | word_list word ' '
4445 $$ = list_append(p, $1, evstr2dstr(p, $2));
4447 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(words_add,v1,v2);$$=v3;}
4451word : string_content
4452 {{VALUE v1,v2,v3,v4;v1=dispatch0(word_new);v2=v1;v3=$1;v4=dispatch2(word_add,v2,v3);$$=v4;}}
4453 | word string_content
4456 $$ = literal_concat(p, $1, $2, &@$);
4458 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(word_add,v1,v2);$$=v3;}
4462symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4465 $$ = make_list($3, &@$);
4467 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4471symbol_list : /* none */
4476 {VALUE v1;v1=dispatch0(symbols_new);$$=v1;}
4478 | symbol_list word ' '
4481 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4483 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(symbols_add,v1,v2);$$=v3;}
4487qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4490 $$ = make_list($3, &@$);
4492 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4496qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4499 $$ = make_list($3, &@$);
4501 {VALUE v1,v2;v1=$3;v2=dispatch1(array,v1);$$=v2;}
4505qword_list : /* none */
4510 {VALUE v1;v1=dispatch0(qwords_new);$$=v1;}
4512 | qword_list tSTRING_CONTENT ' '
4515 $$ = list_append(p, $1, $2);
4517 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qwords_add,v1,v2);$$=v3;}
4521qsym_list : /* none */
4526 {VALUE v1;v1=dispatch0(qsymbols_new);$$=v1;}
4528 | qsym_list tSTRING_CONTENT ' '
4531 $$ = symbol_append(p, $1, $2);
4533 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(qsymbols_add,v1,v2);$$=v3;}
4537string_contents : /* none */
4542 {VALUE v1;v1=dispatch0(string_content);$$=v1;}
4545 $$ = ripper_new_yylval(p, 0, $$, 0);
4548 | string_contents string_content
4551 $$ = literal_concat(p, $1, $2, &@$);
4553 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(string_add,v1,v2);$$=v3;}
4556 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4557 !RNODE($1)->nd_cval) {
4558 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4559 RNODE($1)->nd_rval = add_mark_object(p, $$);
4566xstring_contents: /* none */
4571 {VALUE v1;v1=dispatch0(xstring_new);$$=v1;}
4573 | xstring_contents string_content
4576 $$ = literal_concat(p, $1, $2, &@$);
4578 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(xstring_add,v1,v2);$$=v3;}
4582regexp_contents: /* none */
4587 {VALUE v1;v1=dispatch0(regexp_new);$$=v1;}
4590 $$ = ripper_new_yylval(p, 0, $$, 0);
4593 | regexp_contents string_content
4596 NODE *head = $1, *tail = $2;
4604 switch (nd_type(head)) {
4606 nd_set_type(head, NODE_DSTR);
4611 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4614 $$ = list_append(p, head, tail);
4617 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4618 if (ripper_is_node_yylval(n1)) {
4619 s1 = RNODE(n1)->nd_cval;
4620 n1 = RNODE(n1)->nd_rval;
4622 if (ripper_is_node_yylval(n2)) {
4623 s2 = RNODE(n2)->nd_cval;
4624 n2 = RNODE(n2)->nd_rval;
4626 $$ = dispatch2(regexp_add, n1, n2);
4628 $$ = ripper_new_yylval(p, 0, $$, s2);
4634string_content : tSTRING_CONTENT
4635 {$$=ripper_new_yylval(p, 0, get_value($1), $1);}
4638 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4639 $<strterm>$ = p->lex.strterm;
4641 SET_LEX_STATE(EXPR_BEG);
4645 p->lex.strterm = $<strterm>2;
4647 $$ = NEW_EVSTR($3, &@$);
4648 nd_set_line($$, @3.end_pos.lineno);
4650 {VALUE v1,v2;v1=$3;v2=dispatch1(string_dvar,v1);$$=v2;}
4658 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4659 $<strterm>$ = p->lex.strterm;
4663 $<num>$ = p->lex.state;
4664 SET_LEX_STATE(EXPR_BEG);
4667 $<num>$ = p->lex.brace_nest;
4668 p->lex.brace_nest = 0;
4671 $<num>$ = p->heredoc_indent;
4672 p->heredoc_indent = 0;
4674 compstmt tSTRING_DEND
4678 p->lex.strterm = $<strterm>3;
4679 SET_LEX_STATE($<num>4);
4680 p->lex.brace_nest = $<num>5;
4681 p->heredoc_indent = $<num>6;
4682 p->heredoc_line_indent = -1;
4684 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4685 $$ = new_evstr(p, $7, &@$);
4687 {VALUE v1,v2;v1=$7;v2=dispatch1(string_embexpr,v1);$$=v2;}
4694 $$ = NEW_GVAR($1, &@$);
4696 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4701 $$ = NEW_IVAR($1, &@$);
4703 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4708 $$ = NEW_CVAR($1, &@$);
4710 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4721 SET_LEX_STATE(EXPR_END);
4723 $$ = NEW_LIT(ID2SYM($2), &@$);
4725 {VALUE v1,v2,v3,v4;v1=$2;v2=dispatch1(symbol,v1);v3=v2;v4=dispatch1(symbol_literal,v3);$$=v4;}
4735dsym : tSYMBEG string_contents tSTRING_END
4737 SET_LEX_STATE(EXPR_END);
4739 $$ = dsym_node(p, $2, &@$);
4741 {VALUE v1,v2;v1=$2;v2=dispatch1(dyna_symbol,v1);$$=v2;}
4745numeric : simple_numeric
4746 | tUMINUS_NUM simple_numeric %prec tLOWEST
4750 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4752 {VALUE v1,v2,v3;v1=ID2VAL(idUMinus);v2=$2;v3=dispatch2(unary,v1,v2);$$=v3;}
4756simple_numeric : tINTEGER
4762user_variable : tIDENTIFIER
4769keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4770 | keyword_self {$$ = KWD2EID(self, $1);}
4771 | keyword_true {$$ = KWD2EID(true, $1);}
4772 | keyword_false {$$ = KWD2EID(false, $1);}
4773 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4774 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4775 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4778var_ref : user_variable
4781 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4783 if (id_is_var(p, get_id($1))) {
4784 $$ = dispatch1(var_ref, $1);
4787 $$ = dispatch1(vcall, $1);
4794 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4796 {VALUE v1,v2;v1=$1;v2=dispatch1(var_ref,v1);$$=v2;}
4800var_lhs : user_variable
4803 $$ = assignable(p, $1, 0, &@$);
4805 $$=assignable(p, var_field(p, $1));
4810 $$ = assignable(p, $1, 0, &@$);
4812 $$=assignable(p, var_field(p, $1));
4822 SET_LEX_STATE(EXPR_BEG);
4823 p->command_start = TRUE;
4838f_arglist : '(' f_args rparen
4843 {VALUE v1,v2;v1=$2;v2=dispatch1(paren,v1);$$=v2;}
4844 SET_LEX_STATE(EXPR_BEG);
4845 p->command_start = TRUE;
4847 | '(' f_arg ',' args_forward rparen
4849 arg_var(p, idFWD_REST);
4851 arg_var(p, idFWD_KWREST);
4853 arg_var(p, idFWD_BLOCK);
4855 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@4);
4856 $$ = new_args(p, $2, Qnone, idFWD_REST, Qnone, $$, &@4);
4858 {VALUE v1,v2;v1=params_new($2, Qnone, $4, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4859 SET_LEX_STATE(EXPR_BEG);
4860 p->command_start = TRUE;
4862 | '(' args_forward rparen
4864 arg_var(p, idFWD_REST);
4866 arg_var(p, idFWD_KWREST);
4868 arg_var(p, idFWD_BLOCK);
4870 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4871 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4873 {VALUE v1,v2;v1=params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone);v2=dispatch1(paren,v1);$$=v2;}
4874 SET_LEX_STATE(EXPR_BEG);
4875 p->command_start = TRUE;
4878 $<num>$ = p->in_kwarg;
4880 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4884 p->in_kwarg = !!$<num>1;
4886 SET_LEX_STATE(EXPR_BEG);
4887 p->command_start = TRUE;
4891args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4893 $$ = new_args_tail(p, $1, $3, $4, &@3);
4895 | f_kwarg opt_f_block_arg
4897 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4899 | f_kwrest opt_f_block_arg
4901 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4903 | f_no_kwarg opt_f_block_arg
4905 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4909 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4913opt_args_tail : ',' args_tail
4919 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4923f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4925 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4927 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4929 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4931 | f_arg ',' f_optarg opt_args_tail
4933 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4935 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4937 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4939 | f_arg ',' f_rest_arg opt_args_tail
4941 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4943 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4945 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4947 | f_arg opt_args_tail
4949 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4951 | f_optarg ',' f_rest_arg opt_args_tail
4953 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4955 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4957 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4959 | f_optarg opt_args_tail
4961 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4963 | f_optarg ',' f_arg opt_args_tail
4965 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4967 | f_rest_arg opt_args_tail
4969 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4971 | f_rest_arg ',' f_arg opt_args_tail
4973 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4977 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4981 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4982 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4986args_forward : tBDOT3
4991 {VALUE v1;v1=dispatch0(args_forward);$$=v1;}
4995f_bad_arg : tCONSTANT
4998 yyerror1(&@1, "formal argument cannot be a constant");
5001 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5006 yyerror1(&@1, "formal argument cannot be an instance variable");
5009 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5014 yyerror1(&@1, "formal argument cannot be a global variable");
5017 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5022 yyerror1(&@1, "formal argument cannot be a class variable");
5025 {VALUE v1,v2;v1=$1;v2=dispatch1(param_error,v1);$$=v2;}ripper_error(p);
5029f_norm_arg : f_bad_arg
5032 formal_argument(p, get_id($1));
5033 p->max_numparam = ORDINAL_PARAM;
5038f_arg_asgn : f_norm_arg
5047f_arg_item : f_arg_asgn
5051 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5055 | tLPAREN f_margs rparen
5058 ID tid = internal_id(p);
5060 loc.beg_pos = @2.beg_pos;
5061 loc.end_pos = @2.beg_pos;
5063 if (dyna_in_block(p)) {
5064 $2->nd_value = NEW_DVAR(tid, &loc);
5067 $2->nd_value = NEW_LVAR(tid, &loc);
5069 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5072 {VALUE v1,v2;v1=$2;v2=dispatch1(mlhs_paren,v1);$$=v2;}
5077 {$$=rb_ary_new3(1, get_value($1));}
5078 | f_arg ',' f_arg_item
5083 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5084 rb_discard_node(p, $3);
5086 $$=rb_ary_push($1, get_value($3));
5094 arg_var(p, formal_argument(p, id));
5096 p->max_numparam = ORDINAL_PARAM;
5101f_kw : f_label arg_value
5105 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5107 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5113 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5115 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5119f_block_kw : f_label primary_value
5122 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5124 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($2));
5129 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5131 $$=rb_assoc_new(get_value(assignable(p, $1)), 0);
5135f_block_kwarg : f_block_kw
5140 $$=rb_ary_new3(1, get_value($1));
5142 | f_block_kwarg ',' f_block_kw
5145 $$ = kwd_append($1, $3);
5147 $$=rb_ary_push($1, get_value($3));
5157 $$=rb_ary_new3(1, get_value($1));
5162 $$ = kwd_append($1, $3);
5164 $$=rb_ary_push($1, get_value($3));
5172f_no_kwarg : kwrest_mark keyword_nil
5176 {VALUE v1,v2;v1=Qnil;v2=dispatch1(nokw_param,v1);$$=v2;}
5180f_kwrest : kwrest_mark tIDENTIFIER
5182 arg_var(p, shadowing_lvar(p, get_id($2)));
5186 {VALUE v1,v2;v1=$2;v2=dispatch1(kwrest_param,v1);$$=v2;}
5191 $$ = internal_id(p);
5194 {VALUE v1,v2;v1=Qnil;v2=dispatch1(kwrest_param,v1);$$=v2;}
5198f_opt : f_arg_asgn '=' arg_value
5202 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5204 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5208f_block_opt : f_arg_asgn '=' primary_value
5212 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5214 $$=rb_assoc_new(get_value(assignable(p, $1)), get_value($3));
5218f_block_optarg : f_block_opt
5223 $$=rb_ary_new3(1, get_value($1));
5225 | f_block_optarg ',' f_block_opt
5228 $$ = opt_arg_append($1, $3);
5230 $$=rb_ary_push($1, get_value($3));
5239 $$=rb_ary_new3(1, get_value($1));
5241 | f_optarg ',' f_opt
5244 $$ = opt_arg_append($1, $3);
5246 $$=rb_ary_push($1, get_value($3));
5254f_rest_arg : restarg_mark tIDENTIFIER
5256 arg_var(p, shadowing_lvar(p, get_id($2)));
5260 {VALUE v1,v2;v1=$2;v2=dispatch1(rest_param,v1);$$=v2;}
5265 $$ = internal_id(p);
5268 {VALUE v1,v2;v1=Qnil;v2=dispatch1(rest_param,v1);$$=v2;}
5276f_block_arg : blkarg_mark tIDENTIFIER
5278 arg_var(p, shadowing_lvar(p, get_id($2)));
5282 {VALUE v1,v2;v1=$2;v2=dispatch1(blockarg,v1);$$=v2;}
5286opt_f_block_arg : ',' f_block_arg
5301 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5304 switch (nd_type($3)) {
5313 yyerror1(&@3, "can't define singleton method for literals");
5321 {VALUE v1,v2;v1=$3;v2=dispatch1(paren,v1);$$=v2;}
5331 {VALUE v1,v2;v1=$1;v2=dispatch1(assoclist_from_args,v1);$$=v2;}
5336 {$$=rb_ary_new3(1, get_value($1));}
5346 if (assocs->nd_head &&
5347 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5348 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5350 tail = tail->nd_next->nd_head->nd_head;
5352 assocs = list_concat(assocs, tail);
5356 $$=rb_ary_push($1, get_value($3));
5360assoc : arg_value tASSOC arg_value
5363 if (nd_type($1) == NODE_STR) {
5364 nd_set_type($1, NODE_LIT);
5365 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5367 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5369 {VALUE v1,v2,v3;v1=$1;v2=$3;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5374 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5376 {VALUE v1,v2,v3;v1=$1;v2=$2;v3=dispatch2(assoc_new,v1,v2);$$=v3;}
5378 | tSTRING_BEG string_contents tLABEL_END arg_value
5381 YYLTYPE loc = code_loc_gen(&@1, &@3);
5382 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5384 {VALUE v1,v2,v3,v4,v5;v1=$2;v2=dispatch1(dyna_symbol,v1);v3=v2;v4=$4;v5=dispatch2(assoc_new,v3,v4);$$=v5;}
5389 if (nd_type($2) == NODE_HASH &&
5390 !($2->nd_head && $2->nd_head->nd_alen)) {
5391 static VALUE empty_hash;
5393 empty_hash = rb_obj_freeze(rb_hash_new());
5394 rb_gc_register_mark_object(empty_hash);
5396 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5399 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5401 {VALUE v1,v2;v1=$2;v2=dispatch1(assoc_splat,v1);$$=v2;}
5405operation : tIDENTIFIER
5410operation2 : tIDENTIFIER
5416operation3 : tIDENTIFIER
5433opt_terms : /* none */
5444rbracket : opt_nl ']'
5455term : ';' {yyerrok;token_flush(p);}
5456 | '\n' {token_flush(p);}
5460 | terms ';' {yyerrok;}
5472# define yylval (*p->lval)
5474static int regx_options(struct parser_params*);
5475static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5476static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5477static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5478static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5481# define set_yylval_node(x) { \
5483 rb_parser_set_location(p, &_cur_loc); \
5484 yylval.node = (x); \
5486# define set_yylval_str(x) \
5488 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5489 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5491# define set_yylval_literal(x) \
5493 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5494 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5496# define set_yylval_num(x) (yylval.num = (x))
5497# define set_yylval_id(x) (yylval.id = (x))
5498# define set_yylval_name(x) (yylval.id = (x))
5499# define yylval_id() (yylval.id)
5502ripper_yylval_id(struct parser_params *p, ID x)
5504 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5506# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5507# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5508# define set_yylval_id(x) (void)(x)
5509# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5510# define set_yylval_literal(x) add_mark_object(p, (x))
5511# define set_yylval_node(x) (void)(x)
5512# define yylval_id() yylval.id
5513# define _cur_loc NULL_LOC /* dummy */
5516#define set_yylval_noname() set_yylval_id(keyword_nil)
5519#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5520#define dispatch_scan_event(p, t) ((void)0)
5521#define dispatch_delayed_token(p, t) ((void)0)
5522#define has_delayed_token(p) (0)
5524#define literal_flush(p, ptr) ((void)(ptr))
5526#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5529intern_sym(const char *name)
5531 ID id = rb_intern_const(name);
5536ripper_has_scan_event(struct parser_params *p)
5538 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5539 return p->lex.pcur > p->lex.ptok;
5543ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5545 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5546 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5552ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5554 if (!ripper_has_scan_event(p)) return;
5555 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5557#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5560ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5562 int saved_line = p->ruby_sourceline;
5563 const char *saved_tokp = p->lex.ptok;
5565 if (NIL_P(p->delayed.token)) return;
5566 p->ruby_sourceline = p->delayed.line;
5567 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5568 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5569 p->delayed.token = Qnil;
5570 p->ruby_sourceline = saved_line;
5571 p->lex.ptok = saved_tokp;
5573#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5574#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5577#include "ruby/regex.h"
5578#include "ruby/util.h"
5581is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5583 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5587parser_is_identchar(struct parser_params *p)
5589 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5593parser_isascii(struct parser_params *p)
5595 return ISASCII(*(p->lex.pcur-1));
5599token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5601 int column = 1, nonspc = 0, i;
5602 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5604 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5607 if (*ptr != ' ' && *ptr != '\t') {
5612 ptinfo->beg = loc->beg_pos;
5613 ptinfo->indent = column;
5614 ptinfo->nonspc = nonspc;
5618token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5622 if (!p->token_info_enabled) return;
5623 ptinfo = ALLOC(token_info);
5624 ptinfo->token = token;
5625 ptinfo->next = p->token_info;
5626 token_info_setup(ptinfo, p->lex.pbeg, loc);
5628 p->token_info = ptinfo;
5632token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5634 token_info *ptinfo_beg = p->token_info;
5636 if (!ptinfo_beg) return;
5637 p->token_info = ptinfo_beg->next;
5639 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5640 token_info_warn(p, token, ptinfo_beg, 1, loc);
5641 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5645token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5647 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5648 if (!p->token_info_enabled) return;
5649 if (!ptinfo_beg) return;
5650 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5651 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5652 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5653 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5654 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5655 rb_warn3L(ptinfo_end->beg.lineno,
5656 "mismatched indentations at '%s' with '%s' at %d",
5657 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5661parser_precise_mbclen(struct parser_params *p, const char *ptr)
5663 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5664 if (!MBCLEN_CHARFOUND_P(len)) {
5665 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5672static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5675parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5678 int lineno = p->ruby_sourceline;
5682 else if (yylloc->beg_pos.lineno == lineno) {
5683 str = p->lex.lastline;
5688 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5692parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5697 yylloc = RUBY_SET_YYLLOC(current);
5699 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5700 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5701 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5702 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5705 compile_error(p, "%s", msg);
5706 parser_show_error_line(p, yylloc);
5711ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5714 const int max_line_margin = 30;
5715 const char *ptr, *ptr_end, *pt, *pb;
5716 const char *pre = "", *post = "", *pend;
5717 const char *code = "", *caret = "";
5719 const char *const pbeg = RSTRING_PTR(str);
5724 if (!yylloc) return;
5725 pend = RSTRING_END(str);
5726 if (pend > pbeg && pend[-1] == '\n') {
5727 if (--pend > pbeg && pend[-1] == '\r') --pend;
5731 if (lineno == yylloc->end_pos.lineno &&
5732 (pend - pbeg) > yylloc->end_pos.column) {
5733 pt = pbeg + yylloc->end_pos.column;
5737 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5738 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5740 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5741 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5743 len = ptr_end - ptr;
5746 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5747 if (ptr > pbeg) pre = "...";
5749 if (ptr_end < pend) {
5750 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5751 if (ptr_end < pend) post = "...";
5755 if (lineno == yylloc->beg_pos.lineno) {
5756 pb += yylloc->beg_pos.column;
5757 if (pb > pt) pb = pt;
5759 if (pb < ptr) pb = ptr;
5760 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5763 if (RTEST(errbuf)) {
5764 mesg = rb_attr_get(errbuf, idMesg);
5765 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5766 rb_str_cat_cstr(mesg, "\n");
5769 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5771 if (!errbuf && rb_stderr_tty_p()) {
5772#define CSI_BEGIN "\033["
5775 CSI_BEGIN""CSI_SGR"%s" /* pre */
5776 CSI_BEGIN"1"CSI_SGR"%.*s"
5777 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5778 CSI_BEGIN";1"CSI_SGR"%.*s"
5779 CSI_BEGIN""CSI_SGR"%s" /* post */
5782 (int)(pb - ptr), ptr,
5784 (int)(ptr_end - pt), pt,
5790 len = ptr_end - ptr;
5791 lim = pt < pend ? pt : pend;
5792 i = (int)(lim - ptr);
5793 buf = ALLOCA_N(char, i+2);
5798 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5804 memset(p2, '~', (lim - ptr));
5808 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5809 pre, (int)len, code, post,
5812 if (!errbuf) rb_write_error_str(mesg);
5816parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5818 const char *pcur = 0, *ptok = 0;
5820 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5821 p->ruby_sourceline == yylloc->end_pos.lineno) {
5824 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5825 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5827 dispatch1(parse_error, STR_NEW2(msg));
5837parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5844vtable_size(const struct vtable *tbl)
5846 if (!DVARS_TERMINAL_P(tbl)) {
5855static struct vtable *
5856vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5858 struct vtable *tbl = ALLOC(struct vtable);
5861 tbl->tbl = ALLOC_N(ID, tbl->capa);
5865 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5870#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5873vtable_free_gen(struct parser_params *p, int line, const char *name,
5878 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5881 if (!DVARS_TERMINAL_P(tbl)) {
5883 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5885 ruby_sized_xfree(tbl, sizeof(tbl));
5888#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5891vtable_add_gen(struct parser_params *p, int line, const char *name,
5892 struct vtable *tbl, ID id)
5896 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5897 line, name, (void *)tbl, rb_id2name(id));
5900 if (DVARS_TERMINAL_P(tbl)) {
5901 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5904 if (tbl->pos == tbl->capa) {
5905 tbl->capa = tbl->capa * 2;
5906 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5908 tbl->tbl[tbl->pos++] = id;
5910#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5914vtable_pop_gen(struct parser_params *p, int line, const char *name,
5915 struct vtable *tbl, int n)
5918 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5919 line, name, (void *)tbl, n);
5922 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5927#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5931vtable_included(const struct vtable * tbl, ID id)
5935 if (!DVARS_TERMINAL_P(tbl)) {
5936 for (i = 0; i < tbl->pos; i++) {
5937 if (tbl->tbl[i] == id) {
5945static void parser_prepare(struct parser_params *p);
5948static NODE *parser_append_options(struct parser_params *p, NODE *node);
5951debug_lines(VALUE fname)
5954 CONST_ID(script_lines, "SCRIPT_LINES__");
5955 if (rb_const_defined_at(rb_cObject, script_lines)) {
5956 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5957 if (RB_TYPE_P(hash, T_HASH)) {
5958 VALUE lines = rb_ary_new();
5959 rb_hash_aset(hash, fname, lines);
5967e_option_supplied(struct parser_params *p)
5969 return strcmp(p->ruby_sourcefile, "-e") == 0;
5973yycompile0(VALUE arg)
5977 struct parser_params *p = (struct parser_params *)arg;
5980 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5981 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5982 if (p->debug_lines && p->ruby_sourceline > 0) {
5983 VALUE str = STR_NEW0();
5984 n = p->ruby_sourceline;
5986 rb_ary_push(p->debug_lines, str);
5990 if (!e_option_supplied(p)) {
5996#define RUBY_DTRACE_PARSE_HOOK(name) \
5997 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5998 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6000 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6002 RUBY_DTRACE_PARSE_HOOK(END);
6006 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6007 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6008 if (n || p->error_p) {
6009 VALUE mesg = p->error_buffer;
6011 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6013 rb_set_errinfo(mesg);
6016 tree = p->eval_tree;
6018 tree = NEW_NIL(&NULL_LOC);
6021 VALUE opt = p->compile_option;
6023 NODE *body = parser_append_options(p, tree->nd_body);
6024 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6025 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6026 prelude = block_append(p, p->eval_tree_begin, body);
6027 tree->nd_body = prelude;
6028 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6030 p->ast->body.root = tree;
6031 p->ast->body.line_count = p->line_count;
6036yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6040 p->ruby_sourcefile_string = Qnil;
6041 p->ruby_sourcefile = "(none)";
6044 p->ruby_sourcefile_string = rb_fstring(fname);
6045 p->ruby_sourcefile = StringValueCStr(fname);
6047 p->ruby_sourceline = line - 1;
6049 p->ast = ast = rb_ast_new();
6050 rb_suppress_tracing(yycompile0, (VALUE)p);
6052 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6059must_be_ascii_compatible(VALUE s)
6061 rb_encoding *enc = rb_enc_get(s);
6062 if (!rb_enc_asciicompat(enc)) {
6063 rb_raise(rb_eArgError, "invalid source encoding");
6069lex_get_str(struct parser_params *p, VALUE s)
6071 char *beg, *end, *start;
6074 beg = RSTRING_PTR(s);
6075 len = RSTRING_LEN(s);
6077 if (p->lex.gets_.ptr) {
6078 if (len == p->lex.gets_.ptr) return Qnil;
6079 beg += p->lex.gets_.ptr;
6080 len -= p->lex.gets_.ptr;
6082 end = memchr(beg, '\n', len);
6083 if (end) len = ++end - beg;
6084 p->lex.gets_.ptr += len;
6085 return rb_str_subseq(s, beg - start, len);
6089lex_getline(struct parser_params *p)
6091 VALUE line = (*p->lex.gets)(p, p->lex.input);
6092 if (NIL_P(line)) return line;
6093 must_be_ascii_compatible(line);
6095 if (p->debug_lines) {
6096 rb_enc_associate(line, p->enc);
6097 rb_ary_push(p->debug_lines, line);
6104static const rb_data_type_t parser_data_type;
6108parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6110 struct parser_params *p;
6112 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6114 p->lex.gets = lex_get_str;
6115 p->lex.gets_.ptr = 0;
6116 p->lex.input = rb_str_new_frozen(s);
6117 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6119 return yycompile(vparser, p, fname, line);
6123rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6125 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6129rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6131 must_be_ascii_compatible(s);
6132 return parser_compile_string(vparser, f, s, line);
6135VALUE rb_io_gets_internal(VALUE io);
6138lex_io_gets(struct parser_params *p, VALUE io)
6140 return rb_io_gets_internal(io);
6144rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6146 struct parser_params *p;
6148 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6150 p->lex.gets = lex_io_gets;
6151 p->lex.input = file;
6152 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6154 return yycompile(vparser, p, fname, start);
6158lex_generic_gets(struct parser_params *p, VALUE input)
6160 return (*p->lex.gets_.call)(input, p->line_count);
6164rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6166 struct parser_params *p;
6168 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6170 p->lex.gets = lex_generic_gets;
6171 p->lex.gets_.call = lex_gets;
6172 p->lex.input = input;
6173 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6175 return yycompile(vparser, p, fname, start);
6179#define STR_FUNC_ESCAPE 0x01
6180#define STR_FUNC_EXPAND 0x02
6181#define STR_FUNC_REGEXP 0x04
6182#define STR_FUNC_QWORDS 0x08
6183#define STR_FUNC_SYMBOL 0x10
6184#define STR_FUNC_INDENT 0x20
6185#define STR_FUNC_LABEL 0x40
6186#define STR_FUNC_LIST 0x4000
6187#define STR_FUNC_TERM 0x8000
6190 str_label = STR_FUNC_LABEL,
6192 str_dquote = (STR_FUNC_EXPAND),
6193 str_xquote = (STR_FUNC_EXPAND),
6194 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6195 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6196 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6197 str_ssym = (STR_FUNC_SYMBOL),
6198 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6202parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6206 str = rb_enc_str_new(ptr, len, enc);
6207 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6208 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6210 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6211 rb_enc_associate(str, rb_ascii8bit_encoding());
6218#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6219#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6220#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6221#define peek(p,c) peek_n(p, (c), 0)
6222#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6223#define peekc(p) peekc_n(p, 0)
6224#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6228add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6231 if (!has_delayed_token(p)) {
6232 p->delayed.token = rb_str_buf_new(end - tok);
6233 rb_enc_associate(p->delayed.token, p->enc);
6234 p->delayed.line = p->ruby_sourceline;
6235 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6237 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6242#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6246nextline(struct parser_params *p)
6248 VALUE v = p->lex.nextline;
6249 p->lex.nextline = 0;
6254 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6258 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6266 else if (NIL_P(v)) {
6267 /* after here-document without terminator */
6270 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6271 if (p->heredoc_end > 0) {
6272 p->ruby_sourceline = p->heredoc_end;
6275 p->ruby_sourceline++;
6276 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6277 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6279 p->lex.prevline = p->lex.lastline;
6280 p->lex.lastline = v;
6285parser_cr(struct parser_params *p, int c)
6287 if (peek(p, '\n')) {
6291 else if (!p->cr_seen) {
6293 /* carried over with p->lex.nextline for nextc() */
6294 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6300nextc(struct parser_params *p)
6304 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6305 if (nextline(p)) return -1;
6307 c = (unsigned char)*p->lex.pcur++;
6308 if (UNLIKELY(c == '\r')) {
6309 c = parser_cr(p, c);
6316pushback(struct parser_params *p, int c)
6318 if (c == -1) return;
6320 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6325#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6327#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6328#define tok(p) (p)->tokenbuf
6329#define toklen(p) (p)->tokidx
6332looking_at_eol_p(struct parser_params *p)
6334 const char *ptr = p->lex.pcur;
6335 while (ptr < p->lex.pend) {
6336 int c = (unsigned char)*ptr++;
6337 int eol = (c == '\n' || c == '#');
6338 if (eol || !ISSPACE(c)) {
6346newtok(struct parser_params *p)
6349 p->tokline = p->ruby_sourceline;
6352 p->tokenbuf = ALLOC_N(char, 60);
6354 if (p->toksiz > 4096) {
6356 REALLOC_N(p->tokenbuf, char, 60);
6362tokspace(struct parser_params *p, int n)
6366 if (p->tokidx >= p->toksiz) {
6367 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6368 REALLOC_N(p->tokenbuf, char, p->toksiz);
6370 return &p->tokenbuf[p->tokidx-n];
6374tokadd(struct parser_params *p, int c)
6376 p->tokenbuf[p->tokidx++] = (char)c;
6377 if (p->tokidx >= p->toksiz) {
6379 REALLOC_N(p->tokenbuf, char, p->toksiz);
6384tok_hex(struct parser_params *p, size_t *numlen)
6388 c = scan_hex(p->lex.pcur, 2, numlen);
6390 yyerror0("invalid hex escape");
6394 p->lex.pcur += *numlen;
6398#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6401escaped_control_code(int c)
6427#define WARN_SPACE_CHAR(c, prefix) \
6428 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6431tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6432 int regexp_literal, int wide)
6435 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6436 literal_flush(p, p->lex.pcur);
6437 p->lex.pcur += numlen;
6438 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6439 yyerror0("invalid Unicode escape");
6440 return wide && numlen > 0;
6442 if (codepoint > 0x10ffff) {
6443 yyerror0("invalid Unicode codepoint (too large)");
6446 if ((codepoint & 0xfffff800) == 0xd800) {
6447 yyerror0("invalid Unicode codepoint");
6450 if (regexp_literal) {
6451 tokcopy(p, (int)numlen);
6453 else if (codepoint >= 0x80) {
6454 rb_encoding *utf8 = rb_utf8_encoding();
6455 if (*encp && utf8 != *encp) {
6456 YYLTYPE loc = RUBY_INIT_YYLLOC();
6457 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6458 parser_show_error_line(p, &loc);
6462 tokaddmbc(p, codepoint, *encp);
6465 tokadd(p, codepoint);
6470/* return value is for ?\u3042 */
6472tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6473 int term, int symbol_literal, int regexp_literal)
6476 * If `term` is not -1, then we allow multiple codepoints in \u{}
6477 * upto `term` byte, otherwise we're parsing a character literal.
6478 * And then add the codepoints to the current token.
6480 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6482 const int open_brace = '{', close_brace = '}';
6484 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6486 if (peek(p, open_brace)) { /* handle \u{...} form */
6487 const char *second = NULL;
6488 int c, last = nextc(p);
6489 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6490 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6491 while (c != close_brace) {
6492 if (c == term) goto unterminated;
6493 if (second == multiple_codepoints)
6494 second = p->lex.pcur;
6495 if (regexp_literal) tokadd(p, last);
6496 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6499 while (ISSPACE(c = *p->lex.pcur)) {
6500 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6503 if (term == -1 && !second)
6504 second = multiple_codepoints;
6507 if (c != close_brace) {
6510 yyerror0("unterminated Unicode escape");
6513 if (second && second != multiple_codepoints) {
6514 const char *pcur = p->lex.pcur;
6515 p->lex.pcur = second;
6516 dispatch_scan_event(p, tSTRING_CONTENT);
6519 yyerror0(multiple_codepoints);
6523 if (regexp_literal) tokadd(p, close_brace);
6526 else { /* handle \uxxxx form */
6527 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6534#define ESCAPE_CONTROL 1
6535#define ESCAPE_META 2
6538read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6543 switch (c = nextc(p)) {
6544 case '\\': /* Backslash */
6547 case 'n': /* newline */
6550 case 't': /* horizontal tab */
6553 case 'r': /* carriage-return */
6556 case 'f': /* form-feed */
6559 case 'v': /* vertical tab */
6562 case 'a': /* alarm(bell) */
6565 case 'e': /* escape */
6568 case '0': case '1': case '2': case '3': /* octal constant */
6569 case '4': case '5': case '6': case '7':
6571 c = scan_oct(p->lex.pcur, 3, &numlen);
6572 p->lex.pcur += numlen;
6575 case 'x': /* hex constant */
6576 c = tok_hex(p, &numlen);
6577 if (numlen == 0) return 0;
6580 case 'b': /* backspace */
6583 case 's': /* space */
6587 if (flags & ESCAPE_META) goto eof;
6588 if ((c = nextc(p)) != '-') {
6591 if ((c = nextc(p)) == '\\') {
6592 if (peek(p, 'u')) goto eof;
6593 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6595 else if (c == -1 || !ISASCII(c)) goto eof;
6597 int c2 = escaped_control_code(c);
6599 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6600 WARN_SPACE_CHAR(c2, "\\M-");
6603 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6606 else if (ISCNTRL(c)) goto eof;
6607 return ((c & 0xff) | 0x80);
6611 if ((c = nextc(p)) != '-') {
6615 if (flags & ESCAPE_CONTROL) goto eof;
6616 if ((c = nextc(p))== '\\') {
6617 if (peek(p, 'u')) goto eof;
6618 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6622 else if (c == -1 || !ISASCII(c)) goto eof;
6624 int c2 = escaped_control_code(c);
6627 if (flags & ESCAPE_META) {
6628 WARN_SPACE_CHAR(c2, "\\M-");
6631 WARN_SPACE_CHAR(c2, "");
6635 if (flags & ESCAPE_META) {
6636 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6639 WARN_SPACE_CHAR(c2, "\\C-");
6643 else if (ISCNTRL(c)) goto eof;
6649 yyerror0("Invalid escape character syntax");
6659tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6661 int len = rb_enc_codelen(c, enc);
6662 rb_enc_mbcput(c, tokspace(p, len), enc);
6666tokadd_escape(struct parser_params *p, rb_encoding **encp)
6673 switch (c = nextc(p)) {
6675 return 0; /* just ignore */
6677 case '0': case '1': case '2': case '3': /* octal constant */
6678 case '4': case '5': case '6': case '7':
6680 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6681 if (numlen == 0) goto eof;
6682 p->lex.pcur += numlen;
6683 tokcopy(p, (int)numlen + 1);
6687 case 'x': /* hex constant */
6689 tok_hex(p, &numlen);
6690 if (numlen == 0) return -1;
6691 tokcopy(p, (int)numlen + 2);
6696 if (flags & ESCAPE_META) goto eof;
6697 if ((c = nextc(p)) != '-') {
6702 flags |= ESCAPE_META;
6706 if (flags & ESCAPE_CONTROL) goto eof;
6707 if ((c = nextc(p)) != '-') {
6715 if (flags & ESCAPE_CONTROL) goto eof;
6717 flags |= ESCAPE_CONTROL;
6719 if ((c = nextc(p)) == '\\') {
6722 else if (c == -1) goto eof;
6728 yyerror0("Invalid escape character syntax");
6740regx_options(struct parser_params *p)
6748 while (c = nextc(p), ISALPHA(c)) {
6750 options |= RE_OPTION_ONCE;
6752 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6754 if (kc != rb_ascii8bit_encindex()) kcode = c;
6768 YYLTYPE loc = RUBY_INIT_YYLLOC();
6770 compile_error(p, "unknown regexp option%s - %*s",
6771 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6772 parser_show_error_line(p, &loc);
6774 return options | RE_OPTION_ENCODING(kcode);
6778tokadd_mbchar(struct parser_params *p, int c)
6780 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6781 if (len < 0) return -1;
6783 p->lex.pcur += --len;
6784 if (len > 0) tokcopy(p, len);
6789simple_re_meta(int c)
6792 case '$': case '*': case '+': case '.':
6793 case '?': case '^': case '|':
6794 case ')': case ']': case '}': case '>':
6802parser_update_heredoc_indent(struct parser_params *p, int c)
6804 if (p->heredoc_line_indent == -1) {
6805 if (c == '\n') p->heredoc_line_indent = 0;
6809 p->heredoc_line_indent++;
6812 else if (c == '\t') {
6813 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6814 p->heredoc_line_indent = w * TAB_WIDTH;
6817 else if (c != '\n') {
6818 if (p->heredoc_indent > p->heredoc_line_indent) {
6819 p->heredoc_indent = p->heredoc_line_indent;
6821 p->heredoc_line_indent = -1;
6828parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6830 YYLTYPE loc = RUBY_INIT_YYLLOC();
6831 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6832 compile_error(p, "%s mixed within %s source", n1, n2);
6833 parser_show_error_line(p, &loc);
6837parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6839 const char *pos = p->lex.pcur;
6841 parser_mixed_error(p, enc1, enc2);
6846tokadd_string(struct parser_params *p,
6847 int func, int term, int paren, long *nest,
6848 rb_encoding **encp, rb_encoding **enc)
6853#define mixed_error(enc1, enc2) \
6854 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6855#define mixed_escape(beg, enc1, enc2) \
6856 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6858 while ((c = nextc(p)) != -1) {
6859 if (p->heredoc_indent > 0) {
6860 parser_update_heredoc_indent(p, c);
6863 if (paren && c == paren) {
6866 else if (c == term) {
6867 if (!nest || !*nest) {
6873 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6874 int c2 = *p->lex.pcur;
6875 if (c2 == '$' || c2 == '@' || c2 == '{') {
6880 else if (c == '\\') {
6881 literal_flush(p, p->lex.pcur - 1);
6885 if (func & STR_FUNC_QWORDS) break;
6886 if (func & STR_FUNC_EXPAND) {
6887 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6898 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6902 if ((func & STR_FUNC_EXPAND) == 0) {
6906 tokadd_utf8(p, enc, term,
6907 func & STR_FUNC_SYMBOL,
6908 func & STR_FUNC_REGEXP);
6912 if (c == -1) return -1;
6914 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6917 if (func & STR_FUNC_REGEXP) {
6918 if (c == term && !simple_re_meta(c)) {
6923 if ((c = tokadd_escape(p, enc)) < 0)
6925 if (*enc && *enc != *encp) {
6926 mixed_escape(p->lex.ptok+2, *enc, *encp);
6930 else if (func & STR_FUNC_EXPAND) {
6932 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6933 c = read_escape(p, 0, enc);
6935 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6936 /* ignore backslashed spaces in %w */
6938 else if (c != term && !(paren && c == paren)) {
6945 else if (!parser_isascii(p)) {
6950 else if (*enc != *encp) {
6951 mixed_error(*enc, *encp);
6954 if (tokadd_mbchar(p, c) == -1) return -1;
6957 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6965 else if (*enc != *encp) {
6966 mixed_error(*enc, *encp);
6973 if (*enc) *encp = *enc;
6977static inline rb_strterm_t *
6978new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6980 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6983/* imemo_parser_strterm for literal */
6984#define NEW_STRTERM(func, term, paren) \
6985 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6989flush_string_content(struct parser_params *p, rb_encoding *enc)
6991 VALUE content = yylval.val;
6992 if (!ripper_is_node_yylval(content))
6993 content = ripper_new_yylval(p, 0, 0, content);
6994 if (has_delayed_token(p)) {
6995 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6997 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6999 dispatch_delayed_token(p, tSTRING_CONTENT);
7000 p->lex.ptok = p->lex.pcur;
7001 RNODE(content)->nd_rval = yylval.val;
7003 dispatch_scan_event(p, tSTRING_CONTENT);
7004 if (yylval.val != content)
7005 RNODE(content)->nd_rval = yylval.val;
7006 yylval.val = content;
7009#define flush_string_content(p, enc) ((void)(enc))
7012RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7013/* this can be shared with ripper, since it's independent from struct
7016#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7017#define SPECIAL_PUNCT(idx) ( \
7018 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7019 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7020 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7021 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7022 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7024const unsigned int ruby_global_name_punct_bits[] = {
7033static enum yytokentype
7034parser_peek_variable_name(struct parser_params *p)
7037 const char *ptr = p->lex.pcur;
7039 if (ptr + 1 >= p->lex.pend) return 0;
7043 if ((c = *ptr) == '-') {
7044 if (++ptr >= p->lex.pend) return 0;
7047 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7048 return tSTRING_DVAR;
7052 if ((c = *ptr) == '@') {
7053 if (++ptr >= p->lex.pend) return 0;
7059 p->command_start = TRUE;
7060 return tSTRING_DBEG;
7064 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7065 return tSTRING_DVAR;
7069#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7070#define IS_END() IS_lex_state(EXPR_END_ANY)
7071#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7072#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7073#define IS_LABEL_POSSIBLE() (\
7074 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7076#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7077#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7079static inline enum yytokentype
7080parser_string_term(struct parser_params *p, int func)
7083 if (func & STR_FUNC_REGEXP) {
7084 set_yylval_num(regx_options(p));
7085 dispatch_scan_event(p, tREGEXP_END);
7086 SET_LEX_STATE(EXPR_END);
7089 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7091 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7094 SET_LEX_STATE(EXPR_END);
7098static enum yytokentype
7099parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7101 int func = (int)quote->u1.func;
7102 int term = (int)quote->u3.term;
7103 int paren = (int)quote->u2.paren;
7105 rb_encoding *enc = p->enc;
7106 rb_encoding *base_enc = 0;
7109 if (func & STR_FUNC_TERM) {
7110 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7111 SET_LEX_STATE(EXPR_END);
7113 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7116 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7117 do {c = nextc(p);} while (ISSPACE(c));
7120 if (func & STR_FUNC_LIST) {
7121 quote->u1.func &= ~STR_FUNC_LIST;
7124 if (c == term && !quote->u0.nest) {
7125 if (func & STR_FUNC_QWORDS) {
7126 quote->u1.func |= STR_FUNC_TERM;
7127 pushback(p, c); /* dispatch the term at tSTRING_END */
7128 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7131 return parser_string_term(p, func);
7135 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7139 if ((func & STR_FUNC_EXPAND) && c == '#') {
7140 int t = parser_peek_variable_name(p);
7146 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7147 &enc, &base_enc) == -1) {
7150# define unterminated_literal(mesg) yyerror0(mesg)
7152# define unterminated_literal(mesg) compile_error(p, mesg)
7154 literal_flush(p, p->lex.pcur);
7155 if (func & STR_FUNC_QWORDS) {
7156 /* no content to add, bailing out here */
7157 unterminated_literal("unterminated list meets end of file");
7161 if (func & STR_FUNC_REGEXP) {
7162 unterminated_literal("unterminated regexp meets end of file");
7165 unterminated_literal("unterminated string meets end of file");
7167 quote->u1.func |= STR_FUNC_TERM;
7172 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7173 set_yylval_str(lit);
7174 flush_string_content(p, enc);
7176 return tSTRING_CONTENT;
7179static enum yytokentype
7180heredoc_identifier(struct parser_params *p)
7183 * term_len is length of `<<"END"` except `END`,
7184 * in this case term_len is 4 (<, <, " and ").
7186 long len, offset = p->lex.pcur - p->lex.pbeg;
7187 int c = nextc(p), term, func = 0, quote = 0;
7188 enum yytokentype token = tSTRING_BEG;
7193 func = STR_FUNC_INDENT;
7196 else if (c == '~') {
7198 func = STR_FUNC_INDENT;
7204 func |= str_squote; goto quoted;
7206 func |= str_dquote; goto quoted;
7208 token = tXSTRING_BEG;
7209 func |= str_xquote; goto quoted;
7216 while ((c = nextc(p)) != term) {
7217 if (c == -1 || c == '\r' || c == '\n') {
7218 yyerror(NULL, p, "unterminated here document identifier");
7225 if (!parser_is_identchar(p)) {
7227 if (func & STR_FUNC_INDENT) {
7228 pushback(p, indent > 0 ? '~' : '-');
7234 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7235 if (n < 0) return 0;
7237 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7242 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7243 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7244 yyerror(NULL, p, "too long here document identifier");
7245 dispatch_scan_event(p, tHEREDOC_BEG);
7248 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7249 p->lex.strterm->flags |= STRTERM_HEREDOC;
7250 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7251 here->offset = offset;
7252 here->sourceline = p->ruby_sourceline;
7253 here->length = (int)len;
7254 here->quote = quote;
7258 p->heredoc_indent = indent;
7259 p->heredoc_line_indent = 0;
7264heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7269 line = here->lastline;
7270 p->lex.lastline = line;
7271 p->lex.pbeg = RSTRING_PTR(line);
7272 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7273 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7274 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7275 p->heredoc_end = p->ruby_sourceline;
7276 p->ruby_sourceline = (int)here->sourceline;
7277 if (p->eofp) p->lex.nextline = Qnil;
7282dedent_string(VALUE string, int width)
7288 RSTRING_GETMEM(string, str, len);
7289 for (i = 0; i < len && col < width; i++) {
7290 if (str[i] == ' ') {
7293 else if (str[i] == '\t') {
7294 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7295 if (n > width) break;
7303 rb_str_modify(string);
7304 str = RSTRING_PTR(string);
7305 if (RSTRING_LEN(string) != len)
7306 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7307 MEMMOVE(str, str + i, char, len - i);
7308 rb_str_set_len(string, len - i);
7314heredoc_dedent(struct parser_params *p, NODE *root)
7316 NODE *node, *str_node, *prev_node;
7317 int indent = p->heredoc_indent;
7320 if (indent <= 0) return root;
7321 p->heredoc_indent = 0;
7322 if (!root) return root;
7324 prev_node = node = str_node = root;
7325 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7328 VALUE lit = str_node->nd_lit;
7329 if (str_node->flags & NODE_FL_NEWLINE) {
7330 dedent_string(lit, indent);
7335 else if (!literal_concat0(p, prev_lit, lit)) {
7339 NODE *end = node->nd_end;
7340 node = prev_node->nd_next = node->nd_next;
7342 if (nd_type(prev_node) == NODE_DSTR)
7343 nd_set_type(prev_node, NODE_STR);
7351 while ((node = (prev_node = node)->nd_next) != 0) {
7353 if (nd_type(node) != NODE_LIST) break;
7354 if ((str_node = node->nd_head) != 0) {
7355 enum node_type type = nd_type(str_node);
7356 if (type == NODE_STR || type == NODE_DSTR) break;
7366heredoc_dedent(struct parser_params *p, VALUE array)
7368 int indent = p->heredoc_indent;
7370 if (indent <= 0) return array;
7371 p->heredoc_indent = 0;
7372 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7378 * Ripper.dedent_string(input, width) -> Integer
7380 * USE OF RIPPER LIBRARY ONLY.
7382 * Strips up to +width+ leading whitespaces from +input+,
7383 * and returns the stripped column width.
7386parser_dedent_string(VALUE self, VALUE input, VALUE width)
7391 wid = NUM2UINT(width);
7392 col = dedent_string(input, wid);
7393 return INT2NUM(col);
7398whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7400 const char *ptr = p->lex.pbeg;
7404 while (*ptr && ISSPACE(*ptr)) ptr++;
7406 n = p->lex.pend - (ptr + len);
7407 if (n < 0) return FALSE;
7408 if (n > 0 && ptr[len] != '\n') {
7409 if (ptr[len] != '\r') return FALSE;
7410 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7412 return strncmp(eos, ptr, len) == 0;
7416word_match_p(struct parser_params *p, const char *word, long len)
7418 if (strncmp(p->lex.pcur, word, len)) return 0;
7419 if (p->lex.pcur + len == p->lex.pend) return 1;
7420 int c = (unsigned char)p->lex.pcur[len];
7421 if (ISSPACE(c)) return 1;
7423 case '\0': case '\004': case '\032': return 1;
7428#define NUM_SUFFIX_R (1<<0)
7429#define NUM_SUFFIX_I (1<<1)
7430#define NUM_SUFFIX_ALL 3
7433number_literal_suffix(struct parser_params *p, int mask)
7436 const char *lastp = p->lex.pcur;
7438 while ((c = nextc(p)) != -1) {
7439 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7440 result |= (mask & NUM_SUFFIX_I);
7441 mask &= ~NUM_SUFFIX_I;
7442 /* r after i, rational of complex is disallowed */
7443 mask &= ~NUM_SUFFIX_R;
7446 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7447 result |= (mask & NUM_SUFFIX_R);
7448 mask &= ~NUM_SUFFIX_R;
7451 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7452 p->lex.pcur = lastp;
7453 literal_flush(p, p->lex.pcur);
7462static enum yytokentype
7463set_number_literal(struct parser_params *p, VALUE v,
7464 enum yytokentype type, int suffix)
7466 if (suffix & NUM_SUFFIX_I) {
7467 v = rb_complex_raw(INT2FIX(0), v);
7470 set_yylval_literal(v);
7471 SET_LEX_STATE(EXPR_END);
7475static enum yytokentype
7476set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7478 enum yytokentype type = tINTEGER;
7479 if (suffix & NUM_SUFFIX_R) {
7480 v = rb_rational_raw1(v);
7483 return set_number_literal(p, v, type, suffix);
7488dispatch_heredoc_end(struct parser_params *p)
7491 if (has_delayed_token(p))
7492 dispatch_delayed_token(p, tSTRING_CONTENT);
7493 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7494 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7500#define dispatch_heredoc_end(p) ((void)0)
7503static enum yytokentype
7504here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7506 int c, func, indent = 0;
7507 const char *eos, *ptr, *ptr_end;
7510 rb_encoding *enc = p->enc;
7511 rb_encoding *base_enc = 0;
7514 eos = RSTRING_PTR(here->lastline) + here->offset;
7516 indent = (func = here->func) & STR_FUNC_INDENT;
7518 if ((c = nextc(p)) == -1) {
7521 if (!has_delayed_token(p)) {
7522 dispatch_scan_event(p, tSTRING_CONTENT);
7525 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7526 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7527 int cr = ENC_CODERANGE_UNKNOWN;
7528 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7529 if (cr != ENC_CODERANGE_7BIT &&
7530 p->enc == rb_usascii_encoding() &&
7531 enc != rb_utf8_encoding()) {
7532 enc = rb_ascii8bit_encoding();
7535 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7537 dispatch_delayed_token(p, tSTRING_CONTENT);
7541 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7542 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7546 SET_LEX_STATE(EXPR_END);
7551 /* not beginning of line, cannot be the terminator */
7553 else if (p->heredoc_line_indent == -1) {
7554 /* `heredoc_line_indent == -1` means
7555 * - "after an interpolation in the same line", or
7556 * - "in a continuing line"
7558 p->heredoc_line_indent = 0;
7560 else if (whole_match_p(p, eos, len, indent)) {
7561 dispatch_heredoc_end(p);
7563 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7566 SET_LEX_STATE(EXPR_END);
7570 if (!(func & STR_FUNC_EXPAND)) {
7572 ptr = RSTRING_PTR(p->lex.lastline);
7573 ptr_end = p->lex.pend;
7574 if (ptr_end > ptr) {
7575 switch (ptr_end[-1]) {
7577 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7586 if (p->heredoc_indent > 0) {
7588 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7590 p->heredoc_line_indent = 0;
7594 rb_str_cat(str, ptr, ptr_end - ptr);
7596 str = STR_NEW(ptr, ptr_end - ptr);
7597 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7599 if (p->heredoc_indent > 0) {
7602 if (nextc(p) == -1) {
7608 } while (!whole_match_p(p, eos, len, indent));
7611 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7614 int t = parser_peek_variable_name(p);
7615 if (p->heredoc_line_indent != -1) {
7616 if (p->heredoc_indent > p->heredoc_line_indent) {
7617 p->heredoc_indent = p->heredoc_line_indent;
7619 p->heredoc_line_indent = -1;
7628 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7629 if (p->eofp) goto error;
7633 if (c == '\\') p->heredoc_line_indent = -1;
7635 str = STR_NEW3(tok(p), toklen(p), enc, func);
7637 set_yylval_str(str);
7639 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7641 flush_string_content(p, enc);
7642 return tSTRING_CONTENT;
7644 tokadd(p, nextc(p));
7645 if (p->heredoc_indent > 0) {
7649 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7650 if ((c = nextc(p)) == -1) goto error;
7651 } while (!whole_match_p(p, eos, len, indent));
7652 str = STR_NEW3(tok(p), toklen(p), enc, func);
7654 dispatch_heredoc_end(p);
7656 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7659 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7661 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7662 set_yylval_str(str);
7664 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7666 return tSTRING_CONTENT;
7672arg_ambiguous(struct parser_params *p, char c)
7675 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7677 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7683formal_argument(struct parser_params *p, ID lhs)
7685 switch (id_type(lhs)) {
7690 yyerror0("formal argument cannot be a constant");
7693 yyerror0("formal argument cannot be an instance variable");
7696 yyerror0("formal argument cannot be a global variable");
7699 yyerror0("formal argument cannot be a class variable");
7702 yyerror0("formal argument must be local variable");
7706 lhs = dispatch1(param_error, lhs);
7711 shadowing_lvar(p, lhs);
7716lvar_defined(struct parser_params *p, ID id)
7718 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7721/* emacsen -*- hack */
7723parser_encode_length(struct parser_params *p, const char *name, long len)
7727 if (len > 5 && name[nlen = len - 5] == '-') {
7728 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7731 if (len > 4 && name[nlen = len - 4] == '-') {
7732 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7734 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7735 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7736 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7743parser_set_encode(struct parser_params *p, const char *name)
7745 int idx = rb_enc_find_index(name);
7750 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7752 excargs[0] = rb_eArgError;
7753 excargs[2] = rb_make_backtrace();
7754 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7755 rb_exc_raise(rb_make_exception(3, excargs));
7757 enc = rb_enc_from_index(idx);
7758 if (!rb_enc_asciicompat(enc)) {
7759 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7764 if (p->debug_lines) {
7765 VALUE lines = p->debug_lines;
7766 long i, n = RARRAY_LEN(lines);
7767 for (i = 0; i < n; ++i) {
7768 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7775comment_at_top(struct parser_params *p)
7777 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7778 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7779 while (ptr < ptr_end) {
7780 if (!ISSPACE(*ptr)) return 0;
7786typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7787typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7790magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7792 if (!comment_at_top(p)) {
7795 parser_set_encode(p, val);
7799parser_get_bool(struct parser_params *p, const char *name, const char *val)
7803 if (strcasecmp(val, "true") == 0) {
7808 if (strcasecmp(val, "false") == 0) {
7813 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7818parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7820 int b = parser_get_bool(p, name, val);
7821 if (b >= 0) p->token_info_enabled = b;
7825parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7829 if (p->token_seen) {
7830 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7834 b = parser_get_bool(p, name, val);
7837 if (!p->compile_option)
7838 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7839 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7840 (b ? Qtrue : Qfalse));
7845parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7847 int b = parser_get_bool(p, name, val);
7848 if (b >= 0) p->past_scope_enabled = b;
7852struct magic_comment {
7854 rb_magic_comment_setter_t func;
7855 rb_magic_comment_length_t length;
7858static const struct magic_comment magic_comments[] = {
7859 {"coding", magic_comment_encoding, parser_encode_length},
7860 {"encoding", magic_comment_encoding, parser_encode_length},
7861 {"frozen_string_literal", parser_set_compile_option_flag},
7862 {"warn_indent", parser_set_token_info},
7864 {"warn_past_scope", parser_set_past_scope},
7869magic_comment_marker(const char *str, long len)
7876 if (str[i-1] == '*' && str[i-2] == '-') {
7882 if (i + 1 >= len) return 0;
7883 if (str[i+1] != '-') {
7886 else if (str[i-1] != '-') {
7902parser_magic_comment(struct parser_params *p, const char *str, long len)
7905 VALUE name = 0, val = 0;
7906 const char *beg, *end, *vbeg, *vend;
7907#define str_copy(_s, _p, _n) ((_s) \
7908 ? (void)(rb_str_resize((_s), (_n)), \
7909 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7910 : (void)((_s) = STR_NEW((_p), (_n))))
7912 if (len <= 7) return FALSE;
7913 if (!!(beg = magic_comment_marker(str, len))) {
7914 if (!(end = magic_comment_marker(beg, str + len - beg)))
7918 len = end - beg - 3;
7921 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7923 const struct magic_comment *mc = magic_comments;
7928 for (; len > 0 && *str; str++, --len) {
7930 case '\'': case '"': case ':': case ';':
7933 if (!ISSPACE(*str)) break;
7935 for (beg = str; len > 0; str++, --len) {
7937 case '\'': case '"': case ':': case ';':
7940 if (ISSPACE(*str)) break;
7945 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7948 if (!indicator) return FALSE;
7952 do str++; while (--len > 0 && ISSPACE(*str));
7955 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7968 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7972 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7975 while (len > 0 && (ISSPACE(*str))) --len, str++;
7976 if (len) return FALSE;
7980 str_copy(name, beg, n);
7981 s = RSTRING_PTR(name);
7982 for (i = 0; i < n; ++i) {
7983 if (s[i] == '-') s[i] = '_';
7986 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7989 n = (*mc->length)(p, vbeg, n);
7991 str_copy(val, vbeg, n);
7992 (*mc->func)(p, mc->name, RSTRING_PTR(val));
7995 } while (++mc < magic_comments + numberof(magic_comments));
7997 str_copy(val, vbeg, vend - vbeg);
7998 dispatch2(magic_comment, name, val);
8006set_file_encoding(struct parser_params *p, const char *str, const char *send)
8009 const char *beg = str;
8013 if (send - str <= 6) return;
8015 case 'C': case 'c': str += 6; continue;
8016 case 'O': case 'o': str += 5; continue;
8017 case 'D': case 'd': str += 4; continue;
8018 case 'I': case 'i': str += 3; continue;
8019 case 'N': case 'n': str += 2; continue;
8020 case 'G': case 'g': str += 1; continue;
8027 if (ISSPACE(*str)) break;
8030 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8034 if (++str >= send) return;
8035 } while (ISSPACE(*str));
8037 if (*str != '=' && *str != ':') return;
8042 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8043 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8044 parser_set_encode(p, RSTRING_PTR(s));
8045 rb_str_resize(s, 0);
8049parser_prepare(struct parser_params *p)
8052 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8055 if (peek(p, '!')) p->has_shebang = 1;
8057 case 0xef: /* UTF-8 BOM marker */
8058 if (p->lex.pend - p->lex.pcur >= 2 &&
8059 (unsigned char)p->lex.pcur[0] == 0xbb &&
8060 (unsigned char)p->lex.pcur[1] == 0xbf) {
8061 p->enc = rb_utf8_encoding();
8063 p->lex.pbeg = p->lex.pcur;
8071 p->enc = rb_enc_get(p->lex.lastline);
8075#define ambiguous_operator(tok, op, syn) ( \
8076 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8077 rb_warning0("even though it seems like "syn""))
8079#define ambiguous_operator(tok, op, syn) \
8080 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8082#define warn_balanced(tok, op, syn) ((void) \
8083 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8084 space_seen && !ISSPACE(c) && \
8085 (ambiguous_operator(tok, op, syn), 0)), \
8086 (enum yytokentype)(tok))
8089parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8092 char *point = &str[seen_point];
8093 size_t fraclen = len-seen_point-1;
8094 memmove(point, point+1, fraclen+1);
8095 v = rb_cstr_to_inum(str, 10, FALSE);
8096 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8099static enum yytokentype
8100no_digits(struct parser_params *p)
8102 yyerror0("numeric literal without digits");
8103 if (peek(p, '_')) nextc(p);
8104 /* dummy 0, for tUMINUS_NUM at numeric */
8105 return set_integer_literal(p, INT2FIX(0), 0);
8108static enum yytokentype
8109parse_numeric(struct parser_params *p, int c)
8111 int is_float, seen_point, seen_e, nondigit;
8114 is_float = seen_point = seen_e = nondigit = 0;
8115 SET_LEX_STATE(EXPR_END);
8117 if (c == '-' || c == '+') {
8122 int start = toklen(p);
8124 if (c == 'x' || c == 'X') {
8127 if (c != -1 && ISXDIGIT(c)) {
8130 if (nondigit) break;
8134 if (!ISXDIGIT(c)) break;
8137 } while ((c = nextc(p)) != -1);
8141 if (toklen(p) == start) {
8142 return no_digits(p);
8144 else if (nondigit) goto trailing_uc;
8145 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8146 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8148 if (c == 'b' || c == 'B') {
8151 if (c == '0' || c == '1') {
8154 if (nondigit) break;
8158 if (c != '0' && c != '1') break;
8161 } while ((c = nextc(p)) != -1);
8165 if (toklen(p) == start) {
8166 return no_digits(p);
8168 else if (nondigit) goto trailing_uc;
8169 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8170 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8172 if (c == 'd' || c == 'D') {
8175 if (c != -1 && ISDIGIT(c)) {
8178 if (nondigit) break;
8182 if (!ISDIGIT(c)) break;
8185 } while ((c = nextc(p)) != -1);
8189 if (toklen(p) == start) {
8190 return no_digits(p);
8192 else if (nondigit) goto trailing_uc;
8193 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8194 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8200 if (c == 'o' || c == 'O') {
8201 /* prefixed octal */
8203 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8204 return no_digits(p);
8207 if (c >= '0' && c <= '7') {
8212 if (nondigit) break;
8216 if (c < '0' || c > '9') break;
8217 if (c > '7') goto invalid_octal;
8220 } while ((c = nextc(p)) != -1);
8221 if (toklen(p) > start) {
8224 if (nondigit) goto trailing_uc;
8225 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8226 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8233 if (c > '7' && c <= '9') {
8235 yyerror0("Invalid octal digit");
8237 else if (c == '.' || c == 'e' || c == 'E') {
8242 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8243 return set_integer_literal(p, INT2FIX(0), suffix);
8249 case '0': case '1': case '2': case '3': case '4':
8250 case '5': case '6': case '7': case '8': case '9':
8256 if (nondigit) goto trailing_uc;
8257 if (seen_point || seen_e) {
8262 if (c0 == -1 || !ISDIGIT(c0)) {
8268 seen_point = toklen(p);
8287 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8292 tokadd(p, nondigit);
8296 nondigit = (c == '-' || c == '+') ? c : 0;
8299 case '_': /* `_' in number just ignored */
8300 if (nondigit) goto decode_num;
8314 literal_flush(p, p->lex.pcur - 1);
8315 YYLTYPE loc = RUBY_INIT_YYLLOC();
8316 compile_error(p, "trailing `%c' in number", nondigit);
8317 parser_show_error_line(p, &loc);
8321 enum yytokentype type = tFLOAT;
8324 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8325 if (suffix & NUM_SUFFIX_R) {
8327 v = parse_rational(p, tok(p), toklen(p), seen_point);
8330 double d = strtod(tok(p), 0);
8331 if (errno == ERANGE) {
8332 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8337 return set_number_literal(p, v, type, suffix);
8339 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8340 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8343static enum yytokentype
8344parse_qmark(struct parser_params *p, int space_seen)
8351 SET_LEX_STATE(EXPR_VALUE);
8356 compile_error(p, "incomplete character syntax");
8359 if (rb_enc_isspace(c, p->enc)) {
8361 int c2 = escaped_control_code(c);
8363 WARN_SPACE_CHAR(c2, "?");
8368 SET_LEX_STATE(EXPR_VALUE);
8373 if (!parser_isascii(p)) {
8374 if (tokadd_mbchar(p, c) == -1) return 0;
8376 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8377 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8379 const char *start = p->lex.pcur - 1, *ptr = start;
8381 int n = parser_precise_mbclen(p, ptr);
8382 if (n < 0) return -1;
8384 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8385 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8386 " a conditional operator, put a space after `?'",
8387 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8391 else if (c == '\\') {
8394 enc = rb_utf8_encoding();
8395 tokadd_utf8(p, &enc, -1, 0, 0);
8397 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8399 if (tokadd_mbchar(p, c) == -1) return 0;
8402 c = read_escape(p, 0, &enc);
8410 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8411 set_yylval_str(lit);
8412 SET_LEX_STATE(EXPR_END);
8416static enum yytokentype
8417parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8420 const char *ptok = p->lex.pcur;
8428 if (c == -1 || !ISALNUM(c)) {
8434 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8435 yyerror0("unknown type of %string");
8439 if (c == -1 || term == -1) {
8440 compile_error(p, "unterminated quoted string meets end of file");
8444 if (term == '(') term = ')';
8445 else if (term == '[') term = ']';
8446 else if (term == '{') term = '}';
8447 else if (term == '<') term = '>';
8450 p->lex.ptok = ptok-1;
8453 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8457 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8461 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8465 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8469 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8470 return tSYMBOLS_BEG;
8473 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8474 return tQSYMBOLS_BEG;
8477 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8478 return tXSTRING_BEG;
8481 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8485 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8486 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8490 yyerror0("unknown type of %string");
8494 if ((c = nextc(p)) == '=') {
8496 SET_LEX_STATE(EXPR_BEG);
8499 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8502 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8504 return warn_balanced('%', "%%", "string literal");
8508tokadd_ident(struct parser_params *p, int c)
8511 if (tokadd_mbchar(p, c) == -1) return -1;
8513 } while (parser_is_identchar(p));
8519tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8521 ID ident = TOK_INTERN();
8523 set_yylval_name(ident);
8529parse_numvar(struct parser_params *p)
8533 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8534 const unsigned long nth_ref_max =
8535 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8536 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8537 * turned into a Fixnum, in compile.c */
8539 if (overflow || n > nth_ref_max) {
8540 /* compile_error()? */
8541 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8542 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8549static enum yytokentype
8550parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8552 const char *ptr = p->lex.pcur;
8555 SET_LEX_STATE(EXPR_END);
8556 p->lex.ptok = ptr - 1; /* from '$' */
8560 case '_': /* $_: last read line string */
8562 if (parser_is_identchar(p)) {
8570 case '~': /* $~: match-data */
8571 case '*': /* $*: argv */
8572 case '$': /* $$: pid */
8573 case '?': /* $?: last status */
8574 case '!': /* $!: error string */
8575 case '@': /* $@: error position */
8576 case '/': /* $/: input record separator */
8577 case '\\': /* $\: output record separator */
8578 case ';': /* $;: field separator */
8579 case ',': /* $,: output field separator */
8580 case '.': /* $.: last read line number */
8581 case '=': /* $=: ignorecase */
8582 case ':': /* $:: load path */
8583 case '<': /* $<: reading filename */
8584 case '>': /* $>: default output handle */
8585 case '\"': /* $": already loaded files */
8594 if (parser_is_identchar(p)) {
8595 if (tokadd_mbchar(p, c) == -1) return 0;
8603 set_yylval_name(TOK_INTERN());
8606 case '&': /* $&: last match */
8607 case '`': /* $`: string before last match */
8608 case '\'': /* $': string after last match */
8609 case '+': /* $+: string matches last paren. */
8610 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8615 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8618 case '1': case '2': case '3':
8619 case '4': case '5': case '6':
8620 case '7': case '8': case '9':
8625 } while (c != -1 && ISDIGIT(c));
8627 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8629 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8633 if (!parser_is_identchar(p)) {
8634 YYLTYPE loc = RUBY_INIT_YYLLOC();
8635 if (c == -1 || ISSPACE(c)) {
8636 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8640 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8642 parser_show_error_line(p, &loc);
8643 set_yylval_noname();
8651 if (tokadd_ident(p, c)) return 0;
8652 SET_LEX_STATE(EXPR_END);
8653 tokenize_ident(p, last_state);
8659parser_numbered_param(struct parser_params *p, int n)
8661 if (n < 0) return false;
8663 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8666 if (p->max_numparam == ORDINAL_PARAM) {
8667 compile_error(p, "ordinary parameter is defined");
8670 struct vtable *args = p->lvtbl->args;
8671 if (p->max_numparam < n) {
8672 p->max_numparam = n;
8674 while (n > args->pos) {
8675 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8681static enum yytokentype
8682parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8684 const char *ptr = p->lex.pcur;
8685 enum yytokentype result = tIVAR;
8686 register int c = nextc(p);
8689 p->lex.ptok = ptr - 1; /* from '@' */
8697 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8698 if (c == -1 || !parser_is_identchar(p)) {
8700 RUBY_SET_YYLLOC(loc);
8701 if (result == tIVAR) {
8702 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8705 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8707 parser_show_error_line(p, &loc);
8708 set_yylval_noname();
8709 SET_LEX_STATE(EXPR_END);
8712 else if (ISDIGIT(c)) {
8714 RUBY_SET_YYLLOC(loc);
8715 if (result == tIVAR) {
8716 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8719 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8721 parser_show_error_line(p, &loc);
8722 set_yylval_noname();
8723 SET_LEX_STATE(EXPR_END);
8727 if (tokadd_ident(p, c)) return 0;
8728 tokenize_ident(p, last_state);
8732static enum yytokentype
8733parse_ident(struct parser_params *p, int c, int cmd_state)
8735 enum yytokentype result;
8736 int mb = ENC_CODERANGE_7BIT;
8737 const enum lex_state_e last_state = p->lex.state;
8741 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8742 if (tokadd_mbchar(p, c) == -1) return 0;
8744 } while (parser_is_identchar(p));
8745 if ((c == '!' || c == '?') && !peek(p, '=')) {
8749 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8750 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8751 result = tIDENTIFIER;
8755 result = tCONSTANT; /* assume provisionally */
8760 if (IS_LABEL_POSSIBLE()) {
8761 if (IS_LABEL_SUFFIX(0)) {
8762 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8764 set_yylval_name(TOK_INTERN());
8768 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8769 const struct kwtable *kw;
8771 /* See if it is a reserved word. */
8772 kw = rb_reserved_word(tok(p), toklen(p));
8774 enum lex_state_e state = p->lex.state;
8775 if (IS_lex_state_for(state, EXPR_FNAME)) {
8776 SET_LEX_STATE(EXPR_ENDFN);
8777 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8780 SET_LEX_STATE(kw->state);
8781 if (IS_lex_state(EXPR_BEG)) {
8782 p->command_start = TRUE;
8784 if (kw->id[0] == keyword_do) {
8785 if (lambda_beginning_p()) {
8786 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8787 return keyword_do_LAMBDA;
8789 if (COND_P()) return keyword_do_cond;
8790 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8791 return keyword_do_block;
8794 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8797 if (kw->id[0] != kw->id[1])
8798 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8804 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8806 SET_LEX_STATE(EXPR_CMDARG);
8809 SET_LEX_STATE(EXPR_ARG);
8812 else if (p->lex.state == EXPR_FNAME) {
8813 SET_LEX_STATE(EXPR_ENDFN);
8816 SET_LEX_STATE(EXPR_END);
8819 ident = tokenize_ident(p, last_state);
8820 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8821 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8822 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8823 lvar_defined(p, ident)) {
8824 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8829static enum yytokentype
8830parser_yylex(struct parser_params *p)
8836 enum lex_state_e last_state;
8837 int fallthru = FALSE;
8838 int token_seen = p->token_seen;
8840 if (p->lex.strterm) {
8841 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8842 return here_document(p, &p->lex.strterm->u.heredoc);
8846 return parse_string(p, &p->lex.strterm->u.literal);
8849 cmd_state = p->command_start;
8850 p->command_start = FALSE;
8851 p->token_seen = TRUE;
8853 last_state = p->lex.state;
8857 switch (c = nextc(p)) {
8858 case '\0': /* NUL */
8859 case '\004': /* ^D */
8860 case '\032': /* ^Z */
8861 case -1: /* end of script. */
8865 case ' ': case '\t': case '\f': case '\r':
8866 case '\13': /* '\v' */
8869 while ((c = nextc(p))) {
8871 case ' ': case '\t': case '\f': case '\r':
8872 case '\13': /* '\v' */
8880 dispatch_scan_event(p, tSP);
8884 case '#': /* it's a comment */
8885 p->token_seen = token_seen;
8886 /* no magic_comment in shebang line */
8887 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8888 if (comment_at_top(p)) {
8889 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8893 dispatch_scan_event(p, tCOMMENT);
8897 p->token_seen = token_seen;
8898 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8899 !IS_lex_state(EXPR_LABELED));
8900 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8902 dispatch_scan_event(p, tIGNORED_NL);
8905 if (!c && p->in_kwarg) {
8906 goto normal_newline;
8911 switch (c = nextc(p)) {
8912 case ' ': case '\t': case '\f': case '\r':
8913 case '\13': /* '\v' */
8918 if (space_seen) dispatch_scan_event(p, tSP);
8922 dispatch_delayed_token(p, tIGNORED_NL);
8923 if (peek(p, '.') == (c == '&')) {
8925 dispatch_scan_event(p, tSP);
8930 p->ruby_sourceline--;
8931 p->lex.nextline = p->lex.lastline;
8932 case -1: /* EOF no decrement*/
8934 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8935 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8936 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8937 pushback(p, 1); /* always pushback */
8938 p->lex.ptok = p->lex.pcur;
8942 p->lex.ptok = p->lex.pcur;
8945 goto normal_newline;
8949 p->command_start = TRUE;
8950 SET_LEX_STATE(EXPR_BEG);
8954 if ((c = nextc(p)) == '*') {
8955 if ((c = nextc(p)) == '=') {
8956 set_yylval_id(idPow);
8957 SET_LEX_STATE(EXPR_BEG);
8962 rb_warning0("`**' interpreted as argument prefix");
8965 else if (IS_BEG()) {
8969 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8975 SET_LEX_STATE(EXPR_BEG);
8980 rb_warning0("`*' interpreted as argument prefix");
8983 else if (IS_BEG()) {
8987 c = warn_balanced('*', "*", "argument prefix");
8990 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8995 if (IS_AFTER_OPERATOR()) {
8996 SET_LEX_STATE(EXPR_ARG);
9002 SET_LEX_STATE(EXPR_BEG);
9015 /* skip embedded rd document */
9016 if (word_match_p(p, "begin", 5)) {
9020 dispatch_scan_event(p, tEMBDOC_BEG);
9024 dispatch_scan_event(p, tEMBDOC);
9029 compile_error(p, "embedded document meets end of file");
9032 if (c == '=' && word_match_p(p, "end", 3)) {
9038 dispatch_scan_event(p, tEMBDOC_END);
9043 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9044 if ((c = nextc(p)) == '=') {
9045 if ((c = nextc(p)) == '=') {
9054 else if (c == '>') {
9063 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9065 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9066 int token = heredoc_identifier(p);
9067 if (token) return token < 0 ? 0 : token;
9069 if (IS_AFTER_OPERATOR()) {
9070 SET_LEX_STATE(EXPR_ARG);
9073 if (IS_lex_state(EXPR_CLASS))
9074 p->command_start = TRUE;
9075 SET_LEX_STATE(EXPR_BEG);
9078 if ((c = nextc(p)) == '>') {
9085 if ((c = nextc(p)) == '=') {
9086 set_yylval_id(idLTLT);
9087 SET_LEX_STATE(EXPR_BEG);
9091 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9097 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9098 if ((c = nextc(p)) == '=') {
9102 if ((c = nextc(p)) == '=') {
9103 set_yylval_id(idGTGT);
9104 SET_LEX_STATE(EXPR_BEG);
9114 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9115 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9116 p->lex.ptok = p->lex.pcur-1;
9120 if (IS_lex_state(EXPR_FNAME)) {
9121 SET_LEX_STATE(EXPR_ENDFN);
9124 if (IS_lex_state(EXPR_DOT)) {
9126 SET_LEX_STATE(EXPR_CMDARG);
9128 SET_LEX_STATE(EXPR_ARG);
9131 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9132 return tXSTRING_BEG;
9135 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9136 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9137 p->lex.ptok = p->lex.pcur-1;
9141 return parse_qmark(p, space_seen);
9144 if ((c = nextc(p)) == '&') {
9145 SET_LEX_STATE(EXPR_BEG);
9146 if ((c = nextc(p)) == '=') {
9147 set_yylval_id(idANDOP);
9148 SET_LEX_STATE(EXPR_BEG);
9154 else if (c == '=') {
9156 SET_LEX_STATE(EXPR_BEG);
9159 else if (c == '.') {
9160 set_yylval_id(idANDDOT);
9161 SET_LEX_STATE(EXPR_DOT);
9167 (c = peekc_n(p, 1)) == -1 ||
9168 !(c == '\'' || c == '"' ||
9169 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9170 rb_warning0("`&' interpreted as argument prefix");
9174 else if (IS_BEG()) {
9178 c = warn_balanced('&', "&", "argument prefix");
9180 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9184 if ((c = nextc(p)) == '|') {
9185 SET_LEX_STATE(EXPR_BEG);
9186 if ((c = nextc(p)) == '=') {
9187 set_yylval_id(idOROP);
9188 SET_LEX_STATE(EXPR_BEG);
9192 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9201 SET_LEX_STATE(EXPR_BEG);
9204 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9210 if (IS_AFTER_OPERATOR()) {
9211 SET_LEX_STATE(EXPR_ARG);
9220 SET_LEX_STATE(EXPR_BEG);
9223 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9224 SET_LEX_STATE(EXPR_BEG);
9226 if (c != -1 && ISDIGIT(c)) {
9227 return parse_numeric(p, '+');
9231 SET_LEX_STATE(EXPR_BEG);
9233 return warn_balanced('+', "+", "unary operator");
9237 if (IS_AFTER_OPERATOR()) {
9238 SET_LEX_STATE(EXPR_ARG);
9247 SET_LEX_STATE(EXPR_BEG);
9251 SET_LEX_STATE(EXPR_ENDFN);
9254 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9255 SET_LEX_STATE(EXPR_BEG);
9257 if (c != -1 && ISDIGIT(c)) {
9262 SET_LEX_STATE(EXPR_BEG);
9264 return warn_balanced('-', "-", "unary operator");
9267 int is_beg = IS_BEG();
9268 SET_LEX_STATE(EXPR_BEG);
9269 if ((c = nextc(p)) == '.') {
9270 if ((c = nextc(p)) == '.') {
9271 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9272 rb_warn0("... at EOL, should be parenthesized?");
9274 return is_beg ? tBDOT3 : tDOT3;
9277 return is_beg ? tBDOT2 : tDOT2;
9280 if (c != -1 && ISDIGIT(c)) {
9281 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9282 parse_numeric(p, '.');
9283 if (ISDIGIT(prev)) {
9284 yyerror0("unexpected fraction part after numeric literal");
9287 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9289 SET_LEX_STATE(EXPR_END);
9290 p->lex.ptok = p->lex.pcur;
9294 SET_LEX_STATE(EXPR_DOT);
9298 case '0': case '1': case '2': case '3': case '4':
9299 case '5': case '6': case '7': case '8': case '9':
9300 return parse_numeric(p, c);
9305 SET_LEX_STATE(EXPR_ENDFN);
9306 p->lex.paren_nest--;
9312 SET_LEX_STATE(EXPR_END);
9313 p->lex.paren_nest--;
9317 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9318 if (!p->lex.brace_nest--) return tSTRING_DEND;
9321 SET_LEX_STATE(EXPR_END);
9322 p->lex.paren_nest--;
9328 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9329 SET_LEX_STATE(EXPR_BEG);
9332 set_yylval_id(idCOLON2);
9333 SET_LEX_STATE(EXPR_DOT);
9336 if (IS_END() || ISSPACE(c) || c == '#') {
9338 c = warn_balanced(':', ":", "symbol literal");
9339 SET_LEX_STATE(EXPR_BEG);
9344 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9347 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9353 SET_LEX_STATE(EXPR_FNAME);
9358 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9361 if ((c = nextc(p)) == '=') {
9363 SET_LEX_STATE(EXPR_BEG);
9368 arg_ambiguous(p, '/');
9369 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9372 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9373 return warn_balanced('/', "/", "regexp literal");
9376 if ((c = nextc(p)) == '=') {
9378 SET_LEX_STATE(EXPR_BEG);
9381 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9386 SET_LEX_STATE(EXPR_BEG);
9387 p->command_start = TRUE;
9391 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9395 if (IS_AFTER_OPERATOR()) {
9396 if ((c = nextc(p)) != '@') {
9399 SET_LEX_STATE(EXPR_ARG);
9402 SET_LEX_STATE(EXPR_BEG);
9410 else if (!space_seen) {
9411 /* foo( ... ) => method call, no ambiguity */
9413 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9416 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9417 rb_warning0("parentheses after method name is interpreted as "
9418 "an argument list, not a decomposed argument");
9420 p->lex.paren_nest++;
9423 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9427 p->lex.paren_nest++;
9428 if (IS_AFTER_OPERATOR()) {
9429 if ((c = nextc(p)) == ']') {
9430 SET_LEX_STATE(EXPR_ARG);
9431 if ((c = nextc(p)) == '=') {
9438 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9441 else if (IS_BEG()) {
9444 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9447 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9453 ++p->lex.brace_nest;
9454 if (lambda_beginning_p())
9456 else if (IS_lex_state(EXPR_LABELED))
9457 c = tLBRACE; /* hash */
9458 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9459 c = '{'; /* block (primary) */
9460 else if (IS_lex_state(EXPR_ENDARG))
9461 c = tLBRACE_ARG; /* block (expr) */
9463 c = tLBRACE; /* hash */
9465 p->command_start = TRUE;
9466 SET_LEX_STATE(EXPR_BEG);
9469 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9471 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9480 dispatch_scan_event(p, tSP);
9481 goto retry; /* skip \\n */
9483 if (c == ' ') return tSP;
9484 if (ISSPACE(c)) return c;
9489 return parse_percent(p, space_seen, last_state);
9492 return parse_gvar(p, last_state);
9495 return parse_atmark(p, last_state);
9498 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9499 p->ruby__end__seen = 1;
9505 dispatch_scan_event(p, k__END__);
9513 if (!parser_is_identchar(p)) {
9514 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9523 return parse_ident(p, c, cmd_state);
9526static enum yytokentype
9527yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9533 t = parser_yylex(p);
9535 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9536 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9538 RUBY_SET_YYLLOC(*yylloc);
9540 if (has_delayed_token(p))
9541 dispatch_delayed_token(p, t);
9543 dispatch_scan_event(p, t);
9548#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9551node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9553 NODE *n = rb_ast_newnode(p->ast, type);
9555 rb_node_init(n, type, a0, a1, a2);
9558 nd_set_node_id(n, parser_get_node_id(p));
9563nd_set_loc(NODE *nd, const YYLTYPE *loc)
9566 nd_set_line(nd, loc->beg_pos.lineno);
9571static enum node_type
9572nodetype(NODE *node) /* for debug */
9574 return (enum node_type)nd_type(node);
9580 return nd_line(node);
9584newline_node(NODE *node)
9587 node = remove_begin(node);
9588 node->flags |= NODE_FL_NEWLINE;
9594fixpos(NODE *node, NODE *orig)
9598 nd_set_line(node, nd_line(orig));
9602parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9604 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9608parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9610 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9614block_append(struct parser_params *p, NODE *head, NODE *tail)
9616 NODE *end, *h = head, *nd;
9618 if (tail == 0) return head;
9620 if (h == 0) return tail;
9621 switch (nd_type(h)) {
9628 parser_warning(p, h, "unused literal ignored");
9631 h = end = NEW_BLOCK(head, &head->nd_loc);
9641 switch (nd_type(nd)) {
9647 if (RTEST(ruby_verbose)) {
9648 parser_warning(p, tail, "statement not reached");
9656 if (nd_type(tail) != NODE_BLOCK) {
9657 tail = NEW_BLOCK(tail, &tail->nd_loc);
9658 tail->nd_end = tail;
9660 end->nd_next = tail;
9661 h->nd_end = tail->nd_end;
9662 nd_set_last_loc(head, nd_last_loc(tail));
9666/* append item to the list */
9668list_append(struct parser_params *p, NODE *list, NODE *item)
9672 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9673 if (list->nd_next) {
9674 last = list->nd_next->nd_end;
9681 last->nd_next = NEW_LIST(item, &item->nd_loc);
9682 list->nd_next->nd_end = last->nd_next;
9684 nd_set_last_loc(list, nd_last_loc(item));
9689/* concat two lists */
9691list_concat(NODE *head, NODE *tail)
9695 if (head->nd_next) {
9696 last = head->nd_next->nd_end;
9702 head->nd_alen += tail->nd_alen;
9703 last->nd_next = tail;
9704 if (tail->nd_next) {
9705 head->nd_next->nd_end = tail->nd_next->nd_end;
9708 head->nd_next->nd_end = tail;
9711 nd_set_last_loc(head, nd_last_loc(tail));
9717literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9719 if (NIL_P(tail)) return 1;
9720 if (!rb_enc_compatible(head, tail)) {
9721 compile_error(p, "string literal encodings differ (%s / %s)",
9722 rb_enc_name(rb_enc_get(head)),
9723 rb_enc_name(rb_enc_get(tail)));
9724 rb_str_resize(head, 0);
9725 rb_str_resize(tail, 0);
9728 rb_str_buf_append(head, tail);
9732/* concat two string literals */
9734literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9736 enum node_type htype;
9740 if (!head) return tail;
9741 if (!tail) return head;
9743 htype = nd_type(head);
9744 if (htype == NODE_EVSTR) {
9745 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9746 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9747 head = list_append(p, node, head);
9750 if (p->heredoc_indent > 0) {
9753 nd_set_type(head, NODE_DSTR);
9755 return list_append(p, head, tail);
9760 switch (nd_type(tail)) {
9762 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9763 nd_type(headlast) == NODE_STR) {
9765 lit = headlast->nd_lit;
9770 if (htype == NODE_STR) {
9771 if (!literal_concat0(p, lit, tail->nd_lit)) {
9773 rb_discard_node(p, head);
9774 rb_discard_node(p, tail);
9777 rb_discard_node(p, tail);
9780 list_append(p, head, tail);
9785 if (htype == NODE_STR) {
9786 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9788 tail->nd_lit = head->nd_lit;
9789 rb_discard_node(p, head);
9792 else if (NIL_P(tail->nd_lit)) {
9794 head->nd_alen += tail->nd_alen - 1;
9795 head->nd_next->nd_end->nd_next = tail->nd_next;
9796 head->nd_next->nd_end = tail->nd_next->nd_end;
9797 rb_discard_node(p, tail);
9799 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9800 nd_type(headlast) == NODE_STR) {
9801 lit = headlast->nd_lit;
9802 if (!literal_concat0(p, lit, tail->nd_lit))
9804 tail->nd_lit = Qnil;
9808 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9813 if (htype == NODE_STR) {
9814 nd_set_type(head, NODE_DSTR);
9817 list_append(p, head, tail);
9824evstr2dstr(struct parser_params *p, NODE *node)
9826 if (nd_type(node) == NODE_EVSTR) {
9827 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9828 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9829 node = list_append(p, dstr, node);
9835new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9840 switch (nd_type(node)) {
9841 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9845 return NEW_EVSTR(head, loc);
9849call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9850 const YYLTYPE *op_loc, const YYLTYPE *loc)
9855 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9856 nd_set_line(expr, op_loc->beg_pos.lineno);
9861call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9865 opcall = NEW_OPCALL(recv, id, 0, loc);
9866 nd_set_line(opcall, op_loc->beg_pos.lineno);
9871new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9873 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9874 nd_set_line(qcall, op_loc->beg_pos.lineno);
9879new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9882 if (block) block_dup_check(p, args, block);
9883 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9884 if (block) ret = method_add_block(p, ret, block, loc);
9889#define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9891match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9894 int line = op_loc->beg_pos.lineno;
9898 if (node1 && (n = nd_once_body(node1)) != 0) {
9899 switch (nd_type(n)) {
9902 NODE *match = NEW_MATCH2(node1, node2, loc);
9903 nd_set_line(match, line);
9908 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9909 const VALUE lit = n->nd_lit;
9910 NODE *match = NEW_MATCH2(node1, node2, loc);
9911 match->nd_args = reg_named_capture_assign(p, lit, loc);
9912 nd_set_line(match, line);
9918 if (node2 && (n = nd_once_body(node2)) != 0) {
9921 switch (nd_type(n)) {
9923 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9926 match3 = NEW_MATCH3(node2, node1, loc);
9931 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9932 nd_set_line(n, line);
9938past_dvar_p(struct parser_params *p, ID id)
9940 struct vtable *past = p->lvtbl->past;
9942 if (vtable_included(past, id)) return 1;
9949/* As Ripper#warn does not have arguments for the location, so the
9950 * following messages cannot be separated */
9951#define WARN_LOCATION(type) do { \
9952 if (p->warn_location) { \
9954 VALUE file = rb_source_location(&line); \
9955 rb_warn3(type" in eval may not return location in binding;" \
9956 " use Binding#source_location instead\n" \
9957 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9958 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9963numparam_nested_p(struct parser_params *p)
9965 struct local_vars *local = p->lvtbl;
9966 NODE *outer = local->numparam.outer;
9967 NODE *inner = local->numparam.inner;
9968 if (outer || inner) {
9969 NODE *used = outer ? outer : inner;
9970 compile_error(p, "numbered parameter is already used in\n"
9971 "%s:%d: %s block here",
9972 p->ruby_sourcefile, nd_line(used),
9973 outer ? "outer" : "inner");
9974 parser_show_error_line(p, &used->nd_loc);
9981gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9987 return NEW_SELF(loc);
9989 return NEW_NIL(loc);
9991 return NEW_TRUE(loc);
9993 return NEW_FALSE(loc);
9994 case keyword__FILE__:
9995 WARN_LOCATION("__FILE__");
9997 VALUE file = p->ruby_sourcefile_string;
9999 file = rb_str_new(0, 0);
10001 file = rb_str_dup(file);
10002 node = NEW_STR(file, loc);
10003 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10006 case keyword__LINE__:
10007 WARN_LOCATION("__LINE__");
10008 return NEW_LIT(INT2FIX(p->tokline), loc);
10009 case keyword__ENCODING__:
10010 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10011 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10015 switch (id_type(id)) {
10017 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10018 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10019 if (id == p->cur_arg) {
10020 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10023 if (vidp) *vidp |= LVAR_USED;
10024 node = NEW_DVAR(id, loc);
10027 if (local_id_ref(p, id, &vidp)) {
10028 if (id == p->cur_arg) {
10029 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10032 if (vidp) *vidp |= LVAR_USED;
10033 node = NEW_LVAR(id, loc);
10036 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10037 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10038 if (numparam_nested_p(p)) return 0;
10039 node = NEW_DVAR(id, loc);
10040 struct local_vars *local = p->lvtbl;
10041 if (!local->numparam.current) local->numparam.current = node;
10044# if WARN_PAST_SCOPE
10045 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10046 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10049 /* method call without arguments */
10050 return NEW_VCALL(id, loc);
10052 return NEW_GVAR(id, loc);
10054 return NEW_IVAR(id, loc);
10056 return NEW_CONST(id, loc);
10058 return NEW_CVAR(id, loc);
10060 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10065opt_arg_append(NODE *opt_list, NODE *opt)
10067 NODE *opts = opt_list;
10068 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10070 while (opts->nd_next) {
10071 opts = opts->nd_next;
10072 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10074 opts->nd_next = opt;
10080kwd_append(NODE *kwlist, NODE *kw)
10083 NODE *kws = kwlist;
10084 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10085 while (kws->nd_next) {
10086 kws = kws->nd_next;
10087 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10095new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10097 return NEW_DEFINED(remove_begin_all(expr), loc);
10101symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10103 if (nd_type(symbol) == NODE_DSTR) {
10104 nd_set_type(symbol, NODE_DSYM);
10107 nd_set_type(symbol, NODE_LIT);
10108 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10110 return list_append(p, symbols, symbol);
10114new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10120 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10121 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10124 switch (nd_type(node)) {
10127 VALUE src = node->nd_lit;
10128 nd_set_type(node, NODE_LIT);
10129 nd_set_loc(node, loc);
10130 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10135 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10136 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10139 nd_set_type(node, NODE_DREGX);
10140 nd_set_loc(node, loc);
10141 node->nd_cflag = options & RE_OPTION_MASK;
10142 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10143 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10144 if (nd_type(list->nd_head) == NODE_STR) {
10145 VALUE tail = list->nd_head->nd_lit;
10146 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10147 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10148 if (!literal_concat0(p, lit, tail)) {
10149 return NEW_NIL(loc); /* dummy node on error */
10151 rb_str_resize(tail, 0);
10152 prev->nd_next = list->nd_next;
10153 rb_discard_node(p, list->nd_head);
10154 rb_discard_node(p, list);
10165 if (!node->nd_next) {
10166 VALUE src = node->nd_lit;
10167 nd_set_type(node, NODE_LIT);
10168 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10170 if (options & RE_OPTION_ONCE) {
10171 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10179new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10182 return NEW_KW_ARG(0, (k), loc);
10186new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10189 VALUE lit = STR_NEW0();
10190 NODE *xstr = NEW_XSTR(lit, loc);
10191 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10194 switch (nd_type(node)) {
10196 nd_set_type(node, NODE_XSTR);
10197 nd_set_loc(node, loc);
10200 nd_set_type(node, NODE_DXSTR);
10201 nd_set_loc(node, loc);
10204 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10211check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10215 if (!arg || !p->case_labels) return;
10217 lit = rb_node_case_when_optimizable_literal(arg);
10218 if (lit == Qundef) return;
10219 if (nd_type(arg) == NODE_STR) {
10220 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10223 if (NIL_P(p->case_labels)) {
10224 p->case_labels = rb_obj_hide(rb_hash_new());
10227 VALUE line = rb_hash_lookup(p->case_labels, lit);
10228 if (!NIL_P(line)) {
10229 rb_warning1("duplicated `when' clause with line %d is ignored",
10234 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10239id_is_var(struct parser_params *p, ID id)
10241 if (is_notop_id(id)) {
10242 switch (id & ID_SCOPE_MASK) {
10243 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10246 if (dyna_in_block(p)) {
10247 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10249 if (local_id(p, id)) return 1;
10250 /* method call without arguments */
10254 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10259new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10261 VALUE src = 0, err;
10263 if (ripper_is_node_yylval(re)) {
10264 src = RNODE(re)->nd_cval;
10265 re = RNODE(re)->nd_rval;
10267 if (ripper_is_node_yylval(opt)) {
10268 options = (int)RNODE(opt)->nd_tag;
10269 opt = RNODE(opt)->nd_rval;
10271 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10272 compile_error(p, "%"PRIsVALUE, err);
10274 return dispatch2(regexp_literal, re, opt);
10276#endif /* !RIPPER */
10280static const char rb_parser_lex_state_names[][8] = {
10281 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10282 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10283 "LABEL", "LABELED","FITEM",
10287append_lex_state_name(enum lex_state_e state, VALUE buf)
10290 unsigned int mask = 1;
10291 static const char none[] = "NONE";
10293 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10294 if ((unsigned)state & mask) {
10296 rb_str_cat(buf, "|", 1);
10299 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10303 rb_str_cat(buf, none, sizeof(none)-1);
10309flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10311 VALUE mesg = p->debug_buffer;
10313 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10314 p->debug_buffer = Qnil;
10315 rb_io_puts(1, &mesg, out);
10317 if (!NIL_P(str) && RSTRING_LEN(str)) {
10318 rb_io_write(p->debug_output, str);
10323rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10324 enum lex_state_e to, int line)
10327 mesg = rb_str_new_cstr("lex_state: ");
10328 append_lex_state_name(from, mesg);
10329 rb_str_cat_cstr(mesg, " -> ");
10330 append_lex_state_name(to, mesg);
10331 rb_str_catf(mesg, " at line %d\n", line);
10332 flush_debug_buffer(p, p->debug_output, mesg);
10337rb_parser_lex_state_name(enum lex_state_e state)
10339 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10343append_bitstack_value(stack_type stack, VALUE mesg)
10346 rb_str_cat_cstr(mesg, "0");
10349 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10350 for (; mask && !(stack & mask); mask >>= 1) continue;
10351 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10356rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10357 const char *name, int line)
10359 VALUE mesg = rb_sprintf("%s: ", name);
10360 append_bitstack_value(stack, mesg);
10361 rb_str_catf(mesg, " at line %d\n", line);
10362 flush_debug_buffer(p, p->debug_output, mesg);
10366rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10369 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10372 rb_str_vcatf(mesg, fmt, ap);
10374 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10377 mesg = rb_str_new(0, 0);
10378 append_lex_state_name(p->lex.state, mesg);
10379 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10380 rb_str_resize(mesg, 0);
10381 append_bitstack_value(p->cond_stack, mesg);
10382 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10383 rb_str_resize(mesg, 0);
10384 append_bitstack_value(p->cmdarg_stack, mesg);
10385 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10386 if (p->debug_output == rb_stdout)
10387 p->debug_output = rb_stderr;
10392rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10394 int sourceline = here->sourceline;
10395 int beg_pos = (int)here->offset - here->quote
10396 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10397 int end_pos = (int)here->offset + here->length + here->quote;
10399 yylloc->beg_pos.lineno = sourceline;
10400 yylloc->beg_pos.column = beg_pos;
10401 yylloc->end_pos.lineno = sourceline;
10402 yylloc->end_pos.column = end_pos;
10407rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10409 yylloc->beg_pos.lineno = p->ruby_sourceline;
10410 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10411 yylloc->end_pos.lineno = p->ruby_sourceline;
10412 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10417rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10419 yylloc->beg_pos.lineno = p->ruby_sourceline;
10420 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10421 yylloc->end_pos.lineno = p->ruby_sourceline;
10422 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10425#endif /* !RIPPER */
10428parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10433 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10434 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10436 v = rb_id2str(valp->id);
10438 v = valp->node->nd_rval;
10440 rb_parser_printf(p, "%"PRIsVALUE, v);
10442 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10443 case tSTRING_CONTENT: case tCHAR:
10445 v = valp->node->nd_lit;
10449 rb_parser_printf(p, "%+"PRIsVALUE, v);
10453 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10455 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10460 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10462 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10471assignable0(struct parser_params *p, ID id, const char **err)
10473 if (!id) return -1;
10476 *err = "Can't change the value of self";
10479 *err = "Can't assign to nil";
10482 *err = "Can't assign to true";
10484 case keyword_false:
10485 *err = "Can't assign to false";
10487 case keyword__FILE__:
10488 *err = "Can't assign to __FILE__";
10490 case keyword__LINE__:
10491 *err = "Can't assign to __LINE__";
10493 case keyword__ENCODING__:
10494 *err = "Can't assign to __ENCODING__";
10497 switch (id_type(id)) {
10499 if (dyna_in_block(p)) {
10500 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10501 compile_error(p, "Can't assign to numbered parameter _%d",
10502 NUMPARAM_ID_TO_IDX(id));
10505 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10506 if (dvar_defined(p, id)) return NODE_DASGN;
10507 if (local_id(p, id)) return NODE_LASGN;
10509 return NODE_DASGN_CURR;
10512 if (!local_id(p, id)) local_var(p, id);
10516 case ID_GLOBAL: return NODE_GASGN;
10517 case ID_INSTANCE: return NODE_IASGN;
10519 if (!p->in_def) return NODE_CDECL;
10520 *err = "dynamic constant assignment";
10522 case ID_CLASS: return NODE_CVASGN;
10524 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10531assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10533 const char *err = 0;
10534 int node_type = assignable0(p, id, &err);
10535 switch (node_type) {
10536 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10537 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10538 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10539 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10540 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10541 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10542 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10544 if (err) yyerror1(loc, err);
10545 return NEW_BEGIN(0, loc);
10549assignable(struct parser_params *p, VALUE lhs)
10551 const char *err = 0;
10552 assignable0(p, get_id(lhs), &err);
10553 if (err) lhs = assign_error(p, lhs);
10559is_private_local_id(ID name)
10562 if (name == idUScore) return 1;
10563 if (!is_local_id(name)) return 0;
10564 s = rb_id2str(name);
10566 return RSTRING_PTR(s)[0] == '_';
10570shadowing_lvar_0(struct parser_params *p, ID name)
10572 if (is_private_local_id(name)) return 1;
10573 if (dyna_in_block(p)) {
10574 if (dvar_curr(p, name)) {
10575 yyerror0("duplicated argument name");
10577 else if (dvar_defined(p, name) || local_id(p, name)) {
10578 vtable_add(p->lvtbl->vars, name);
10579 if (p->lvtbl->used) {
10580 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10586 if (local_id(p, name)) {
10587 yyerror0("duplicated argument name");
10594shadowing_lvar(struct parser_params *p, ID name)
10596 shadowing_lvar_0(p, name);
10601new_bv(struct parser_params *p, ID name)
10604 if (!is_local_id(name)) {
10605 compile_error(p, "invalid local variable - %"PRIsVALUE,
10609 if (!shadowing_lvar_0(p, name)) return;
10615aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10617 return NEW_ATTRASGN(recv, tASET, idx, loc);
10621block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10623 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10624 compile_error(p, "both block arg and actual block given");
10629attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10631 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10632 return NEW_ATTRASGN(recv, id, 0, loc);
10636rb_backref_error(struct parser_params *p, NODE *node)
10638 switch (nd_type(node)) {
10640 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10642 case NODE_BACK_REF:
10643 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10649arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10651 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10652 switch (nd_type(node1)) {
10654 return list_append(p, node1, node2);
10655 case NODE_BLOCK_PASS:
10656 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10657 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10659 case NODE_ARGSPUSH:
10660 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10661 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10662 nd_set_type(node1, NODE_ARGSCAT);
10665 if (nd_type(node1->nd_body) != NODE_LIST) break;
10666 node1->nd_body = list_append(p, node1->nd_body, node2);
10667 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10670 return NEW_ARGSPUSH(node1, node2, loc);
10674arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10676 if (!node2) return node1;
10677 switch (nd_type(node1)) {
10678 case NODE_BLOCK_PASS:
10679 if (node1->nd_head)
10680 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10682 node1->nd_head = NEW_LIST(node2, loc);
10684 case NODE_ARGSPUSH:
10685 if (nd_type(node2) != NODE_LIST) break;
10686 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10687 nd_set_type(node1, NODE_ARGSCAT);
10690 if (nd_type(node2) != NODE_LIST ||
10691 nd_type(node1->nd_body) != NODE_LIST) break;
10692 node1->nd_body = list_concat(node1->nd_body, node2);
10695 return NEW_ARGSCAT(node1, node2, loc);
10699last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10702 if ((n1 = splat_array(args)) != 0) {
10703 return list_append(p, n1, last_arg);
10705 return arg_append(p, args, last_arg, loc);
10709rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10712 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10713 return list_concat(n1, rest_arg);
10715 return arg_concat(p, args, rest_arg, loc);
10719splat_array(NODE* node)
10721 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10722 if (nd_type(node) == NODE_LIST) return node;
10727mark_lvar_used(struct parser_params *p, NODE *rhs)
10731 switch (nd_type(rhs)) {
10733 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10734 if (vidp) *vidp |= LVAR_USED;
10738 case NODE_DASGN_CURR:
10739 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10740 if (vidp) *vidp |= LVAR_USED;
10745 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10746 mark_lvar_used(p, rhs->nd_head);
10754node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10756 if (!lhs) return 0;
10758 switch (nd_type(lhs)) {
10763 case NODE_DASGN_CURR:
10767 lhs->nd_value = rhs;
10768 nd_set_loc(lhs, loc);
10771 case NODE_ATTRASGN:
10772 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10773 nd_set_loc(lhs, loc);
10777 /* should not happen */
10785value_expr_check(struct parser_params *p, NODE *node)
10787 NODE *void_node = 0, *vn;
10790 rb_warning0("empty expression");
10793 switch (nd_type(node)) {
10799 return void_node ? void_node : node;
10802 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10803 compile_error(p, "unexpected node");
10806 if (node->nd_body->nd_body) {
10809 /* single line pattern matching */
10810 return void_node ? void_node : node;
10813 while (node->nd_next) {
10814 node = node->nd_next;
10816 node = node->nd_head;
10820 node = node->nd_body;
10825 if (!node->nd_body) {
10828 else if (!node->nd_else) {
10831 vn = value_expr_check(p, node->nd_body);
10832 if (!vn) return NULL;
10833 if (!void_node) void_node = vn;
10834 node = node->nd_else;
10839 node = node->nd_1st;
10844 case NODE_DASGN_CURR:
10846 mark_lvar_used(p, node);
10858value_expr_gen(struct parser_params *p, NODE *node)
10860 NODE *void_node = value_expr_check(p, node);
10862 yyerror1(&void_node->nd_loc, "void value expression");
10863 /* or "control never reach"? */
10869void_expr(struct parser_params *p, NODE *node)
10871 const char *useless = 0;
10873 if (!RTEST(ruby_verbose)) return;
10875 if (!node || !(node = nd_once_body(node))) return;
10876 switch (nd_type(node)) {
10878 switch (node->nd_mid) {
10897 useless = rb_id2name(node->nd_mid);
10908 case NODE_BACK_REF:
10909 useless = "a variable";
10912 useless = "a constant";
10918 useless = "a literal";
10943 useless = "defined?";
10948 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10953void_stmts(struct parser_params *p, NODE *node)
10955 NODE *const n = node;
10956 if (!RTEST(ruby_verbose)) return n;
10957 if (!node) return n;
10958 if (nd_type(node) != NODE_BLOCK) return n;
10960 while (node->nd_next) {
10961 void_expr(p, node->nd_head);
10962 node = node->nd_next;
10968remove_begin(NODE *node)
10970 NODE **n = &node, *n1 = node;
10971 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10972 *n = n1 = n1->nd_body;
10978remove_begin_all(NODE *node)
10980 NODE **n = &node, *n1 = node;
10981 while (n1 && nd_type(n1) == NODE_BEGIN) {
10982 *n = n1 = n1->nd_body;
10988reduce_nodes(struct parser_params *p, NODE **body)
10990 NODE *node = *body;
10993 *body = NEW_NIL(&NULL_LOC);
10996#define subnodes(n1, n2) \
10997 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10998 (!node->n2) ? (body = &node->n1, 1) : \
10999 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11002 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11003 switch (nd_type(node)) {
11009 *body = node = node->nd_stts;
11010 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11013 *body = node = node->nd_body;
11014 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11017 body = &node->nd_end->nd_head;
11021 if (subnodes(nd_body, nd_else)) break;
11024 body = &node->nd_body;
11027 if (!subnodes(nd_body, nd_next)) goto end;
11030 if (!subnodes(nd_head, nd_resq)) goto end;
11033 if (node->nd_else) {
11034 body = &node->nd_resq;
11037 if (!subnodes(nd_head, nd_resq)) goto end;
11043 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11050is_static_content(NODE *node)
11052 if (!node) return 1;
11053 switch (nd_type(node)) {
11055 if (!(node = node->nd_head)) break;
11058 if (!is_static_content(node->nd_head)) return 0;
11059 } while ((node = node->nd_next) != 0);
11074assign_in_cond(struct parser_params *p, NODE *node)
11076 switch (nd_type(node)) {
11080 case NODE_DASGN_CURR:
11089 if (!node->nd_value) return 1;
11090 if (is_static_content(node->nd_value)) {
11091 /* reports always */
11092 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11103#define SWITCH_BY_COND_TYPE(t, w, arg) \
11105 case COND_IN_OP: break; \
11106 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11107 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11110static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11113range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11115 enum node_type type;
11117 if (node == 0) return 0;
11119 type = nd_type(node);
11121 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11122 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11123 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11125 return cond0(p, node, COND_IN_FF, loc);
11129cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11131 if (node == 0) return 0;
11132 if (!(node = nd_once_body(node))) return 0;
11133 assign_in_cond(p, node);
11135 switch (nd_type(node)) {
11139 SWITCH_BY_COND_TYPE(type, warn, "string ")
11143 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11145 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11149 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11150 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11155 node->nd_beg = range_op(p, node->nd_beg, loc);
11156 node->nd_end = range_op(p, node->nd_end, loc);
11157 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11158 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11162 SWITCH_BY_COND_TYPE(type, warning, "string ")
11166 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11167 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11168 nd_set_type(node, NODE_MATCH);
11170 else if (node->nd_lit == Qtrue ||
11171 node->nd_lit == Qfalse) {
11172 /* booleans are OK, e.g., while true */
11175 SWITCH_BY_COND_TYPE(type, warning, "")
11184cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11186 if (node == 0) return 0;
11187 return cond0(p, node, COND_IN_COND, loc);
11191method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11193 if (node == 0) return 0;
11194 return cond0(p, node, COND_IN_OP, loc);
11198new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11200 if (!cc) return right;
11201 cc = cond0(p, cc, COND_IN_COND, loc);
11202 return newline_node(NEW_IF(cc, left, right, loc));
11206new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11208 if (!cc) return right;
11209 cc = cond0(p, cc, COND_IN_COND, loc);
11210 return newline_node(NEW_UNLESS(cc, left, right, loc));
11214logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11215 const YYLTYPE *op_loc, const YYLTYPE *loc)
11217 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11220 if (left && (enum node_type)nd_type(left) == type) {
11221 NODE *node = left, *second;
11222 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11225 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11226 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11227 left->nd_loc.end_pos = loc->end_pos;
11230 op = NEW_NODE(type, left, right, 0, loc);
11231 nd_set_line(op, op_loc->beg_pos.lineno);
11236no_blockarg(struct parser_params *p, NODE *node)
11238 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11239 compile_error(p, "block argument should not be given");
11244ret_args(struct parser_params *p, NODE *node)
11247 no_blockarg(p, node);
11248 if (nd_type(node) == NODE_LIST) {
11249 if (node->nd_next == 0) {
11250 node = node->nd_head;
11253 nd_set_type(node, NODE_VALUES);
11261new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11263 if (node) no_blockarg(p, node);
11265 return NEW_YIELD(node, loc);
11269negate_lit(struct parser_params *p, VALUE lit)
11271 if (FIXNUM_P(lit)) {
11272 return LONG2FIX(-FIX2LONG(lit));
11274 if (SPECIAL_CONST_P(lit)) {
11276 if (FLONUM_P(lit)) {
11277 return DBL2NUM(-RFLOAT_VALUE(lit));
11282 switch (BUILTIN_TYPE(lit)) {
11284 BIGNUM_NEGATE(lit);
11285 lit = rb_big_norm(lit);
11288 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11291 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11292 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11295 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11299 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11300 rb_builtin_class_name(lit));
11307arg_blk_pass(NODE *node1, NODE *node2)
11310 if (!node1) return node2;
11311 node2->nd_head = node1;
11312 nd_set_first_lineno(node2, nd_first_lineno(node1));
11313 nd_set_first_column(node2, nd_first_column(node1));
11320args_info_empty_p(struct rb_args_info *args)
11322 if (args->pre_args_num) return false;
11323 if (args->post_args_num) return false;
11324 if (args->rest_arg) return false;
11325 if (args->opt_args) return false;
11326 if (args->block_arg) return false;
11327 if (args->kw_args) return false;
11328 if (args->kw_rest_arg) return false;
11333new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11335 int saved_line = p->ruby_sourceline;
11336 struct rb_args_info *args = tail->nd_ainfo;
11338 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11339 args->pre_init = pre_args ? pre_args->nd_next : 0;
11341 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11342 args->post_init = post_args ? post_args->nd_next : 0;
11343 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11345 args->rest_arg = rest_arg;
11347 args->opt_args = opt_args;
11349 args->ruby2_keywords = rest_arg == idFWD_REST;
11351 p->ruby_sourceline = saved_line;
11352 nd_set_loc(tail, loc);
11358new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11360 int saved_line = p->ruby_sourceline;
11362 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11363 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11364 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11365 args->imemo = tmpbuf;
11366 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11367 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11368 if (p->error_p) return node;
11370 args->block_arg = block;
11371 args->kw_args = kw_args;
11375 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11376 * variable order: k1, kr1, k2, &b, internal_id, krest
11378 * variable order: kr1, k1, k2, internal_id, krest, &b
11380 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11381 struct vtable *vtargs = p->lvtbl->args;
11382 NODE *kwn = kw_args;
11384 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11385 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11387 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11389 --required_kw_vars;
11390 kwn = kwn->nd_next;
11393 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11394 ID vid = kwn->nd_body->nd_vid;
11395 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11396 *required_kw_vars++ = vid;
11403 arg_var(p, kw_bits);
11404 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11405 if (block) arg_var(p, block);
11407 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11408 args->kw_rest_arg->nd_cflag = kw_bits;
11410 else if (kw_rest_arg == idNil) {
11411 args->no_kwarg = 1;
11413 else if (kw_rest_arg) {
11414 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11417 p->ruby_sourceline = saved_line;
11422args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11424 if (max_numparam > NO_PARAM) {
11426 YYLTYPE loc = RUBY_INIT_YYLLOC();
11427 args = new_args_tail(p, 0, 0, 0, 0);
11428 nd_set_loc(args, &loc);
11430 args->nd_ainfo->pre_args_num = max_numparam;
11436new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11438 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11440 aryptn->nd_pconst = constant;
11443 NODE *pre_args = NEW_LIST(pre_arg, loc);
11444 if (apinfo->pre_args) {
11445 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11448 apinfo->pre_args = pre_args;
11455new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11457 int saved_line = p->ruby_sourceline;
11459 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11460 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11461 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11462 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11463 apinfo->imemo = tmpbuf;
11464 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11466 apinfo->pre_args = pre_args;
11470 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11473 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11477 apinfo->rest_arg = NULL;
11480 apinfo->post_args = post_args;
11482 p->ruby_sourceline = saved_line;
11487new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11489 hshptn->nd_pconst = constant;
11494new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11496 int saved_line = p->ruby_sourceline;
11497 NODE *node, *kw_rest_arg_node;
11499 if (kw_rest_arg == idNil) {
11500 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11502 else if (kw_rest_arg) {
11503 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11506 kw_rest_arg_node = NULL;
11509 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11511 p->ruby_sourceline = saved_line;
11516new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11518 NODE *node = NEW_CASE3(val, pat, loc);
11520 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11521 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11526dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11531 return NEW_LIT(ID2SYM(idNULL), loc);
11534 switch (nd_type(node)) {
11536 nd_set_type(node, NODE_DSYM);
11537 nd_set_loc(node, loc);
11540 lit = node->nd_lit;
11541 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11542 nd_set_type(node, NODE_LIT);
11543 nd_set_loc(node, loc);
11546 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11553append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11555 NODE *node = (NODE *)v;
11556 NODE **result = (NODE **)h;
11558 node->nd_next->nd_end = node->nd_next;
11559 node->nd_next->nd_next = 0;
11561 list_concat(*result, node);
11564 return ST_CONTINUE;
11568remove_duplicate_keys(struct parser_params *p, NODE *hash)
11570 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11572 rb_code_location_t loc = hash->nd_loc;
11573 while (hash && hash->nd_head && hash->nd_next) {
11574 NODE *head = hash->nd_head;
11575 NODE *value = hash->nd_next;
11576 NODE *next = value->nd_next;
11577 VALUE key = (VALUE)head;
11579 if (nd_type(head) == NODE_LIT &&
11580 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11581 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11582 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11583 head->nd_lit, nd_line(head));
11584 head = ((NODE *)data)->nd_next;
11585 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11588 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11592 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11593 st_free_table(literal_keys);
11595 if (!result) result = hash;
11596 else list_concat(result, hash);
11598 result->nd_loc = loc;
11603new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11605 if (hash) hash = remove_duplicate_keys(p, hash);
11606 return NEW_HASH(hash, loc);
11611error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11613 if (is_private_local_id(id)) {
11616 if (st_is_member(p->pvtbl, id)) {
11617 yyerror1(loc, "duplicated variable name");
11620 st_insert(p->pvtbl, (st_data_t)id, 0);
11625error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11628 p->pktbl = st_init_numtable();
11630 else if (st_is_member(p->pktbl, key)) {
11631 yyerror1(loc, "duplicated key name");
11634 st_insert(p->pktbl, (st_data_t)key, 0);
11639new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11641 return NEW_HASH(hash, loc);
11643#endif /* !RIPPER */
11647new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11652 ID vid = lhs->nd_vid;
11653 YYLTYPE lhs_loc = lhs->nd_loc;
11655 lhs->nd_value = rhs;
11656 nd_set_loc(lhs, loc);
11657 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11658 if (is_notop_id(vid)) {
11659 switch (id_type(vid)) {
11663 asgn->nd_aid = vid;
11667 else if (op == tANDOP) {
11668 lhs->nd_value = rhs;
11669 nd_set_loc(lhs, loc);
11670 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11674 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11675 nd_set_loc(asgn, loc);
11679 asgn = NEW_BEGIN(0, loc);
11685new_ary_op_assign(struct parser_params *p, NODE *ary,
11686 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11690 args = make_list(args, args_loc);
11691 if (nd_type(args) == NODE_BLOCK_PASS) {
11692 args = NEW_ARGSCAT(args, rhs, loc);
11695 args = arg_concat(p, args, rhs, loc);
11697 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11703new_attr_op_assign(struct parser_params *p, NODE *lhs,
11704 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11708 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11714new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11719 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11722 asgn = NEW_BEGIN(0, loc);
11729const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11732 yyerror1(loc, "dynamic constant assignment");
11734 return NEW_CDECL(0, 0, (path), loc);
11738const_decl(struct parser_params *p, VALUE path)
11741 path = dispatch1(assign_error, path);
11748assign_error(struct parser_params *p, VALUE a)
11750 a = dispatch1(assign_error, a);
11756var_field(struct parser_params *p, VALUE a)
11758 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11764new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11766 NODE *result = head;
11768 NODE *tmp = rescue_else ? rescue_else : rescue;
11769 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11771 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11772 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11774 else if (rescue_else) {
11775 result = block_append(p, result, rescue_else);
11778 result = NEW_ENSURE(result, ensure, loc);
11780 fixpos(result, head);
11786warn_unused_var(struct parser_params *p, struct local_vars *local)
11790 if (!local->used) return;
11791 cnt = local->used->pos;
11792 if (cnt != local->vars->pos) {
11793 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11796 ID *v = local->vars->tbl;
11797 ID *u = local->used->tbl;
11798 for (int i = 0; i < cnt; ++i) {
11799 if (!v[i] || (u[i] & LVAR_USED)) continue;
11800 if (is_private_local_id(v[i])) continue;
11801 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11807local_push(struct parser_params *p, int toplevel_scope)
11809 struct local_vars *local;
11810 int inherits_dvars = toplevel_scope && compile_for_eval;
11811 int warn_unused_vars = RTEST(ruby_verbose);
11813 local = ALLOC(struct local_vars);
11814 local->prev = p->lvtbl;
11815 local->args = vtable_alloc(0);
11816 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11818 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11819 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11820 local->numparam.outer = 0;
11821 local->numparam.inner = 0;
11822 local->numparam.current = 0;
11824 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11826# if WARN_PAST_SCOPE
11835local_pop(struct parser_params *p)
11837 struct local_vars *local = p->lvtbl->prev;
11838 if (p->lvtbl->used) {
11839 warn_unused_var(p, p->lvtbl);
11840 vtable_free(p->lvtbl->used);
11842# if WARN_PAST_SCOPE
11843 while (p->lvtbl->past) {
11844 struct vtable *past = p->lvtbl->past;
11845 p->lvtbl->past = past->prev;
11849 vtable_free(p->lvtbl->args);
11850 vtable_free(p->lvtbl->vars);
11853 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11859local_tbl(struct parser_params *p)
11861 int cnt_args = vtable_size(p->lvtbl->args);
11862 int cnt_vars = vtable_size(p->lvtbl->vars);
11863 int cnt = cnt_args + cnt_vars;
11867 if (cnt <= 0) return 0;
11868 buf = ALLOC_N(ID, cnt + 2);
11869 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11870 /* remove IDs duplicated to warn shadowing */
11871 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11872 ID id = p->lvtbl->vars->tbl[i];
11873 if (!vtable_included(p->lvtbl->args, id)) {
11878 REALLOC_N(buf, ID, (cnt = j) + 2);
11881 rb_ast_add_local_table(p->ast, buf);
11887node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11893 n = NEW_NODE(type, a0, a1, a2, loc);
11900numparam_name(struct parser_params *p, ID id)
11902 if (!NUMPARAM_ID_P(id)) return;
11903 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11904 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11908arg_var(struct parser_params *p, ID id)
11910 numparam_name(p, id);
11911 vtable_add(p->lvtbl->args, id);
11915local_var(struct parser_params *p, ID id)
11917 numparam_name(p, id);
11918 vtable_add(p->lvtbl->vars, id);
11919 if (p->lvtbl->used) {
11920 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11925local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11927 struct vtable *vars, *args, *used;
11929 vars = p->lvtbl->vars;
11930 args = p->lvtbl->args;
11931 used = p->lvtbl->used;
11933 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11936 if (used) used = used->prev;
11939 if (vars && vars->prev == DVARS_INHERIT) {
11940 return rb_local_defined(id, p->parent_iseq);
11942 else if (vtable_included(args, id)) {
11946 int i = vtable_included(vars, id);
11947 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11953local_id(struct parser_params *p, ID id)
11955 return local_id_ref(p, id, NULL);
11959numparam_push(struct parser_params *p)
11962 struct local_vars *local = p->lvtbl;
11963 NODE *inner = local->numparam.inner;
11964 if (!local->numparam.outer) {
11965 local->numparam.outer = local->numparam.current;
11967 local->numparam.inner = 0;
11968 local->numparam.current = 0;
11976numparam_pop(struct parser_params *p, NODE *prev_inner)
11979 struct local_vars *local = p->lvtbl;
11981 /* prefer first one */
11982 local->numparam.inner = prev_inner;
11984 else if (local->numparam.current) {
11985 /* current and inner are exclusive */
11986 local->numparam.inner = local->numparam.current;
11988 if (p->max_numparam > NO_PARAM) {
11989 /* current and outer are exclusive */
11990 local->numparam.current = local->numparam.outer;
11991 local->numparam.outer = 0;
11994 /* no numbered parameter */
11995 local->numparam.current = 0;
12000static const struct vtable *
12001dyna_push(struct parser_params *p)
12003 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12004 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12005 if (p->lvtbl->used) {
12006 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12008 return p->lvtbl->args;
12012dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12014 struct vtable *tmp = *vtblp;
12015 *vtblp = tmp->prev;
12016# if WARN_PAST_SCOPE
12017 if (p->past_scope_enabled) {
12018 tmp->prev = p->lvtbl->past;
12019 p->lvtbl->past = tmp;
12027dyna_pop_1(struct parser_params *p)
12029 struct vtable *tmp;
12031 if ((tmp = p->lvtbl->used) != 0) {
12032 warn_unused_var(p, p->lvtbl);
12033 p->lvtbl->used = p->lvtbl->used->prev;
12036 dyna_pop_vtable(p, &p->lvtbl->args);
12037 dyna_pop_vtable(p, &p->lvtbl->vars);
12041dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12043 while (p->lvtbl->args != lvargs) {
12045 if (!p->lvtbl->args) {
12046 struct local_vars *local = p->lvtbl->prev;
12047 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12055dyna_in_block(struct parser_params *p)
12057 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12061dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12063 struct vtable *vars, *args, *used;
12066 args = p->lvtbl->args;
12067 vars = p->lvtbl->vars;
12068 used = p->lvtbl->used;
12070 while (!DVARS_TERMINAL_P(vars)) {
12071 if (vtable_included(args, id)) {
12074 if ((i = vtable_included(vars, id)) != 0) {
12075 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12080 if (!vidrefp) used = 0;
12081 if (used) used = used->prev;
12084 if (vars == DVARS_INHERIT) {
12085 return rb_dvar_defined(id, p->parent_iseq);
12092dvar_defined(struct parser_params *p, ID id)
12094 return dvar_defined_ref(p, id, NULL);
12098dvar_curr(struct parser_params *p, ID id)
12100 return (vtable_included(p->lvtbl->args, id) ||
12101 vtable_included(p->lvtbl->vars, id));
12105reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12108 "regexp encoding option '%c' differs from source encoding '%s'",
12109 c, rb_enc_name(rb_enc_get(str)));
12114rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12116 int c = RE_OPTION_ENCODING_IDX(options);
12120 rb_char_to_option_kcode(c, &opt, &idx);
12121 if (idx != ENCODING_GET(str) &&
12122 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12125 ENCODING_SET(str, idx);
12127 else if (RE_OPTION_ENCODING_NONE(options)) {
12128 if (!ENCODING_IS_ASCII8BIT(str) &&
12129 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12133 rb_enc_associate(str, rb_ascii8bit_encoding());
12135 else if (p->enc == rb_usascii_encoding()) {
12136 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12137 /* raise in re.c */
12138 rb_enc_associate(str, rb_usascii_encoding());
12141 rb_enc_associate(str, rb_ascii8bit_encoding());
12151reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12153 int c = rb_reg_fragment_setenc(p, str, options);
12154 if (c) reg_fragment_enc_error(p, str, c);
12158reg_fragment_check(struct parser_params* p, VALUE str, int options)
12161 reg_fragment_setenc(p, str, options);
12162 err = rb_reg_check_preprocess(str);
12164 err = rb_obj_as_string(err);
12165 compile_error(p, "%"PRIsVALUE, err);
12172 struct parser_params* parser;
12175 const YYLTYPE *loc;
12176} reg_named_capture_assign_t;
12179reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12180 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12182 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12183 struct parser_params* p = arg->parser;
12184 rb_encoding *enc = arg->enc;
12185 long len = name_end - name;
12186 const char *s = (const char *)name;
12190 if (!len) return ST_CONTINUE;
12191 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12192 return ST_CONTINUE;
12194 var = intern_cstr(s, len, enc);
12195 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12196 if (!lvar_defined(p, var)) return ST_CONTINUE;
12198 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12199 succ = arg->succ_block;
12200 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12201 succ = block_append(p, succ, node);
12202 arg->succ_block = succ;
12203 return ST_CONTINUE;
12207reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12209 reg_named_capture_assign_t arg;
12212 arg.enc = rb_enc_get(regexp);
12213 arg.succ_block = 0;
12215 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12217 if (!arg.succ_block) return 0;
12218 return arg.succ_block->nd_next;
12222parser_reg_compile(struct parser_params* p, VALUE str, int options)
12224 reg_fragment_setenc(p, str, options);
12225 return rb_parser_reg_compile(p, str, options);
12229rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12231 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12235reg_compile(struct parser_params* p, VALUE str, int options)
12240 err = rb_errinfo();
12241 re = parser_reg_compile(p, str, options);
12243 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12244 rb_set_errinfo(err);
12245 compile_error(p, "%"PRIsVALUE, m);
12252parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12254 VALUE err = rb_errinfo();
12256 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12257 int c = rb_reg_fragment_setenc(p, str, options);
12258 if (c) reg_fragment_enc_error(p, str, c);
12259 re = rb_parser_reg_compile(p, str, options);
12261 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12262 rb_set_errinfo(err);
12270rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12272 struct parser_params *p;
12273 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12274 p->do_print = print;
12276 p->do_chomp = chomp;
12277 p->do_split = split;
12281rb_parser_warn_location(VALUE vparser, int warn)
12283 struct parser_params *p;
12284 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12285 p->warn_location = warn;
12289parser_append_options(struct parser_params *p, NODE *node)
12291 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12292 const YYLTYPE *const LOC = &default_location;
12295 NODE *print = NEW_FCALL(rb_intern("print"),
12296 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12298 node = block_append(p, node, print);
12303 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12304 NODE *split = NEW_GASGN(rb_intern("$F"),
12305 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12306 rb_intern("split"), args, LOC),
12308 node = block_append(p, split, node);
12311 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12312 rb_intern("chomp!"), 0, LOC);
12313 node = block_append(p, chomp, node);
12316 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12325 /* just to suppress unused-function warnings */
12331internal_id(struct parser_params *p)
12333 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12334 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12336 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12338#endif /* !RIPPER */
12341parser_initialize(struct parser_params *p)
12343 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12344 p->command_start = TRUE;
12345 p->ruby_sourcefile_string = Qnil;
12346 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12349 p->delayed.token = Qnil;
12351 p->parsing_thread = Qnil;
12353 p->error_buffer = Qfalse;
12355 p->debug_buffer = Qnil;
12356 p->debug_output = rb_stdout;
12357 p->enc = rb_utf8_encoding();
12361#define parser_mark ripper_parser_mark
12362#define parser_free ripper_parser_free
12366parser_mark(void *ptr)
12368 struct parser_params *p = (struct parser_params*)ptr;
12370 rb_gc_mark(p->lex.input);
12371 rb_gc_mark(p->lex.prevline);
12372 rb_gc_mark(p->lex.lastline);
12373 rb_gc_mark(p->lex.nextline);
12374 rb_gc_mark(p->ruby_sourcefile_string);
12375 rb_gc_mark((VALUE)p->lex.strterm);
12376 rb_gc_mark((VALUE)p->ast);
12377 rb_gc_mark(p->case_labels);
12379 rb_gc_mark(p->debug_lines);
12380 rb_gc_mark(p->compile_option);
12381 rb_gc_mark(p->error_buffer);
12383 rb_gc_mark(p->delayed.token);
12384 rb_gc_mark(p->value);
12385 rb_gc_mark(p->result);
12386 rb_gc_mark(p->parsing_thread);
12388 rb_gc_mark(p->debug_buffer);
12389 rb_gc_mark(p->debug_output);
12391 rb_gc_mark((VALUE)p->heap);
12396parser_free(void *ptr)
12398 struct parser_params *p = (struct parser_params*)ptr;
12399 struct local_vars *local, *prev;
12402 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12404 for (local = p->lvtbl; local; local = prev) {
12405 if (local->vars) xfree(local->vars);
12406 prev = local->prev;
12410 token_info *ptinfo;
12411 while ((ptinfo = p->token_info) != 0) {
12412 p->token_info = ptinfo->next;
12420parser_memsize(const void *ptr)
12422 struct parser_params *p = (struct parser_params*)ptr;
12423 struct local_vars *local;
12424 size_t size = sizeof(*p);
12427 for (local = p->lvtbl; local; local = local->prev) {
12428 size += sizeof(*local);
12429 if (local->vars) size += local->vars->capa * sizeof(ID);
12434static const rb_data_type_t parser_data_type = {
12445 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12449#undef rb_reserved_word
12451const struct kwtable *
12452rb_reserved_word(const char *str, unsigned int len)
12454 return reserved_word(str, len);
12460 struct parser_params *p;
12461 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12462 &parser_data_type, p);
12463 parser_initialize(p);
12468rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12470 struct parser_params *p;
12472 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12473 p->error_buffer = main ? Qfalse : Qnil;
12474 p->parent_iseq = base;
12480#define rb_parser_end_seen_p ripper_parser_end_seen_p
12481#define rb_parser_encoding ripper_parser_encoding
12482#define rb_parser_get_yydebug ripper_parser_get_yydebug
12483#define rb_parser_set_yydebug ripper_parser_set_yydebug
12484#define rb_parser_get_debug_output ripper_parser_get_debug_output
12485#define rb_parser_set_debug_output ripper_parser_set_debug_output
12486static VALUE ripper_parser_end_seen_p(VALUE vparser);
12487static VALUE ripper_parser_encoding(VALUE vparser);
12488static VALUE ripper_parser_get_yydebug(VALUE self);
12489static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12490static VALUE ripper_parser_get_debug_output(VALUE self);
12491static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12495 * ripper.error? -> Boolean
12497 * Return true if parsed source has errors.
12500ripper_error_p(VALUE vparser)
12502 struct parser_params *p;
12504 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12505 return p->error_p ? Qtrue : Qfalse;
12511 * ripper.end_seen? -> Boolean
12513 * Return true if parsed source ended by +\_\_END\_\_+.
12516rb_parser_end_seen_p(VALUE vparser)
12518 struct parser_params *p;
12520 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12521 return p->ruby__end__seen ? Qtrue : Qfalse;
12526 * ripper.encoding -> encoding
12528 * Return encoding of the source.
12531rb_parser_encoding(VALUE vparser)
12533 struct parser_params *p;
12535 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12536 return rb_enc_from_encoding(p->enc);
12542 * ripper.yydebug -> true or false
12547rb_parser_get_yydebug(VALUE self)
12549 struct parser_params *p;
12551 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12552 return p->debug ? Qtrue : Qfalse;
12558 * ripper.yydebug = flag
12563rb_parser_set_yydebug(VALUE self, VALUE flag)
12565 struct parser_params *p;
12567 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12568 p->debug = RTEST(flag);
12574 * ripper.debug_output -> obj
12576 * Get debug output.
12579rb_parser_get_debug_output(VALUE self)
12581 struct parser_params *p;
12583 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12584 return p->debug_output;
12589 * ripper.debug_output = obj
12591 * Set debug output.
12594rb_parser_set_debug_output(VALUE self, VALUE output)
12596 struct parser_params *p;
12598 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12599 return p->debug_output = output;
12604#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12605/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12606 * potential memory leak */
12607#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12608#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12609 (new)->cnt = (cnt), (ptr))
12612rb_parser_malloc(struct parser_params *p, size_t size)
12614 size_t cnt = HEAPCNT(1, size);
12615 rb_imemo_tmpbuf_t *n = NEWHEAP();
12616 void *ptr = xmalloc(size);
12618 return ADD2HEAP(n, cnt, ptr);
12622rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12624 size_t cnt = HEAPCNT(nelem, size);
12625 rb_imemo_tmpbuf_t *n = NEWHEAP();
12626 void *ptr = xcalloc(nelem, size);
12628 return ADD2HEAP(n, cnt, ptr);
12632rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12634 rb_imemo_tmpbuf_t *n;
12635 size_t cnt = HEAPCNT(1, size);
12637 if (ptr && (n = p->heap) != NULL) {
12639 if (n->ptr == ptr) {
12640 n->ptr = ptr = xrealloc(ptr, size);
12641 if (n->cnt) n->cnt = cnt;
12644 } while ((n = n->next) != NULL);
12647 ptr = xrealloc(ptr, size);
12648 return ADD2HEAP(n, cnt, ptr);
12652rb_parser_free(struct parser_params *p, void *ptr)
12654 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12656 while ((n = *prev) != NULL) {
12657 if (n->ptr == ptr) {
12659 rb_gc_force_recycle((VALUE)n);
12669rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12672 VALUE mesg = p->debug_buffer;
12674 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12676 rb_str_vcatf(mesg, fmt, ap);
12678 if (RSTRING_END(mesg)[-1] == '\n') {
12679 rb_io_write(p->debug_output, mesg);
12680 p->debug_buffer = Qnil;
12685parser_compile_error(struct parser_params *p, const char *fmt, ...)
12689 rb_io_flush(p->debug_output);
12693 rb_syntax_error_append(p->error_buffer,
12694 p->ruby_sourcefile_string,
12695 p->ruby_sourceline,
12696 rb_long2int(p->lex.pcur - p->lex.pbeg),
12702count_char(const char *str, int c)
12705 while (str[n] == c) ++n;
12710 * strip enclosing double-quotes, same as the default yytnamerr except
12711 * for that single-quotes matching back-quotes do not stop stripping.
12713 * "\"`class' keyword\"" => "`class' keyword"
12715RUBY_FUNC_EXPORTED size_t
12716rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12718 if (*yystr == '"') {
12719 size_t yyn = 0, bquote = 0;
12720 const char *yyp = yystr;
12726 bquote = count_char(yyp+1, '`') + 1;
12727 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12735 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12736 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12742 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12743 if (yyres) memcpy(yyres + yyn, yyp, 3);
12748 goto do_not_strip_quotes;
12751 goto do_not_strip_quotes;
12754 if (*++yyp != '\\')
12755 goto do_not_strip_quotes;
12756 /* Fall through. */
12771 do_not_strip_quotes: ;
12774 if (!yyres) return strlen(yystr);
12776 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12784ripper_validate_object(VALUE self, VALUE x)
12786 if (x == Qfalse) return x;
12787 if (x == Qtrue) return x;
12788 if (x == Qnil) return x;
12790 rb_raise(rb_eArgError, "Qundef given");
12791 if (FIXNUM_P(x)) return x;
12792 if (SYMBOL_P(x)) return x;
12793 switch (BUILTIN_TYPE(x)) {
12803 if (nd_type((NODE *)x) != NODE_RIPPER) {
12804 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12806 x = ((NODE *)x)->nd_rval;
12809 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12810 (void *)x, rb_obj_classname(x));
12812 if (!RBASIC_CLASS(x)) {
12813 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12814 (void *)x, rb_builtin_type_name(TYPE(x)));
12820#define validate(x) ((x) = get_value(x))
12823ripper_dispatch0(struct parser_params *p, ID mid)
12825 return rb_funcall(p->value, mid, 0);
12829ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12832 return rb_funcall(p->value, mid, 1, a);
12836ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12840 return rb_funcall(p->value, mid, 2, a, b);
12844ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12849 return rb_funcall(p->value, mid, 3, a, b, c);
12853ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12859 return rb_funcall(p->value, mid, 4, a, b, c, d);
12863ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12870 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12874ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12883 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12887ripper_get_id(VALUE v)
12890 if (!RB_TYPE_P(v, T_NODE)) return 0;
12892 if (nd_type(nd) != NODE_RIPPER) return 0;
12897ripper_get_value(VALUE v)
12900 if (v == Qundef) return Qnil;
12901 if (!RB_TYPE_P(v, T_NODE)) return v;
12903 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12904 return nd->nd_rval;
12908ripper_error(struct parser_params *p)
12914ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12919 va_start(args, fmt);
12920 str = rb_vsprintf(fmt, args);
12922 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12927ripper_lex_get_generic(struct parser_params *p, VALUE src)
12929 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12930 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12931 rb_raise(rb_eTypeError,
12932 "gets returned %"PRIsVALUE" (expected String or nil)",
12933 rb_obj_class(line));
12939ripper_lex_io_get(struct parser_params *p, VALUE src)
12941 return rb_io_gets(src);
12945ripper_s_allocate(VALUE klass)
12947 struct parser_params *p;
12948 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12949 &parser_data_type, p);
12954#define ripper_initialized_p(r) ((r)->lex.input != 0)
12958 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12960 * Create a new Ripper object.
12961 * _src_ must be a String, an IO, or an Object which has #gets method.
12963 * This method does not starts parsing.
12964 * See also Ripper#parse and Ripper.parse.
12967ripper_initialize(int argc, VALUE *argv, VALUE self)
12969 struct parser_params *p;
12970 VALUE src, fname, lineno;
12972 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12973 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12974 if (RB_TYPE_P(src, T_FILE)) {
12975 p->lex.gets = ripper_lex_io_get;
12977 else if (rb_respond_to(src, id_gets)) {
12978 p->lex.gets = ripper_lex_get_generic;
12982 p->lex.gets = lex_get_str;
12984 p->lex.input = src;
12986 if (NIL_P(fname)) {
12987 fname = STR_NEW2("(ripper)");
12991 StringValueCStr(fname);
12992 fname = rb_str_new_frozen(fname);
12994 parser_initialize(p);
12996 p->ruby_sourcefile_string = fname;
12997 p->ruby_sourcefile = RSTRING_PTR(fname);
12998 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13004ripper_parse0(VALUE parser_v)
13006 struct parser_params *p;
13008 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13010 p->ast = rb_ast_new();
13011 ripper_yyparse((void*)p);
13012 rb_ast_dispose(p->ast);
13018ripper_ensure(VALUE parser_v)
13020 struct parser_params *p;
13022 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13023 p->parsing_thread = Qnil;
13031 * Start parsing and returns the value of the root action.
13034ripper_parse(VALUE self)
13036 struct parser_params *p;
13038 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13039 if (!ripper_initialized_p(p)) {
13040 rb_raise(rb_eArgError, "method called for uninitialized object");
13042 if (!NIL_P(p->parsing_thread)) {
13043 if (p->parsing_thread == rb_thread_current())
13044 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13046 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13048 p->parsing_thread = rb_thread_current();
13049 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13056 * ripper.column -> Integer
13058 * Return column number of current parsing line.
13059 * This number starts from 0.
13062ripper_column(VALUE self)
13064 struct parser_params *p;
13067 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13068 if (!ripper_initialized_p(p)) {
13069 rb_raise(rb_eArgError, "method called for uninitialized object");
13071 if (NIL_P(p->parsing_thread)) return Qnil;
13072 col = p->lex.ptok - p->lex.pbeg;
13073 return LONG2NUM(col);
13078 * ripper.filename -> String
13080 * Return current parsing filename.
13083ripper_filename(VALUE self)
13085 struct parser_params *p;
13087 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13088 if (!ripper_initialized_p(p)) {
13089 rb_raise(rb_eArgError, "method called for uninitialized object");
13091 return p->ruby_sourcefile_string;
13096 * ripper.lineno -> Integer
13098 * Return line number of current parsing line.
13099 * This number starts from 1.
13102ripper_lineno(VALUE self)
13104 struct parser_params *p;
13106 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13107 if (!ripper_initialized_p(p)) {
13108 rb_raise(rb_eArgError, "method called for uninitialized object");
13110 if (NIL_P(p->parsing_thread)) return Qnil;
13111 return INT2NUM(p->ruby_sourceline);
13116 * ripper.state -> Integer
13118 * Return scanner state of current token.
13121ripper_state(VALUE self)
13123 struct parser_params *p;
13125 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13126 if (!ripper_initialized_p(p)) {
13127 rb_raise(rb_eArgError, "method called for uninitialized object");
13129 if (NIL_P(p->parsing_thread)) return Qnil;
13130 return INT2NUM(p->lex.state);
13135 * ripper.token -> String
13137 * Return the current token string.
13140ripper_token(VALUE self)
13142 struct parser_params *p;
13145 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13146 if (!ripper_initialized_p(p)) {
13147 rb_raise(rb_eArgError, "method called for uninitialized object");
13149 if (NIL_P(p->parsing_thread)) return Qnil;
13150 pos = p->lex.ptok - p->lex.pbeg;
13151 len = p->lex.pcur - p->lex.ptok;
13152 return rb_str_subseq(p->lex.lastline, pos, len);
13158ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13161 if (obj == Qundef) {
13162 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13169ripper_value(VALUE self, VALUE obj)
13171 return ULONG2NUM(obj);
13177 * Ripper.lex_state_name(integer) -> string
13179 * Returns a string representation of lex_state.
13182ripper_lex_state_name(VALUE self, VALUE state)
13184 return rb_parser_lex_state_name(NUM2INT(state));
13190 ripper_init_eventids1();
13191 ripper_init_eventids2();
13192 id_warn = rb_intern_const("warn");
13193 id_warning = rb_intern_const("warning");
13194 id_gets = rb_intern_const("gets");
13195 id_assoc = rb_intern_const("=>");
13197 (void)yystpcpy; /* may not used in newer bison */
13207 Ripper = rb_define_class("Ripper", rb_cObject);
13208 /* version of Ripper */
13209 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13210 rb_define_alloc_func(Ripper, ripper_s_allocate);
13211 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13212 rb_define_method(Ripper, "parse", ripper_parse, 0);
13213 rb_define_method(Ripper, "column", ripper_column, 0);
13214 rb_define_method(Ripper, "filename", ripper_filename, 0);
13215 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13216 rb_define_method(Ripper, "state", ripper_state, 0);
13217 rb_define_method(Ripper, "token", ripper_token, 0);
13218 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13219 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13220 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13221 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13222 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13223 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13224 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13226 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13227 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13228 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13231 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13232 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13234 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13236 /* ignore newline, +/- is a sign. */
13237 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
13238 /* newline significant, +/- is an operator. */
13239 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
13240 /* ditto, and unbound braces. */
13241 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
13242 /* ditto, and unbound braces. */
13243 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
13244 /* newline significant, +/- is an operator. */
13245 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
13246 /* newline significant, +/- is an operator. */
13247 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
13248 /* newline significant, +/- is an operator. */
13249 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
13250 /* ignore newline, no reserved words. */
13251 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
13252 /* right after `.' or `::', no reserved words. */
13253 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
13254 /* immediate after `class', no here document. */
13255 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
13256 /* flag bit, label is allowed. */
13257 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
13258 /* flag bit, just after a label. */
13259 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
13260 /* symbol literal as FNAME. */
13261 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
13262 /* equals to +EXPR_BEG+ */
13263 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
13264 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
13265 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
13266 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
13267 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
13268 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
13269 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
13270 /* equals to +0+ */
13271 rb_define_const(Ripper, "EXPR_NONE", INT2NUM(EXPR_NONE));
13273 ripper_init_eventids1_table(Ripper);
13274 ripper_init_eventids2_table(Ripper);
13277 /* Hack to let RDoc document SCRIPT_LINES__ */
13280 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13281 * after the assignment will be added as an Array of lines with the file
13284 rb_define_global_const("SCRIPT_LINES__", Qnil);
13293 * c-file-style: "ruby"