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"
51 #include "ruby/ruby.h"
53 #include "ruby/util.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)}, \
125 enum lex_state_bits {
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__)
170 static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
172 typedef VALUE stack_type;
174 static 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;
234 static 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)
240 typedef struct token_info {
242 rb_code_position_t beg;
245 struct token_info *next;
248 typedef struct rb_strterm_struct rb_strterm_t;
251 Structure of Lexer Buffer:
253 lex.pbeg lex.ptok lex.pcur lex.pend
255 |------------+------------+------------|
259 struct 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)
371 push_pvtbl(struct parser_params *p)
373 st_table *tbl = p->pvtbl;
374 p->pvtbl = st_init_numtable();
379 pop_pvtbl(struct parser_params *p, st_table *tbl)
381 st_free_table(p->pvtbl);
386 push_pktbl(struct parser_params *p)
388 st_table *tbl = p->pktbl;
394 pop_pktbl(struct parser_params *p, st_table *tbl)
396 if (p->pktbl) st_free_table(p->pktbl);
400 RBIMPL_ATTR_NONNULL((1, 2, 3))
401 static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
402 RBIMPL_ATTR_NONNULL((1, 2))
403 static 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)
409 static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
410 static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
411 static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
412 static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
413 static 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 '&'
431 static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
435 rb_discard_node(struct parser_params *p, NODE *n)
437 rb_ast_delete_node(p->ast, n);
443 add_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);
453 static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
456 static 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))
459 static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
462 parser_get_node_id(struct parser_params *p)
464 int node_id = p->node_id;
471 set_line_body(NODE *body, int line)
474 switch (nd_type(body)) {
477 nd_set_line(body, line);
481 #define yyparse ruby_yyparse
483 static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
484 static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
485 #define new_nil(loc) NEW_NIL(loc)
486 static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
487 static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
488 static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
489 static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
491 static NODE *newline_node(NODE*);
492 static void fixpos(NODE*,NODE*);
494 static int value_expr_gen(struct parser_params*,NODE*);
495 static void void_expr(struct parser_params*,NODE*);
496 static NODE *remove_begin(NODE*);
497 static NODE *remove_begin_all(NODE*);
498 #define value_expr(node) value_expr_gen(p, (node))
499 static NODE *void_stmts(struct parser_params*,NODE*);
500 static void reduce_nodes(struct parser_params*,NODE**);
501 static void block_dup_check(struct parser_params*,NODE*,NODE*);
503 static NODE *block_append(struct parser_params*,NODE*,NODE*);
504 static NODE *list_append(struct parser_params*,NODE*,NODE*);
505 static NODE *list_concat(NODE*,NODE*);
506 static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
507 static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
508 static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
509 static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
510 static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
511 static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
512 static NODE *evstr2dstr(struct parser_params*,NODE*);
513 static NODE *splat_array(NODE*);
514 static void mark_lvar_used(struct parser_params *p, NODE *rhs);
516 static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
517 static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
518 static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
519 static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
520 static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {b->nd_iter = m; b->nd_loc = *loc; return b;}
522 static bool args_info_empty_p(struct rb_args_info *args);
523 static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
524 static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
525 static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
526 static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
527 static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
528 static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
529 static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
530 static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
532 static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
533 static NODE *args_with_numbered(struct parser_params*,NODE*,int);
535 static VALUE negate_lit(struct parser_params*, VALUE);
536 static NODE *ret_args(struct parser_params*,NODE*);
537 static NODE *arg_blk_pass(NODE*,NODE*);
538 static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
539 static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
541 static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
542 static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
544 static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
545 static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
547 static void rb_backref_error(struct parser_params*,NODE*);
548 static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
550 static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
551 static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
552 static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
553 static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
554 static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
556 static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
558 static NODE *opt_arg_append(NODE*, NODE*);
559 static NODE *kwd_append(NODE*, NODE*);
561 static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
562 static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
564 static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
566 static 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))
570 static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
572 static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
574 static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
576 static rb_ast_id_table_t *local_tbl(struct parser_params*);
578 static VALUE reg_compile(struct parser_params*, VALUE, int);
579 static void reg_fragment_setenc(struct parser_params*, VALUE, int);
580 static int reg_fragment_check(struct parser_params*, VALUE, int);
581 static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
583 static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
584 static NODE *heredoc_dedent(struct parser_params*,NODE*);
586 static 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)
595 static inline int ripper_is_node_yylval(VALUE n);
598 ripper_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);
607 ripper_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
617 static ID ripper_get_id(VALUE);
618 #define get_id(id) ripper_get_id(id)
619 static VALUE ripper_get_value(VALUE);
620 #define get_value(val) ripper_get_value(val)
621 #define get_num(num) (int)get_id(num)
622 static VALUE assignable(struct parser_params*,VALUE);
623 static 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
633 static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
635 static VALUE const_decl(struct parser_params *p, VALUE path);
637 static VALUE var_field(struct parser_params *p, VALUE a);
638 static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
640 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
642 static VALUE backref_error(struct parser_params*, NODE *, VALUE);
645 /* forward declaration */
646 typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
648 RUBY_SYMBOL_EXPORT_BEGIN
649 VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
650 int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
651 enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
652 VALUE rb_parser_lex_state_name(enum lex_state_e state);
653 void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
654 PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
655 YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
656 YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
657 YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
658 RUBY_SYMBOL_EXPORT_END
660 static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
661 static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
663 static ID formal_argument(struct parser_params*, ID);
665 static ID formal_argument(struct parser_params*, VALUE);
667 static ID shadowing_lvar(struct parser_params*,ID);
668 static void new_bv(struct parser_params*,ID);
670 static void local_push(struct parser_params*,int);
671 static void local_pop(struct parser_params*);
672 static void local_var(struct parser_params*, ID);
673 static void arg_var(struct parser_params*, ID);
674 static int local_id(struct parser_params *p, ID id);
675 static int local_id_ref(struct parser_params*, ID, ID **);
677 static ID internal_id(struct parser_params*);
678 static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
680 static int check_forwarding_args(struct parser_params*);
681 static void add_forwarding_args(struct parser_params *p);
683 static const struct vtable *dyna_push(struct parser_params *);
684 static void dyna_pop(struct parser_params*, const struct vtable *);
685 static int dyna_in_block(struct parser_params*);
686 #define dyna_var(p, id) local_var(p, id)
687 static int dvar_defined(struct parser_params*, ID);
688 static int dvar_defined_ref(struct parser_params*, ID, ID**);
689 static int dvar_curr(struct parser_params*,ID);
691 static int lvar_defined(struct parser_params*, ID);
693 static NODE *numparam_push(struct parser_params *p);
694 static void numparam_pop(struct parser_params *p, NODE *prev_inner);
697 # define METHOD_NOT idNOT
699 # define METHOD_NOT '!'
702 #define idFWD_REST '*'
703 #ifdef RUBY3_KEYWORDS
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 */
719 typedef 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)
740 struct 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
760 STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
762 #define STRTERM_HEREDOC IMEMO_FL_USER0
764 struct rb_strterm_struct {
767 rb_strterm_literal_t literal;
768 rb_strterm_heredoc_t heredoc;
774 rb_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)
785 size_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"
801 static inline VALUE intern_sym(const char *name);
803 #include "eventids1.c"
804 #include "eventids2.c"
806 static VALUE ripper_dispatch0(struct parser_params*,ID);
807 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
808 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
809 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
810 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
811 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
812 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
813 static 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))
835 new_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));
843 new_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);
853 args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
859 new_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);
876 new_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);
895 new_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);
904 new_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)
921 new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
927 new_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);
935 new_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))
953 static 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
961 set_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);
973 rescued_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);
985 restore_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;
994 endless_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))
1037 static 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
1060 PRINTF_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
1064 # define WARN_S(s) s
1065 # define WARN_I(i) i
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
1076 PRINTF_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") : \
1084 static 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) %*/
1353 top_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
1393 begin_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)) %*/
1426 compstmt : 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);
1459 stmt_or_begin : stmt
1465 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1473 stmt : 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) %*/
1611 command_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)) %*/
1722 command_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);
1820 defn_head : k_def def_name
1824 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
1829 defs_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;
1855 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1861 command_call : command
1865 block_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) %*/
1875 cmd_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);
1895 command : 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) %*/
1992 mlhs_inner : mlhs_basic
1993 | tLPAREN mlhs_inner rparen
1996 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1998 /*% ripper: mlhs_paren!($2) %*/
2002 mlhs_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) %*/
2074 mlhs_item : mlhs_node
2075 | tLPAREN mlhs_inner rparen
2080 /*% ripper: mlhs_paren!($2) %*/
2084 mlhs_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) %*/
2100 mlhs_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) %*/
2116 mlhs_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) %*/
2265 cpath : 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)) %*/
2326 op : '|' { 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($$ = '`'); }
2358 reswords : 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
2372 arg : 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) %*/
2655 relop : '>' {$$ = '>';}
2661 rel_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)) %*/
2710 arg_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) %*/
2725 paren_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) %*/
2758 opt_paren_args : none
2762 opt_call_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);
2856 block_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), &@$);
2876 opt_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) %*/
2923 mrhs : 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! %*/
3336 primary_value : primary
3343 k_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;
3366 k_unless : keyword_unless
3368 token_info_push(p, "unless", &@$);
3372 k_while : keyword_while
3374 token_info_push(p, "while", &@$);
3378 k_until : keyword_until
3380 token_info_push(p, "until", &@$);
3384 k_case : keyword_case
3386 token_info_push(p, "case", &@$);
3392 token_info_push(p, "for", &@$);
3396 k_class : keyword_class
3398 token_info_push(p, "class", &@$);
3403 k_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", &@$);
3423 k_do_block : keyword_do_block
3425 token_info_push(p, "do", &@$);
3429 k_rescue : keyword_rescue
3431 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3435 k_ensure : keyword_ensure
3437 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3441 k_when : keyword_when
3443 token_info_warn(p, "when", p->token_info, 0, &@$);
3447 k_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;
3462 k_elsif : keyword_elsif
3465 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3471 token_info_pop(p, "end", &@$);
3475 k_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) %*/
3535 f_marg_list : f_marg
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) %*/
3551 f_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) %*/
3588 f_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 %*/
3605 f_any_kwrest : f_kwrest
3606 | f_no_kwarg {$$ = ID2VAL(idNil);}
3609 f_eq : {p->ctxt.in_argdef = 0;} '=';
3611 block_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);
3629 opt_block_args_tail : ',' block_args_tail
3635 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3639 excessed_comma : ','
3641 /* magic number for rest_id in iseq_set_arguments() */
3643 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3645 /*% ripper: excessed_comma! %*/
3649 block_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, &@$);
3712 opt_block_param : none
3715 p->command_start = TRUE;
3719 block_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)) %*/
3742 opt_bv_decl : opt_nl
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);
3812 f_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;
3832 lambda_body : tLAMBEG compstmt '}'
3834 token_info_pop(p, "}", &@3);
3837 | keyword_do_LAMBDA bodystmt k_end
3843 do_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);
3853 block_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) %*/
3890 method_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)) %*/
3965 brace_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);
3983 brace_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);
4005 do_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);
4029 case_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) %*/
4061 case_body : k_when case_args then
4066 $$ = NEW_WHEN($2, $4, $5, &@$);
4069 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4077 p_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)) %*/
4108 p_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) %*/
4127 p_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, &@$);
4158 p_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) %*/
4170 p_alt : p_alt '|' p_expr_basic
4173 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4175 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4180 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4181 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4183 p_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, &@$);
4327 p_args_head : p_arg ','
4331 | p_args_head p_arg ','
4334 $$ = list_concat($1, $2);
4336 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4340 p_args_tail : p_rest
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, &@$);
4350 p_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!");
4360 p_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)) %*/
4389 p_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) %*/
4418 p_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");
4464 p_kwrest : kwrest_mark tIDENTIFIER
4474 p_kwnorest : kwrest_mark keyword_nil
4480 p_any_kwrest : p_kwrest
4481 | p_kwnorest {$$ = ID2VAL(idNil);}
4484 p_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) %*/
4540 p_primitive : literal
4551 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4553 /*% ripper: var_ref!($1) %*/
4558 p_variable : tIDENTIFIER
4561 error_duplicate_pattern_variable(p, $1, &@1);
4562 $$ = assignable(p, $1, 0, &@$);
4564 /*% ripper: assignable(p, var_field(p, $1)) %*/
4568 p_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) %*/
4588 p_expr_ref : '^' tLPAREN expr_value ')'
4591 $$ = NEW_BEGIN($3, &@$);
4593 /*% ripper: begin!($3) %*/
4597 p_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) %*/
4620 opt_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)) %*/
4635 exc_list : arg_value
4638 $$ = NEW_LIST($1, &@$);
4640 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4645 if (!($$ = splat_array($1))) $$ = $1;
4652 exc_var : tASSOC lhs
4659 opt_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) %*/
4701 string1 : tSTRING_BEG string_contents tSTRING_END
4704 $$ = heredoc_dedent(p, $2);
4705 if ($$) nd_set_loc($$, &@$);
4707 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4711 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4714 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4716 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4720 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4722 $$ = new_regexp(p, $2, $3, &@$);
4726 words : tWORDS_BEG ' ' word_list tSTRING_END
4729 $$ = make_list($3, &@$);
4731 /*% ripper: array!($3) %*/
4735 word_list : /* none */
4740 /*% ripper: words_new! %*/
4742 | word_list word ' '
4745 $$ = list_append(p, $1, evstr2dstr(p, $2));
4747 /*% ripper: words_add!($1, $2) %*/
4751 word : 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) %*/
4762 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4765 $$ = make_list($3, &@$);
4767 /*% ripper: array!($3) %*/
4771 symbol_list : /* none */
4776 /*% ripper: symbols_new! %*/
4778 | symbol_list word ' '
4781 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4783 /*% ripper: symbols_add!($1, $2) %*/
4787 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4790 $$ = make_list($3, &@$);
4792 /*% ripper: array!($3) %*/
4796 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4799 $$ = make_list($3, &@$);
4801 /*% ripper: array!($3) %*/
4805 qword_list : /* none */
4810 /*% ripper: qwords_new! %*/
4812 | qword_list tSTRING_CONTENT ' '
4815 $$ = list_append(p, $1, $2);
4817 /*% ripper: qwords_add!($1, $2) %*/
4821 qsym_list : /* none */
4826 /*% ripper: qsymbols_new! %*/
4828 | qsym_list tSTRING_CONTENT ' '
4831 $$ = symbol_append(p, $1, $2);
4833 /*% ripper: qsymbols_add!($1, $2) %*/
4837 string_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, $$);
4866 xstring_contents: /* none */
4871 /*% ripper: xstring_new! %*/
4873 | xstring_contents string_content
4876 $$ = literal_concat(p, $1, $2, &@$);
4878 /*% ripper: xstring_add!($1, $2) %*/
4882 regexp_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);
4934 string_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)) %*/
5035 dsym : tSYMBEG string_contents tSTRING_END
5037 SET_LEX_STATE(EXPR_END);
5039 $$ = dsym_node(p, $2, &@$);
5041 /*% ripper: dyna_symbol!($2) %*/
5045 numeric : 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) %*/
5056 simple_numeric : tINTEGER
5062 nonlocal_var : tIVAR
5067 user_variable : tIDENTIFIER
5074 keyword_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);}
5083 var_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) %*/
5105 var_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 %*/
5143 f_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);
5152 f_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;
5164 f_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;
5181 args_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);
5204 opt_args_tail : ',' args_tail
5210 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5214 f_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);
5277 args_forward : tBDOT3
5282 /*% ripper: args_forward! %*/
5286 f_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) %*/
5324 f_norm_arg : f_bad_arg
5327 formal_argument(p, $1);
5328 p->max_numparam = ORDINAL_PARAM;
5333 f_arg_asgn : f_norm_arg
5342 f_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;
5396 f_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) %*/
5416 f_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) %*/
5434 f_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)) %*/
5471 f_no_kwarg : kwrest_mark keyword_nil
5475 /*% ripper: nokw_param!(Qnil) %*/
5479 f_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) %*/
5497 f_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)) %*/
5508 f_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)) %*/
5519 f_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)) %*/
5555 f_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) %*/
5577 f_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);
5595 opt_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)) %*/
5669 assoc : 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) %*/
5723 operation : tIDENTIFIER
5728 operation2 : tIDENTIFIER
5734 operation3 : tIDENTIFIER
5751 opt_terms : /* none */
5762 rbracket : opt_nl ']'
5768 trailer : /* none */
5773 term : ';' {yyerrok;token_flush(p);}
5774 | '\n' {token_flush(p);}
5778 | terms ';' {yyerrok;}
5790 # define yylval (*p->lval)
5792 static int regx_options(struct parser_params*);
5793 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5794 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5795 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5796 static 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)
5820 ripper_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))
5847 intern_sym(const char *name)
5849 ID id = rb_intern_const(name);
5854 ripper_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;
5861 ripper_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);
5870 ripper_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)
5878 ripper_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))
5896 is_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);
5902 parser_is_identchar(struct parser_params *p)
5904 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5908 parser_isascii(struct parser_params *p)
5910 return ISASCII(*(p->lex.pcur-1));
5914 token_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;
5933 token_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;
5947 token_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));
5960 token_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));
5980 token_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));
5996 parser_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));
6007 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6010 parser_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);
6027 parser_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);
6046 parser_yyerror0(struct parser_params *p, const char *msg)
6049 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6053 ruby_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);
6158 parser_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);
6177 parser_yyerror0(struct parser_params *p, const char *msg)
6179 dispatch1(parse_error, STR_NEW2(msg));
6185 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6188 #endif /* !RIPPER */
6192 vtable_size(const struct vtable *tbl)
6194 if (!DVARS_TERMINAL_P(tbl)) {
6203 static struct vtable *
6204 vtable_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)
6221 vtable_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)
6239 vtable_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)
6262 vtable_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)
6279 vtable_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) {
6293 static void parser_prepare(struct parser_params *p);
6296 static NODE *parser_append_options(struct parser_params *p, NODE *node);
6299 debug_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);
6315 e_option_supplied(struct parser_params *p)
6317 return strcmp(p->ruby_sourcefile, "-e") == 0;
6321 yycompile0(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);
6392 yycompile(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 */
6418 #endif /* !RIPPER */
6420 static rb_encoding *
6421 must_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");
6431 lex_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);
6451 lex_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
6461 static const rb_data_type_t parser_data_type;
6465 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6467 struct parser_params *p;
6469 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6471 p->lex.gets = lex_get_str;
6472 p->lex.gets_.ptr = 0;
6473 p->lex.input = rb_str_new_frozen(s);
6474 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6476 return yycompile(vparser, p, fname, line);
6480 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6482 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6486 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6488 must_be_ascii_compatible(s);
6489 return parser_compile_string(vparser, f, s, line);
6492 VALUE rb_io_gets_internal(VALUE io);
6495 lex_io_gets(struct parser_params *p, VALUE io)
6497 return rb_io_gets_internal(io);
6501 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6503 struct parser_params *p;
6505 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6507 p->lex.gets = lex_io_gets;
6508 p->lex.input = file;
6509 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6511 return yycompile(vparser, p, fname, start);
6515 lex_generic_gets(struct parser_params *p, VALUE input)
6517 return (*p->lex.gets_.call)(input, p->line_count);
6521 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6523 struct parser_params *p;
6525 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6527 p->lex.gets = lex_generic_gets;
6528 p->lex.gets_.call = lex_gets;
6529 p->lex.input = input;
6530 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6532 return yycompile(vparser, p, fname, start);
6534 #endif /* !RIPPER */
6536 #define STR_FUNC_ESCAPE 0x01
6537 #define STR_FUNC_EXPAND 0x02
6538 #define STR_FUNC_REGEXP 0x04
6539 #define STR_FUNC_QWORDS 0x08
6540 #define STR_FUNC_SYMBOL 0x10
6541 #define STR_FUNC_INDENT 0x20
6542 #define STR_FUNC_LABEL 0x40
6543 #define STR_FUNC_LIST 0x4000
6544 #define STR_FUNC_TERM 0x8000
6547 str_label = STR_FUNC_LABEL,
6549 str_dquote = (STR_FUNC_EXPAND),
6550 str_xquote = (STR_FUNC_EXPAND),
6551 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6552 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6553 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6554 str_ssym = (STR_FUNC_SYMBOL),
6555 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6559 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6563 str = rb_enc_str_new(ptr, len, enc);
6564 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6565 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6567 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6568 rb_enc_associate(str, rb_ascii8bit_encoding());
6575 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6576 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6577 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6578 #define peek(p,c) peek_n(p, (c), 0)
6579 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6580 #define peekc(p) peekc_n(p, 0)
6581 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6585 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6588 if (!has_delayed_token(p)) {
6589 p->delayed.token = rb_str_buf_new(end - tok);
6590 rb_enc_associate(p->delayed.token, p->enc);
6591 p->delayed.line = p->ruby_sourceline;
6592 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6594 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6599 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6603 nextline(struct parser_params *p, int set_encoding)
6605 VALUE v = p->lex.nextline;
6606 p->lex.nextline = 0;
6611 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6615 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6622 if (p->debug_lines) {
6623 if (set_encoding) rb_enc_associate(v, p->enc);
6624 rb_ary_push(p->debug_lines, v);
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;
6648 parser_cr(struct parser_params *p, int c)
6650 if (peek(p, '\n')) {
6658 nextc0(struct parser_params *p, int set_encoding)
6662 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6663 if (nextline(p, set_encoding)) return -1;
6665 c = (unsigned char)*p->lex.pcur++;
6666 if (UNLIKELY(c == '\r')) {
6667 c = parser_cr(p, c);
6672 #define nextc(p) nextc0(p, TRUE)
6675 pushback(struct parser_params *p, int c)
6677 if (c == -1) return;
6679 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6684 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6686 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6687 #define tok(p) (p)->tokenbuf
6688 #define toklen(p) (p)->tokidx
6691 looking_at_eol_p(struct parser_params *p)
6693 const char *ptr = p->lex.pcur;
6694 while (ptr < p->lex.pend) {
6695 int c = (unsigned char)*ptr++;
6696 int eol = (c == '\n' || c == '#');
6697 if (eol || !ISSPACE(c)) {
6705 newtok(struct parser_params *p)
6708 p->tokline = p->ruby_sourceline;
6711 p->tokenbuf = ALLOC_N(char, 60);
6713 if (p->toksiz > 4096) {
6715 REALLOC_N(p->tokenbuf, char, 60);
6721 tokspace(struct parser_params *p, int n)
6725 if (p->tokidx >= p->toksiz) {
6726 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6727 REALLOC_N(p->tokenbuf, char, p->toksiz);
6729 return &p->tokenbuf[p->tokidx-n];
6733 tokadd(struct parser_params *p, int c)
6735 p->tokenbuf[p->tokidx++] = (char)c;
6736 if (p->tokidx >= p->toksiz) {
6738 REALLOC_N(p->tokenbuf, char, p->toksiz);
6743 tok_hex(struct parser_params *p, size_t *numlen)
6747 c = scan_hex(p->lex.pcur, 2, numlen);
6749 yyerror0("invalid hex escape");
6753 p->lex.pcur += *numlen;
6757 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6760 escaped_control_code(int c)
6786 #define WARN_SPACE_CHAR(c, prefix) \
6787 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6790 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6791 int regexp_literal, int wide)
6794 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6795 literal_flush(p, p->lex.pcur);
6796 p->lex.pcur += numlen;
6797 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6798 yyerror0("invalid Unicode escape");
6799 return wide && numlen > 0;
6801 if (codepoint > 0x10ffff) {
6802 yyerror0("invalid Unicode codepoint (too large)");
6805 if ((codepoint & 0xfffff800) == 0xd800) {
6806 yyerror0("invalid Unicode codepoint");
6809 if (regexp_literal) {
6810 tokcopy(p, (int)numlen);
6812 else if (codepoint >= 0x80) {
6813 rb_encoding *utf8 = rb_utf8_encoding();
6814 if (*encp && utf8 != *encp) {
6815 YYLTYPE loc = RUBY_INIT_YYLLOC();
6816 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6817 parser_show_error_line(p, &loc);
6821 tokaddmbc(p, codepoint, *encp);
6824 tokadd(p, codepoint);
6829 /* return value is for ?\u3042 */
6831 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6832 int term, int symbol_literal, int regexp_literal)
6835 * If `term` is not -1, then we allow multiple codepoints in \u{}
6836 * upto `term` byte, otherwise we're parsing a character literal.
6837 * And then add the codepoints to the current token.
6839 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6841 const int open_brace = '{', close_brace = '}';
6843 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6845 if (peek(p, open_brace)) { /* handle \u{...} form */
6846 const char *second = NULL;
6847 int c, last = nextc(p);
6848 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6849 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6850 while (c != close_brace) {
6851 if (c == term) goto unterminated;
6852 if (second == multiple_codepoints)
6853 second = p->lex.pcur;
6854 if (regexp_literal) tokadd(p, last);
6855 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6858 while (ISSPACE(c = *p->lex.pcur)) {
6859 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6862 if (term == -1 && !second)
6863 second = multiple_codepoints;
6866 if (c != close_brace) {
6869 yyerror0("unterminated Unicode escape");
6872 if (second && second != multiple_codepoints) {
6873 const char *pcur = p->lex.pcur;
6874 p->lex.pcur = second;
6875 dispatch_scan_event(p, tSTRING_CONTENT);
6878 yyerror0(multiple_codepoints);
6882 if (regexp_literal) tokadd(p, close_brace);
6885 else { /* handle \uxxxx form */
6886 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6893 #define ESCAPE_CONTROL 1
6894 #define ESCAPE_META 2
6897 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6902 switch (c = nextc(p)) {
6903 case '\\': /* Backslash */
6906 case 'n': /* newline */
6909 case 't': /* horizontal tab */
6912 case 'r': /* carriage-return */
6915 case 'f': /* form-feed */
6918 case 'v': /* vertical tab */
6921 case 'a': /* alarm(bell) */
6924 case 'e': /* escape */
6927 case '0': case '1': case '2': case '3': /* octal constant */
6928 case '4': case '5': case '6': case '7':
6930 c = scan_oct(p->lex.pcur, 3, &numlen);
6931 p->lex.pcur += numlen;
6934 case 'x': /* hex constant */
6935 c = tok_hex(p, &numlen);
6936 if (numlen == 0) return 0;
6939 case 'b': /* backspace */
6942 case 's': /* space */
6946 if (flags & ESCAPE_META) goto eof;
6947 if ((c = nextc(p)) != '-') {
6950 if ((c = nextc(p)) == '\\') {
6956 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6958 else if (c == -1 || !ISASCII(c)) goto eof;
6960 int c2 = escaped_control_code(c);
6962 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6963 WARN_SPACE_CHAR(c2, "\\M-");
6966 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6969 else if (ISCNTRL(c)) goto eof;
6970 return ((c & 0xff) | 0x80);
6974 if ((c = nextc(p)) != '-') {
6978 if (flags & ESCAPE_CONTROL) goto eof;
6979 if ((c = nextc(p))== '\\') {
6985 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6989 else if (c == -1 || !ISASCII(c)) goto eof;
6991 int c2 = escaped_control_code(c);
6994 if (flags & ESCAPE_META) {
6995 WARN_SPACE_CHAR(c2, "\\M-");
6998 WARN_SPACE_CHAR(c2, "");
7002 if (flags & ESCAPE_META) {
7003 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7006 WARN_SPACE_CHAR(c2, "\\C-");
7010 else if (ISCNTRL(c)) goto eof;
7016 yyerror0("Invalid escape character syntax");
7026 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7028 int len = rb_enc_codelen(c, enc);
7029 rb_enc_mbcput(c, tokspace(p, len), enc);
7033 tokadd_escape(struct parser_params *p, rb_encoding **encp)
7038 switch (c = nextc(p)) {
7040 return 0; /* just ignore */
7042 case '0': case '1': case '2': case '3': /* octal constant */
7043 case '4': case '5': case '6': case '7':
7045 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7046 if (numlen == 0) goto eof;
7047 p->lex.pcur += numlen;
7048 tokcopy(p, (int)numlen + 1);
7052 case 'x': /* hex constant */
7054 tok_hex(p, &numlen);
7055 if (numlen == 0) return -1;
7056 tokcopy(p, (int)numlen + 2);
7062 yyerror0("Invalid escape character syntax");
7074 regx_options(struct parser_params *p)
7082 while (c = nextc(p), ISALPHA(c)) {
7084 options |= RE_OPTION_ONCE;
7086 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7088 if (kc != rb_ascii8bit_encindex()) kcode = c;
7102 YYLTYPE loc = RUBY_INIT_YYLLOC();
7104 compile_error(p, "unknown regexp option%s - %*s",
7105 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7106 parser_show_error_line(p, &loc);
7108 return options | RE_OPTION_ENCODING(kcode);
7112 tokadd_mbchar(struct parser_params *p, int c)
7114 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7115 if (len < 0) return -1;
7117 p->lex.pcur += --len;
7118 if (len > 0) tokcopy(p, len);
7123 simple_re_meta(int c)
7126 case '$': case '*': case '+': case '.':
7127 case '?': case '^': case '|':
7128 case ')': case ']': case '}': case '>':
7136 parser_update_heredoc_indent(struct parser_params *p, int c)
7138 if (p->heredoc_line_indent == -1) {
7139 if (c == '\n') p->heredoc_line_indent = 0;
7143 p->heredoc_line_indent++;
7146 else if (c == '\t') {
7147 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7148 p->heredoc_line_indent = w * TAB_WIDTH;
7151 else if (c != '\n') {
7152 if (p->heredoc_indent > p->heredoc_line_indent) {
7153 p->heredoc_indent = p->heredoc_line_indent;
7155 p->heredoc_line_indent = -1;
7162 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7164 YYLTYPE loc = RUBY_INIT_YYLLOC();
7165 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7166 compile_error(p, "%s mixed within %s source", n1, n2);
7167 parser_show_error_line(p, &loc);
7171 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7173 const char *pos = p->lex.pcur;
7175 parser_mixed_error(p, enc1, enc2);
7180 tokadd_string(struct parser_params *p,
7181 int func, int term, int paren, long *nest,
7182 rb_encoding **encp, rb_encoding **enc)
7187 #define mixed_error(enc1, enc2) \
7188 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7189 #define mixed_escape(beg, enc1, enc2) \
7190 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7192 while ((c = nextc(p)) != -1) {
7193 if (p->heredoc_indent > 0) {
7194 parser_update_heredoc_indent(p, c);
7197 if (paren && c == paren) {
7200 else if (c == term) {
7201 if (!nest || !*nest) {
7207 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7208 int c2 = *p->lex.pcur;
7209 if (c2 == '$' || c2 == '@' || c2 == '{') {
7214 else if (c == '\\') {
7215 literal_flush(p, p->lex.pcur - 1);
7219 if (func & STR_FUNC_QWORDS) break;
7220 if (func & STR_FUNC_EXPAND) {
7221 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7232 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7236 if ((func & STR_FUNC_EXPAND) == 0) {
7240 tokadd_utf8(p, enc, term,
7241 func & STR_FUNC_SYMBOL,
7242 func & STR_FUNC_REGEXP);
7246 if (c == -1) return -1;
7248 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7251 if (func & STR_FUNC_REGEXP) {
7257 c = read_escape(p, 0, enc);
7261 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7262 for (i = 0; i < 4; i++) {
7263 tokadd(p, escbuf[i]);
7269 if (c == term && !simple_re_meta(c)) {
7274 if ((c = tokadd_escape(p, enc)) < 0)
7276 if (*enc && *enc != *encp) {
7277 mixed_escape(p->lex.ptok+2, *enc, *encp);
7281 else if (func & STR_FUNC_EXPAND) {
7283 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7284 c = read_escape(p, 0, enc);
7286 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7287 /* ignore backslashed spaces in %w */
7289 else if (c != term && !(paren && c == paren)) {
7296 else if (!parser_isascii(p)) {
7301 else if (*enc != *encp) {
7302 mixed_error(*enc, *encp);
7305 if (tokadd_mbchar(p, c) == -1) return -1;
7308 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7316 else if (*enc != *encp) {
7317 mixed_error(*enc, *encp);
7324 if (*enc) *encp = *enc;
7328 static inline rb_strterm_t *
7329 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7331 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7334 /* imemo_parser_strterm for literal */
7335 #define NEW_STRTERM(func, term, paren) \
7336 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7340 flush_string_content(struct parser_params *p, rb_encoding *enc)
7342 VALUE content = yylval.val;
7343 if (!ripper_is_node_yylval(content))
7344 content = ripper_new_yylval(p, 0, 0, content);
7345 if (has_delayed_token(p)) {
7346 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7348 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7350 dispatch_delayed_token(p, tSTRING_CONTENT);
7351 p->lex.ptok = p->lex.pcur;
7352 RNODE(content)->nd_rval = yylval.val;
7354 dispatch_scan_event(p, tSTRING_CONTENT);
7355 if (yylval.val != content)
7356 RNODE(content)->nd_rval = yylval.val;
7357 yylval.val = content;
7360 #define flush_string_content(p, enc) ((void)(enc))
7363 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7364 /* this can be shared with ripper, since it's independent from struct
7367 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7368 #define SPECIAL_PUNCT(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) | \
7373 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7375 const unsigned int ruby_global_name_punct_bits[] = {
7381 #undef SPECIAL_PUNCT
7384 static enum yytokentype
7385 parser_peek_variable_name(struct parser_params *p)
7388 const char *ptr = p->lex.pcur;
7390 if (ptr + 1 >= p->lex.pend) return 0;
7394 if ((c = *ptr) == '-') {
7395 if (++ptr >= p->lex.pend) return 0;
7398 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7399 return tSTRING_DVAR;
7403 if ((c = *ptr) == '@') {
7404 if (++ptr >= p->lex.pend) return 0;
7410 p->command_start = TRUE;
7411 return tSTRING_DBEG;
7415 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7416 return tSTRING_DVAR;
7420 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7421 #define IS_END() IS_lex_state(EXPR_END_ANY)
7422 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7423 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7424 #define IS_LABEL_POSSIBLE() (\
7425 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7427 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7428 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7430 static inline enum yytokentype
7431 parser_string_term(struct parser_params *p, int func)
7434 if (func & STR_FUNC_REGEXP) {
7435 set_yylval_num(regx_options(p));
7436 dispatch_scan_event(p, tREGEXP_END);
7437 SET_LEX_STATE(EXPR_END);
7440 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7442 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7445 SET_LEX_STATE(EXPR_END);
7449 static enum yytokentype
7450 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7452 int func = (int)quote->u1.func;
7453 int term = (int)quote->u3.term;
7454 int paren = (int)quote->u2.paren;
7456 rb_encoding *enc = p->enc;
7457 rb_encoding *base_enc = 0;
7460 if (func & STR_FUNC_TERM) {
7461 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7462 SET_LEX_STATE(EXPR_END);
7464 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7467 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7468 do {c = nextc(p);} while (ISSPACE(c));
7471 if (func & STR_FUNC_LIST) {
7472 quote->u1.func &= ~STR_FUNC_LIST;
7475 if (c == term && !quote->u0.nest) {
7476 if (func & STR_FUNC_QWORDS) {
7477 quote->u1.func |= STR_FUNC_TERM;
7478 pushback(p, c); /* dispatch the term at tSTRING_END */
7479 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7482 return parser_string_term(p, func);
7486 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7490 if ((func & STR_FUNC_EXPAND) && c == '#') {
7491 int t = parser_peek_variable_name(p);
7497 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7498 &enc, &base_enc) == -1) {
7501 # define unterminated_literal(mesg) yyerror0(mesg)
7503 # define unterminated_literal(mesg) compile_error(p, mesg)
7505 literal_flush(p, p->lex.pcur);
7506 if (func & STR_FUNC_QWORDS) {
7507 /* no content to add, bailing out here */
7508 unterminated_literal("unterminated list meets end of file");
7512 if (func & STR_FUNC_REGEXP) {
7513 unterminated_literal("unterminated regexp meets end of file");
7516 unterminated_literal("unterminated string meets end of file");
7518 quote->u1.func |= STR_FUNC_TERM;
7523 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7524 set_yylval_str(lit);
7525 flush_string_content(p, enc);
7527 return tSTRING_CONTENT;
7530 static enum yytokentype
7531 heredoc_identifier(struct parser_params *p)
7534 * term_len is length of `<<"END"` except `END`,
7535 * in this case term_len is 4 (<, <, " and ").
7537 long len, offset = p->lex.pcur - p->lex.pbeg;
7538 int c = nextc(p), term, func = 0, quote = 0;
7539 enum yytokentype token = tSTRING_BEG;
7544 func = STR_FUNC_INDENT;
7547 else if (c == '~') {
7549 func = STR_FUNC_INDENT;
7555 func |= str_squote; goto quoted;
7557 func |= str_dquote; goto quoted;
7559 token = tXSTRING_BEG;
7560 func |= str_xquote; goto quoted;
7567 while ((c = nextc(p)) != term) {
7568 if (c == -1 || c == '\r' || c == '\n') {
7569 yyerror0("unterminated here document identifier");
7576 if (!parser_is_identchar(p)) {
7578 if (func & STR_FUNC_INDENT) {
7579 pushback(p, indent > 0 ? '~' : '-');
7585 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7586 if (n < 0) return 0;
7588 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7593 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7594 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7595 yyerror0("too long here document identifier");
7596 dispatch_scan_event(p, tHEREDOC_BEG);
7599 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7600 p->lex.strterm->flags |= STRTERM_HEREDOC;
7601 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7602 here->offset = offset;
7603 here->sourceline = p->ruby_sourceline;
7604 here->length = (int)len;
7605 here->quote = quote;
7609 p->heredoc_indent = indent;
7610 p->heredoc_line_indent = 0;
7615 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7620 line = here->lastline;
7621 p->lex.lastline = line;
7622 p->lex.pbeg = RSTRING_PTR(line);
7623 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7624 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7625 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7626 p->heredoc_end = p->ruby_sourceline;
7627 p->ruby_sourceline = (int)here->sourceline;
7628 if (p->eofp) p->lex.nextline = Qnil;
7633 dedent_string(VALUE string, int width)
7639 RSTRING_GETMEM(string, str, len);
7640 for (i = 0; i < len && col < width; i++) {
7641 if (str[i] == ' ') {
7644 else if (str[i] == '\t') {
7645 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7646 if (n > width) break;
7654 rb_str_modify(string);
7655 str = RSTRING_PTR(string);
7656 if (RSTRING_LEN(string) != len)
7657 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7658 MEMMOVE(str, str + i, char, len - i);
7659 rb_str_set_len(string, len - i);
7665 heredoc_dedent(struct parser_params *p, NODE *root)
7667 NODE *node, *str_node, *prev_node;
7668 int indent = p->heredoc_indent;
7671 if (indent <= 0) return root;
7672 p->heredoc_indent = 0;
7673 if (!root) return root;
7675 prev_node = node = str_node = root;
7676 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
7679 VALUE lit = str_node->nd_lit;
7680 if (str_node->flags & NODE_FL_NEWLINE) {
7681 dedent_string(lit, indent);
7686 else if (!literal_concat0(p, prev_lit, lit)) {
7690 NODE *end = node->nd_end;
7691 node = prev_node->nd_next = node->nd_next;
7693 if (nd_type_p(prev_node, NODE_DSTR))
7694 nd_set_type(prev_node, NODE_STR);
7702 while ((node = (prev_node = node)->nd_next) != 0) {
7704 if (!nd_type_p(node, NODE_LIST)) break;
7705 if ((str_node = node->nd_head) != 0) {
7706 enum node_type type = nd_type(str_node);
7707 if (type == NODE_STR || type == NODE_DSTR) break;
7717 heredoc_dedent(struct parser_params *p, VALUE array)
7719 int indent = p->heredoc_indent;
7721 if (indent <= 0) return array;
7722 p->heredoc_indent = 0;
7723 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7729 * Ripper.dedent_string(input, width) -> Integer
7731 * USE OF RIPPER LIBRARY ONLY.
7733 * Strips up to +width+ leading whitespaces from +input+,
7734 * and returns the stripped column width.
7737 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7742 wid = NUM2UINT(width);
7743 col = dedent_string(input, wid);
7744 return INT2NUM(col);
7749 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7751 const char *ptr = p->lex.pbeg;
7755 while (*ptr && ISSPACE(*ptr)) ptr++;
7757 n = p->lex.pend - (ptr + len);
7758 if (n < 0) return FALSE;
7759 if (n > 0 && ptr[len] != '\n') {
7760 if (ptr[len] != '\r') return FALSE;
7761 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7763 return strncmp(eos, ptr, len) == 0;
7767 word_match_p(struct parser_params *p, const char *word, long len)
7769 if (strncmp(p->lex.pcur, word, len)) return 0;
7770 if (p->lex.pcur + len == p->lex.pend) return 1;
7771 int c = (unsigned char)p->lex.pcur[len];
7772 if (ISSPACE(c)) return 1;
7774 case '\0': case '\004': case '\032': return 1;
7779 #define NUM_SUFFIX_R (1<<0)
7780 #define NUM_SUFFIX_I (1<<1)
7781 #define NUM_SUFFIX_ALL 3
7784 number_literal_suffix(struct parser_params *p, int mask)
7787 const char *lastp = p->lex.pcur;
7789 while ((c = nextc(p)) != -1) {
7790 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7791 result |= (mask & NUM_SUFFIX_I);
7792 mask &= ~NUM_SUFFIX_I;
7793 /* r after i, rational of complex is disallowed */
7794 mask &= ~NUM_SUFFIX_R;
7797 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7798 result |= (mask & NUM_SUFFIX_R);
7799 mask &= ~NUM_SUFFIX_R;
7802 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7803 p->lex.pcur = lastp;
7804 literal_flush(p, p->lex.pcur);
7813 static enum yytokentype
7814 set_number_literal(struct parser_params *p, VALUE v,
7815 enum yytokentype type, int suffix)
7817 if (suffix & NUM_SUFFIX_I) {
7818 v = rb_complex_raw(INT2FIX(0), v);
7821 set_yylval_literal(v);
7822 SET_LEX_STATE(EXPR_END);
7826 static enum yytokentype
7827 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7829 enum yytokentype type = tINTEGER;
7830 if (suffix & NUM_SUFFIX_R) {
7831 v = rb_rational_raw1(v);
7834 return set_number_literal(p, v, type, suffix);
7839 dispatch_heredoc_end(struct parser_params *p)
7842 if (has_delayed_token(p))
7843 dispatch_delayed_token(p, tSTRING_CONTENT);
7844 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7845 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7851 #define dispatch_heredoc_end(p) ((void)0)
7854 static enum yytokentype
7855 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7857 int c, func, indent = 0;
7858 const char *eos, *ptr, *ptr_end;
7861 rb_encoding *enc = p->enc;
7862 rb_encoding *base_enc = 0;
7865 eos = RSTRING_PTR(here->lastline) + here->offset;
7867 indent = (func = here->func) & STR_FUNC_INDENT;
7869 if ((c = nextc(p)) == -1) {
7872 if (!has_delayed_token(p)) {
7873 dispatch_scan_event(p, tSTRING_CONTENT);
7876 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7877 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7878 int cr = ENC_CODERANGE_UNKNOWN;
7879 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7880 if (cr != ENC_CODERANGE_7BIT &&
7881 p->enc == rb_usascii_encoding() &&
7882 enc != rb_utf8_encoding()) {
7883 enc = rb_ascii8bit_encoding();
7886 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7888 dispatch_delayed_token(p, tSTRING_CONTENT);
7892 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7893 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7897 SET_LEX_STATE(EXPR_END);
7902 /* not beginning of line, cannot be the terminator */
7904 else if (p->heredoc_line_indent == -1) {
7905 /* `heredoc_line_indent == -1` means
7906 * - "after an interpolation in the same line", or
7907 * - "in a continuing line"
7909 p->heredoc_line_indent = 0;
7911 else if (whole_match_p(p, eos, len, indent)) {
7912 dispatch_heredoc_end(p);
7914 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7917 SET_LEX_STATE(EXPR_END);
7921 if (!(func & STR_FUNC_EXPAND)) {
7923 ptr = RSTRING_PTR(p->lex.lastline);
7924 ptr_end = p->lex.pend;
7925 if (ptr_end > ptr) {
7926 switch (ptr_end[-1]) {
7928 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7937 if (p->heredoc_indent > 0) {
7939 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7941 p->heredoc_line_indent = 0;
7945 rb_str_cat(str, ptr, ptr_end - ptr);
7947 str = STR_NEW(ptr, ptr_end - ptr);
7948 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7950 if (p->heredoc_indent > 0) {
7953 if (nextc(p) == -1) {
7959 } while (!whole_match_p(p, eos, len, indent));
7962 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7965 int t = parser_peek_variable_name(p);
7966 if (p->heredoc_line_indent != -1) {
7967 if (p->heredoc_indent > p->heredoc_line_indent) {
7968 p->heredoc_indent = p->heredoc_line_indent;
7970 p->heredoc_line_indent = -1;
7979 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7980 if (p->eofp) goto error;
7984 if (c == '\\') p->heredoc_line_indent = -1;
7986 str = STR_NEW3(tok(p), toklen(p), enc, func);
7988 set_yylval_str(str);
7990 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7992 flush_string_content(p, enc);
7993 return tSTRING_CONTENT;
7995 tokadd(p, nextc(p));
7996 if (p->heredoc_indent > 0) {
8000 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8001 if ((c = nextc(p)) == -1) goto error;
8002 } while (!whole_match_p(p, eos, len, indent));
8003 str = STR_NEW3(tok(p), toklen(p), enc, func);
8005 dispatch_heredoc_end(p);
8007 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8010 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8012 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8013 set_yylval_str(str);
8015 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8017 return tSTRING_CONTENT;
8023 arg_ambiguous(struct parser_params *p, char c)
8027 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8030 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8033 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8040 formal_argument(struct parser_params *p, ID lhs)
8042 formal_argument(struct parser_params *p, VALUE lhs)
8045 ID id = get_id(lhs);
8047 switch (id_type(id)) {
8051 # define ERR(mesg) yyerror0(mesg)
8053 # define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8056 ERR("formal argument cannot be a constant");
8059 ERR("formal argument cannot be an instance variable");
8062 ERR("formal argument cannot be a global variable");
8065 ERR("formal argument cannot be a class variable");
8068 ERR("formal argument must be local variable");
8072 shadowing_lvar(p, id);
8077 lvar_defined(struct parser_params *p, ID id)
8079 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8082 /* emacsen -*- hack */
8084 parser_encode_length(struct parser_params *p, const char *name, long len)
8088 if (len > 5 && name[nlen = len - 5] == '-') {
8089 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8092 if (len > 4 && name[nlen = len - 4] == '-') {
8093 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8095 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8096 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8097 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8104 parser_set_encode(struct parser_params *p, const char *name)
8106 int idx = rb_enc_find_index(name);
8111 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8113 excargs[0] = rb_eArgError;
8114 excargs[2] = rb_make_backtrace();
8115 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8116 rb_exc_raise(rb_make_exception(3, excargs));
8118 enc = rb_enc_from_index(idx);
8119 if (!rb_enc_asciicompat(enc)) {
8120 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8125 if (p->debug_lines) {
8126 VALUE lines = p->debug_lines;
8127 long i, n = RARRAY_LEN(lines);
8128 for (i = 0; i < n; ++i) {
8129 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8136 comment_at_top(struct parser_params *p)
8138 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8139 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8140 while (ptr < ptr_end) {
8141 if (!ISSPACE(*ptr)) return 0;
8147 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8148 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8150 static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8153 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8155 if (!comment_at_top(p)) {
8158 parser_set_encode(p, val);
8162 parser_get_bool(struct parser_params *p, const char *name, const char *val)
8166 if (STRCASECMP(val, "true") == 0) {
8171 if (STRCASECMP(val, "false") == 0) {
8176 return parser_invalid_pragma_value(p, name, val);
8180 parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8182 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8187 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8189 int b = parser_get_bool(p, name, val);
8190 if (b >= 0) p->token_info_enabled = b;
8194 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8198 if (p->token_seen) {
8199 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8203 b = parser_get_bool(p, name, val);
8206 if (!p->compile_option)
8207 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8208 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8213 parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8215 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8216 if (*s == ' ' || *s == '\t') continue;
8217 if (*s == '#') break;
8218 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8224 if (STRCASECMP(val, "none") == 0) {
8225 p->ctxt.shareable_constant_value = shareable_none;
8230 if (STRCASECMP(val, "literal") == 0) {
8231 p->ctxt.shareable_constant_value = shareable_literal;
8236 if (STRCASECMP(val, "experimental_copy") == 0) {
8237 p->ctxt.shareable_constant_value = shareable_copy;
8240 if (STRCASECMP(val, "experimental_everything") == 0) {
8241 p->ctxt.shareable_constant_value = shareable_everything;
8246 parser_invalid_pragma_value(p, name, val);
8249 # if WARN_PAST_SCOPE
8251 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8253 int b = parser_get_bool(p, name, val);
8254 if (b >= 0) p->past_scope_enabled = b;
8258 struct magic_comment {
8260 rb_magic_comment_setter_t func;
8261 rb_magic_comment_length_t length;
8264 static const struct magic_comment magic_comments[] = {
8265 {"coding", magic_comment_encoding, parser_encode_length},
8266 {"encoding", magic_comment_encoding, parser_encode_length},
8267 {"frozen_string_literal", parser_set_compile_option_flag},
8268 {"shareable_constant_value", parser_set_shareable_constant_value},
8269 {"warn_indent", parser_set_token_info},
8270 # if WARN_PAST_SCOPE
8271 {"warn_past_scope", parser_set_past_scope},
8276 magic_comment_marker(const char *str, long len)
8283 if (str[i-1] == '*' && str[i-2] == '-') {
8289 if (i + 1 >= len) return 0;
8290 if (str[i+1] != '-') {
8293 else if (str[i-1] != '-') {
8309 parser_magic_comment(struct parser_params *p, const char *str, long len)
8312 VALUE name = 0, val = 0;
8313 const char *beg, *end, *vbeg, *vend;
8314 #define str_copy(_s, _p, _n) ((_s) \
8315 ? (void)(rb_str_resize((_s), (_n)), \
8316 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8317 : (void)((_s) = STR_NEW((_p), (_n))))
8319 if (len <= 7) return FALSE;
8320 if (!!(beg = magic_comment_marker(str, len))) {
8321 if (!(end = magic_comment_marker(beg, str + len - beg)))
8325 len = end - beg - 3;
8328 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8330 const struct magic_comment *mc = magic_comments;
8335 for (; len > 0 && *str; str++, --len) {
8337 case '\'': case '"': case ':': case ';':
8340 if (!ISSPACE(*str)) break;
8342 for (beg = str; len > 0; str++, --len) {
8344 case '\'': case '"': case ':': case ';':
8347 if (ISSPACE(*str)) break;
8352 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8355 if (!indicator) return FALSE;
8359 do str++; while (--len > 0 && ISSPACE(*str));
8362 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8375 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8379 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8382 while (len > 0 && (ISSPACE(*str))) --len, str++;
8383 if (len) return FALSE;
8387 str_copy(name, beg, n);
8388 s = RSTRING_PTR(name);
8389 for (i = 0; i < n; ++i) {
8390 if (s[i] == '-') s[i] = '_';
8393 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8396 n = (*mc->length)(p, vbeg, n);
8398 str_copy(val, vbeg, n);
8399 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8402 } while (++mc < magic_comments + numberof(magic_comments));
8404 str_copy(val, vbeg, vend - vbeg);
8405 dispatch2(magic_comment, name, val);
8413 set_file_encoding(struct parser_params *p, const char *str, const char *send)
8416 const char *beg = str;
8420 if (send - str <= 6) return;
8422 case 'C': case 'c': str += 6; continue;
8423 case 'O': case 'o': str += 5; continue;
8424 case 'D': case 'd': str += 4; continue;
8425 case 'I': case 'i': str += 3; continue;
8426 case 'N': case 'n': str += 2; continue;
8427 case 'G': case 'g': str += 1; continue;
8434 if (ISSPACE(*str)) break;
8437 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8442 if (++str >= send) return;
8443 } while (ISSPACE(*str));
8445 if (*str != '=' && *str != ':') return;
8450 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8451 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8452 parser_set_encode(p, RSTRING_PTR(s));
8453 rb_str_resize(s, 0);
8457 parser_prepare(struct parser_params *p)
8459 int c = nextc0(p, FALSE);
8460 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8463 if (peek(p, '!')) p->has_shebang = 1;
8465 case 0xef: /* UTF-8 BOM marker */
8466 if (p->lex.pend - p->lex.pcur >= 2 &&
8467 (unsigned char)p->lex.pcur[0] == 0xbb &&
8468 (unsigned char)p->lex.pcur[1] == 0xbf) {
8469 p->enc = rb_utf8_encoding();
8472 if (p->debug_lines) {
8473 rb_enc_associate(p->lex.lastline, p->enc);
8476 p->lex.pbeg = p->lex.pcur;
8484 p->enc = rb_enc_get(p->lex.lastline);
8488 #define ambiguous_operator(tok, op, syn) ( \
8489 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8490 rb_warning0("even though it seems like "syn""))
8492 #define ambiguous_operator(tok, op, syn) \
8493 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8495 #define warn_balanced(tok, op, syn) ((void) \
8496 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8497 space_seen && !ISSPACE(c) && \
8498 (ambiguous_operator(tok, op, syn), 0)), \
8499 (enum yytokentype)(tok))
8502 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8505 char *point = &str[seen_point];
8506 size_t fraclen = len-seen_point-1;
8507 memmove(point, point+1, fraclen+1);
8508 v = rb_cstr_to_inum(str, 10, FALSE);
8509 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8512 static enum yytokentype
8513 no_digits(struct parser_params *p)
8515 yyerror0("numeric literal without digits");
8516 if (peek(p, '_')) nextc(p);
8517 /* dummy 0, for tUMINUS_NUM at numeric */
8518 return set_integer_literal(p, INT2FIX(0), 0);
8521 static enum yytokentype
8522 parse_numeric(struct parser_params *p, int c)
8524 int is_float, seen_point, seen_e, nondigit;
8527 is_float = seen_point = seen_e = nondigit = 0;
8528 SET_LEX_STATE(EXPR_END);
8530 if (c == '-' || c == '+') {
8535 int start = toklen(p);
8537 if (c == 'x' || c == 'X') {
8540 if (c != -1 && ISXDIGIT(c)) {
8543 if (nondigit) break;
8547 if (!ISXDIGIT(c)) break;
8550 } while ((c = nextc(p)) != -1);
8554 if (toklen(p) == start) {
8555 return no_digits(p);
8557 else if (nondigit) goto trailing_uc;
8558 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8559 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8561 if (c == 'b' || c == 'B') {
8564 if (c == '0' || c == '1') {
8567 if (nondigit) break;
8571 if (c != '0' && c != '1') break;
8574 } while ((c = nextc(p)) != -1);
8578 if (toklen(p) == start) {
8579 return no_digits(p);
8581 else if (nondigit) goto trailing_uc;
8582 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8583 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8585 if (c == 'd' || c == 'D') {
8588 if (c != -1 && ISDIGIT(c)) {
8591 if (nondigit) break;
8595 if (!ISDIGIT(c)) break;
8598 } while ((c = nextc(p)) != -1);
8602 if (toklen(p) == start) {
8603 return no_digits(p);
8605 else if (nondigit) goto trailing_uc;
8606 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8607 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8613 if (c == 'o' || c == 'O') {
8614 /* prefixed octal */
8616 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8617 return no_digits(p);
8620 if (c >= '0' && c <= '7') {
8625 if (nondigit) break;
8629 if (c < '0' || c > '9') break;
8630 if (c > '7') goto invalid_octal;
8633 } while ((c = nextc(p)) != -1);
8634 if (toklen(p) > start) {
8637 if (nondigit) goto trailing_uc;
8638 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8639 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8646 if (c > '7' && c <= '9') {
8648 yyerror0("Invalid octal digit");
8650 else if (c == '.' || c == 'e' || c == 'E') {
8655 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8656 return set_integer_literal(p, INT2FIX(0), suffix);
8662 case '0': case '1': case '2': case '3': case '4':
8663 case '5': case '6': case '7': case '8': case '9':
8669 if (nondigit) goto trailing_uc;
8670 if (seen_point || seen_e) {
8675 if (c0 == -1 || !ISDIGIT(c0)) {
8681 seen_point = toklen(p);
8700 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8705 tokadd(p, nondigit);
8709 nondigit = (c == '-' || c == '+') ? c : 0;
8712 case '_': /* `_' in number just ignored */
8713 if (nondigit) goto decode_num;
8727 literal_flush(p, p->lex.pcur - 1);
8728 YYLTYPE loc = RUBY_INIT_YYLLOC();
8729 compile_error(p, "trailing `%c' in number", nondigit);
8730 parser_show_error_line(p, &loc);
8734 enum yytokentype type = tFLOAT;
8737 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8738 if (suffix & NUM_SUFFIX_R) {
8740 v = parse_rational(p, tok(p), toklen(p), seen_point);
8743 double d = strtod(tok(p), 0);
8744 if (errno == ERANGE) {
8745 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8750 return set_number_literal(p, v, type, suffix);
8752 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8753 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8756 static enum yytokentype
8757 parse_qmark(struct parser_params *p, int space_seen)
8764 SET_LEX_STATE(EXPR_VALUE);
8769 compile_error(p, "incomplete character syntax");
8772 if (rb_enc_isspace(c, p->enc)) {
8774 int c2 = escaped_control_code(c);
8776 WARN_SPACE_CHAR(c2, "?");
8781 SET_LEX_STATE(EXPR_VALUE);
8786 if (!parser_isascii(p)) {
8787 if (tokadd_mbchar(p, c) == -1) return 0;
8789 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8790 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8792 const char *start = p->lex.pcur - 1, *ptr = start;
8794 int n = parser_precise_mbclen(p, ptr);
8795 if (n < 0) return -1;
8797 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8798 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8799 " a conditional operator, put a space after `?'",
8800 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8804 else if (c == '\\') {
8807 enc = rb_utf8_encoding();
8808 tokadd_utf8(p, &enc, -1, 0, 0);
8810 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8812 if (tokadd_mbchar(p, c) == -1) return 0;
8815 c = read_escape(p, 0, &enc);
8823 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8824 set_yylval_str(lit);
8825 SET_LEX_STATE(EXPR_END);
8829 static enum yytokentype
8830 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8833 const char *ptok = p->lex.pcur;
8841 if (c == -1) goto unterminated;
8844 if (!ISASCII(c)) goto unknown;
8849 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8852 c = parser_precise_mbclen(p, p->lex.pcur);
8853 if (c < 0) return 0;
8855 yyerror0("unknown type of %string");
8861 compile_error(p, "unterminated quoted string meets end of file");
8865 if (term == '(') term = ')';
8866 else if (term == '[') term = ']';
8867 else if (term == '{') term = '}';
8868 else if (term == '<') term = '>';
8871 p->lex.ptok = ptok-1;
8874 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8878 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8882 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8886 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8890 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8891 return tSYMBOLS_BEG;
8894 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8895 return tQSYMBOLS_BEG;
8898 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8899 return tXSTRING_BEG;
8902 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8906 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8907 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8911 yyerror0("unknown type of %string");
8915 if ((c = nextc(p)) == '=') {
8917 SET_LEX_STATE(EXPR_BEG);
8920 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8923 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8925 return warn_balanced('%', "%%", "string literal");
8929 tokadd_ident(struct parser_params *p, int c)
8932 if (tokadd_mbchar(p, c) == -1) return -1;
8934 } while (parser_is_identchar(p));
8940 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8942 ID ident = TOK_INTERN();
8944 set_yylval_name(ident);
8950 parse_numvar(struct parser_params *p)
8954 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8955 const unsigned long nth_ref_max =
8956 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8957 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8958 * turned into a Fixnum, in compile.c */
8960 if (overflow || n > nth_ref_max) {
8961 /* compile_error()? */
8962 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8963 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8970 static enum yytokentype
8971 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8973 const char *ptr = p->lex.pcur;
8976 SET_LEX_STATE(EXPR_END);
8977 p->lex.ptok = ptr - 1; /* from '$' */
8981 case '_': /* $_: last read line string */
8983 if (parser_is_identchar(p)) {
8991 case '~': /* $~: match-data */
8992 case '*': /* $*: argv */
8993 case '$': /* $$: pid */
8994 case '?': /* $?: last status */
8995 case '!': /* $!: error string */
8996 case '@': /* $@: error position */
8997 case '/': /* $/: input record separator */
8998 case '\\': /* $\: output record separator */
8999 case ';': /* $;: field separator */
9000 case ',': /* $,: output field separator */
9001 case '.': /* $.: last read line number */
9002 case '=': /* $=: ignorecase */
9003 case ':': /* $:: load path */
9004 case '<': /* $<: reading filename */
9005 case '>': /* $>: default output handle */
9006 case '\"': /* $": already loaded files */
9015 if (parser_is_identchar(p)) {
9016 if (tokadd_mbchar(p, c) == -1) return 0;
9024 set_yylval_name(TOK_INTERN());
9027 case '&': /* $&: last match */
9028 case '`': /* $`: string before last match */
9029 case '\'': /* $': string after last match */
9030 case '+': /* $+: string matches last paren. */
9031 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9036 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9039 case '1': case '2': case '3':
9040 case '4': case '5': case '6':
9041 case '7': case '8': case '9':
9046 } while (c != -1 && ISDIGIT(c));
9048 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9050 c = parse_numvar(p);
9051 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9055 if (!parser_is_identchar(p)) {
9056 YYLTYPE loc = RUBY_INIT_YYLLOC();
9057 if (c == -1 || ISSPACE(c)) {
9058 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9062 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9064 parser_show_error_line(p, &loc);
9065 set_yylval_noname();
9073 if (tokadd_ident(p, c)) return 0;
9074 SET_LEX_STATE(EXPR_END);
9075 tokenize_ident(p, last_state);
9081 parser_numbered_param(struct parser_params *p, int n)
9083 if (n < 0) return false;
9085 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9088 if (p->max_numparam == ORDINAL_PARAM) {
9089 compile_error(p, "ordinary parameter is defined");
9092 struct vtable *args = p->lvtbl->args;
9093 if (p->max_numparam < n) {
9094 p->max_numparam = n;
9096 while (n > args->pos) {
9097 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9103 static enum yytokentype
9104 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9106 const char *ptr = p->lex.pcur;
9107 enum yytokentype result = tIVAR;
9108 register int c = nextc(p);
9111 p->lex.ptok = ptr - 1; /* from '@' */
9119 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9120 if (c == -1 || !parser_is_identchar(p)) {
9122 RUBY_SET_YYLLOC(loc);
9123 if (result == tIVAR) {
9124 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9127 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9129 parser_show_error_line(p, &loc);
9130 set_yylval_noname();
9131 SET_LEX_STATE(EXPR_END);
9134 else if (ISDIGIT(c)) {
9136 RUBY_SET_YYLLOC(loc);
9137 if (result == tIVAR) {
9138 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9141 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9143 parser_show_error_line(p, &loc);
9144 set_yylval_noname();
9145 SET_LEX_STATE(EXPR_END);
9149 if (tokadd_ident(p, c)) return 0;
9150 tokenize_ident(p, last_state);
9154 static enum yytokentype
9155 parse_ident(struct parser_params *p, int c, int cmd_state)
9157 enum yytokentype result;
9158 int mb = ENC_CODERANGE_7BIT;
9159 const enum lex_state_e last_state = p->lex.state;
9163 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9164 if (tokadd_mbchar(p, c) == -1) return 0;
9166 } while (parser_is_identchar(p));
9167 if ((c == '!' || c == '?') && !peek(p, '=')) {
9171 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9172 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9173 result = tIDENTIFIER;
9177 result = tCONSTANT; /* assume provisionally */
9182 if (IS_LABEL_POSSIBLE()) {
9183 if (IS_LABEL_SUFFIX(0)) {
9184 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9186 set_yylval_name(TOK_INTERN());
9190 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9191 const struct kwtable *kw;
9193 /* See if it is a reserved word. */
9194 kw = rb_reserved_word(tok(p), toklen(p));
9196 enum lex_state_e state = p->lex.state;
9197 if (IS_lex_state_for(state, EXPR_FNAME)) {
9198 SET_LEX_STATE(EXPR_ENDFN);
9199 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9202 SET_LEX_STATE(kw->state);
9203 if (IS_lex_state(EXPR_BEG)) {
9204 p->command_start = TRUE;
9206 if (kw->id[0] == keyword_do) {
9207 if (lambda_beginning_p()) {
9208 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9209 return keyword_do_LAMBDA;
9211 if (COND_P()) return keyword_do_cond;
9212 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9213 return keyword_do_block;
9216 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9219 if (kw->id[0] != kw->id[1])
9220 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9226 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9228 SET_LEX_STATE(EXPR_CMDARG);
9231 SET_LEX_STATE(EXPR_ARG);
9234 else if (p->lex.state == EXPR_FNAME) {
9235 SET_LEX_STATE(EXPR_ENDFN);
9238 SET_LEX_STATE(EXPR_END);
9241 ident = tokenize_ident(p, last_state);
9242 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9243 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9244 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9245 lvar_defined(p, ident)) {
9246 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9251 static enum yytokentype
9252 parser_yylex(struct parser_params *p)
9258 enum lex_state_e last_state;
9259 int fallthru = FALSE;
9260 int token_seen = p->token_seen;
9262 if (p->lex.strterm) {
9263 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9264 return here_document(p, &p->lex.strterm->u.heredoc);
9268 return parse_string(p, &p->lex.strterm->u.literal);
9271 cmd_state = p->command_start;
9272 p->command_start = FALSE;
9273 p->token_seen = TRUE;
9275 last_state = p->lex.state;
9279 switch (c = nextc(p)) {
9280 case '\0': /* NUL */
9281 case '\004': /* ^D */
9282 case '\032': /* ^Z */
9283 case -1: /* end of script. */
9290 /* carried over with p->lex.nextline for nextc() */
9291 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9294 case ' ': case '\t': case '\f':
9295 case '\13': /* '\v' */
9298 while ((c = nextc(p))) {
9300 case ' ': case '\t': case '\f': case '\r':
9301 case '\13': /* '\v' */
9309 dispatch_scan_event(p, tSP);
9313 case '#': /* it's a comment */
9314 p->token_seen = token_seen;
9315 /* no magic_comment in shebang line */
9316 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9317 if (comment_at_top(p)) {
9318 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9322 dispatch_scan_event(p, tCOMMENT);
9326 p->token_seen = token_seen;
9327 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9328 !IS_lex_state(EXPR_LABELED));
9329 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9331 dispatch_scan_event(p, tIGNORED_NL);
9334 if (!c && p->ctxt.in_kwarg) {
9335 goto normal_newline;
9340 switch (c = nextc(p)) {
9341 case ' ': case '\t': case '\f': case '\r':
9342 case '\13': /* '\v' */
9347 if (space_seen) dispatch_scan_event(p, tSP);
9351 dispatch_delayed_token(p, tIGNORED_NL);
9352 if (peek(p, '.') == (c == '&')) {
9354 dispatch_scan_event(p, tSP);
9359 p->ruby_sourceline--;
9360 p->lex.nextline = p->lex.lastline;
9361 case -1: /* EOF no decrement*/
9363 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9364 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9365 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9366 pushback(p, 1); /* always pushback */
9367 p->lex.ptok = p->lex.pcur;
9371 p->lex.ptok = p->lex.pcur;
9374 goto normal_newline;
9378 p->command_start = TRUE;
9379 SET_LEX_STATE(EXPR_BEG);
9383 if ((c = nextc(p)) == '*') {
9384 if ((c = nextc(p)) == '=') {
9385 set_yylval_id(idPow);
9386 SET_LEX_STATE(EXPR_BEG);
9391 rb_warning0("`**' interpreted as argument prefix");
9394 else if (IS_BEG()) {
9398 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9404 SET_LEX_STATE(EXPR_BEG);
9409 rb_warning0("`*' interpreted as argument prefix");
9412 else if (IS_BEG()) {
9416 c = warn_balanced('*', "*", "argument prefix");
9419 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9424 if (IS_AFTER_OPERATOR()) {
9425 SET_LEX_STATE(EXPR_ARG);
9431 SET_LEX_STATE(EXPR_BEG);
9444 /* skip embedded rd document */
9445 if (word_match_p(p, "begin", 5)) {
9449 dispatch_scan_event(p, tEMBDOC_BEG);
9453 dispatch_scan_event(p, tEMBDOC);
9458 compile_error(p, "embedded document meets end of file");
9461 if (c == '=' && word_match_p(p, "end", 3)) {
9467 dispatch_scan_event(p, tEMBDOC_END);
9472 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9473 if ((c = nextc(p)) == '=') {
9474 if ((c = nextc(p)) == '=') {
9483 else if (c == '>') {
9492 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9494 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9495 int token = heredoc_identifier(p);
9496 if (token) return token < 0 ? 0 : token;
9498 if (IS_AFTER_OPERATOR()) {
9499 SET_LEX_STATE(EXPR_ARG);
9502 if (IS_lex_state(EXPR_CLASS))
9503 p->command_start = TRUE;
9504 SET_LEX_STATE(EXPR_BEG);
9507 if ((c = nextc(p)) == '>') {
9514 if ((c = nextc(p)) == '=') {
9515 set_yylval_id(idLTLT);
9516 SET_LEX_STATE(EXPR_BEG);
9520 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9526 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9527 if ((c = nextc(p)) == '=') {
9531 if ((c = nextc(p)) == '=') {
9532 set_yylval_id(idGTGT);
9533 SET_LEX_STATE(EXPR_BEG);
9543 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9544 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9545 p->lex.ptok = p->lex.pcur-1;
9549 if (IS_lex_state(EXPR_FNAME)) {
9550 SET_LEX_STATE(EXPR_ENDFN);
9553 if (IS_lex_state(EXPR_DOT)) {
9555 SET_LEX_STATE(EXPR_CMDARG);
9557 SET_LEX_STATE(EXPR_ARG);
9560 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9561 return tXSTRING_BEG;
9564 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9565 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9566 p->lex.ptok = p->lex.pcur-1;
9570 return parse_qmark(p, space_seen);
9573 if ((c = nextc(p)) == '&') {
9574 SET_LEX_STATE(EXPR_BEG);
9575 if ((c = nextc(p)) == '=') {
9576 set_yylval_id(idANDOP);
9577 SET_LEX_STATE(EXPR_BEG);
9583 else if (c == '=') {
9585 SET_LEX_STATE(EXPR_BEG);
9588 else if (c == '.') {
9589 set_yylval_id(idANDDOT);
9590 SET_LEX_STATE(EXPR_DOT);
9596 (c = peekc_n(p, 1)) == -1 ||
9597 !(c == '\'' || c == '"' ||
9598 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9599 rb_warning0("`&' interpreted as argument prefix");
9603 else if (IS_BEG()) {
9607 c = warn_balanced('&', "&", "argument prefix");
9609 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9613 if ((c = nextc(p)) == '|') {
9614 SET_LEX_STATE(EXPR_BEG);
9615 if ((c = nextc(p)) == '=') {
9616 set_yylval_id(idOROP);
9617 SET_LEX_STATE(EXPR_BEG);
9621 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9630 SET_LEX_STATE(EXPR_BEG);
9633 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9639 if (IS_AFTER_OPERATOR()) {
9640 SET_LEX_STATE(EXPR_ARG);
9649 SET_LEX_STATE(EXPR_BEG);
9652 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9653 SET_LEX_STATE(EXPR_BEG);
9655 if (c != -1 && ISDIGIT(c)) {
9656 return parse_numeric(p, '+');
9660 SET_LEX_STATE(EXPR_BEG);
9662 return warn_balanced('+', "+", "unary operator");
9666 if (IS_AFTER_OPERATOR()) {
9667 SET_LEX_STATE(EXPR_ARG);
9676 SET_LEX_STATE(EXPR_BEG);
9680 SET_LEX_STATE(EXPR_ENDFN);
9683 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9684 SET_LEX_STATE(EXPR_BEG);
9686 if (c != -1 && ISDIGIT(c)) {
9691 SET_LEX_STATE(EXPR_BEG);
9693 return warn_balanced('-', "-", "unary operator");
9696 int is_beg = IS_BEG();
9697 SET_LEX_STATE(EXPR_BEG);
9698 if ((c = nextc(p)) == '.') {
9699 if ((c = nextc(p)) == '.') {
9700 if (p->ctxt.in_argdef) {
9701 SET_LEX_STATE(EXPR_ENDARG);
9704 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9705 rb_warn0("... at EOL, should be parenthesized?");
9707 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9708 if (IS_lex_state_for(last_state, EXPR_LABEL))
9711 return is_beg ? tBDOT3 : tDOT3;
9714 return is_beg ? tBDOT2 : tDOT2;
9717 if (c != -1 && ISDIGIT(c)) {
9718 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9719 parse_numeric(p, '.');
9720 if (ISDIGIT(prev)) {
9721 yyerror0("unexpected fraction part after numeric literal");
9724 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9726 SET_LEX_STATE(EXPR_END);
9727 p->lex.ptok = p->lex.pcur;
9731 SET_LEX_STATE(EXPR_DOT);
9735 case '0': case '1': case '2': case '3': case '4':
9736 case '5': case '6': case '7': case '8': case '9':
9737 return parse_numeric(p, c);
9742 SET_LEX_STATE(EXPR_ENDFN);
9743 p->lex.paren_nest--;
9749 SET_LEX_STATE(EXPR_END);
9750 p->lex.paren_nest--;
9754 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9755 if (!p->lex.brace_nest--) return tSTRING_DEND;
9758 SET_LEX_STATE(EXPR_END);
9759 p->lex.paren_nest--;
9765 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9766 SET_LEX_STATE(EXPR_BEG);
9769 set_yylval_id(idCOLON2);
9770 SET_LEX_STATE(EXPR_DOT);
9773 if (IS_END() || ISSPACE(c) || c == '#') {
9775 c = warn_balanced(':', ":", "symbol literal");
9776 SET_LEX_STATE(EXPR_BEG);
9781 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9784 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9790 SET_LEX_STATE(EXPR_FNAME);
9795 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9798 if ((c = nextc(p)) == '=') {
9800 SET_LEX_STATE(EXPR_BEG);
9805 arg_ambiguous(p, '/');
9806 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9809 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9810 return warn_balanced('/', "/", "regexp literal");
9813 if ((c = nextc(p)) == '=') {
9815 SET_LEX_STATE(EXPR_BEG);
9818 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9823 SET_LEX_STATE(EXPR_BEG);
9824 p->command_start = TRUE;
9828 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9832 if (IS_AFTER_OPERATOR()) {
9833 if ((c = nextc(p)) != '@') {
9836 SET_LEX_STATE(EXPR_ARG);
9839 SET_LEX_STATE(EXPR_BEG);
9847 else if (!space_seen) {
9848 /* foo( ... ) => method call, no ambiguity */
9850 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9853 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9854 rb_warning0("parentheses after method name is interpreted as "
9855 "an argument list, not a decomposed argument");
9857 p->lex.paren_nest++;
9860 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9864 p->lex.paren_nest++;
9865 if (IS_AFTER_OPERATOR()) {
9866 if ((c = nextc(p)) == ']') {
9867 p->lex.paren_nest--;
9868 SET_LEX_STATE(EXPR_ARG);
9869 if ((c = nextc(p)) == '=') {
9876 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9879 else if (IS_BEG()) {
9882 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9885 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9891 ++p->lex.brace_nest;
9892 if (lambda_beginning_p())
9894 else if (IS_lex_state(EXPR_LABELED))
9895 c = tLBRACE; /* hash */
9896 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9897 c = '{'; /* block (primary) */
9898 else if (IS_lex_state(EXPR_ENDARG))
9899 c = tLBRACE_ARG; /* block (expr) */
9901 c = tLBRACE; /* hash */
9903 p->command_start = TRUE;
9904 SET_LEX_STATE(EXPR_BEG);
9907 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9909 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9918 dispatch_scan_event(p, tSP);
9919 goto retry; /* skip \\n */
9921 if (c == ' ') return tSP;
9922 if (ISSPACE(c)) return c;
9927 return parse_percent(p, space_seen, last_state);
9930 return parse_gvar(p, last_state);
9933 return parse_atmark(p, last_state);
9936 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9937 p->ruby__end__seen = 1;
9943 dispatch_scan_event(p, k__END__);
9951 if (!parser_is_identchar(p)) {
9952 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9961 return parse_ident(p, c, cmd_state);
9964 static enum yytokentype
9965 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9971 t = parser_yylex(p);
9973 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9974 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9976 RUBY_SET_YYLLOC(*yylloc);
9978 if (has_delayed_token(p))
9979 dispatch_delayed_token(p, t);
9981 dispatch_scan_event(p, t);
9986 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9989 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9991 NODE *n = rb_ast_newnode(p->ast, type);
9993 rb_node_init(n, type, a0, a1, a2);
9996 nd_set_node_id(n, parser_get_node_id(p));
10001 nd_set_loc(NODE *nd, const YYLTYPE *loc)
10004 nd_set_line(nd, loc->beg_pos.lineno);
10009 static enum node_type
10010 nodetype(NODE *node) /* for debug */
10012 return (enum node_type)nd_type(node);
10016 nodeline(NODE *node)
10018 return nd_line(node);
10022 newline_node(NODE *node)
10025 node = remove_begin(node);
10026 node->flags |= NODE_FL_NEWLINE;
10032 fixpos(NODE *node, NODE *orig)
10036 nd_set_line(node, nd_line(orig));
10040 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10042 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10046 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10048 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10052 block_append(struct parser_params *p, NODE *head, NODE *tail)
10054 NODE *end, *h = head, *nd;
10056 if (tail == 0) return head;
10058 if (h == 0) return tail;
10059 switch (nd_type(h)) {
10066 parser_warning(p, h, "unused literal ignored");
10069 h = end = NEW_BLOCK(head, &head->nd_loc);
10079 switch (nd_type(nd)) {
10085 if (RTEST(ruby_verbose)) {
10086 parser_warning(p, tail, "statement not reached");
10094 if (!nd_type_p(tail, NODE_BLOCK)) {
10095 tail = NEW_BLOCK(tail, &tail->nd_loc);
10096 tail->nd_end = tail;
10098 end->nd_next = tail;
10099 h->nd_end = tail->nd_end;
10100 nd_set_last_loc(head, nd_last_loc(tail));
10104 /* append item to the list */
10106 list_append(struct parser_params *p, NODE *list, NODE *item)
10110 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10111 if (list->nd_next) {
10112 last = list->nd_next->nd_end;
10118 list->nd_alen += 1;
10119 last->nd_next = NEW_LIST(item, &item->nd_loc);
10120 list->nd_next->nd_end = last->nd_next;
10122 nd_set_last_loc(list, nd_last_loc(item));
10127 /* concat two lists */
10129 list_concat(NODE *head, NODE *tail)
10133 if (head->nd_next) {
10134 last = head->nd_next->nd_end;
10140 head->nd_alen += tail->nd_alen;
10141 last->nd_next = tail;
10142 if (tail->nd_next) {
10143 head->nd_next->nd_end = tail->nd_next->nd_end;
10146 head->nd_next->nd_end = tail;
10149 nd_set_last_loc(head, nd_last_loc(tail));
10155 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10157 if (NIL_P(tail)) return 1;
10158 if (!rb_enc_compatible(head, tail)) {
10159 compile_error(p, "string literal encodings differ (%s / %s)",
10160 rb_enc_name(rb_enc_get(head)),
10161 rb_enc_name(rb_enc_get(tail)));
10162 rb_str_resize(head, 0);
10163 rb_str_resize(tail, 0);
10166 rb_str_buf_append(head, tail);
10171 string_literal_head(enum node_type htype, NODE *head)
10173 if (htype != NODE_DSTR) return Qfalse;
10174 if (head->nd_next) {
10175 head = head->nd_next->nd_end->nd_head;
10176 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10178 const VALUE lit = head->nd_lit;
10179 ASSUME(lit != Qfalse);
10183 /* concat two string literals */
10185 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10187 enum node_type htype;
10190 if (!head) return tail;
10191 if (!tail) return head;
10193 htype = nd_type(head);
10194 if (htype == NODE_EVSTR) {
10195 head = new_dstr(p, head, loc);
10198 if (p->heredoc_indent > 0) {
10201 nd_set_type(head, NODE_DSTR);
10203 return list_append(p, head, tail);
10208 switch (nd_type(tail)) {
10210 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10214 lit = head->nd_lit;
10216 if (htype == NODE_STR) {
10217 if (!literal_concat0(p, lit, tail->nd_lit)) {
10219 rb_discard_node(p, head);
10220 rb_discard_node(p, tail);
10223 rb_discard_node(p, tail);
10226 list_append(p, head, tail);
10231 if (htype == NODE_STR) {
10232 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10234 tail->nd_lit = head->nd_lit;
10235 rb_discard_node(p, head);
10238 else if (NIL_P(tail->nd_lit)) {
10240 head->nd_alen += tail->nd_alen - 1;
10241 if (!head->nd_next) {
10242 head->nd_next = tail->nd_next;
10244 else if (tail->nd_next) {
10245 head->nd_next->nd_end->nd_next = tail->nd_next;
10246 head->nd_next->nd_end = tail->nd_next->nd_end;
10248 rb_discard_node(p, tail);
10250 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10251 if (!literal_concat0(p, lit, tail->nd_lit))
10253 tail->nd_lit = Qnil;
10257 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10262 if (htype == NODE_STR) {
10263 nd_set_type(head, NODE_DSTR);
10266 list_append(p, head, tail);
10273 evstr2dstr(struct parser_params *p, NODE *node)
10275 if (nd_type_p(node, NODE_EVSTR)) {
10276 node = new_dstr(p, node, &node->nd_loc);
10282 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10287 switch (nd_type(node)) {
10289 nd_set_type(node, NODE_DSTR);
10297 return NEW_EVSTR(head, loc);
10301 new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10303 VALUE lit = STR_NEW0();
10304 NODE *dstr = NEW_DSTR(lit, loc);
10305 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10306 return list_append(p, dstr, node);
10310 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10311 const YYLTYPE *op_loc, const YYLTYPE *loc)
10316 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10317 nd_set_line(expr, op_loc->beg_pos.lineno);
10322 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10326 opcall = NEW_OPCALL(recv, id, 0, loc);
10327 nd_set_line(opcall, op_loc->beg_pos.lineno);
10332 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10334 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10335 nd_set_line(qcall, op_loc->beg_pos.lineno);
10340 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10343 if (block) block_dup_check(p, args, block);
10344 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10345 if (block) ret = method_add_block(p, ret, block, loc);
10350 #define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10352 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10355 int line = op_loc->beg_pos.lineno;
10359 if (node1 && (n = nd_once_body(node1)) != 0) {
10360 switch (nd_type(n)) {
10363 NODE *match = NEW_MATCH2(node1, node2, loc);
10364 nd_set_line(match, line);
10369 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10370 const VALUE lit = n->nd_lit;
10371 NODE *match = NEW_MATCH2(node1, node2, loc);
10372 match->nd_args = reg_named_capture_assign(p, lit, loc);
10373 nd_set_line(match, line);
10379 if (node2 && (n = nd_once_body(node2)) != 0) {
10382 switch (nd_type(n)) {
10384 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10387 match3 = NEW_MATCH3(node2, node1, loc);
10392 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10393 nd_set_line(n, line);
10397 # if WARN_PAST_SCOPE
10399 past_dvar_p(struct parser_params *p, ID id)
10401 struct vtable *past = p->lvtbl->past;
10403 if (vtable_included(past, id)) return 1;
10411 numparam_nested_p(struct parser_params *p)
10413 struct local_vars *local = p->lvtbl;
10414 NODE *outer = local->numparam.outer;
10415 NODE *inner = local->numparam.inner;
10416 if (outer || inner) {
10417 NODE *used = outer ? outer : inner;
10418 compile_error(p, "numbered parameter is already used in\n"
10419 "%s:%d: %s block here",
10420 p->ruby_sourcefile, nd_line(used),
10421 outer ? "outer" : "inner");
10422 parser_show_error_line(p, &used->nd_loc);
10429 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10435 return NEW_SELF(loc);
10437 return NEW_NIL(loc);
10439 return NEW_TRUE(loc);
10440 case keyword_false:
10441 return NEW_FALSE(loc);
10442 case keyword__FILE__:
10444 VALUE file = p->ruby_sourcefile_string;
10446 file = rb_str_new(0, 0);
10448 file = rb_str_dup(file);
10449 node = NEW_STR(file, loc);
10450 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10453 case keyword__LINE__:
10454 return NEW_LIT(INT2FIX(p->tokline), loc);
10455 case keyword__ENCODING__:
10456 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10457 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10461 switch (id_type(id)) {
10463 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10464 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10465 if (id == p->cur_arg) {
10466 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10469 if (vidp) *vidp |= LVAR_USED;
10470 node = NEW_DVAR(id, loc);
10473 if (local_id_ref(p, id, &vidp)) {
10474 if (id == p->cur_arg) {
10475 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10478 if (vidp) *vidp |= LVAR_USED;
10479 node = NEW_LVAR(id, loc);
10482 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10483 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10484 if (numparam_nested_p(p)) return 0;
10485 node = NEW_DVAR(id, loc);
10486 struct local_vars *local = p->lvtbl;
10487 if (!local->numparam.current) local->numparam.current = node;
10490 # if WARN_PAST_SCOPE
10491 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10492 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10495 /* method call without arguments */
10496 return NEW_VCALL(id, loc);
10498 return NEW_GVAR(id, loc);
10500 return NEW_IVAR(id, loc);
10502 return NEW_CONST(id, loc);
10504 return NEW_CVAR(id, loc);
10506 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10511 opt_arg_append(NODE *opt_list, NODE *opt)
10513 NODE *opts = opt_list;
10514 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10516 while (opts->nd_next) {
10517 opts = opts->nd_next;
10518 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10520 opts->nd_next = opt;
10526 kwd_append(NODE *kwlist, NODE *kw)
10529 NODE *kws = kwlist;
10530 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10531 while (kws->nd_next) {
10532 kws = kws->nd_next;
10533 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10541 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10543 return NEW_DEFINED(remove_begin_all(expr), loc);
10547 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10549 enum node_type type = nd_type(symbol);
10552 nd_set_type(symbol, NODE_DSYM);
10555 nd_set_type(symbol, NODE_LIT);
10556 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10559 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10561 return list_append(p, symbols, symbol);
10565 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10571 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10572 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10575 switch (nd_type(node)) {
10578 VALUE src = node->nd_lit;
10579 nd_set_type(node, NODE_LIT);
10580 nd_set_loc(node, loc);
10581 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10586 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10587 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10590 nd_set_type(node, NODE_DREGX);
10591 nd_set_loc(node, loc);
10592 node->nd_cflag = options & RE_OPTION_MASK;
10593 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10594 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10595 NODE *frag = list->nd_head;
10596 enum node_type type = nd_type(frag);
10597 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10598 VALUE tail = frag->nd_lit;
10599 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10600 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10601 if (!literal_concat0(p, lit, tail)) {
10602 return NEW_NIL(loc); /* dummy node on error */
10604 rb_str_resize(tail, 0);
10605 prev->nd_next = list->nd_next;
10606 rb_discard_node(p, list->nd_head);
10607 rb_discard_node(p, list);
10618 if (!node->nd_next) {
10619 VALUE src = node->nd_lit;
10620 nd_set_type(node, NODE_LIT);
10621 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10623 if (options & RE_OPTION_ONCE) {
10624 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10632 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10635 return NEW_KW_ARG(0, (k), loc);
10639 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10642 VALUE lit = STR_NEW0();
10643 NODE *xstr = NEW_XSTR(lit, loc);
10644 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10647 switch (nd_type(node)) {
10649 nd_set_type(node, NODE_XSTR);
10650 nd_set_loc(node, loc);
10653 nd_set_type(node, NODE_DXSTR);
10654 nd_set_loc(node, loc);
10657 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10664 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10668 if (!arg || !p->case_labels) return;
10670 lit = rb_node_case_when_optimizable_literal(arg);
10671 if (lit == Qundef) return;
10672 if (nd_type_p(arg, NODE_STR)) {
10673 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10676 if (NIL_P(p->case_labels)) {
10677 p->case_labels = rb_obj_hide(rb_hash_new());
10680 VALUE line = rb_hash_lookup(p->case_labels, lit);
10681 if (!NIL_P(line)) {
10682 rb_warning1("duplicated `when' clause with line %d is ignored",
10687 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10690 #else /* !RIPPER */
10692 id_is_var(struct parser_params *p, ID id)
10694 if (is_notop_id(id)) {
10695 switch (id & ID_SCOPE_MASK) {
10696 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10699 if (dyna_in_block(p)) {
10700 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10702 if (local_id(p, id)) return 1;
10703 /* method call without arguments */
10707 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10712 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10714 VALUE src = 0, err;
10716 if (ripper_is_node_yylval(re)) {
10717 src = RNODE(re)->nd_cval;
10718 re = RNODE(re)->nd_rval;
10720 if (ripper_is_node_yylval(opt)) {
10721 options = (int)RNODE(opt)->nd_tag;
10722 opt = RNODE(opt)->nd_rval;
10724 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10725 compile_error(p, "%"PRIsVALUE, err);
10727 return dispatch2(regexp_literal, re, opt);
10729 #endif /* !RIPPER */
10731 static inline enum lex_state_e
10732 parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
10735 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
10737 return p->lex.state = ls;
10741 static const char rb_parser_lex_state_names[][8] = {
10742 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10743 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10744 "LABEL", "LABELED","FITEM",
10748 append_lex_state_name(enum lex_state_e state, VALUE buf)
10751 unsigned int mask = 1;
10752 static const char none[] = "NONE";
10754 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10755 if ((unsigned)state & mask) {
10757 rb_str_cat(buf, "|", 1);
10760 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10764 rb_str_cat(buf, none, sizeof(none)-1);
10770 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10772 VALUE mesg = p->debug_buffer;
10774 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10775 p->debug_buffer = Qnil;
10776 rb_io_puts(1, &mesg, out);
10778 if (!NIL_P(str) && RSTRING_LEN(str)) {
10779 rb_io_write(p->debug_output, str);
10784 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10785 enum lex_state_e to, int line)
10788 mesg = rb_str_new_cstr("lex_state: ");
10789 append_lex_state_name(from, mesg);
10790 rb_str_cat_cstr(mesg, " -> ");
10791 append_lex_state_name(to, mesg);
10792 rb_str_catf(mesg, " at line %d\n", line);
10793 flush_debug_buffer(p, p->debug_output, mesg);
10798 rb_parser_lex_state_name(enum lex_state_e state)
10800 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10804 append_bitstack_value(stack_type stack, VALUE mesg)
10807 rb_str_cat_cstr(mesg, "0");
10810 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10811 for (; mask && !(stack & mask); mask >>= 1) continue;
10812 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10817 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10818 const char *name, int line)
10820 VALUE mesg = rb_sprintf("%s: ", name);
10821 append_bitstack_value(stack, mesg);
10822 rb_str_catf(mesg, " at line %d\n", line);
10823 flush_debug_buffer(p, p->debug_output, mesg);
10827 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10830 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10833 rb_str_vcatf(mesg, fmt, ap);
10835 yyerror0(RSTRING_PTR(mesg));
10838 mesg = rb_str_new(0, 0);
10839 append_lex_state_name(p->lex.state, mesg);
10840 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10841 rb_str_resize(mesg, 0);
10842 append_bitstack_value(p->cond_stack, mesg);
10843 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10844 rb_str_resize(mesg, 0);
10845 append_bitstack_value(p->cmdarg_stack, mesg);
10846 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10847 if (p->debug_output == rb_ractor_stdout())
10848 p->debug_output = rb_ractor_stderr();
10853 rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
10855 yylloc->beg_pos.lineno = sourceline;
10856 yylloc->beg_pos.column = beg_pos;
10857 yylloc->end_pos.lineno = sourceline;
10858 yylloc->end_pos.column = end_pos;
10863 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10865 int sourceline = here->sourceline;
10866 int beg_pos = (int)here->offset - here->quote
10867 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10868 int end_pos = (int)here->offset + here->length + here->quote;
10870 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10874 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10876 int sourceline = p->ruby_sourceline;
10877 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10878 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
10879 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10883 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10885 int sourceline = p->ruby_sourceline;
10886 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10887 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
10888 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10890 #endif /* !RIPPER */
10893 assignable0(struct parser_params *p, ID id, const char **err)
10895 if (!id) return -1;
10898 *err = "Can't change the value of self";
10901 *err = "Can't assign to nil";
10904 *err = "Can't assign to true";
10906 case keyword_false:
10907 *err = "Can't assign to false";
10909 case keyword__FILE__:
10910 *err = "Can't assign to __FILE__";
10912 case keyword__LINE__:
10913 *err = "Can't assign to __LINE__";
10915 case keyword__ENCODING__:
10916 *err = "Can't assign to __ENCODING__";
10919 switch (id_type(id)) {
10921 if (dyna_in_block(p)) {
10922 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10923 compile_error(p, "Can't assign to numbered parameter _%d",
10924 NUMPARAM_ID_TO_IDX(id));
10927 if (dvar_curr(p, id)) return NODE_DASGN;
10928 if (dvar_defined(p, id)) return NODE_DASGN;
10929 if (local_id(p, id)) return NODE_LASGN;
10934 if (!local_id(p, id)) local_var(p, id);
10938 case ID_GLOBAL: return NODE_GASGN;
10939 case ID_INSTANCE: return NODE_IASGN;
10941 if (!p->ctxt.in_def) return NODE_CDECL;
10942 *err = "dynamic constant assignment";
10944 case ID_CLASS: return NODE_CVASGN;
10946 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10953 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10955 const char *err = 0;
10956 int node_type = assignable0(p, id, &err);
10957 switch (node_type) {
10958 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10959 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10960 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10961 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10962 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10963 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10965 if (err) yyerror1(loc, err);
10966 return NEW_BEGIN(0, loc);
10970 assignable(struct parser_params *p, VALUE lhs)
10972 const char *err = 0;
10973 assignable0(p, get_id(lhs), &err);
10974 if (err) lhs = assign_error(p, err, lhs);
10980 is_private_local_id(ID name)
10983 if (name == idUScore) return 1;
10984 if (!is_local_id(name)) return 0;
10985 s = rb_id2str(name);
10987 return RSTRING_PTR(s)[0] == '_';
10991 shadowing_lvar_0(struct parser_params *p, ID name)
10993 if (dyna_in_block(p)) {
10994 if (dvar_curr(p, name)) {
10995 if (is_private_local_id(name)) return 1;
10996 yyerror0("duplicated argument name");
10998 else if (dvar_defined(p, name) || local_id(p, name)) {
10999 vtable_add(p->lvtbl->vars, name);
11000 if (p->lvtbl->used) {
11001 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11007 if (local_id(p, name)) {
11008 if (is_private_local_id(name)) return 1;
11009 yyerror0("duplicated argument name");
11016 shadowing_lvar(struct parser_params *p, ID name)
11018 shadowing_lvar_0(p, name);
11023 new_bv(struct parser_params *p, ID name)
11026 if (!is_local_id(name)) {
11027 compile_error(p, "invalid local variable - %"PRIsVALUE,
11031 if (!shadowing_lvar_0(p, name)) return;
11037 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11039 return NEW_ATTRASGN(recv, tASET, idx, loc);
11043 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11045 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11046 compile_error(p, "both block arg and actual block given");
11051 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11053 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11054 return NEW_ATTRASGN(recv, id, 0, loc);
11058 rb_backref_error(struct parser_params *p, NODE *node)
11060 switch (nd_type(node)) {
11062 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11064 case NODE_BACK_REF:
11065 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11071 backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11073 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11074 rb_str_append(mesg, ref->nd_cval);
11075 return dispatch2(assign_error, mesg, expr);
11081 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11083 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11084 switch (nd_type(node1)) {
11086 return list_append(p, node1, node2);
11087 case NODE_BLOCK_PASS:
11088 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11089 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11091 case NODE_ARGSPUSH:
11092 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11093 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11094 nd_set_type(node1, NODE_ARGSCAT);
11097 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11098 node1->nd_body = list_append(p, node1->nd_body, node2);
11099 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11102 return NEW_ARGSPUSH(node1, node2, loc);
11106 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11108 if (!node2) return node1;
11109 switch (nd_type(node1)) {
11110 case NODE_BLOCK_PASS:
11111 if (node1->nd_head)
11112 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11114 node1->nd_head = NEW_LIST(node2, loc);
11116 case NODE_ARGSPUSH:
11117 if (!nd_type_p(node2, NODE_LIST)) break;
11118 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11119 nd_set_type(node1, NODE_ARGSCAT);
11122 if (!nd_type_p(node2, NODE_LIST) ||
11123 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11124 node1->nd_body = list_concat(node1->nd_body, node2);
11127 return NEW_ARGSCAT(node1, node2, loc);
11131 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11134 if ((n1 = splat_array(args)) != 0) {
11135 return list_append(p, n1, last_arg);
11137 return arg_append(p, args, last_arg, loc);
11141 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11144 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11145 return list_concat(n1, rest_arg);
11147 return arg_concat(p, args, rest_arg, loc);
11151 splat_array(NODE* node)
11153 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11154 if (nd_type_p(node, NODE_LIST)) return node;
11159 mark_lvar_used(struct parser_params *p, NODE *rhs)
11163 switch (nd_type(rhs)) {
11165 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11166 if (vidp) *vidp |= LVAR_USED;
11170 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11171 if (vidp) *vidp |= LVAR_USED;
11176 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11177 mark_lvar_used(p, rhs->nd_head);
11185 const_decl_path(struct parser_params *p, NODE **dest)
11188 if (!nd_type_p(n, NODE_CALL)) {
11189 const YYLTYPE *loc = &n->nd_loc;
11192 path = rb_id2str(n->nd_vid);
11196 path = rb_ary_new();
11197 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11198 rb_ary_push(path, rb_id2str(n->nd_mid));
11200 if (n && nd_type_p(n, NODE_CONST)) {
11202 rb_ary_push(path, rb_id2str(n->nd_vid));
11204 else if (n && nd_type_p(n, NODE_COLON3)) {
11206 rb_ary_push(path, rb_str_new(0, 0));
11209 // expression::Name
11210 rb_ary_push(path, rb_str_new_cstr("..."));
11212 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11213 path = rb_fstring(path);
11215 *dest = n = NEW_LIT(path, loc);
11216 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11221 extern VALUE rb_mRubyVMFrozenCore;
11224 make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11226 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11229 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11230 NEW_LIST(value, loc), loc);
11233 return NEW_CALL(fcore, rb_intern("make_shareable"),
11234 NEW_LIST(value, loc), loc);
11239 ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11241 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11242 NODE *args = NEW_LIST(value, loc);
11243 args = list_append(p, args, const_decl_path(p, dest));
11244 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11247 static int is_static_content(NODE *node);
11250 shareable_literal_value(NODE *node)
11252 if (!node) return Qnil;
11253 enum node_type type = nd_type(node);
11262 return node->nd_lit;
11268 #ifndef SHAREABLE_BARE_EXPRESSION
11269 #define SHAREABLE_BARE_EXPRESSION 1
11273 shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11274 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11276 # define shareable_literal_constant_next(n) \
11277 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11280 if (!value) return 0;
11281 enum node_type type = nd_type(value);
11290 if (shareable == shareable_literal) {
11291 value = NEW_CALL(value, idUMinus, 0, loc);
11296 lit = rb_fstring(value->nd_lit);
11297 nd_set_type(value, NODE_LIT);
11298 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11302 lit = rb_ary_new();
11303 OBJ_FREEZE_RAW(lit);
11304 NODE *n = NEW_LIT(lit, loc);
11305 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11309 lit = rb_ary_new();
11310 for (NODE *n = value; n; n = n->nd_next) {
11311 NODE *elt = n->nd_head;
11313 elt = shareable_literal_constant_next(elt);
11317 else if (RTEST(lit)) {
11323 VALUE e = shareable_literal_value(elt);
11325 rb_ary_push(lit, e);
11329 lit = Qnil; /* make shareable at runtime */
11336 if (!value->nd_brace) return 0;
11337 lit = rb_hash_new();
11338 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11339 NODE *key = n->nd_head;
11340 NODE *val = n->nd_next->nd_head;
11342 key = shareable_literal_constant_next(key);
11346 else if (RTEST(lit)) {
11347 rb_hash_clear(lit);
11352 val = shareable_literal_constant_next(val);
11354 n->nd_next->nd_head = val;
11356 else if (RTEST(lit)) {
11357 rb_hash_clear(lit);
11362 VALUE k = shareable_literal_value(key);
11363 VALUE v = shareable_literal_value(val);
11364 if (k != Qundef && v != Qundef) {
11365 rb_hash_aset(lit, k, v);
11368 rb_hash_clear(lit);
11369 lit = Qnil; /* make shareable at runtime */
11376 if (shareable == shareable_literal &&
11377 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11378 return ensure_shareable_node(p, dest, value, loc);
11383 /* Array or Hash */
11384 if (!lit) return 0;
11386 // if shareable_literal, all elements should have been ensured
11388 value = make_shareable_node(p, value, false, loc);
11391 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11392 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11396 # undef shareable_literal_constant_next
11400 shareable_constant_value(struct parser_params *p, enum shareability shareable,
11401 NODE *lhs, NODE *value, const YYLTYPE *loc)
11403 if (!value) return 0;
11404 switch (shareable) {
11405 case shareable_none:
11408 case shareable_literal:
11410 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11411 if (lit) return lit;
11416 case shareable_copy:
11417 case shareable_everything:
11419 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11420 if (lit) return lit;
11421 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11426 UNREACHABLE_RETURN(0);
11431 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11433 if (!lhs) return 0;
11435 switch (nd_type(lhs)) {
11437 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11446 lhs->nd_value = rhs;
11447 nd_set_loc(lhs, loc);
11450 case NODE_ATTRASGN:
11451 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11452 nd_set_loc(lhs, loc);
11456 /* should not happen */
11464 value_expr_check(struct parser_params *p, NODE *node)
11466 NODE *void_node = 0, *vn;
11469 rb_warning0("empty expression");
11472 switch (nd_type(node)) {
11478 return void_node ? void_node : node;
11481 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
11482 compile_error(p, "unexpected node");
11485 if (node->nd_body->nd_body) {
11488 /* single line pattern matching */
11489 return void_node ? void_node : node;
11492 while (node->nd_next) {
11493 node = node->nd_next;
11495 node = node->nd_head;
11499 node = node->nd_body;
11504 if (!node->nd_body) {
11507 else if (!node->nd_else) {
11510 vn = value_expr_check(p, node->nd_body);
11511 if (!vn) return NULL;
11512 if (!void_node) void_node = vn;
11513 node = node->nd_else;
11518 node = node->nd_1st;
11524 mark_lvar_used(p, node);
11536 value_expr_gen(struct parser_params *p, NODE *node)
11538 NODE *void_node = value_expr_check(p, node);
11540 yyerror1(&void_node->nd_loc, "void value expression");
11541 /* or "control never reach"? */
11547 void_expr(struct parser_params *p, NODE *node)
11549 const char *useless = 0;
11551 if (!RTEST(ruby_verbose)) return;
11553 if (!node || !(node = nd_once_body(node))) return;
11554 switch (nd_type(node)) {
11556 switch (node->nd_mid) {
11575 useless = rb_id2name(node->nd_mid);
11586 case NODE_BACK_REF:
11587 useless = "a variable";
11590 useless = "a constant";
11596 useless = "a literal";
11621 useless = "defined?";
11626 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11631 void_stmts(struct parser_params *p, NODE *node)
11633 NODE *const n = node;
11634 if (!RTEST(ruby_verbose)) return n;
11635 if (!node) return n;
11636 if (!nd_type_p(node, NODE_BLOCK)) return n;
11638 while (node->nd_next) {
11639 void_expr(p, node->nd_head);
11640 node = node->nd_next;
11646 remove_begin(NODE *node)
11648 NODE **n = &node, *n1 = node;
11649 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
11650 *n = n1 = n1->nd_body;
11656 remove_begin_all(NODE *node)
11658 NODE **n = &node, *n1 = node;
11659 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
11660 *n = n1 = n1->nd_body;
11666 reduce_nodes(struct parser_params *p, NODE **body)
11668 NODE *node = *body;
11671 *body = NEW_NIL(&NULL_LOC);
11674 #define subnodes(n1, n2) \
11675 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11676 (!node->n2) ? (body = &node->n1, 1) : \
11677 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11680 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11681 switch (nd_type(node)) {
11687 *body = node = node->nd_stts;
11688 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11691 *body = node = node->nd_body;
11692 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11695 body = &node->nd_end->nd_head;
11699 if (subnodes(nd_body, nd_else)) break;
11702 body = &node->nd_body;
11705 if (!subnodes(nd_body, nd_next)) goto end;
11708 if (!subnodes(nd_head, nd_resq)) goto end;
11711 if (node->nd_else) {
11712 body = &node->nd_resq;
11715 if (!subnodes(nd_head, nd_resq)) goto end;
11721 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11728 is_static_content(NODE *node)
11730 if (!node) return 1;
11731 switch (nd_type(node)) {
11733 if (!(node = node->nd_head)) break;
11736 if (!is_static_content(node->nd_head)) return 0;
11737 } while ((node = node->nd_next) != 0);
11752 assign_in_cond(struct parser_params *p, NODE *node)
11754 switch (nd_type(node)) {
11766 if (!node->nd_value) return 1;
11767 if (is_static_content(node->nd_value)) {
11768 /* reports always */
11769 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11780 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11782 case COND_IN_OP: break; \
11783 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11784 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11787 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11790 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11792 enum node_type type;
11794 if (node == 0) return 0;
11796 type = nd_type(node);
11798 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11799 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11800 ID lineno = rb_intern("$.");
11801 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11803 return cond0(p, node, COND_IN_FF, loc);
11807 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11809 if (node == 0) return 0;
11810 if (!(node = nd_once_body(node))) return 0;
11811 assign_in_cond(p, node);
11813 switch (nd_type(node)) {
11817 SWITCH_BY_COND_TYPE(type, warn, "string ")
11821 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11823 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11827 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11828 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11833 node->nd_beg = range_op(p, node->nd_beg, loc);
11834 node->nd_end = range_op(p, node->nd_end, loc);
11835 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
11836 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
11841 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
11845 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11846 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11847 nd_set_type(node, NODE_MATCH);
11849 else if (node->nd_lit == Qtrue ||
11850 node->nd_lit == Qfalse) {
11851 /* booleans are OK, e.g., while true */
11853 else if (SYMBOL_P(node->nd_lit)) {
11857 SWITCH_BY_COND_TYPE(type, warning, "")
11866 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11868 if (node == 0) return 0;
11869 return cond0(p, node, COND_IN_COND, loc);
11873 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11875 if (node == 0) return 0;
11876 return cond0(p, node, COND_IN_OP, loc);
11880 new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11882 YYLTYPE loc = {*pos, *pos};
11883 return NEW_NIL(&loc);
11887 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11889 if (!cc) return right;
11890 cc = cond0(p, cc, COND_IN_COND, loc);
11891 return newline_node(NEW_IF(cc, left, right, loc));
11895 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11897 if (!cc) return right;
11898 cc = cond0(p, cc, COND_IN_COND, loc);
11899 return newline_node(NEW_UNLESS(cc, left, right, loc));
11903 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11904 const YYLTYPE *op_loc, const YYLTYPE *loc)
11906 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11909 if (left && nd_type_p(left, type)) {
11910 NODE *node = left, *second;
11911 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
11914 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11915 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11916 left->nd_loc.end_pos = loc->end_pos;
11919 op = NEW_NODE(type, left, right, 0, loc);
11920 nd_set_line(op, op_loc->beg_pos.lineno);
11925 no_blockarg(struct parser_params *p, NODE *node)
11927 if (node && nd_type_p(node, NODE_BLOCK_PASS)) {
11928 compile_error(p, "block argument should not be given");
11933 ret_args(struct parser_params *p, NODE *node)
11936 no_blockarg(p, node);
11937 if (nd_type_p(node, NODE_LIST)) {
11938 if (node->nd_next == 0) {
11939 node = node->nd_head;
11942 nd_set_type(node, NODE_VALUES);
11950 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11952 if (node) no_blockarg(p, node);
11954 return NEW_YIELD(node, loc);
11958 negate_lit(struct parser_params *p, VALUE lit)
11960 if (FIXNUM_P(lit)) {
11961 return LONG2FIX(-FIX2LONG(lit));
11963 if (SPECIAL_CONST_P(lit)) {
11965 if (FLONUM_P(lit)) {
11966 return DBL2NUM(-RFLOAT_VALUE(lit));
11971 switch (BUILTIN_TYPE(lit)) {
11973 BIGNUM_NEGATE(lit);
11974 lit = rb_big_norm(lit);
11977 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11980 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11981 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11984 lit = DBL2NUM(-RFLOAT_VALUE(lit));
11988 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11989 rb_builtin_class_name(lit));
11996 arg_blk_pass(NODE *node1, NODE *node2)
11999 if (!node1) return node2;
12000 node2->nd_head = node1;
12001 nd_set_first_lineno(node2, nd_first_lineno(node1));
12002 nd_set_first_column(node2, nd_first_column(node1));
12009 args_info_empty_p(struct rb_args_info *args)
12011 if (args->pre_args_num) return false;
12012 if (args->post_args_num) return false;
12013 if (args->rest_arg) return false;
12014 if (args->opt_args) return false;
12015 if (args->block_arg) return false;
12016 if (args->kw_args) return false;
12017 if (args->kw_rest_arg) return false;
12022 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12024 int saved_line = p->ruby_sourceline;
12025 struct rb_args_info *args = tail->nd_ainfo;
12027 if (args->block_arg == idFWD_BLOCK) {
12029 yyerror1(&tail->nd_loc, "... after rest argument");
12032 rest_arg = idFWD_REST;
12035 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12036 args->pre_init = pre_args ? pre_args->nd_next : 0;
12038 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12039 args->post_init = post_args ? post_args->nd_next : 0;
12040 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12042 args->rest_arg = rest_arg;
12044 args->opt_args = opt_args;
12046 args->ruby2_keywords = rest_arg == idFWD_REST;
12048 p->ruby_sourceline = saved_line;
12049 nd_set_loc(tail, loc);
12055 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12057 int saved_line = p->ruby_sourceline;
12059 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12060 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12061 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12062 args->imemo = tmpbuf;
12063 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12064 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12065 if (p->error_p) return node;
12067 args->block_arg = block;
12068 args->kw_args = kw_args;
12072 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12073 * variable order: k1, kr1, k2, &b, internal_id, krest
12075 * variable order: kr1, k1, k2, internal_id, krest, &b
12077 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12078 struct vtable *vtargs = p->lvtbl->args;
12079 NODE *kwn = kw_args;
12081 if (block) block = vtargs->tbl[vtargs->pos-1];
12082 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12083 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12085 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12087 --required_kw_vars;
12088 kwn = kwn->nd_next;
12091 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12092 ID vid = kwn->nd_body->nd_vid;
12093 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12094 *required_kw_vars++ = vid;
12101 arg_var(p, kw_bits);
12102 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12103 if (block) arg_var(p, block);
12105 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12106 args->kw_rest_arg->nd_cflag = kw_bits;
12108 else if (kw_rest_arg == idNil) {
12109 args->no_kwarg = 1;
12111 else if (kw_rest_arg) {
12112 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12115 p->ruby_sourceline = saved_line;
12120 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12122 if (max_numparam > NO_PARAM) {
12124 YYLTYPE loc = RUBY_INIT_YYLLOC();
12125 args = new_args_tail(p, 0, 0, 0, 0);
12126 nd_set_loc(args, &loc);
12128 args->nd_ainfo->pre_args_num = max_numparam;
12134 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12136 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12138 aryptn->nd_pconst = constant;
12141 NODE *pre_args = NEW_LIST(pre_arg, loc);
12142 if (apinfo->pre_args) {
12143 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12146 apinfo->pre_args = pre_args;
12153 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12155 int saved_line = p->ruby_sourceline;
12157 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12158 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12159 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12160 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12161 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12163 apinfo->pre_args = pre_args;
12167 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12170 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12174 apinfo->rest_arg = NULL;
12177 apinfo->post_args = post_args;
12179 p->ruby_sourceline = saved_line;
12184 new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12186 fndptn->nd_pconst = constant;
12192 new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12194 int saved_line = p->ruby_sourceline;
12196 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12197 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12198 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12199 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12200 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12202 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12203 fpinfo->args = args;
12204 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12206 p->ruby_sourceline = saved_line;
12211 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12213 hshptn->nd_pconst = constant;
12218 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12220 int saved_line = p->ruby_sourceline;
12221 NODE *node, *kw_rest_arg_node;
12223 if (kw_rest_arg == idNil) {
12224 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12226 else if (kw_rest_arg) {
12227 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12230 kw_rest_arg_node = NULL;
12233 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12235 p->ruby_sourceline = saved_line;
12240 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12245 return NEW_LIT(ID2SYM(idNULL), loc);
12248 switch (nd_type(node)) {
12250 nd_set_type(node, NODE_DSYM);
12251 nd_set_loc(node, loc);
12254 lit = node->nd_lit;
12255 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12256 nd_set_type(node, NODE_LIT);
12257 nd_set_loc(node, loc);
12260 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12267 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12269 NODE *node = (NODE *)v;
12270 NODE **result = (NODE **)h;
12272 node->nd_next->nd_end = node->nd_next;
12273 node->nd_next->nd_next = 0;
12275 list_concat(*result, node);
12278 return ST_CONTINUE;
12282 hash_literal_key_p(VALUE k)
12284 switch (OBJ_BUILTIN_TYPE(k)) {
12293 literal_cmp(VALUE val, VALUE lit)
12295 if (val == lit) return 0;
12296 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12297 return rb_iseq_cdhash_cmp(val, lit);
12301 literal_hash(VALUE a)
12303 if (!hash_literal_key_p(a)) return (st_index_t)a;
12304 return rb_iseq_cdhash_hash(a);
12307 static const struct st_hash_type literal_type = {
12313 remove_duplicate_keys(struct parser_params *p, NODE *hash)
12315 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12317 NODE *last_expr = 0;
12318 rb_code_location_t loc = hash->nd_loc;
12319 while (hash && hash->nd_head && hash->nd_next) {
12320 NODE *head = hash->nd_head;
12321 NODE *value = hash->nd_next;
12322 NODE *next = value->nd_next;
12323 st_data_t key = (st_data_t)head;
12325 value->nd_next = 0;
12326 if (nd_type_p(head, NODE_LIT) &&
12327 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12328 NODE *dup_value = ((NODE *)data)->nd_next;
12329 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12330 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12331 head->nd_lit, nd_line(head));
12332 if (dup_value == last_expr) {
12333 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12336 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12339 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12340 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12343 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12344 st_free_table(literal_keys);
12346 if (!result) result = hash;
12347 else list_concat(result, hash);
12349 result->nd_loc = loc;
12354 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12356 if (hash) hash = remove_duplicate_keys(p, hash);
12357 return NEW_HASH(hash, loc);
12362 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12364 if (is_private_local_id(id)) {
12367 if (st_is_member(p->pvtbl, id)) {
12368 yyerror1(loc, "duplicated variable name");
12371 st_insert(p->pvtbl, (st_data_t)id, 0);
12376 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12379 p->pktbl = st_init_numtable();
12381 else if (st_is_member(p->pktbl, key)) {
12382 yyerror1(loc, "duplicated key name");
12385 st_insert(p->pktbl, (st_data_t)key, 0);
12390 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12392 return NEW_HASH(hash, loc);
12394 #endif /* !RIPPER */
12398 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12403 ID vid = lhs->nd_vid;
12404 YYLTYPE lhs_loc = lhs->nd_loc;
12405 int shareable = ctxt.shareable_constant_value;
12407 switch (nd_type(lhs)) {
12418 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12419 lhs->nd_value = rhs;
12420 nd_set_loc(lhs, loc);
12421 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12422 if (is_notop_id(vid)) {
12423 switch (id_type(vid)) {
12427 asgn->nd_aid = vid;
12431 else if (op == tANDOP) {
12433 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12435 lhs->nd_value = rhs;
12436 nd_set_loc(lhs, loc);
12437 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12441 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12443 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12445 asgn->nd_value = rhs;
12446 nd_set_loc(asgn, loc);
12450 asgn = NEW_BEGIN(0, loc);
12456 new_ary_op_assign(struct parser_params *p, NODE *ary,
12457 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12461 args = make_list(args, args_loc);
12462 if (nd_type_p(args, NODE_BLOCK_PASS)) {
12463 args = NEW_ARGSCAT(args, rhs, loc);
12466 args = arg_concat(p, args, rhs, loc);
12468 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12474 new_attr_op_assign(struct parser_params *p, NODE *lhs,
12475 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12479 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12485 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12490 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12491 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12494 asgn = NEW_BEGIN(0, loc);
12501 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12503 if (p->ctxt.in_def) {
12504 yyerror1(loc, "dynamic constant assignment");
12506 return NEW_CDECL(0, 0, (path), loc);
12510 const_decl(struct parser_params *p, VALUE path)
12512 if (p->ctxt.in_def) {
12513 path = assign_error(p, "dynamic constant assignment", path);
12519 assign_error(struct parser_params *p, const char *mesg, VALUE a)
12521 a = dispatch2(assign_error, ERR_MESG(), a);
12527 var_field(struct parser_params *p, VALUE a)
12529 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12535 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12537 NODE *result = head;
12539 NODE *tmp = rescue_else ? rescue_else : rescue;
12540 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12542 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12543 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12545 else if (rescue_else) {
12546 result = block_append(p, result, rescue_else);
12549 result = NEW_ENSURE(result, ensure, loc);
12551 fixpos(result, head);
12557 warn_unused_var(struct parser_params *p, struct local_vars *local)
12561 if (!local->used) return;
12562 cnt = local->used->pos;
12563 if (cnt != local->vars->pos) {
12564 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12567 ID *v = local->vars->tbl;
12568 ID *u = local->used->tbl;
12569 for (int i = 0; i < cnt; ++i) {
12570 if (!v[i] || (u[i] & LVAR_USED)) continue;
12571 if (is_private_local_id(v[i])) continue;
12572 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12578 local_push(struct parser_params *p, int toplevel_scope)
12580 struct local_vars *local;
12581 int inherits_dvars = toplevel_scope && compile_for_eval;
12582 int warn_unused_vars = RTEST(ruby_verbose);
12584 local = ALLOC(struct local_vars);
12585 local->prev = p->lvtbl;
12586 local->args = vtable_alloc(0);
12587 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12589 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12590 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12591 local->numparam.outer = 0;
12592 local->numparam.inner = 0;
12593 local->numparam.current = 0;
12595 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12597 # if WARN_PAST_SCOPE
12606 local_pop(struct parser_params *p)
12608 struct local_vars *local = p->lvtbl->prev;
12609 if (p->lvtbl->used) {
12610 warn_unused_var(p, p->lvtbl);
12611 vtable_free(p->lvtbl->used);
12613 # if WARN_PAST_SCOPE
12614 while (p->lvtbl->past) {
12615 struct vtable *past = p->lvtbl->past;
12616 p->lvtbl->past = past->prev;
12620 vtable_free(p->lvtbl->args);
12621 vtable_free(p->lvtbl->vars);
12624 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12629 static rb_ast_id_table_t *
12630 local_tbl(struct parser_params *p)
12632 int cnt_args = vtable_size(p->lvtbl->args);
12633 int cnt_vars = vtable_size(p->lvtbl->vars);
12634 int cnt = cnt_args + cnt_vars;
12636 rb_ast_id_table_t *tbl;
12638 if (cnt <= 0) return 0;
12639 tbl = rb_ast_new_local_table(p->ast, cnt);
12640 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
12641 /* remove IDs duplicated to warn shadowing */
12642 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
12643 ID id = p->lvtbl->vars->tbl[i];
12644 if (!vtable_included(p->lvtbl->args, id)) {
12645 tbl->ids[j++] = id;
12649 tbl = rb_ast_resize_latest_local_table(p->ast, j);
12656 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12658 rb_ast_id_table_t *a0;
12662 n = NEW_NODE(type, a0, a1, a2, loc);
12669 numparam_name(struct parser_params *p, ID id)
12671 if (!NUMPARAM_ID_P(id)) return;
12672 compile_error(p, "_%d is reserved for numbered parameter",
12673 NUMPARAM_ID_TO_IDX(id));
12677 arg_var(struct parser_params *p, ID id)
12679 numparam_name(p, id);
12680 vtable_add(p->lvtbl->args, id);
12684 local_var(struct parser_params *p, ID id)
12686 numparam_name(p, id);
12687 vtable_add(p->lvtbl->vars, id);
12688 if (p->lvtbl->used) {
12689 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12694 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12696 struct vtable *vars, *args, *used;
12698 vars = p->lvtbl->vars;
12699 args = p->lvtbl->args;
12700 used = p->lvtbl->used;
12702 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12705 if (used) used = used->prev;
12708 if (vars && vars->prev == DVARS_INHERIT) {
12709 return rb_local_defined(id, p->parent_iseq);
12711 else if (vtable_included(args, id)) {
12715 int i = vtable_included(vars, id);
12716 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12722 local_id(struct parser_params *p, ID id)
12724 return local_id_ref(p, id, NULL);
12728 check_forwarding_args(struct parser_params *p)
12730 if (local_id(p, idFWD_REST) &&
12732 local_id(p, idFWD_KWREST) &&
12734 local_id(p, idFWD_BLOCK)) return TRUE;
12735 compile_error(p, "unexpected ...");
12740 add_forwarding_args(struct parser_params *p)
12742 arg_var(p, idFWD_REST);
12744 arg_var(p, idFWD_KWREST);
12746 arg_var(p, idFWD_BLOCK);
12751 new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12753 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12755 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12757 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12758 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12760 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12762 return arg_blk_pass(args, block);
12767 numparam_push(struct parser_params *p)
12770 struct local_vars *local = p->lvtbl;
12771 NODE *inner = local->numparam.inner;
12772 if (!local->numparam.outer) {
12773 local->numparam.outer = local->numparam.current;
12775 local->numparam.inner = 0;
12776 local->numparam.current = 0;
12784 numparam_pop(struct parser_params *p, NODE *prev_inner)
12787 struct local_vars *local = p->lvtbl;
12789 /* prefer first one */
12790 local->numparam.inner = prev_inner;
12792 else if (local->numparam.current) {
12793 /* current and inner are exclusive */
12794 local->numparam.inner = local->numparam.current;
12796 if (p->max_numparam > NO_PARAM) {
12797 /* current and outer are exclusive */
12798 local->numparam.current = local->numparam.outer;
12799 local->numparam.outer = 0;
12802 /* no numbered parameter */
12803 local->numparam.current = 0;
12808 static const struct vtable *
12809 dyna_push(struct parser_params *p)
12811 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12812 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12813 if (p->lvtbl->used) {
12814 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12816 return p->lvtbl->args;
12820 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12822 struct vtable *tmp = *vtblp;
12823 *vtblp = tmp->prev;
12824 # if WARN_PAST_SCOPE
12825 if (p->past_scope_enabled) {
12826 tmp->prev = p->lvtbl->past;
12827 p->lvtbl->past = tmp;
12835 dyna_pop_1(struct parser_params *p)
12837 struct vtable *tmp;
12839 if ((tmp = p->lvtbl->used) != 0) {
12840 warn_unused_var(p, p->lvtbl);
12841 p->lvtbl->used = p->lvtbl->used->prev;
12844 dyna_pop_vtable(p, &p->lvtbl->args);
12845 dyna_pop_vtable(p, &p->lvtbl->vars);
12849 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12851 while (p->lvtbl->args != lvargs) {
12853 if (!p->lvtbl->args) {
12854 struct local_vars *local = p->lvtbl->prev;
12855 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12863 dyna_in_block(struct parser_params *p)
12865 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12869 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12871 struct vtable *vars, *args, *used;
12874 args = p->lvtbl->args;
12875 vars = p->lvtbl->vars;
12876 used = p->lvtbl->used;
12878 while (!DVARS_TERMINAL_P(vars)) {
12879 if (vtable_included(args, id)) {
12882 if ((i = vtable_included(vars, id)) != 0) {
12883 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12888 if (!vidrefp) used = 0;
12889 if (used) used = used->prev;
12892 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12893 return rb_dvar_defined(id, p->parent_iseq);
12900 dvar_defined(struct parser_params *p, ID id)
12902 return dvar_defined_ref(p, id, NULL);
12906 dvar_curr(struct parser_params *p, ID id)
12908 return (vtable_included(p->lvtbl->args, id) ||
12909 vtable_included(p->lvtbl->vars, id));
12913 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12916 "regexp encoding option '%c' differs from source encoding '%s'",
12917 c, rb_enc_name(rb_enc_get(str)));
12922 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12924 int c = RE_OPTION_ENCODING_IDX(options);
12928 rb_char_to_option_kcode(c, &opt, &idx);
12929 if (idx != ENCODING_GET(str) &&
12930 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12933 ENCODING_SET(str, idx);
12935 else if (RE_OPTION_ENCODING_NONE(options)) {
12936 if (!ENCODING_IS_ASCII8BIT(str) &&
12937 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12941 rb_enc_associate(str, rb_ascii8bit_encoding());
12943 else if (p->enc == rb_usascii_encoding()) {
12944 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12945 /* raise in re.c */
12946 rb_enc_associate(str, rb_usascii_encoding());
12949 rb_enc_associate(str, rb_ascii8bit_encoding());
12959 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12961 int c = rb_reg_fragment_setenc(p, str, options);
12962 if (c) reg_fragment_enc_error(p, str, c);
12966 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12969 reg_fragment_setenc(p, str, options);
12970 err = rb_reg_check_preprocess(str);
12972 err = rb_obj_as_string(err);
12973 compile_error(p, "%"PRIsVALUE, err);
12980 struct parser_params* parser;
12983 const YYLTYPE *loc;
12984 } reg_named_capture_assign_t;
12987 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12988 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12990 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12991 struct parser_params* p = arg->parser;
12992 rb_encoding *enc = arg->enc;
12993 long len = name_end - name;
12994 const char *s = (const char *)name;
12998 if (!len) return ST_CONTINUE;
12999 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13000 return ST_CONTINUE;
13002 var = intern_cstr(s, len, enc);
13003 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13004 if (!lvar_defined(p, var)) return ST_CONTINUE;
13006 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13007 succ = arg->succ_block;
13008 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13009 succ = block_append(p, succ, node);
13010 arg->succ_block = succ;
13011 return ST_CONTINUE;
13015 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13017 reg_named_capture_assign_t arg;
13020 arg.enc = rb_enc_get(regexp);
13021 arg.succ_block = 0;
13023 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13025 if (!arg.succ_block) return 0;
13026 return arg.succ_block->nd_next;
13030 parser_reg_compile(struct parser_params* p, VALUE str, int options)
13032 reg_fragment_setenc(p, str, options);
13033 return rb_parser_reg_compile(p, str, options);
13037 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13039 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13043 reg_compile(struct parser_params* p, VALUE str, int options)
13048 err = rb_errinfo();
13049 re = parser_reg_compile(p, str, options);
13051 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13052 rb_set_errinfo(err);
13053 compile_error(p, "%"PRIsVALUE, m);
13060 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13062 VALUE err = rb_errinfo();
13064 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13065 int c = rb_reg_fragment_setenc(p, str, options);
13066 if (c) reg_fragment_enc_error(p, str, c);
13067 re = rb_parser_reg_compile(p, str, options);
13069 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13070 rb_set_errinfo(err);
13078 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13080 struct parser_params *p;
13081 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13082 p->do_print = print;
13084 p->do_chomp = chomp;
13085 p->do_split = split;
13089 parser_append_options(struct parser_params *p, NODE *node)
13091 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13092 const YYLTYPE *const LOC = &default_location;
13095 NODE *print = NEW_FCALL(rb_intern("print"),
13096 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13098 node = block_append(p, node, print);
13103 ID ifs = rb_intern("$;");
13104 ID fields = rb_intern("$F");
13105 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13106 NODE *split = NEW_GASGN(fields,
13107 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13108 rb_intern("split"), args, LOC),
13110 node = block_append(p, split, node);
13113 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13114 rb_intern("chomp!"), 0, LOC);
13115 node = block_append(p, chomp, node);
13118 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
13125 rb_init_parse(void)
13127 /* just to suppress unused-function warnings */
13133 internal_id(struct parser_params *p)
13135 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13137 #endif /* !RIPPER */
13140 parser_initialize(struct parser_params *p)
13142 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13143 p->command_start = TRUE;
13144 p->ruby_sourcefile_string = Qnil;
13145 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13148 p->delayed.token = Qnil;
13150 p->parsing_thread = Qnil;
13152 p->error_buffer = Qfalse;
13154 p->debug_buffer = Qnil;
13155 p->debug_output = rb_ractor_stdout();
13156 p->enc = rb_utf8_encoding();
13160 #define parser_mark ripper_parser_mark
13161 #define parser_free ripper_parser_free
13165 parser_mark(void *ptr)
13167 struct parser_params *p = (struct parser_params*)ptr;
13169 rb_gc_mark(p->lex.input);
13170 rb_gc_mark(p->lex.prevline);
13171 rb_gc_mark(p->lex.lastline);
13172 rb_gc_mark(p->lex.nextline);
13173 rb_gc_mark(p->ruby_sourcefile_string);
13174 rb_gc_mark((VALUE)p->lex.strterm);
13175 rb_gc_mark((VALUE)p->ast);
13176 rb_gc_mark(p->case_labels);
13178 rb_gc_mark(p->debug_lines);
13179 rb_gc_mark(p->compile_option);
13180 rb_gc_mark(p->error_buffer);
13182 rb_gc_mark(p->delayed.token);
13183 rb_gc_mark(p->value);
13184 rb_gc_mark(p->result);
13185 rb_gc_mark(p->parsing_thread);
13187 rb_gc_mark(p->debug_buffer);
13188 rb_gc_mark(p->debug_output);
13190 rb_gc_mark((VALUE)p->heap);
13195 parser_free(void *ptr)
13197 struct parser_params *p = (struct parser_params*)ptr;
13198 struct local_vars *local, *prev;
13201 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13203 for (local = p->lvtbl; local; local = prev) {
13204 if (local->vars) xfree(local->vars);
13205 prev = local->prev;
13209 token_info *ptinfo;
13210 while ((ptinfo = p->token_info) != 0) {
13211 p->token_info = ptinfo->next;
13219 parser_memsize(const void *ptr)
13221 struct parser_params *p = (struct parser_params*)ptr;
13222 struct local_vars *local;
13223 size_t size = sizeof(*p);
13226 for (local = p->lvtbl; local; local = local->prev) {
13227 size += sizeof(*local);
13228 if (local->vars) size += local->vars->capa * sizeof(ID);
13233 static const rb_data_type_t parser_data_type = {
13244 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13248 #undef rb_reserved_word
13250 const struct kwtable *
13251 rb_reserved_word(const char *str, unsigned int len)
13253 return reserved_word(str, len);
13257 rb_parser_new(void)
13259 struct parser_params *p;
13260 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13261 &parser_data_type, p);
13262 parser_initialize(p);
13267 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13269 struct parser_params *p;
13271 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13272 p->error_buffer = main ? Qfalse : Qnil;
13273 p->parent_iseq = base;
13278 rb_parser_keep_script_lines(VALUE vparser)
13280 struct parser_params *p;
13282 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13283 p->keep_script_lines = 1;
13288 #define rb_parser_end_seen_p ripper_parser_end_seen_p
13289 #define rb_parser_encoding ripper_parser_encoding
13290 #define rb_parser_get_yydebug ripper_parser_get_yydebug
13291 #define rb_parser_set_yydebug ripper_parser_set_yydebug
13292 #define rb_parser_get_debug_output ripper_parser_get_debug_output
13293 #define rb_parser_set_debug_output ripper_parser_set_debug_output
13294 static VALUE ripper_parser_end_seen_p(VALUE vparser);
13295 static VALUE ripper_parser_encoding(VALUE vparser);
13296 static VALUE ripper_parser_get_yydebug(VALUE self);
13297 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13298 static VALUE ripper_parser_get_debug_output(VALUE self);
13299 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13303 * ripper.error? -> Boolean
13305 * Return true if parsed source has errors.
13308 ripper_error_p(VALUE vparser)
13310 struct parser_params *p;
13312 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13313 return RBOOL(p->error_p);
13319 * ripper.end_seen? -> Boolean
13321 * Return true if parsed source ended by +\_\_END\_\_+.
13324 rb_parser_end_seen_p(VALUE vparser)
13326 struct parser_params *p;
13328 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13329 return RBOOL(p->ruby__end__seen);
13334 * ripper.encoding -> encoding
13336 * Return encoding of the source.
13339 rb_parser_encoding(VALUE vparser)
13341 struct parser_params *p;
13343 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13344 return rb_enc_from_encoding(p->enc);
13350 * ripper.yydebug -> true or false
13355 rb_parser_get_yydebug(VALUE self)
13357 struct parser_params *p;
13359 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13360 return RBOOL(p->debug);
13366 * ripper.yydebug = flag
13371 rb_parser_set_yydebug(VALUE self, VALUE flag)
13373 struct parser_params *p;
13375 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13376 p->debug = RTEST(flag);
13382 * ripper.debug_output -> obj
13384 * Get debug output.
13387 rb_parser_get_debug_output(VALUE self)
13389 struct parser_params *p;
13391 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13392 return p->debug_output;
13397 * ripper.debug_output = obj
13399 * Set debug output.
13402 rb_parser_set_debug_output(VALUE self, VALUE output)
13404 struct parser_params *p;
13406 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13407 return p->debug_output = output;
13412 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13413 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13414 * potential memory leak */
13415 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13416 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13417 (new)->cnt = (cnt), (ptr))
13420 rb_parser_malloc(struct parser_params *p, size_t size)
13422 size_t cnt = HEAPCNT(1, size);
13423 rb_imemo_tmpbuf_t *n = NEWHEAP();
13424 void *ptr = xmalloc(size);
13426 return ADD2HEAP(n, cnt, ptr);
13430 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13432 size_t cnt = HEAPCNT(nelem, size);
13433 rb_imemo_tmpbuf_t *n = NEWHEAP();
13434 void *ptr = xcalloc(nelem, size);
13436 return ADD2HEAP(n, cnt, ptr);
13440 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13442 rb_imemo_tmpbuf_t *n;
13443 size_t cnt = HEAPCNT(1, size);
13445 if (ptr && (n = p->heap) != NULL) {
13447 if (n->ptr == ptr) {
13448 n->ptr = ptr = xrealloc(ptr, size);
13449 if (n->cnt) n->cnt = cnt;
13452 } while ((n = n->next) != NULL);
13455 ptr = xrealloc(ptr, size);
13456 return ADD2HEAP(n, cnt, ptr);
13460 rb_parser_free(struct parser_params *p, void *ptr)
13462 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13464 while ((n = *prev) != NULL) {
13465 if (n->ptr == ptr) {
13475 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13478 VALUE mesg = p->debug_buffer;
13480 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13482 rb_str_vcatf(mesg, fmt, ap);
13484 if (RSTRING_END(mesg)[-1] == '\n') {
13485 rb_io_write(p->debug_output, mesg);
13486 p->debug_buffer = Qnil;
13491 parser_compile_error(struct parser_params *p, const char *fmt, ...)
13495 rb_io_flush(p->debug_output);
13499 rb_syntax_error_append(p->error_buffer,
13500 p->ruby_sourcefile_string,
13501 p->ruby_sourceline,
13502 rb_long2int(p->lex.pcur - p->lex.pbeg),
13508 count_char(const char *str, int c)
13511 while (str[n] == c) ++n;
13516 * strip enclosing double-quotes, same as the default yytnamerr except
13517 * for that single-quotes matching back-quotes do not stop stripping.
13519 * "\"`class' keyword\"" => "`class' keyword"
13521 RUBY_FUNC_EXPORTED size_t
13522 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13524 if (*yystr == '"') {
13525 size_t yyn = 0, bquote = 0;
13526 const char *yyp = yystr;
13532 bquote = count_char(yyp+1, '`') + 1;
13533 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13541 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13542 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13548 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13549 if (yyres) memcpy(yyres + yyn, yyp, 3);
13554 goto do_not_strip_quotes;
13557 goto do_not_strip_quotes;
13560 if (*++yyp != '\\')
13561 goto do_not_strip_quotes;
13562 /* Fall through. */
13577 do_not_strip_quotes: ;
13580 if (!yyres) return strlen(yystr);
13582 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13587 #ifdef RIPPER_DEBUG
13590 ripper_validate_object(VALUE self, VALUE x)
13592 if (x == Qfalse) return x;
13593 if (x == Qtrue) return x;
13594 if (x == Qnil) return x;
13596 rb_raise(rb_eArgError, "Qundef given");
13597 if (FIXNUM_P(x)) return x;
13598 if (SYMBOL_P(x)) return x;
13599 switch (BUILTIN_TYPE(x)) {
13609 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
13610 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13612 x = ((NODE *)x)->nd_rval;
13615 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13616 (void *)x, rb_obj_classname(x));
13618 if (!RBASIC_CLASS(x)) {
13619 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13620 (void *)x, rb_builtin_type_name(TYPE(x)));
13626 #define validate(x) ((x) = get_value(x))
13629 ripper_dispatch0(struct parser_params *p, ID mid)
13631 return rb_funcall(p->value, mid, 0);
13635 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13638 return rb_funcall(p->value, mid, 1, a);
13642 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13646 return rb_funcall(p->value, mid, 2, a, b);
13650 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13655 return rb_funcall(p->value, mid, 3, a, b, c);
13659 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13665 return rb_funcall(p->value, mid, 4, a, b, c, d);
13669 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13676 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13680 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13689 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13693 ripper_get_id(VALUE v)
13696 if (!RB_TYPE_P(v, T_NODE)) return 0;
13698 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
13703 ripper_get_value(VALUE v)
13706 if (v == Qundef) return Qnil;
13707 if (!RB_TYPE_P(v, T_NODE)) return v;
13709 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
13710 return nd->nd_rval;
13714 ripper_error(struct parser_params *p)
13720 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13725 va_start(args, fmt);
13726 str = rb_vsprintf(fmt, args);
13728 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13733 ripper_lex_get_generic(struct parser_params *p, VALUE src)
13735 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13736 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13737 rb_raise(rb_eTypeError,
13738 "gets returned %"PRIsVALUE" (expected String or nil)",
13739 rb_obj_class(line));
13745 ripper_lex_io_get(struct parser_params *p, VALUE src)
13747 return rb_io_gets(src);
13751 ripper_s_allocate(VALUE klass)
13753 struct parser_params *p;
13754 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13755 &parser_data_type, p);
13760 #define ripper_initialized_p(r) ((r)->lex.input != 0)
13764 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13766 * Create a new Ripper object.
13767 * _src_ must be a String, an IO, or an Object which has #gets method.
13769 * This method does not starts parsing.
13770 * See also Ripper#parse and Ripper.parse.
13773 ripper_initialize(int argc, VALUE *argv, VALUE self)
13775 struct parser_params *p;
13776 VALUE src, fname, lineno;
13778 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13779 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13780 if (RB_TYPE_P(src, T_FILE)) {
13781 p->lex.gets = ripper_lex_io_get;
13783 else if (rb_respond_to(src, id_gets)) {
13784 p->lex.gets = ripper_lex_get_generic;
13788 p->lex.gets = lex_get_str;
13790 p->lex.input = src;
13792 if (NIL_P(fname)) {
13793 fname = STR_NEW2("(ripper)");
13797 StringValueCStr(fname);
13798 fname = rb_str_new_frozen(fname);
13800 parser_initialize(p);
13802 p->ruby_sourcefile_string = fname;
13803 p->ruby_sourcefile = RSTRING_PTR(fname);
13804 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13810 ripper_parse0(VALUE parser_v)
13812 struct parser_params *p;
13814 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13816 p->ast = rb_ast_new();
13817 ripper_yyparse((void*)p);
13818 rb_ast_dispose(p->ast);
13824 ripper_ensure(VALUE parser_v)
13826 struct parser_params *p;
13828 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13829 p->parsing_thread = Qnil;
13837 * Start parsing and returns the value of the root action.
13840 ripper_parse(VALUE self)
13842 struct parser_params *p;
13844 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13845 if (!ripper_initialized_p(p)) {
13846 rb_raise(rb_eArgError, "method called for uninitialized object");
13848 if (!NIL_P(p->parsing_thread)) {
13849 if (p->parsing_thread == rb_thread_current())
13850 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13852 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13854 p->parsing_thread = rb_thread_current();
13855 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13862 * ripper.column -> Integer
13864 * Return column number of current parsing line.
13865 * This number starts from 0.
13868 ripper_column(VALUE self)
13870 struct parser_params *p;
13873 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13874 if (!ripper_initialized_p(p)) {
13875 rb_raise(rb_eArgError, "method called for uninitialized object");
13877 if (NIL_P(p->parsing_thread)) return Qnil;
13878 col = p->lex.ptok - p->lex.pbeg;
13879 return LONG2NUM(col);
13884 * ripper.filename -> String
13886 * Return current parsing filename.
13889 ripper_filename(VALUE self)
13891 struct parser_params *p;
13893 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13894 if (!ripper_initialized_p(p)) {
13895 rb_raise(rb_eArgError, "method called for uninitialized object");
13897 return p->ruby_sourcefile_string;
13902 * ripper.lineno -> Integer
13904 * Return line number of current parsing line.
13905 * This number starts from 1.
13908 ripper_lineno(VALUE self)
13910 struct parser_params *p;
13912 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13913 if (!ripper_initialized_p(p)) {
13914 rb_raise(rb_eArgError, "method called for uninitialized object");
13916 if (NIL_P(p->parsing_thread)) return Qnil;
13917 return INT2NUM(p->ruby_sourceline);
13922 * ripper.state -> Integer
13924 * Return scanner state of current token.
13927 ripper_state(VALUE self)
13929 struct parser_params *p;
13931 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13932 if (!ripper_initialized_p(p)) {
13933 rb_raise(rb_eArgError, "method called for uninitialized object");
13935 if (NIL_P(p->parsing_thread)) return Qnil;
13936 return INT2NUM(p->lex.state);
13941 * ripper.token -> String
13943 * Return the current token string.
13946 ripper_token(VALUE self)
13948 struct parser_params *p;
13951 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13952 if (!ripper_initialized_p(p)) {
13953 rb_raise(rb_eArgError, "method called for uninitialized object");
13955 if (NIL_P(p->parsing_thread)) return Qnil;
13956 pos = p->lex.ptok - p->lex.pbeg;
13957 len = p->lex.pcur - p->lex.ptok;
13958 return rb_str_subseq(p->lex.lastline, pos, len);
13961 #ifdef RIPPER_DEBUG
13964 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13967 if (obj == Qundef) {
13968 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13975 ripper_value(VALUE self, VALUE obj)
13977 return ULONG2NUM(obj);
13983 * Ripper.lex_state_name(integer) -> string
13985 * Returns a string representation of lex_state.
13988 ripper_lex_state_name(VALUE self, VALUE state)
13990 return rb_parser_lex_state_name(NUM2INT(state));
13996 ripper_init_eventids1();
13997 ripper_init_eventids2();
13998 id_warn = rb_intern_const("warn");
13999 id_warning = rb_intern_const("warning");
14000 id_gets = rb_intern_const("gets");
14001 id_assoc = rb_intern_const("=>");
14003 (void)yystpcpy; /* may not used in newer bison */
14009 InitVM_ripper(void)
14013 Ripper = rb_define_class("Ripper", rb_cObject);
14014 /* version of Ripper */
14015 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14016 rb_define_alloc_func(Ripper, ripper_s_allocate);
14017 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14018 rb_define_method(Ripper, "parse", ripper_parse, 0);
14019 rb_define_method(Ripper, "column", ripper_column, 0);
14020 rb_define_method(Ripper, "filename", ripper_filename, 0);
14021 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14022 rb_define_method(Ripper, "state", ripper_state, 0);
14023 rb_define_method(Ripper, "token", ripper_token, 0);
14024 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14025 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14026 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14027 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14028 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14029 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14030 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14031 #ifdef RIPPER_DEBUG
14032 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14033 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14034 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14037 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14038 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14040 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14042 <% @exprs.each do |expr, desc| -%>
14044 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14046 ripper_init_eventids1_table(Ripper);
14047 ripper_init_eventids2_table(Ripper);
14050 /* Hack to let RDoc document SCRIPT_LINES__ */
14053 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14054 * after the assignment will be added as an Array of lines with the file
14057 rb_define_global_const("SCRIPT_LINES__", Qnil);
14061 #endif /* RIPPER */
14066 * c-file-style: "ruby"