1/**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10**********************************************************************/
15# error needs pure parser
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20#define YYLTYPE rb_code_location_t
21#define YYLTYPE_IS_DECLARED 1
23#include "ruby/internal/config.h"
32#include "internal/compile.h"
33#include "internal/compilers.h"
34#include "internal/complex.h"
35#include "internal/error.h"
36#include "internal/hash.h"
37#include "internal/imemo.h"
38#include "internal/io.h"
39#include "internal/numeric.h"
40#include "internal/parse.h"
41#include "internal/rational.h"
42#include "internal/re.h"
43#include "internal/symbol.h"
44#include "internal/thread.h"
45#include "internal/variable.h"
49#include "ruby/encoding.h"
50#include "ruby/regex.h"
54#include "ruby/ractor.h"
65 unsigned int in_defined: 1;
66 unsigned int in_kwarg: 1;
67 unsigned int in_argdef: 1;
68 unsigned int in_def: 1;
69 unsigned int in_class: 1;
70 BITFIELD(enum shareability, shareable_constant_value, 2);
75#define NO_LEX_CTXT (struct lex_context){0}
77#define AREF(ary, i) RARRAY_AREF(ary, i)
79#ifndef WARN_PAST_SCOPE
80# define WARN_PAST_SCOPE 0
85#define yydebug (p->debug) /* disable the global variable definition */
87#define YYMALLOC(size) rb_parser_malloc(p, (size))
88#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
89#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
90#define YYFREE(ptr) rb_parser_free(p, (ptr))
91#define YYFPRINTF rb_parser_printf
92#define YY_LOCATION_PRINT(File, loc) \
93 rb_parser_printf(p, "%d.%d-%d.%d", \
94 (loc).beg_pos.lineno, (loc).beg_pos.column,\
95 (loc).end_pos.lineno, (loc).end_pos.column)
96#define YYLLOC_DEFAULT(Current, Rhs, N) \
100 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
101 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
105 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
106 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
110 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
111 "nesting too deep" : (Msgid))
113#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
114 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
115#define RUBY_SET_YYLLOC_OF_NONE(Current) \
116 rb_parser_set_location_of_none(p, &(Current))
117#define RUBY_SET_YYLLOC(Current) \
118 rb_parser_set_location(p, &(Current))
119#define RUBY_INIT_YYLLOC() \
121 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
122 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
126 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
127 EXPR_END_bit, /* newline significant, +/- is an operator. */
128 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
129 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
130 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
131 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
132 EXPR_MID_bit, /* newline significant, +/- is an operator. */
133 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
134 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
135 EXPR_CLASS_bit, /* immediate after `class', no here document. */
136 EXPR_LABEL_bit, /* flag bit, label is allowed. */
137 EXPR_LABELED_bit, /* flag bit, just after a label. */
138 EXPR_FITEM_bit, /* symbol literal as FNAME. */
141/* examine combinations */
143#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
157 EXPR_VALUE = EXPR_BEG,
158 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
159 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
160 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
163#define IS_lex_state_for(x, ls) ((x) & (ls))
164#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
165#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
166#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
168# define SET_LEX_STATE(ls) \
169 parser_set_lex_state(p, ls, __LINE__)
170static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
172typedef VALUE stack_type;
174static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
176# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
177# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
178# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
179# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
180# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
182/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
183 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
184#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
185#define COND_POP() BITSTACK_POP(cond_stack)
186#define COND_P() BITSTACK_SET_P(cond_stack)
187#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
189/* A flag to identify keyword_do_block; "do" keyword after command_call.
190 Example: `foo 1, 2 do`. */
191#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
192#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
193#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
194#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
210 struct local_vars *prev;
213 NODE *outer, *inner, *current;
224#define NUMPARAM_ID_P(id) numparam_id_p(id)
225#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
226#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
230 if (!is_local_id(id)) return 0;
231 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
232 return idx > 0 && idx <= NUMPARAM_MAX;
234static void numparam_name(struct parser_params *p, ID id);
236#define DVARS_INHERIT ((void*)1)
237#define DVARS_TOPSCOPE NULL
238#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
240typedef struct token_info {
242 rb_code_position_t beg;
245 struct token_info *next;
248typedef struct rb_strterm_struct rb_strterm_t;
251 Structure of Lexer Buffer:
253 lex.pbeg lex.ptok lex.pcur lex.pend
255 |------------+------------+------------|
259struct parser_params {
260 rb_imemo_tmpbuf_t *heap;
265 rb_strterm_t *strterm;
266 VALUE (*gets)(struct parser_params*,VALUE);
277 VALUE (*call)(VALUE, int);
279 enum lex_state_e state;
280 /* track the nest level of any parens "()[]{}" */
282 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
284 /* track the nest level of only braces "{}" */
287 stack_type cond_stack;
288 stack_type cmdarg_stack;
294 int heredoc_line_indent;
296 struct local_vars *lvtbl;
300 int ruby_sourceline; /* current line no. */
301 const char *ruby_sourcefile; /* current source file */
302 VALUE ruby_sourcefile_string;
304 token_info *token_info;
306 VALUE compile_option;
318 struct lex_context ctxt;
320 unsigned int command_start:1;
321 unsigned int eofp: 1;
322 unsigned int ruby__end__seen: 1;
323 unsigned int debug: 1;
324 unsigned int has_shebang: 1;
325 unsigned int token_seen: 1;
326 unsigned int token_info_enabled: 1;
328 unsigned int past_scope_enabled: 1;
330 unsigned int error_p: 1;
331 unsigned int cr_seen: 1;
336 unsigned int do_print: 1;
337 unsigned int do_loop: 1;
338 unsigned int do_chomp: 1;
339 unsigned int do_split: 1;
340 unsigned int keep_script_lines: 1;
342 NODE *eval_tree_begin;
346 const struct rb_iseq_struct *parent_iseq;
358 VALUE parsing_thread;
362#define intern_cstr(n,l,en) rb_intern3(n,l,en)
364#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
365#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
366#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
367#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
368#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
371push_pvtbl(struct parser_params *p)
373 st_table *tbl = p->pvtbl;
374 p->pvtbl = st_init_numtable();
379pop_pvtbl(struct parser_params *p, st_table *tbl)
381 st_free_table(p->pvtbl);
386push_pktbl(struct parser_params *p)
388 st_table *tbl = p->pktbl;
394pop_pktbl(struct parser_params *p, st_table *tbl)
396 if (p->pktbl) st_free_table(p->pktbl);
400RBIMPL_ATTR_NONNULL((1, 2, 3))
401static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
402RBIMPL_ATTR_NONNULL((1, 2))
403static int parser_yyerror0(struct parser_params*, const char*);
404#define yyerror0(msg) parser_yyerror0(p, (msg))
405#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
406#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
407#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
409static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
410static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
411static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
412static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
413static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
416#define compile_for_eval (0)
418#define compile_for_eval (p->parent_iseq != 0)
421#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
423#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
424#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
425#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
427#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
429#define ANON_BLOCK_ID '&'
431static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
435rb_discard_node(struct parser_params *p, NODE *n)
437 rb_ast_delete_node(p->ast, n);
443add_mark_object(struct parser_params *p, VALUE obj)
445 if (!SPECIAL_CONST_P(obj)
446 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
448 rb_ast_add_mark_object(p->ast, obj);
453static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
456static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
457#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
459static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
462parser_get_node_id(struct parser_params *p)
464 int node_id = p->node_id;
471set_line_body(NODE *body, int line)
474 switch (nd_type(body)) {
477 nd_set_line(body, line);
481#define yyparse ruby_yyparse
483static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
484static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
485#define new_nil(loc) NEW_NIL(loc)
486static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
487static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
488static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
489static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
491static NODE *newline_node(NODE*);
492static void fixpos(NODE*,NODE*);
494static int value_expr_gen(struct parser_params*,NODE*);
495static void void_expr(struct parser_params*,NODE*);
496static NODE *remove_begin(NODE*);
497static NODE *remove_begin_all(NODE*);
498#define value_expr(node) value_expr_gen(p, (node))
499static NODE *void_stmts(struct parser_params*,NODE*);
500static void reduce_nodes(struct parser_params*,NODE**);
501static void block_dup_check(struct parser_params*,NODE*,NODE*);
503static NODE *block_append(struct parser_params*,NODE*,NODE*);
504static NODE *list_append(struct parser_params*,NODE*,NODE*);
505static NODE *list_concat(NODE*,NODE*);
506static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
507static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
508static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
509static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
510static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
511static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
512static NODE *evstr2dstr(struct parser_params*,NODE*);
513static NODE *splat_array(NODE*);
514static void mark_lvar_used(struct parser_params *p, NODE *rhs);
516static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
517static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
518static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
519static 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);
520static 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;}
522static bool args_info_empty_p(struct rb_args_info *args);
523static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
524static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
525static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
526static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
527static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
528static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
529static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
530static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
532static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
533static NODE *args_with_numbered(struct parser_params*,NODE*,int);
535static VALUE negate_lit(struct parser_params*, VALUE);
536static NODE *ret_args(struct parser_params*,NODE*);
537static NODE *arg_blk_pass(NODE*,NODE*);
538static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
539static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
541static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
542static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
544static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
545static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
547static void rb_backref_error(struct parser_params*,NODE*);
548static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
550static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
551static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
552static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
553static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
554static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
556static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
558static NODE *opt_arg_append(NODE*, NODE*);
559static NODE *kwd_append(NODE*, NODE*);
561static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
562static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
564static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
566static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
568#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
570static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
572static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
574static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
576static rb_ast_id_table_t *local_tbl(struct parser_params*);
578static VALUE reg_compile(struct parser_params*, VALUE, int);
579static void reg_fragment_setenc(struct parser_params*, VALUE, int);
580static int reg_fragment_check(struct parser_params*, VALUE, int);
581static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
583static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
584static NODE *heredoc_dedent(struct parser_params*,NODE*);
586static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
588#define get_id(id) (id)
589#define get_value(val) (val)
590#define get_num(num) (num)
592#define NODE_RIPPER NODE_CDECL
593#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
595static inline int ripper_is_node_yylval(VALUE n);
598ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
600 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
601 add_mark_object(p, b);
602 add_mark_object(p, c);
603 return NEW_RIPPER(a, b, c, &NULL_LOC);
607ripper_is_node_yylval(VALUE n)
609 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
612#define value_expr(node) ((void)(node))
613#define remove_begin(node) (node)
614#define void_stmts(p,x) (x)
615#define rb_dvar_defined(id, base) 0
616#define rb_local_defined(id, base) 0
617static ID ripper_get_id(VALUE);
618#define get_id(id) ripper_get_id(id)
619static VALUE ripper_get_value(VALUE);
620#define get_value(val) ripper_get_value(val)
621#define get_num(num) (int)get_id(num)
622static VALUE assignable(struct parser_params*,VALUE);
623static int id_is_var(struct parser_params *p, ID id);
625#define method_cond(p,node,loc) (node)
626#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
627#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
628#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
629#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
631#define new_nil(loc) Qnil
633static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
635static VALUE const_decl(struct parser_params *p, VALUE path);
637static VALUE var_field(struct parser_params *p, VALUE a);
638static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
640static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
642static VALUE backref_error(struct parser_params*, NODE *, VALUE);
645/* forward declaration */
646typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
648RUBY_SYMBOL_EXPORT_BEGIN
649VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
650int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
651enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
652VALUE rb_parser_lex_state_name(enum lex_state_e state);
653void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
654PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
655YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
656YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
657YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
658RUBY_SYMBOL_EXPORT_END
660static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
661static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
663static ID formal_argument(struct parser_params*, ID);
665static ID formal_argument(struct parser_params*, VALUE);
667static ID shadowing_lvar(struct parser_params*,ID);
668static void new_bv(struct parser_params*,ID);
670static void local_push(struct parser_params*,int);
671static void local_pop(struct parser_params*);
672static void local_var(struct parser_params*, ID);
673static void arg_var(struct parser_params*, ID);
674static int local_id(struct parser_params *p, ID id);
675static int local_id_ref(struct parser_params*, ID, ID **);
677static ID internal_id(struct parser_params*);
678static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
680static int check_forwarding_args(struct parser_params*);
681static void add_forwarding_args(struct parser_params *p);
683static const struct vtable *dyna_push(struct parser_params *);
684static void dyna_pop(struct parser_params*, const struct vtable *);
685static int dyna_in_block(struct parser_params*);
686#define dyna_var(p, id) local_var(p, id)
687static int dvar_defined(struct parser_params*, ID);
688static int dvar_defined_ref(struct parser_params*, ID, ID**);
689static int dvar_curr(struct parser_params*,ID);
691static int lvar_defined(struct parser_params*, ID);
693static NODE *numparam_push(struct parser_params *p);
694static void numparam_pop(struct parser_params *p, NODE *prev_inner);
697# define METHOD_NOT idNOT
699# define METHOD_NOT '!'
702#define idFWD_REST '*'
704#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
706#define idFWD_KWREST 0
708#define idFWD_BLOCK '&'
710#define RE_OPTION_ONCE (1<<16)
711#define RE_OPTION_ENCODING_SHIFT 8
712#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
713#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
714#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
715#define RE_OPTION_MASK 0xff
716#define RE_OPTION_ARG_ENCODING_NONE 32
718/* structs for managing terminator of string literal and heredocment */
719typedef struct rb_strterm_literal_struct {
726 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
730 long paren; /* '(' of `%q(...)` */
734 long term; /* ')' of `%q(...)` */
736} rb_strterm_literal_t;
738#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
740struct rb_strterm_heredoc_struct {
741 VALUE lastline; /* the string of line that contains `<<"END"` */
742 long offset; /* the column of END in `<<"END"` */
743 int sourceline; /* lineno of the line that contains `<<"END"` */
744 unsigned length /* the length of END in `<<"END"` */
745#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
746 : HERETERM_LENGTH_BITS
747# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
749# define HERETERM_LENGTH_MAX UINT_MAX
752#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
760STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
762#define STRTERM_HEREDOC IMEMO_FL_USER0
764struct rb_strterm_struct {
767 rb_strterm_literal_t literal;
768 rb_strterm_heredoc_t heredoc;
774rb_strterm_mark(VALUE obj)
776 rb_strterm_t *strterm = (rb_strterm_t*)obj;
777 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
778 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
779 rb_gc_mark(heredoc->lastline);
784#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
785size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
787#define TOKEN2ID(tok) ( \
788 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
789 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
790 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
791 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
792 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
793 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
794 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
796/****** Ripper *******/
799#define RIPPER_VERSION "0.1.0"
801static inline VALUE intern_sym(const char *name);
803#include "eventids1.c"
804#include "eventids2.c"
806static VALUE ripper_dispatch0(struct parser_params*,ID);
807static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
808static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
809static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
810static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
811static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
812static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
813static void ripper_error(struct parser_params *p);
815#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
816#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
817#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
818#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
819#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
820#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
821#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))
823#define yyparse ripper_yyparse
825#define ID2VAL(id) STATIC_ID2SYM(id)
826#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
827#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
829#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
830 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
832#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
835new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
837 NODE *t = (NODE *)tail;
838 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
839 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
843new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
845 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
846 add_mark_object(p, kw_args);
847 add_mark_object(p, kw_rest_arg);
848 add_mark_object(p, block);
853args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
859new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
861 NODE *t = (NODE *)aryptn;
862 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
864 if (!NIL_P(pre_arg)) {
865 if (!NIL_P(pre_args)) {
866 rb_ary_unshift(pre_args, pre_arg);
869 pre_args = rb_ary_new_from_args(1, pre_arg);
872 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
876new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
881 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
887 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
888 add_mark_object(p, pre_args);
889 add_mark_object(p, rest_arg);
890 add_mark_object(p, post_args);
895new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
897 NODE *t = (NODE *)fndptn;
898 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
900 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
904new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
908 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
909 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
911 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
912 add_mark_object(p, pre_rest_arg);
913 add_mark_object(p, args);
914 add_mark_object(p, post_rest_arg);
918#define new_hash(p,h,l) rb_ary_new_from_args(0)
921new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
927new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
929 NODE *t = (NODE *)hshptn;
930 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
931 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
935new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
939 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
944 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
946 add_mark_object(p, kw_args);
947 add_mark_object(p, kw_rest_arg);
951#define new_defined(p,expr,loc) dispatch1(defined, (expr))
953static VALUE heredoc_dedent(struct parser_params*,VALUE);
956#define ID2VAL(id) (id)
957#define TOKEN2VAL(t) ID2VAL(t)
958#define KWD2EID(t, v) keyword_##t
961set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
963 body = remove_begin(body);
964 reduce_nodes(p, &body);
965 n->nd_defn = NEW_SCOPE(args, body, loc);
967 nd_set_line(n->nd_defn, loc->end_pos.lineno);
968 set_line_body(body, loc->beg_pos.lineno);
973rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
974 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
976 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
977 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
978 loc.beg_pos = arg_loc->beg_pos;
979 return NEW_RESCUE(arg, rescue, 0, &loc);
985restore_defun(struct parser_params *p, NODE *name)
987 YYSTYPE c = {.val = name->nd_cval};
988 p->cur_arg = name->nd_vid;
989 p->ctxt.in_def = c.ctxt.in_def;
990 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
994endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
997 defn = defn->nd_defn;
999 ID mid = defn->nd_mid;
1000 if (is_attrset_id(mid)) {
1001 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1003 token_info_drop(p, "def", loc->beg_pos);
1009# define ifndef_ripper(x) (x)
1012# define Qnull Qundef
1013# define ifndef_ripper(x)
1016# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1017# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1018# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1019# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1020# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1021# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1022# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1023# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1024# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1025# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1026# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1027# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1028# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1029# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1030# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1031# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1032# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1033# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1034# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1035# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1037static ID id_warn, id_warning, id_gets, id_assoc;
1038# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1039# define WARN_S_L(s,l) STR_NEW(s,l)
1040# define WARN_S(s) STR_NEW2(s)
1041# define WARN_I(i) INT2NUM(i)
1042# define WARN_ID(i) rb_id2str(i)
1043# define WARN_IVAL(i) i
1044# define PRIsWARN "s"
1045# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1046# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1047# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1048# ifdef HAVE_VA_ARGS_MACRO
1049# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1051# define WARN_CALL rb_funcall
1053# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1054# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1055# ifdef HAVE_VA_ARGS_MACRO
1056# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1058# define WARNING_CALL rb_funcall
1060PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1061# define compile_error ripper_compile_error
1063# define WARN_S_L(s,l) s
1066# define WARN_ID(i) rb_id2name(i)
1067# define WARN_IVAL(i) NUM2INT(i)
1068# define PRIsWARN PRIsVALUE
1069# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1070# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1071# define WARN_CALL rb_compile_warn
1072# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1073# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1074# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1075# define WARNING_CALL rb_compile_warning
1076PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1077# define compile_error parser_compile_error
1080#define WARN_EOL(tok) \
1081 (looking_at_eol_p(p) ? \
1082 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1084static int looking_at_eol_p(struct parser_params *p);
1089%define parse.error verbose
1092 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1094 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1096} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1099 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1101 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1103} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1106 rb_parser_printf(p, "$%ld", $$->nd_nth);
1108 rb_parser_printf(p, "%"PRIsVALUE, $$);
1113 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1115 rb_parser_printf(p, "%"PRIsVALUE, $$);
1119%lex-param {struct parser_params *p}
1120%parse-param {struct parser_params *p}
1123 RUBY_SET_YYLLOC_OF_NONE(@$);
1132 const struct vtable *vars;
1133 struct rb_strterm_struct *strterm;
1134 struct lex_context ctxt;
1138 keyword_class "`class'"
1139 keyword_module "`module'"
1141 keyword_undef "`undef'"
1142 keyword_begin "`begin'"
1143 keyword_rescue "`rescue'"
1144 keyword_ensure "`ensure'"
1147 keyword_unless "`unless'"
1148 keyword_then "`then'"
1149 keyword_elsif "`elsif'"
1150 keyword_else "`else'"
1151 keyword_case "`case'"
1152 keyword_when "`when'"
1153 keyword_while "`while'"
1154 keyword_until "`until'"
1156 keyword_break "`break'"
1157 keyword_next "`next'"
1158 keyword_redo "`redo'"
1159 keyword_retry "`retry'"
1162 keyword_do_cond "`do' for condition"
1163 keyword_do_block "`do' for block"
1164 keyword_do_LAMBDA "`do' for lambda"
1165 keyword_return "`return'"
1166 keyword_yield "`yield'"
1167 keyword_super "`super'"
1168 keyword_self "`self'"
1170 keyword_true "`true'"
1171 keyword_false "`false'"
1175 modifier_if "`if' modifier"
1176 modifier_unless "`unless' modifier"
1177 modifier_while "`while' modifier"
1178 modifier_until "`until' modifier"
1179 modifier_rescue "`rescue' modifier"
1180 keyword_alias "`alias'"
1181 keyword_defined "`defined?'"
1182 keyword_BEGIN "`BEGIN'"
1184 keyword__LINE__ "`__LINE__'"
1185 keyword__FILE__ "`__FILE__'"
1186 keyword__ENCODING__ "`__ENCODING__'"
1188%token <id> tIDENTIFIER "local variable or method"
1189%token <id> tFID "method"
1190%token <id> tGVAR "global variable"
1191%token <id> tIVAR "instance variable"
1192%token <id> tCONSTANT "constant"
1193%token <id> tCVAR "class variable"
1194%token <id> tLABEL "label"
1195%token <node> tINTEGER "integer literal"
1196%token <node> tFLOAT "float literal"
1197%token <node> tRATIONAL "rational literal"
1198%token <node> tIMAGINARY "imaginary literal"
1199%token <node> tCHAR "char literal"
1200%token <node> tNTH_REF "numbered reference"
1201%token <node> tBACK_REF "back reference"
1202%token <node> tSTRING_CONTENT "literal content"
1203%token <num> tREGEXP_END
1205%type <node> singleton strings string string1 xstring regexp
1206%type <node> string_contents xstring_contents regexp_contents string_content
1207%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1208%type <node> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1209%type <node> top_compstmt top_stmts top_stmt begin_block
1210%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1211%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1212%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1213%type <node> args call_args opt_call_args
1214%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1215%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1216%type <node> command_rhs arg_rhs
1217%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1218%type <node> f_block_optarg f_block_opt
1219%type <node> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1220%type <node> f_optarg f_marg f_marg_list f_margs f_rest_marg
1221%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1222%type <node> block_param opt_block_param block_param_def f_opt
1223%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1224%type <node> bv_decls opt_bv_decl bvar
1225%type <node> lambda f_larglist lambda_body brace_body do_body
1226%type <node> brace_block cmd_brace_block do_block lhs none fitem
1227%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1228%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1229%type <node> p_expr p_as p_alt p_expr_basic p_find
1230%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1231%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
1232%type <node> p_kwargs p_kwarg p_kw
1233%type <id> keyword_variable user_variable sym operation operation2 operation3
1234%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1235%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1236%type <id> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1237%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var
1238 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1239%token END_OF_INPUT 0 "end-of-input"
1241/* escaped chars, should be ignored otherwise */
1242%token <id> '\\' "backslash"
1243%token tSP "escaped space"
1244%token <id> '\t' "escaped horizontal tab"
1245%token <id> '\f' "escaped form feed"
1246%token <id> '\r' "escaped carriage return"
1247%token <id> '\13' "escaped vertical tab"
1248%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1249%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1250%token tPOW RUBY_TOKEN(POW) "**"
1251%token tCMP RUBY_TOKEN(CMP) "<=>"
1252%token tEQ RUBY_TOKEN(EQ) "=="
1253%token tEQQ RUBY_TOKEN(EQQ) "==="
1254%token tNEQ RUBY_TOKEN(NEQ) "!="
1255%token tGEQ RUBY_TOKEN(GEQ) ">="
1256%token tLEQ RUBY_TOKEN(LEQ) "<="
1257%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1258%token tOROP RUBY_TOKEN(OROP) "||"
1259%token tMATCH RUBY_TOKEN(MATCH) "=~"
1260%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1261%token tDOT2 RUBY_TOKEN(DOT2) ".."
1262%token tDOT3 RUBY_TOKEN(DOT3) "..."
1263%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1264%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1265%token tAREF RUBY_TOKEN(AREF) "[]"
1266%token tASET RUBY_TOKEN(ASET) "[]="
1267%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1268%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1269%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1270%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1271%token tCOLON3 ":: at EXPR_BEG"
1272%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1275%token tLPAREN_ARG "( arg"
1279%token tLBRACE_ARG "{ arg"
1281%token tDSTAR "**arg"
1284%token tSYMBEG "symbol literal"
1285%token tSTRING_BEG "string literal"
1286%token tXSTRING_BEG "backtick literal"
1287%token tREGEXP_BEG "regexp literal"
1288%token tWORDS_BEG "word list"
1289%token tQWORDS_BEG "verbatim word list"
1290%token tSYMBOLS_BEG "symbol list"
1291%token tQSYMBOLS_BEG "verbatim symbol list"
1292%token tSTRING_END "terminator"
1293%token tSTRING_DEND "'}'"
1294%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1301%nonassoc tLBRACE_ARG
1303%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1304%left keyword_or keyword_and
1306%nonassoc keyword_defined
1308%left modifier_rescue
1310%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1313%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1314%left '>' tGEQ '<' tLEQ
1320%right tUMINUS_NUM tUMINUS
1322%right '!' '~' tUPLUS
1328 SET_LEX_STATE(EXPR_BEG);
1329 local_push(p, ifndef_ripper(1)+0);
1334 if ($2 && !compile_for_eval) {
1336 /* last expression should not be void */
1337 if (nd_type_p(node, NODE_BLOCK)) {
1338 while (node->nd_next) {
1339 node = node->nd_next;
1341 node = node->nd_head;
1343 node = remove_begin(node);
1346 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1348 /*% ripper[final]: program!($2) %*/
1353top_compstmt : top_stmts opt_terms
1355 $$ = void_stmts(p, $1);
1362 $$ = NEW_BEGIN(0, &@$);
1364 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1369 $$ = newline_node($1);
1371 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1373 | top_stmts terms top_stmt
1376 $$ = block_append(p, $1, newline_node($3));
1378 /*% ripper: stmts_add!($1, $3) %*/
1382 $$ = remove_begin($2);
1387 | keyword_BEGIN begin_block
1393begin_block : '{' top_compstmt '}'
1396 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1397 NEW_BEGIN($2, &@$));
1398 $$ = NEW_BEGIN(0, &@$);
1400 /*% ripper: BEGIN!($2) %*/
1406 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1411 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1413 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1420 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1422 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1426compstmt : stmts opt_terms
1428 $$ = void_stmts(p, $1);
1435 $$ = NEW_BEGIN(0, &@$);
1437 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1442 $$ = newline_node($1);
1444 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1446 | stmts terms stmt_or_begin
1449 $$ = block_append(p, $1, newline_node($3));
1451 /*% ripper: stmts_add!($1, $3) %*/
1455 $$ = remove_begin($2);
1465 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1473stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1476 $$ = NEW_ALIAS($2, $4, &@$);
1478 /*% ripper: alias!($2, $4) %*/
1480 | keyword_alias tGVAR tGVAR
1483 $$ = NEW_VALIAS($2, $3, &@$);
1485 /*% ripper: var_alias!($2, $3) %*/
1487 | keyword_alias tGVAR tBACK_REF
1492 buf[1] = (char)$3->nd_nth;
1493 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1495 /*% ripper: var_alias!($2, $3) %*/
1497 | keyword_alias tGVAR tNTH_REF
1499 static const char mesg[] = "can't make alias for the number variables";
1501 yyerror1(&@3, mesg);
1502 $$ = NEW_BEGIN(0, &@$);
1504 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1506 | keyword_undef undef_list
1511 /*% ripper: undef!($2) %*/
1513 | stmt modifier_if expr_value
1516 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1519 /*% ripper: if_mod!($3, $1) %*/
1521 | stmt modifier_unless expr_value
1524 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1527 /*% ripper: unless_mod!($3, $1) %*/
1529 | stmt modifier_while expr_value
1532 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1533 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1536 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1539 /*% ripper: while_mod!($3, $1) %*/
1541 | stmt modifier_until expr_value
1544 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1545 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1548 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1551 /*% ripper: until_mod!($3, $1) %*/
1553 | stmt modifier_rescue stmt
1557 YYLTYPE loc = code_loc_gen(&@2, &@3);
1558 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1559 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1561 /*% ripper: rescue_mod!($1, $3) %*/
1563 | keyword_END '{' compstmt '}'
1565 if (p->ctxt.in_def) {
1566 rb_warn0("END in method; use at_exit");
1570 NODE *scope = NEW_NODE(
1571 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1572 $$ = NEW_POSTEXE(scope, &@$);
1575 /*% ripper: END!($3) %*/
1578 | mlhs '=' lex_ctxt command_call
1582 $$ = node_assign(p, $1, $4, $3, &@$);
1584 /*% ripper: massign!($1, $4) %*/
1586 | lhs '=' lex_ctxt mrhs
1589 $$ = node_assign(p, $1, $4, $3, &@$);
1591 /*% ripper: assign!($1, $4) %*/
1593 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1596 YYLTYPE loc = code_loc_gen(&@5, &@6);
1597 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1599 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1601 | mlhs '=' lex_ctxt mrhs_arg
1604 $$ = node_assign(p, $1, $4, $3, &@$);
1606 /*% ripper: massign!($1, $4) %*/
1611command_asgn : lhs '=' lex_ctxt command_rhs
1614 $$ = node_assign(p, $1, $4, $3, &@$);
1616 /*% ripper: assign!($1, $4) %*/
1618 | var_lhs tOP_ASGN lex_ctxt command_rhs
1621 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1623 /*% ripper: opassign!($1, $2, $4) %*/
1625 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1628 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1630 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1633 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1636 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1638 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1640 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1643 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1645 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1647 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1650 YYLTYPE loc = code_loc_gen(&@1, &@3);
1651 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1653 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1655 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1658 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1660 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
1662 | defn_head f_opt_paren_args '=' command
1664 endless_method_name(p, $<node>1, &@1);
1665 restore_defun(p, $<node>1->nd_defn);
1667 $$ = set_defun_body(p, $1, $2, $4, &@$);
1669 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1670 /*% ripper: def!(get_value($1), $2, $4) %*/
1673 | defn_head f_opt_paren_args '=' command modifier_rescue arg
1675 endless_method_name(p, $<node>1, &@1);
1676 restore_defun(p, $<node>1->nd_defn);
1678 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1679 $$ = set_defun_body(p, $1, $2, $4, &@$);
1681 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1682 /*% ripper: def!(get_value($1), $2, $4) %*/
1685 | defs_head f_opt_paren_args '=' command
1687 endless_method_name(p, $<node>1, &@1);
1688 restore_defun(p, $<node>1->nd_defn);
1690 $$ = set_defun_body(p, $1, $2, $4, &@$);
1694 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1695 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1698 | defs_head f_opt_paren_args '=' command modifier_rescue arg
1700 endless_method_name(p, $<node>1, &@1);
1701 restore_defun(p, $<node>1->nd_defn);
1703 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1704 $$ = set_defun_body(p, $1, $2, $4, &@$);
1708 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1709 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1712 | backref tOP_ASGN lex_ctxt command_rhs
1715 rb_backref_error(p, $1);
1716 $$ = NEW_BEGIN(0, &@$);
1718 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1722command_rhs : command_call %prec tOP_ASGN
1727 | command_call modifier_rescue stmt
1730 YYLTYPE loc = code_loc_gen(&@2, &@3);
1732 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1734 /*% ripper: rescue_mod!($1, $3) %*/
1740 | expr keyword_and expr
1742 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1744 | expr keyword_or expr
1746 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1748 | keyword_not opt_nl expr
1750 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1754 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1759 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1760 p->command_start = FALSE;
1762 p->ctxt.in_kwarg = 1;
1763 $<tbl>$ = push_pvtbl(p);
1766 $<tbl>$ = push_pktbl(p);
1770 pop_pktbl(p, $<tbl>4);
1771 pop_pvtbl(p, $<tbl>3);
1772 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
1774 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
1776 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1781 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1782 p->command_start = FALSE;
1784 p->ctxt.in_kwarg = 1;
1785 $<tbl>$ = push_pvtbl(p);
1788 $<tbl>$ = push_pktbl(p);
1792 pop_pktbl(p, $<tbl>4);
1793 pop_pvtbl(p, $<tbl>3);
1794 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
1796 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
1798 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1800 | arg %prec tLBRACE_ARG
1805 ID fname = get_id($1);
1806 ID cur_arg = p->cur_arg;
1807 YYSTYPE c = {.ctxt = p->ctxt};
1808 numparam_name(p, fname);
1812 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
1815 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
1820defn_head : k_def def_name
1824 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
1829defs_head : k_def singleton dot_or_colon
1831 SET_LEX_STATE(EXPR_FNAME);
1832 p->ctxt.in_argdef = 1;
1836 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
1839 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
1841 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
1842 add_mark_object(p, ary);
1843 $<node>$->nd_rval = ary;
1855expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1861command_call : command
1865block_command : block_call
1866 | block_call call_op2 operation2 command_args
1869 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1871 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
1875cmd_brace_block : tLBRACE_ARG brace_body '}'
1879 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1880 nd_set_line($$, @1.end_pos.lineno);
1888 $$ = NEW_FCALL($1, 0, &@$);
1889 nd_set_line($$, p->tokline);
1895command : fcall command_args %prec tLOWEST
1899 nd_set_last_loc($1, @2.end_pos);
1902 /*% ripper: command!($1, $2) %*/
1904 | fcall command_args cmd_brace_block
1907 block_dup_check(p, $2, $3);
1909 $$ = method_add_block(p, $1, $3, &@$);
1911 nd_set_last_loc($1, @2.end_pos);
1913 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
1915 | primary_value call_op operation2 command_args %prec tLOWEST
1918 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1920 /*% ripper: command_call!($1, $2, $3, $4) %*/
1922 | primary_value call_op operation2 command_args cmd_brace_block
1925 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1927 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
1929 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1932 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1934 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
1936 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1939 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1941 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
1943 | keyword_super command_args
1946 $$ = NEW_SUPER($2, &@$);
1949 /*% ripper: super!($2) %*/
1951 | keyword_yield command_args
1954 $$ = new_yield(p, $2, &@$);
1957 /*% ripper: yield!($2) %*/
1959 | k_return call_args
1962 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1964 /*% ripper: return!($2) %*/
1966 | keyword_break call_args
1969 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1971 /*% ripper: break!($2) %*/
1973 | keyword_next call_args
1976 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1978 /*% ripper: next!($2) %*/
1983 | tLPAREN mlhs_inner rparen
1988 /*% ripper: mlhs_paren!($2) %*/
1992mlhs_inner : mlhs_basic
1993 | tLPAREN mlhs_inner rparen
1996 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1998 /*% ripper: mlhs_paren!($2) %*/
2002mlhs_basic : mlhs_head
2005 $$ = NEW_MASGN($1, 0, &@$);
2009 | mlhs_head mlhs_item
2012 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2014 /*% ripper: mlhs_add!($1, $2) %*/
2016 | mlhs_head tSTAR mlhs_node
2019 $$ = NEW_MASGN($1, $3, &@$);
2021 /*% ripper: mlhs_add_star!($1, $3) %*/
2023 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2026 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2028 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2033 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2035 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2037 | mlhs_head tSTAR ',' mlhs_post
2040 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2042 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2047 $$ = NEW_MASGN(0, $2, &@$);
2049 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2051 | tSTAR mlhs_node ',' mlhs_post
2054 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2056 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2061 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2063 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2065 | tSTAR ',' mlhs_post
2068 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2070 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2074mlhs_item : mlhs_node
2075 | tLPAREN mlhs_inner rparen
2080 /*% ripper: mlhs_paren!($2) %*/
2084mlhs_head : mlhs_item ','
2087 $$ = NEW_LIST($1, &@1);
2089 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2091 | mlhs_head mlhs_item ','
2094 $$ = list_append(p, $1, $2);
2096 /*% ripper: mlhs_add!($1, $2) %*/
2100mlhs_post : mlhs_item
2103 $$ = NEW_LIST($1, &@$);
2105 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2107 | mlhs_post ',' mlhs_item
2110 $$ = list_append(p, $1, $3);
2112 /*% ripper: mlhs_add!($1, $3) %*/
2116mlhs_node : user_variable
2119 $$ = assignable(p, $1, 0, &@$);
2121 /*% ripper: assignable(p, var_field(p, $1)) %*/
2126 $$ = assignable(p, $1, 0, &@$);
2128 /*% ripper: assignable(p, var_field(p, $1)) %*/
2130 | primary_value '[' opt_call_args rbracket
2133 $$ = aryset(p, $1, $3, &@$);
2135 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2137 | primary_value call_op tIDENTIFIER
2139 if ($2 == tANDDOT) {
2140 yyerror1(&@2, "&. inside multiple assignment destination");
2143 $$ = attrset(p, $1, $2, $3, &@$);
2145 /*% ripper: field!($1, $2, $3) %*/
2147 | primary_value tCOLON2 tIDENTIFIER
2150 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2152 /*% ripper: const_path_field!($1, $3) %*/
2154 | primary_value call_op tCONSTANT
2156 if ($2 == tANDDOT) {
2157 yyerror1(&@2, "&. inside multiple assignment destination");
2160 $$ = attrset(p, $1, $2, $3, &@$);
2162 /*% ripper: field!($1, $2, $3) %*/
2164 | primary_value tCOLON2 tCONSTANT
2167 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2169 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2174 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2176 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2181 rb_backref_error(p, $1);
2182 $$ = NEW_BEGIN(0, &@$);
2184 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2191 $$ = assignable(p, $1, 0, &@$);
2193 /*% ripper: assignable(p, var_field(p, $1)) %*/
2198 $$ = assignable(p, $1, 0, &@$);
2200 /*% ripper: assignable(p, var_field(p, $1)) %*/
2202 | primary_value '[' opt_call_args rbracket
2205 $$ = aryset(p, $1, $3, &@$);
2207 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2209 | primary_value call_op tIDENTIFIER
2212 $$ = attrset(p, $1, $2, $3, &@$);
2214 /*% ripper: field!($1, $2, $3) %*/
2216 | primary_value tCOLON2 tIDENTIFIER
2219 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2221 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
2223 | primary_value call_op tCONSTANT
2226 $$ = attrset(p, $1, $2, $3, &@$);
2228 /*% ripper: field!($1, $2, $3) %*/
2230 | primary_value tCOLON2 tCONSTANT
2233 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2235 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2240 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2242 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2247 rb_backref_error(p, $1);
2248 $$ = NEW_BEGIN(0, &@$);
2250 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2256 static const char mesg[] = "class/module name must be CONSTANT";
2258 yyerror1(&@1, mesg);
2260 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2265cpath : tCOLON3 cname
2268 $$ = NEW_COLON3($2, &@$);
2270 /*% ripper: top_const_ref!($2) %*/
2275 $$ = NEW_COLON2(0, $$, &@$);
2277 /*% ripper: const_ref!($1) %*/
2279 | primary_value tCOLON2 cname
2282 $$ = NEW_COLON2($1, $3, &@$);
2284 /*% ripper: const_path_ref!($1, $3) %*/
2293 SET_LEX_STATE(EXPR_ENDFN);
2302 $$ = NEW_LIT(ID2SYM($1), &@$);
2304 /*% ripper: symbol_literal!($1) %*/
2312 $$ = NEW_UNDEF($1, &@$);
2314 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2316 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2319 NODE *undef = NEW_UNDEF($4, &@4);
2320 $$ = block_append(p, $1, undef);
2322 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2326op : '|' { ifndef_ripper($$ = '|'); }
2327 | '^' { ifndef_ripper($$ = '^'); }
2328 | '&' { ifndef_ripper($$ = '&'); }
2329 | tCMP { ifndef_ripper($$ = tCMP); }
2330 | tEQ { ifndef_ripper($$ = tEQ); }
2331 | tEQQ { ifndef_ripper($$ = tEQQ); }
2332 | tMATCH { ifndef_ripper($$ = tMATCH); }
2333 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2334 | '>' { ifndef_ripper($$ = '>'); }
2335 | tGEQ { ifndef_ripper($$ = tGEQ); }
2336 | '<' { ifndef_ripper($$ = '<'); }
2337 | tLEQ { ifndef_ripper($$ = tLEQ); }
2338 | tNEQ { ifndef_ripper($$ = tNEQ); }
2339 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2340 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2341 | '+' { ifndef_ripper($$ = '+'); }
2342 | '-' { ifndef_ripper($$ = '-'); }
2343 | '*' { ifndef_ripper($$ = '*'); }
2344 | tSTAR { ifndef_ripper($$ = '*'); }
2345 | '/' { ifndef_ripper($$ = '/'); }
2346 | '%' { ifndef_ripper($$ = '%'); }
2347 | tPOW { ifndef_ripper($$ = tPOW); }
2348 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2349 | '!' { ifndef_ripper($$ = '!'); }
2350 | '~' { ifndef_ripper($$ = '~'); }
2351 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2352 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2353 | tAREF { ifndef_ripper($$ = tAREF); }
2354 | tASET { ifndef_ripper($$ = tASET); }
2355 | '`' { ifndef_ripper($$ = '`'); }
2358reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2359 | keyword_BEGIN | keyword_END
2360 | keyword_alias | keyword_and | keyword_begin
2361 | keyword_break | keyword_case | keyword_class | keyword_def
2362 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2363 | keyword_end | keyword_ensure | keyword_false
2364 | keyword_for | keyword_in | keyword_module | keyword_next
2365 | keyword_nil | keyword_not | keyword_or | keyword_redo
2366 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2367 | keyword_super | keyword_then | keyword_true | keyword_undef
2368 | keyword_when | keyword_yield | keyword_if | keyword_unless
2369 | keyword_while | keyword_until
2372arg : lhs '=' lex_ctxt arg_rhs
2375 $$ = node_assign(p, $1, $4, $3, &@$);
2377 /*% ripper: assign!($1, $4) %*/
2379 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2382 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2384 /*% ripper: opassign!($1, $2, $4) %*/
2386 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2389 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2391 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2393 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2396 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2398 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2400 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2403 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2405 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2407 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2410 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2412 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
2414 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2417 YYLTYPE loc = code_loc_gen(&@1, &@3);
2418 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2420 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2422 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2425 YYLTYPE loc = code_loc_gen(&@1, &@2);
2426 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2428 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2430 | backref tOP_ASGN lex_ctxt arg_rhs
2433 rb_backref_error(p, $1);
2434 $$ = NEW_BEGIN(0, &@$);
2436 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2443 $$ = NEW_DOT2($1, $3, &@$);
2445 /*% ripper: dot2!($1, $3) %*/
2452 $$ = NEW_DOT3($1, $3, &@$);
2454 /*% ripper: dot3!($1, $3) %*/
2460 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2462 /*% ripper: dot2!($1, Qnil) %*/
2468 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2470 /*% ripper: dot3!($1, Qnil) %*/
2476 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2478 /*% ripper: dot2!(Qnil, $2) %*/
2484 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2486 /*% ripper: dot3!(Qnil, $2) %*/
2490 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2494 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2498 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2502 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2506 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2510 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2512 | tUMINUS_NUM simple_numeric tPOW arg
2514 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2518 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2522 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2526 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2530 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2534 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2538 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2540 | rel_expr %prec tCMP
2543 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2547 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2551 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2555 $$ = match_op(p, $1, $3, &@2, &@$);
2559 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2563 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2567 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2571 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2575 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2579 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2583 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2585 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2587 p->ctxt.in_defined = 0;
2588 $$ = new_defined(p, $4, &@$);
2590 | arg '?' arg opt_nl ':' arg
2594 $$ = new_if(p, $1, $3, $6, &@$);
2597 /*% ripper: ifop!($1, $3, $6) %*/
2599 | defn_head f_opt_paren_args '=' arg
2601 endless_method_name(p, $<node>1, &@1);
2602 restore_defun(p, $<node>1->nd_defn);
2604 $$ = set_defun_body(p, $1, $2, $4, &@$);
2606 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2607 /*% ripper: def!(get_value($1), $2, $4) %*/
2610 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2612 endless_method_name(p, $<node>1, &@1);
2613 restore_defun(p, $<node>1->nd_defn);
2615 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2616 $$ = set_defun_body(p, $1, $2, $4, &@$);
2618 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2619 /*% ripper: def!(get_value($1), $2, $4) %*/
2622 | defs_head f_opt_paren_args '=' arg
2624 endless_method_name(p, $<node>1, &@1);
2625 restore_defun(p, $<node>1->nd_defn);
2627 $$ = set_defun_body(p, $1, $2, $4, &@$);
2631 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2632 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2635 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2637 endless_method_name(p, $<node>1, &@1);
2638 restore_defun(p, $<node>1->nd_defn);
2640 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2641 $$ = set_defun_body(p, $1, $2, $4, &@$);
2645 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2646 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2655relop : '>' {$$ = '>';}
2661rel_expr : arg relop arg %prec '>'
2663 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2665 | rel_expr relop arg %prec '>'
2667 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2668 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2694 | args ',' assocs trailer
2697 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2699 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2704 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2706 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2710arg_rhs : arg %prec tOP_ASGN
2715 | arg modifier_rescue arg
2719 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2721 /*% ripper: rescue_mod!($1, $3) %*/
2725paren_args : '(' opt_call_args rparen
2730 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2732 | '(' args ',' args_forward rparen
2734 if (!check_forwarding_args(p)) {
2739 $$ = new_args_forward_call(p, $2, &@4, &@$);
2741 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
2744 | '(' args_forward rparen
2746 if (!check_forwarding_args(p)) {
2751 $$ = new_args_forward_call(p, 0, &@2, &@$);
2753 /*% ripper: arg_paren!($2) %*/
2758opt_paren_args : none
2768 | args ',' assocs ','
2771 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2773 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2778 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2780 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2788 $$ = NEW_LIST($1, &@$);
2790 /*% ripper: args_add!(args_new!, $1) %*/
2792 | args opt_block_arg
2795 $$ = arg_blk_pass($1, $2);
2797 /*% ripper: args_add_block!($1, $2) %*/
2799 | assocs opt_block_arg
2802 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2803 $$ = arg_blk_pass($$, $2);
2805 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
2807 | args ',' assocs opt_block_arg
2810 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2811 $$ = arg_blk_pass($$, $4);
2813 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
2816 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
2820 /* If call_args starts with a open paren '(' or '[',
2821 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2822 * but the push must be done after CMDARG_PUSH(1).
2823 * So this code makes them consistent by first cancelling
2824 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2825 * and finally redoing CMDARG_PUSH(0).
2829 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2832 if (lookahead) CMDARG_POP();
2834 if (lookahead) CMDARG_PUSH(0);
2838 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2839 * but the push must be done after CMDARG_POP() in the parser.
2840 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2841 * CMDARG_POP() to pop 1 pushed by command_args,
2842 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2849 if (lookahead) CMDARG_POP();
2851 if (lookahead) CMDARG_PUSH(0);
2856block_arg : tAMPER arg_value
2859 $$ = NEW_BLOCK_PASS($2, &@$);
2866 if (!local_id(p, ANON_BLOCK_ID)) {
2867 compile_error(p, "no anonymous block parameter");
2869 $$ = NEW_BLOCK_PASS(NEW_LVAR(ANON_BLOCK_ID, &@1), &@$);
2876opt_block_arg : ',' block_arg
2890 $$ = NEW_LIST($1, &@$);
2892 /*% ripper: args_add!(args_new!, $1) %*/
2897 $$ = NEW_SPLAT($2, &@$);
2899 /*% ripper: args_add_star!(args_new!, $2) %*/
2901 | args ',' arg_value
2904 $$ = last_arg_append(p, $1, $3, &@$);
2906 /*% ripper: args_add!($1, $3) %*/
2908 | args ',' tSTAR arg_value
2911 $$ = rest_arg_append(p, $1, $4, &@$);
2913 /*% ripper: args_add_star!($1, $4) %*/
2923mrhs : args ',' arg_value
2926 $$ = last_arg_append(p, $1, $3, &@$);
2928 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
2930 | args ',' tSTAR arg_value
2933 $$ = rest_arg_append(p, $1, $4, &@$);
2935 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
2940 $$ = NEW_SPLAT($2, &@$);
2942 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
2959 $$ = NEW_FCALL($1, 0, &@$);
2961 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
2972 set_line_body($3, @1.end_pos.lineno);
2973 $$ = NEW_BEGIN($3, &@$);
2974 nd_set_line($$, @1.end_pos.lineno);
2976 /*% ripper: begin!($3) %*/
2978 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2981 $$ = NEW_BEGIN(0, &@$);
2983 /*% ripper: paren!(0) %*/
2985 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2988 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
2991 /*% ripper: paren!($2) %*/
2993 | tLPAREN compstmt ')'
2996 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
2999 /*% ripper: paren!($2) %*/
3001 | primary_value tCOLON2 tCONSTANT
3004 $$ = NEW_COLON2($1, $3, &@$);
3006 /*% ripper: const_path_ref!($1, $3) %*/
3011 $$ = NEW_COLON3($2, &@$);
3013 /*% ripper: top_const_ref!($2) %*/
3015 | tLBRACK aref_args ']'
3018 $$ = make_list($2, &@$);
3020 /*% ripper: array!(escape_Qundef($2)) %*/
3022 | tLBRACE assoc_list '}'
3025 $$ = new_hash(p, $2, &@$);
3026 $$->nd_brace = TRUE;
3028 /*% ripper: hash!(escape_Qundef($2)) %*/
3033 $$ = NEW_RETURN(0, &@$);
3035 /*% ripper: return0! %*/
3037 | keyword_yield '(' call_args rparen
3040 $$ = new_yield(p, $3, &@$);
3042 /*% ripper: yield!(paren!($3)) %*/
3044 | keyword_yield '(' rparen
3047 $$ = NEW_YIELD(0, &@$);
3049 /*% ripper: yield!(paren!(args_new!)) %*/
3054 $$ = NEW_YIELD(0, &@$);
3056 /*% ripper: yield0! %*/
3058 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3060 p->ctxt.in_defined = 0;
3061 $$ = new_defined(p, $5, &@$);
3063 | keyword_not '(' expr rparen
3065 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3067 | keyword_not '(' rparen
3069 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3074 $$ = method_add_block(p, $1, $2, &@$);
3076 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3079 | method_call brace_block
3082 block_dup_check(p, $1->nd_args, $2);
3083 $$ = method_add_block(p, $1, $2, &@$);
3085 /*% ripper: method_add_block!($1, $2) %*/
3088 | k_if expr_value then
3094 $$ = new_if(p, $2, $4, $5, &@$);
3097 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3099 | k_unless expr_value then
3105 $$ = new_unless(p, $2, $4, $5, &@$);
3108 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3110 | k_while expr_value_do
3115 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3118 /*% ripper: while!($2, $3) %*/
3120 | k_until expr_value_do
3125 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3128 /*% ripper: until!($2, $3) %*/
3130 | k_case expr_value opt_terms
3132 $<val>$ = p->case_labels;
3133 p->case_labels = Qnil;
3138 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3139 p->case_labels = $<val>4;
3141 $$ = NEW_CASE($2, $5, &@$);
3144 /*% ripper: case!($2, $5) %*/
3148 $<val>$ = p->case_labels;
3154 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3155 p->case_labels = $<val>3;
3157 $$ = NEW_CASE2($4, &@$);
3159 /*% ripper: case!(Qnil, $4) %*/
3161 | k_case expr_value opt_terms
3166 $$ = NEW_CASE3($2, $4, &@$);
3168 /*% ripper: case!($2, $4) %*/
3170 | k_for for_var keyword_in expr_value_do
3178 * e.each{|*x| a, b, c = x}
3182 * e.each{|x| a, = x}
3184 ID id = internal_id(p);
3185 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3186 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3187 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3188 tbl->ids[0] = id; /* internal id */
3190 switch (nd_type($2)) {
3192 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3193 $2->nd_value = internal_var;
3198 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3199 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3201 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3202 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3204 /* {|*internal_id| <m> = internal_id; ... } */
3205 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3206 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3207 $$ = NEW_FOR($4, scope, &@$);
3210 /*% ripper: for!($2, $4, $5) %*/
3212 | k_class cpath superclass
3214 if (p->ctxt.in_def) {
3215 YYLTYPE loc = code_loc_gen(&@1, &@2);
3216 yyerror1(&loc, "class definition in method body");
3218 p->ctxt.in_class = 1;
3225 $$ = NEW_CLASS($2, $5, $3, &@$);
3226 nd_set_line($$->nd_body, @6.end_pos.lineno);
3227 set_line_body($5, @3.end_pos.lineno);
3228 nd_set_line($$, @3.end_pos.lineno);
3230 /*% ripper: class!($2, $3, $5) %*/
3232 p->ctxt.in_class = $<ctxt>1.in_class;
3233 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3235 | k_class tLSHFT expr
3238 p->ctxt.in_class = 0;
3246 $$ = NEW_SCLASS($3, $6, &@$);
3247 nd_set_line($$->nd_body, @7.end_pos.lineno);
3248 set_line_body($6, nd_line($3));
3251 /*% ripper: sclass!($3, $6) %*/
3253 p->ctxt.in_def = $<ctxt>1.in_def;
3254 p->ctxt.in_class = $<ctxt>1.in_class;
3255 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3259 if (p->ctxt.in_def) {
3260 YYLTYPE loc = code_loc_gen(&@1, &@2);
3261 yyerror1(&loc, "module definition in method body");
3263 p->ctxt.in_class = 1;
3270 $$ = NEW_MODULE($2, $4, &@$);
3271 nd_set_line($$->nd_body, @5.end_pos.lineno);
3272 set_line_body($4, @2.end_pos.lineno);
3273 nd_set_line($$, @2.end_pos.lineno);
3275 /*% ripper: module!($2, $4) %*/
3277 p->ctxt.in_class = $<ctxt>1.in_class;
3278 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3285 restore_defun(p, $<node>1->nd_defn);
3287 $$ = set_defun_body(p, $1, $2, $3, &@$);
3289 /*% ripper: def!(get_value($1), $2, $3) %*/
3297 restore_defun(p, $<node>1->nd_defn);
3299 $$ = set_defun_body(p, $1, $2, $3, &@$);
3303 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $3) %*/
3309 $$ = NEW_BREAK(0, &@$);
3311 /*% ripper: break!(args_new!) %*/
3316 $$ = NEW_NEXT(0, &@$);
3318 /*% ripper: next!(args_new!) %*/
3325 /*% ripper: redo! %*/
3330 $$ = NEW_RETRY(&@$);
3332 /*% ripper: retry! %*/
3336primary_value : primary
3343k_begin : keyword_begin
3345 token_info_push(p, "begin", &@$);
3352 token_info_push(p, "if", &@$);
3353 if (p->token_info && p->token_info->nonspc &&
3354 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3355 const char *tok = p->lex.ptok;
3356 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3357 beg += rb_strlen_lit("else");
3358 while (beg < tok && ISSPACE(*beg)) beg++;
3360 p->token_info->nonspc = 0;
3366k_unless : keyword_unless
3368 token_info_push(p, "unless", &@$);
3372k_while : keyword_while
3374 token_info_push(p, "while", &@$);
3378k_until : keyword_until
3380 token_info_push(p, "until", &@$);
3384k_case : keyword_case
3386 token_info_push(p, "case", &@$);
3392 token_info_push(p, "for", &@$);
3396k_class : keyword_class
3398 token_info_push(p, "class", &@$);
3403k_module : keyword_module
3405 token_info_push(p, "module", &@$);
3412 token_info_push(p, "def", &@$);
3413 p->ctxt.in_argdef = 1;
3419 token_info_push(p, "do", &@$);
3423k_do_block : keyword_do_block
3425 token_info_push(p, "do", &@$);
3429k_rescue : keyword_rescue
3431 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3435k_ensure : keyword_ensure
3437 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3441k_when : keyword_when
3443 token_info_warn(p, "when", p->token_info, 0, &@$);
3447k_else : keyword_else
3449 token_info *ptinfo_beg = p->token_info;
3450 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3451 token_info_warn(p, "else", p->token_info, same, &@$);
3454 e.next = ptinfo_beg->next;
3456 token_info_setup(&e, p->lex.pbeg, &@$);
3457 if (!e.nonspc) *ptinfo_beg = e;
3462k_elsif : keyword_elsif
3465 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3471 token_info_pop(p, "end", &@$);
3475k_return : keyword_return
3477 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3478 yyerror1(&@1, "Invalid return in class/module body");
3492 | k_elsif expr_value then
3497 $$ = new_if(p, $2, $4, $5, &@$);
3500 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3510 /*% ripper: else!($2) %*/
3521 $$ = assignable(p, $1, 0, &@$);
3522 mark_lvar_used(p, $$);
3524 /*% ripper: assignable(p, $1) %*/
3526 | tLPAREN f_margs rparen
3531 /*% ripper: mlhs_paren!($2) %*/
3538 $$ = NEW_LIST($1, &@$);
3540 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3542 | f_marg_list ',' f_marg
3545 $$ = list_append(p, $1, $3);
3547 /*% ripper: mlhs_add!($1, $3) %*/
3551f_margs : f_marg_list
3554 $$ = NEW_MASGN($1, 0, &@$);
3558 | f_marg_list ',' f_rest_marg
3561 $$ = NEW_MASGN($1, $3, &@$);
3563 /*% ripper: mlhs_add_star!($1, $3) %*/
3565 | f_marg_list ',' f_rest_marg ',' f_marg_list
3568 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3570 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3575 $$ = NEW_MASGN(0, $1, &@$);
3577 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3579 | f_rest_marg ',' f_marg_list
3582 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3584 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3588f_rest_marg : tSTAR f_norm_arg
3591 $$ = assignable(p, $2, 0, &@$);
3592 mark_lvar_used(p, $$);
3594 /*% ripper: assignable(p, $2) %*/
3599 $$ = NODE_SPECIAL_NO_NAME_REST;
3601 /*% ripper: Qnil %*/
3605f_any_kwrest : f_kwrest
3606 | f_no_kwarg {$$ = ID2VAL(idNil);}
3609f_eq : {p->ctxt.in_argdef = 0;} '=';
3611block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3613 $$ = new_args_tail(p, $1, $3, $4, &@3);
3615 | f_block_kwarg opt_f_block_arg
3617 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3619 | f_any_kwrest opt_f_block_arg
3621 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3625 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3629opt_block_args_tail : ',' block_args_tail
3635 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3641 /* magic number for rest_id in iseq_set_arguments() */
3643 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3645 /*% ripper: excessed_comma! %*/
3649block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3651 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3653 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3655 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3657 | f_arg ',' f_block_optarg opt_block_args_tail
3659 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3661 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3663 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3665 | f_arg ',' f_rest_arg opt_block_args_tail
3667 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3669 | f_arg excessed_comma
3671 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3672 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3674 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3676 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3678 | f_arg opt_block_args_tail
3680 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3682 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3684 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3686 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3688 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3690 | f_block_optarg opt_block_args_tail
3692 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3694 | f_block_optarg ',' f_arg opt_block_args_tail
3696 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3698 | f_rest_arg opt_block_args_tail
3700 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3702 | f_rest_arg ',' f_arg opt_block_args_tail
3704 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3708 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3712opt_block_param : none
3715 p->command_start = TRUE;
3719block_param_def : '|' opt_bv_decl '|'
3722 p->max_numparam = ORDINAL_PARAM;
3723 p->ctxt.in_argdef = 0;
3727 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
3729 | '|' block_param opt_bv_decl '|'
3732 p->max_numparam = ORDINAL_PARAM;
3733 p->ctxt.in_argdef = 0;
3737 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
3746 | opt_nl ';' bv_decls opt_nl
3756 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
3758 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
3763 new_bv(p, get_id($1));
3764 /*% ripper: get_value($1) %*/
3774 token_info_push(p, "->", &@1);
3775 $<vars>1 = dyna_push(p);
3776 $<num>$ = p->lex.lpar_beg;
3777 p->lex.lpar_beg = p->lex.paren_nest;
3780 $<num>$ = p->max_numparam;
3781 p->max_numparam = 0;
3784 $<node>$ = numparam_push(p);
3792 int max_numparam = p->max_numparam;
3793 p->lex.lpar_beg = $<num>2;
3794 p->max_numparam = $<num>3;
3796 $5 = args_with_numbered(p, $5, max_numparam);
3799 YYLTYPE loc = code_loc_gen(&@5, &@7);
3800 $$ = NEW_LAMBDA($5, $7, &loc);
3801 nd_set_line($$->nd_body, @7.end_pos.lineno);
3802 nd_set_line($$, @5.end_pos.lineno);
3803 nd_set_first_loc($$, @1.beg_pos);
3806 /*% ripper: lambda!($5, $7) %*/
3807 numparam_pop(p, $<node>4);
3808 dyna_pop(p, $<vars>1);
3812f_larglist : '(' f_args opt_bv_decl ')'
3814 p->ctxt.in_argdef = 0;
3817 p->max_numparam = ORDINAL_PARAM;
3819 /*% ripper: paren!($2) %*/
3823 p->ctxt.in_argdef = 0;
3825 if (!args_info_empty_p($1->nd_ainfo))
3826 p->max_numparam = ORDINAL_PARAM;
3832lambda_body : tLAMBEG compstmt '}'
3834 token_info_pop(p, "}", &@3);
3837 | keyword_do_LAMBDA bodystmt k_end
3843do_block : k_do_block do_body k_end
3847 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3848 nd_set_line($$, @1.end_pos.lineno);
3853block_call : command do_block
3856 if (nd_type_p($1, NODE_YIELD)) {
3857 compile_error(p, "block given to yield");
3860 block_dup_check(p, $1->nd_args, $2);
3862 $$ = method_add_block(p, $1, $2, &@$);
3865 /*% ripper: method_add_block!($1, $2) %*/
3867 | block_call call_op2 operation2 opt_paren_args
3870 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3872 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3874 | block_call call_op2 operation2 opt_paren_args brace_block
3877 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3879 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
3881 | block_call call_op2 operation2 command_args do_block
3884 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3886 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
3890method_call : fcall paren_args
3895 nd_set_last_loc($1, @2.end_pos);
3897 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
3899 | primary_value call_op operation2 opt_paren_args
3902 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3903 nd_set_line($$, @3.end_pos.lineno);
3905 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3907 | primary_value tCOLON2 operation2 paren_args
3910 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3911 nd_set_line($$, @3.end_pos.lineno);
3913 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
3915 | primary_value tCOLON2 operation3
3918 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3920 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
3922 | primary_value call_op paren_args
3925 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3926 nd_set_line($$, @2.end_pos.lineno);
3928 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
3930 | primary_value tCOLON2 paren_args
3933 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3934 nd_set_line($$, @2.end_pos.lineno);
3936 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
3938 | keyword_super paren_args
3941 $$ = NEW_SUPER($2, &@$);
3943 /*% ripper: super!($2) %*/
3948 $$ = NEW_ZSUPER(&@$);
3950 /*% ripper: zsuper! %*/
3952 | primary_value '[' opt_call_args rbracket
3955 if ($1 && nd_type_p($1, NODE_SELF))
3956 $$ = NEW_FCALL(tAREF, $3, &@$);
3958 $$ = NEW_CALL($1, tAREF, $3, &@$);
3961 /*% ripper: aref!($1, escape_Qundef($3)) %*/
3965brace_block : '{' brace_body '}'
3969 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3970 nd_set_line($$, @1.end_pos.lineno);
3973 | k_do do_body k_end
3977 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3978 nd_set_line($$, @1.end_pos.lineno);
3983brace_body : {$<vars>$ = dyna_push(p);}
3985 $<num>$ = p->max_numparam;
3986 p->max_numparam = 0;
3989 $<node>$ = numparam_push(p);
3991 opt_block_param compstmt
3993 int max_numparam = p->max_numparam;
3994 p->max_numparam = $<num>2;
3995 $4 = args_with_numbered(p, $4, max_numparam);
3997 $$ = NEW_ITER($4, $5, &@$);
3999 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
4000 numparam_pop(p, $<node>3);
4001 dyna_pop(p, $<vars>1);
4005do_body : {$<vars>$ = dyna_push(p);}
4007 $<num>$ = p->max_numparam;
4008 p->max_numparam = 0;
4011 $<node>$ = numparam_push(p);
4014 opt_block_param bodystmt
4016 int max_numparam = p->max_numparam;
4017 p->max_numparam = $<num>2;
4018 $4 = args_with_numbered(p, $4, max_numparam);
4020 $$ = NEW_ITER($4, $5, &@$);
4022 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4024 numparam_pop(p, $<node>3);
4025 dyna_pop(p, $<vars>1);
4029case_args : arg_value
4032 check_literal_when(p, $1, &@1);
4033 $$ = NEW_LIST($1, &@$);
4035 /*% ripper: args_add!(args_new!, $1) %*/
4040 $$ = NEW_SPLAT($2, &@$);
4042 /*% ripper: args_add_star!(args_new!, $2) %*/
4044 | case_args ',' arg_value
4047 check_literal_when(p, $3, &@3);
4048 $$ = last_arg_append(p, $1, $3, &@$);
4050 /*% ripper: args_add!($1, $3) %*/
4052 | case_args ',' tSTAR arg_value
4055 $$ = rest_arg_append(p, $1, $4, &@$);
4057 /*% ripper: args_add_star!($1, $4) %*/
4061case_body : k_when case_args then
4066 $$ = NEW_WHEN($2, $4, $5, &@$);
4069 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4077p_case_body : keyword_in
4079 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4080 p->command_start = FALSE;
4082 p->ctxt.in_kwarg = 1;
4083 $<tbl>$ = push_pvtbl(p);
4086 $<tbl>$ = push_pktbl(p);
4090 pop_pktbl(p, $<tbl>3);
4091 pop_pvtbl(p, $<tbl>2);
4092 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4098 $$ = NEW_IN($4, $7, $8, &@$);
4100 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4108p_top_expr : p_top_expr_body
4109 | p_top_expr_body modifier_if expr_value
4112 $$ = new_if(p, $3, $1, 0, &@$);
4115 /*% ripper: if_mod!($3, $1) %*/
4117 | p_top_expr_body modifier_unless expr_value
4120 $$ = new_unless(p, $3, $1, 0, &@$);
4123 /*% ripper: unless_mod!($3, $1) %*/
4127p_top_expr_body : p_expr
4130 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4131 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4135 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4137 nd_set_first_loc($$, @1.beg_pos);
4143 $$ = new_find_pattern(p, Qnone, $1, &@$);
4147 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4151 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4158p_as : p_expr tASSOC p_variable
4161 NODE *n = NEW_LIST($1, &@$);
4162 n = list_append(p, n, $3);
4163 $$ = new_hash(p, n, &@$);
4165 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4170p_alt : p_alt '|' p_expr_basic
4173 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4175 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4180p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4181p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4183p_expr_basic : p_value
4185 | p_const p_lparen p_args rparen
4187 pop_pktbl(p, $<tbl>2);
4188 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4190 nd_set_first_loc($$, @1.beg_pos);
4194 | p_const p_lparen p_find rparen
4196 pop_pktbl(p, $<tbl>2);
4197 $$ = new_find_pattern(p, $1, $3, &@$);
4199 nd_set_first_loc($$, @1.beg_pos);
4203 | p_const p_lparen p_kwargs rparen
4205 pop_pktbl(p, $<tbl>2);
4206 $$ = new_hash_pattern(p, $1, $3, &@$);
4208 nd_set_first_loc($$, @1.beg_pos);
4212 | p_const '(' rparen
4214 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4215 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4217 | p_const p_lbracket p_args rbracket
4219 pop_pktbl(p, $<tbl>2);
4220 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4222 nd_set_first_loc($$, @1.beg_pos);
4226 | p_const p_lbracket p_find rbracket
4228 pop_pktbl(p, $<tbl>2);
4229 $$ = new_find_pattern(p, $1, $3, &@$);
4231 nd_set_first_loc($$, @1.beg_pos);
4235 | p_const p_lbracket p_kwargs rbracket
4237 pop_pktbl(p, $<tbl>2);
4238 $$ = new_hash_pattern(p, $1, $3, &@$);
4240 nd_set_first_loc($$, @1.beg_pos);
4244 | p_const '[' rbracket
4246 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4247 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4249 | tLBRACK p_args rbracket
4251 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4253 | tLBRACK p_find rbracket
4255 $$ = new_find_pattern(p, Qnone, $2, &@$);
4259 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4260 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4264 $<tbl>$ = push_pktbl(p);
4266 p->ctxt.in_kwarg = 0;
4270 pop_pktbl(p, $<tbl>2);
4271 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4272 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4276 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4277 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4279 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4281 pop_pktbl(p, $<tbl>2);
4289 NODE *pre_args = NEW_LIST($1, &@$);
4290 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4292 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4297 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4302 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4304 VALUE pre_args = rb_ary_concat($1, get_value($2));
4305 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4308 | p_args_head tSTAR tIDENTIFIER
4310 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4312 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4314 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4318 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4320 | p_args_head tSTAR ',' p_args_post
4322 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4327p_args_head : p_arg ','
4331 | p_args_head p_arg ','
4334 $$ = list_concat($1, $2);
4336 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4342 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4344 | p_rest ',' p_args_post
4346 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4350p_find : p_rest ',' p_args_post ',' p_rest
4352 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4354 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
4355 rb_warn0L_experimental(nd_line($$), "Find pattern is experimental, and the behavior may change in future versions of Ruby!");
4360p_rest : tSTAR tIDENTIFIER
4371 | p_args_post ',' p_arg
4374 $$ = list_concat($1, $3);
4376 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4383 $$ = NEW_LIST($1, &@$);
4385 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4389p_kwargs : p_kwarg ',' p_any_kwrest
4391 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4395 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4399 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4403 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4408 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4412 $$ = list_concat($1, $3);
4414 /*% ripper: rb_ary_push($1, $3) %*/
4418p_kw : p_kw_label p_expr
4420 error_duplicate_pattern_key(p, get_id($1), &@1);
4422 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4424 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4428 error_duplicate_pattern_key(p, get_id($1), &@1);
4429 if ($1 && !is_local_id(get_id($1))) {
4430 yyerror1(&@1, "key must be valid as local variables");
4432 error_duplicate_pattern_variable(p, get_id($1), &@1);
4434 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4436 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4441 | tSTRING_BEG string_contents tLABEL_END
4443 YYLTYPE loc = code_loc_gen(&@1, &@3);
4445 if (!$2 || nd_type_p($2, NODE_STR)) {
4446 NODE *node = dsym_node(p, $2, &loc);
4447 $$ = SYM2ID(node->nd_lit);
4450 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4451 VALUE label = RNODE($2)->nd_cval;
4452 VALUE rval = RNODE($2)->nd_rval;
4453 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4454 RNODE($$)->nd_loc = loc;
4458 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4464p_kwrest : kwrest_mark tIDENTIFIER
4474p_kwnorest : kwrest_mark keyword_nil
4480p_any_kwrest : p_kwrest
4481 | p_kwnorest {$$ = ID2VAL(idNil);}
4484p_value : p_primitive
4485 | p_primitive tDOT2 p_primitive
4490 $$ = NEW_DOT2($1, $3, &@$);
4492 /*% ripper: dot2!($1, $3) %*/
4494 | p_primitive tDOT3 p_primitive
4499 $$ = NEW_DOT3($1, $3, &@$);
4501 /*% ripper: dot3!($1, $3) %*/
4507 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4509 /*% ripper: dot2!($1, Qnil) %*/
4515 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4517 /*% ripper: dot3!($1, Qnil) %*/
4522 | tBDOT2 p_primitive
4526 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4528 /*% ripper: dot2!(Qnil, $2) %*/
4530 | tBDOT3 p_primitive
4534 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4536 /*% ripper: dot3!(Qnil, $2) %*/
4540p_primitive : literal
4551 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4553 /*% ripper: var_ref!($1) %*/
4558p_variable : tIDENTIFIER
4561 error_duplicate_pattern_variable(p, $1, &@1);
4562 $$ = assignable(p, $1, 0, &@$);
4564 /*% ripper: assignable(p, var_field(p, $1)) %*/
4568p_var_ref : '^' tIDENTIFIER
4571 NODE *n = gettable(p, $2, &@$);
4572 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4573 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4577 /*% ripper: var_ref!($2) %*/
4582 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4584 /*% ripper: var_ref!($2) %*/
4588p_expr_ref : '^' tLPAREN expr_value ')'
4591 $$ = NEW_BEGIN($3, &@$);
4593 /*% ripper: begin!($3) %*/
4597p_const : tCOLON3 cname
4600 $$ = NEW_COLON3($2, &@$);
4602 /*% ripper: top_const_ref!($2) %*/
4604 | p_const tCOLON2 cname
4607 $$ = NEW_COLON2($1, $3, &@$);
4609 /*% ripper: const_path_ref!($1, $3) %*/
4614 $$ = gettable(p, $1, &@$);
4616 /*% ripper: var_ref!($1) %*/
4620opt_rescue : k_rescue exc_list exc_var then
4625 $$ = NEW_RESBODY($2,
4626 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4628 fixpos($$, $2?$2:$5);
4630 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4638 $$ = NEW_LIST($1, &@$);
4640 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4645 if (!($$ = splat_array($1))) $$ = $1;
4659opt_ensure : k_ensure compstmt
4664 /*% ripper: ensure!($2) %*/
4678 node = NEW_STR(STR_NEW0(), &@$);
4679 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4682 node = evstr2dstr(p, node);
4695 $$ = literal_concat(p, $1, $2, &@$);
4697 /*% ripper: string_concat!($1, $2) %*/
4701string1 : tSTRING_BEG string_contents tSTRING_END
4704 $$ = heredoc_dedent(p, $2);
4705 if ($$) nd_set_loc($$, &@$);
4707 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4711xstring : tXSTRING_BEG xstring_contents tSTRING_END
4714 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4716 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4720regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4722 $$ = new_regexp(p, $2, $3, &@$);
4726words : tWORDS_BEG ' ' word_list tSTRING_END
4729 $$ = make_list($3, &@$);
4731 /*% ripper: array!($3) %*/
4735word_list : /* none */
4740 /*% ripper: words_new! %*/
4742 | word_list word ' '
4745 $$ = list_append(p, $1, evstr2dstr(p, $2));
4747 /*% ripper: words_add!($1, $2) %*/
4751word : string_content
4752 /*% ripper[brace]: word_add!(word_new!, $1) %*/
4753 | word string_content
4756 $$ = literal_concat(p, $1, $2, &@$);
4758 /*% ripper: word_add!($1, $2) %*/
4762symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4765 $$ = make_list($3, &@$);
4767 /*% ripper: array!($3) %*/
4771symbol_list : /* none */
4776 /*% ripper: symbols_new! %*/
4778 | symbol_list word ' '
4781 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4783 /*% ripper: symbols_add!($1, $2) %*/
4787qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4790 $$ = make_list($3, &@$);
4792 /*% ripper: array!($3) %*/
4796qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4799 $$ = make_list($3, &@$);
4801 /*% ripper: array!($3) %*/
4805qword_list : /* none */
4810 /*% ripper: qwords_new! %*/
4812 | qword_list tSTRING_CONTENT ' '
4815 $$ = list_append(p, $1, $2);
4817 /*% ripper: qwords_add!($1, $2) %*/
4821qsym_list : /* none */
4826 /*% ripper: qsymbols_new! %*/
4828 | qsym_list tSTRING_CONTENT ' '
4831 $$ = symbol_append(p, $1, $2);
4833 /*% ripper: qsymbols_add!($1, $2) %*/
4837string_contents : /* none */
4842 /*% ripper: string_content! %*/
4845 $$ = ripper_new_yylval(p, 0, $$, 0);
4848 | string_contents string_content
4851 $$ = literal_concat(p, $1, $2, &@$);
4853 /*% ripper: string_add!($1, $2) %*/
4856 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4857 !RNODE($1)->nd_cval) {
4858 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4859 RNODE($1)->nd_rval = add_mark_object(p, $$);
4866xstring_contents: /* none */
4871 /*% ripper: xstring_new! %*/
4873 | xstring_contents string_content
4876 $$ = literal_concat(p, $1, $2, &@$);
4878 /*% ripper: xstring_add!($1, $2) %*/
4882regexp_contents: /* none */
4887 /*% ripper: regexp_new! %*/
4890 $$ = ripper_new_yylval(p, 0, $$, 0);
4893 | regexp_contents string_content
4896 NODE *head = $1, *tail = $2;
4904 switch (nd_type(head)) {
4906 nd_set_type(head, NODE_DSTR);
4911 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4914 $$ = list_append(p, head, tail);
4917 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4918 if (ripper_is_node_yylval(n1)) {
4919 s1 = RNODE(n1)->nd_cval;
4920 n1 = RNODE(n1)->nd_rval;
4922 if (ripper_is_node_yylval(n2)) {
4923 s2 = RNODE(n2)->nd_cval;
4924 n2 = RNODE(n2)->nd_rval;
4926 $$ = dispatch2(regexp_add, n1, n2);
4928 $$ = ripper_new_yylval(p, 0, $$, s2);
4934string_content : tSTRING_CONTENT
4935 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
4938 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4939 $<strterm>$ = p->lex.strterm;
4941 SET_LEX_STATE(EXPR_BEG);
4945 p->lex.strterm = $<strterm>2;
4947 $$ = NEW_EVSTR($3, &@$);
4948 nd_set_line($$, @3.end_pos.lineno);
4950 /*% ripper: string_dvar!($3) %*/
4958 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4959 $<strterm>$ = p->lex.strterm;
4963 $<num>$ = p->lex.state;
4964 SET_LEX_STATE(EXPR_BEG);
4967 $<num>$ = p->lex.brace_nest;
4968 p->lex.brace_nest = 0;
4971 $<num>$ = p->heredoc_indent;
4972 p->heredoc_indent = 0;
4974 compstmt tSTRING_DEND
4978 p->lex.strterm = $<strterm>3;
4979 SET_LEX_STATE($<num>4);
4980 p->lex.brace_nest = $<num>5;
4981 p->heredoc_indent = $<num>6;
4982 p->heredoc_line_indent = -1;
4984 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4985 $$ = new_evstr(p, $7, &@$);
4987 /*% ripper: string_embexpr!($7) %*/
4994 $$ = NEW_GVAR($1, &@$);
4996 /*% ripper: var_ref!($1) %*/
5001 $$ = NEW_IVAR($1, &@$);
5003 /*% ripper: var_ref!($1) %*/
5008 $$ = NEW_CVAR($1, &@$);
5010 /*% ripper: var_ref!($1) %*/
5021 SET_LEX_STATE(EXPR_END);
5023 $$ = NEW_LIT(ID2SYM($2), &@$);
5025 /*% ripper: symbol_literal!(symbol!($2)) %*/
5035dsym : tSYMBEG string_contents tSTRING_END
5037 SET_LEX_STATE(EXPR_END);
5039 $$ = dsym_node(p, $2, &@$);
5041 /*% ripper: dyna_symbol!($2) %*/
5045numeric : simple_numeric
5046 | tUMINUS_NUM simple_numeric %prec tLOWEST
5050 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5052 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5056simple_numeric : tINTEGER
5067user_variable : tIDENTIFIER
5074keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5075 | keyword_self {$$ = KWD2EID(self, $1);}
5076 | keyword_true {$$ = KWD2EID(true, $1);}
5077 | keyword_false {$$ = KWD2EID(false, $1);}
5078 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5079 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5080 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5083var_ref : user_variable
5086 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5088 if (id_is_var(p, get_id($1))) {
5089 $$ = dispatch1(var_ref, $1);
5092 $$ = dispatch1(vcall, $1);
5099 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5101 /*% ripper: var_ref!($1) %*/
5105var_lhs : user_variable
5108 $$ = assignable(p, $1, 0, &@$);
5110 /*% ripper: assignable(p, var_field(p, $1)) %*/
5115 $$ = assignable(p, $1, 0, &@$);
5117 /*% ripper: assignable(p, var_field(p, $1)) %*/
5127 SET_LEX_STATE(EXPR_BEG);
5128 p->command_start = TRUE;
5139 /*% ripper: Qnil %*/
5143f_opt_paren_args: f_paren_args
5146 p->ctxt.in_argdef = 0;
5147 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5148 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5152f_paren_args : '(' f_args rparen
5157 /*% ripper: paren!($2) %*/
5158 SET_LEX_STATE(EXPR_BEG);
5159 p->command_start = TRUE;
5160 p->ctxt.in_argdef = 0;
5164f_arglist : f_paren_args
5167 p->ctxt.in_kwarg = 1;
5168 p->ctxt.in_argdef = 1;
5169 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5173 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5174 p->ctxt.in_argdef = 0;
5176 SET_LEX_STATE(EXPR_BEG);
5177 p->command_start = TRUE;
5181args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5183 $$ = new_args_tail(p, $1, $3, $4, &@3);
5185 | f_kwarg opt_f_block_arg
5187 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5189 | f_any_kwrest opt_f_block_arg
5191 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5195 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5199 add_forwarding_args(p);
5200 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5204opt_args_tail : ',' args_tail
5210 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5214f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5216 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5218 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5220 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5222 | f_arg ',' f_optarg opt_args_tail
5224 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5226 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5228 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5230 | f_arg ',' f_rest_arg opt_args_tail
5232 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5234 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5236 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5238 | f_arg opt_args_tail
5240 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5242 | f_optarg ',' f_rest_arg opt_args_tail
5244 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5246 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5248 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5250 | f_optarg opt_args_tail
5252 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5254 | f_optarg ',' f_arg opt_args_tail
5256 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5258 | f_rest_arg opt_args_tail
5260 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5262 | f_rest_arg ',' f_arg opt_args_tail
5264 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5268 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5272 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5273 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5277args_forward : tBDOT3
5282 /*% ripper: args_forward! %*/
5286f_bad_arg : tCONSTANT
5288 static const char mesg[] = "formal argument cannot be a constant";
5290 yyerror1(&@1, mesg);
5293 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5297 static const char mesg[] = "formal argument cannot be an instance variable";
5299 yyerror1(&@1, mesg);
5302 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5306 static const char mesg[] = "formal argument cannot be a global variable";
5308 yyerror1(&@1, mesg);
5311 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5315 static const char mesg[] = "formal argument cannot be a class variable";
5317 yyerror1(&@1, mesg);
5320 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5324f_norm_arg : f_bad_arg
5327 formal_argument(p, $1);
5328 p->max_numparam = ORDINAL_PARAM;
5333f_arg_asgn : f_norm_arg
5342f_arg_item : f_arg_asgn
5346 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5348 /*% ripper: get_value($1) %*/
5350 | tLPAREN f_margs rparen
5353 ID tid = internal_id(p);
5355 loc.beg_pos = @2.beg_pos;
5356 loc.end_pos = @2.beg_pos;
5358 if (dyna_in_block(p)) {
5359 $2->nd_value = NEW_DVAR(tid, &loc);
5362 $2->nd_value = NEW_LVAR(tid, &loc);
5364 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5367 /*% ripper: mlhs_paren!($2) %*/
5372 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5373 | f_arg ',' f_arg_item
5378 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5379 rb_discard_node(p, $3);
5381 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5388 arg_var(p, formal_argument(p, $1));
5389 p->cur_arg = get_id($1);
5390 p->max_numparam = ORDINAL_PARAM;
5391 p->ctxt.in_argdef = 0;
5396f_kw : f_label arg_value
5399 p->ctxt.in_argdef = 1;
5401 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5403 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5408 p->ctxt.in_argdef = 1;
5410 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5412 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5416f_block_kw : f_label primary_value
5418 p->ctxt.in_argdef = 1;
5420 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5422 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5426 p->ctxt.in_argdef = 1;
5428 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5430 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5434f_block_kwarg : f_block_kw
5439 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5441 | f_block_kwarg ',' f_block_kw
5444 $$ = kwd_append($1, $3);
5446 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5456 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5461 $$ = kwd_append($1, $3);
5463 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5471f_no_kwarg : kwrest_mark keyword_nil
5475 /*% ripper: nokw_param!(Qnil) %*/
5479f_kwrest : kwrest_mark tIDENTIFIER
5481 arg_var(p, shadowing_lvar(p, get_id($2)));
5485 /*% ripper: kwrest_param!($2) %*/
5490 $$ = internal_id(p);
5493 /*% ripper: kwrest_param!(Qnil) %*/
5497f_opt : f_arg_asgn f_eq arg_value
5500 p->ctxt.in_argdef = 1;
5502 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5504 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5508f_block_opt : f_arg_asgn f_eq primary_value
5511 p->ctxt.in_argdef = 1;
5513 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5515 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5519f_block_optarg : f_block_opt
5524 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5526 | f_block_optarg ',' f_block_opt
5529 $$ = opt_arg_append($1, $3);
5531 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5540 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5542 | f_optarg ',' f_opt
5545 $$ = opt_arg_append($1, $3);
5547 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5555f_rest_arg : restarg_mark tIDENTIFIER
5557 arg_var(p, shadowing_lvar(p, get_id($2)));
5561 /*% ripper: rest_param!($2) %*/
5566 $$ = internal_id(p);
5569 /*% ripper: rest_param!(Qnil) %*/
5577f_block_arg : blkarg_mark tIDENTIFIER
5579 arg_var(p, shadowing_lvar(p, get_id($2)));
5583 /*% ripper: blockarg!($2) %*/
5588 arg_var(p, shadowing_lvar(p, get_id(ANON_BLOCK_ID)));
5590 $$ = dispatch1(blockarg, Qnil);
5595opt_f_block_arg : ',' f_block_arg
5610 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5613 switch (nd_type($3)) {
5622 yyerror1(&@3, "can't define singleton method for literals");
5630 /*% ripper: paren!($3) %*/
5640 /*% ripper: assoclist_from_args!($1) %*/
5645 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5655 if (assocs->nd_head &&
5656 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
5657 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
5659 tail = tail->nd_next->nd_head->nd_head;
5661 assocs = list_concat(assocs, tail);
5665 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5669assoc : arg_value tASSOC arg_value
5672 if (nd_type_p($1, NODE_STR)) {
5673 nd_set_type($1, NODE_LIT);
5674 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5676 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5678 /*% ripper: assoc_new!($1, $3) %*/
5683 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5685 /*% ripper: assoc_new!($1, $2) %*/
5690 NODE *val = gettable(p, $1, &@$);
5691 if (!val) val = NEW_BEGIN(0, &@$);
5692 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
5694 /*% ripper: assoc_new!($1, Qnil) %*/
5696 | tSTRING_BEG string_contents tLABEL_END arg_value
5699 YYLTYPE loc = code_loc_gen(&@1, &@3);
5700 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5702 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
5707 if (nd_type_p($2, NODE_HASH) &&
5708 !($2->nd_head && $2->nd_head->nd_alen)) {
5709 static VALUE empty_hash;
5711 empty_hash = rb_obj_freeze(rb_hash_new());
5712 rb_gc_register_mark_object(empty_hash);
5714 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5717 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5719 /*% ripper: assoc_splat!($2) %*/
5723operation : tIDENTIFIER
5728operation2 : tIDENTIFIER
5734operation3 : tIDENTIFIER
5751opt_terms : /* none */
5762rbracket : opt_nl ']'
5773term : ';' {yyerrok;token_flush(p);}
5774 | '\n' {token_flush(p);}
5778 | terms ';' {yyerrok;}
5790# define yylval (*p->lval)
5792static int regx_options(struct parser_params*);
5793static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5794static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5795static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5796static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5799# define set_yylval_node(x) { \
5801 rb_parser_set_location(p, &_cur_loc); \
5802 yylval.node = (x); \
5804# define set_yylval_str(x) \
5806 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5807 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5809# define set_yylval_literal(x) \
5811 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5812 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5814# define set_yylval_num(x) (yylval.num = (x))
5815# define set_yylval_id(x) (yylval.id = (x))
5816# define set_yylval_name(x) (yylval.id = (x))
5817# define yylval_id() (yylval.id)
5820ripper_yylval_id(struct parser_params *p, ID x)
5822 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5824# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5825# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5826# define set_yylval_id(x) (void)(x)
5827# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5828# define set_yylval_literal(x) add_mark_object(p, (x))
5829# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
5830# define yylval_id() yylval.id
5831# define _cur_loc NULL_LOC /* dummy */
5834#define set_yylval_noname() set_yylval_id(keyword_nil)
5837#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5838#define dispatch_scan_event(p, t) ((void)0)
5839#define dispatch_delayed_token(p, t) ((void)0)
5840#define has_delayed_token(p) (0)
5842#define literal_flush(p, ptr) ((void)(ptr))
5844#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5847intern_sym(const char *name)
5849 ID id = rb_intern_const(name);
5854ripper_has_scan_event(struct parser_params *p)
5856 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5857 return p->lex.pcur > p->lex.ptok;
5861ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5863 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5864 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5870ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5872 if (!ripper_has_scan_event(p)) return;
5873 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5875#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5878ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5880 int saved_line = p->ruby_sourceline;
5881 const char *saved_tokp = p->lex.ptok;
5883 if (NIL_P(p->delayed.token)) return;
5884 p->ruby_sourceline = p->delayed.line;
5885 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5886 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5887 p->delayed.token = Qnil;
5888 p->ruby_sourceline = saved_line;
5889 p->lex.ptok = saved_tokp;
5891#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5892#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5896is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5898 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5902parser_is_identchar(struct parser_params *p)
5904 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5908parser_isascii(struct parser_params *p)
5910 return ISASCII(*(p->lex.pcur-1));
5914token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5916 int column = 1, nonspc = 0, i;
5917 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5919 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5922 if (*ptr != ' ' && *ptr != '\t') {
5927 ptinfo->beg = loc->beg_pos;
5928 ptinfo->indent = column;
5929 ptinfo->nonspc = nonspc;
5933token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5937 if (!p->token_info_enabled) return;
5938 ptinfo = ALLOC(token_info);
5939 ptinfo->token = token;
5940 ptinfo->next = p->token_info;
5941 token_info_setup(ptinfo, p->lex.pbeg, loc);
5943 p->token_info = ptinfo;
5947token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5949 token_info *ptinfo_beg = p->token_info;
5951 if (!ptinfo_beg) return;
5952 p->token_info = ptinfo_beg->next;
5954 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5955 token_info_warn(p, token, ptinfo_beg, 1, loc);
5956 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5960token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
5962 token_info *ptinfo_beg = p->token_info;
5964 if (!ptinfo_beg) return;
5965 p->token_info = ptinfo_beg->next;
5967 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
5968 ptinfo_beg->beg.column != beg_pos.column ||
5969 strcmp(ptinfo_beg->token, token)) {
5970 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
5971 beg_pos.lineno, beg_pos.column, token,
5972 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
5976 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5980token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5982 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5983 if (!p->token_info_enabled) return;
5984 if (!ptinfo_beg) return;
5985 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5986 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5987 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5988 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5989 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5990 rb_warn3L(ptinfo_end->beg.lineno,
5991 "mismatched indentations at '%s' with '%s' at %d",
5992 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5996parser_precise_mbclen(struct parser_params *p, const char *ptr)
5998 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5999 if (!MBCLEN_CHARFOUND_P(len)) {
6000 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
6007static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6010parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6013 int lineno = p->ruby_sourceline;
6017 else if (yylloc->beg_pos.lineno == lineno) {
6018 str = p->lex.lastline;
6023 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6027parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6033 yylloc = RUBY_SET_YYLLOC(current);
6035 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6036 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6040 compile_error(p, "%s", msg);
6041 parser_show_error_line(p, yylloc);
6046parser_yyerror0(struct parser_params *p, const char *msg)
6049 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6053ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6056 const int max_line_margin = 30;
6057 const char *ptr, *ptr_end, *pt, *pb;
6058 const char *pre = "", *post = "", *pend;
6059 const char *code = "", *caret = "";
6061 const char *const pbeg = RSTRING_PTR(str);
6066 if (!yylloc) return;
6067 pend = RSTRING_END(str);
6068 if (pend > pbeg && pend[-1] == '\n') {
6069 if (--pend > pbeg && pend[-1] == '\r') --pend;
6073 if (lineno == yylloc->end_pos.lineno &&
6074 (pend - pbeg) > yylloc->end_pos.column) {
6075 pt = pbeg + yylloc->end_pos.column;
6079 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6080 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6082 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6083 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6085 len = ptr_end - ptr;
6088 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6089 if (ptr > pbeg) pre = "...";
6091 if (ptr_end < pend) {
6092 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6093 if (ptr_end < pend) post = "...";
6097 if (lineno == yylloc->beg_pos.lineno) {
6098 pb += yylloc->beg_pos.column;
6099 if (pb > pt) pb = pt;
6101 if (pb < ptr) pb = ptr;
6102 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6105 if (RTEST(errbuf)) {
6106 mesg = rb_attr_get(errbuf, idMesg);
6107 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6108 rb_str_cat_cstr(mesg, "\n");
6111 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6113 if (!errbuf && rb_stderr_tty_p()) {
6114#define CSI_BEGIN "\033["
6117 CSI_BEGIN""CSI_SGR"%s" /* pre */
6118 CSI_BEGIN"1"CSI_SGR"%.*s"
6119 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6120 CSI_BEGIN";1"CSI_SGR"%.*s"
6121 CSI_BEGIN""CSI_SGR"%s" /* post */
6124 (int)(pb - ptr), ptr,
6126 (int)(ptr_end - pt), pt,
6132 len = ptr_end - ptr;
6133 lim = pt < pend ? pt : pend;
6134 i = (int)(lim - ptr);
6135 buf = ALLOCA_N(char, i+2);
6140 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6146 memset(p2, '~', (lim - ptr));
6150 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6151 pre, (int)len, code, post,
6154 if (!errbuf) rb_write_error_str(mesg);
6158parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6160 const char *pcur = 0, *ptok = 0;
6161 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6162 p->ruby_sourceline == yylloc->end_pos.lineno) {
6165 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6166 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6168 parser_yyerror0(p, msg);
6177parser_yyerror0(struct parser_params *p, const char *msg)
6179 dispatch1(parse_error, STR_NEW2(msg));
6185parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6192vtable_size(const struct vtable *tbl)
6194 if (!DVARS_TERMINAL_P(tbl)) {
6203static struct vtable *
6204vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6206 struct vtable *tbl = ALLOC(struct vtable);
6209 tbl->tbl = ALLOC_N(ID, tbl->capa);
6213 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6218#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6221vtable_free_gen(struct parser_params *p, int line, const char *name,
6226 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6229 if (!DVARS_TERMINAL_P(tbl)) {
6231 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6233 ruby_sized_xfree(tbl, sizeof(*tbl));
6236#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6239vtable_add_gen(struct parser_params *p, int line, const char *name,
6240 struct vtable *tbl, ID id)
6244 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6245 line, name, (void *)tbl, rb_id2name(id));
6248 if (DVARS_TERMINAL_P(tbl)) {
6249 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6252 if (tbl->pos == tbl->capa) {
6253 tbl->capa = tbl->capa * 2;
6254 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6256 tbl->tbl[tbl->pos++] = id;
6258#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6262vtable_pop_gen(struct parser_params *p, int line, const char *name,
6263 struct vtable *tbl, int n)
6266 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6267 line, name, (void *)tbl, n);
6270 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6275#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6279vtable_included(const struct vtable * tbl, ID id)
6283 if (!DVARS_TERMINAL_P(tbl)) {
6284 for (i = 0; i < tbl->pos; i++) {
6285 if (tbl->tbl[i] == id) {
6293static void parser_prepare(struct parser_params *p);
6296static NODE *parser_append_options(struct parser_params *p, NODE *node);
6299debug_lines(VALUE fname)
6302 CONST_ID(script_lines, "SCRIPT_LINES__");
6303 if (rb_const_defined_at(rb_cObject, script_lines)) {
6304 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6305 if (RB_TYPE_P(hash, T_HASH)) {
6306 VALUE lines = rb_ary_new();
6307 rb_hash_aset(hash, fname, lines);
6315e_option_supplied(struct parser_params *p)
6317 return strcmp(p->ruby_sourcefile, "-e") == 0;
6321yycompile0(VALUE arg)
6325 struct parser_params *p = (struct parser_params *)arg;
6328 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6329 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6330 if (p->debug_lines && p->ruby_sourceline > 0) {
6331 VALUE str = rb_default_rs;
6332 n = p->ruby_sourceline;
6334 rb_ary_push(p->debug_lines, str);
6338 if (!e_option_supplied(p)) {
6343 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6344 if (!p->debug_lines) {
6345 p->debug_lines = rb_ary_new();
6348 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6352#define RUBY_DTRACE_PARSE_HOOK(name) \
6353 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6354 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6356 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6358 RUBY_DTRACE_PARSE_HOOK(END);
6362 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6363 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6364 if (n || p->error_p) {
6365 VALUE mesg = p->error_buffer;
6367 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6369 rb_set_errinfo(mesg);
6372 tree = p->eval_tree;
6374 tree = NEW_NIL(&NULL_LOC);
6377 VALUE opt = p->compile_option;
6379 NODE *body = parser_append_options(p, tree->nd_body);
6380 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6381 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6382 prelude = block_append(p, p->eval_tree_begin, body);
6383 tree->nd_body = prelude;
6384 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6386 p->ast->body.root = tree;
6387 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6392yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6396 p->ruby_sourcefile_string = Qnil;
6397 p->ruby_sourcefile = "(none)";
6400 p->ruby_sourcefile_string = rb_fstring(fname);
6401 p->ruby_sourcefile = StringValueCStr(fname);
6403 p->ruby_sourceline = line - 1;
6407 p->ast = ast = rb_ast_new();
6408 rb_suppress_tracing(yycompile0, (VALUE)p);
6410 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6421must_be_ascii_compatible(VALUE s)
6423 rb_encoding *enc = rb_enc_get(s);
6424 if (!rb_enc_asciicompat(enc)) {
6425 rb_raise(rb_eArgError, "invalid source encoding");
6431lex_get_str(struct parser_params *p, VALUE s)
6433 char *beg, *end, *start;
6436 beg = RSTRING_PTR(s);
6437 len = RSTRING_LEN(s);
6439 if (p->lex.gets_.ptr) {
6440 if (len == p->lex.gets_.ptr) return Qnil;
6441 beg += p->lex.gets_.ptr;
6442 len -= p->lex.gets_.ptr;
6444 end = memchr(beg, '\n', len);
6445 if (end) len = ++end - beg;
6446 p->lex.gets_.ptr += len;
6447 return rb_str_subseq(s, beg - start, len);
6451lex_getline(struct parser_params *p)
6453 VALUE line = (*p->lex.gets)(p, p->lex.input);
6454 if (NIL_P(line)) return line;
6455 must_be_ascii_compatible(line);
6456 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6458 if (p->debug_lines) {
6459 rb_enc_associate(line, p->enc);
6460 rb_ary_push(p->debug_lines, line);
6467static const rb_data_type_t parser_data_type;
6471parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6473 struct parser_params *p;
6475 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6477 p->lex.gets = lex_get_str;
6478 p->lex.gets_.ptr = 0;
6479 p->lex.input = rb_str_new_frozen(s);
6480 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6482 return yycompile(vparser, p, fname, line);
6486rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6488 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6492rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6494 must_be_ascii_compatible(s);
6495 return parser_compile_string(vparser, f, s, line);
6498VALUE rb_io_gets_internal(VALUE io);
6501lex_io_gets(struct parser_params *p, VALUE io)
6503 return rb_io_gets_internal(io);
6507rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6509 struct parser_params *p;
6511 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6513 p->lex.gets = lex_io_gets;
6514 p->lex.input = file;
6515 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6517 return yycompile(vparser, p, fname, start);
6521lex_generic_gets(struct parser_params *p, VALUE input)
6523 return (*p->lex.gets_.call)(input, p->line_count);
6527rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6529 struct parser_params *p;
6531 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6533 p->lex.gets = lex_generic_gets;
6534 p->lex.gets_.call = lex_gets;
6535 p->lex.input = input;
6536 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6538 return yycompile(vparser, p, fname, start);
6542#define STR_FUNC_ESCAPE 0x01
6543#define STR_FUNC_EXPAND 0x02
6544#define STR_FUNC_REGEXP 0x04
6545#define STR_FUNC_QWORDS 0x08
6546#define STR_FUNC_SYMBOL 0x10
6547#define STR_FUNC_INDENT 0x20
6548#define STR_FUNC_LABEL 0x40
6549#define STR_FUNC_LIST 0x4000
6550#define STR_FUNC_TERM 0x8000
6553 str_label = STR_FUNC_LABEL,
6555 str_dquote = (STR_FUNC_EXPAND),
6556 str_xquote = (STR_FUNC_EXPAND),
6557 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6558 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6559 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6560 str_ssym = (STR_FUNC_SYMBOL),
6561 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6565parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6569 str = rb_enc_str_new(ptr, len, enc);
6570 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6571 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6573 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6574 rb_enc_associate(str, rb_ascii8bit_encoding());
6581#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6582#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6583#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6584#define peek(p,c) peek_n(p, (c), 0)
6585#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6586#define peekc(p) peekc_n(p, 0)
6587#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6591add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6594 if (!has_delayed_token(p)) {
6595 p->delayed.token = rb_str_buf_new(end - tok);
6596 rb_enc_associate(p->delayed.token, p->enc);
6597 p->delayed.line = p->ruby_sourceline;
6598 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6600 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6605#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6609nextline(struct parser_params *p)
6611 VALUE v = p->lex.nextline;
6612 p->lex.nextline = 0;
6617 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6621 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6629 else if (NIL_P(v)) {
6630 /* after here-document without terminator */
6633 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6634 if (p->heredoc_end > 0) {
6635 p->ruby_sourceline = p->heredoc_end;
6638 p->ruby_sourceline++;
6639 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6640 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6642 p->lex.prevline = p->lex.lastline;
6643 p->lex.lastline = v;
6648parser_cr(struct parser_params *p, int c)
6650 if (peek(p, '\n')) {
6658nextc(struct parser_params *p)
6662 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6663 if (nextline(p)) return -1;
6665 c = (unsigned char)*p->lex.pcur++;
6666 if (UNLIKELY(c == '\r')) {
6667 c = parser_cr(p, c);
6674pushback(struct parser_params *p, int c)
6676 if (c == -1) return;
6678 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6683#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6685#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6686#define tok(p) (p)->tokenbuf
6687#define toklen(p) (p)->tokidx
6690looking_at_eol_p(struct parser_params *p)
6692 const char *ptr = p->lex.pcur;
6693 while (ptr < p->lex.pend) {
6694 int c = (unsigned char)*ptr++;
6695 int eol = (c == '\n' || c == '#');
6696 if (eol || !ISSPACE(c)) {
6704newtok(struct parser_params *p)
6707 p->tokline = p->ruby_sourceline;
6710 p->tokenbuf = ALLOC_N(char, 60);
6712 if (p->toksiz > 4096) {
6714 REALLOC_N(p->tokenbuf, char, 60);
6720tokspace(struct parser_params *p, int n)
6724 if (p->tokidx >= p->toksiz) {
6725 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6726 REALLOC_N(p->tokenbuf, char, p->toksiz);
6728 return &p->tokenbuf[p->tokidx-n];
6732tokadd(struct parser_params *p, int c)
6734 p->tokenbuf[p->tokidx++] = (char)c;
6735 if (p->tokidx >= p->toksiz) {
6737 REALLOC_N(p->tokenbuf, char, p->toksiz);
6742tok_hex(struct parser_params *p, size_t *numlen)
6746 c = scan_hex(p->lex.pcur, 2, numlen);
6748 yyerror0("invalid hex escape");
6752 p->lex.pcur += *numlen;
6756#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6759escaped_control_code(int c)
6785#define WARN_SPACE_CHAR(c, prefix) \
6786 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6789tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6790 int regexp_literal, int wide)
6793 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6794 literal_flush(p, p->lex.pcur);
6795 p->lex.pcur += numlen;
6796 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6797 yyerror0("invalid Unicode escape");
6798 return wide && numlen > 0;
6800 if (codepoint > 0x10ffff) {
6801 yyerror0("invalid Unicode codepoint (too large)");
6804 if ((codepoint & 0xfffff800) == 0xd800) {
6805 yyerror0("invalid Unicode codepoint");
6808 if (regexp_literal) {
6809 tokcopy(p, (int)numlen);
6811 else if (codepoint >= 0x80) {
6812 rb_encoding *utf8 = rb_utf8_encoding();
6813 if (*encp && utf8 != *encp) {
6814 YYLTYPE loc = RUBY_INIT_YYLLOC();
6815 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6816 parser_show_error_line(p, &loc);
6820 tokaddmbc(p, codepoint, *encp);
6823 tokadd(p, codepoint);
6828/* return value is for ?\u3042 */
6830tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6831 int term, int symbol_literal, int regexp_literal)
6834 * If `term` is not -1, then we allow multiple codepoints in \u{}
6835 * upto `term` byte, otherwise we're parsing a character literal.
6836 * And then add the codepoints to the current token.
6838 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6840 const int open_brace = '{', close_brace = '}';
6842 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6844 if (peek(p, open_brace)) { /* handle \u{...} form */
6845 const char *second = NULL;
6846 int c, last = nextc(p);
6847 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6848 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6849 while (c != close_brace) {
6850 if (c == term) goto unterminated;
6851 if (second == multiple_codepoints)
6852 second = p->lex.pcur;
6853 if (regexp_literal) tokadd(p, last);
6854 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6857 while (ISSPACE(c = *p->lex.pcur)) {
6858 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6861 if (term == -1 && !second)
6862 second = multiple_codepoints;
6865 if (c != close_brace) {
6868 yyerror0("unterminated Unicode escape");
6871 if (second && second != multiple_codepoints) {
6872 const char *pcur = p->lex.pcur;
6873 p->lex.pcur = second;
6874 dispatch_scan_event(p, tSTRING_CONTENT);
6877 yyerror0(multiple_codepoints);
6881 if (regexp_literal) tokadd(p, close_brace);
6884 else { /* handle \uxxxx form */
6885 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6892#define ESCAPE_CONTROL 1
6893#define ESCAPE_META 2
6896read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6901 switch (c = nextc(p)) {
6902 case '\\': /* Backslash */
6905 case 'n': /* newline */
6908 case 't': /* horizontal tab */
6911 case 'r': /* carriage-return */
6914 case 'f': /* form-feed */
6917 case 'v': /* vertical tab */
6920 case 'a': /* alarm(bell) */
6923 case 'e': /* escape */
6926 case '0': case '1': case '2': case '3': /* octal constant */
6927 case '4': case '5': case '6': case '7':
6929 c = scan_oct(p->lex.pcur, 3, &numlen);
6930 p->lex.pcur += numlen;
6933 case 'x': /* hex constant */
6934 c = tok_hex(p, &numlen);
6935 if (numlen == 0) return 0;
6938 case 'b': /* backspace */
6941 case 's': /* space */
6945 if (flags & ESCAPE_META) goto eof;
6946 if ((c = nextc(p)) != '-') {
6949 if ((c = nextc(p)) == '\\') {
6955 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6957 else if (c == -1 || !ISASCII(c)) goto eof;
6959 int c2 = escaped_control_code(c);
6961 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6962 WARN_SPACE_CHAR(c2, "\\M-");
6965 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6968 else if (ISCNTRL(c)) goto eof;
6969 return ((c & 0xff) | 0x80);
6973 if ((c = nextc(p)) != '-') {
6977 if (flags & ESCAPE_CONTROL) goto eof;
6978 if ((c = nextc(p))== '\\') {
6984 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6988 else if (c == -1 || !ISASCII(c)) goto eof;
6990 int c2 = escaped_control_code(c);
6993 if (flags & ESCAPE_META) {
6994 WARN_SPACE_CHAR(c2, "\\M-");
6997 WARN_SPACE_CHAR(c2, "");
7001 if (flags & ESCAPE_META) {
7002 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7005 WARN_SPACE_CHAR(c2, "\\C-");
7009 else if (ISCNTRL(c)) goto eof;
7015 yyerror0("Invalid escape character syntax");
7025tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7027 int len = rb_enc_codelen(c, enc);
7028 rb_enc_mbcput(c, tokspace(p, len), enc);
7032tokadd_escape(struct parser_params *p, rb_encoding **encp)
7037 switch (c = nextc(p)) {
7039 return 0; /* just ignore */
7041 case '0': case '1': case '2': case '3': /* octal constant */
7042 case '4': case '5': case '6': case '7':
7044 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7045 if (numlen == 0) goto eof;
7046 p->lex.pcur += numlen;
7047 tokcopy(p, (int)numlen + 1);
7051 case 'x': /* hex constant */
7053 tok_hex(p, &numlen);
7054 if (numlen == 0) return -1;
7055 tokcopy(p, (int)numlen + 2);
7061 yyerror0("Invalid escape character syntax");
7073regx_options(struct parser_params *p)
7081 while (c = nextc(p), ISALPHA(c)) {
7083 options |= RE_OPTION_ONCE;
7085 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7087 if (kc != rb_ascii8bit_encindex()) kcode = c;
7101 YYLTYPE loc = RUBY_INIT_YYLLOC();
7103 compile_error(p, "unknown regexp option%s - %*s",
7104 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7105 parser_show_error_line(p, &loc);
7107 return options | RE_OPTION_ENCODING(kcode);
7111tokadd_mbchar(struct parser_params *p, int c)
7113 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7114 if (len < 0) return -1;
7116 p->lex.pcur += --len;
7117 if (len > 0) tokcopy(p, len);
7122simple_re_meta(int c)
7125 case '$': case '*': case '+': case '.':
7126 case '?': case '^': case '|':
7127 case ')': case ']': case '}': case '>':
7135parser_update_heredoc_indent(struct parser_params *p, int c)
7137 if (p->heredoc_line_indent == -1) {
7138 if (c == '\n') p->heredoc_line_indent = 0;
7142 p->heredoc_line_indent++;
7145 else if (c == '\t') {
7146 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7147 p->heredoc_line_indent = w * TAB_WIDTH;
7150 else if (c != '\n') {
7151 if (p->heredoc_indent > p->heredoc_line_indent) {
7152 p->heredoc_indent = p->heredoc_line_indent;
7154 p->heredoc_line_indent = -1;
7161parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7163 YYLTYPE loc = RUBY_INIT_YYLLOC();
7164 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7165 compile_error(p, "%s mixed within %s source", n1, n2);
7166 parser_show_error_line(p, &loc);
7170parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7172 const char *pos = p->lex.pcur;
7174 parser_mixed_error(p, enc1, enc2);
7179tokadd_string(struct parser_params *p,
7180 int func, int term, int paren, long *nest,
7181 rb_encoding **encp, rb_encoding **enc)
7186#define mixed_error(enc1, enc2) \
7187 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7188#define mixed_escape(beg, enc1, enc2) \
7189 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7191 while ((c = nextc(p)) != -1) {
7192 if (p->heredoc_indent > 0) {
7193 parser_update_heredoc_indent(p, c);
7196 if (paren && c == paren) {
7199 else if (c == term) {
7200 if (!nest || !*nest) {
7206 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7207 int c2 = *p->lex.pcur;
7208 if (c2 == '$' || c2 == '@' || c2 == '{') {
7213 else if (c == '\\') {
7214 literal_flush(p, p->lex.pcur - 1);
7218 if (func & STR_FUNC_QWORDS) break;
7219 if (func & STR_FUNC_EXPAND) {
7220 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7231 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7235 if ((func & STR_FUNC_EXPAND) == 0) {
7239 tokadd_utf8(p, enc, term,
7240 func & STR_FUNC_SYMBOL,
7241 func & STR_FUNC_REGEXP);
7245 if (c == -1) return -1;
7247 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7250 if (func & STR_FUNC_REGEXP) {
7256 c = read_escape(p, 0, enc);
7260 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7261 for (i = 0; i < 4; i++) {
7262 tokadd(p, escbuf[i]);
7268 if (c == term && !simple_re_meta(c)) {
7273 if ((c = tokadd_escape(p, enc)) < 0)
7275 if (*enc && *enc != *encp) {
7276 mixed_escape(p->lex.ptok+2, *enc, *encp);
7280 else if (func & STR_FUNC_EXPAND) {
7282 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7283 c = read_escape(p, 0, enc);
7285 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7286 /* ignore backslashed spaces in %w */
7288 else if (c != term && !(paren && c == paren)) {
7295 else if (!parser_isascii(p)) {
7300 else if (*enc != *encp) {
7301 mixed_error(*enc, *encp);
7304 if (tokadd_mbchar(p, c) == -1) return -1;
7307 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7315 else if (*enc != *encp) {
7316 mixed_error(*enc, *encp);
7323 if (*enc) *encp = *enc;
7327static inline rb_strterm_t *
7328new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7330 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7333/* imemo_parser_strterm for literal */
7334#define NEW_STRTERM(func, term, paren) \
7335 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7339flush_string_content(struct parser_params *p, rb_encoding *enc)
7341 VALUE content = yylval.val;
7342 if (!ripper_is_node_yylval(content))
7343 content = ripper_new_yylval(p, 0, 0, content);
7344 if (has_delayed_token(p)) {
7345 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7347 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7349 dispatch_delayed_token(p, tSTRING_CONTENT);
7350 p->lex.ptok = p->lex.pcur;
7351 RNODE(content)->nd_rval = yylval.val;
7353 dispatch_scan_event(p, tSTRING_CONTENT);
7354 if (yylval.val != content)
7355 RNODE(content)->nd_rval = yylval.val;
7356 yylval.val = content;
7359#define flush_string_content(p, enc) ((void)(enc))
7362RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7363/* this can be shared with ripper, since it's independent from struct
7366#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7367#define SPECIAL_PUNCT(idx) ( \
7368 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7369 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7370 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7371 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7372 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7374const unsigned int ruby_global_name_punct_bits[] = {
7383static enum yytokentype
7384parser_peek_variable_name(struct parser_params *p)
7387 const char *ptr = p->lex.pcur;
7389 if (ptr + 1 >= p->lex.pend) return 0;
7393 if ((c = *ptr) == '-') {
7394 if (++ptr >= p->lex.pend) return 0;
7397 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7398 return tSTRING_DVAR;
7402 if ((c = *ptr) == '@') {
7403 if (++ptr >= p->lex.pend) return 0;
7409 p->command_start = TRUE;
7410 return tSTRING_DBEG;
7414 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7415 return tSTRING_DVAR;
7419#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7420#define IS_END() IS_lex_state(EXPR_END_ANY)
7421#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7422#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7423#define IS_LABEL_POSSIBLE() (\
7424 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7426#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7427#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7429static inline enum yytokentype
7430parser_string_term(struct parser_params *p, int func)
7433 if (func & STR_FUNC_REGEXP) {
7434 set_yylval_num(regx_options(p));
7435 dispatch_scan_event(p, tREGEXP_END);
7436 SET_LEX_STATE(EXPR_END);
7439 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7441 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7444 SET_LEX_STATE(EXPR_END);
7448static enum yytokentype
7449parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7451 int func = (int)quote->u1.func;
7452 int term = (int)quote->u3.term;
7453 int paren = (int)quote->u2.paren;
7455 rb_encoding *enc = p->enc;
7456 rb_encoding *base_enc = 0;
7459 if (func & STR_FUNC_TERM) {
7460 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7461 SET_LEX_STATE(EXPR_END);
7463 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7466 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7467 do {c = nextc(p);} while (ISSPACE(c));
7470 if (func & STR_FUNC_LIST) {
7471 quote->u1.func &= ~STR_FUNC_LIST;
7474 if (c == term && !quote->u0.nest) {
7475 if (func & STR_FUNC_QWORDS) {
7476 quote->u1.func |= STR_FUNC_TERM;
7477 pushback(p, c); /* dispatch the term at tSTRING_END */
7478 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7481 return parser_string_term(p, func);
7485 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7489 if ((func & STR_FUNC_EXPAND) && c == '#') {
7490 int t = parser_peek_variable_name(p);
7496 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7497 &enc, &base_enc) == -1) {
7500# define unterminated_literal(mesg) yyerror0(mesg)
7502# define unterminated_literal(mesg) compile_error(p, mesg)
7504 literal_flush(p, p->lex.pcur);
7505 if (func & STR_FUNC_QWORDS) {
7506 /* no content to add, bailing out here */
7507 unterminated_literal("unterminated list meets end of file");
7511 if (func & STR_FUNC_REGEXP) {
7512 unterminated_literal("unterminated regexp meets end of file");
7515 unterminated_literal("unterminated string meets end of file");
7517 quote->u1.func |= STR_FUNC_TERM;
7522 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7523 set_yylval_str(lit);
7524 flush_string_content(p, enc);
7526 return tSTRING_CONTENT;
7529static enum yytokentype
7530heredoc_identifier(struct parser_params *p)
7533 * term_len is length of `<<"END"` except `END`,
7534 * in this case term_len is 4 (<, <, " and ").
7536 long len, offset = p->lex.pcur - p->lex.pbeg;
7537 int c = nextc(p), term, func = 0, quote = 0;
7538 enum yytokentype token = tSTRING_BEG;
7543 func = STR_FUNC_INDENT;
7546 else if (c == '~') {
7548 func = STR_FUNC_INDENT;
7554 func |= str_squote; goto quoted;
7556 func |= str_dquote; goto quoted;
7558 token = tXSTRING_BEG;
7559 func |= str_xquote; goto quoted;
7566 while ((c = nextc(p)) != term) {
7567 if (c == -1 || c == '\r' || c == '\n') {
7568 yyerror0("unterminated here document identifier");
7575 if (!parser_is_identchar(p)) {
7577 if (func & STR_FUNC_INDENT) {
7578 pushback(p, indent > 0 ? '~' : '-');
7584 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7585 if (n < 0) return 0;
7587 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7592 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7593 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7594 yyerror0("too long here document identifier");
7595 dispatch_scan_event(p, tHEREDOC_BEG);
7598 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7599 p->lex.strterm->flags |= STRTERM_HEREDOC;
7600 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7601 here->offset = offset;
7602 here->sourceline = p->ruby_sourceline;
7603 here->length = (int)len;
7604 here->quote = quote;
7608 p->heredoc_indent = indent;
7609 p->heredoc_line_indent = 0;
7614heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7619 line = here->lastline;
7620 p->lex.lastline = line;
7621 p->lex.pbeg = RSTRING_PTR(line);
7622 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7623 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7624 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7625 p->heredoc_end = p->ruby_sourceline;
7626 p->ruby_sourceline = (int)here->sourceline;
7627 if (p->eofp) p->lex.nextline = Qnil;
7632dedent_string(VALUE string, int width)
7638 RSTRING_GETMEM(string, str, len);
7639 for (i = 0; i < len && col < width; i++) {
7640 if (str[i] == ' ') {
7643 else if (str[i] == '\t') {
7644 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7645 if (n > width) break;
7653 rb_str_modify(string);
7654 str = RSTRING_PTR(string);
7655 if (RSTRING_LEN(string) != len)
7656 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7657 MEMMOVE(str, str + i, char, len - i);
7658 rb_str_set_len(string, len - i);
7664heredoc_dedent(struct parser_params *p, NODE *root)
7666 NODE *node, *str_node, *prev_node;
7667 int indent = p->heredoc_indent;
7670 if (indent <= 0) return root;
7671 p->heredoc_indent = 0;
7672 if (!root) return root;
7674 prev_node = node = str_node = root;
7675 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
7678 VALUE lit = str_node->nd_lit;
7679 if (str_node->flags & NODE_FL_NEWLINE) {
7680 dedent_string(lit, indent);
7685 else if (!literal_concat0(p, prev_lit, lit)) {
7689 NODE *end = node->nd_end;
7690 node = prev_node->nd_next = node->nd_next;
7692 if (nd_type_p(prev_node, NODE_DSTR))
7693 nd_set_type(prev_node, NODE_STR);
7701 while ((node = (prev_node = node)->nd_next) != 0) {
7703 if (!nd_type_p(node, NODE_LIST)) break;
7704 if ((str_node = node->nd_head) != 0) {
7705 enum node_type type = nd_type(str_node);
7706 if (type == NODE_STR || type == NODE_DSTR) break;
7716heredoc_dedent(struct parser_params *p, VALUE array)
7718 int indent = p->heredoc_indent;
7720 if (indent <= 0) return array;
7721 p->heredoc_indent = 0;
7722 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7728 * Ripper.dedent_string(input, width) -> Integer
7730 * USE OF RIPPER LIBRARY ONLY.
7732 * Strips up to +width+ leading whitespaces from +input+,
7733 * and returns the stripped column width.
7736parser_dedent_string(VALUE self, VALUE input, VALUE width)
7741 wid = NUM2UINT(width);
7742 col = dedent_string(input, wid);
7743 return INT2NUM(col);
7748whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7750 const char *ptr = p->lex.pbeg;
7754 while (*ptr && ISSPACE(*ptr)) ptr++;
7756 n = p->lex.pend - (ptr + len);
7757 if (n < 0) return FALSE;
7758 if (n > 0 && ptr[len] != '\n') {
7759 if (ptr[len] != '\r') return FALSE;
7760 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7762 return strncmp(eos, ptr, len) == 0;
7766word_match_p(struct parser_params *p, const char *word, long len)
7768 if (strncmp(p->lex.pcur, word, len)) return 0;
7769 if (p->lex.pcur + len == p->lex.pend) return 1;
7770 int c = (unsigned char)p->lex.pcur[len];
7771 if (ISSPACE(c)) return 1;
7773 case '\0': case '\004': case '\032': return 1;
7778#define NUM_SUFFIX_R (1<<0)
7779#define NUM_SUFFIX_I (1<<1)
7780#define NUM_SUFFIX_ALL 3
7783number_literal_suffix(struct parser_params *p, int mask)
7786 const char *lastp = p->lex.pcur;
7788 while ((c = nextc(p)) != -1) {
7789 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7790 result |= (mask & NUM_SUFFIX_I);
7791 mask &= ~NUM_SUFFIX_I;
7792 /* r after i, rational of complex is disallowed */
7793 mask &= ~NUM_SUFFIX_R;
7796 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7797 result |= (mask & NUM_SUFFIX_R);
7798 mask &= ~NUM_SUFFIX_R;
7801 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7802 p->lex.pcur = lastp;
7803 literal_flush(p, p->lex.pcur);
7812static enum yytokentype
7813set_number_literal(struct parser_params *p, VALUE v,
7814 enum yytokentype type, int suffix)
7816 if (suffix & NUM_SUFFIX_I) {
7817 v = rb_complex_raw(INT2FIX(0), v);
7820 set_yylval_literal(v);
7821 SET_LEX_STATE(EXPR_END);
7825static enum yytokentype
7826set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7828 enum yytokentype type = tINTEGER;
7829 if (suffix & NUM_SUFFIX_R) {
7830 v = rb_rational_raw1(v);
7833 return set_number_literal(p, v, type, suffix);
7838dispatch_heredoc_end(struct parser_params *p)
7841 if (has_delayed_token(p))
7842 dispatch_delayed_token(p, tSTRING_CONTENT);
7843 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7844 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7850#define dispatch_heredoc_end(p) ((void)0)
7853static enum yytokentype
7854here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7856 int c, func, indent = 0;
7857 const char *eos, *ptr, *ptr_end;
7860 rb_encoding *enc = p->enc;
7861 rb_encoding *base_enc = 0;
7864 eos = RSTRING_PTR(here->lastline) + here->offset;
7866 indent = (func = here->func) & STR_FUNC_INDENT;
7868 if ((c = nextc(p)) == -1) {
7871 if (!has_delayed_token(p)) {
7872 dispatch_scan_event(p, tSTRING_CONTENT);
7875 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7876 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7877 int cr = ENC_CODERANGE_UNKNOWN;
7878 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7879 if (cr != ENC_CODERANGE_7BIT &&
7880 p->enc == rb_usascii_encoding() &&
7881 enc != rb_utf8_encoding()) {
7882 enc = rb_ascii8bit_encoding();
7885 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7887 dispatch_delayed_token(p, tSTRING_CONTENT);
7891 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7892 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7896 SET_LEX_STATE(EXPR_END);
7901 /* not beginning of line, cannot be the terminator */
7903 else if (p->heredoc_line_indent == -1) {
7904 /* `heredoc_line_indent == -1` means
7905 * - "after an interpolation in the same line", or
7906 * - "in a continuing line"
7908 p->heredoc_line_indent = 0;
7910 else if (whole_match_p(p, eos, len, indent)) {
7911 dispatch_heredoc_end(p);
7913 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7916 SET_LEX_STATE(EXPR_END);
7920 if (!(func & STR_FUNC_EXPAND)) {
7922 ptr = RSTRING_PTR(p->lex.lastline);
7923 ptr_end = p->lex.pend;
7924 if (ptr_end > ptr) {
7925 switch (ptr_end[-1]) {
7927 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7936 if (p->heredoc_indent > 0) {
7938 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7940 p->heredoc_line_indent = 0;
7944 rb_str_cat(str, ptr, ptr_end - ptr);
7946 str = STR_NEW(ptr, ptr_end - ptr);
7947 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7949 if (p->heredoc_indent > 0) {
7952 if (nextc(p) == -1) {
7958 } while (!whole_match_p(p, eos, len, indent));
7961 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7964 int t = parser_peek_variable_name(p);
7965 if (p->heredoc_line_indent != -1) {
7966 if (p->heredoc_indent > p->heredoc_line_indent) {
7967 p->heredoc_indent = p->heredoc_line_indent;
7969 p->heredoc_line_indent = -1;
7978 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7979 if (p->eofp) goto error;
7983 if (c == '\\') p->heredoc_line_indent = -1;
7985 str = STR_NEW3(tok(p), toklen(p), enc, func);
7987 set_yylval_str(str);
7989 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7991 flush_string_content(p, enc);
7992 return tSTRING_CONTENT;
7994 tokadd(p, nextc(p));
7995 if (p->heredoc_indent > 0) {
7999 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8000 if ((c = nextc(p)) == -1) goto error;
8001 } while (!whole_match_p(p, eos, len, indent));
8002 str = STR_NEW3(tok(p), toklen(p), enc, func);
8004 dispatch_heredoc_end(p);
8006 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8009 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8011 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8012 set_yylval_str(str);
8014 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8016 return tSTRING_CONTENT;
8022arg_ambiguous(struct parser_params *p, char c)
8026 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8029 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8032 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8039formal_argument(struct parser_params *p, ID lhs)
8041formal_argument(struct parser_params *p, VALUE lhs)
8044 ID id = get_id(lhs);
8046 switch (id_type(id)) {
8050# define ERR(mesg) yyerror0(mesg)
8052# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8055 ERR("formal argument cannot be a constant");
8058 ERR("formal argument cannot be an instance variable");
8061 ERR("formal argument cannot be a global variable");
8064 ERR("formal argument cannot be a class variable");
8067 ERR("formal argument must be local variable");
8071 shadowing_lvar(p, id);
8076lvar_defined(struct parser_params *p, ID id)
8078 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8081/* emacsen -*- hack */
8083parser_encode_length(struct parser_params *p, const char *name, long len)
8087 if (len > 5 && name[nlen = len - 5] == '-') {
8088 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8091 if (len > 4 && name[nlen = len - 4] == '-') {
8092 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8094 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8095 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8096 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8103parser_set_encode(struct parser_params *p, const char *name)
8105 int idx = rb_enc_find_index(name);
8110 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8112 excargs[0] = rb_eArgError;
8113 excargs[2] = rb_make_backtrace();
8114 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8115 rb_exc_raise(rb_make_exception(3, excargs));
8117 enc = rb_enc_from_index(idx);
8118 if (!rb_enc_asciicompat(enc)) {
8119 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8124 if (p->debug_lines) {
8125 VALUE lines = p->debug_lines;
8126 long i, n = RARRAY_LEN(lines);
8127 for (i = 0; i < n; ++i) {
8128 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8135comment_at_top(struct parser_params *p)
8137 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8138 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8139 while (ptr < ptr_end) {
8140 if (!ISSPACE(*ptr)) return 0;
8146typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8147typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8149static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8152magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8154 if (!comment_at_top(p)) {
8157 parser_set_encode(p, val);
8161parser_get_bool(struct parser_params *p, const char *name, const char *val)
8165 if (STRCASECMP(val, "true") == 0) {
8170 if (STRCASECMP(val, "false") == 0) {
8175 return parser_invalid_pragma_value(p, name, val);
8179parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8181 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8186parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8188 int b = parser_get_bool(p, name, val);
8189 if (b >= 0) p->token_info_enabled = b;
8193parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8197 if (p->token_seen) {
8198 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8202 b = parser_get_bool(p, name, val);
8205 if (!p->compile_option)
8206 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8207 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8212parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8214 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8215 if (*s == ' ' || *s == '\t') continue;
8216 if (*s == '#') break;
8217 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8223 if (STRCASECMP(val, "none") == 0) {
8224 p->ctxt.shareable_constant_value = shareable_none;
8229 if (STRCASECMP(val, "literal") == 0) {
8230 p->ctxt.shareable_constant_value = shareable_literal;
8235 if (STRCASECMP(val, "experimental_copy") == 0) {
8236 p->ctxt.shareable_constant_value = shareable_copy;
8239 if (STRCASECMP(val, "experimental_everything") == 0) {
8240 p->ctxt.shareable_constant_value = shareable_everything;
8245 parser_invalid_pragma_value(p, name, val);
8250parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8252 int b = parser_get_bool(p, name, val);
8253 if (b >= 0) p->past_scope_enabled = b;
8257struct magic_comment {
8259 rb_magic_comment_setter_t func;
8260 rb_magic_comment_length_t length;
8263static const struct magic_comment magic_comments[] = {
8264 {"coding", magic_comment_encoding, parser_encode_length},
8265 {"encoding", magic_comment_encoding, parser_encode_length},
8266 {"frozen_string_literal", parser_set_compile_option_flag},
8267 {"shareable_constant_value", parser_set_shareable_constant_value},
8268 {"warn_indent", parser_set_token_info},
8270 {"warn_past_scope", parser_set_past_scope},
8275magic_comment_marker(const char *str, long len)
8282 if (str[i-1] == '*' && str[i-2] == '-') {
8288 if (i + 1 >= len) return 0;
8289 if (str[i+1] != '-') {
8292 else if (str[i-1] != '-') {
8308parser_magic_comment(struct parser_params *p, const char *str, long len)
8311 VALUE name = 0, val = 0;
8312 const char *beg, *end, *vbeg, *vend;
8313#define str_copy(_s, _p, _n) ((_s) \
8314 ? (void)(rb_str_resize((_s), (_n)), \
8315 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8316 : (void)((_s) = STR_NEW((_p), (_n))))
8318 if (len <= 7) return FALSE;
8319 if (!!(beg = magic_comment_marker(str, len))) {
8320 if (!(end = magic_comment_marker(beg, str + len - beg)))
8324 len = end - beg - 3;
8327 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8329 const struct magic_comment *mc = magic_comments;
8334 for (; len > 0 && *str; str++, --len) {
8336 case '\'': case '"': case ':': case ';':
8339 if (!ISSPACE(*str)) break;
8341 for (beg = str; len > 0; str++, --len) {
8343 case '\'': case '"': case ':': case ';':
8346 if (ISSPACE(*str)) break;
8351 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8354 if (!indicator) return FALSE;
8358 do str++; while (--len > 0 && ISSPACE(*str));
8361 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8374 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8378 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8381 while (len > 0 && (ISSPACE(*str))) --len, str++;
8382 if (len) return FALSE;
8386 str_copy(name, beg, n);
8387 s = RSTRING_PTR(name);
8388 for (i = 0; i < n; ++i) {
8389 if (s[i] == '-') s[i] = '_';
8392 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8395 n = (*mc->length)(p, vbeg, n);
8397 str_copy(val, vbeg, n);
8398 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8401 } while (++mc < magic_comments + numberof(magic_comments));
8403 str_copy(val, vbeg, vend - vbeg);
8404 dispatch2(magic_comment, name, val);
8412set_file_encoding(struct parser_params *p, const char *str, const char *send)
8415 const char *beg = str;
8419 if (send - str <= 6) return;
8421 case 'C': case 'c': str += 6; continue;
8422 case 'O': case 'o': str += 5; continue;
8423 case 'D': case 'd': str += 4; continue;
8424 case 'I': case 'i': str += 3; continue;
8425 case 'N': case 'n': str += 2; continue;
8426 case 'G': case 'g': str += 1; continue;
8433 if (ISSPACE(*str)) break;
8436 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8441 if (++str >= send) return;
8442 } while (ISSPACE(*str));
8444 if (*str != '=' && *str != ':') return;
8449 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8450 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8451 parser_set_encode(p, RSTRING_PTR(s));
8452 rb_str_resize(s, 0);
8456parser_prepare(struct parser_params *p)
8459 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8462 if (peek(p, '!')) p->has_shebang = 1;
8464 case 0xef: /* UTF-8 BOM marker */
8465 if (p->lex.pend - p->lex.pcur >= 2 &&
8466 (unsigned char)p->lex.pcur[0] == 0xbb &&
8467 (unsigned char)p->lex.pcur[1] == 0xbf) {
8468 p->enc = rb_utf8_encoding();
8470 p->lex.pbeg = p->lex.pcur;
8478 p->enc = rb_enc_get(p->lex.lastline);
8482#define ambiguous_operator(tok, op, syn) ( \
8483 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8484 rb_warning0("even though it seems like "syn""))
8486#define ambiguous_operator(tok, op, syn) \
8487 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8489#define warn_balanced(tok, op, syn) ((void) \
8490 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8491 space_seen && !ISSPACE(c) && \
8492 (ambiguous_operator(tok, op, syn), 0)), \
8493 (enum yytokentype)(tok))
8496parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8499 char *point = &str[seen_point];
8500 size_t fraclen = len-seen_point-1;
8501 memmove(point, point+1, fraclen+1);
8502 v = rb_cstr_to_inum(str, 10, FALSE);
8503 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8506static enum yytokentype
8507no_digits(struct parser_params *p)
8509 yyerror0("numeric literal without digits");
8510 if (peek(p, '_')) nextc(p);
8511 /* dummy 0, for tUMINUS_NUM at numeric */
8512 return set_integer_literal(p, INT2FIX(0), 0);
8515static enum yytokentype
8516parse_numeric(struct parser_params *p, int c)
8518 int is_float, seen_point, seen_e, nondigit;
8521 is_float = seen_point = seen_e = nondigit = 0;
8522 SET_LEX_STATE(EXPR_END);
8524 if (c == '-' || c == '+') {
8529 int start = toklen(p);
8531 if (c == 'x' || c == 'X') {
8534 if (c != -1 && ISXDIGIT(c)) {
8537 if (nondigit) break;
8541 if (!ISXDIGIT(c)) break;
8544 } while ((c = nextc(p)) != -1);
8548 if (toklen(p) == start) {
8549 return no_digits(p);
8551 else if (nondigit) goto trailing_uc;
8552 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8553 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8555 if (c == 'b' || c == 'B') {
8558 if (c == '0' || c == '1') {
8561 if (nondigit) break;
8565 if (c != '0' && c != '1') break;
8568 } while ((c = nextc(p)) != -1);
8572 if (toklen(p) == start) {
8573 return no_digits(p);
8575 else if (nondigit) goto trailing_uc;
8576 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8577 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8579 if (c == 'd' || c == 'D') {
8582 if (c != -1 && ISDIGIT(c)) {
8585 if (nondigit) break;
8589 if (!ISDIGIT(c)) break;
8592 } while ((c = nextc(p)) != -1);
8596 if (toklen(p) == start) {
8597 return no_digits(p);
8599 else if (nondigit) goto trailing_uc;
8600 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8601 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8607 if (c == 'o' || c == 'O') {
8608 /* prefixed octal */
8610 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8611 return no_digits(p);
8614 if (c >= '0' && c <= '7') {
8619 if (nondigit) break;
8623 if (c < '0' || c > '9') break;
8624 if (c > '7') goto invalid_octal;
8627 } while ((c = nextc(p)) != -1);
8628 if (toklen(p) > start) {
8631 if (nondigit) goto trailing_uc;
8632 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8633 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8640 if (c > '7' && c <= '9') {
8642 yyerror0("Invalid octal digit");
8644 else if (c == '.' || c == 'e' || c == 'E') {
8649 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8650 return set_integer_literal(p, INT2FIX(0), suffix);
8656 case '0': case '1': case '2': case '3': case '4':
8657 case '5': case '6': case '7': case '8': case '9':
8663 if (nondigit) goto trailing_uc;
8664 if (seen_point || seen_e) {
8669 if (c0 == -1 || !ISDIGIT(c0)) {
8675 seen_point = toklen(p);
8694 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8699 tokadd(p, nondigit);
8703 nondigit = (c == '-' || c == '+') ? c : 0;
8706 case '_': /* `_' in number just ignored */
8707 if (nondigit) goto decode_num;
8721 literal_flush(p, p->lex.pcur - 1);
8722 YYLTYPE loc = RUBY_INIT_YYLLOC();
8723 compile_error(p, "trailing `%c' in number", nondigit);
8724 parser_show_error_line(p, &loc);
8728 enum yytokentype type = tFLOAT;
8731 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8732 if (suffix & NUM_SUFFIX_R) {
8734 v = parse_rational(p, tok(p), toklen(p), seen_point);
8737 double d = strtod(tok(p), 0);
8738 if (errno == ERANGE) {
8739 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8744 return set_number_literal(p, v, type, suffix);
8746 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8747 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8750static enum yytokentype
8751parse_qmark(struct parser_params *p, int space_seen)
8758 SET_LEX_STATE(EXPR_VALUE);
8763 compile_error(p, "incomplete character syntax");
8766 if (rb_enc_isspace(c, p->enc)) {
8768 int c2 = escaped_control_code(c);
8770 WARN_SPACE_CHAR(c2, "?");
8775 SET_LEX_STATE(EXPR_VALUE);
8780 if (!parser_isascii(p)) {
8781 if (tokadd_mbchar(p, c) == -1) return 0;
8783 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8784 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8786 const char *start = p->lex.pcur - 1, *ptr = start;
8788 int n = parser_precise_mbclen(p, ptr);
8789 if (n < 0) return -1;
8791 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8792 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8793 " a conditional operator, put a space after `?'",
8794 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8798 else if (c == '\\') {
8801 enc = rb_utf8_encoding();
8802 tokadd_utf8(p, &enc, -1, 0, 0);
8804 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8806 if (tokadd_mbchar(p, c) == -1) return 0;
8809 c = read_escape(p, 0, &enc);
8817 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8818 set_yylval_str(lit);
8819 SET_LEX_STATE(EXPR_END);
8823static enum yytokentype
8824parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8827 const char *ptok = p->lex.pcur;
8835 if (c == -1) goto unterminated;
8838 if (!ISASCII(c)) goto unknown;
8843 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8846 c = parser_precise_mbclen(p, p->lex.pcur);
8847 if (c < 0) return 0;
8849 yyerror0("unknown type of %string");
8855 compile_error(p, "unterminated quoted string meets end of file");
8859 if (term == '(') term = ')';
8860 else if (term == '[') term = ']';
8861 else if (term == '{') term = '}';
8862 else if (term == '<') term = '>';
8865 p->lex.ptok = ptok-1;
8868 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8872 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8876 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8880 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8884 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8885 return tSYMBOLS_BEG;
8888 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8889 return tQSYMBOLS_BEG;
8892 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8893 return tXSTRING_BEG;
8896 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8900 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8901 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8905 yyerror0("unknown type of %string");
8909 if ((c = nextc(p)) == '=') {
8911 SET_LEX_STATE(EXPR_BEG);
8914 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8917 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8919 return warn_balanced('%', "%%", "string literal");
8923tokadd_ident(struct parser_params *p, int c)
8926 if (tokadd_mbchar(p, c) == -1) return -1;
8928 } while (parser_is_identchar(p));
8934tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8936 ID ident = TOK_INTERN();
8938 set_yylval_name(ident);
8944parse_numvar(struct parser_params *p)
8948 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8949 const unsigned long nth_ref_max =
8950 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8951 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8952 * turned into a Fixnum, in compile.c */
8954 if (overflow || n > nth_ref_max) {
8955 /* compile_error()? */
8956 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8957 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8964static enum yytokentype
8965parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8967 const char *ptr = p->lex.pcur;
8970 SET_LEX_STATE(EXPR_END);
8971 p->lex.ptok = ptr - 1; /* from '$' */
8975 case '_': /* $_: last read line string */
8977 if (parser_is_identchar(p)) {
8985 case '~': /* $~: match-data */
8986 case '*': /* $*: argv */
8987 case '$': /* $$: pid */
8988 case '?': /* $?: last status */
8989 case '!': /* $!: error string */
8990 case '@': /* $@: error position */
8991 case '/': /* $/: input record separator */
8992 case '\\': /* $\: output record separator */
8993 case ';': /* $;: field separator */
8994 case ',': /* $,: output field separator */
8995 case '.': /* $.: last read line number */
8996 case '=': /* $=: ignorecase */
8997 case ':': /* $:: load path */
8998 case '<': /* $<: reading filename */
8999 case '>': /* $>: default output handle */
9000 case '\"': /* $": already loaded files */
9009 if (parser_is_identchar(p)) {
9010 if (tokadd_mbchar(p, c) == -1) return 0;
9018 set_yylval_name(TOK_INTERN());
9021 case '&': /* $&: last match */
9022 case '`': /* $`: string before last match */
9023 case '\'': /* $': string after last match */
9024 case '+': /* $+: string matches last paren. */
9025 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9030 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9033 case '1': case '2': case '3':
9034 case '4': case '5': case '6':
9035 case '7': case '8': case '9':
9040 } while (c != -1 && ISDIGIT(c));
9042 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9044 c = parse_numvar(p);
9045 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9049 if (!parser_is_identchar(p)) {
9050 YYLTYPE loc = RUBY_INIT_YYLLOC();
9051 if (c == -1 || ISSPACE(c)) {
9052 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9056 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9058 parser_show_error_line(p, &loc);
9059 set_yylval_noname();
9067 if (tokadd_ident(p, c)) return 0;
9068 SET_LEX_STATE(EXPR_END);
9069 tokenize_ident(p, last_state);
9075parser_numbered_param(struct parser_params *p, int n)
9077 if (n < 0) return false;
9079 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9082 if (p->max_numparam == ORDINAL_PARAM) {
9083 compile_error(p, "ordinary parameter is defined");
9086 struct vtable *args = p->lvtbl->args;
9087 if (p->max_numparam < n) {
9088 p->max_numparam = n;
9090 while (n > args->pos) {
9091 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9097static enum yytokentype
9098parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9100 const char *ptr = p->lex.pcur;
9101 enum yytokentype result = tIVAR;
9102 register int c = nextc(p);
9105 p->lex.ptok = ptr - 1; /* from '@' */
9113 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9114 if (c == -1 || !parser_is_identchar(p)) {
9116 RUBY_SET_YYLLOC(loc);
9117 if (result == tIVAR) {
9118 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9121 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9123 parser_show_error_line(p, &loc);
9124 set_yylval_noname();
9125 SET_LEX_STATE(EXPR_END);
9128 else if (ISDIGIT(c)) {
9130 RUBY_SET_YYLLOC(loc);
9131 if (result == tIVAR) {
9132 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9135 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9137 parser_show_error_line(p, &loc);
9138 set_yylval_noname();
9139 SET_LEX_STATE(EXPR_END);
9143 if (tokadd_ident(p, c)) return 0;
9144 tokenize_ident(p, last_state);
9148static enum yytokentype
9149parse_ident(struct parser_params *p, int c, int cmd_state)
9151 enum yytokentype result;
9152 int mb = ENC_CODERANGE_7BIT;
9153 const enum lex_state_e last_state = p->lex.state;
9157 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9158 if (tokadd_mbchar(p, c) == -1) return 0;
9160 } while (parser_is_identchar(p));
9161 if ((c == '!' || c == '?') && !peek(p, '=')) {
9165 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9166 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9167 result = tIDENTIFIER;
9171 result = tCONSTANT; /* assume provisionally */
9176 if (IS_LABEL_POSSIBLE()) {
9177 if (IS_LABEL_SUFFIX(0)) {
9178 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9180 set_yylval_name(TOK_INTERN());
9184 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9185 const struct kwtable *kw;
9187 /* See if it is a reserved word. */
9188 kw = rb_reserved_word(tok(p), toklen(p));
9190 enum lex_state_e state = p->lex.state;
9191 if (IS_lex_state_for(state, EXPR_FNAME)) {
9192 SET_LEX_STATE(EXPR_ENDFN);
9193 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9196 SET_LEX_STATE(kw->state);
9197 if (IS_lex_state(EXPR_BEG)) {
9198 p->command_start = TRUE;
9200 if (kw->id[0] == keyword_do) {
9201 if (lambda_beginning_p()) {
9202 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9203 return keyword_do_LAMBDA;
9205 if (COND_P()) return keyword_do_cond;
9206 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9207 return keyword_do_block;
9210 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9213 if (kw->id[0] != kw->id[1])
9214 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9220 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9222 SET_LEX_STATE(EXPR_CMDARG);
9225 SET_LEX_STATE(EXPR_ARG);
9228 else if (p->lex.state == EXPR_FNAME) {
9229 SET_LEX_STATE(EXPR_ENDFN);
9232 SET_LEX_STATE(EXPR_END);
9235 ident = tokenize_ident(p, last_state);
9236 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9237 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9238 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9239 lvar_defined(p, ident)) {
9240 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9245static enum yytokentype
9246parser_yylex(struct parser_params *p)
9252 enum lex_state_e last_state;
9253 int fallthru = FALSE;
9254 int token_seen = p->token_seen;
9256 if (p->lex.strterm) {
9257 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9258 return here_document(p, &p->lex.strterm->u.heredoc);
9262 return parse_string(p, &p->lex.strterm->u.literal);
9265 cmd_state = p->command_start;
9266 p->command_start = FALSE;
9267 p->token_seen = TRUE;
9269 last_state = p->lex.state;
9273 switch (c = nextc(p)) {
9274 case '\0': /* NUL */
9275 case '\004': /* ^D */
9276 case '\032': /* ^Z */
9277 case -1: /* end of script. */
9284 /* carried over with p->lex.nextline for nextc() */
9285 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9288 case ' ': case '\t': case '\f':
9289 case '\13': /* '\v' */
9292 while ((c = nextc(p))) {
9294 case ' ': case '\t': case '\f': case '\r':
9295 case '\13': /* '\v' */
9303 dispatch_scan_event(p, tSP);
9307 case '#': /* it's a comment */
9308 p->token_seen = token_seen;
9309 /* no magic_comment in shebang line */
9310 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9311 if (comment_at_top(p)) {
9312 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9316 dispatch_scan_event(p, tCOMMENT);
9320 p->token_seen = token_seen;
9321 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9322 !IS_lex_state(EXPR_LABELED));
9323 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9325 dispatch_scan_event(p, tIGNORED_NL);
9328 if (!c && p->ctxt.in_kwarg) {
9329 goto normal_newline;
9334 switch (c = nextc(p)) {
9335 case ' ': case '\t': case '\f': case '\r':
9336 case '\13': /* '\v' */
9341 if (space_seen) dispatch_scan_event(p, tSP);
9345 dispatch_delayed_token(p, tIGNORED_NL);
9346 if (peek(p, '.') == (c == '&')) {
9348 dispatch_scan_event(p, tSP);
9353 p->ruby_sourceline--;
9354 p->lex.nextline = p->lex.lastline;
9355 case -1: /* EOF no decrement*/
9357 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9358 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9359 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9360 pushback(p, 1); /* always pushback */
9361 p->lex.ptok = p->lex.pcur;
9365 p->lex.ptok = p->lex.pcur;
9368 goto normal_newline;
9372 p->command_start = TRUE;
9373 SET_LEX_STATE(EXPR_BEG);
9377 if ((c = nextc(p)) == '*') {
9378 if ((c = nextc(p)) == '=') {
9379 set_yylval_id(idPow);
9380 SET_LEX_STATE(EXPR_BEG);
9385 rb_warning0("`**' interpreted as argument prefix");
9388 else if (IS_BEG()) {
9392 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9398 SET_LEX_STATE(EXPR_BEG);
9403 rb_warning0("`*' interpreted as argument prefix");
9406 else if (IS_BEG()) {
9410 c = warn_balanced('*', "*", "argument prefix");
9413 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9418 if (IS_AFTER_OPERATOR()) {
9419 SET_LEX_STATE(EXPR_ARG);
9425 SET_LEX_STATE(EXPR_BEG);
9438 /* skip embedded rd document */
9439 if (word_match_p(p, "begin", 5)) {
9443 dispatch_scan_event(p, tEMBDOC_BEG);
9447 dispatch_scan_event(p, tEMBDOC);
9452 compile_error(p, "embedded document meets end of file");
9455 if (c == '=' && word_match_p(p, "end", 3)) {
9461 dispatch_scan_event(p, tEMBDOC_END);
9466 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9467 if ((c = nextc(p)) == '=') {
9468 if ((c = nextc(p)) == '=') {
9477 else if (c == '>') {
9486 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9488 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9489 int token = heredoc_identifier(p);
9490 if (token) return token < 0 ? 0 : token;
9492 if (IS_AFTER_OPERATOR()) {
9493 SET_LEX_STATE(EXPR_ARG);
9496 if (IS_lex_state(EXPR_CLASS))
9497 p->command_start = TRUE;
9498 SET_LEX_STATE(EXPR_BEG);
9501 if ((c = nextc(p)) == '>') {
9508 if ((c = nextc(p)) == '=') {
9509 set_yylval_id(idLTLT);
9510 SET_LEX_STATE(EXPR_BEG);
9514 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9520 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9521 if ((c = nextc(p)) == '=') {
9525 if ((c = nextc(p)) == '=') {
9526 set_yylval_id(idGTGT);
9527 SET_LEX_STATE(EXPR_BEG);
9537 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9538 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9539 p->lex.ptok = p->lex.pcur-1;
9543 if (IS_lex_state(EXPR_FNAME)) {
9544 SET_LEX_STATE(EXPR_ENDFN);
9547 if (IS_lex_state(EXPR_DOT)) {
9549 SET_LEX_STATE(EXPR_CMDARG);
9551 SET_LEX_STATE(EXPR_ARG);
9554 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9555 return tXSTRING_BEG;
9558 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9559 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9560 p->lex.ptok = p->lex.pcur-1;
9564 return parse_qmark(p, space_seen);
9567 if ((c = nextc(p)) == '&') {
9568 SET_LEX_STATE(EXPR_BEG);
9569 if ((c = nextc(p)) == '=') {
9570 set_yylval_id(idANDOP);
9571 SET_LEX_STATE(EXPR_BEG);
9577 else if (c == '=') {
9579 SET_LEX_STATE(EXPR_BEG);
9582 else if (c == '.') {
9583 set_yylval_id(idANDDOT);
9584 SET_LEX_STATE(EXPR_DOT);
9590 (c = peekc_n(p, 1)) == -1 ||
9591 !(c == '\'' || c == '"' ||
9592 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9593 rb_warning0("`&' interpreted as argument prefix");
9597 else if (IS_BEG()) {
9601 c = warn_balanced('&', "&", "argument prefix");
9603 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9607 if ((c = nextc(p)) == '|') {
9608 SET_LEX_STATE(EXPR_BEG);
9609 if ((c = nextc(p)) == '=') {
9610 set_yylval_id(idOROP);
9611 SET_LEX_STATE(EXPR_BEG);
9615 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9624 SET_LEX_STATE(EXPR_BEG);
9627 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9633 if (IS_AFTER_OPERATOR()) {
9634 SET_LEX_STATE(EXPR_ARG);
9643 SET_LEX_STATE(EXPR_BEG);
9646 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9647 SET_LEX_STATE(EXPR_BEG);
9649 if (c != -1 && ISDIGIT(c)) {
9650 return parse_numeric(p, '+');
9654 SET_LEX_STATE(EXPR_BEG);
9656 return warn_balanced('+', "+", "unary operator");
9660 if (IS_AFTER_OPERATOR()) {
9661 SET_LEX_STATE(EXPR_ARG);
9670 SET_LEX_STATE(EXPR_BEG);
9674 SET_LEX_STATE(EXPR_ENDFN);
9677 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9678 SET_LEX_STATE(EXPR_BEG);
9680 if (c != -1 && ISDIGIT(c)) {
9685 SET_LEX_STATE(EXPR_BEG);
9687 return warn_balanced('-', "-", "unary operator");
9690 int is_beg = IS_BEG();
9691 SET_LEX_STATE(EXPR_BEG);
9692 if ((c = nextc(p)) == '.') {
9693 if ((c = nextc(p)) == '.') {
9694 if (p->ctxt.in_argdef) {
9695 SET_LEX_STATE(EXPR_ENDARG);
9698 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9699 rb_warn0("... at EOL, should be parenthesized?");
9701 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9702 if (IS_lex_state_for(last_state, EXPR_LABEL))
9705 return is_beg ? tBDOT3 : tDOT3;
9708 return is_beg ? tBDOT2 : tDOT2;
9711 if (c != -1 && ISDIGIT(c)) {
9712 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9713 parse_numeric(p, '.');
9714 if (ISDIGIT(prev)) {
9715 yyerror0("unexpected fraction part after numeric literal");
9718 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9720 SET_LEX_STATE(EXPR_END);
9721 p->lex.ptok = p->lex.pcur;
9725 SET_LEX_STATE(EXPR_DOT);
9729 case '0': case '1': case '2': case '3': case '4':
9730 case '5': case '6': case '7': case '8': case '9':
9731 return parse_numeric(p, c);
9736 SET_LEX_STATE(EXPR_ENDFN);
9737 p->lex.paren_nest--;
9743 SET_LEX_STATE(EXPR_END);
9744 p->lex.paren_nest--;
9748 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9749 if (!p->lex.brace_nest--) return tSTRING_DEND;
9752 SET_LEX_STATE(EXPR_END);
9753 p->lex.paren_nest--;
9759 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9760 SET_LEX_STATE(EXPR_BEG);
9763 set_yylval_id(idCOLON2);
9764 SET_LEX_STATE(EXPR_DOT);
9767 if (IS_END() || ISSPACE(c) || c == '#') {
9769 c = warn_balanced(':', ":", "symbol literal");
9770 SET_LEX_STATE(EXPR_BEG);
9775 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9778 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9784 SET_LEX_STATE(EXPR_FNAME);
9789 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9792 if ((c = nextc(p)) == '=') {
9794 SET_LEX_STATE(EXPR_BEG);
9799 arg_ambiguous(p, '/');
9800 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9803 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9804 return warn_balanced('/', "/", "regexp literal");
9807 if ((c = nextc(p)) == '=') {
9809 SET_LEX_STATE(EXPR_BEG);
9812 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9817 SET_LEX_STATE(EXPR_BEG);
9818 p->command_start = TRUE;
9822 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9826 if (IS_AFTER_OPERATOR()) {
9827 if ((c = nextc(p)) != '@') {
9830 SET_LEX_STATE(EXPR_ARG);
9833 SET_LEX_STATE(EXPR_BEG);
9841 else if (!space_seen) {
9842 /* foo( ... ) => method call, no ambiguity */
9844 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9847 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9848 rb_warning0("parentheses after method name is interpreted as "
9849 "an argument list, not a decomposed argument");
9851 p->lex.paren_nest++;
9854 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9858 p->lex.paren_nest++;
9859 if (IS_AFTER_OPERATOR()) {
9860 if ((c = nextc(p)) == ']') {
9861 p->lex.paren_nest--;
9862 SET_LEX_STATE(EXPR_ARG);
9863 if ((c = nextc(p)) == '=') {
9870 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9873 else if (IS_BEG()) {
9876 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9879 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9885 ++p->lex.brace_nest;
9886 if (lambda_beginning_p())
9888 else if (IS_lex_state(EXPR_LABELED))
9889 c = tLBRACE; /* hash */
9890 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9891 c = '{'; /* block (primary) */
9892 else if (IS_lex_state(EXPR_ENDARG))
9893 c = tLBRACE_ARG; /* block (expr) */
9895 c = tLBRACE; /* hash */
9897 p->command_start = TRUE;
9898 SET_LEX_STATE(EXPR_BEG);
9901 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9903 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9912 dispatch_scan_event(p, tSP);
9913 goto retry; /* skip \\n */
9915 if (c == ' ') return tSP;
9916 if (ISSPACE(c)) return c;
9921 return parse_percent(p, space_seen, last_state);
9924 return parse_gvar(p, last_state);
9927 return parse_atmark(p, last_state);
9930 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9931 p->ruby__end__seen = 1;
9937 dispatch_scan_event(p, k__END__);
9945 if (!parser_is_identchar(p)) {
9946 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9955 return parse_ident(p, c, cmd_state);
9958static enum yytokentype
9959yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9965 t = parser_yylex(p);
9967 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9968 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9970 RUBY_SET_YYLLOC(*yylloc);
9972 if (has_delayed_token(p))
9973 dispatch_delayed_token(p, t);
9975 dispatch_scan_event(p, t);
9980#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9983node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9985 NODE *n = rb_ast_newnode(p->ast, type);
9987 rb_node_init(n, type, a0, a1, a2);
9990 nd_set_node_id(n, parser_get_node_id(p));
9995nd_set_loc(NODE *nd, const YYLTYPE *loc)
9998 nd_set_line(nd, loc->beg_pos.lineno);
10003static enum node_type
10004nodetype(NODE *node) /* for debug */
10006 return (enum node_type)nd_type(node);
10010nodeline(NODE *node)
10012 return nd_line(node);
10016newline_node(NODE *node)
10019 node = remove_begin(node);
10020 node->flags |= NODE_FL_NEWLINE;
10026fixpos(NODE *node, NODE *orig)
10030 nd_set_line(node, nd_line(orig));
10034parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10036 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10040parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10042 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10046block_append(struct parser_params *p, NODE *head, NODE *tail)
10048 NODE *end, *h = head, *nd;
10050 if (tail == 0) return head;
10052 if (h == 0) return tail;
10053 switch (nd_type(h)) {
10060 parser_warning(p, h, "unused literal ignored");
10063 h = end = NEW_BLOCK(head, &head->nd_loc);
10073 switch (nd_type(nd)) {
10079 if (RTEST(ruby_verbose)) {
10080 parser_warning(p, tail, "statement not reached");
10088 if (!nd_type_p(tail, NODE_BLOCK)) {
10089 tail = NEW_BLOCK(tail, &tail->nd_loc);
10090 tail->nd_end = tail;
10092 end->nd_next = tail;
10093 h->nd_end = tail->nd_end;
10094 nd_set_last_loc(head, nd_last_loc(tail));
10098/* append item to the list */
10100list_append(struct parser_params *p, NODE *list, NODE *item)
10104 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10105 if (list->nd_next) {
10106 last = list->nd_next->nd_end;
10112 list->nd_alen += 1;
10113 last->nd_next = NEW_LIST(item, &item->nd_loc);
10114 list->nd_next->nd_end = last->nd_next;
10116 nd_set_last_loc(list, nd_last_loc(item));
10121/* concat two lists */
10123list_concat(NODE *head, NODE *tail)
10127 if (head->nd_next) {
10128 last = head->nd_next->nd_end;
10134 head->nd_alen += tail->nd_alen;
10135 last->nd_next = tail;
10136 if (tail->nd_next) {
10137 head->nd_next->nd_end = tail->nd_next->nd_end;
10140 head->nd_next->nd_end = tail;
10143 nd_set_last_loc(head, nd_last_loc(tail));
10149literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10151 if (NIL_P(tail)) return 1;
10152 if (!rb_enc_compatible(head, tail)) {
10153 compile_error(p, "string literal encodings differ (%s / %s)",
10154 rb_enc_name(rb_enc_get(head)),
10155 rb_enc_name(rb_enc_get(tail)));
10156 rb_str_resize(head, 0);
10157 rb_str_resize(tail, 0);
10160 rb_str_buf_append(head, tail);
10165string_literal_head(enum node_type htype, NODE *head)
10167 if (htype != NODE_DSTR) return Qfalse;
10168 if (head->nd_next) {
10169 head = head->nd_next->nd_end->nd_head;
10170 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10172 const VALUE lit = head->nd_lit;
10173 ASSUME(lit != Qfalse);
10177/* concat two string literals */
10179literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10181 enum node_type htype;
10184 if (!head) return tail;
10185 if (!tail) return head;
10187 htype = nd_type(head);
10188 if (htype == NODE_EVSTR) {
10189 head = new_dstr(p, head, loc);
10192 if (p->heredoc_indent > 0) {
10195 nd_set_type(head, NODE_DSTR);
10197 return list_append(p, head, tail);
10202 switch (nd_type(tail)) {
10204 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10208 lit = head->nd_lit;
10210 if (htype == NODE_STR) {
10211 if (!literal_concat0(p, lit, tail->nd_lit)) {
10213 rb_discard_node(p, head);
10214 rb_discard_node(p, tail);
10217 rb_discard_node(p, tail);
10220 list_append(p, head, tail);
10225 if (htype == NODE_STR) {
10226 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10228 tail->nd_lit = head->nd_lit;
10229 rb_discard_node(p, head);
10232 else if (NIL_P(tail->nd_lit)) {
10234 head->nd_alen += tail->nd_alen - 1;
10235 if (!head->nd_next) {
10236 head->nd_next = tail->nd_next;
10238 else if (tail->nd_next) {
10239 head->nd_next->nd_end->nd_next = tail->nd_next;
10240 head->nd_next->nd_end = tail->nd_next->nd_end;
10242 rb_discard_node(p, tail);
10244 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10245 if (!literal_concat0(p, lit, tail->nd_lit))
10247 tail->nd_lit = Qnil;
10251 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10256 if (htype == NODE_STR) {
10257 nd_set_type(head, NODE_DSTR);
10260 list_append(p, head, tail);
10267evstr2dstr(struct parser_params *p, NODE *node)
10269 if (nd_type_p(node, NODE_EVSTR)) {
10270 node = new_dstr(p, node, &node->nd_loc);
10276new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10281 switch (nd_type(node)) {
10283 nd_set_type(node, NODE_DSTR);
10291 return NEW_EVSTR(head, loc);
10295new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10297 VALUE lit = STR_NEW0();
10298 NODE *dstr = NEW_DSTR(lit, loc);
10299 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10300 return list_append(p, dstr, node);
10304call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10305 const YYLTYPE *op_loc, const YYLTYPE *loc)
10310 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10311 nd_set_line(expr, op_loc->beg_pos.lineno);
10316call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10320 opcall = NEW_OPCALL(recv, id, 0, loc);
10321 nd_set_line(opcall, op_loc->beg_pos.lineno);
10326new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10328 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10329 nd_set_line(qcall, op_loc->beg_pos.lineno);
10334new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10337 if (block) block_dup_check(p, args, block);
10338 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10339 if (block) ret = method_add_block(p, ret, block, loc);
10344#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10346match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10349 int line = op_loc->beg_pos.lineno;
10353 if (node1 && (n = nd_once_body(node1)) != 0) {
10354 switch (nd_type(n)) {
10357 NODE *match = NEW_MATCH2(node1, node2, loc);
10358 nd_set_line(match, line);
10363 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10364 const VALUE lit = n->nd_lit;
10365 NODE *match = NEW_MATCH2(node1, node2, loc);
10366 match->nd_args = reg_named_capture_assign(p, lit, loc);
10367 nd_set_line(match, line);
10373 if (node2 && (n = nd_once_body(node2)) != 0) {
10376 switch (nd_type(n)) {
10378 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10381 match3 = NEW_MATCH3(node2, node1, loc);
10386 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10387 nd_set_line(n, line);
10391# if WARN_PAST_SCOPE
10393past_dvar_p(struct parser_params *p, ID id)
10395 struct vtable *past = p->lvtbl->past;
10397 if (vtable_included(past, id)) return 1;
10405numparam_nested_p(struct parser_params *p)
10407 struct local_vars *local = p->lvtbl;
10408 NODE *outer = local->numparam.outer;
10409 NODE *inner = local->numparam.inner;
10410 if (outer || inner) {
10411 NODE *used = outer ? outer : inner;
10412 compile_error(p, "numbered parameter is already used in\n"
10413 "%s:%d: %s block here",
10414 p->ruby_sourcefile, nd_line(used),
10415 outer ? "outer" : "inner");
10416 parser_show_error_line(p, &used->nd_loc);
10423gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10429 return NEW_SELF(loc);
10431 return NEW_NIL(loc);
10433 return NEW_TRUE(loc);
10434 case keyword_false:
10435 return NEW_FALSE(loc);
10436 case keyword__FILE__:
10438 VALUE file = p->ruby_sourcefile_string;
10440 file = rb_str_new(0, 0);
10442 file = rb_str_dup(file);
10443 node = NEW_STR(file, loc);
10444 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10447 case keyword__LINE__:
10448 return NEW_LIT(INT2FIX(p->tokline), loc);
10449 case keyword__ENCODING__:
10450 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10451 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10455 switch (id_type(id)) {
10457 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10458 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10459 if (id == p->cur_arg) {
10460 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10463 if (vidp) *vidp |= LVAR_USED;
10464 node = NEW_DVAR(id, loc);
10467 if (local_id_ref(p, id, &vidp)) {
10468 if (id == p->cur_arg) {
10469 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10472 if (vidp) *vidp |= LVAR_USED;
10473 node = NEW_LVAR(id, loc);
10476 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10477 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10478 if (numparam_nested_p(p)) return 0;
10479 node = NEW_DVAR(id, loc);
10480 struct local_vars *local = p->lvtbl;
10481 if (!local->numparam.current) local->numparam.current = node;
10484# if WARN_PAST_SCOPE
10485 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10486 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10489 /* method call without arguments */
10490 return NEW_VCALL(id, loc);
10492 return NEW_GVAR(id, loc);
10494 return NEW_IVAR(id, loc);
10496 return NEW_CONST(id, loc);
10498 return NEW_CVAR(id, loc);
10500 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10505opt_arg_append(NODE *opt_list, NODE *opt)
10507 NODE *opts = opt_list;
10508 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10510 while (opts->nd_next) {
10511 opts = opts->nd_next;
10512 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10514 opts->nd_next = opt;
10520kwd_append(NODE *kwlist, NODE *kw)
10523 NODE *kws = kwlist;
10524 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10525 while (kws->nd_next) {
10526 kws = kws->nd_next;
10527 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10535new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10537 return NEW_DEFINED(remove_begin_all(expr), loc);
10541symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10543 enum node_type type = nd_type(symbol);
10546 nd_set_type(symbol, NODE_DSYM);
10549 nd_set_type(symbol, NODE_LIT);
10550 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10553 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10555 return list_append(p, symbols, symbol);
10559new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10565 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10566 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10569 switch (nd_type(node)) {
10572 VALUE src = node->nd_lit;
10573 nd_set_type(node, NODE_LIT);
10574 nd_set_loc(node, loc);
10575 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10580 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10581 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10584 nd_set_type(node, NODE_DREGX);
10585 nd_set_loc(node, loc);
10586 node->nd_cflag = options & RE_OPTION_MASK;
10587 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10588 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10589 NODE *frag = list->nd_head;
10590 enum node_type type = nd_type(frag);
10591 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10592 VALUE tail = frag->nd_lit;
10593 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10594 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10595 if (!literal_concat0(p, lit, tail)) {
10596 return NEW_NIL(loc); /* dummy node on error */
10598 rb_str_resize(tail, 0);
10599 prev->nd_next = list->nd_next;
10600 rb_discard_node(p, list->nd_head);
10601 rb_discard_node(p, list);
10612 if (!node->nd_next) {
10613 VALUE src = node->nd_lit;
10614 nd_set_type(node, NODE_LIT);
10615 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10617 if (options & RE_OPTION_ONCE) {
10618 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10626new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10629 return NEW_KW_ARG(0, (k), loc);
10633new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10636 VALUE lit = STR_NEW0();
10637 NODE *xstr = NEW_XSTR(lit, loc);
10638 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10641 switch (nd_type(node)) {
10643 nd_set_type(node, NODE_XSTR);
10644 nd_set_loc(node, loc);
10647 nd_set_type(node, NODE_DXSTR);
10648 nd_set_loc(node, loc);
10651 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10658check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10662 if (!arg || !p->case_labels) return;
10664 lit = rb_node_case_when_optimizable_literal(arg);
10665 if (lit == Qundef) return;
10666 if (nd_type_p(arg, NODE_STR)) {
10667 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10670 if (NIL_P(p->case_labels)) {
10671 p->case_labels = rb_obj_hide(rb_hash_new());
10674 VALUE line = rb_hash_lookup(p->case_labels, lit);
10675 if (!NIL_P(line)) {
10676 rb_warning1("duplicated `when' clause with line %d is ignored",
10681 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10686id_is_var(struct parser_params *p, ID id)
10688 if (is_notop_id(id)) {
10689 switch (id & ID_SCOPE_MASK) {
10690 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10693 if (dyna_in_block(p)) {
10694 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10696 if (local_id(p, id)) return 1;
10697 /* method call without arguments */
10701 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10706new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10708 VALUE src = 0, err;
10710 if (ripper_is_node_yylval(re)) {
10711 src = RNODE(re)->nd_cval;
10712 re = RNODE(re)->nd_rval;
10714 if (ripper_is_node_yylval(opt)) {
10715 options = (int)RNODE(opt)->nd_tag;
10716 opt = RNODE(opt)->nd_rval;
10718 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10719 compile_error(p, "%"PRIsVALUE, err);
10721 return dispatch2(regexp_literal, re, opt);
10723#endif /* !RIPPER */
10725static inline enum lex_state_e
10726parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
10729 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
10731 return p->lex.state = ls;
10735static const char rb_parser_lex_state_names[][8] = {
10736 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10737 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10738 "LABEL", "LABELED","FITEM",
10742append_lex_state_name(enum lex_state_e state, VALUE buf)
10745 unsigned int mask = 1;
10746 static const char none[] = "NONE";
10748 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10749 if ((unsigned)state & mask) {
10751 rb_str_cat(buf, "|", 1);
10754 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10758 rb_str_cat(buf, none, sizeof(none)-1);
10764flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10766 VALUE mesg = p->debug_buffer;
10768 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10769 p->debug_buffer = Qnil;
10770 rb_io_puts(1, &mesg, out);
10772 if (!NIL_P(str) && RSTRING_LEN(str)) {
10773 rb_io_write(p->debug_output, str);
10778rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10779 enum lex_state_e to, int line)
10782 mesg = rb_str_new_cstr("lex_state: ");
10783 append_lex_state_name(from, mesg);
10784 rb_str_cat_cstr(mesg, " -> ");
10785 append_lex_state_name(to, mesg);
10786 rb_str_catf(mesg, " at line %d\n", line);
10787 flush_debug_buffer(p, p->debug_output, mesg);
10792rb_parser_lex_state_name(enum lex_state_e state)
10794 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10798append_bitstack_value(stack_type stack, VALUE mesg)
10801 rb_str_cat_cstr(mesg, "0");
10804 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10805 for (; mask && !(stack & mask); mask >>= 1) continue;
10806 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10811rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10812 const char *name, int line)
10814 VALUE mesg = rb_sprintf("%s: ", name);
10815 append_bitstack_value(stack, mesg);
10816 rb_str_catf(mesg, " at line %d\n", line);
10817 flush_debug_buffer(p, p->debug_output, mesg);
10821rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10824 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10827 rb_str_vcatf(mesg, fmt, ap);
10829 yyerror0(RSTRING_PTR(mesg));
10832 mesg = rb_str_new(0, 0);
10833 append_lex_state_name(p->lex.state, mesg);
10834 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10835 rb_str_resize(mesg, 0);
10836 append_bitstack_value(p->cond_stack, mesg);
10837 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10838 rb_str_resize(mesg, 0);
10839 append_bitstack_value(p->cmdarg_stack, mesg);
10840 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10841 if (p->debug_output == rb_ractor_stdout())
10842 p->debug_output = rb_ractor_stderr();
10847rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
10849 yylloc->beg_pos.lineno = sourceline;
10850 yylloc->beg_pos.column = beg_pos;
10851 yylloc->end_pos.lineno = sourceline;
10852 yylloc->end_pos.column = end_pos;
10857rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10859 int sourceline = here->sourceline;
10860 int beg_pos = (int)here->offset - here->quote
10861 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10862 int end_pos = (int)here->offset + here->length + here->quote;
10864 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10868rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10870 int sourceline = p->ruby_sourceline;
10871 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10872 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
10873 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10877rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10879 int sourceline = p->ruby_sourceline;
10880 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10881 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
10882 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10884#endif /* !RIPPER */
10887assignable0(struct parser_params *p, ID id, const char **err)
10889 if (!id) return -1;
10892 *err = "Can't change the value of self";
10895 *err = "Can't assign to nil";
10898 *err = "Can't assign to true";
10900 case keyword_false:
10901 *err = "Can't assign to false";
10903 case keyword__FILE__:
10904 *err = "Can't assign to __FILE__";
10906 case keyword__LINE__:
10907 *err = "Can't assign to __LINE__";
10909 case keyword__ENCODING__:
10910 *err = "Can't assign to __ENCODING__";
10913 switch (id_type(id)) {
10915 if (dyna_in_block(p)) {
10916 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10917 compile_error(p, "Can't assign to numbered parameter _%d",
10918 NUMPARAM_ID_TO_IDX(id));
10921 if (dvar_curr(p, id)) return NODE_DASGN;
10922 if (dvar_defined(p, id)) return NODE_DASGN;
10923 if (local_id(p, id)) return NODE_LASGN;
10928 if (!local_id(p, id)) local_var(p, id);
10932 case ID_GLOBAL: return NODE_GASGN;
10933 case ID_INSTANCE: return NODE_IASGN;
10935 if (!p->ctxt.in_def) return NODE_CDECL;
10936 *err = "dynamic constant assignment";
10938 case ID_CLASS: return NODE_CVASGN;
10940 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10947assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10949 const char *err = 0;
10950 int node_type = assignable0(p, id, &err);
10951 switch (node_type) {
10952 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10953 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10954 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10955 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10956 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10957 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10959 if (err) yyerror1(loc, err);
10960 return NEW_BEGIN(0, loc);
10964assignable(struct parser_params *p, VALUE lhs)
10966 const char *err = 0;
10967 assignable0(p, get_id(lhs), &err);
10968 if (err) lhs = assign_error(p, err, lhs);
10974is_private_local_id(ID name)
10977 if (name == idUScore) return 1;
10978 if (!is_local_id(name)) return 0;
10979 s = rb_id2str(name);
10981 return RSTRING_PTR(s)[0] == '_';
10985shadowing_lvar_0(struct parser_params *p, ID name)
10987 if (is_private_local_id(name)) return 1;
10988 if (dyna_in_block(p)) {
10989 if (dvar_curr(p, name)) {
10990 yyerror0("duplicated argument name");
10992 else if (dvar_defined(p, name) || local_id(p, name)) {
10993 vtable_add(p->lvtbl->vars, name);
10994 if (p->lvtbl->used) {
10995 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11001 if (local_id(p, name)) {
11002 yyerror0("duplicated argument name");
11009shadowing_lvar(struct parser_params *p, ID name)
11011 shadowing_lvar_0(p, name);
11016new_bv(struct parser_params *p, ID name)
11019 if (!is_local_id(name)) {
11020 compile_error(p, "invalid local variable - %"PRIsVALUE,
11024 if (!shadowing_lvar_0(p, name)) return;
11030aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11032 return NEW_ATTRASGN(recv, tASET, idx, loc);
11036block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11038 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11039 compile_error(p, "both block arg and actual block given");
11044attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11046 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11047 return NEW_ATTRASGN(recv, id, 0, loc);
11051rb_backref_error(struct parser_params *p, NODE *node)
11053 switch (nd_type(node)) {
11055 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11057 case NODE_BACK_REF:
11058 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11064backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11066 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11067 rb_str_append(mesg, ref->nd_cval);
11068 return dispatch2(assign_error, mesg, expr);
11074arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11076 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11077 switch (nd_type(node1)) {
11079 return list_append(p, node1, node2);
11080 case NODE_BLOCK_PASS:
11081 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11082 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11084 case NODE_ARGSPUSH:
11085 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11086 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11087 nd_set_type(node1, NODE_ARGSCAT);
11090 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11091 node1->nd_body = list_append(p, node1->nd_body, node2);
11092 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11095 return NEW_ARGSPUSH(node1, node2, loc);
11099arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11101 if (!node2) return node1;
11102 switch (nd_type(node1)) {
11103 case NODE_BLOCK_PASS:
11104 if (node1->nd_head)
11105 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11107 node1->nd_head = NEW_LIST(node2, loc);
11109 case NODE_ARGSPUSH:
11110 if (!nd_type_p(node2, NODE_LIST)) break;
11111 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11112 nd_set_type(node1, NODE_ARGSCAT);
11115 if (!nd_type_p(node2, NODE_LIST) ||
11116 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11117 node1->nd_body = list_concat(node1->nd_body, node2);
11120 return NEW_ARGSCAT(node1, node2, loc);
11124last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11127 if ((n1 = splat_array(args)) != 0) {
11128 return list_append(p, n1, last_arg);
11130 return arg_append(p, args, last_arg, loc);
11134rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11137 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11138 return list_concat(n1, rest_arg);
11140 return arg_concat(p, args, rest_arg, loc);
11144splat_array(NODE* node)
11146 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11147 if (nd_type_p(node, NODE_LIST)) return node;
11152mark_lvar_used(struct parser_params *p, NODE *rhs)
11156 switch (nd_type(rhs)) {
11158 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11159 if (vidp) *vidp |= LVAR_USED;
11163 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11164 if (vidp) *vidp |= LVAR_USED;
11169 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11170 mark_lvar_used(p, rhs->nd_head);
11178const_decl_path(struct parser_params *p, NODE **dest)
11181 if (!nd_type_p(n, NODE_CALL)) {
11182 const YYLTYPE *loc = &n->nd_loc;
11185 path = rb_id2str(n->nd_vid);
11189 path = rb_ary_new();
11190 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11191 rb_ary_push(path, rb_id2str(n->nd_mid));
11193 if (n && nd_type_p(n, NODE_CONST)) {
11195 rb_ary_push(path, rb_id2str(n->nd_vid));
11197 else if (n && nd_type_p(n, NODE_COLON3)) {
11199 rb_ary_push(path, rb_str_new(0, 0));
11202 // expression::Name
11203 rb_ary_push(path, rb_str_new_cstr("..."));
11205 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11206 path = rb_fstring(path);
11208 *dest = n = NEW_LIT(path, loc);
11209 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11214extern VALUE rb_mRubyVMFrozenCore;
11217make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11219 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11222 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11223 NEW_LIST(value, loc), loc);
11226 return NEW_CALL(fcore, rb_intern("make_shareable"),
11227 NEW_LIST(value, loc), loc);
11232ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11234 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11235 NODE *args = NEW_LIST(value, loc);
11236 args = list_append(p, args, const_decl_path(p, dest));
11237 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11240static int is_static_content(NODE *node);
11243shareable_literal_value(NODE *node)
11245 if (!node) return Qnil;
11246 enum node_type type = nd_type(node);
11255 return node->nd_lit;
11261#ifndef SHAREABLE_BARE_EXPRESSION
11262#define SHAREABLE_BARE_EXPRESSION 1
11266shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11267 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11269# define shareable_literal_constant_next(n) \
11270 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11273 if (!value) return 0;
11274 enum node_type type = nd_type(value);
11283 if (shareable == shareable_literal) {
11284 value = NEW_CALL(value, idUMinus, 0, loc);
11289 lit = rb_fstring(value->nd_lit);
11290 nd_set_type(value, NODE_LIT);
11291 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11295 lit = rb_ary_new();
11296 OBJ_FREEZE_RAW(lit);
11297 NODE *n = NEW_LIT(lit, loc);
11298 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11302 lit = rb_ary_new();
11303 for (NODE *n = value; n; n = n->nd_next) {
11304 NODE *elt = n->nd_head;
11306 elt = shareable_literal_constant_next(elt);
11310 else if (RTEST(lit)) {
11316 VALUE e = shareable_literal_value(elt);
11318 rb_ary_push(lit, e);
11322 lit = Qnil; /* make shareable at runtime */
11329 if (!value->nd_brace) return 0;
11330 lit = rb_hash_new();
11331 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11332 NODE *key = n->nd_head;
11333 NODE *val = n->nd_next->nd_head;
11335 key = shareable_literal_constant_next(key);
11339 else if (RTEST(lit)) {
11340 rb_hash_clear(lit);
11345 val = shareable_literal_constant_next(val);
11347 n->nd_next->nd_head = val;
11349 else if (RTEST(lit)) {
11350 rb_hash_clear(lit);
11355 VALUE k = shareable_literal_value(key);
11356 VALUE v = shareable_literal_value(val);
11357 if (k != Qundef && v != Qundef) {
11358 rb_hash_aset(lit, k, v);
11361 rb_hash_clear(lit);
11362 lit = Qnil; /* make shareable at runtime */
11369 if (shareable == shareable_literal &&
11370 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11371 return ensure_shareable_node(p, dest, value, loc);
11376 /* Array or Hash */
11377 if (!lit) return 0;
11379 // if shareable_literal, all elements should have been ensured
11381 value = make_shareable_node(p, value, false, loc);
11384 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11385 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11389# undef shareable_literal_constant_next
11393shareable_constant_value(struct parser_params *p, enum shareability shareable,
11394 NODE *lhs, NODE *value, const YYLTYPE *loc)
11396 if (!value) return 0;
11397 switch (shareable) {
11398 case shareable_none:
11401 case shareable_literal:
11403 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11404 if (lit) return lit;
11409 case shareable_copy:
11410 case shareable_everything:
11412 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11413 if (lit) return lit;
11414 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11419 UNREACHABLE_RETURN(0);
11424node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11426 if (!lhs) return 0;
11428 switch (nd_type(lhs)) {
11430 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11439 lhs->nd_value = rhs;
11440 nd_set_loc(lhs, loc);
11443 case NODE_ATTRASGN:
11444 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11445 nd_set_loc(lhs, loc);
11449 /* should not happen */
11457value_expr_check(struct parser_params *p, NODE *node)
11459 NODE *void_node = 0, *vn;
11462 rb_warning0("empty expression");
11465 switch (nd_type(node)) {
11471 return void_node ? void_node : node;
11474 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
11475 compile_error(p, "unexpected node");
11478 if (node->nd_body->nd_body) {
11481 /* single line pattern matching */
11482 return void_node ? void_node : node;
11485 while (node->nd_next) {
11486 node = node->nd_next;
11488 node = node->nd_head;
11492 node = node->nd_body;
11497 if (!node->nd_body) {
11500 else if (!node->nd_else) {
11503 vn = value_expr_check(p, node->nd_body);
11504 if (!vn) return NULL;
11505 if (!void_node) void_node = vn;
11506 node = node->nd_else;
11511 node = node->nd_1st;
11517 mark_lvar_used(p, node);
11529value_expr_gen(struct parser_params *p, NODE *node)
11531 NODE *void_node = value_expr_check(p, node);
11533 yyerror1(&void_node->nd_loc, "void value expression");
11534 /* or "control never reach"? */
11540void_expr(struct parser_params *p, NODE *node)
11542 const char *useless = 0;
11544 if (!RTEST(ruby_verbose)) return;
11546 if (!node || !(node = nd_once_body(node))) return;
11547 switch (nd_type(node)) {
11549 switch (node->nd_mid) {
11568 useless = rb_id2name(node->nd_mid);
11579 case NODE_BACK_REF:
11580 useless = "a variable";
11583 useless = "a constant";
11589 useless = "a literal";
11614 useless = "defined?";
11619 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11624void_stmts(struct parser_params *p, NODE *node)
11626 NODE *const n = node;
11627 if (!RTEST(ruby_verbose)) return n;
11628 if (!node) return n;
11629 if (!nd_type_p(node, NODE_BLOCK)) return n;
11631 while (node->nd_next) {
11632 void_expr(p, node->nd_head);
11633 node = node->nd_next;
11639remove_begin(NODE *node)
11641 NODE **n = &node, *n1 = node;
11642 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
11643 *n = n1 = n1->nd_body;
11649remove_begin_all(NODE *node)
11651 NODE **n = &node, *n1 = node;
11652 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
11653 *n = n1 = n1->nd_body;
11659reduce_nodes(struct parser_params *p, NODE **body)
11661 NODE *node = *body;
11664 *body = NEW_NIL(&NULL_LOC);
11667#define subnodes(n1, n2) \
11668 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11669 (!node->n2) ? (body = &node->n1, 1) : \
11670 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11673 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11674 switch (nd_type(node)) {
11680 *body = node = node->nd_stts;
11681 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11684 *body = node = node->nd_body;
11685 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11688 body = &node->nd_end->nd_head;
11692 if (subnodes(nd_body, nd_else)) break;
11695 body = &node->nd_body;
11698 if (!subnodes(nd_body, nd_next)) goto end;
11701 if (!subnodes(nd_head, nd_resq)) goto end;
11704 if (node->nd_else) {
11705 body = &node->nd_resq;
11708 if (!subnodes(nd_head, nd_resq)) goto end;
11714 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11721is_static_content(NODE *node)
11723 if (!node) return 1;
11724 switch (nd_type(node)) {
11726 if (!(node = node->nd_head)) break;
11729 if (!is_static_content(node->nd_head)) return 0;
11730 } while ((node = node->nd_next) != 0);
11745assign_in_cond(struct parser_params *p, NODE *node)
11747 switch (nd_type(node)) {
11759 if (!node->nd_value) return 1;
11760 if (is_static_content(node->nd_value)) {
11761 /* reports always */
11762 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11773#define SWITCH_BY_COND_TYPE(t, w, arg) \
11775 case COND_IN_OP: break; \
11776 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11777 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11780static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11783range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11785 enum node_type type;
11787 if (node == 0) return 0;
11789 type = nd_type(node);
11791 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11792 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11793 ID lineno = rb_intern("$.");
11794 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11796 return cond0(p, node, COND_IN_FF, loc);
11800cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11802 if (node == 0) return 0;
11803 if (!(node = nd_once_body(node))) return 0;
11804 assign_in_cond(p, node);
11806 switch (nd_type(node)) {
11810 SWITCH_BY_COND_TYPE(type, warn, "string ")
11814 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11816 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11820 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11821 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11826 node->nd_beg = range_op(p, node->nd_beg, loc);
11827 node->nd_end = range_op(p, node->nd_end, loc);
11828 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
11829 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
11834 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
11838 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11839 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11840 nd_set_type(node, NODE_MATCH);
11842 else if (node->nd_lit == Qtrue ||
11843 node->nd_lit == Qfalse) {
11844 /* booleans are OK, e.g., while true */
11846 else if (SYMBOL_P(node->nd_lit)) {
11850 SWITCH_BY_COND_TYPE(type, warning, "")
11859cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11861 if (node == 0) return 0;
11862 return cond0(p, node, COND_IN_COND, loc);
11866method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11868 if (node == 0) return 0;
11869 return cond0(p, node, COND_IN_OP, loc);
11873new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11875 YYLTYPE loc = {*pos, *pos};
11876 return NEW_NIL(&loc);
11880new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11882 if (!cc) return right;
11883 cc = cond0(p, cc, COND_IN_COND, loc);
11884 return newline_node(NEW_IF(cc, left, right, loc));
11888new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11890 if (!cc) return right;
11891 cc = cond0(p, cc, COND_IN_COND, loc);
11892 return newline_node(NEW_UNLESS(cc, left, right, loc));
11896logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11897 const YYLTYPE *op_loc, const YYLTYPE *loc)
11899 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11902 if (left && nd_type_p(left, type)) {
11903 NODE *node = left, *second;
11904 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
11907 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11908 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11909 left->nd_loc.end_pos = loc->end_pos;
11912 op = NEW_NODE(type, left, right, 0, loc);
11913 nd_set_line(op, op_loc->beg_pos.lineno);
11918no_blockarg(struct parser_params *p, NODE *node)
11920 if (node && nd_type_p(node, NODE_BLOCK_PASS)) {
11921 compile_error(p, "block argument should not be given");
11926ret_args(struct parser_params *p, NODE *node)
11929 no_blockarg(p, node);
11930 if (nd_type_p(node, NODE_LIST)) {
11931 if (node->nd_next == 0) {
11932 node = node->nd_head;
11935 nd_set_type(node, NODE_VALUES);
11943new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11945 if (node) no_blockarg(p, node);
11947 return NEW_YIELD(node, loc);
11951negate_lit(struct parser_params *p, VALUE lit)
11953 if (FIXNUM_P(lit)) {
11954 return LONG2FIX(-FIX2LONG(lit));
11956 if (SPECIAL_CONST_P(lit)) {
11958 if (FLONUM_P(lit)) {
11959 return DBL2NUM(-RFLOAT_VALUE(lit));
11964 switch (BUILTIN_TYPE(lit)) {
11966 BIGNUM_NEGATE(lit);
11967 lit = rb_big_norm(lit);
11970 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11973 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11974 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11977 lit = DBL2NUM(-RFLOAT_VALUE(lit));
11981 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11982 rb_builtin_class_name(lit));
11989arg_blk_pass(NODE *node1, NODE *node2)
11992 if (!node1) return node2;
11993 node2->nd_head = node1;
11994 nd_set_first_lineno(node2, nd_first_lineno(node1));
11995 nd_set_first_column(node2, nd_first_column(node1));
12002args_info_empty_p(struct rb_args_info *args)
12004 if (args->pre_args_num) return false;
12005 if (args->post_args_num) return false;
12006 if (args->rest_arg) return false;
12007 if (args->opt_args) return false;
12008 if (args->block_arg) return false;
12009 if (args->kw_args) return false;
12010 if (args->kw_rest_arg) return false;
12015new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12017 int saved_line = p->ruby_sourceline;
12018 struct rb_args_info *args = tail->nd_ainfo;
12020 if (args->block_arg == idFWD_BLOCK) {
12022 yyerror1(&tail->nd_loc, "... after rest argument");
12025 rest_arg = idFWD_REST;
12028 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12029 args->pre_init = pre_args ? pre_args->nd_next : 0;
12031 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12032 args->post_init = post_args ? post_args->nd_next : 0;
12033 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12035 args->rest_arg = rest_arg;
12037 args->opt_args = opt_args;
12039 args->ruby2_keywords = rest_arg == idFWD_REST;
12041 p->ruby_sourceline = saved_line;
12042 nd_set_loc(tail, loc);
12048new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12050 int saved_line = p->ruby_sourceline;
12052 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12053 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12054 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12055 args->imemo = tmpbuf;
12056 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12057 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12058 if (p->error_p) return node;
12060 args->block_arg = block;
12061 args->kw_args = kw_args;
12065 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12066 * variable order: k1, kr1, k2, &b, internal_id, krest
12068 * variable order: kr1, k1, k2, internal_id, krest, &b
12070 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12071 struct vtable *vtargs = p->lvtbl->args;
12072 NODE *kwn = kw_args;
12074 if (block) block = vtargs->tbl[vtargs->pos-1];
12075 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12076 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12078 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12080 --required_kw_vars;
12081 kwn = kwn->nd_next;
12084 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12085 ID vid = kwn->nd_body->nd_vid;
12086 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12087 *required_kw_vars++ = vid;
12094 arg_var(p, kw_bits);
12095 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12096 if (block) arg_var(p, block);
12098 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12099 args->kw_rest_arg->nd_cflag = kw_bits;
12101 else if (kw_rest_arg == idNil) {
12102 args->no_kwarg = 1;
12104 else if (kw_rest_arg) {
12105 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12108 p->ruby_sourceline = saved_line;
12113args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12115 if (max_numparam > NO_PARAM) {
12117 YYLTYPE loc = RUBY_INIT_YYLLOC();
12118 args = new_args_tail(p, 0, 0, 0, 0);
12119 nd_set_loc(args, &loc);
12121 args->nd_ainfo->pre_args_num = max_numparam;
12127new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12129 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12131 aryptn->nd_pconst = constant;
12134 NODE *pre_args = NEW_LIST(pre_arg, loc);
12135 if (apinfo->pre_args) {
12136 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12139 apinfo->pre_args = pre_args;
12146new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12148 int saved_line = p->ruby_sourceline;
12150 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12151 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12152 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12153 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12154 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12156 apinfo->pre_args = pre_args;
12160 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12163 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12167 apinfo->rest_arg = NULL;
12170 apinfo->post_args = post_args;
12172 p->ruby_sourceline = saved_line;
12177new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12179 fndptn->nd_pconst = constant;
12185new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12187 int saved_line = p->ruby_sourceline;
12189 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12190 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12191 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12192 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12193 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12195 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12196 fpinfo->args = args;
12197 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12199 p->ruby_sourceline = saved_line;
12204new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12206 hshptn->nd_pconst = constant;
12211new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12213 int saved_line = p->ruby_sourceline;
12214 NODE *node, *kw_rest_arg_node;
12216 if (kw_rest_arg == idNil) {
12217 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12219 else if (kw_rest_arg) {
12220 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12223 kw_rest_arg_node = NULL;
12226 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12228 p->ruby_sourceline = saved_line;
12233dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12238 return NEW_LIT(ID2SYM(idNULL), loc);
12241 switch (nd_type(node)) {
12243 nd_set_type(node, NODE_DSYM);
12244 nd_set_loc(node, loc);
12247 lit = node->nd_lit;
12248 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12249 nd_set_type(node, NODE_LIT);
12250 nd_set_loc(node, loc);
12253 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12260append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12262 NODE *node = (NODE *)v;
12263 NODE **result = (NODE **)h;
12265 node->nd_next->nd_end = node->nd_next;
12266 node->nd_next->nd_next = 0;
12268 list_concat(*result, node);
12271 return ST_CONTINUE;
12275hash_literal_key_p(VALUE k)
12277 switch (OBJ_BUILTIN_TYPE(k)) {
12286literal_cmp(VALUE val, VALUE lit)
12288 if (val == lit) return 0;
12289 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12290 return rb_iseq_cdhash_cmp(val, lit);
12294literal_hash(VALUE a)
12296 if (!hash_literal_key_p(a)) return (st_index_t)a;
12297 return rb_iseq_cdhash_hash(a);
12300static const struct st_hash_type literal_type = {
12306remove_duplicate_keys(struct parser_params *p, NODE *hash)
12308 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12310 NODE *last_expr = 0;
12311 rb_code_location_t loc = hash->nd_loc;
12312 while (hash && hash->nd_head && hash->nd_next) {
12313 NODE *head = hash->nd_head;
12314 NODE *value = hash->nd_next;
12315 NODE *next = value->nd_next;
12316 st_data_t key = (st_data_t)head;
12318 value->nd_next = 0;
12319 if (nd_type_p(head, NODE_LIT) &&
12320 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12321 NODE *dup_value = ((NODE *)data)->nd_next;
12322 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12323 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12324 head->nd_lit, nd_line(head));
12325 if (dup_value == last_expr) {
12326 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12329 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12332 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12333 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12336 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12337 st_free_table(literal_keys);
12339 if (!result) result = hash;
12340 else list_concat(result, hash);
12342 result->nd_loc = loc;
12347new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12349 if (hash) hash = remove_duplicate_keys(p, hash);
12350 return NEW_HASH(hash, loc);
12355error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12357 if (is_private_local_id(id)) {
12360 if (st_is_member(p->pvtbl, id)) {
12361 yyerror1(loc, "duplicated variable name");
12364 st_insert(p->pvtbl, (st_data_t)id, 0);
12369error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12372 p->pktbl = st_init_numtable();
12374 else if (st_is_member(p->pktbl, key)) {
12375 yyerror1(loc, "duplicated key name");
12378 st_insert(p->pktbl, (st_data_t)key, 0);
12383new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12385 return NEW_HASH(hash, loc);
12387#endif /* !RIPPER */
12391new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12396 ID vid = lhs->nd_vid;
12397 YYLTYPE lhs_loc = lhs->nd_loc;
12398 int shareable = ctxt.shareable_constant_value;
12400 switch (nd_type(lhs)) {
12411 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12412 lhs->nd_value = rhs;
12413 nd_set_loc(lhs, loc);
12414 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12415 if (is_notop_id(vid)) {
12416 switch (id_type(vid)) {
12420 asgn->nd_aid = vid;
12424 else if (op == tANDOP) {
12426 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12428 lhs->nd_value = rhs;
12429 nd_set_loc(lhs, loc);
12430 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12434 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12436 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12438 asgn->nd_value = rhs;
12439 nd_set_loc(asgn, loc);
12443 asgn = NEW_BEGIN(0, loc);
12449new_ary_op_assign(struct parser_params *p, NODE *ary,
12450 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12454 args = make_list(args, args_loc);
12455 if (nd_type_p(args, NODE_BLOCK_PASS)) {
12456 args = NEW_ARGSCAT(args, rhs, loc);
12459 args = arg_concat(p, args, rhs, loc);
12461 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12467new_attr_op_assign(struct parser_params *p, NODE *lhs,
12468 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12472 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12478new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12483 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12484 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12487 asgn = NEW_BEGIN(0, loc);
12494const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12496 if (p->ctxt.in_def) {
12497 yyerror1(loc, "dynamic constant assignment");
12499 return NEW_CDECL(0, 0, (path), loc);
12503const_decl(struct parser_params *p, VALUE path)
12505 if (p->ctxt.in_def) {
12506 path = assign_error(p, "dynamic constant assignment", path);
12512assign_error(struct parser_params *p, const char *mesg, VALUE a)
12514 a = dispatch2(assign_error, ERR_MESG(), a);
12520var_field(struct parser_params *p, VALUE a)
12522 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12528new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12530 NODE *result = head;
12532 NODE *tmp = rescue_else ? rescue_else : rescue;
12533 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12535 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12536 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12538 else if (rescue_else) {
12539 result = block_append(p, result, rescue_else);
12542 result = NEW_ENSURE(result, ensure, loc);
12544 fixpos(result, head);
12550warn_unused_var(struct parser_params *p, struct local_vars *local)
12554 if (!local->used) return;
12555 cnt = local->used->pos;
12556 if (cnt != local->vars->pos) {
12557 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12560 ID *v = local->vars->tbl;
12561 ID *u = local->used->tbl;
12562 for (int i = 0; i < cnt; ++i) {
12563 if (!v[i] || (u[i] & LVAR_USED)) continue;
12564 if (is_private_local_id(v[i])) continue;
12565 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12571local_push(struct parser_params *p, int toplevel_scope)
12573 struct local_vars *local;
12574 int inherits_dvars = toplevel_scope && compile_for_eval;
12575 int warn_unused_vars = RTEST(ruby_verbose);
12577 local = ALLOC(struct local_vars);
12578 local->prev = p->lvtbl;
12579 local->args = vtable_alloc(0);
12580 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12582 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12583 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12584 local->numparam.outer = 0;
12585 local->numparam.inner = 0;
12586 local->numparam.current = 0;
12588 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12590# if WARN_PAST_SCOPE
12599local_pop(struct parser_params *p)
12601 struct local_vars *local = p->lvtbl->prev;
12602 if (p->lvtbl->used) {
12603 warn_unused_var(p, p->lvtbl);
12604 vtable_free(p->lvtbl->used);
12606# if WARN_PAST_SCOPE
12607 while (p->lvtbl->past) {
12608 struct vtable *past = p->lvtbl->past;
12609 p->lvtbl->past = past->prev;
12613 vtable_free(p->lvtbl->args);
12614 vtable_free(p->lvtbl->vars);
12617 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12622static rb_ast_id_table_t *
12623local_tbl(struct parser_params *p)
12625 int cnt_args = vtable_size(p->lvtbl->args);
12626 int cnt_vars = vtable_size(p->lvtbl->vars);
12627 int cnt = cnt_args + cnt_vars;
12629 rb_ast_id_table_t *tbl;
12631 if (cnt <= 0) return 0;
12632 tbl = rb_ast_new_local_table(p->ast, cnt);
12633 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
12634 /* remove IDs duplicated to warn shadowing */
12635 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
12636 ID id = p->lvtbl->vars->tbl[i];
12637 if (!vtable_included(p->lvtbl->args, id)) {
12638 tbl->ids[j++] = id;
12642 tbl = rb_ast_resize_latest_local_table(p->ast, j);
12649node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12651 rb_ast_id_table_t *a0;
12655 n = NEW_NODE(type, a0, a1, a2, loc);
12662numparam_name(struct parser_params *p, ID id)
12664 if (!NUMPARAM_ID_P(id)) return;
12665 compile_error(p, "_%d is reserved for numbered parameter",
12666 NUMPARAM_ID_TO_IDX(id));
12670arg_var(struct parser_params *p, ID id)
12672 numparam_name(p, id);
12673 vtable_add(p->lvtbl->args, id);
12677local_var(struct parser_params *p, ID id)
12679 numparam_name(p, id);
12680 vtable_add(p->lvtbl->vars, id);
12681 if (p->lvtbl->used) {
12682 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12687local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12689 struct vtable *vars, *args, *used;
12691 vars = p->lvtbl->vars;
12692 args = p->lvtbl->args;
12693 used = p->lvtbl->used;
12695 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12698 if (used) used = used->prev;
12701 if (vars && vars->prev == DVARS_INHERIT) {
12702 return rb_local_defined(id, p->parent_iseq);
12704 else if (vtable_included(args, id)) {
12708 int i = vtable_included(vars, id);
12709 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12715local_id(struct parser_params *p, ID id)
12717 return local_id_ref(p, id, NULL);
12721check_forwarding_args(struct parser_params *p)
12723 if (local_id(p, idFWD_REST) &&
12725 local_id(p, idFWD_KWREST) &&
12727 local_id(p, idFWD_BLOCK)) return TRUE;
12728 compile_error(p, "unexpected ...");
12733add_forwarding_args(struct parser_params *p)
12735 arg_var(p, idFWD_REST);
12737 arg_var(p, idFWD_KWREST);
12739 arg_var(p, idFWD_BLOCK);
12744new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12746 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12748 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12750 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12751 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12753 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12755 return arg_blk_pass(args, block);
12760numparam_push(struct parser_params *p)
12763 struct local_vars *local = p->lvtbl;
12764 NODE *inner = local->numparam.inner;
12765 if (!local->numparam.outer) {
12766 local->numparam.outer = local->numparam.current;
12768 local->numparam.inner = 0;
12769 local->numparam.current = 0;
12777numparam_pop(struct parser_params *p, NODE *prev_inner)
12780 struct local_vars *local = p->lvtbl;
12782 /* prefer first one */
12783 local->numparam.inner = prev_inner;
12785 else if (local->numparam.current) {
12786 /* current and inner are exclusive */
12787 local->numparam.inner = local->numparam.current;
12789 if (p->max_numparam > NO_PARAM) {
12790 /* current and outer are exclusive */
12791 local->numparam.current = local->numparam.outer;
12792 local->numparam.outer = 0;
12795 /* no numbered parameter */
12796 local->numparam.current = 0;
12801static const struct vtable *
12802dyna_push(struct parser_params *p)
12804 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12805 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12806 if (p->lvtbl->used) {
12807 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12809 return p->lvtbl->args;
12813dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12815 struct vtable *tmp = *vtblp;
12816 *vtblp = tmp->prev;
12817# if WARN_PAST_SCOPE
12818 if (p->past_scope_enabled) {
12819 tmp->prev = p->lvtbl->past;
12820 p->lvtbl->past = tmp;
12828dyna_pop_1(struct parser_params *p)
12830 struct vtable *tmp;
12832 if ((tmp = p->lvtbl->used) != 0) {
12833 warn_unused_var(p, p->lvtbl);
12834 p->lvtbl->used = p->lvtbl->used->prev;
12837 dyna_pop_vtable(p, &p->lvtbl->args);
12838 dyna_pop_vtable(p, &p->lvtbl->vars);
12842dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12844 while (p->lvtbl->args != lvargs) {
12846 if (!p->lvtbl->args) {
12847 struct local_vars *local = p->lvtbl->prev;
12848 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12856dyna_in_block(struct parser_params *p)
12858 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12862dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12864 struct vtable *vars, *args, *used;
12867 args = p->lvtbl->args;
12868 vars = p->lvtbl->vars;
12869 used = p->lvtbl->used;
12871 while (!DVARS_TERMINAL_P(vars)) {
12872 if (vtable_included(args, id)) {
12875 if ((i = vtable_included(vars, id)) != 0) {
12876 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12881 if (!vidrefp) used = 0;
12882 if (used) used = used->prev;
12885 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12886 return rb_dvar_defined(id, p->parent_iseq);
12893dvar_defined(struct parser_params *p, ID id)
12895 return dvar_defined_ref(p, id, NULL);
12899dvar_curr(struct parser_params *p, ID id)
12901 return (vtable_included(p->lvtbl->args, id) ||
12902 vtable_included(p->lvtbl->vars, id));
12906reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12909 "regexp encoding option '%c' differs from source encoding '%s'",
12910 c, rb_enc_name(rb_enc_get(str)));
12915rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12917 int c = RE_OPTION_ENCODING_IDX(options);
12921 rb_char_to_option_kcode(c, &opt, &idx);
12922 if (idx != ENCODING_GET(str) &&
12923 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12926 ENCODING_SET(str, idx);
12928 else if (RE_OPTION_ENCODING_NONE(options)) {
12929 if (!ENCODING_IS_ASCII8BIT(str) &&
12930 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12934 rb_enc_associate(str, rb_ascii8bit_encoding());
12936 else if (p->enc == rb_usascii_encoding()) {
12937 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12938 /* raise in re.c */
12939 rb_enc_associate(str, rb_usascii_encoding());
12942 rb_enc_associate(str, rb_ascii8bit_encoding());
12952reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12954 int c = rb_reg_fragment_setenc(p, str, options);
12955 if (c) reg_fragment_enc_error(p, str, c);
12959reg_fragment_check(struct parser_params* p, VALUE str, int options)
12962 reg_fragment_setenc(p, str, options);
12963 err = rb_reg_check_preprocess(str);
12965 err = rb_obj_as_string(err);
12966 compile_error(p, "%"PRIsVALUE, err);
12973 struct parser_params* parser;
12976 const YYLTYPE *loc;
12977} reg_named_capture_assign_t;
12980reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12981 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12983 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12984 struct parser_params* p = arg->parser;
12985 rb_encoding *enc = arg->enc;
12986 long len = name_end - name;
12987 const char *s = (const char *)name;
12991 if (!len) return ST_CONTINUE;
12992 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12993 return ST_CONTINUE;
12995 var = intern_cstr(s, len, enc);
12996 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12997 if (!lvar_defined(p, var)) return ST_CONTINUE;
12999 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13000 succ = arg->succ_block;
13001 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13002 succ = block_append(p, succ, node);
13003 arg->succ_block = succ;
13004 return ST_CONTINUE;
13008reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13010 reg_named_capture_assign_t arg;
13013 arg.enc = rb_enc_get(regexp);
13014 arg.succ_block = 0;
13016 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13018 if (!arg.succ_block) return 0;
13019 return arg.succ_block->nd_next;
13023parser_reg_compile(struct parser_params* p, VALUE str, int options)
13025 reg_fragment_setenc(p, str, options);
13026 return rb_parser_reg_compile(p, str, options);
13030rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13032 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13036reg_compile(struct parser_params* p, VALUE str, int options)
13041 err = rb_errinfo();
13042 re = parser_reg_compile(p, str, options);
13044 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13045 rb_set_errinfo(err);
13046 compile_error(p, "%"PRIsVALUE, m);
13053parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13055 VALUE err = rb_errinfo();
13057 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13058 int c = rb_reg_fragment_setenc(p, str, options);
13059 if (c) reg_fragment_enc_error(p, str, c);
13060 re = rb_parser_reg_compile(p, str, options);
13062 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13063 rb_set_errinfo(err);
13071rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13073 struct parser_params *p;
13074 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13075 p->do_print = print;
13077 p->do_chomp = chomp;
13078 p->do_split = split;
13082parser_append_options(struct parser_params *p, NODE *node)
13084 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13085 const YYLTYPE *const LOC = &default_location;
13088 NODE *print = NEW_FCALL(rb_intern("print"),
13089 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13091 node = block_append(p, node, print);
13096 ID ifs = rb_intern("$;");
13097 ID fields = rb_intern("$F");
13098 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13099 NODE *split = NEW_GASGN(fields,
13100 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13101 rb_intern("split"), args, LOC),
13103 node = block_append(p, split, node);
13106 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13107 rb_intern("chomp!"), 0, LOC);
13108 node = block_append(p, chomp, node);
13111 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
13120 /* just to suppress unused-function warnings */
13126internal_id(struct parser_params *p)
13128 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13130#endif /* !RIPPER */
13133parser_initialize(struct parser_params *p)
13135 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13136 p->command_start = TRUE;
13137 p->ruby_sourcefile_string = Qnil;
13138 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13141 p->delayed.token = Qnil;
13143 p->parsing_thread = Qnil;
13145 p->error_buffer = Qfalse;
13147 p->debug_buffer = Qnil;
13148 p->debug_output = rb_ractor_stdout();
13149 p->enc = rb_utf8_encoding();
13153#define parser_mark ripper_parser_mark
13154#define parser_free ripper_parser_free
13158parser_mark(void *ptr)
13160 struct parser_params *p = (struct parser_params*)ptr;
13162 rb_gc_mark(p->lex.input);
13163 rb_gc_mark(p->lex.prevline);
13164 rb_gc_mark(p->lex.lastline);
13165 rb_gc_mark(p->lex.nextline);
13166 rb_gc_mark(p->ruby_sourcefile_string);
13167 rb_gc_mark((VALUE)p->lex.strterm);
13168 rb_gc_mark((VALUE)p->ast);
13169 rb_gc_mark(p->case_labels);
13171 rb_gc_mark(p->debug_lines);
13172 rb_gc_mark(p->compile_option);
13173 rb_gc_mark(p->error_buffer);
13175 rb_gc_mark(p->delayed.token);
13176 rb_gc_mark(p->value);
13177 rb_gc_mark(p->result);
13178 rb_gc_mark(p->parsing_thread);
13180 rb_gc_mark(p->debug_buffer);
13181 rb_gc_mark(p->debug_output);
13183 rb_gc_mark((VALUE)p->heap);
13188parser_free(void *ptr)
13190 struct parser_params *p = (struct parser_params*)ptr;
13191 struct local_vars *local, *prev;
13194 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13196 for (local = p->lvtbl; local; local = prev) {
13197 if (local->vars) xfree(local->vars);
13198 prev = local->prev;
13202 token_info *ptinfo;
13203 while ((ptinfo = p->token_info) != 0) {
13204 p->token_info = ptinfo->next;
13212parser_memsize(const void *ptr)
13214 struct parser_params *p = (struct parser_params*)ptr;
13215 struct local_vars *local;
13216 size_t size = sizeof(*p);
13219 for (local = p->lvtbl; local; local = local->prev) {
13220 size += sizeof(*local);
13221 if (local->vars) size += local->vars->capa * sizeof(ID);
13226static const rb_data_type_t parser_data_type = {
13237 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13241#undef rb_reserved_word
13243const struct kwtable *
13244rb_reserved_word(const char *str, unsigned int len)
13246 return reserved_word(str, len);
13252 struct parser_params *p;
13253 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13254 &parser_data_type, p);
13255 parser_initialize(p);
13260rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13262 struct parser_params *p;
13264 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13265 p->error_buffer = main ? Qfalse : Qnil;
13266 p->parent_iseq = base;
13271rb_parser_keep_script_lines(VALUE vparser)
13273 struct parser_params *p;
13275 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13276 p->keep_script_lines = 1;
13281#define rb_parser_end_seen_p ripper_parser_end_seen_p
13282#define rb_parser_encoding ripper_parser_encoding
13283#define rb_parser_get_yydebug ripper_parser_get_yydebug
13284#define rb_parser_set_yydebug ripper_parser_set_yydebug
13285#define rb_parser_get_debug_output ripper_parser_get_debug_output
13286#define rb_parser_set_debug_output ripper_parser_set_debug_output
13287static VALUE ripper_parser_end_seen_p(VALUE vparser);
13288static VALUE ripper_parser_encoding(VALUE vparser);
13289static VALUE ripper_parser_get_yydebug(VALUE self);
13290static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13291static VALUE ripper_parser_get_debug_output(VALUE self);
13292static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13296 * ripper.error? -> Boolean
13298 * Return true if parsed source has errors.
13301ripper_error_p(VALUE vparser)
13303 struct parser_params *p;
13305 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13306 return RBOOL(p->error_p);
13312 * ripper.end_seen? -> Boolean
13314 * Return true if parsed source ended by +\_\_END\_\_+.
13317rb_parser_end_seen_p(VALUE vparser)
13319 struct parser_params *p;
13321 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13322 return RBOOL(p->ruby__end__seen);
13327 * ripper.encoding -> encoding
13329 * Return encoding of the source.
13332rb_parser_encoding(VALUE vparser)
13334 struct parser_params *p;
13336 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13337 return rb_enc_from_encoding(p->enc);
13343 * ripper.yydebug -> true or false
13348rb_parser_get_yydebug(VALUE self)
13350 struct parser_params *p;
13352 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13353 return RBOOL(p->debug);
13359 * ripper.yydebug = flag
13364rb_parser_set_yydebug(VALUE self, VALUE flag)
13366 struct parser_params *p;
13368 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13369 p->debug = RTEST(flag);
13375 * ripper.debug_output -> obj
13377 * Get debug output.
13380rb_parser_get_debug_output(VALUE self)
13382 struct parser_params *p;
13384 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13385 return p->debug_output;
13390 * ripper.debug_output = obj
13392 * Set debug output.
13395rb_parser_set_debug_output(VALUE self, VALUE output)
13397 struct parser_params *p;
13399 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13400 return p->debug_output = output;
13405#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13406/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13407 * potential memory leak */
13408#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13409#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13410 (new)->cnt = (cnt), (ptr))
13413rb_parser_malloc(struct parser_params *p, size_t size)
13415 size_t cnt = HEAPCNT(1, size);
13416 rb_imemo_tmpbuf_t *n = NEWHEAP();
13417 void *ptr = xmalloc(size);
13419 return ADD2HEAP(n, cnt, ptr);
13423rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13425 size_t cnt = HEAPCNT(nelem, size);
13426 rb_imemo_tmpbuf_t *n = NEWHEAP();
13427 void *ptr = xcalloc(nelem, size);
13429 return ADD2HEAP(n, cnt, ptr);
13433rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13435 rb_imemo_tmpbuf_t *n;
13436 size_t cnt = HEAPCNT(1, size);
13438 if (ptr && (n = p->heap) != NULL) {
13440 if (n->ptr == ptr) {
13441 n->ptr = ptr = xrealloc(ptr, size);
13442 if (n->cnt) n->cnt = cnt;
13445 } while ((n = n->next) != NULL);
13448 ptr = xrealloc(ptr, size);
13449 return ADD2HEAP(n, cnt, ptr);
13453rb_parser_free(struct parser_params *p, void *ptr)
13455 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13457 while ((n = *prev) != NULL) {
13458 if (n->ptr == ptr) {
13468rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13471 VALUE mesg = p->debug_buffer;
13473 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13475 rb_str_vcatf(mesg, fmt, ap);
13477 if (RSTRING_END(mesg)[-1] == '\n') {
13478 rb_io_write(p->debug_output, mesg);
13479 p->debug_buffer = Qnil;
13484parser_compile_error(struct parser_params *p, const char *fmt, ...)
13488 rb_io_flush(p->debug_output);
13492 rb_syntax_error_append(p->error_buffer,
13493 p->ruby_sourcefile_string,
13494 p->ruby_sourceline,
13495 rb_long2int(p->lex.pcur - p->lex.pbeg),
13501count_char(const char *str, int c)
13504 while (str[n] == c) ++n;
13509 * strip enclosing double-quotes, same as the default yytnamerr except
13510 * for that single-quotes matching back-quotes do not stop stripping.
13512 * "\"`class' keyword\"" => "`class' keyword"
13514RUBY_FUNC_EXPORTED size_t
13515rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13517 if (*yystr == '"') {
13518 size_t yyn = 0, bquote = 0;
13519 const char *yyp = yystr;
13525 bquote = count_char(yyp+1, '`') + 1;
13526 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13534 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13535 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13541 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13542 if (yyres) memcpy(yyres + yyn, yyp, 3);
13547 goto do_not_strip_quotes;
13550 goto do_not_strip_quotes;
13553 if (*++yyp != '\\')
13554 goto do_not_strip_quotes;
13555 /* Fall through. */
13570 do_not_strip_quotes: ;
13573 if (!yyres) return strlen(yystr);
13575 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13583ripper_validate_object(VALUE self, VALUE x)
13585 if (x == Qfalse) return x;
13586 if (x == Qtrue) return x;
13587 if (x == Qnil) return x;
13589 rb_raise(rb_eArgError, "Qundef given");
13590 if (FIXNUM_P(x)) return x;
13591 if (SYMBOL_P(x)) return x;
13592 switch (BUILTIN_TYPE(x)) {
13602 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
13603 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13605 x = ((NODE *)x)->nd_rval;
13608 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13609 (void *)x, rb_obj_classname(x));
13611 if (!RBASIC_CLASS(x)) {
13612 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13613 (void *)x, rb_builtin_type_name(TYPE(x)));
13619#define validate(x) ((x) = get_value(x))
13622ripper_dispatch0(struct parser_params *p, ID mid)
13624 return rb_funcall(p->value, mid, 0);
13628ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13631 return rb_funcall(p->value, mid, 1, a);
13635ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13639 return rb_funcall(p->value, mid, 2, a, b);
13643ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13648 return rb_funcall(p->value, mid, 3, a, b, c);
13652ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13658 return rb_funcall(p->value, mid, 4, a, b, c, d);
13662ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13669 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13673ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13682 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13686ripper_get_id(VALUE v)
13689 if (!RB_TYPE_P(v, T_NODE)) return 0;
13691 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
13696ripper_get_value(VALUE v)
13699 if (v == Qundef) return Qnil;
13700 if (!RB_TYPE_P(v, T_NODE)) return v;
13702 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
13703 return nd->nd_rval;
13707ripper_error(struct parser_params *p)
13713ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13718 va_start(args, fmt);
13719 str = rb_vsprintf(fmt, args);
13721 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13726ripper_lex_get_generic(struct parser_params *p, VALUE src)
13728 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13729 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13730 rb_raise(rb_eTypeError,
13731 "gets returned %"PRIsVALUE" (expected String or nil)",
13732 rb_obj_class(line));
13738ripper_lex_io_get(struct parser_params *p, VALUE src)
13740 return rb_io_gets(src);
13744ripper_s_allocate(VALUE klass)
13746 struct parser_params *p;
13747 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13748 &parser_data_type, p);
13753#define ripper_initialized_p(r) ((r)->lex.input != 0)
13757 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13759 * Create a new Ripper object.
13760 * _src_ must be a String, an IO, or an Object which has #gets method.
13762 * This method does not starts parsing.
13763 * See also Ripper#parse and Ripper.parse.
13766ripper_initialize(int argc, VALUE *argv, VALUE self)
13768 struct parser_params *p;
13769 VALUE src, fname, lineno;
13771 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13772 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13773 if (RB_TYPE_P(src, T_FILE)) {
13774 p->lex.gets = ripper_lex_io_get;
13776 else if (rb_respond_to(src, id_gets)) {
13777 p->lex.gets = ripper_lex_get_generic;
13781 p->lex.gets = lex_get_str;
13783 p->lex.input = src;
13785 if (NIL_P(fname)) {
13786 fname = STR_NEW2("(ripper)");
13790 StringValueCStr(fname);
13791 fname = rb_str_new_frozen(fname);
13793 parser_initialize(p);
13795 p->ruby_sourcefile_string = fname;
13796 p->ruby_sourcefile = RSTRING_PTR(fname);
13797 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13803ripper_parse0(VALUE parser_v)
13805 struct parser_params *p;
13807 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13809 p->ast = rb_ast_new();
13810 ripper_yyparse((void*)p);
13811 rb_ast_dispose(p->ast);
13817ripper_ensure(VALUE parser_v)
13819 struct parser_params *p;
13821 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13822 p->parsing_thread = Qnil;
13830 * Start parsing and returns the value of the root action.
13833ripper_parse(VALUE self)
13835 struct parser_params *p;
13837 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13838 if (!ripper_initialized_p(p)) {
13839 rb_raise(rb_eArgError, "method called for uninitialized object");
13841 if (!NIL_P(p->parsing_thread)) {
13842 if (p->parsing_thread == rb_thread_current())
13843 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13845 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13847 p->parsing_thread = rb_thread_current();
13848 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13855 * ripper.column -> Integer
13857 * Return column number of current parsing line.
13858 * This number starts from 0.
13861ripper_column(VALUE self)
13863 struct parser_params *p;
13866 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13867 if (!ripper_initialized_p(p)) {
13868 rb_raise(rb_eArgError, "method called for uninitialized object");
13870 if (NIL_P(p->parsing_thread)) return Qnil;
13871 col = p->lex.ptok - p->lex.pbeg;
13872 return LONG2NUM(col);
13877 * ripper.filename -> String
13879 * Return current parsing filename.
13882ripper_filename(VALUE self)
13884 struct parser_params *p;
13886 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13887 if (!ripper_initialized_p(p)) {
13888 rb_raise(rb_eArgError, "method called for uninitialized object");
13890 return p->ruby_sourcefile_string;
13895 * ripper.lineno -> Integer
13897 * Return line number of current parsing line.
13898 * This number starts from 1.
13901ripper_lineno(VALUE self)
13903 struct parser_params *p;
13905 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13906 if (!ripper_initialized_p(p)) {
13907 rb_raise(rb_eArgError, "method called for uninitialized object");
13909 if (NIL_P(p->parsing_thread)) return Qnil;
13910 return INT2NUM(p->ruby_sourceline);
13915 * ripper.state -> Integer
13917 * Return scanner state of current token.
13920ripper_state(VALUE self)
13922 struct parser_params *p;
13924 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13925 if (!ripper_initialized_p(p)) {
13926 rb_raise(rb_eArgError, "method called for uninitialized object");
13928 if (NIL_P(p->parsing_thread)) return Qnil;
13929 return INT2NUM(p->lex.state);
13934 * ripper.token -> String
13936 * Return the current token string.
13939ripper_token(VALUE self)
13941 struct parser_params *p;
13944 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13945 if (!ripper_initialized_p(p)) {
13946 rb_raise(rb_eArgError, "method called for uninitialized object");
13948 if (NIL_P(p->parsing_thread)) return Qnil;
13949 pos = p->lex.ptok - p->lex.pbeg;
13950 len = p->lex.pcur - p->lex.ptok;
13951 return rb_str_subseq(p->lex.lastline, pos, len);
13957ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13960 if (obj == Qundef) {
13961 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13968ripper_value(VALUE self, VALUE obj)
13970 return ULONG2NUM(obj);
13976 * Ripper.lex_state_name(integer) -> string
13978 * Returns a string representation of lex_state.
13981ripper_lex_state_name(VALUE self, VALUE state)
13983 return rb_parser_lex_state_name(NUM2INT(state));
13989 ripper_init_eventids1();
13990 ripper_init_eventids2();
13991 id_warn = rb_intern_const("warn");
13992 id_warning = rb_intern_const("warning");
13993 id_gets = rb_intern_const("gets");
13994 id_assoc = rb_intern_const("=>");
13996 (void)yystpcpy; /* may not used in newer bison */
14006 Ripper = rb_define_class("Ripper", rb_cObject);
14007 /* version of Ripper */
14008 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14009 rb_define_alloc_func(Ripper, ripper_s_allocate);
14010 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14011 rb_define_method(Ripper, "parse", ripper_parse, 0);
14012 rb_define_method(Ripper, "column", ripper_column, 0);
14013 rb_define_method(Ripper, "filename", ripper_filename, 0);
14014 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14015 rb_define_method(Ripper, "state", ripper_state, 0);
14016 rb_define_method(Ripper, "token", ripper_token, 0);
14017 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14018 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14019 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14020 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14021 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14022 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14023 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14025 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14026 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14027 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14030 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14031 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14033 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14035<% @exprs.each do |expr, desc| -%>
14037 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14039 ripper_init_eventids1_table(Ripper);
14040 ripper_init_eventids2_table(Ripper);
14043 /* Hack to let RDoc document SCRIPT_LINES__ */
14046 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14047 * after the assignment will be added as an Array of lines with the file
14050 rb_define_global_const("SCRIPT_LINES__", Qnil);
14059 * c-file-style: "ruby"