[ruby/win32ole] Undefine allocator of WIN32OLE_VARIABLE to get rid of warning
[ruby-80x24.org.git] / parse.y
blob7555d0db1662d4b7c3dee64fc58493dd23ec54b0
1 /**********************************************************************
3 parse.y -
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
14 #if !YYPURE
15 # error needs pure parser
16 #endif
17 #define YYDEBUG 1
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"
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdio.h>
29 struct lex_context;
31 #include "internal.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"
46 #include "node.h"
47 #include "probes.h"
48 #include "regenc.h"
49 #include "ruby/encoding.h"
50 #include "ruby/regex.h"
51 #include "ruby/ruby.h"
52 #include "ruby/st.h"
53 #include "ruby/util.h"
54 #include "ruby/ractor.h"
55 #include "symbol.h"
57 enum shareability {
58 shareable_none,
59 shareable_literal,
60 shareable_copy,
61 shareable_everything,
64 struct lex_context {
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);
73 #include "parse.h"
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
81 #endif
83 #define TAB_WIDTH 8
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) \
97 do \
98 if (N) \
99 { \
100 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
101 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
103 else \
105 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
106 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
108 while (0)
109 #define YY_(Msgid) \
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. */
139 EXPR_MAX_STATE
141 /* examine combinations */
142 enum lex_state_e {
143 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
144 DEF_EXPR(BEG),
145 DEF_EXPR(END),
146 DEF_EXPR(ENDARG),
147 DEF_EXPR(ENDFN),
148 DEF_EXPR(ARG),
149 DEF_EXPR(CMDARG),
150 DEF_EXPR(MID),
151 DEF_EXPR(FNAME),
152 DEF_EXPR(DOT),
153 DEF_EXPR(CLASS),
154 DEF_EXPR(LABEL),
155 DEF_EXPR(LABELED),
156 DEF_EXPR(FITEM),
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),
161 EXPR_NONE = 0
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))
196 struct vtable {
197 ID *tbl;
198 int pos;
199 int capa;
200 struct vtable *prev;
203 struct local_vars {
204 struct vtable *args;
205 struct vtable *vars;
206 struct vtable *used;
207 # if WARN_PAST_SCOPE
208 struct vtable *past;
209 # endif
210 struct local_vars *prev;
211 # ifndef RIPPER
212 struct {
213 NODE *outer, *inner, *current;
214 } numparam;
215 # endif
218 enum {
219 ORDINAL_PARAM = -1,
220 NO_PARAM = 0,
221 NUMPARAM_MAX = 9,
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))
227 static int
228 numparam_id_p(ID id)
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 {
241 const char *token;
242 rb_code_position_t beg;
243 int indent;
244 int nonspc;
245 struct token_info *next;
246 } token_info;
248 typedef struct rb_strterm_struct rb_strterm_t;
251 Structure of Lexer Buffer:
253 lex.pbeg lex.ptok lex.pcur lex.pend
254 | | | |
255 |------------+------------+------------|
256 |<---------->|
257 token
259 struct parser_params {
260 rb_imemo_tmpbuf_t *heap;
262 YYSTYPE *lval;
264 struct {
265 rb_strterm_t *strterm;
266 VALUE (*gets)(struct parser_params*,VALUE);
267 VALUE input;
268 VALUE prevline;
269 VALUE lastline;
270 VALUE nextline;
271 const char *pbeg;
272 const char *pcur;
273 const char *pend;
274 const char *ptok;
275 union {
276 long ptr;
277 VALUE (*call)(VALUE, int);
278 } gets_;
279 enum lex_state_e state;
280 /* track the nest level of any parens "()[]{}" */
281 int paren_nest;
282 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
283 int lpar_beg;
284 /* track the nest level of only braces "{}" */
285 int brace_nest;
286 } lex;
287 stack_type cond_stack;
288 stack_type cmdarg_stack;
289 int tokidx;
290 int toksiz;
291 int tokline;
292 int heredoc_end;
293 int heredoc_indent;
294 int heredoc_line_indent;
295 char *tokenbuf;
296 struct local_vars *lvtbl;
297 st_table *pvtbl;
298 st_table *pktbl;
299 int line_count;
300 int ruby_sourceline; /* current line no. */
301 const char *ruby_sourcefile; /* current source file */
302 VALUE ruby_sourcefile_string;
303 rb_encoding *enc;
304 token_info *token_info;
305 VALUE case_labels;
306 VALUE compile_option;
308 VALUE debug_buffer;
309 VALUE debug_output;
311 ID cur_arg;
313 rb_ast_t *ast;
314 int node_id;
316 int max_numparam;
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;
327 # if WARN_PAST_SCOPE
328 unsigned int past_scope_enabled: 1;
329 # endif
330 unsigned int error_p: 1;
331 unsigned int cr_seen: 1;
333 #ifndef RIPPER
334 /* Ruby core only */
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;
343 NODE *eval_tree;
344 VALUE error_buffer;
345 VALUE debug_lines;
346 const struct rb_iseq_struct *parent_iseq;
347 #else
348 /* Ripper only */
350 struct {
351 VALUE token;
352 int line;
353 int col;
354 } delayed;
356 VALUE value;
357 VALUE result;
358 VALUE parsing_thread;
359 #endif
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)
370 static st_table *
371 push_pvtbl(struct parser_params *p)
373 st_table *tbl = p->pvtbl;
374 p->pvtbl = st_init_numtable();
375 return tbl;
378 static void
379 pop_pvtbl(struct parser_params *p, st_table *tbl)
381 st_free_table(p->pvtbl);
382 p->pvtbl = tbl;
385 static st_table *
386 push_pktbl(struct parser_params *p)
388 st_table *tbl = p->pktbl;
389 p->pktbl = 0;
390 return tbl;
393 static void
394 pop_pktbl(struct parser_params *p, st_table *tbl)
396 if (p->pktbl) st_free_table(p->pktbl);
397 p->pktbl = tbl;
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);
415 #ifdef RIPPER
416 #define compile_for_eval (0)
417 #else
418 #define compile_for_eval (p->parent_iseq != 0)
419 #endif
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*);
433 #ifndef RIPPER
434 static inline void
435 rb_discard_node(struct parser_params *p, NODE *n)
437 rb_ast_delete_node(p->ast, n);
439 #endif
441 #ifdef RIPPER
442 static inline VALUE
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);
450 return obj;
452 #else
453 static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
454 #endif
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);
461 static int
462 parser_get_node_id(struct parser_params *p)
464 int node_id = p->node_id;
465 p->node_id++;
466 return node_id;
469 #ifndef RIPPER
470 static inline void
471 set_line_body(NODE *body, int line)
473 if (!body) return;
474 switch (nd_type(body)) {
475 case NODE_RESCUE:
476 case NODE_ENSURE:
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)
591 #else /* RIPPER */
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);
597 static inline VALUE
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);
606 static inline int
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);
643 #endif /* !RIPPER */
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);
662 #ifndef RIPPER
663 static ID formal_argument(struct parser_params*, ID);
664 #else
665 static ID formal_argument(struct parser_params*, VALUE);
666 #endif
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 **);
676 #ifndef RIPPER
677 static ID internal_id(struct parser_params*);
678 static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
679 #endif
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);
696 #ifdef RIPPER
697 # define METHOD_NOT idNOT
698 #else
699 # define METHOD_NOT '!'
700 #endif
702 #define idFWD_REST '*'
703 #ifdef RUBY3_KEYWORDS
704 #define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
705 #else
706 #define idFWD_KWREST 0
707 #endif
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 {
720 union {
721 VALUE dummy;
722 long nest;
723 } u0;
724 union {
725 VALUE dummy;
726 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
727 } u1;
728 union {
729 VALUE dummy;
730 long paren; /* '(' of `%q(...)` */
731 } u2;
732 union {
733 VALUE dummy;
734 long term; /* ')' of `%q(...)` */
735 } u3;
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)
748 #else
749 # define HERETERM_LENGTH_MAX UINT_MAX
750 #endif
752 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
753 unsigned quote: 1;
754 unsigned func: 8;
755 #else
756 uint8_t quote;
757 uint8_t func;
758 #endif
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 {
765 VALUE flags;
766 union {
767 rb_strterm_literal_t literal;
768 rb_strterm_heredoc_t heredoc;
769 } u;
772 #ifndef RIPPER
773 void
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);
782 #endif
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 *******/
798 #ifdef 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))
834 static inline VALUE
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));
842 static inline VALUE
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);
849 return (VALUE)t;
852 static inline VALUE
853 args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
855 return args;
858 static VALUE
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);
868 else {
869 pre_args = rb_ary_new_from_args(1, pre_arg);
872 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
875 static VALUE
876 new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
878 NODE *t;
880 if (has_rest) {
881 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
883 else {
884 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);
891 return (VALUE)t;
894 static VALUE
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);
903 static VALUE
904 new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
906 NODE *t;
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);
915 return (VALUE)t;
918 #define new_hash(p,h,l) rb_ary_new_from_args(0)
920 static VALUE
921 new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
923 return ary;
926 static VALUE
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);
934 static VALUE
935 new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
937 NODE *t;
938 if (kw_rest_arg) {
939 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
941 else {
942 kw_rest_arg = Qnil;
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);
948 return (VALUE)t;
951 #define new_defined(p,expr,loc) dispatch1(defined, (expr))
953 static VALUE heredoc_dedent(struct parser_params*,VALUE);
955 #else
956 #define ID2VAL(id) (id)
957 #define TOKEN2VAL(t) ID2VAL(t)
958 #define KWD2EID(t, v) keyword_##t
960 static NODE *
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);
966 n->nd_loc = *loc;
967 nd_set_line(n->nd_defn, loc->end_pos.lineno);
968 set_line_body(body, loc->beg_pos.lineno);
969 return n;
972 static NODE *
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);
982 #endif /* RIPPER */
984 static void
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;
993 static void
994 endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
996 #ifdef RIPPER
997 defn = defn->nd_defn;
998 #endif
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);
1006 #ifndef RIPPER
1007 # define Qnone 0
1008 # define Qnull 0
1009 # define ifndef_ripper(x) (x)
1010 #else
1011 # define Qnone Qnil
1012 # define Qnull Qundef
1013 # define ifndef_ripper(x)
1014 #endif
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))
1036 #ifdef RIPPER
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__)
1050 # else
1051 # define WARN_CALL rb_funcall
1052 # endif
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__)
1057 # else
1058 # define WARNING_CALL rb_funcall
1059 # endif
1060 PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1061 # define compile_error ripper_compile_error
1062 #else
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
1078 #endif
1080 #define WARN_EOL(tok) \
1081 (looking_at_eol_p(p) ? \
1082 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1083 (void)0)
1084 static int looking_at_eol_p(struct parser_params *p);
1087 %expect 0
1088 %define api.pure
1089 %define parse.error verbose
1090 %printer {
1091 #ifndef RIPPER
1092 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1093 #else
1094 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1095 #endif
1096 } tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1097 %printer {
1098 #ifndef RIPPER
1099 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1100 #else
1101 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1102 #endif
1103 } tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1104 %printer {
1105 #ifndef RIPPER
1106 rb_parser_printf(p, "$%ld", $$->nd_nth);
1107 #else
1108 rb_parser_printf(p, "%"PRIsVALUE, $$);
1109 #endif
1110 } tNTH_REF
1111 %printer {
1112 #ifndef RIPPER
1113 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1114 #else
1115 rb_parser_printf(p, "%"PRIsVALUE, $$);
1116 #endif
1117 } tBACK_REF
1119 %lex-param {struct parser_params *p}
1120 %parse-param {struct parser_params *p}
1121 %initial-action
1123 RUBY_SET_YYLLOC_OF_NONE(@$);
1126 %union {
1127 VALUE val;
1128 NODE *node;
1129 ID id;
1130 int num;
1131 st_table *tbl;
1132 const struct vtable *vars;
1133 struct rb_strterm_struct *strterm;
1134 struct lex_context ctxt;
1137 %token <id>
1138 keyword_class "`class'"
1139 keyword_module "`module'"
1140 keyword_def "`def'"
1141 keyword_undef "`undef'"
1142 keyword_begin "`begin'"
1143 keyword_rescue "`rescue'"
1144 keyword_ensure "`ensure'"
1145 keyword_end "`end'"
1146 keyword_if "`if'"
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'"
1155 keyword_for "`for'"
1156 keyword_break "`break'"
1157 keyword_next "`next'"
1158 keyword_redo "`redo'"
1159 keyword_retry "`retry'"
1160 keyword_in "`in'"
1161 keyword_do "`do'"
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'"
1169 keyword_nil "`nil'"
1170 keyword_true "`true'"
1171 keyword_false "`false'"
1172 keyword_and "`and'"
1173 keyword_or "`or'"
1174 keyword_not "`not'"
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'"
1183 keyword_END "`END'"
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"
1240 %token <id> '.'
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. */
1273 %token tASSOC "=>"
1274 %token tLPAREN "("
1275 %token tLPAREN_ARG "( arg"
1276 %token tRPAREN ")"
1277 %token tLBRACK "["
1278 %token tLBRACE "{"
1279 %token tLBRACE_ARG "{ arg"
1280 %token tSTAR "*"
1281 %token tDSTAR "**arg"
1282 %token tAMPER "&"
1283 %token tLAMBDA "->"
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
1297 * precedence table
1300 %nonassoc tLOWEST
1301 %nonassoc tLBRACE_ARG
1303 %nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1304 %left keyword_or keyword_and
1305 %right keyword_not
1306 %nonassoc keyword_defined
1307 %right '=' tOP_ASGN
1308 %left modifier_rescue
1309 %right '?' ':'
1310 %nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1311 %left tOROP
1312 %left tANDOP
1313 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1314 %left '>' tGEQ '<' tLEQ
1315 %left '|' '^'
1316 %left '&'
1317 %left tLSHFT tRSHFT
1318 %left '+' '-'
1319 %left '*' '/' '%'
1320 %right tUMINUS_NUM tUMINUS
1321 %right tPOW
1322 %right '!' '~' tUPLUS
1324 %token tLAST_TOKEN
1327 program : {
1328 SET_LEX_STATE(EXPR_BEG);
1329 local_push(p, ifndef_ripper(1)+0);
1331 top_compstmt
1333 /*%%%*/
1334 if ($2 && !compile_for_eval) {
1335 NODE *node = $2;
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);
1344 void_expr(p, node);
1346 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1347 /*% %*/
1348 /*% ripper[final]: program!($2) %*/
1349 local_pop(p);
1353 top_compstmt : top_stmts opt_terms
1355 $$ = void_stmts(p, $1);
1359 top_stmts : none
1361 /*%%%*/
1362 $$ = NEW_BEGIN(0, &@$);
1363 /*% %*/
1364 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1366 | top_stmt
1368 /*%%%*/
1369 $$ = newline_node($1);
1370 /*% %*/
1371 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1373 | top_stmts terms top_stmt
1375 /*%%%*/
1376 $$ = block_append(p, $1, newline_node($3));
1377 /*% %*/
1378 /*% ripper: stmts_add!($1, $3) %*/
1380 | error top_stmt
1382 $$ = remove_begin($2);
1386 top_stmt : stmt
1387 | keyword_BEGIN begin_block
1389 $$ = $2;
1393 begin_block : '{' top_compstmt '}'
1395 /*%%%*/
1396 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1397 NEW_BEGIN($2, &@$));
1398 $$ = NEW_BEGIN(0, &@$);
1399 /*% %*/
1400 /*% ripper: BEGIN!($2) %*/
1404 bodystmt : compstmt
1405 opt_rescue
1406 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1407 compstmt
1408 opt_ensure
1410 /*%%%*/
1411 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1412 /*% %*/
1413 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1415 | compstmt
1416 opt_rescue
1417 opt_ensure
1419 /*%%%*/
1420 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1421 /*% %*/
1422 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1426 compstmt : stmts opt_terms
1428 $$ = void_stmts(p, $1);
1432 stmts : none
1434 /*%%%*/
1435 $$ = NEW_BEGIN(0, &@$);
1436 /*% %*/
1437 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1439 | stmt_or_begin
1441 /*%%%*/
1442 $$ = newline_node($1);
1443 /*% %*/
1444 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1446 | stmts terms stmt_or_begin
1448 /*%%%*/
1449 $$ = block_append(p, $1, newline_node($3));
1450 /*% %*/
1451 /*% ripper: stmts_add!($1, $3) %*/
1453 | error stmt
1455 $$ = remove_begin($2);
1459 stmt_or_begin : stmt
1461 $$ = $1;
1463 | keyword_BEGIN
1465 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1467 begin_block
1469 $$ = $3;
1473 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1475 /*%%%*/
1476 $$ = NEW_ALIAS($2, $4, &@$);
1477 /*% %*/
1478 /*% ripper: alias!($2, $4) %*/
1480 | keyword_alias tGVAR tGVAR
1482 /*%%%*/
1483 $$ = NEW_VALIAS($2, $3, &@$);
1484 /*% %*/
1485 /*% ripper: var_alias!($2, $3) %*/
1487 | keyword_alias tGVAR tBACK_REF
1489 /*%%%*/
1490 char buf[2];
1491 buf[0] = '$';
1492 buf[1] = (char)$3->nd_nth;
1493 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1494 /*% %*/
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";
1500 /*%%%*/
1501 yyerror1(&@3, mesg);
1502 $$ = NEW_BEGIN(0, &@$);
1503 /*% %*/
1504 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1506 | keyword_undef undef_list
1508 /*%%%*/
1509 $$ = $2;
1510 /*% %*/
1511 /*% ripper: undef!($2) %*/
1513 | stmt modifier_if expr_value
1515 /*%%%*/
1516 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1517 fixpos($$, $3);
1518 /*% %*/
1519 /*% ripper: if_mod!($3, $1) %*/
1521 | stmt modifier_unless expr_value
1523 /*%%%*/
1524 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1525 fixpos($$, $3);
1526 /*% %*/
1527 /*% ripper: unless_mod!($3, $1) %*/
1529 | stmt modifier_while expr_value
1531 /*%%%*/
1532 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1533 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1535 else {
1536 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1538 /*% %*/
1539 /*% ripper: while_mod!($3, $1) %*/
1541 | stmt modifier_until expr_value
1543 /*%%%*/
1544 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1545 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1547 else {
1548 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1550 /*% %*/
1551 /*% ripper: until_mod!($3, $1) %*/
1553 | stmt modifier_rescue stmt
1555 /*%%%*/
1556 NODE *resq;
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, &@$);
1560 /*% %*/
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");
1568 /*%%%*/
1570 NODE *scope = NEW_NODE(
1571 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1572 $$ = NEW_POSTEXE(scope, &@$);
1574 /*% %*/
1575 /*% ripper: END!($3) %*/
1577 | command_asgn
1578 | mlhs '=' lex_ctxt command_call
1580 /*%%%*/
1581 value_expr($4);
1582 $$ = node_assign(p, $1, $4, $3, &@$);
1583 /*% %*/
1584 /*% ripper: massign!($1, $4) %*/
1586 | lhs '=' lex_ctxt mrhs
1588 /*%%%*/
1589 $$ = node_assign(p, $1, $4, $3, &@$);
1590 /*% %*/
1591 /*% ripper: assign!($1, $4) %*/
1593 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1595 /*%%%*/
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, &@$);
1598 /*% %*/
1599 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1601 | mlhs '=' lex_ctxt mrhs_arg
1603 /*%%%*/
1604 $$ = node_assign(p, $1, $4, $3, &@$);
1605 /*% %*/
1606 /*% ripper: massign!($1, $4) %*/
1608 | expr
1611 command_asgn : lhs '=' lex_ctxt command_rhs
1613 /*%%%*/
1614 $$ = node_assign(p, $1, $4, $3, &@$);
1615 /*% %*/
1616 /*% ripper: assign!($1, $4) %*/
1618 | var_lhs tOP_ASGN lex_ctxt command_rhs
1620 /*%%%*/
1621 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1622 /*% %*/
1623 /*% ripper: opassign!($1, $2, $4) %*/
1625 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1627 /*%%%*/
1628 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1629 /*% %*/
1630 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1633 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1635 /*%%%*/
1636 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1637 /*% %*/
1638 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1640 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1642 /*%%%*/
1643 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1644 /*% %*/
1645 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1647 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1649 /*%%%*/
1650 YYLTYPE loc = code_loc_gen(&@1, &@3);
1651 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1652 /*% %*/
1653 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1655 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1657 /*%%%*/
1658 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1659 /*% %*/
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);
1666 /*%%%*/
1667 $$ = set_defun_body(p, $1, $2, $4, &@$);
1668 /*% %*/
1669 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1670 /*% ripper: def!(get_value($1), $2, $4) %*/
1671 local_pop(p);
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);
1677 /*%%%*/
1678 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1679 $$ = set_defun_body(p, $1, $2, $4, &@$);
1680 /*% %*/
1681 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1682 /*% ripper: def!(get_value($1), $2, $4) %*/
1683 local_pop(p);
1685 | defs_head f_opt_paren_args '=' command
1687 endless_method_name(p, $<node>1, &@1);
1688 restore_defun(p, $<node>1->nd_defn);
1689 /*%%%*/
1690 $$ = set_defun_body(p, $1, $2, $4, &@$);
1692 $1 = get_value($1);
1694 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1695 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1696 local_pop(p);
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);
1702 /*%%%*/
1703 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1704 $$ = set_defun_body(p, $1, $2, $4, &@$);
1706 $1 = get_value($1);
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) %*/
1710 local_pop(p);
1712 | backref tOP_ASGN lex_ctxt command_rhs
1714 /*%%%*/
1715 rb_backref_error(p, $1);
1716 $$ = NEW_BEGIN(0, &@$);
1717 /*% %*/
1718 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1722 command_rhs : command_call %prec tOP_ASGN
1724 value_expr($1);
1725 $$ = $1;
1727 | command_call modifier_rescue stmt
1729 /*%%%*/
1730 YYLTYPE loc = code_loc_gen(&@2, &@3);
1731 value_expr($1);
1732 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1733 /*% %*/
1734 /*% ripper: rescue_mod!($1, $3) %*/
1736 | command_asgn
1739 expr : command_call
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, &@$);
1752 | '!' command_call
1754 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1756 | arg tASSOC
1758 value_expr($1);
1759 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1760 p->command_start = FALSE;
1761 $<ctxt>2 = p->ctxt;
1762 p->ctxt.in_kwarg = 1;
1763 $<tbl>$ = push_pvtbl(p);
1765 p_top_expr_body
1767 pop_pvtbl(p, $<tbl>3);
1768 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
1769 /*%%%*/
1770 $$ = NEW_CASE3($1, NEW_IN($4, 0, 0, &@4), &@$);
1771 /*% %*/
1772 /*% ripper: case!($1, in!($4, Qnil, Qnil)) %*/
1774 | arg keyword_in
1776 value_expr($1);
1777 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1778 p->command_start = FALSE;
1779 $<ctxt>2 = p->ctxt;
1780 p->ctxt.in_kwarg = 1;
1781 $<tbl>$ = push_pvtbl(p);
1783 p_top_expr_body
1785 pop_pvtbl(p, $<tbl>3);
1786 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
1787 /*%%%*/
1788 $$ = NEW_CASE3($1, NEW_IN($4, NEW_TRUE(&@4), NEW_FALSE(&@4), &@4), &@$);
1789 /*% %*/
1790 /*% ripper: case!($1, in!($4, Qnil, Qnil)) %*/
1792 | arg %prec tLBRACE_ARG
1795 def_name : fname
1797 ID fname = get_id($1);
1798 ID cur_arg = p->cur_arg;
1799 YYSTYPE c = {.ctxt = p->ctxt};
1800 numparam_name(p, fname);
1801 local_push(p, 0);
1802 p->cur_arg = 0;
1803 p->ctxt.in_def = 1;
1804 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
1805 /*%%%*/
1807 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
1812 defn_head : k_def def_name
1814 $$ = $2;
1815 /*%%%*/
1816 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
1817 /*% %*/
1821 defs_head : k_def singleton dot_or_colon
1823 SET_LEX_STATE(EXPR_FNAME);
1824 p->ctxt.in_argdef = 1;
1826 def_name
1828 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
1829 $$ = $5;
1830 /*%%%*/
1831 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
1833 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
1834 add_mark_object(p, ary);
1835 $<node>$->nd_rval = ary;
1840 expr_value : expr
1842 value_expr($1);
1843 $$ = $1;
1847 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1849 $$ = $2;
1853 command_call : command
1854 | block_command
1857 block_command : block_call
1858 | block_call call_op2 operation2 command_args
1860 /*%%%*/
1861 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1862 /*% %*/
1863 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
1867 cmd_brace_block : tLBRACE_ARG brace_body '}'
1869 $$ = $2;
1870 /*%%%*/
1871 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1872 nd_set_line($$, @1.end_pos.lineno);
1873 /*% %*/
1877 fcall : operation
1879 /*%%%*/
1880 $$ = NEW_FCALL($1, 0, &@$);
1881 nd_set_line($$, p->tokline);
1882 /*% %*/
1883 /*% ripper: $1 %*/
1887 command : fcall command_args %prec tLOWEST
1889 /*%%%*/
1890 $1->nd_args = $2;
1891 nd_set_last_loc($1, @2.end_pos);
1892 $$ = $1;
1893 /*% %*/
1894 /*% ripper: command!($1, $2) %*/
1896 | fcall command_args cmd_brace_block
1898 /*%%%*/
1899 block_dup_check(p, $2, $3);
1900 $1->nd_args = $2;
1901 $$ = method_add_block(p, $1, $3, &@$);
1902 fixpos($$, $1);
1903 nd_set_last_loc($1, @2.end_pos);
1904 /*% %*/
1905 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
1907 | primary_value call_op operation2 command_args %prec tLOWEST
1909 /*%%%*/
1910 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1911 /*% %*/
1912 /*% ripper: command_call!($1, $2, $3, $4) %*/
1914 | primary_value call_op operation2 command_args cmd_brace_block
1916 /*%%%*/
1917 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1918 /*% %*/
1919 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
1921 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1923 /*%%%*/
1924 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1925 /*% %*/
1926 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
1928 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1930 /*%%%*/
1931 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1932 /*% %*/
1933 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
1935 | keyword_super command_args
1937 /*%%%*/
1938 $$ = NEW_SUPER($2, &@$);
1939 fixpos($$, $2);
1940 /*% %*/
1941 /*% ripper: super!($2) %*/
1943 | keyword_yield command_args
1945 /*%%%*/
1946 $$ = new_yield(p, $2, &@$);
1947 fixpos($$, $2);
1948 /*% %*/
1949 /*% ripper: yield!($2) %*/
1951 | k_return call_args
1953 /*%%%*/
1954 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1955 /*% %*/
1956 /*% ripper: return!($2) %*/
1958 | keyword_break call_args
1960 /*%%%*/
1961 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1962 /*% %*/
1963 /*% ripper: break!($2) %*/
1965 | keyword_next call_args
1967 /*%%%*/
1968 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1969 /*% %*/
1970 /*% ripper: next!($2) %*/
1974 mlhs : mlhs_basic
1975 | tLPAREN mlhs_inner rparen
1977 /*%%%*/
1978 $$ = $2;
1979 /*% %*/
1980 /*% ripper: mlhs_paren!($2) %*/
1984 mlhs_inner : mlhs_basic
1985 | tLPAREN mlhs_inner rparen
1987 /*%%%*/
1988 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1989 /*% %*/
1990 /*% ripper: mlhs_paren!($2) %*/
1994 mlhs_basic : mlhs_head
1996 /*%%%*/
1997 $$ = NEW_MASGN($1, 0, &@$);
1998 /*% %*/
1999 /*% ripper: $1 %*/
2001 | mlhs_head mlhs_item
2003 /*%%%*/
2004 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2005 /*% %*/
2006 /*% ripper: mlhs_add!($1, $2) %*/
2008 | mlhs_head tSTAR mlhs_node
2010 /*%%%*/
2011 $$ = NEW_MASGN($1, $3, &@$);
2012 /*% %*/
2013 /*% ripper: mlhs_add_star!($1, $3) %*/
2015 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2017 /*%%%*/
2018 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2019 /*% %*/
2020 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2022 | mlhs_head tSTAR
2024 /*%%%*/
2025 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2026 /*% %*/
2027 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2029 | mlhs_head tSTAR ',' mlhs_post
2031 /*%%%*/
2032 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2033 /*% %*/
2034 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2036 | tSTAR mlhs_node
2038 /*%%%*/
2039 $$ = NEW_MASGN(0, $2, &@$);
2040 /*% %*/
2041 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2043 | tSTAR mlhs_node ',' mlhs_post
2045 /*%%%*/
2046 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2047 /*% %*/
2048 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2050 | tSTAR
2052 /*%%%*/
2053 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2054 /*% %*/
2055 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2057 | tSTAR ',' mlhs_post
2059 /*%%%*/
2060 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2061 /*% %*/
2062 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2066 mlhs_item : mlhs_node
2067 | tLPAREN mlhs_inner rparen
2069 /*%%%*/
2070 $$ = $2;
2071 /*% %*/
2072 /*% ripper: mlhs_paren!($2) %*/
2076 mlhs_head : mlhs_item ','
2078 /*%%%*/
2079 $$ = NEW_LIST($1, &@1);
2080 /*% %*/
2081 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2083 | mlhs_head mlhs_item ','
2085 /*%%%*/
2086 $$ = list_append(p, $1, $2);
2087 /*% %*/
2088 /*% ripper: mlhs_add!($1, $2) %*/
2092 mlhs_post : mlhs_item
2094 /*%%%*/
2095 $$ = NEW_LIST($1, &@$);
2096 /*% %*/
2097 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2099 | mlhs_post ',' mlhs_item
2101 /*%%%*/
2102 $$ = list_append(p, $1, $3);
2103 /*% %*/
2104 /*% ripper: mlhs_add!($1, $3) %*/
2108 mlhs_node : user_variable
2110 /*%%%*/
2111 $$ = assignable(p, $1, 0, &@$);
2112 /*% %*/
2113 /*% ripper: assignable(p, var_field(p, $1)) %*/
2115 | keyword_variable
2117 /*%%%*/
2118 $$ = assignable(p, $1, 0, &@$);
2119 /*% %*/
2120 /*% ripper: assignable(p, var_field(p, $1)) %*/
2122 | primary_value '[' opt_call_args rbracket
2124 /*%%%*/
2125 $$ = aryset(p, $1, $3, &@$);
2126 /*% %*/
2127 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2129 | primary_value call_op tIDENTIFIER
2131 if ($2 == tANDDOT) {
2132 yyerror1(&@2, "&. inside multiple assignment destination");
2134 /*%%%*/
2135 $$ = attrset(p, $1, $2, $3, &@$);
2136 /*% %*/
2137 /*% ripper: field!($1, $2, $3) %*/
2139 | primary_value tCOLON2 tIDENTIFIER
2141 /*%%%*/
2142 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2143 /*% %*/
2144 /*% ripper: const_path_field!($1, $3) %*/
2146 | primary_value call_op tCONSTANT
2148 if ($2 == tANDDOT) {
2149 yyerror1(&@2, "&. inside multiple assignment destination");
2151 /*%%%*/
2152 $$ = attrset(p, $1, $2, $3, &@$);
2153 /*% %*/
2154 /*% ripper: field!($1, $2, $3) %*/
2156 | primary_value tCOLON2 tCONSTANT
2158 /*%%%*/
2159 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2160 /*% %*/
2161 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2163 | tCOLON3 tCONSTANT
2165 /*%%%*/
2166 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2167 /*% %*/
2168 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2170 | backref
2172 /*%%%*/
2173 rb_backref_error(p, $1);
2174 $$ = NEW_BEGIN(0, &@$);
2175 /*% %*/
2176 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2180 lhs : user_variable
2182 /*%%%*/
2183 $$ = assignable(p, $1, 0, &@$);
2184 /*% %*/
2185 /*% ripper: assignable(p, var_field(p, $1)) %*/
2187 | keyword_variable
2189 /*%%%*/
2190 $$ = assignable(p, $1, 0, &@$);
2191 /*% %*/
2192 /*% ripper: assignable(p, var_field(p, $1)) %*/
2194 | primary_value '[' opt_call_args rbracket
2196 /*%%%*/
2197 $$ = aryset(p, $1, $3, &@$);
2198 /*% %*/
2199 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2201 | primary_value call_op tIDENTIFIER
2203 /*%%%*/
2204 $$ = attrset(p, $1, $2, $3, &@$);
2205 /*% %*/
2206 /*% ripper: field!($1, $2, $3) %*/
2208 | primary_value tCOLON2 tIDENTIFIER
2210 /*%%%*/
2211 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2212 /*% %*/
2213 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
2215 | primary_value call_op tCONSTANT
2217 /*%%%*/
2218 $$ = attrset(p, $1, $2, $3, &@$);
2219 /*% %*/
2220 /*% ripper: field!($1, $2, $3) %*/
2222 | primary_value tCOLON2 tCONSTANT
2224 /*%%%*/
2225 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2226 /*% %*/
2227 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2229 | tCOLON3 tCONSTANT
2231 /*%%%*/
2232 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2233 /*% %*/
2234 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2236 | backref
2238 /*%%%*/
2239 rb_backref_error(p, $1);
2240 $$ = NEW_BEGIN(0, &@$);
2241 /*% %*/
2242 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2246 cname : tIDENTIFIER
2248 static const char mesg[] = "class/module name must be CONSTANT";
2249 /*%%%*/
2250 yyerror1(&@1, mesg);
2251 /*% %*/
2252 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2254 | tCONSTANT
2257 cpath : tCOLON3 cname
2259 /*%%%*/
2260 $$ = NEW_COLON3($2, &@$);
2261 /*% %*/
2262 /*% ripper: top_const_ref!($2) %*/
2264 | cname
2266 /*%%%*/
2267 $$ = NEW_COLON2(0, $$, &@$);
2268 /*% %*/
2269 /*% ripper: const_ref!($1) %*/
2271 | primary_value tCOLON2 cname
2273 /*%%%*/
2274 $$ = NEW_COLON2($1, $3, &@$);
2275 /*% %*/
2276 /*% ripper: const_path_ref!($1, $3) %*/
2280 fname : tIDENTIFIER
2281 | tCONSTANT
2282 | tFID
2283 | op
2285 SET_LEX_STATE(EXPR_ENDFN);
2286 $$ = $1;
2288 | reswords
2291 fitem : fname
2293 /*%%%*/
2294 $$ = NEW_LIT(ID2SYM($1), &@$);
2295 /*% %*/
2296 /*% ripper: symbol_literal!($1) %*/
2298 | symbol
2301 undef_list : fitem
2303 /*%%%*/
2304 $$ = NEW_UNDEF($1, &@$);
2305 /*% %*/
2306 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2308 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2310 /*%%%*/
2311 NODE *undef = NEW_UNDEF($4, &@4);
2312 $$ = block_append(p, $1, undef);
2313 /*% %*/
2314 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2318 op : '|' { ifndef_ripper($$ = '|'); }
2319 | '^' { ifndef_ripper($$ = '^'); }
2320 | '&' { ifndef_ripper($$ = '&'); }
2321 | tCMP { ifndef_ripper($$ = tCMP); }
2322 | tEQ { ifndef_ripper($$ = tEQ); }
2323 | tEQQ { ifndef_ripper($$ = tEQQ); }
2324 | tMATCH { ifndef_ripper($$ = tMATCH); }
2325 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2326 | '>' { ifndef_ripper($$ = '>'); }
2327 | tGEQ { ifndef_ripper($$ = tGEQ); }
2328 | '<' { ifndef_ripper($$ = '<'); }
2329 | tLEQ { ifndef_ripper($$ = tLEQ); }
2330 | tNEQ { ifndef_ripper($$ = tNEQ); }
2331 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2332 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2333 | '+' { ifndef_ripper($$ = '+'); }
2334 | '-' { ifndef_ripper($$ = '-'); }
2335 | '*' { ifndef_ripper($$ = '*'); }
2336 | tSTAR { ifndef_ripper($$ = '*'); }
2337 | '/' { ifndef_ripper($$ = '/'); }
2338 | '%' { ifndef_ripper($$ = '%'); }
2339 | tPOW { ifndef_ripper($$ = tPOW); }
2340 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2341 | '!' { ifndef_ripper($$ = '!'); }
2342 | '~' { ifndef_ripper($$ = '~'); }
2343 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2344 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2345 | tAREF { ifndef_ripper($$ = tAREF); }
2346 | tASET { ifndef_ripper($$ = tASET); }
2347 | '`' { ifndef_ripper($$ = '`'); }
2350 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2351 | keyword_BEGIN | keyword_END
2352 | keyword_alias | keyword_and | keyword_begin
2353 | keyword_break | keyword_case | keyword_class | keyword_def
2354 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2355 | keyword_end | keyword_ensure | keyword_false
2356 | keyword_for | keyword_in | keyword_module | keyword_next
2357 | keyword_nil | keyword_not | keyword_or | keyword_redo
2358 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2359 | keyword_super | keyword_then | keyword_true | keyword_undef
2360 | keyword_when | keyword_yield | keyword_if | keyword_unless
2361 | keyword_while | keyword_until
2364 arg : lhs '=' lex_ctxt arg_rhs
2366 /*%%%*/
2367 $$ = node_assign(p, $1, $4, $3, &@$);
2368 /*% %*/
2369 /*% ripper: assign!($1, $4) %*/
2371 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2373 /*%%%*/
2374 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2375 /*% %*/
2376 /*% ripper: opassign!($1, $2, $4) %*/
2378 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2380 /*%%%*/
2381 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2382 /*% %*/
2383 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2385 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2387 /*%%%*/
2388 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2389 /*% %*/
2390 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2392 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2394 /*%%%*/
2395 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2396 /*% %*/
2397 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2399 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2401 /*%%%*/
2402 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2403 /*% %*/
2404 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
2406 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2408 /*%%%*/
2409 YYLTYPE loc = code_loc_gen(&@1, &@3);
2410 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2411 /*% %*/
2412 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2414 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2416 /*%%%*/
2417 YYLTYPE loc = code_loc_gen(&@1, &@2);
2418 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2419 /*% %*/
2420 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2422 | backref tOP_ASGN lex_ctxt arg_rhs
2424 /*%%%*/
2425 rb_backref_error(p, $1);
2426 $$ = NEW_BEGIN(0, &@$);
2427 /*% %*/
2428 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2430 | arg tDOT2 arg
2432 /*%%%*/
2433 value_expr($1);
2434 value_expr($3);
2435 $$ = NEW_DOT2($1, $3, &@$);
2436 /*% %*/
2437 /*% ripper: dot2!($1, $3) %*/
2439 | arg tDOT3 arg
2441 /*%%%*/
2442 value_expr($1);
2443 value_expr($3);
2444 $$ = NEW_DOT3($1, $3, &@$);
2445 /*% %*/
2446 /*% ripper: dot3!($1, $3) %*/
2448 | arg tDOT2
2450 /*%%%*/
2451 value_expr($1);
2452 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2453 /*% %*/
2454 /*% ripper: dot2!($1, Qnil) %*/
2456 | arg tDOT3
2458 /*%%%*/
2459 value_expr($1);
2460 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2461 /*% %*/
2462 /*% ripper: dot3!($1, Qnil) %*/
2464 | tBDOT2 arg
2466 /*%%%*/
2467 value_expr($2);
2468 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2469 /*% %*/
2470 /*% ripper: dot2!(Qnil, $2) %*/
2472 | tBDOT3 arg
2474 /*%%%*/
2475 value_expr($2);
2476 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2477 /*% %*/
2478 /*% ripper: dot3!(Qnil, $2) %*/
2480 | arg '+' arg
2482 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2484 | arg '-' arg
2486 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2488 | arg '*' arg
2490 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2492 | arg '/' arg
2494 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2496 | arg '%' arg
2498 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2500 | arg tPOW arg
2502 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2504 | tUMINUS_NUM simple_numeric tPOW arg
2506 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2508 | tUPLUS arg
2510 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2512 | tUMINUS arg
2514 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2516 | arg '|' arg
2518 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2520 | arg '^' arg
2522 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2524 | arg '&' arg
2526 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2528 | arg tCMP arg
2530 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2532 | rel_expr %prec tCMP
2533 | arg tEQ arg
2535 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2537 | arg tEQQ arg
2539 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2541 | arg tNEQ arg
2543 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2545 | arg tMATCH arg
2547 $$ = match_op(p, $1, $3, &@2, &@$);
2549 | arg tNMATCH arg
2551 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2553 | '!' arg
2555 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2557 | '~' arg
2559 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2561 | arg tLSHFT arg
2563 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2565 | arg tRSHFT arg
2567 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2569 | arg tANDOP arg
2571 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2573 | arg tOROP arg
2575 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2577 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2579 p->ctxt.in_defined = 0;
2580 $$ = new_defined(p, $4, &@$);
2582 | arg '?' arg opt_nl ':' arg
2584 /*%%%*/
2585 value_expr($1);
2586 $$ = new_if(p, $1, $3, $6, &@$);
2587 fixpos($$, $1);
2588 /*% %*/
2589 /*% ripper: ifop!($1, $3, $6) %*/
2591 | defn_head f_opt_paren_args '=' arg
2593 endless_method_name(p, $<node>1, &@1);
2594 restore_defun(p, $<node>1->nd_defn);
2595 /*%%%*/
2596 $$ = set_defun_body(p, $1, $2, $4, &@$);
2597 /*% %*/
2598 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2599 /*% ripper: def!(get_value($1), $2, $4) %*/
2600 local_pop(p);
2602 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2604 endless_method_name(p, $<node>1, &@1);
2605 restore_defun(p, $<node>1->nd_defn);
2606 /*%%%*/
2607 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2608 $$ = set_defun_body(p, $1, $2, $4, &@$);
2609 /*% %*/
2610 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2611 /*% ripper: def!(get_value($1), $2, $4) %*/
2612 local_pop(p);
2614 | defs_head f_opt_paren_args '=' arg
2616 endless_method_name(p, $<node>1, &@1);
2617 restore_defun(p, $<node>1->nd_defn);
2618 /*%%%*/
2619 $$ = set_defun_body(p, $1, $2, $4, &@$);
2621 $1 = get_value($1);
2623 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2624 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2625 local_pop(p);
2627 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2629 endless_method_name(p, $<node>1, &@1);
2630 restore_defun(p, $<node>1->nd_defn);
2631 /*%%%*/
2632 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2633 $$ = set_defun_body(p, $1, $2, $4, &@$);
2635 $1 = get_value($1);
2637 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2638 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2639 local_pop(p);
2641 | primary
2643 $$ = $1;
2647 relop : '>' {$$ = '>';}
2648 | '<' {$$ = '<';}
2649 | tGEQ {$$ = idGE;}
2650 | tLEQ {$$ = idLE;}
2653 rel_expr : arg relop arg %prec '>'
2655 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2657 | rel_expr relop arg %prec '>'
2659 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2660 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2664 lex_ctxt : tSP
2666 $$ = p->ctxt;
2668 | none
2670 $$ = p->ctxt;
2674 arg_value : arg
2676 value_expr($1);
2677 $$ = $1;
2681 aref_args : none
2682 | args trailer
2684 $$ = $1;
2686 | args ',' assocs trailer
2688 /*%%%*/
2689 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2690 /*% %*/
2691 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2693 | assocs trailer
2695 /*%%%*/
2696 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2697 /*% %*/
2698 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2702 arg_rhs : arg %prec tOP_ASGN
2704 value_expr($1);
2705 $$ = $1;
2707 | arg modifier_rescue arg
2709 /*%%%*/
2710 value_expr($1);
2711 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2712 /*% %*/
2713 /*% ripper: rescue_mod!($1, $3) %*/
2717 paren_args : '(' opt_call_args rparen
2719 /*%%%*/
2720 $$ = $2;
2721 /*% %*/
2722 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2724 | '(' args ',' args_forward rparen
2726 if (!check_forwarding_args(p)) {
2727 $$ = Qnone;
2729 else {
2730 /*%%%*/
2731 $$ = new_args_forward_call(p, $2, &@4, &@$);
2732 /*% %*/
2733 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
2736 | '(' args_forward rparen
2738 if (!check_forwarding_args(p)) {
2739 $$ = Qnone;
2741 else {
2742 /*%%%*/
2743 $$ = new_args_forward_call(p, 0, &@2, &@$);
2744 /*% %*/
2745 /*% ripper: arg_paren!($2) %*/
2750 opt_paren_args : none
2751 | paren_args
2754 opt_call_args : none
2755 | call_args
2756 | args ','
2758 $$ = $1;
2760 | args ',' assocs ','
2762 /*%%%*/
2763 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2764 /*% %*/
2765 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2767 | assocs ','
2769 /*%%%*/
2770 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2771 /*% %*/
2772 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2776 call_args : command
2778 /*%%%*/
2779 value_expr($1);
2780 $$ = NEW_LIST($1, &@$);
2781 /*% %*/
2782 /*% ripper: args_add!(args_new!, $1) %*/
2784 | args opt_block_arg
2786 /*%%%*/
2787 $$ = arg_blk_pass($1, $2);
2788 /*% %*/
2789 /*% ripper: args_add_block!($1, $2) %*/
2791 | assocs opt_block_arg
2793 /*%%%*/
2794 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2795 $$ = arg_blk_pass($$, $2);
2796 /*% %*/
2797 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
2799 | args ',' assocs opt_block_arg
2801 /*%%%*/
2802 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2803 $$ = arg_blk_pass($$, $4);
2804 /*% %*/
2805 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
2807 | block_arg
2808 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
2811 command_args : {
2812 /* If call_args starts with a open paren '(' or '[',
2813 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2814 * but the push must be done after CMDARG_PUSH(1).
2815 * So this code makes them consistent by first cancelling
2816 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2817 * and finally redoing CMDARG_PUSH(0).
2819 int lookahead = 0;
2820 switch (yychar) {
2821 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2822 lookahead = 1;
2824 if (lookahead) CMDARG_POP();
2825 CMDARG_PUSH(1);
2826 if (lookahead) CMDARG_PUSH(0);
2828 call_args
2830 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2831 * but the push must be done after CMDARG_POP() in the parser.
2832 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2833 * CMDARG_POP() to pop 1 pushed by command_args,
2834 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2836 int lookahead = 0;
2837 switch (yychar) {
2838 case tLBRACE_ARG:
2839 lookahead = 1;
2841 if (lookahead) CMDARG_POP();
2842 CMDARG_POP();
2843 if (lookahead) CMDARG_PUSH(0);
2844 $$ = $2;
2848 block_arg : tAMPER arg_value
2850 /*%%%*/
2851 $$ = NEW_BLOCK_PASS($2, &@$);
2852 /*% %*/
2853 /*% ripper: $2 %*/
2855 | tAMPER
2857 /*%%%*/
2858 if (!local_id(p, ANON_BLOCK_ID)) {
2859 compile_error(p, "no anonymous block parameter");
2861 $$ = NEW_BLOCK_PASS(NEW_LVAR(ANON_BLOCK_ID, &@1), &@$);
2863 $$ = Qnil;
2868 opt_block_arg : ',' block_arg
2870 $$ = $2;
2872 | none
2874 $$ = 0;
2878 /* value */
2879 args : arg_value
2881 /*%%%*/
2882 $$ = NEW_LIST($1, &@$);
2883 /*% %*/
2884 /*% ripper: args_add!(args_new!, $1) %*/
2886 | tSTAR arg_value
2888 /*%%%*/
2889 $$ = NEW_SPLAT($2, &@$);
2890 /*% %*/
2891 /*% ripper: args_add_star!(args_new!, $2) %*/
2893 | args ',' arg_value
2895 /*%%%*/
2896 $$ = last_arg_append(p, $1, $3, &@$);
2897 /*% %*/
2898 /*% ripper: args_add!($1, $3) %*/
2900 | args ',' tSTAR arg_value
2902 /*%%%*/
2903 $$ = rest_arg_append(p, $1, $4, &@$);
2904 /*% %*/
2905 /*% ripper: args_add_star!($1, $4) %*/
2909 /* value */
2910 mrhs_arg : mrhs
2911 | arg_value
2914 /* value */
2915 mrhs : args ',' arg_value
2917 /*%%%*/
2918 $$ = last_arg_append(p, $1, $3, &@$);
2919 /*% %*/
2920 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
2922 | args ',' tSTAR arg_value
2924 /*%%%*/
2925 $$ = rest_arg_append(p, $1, $4, &@$);
2926 /*% %*/
2927 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
2929 | tSTAR arg_value
2931 /*%%%*/
2932 $$ = NEW_SPLAT($2, &@$);
2933 /*% %*/
2934 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
2938 primary : literal
2939 | strings
2940 | xstring
2941 | regexp
2942 | words
2943 | qwords
2944 | symbols
2945 | qsymbols
2946 | var_ref
2947 | backref
2948 | tFID
2950 /*%%%*/
2951 $$ = NEW_FCALL($1, 0, &@$);
2952 /*% %*/
2953 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
2955 | k_begin
2957 CMDARG_PUSH(0);
2959 bodystmt
2960 k_end
2962 CMDARG_POP();
2963 /*%%%*/
2964 set_line_body($3, @1.end_pos.lineno);
2965 $$ = NEW_BEGIN($3, &@$);
2966 nd_set_line($$, @1.end_pos.lineno);
2967 /*% %*/
2968 /*% ripper: begin!($3) %*/
2970 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2972 /*%%%*/
2973 $$ = NEW_BEGIN(0, &@$);
2974 /*% %*/
2975 /*% ripper: paren!(0) %*/
2977 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2979 /*%%%*/
2980 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
2981 $$ = $2;
2982 /*% %*/
2983 /*% ripper: paren!($2) %*/
2985 | tLPAREN compstmt ')'
2987 /*%%%*/
2988 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
2989 $$ = $2;
2990 /*% %*/
2991 /*% ripper: paren!($2) %*/
2993 | primary_value tCOLON2 tCONSTANT
2995 /*%%%*/
2996 $$ = NEW_COLON2($1, $3, &@$);
2997 /*% %*/
2998 /*% ripper: const_path_ref!($1, $3) %*/
3000 | tCOLON3 tCONSTANT
3002 /*%%%*/
3003 $$ = NEW_COLON3($2, &@$);
3004 /*% %*/
3005 /*% ripper: top_const_ref!($2) %*/
3007 | tLBRACK aref_args ']'
3009 /*%%%*/
3010 $$ = make_list($2, &@$);
3011 /*% %*/
3012 /*% ripper: array!(escape_Qundef($2)) %*/
3014 | tLBRACE assoc_list '}'
3016 /*%%%*/
3017 $$ = new_hash(p, $2, &@$);
3018 $$->nd_brace = TRUE;
3019 /*% %*/
3020 /*% ripper: hash!(escape_Qundef($2)) %*/
3022 | k_return
3024 /*%%%*/
3025 $$ = NEW_RETURN(0, &@$);
3026 /*% %*/
3027 /*% ripper: return0! %*/
3029 | keyword_yield '(' call_args rparen
3031 /*%%%*/
3032 $$ = new_yield(p, $3, &@$);
3033 /*% %*/
3034 /*% ripper: yield!(paren!($3)) %*/
3036 | keyword_yield '(' rparen
3038 /*%%%*/
3039 $$ = NEW_YIELD(0, &@$);
3040 /*% %*/
3041 /*% ripper: yield!(paren!(args_new!)) %*/
3043 | keyword_yield
3045 /*%%%*/
3046 $$ = NEW_YIELD(0, &@$);
3047 /*% %*/
3048 /*% ripper: yield0! %*/
3050 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3052 p->ctxt.in_defined = 0;
3053 $$ = new_defined(p, $5, &@$);
3055 | keyword_not '(' expr rparen
3057 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3059 | keyword_not '(' rparen
3061 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3063 | fcall brace_block
3065 /*%%%*/
3066 $$ = method_add_block(p, $1, $2, &@$);
3067 /*% %*/
3068 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3070 | method_call
3071 | method_call brace_block
3073 /*%%%*/
3074 block_dup_check(p, $1->nd_args, $2);
3075 $$ = method_add_block(p, $1, $2, &@$);
3076 /*% %*/
3077 /*% ripper: method_add_block!($1, $2) %*/
3079 | lambda
3080 | k_if expr_value then
3081 compstmt
3082 if_tail
3083 k_end
3085 /*%%%*/
3086 $$ = new_if(p, $2, $4, $5, &@$);
3087 fixpos($$, $2);
3088 /*% %*/
3089 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3091 | k_unless expr_value then
3092 compstmt
3093 opt_else
3094 k_end
3096 /*%%%*/
3097 $$ = new_unless(p, $2, $4, $5, &@$);
3098 fixpos($$, $2);
3099 /*% %*/
3100 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3102 | k_while expr_value_do
3103 compstmt
3104 k_end
3106 /*%%%*/
3107 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3108 fixpos($$, $2);
3109 /*% %*/
3110 /*% ripper: while!($2, $3) %*/
3112 | k_until expr_value_do
3113 compstmt
3114 k_end
3116 /*%%%*/
3117 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3118 fixpos($$, $2);
3119 /*% %*/
3120 /*% ripper: until!($2, $3) %*/
3122 | k_case expr_value opt_terms
3124 $<val>$ = p->case_labels;
3125 p->case_labels = Qnil;
3127 case_body
3128 k_end
3130 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3131 p->case_labels = $<val>4;
3132 /*%%%*/
3133 $$ = NEW_CASE($2, $5, &@$);
3134 fixpos($$, $2);
3135 /*% %*/
3136 /*% ripper: case!($2, $5) %*/
3138 | k_case opt_terms
3140 $<val>$ = p->case_labels;
3141 p->case_labels = 0;
3143 case_body
3144 k_end
3146 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3147 p->case_labels = $<val>3;
3148 /*%%%*/
3149 $$ = NEW_CASE2($4, &@$);
3150 /*% %*/
3151 /*% ripper: case!(Qnil, $4) %*/
3153 | k_case expr_value opt_terms
3154 p_case_body
3155 k_end
3157 /*%%%*/
3158 $$ = NEW_CASE3($2, $4, &@$);
3159 /*% %*/
3160 /*% ripper: case!($2, $4) %*/
3162 | k_for for_var keyword_in expr_value_do
3163 compstmt
3164 k_end
3166 /*%%%*/
3168 * for a, b, c in e
3169 * #=>
3170 * e.each{|*x| a, b, c = x}
3172 * for a in e
3173 * #=>
3174 * e.each{|x| a, = x}
3176 ID id = internal_id(p);
3177 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3178 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3179 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3180 tbl->ids[0] = id; /* internal id */
3182 switch (nd_type($2)) {
3183 case NODE_LASGN:
3184 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3185 $2->nd_value = internal_var;
3186 id = 0;
3187 m->nd_plen = 1;
3188 m->nd_next = $2;
3189 break;
3190 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3191 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3192 break;
3193 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3194 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3196 /* {|*internal_id| <m> = internal_id; ... } */
3197 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3198 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3199 $$ = NEW_FOR($4, scope, &@$);
3200 fixpos($$, $2);
3201 /*% %*/
3202 /*% ripper: for!($2, $4, $5) %*/
3204 | k_class cpath superclass
3206 if (p->ctxt.in_def) {
3207 YYLTYPE loc = code_loc_gen(&@1, &@2);
3208 yyerror1(&loc, "class definition in method body");
3210 p->ctxt.in_class = 1;
3211 local_push(p, 0);
3213 bodystmt
3214 k_end
3216 /*%%%*/
3217 $$ = NEW_CLASS($2, $5, $3, &@$);
3218 nd_set_line($$->nd_body, @6.end_pos.lineno);
3219 set_line_body($5, @3.end_pos.lineno);
3220 nd_set_line($$, @3.end_pos.lineno);
3221 /*% %*/
3222 /*% ripper: class!($2, $3, $5) %*/
3223 local_pop(p);
3224 p->ctxt.in_class = $<ctxt>1.in_class;
3225 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3227 | k_class tLSHFT expr
3229 p->ctxt.in_def = 0;
3230 p->ctxt.in_class = 0;
3231 local_push(p, 0);
3233 term
3234 bodystmt
3235 k_end
3237 /*%%%*/
3238 $$ = NEW_SCLASS($3, $6, &@$);
3239 nd_set_line($$->nd_body, @7.end_pos.lineno);
3240 set_line_body($6, nd_line($3));
3241 fixpos($$, $3);
3242 /*% %*/
3243 /*% ripper: sclass!($3, $6) %*/
3244 local_pop(p);
3245 p->ctxt.in_def = $<ctxt>1.in_def;
3246 p->ctxt.in_class = $<ctxt>1.in_class;
3247 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3249 | k_module cpath
3251 if (p->ctxt.in_def) {
3252 YYLTYPE loc = code_loc_gen(&@1, &@2);
3253 yyerror1(&loc, "module definition in method body");
3255 p->ctxt.in_class = 1;
3256 local_push(p, 0);
3258 bodystmt
3259 k_end
3261 /*%%%*/
3262 $$ = NEW_MODULE($2, $4, &@$);
3263 nd_set_line($$->nd_body, @5.end_pos.lineno);
3264 set_line_body($4, @2.end_pos.lineno);
3265 nd_set_line($$, @2.end_pos.lineno);
3266 /*% %*/
3267 /*% ripper: module!($2, $4) %*/
3268 local_pop(p);
3269 p->ctxt.in_class = $<ctxt>1.in_class;
3270 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3272 | defn_head
3273 f_arglist
3274 bodystmt
3275 k_end
3277 restore_defun(p, $<node>1->nd_defn);
3278 /*%%%*/
3279 $$ = set_defun_body(p, $1, $2, $3, &@$);
3280 /*% %*/
3281 /*% ripper: def!(get_value($1), $2, $3) %*/
3282 local_pop(p);
3284 | defs_head
3285 f_arglist
3286 bodystmt
3287 k_end
3289 restore_defun(p, $<node>1->nd_defn);
3290 /*%%%*/
3291 $$ = set_defun_body(p, $1, $2, $3, &@$);
3293 $1 = get_value($1);
3295 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $3) %*/
3296 local_pop(p);
3298 | keyword_break
3300 /*%%%*/
3301 $$ = NEW_BREAK(0, &@$);
3302 /*% %*/
3303 /*% ripper: break!(args_new!) %*/
3305 | keyword_next
3307 /*%%%*/
3308 $$ = NEW_NEXT(0, &@$);
3309 /*% %*/
3310 /*% ripper: next!(args_new!) %*/
3312 | keyword_redo
3314 /*%%%*/
3315 $$ = NEW_REDO(&@$);
3316 /*% %*/
3317 /*% ripper: redo! %*/
3319 | keyword_retry
3321 /*%%%*/
3322 $$ = NEW_RETRY(&@$);
3323 /*% %*/
3324 /*% ripper: retry! %*/
3328 primary_value : primary
3330 value_expr($1);
3331 $$ = $1;
3335 k_begin : keyword_begin
3337 token_info_push(p, "begin", &@$);
3341 k_if : keyword_if
3343 WARN_EOL("if");
3344 token_info_push(p, "if", &@$);
3345 if (p->token_info && p->token_info->nonspc &&
3346 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3347 const char *tok = p->lex.ptok;
3348 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3349 beg += rb_strlen_lit("else");
3350 while (beg < tok && ISSPACE(*beg)) beg++;
3351 if (beg == tok) {
3352 p->token_info->nonspc = 0;
3358 k_unless : keyword_unless
3360 token_info_push(p, "unless", &@$);
3364 k_while : keyword_while
3366 token_info_push(p, "while", &@$);
3370 k_until : keyword_until
3372 token_info_push(p, "until", &@$);
3376 k_case : keyword_case
3378 token_info_push(p, "case", &@$);
3382 k_for : keyword_for
3384 token_info_push(p, "for", &@$);
3388 k_class : keyword_class
3390 token_info_push(p, "class", &@$);
3391 $<ctxt>$ = p->ctxt;
3395 k_module : keyword_module
3397 token_info_push(p, "module", &@$);
3398 $<ctxt>$ = p->ctxt;
3402 k_def : keyword_def
3404 token_info_push(p, "def", &@$);
3405 p->ctxt.in_argdef = 1;
3409 k_do : keyword_do
3411 token_info_push(p, "do", &@$);
3415 k_do_block : keyword_do_block
3417 token_info_push(p, "do", &@$);
3421 k_rescue : keyword_rescue
3423 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3427 k_ensure : keyword_ensure
3429 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3433 k_when : keyword_when
3435 token_info_warn(p, "when", p->token_info, 0, &@$);
3439 k_else : keyword_else
3441 token_info *ptinfo_beg = p->token_info;
3442 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3443 token_info_warn(p, "else", p->token_info, same, &@$);
3444 if (same) {
3445 token_info e;
3446 e.next = ptinfo_beg->next;
3447 e.token = "else";
3448 token_info_setup(&e, p->lex.pbeg, &@$);
3449 if (!e.nonspc) *ptinfo_beg = e;
3454 k_elsif : keyword_elsif
3456 WARN_EOL("elsif");
3457 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3461 k_end : keyword_end
3463 token_info_pop(p, "end", &@$);
3467 k_return : keyword_return
3469 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3470 yyerror1(&@1, "Invalid return in class/module body");
3474 then : term
3475 | keyword_then
3476 | term keyword_then
3479 do : term
3480 | keyword_do_cond
3483 if_tail : opt_else
3484 | k_elsif expr_value then
3485 compstmt
3486 if_tail
3488 /*%%%*/
3489 $$ = new_if(p, $2, $4, $5, &@$);
3490 fixpos($$, $2);
3491 /*% %*/
3492 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3496 opt_else : none
3497 | k_else compstmt
3499 /*%%%*/
3500 $$ = $2;
3501 /*% %*/
3502 /*% ripper: else!($2) %*/
3506 for_var : lhs
3507 | mlhs
3510 f_marg : f_norm_arg
3512 /*%%%*/
3513 $$ = assignable(p, $1, 0, &@$);
3514 mark_lvar_used(p, $$);
3515 /*% %*/
3516 /*% ripper: assignable(p, $1) %*/
3518 | tLPAREN f_margs rparen
3520 /*%%%*/
3521 $$ = $2;
3522 /*% %*/
3523 /*% ripper: mlhs_paren!($2) %*/
3527 f_marg_list : f_marg
3529 /*%%%*/
3530 $$ = NEW_LIST($1, &@$);
3531 /*% %*/
3532 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3534 | f_marg_list ',' f_marg
3536 /*%%%*/
3537 $$ = list_append(p, $1, $3);
3538 /*% %*/
3539 /*% ripper: mlhs_add!($1, $3) %*/
3543 f_margs : f_marg_list
3545 /*%%%*/
3546 $$ = NEW_MASGN($1, 0, &@$);
3547 /*% %*/
3548 /*% ripper: $1 %*/
3550 | f_marg_list ',' f_rest_marg
3552 /*%%%*/
3553 $$ = NEW_MASGN($1, $3, &@$);
3554 /*% %*/
3555 /*% ripper: mlhs_add_star!($1, $3) %*/
3557 | f_marg_list ',' f_rest_marg ',' f_marg_list
3559 /*%%%*/
3560 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3561 /*% %*/
3562 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3564 | f_rest_marg
3566 /*%%%*/
3567 $$ = NEW_MASGN(0, $1, &@$);
3568 /*% %*/
3569 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3571 | f_rest_marg ',' f_marg_list
3573 /*%%%*/
3574 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3575 /*% %*/
3576 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3580 f_rest_marg : tSTAR f_norm_arg
3582 /*%%%*/
3583 $$ = assignable(p, $2, 0, &@$);
3584 mark_lvar_used(p, $$);
3585 /*% %*/
3586 /*% ripper: assignable(p, $2) %*/
3588 | tSTAR
3590 /*%%%*/
3591 $$ = NODE_SPECIAL_NO_NAME_REST;
3592 /*% %*/
3593 /*% ripper: Qnil %*/
3597 f_any_kwrest : f_kwrest
3598 | f_no_kwarg {$$ = ID2VAL(idNil);}
3601 f_eq : {p->ctxt.in_argdef = 0;} '=';
3603 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3605 $$ = new_args_tail(p, $1, $3, $4, &@3);
3607 | f_block_kwarg opt_f_block_arg
3609 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3611 | f_any_kwrest opt_f_block_arg
3613 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3615 | f_block_arg
3617 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3621 opt_block_args_tail : ',' block_args_tail
3623 $$ = $2;
3625 | /* none */
3627 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3631 excessed_comma : ','
3633 /* magic number for rest_id in iseq_set_arguments() */
3634 /*%%%*/
3635 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3636 /*% %*/
3637 /*% ripper: excessed_comma! %*/
3641 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3643 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3645 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3647 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3649 | f_arg ',' f_block_optarg opt_block_args_tail
3651 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3653 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3655 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3657 | f_arg ',' f_rest_arg opt_block_args_tail
3659 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3661 | f_arg excessed_comma
3663 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3664 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3666 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3668 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3670 | f_arg opt_block_args_tail
3672 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3674 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3676 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3678 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3680 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3682 | f_block_optarg opt_block_args_tail
3684 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3686 | f_block_optarg ',' f_arg opt_block_args_tail
3688 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3690 | f_rest_arg opt_block_args_tail
3692 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3694 | f_rest_arg ',' f_arg opt_block_args_tail
3696 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3698 | block_args_tail
3700 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3704 opt_block_param : none
3705 | block_param_def
3707 p->command_start = TRUE;
3711 block_param_def : '|' opt_bv_decl '|'
3713 p->cur_arg = 0;
3714 p->max_numparam = ORDINAL_PARAM;
3715 p->ctxt.in_argdef = 0;
3716 /*%%%*/
3717 $$ = 0;
3718 /*% %*/
3719 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
3721 | '|' block_param opt_bv_decl '|'
3723 p->cur_arg = 0;
3724 p->max_numparam = ORDINAL_PARAM;
3725 p->ctxt.in_argdef = 0;
3726 /*%%%*/
3727 $$ = $2;
3728 /*% %*/
3729 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
3734 opt_bv_decl : opt_nl
3736 $$ = 0;
3738 | opt_nl ';' bv_decls opt_nl
3740 /*%%%*/
3741 $$ = 0;
3742 /*% %*/
3743 /*% ripper: $3 %*/
3747 bv_decls : bvar
3748 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
3749 | bv_decls ',' bvar
3750 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
3753 bvar : tIDENTIFIER
3755 new_bv(p, get_id($1));
3756 /*% ripper: get_value($1) %*/
3758 | f_bad_arg
3760 $$ = 0;
3764 lambda : tLAMBDA
3766 token_info_push(p, "->", &@1);
3767 $<vars>1 = dyna_push(p);
3768 $<num>$ = p->lex.lpar_beg;
3769 p->lex.lpar_beg = p->lex.paren_nest;
3772 $<num>$ = p->max_numparam;
3773 p->max_numparam = 0;
3776 $<node>$ = numparam_push(p);
3778 f_larglist
3780 CMDARG_PUSH(0);
3782 lambda_body
3784 int max_numparam = p->max_numparam;
3785 p->lex.lpar_beg = $<num>2;
3786 p->max_numparam = $<num>3;
3787 CMDARG_POP();
3788 $5 = args_with_numbered(p, $5, max_numparam);
3789 /*%%%*/
3791 YYLTYPE loc = code_loc_gen(&@5, &@7);
3792 $$ = NEW_LAMBDA($5, $7, &loc);
3793 nd_set_line($$->nd_body, @7.end_pos.lineno);
3794 nd_set_line($$, @5.end_pos.lineno);
3795 nd_set_first_loc($$, @1.beg_pos);
3797 /*% %*/
3798 /*% ripper: lambda!($5, $7) %*/
3799 numparam_pop(p, $<node>4);
3800 dyna_pop(p, $<vars>1);
3804 f_larglist : '(' f_args opt_bv_decl ')'
3806 p->ctxt.in_argdef = 0;
3807 /*%%%*/
3808 $$ = $2;
3809 p->max_numparam = ORDINAL_PARAM;
3810 /*% %*/
3811 /*% ripper: paren!($2) %*/
3813 | f_args
3815 p->ctxt.in_argdef = 0;
3816 /*%%%*/
3817 if (!args_info_empty_p($1->nd_ainfo))
3818 p->max_numparam = ORDINAL_PARAM;
3819 /*% %*/
3820 $$ = $1;
3824 lambda_body : tLAMBEG compstmt '}'
3826 token_info_pop(p, "}", &@3);
3827 $$ = $2;
3829 | keyword_do_LAMBDA bodystmt k_end
3831 $$ = $2;
3835 do_block : k_do_block do_body k_end
3837 $$ = $2;
3838 /*%%%*/
3839 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3840 nd_set_line($$, @1.end_pos.lineno);
3841 /*% %*/
3845 block_call : command do_block
3847 /*%%%*/
3848 if (nd_type_p($1, NODE_YIELD)) {
3849 compile_error(p, "block given to yield");
3851 else {
3852 block_dup_check(p, $1->nd_args, $2);
3854 $$ = method_add_block(p, $1, $2, &@$);
3855 fixpos($$, $1);
3856 /*% %*/
3857 /*% ripper: method_add_block!($1, $2) %*/
3859 | block_call call_op2 operation2 opt_paren_args
3861 /*%%%*/
3862 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3863 /*% %*/
3864 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3866 | block_call call_op2 operation2 opt_paren_args brace_block
3868 /*%%%*/
3869 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3870 /*% %*/
3871 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
3873 | block_call call_op2 operation2 command_args do_block
3875 /*%%%*/
3876 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3877 /*% %*/
3878 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
3882 method_call : fcall paren_args
3884 /*%%%*/
3885 $$ = $1;
3886 $$->nd_args = $2;
3887 nd_set_last_loc($1, @2.end_pos);
3888 /*% %*/
3889 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
3891 | primary_value call_op operation2 opt_paren_args
3893 /*%%%*/
3894 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3895 nd_set_line($$, @3.end_pos.lineno);
3896 /*% %*/
3897 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3899 | primary_value tCOLON2 operation2 paren_args
3901 /*%%%*/
3902 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3903 nd_set_line($$, @3.end_pos.lineno);
3904 /*% %*/
3905 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
3907 | primary_value tCOLON2 operation3
3909 /*%%%*/
3910 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3911 /*% %*/
3912 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
3914 | primary_value call_op paren_args
3916 /*%%%*/
3917 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3918 nd_set_line($$, @2.end_pos.lineno);
3919 /*% %*/
3920 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
3922 | primary_value tCOLON2 paren_args
3924 /*%%%*/
3925 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3926 nd_set_line($$, @2.end_pos.lineno);
3927 /*% %*/
3928 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
3930 | keyword_super paren_args
3932 /*%%%*/
3933 $$ = NEW_SUPER($2, &@$);
3934 /*% %*/
3935 /*% ripper: super!($2) %*/
3937 | keyword_super
3939 /*%%%*/
3940 $$ = NEW_ZSUPER(&@$);
3941 /*% %*/
3942 /*% ripper: zsuper! %*/
3944 | primary_value '[' opt_call_args rbracket
3946 /*%%%*/
3947 if ($1 && nd_type_p($1, NODE_SELF))
3948 $$ = NEW_FCALL(tAREF, $3, &@$);
3949 else
3950 $$ = NEW_CALL($1, tAREF, $3, &@$);
3951 fixpos($$, $1);
3952 /*% %*/
3953 /*% ripper: aref!($1, escape_Qundef($3)) %*/
3957 brace_block : '{' brace_body '}'
3959 $$ = $2;
3960 /*%%%*/
3961 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3962 nd_set_line($$, @1.end_pos.lineno);
3963 /*% %*/
3965 | k_do do_body k_end
3967 $$ = $2;
3968 /*%%%*/
3969 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3970 nd_set_line($$, @1.end_pos.lineno);
3971 /*% %*/
3975 brace_body : {$<vars>$ = dyna_push(p);}
3977 $<num>$ = p->max_numparam;
3978 p->max_numparam = 0;
3981 $<node>$ = numparam_push(p);
3983 opt_block_param compstmt
3985 int max_numparam = p->max_numparam;
3986 p->max_numparam = $<num>2;
3987 $4 = args_with_numbered(p, $4, max_numparam);
3988 /*%%%*/
3989 $$ = NEW_ITER($4, $5, &@$);
3990 /*% %*/
3991 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
3992 numparam_pop(p, $<node>3);
3993 dyna_pop(p, $<vars>1);
3997 do_body : {$<vars>$ = dyna_push(p);}
3999 $<num>$ = p->max_numparam;
4000 p->max_numparam = 0;
4003 $<node>$ = numparam_push(p);
4004 CMDARG_PUSH(0);
4006 opt_block_param bodystmt
4008 int max_numparam = p->max_numparam;
4009 p->max_numparam = $<num>2;
4010 $4 = args_with_numbered(p, $4, max_numparam);
4011 /*%%%*/
4012 $$ = NEW_ITER($4, $5, &@$);
4013 /*% %*/
4014 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4015 CMDARG_POP();
4016 numparam_pop(p, $<node>3);
4017 dyna_pop(p, $<vars>1);
4021 case_args : arg_value
4023 /*%%%*/
4024 check_literal_when(p, $1, &@1);
4025 $$ = NEW_LIST($1, &@$);
4026 /*% %*/
4027 /*% ripper: args_add!(args_new!, $1) %*/
4029 | tSTAR arg_value
4031 /*%%%*/
4032 $$ = NEW_SPLAT($2, &@$);
4033 /*% %*/
4034 /*% ripper: args_add_star!(args_new!, $2) %*/
4036 | case_args ',' arg_value
4038 /*%%%*/
4039 check_literal_when(p, $3, &@3);
4040 $$ = last_arg_append(p, $1, $3, &@$);
4041 /*% %*/
4042 /*% ripper: args_add!($1, $3) %*/
4044 | case_args ',' tSTAR arg_value
4046 /*%%%*/
4047 $$ = rest_arg_append(p, $1, $4, &@$);
4048 /*% %*/
4049 /*% ripper: args_add_star!($1, $4) %*/
4053 case_body : k_when case_args then
4054 compstmt
4055 cases
4057 /*%%%*/
4058 $$ = NEW_WHEN($2, $4, $5, &@$);
4059 fixpos($$, $2);
4060 /*% %*/
4061 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4065 cases : opt_else
4066 | case_body
4069 p_case_body : keyword_in
4071 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4072 p->command_start = FALSE;
4073 $<ctxt>1 = p->ctxt;
4074 p->ctxt.in_kwarg = 1;
4075 $<tbl>$ = push_pvtbl(p);
4078 $<tbl>$ = push_pktbl(p);
4080 p_top_expr then
4082 pop_pktbl(p, $<tbl>3);
4083 pop_pvtbl(p, $<tbl>2);
4084 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4086 compstmt
4087 p_cases
4089 /*%%%*/
4090 $$ = NEW_IN($4, $7, $8, &@$);
4091 /*% %*/
4092 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4096 p_cases : opt_else
4097 | p_case_body
4100 p_top_expr : p_top_expr_body
4101 | p_top_expr_body modifier_if expr_value
4103 /*%%%*/
4104 $$ = new_if(p, $3, $1, 0, &@$);
4105 fixpos($$, $3);
4106 /*% %*/
4107 /*% ripper: if_mod!($3, $1) %*/
4109 | p_top_expr_body modifier_unless expr_value
4111 /*%%%*/
4112 $$ = new_unless(p, $3, $1, 0, &@$);
4113 fixpos($$, $3);
4114 /*% %*/
4115 /*% ripper: unless_mod!($3, $1) %*/
4119 p_top_expr_body : p_expr
4120 | p_expr ','
4122 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4123 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4125 | p_expr ',' p_args
4127 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4128 /*%%%*/
4129 nd_set_first_loc($$, @1.beg_pos);
4133 | p_find
4135 $$ = new_find_pattern(p, Qnone, $1, &@$);
4137 | p_args_tail
4139 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4141 | p_kwargs
4143 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4147 p_expr : p_as
4150 p_as : p_expr tASSOC p_variable
4152 /*%%%*/
4153 NODE *n = NEW_LIST($1, &@$);
4154 n = list_append(p, n, $3);
4155 $$ = new_hash(p, n, &@$);
4156 /*% %*/
4157 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4159 | p_alt
4162 p_alt : p_alt '|' p_expr_basic
4164 /*%%%*/
4165 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4166 /*% %*/
4167 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4169 | p_expr_basic
4172 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4173 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4175 p_expr_basic : p_value
4176 | p_variable
4177 | p_const p_lparen p_args rparen
4179 pop_pktbl(p, $<tbl>2);
4180 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4181 /*%%%*/
4182 nd_set_first_loc($$, @1.beg_pos);
4186 | p_const p_lparen p_find rparen
4188 pop_pktbl(p, $<tbl>2);
4189 $$ = new_find_pattern(p, $1, $3, &@$);
4190 /*%%%*/
4191 nd_set_first_loc($$, @1.beg_pos);
4195 | p_const p_lparen p_kwargs rparen
4197 pop_pktbl(p, $<tbl>2);
4198 $$ = new_hash_pattern(p, $1, $3, &@$);
4199 /*%%%*/
4200 nd_set_first_loc($$, @1.beg_pos);
4204 | p_const '(' rparen
4206 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4207 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4209 | p_const p_lbracket p_args rbracket
4211 pop_pktbl(p, $<tbl>2);
4212 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4213 /*%%%*/
4214 nd_set_first_loc($$, @1.beg_pos);
4218 | p_const p_lbracket p_find rbracket
4220 pop_pktbl(p, $<tbl>2);
4221 $$ = new_find_pattern(p, $1, $3, &@$);
4222 /*%%%*/
4223 nd_set_first_loc($$, @1.beg_pos);
4227 | p_const p_lbracket p_kwargs rbracket
4229 pop_pktbl(p, $<tbl>2);
4230 $$ = new_hash_pattern(p, $1, $3, &@$);
4231 /*%%%*/
4232 nd_set_first_loc($$, @1.beg_pos);
4236 | p_const '[' rbracket
4238 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4239 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4241 | tLBRACK p_args rbracket
4243 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4245 | tLBRACK p_find rbracket
4247 $$ = new_find_pattern(p, Qnone, $2, &@$);
4249 | tLBRACK rbracket
4251 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4252 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4254 | tLBRACE
4256 $<tbl>$ = push_pktbl(p);
4257 $<ctxt>1 = p->ctxt;
4258 p->ctxt.in_kwarg = 0;
4260 p_kwargs rbrace
4262 pop_pktbl(p, $<tbl>2);
4263 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4264 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4266 | tLBRACE rbrace
4268 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4269 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4271 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4273 pop_pktbl(p, $<tbl>2);
4274 $$ = $3;
4278 p_args : p_expr
4280 /*%%%*/
4281 NODE *pre_args = NEW_LIST($1, &@$);
4282 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4284 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4287 | p_args_head
4289 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4291 | p_args_head p_arg
4293 /*%%%*/
4294 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4296 VALUE pre_args = rb_ary_concat($1, get_value($2));
4297 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4300 | p_args_head tSTAR tIDENTIFIER
4302 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4304 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4306 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4308 | p_args_head tSTAR
4310 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4312 | p_args_head tSTAR ',' p_args_post
4314 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4316 | p_args_tail
4319 p_args_head : p_arg ','
4321 $$ = $1;
4323 | p_args_head p_arg ','
4325 /*%%%*/
4326 $$ = list_concat($1, $2);
4327 /*% %*/
4328 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4332 p_args_tail : p_rest
4334 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4336 | p_rest ',' p_args_post
4338 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4342 p_find : p_rest ',' p_args_post ',' p_rest
4344 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4346 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
4347 rb_warn0L_experimental(nd_line($$), "Find pattern is experimental, and the behavior may change in future versions of Ruby!");
4352 p_rest : tSTAR tIDENTIFIER
4354 $$ = $2;
4356 | tSTAR
4358 $$ = 0;
4362 p_args_post : p_arg
4363 | p_args_post ',' p_arg
4365 /*%%%*/
4366 $$ = list_concat($1, $3);
4367 /*% %*/
4368 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4372 p_arg : p_expr
4374 /*%%%*/
4375 $$ = NEW_LIST($1, &@$);
4376 /*% %*/
4377 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4381 p_kwargs : p_kwarg ',' p_any_kwrest
4383 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4385 | p_kwarg
4387 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4389 | p_kwarg ','
4391 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4393 | p_any_kwrest
4395 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4399 p_kwarg : p_kw
4400 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4401 | p_kwarg ',' p_kw
4403 /*%%%*/
4404 $$ = list_concat($1, $3);
4405 /*% %*/
4406 /*% ripper: rb_ary_push($1, $3) %*/
4410 p_kw : p_kw_label p_expr
4412 error_duplicate_pattern_key(p, get_id($1), &@1);
4413 /*%%%*/
4414 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4415 /*% %*/
4416 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4418 | p_kw_label
4420 error_duplicate_pattern_key(p, get_id($1), &@1);
4421 if ($1 && !is_local_id(get_id($1))) {
4422 yyerror1(&@1, "key must be valid as local variables");
4424 error_duplicate_pattern_variable(p, get_id($1), &@1);
4425 /*%%%*/
4426 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4427 /*% %*/
4428 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4432 p_kw_label : tLABEL
4433 | tSTRING_BEG string_contents tLABEL_END
4435 YYLTYPE loc = code_loc_gen(&@1, &@3);
4436 /*%%%*/
4437 if (!$2 || nd_type_p($2, NODE_STR)) {
4438 NODE *node = dsym_node(p, $2, &loc);
4439 $$ = SYM2ID(node->nd_lit);
4442 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4443 VALUE label = RNODE($2)->nd_cval;
4444 VALUE rval = RNODE($2)->nd_rval;
4445 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4446 RNODE($$)->nd_loc = loc;
4449 else {
4450 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4451 $$ = 0;
4456 p_kwrest : kwrest_mark tIDENTIFIER
4458 $$ = $2;
4460 | kwrest_mark
4462 $$ = 0;
4466 p_kwnorest : kwrest_mark keyword_nil
4468 $$ = 0;
4472 p_any_kwrest : p_kwrest
4473 | p_kwnorest {$$ = ID2VAL(idNil);}
4476 p_value : p_primitive
4477 | p_primitive tDOT2 p_primitive
4479 /*%%%*/
4480 value_expr($1);
4481 value_expr($3);
4482 $$ = NEW_DOT2($1, $3, &@$);
4483 /*% %*/
4484 /*% ripper: dot2!($1, $3) %*/
4486 | p_primitive tDOT3 p_primitive
4488 /*%%%*/
4489 value_expr($1);
4490 value_expr($3);
4491 $$ = NEW_DOT3($1, $3, &@$);
4492 /*% %*/
4493 /*% ripper: dot3!($1, $3) %*/
4495 | p_primitive tDOT2
4497 /*%%%*/
4498 value_expr($1);
4499 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4500 /*% %*/
4501 /*% ripper: dot2!($1, Qnil) %*/
4503 | p_primitive tDOT3
4505 /*%%%*/
4506 value_expr($1);
4507 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4508 /*% %*/
4509 /*% ripper: dot3!($1, Qnil) %*/
4511 | p_var_ref
4512 | p_expr_ref
4513 | p_const
4514 | tBDOT2 p_primitive
4516 /*%%%*/
4517 value_expr($2);
4518 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4519 /*% %*/
4520 /*% ripper: dot2!(Qnil, $2) %*/
4522 | tBDOT3 p_primitive
4524 /*%%%*/
4525 value_expr($2);
4526 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4527 /*% %*/
4528 /*% ripper: dot3!(Qnil, $2) %*/
4532 p_primitive : literal
4533 | strings
4534 | xstring
4535 | regexp
4536 | words
4537 | qwords
4538 | symbols
4539 | qsymbols
4540 | keyword_variable
4542 /*%%%*/
4543 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4544 /*% %*/
4545 /*% ripper: var_ref!($1) %*/
4547 | lambda
4550 p_variable : tIDENTIFIER
4552 /*%%%*/
4553 error_duplicate_pattern_variable(p, $1, &@1);
4554 $$ = assignable(p, $1, 0, &@$);
4555 /*% %*/
4556 /*% ripper: assignable(p, var_field(p, $1)) %*/
4560 p_var_ref : '^' tIDENTIFIER
4562 /*%%%*/
4563 NODE *n = gettable(p, $2, &@$);
4564 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4565 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4567 $$ = n;
4568 /*% %*/
4569 /*% ripper: var_ref!($2) %*/
4571 | '^' nonlocal_var
4573 /*%%%*/
4574 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4575 /*% %*/
4576 /*% ripper: var_ref!($2) %*/
4580 p_expr_ref : '^' tLPAREN expr_value ')'
4582 /*%%%*/
4583 $$ = NEW_BEGIN($3, &@$);
4584 /*% %*/
4585 /*% ripper: begin!($3) %*/
4589 p_const : tCOLON3 cname
4591 /*%%%*/
4592 $$ = NEW_COLON3($2, &@$);
4593 /*% %*/
4594 /*% ripper: top_const_ref!($2) %*/
4596 | p_const tCOLON2 cname
4598 /*%%%*/
4599 $$ = NEW_COLON2($1, $3, &@$);
4600 /*% %*/
4601 /*% ripper: const_path_ref!($1, $3) %*/
4603 | tCONSTANT
4605 /*%%%*/
4606 $$ = gettable(p, $1, &@$);
4607 /*% %*/
4608 /*% ripper: var_ref!($1) %*/
4612 opt_rescue : k_rescue exc_list exc_var then
4613 compstmt
4614 opt_rescue
4616 /*%%%*/
4617 $$ = NEW_RESBODY($2,
4618 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4619 $6, &@$);
4620 fixpos($$, $2?$2:$5);
4621 /*% %*/
4622 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4624 | none
4627 exc_list : arg_value
4629 /*%%%*/
4630 $$ = NEW_LIST($1, &@$);
4631 /*% %*/
4632 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4634 | mrhs
4636 /*%%%*/
4637 if (!($$ = splat_array($1))) $$ = $1;
4638 /*% %*/
4639 /*% ripper: $1 %*/
4641 | none
4644 exc_var : tASSOC lhs
4646 $$ = $2;
4648 | none
4651 opt_ensure : k_ensure compstmt
4653 /*%%%*/
4654 $$ = $2;
4655 /*% %*/
4656 /*% ripper: ensure!($2) %*/
4658 | none
4661 literal : numeric
4662 | symbol
4665 strings : string
4667 /*%%%*/
4668 NODE *node = $1;
4669 if (!node) {
4670 node = NEW_STR(STR_NEW0(), &@$);
4671 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4673 else {
4674 node = evstr2dstr(p, node);
4676 $$ = node;
4677 /*% %*/
4678 /*% ripper: $1 %*/
4682 string : tCHAR
4683 | string1
4684 | string string1
4686 /*%%%*/
4687 $$ = literal_concat(p, $1, $2, &@$);
4688 /*% %*/
4689 /*% ripper: string_concat!($1, $2) %*/
4693 string1 : tSTRING_BEG string_contents tSTRING_END
4695 /*%%%*/
4696 $$ = heredoc_dedent(p, $2);
4697 if ($$) nd_set_loc($$, &@$);
4698 /*% %*/
4699 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4703 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4705 /*%%%*/
4706 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4707 /*% %*/
4708 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4712 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4714 $$ = new_regexp(p, $2, $3, &@$);
4718 words : tWORDS_BEG ' ' word_list tSTRING_END
4720 /*%%%*/
4721 $$ = make_list($3, &@$);
4722 /*% %*/
4723 /*% ripper: array!($3) %*/
4727 word_list : /* none */
4729 /*%%%*/
4730 $$ = 0;
4731 /*% %*/
4732 /*% ripper: words_new! %*/
4734 | word_list word ' '
4736 /*%%%*/
4737 $$ = list_append(p, $1, evstr2dstr(p, $2));
4738 /*% %*/
4739 /*% ripper: words_add!($1, $2) %*/
4743 word : string_content
4744 /*% ripper[brace]: word_add!(word_new!, $1) %*/
4745 | word string_content
4747 /*%%%*/
4748 $$ = literal_concat(p, $1, $2, &@$);
4749 /*% %*/
4750 /*% ripper: word_add!($1, $2) %*/
4754 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4756 /*%%%*/
4757 $$ = make_list($3, &@$);
4758 /*% %*/
4759 /*% ripper: array!($3) %*/
4763 symbol_list : /* none */
4765 /*%%%*/
4766 $$ = 0;
4767 /*% %*/
4768 /*% ripper: symbols_new! %*/
4770 | symbol_list word ' '
4772 /*%%%*/
4773 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4774 /*% %*/
4775 /*% ripper: symbols_add!($1, $2) %*/
4779 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4781 /*%%%*/
4782 $$ = make_list($3, &@$);
4783 /*% %*/
4784 /*% ripper: array!($3) %*/
4788 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4790 /*%%%*/
4791 $$ = make_list($3, &@$);
4792 /*% %*/
4793 /*% ripper: array!($3) %*/
4797 qword_list : /* none */
4799 /*%%%*/
4800 $$ = 0;
4801 /*% %*/
4802 /*% ripper: qwords_new! %*/
4804 | qword_list tSTRING_CONTENT ' '
4806 /*%%%*/
4807 $$ = list_append(p, $1, $2);
4808 /*% %*/
4809 /*% ripper: qwords_add!($1, $2) %*/
4813 qsym_list : /* none */
4815 /*%%%*/
4816 $$ = 0;
4817 /*% %*/
4818 /*% ripper: qsymbols_new! %*/
4820 | qsym_list tSTRING_CONTENT ' '
4822 /*%%%*/
4823 $$ = symbol_append(p, $1, $2);
4824 /*% %*/
4825 /*% ripper: qsymbols_add!($1, $2) %*/
4829 string_contents : /* none */
4831 /*%%%*/
4832 $$ = 0;
4833 /*% %*/
4834 /*% ripper: string_content! %*/
4835 /*%%%*/
4837 $$ = ripper_new_yylval(p, 0, $$, 0);
4840 | string_contents string_content
4842 /*%%%*/
4843 $$ = literal_concat(p, $1, $2, &@$);
4844 /*% %*/
4845 /*% ripper: string_add!($1, $2) %*/
4846 /*%%%*/
4848 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4849 !RNODE($1)->nd_cval) {
4850 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4851 RNODE($1)->nd_rval = add_mark_object(p, $$);
4852 $$ = $1;
4858 xstring_contents: /* none */
4860 /*%%%*/
4861 $$ = 0;
4862 /*% %*/
4863 /*% ripper: xstring_new! %*/
4865 | xstring_contents string_content
4867 /*%%%*/
4868 $$ = literal_concat(p, $1, $2, &@$);
4869 /*% %*/
4870 /*% ripper: xstring_add!($1, $2) %*/
4874 regexp_contents: /* none */
4876 /*%%%*/
4877 $$ = 0;
4878 /*% %*/
4879 /*% ripper: regexp_new! %*/
4880 /*%%%*/
4882 $$ = ripper_new_yylval(p, 0, $$, 0);
4885 | regexp_contents string_content
4887 /*%%%*/
4888 NODE *head = $1, *tail = $2;
4889 if (!head) {
4890 $$ = tail;
4892 else if (!tail) {
4893 $$ = head;
4895 else {
4896 switch (nd_type(head)) {
4897 case NODE_STR:
4898 nd_set_type(head, NODE_DSTR);
4899 break;
4900 case NODE_DSTR:
4901 break;
4902 default:
4903 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4904 break;
4906 $$ = list_append(p, head, tail);
4909 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4910 if (ripper_is_node_yylval(n1)) {
4911 s1 = RNODE(n1)->nd_cval;
4912 n1 = RNODE(n1)->nd_rval;
4914 if (ripper_is_node_yylval(n2)) {
4915 s2 = RNODE(n2)->nd_cval;
4916 n2 = RNODE(n2)->nd_rval;
4918 $$ = dispatch2(regexp_add, n1, n2);
4919 if (!s1 && s2) {
4920 $$ = ripper_new_yylval(p, 0, $$, s2);
4926 string_content : tSTRING_CONTENT
4927 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
4928 | tSTRING_DVAR
4930 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4931 $<strterm>$ = p->lex.strterm;
4932 p->lex.strterm = 0;
4933 SET_LEX_STATE(EXPR_BEG);
4935 string_dvar
4937 p->lex.strterm = $<strterm>2;
4938 /*%%%*/
4939 $$ = NEW_EVSTR($3, &@$);
4940 nd_set_line($$, @3.end_pos.lineno);
4941 /*% %*/
4942 /*% ripper: string_dvar!($3) %*/
4944 | tSTRING_DBEG
4946 CMDARG_PUSH(0);
4947 COND_PUSH(0);
4950 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4951 $<strterm>$ = p->lex.strterm;
4952 p->lex.strterm = 0;
4955 $<num>$ = p->lex.state;
4956 SET_LEX_STATE(EXPR_BEG);
4959 $<num>$ = p->lex.brace_nest;
4960 p->lex.brace_nest = 0;
4963 $<num>$ = p->heredoc_indent;
4964 p->heredoc_indent = 0;
4966 compstmt tSTRING_DEND
4968 COND_POP();
4969 CMDARG_POP();
4970 p->lex.strterm = $<strterm>3;
4971 SET_LEX_STATE($<num>4);
4972 p->lex.brace_nest = $<num>5;
4973 p->heredoc_indent = $<num>6;
4974 p->heredoc_line_indent = -1;
4975 /*%%%*/
4976 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4977 $$ = new_evstr(p, $7, &@$);
4978 /*% %*/
4979 /*% ripper: string_embexpr!($7) %*/
4983 string_dvar : tGVAR
4985 /*%%%*/
4986 $$ = NEW_GVAR($1, &@$);
4987 /*% %*/
4988 /*% ripper: var_ref!($1) %*/
4990 | tIVAR
4992 /*%%%*/
4993 $$ = NEW_IVAR($1, &@$);
4994 /*% %*/
4995 /*% ripper: var_ref!($1) %*/
4997 | tCVAR
4999 /*%%%*/
5000 $$ = NEW_CVAR($1, &@$);
5001 /*% %*/
5002 /*% ripper: var_ref!($1) %*/
5004 | backref
5007 symbol : ssym
5008 | dsym
5011 ssym : tSYMBEG sym
5013 SET_LEX_STATE(EXPR_END);
5014 /*%%%*/
5015 $$ = NEW_LIT(ID2SYM($2), &@$);
5016 /*% %*/
5017 /*% ripper: symbol_literal!(symbol!($2)) %*/
5021 sym : fname
5022 | tIVAR
5023 | tGVAR
5024 | tCVAR
5027 dsym : tSYMBEG string_contents tSTRING_END
5029 SET_LEX_STATE(EXPR_END);
5030 /*%%%*/
5031 $$ = dsym_node(p, $2, &@$);
5032 /*% %*/
5033 /*% ripper: dyna_symbol!($2) %*/
5037 numeric : simple_numeric
5038 | tUMINUS_NUM simple_numeric %prec tLOWEST
5040 /*%%%*/
5041 $$ = $2;
5042 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5043 /*% %*/
5044 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5048 simple_numeric : tINTEGER
5049 | tFLOAT
5050 | tRATIONAL
5051 | tIMAGINARY
5054 nonlocal_var : tIVAR
5055 | tGVAR
5056 | tCVAR
5059 user_variable : tIDENTIFIER
5060 | tIVAR
5061 | tGVAR
5062 | tCONSTANT
5063 | tCVAR
5066 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5067 | keyword_self {$$ = KWD2EID(self, $1);}
5068 | keyword_true {$$ = KWD2EID(true, $1);}
5069 | keyword_false {$$ = KWD2EID(false, $1);}
5070 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5071 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5072 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5075 var_ref : user_variable
5077 /*%%%*/
5078 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5080 if (id_is_var(p, get_id($1))) {
5081 $$ = dispatch1(var_ref, $1);
5083 else {
5084 $$ = dispatch1(vcall, $1);
5088 | keyword_variable
5090 /*%%%*/
5091 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5092 /*% %*/
5093 /*% ripper: var_ref!($1) %*/
5097 var_lhs : user_variable
5099 /*%%%*/
5100 $$ = assignable(p, $1, 0, &@$);
5101 /*% %*/
5102 /*% ripper: assignable(p, var_field(p, $1)) %*/
5104 | keyword_variable
5106 /*%%%*/
5107 $$ = assignable(p, $1, 0, &@$);
5108 /*% %*/
5109 /*% ripper: assignable(p, var_field(p, $1)) %*/
5113 backref : tNTH_REF
5114 | tBACK_REF
5117 superclass : '<'
5119 SET_LEX_STATE(EXPR_BEG);
5120 p->command_start = TRUE;
5122 expr_value term
5124 $$ = $3;
5126 | /* none */
5128 /*%%%*/
5129 $$ = 0;
5130 /*% %*/
5131 /*% ripper: Qnil %*/
5135 f_opt_paren_args: f_paren_args
5136 | none
5138 p->ctxt.in_argdef = 0;
5139 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5140 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5144 f_paren_args : '(' f_args rparen
5146 /*%%%*/
5147 $$ = $2;
5148 /*% %*/
5149 /*% ripper: paren!($2) %*/
5150 SET_LEX_STATE(EXPR_BEG);
5151 p->command_start = TRUE;
5152 p->ctxt.in_argdef = 0;
5156 f_arglist : f_paren_args
5158 $<ctxt>$ = p->ctxt;
5159 p->ctxt.in_kwarg = 1;
5160 p->ctxt.in_argdef = 1;
5161 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5163 f_args term
5165 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5166 p->ctxt.in_argdef = 0;
5167 $$ = $2;
5168 SET_LEX_STATE(EXPR_BEG);
5169 p->command_start = TRUE;
5173 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5175 $$ = new_args_tail(p, $1, $3, $4, &@3);
5177 | f_kwarg opt_f_block_arg
5179 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5181 | f_any_kwrest opt_f_block_arg
5183 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5185 | f_block_arg
5187 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5189 | args_forward
5191 add_forwarding_args(p);
5192 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5196 opt_args_tail : ',' args_tail
5198 $$ = $2;
5200 | /* none */
5202 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5206 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5208 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5210 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5212 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5214 | f_arg ',' f_optarg opt_args_tail
5216 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5218 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5220 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5222 | f_arg ',' f_rest_arg opt_args_tail
5224 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5226 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5228 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5230 | f_arg opt_args_tail
5232 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5234 | f_optarg ',' f_rest_arg opt_args_tail
5236 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5238 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5240 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5242 | f_optarg opt_args_tail
5244 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5246 | f_optarg ',' f_arg opt_args_tail
5248 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5250 | f_rest_arg opt_args_tail
5252 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5254 | f_rest_arg ',' f_arg opt_args_tail
5256 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5258 | args_tail
5260 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5262 | /* none */
5264 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5265 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5269 args_forward : tBDOT3
5271 /*%%%*/
5272 $$ = idFWD_KWREST;
5273 /*% %*/
5274 /*% ripper: args_forward! %*/
5278 f_bad_arg : tCONSTANT
5280 static const char mesg[] = "formal argument cannot be a constant";
5281 /*%%%*/
5282 yyerror1(&@1, mesg);
5283 $$ = 0;
5284 /*% %*/
5285 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5287 | tIVAR
5289 static const char mesg[] = "formal argument cannot be an instance variable";
5290 /*%%%*/
5291 yyerror1(&@1, mesg);
5292 $$ = 0;
5293 /*% %*/
5294 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5296 | tGVAR
5298 static const char mesg[] = "formal argument cannot be a global variable";
5299 /*%%%*/
5300 yyerror1(&@1, mesg);
5301 $$ = 0;
5302 /*% %*/
5303 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5305 | tCVAR
5307 static const char mesg[] = "formal argument cannot be a class variable";
5308 /*%%%*/
5309 yyerror1(&@1, mesg);
5310 $$ = 0;
5311 /*% %*/
5312 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5316 f_norm_arg : f_bad_arg
5317 | tIDENTIFIER
5319 formal_argument(p, $1);
5320 p->max_numparam = ORDINAL_PARAM;
5321 $$ = $1;
5325 f_arg_asgn : f_norm_arg
5327 ID id = get_id($1);
5328 arg_var(p, id);
5329 p->cur_arg = id;
5330 $$ = $1;
5334 f_arg_item : f_arg_asgn
5336 p->cur_arg = 0;
5337 /*%%%*/
5338 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5339 /*% %*/
5340 /*% ripper: get_value($1) %*/
5342 | tLPAREN f_margs rparen
5344 /*%%%*/
5345 ID tid = internal_id(p);
5346 YYLTYPE loc;
5347 loc.beg_pos = @2.beg_pos;
5348 loc.end_pos = @2.beg_pos;
5349 arg_var(p, tid);
5350 if (dyna_in_block(p)) {
5351 $2->nd_value = NEW_DVAR(tid, &loc);
5353 else {
5354 $2->nd_value = NEW_LVAR(tid, &loc);
5356 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5357 $$->nd_next = $2;
5358 /*% %*/
5359 /*% ripper: mlhs_paren!($2) %*/
5363 f_arg : f_arg_item
5364 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5365 | f_arg ',' f_arg_item
5367 /*%%%*/
5368 $$ = $1;
5369 $$->nd_plen++;
5370 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5371 rb_discard_node(p, $3);
5372 /*% %*/
5373 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5378 f_label : tLABEL
5380 arg_var(p, formal_argument(p, $1));
5381 p->cur_arg = get_id($1);
5382 p->max_numparam = ORDINAL_PARAM;
5383 p->ctxt.in_argdef = 0;
5384 $$ = $1;
5388 f_kw : f_label arg_value
5390 p->cur_arg = 0;
5391 p->ctxt.in_argdef = 1;
5392 /*%%%*/
5393 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5394 /*% %*/
5395 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5397 | f_label
5399 p->cur_arg = 0;
5400 p->ctxt.in_argdef = 1;
5401 /*%%%*/
5402 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5403 /*% %*/
5404 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5408 f_block_kw : f_label primary_value
5410 p->ctxt.in_argdef = 1;
5411 /*%%%*/
5412 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5413 /*% %*/
5414 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5416 | f_label
5418 p->ctxt.in_argdef = 1;
5419 /*%%%*/
5420 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5421 /*% %*/
5422 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5426 f_block_kwarg : f_block_kw
5428 /*%%%*/
5429 $$ = $1;
5430 /*% %*/
5431 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5433 | f_block_kwarg ',' f_block_kw
5435 /*%%%*/
5436 $$ = kwd_append($1, $3);
5437 /*% %*/
5438 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5443 f_kwarg : f_kw
5445 /*%%%*/
5446 $$ = $1;
5447 /*% %*/
5448 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5450 | f_kwarg ',' f_kw
5452 /*%%%*/
5453 $$ = kwd_append($1, $3);
5454 /*% %*/
5455 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5459 kwrest_mark : tPOW
5460 | tDSTAR
5463 f_no_kwarg : kwrest_mark keyword_nil
5465 /*%%%*/
5466 /*% %*/
5467 /*% ripper: nokw_param!(Qnil) %*/
5471 f_kwrest : kwrest_mark tIDENTIFIER
5473 arg_var(p, shadowing_lvar(p, get_id($2)));
5474 /*%%%*/
5475 $$ = $2;
5476 /*% %*/
5477 /*% ripper: kwrest_param!($2) %*/
5479 | kwrest_mark
5481 /*%%%*/
5482 $$ = internal_id(p);
5483 arg_var(p, $$);
5484 /*% %*/
5485 /*% ripper: kwrest_param!(Qnil) %*/
5489 f_opt : f_arg_asgn f_eq arg_value
5491 p->cur_arg = 0;
5492 p->ctxt.in_argdef = 1;
5493 /*%%%*/
5494 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5495 /*% %*/
5496 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5500 f_block_opt : f_arg_asgn f_eq primary_value
5502 p->cur_arg = 0;
5503 p->ctxt.in_argdef = 1;
5504 /*%%%*/
5505 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5506 /*% %*/
5507 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5511 f_block_optarg : f_block_opt
5513 /*%%%*/
5514 $$ = $1;
5515 /*% %*/
5516 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5518 | f_block_optarg ',' f_block_opt
5520 /*%%%*/
5521 $$ = opt_arg_append($1, $3);
5522 /*% %*/
5523 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5527 f_optarg : f_opt
5529 /*%%%*/
5530 $$ = $1;
5531 /*% %*/
5532 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5534 | f_optarg ',' f_opt
5536 /*%%%*/
5537 $$ = opt_arg_append($1, $3);
5538 /*% %*/
5539 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5543 restarg_mark : '*'
5544 | tSTAR
5547 f_rest_arg : restarg_mark tIDENTIFIER
5549 arg_var(p, shadowing_lvar(p, get_id($2)));
5550 /*%%%*/
5551 $$ = $2;
5552 /*% %*/
5553 /*% ripper: rest_param!($2) %*/
5555 | restarg_mark
5557 /*%%%*/
5558 $$ = internal_id(p);
5559 arg_var(p, $$);
5560 /*% %*/
5561 /*% ripper: rest_param!(Qnil) %*/
5565 blkarg_mark : '&'
5566 | tAMPER
5569 f_block_arg : blkarg_mark tIDENTIFIER
5571 arg_var(p, shadowing_lvar(p, get_id($2)));
5572 /*%%%*/
5573 $$ = $2;
5574 /*% %*/
5575 /*% ripper: blockarg!($2) %*/
5577 | blkarg_mark
5579 /*%%%*/
5580 arg_var(p, shadowing_lvar(p, get_id(ANON_BLOCK_ID)));
5582 $$ = dispatch1(blockarg, Qnil);
5587 opt_f_block_arg : ',' f_block_arg
5589 $$ = $2;
5591 | none
5593 $$ = Qnull;
5597 singleton : var_ref
5599 value_expr($1);
5600 $$ = $1;
5602 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5604 /*%%%*/
5605 switch (nd_type($3)) {
5606 case NODE_STR:
5607 case NODE_DSTR:
5608 case NODE_XSTR:
5609 case NODE_DXSTR:
5610 case NODE_DREGX:
5611 case NODE_LIT:
5612 case NODE_LIST:
5613 case NODE_ZLIST:
5614 yyerror1(&@3, "can't define singleton method for literals");
5615 break;
5616 default:
5617 value_expr($3);
5618 break;
5620 $$ = $3;
5621 /*% %*/
5622 /*% ripper: paren!($3) %*/
5626 assoc_list : none
5627 | assocs trailer
5629 /*%%%*/
5630 $$ = $1;
5631 /*% %*/
5632 /*% ripper: assoclist_from_args!($1) %*/
5636 assocs : assoc
5637 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5638 | assocs ',' assoc
5640 /*%%%*/
5641 NODE *assocs = $1;
5642 NODE *tail = $3;
5643 if (!assocs) {
5644 assocs = tail;
5646 else if (tail) {
5647 if (assocs->nd_head &&
5648 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
5649 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
5650 /* DSTAR */
5651 tail = tail->nd_next->nd_head->nd_head;
5653 assocs = list_concat(assocs, tail);
5655 $$ = assocs;
5656 /*% %*/
5657 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5661 assoc : arg_value tASSOC arg_value
5663 /*%%%*/
5664 if (nd_type_p($1, NODE_STR)) {
5665 nd_set_type($1, NODE_LIT);
5666 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5668 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5669 /*% %*/
5670 /*% ripper: assoc_new!($1, $3) %*/
5672 | tLABEL arg_value
5674 /*%%%*/
5675 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5676 /*% %*/
5677 /*% ripper: assoc_new!($1, $2) %*/
5679 | tLABEL
5681 /*%%%*/
5682 NODE *val = gettable(p, $1, &@$);
5683 if (!val) val = NEW_BEGIN(0, &@$);
5684 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
5685 /*% %*/
5686 /*% ripper: assoc_new!($1, Qnil) %*/
5688 | tSTRING_BEG string_contents tLABEL_END arg_value
5690 /*%%%*/
5691 YYLTYPE loc = code_loc_gen(&@1, &@3);
5692 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5693 /*% %*/
5694 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
5696 | tDSTAR arg_value
5698 /*%%%*/
5699 if (nd_type_p($2, NODE_HASH) &&
5700 !($2->nd_head && $2->nd_head->nd_alen)) {
5701 static VALUE empty_hash;
5702 if (!empty_hash) {
5703 empty_hash = rb_obj_freeze(rb_hash_new());
5704 rb_gc_register_mark_object(empty_hash);
5706 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5708 else
5709 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5710 /*% %*/
5711 /*% ripper: assoc_splat!($2) %*/
5715 operation : tIDENTIFIER
5716 | tCONSTANT
5717 | tFID
5720 operation2 : tIDENTIFIER
5721 | tCONSTANT
5722 | tFID
5723 | op
5726 operation3 : tIDENTIFIER
5727 | tFID
5728 | op
5731 dot_or_colon : '.'
5732 | tCOLON2
5735 call_op : '.'
5736 | tANDDOT
5739 call_op2 : call_op
5740 | tCOLON2
5743 opt_terms : /* none */
5744 | terms
5747 opt_nl : /* none */
5748 | '\n'
5751 rparen : opt_nl ')'
5754 rbracket : opt_nl ']'
5757 rbrace : opt_nl '}'
5760 trailer : /* none */
5761 | '\n'
5762 | ','
5765 term : ';' {yyerrok;token_flush(p);}
5766 | '\n' {token_flush(p);}
5769 terms : term
5770 | terms ';' {yyerrok;}
5773 none : /* none */
5775 $$ = Qnull;
5779 # undef p
5780 # undef yylex
5781 # undef yylval
5782 # define yylval (*p->lval)
5784 static int regx_options(struct parser_params*);
5785 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5786 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5787 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5788 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5790 #ifndef RIPPER
5791 # define set_yylval_node(x) { \
5792 YYLTYPE _cur_loc; \
5793 rb_parser_set_location(p, &_cur_loc); \
5794 yylval.node = (x); \
5796 # define set_yylval_str(x) \
5797 do { \
5798 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5799 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5800 } while(0)
5801 # define set_yylval_literal(x) \
5802 do { \
5803 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5804 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5805 } while(0)
5806 # define set_yylval_num(x) (yylval.num = (x))
5807 # define set_yylval_id(x) (yylval.id = (x))
5808 # define set_yylval_name(x) (yylval.id = (x))
5809 # define yylval_id() (yylval.id)
5810 #else
5811 static inline VALUE
5812 ripper_yylval_id(struct parser_params *p, ID x)
5814 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5816 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5817 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5818 # define set_yylval_id(x) (void)(x)
5819 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5820 # define set_yylval_literal(x) add_mark_object(p, (x))
5821 # define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
5822 # define yylval_id() yylval.id
5823 # define _cur_loc NULL_LOC /* dummy */
5824 #endif
5826 #define set_yylval_noname() set_yylval_id(keyword_nil)
5828 #ifndef RIPPER
5829 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5830 #define dispatch_scan_event(p, t) ((void)0)
5831 #define dispatch_delayed_token(p, t) ((void)0)
5832 #define has_delayed_token(p) (0)
5833 #else
5834 #define literal_flush(p, ptr) ((void)(ptr))
5836 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5838 static inline VALUE
5839 intern_sym(const char *name)
5841 ID id = rb_intern_const(name);
5842 return ID2SYM(id);
5845 static int
5846 ripper_has_scan_event(struct parser_params *p)
5848 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5849 return p->lex.pcur > p->lex.ptok;
5852 static VALUE
5853 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5855 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5856 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5857 token_flush(p);
5858 return rval;
5861 static void
5862 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5864 if (!ripper_has_scan_event(p)) return;
5865 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5867 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5869 static void
5870 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5872 int saved_line = p->ruby_sourceline;
5873 const char *saved_tokp = p->lex.ptok;
5875 if (NIL_P(p->delayed.token)) return;
5876 p->ruby_sourceline = p->delayed.line;
5877 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5878 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5879 p->delayed.token = Qnil;
5880 p->ruby_sourceline = saved_line;
5881 p->lex.ptok = saved_tokp;
5883 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5884 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5885 #endif /* RIPPER */
5887 static inline int
5888 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5890 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5893 static inline int
5894 parser_is_identchar(struct parser_params *p)
5896 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5899 static inline int
5900 parser_isascii(struct parser_params *p)
5902 return ISASCII(*(p->lex.pcur-1));
5905 static void
5906 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5908 int column = 1, nonspc = 0, i;
5909 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5910 if (*ptr == '\t') {
5911 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5913 column++;
5914 if (*ptr != ' ' && *ptr != '\t') {
5915 nonspc = 1;
5919 ptinfo->beg = loc->beg_pos;
5920 ptinfo->indent = column;
5921 ptinfo->nonspc = nonspc;
5924 static void
5925 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5927 token_info *ptinfo;
5929 if (!p->token_info_enabled) return;
5930 ptinfo = ALLOC(token_info);
5931 ptinfo->token = token;
5932 ptinfo->next = p->token_info;
5933 token_info_setup(ptinfo, p->lex.pbeg, loc);
5935 p->token_info = ptinfo;
5938 static void
5939 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5941 token_info *ptinfo_beg = p->token_info;
5943 if (!ptinfo_beg) return;
5944 p->token_info = ptinfo_beg->next;
5946 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5947 token_info_warn(p, token, ptinfo_beg, 1, loc);
5948 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5951 static void
5952 token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
5954 token_info *ptinfo_beg = p->token_info;
5956 if (!ptinfo_beg) return;
5957 p->token_info = ptinfo_beg->next;
5959 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
5960 ptinfo_beg->beg.column != beg_pos.column ||
5961 strcmp(ptinfo_beg->token, token)) {
5962 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
5963 beg_pos.lineno, beg_pos.column, token,
5964 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
5965 ptinfo_beg->token);
5968 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5971 static void
5972 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5974 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5975 if (!p->token_info_enabled) return;
5976 if (!ptinfo_beg) return;
5977 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5978 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5979 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5980 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5981 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5982 rb_warn3L(ptinfo_end->beg.lineno,
5983 "mismatched indentations at '%s' with '%s' at %d",
5984 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5987 static int
5988 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5990 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5991 if (!MBCLEN_CHARFOUND_P(len)) {
5992 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5993 return -1;
5995 return len;
5998 #ifndef RIPPER
5999 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6001 static inline void
6002 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6004 VALUE str;
6005 int lineno = p->ruby_sourceline;
6006 if (!yylloc) {
6007 return;
6009 else if (yylloc->beg_pos.lineno == lineno) {
6010 str = p->lex.lastline;
6012 else {
6013 return;
6015 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6018 static int
6019 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6021 #if 0
6022 YYLTYPE current;
6024 if (!yylloc) {
6025 yylloc = RUBY_SET_YYLLOC(current);
6027 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6028 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6029 yylloc = 0;
6031 #endif
6032 compile_error(p, "%s", msg);
6033 parser_show_error_line(p, yylloc);
6034 return 0;
6037 static int
6038 parser_yyerror0(struct parser_params *p, const char *msg)
6040 YYLTYPE current;
6041 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6044 static void
6045 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6047 VALUE mesg;
6048 const int max_line_margin = 30;
6049 const char *ptr, *ptr_end, *pt, *pb;
6050 const char *pre = "", *post = "", *pend;
6051 const char *code = "", *caret = "";
6052 const char *lim;
6053 const char *const pbeg = RSTRING_PTR(str);
6054 char *buf;
6055 long len;
6056 int i;
6058 if (!yylloc) return;
6059 pend = RSTRING_END(str);
6060 if (pend > pbeg && pend[-1] == '\n') {
6061 if (--pend > pbeg && pend[-1] == '\r') --pend;
6064 pt = pend;
6065 if (lineno == yylloc->end_pos.lineno &&
6066 (pend - pbeg) > yylloc->end_pos.column) {
6067 pt = pbeg + yylloc->end_pos.column;
6070 ptr = ptr_end = pt;
6071 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6072 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6074 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6075 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6077 len = ptr_end - ptr;
6078 if (len > 4) {
6079 if (ptr > pbeg) {
6080 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6081 if (ptr > pbeg) pre = "...";
6083 if (ptr_end < pend) {
6084 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6085 if (ptr_end < pend) post = "...";
6088 pb = pbeg;
6089 if (lineno == yylloc->beg_pos.lineno) {
6090 pb += yylloc->beg_pos.column;
6091 if (pb > pt) pb = pt;
6093 if (pb < ptr) pb = ptr;
6094 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6095 return;
6097 if (RTEST(errbuf)) {
6098 mesg = rb_attr_get(errbuf, idMesg);
6099 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6100 rb_str_cat_cstr(mesg, "\n");
6102 else {
6103 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6105 if (!errbuf && rb_stderr_tty_p()) {
6106 #define CSI_BEGIN "\033["
6107 #define CSI_SGR "m"
6108 rb_str_catf(mesg,
6109 CSI_BEGIN""CSI_SGR"%s" /* pre */
6110 CSI_BEGIN"1"CSI_SGR"%.*s"
6111 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6112 CSI_BEGIN";1"CSI_SGR"%.*s"
6113 CSI_BEGIN""CSI_SGR"%s" /* post */
6114 "\n",
6115 pre,
6116 (int)(pb - ptr), ptr,
6117 (int)(pt - pb), pb,
6118 (int)(ptr_end - pt), pt,
6119 post);
6121 else {
6122 char *p2;
6124 len = ptr_end - ptr;
6125 lim = pt < pend ? pt : pend;
6126 i = (int)(lim - ptr);
6127 buf = ALLOCA_N(char, i+2);
6128 code = ptr;
6129 caret = p2 = buf;
6130 if (ptr <= pb) {
6131 while (ptr < pb) {
6132 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6134 *p2++ = '^';
6135 ptr++;
6137 if (lim > ptr) {
6138 memset(p2, '~', (lim - ptr));
6139 p2 += (lim - ptr);
6141 *p2 = '\0';
6142 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6143 pre, (int)len, code, post,
6144 pre, caret);
6146 if (!errbuf) rb_write_error_str(mesg);
6148 #else
6149 static int
6150 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6152 const char *pcur = 0, *ptok = 0;
6153 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6154 p->ruby_sourceline == yylloc->end_pos.lineno) {
6155 pcur = p->lex.pcur;
6156 ptok = p->lex.ptok;
6157 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6158 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6160 parser_yyerror0(p, msg);
6161 if (pcur) {
6162 p->lex.ptok = ptok;
6163 p->lex.pcur = pcur;
6165 return 0;
6168 static int
6169 parser_yyerror0(struct parser_params *p, const char *msg)
6171 dispatch1(parse_error, STR_NEW2(msg));
6172 ripper_error(p);
6173 return 0;
6176 static inline void
6177 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6180 #endif /* !RIPPER */
6182 #ifndef RIPPER
6183 static int
6184 vtable_size(const struct vtable *tbl)
6186 if (!DVARS_TERMINAL_P(tbl)) {
6187 return tbl->pos;
6189 else {
6190 return 0;
6193 #endif
6195 static struct vtable *
6196 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6198 struct vtable *tbl = ALLOC(struct vtable);
6199 tbl->pos = 0;
6200 tbl->capa = 8;
6201 tbl->tbl = ALLOC_N(ID, tbl->capa);
6202 tbl->prev = prev;
6203 #ifndef RIPPER
6204 if (p->debug) {
6205 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6207 #endif
6208 return tbl;
6210 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6212 static void
6213 vtable_free_gen(struct parser_params *p, int line, const char *name,
6214 struct vtable *tbl)
6216 #ifndef RIPPER
6217 if (p->debug) {
6218 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6220 #endif
6221 if (!DVARS_TERMINAL_P(tbl)) {
6222 if (tbl->tbl) {
6223 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6225 ruby_sized_xfree(tbl, sizeof(*tbl));
6228 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6230 static void
6231 vtable_add_gen(struct parser_params *p, int line, const char *name,
6232 struct vtable *tbl, ID id)
6234 #ifndef RIPPER
6235 if (p->debug) {
6236 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6237 line, name, (void *)tbl, rb_id2name(id));
6239 #endif
6240 if (DVARS_TERMINAL_P(tbl)) {
6241 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6242 return;
6244 if (tbl->pos == tbl->capa) {
6245 tbl->capa = tbl->capa * 2;
6246 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6248 tbl->tbl[tbl->pos++] = id;
6250 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6252 #ifndef RIPPER
6253 static void
6254 vtable_pop_gen(struct parser_params *p, int line, const char *name,
6255 struct vtable *tbl, int n)
6257 if (p->debug) {
6258 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6259 line, name, (void *)tbl, n);
6261 if (tbl->pos < n) {
6262 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6263 return;
6265 tbl->pos -= n;
6267 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6268 #endif
6270 static int
6271 vtable_included(const struct vtable * tbl, ID id)
6273 int i;
6275 if (!DVARS_TERMINAL_P(tbl)) {
6276 for (i = 0; i < tbl->pos; i++) {
6277 if (tbl->tbl[i] == id) {
6278 return i+1;
6282 return 0;
6285 static void parser_prepare(struct parser_params *p);
6287 #ifndef RIPPER
6288 static NODE *parser_append_options(struct parser_params *p, NODE *node);
6290 static VALUE
6291 debug_lines(VALUE fname)
6293 ID script_lines;
6294 CONST_ID(script_lines, "SCRIPT_LINES__");
6295 if (rb_const_defined_at(rb_cObject, script_lines)) {
6296 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6297 if (RB_TYPE_P(hash, T_HASH)) {
6298 VALUE lines = rb_ary_new();
6299 rb_hash_aset(hash, fname, lines);
6300 return lines;
6303 return 0;
6306 static int
6307 e_option_supplied(struct parser_params *p)
6309 return strcmp(p->ruby_sourcefile, "-e") == 0;
6312 static VALUE
6313 yycompile0(VALUE arg)
6315 int n;
6316 NODE *tree;
6317 struct parser_params *p = (struct parser_params *)arg;
6318 VALUE cov = Qfalse;
6320 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6321 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6322 if (p->debug_lines && p->ruby_sourceline > 0) {
6323 VALUE str = rb_default_rs;
6324 n = p->ruby_sourceline;
6325 do {
6326 rb_ary_push(p->debug_lines, str);
6327 } while (--n);
6330 if (!e_option_supplied(p)) {
6331 cov = Qtrue;
6335 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6336 if (!p->debug_lines) {
6337 p->debug_lines = rb_ary_new();
6340 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6343 parser_prepare(p);
6344 #define RUBY_DTRACE_PARSE_HOOK(name) \
6345 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6346 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6348 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6349 n = yyparse(p);
6350 RUBY_DTRACE_PARSE_HOOK(END);
6351 p->debug_lines = 0;
6353 p->lex.strterm = 0;
6354 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6355 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6356 if (n || p->error_p) {
6357 VALUE mesg = p->error_buffer;
6358 if (!mesg) {
6359 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6361 rb_set_errinfo(mesg);
6362 return FALSE;
6364 tree = p->eval_tree;
6365 if (!tree) {
6366 tree = NEW_NIL(&NULL_LOC);
6368 else {
6369 VALUE opt = p->compile_option;
6370 NODE *prelude;
6371 NODE *body = parser_append_options(p, tree->nd_body);
6372 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6373 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6374 prelude = block_append(p, p->eval_tree_begin, body);
6375 tree->nd_body = prelude;
6376 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6378 p->ast->body.root = tree;
6379 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6380 return TRUE;
6383 static rb_ast_t *
6384 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6386 rb_ast_t *ast;
6387 if (NIL_P(fname)) {
6388 p->ruby_sourcefile_string = Qnil;
6389 p->ruby_sourcefile = "(none)";
6391 else {
6392 p->ruby_sourcefile_string = rb_fstring(fname);
6393 p->ruby_sourcefile = StringValueCStr(fname);
6395 p->ruby_sourceline = line - 1;
6397 p->lvtbl = NULL;
6399 p->ast = ast = rb_ast_new();
6400 rb_suppress_tracing(yycompile0, (VALUE)p);
6401 p->ast = 0;
6402 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6404 while (p->lvtbl) {
6405 local_pop(p);
6408 return ast;
6410 #endif /* !RIPPER */
6412 static rb_encoding *
6413 must_be_ascii_compatible(VALUE s)
6415 rb_encoding *enc = rb_enc_get(s);
6416 if (!rb_enc_asciicompat(enc)) {
6417 rb_raise(rb_eArgError, "invalid source encoding");
6419 return enc;
6422 static VALUE
6423 lex_get_str(struct parser_params *p, VALUE s)
6425 char *beg, *end, *start;
6426 long len;
6428 beg = RSTRING_PTR(s);
6429 len = RSTRING_LEN(s);
6430 start = beg;
6431 if (p->lex.gets_.ptr) {
6432 if (len == p->lex.gets_.ptr) return Qnil;
6433 beg += p->lex.gets_.ptr;
6434 len -= p->lex.gets_.ptr;
6436 end = memchr(beg, '\n', len);
6437 if (end) len = ++end - beg;
6438 p->lex.gets_.ptr += len;
6439 return rb_str_subseq(s, beg - start, len);
6442 static VALUE
6443 lex_getline(struct parser_params *p)
6445 VALUE line = (*p->lex.gets)(p, p->lex.input);
6446 if (NIL_P(line)) return line;
6447 must_be_ascii_compatible(line);
6448 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6449 #ifndef RIPPER
6450 if (p->debug_lines) {
6451 rb_enc_associate(line, p->enc);
6452 rb_ary_push(p->debug_lines, line);
6454 #endif
6455 p->line_count++;
6456 return line;
6459 static const rb_data_type_t parser_data_type;
6461 #ifndef RIPPER
6462 static rb_ast_t*
6463 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6465 struct parser_params *p;
6467 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6469 p->lex.gets = lex_get_str;
6470 p->lex.gets_.ptr = 0;
6471 p->lex.input = rb_str_new_frozen(s);
6472 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6474 return yycompile(vparser, p, fname, line);
6477 rb_ast_t*
6478 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6480 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6483 rb_ast_t*
6484 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6486 must_be_ascii_compatible(s);
6487 return parser_compile_string(vparser, f, s, line);
6490 VALUE rb_io_gets_internal(VALUE io);
6492 static VALUE
6493 lex_io_gets(struct parser_params *p, VALUE io)
6495 return rb_io_gets_internal(io);
6498 rb_ast_t*
6499 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6501 struct parser_params *p;
6503 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6505 p->lex.gets = lex_io_gets;
6506 p->lex.input = file;
6507 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6509 return yycompile(vparser, p, fname, start);
6512 static VALUE
6513 lex_generic_gets(struct parser_params *p, VALUE input)
6515 return (*p->lex.gets_.call)(input, p->line_count);
6518 rb_ast_t*
6519 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6521 struct parser_params *p;
6523 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6525 p->lex.gets = lex_generic_gets;
6526 p->lex.gets_.call = lex_gets;
6527 p->lex.input = input;
6528 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6530 return yycompile(vparser, p, fname, start);
6532 #endif /* !RIPPER */
6534 #define STR_FUNC_ESCAPE 0x01
6535 #define STR_FUNC_EXPAND 0x02
6536 #define STR_FUNC_REGEXP 0x04
6537 #define STR_FUNC_QWORDS 0x08
6538 #define STR_FUNC_SYMBOL 0x10
6539 #define STR_FUNC_INDENT 0x20
6540 #define STR_FUNC_LABEL 0x40
6541 #define STR_FUNC_LIST 0x4000
6542 #define STR_FUNC_TERM 0x8000
6544 enum string_type {
6545 str_label = STR_FUNC_LABEL,
6546 str_squote = (0),
6547 str_dquote = (STR_FUNC_EXPAND),
6548 str_xquote = (STR_FUNC_EXPAND),
6549 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6550 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6551 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6552 str_ssym = (STR_FUNC_SYMBOL),
6553 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6556 static VALUE
6557 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6559 VALUE str;
6561 str = rb_enc_str_new(ptr, len, enc);
6562 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6563 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6565 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6566 rb_enc_associate(str, rb_ascii8bit_encoding());
6570 return str;
6573 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6574 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6575 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6576 #define peek(p,c) peek_n(p, (c), 0)
6577 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6578 #define peekc(p) peekc_n(p, 0)
6579 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6581 #ifdef RIPPER
6582 static void
6583 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6585 if (tok < end) {
6586 if (!has_delayed_token(p)) {
6587 p->delayed.token = rb_str_buf_new(end - tok);
6588 rb_enc_associate(p->delayed.token, p->enc);
6589 p->delayed.line = p->ruby_sourceline;
6590 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6592 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6593 p->lex.ptok = end;
6596 #else
6597 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6598 #endif
6600 static int
6601 nextline(struct parser_params *p)
6603 VALUE v = p->lex.nextline;
6604 p->lex.nextline = 0;
6605 if (!v) {
6606 if (p->eofp)
6607 return -1;
6609 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6610 goto end_of_input;
6613 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6614 end_of_input:
6615 p->eofp = 1;
6616 lex_goto_eol(p);
6617 return -1;
6619 p->cr_seen = FALSE;
6621 else if (NIL_P(v)) {
6622 /* after here-document without terminator */
6623 goto end_of_input;
6625 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6626 if (p->heredoc_end > 0) {
6627 p->ruby_sourceline = p->heredoc_end;
6628 p->heredoc_end = 0;
6630 p->ruby_sourceline++;
6631 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6632 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6633 token_flush(p);
6634 p->lex.prevline = p->lex.lastline;
6635 p->lex.lastline = v;
6636 return 0;
6639 static int
6640 parser_cr(struct parser_params *p, int c)
6642 if (peek(p, '\n')) {
6643 p->lex.pcur++;
6644 c = '\n';
6646 return c;
6649 static inline int
6650 nextc(struct parser_params *p)
6652 int c;
6654 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6655 if (nextline(p)) return -1;
6657 c = (unsigned char)*p->lex.pcur++;
6658 if (UNLIKELY(c == '\r')) {
6659 c = parser_cr(p, c);
6662 return c;
6665 static void
6666 pushback(struct parser_params *p, int c)
6668 if (c == -1) return;
6669 p->lex.pcur--;
6670 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6671 p->lex.pcur--;
6675 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6677 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6678 #define tok(p) (p)->tokenbuf
6679 #define toklen(p) (p)->tokidx
6681 static int
6682 looking_at_eol_p(struct parser_params *p)
6684 const char *ptr = p->lex.pcur;
6685 while (ptr < p->lex.pend) {
6686 int c = (unsigned char)*ptr++;
6687 int eol = (c == '\n' || c == '#');
6688 if (eol || !ISSPACE(c)) {
6689 return eol;
6692 return TRUE;
6695 static char*
6696 newtok(struct parser_params *p)
6698 p->tokidx = 0;
6699 p->tokline = p->ruby_sourceline;
6700 if (!p->tokenbuf) {
6701 p->toksiz = 60;
6702 p->tokenbuf = ALLOC_N(char, 60);
6704 if (p->toksiz > 4096) {
6705 p->toksiz = 60;
6706 REALLOC_N(p->tokenbuf, char, 60);
6708 return p->tokenbuf;
6711 static char *
6712 tokspace(struct parser_params *p, int n)
6714 p->tokidx += n;
6716 if (p->tokidx >= p->toksiz) {
6717 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6718 REALLOC_N(p->tokenbuf, char, p->toksiz);
6720 return &p->tokenbuf[p->tokidx-n];
6723 static void
6724 tokadd(struct parser_params *p, int c)
6726 p->tokenbuf[p->tokidx++] = (char)c;
6727 if (p->tokidx >= p->toksiz) {
6728 p->toksiz *= 2;
6729 REALLOC_N(p->tokenbuf, char, p->toksiz);
6733 static int
6734 tok_hex(struct parser_params *p, size_t *numlen)
6736 int c;
6738 c = scan_hex(p->lex.pcur, 2, numlen);
6739 if (!*numlen) {
6740 yyerror0("invalid hex escape");
6741 token_flush(p);
6742 return 0;
6744 p->lex.pcur += *numlen;
6745 return c;
6748 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6750 static int
6751 escaped_control_code(int c)
6753 int c2 = 0;
6754 switch (c) {
6755 case ' ':
6756 c2 = 's';
6757 break;
6758 case '\n':
6759 c2 = 'n';
6760 break;
6761 case '\t':
6762 c2 = 't';
6763 break;
6764 case '\v':
6765 c2 = 'v';
6766 break;
6767 case '\r':
6768 c2 = 'r';
6769 break;
6770 case '\f':
6771 c2 = 'f';
6772 break;
6774 return c2;
6777 #define WARN_SPACE_CHAR(c, prefix) \
6778 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6780 static int
6781 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6782 int regexp_literal, int wide)
6784 size_t numlen;
6785 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6786 literal_flush(p, p->lex.pcur);
6787 p->lex.pcur += numlen;
6788 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6789 yyerror0("invalid Unicode escape");
6790 return wide && numlen > 0;
6792 if (codepoint > 0x10ffff) {
6793 yyerror0("invalid Unicode codepoint (too large)");
6794 return wide;
6796 if ((codepoint & 0xfffff800) == 0xd800) {
6797 yyerror0("invalid Unicode codepoint");
6798 return wide;
6800 if (regexp_literal) {
6801 tokcopy(p, (int)numlen);
6803 else if (codepoint >= 0x80) {
6804 rb_encoding *utf8 = rb_utf8_encoding();
6805 if (*encp && utf8 != *encp) {
6806 YYLTYPE loc = RUBY_INIT_YYLLOC();
6807 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6808 parser_show_error_line(p, &loc);
6809 return wide;
6811 *encp = utf8;
6812 tokaddmbc(p, codepoint, *encp);
6814 else {
6815 tokadd(p, codepoint);
6817 return TRUE;
6820 /* return value is for ?\u3042 */
6821 static void
6822 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6823 int term, int symbol_literal, int regexp_literal)
6826 * If `term` is not -1, then we allow multiple codepoints in \u{}
6827 * upto `term` byte, otherwise we're parsing a character literal.
6828 * And then add the codepoints to the current token.
6830 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6832 const int open_brace = '{', close_brace = '}';
6834 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6836 if (peek(p, open_brace)) { /* handle \u{...} form */
6837 const char *second = NULL;
6838 int c, last = nextc(p);
6839 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6840 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6841 while (c != close_brace) {
6842 if (c == term) goto unterminated;
6843 if (second == multiple_codepoints)
6844 second = p->lex.pcur;
6845 if (regexp_literal) tokadd(p, last);
6846 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6847 break;
6849 while (ISSPACE(c = *p->lex.pcur)) {
6850 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6851 last = c;
6853 if (term == -1 && !second)
6854 second = multiple_codepoints;
6857 if (c != close_brace) {
6858 unterminated:
6859 token_flush(p);
6860 yyerror0("unterminated Unicode escape");
6861 return;
6863 if (second && second != multiple_codepoints) {
6864 const char *pcur = p->lex.pcur;
6865 p->lex.pcur = second;
6866 dispatch_scan_event(p, tSTRING_CONTENT);
6867 token_flush(p);
6868 p->lex.pcur = pcur;
6869 yyerror0(multiple_codepoints);
6870 token_flush(p);
6873 if (regexp_literal) tokadd(p, close_brace);
6874 nextc(p);
6876 else { /* handle \uxxxx form */
6877 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6878 token_flush(p);
6879 return;
6884 #define ESCAPE_CONTROL 1
6885 #define ESCAPE_META 2
6887 static int
6888 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6890 int c;
6891 size_t numlen;
6893 switch (c = nextc(p)) {
6894 case '\\': /* Backslash */
6895 return c;
6897 case 'n': /* newline */
6898 return '\n';
6900 case 't': /* horizontal tab */
6901 return '\t';
6903 case 'r': /* carriage-return */
6904 return '\r';
6906 case 'f': /* form-feed */
6907 return '\f';
6909 case 'v': /* vertical tab */
6910 return '\13';
6912 case 'a': /* alarm(bell) */
6913 return '\007';
6915 case 'e': /* escape */
6916 return 033;
6918 case '0': case '1': case '2': case '3': /* octal constant */
6919 case '4': case '5': case '6': case '7':
6920 pushback(p, c);
6921 c = scan_oct(p->lex.pcur, 3, &numlen);
6922 p->lex.pcur += numlen;
6923 return c;
6925 case 'x': /* hex constant */
6926 c = tok_hex(p, &numlen);
6927 if (numlen == 0) return 0;
6928 return c;
6930 case 'b': /* backspace */
6931 return '\010';
6933 case 's': /* space */
6934 return ' ';
6936 case 'M':
6937 if (flags & ESCAPE_META) goto eof;
6938 if ((c = nextc(p)) != '-') {
6939 goto eof;
6941 if ((c = nextc(p)) == '\\') {
6942 switch (peekc(p)) {
6943 case 'u': case 'U':
6944 nextc(p);
6945 goto eof;
6947 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6949 else if (c == -1 || !ISASCII(c)) goto eof;
6950 else {
6951 int c2 = escaped_control_code(c);
6952 if (c2) {
6953 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6954 WARN_SPACE_CHAR(c2, "\\M-");
6956 else {
6957 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6960 else if (ISCNTRL(c)) goto eof;
6961 return ((c & 0xff) | 0x80);
6964 case 'C':
6965 if ((c = nextc(p)) != '-') {
6966 goto eof;
6968 case 'c':
6969 if (flags & ESCAPE_CONTROL) goto eof;
6970 if ((c = nextc(p))== '\\') {
6971 switch (peekc(p)) {
6972 case 'u': case 'U':
6973 nextc(p);
6974 goto eof;
6976 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6978 else if (c == '?')
6979 return 0177;
6980 else if (c == -1 || !ISASCII(c)) goto eof;
6981 else {
6982 int c2 = escaped_control_code(c);
6983 if (c2) {
6984 if (ISCNTRL(c)) {
6985 if (flags & ESCAPE_META) {
6986 WARN_SPACE_CHAR(c2, "\\M-");
6988 else {
6989 WARN_SPACE_CHAR(c2, "");
6992 else {
6993 if (flags & ESCAPE_META) {
6994 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6996 else {
6997 WARN_SPACE_CHAR(c2, "\\C-");
7001 else if (ISCNTRL(c)) goto eof;
7003 return c & 0x9f;
7005 eof:
7006 case -1:
7007 yyerror0("Invalid escape character syntax");
7008 token_flush(p);
7009 return '\0';
7011 default:
7012 return c;
7016 static void
7017 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7019 int len = rb_enc_codelen(c, enc);
7020 rb_enc_mbcput(c, tokspace(p, len), enc);
7023 static int
7024 tokadd_escape(struct parser_params *p, rb_encoding **encp)
7026 int c;
7027 size_t numlen;
7029 switch (c = nextc(p)) {
7030 case '\n':
7031 return 0; /* just ignore */
7033 case '0': case '1': case '2': case '3': /* octal constant */
7034 case '4': case '5': case '6': case '7':
7036 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7037 if (numlen == 0) goto eof;
7038 p->lex.pcur += numlen;
7039 tokcopy(p, (int)numlen + 1);
7041 return 0;
7043 case 'x': /* hex constant */
7045 tok_hex(p, &numlen);
7046 if (numlen == 0) return -1;
7047 tokcopy(p, (int)numlen + 2);
7049 return 0;
7051 eof:
7052 case -1:
7053 yyerror0("Invalid escape character syntax");
7054 token_flush(p);
7055 return -1;
7057 default:
7058 tokadd(p, '\\');
7059 tokadd(p, c);
7061 return 0;
7064 static int
7065 regx_options(struct parser_params *p)
7067 int kcode = 0;
7068 int kopt = 0;
7069 int options = 0;
7070 int c, opt, kc;
7072 newtok(p);
7073 while (c = nextc(p), ISALPHA(c)) {
7074 if (c == 'o') {
7075 options |= RE_OPTION_ONCE;
7077 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7078 if (kc >= 0) {
7079 if (kc != rb_ascii8bit_encindex()) kcode = c;
7080 kopt = opt;
7082 else {
7083 options |= opt;
7086 else {
7087 tokadd(p, c);
7090 options |= kopt;
7091 pushback(p, c);
7092 if (toklen(p)) {
7093 YYLTYPE loc = RUBY_INIT_YYLLOC();
7094 tokfix(p);
7095 compile_error(p, "unknown regexp option%s - %*s",
7096 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7097 parser_show_error_line(p, &loc);
7099 return options | RE_OPTION_ENCODING(kcode);
7102 static int
7103 tokadd_mbchar(struct parser_params *p, int c)
7105 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7106 if (len < 0) return -1;
7107 tokadd(p, c);
7108 p->lex.pcur += --len;
7109 if (len > 0) tokcopy(p, len);
7110 return c;
7113 static inline int
7114 simple_re_meta(int c)
7116 switch (c) {
7117 case '$': case '*': case '+': case '.':
7118 case '?': case '^': case '|':
7119 case ')': case ']': case '}': case '>':
7120 return TRUE;
7121 default:
7122 return FALSE;
7126 static int
7127 parser_update_heredoc_indent(struct parser_params *p, int c)
7129 if (p->heredoc_line_indent == -1) {
7130 if (c == '\n') p->heredoc_line_indent = 0;
7132 else {
7133 if (c == ' ') {
7134 p->heredoc_line_indent++;
7135 return TRUE;
7137 else if (c == '\t') {
7138 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7139 p->heredoc_line_indent = w * TAB_WIDTH;
7140 return TRUE;
7142 else if (c != '\n') {
7143 if (p->heredoc_indent > p->heredoc_line_indent) {
7144 p->heredoc_indent = p->heredoc_line_indent;
7146 p->heredoc_line_indent = -1;
7149 return FALSE;
7152 static void
7153 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7155 YYLTYPE loc = RUBY_INIT_YYLLOC();
7156 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7157 compile_error(p, "%s mixed within %s source", n1, n2);
7158 parser_show_error_line(p, &loc);
7161 static void
7162 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7164 const char *pos = p->lex.pcur;
7165 p->lex.pcur = beg;
7166 parser_mixed_error(p, enc1, enc2);
7167 p->lex.pcur = pos;
7170 static int
7171 tokadd_string(struct parser_params *p,
7172 int func, int term, int paren, long *nest,
7173 rb_encoding **encp, rb_encoding **enc)
7175 int c;
7176 bool erred = false;
7178 #define mixed_error(enc1, enc2) \
7179 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7180 #define mixed_escape(beg, enc1, enc2) \
7181 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7183 while ((c = nextc(p)) != -1) {
7184 if (p->heredoc_indent > 0) {
7185 parser_update_heredoc_indent(p, c);
7188 if (paren && c == paren) {
7189 ++*nest;
7191 else if (c == term) {
7192 if (!nest || !*nest) {
7193 pushback(p, c);
7194 break;
7196 --*nest;
7198 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7199 int c2 = *p->lex.pcur;
7200 if (c2 == '$' || c2 == '@' || c2 == '{') {
7201 pushback(p, c);
7202 break;
7205 else if (c == '\\') {
7206 literal_flush(p, p->lex.pcur - 1);
7207 c = nextc(p);
7208 switch (c) {
7209 case '\n':
7210 if (func & STR_FUNC_QWORDS) break;
7211 if (func & STR_FUNC_EXPAND) {
7212 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7213 continue;
7214 if (c == term) {
7215 c = '\\';
7216 goto terminate;
7219 tokadd(p, '\\');
7220 break;
7222 case '\\':
7223 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7224 break;
7226 case 'u':
7227 if ((func & STR_FUNC_EXPAND) == 0) {
7228 tokadd(p, '\\');
7229 break;
7231 tokadd_utf8(p, enc, term,
7232 func & STR_FUNC_SYMBOL,
7233 func & STR_FUNC_REGEXP);
7234 continue;
7236 default:
7237 if (c == -1) return -1;
7238 if (!ISASCII(c)) {
7239 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7240 goto non_ascii;
7242 if (func & STR_FUNC_REGEXP) {
7243 switch (c) {
7244 case 'c':
7245 case 'C':
7246 case 'M': {
7247 pushback(p, c);
7248 c = read_escape(p, 0, enc);
7250 int i;
7251 char escbuf[5];
7252 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7253 for (i = 0; i < 4; i++) {
7254 tokadd(p, escbuf[i]);
7256 continue;
7260 if (c == term && !simple_re_meta(c)) {
7261 tokadd(p, c);
7262 continue;
7264 pushback(p, c);
7265 if ((c = tokadd_escape(p, enc)) < 0)
7266 return -1;
7267 if (*enc && *enc != *encp) {
7268 mixed_escape(p->lex.ptok+2, *enc, *encp);
7270 continue;
7272 else if (func & STR_FUNC_EXPAND) {
7273 pushback(p, c);
7274 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7275 c = read_escape(p, 0, enc);
7277 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7278 /* ignore backslashed spaces in %w */
7280 else if (c != term && !(paren && c == paren)) {
7281 tokadd(p, '\\');
7282 pushback(p, c);
7283 continue;
7287 else if (!parser_isascii(p)) {
7288 non_ascii:
7289 if (!*enc) {
7290 *enc = *encp;
7292 else if (*enc != *encp) {
7293 mixed_error(*enc, *encp);
7294 continue;
7296 if (tokadd_mbchar(p, c) == -1) return -1;
7297 continue;
7299 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7300 pushback(p, c);
7301 break;
7303 if (c & 0x80) {
7304 if (!*enc) {
7305 *enc = *encp;
7307 else if (*enc != *encp) {
7308 mixed_error(*enc, *encp);
7309 continue;
7312 tokadd(p, c);
7314 terminate:
7315 if (*enc) *encp = *enc;
7316 return c;
7319 static inline rb_strterm_t *
7320 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7322 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7325 /* imemo_parser_strterm for literal */
7326 #define NEW_STRTERM(func, term, paren) \
7327 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7329 #ifdef RIPPER
7330 static void
7331 flush_string_content(struct parser_params *p, rb_encoding *enc)
7333 VALUE content = yylval.val;
7334 if (!ripper_is_node_yylval(content))
7335 content = ripper_new_yylval(p, 0, 0, content);
7336 if (has_delayed_token(p)) {
7337 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7338 if (len > 0) {
7339 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7341 dispatch_delayed_token(p, tSTRING_CONTENT);
7342 p->lex.ptok = p->lex.pcur;
7343 RNODE(content)->nd_rval = yylval.val;
7345 dispatch_scan_event(p, tSTRING_CONTENT);
7346 if (yylval.val != content)
7347 RNODE(content)->nd_rval = yylval.val;
7348 yylval.val = content;
7350 #else
7351 #define flush_string_content(p, enc) ((void)(enc))
7352 #endif
7354 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7355 /* this can be shared with ripper, since it's independent from struct
7356 * parser_params. */
7357 #ifndef RIPPER
7358 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7359 #define SPECIAL_PUNCT(idx) ( \
7360 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7361 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7362 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7363 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7364 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7365 BIT('0', idx))
7366 const unsigned int ruby_global_name_punct_bits[] = {
7367 SPECIAL_PUNCT(0),
7368 SPECIAL_PUNCT(1),
7369 SPECIAL_PUNCT(2),
7371 #undef BIT
7372 #undef SPECIAL_PUNCT
7373 #endif
7375 static enum yytokentype
7376 parser_peek_variable_name(struct parser_params *p)
7378 int c;
7379 const char *ptr = p->lex.pcur;
7381 if (ptr + 1 >= p->lex.pend) return 0;
7382 c = *ptr++;
7383 switch (c) {
7384 case '$':
7385 if ((c = *ptr) == '-') {
7386 if (++ptr >= p->lex.pend) return 0;
7387 c = *ptr;
7389 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7390 return tSTRING_DVAR;
7392 break;
7393 case '@':
7394 if ((c = *ptr) == '@') {
7395 if (++ptr >= p->lex.pend) return 0;
7396 c = *ptr;
7398 break;
7399 case '{':
7400 p->lex.pcur = ptr;
7401 p->command_start = TRUE;
7402 return tSTRING_DBEG;
7403 default:
7404 return 0;
7406 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7407 return tSTRING_DVAR;
7408 return 0;
7411 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7412 #define IS_END() IS_lex_state(EXPR_END_ANY)
7413 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7414 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7415 #define IS_LABEL_POSSIBLE() (\
7416 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7417 IS_ARG())
7418 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7419 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7421 static inline enum yytokentype
7422 parser_string_term(struct parser_params *p, int func)
7424 p->lex.strterm = 0;
7425 if (func & STR_FUNC_REGEXP) {
7426 set_yylval_num(regx_options(p));
7427 dispatch_scan_event(p, tREGEXP_END);
7428 SET_LEX_STATE(EXPR_END);
7429 return tREGEXP_END;
7431 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7432 nextc(p);
7433 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7434 return tLABEL_END;
7436 SET_LEX_STATE(EXPR_END);
7437 return tSTRING_END;
7440 static enum yytokentype
7441 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7443 int func = (int)quote->u1.func;
7444 int term = (int)quote->u3.term;
7445 int paren = (int)quote->u2.paren;
7446 int c, space = 0;
7447 rb_encoding *enc = p->enc;
7448 rb_encoding *base_enc = 0;
7449 VALUE lit;
7451 if (func & STR_FUNC_TERM) {
7452 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7453 SET_LEX_STATE(EXPR_END);
7454 p->lex.strterm = 0;
7455 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7457 c = nextc(p);
7458 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7459 do {c = nextc(p);} while (ISSPACE(c));
7460 space = 1;
7462 if (func & STR_FUNC_LIST) {
7463 quote->u1.func &= ~STR_FUNC_LIST;
7464 space = 1;
7466 if (c == term && !quote->u0.nest) {
7467 if (func & STR_FUNC_QWORDS) {
7468 quote->u1.func |= STR_FUNC_TERM;
7469 pushback(p, c); /* dispatch the term at tSTRING_END */
7470 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7471 return ' ';
7473 return parser_string_term(p, func);
7475 if (space) {
7476 pushback(p, c);
7477 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7478 return ' ';
7480 newtok(p);
7481 if ((func & STR_FUNC_EXPAND) && c == '#') {
7482 int t = parser_peek_variable_name(p);
7483 if (t) return t;
7484 tokadd(p, '#');
7485 c = nextc(p);
7487 pushback(p, c);
7488 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7489 &enc, &base_enc) == -1) {
7490 if (p->eofp) {
7491 #ifndef RIPPER
7492 # define unterminated_literal(mesg) yyerror0(mesg)
7493 #else
7494 # define unterminated_literal(mesg) compile_error(p, mesg)
7495 #endif
7496 literal_flush(p, p->lex.pcur);
7497 if (func & STR_FUNC_QWORDS) {
7498 /* no content to add, bailing out here */
7499 unterminated_literal("unterminated list meets end of file");
7500 p->lex.strterm = 0;
7501 return tSTRING_END;
7503 if (func & STR_FUNC_REGEXP) {
7504 unterminated_literal("unterminated regexp meets end of file");
7506 else {
7507 unterminated_literal("unterminated string meets end of file");
7509 quote->u1.func |= STR_FUNC_TERM;
7513 tokfix(p);
7514 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7515 set_yylval_str(lit);
7516 flush_string_content(p, enc);
7518 return tSTRING_CONTENT;
7521 static enum yytokentype
7522 heredoc_identifier(struct parser_params *p)
7525 * term_len is length of `<<"END"` except `END`,
7526 * in this case term_len is 4 (<, <, " and ").
7528 long len, offset = p->lex.pcur - p->lex.pbeg;
7529 int c = nextc(p), term, func = 0, quote = 0;
7530 enum yytokentype token = tSTRING_BEG;
7531 int indent = 0;
7533 if (c == '-') {
7534 c = nextc(p);
7535 func = STR_FUNC_INDENT;
7536 offset++;
7538 else if (c == '~') {
7539 c = nextc(p);
7540 func = STR_FUNC_INDENT;
7541 offset++;
7542 indent = INT_MAX;
7544 switch (c) {
7545 case '\'':
7546 func |= str_squote; goto quoted;
7547 case '"':
7548 func |= str_dquote; goto quoted;
7549 case '`':
7550 token = tXSTRING_BEG;
7551 func |= str_xquote; goto quoted;
7553 quoted:
7554 quote++;
7555 offset++;
7556 term = c;
7557 len = 0;
7558 while ((c = nextc(p)) != term) {
7559 if (c == -1 || c == '\r' || c == '\n') {
7560 yyerror0("unterminated here document identifier");
7561 return -1;
7564 break;
7566 default:
7567 if (!parser_is_identchar(p)) {
7568 pushback(p, c);
7569 if (func & STR_FUNC_INDENT) {
7570 pushback(p, indent > 0 ? '~' : '-');
7572 return 0;
7574 func |= str_dquote;
7575 do {
7576 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7577 if (n < 0) return 0;
7578 p->lex.pcur += --n;
7579 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7580 pushback(p, c);
7581 break;
7584 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7585 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7586 yyerror0("too long here document identifier");
7587 dispatch_scan_event(p, tHEREDOC_BEG);
7588 lex_goto_eol(p);
7590 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7591 p->lex.strterm->flags |= STRTERM_HEREDOC;
7592 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7593 here->offset = offset;
7594 here->sourceline = p->ruby_sourceline;
7595 here->length = (int)len;
7596 here->quote = quote;
7597 here->func = func;
7599 token_flush(p);
7600 p->heredoc_indent = indent;
7601 p->heredoc_line_indent = 0;
7602 return token;
7605 static void
7606 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7608 VALUE line;
7610 p->lex.strterm = 0;
7611 line = here->lastline;
7612 p->lex.lastline = line;
7613 p->lex.pbeg = RSTRING_PTR(line);
7614 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7615 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7616 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7617 p->heredoc_end = p->ruby_sourceline;
7618 p->ruby_sourceline = (int)here->sourceline;
7619 if (p->eofp) p->lex.nextline = Qnil;
7620 p->eofp = 0;
7623 static int
7624 dedent_string(VALUE string, int width)
7626 char *str;
7627 long len;
7628 int i, col = 0;
7630 RSTRING_GETMEM(string, str, len);
7631 for (i = 0; i < len && col < width; i++) {
7632 if (str[i] == ' ') {
7633 col++;
7635 else if (str[i] == '\t') {
7636 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7637 if (n > width) break;
7638 col = n;
7640 else {
7641 break;
7644 if (!i) return 0;
7645 rb_str_modify(string);
7646 str = RSTRING_PTR(string);
7647 if (RSTRING_LEN(string) != len)
7648 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7649 MEMMOVE(str, str + i, char, len - i);
7650 rb_str_set_len(string, len - i);
7651 return i;
7654 #ifndef RIPPER
7655 static NODE *
7656 heredoc_dedent(struct parser_params *p, NODE *root)
7658 NODE *node, *str_node, *prev_node;
7659 int indent = p->heredoc_indent;
7660 VALUE prev_lit = 0;
7662 if (indent <= 0) return root;
7663 p->heredoc_indent = 0;
7664 if (!root) return root;
7666 prev_node = node = str_node = root;
7667 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
7669 while (str_node) {
7670 VALUE lit = str_node->nd_lit;
7671 if (str_node->flags & NODE_FL_NEWLINE) {
7672 dedent_string(lit, indent);
7674 if (!prev_lit) {
7675 prev_lit = lit;
7677 else if (!literal_concat0(p, prev_lit, lit)) {
7678 return 0;
7680 else {
7681 NODE *end = node->nd_end;
7682 node = prev_node->nd_next = node->nd_next;
7683 if (!node) {
7684 if (nd_type_p(prev_node, NODE_DSTR))
7685 nd_set_type(prev_node, NODE_STR);
7686 break;
7688 node->nd_end = end;
7689 goto next_str;
7692 str_node = 0;
7693 while ((node = (prev_node = node)->nd_next) != 0) {
7694 next_str:
7695 if (!nd_type_p(node, NODE_LIST)) break;
7696 if ((str_node = node->nd_head) != 0) {
7697 enum node_type type = nd_type(str_node);
7698 if (type == NODE_STR || type == NODE_DSTR) break;
7699 prev_lit = 0;
7700 str_node = 0;
7704 return root;
7706 #else /* RIPPER */
7707 static VALUE
7708 heredoc_dedent(struct parser_params *p, VALUE array)
7710 int indent = p->heredoc_indent;
7712 if (indent <= 0) return array;
7713 p->heredoc_indent = 0;
7714 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7715 return array;
7719 * call-seq:
7720 * Ripper.dedent_string(input, width) -> Integer
7722 * USE OF RIPPER LIBRARY ONLY.
7724 * Strips up to +width+ leading whitespaces from +input+,
7725 * and returns the stripped column width.
7727 static VALUE
7728 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7730 int wid, col;
7732 StringValue(input);
7733 wid = NUM2UINT(width);
7734 col = dedent_string(input, wid);
7735 return INT2NUM(col);
7737 #endif
7739 static int
7740 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7742 const char *ptr = p->lex.pbeg;
7743 long n;
7745 if (indent) {
7746 while (*ptr && ISSPACE(*ptr)) ptr++;
7748 n = p->lex.pend - (ptr + len);
7749 if (n < 0) return FALSE;
7750 if (n > 0 && ptr[len] != '\n') {
7751 if (ptr[len] != '\r') return FALSE;
7752 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7754 return strncmp(eos, ptr, len) == 0;
7757 static int
7758 word_match_p(struct parser_params *p, const char *word, long len)
7760 if (strncmp(p->lex.pcur, word, len)) return 0;
7761 if (p->lex.pcur + len == p->lex.pend) return 1;
7762 int c = (unsigned char)p->lex.pcur[len];
7763 if (ISSPACE(c)) return 1;
7764 switch (c) {
7765 case '\0': case '\004': case '\032': return 1;
7767 return 0;
7770 #define NUM_SUFFIX_R (1<<0)
7771 #define NUM_SUFFIX_I (1<<1)
7772 #define NUM_SUFFIX_ALL 3
7774 static int
7775 number_literal_suffix(struct parser_params *p, int mask)
7777 int c, result = 0;
7778 const char *lastp = p->lex.pcur;
7780 while ((c = nextc(p)) != -1) {
7781 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7782 result |= (mask & NUM_SUFFIX_I);
7783 mask &= ~NUM_SUFFIX_I;
7784 /* r after i, rational of complex is disallowed */
7785 mask &= ~NUM_SUFFIX_R;
7786 continue;
7788 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7789 result |= (mask & NUM_SUFFIX_R);
7790 mask &= ~NUM_SUFFIX_R;
7791 continue;
7793 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7794 p->lex.pcur = lastp;
7795 literal_flush(p, p->lex.pcur);
7796 return 0;
7798 pushback(p, c);
7799 break;
7801 return result;
7804 static enum yytokentype
7805 set_number_literal(struct parser_params *p, VALUE v,
7806 enum yytokentype type, int suffix)
7808 if (suffix & NUM_SUFFIX_I) {
7809 v = rb_complex_raw(INT2FIX(0), v);
7810 type = tIMAGINARY;
7812 set_yylval_literal(v);
7813 SET_LEX_STATE(EXPR_END);
7814 return type;
7817 static enum yytokentype
7818 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7820 enum yytokentype type = tINTEGER;
7821 if (suffix & NUM_SUFFIX_R) {
7822 v = rb_rational_raw1(v);
7823 type = tRATIONAL;
7825 return set_number_literal(p, v, type, suffix);
7828 #ifdef RIPPER
7829 static void
7830 dispatch_heredoc_end(struct parser_params *p)
7832 VALUE str;
7833 if (has_delayed_token(p))
7834 dispatch_delayed_token(p, tSTRING_CONTENT);
7835 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7836 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7837 lex_goto_eol(p);
7838 token_flush(p);
7841 #else
7842 #define dispatch_heredoc_end(p) ((void)0)
7843 #endif
7845 static enum yytokentype
7846 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7848 int c, func, indent = 0;
7849 const char *eos, *ptr, *ptr_end;
7850 long len;
7851 VALUE str = 0;
7852 rb_encoding *enc = p->enc;
7853 rb_encoding *base_enc = 0;
7854 int bol;
7856 eos = RSTRING_PTR(here->lastline) + here->offset;
7857 len = here->length;
7858 indent = (func = here->func) & STR_FUNC_INDENT;
7860 if ((c = nextc(p)) == -1) {
7861 error:
7862 #ifdef RIPPER
7863 if (!has_delayed_token(p)) {
7864 dispatch_scan_event(p, tSTRING_CONTENT);
7866 else {
7867 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7868 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7869 int cr = ENC_CODERANGE_UNKNOWN;
7870 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7871 if (cr != ENC_CODERANGE_7BIT &&
7872 p->enc == rb_usascii_encoding() &&
7873 enc != rb_utf8_encoding()) {
7874 enc = rb_ascii8bit_encoding();
7877 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7879 dispatch_delayed_token(p, tSTRING_CONTENT);
7881 lex_goto_eol(p);
7882 #endif
7883 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7884 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7885 (int)len, eos);
7886 token_flush(p);
7887 p->lex.strterm = 0;
7888 SET_LEX_STATE(EXPR_END);
7889 return tSTRING_END;
7891 bol = was_bol(p);
7892 if (!bol) {
7893 /* not beginning of line, cannot be the terminator */
7895 else if (p->heredoc_line_indent == -1) {
7896 /* `heredoc_line_indent == -1` means
7897 * - "after an interpolation in the same line", or
7898 * - "in a continuing line"
7900 p->heredoc_line_indent = 0;
7902 else if (whole_match_p(p, eos, len, indent)) {
7903 dispatch_heredoc_end(p);
7904 restore:
7905 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7906 token_flush(p);
7907 p->lex.strterm = 0;
7908 SET_LEX_STATE(EXPR_END);
7909 return tSTRING_END;
7912 if (!(func & STR_FUNC_EXPAND)) {
7913 do {
7914 ptr = RSTRING_PTR(p->lex.lastline);
7915 ptr_end = p->lex.pend;
7916 if (ptr_end > ptr) {
7917 switch (ptr_end[-1]) {
7918 case '\n':
7919 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7920 ptr_end++;
7921 break;
7923 case '\r':
7924 --ptr_end;
7928 if (p->heredoc_indent > 0) {
7929 long i = 0;
7930 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7931 i++;
7932 p->heredoc_line_indent = 0;
7935 if (str)
7936 rb_str_cat(str, ptr, ptr_end - ptr);
7937 else
7938 str = STR_NEW(ptr, ptr_end - ptr);
7939 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7940 lex_goto_eol(p);
7941 if (p->heredoc_indent > 0) {
7942 goto flush_str;
7944 if (nextc(p) == -1) {
7945 if (str) {
7946 str = 0;
7948 goto error;
7950 } while (!whole_match_p(p, eos, len, indent));
7952 else {
7953 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7954 newtok(p);
7955 if (c == '#') {
7956 int t = parser_peek_variable_name(p);
7957 if (p->heredoc_line_indent != -1) {
7958 if (p->heredoc_indent > p->heredoc_line_indent) {
7959 p->heredoc_indent = p->heredoc_line_indent;
7961 p->heredoc_line_indent = -1;
7963 if (t) return t;
7964 tokadd(p, '#');
7965 c = nextc(p);
7967 do {
7968 pushback(p, c);
7969 enc = p->enc;
7970 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7971 if (p->eofp) goto error;
7972 goto restore;
7974 if (c != '\n') {
7975 if (c == '\\') p->heredoc_line_indent = -1;
7976 flush:
7977 str = STR_NEW3(tok(p), toklen(p), enc, func);
7978 flush_str:
7979 set_yylval_str(str);
7980 #ifndef RIPPER
7981 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7982 #endif
7983 flush_string_content(p, enc);
7984 return tSTRING_CONTENT;
7986 tokadd(p, nextc(p));
7987 if (p->heredoc_indent > 0) {
7988 lex_goto_eol(p);
7989 goto flush;
7991 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7992 if ((c = nextc(p)) == -1) goto error;
7993 } while (!whole_match_p(p, eos, len, indent));
7994 str = STR_NEW3(tok(p), toklen(p), enc, func);
7996 dispatch_heredoc_end(p);
7997 #ifdef RIPPER
7998 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7999 yylval.val, str);
8000 #endif
8001 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8002 token_flush(p);
8003 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8004 set_yylval_str(str);
8005 #ifndef RIPPER
8006 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8007 #endif
8008 return tSTRING_CONTENT;
8011 #include "lex.c"
8013 static int
8014 arg_ambiguous(struct parser_params *p, char c)
8016 #ifndef RIPPER
8017 if (c == '/') {
8018 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8020 else {
8021 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8023 #else
8024 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8025 #endif
8026 return TRUE;
8029 static ID
8030 #ifndef RIPPER
8031 formal_argument(struct parser_params *p, ID lhs)
8032 #else
8033 formal_argument(struct parser_params *p, VALUE lhs)
8034 #endif
8036 ID id = get_id(lhs);
8038 switch (id_type(id)) {
8039 case ID_LOCAL:
8040 break;
8041 #ifndef RIPPER
8042 # define ERR(mesg) yyerror0(mesg)
8043 #else
8044 # define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8045 #endif
8046 case ID_CONST:
8047 ERR("formal argument cannot be a constant");
8048 return 0;
8049 case ID_INSTANCE:
8050 ERR("formal argument cannot be an instance variable");
8051 return 0;
8052 case ID_GLOBAL:
8053 ERR("formal argument cannot be a global variable");
8054 return 0;
8055 case ID_CLASS:
8056 ERR("formal argument cannot be a class variable");
8057 return 0;
8058 default:
8059 ERR("formal argument must be local variable");
8060 return 0;
8061 #undef ERR
8063 shadowing_lvar(p, id);
8064 return lhs;
8067 static int
8068 lvar_defined(struct parser_params *p, ID id)
8070 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8073 /* emacsen -*- hack */
8074 static long
8075 parser_encode_length(struct parser_params *p, const char *name, long len)
8077 long nlen;
8079 if (len > 5 && name[nlen = len - 5] == '-') {
8080 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8081 return nlen;
8083 if (len > 4 && name[nlen = len - 4] == '-') {
8084 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8085 return nlen;
8086 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8087 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8088 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8089 return nlen;
8091 return len;
8094 static void
8095 parser_set_encode(struct parser_params *p, const char *name)
8097 int idx = rb_enc_find_index(name);
8098 rb_encoding *enc;
8099 VALUE excargs[3];
8101 if (idx < 0) {
8102 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8103 error:
8104 excargs[0] = rb_eArgError;
8105 excargs[2] = rb_make_backtrace();
8106 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8107 rb_exc_raise(rb_make_exception(3, excargs));
8109 enc = rb_enc_from_index(idx);
8110 if (!rb_enc_asciicompat(enc)) {
8111 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8112 goto error;
8114 p->enc = enc;
8115 #ifndef RIPPER
8116 if (p->debug_lines) {
8117 VALUE lines = p->debug_lines;
8118 long i, n = RARRAY_LEN(lines);
8119 for (i = 0; i < n; ++i) {
8120 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8123 #endif
8126 static int
8127 comment_at_top(struct parser_params *p)
8129 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8130 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8131 while (ptr < ptr_end) {
8132 if (!ISSPACE(*ptr)) return 0;
8133 ptr++;
8135 return 1;
8138 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8139 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8141 static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8143 static void
8144 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8146 if (!comment_at_top(p)) {
8147 return;
8149 parser_set_encode(p, val);
8152 static int
8153 parser_get_bool(struct parser_params *p, const char *name, const char *val)
8155 switch (*val) {
8156 case 't': case 'T':
8157 if (STRCASECMP(val, "true") == 0) {
8158 return TRUE;
8160 break;
8161 case 'f': case 'F':
8162 if (STRCASECMP(val, "false") == 0) {
8163 return FALSE;
8165 break;
8167 return parser_invalid_pragma_value(p, name, val);
8170 static int
8171 parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8173 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8174 return -1;
8177 static void
8178 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8180 int b = parser_get_bool(p, name, val);
8181 if (b >= 0) p->token_info_enabled = b;
8184 static void
8185 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8187 int b;
8189 if (p->token_seen) {
8190 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8191 return;
8194 b = parser_get_bool(p, name, val);
8195 if (b < 0) return;
8197 if (!p->compile_option)
8198 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8199 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8200 RBOOL(b));
8203 static void
8204 parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8206 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8207 if (*s == ' ' || *s == '\t') continue;
8208 if (*s == '#') break;
8209 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8210 return;
8213 switch (*val) {
8214 case 'n': case 'N':
8215 if (STRCASECMP(val, "none") == 0) {
8216 p->ctxt.shareable_constant_value = shareable_none;
8217 return;
8219 break;
8220 case 'l': case 'L':
8221 if (STRCASECMP(val, "literal") == 0) {
8222 p->ctxt.shareable_constant_value = shareable_literal;
8223 return;
8225 break;
8226 case 'e': case 'E':
8227 if (STRCASECMP(val, "experimental_copy") == 0) {
8228 p->ctxt.shareable_constant_value = shareable_copy;
8229 return;
8231 if (STRCASECMP(val, "experimental_everything") == 0) {
8232 p->ctxt.shareable_constant_value = shareable_everything;
8233 return;
8235 break;
8237 parser_invalid_pragma_value(p, name, val);
8240 # if WARN_PAST_SCOPE
8241 static void
8242 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8244 int b = parser_get_bool(p, name, val);
8245 if (b >= 0) p->past_scope_enabled = b;
8247 # endif
8249 struct magic_comment {
8250 const char *name;
8251 rb_magic_comment_setter_t func;
8252 rb_magic_comment_length_t length;
8255 static const struct magic_comment magic_comments[] = {
8256 {"coding", magic_comment_encoding, parser_encode_length},
8257 {"encoding", magic_comment_encoding, parser_encode_length},
8258 {"frozen_string_literal", parser_set_compile_option_flag},
8259 {"shareable_constant_value", parser_set_shareable_constant_value},
8260 {"warn_indent", parser_set_token_info},
8261 # if WARN_PAST_SCOPE
8262 {"warn_past_scope", parser_set_past_scope},
8263 # endif
8266 static const char *
8267 magic_comment_marker(const char *str, long len)
8269 long i = 2;
8271 while (i < len) {
8272 switch (str[i]) {
8273 case '-':
8274 if (str[i-1] == '*' && str[i-2] == '-') {
8275 return str + i + 1;
8277 i += 2;
8278 break;
8279 case '*':
8280 if (i + 1 >= len) return 0;
8281 if (str[i+1] != '-') {
8282 i += 4;
8284 else if (str[i-1] != '-') {
8285 i += 2;
8287 else {
8288 return str + i + 2;
8290 break;
8291 default:
8292 i += 3;
8293 break;
8296 return 0;
8299 static int
8300 parser_magic_comment(struct parser_params *p, const char *str, long len)
8302 int indicator = 0;
8303 VALUE name = 0, val = 0;
8304 const char *beg, *end, *vbeg, *vend;
8305 #define str_copy(_s, _p, _n) ((_s) \
8306 ? (void)(rb_str_resize((_s), (_n)), \
8307 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8308 : (void)((_s) = STR_NEW((_p), (_n))))
8310 if (len <= 7) return FALSE;
8311 if (!!(beg = magic_comment_marker(str, len))) {
8312 if (!(end = magic_comment_marker(beg, str + len - beg)))
8313 return FALSE;
8314 indicator = TRUE;
8315 str = beg;
8316 len = end - beg - 3;
8319 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8320 while (len > 0) {
8321 const struct magic_comment *mc = magic_comments;
8322 char *s;
8323 int i;
8324 long n = 0;
8326 for (; len > 0 && *str; str++, --len) {
8327 switch (*str) {
8328 case '\'': case '"': case ':': case ';':
8329 continue;
8331 if (!ISSPACE(*str)) break;
8333 for (beg = str; len > 0; str++, --len) {
8334 switch (*str) {
8335 case '\'': case '"': case ':': case ';':
8336 break;
8337 default:
8338 if (ISSPACE(*str)) break;
8339 continue;
8341 break;
8343 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8344 if (!len) break;
8345 if (*str != ':') {
8346 if (!indicator) return FALSE;
8347 continue;
8350 do str++; while (--len > 0 && ISSPACE(*str));
8351 if (!len) break;
8352 if (*str == '"') {
8353 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8354 if (*str == '\\') {
8355 --len;
8356 ++str;
8359 vend = str;
8360 if (len) {
8361 --len;
8362 ++str;
8365 else {
8366 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8367 vend = str;
8369 if (indicator) {
8370 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8372 else {
8373 while (len > 0 && (ISSPACE(*str))) --len, str++;
8374 if (len) return FALSE;
8377 n = end - beg;
8378 str_copy(name, beg, n);
8379 s = RSTRING_PTR(name);
8380 for (i = 0; i < n; ++i) {
8381 if (s[i] == '-') s[i] = '_';
8383 do {
8384 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8385 n = vend - vbeg;
8386 if (mc->length) {
8387 n = (*mc->length)(p, vbeg, n);
8389 str_copy(val, vbeg, n);
8390 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8391 break;
8393 } while (++mc < magic_comments + numberof(magic_comments));
8394 #ifdef RIPPER
8395 str_copy(val, vbeg, vend - vbeg);
8396 dispatch2(magic_comment, name, val);
8397 #endif
8400 return TRUE;
8403 static void
8404 set_file_encoding(struct parser_params *p, const char *str, const char *send)
8406 int sep = 0;
8407 const char *beg = str;
8408 VALUE s;
8410 for (;;) {
8411 if (send - str <= 6) return;
8412 switch (str[6]) {
8413 case 'C': case 'c': str += 6; continue;
8414 case 'O': case 'o': str += 5; continue;
8415 case 'D': case 'd': str += 4; continue;
8416 case 'I': case 'i': str += 3; continue;
8417 case 'N': case 'n': str += 2; continue;
8418 case 'G': case 'g': str += 1; continue;
8419 case '=': case ':':
8420 sep = 1;
8421 str += 6;
8422 break;
8423 default:
8424 str += 6;
8425 if (ISSPACE(*str)) break;
8426 continue;
8428 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8429 sep = 0;
8431 for (;;) {
8432 do {
8433 if (++str >= send) return;
8434 } while (ISSPACE(*str));
8435 if (sep) break;
8436 if (*str != '=' && *str != ':') return;
8437 sep = 1;
8438 str++;
8440 beg = str;
8441 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8442 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8443 parser_set_encode(p, RSTRING_PTR(s));
8444 rb_str_resize(s, 0);
8447 static void
8448 parser_prepare(struct parser_params *p)
8450 int c = nextc(p);
8451 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8452 switch (c) {
8453 case '#':
8454 if (peek(p, '!')) p->has_shebang = 1;
8455 break;
8456 case 0xef: /* UTF-8 BOM marker */
8457 if (p->lex.pend - p->lex.pcur >= 2 &&
8458 (unsigned char)p->lex.pcur[0] == 0xbb &&
8459 (unsigned char)p->lex.pcur[1] == 0xbf) {
8460 p->enc = rb_utf8_encoding();
8461 p->lex.pcur += 2;
8462 p->lex.pbeg = p->lex.pcur;
8463 return;
8465 break;
8466 case EOF:
8467 return;
8469 pushback(p, c);
8470 p->enc = rb_enc_get(p->lex.lastline);
8473 #ifndef RIPPER
8474 #define ambiguous_operator(tok, op, syn) ( \
8475 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8476 rb_warning0("even though it seems like "syn""))
8477 #else
8478 #define ambiguous_operator(tok, op, syn) \
8479 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8480 #endif
8481 #define warn_balanced(tok, op, syn) ((void) \
8482 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8483 space_seen && !ISSPACE(c) && \
8484 (ambiguous_operator(tok, op, syn), 0)), \
8485 (enum yytokentype)(tok))
8487 static VALUE
8488 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8490 VALUE v;
8491 char *point = &str[seen_point];
8492 size_t fraclen = len-seen_point-1;
8493 memmove(point, point+1, fraclen+1);
8494 v = rb_cstr_to_inum(str, 10, FALSE);
8495 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8498 static enum yytokentype
8499 no_digits(struct parser_params *p)
8501 yyerror0("numeric literal without digits");
8502 if (peek(p, '_')) nextc(p);
8503 /* dummy 0, for tUMINUS_NUM at numeric */
8504 return set_integer_literal(p, INT2FIX(0), 0);
8507 static enum yytokentype
8508 parse_numeric(struct parser_params *p, int c)
8510 int is_float, seen_point, seen_e, nondigit;
8511 int suffix;
8513 is_float = seen_point = seen_e = nondigit = 0;
8514 SET_LEX_STATE(EXPR_END);
8515 newtok(p);
8516 if (c == '-' || c == '+') {
8517 tokadd(p, c);
8518 c = nextc(p);
8520 if (c == '0') {
8521 int start = toklen(p);
8522 c = nextc(p);
8523 if (c == 'x' || c == 'X') {
8524 /* hexadecimal */
8525 c = nextc(p);
8526 if (c != -1 && ISXDIGIT(c)) {
8527 do {
8528 if (c == '_') {
8529 if (nondigit) break;
8530 nondigit = c;
8531 continue;
8533 if (!ISXDIGIT(c)) break;
8534 nondigit = 0;
8535 tokadd(p, c);
8536 } while ((c = nextc(p)) != -1);
8538 pushback(p, c);
8539 tokfix(p);
8540 if (toklen(p) == start) {
8541 return no_digits(p);
8543 else if (nondigit) goto trailing_uc;
8544 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8545 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8547 if (c == 'b' || c == 'B') {
8548 /* binary */
8549 c = nextc(p);
8550 if (c == '0' || c == '1') {
8551 do {
8552 if (c == '_') {
8553 if (nondigit) break;
8554 nondigit = c;
8555 continue;
8557 if (c != '0' && c != '1') break;
8558 nondigit = 0;
8559 tokadd(p, c);
8560 } while ((c = nextc(p)) != -1);
8562 pushback(p, c);
8563 tokfix(p);
8564 if (toklen(p) == start) {
8565 return no_digits(p);
8567 else if (nondigit) goto trailing_uc;
8568 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8569 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8571 if (c == 'd' || c == 'D') {
8572 /* decimal */
8573 c = nextc(p);
8574 if (c != -1 && ISDIGIT(c)) {
8575 do {
8576 if (c == '_') {
8577 if (nondigit) break;
8578 nondigit = c;
8579 continue;
8581 if (!ISDIGIT(c)) break;
8582 nondigit = 0;
8583 tokadd(p, c);
8584 } while ((c = nextc(p)) != -1);
8586 pushback(p, c);
8587 tokfix(p);
8588 if (toklen(p) == start) {
8589 return no_digits(p);
8591 else if (nondigit) goto trailing_uc;
8592 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8593 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8595 if (c == '_') {
8596 /* 0_0 */
8597 goto octal_number;
8599 if (c == 'o' || c == 'O') {
8600 /* prefixed octal */
8601 c = nextc(p);
8602 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8603 return no_digits(p);
8606 if (c >= '0' && c <= '7') {
8607 /* octal */
8608 octal_number:
8609 do {
8610 if (c == '_') {
8611 if (nondigit) break;
8612 nondigit = c;
8613 continue;
8615 if (c < '0' || c > '9') break;
8616 if (c > '7') goto invalid_octal;
8617 nondigit = 0;
8618 tokadd(p, c);
8619 } while ((c = nextc(p)) != -1);
8620 if (toklen(p) > start) {
8621 pushback(p, c);
8622 tokfix(p);
8623 if (nondigit) goto trailing_uc;
8624 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8625 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8627 if (nondigit) {
8628 pushback(p, c);
8629 goto trailing_uc;
8632 if (c > '7' && c <= '9') {
8633 invalid_octal:
8634 yyerror0("Invalid octal digit");
8636 else if (c == '.' || c == 'e' || c == 'E') {
8637 tokadd(p, '0');
8639 else {
8640 pushback(p, c);
8641 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8642 return set_integer_literal(p, INT2FIX(0), suffix);
8646 for (;;) {
8647 switch (c) {
8648 case '0': case '1': case '2': case '3': case '4':
8649 case '5': case '6': case '7': case '8': case '9':
8650 nondigit = 0;
8651 tokadd(p, c);
8652 break;
8654 case '.':
8655 if (nondigit) goto trailing_uc;
8656 if (seen_point || seen_e) {
8657 goto decode_num;
8659 else {
8660 int c0 = nextc(p);
8661 if (c0 == -1 || !ISDIGIT(c0)) {
8662 pushback(p, c0);
8663 goto decode_num;
8665 c = c0;
8667 seen_point = toklen(p);
8668 tokadd(p, '.');
8669 tokadd(p, c);
8670 is_float++;
8671 nondigit = 0;
8672 break;
8674 case 'e':
8675 case 'E':
8676 if (nondigit) {
8677 pushback(p, c);
8678 c = nondigit;
8679 goto decode_num;
8681 if (seen_e) {
8682 goto decode_num;
8684 nondigit = c;
8685 c = nextc(p);
8686 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8687 pushback(p, c);
8688 nondigit = 0;
8689 goto decode_num;
8691 tokadd(p, nondigit);
8692 seen_e++;
8693 is_float++;
8694 tokadd(p, c);
8695 nondigit = (c == '-' || c == '+') ? c : 0;
8696 break;
8698 case '_': /* `_' in number just ignored */
8699 if (nondigit) goto decode_num;
8700 nondigit = c;
8701 break;
8703 default:
8704 goto decode_num;
8706 c = nextc(p);
8709 decode_num:
8710 pushback(p, c);
8711 if (nondigit) {
8712 trailing_uc:
8713 literal_flush(p, p->lex.pcur - 1);
8714 YYLTYPE loc = RUBY_INIT_YYLLOC();
8715 compile_error(p, "trailing `%c' in number", nondigit);
8716 parser_show_error_line(p, &loc);
8718 tokfix(p);
8719 if (is_float) {
8720 enum yytokentype type = tFLOAT;
8721 VALUE v;
8723 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8724 if (suffix & NUM_SUFFIX_R) {
8725 type = tRATIONAL;
8726 v = parse_rational(p, tok(p), toklen(p), seen_point);
8728 else {
8729 double d = strtod(tok(p), 0);
8730 if (errno == ERANGE) {
8731 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8732 errno = 0;
8734 v = DBL2NUM(d);
8736 return set_number_literal(p, v, type, suffix);
8738 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8739 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8742 static enum yytokentype
8743 parse_qmark(struct parser_params *p, int space_seen)
8745 rb_encoding *enc;
8746 register int c;
8747 VALUE lit;
8749 if (IS_END()) {
8750 SET_LEX_STATE(EXPR_VALUE);
8751 return '?';
8753 c = nextc(p);
8754 if (c == -1) {
8755 compile_error(p, "incomplete character syntax");
8756 return 0;
8758 if (rb_enc_isspace(c, p->enc)) {
8759 if (!IS_ARG()) {
8760 int c2 = escaped_control_code(c);
8761 if (c2) {
8762 WARN_SPACE_CHAR(c2, "?");
8765 ternary:
8766 pushback(p, c);
8767 SET_LEX_STATE(EXPR_VALUE);
8768 return '?';
8770 newtok(p);
8771 enc = p->enc;
8772 if (!parser_isascii(p)) {
8773 if (tokadd_mbchar(p, c) == -1) return 0;
8775 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8776 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8777 if (space_seen) {
8778 const char *start = p->lex.pcur - 1, *ptr = start;
8779 do {
8780 int n = parser_precise_mbclen(p, ptr);
8781 if (n < 0) return -1;
8782 ptr += n;
8783 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8784 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8785 " a conditional operator, put a space after `?'",
8786 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8788 goto ternary;
8790 else if (c == '\\') {
8791 if (peek(p, 'u')) {
8792 nextc(p);
8793 enc = rb_utf8_encoding();
8794 tokadd_utf8(p, &enc, -1, 0, 0);
8796 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8797 nextc(p);
8798 if (tokadd_mbchar(p, c) == -1) return 0;
8800 else {
8801 c = read_escape(p, 0, &enc);
8802 tokadd(p, c);
8805 else {
8806 tokadd(p, c);
8808 tokfix(p);
8809 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8810 set_yylval_str(lit);
8811 SET_LEX_STATE(EXPR_END);
8812 return tCHAR;
8815 static enum yytokentype
8816 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8818 register int c;
8819 const char *ptok = p->lex.pcur;
8821 if (IS_BEG()) {
8822 int term;
8823 int paren;
8825 c = nextc(p);
8826 quotation:
8827 if (c == -1) goto unterminated;
8828 if (!ISALNUM(c)) {
8829 term = c;
8830 if (!ISASCII(c)) goto unknown;
8831 c = 'Q';
8833 else {
8834 term = nextc(p);
8835 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8836 unknown:
8837 pushback(p, term);
8838 c = parser_precise_mbclen(p, p->lex.pcur);
8839 if (c < 0) return 0;
8840 p->lex.pcur += c;
8841 yyerror0("unknown type of %string");
8842 return 0;
8845 if (term == -1) {
8846 unterminated:
8847 compile_error(p, "unterminated quoted string meets end of file");
8848 return 0;
8850 paren = term;
8851 if (term == '(') term = ')';
8852 else if (term == '[') term = ']';
8853 else if (term == '{') term = '}';
8854 else if (term == '<') term = '>';
8855 else paren = 0;
8857 p->lex.ptok = ptok-1;
8858 switch (c) {
8859 case 'Q':
8860 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8861 return tSTRING_BEG;
8863 case 'q':
8864 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8865 return tSTRING_BEG;
8867 case 'W':
8868 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8869 return tWORDS_BEG;
8871 case 'w':
8872 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8873 return tQWORDS_BEG;
8875 case 'I':
8876 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8877 return tSYMBOLS_BEG;
8879 case 'i':
8880 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8881 return tQSYMBOLS_BEG;
8883 case 'x':
8884 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8885 return tXSTRING_BEG;
8887 case 'r':
8888 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8889 return tREGEXP_BEG;
8891 case 's':
8892 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8893 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8894 return tSYMBEG;
8896 default:
8897 yyerror0("unknown type of %string");
8898 return 0;
8901 if ((c = nextc(p)) == '=') {
8902 set_yylval_id('%');
8903 SET_LEX_STATE(EXPR_BEG);
8904 return tOP_ASGN;
8906 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8907 goto quotation;
8909 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8910 pushback(p, c);
8911 return warn_balanced('%', "%%", "string literal");
8914 static int
8915 tokadd_ident(struct parser_params *p, int c)
8917 do {
8918 if (tokadd_mbchar(p, c) == -1) return -1;
8919 c = nextc(p);
8920 } while (parser_is_identchar(p));
8921 pushback(p, c);
8922 return 0;
8925 static ID
8926 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8928 ID ident = TOK_INTERN();
8930 set_yylval_name(ident);
8932 return ident;
8935 static int
8936 parse_numvar(struct parser_params *p)
8938 size_t len;
8939 int overflow;
8940 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8941 const unsigned long nth_ref_max =
8942 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8943 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8944 * turned into a Fixnum, in compile.c */
8946 if (overflow || n > nth_ref_max) {
8947 /* compile_error()? */
8948 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8949 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8951 else {
8952 return (int)n;
8956 static enum yytokentype
8957 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8959 const char *ptr = p->lex.pcur;
8960 register int c;
8962 SET_LEX_STATE(EXPR_END);
8963 p->lex.ptok = ptr - 1; /* from '$' */
8964 newtok(p);
8965 c = nextc(p);
8966 switch (c) {
8967 case '_': /* $_: last read line string */
8968 c = nextc(p);
8969 if (parser_is_identchar(p)) {
8970 tokadd(p, '$');
8971 tokadd(p, '_');
8972 break;
8974 pushback(p, c);
8975 c = '_';
8976 /* fall through */
8977 case '~': /* $~: match-data */
8978 case '*': /* $*: argv */
8979 case '$': /* $$: pid */
8980 case '?': /* $?: last status */
8981 case '!': /* $!: error string */
8982 case '@': /* $@: error position */
8983 case '/': /* $/: input record separator */
8984 case '\\': /* $\: output record separator */
8985 case ';': /* $;: field separator */
8986 case ',': /* $,: output field separator */
8987 case '.': /* $.: last read line number */
8988 case '=': /* $=: ignorecase */
8989 case ':': /* $:: load path */
8990 case '<': /* $<: reading filename */
8991 case '>': /* $>: default output handle */
8992 case '\"': /* $": already loaded files */
8993 tokadd(p, '$');
8994 tokadd(p, c);
8995 goto gvar;
8997 case '-':
8998 tokadd(p, '$');
8999 tokadd(p, c);
9000 c = nextc(p);
9001 if (parser_is_identchar(p)) {
9002 if (tokadd_mbchar(p, c) == -1) return 0;
9004 else {
9005 pushback(p, c);
9006 pushback(p, '-');
9007 return '$';
9009 gvar:
9010 set_yylval_name(TOK_INTERN());
9011 return tGVAR;
9013 case '&': /* $&: last match */
9014 case '`': /* $`: string before last match */
9015 case '\'': /* $': string after last match */
9016 case '+': /* $+: string matches last paren. */
9017 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9018 tokadd(p, '$');
9019 tokadd(p, c);
9020 goto gvar;
9022 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9023 return tBACK_REF;
9025 case '1': case '2': case '3':
9026 case '4': case '5': case '6':
9027 case '7': case '8': case '9':
9028 tokadd(p, '$');
9029 do {
9030 tokadd(p, c);
9031 c = nextc(p);
9032 } while (c != -1 && ISDIGIT(c));
9033 pushback(p, c);
9034 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9035 tokfix(p);
9036 c = parse_numvar(p);
9037 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9038 return tNTH_REF;
9040 default:
9041 if (!parser_is_identchar(p)) {
9042 YYLTYPE loc = RUBY_INIT_YYLLOC();
9043 if (c == -1 || ISSPACE(c)) {
9044 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9046 else {
9047 pushback(p, c);
9048 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9050 parser_show_error_line(p, &loc);
9051 set_yylval_noname();
9052 return tGVAR;
9054 /* fall through */
9055 case '0':
9056 tokadd(p, '$');
9059 if (tokadd_ident(p, c)) return 0;
9060 SET_LEX_STATE(EXPR_END);
9061 tokenize_ident(p, last_state);
9062 return tGVAR;
9065 #ifndef RIPPER
9066 static bool
9067 parser_numbered_param(struct parser_params *p, int n)
9069 if (n < 0) return false;
9071 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9072 return false;
9074 if (p->max_numparam == ORDINAL_PARAM) {
9075 compile_error(p, "ordinary parameter is defined");
9076 return false;
9078 struct vtable *args = p->lvtbl->args;
9079 if (p->max_numparam < n) {
9080 p->max_numparam = n;
9082 while (n > args->pos) {
9083 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9085 return true;
9087 #endif
9089 static enum yytokentype
9090 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9092 const char *ptr = p->lex.pcur;
9093 enum yytokentype result = tIVAR;
9094 register int c = nextc(p);
9095 YYLTYPE loc;
9097 p->lex.ptok = ptr - 1; /* from '@' */
9098 newtok(p);
9099 tokadd(p, '@');
9100 if (c == '@') {
9101 result = tCVAR;
9102 tokadd(p, '@');
9103 c = nextc(p);
9105 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9106 if (c == -1 || !parser_is_identchar(p)) {
9107 pushback(p, c);
9108 RUBY_SET_YYLLOC(loc);
9109 if (result == tIVAR) {
9110 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9112 else {
9113 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9115 parser_show_error_line(p, &loc);
9116 set_yylval_noname();
9117 SET_LEX_STATE(EXPR_END);
9118 return result;
9120 else if (ISDIGIT(c)) {
9121 pushback(p, c);
9122 RUBY_SET_YYLLOC(loc);
9123 if (result == tIVAR) {
9124 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9126 else {
9127 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9129 parser_show_error_line(p, &loc);
9130 set_yylval_noname();
9131 SET_LEX_STATE(EXPR_END);
9132 return result;
9135 if (tokadd_ident(p, c)) return 0;
9136 tokenize_ident(p, last_state);
9137 return result;
9140 static enum yytokentype
9141 parse_ident(struct parser_params *p, int c, int cmd_state)
9143 enum yytokentype result;
9144 int mb = ENC_CODERANGE_7BIT;
9145 const enum lex_state_e last_state = p->lex.state;
9146 ID ident;
9148 do {
9149 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9150 if (tokadd_mbchar(p, c) == -1) return 0;
9151 c = nextc(p);
9152 } while (parser_is_identchar(p));
9153 if ((c == '!' || c == '?') && !peek(p, '=')) {
9154 result = tFID;
9155 tokadd(p, c);
9157 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9158 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9159 result = tIDENTIFIER;
9160 tokadd(p, c);
9162 else {
9163 result = tCONSTANT; /* assume provisionally */
9164 pushback(p, c);
9166 tokfix(p);
9168 if (IS_LABEL_POSSIBLE()) {
9169 if (IS_LABEL_SUFFIX(0)) {
9170 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9171 nextc(p);
9172 set_yylval_name(TOK_INTERN());
9173 return tLABEL;
9176 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
9177 const struct kwtable *kw;
9179 /* See if it is a reserved word. */
9180 kw = rb_reserved_word(tok(p), toklen(p));
9181 if (kw) {
9182 enum lex_state_e state = p->lex.state;
9183 if (IS_lex_state_for(state, EXPR_FNAME)) {
9184 SET_LEX_STATE(EXPR_ENDFN);
9185 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9186 return kw->id[0];
9188 SET_LEX_STATE(kw->state);
9189 if (IS_lex_state(EXPR_BEG)) {
9190 p->command_start = TRUE;
9192 if (kw->id[0] == keyword_do) {
9193 if (lambda_beginning_p()) {
9194 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9195 return keyword_do_LAMBDA;
9197 if (COND_P()) return keyword_do_cond;
9198 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9199 return keyword_do_block;
9200 return keyword_do;
9202 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
9203 return kw->id[0];
9204 else {
9205 if (kw->id[0] != kw->id[1])
9206 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9207 return kw->id[1];
9212 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9213 if (cmd_state) {
9214 SET_LEX_STATE(EXPR_CMDARG);
9216 else {
9217 SET_LEX_STATE(EXPR_ARG);
9220 else if (p->lex.state == EXPR_FNAME) {
9221 SET_LEX_STATE(EXPR_ENDFN);
9223 else {
9224 SET_LEX_STATE(EXPR_END);
9227 ident = tokenize_ident(p, last_state);
9228 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9229 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9230 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9231 lvar_defined(p, ident)) {
9232 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9234 return result;
9237 static enum yytokentype
9238 parser_yylex(struct parser_params *p)
9240 register int c;
9241 int space_seen = 0;
9242 int cmd_state;
9243 int label;
9244 enum lex_state_e last_state;
9245 int fallthru = FALSE;
9246 int token_seen = p->token_seen;
9248 if (p->lex.strterm) {
9249 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9250 return here_document(p, &p->lex.strterm->u.heredoc);
9252 else {
9253 token_flush(p);
9254 return parse_string(p, &p->lex.strterm->u.literal);
9257 cmd_state = p->command_start;
9258 p->command_start = FALSE;
9259 p->token_seen = TRUE;
9260 retry:
9261 last_state = p->lex.state;
9262 #ifndef RIPPER
9263 token_flush(p);
9264 #endif
9265 switch (c = nextc(p)) {
9266 case '\0': /* NUL */
9267 case '\004': /* ^D */
9268 case '\032': /* ^Z */
9269 case -1: /* end of script. */
9270 return 0;
9272 /* white spaces */
9273 case '\r':
9274 if (!p->cr_seen) {
9275 p->cr_seen = TRUE;
9276 /* carried over with p->lex.nextline for nextc() */
9277 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9279 /* fall through */
9280 case ' ': case '\t': case '\f':
9281 case '\13': /* '\v' */
9282 space_seen = 1;
9283 #ifdef RIPPER
9284 while ((c = nextc(p))) {
9285 switch (c) {
9286 case ' ': case '\t': case '\f': case '\r':
9287 case '\13': /* '\v' */
9288 break;
9289 default:
9290 goto outofloop;
9293 outofloop:
9294 pushback(p, c);
9295 dispatch_scan_event(p, tSP);
9296 #endif
9297 goto retry;
9299 case '#': /* it's a comment */
9300 p->token_seen = token_seen;
9301 /* no magic_comment in shebang line */
9302 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9303 if (comment_at_top(p)) {
9304 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9307 lex_goto_eol(p);
9308 dispatch_scan_event(p, tCOMMENT);
9309 fallthru = TRUE;
9310 /* fall through */
9311 case '\n':
9312 p->token_seen = token_seen;
9313 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9314 !IS_lex_state(EXPR_LABELED));
9315 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9316 if (!fallthru) {
9317 dispatch_scan_event(p, tIGNORED_NL);
9319 fallthru = FALSE;
9320 if (!c && p->ctxt.in_kwarg) {
9321 goto normal_newline;
9323 goto retry;
9325 while (1) {
9326 switch (c = nextc(p)) {
9327 case ' ': case '\t': case '\f': case '\r':
9328 case '\13': /* '\v' */
9329 space_seen = 1;
9330 break;
9331 case '#':
9332 pushback(p, c);
9333 if (space_seen) dispatch_scan_event(p, tSP);
9334 goto retry;
9335 case '&':
9336 case '.': {
9337 dispatch_delayed_token(p, tIGNORED_NL);
9338 if (peek(p, '.') == (c == '&')) {
9339 pushback(p, c);
9340 dispatch_scan_event(p, tSP);
9341 goto retry;
9344 default:
9345 p->ruby_sourceline--;
9346 p->lex.nextline = p->lex.lastline;
9347 case -1: /* EOF no decrement*/
9348 #ifndef RIPPER
9349 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
9350 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
9351 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
9352 pushback(p, 1); /* always pushback */
9353 p->lex.ptok = p->lex.pcur;
9354 #else
9355 lex_goto_eol(p);
9356 if (c != -1) {
9357 p->lex.ptok = p->lex.pcur;
9359 #endif
9360 goto normal_newline;
9363 normal_newline:
9364 p->command_start = TRUE;
9365 SET_LEX_STATE(EXPR_BEG);
9366 return '\n';
9368 case '*':
9369 if ((c = nextc(p)) == '*') {
9370 if ((c = nextc(p)) == '=') {
9371 set_yylval_id(idPow);
9372 SET_LEX_STATE(EXPR_BEG);
9373 return tOP_ASGN;
9375 pushback(p, c);
9376 if (IS_SPCARG(c)) {
9377 rb_warning0("`**' interpreted as argument prefix");
9378 c = tDSTAR;
9380 else if (IS_BEG()) {
9381 c = tDSTAR;
9383 else {
9384 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9387 else {
9388 if (c == '=') {
9389 set_yylval_id('*');
9390 SET_LEX_STATE(EXPR_BEG);
9391 return tOP_ASGN;
9393 pushback(p, c);
9394 if (IS_SPCARG(c)) {
9395 rb_warning0("`*' interpreted as argument prefix");
9396 c = tSTAR;
9398 else if (IS_BEG()) {
9399 c = tSTAR;
9401 else {
9402 c = warn_balanced('*', "*", "argument prefix");
9405 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9406 return c;
9408 case '!':
9409 c = nextc(p);
9410 if (IS_AFTER_OPERATOR()) {
9411 SET_LEX_STATE(EXPR_ARG);
9412 if (c == '@') {
9413 return '!';
9416 else {
9417 SET_LEX_STATE(EXPR_BEG);
9419 if (c == '=') {
9420 return tNEQ;
9422 if (c == '~') {
9423 return tNMATCH;
9425 pushback(p, c);
9426 return '!';
9428 case '=':
9429 if (was_bol(p)) {
9430 /* skip embedded rd document */
9431 if (word_match_p(p, "begin", 5)) {
9432 int first_p = TRUE;
9434 lex_goto_eol(p);
9435 dispatch_scan_event(p, tEMBDOC_BEG);
9436 for (;;) {
9437 lex_goto_eol(p);
9438 if (!first_p) {
9439 dispatch_scan_event(p, tEMBDOC);
9441 first_p = FALSE;
9442 c = nextc(p);
9443 if (c == -1) {
9444 compile_error(p, "embedded document meets end of file");
9445 return 0;
9447 if (c == '=' && word_match_p(p, "end", 3)) {
9448 break;
9450 pushback(p, c);
9452 lex_goto_eol(p);
9453 dispatch_scan_event(p, tEMBDOC_END);
9454 goto retry;
9458 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9459 if ((c = nextc(p)) == '=') {
9460 if ((c = nextc(p)) == '=') {
9461 return tEQQ;
9463 pushback(p, c);
9464 return tEQ;
9466 if (c == '~') {
9467 return tMATCH;
9469 else if (c == '>') {
9470 return tASSOC;
9472 pushback(p, c);
9473 return '=';
9475 case '<':
9476 c = nextc(p);
9477 if (c == '<' &&
9478 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9479 !IS_END() &&
9480 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9481 int token = heredoc_identifier(p);
9482 if (token) return token < 0 ? 0 : token;
9484 if (IS_AFTER_OPERATOR()) {
9485 SET_LEX_STATE(EXPR_ARG);
9487 else {
9488 if (IS_lex_state(EXPR_CLASS))
9489 p->command_start = TRUE;
9490 SET_LEX_STATE(EXPR_BEG);
9492 if (c == '=') {
9493 if ((c = nextc(p)) == '>') {
9494 return tCMP;
9496 pushback(p, c);
9497 return tLEQ;
9499 if (c == '<') {
9500 if ((c = nextc(p)) == '=') {
9501 set_yylval_id(idLTLT);
9502 SET_LEX_STATE(EXPR_BEG);
9503 return tOP_ASGN;
9505 pushback(p, c);
9506 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9508 pushback(p, c);
9509 return '<';
9511 case '>':
9512 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9513 if ((c = nextc(p)) == '=') {
9514 return tGEQ;
9516 if (c == '>') {
9517 if ((c = nextc(p)) == '=') {
9518 set_yylval_id(idGTGT);
9519 SET_LEX_STATE(EXPR_BEG);
9520 return tOP_ASGN;
9522 pushback(p, c);
9523 return tRSHFT;
9525 pushback(p, c);
9526 return '>';
9528 case '"':
9529 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9530 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9531 p->lex.ptok = p->lex.pcur-1;
9532 return tSTRING_BEG;
9534 case '`':
9535 if (IS_lex_state(EXPR_FNAME)) {
9536 SET_LEX_STATE(EXPR_ENDFN);
9537 return c;
9539 if (IS_lex_state(EXPR_DOT)) {
9540 if (cmd_state)
9541 SET_LEX_STATE(EXPR_CMDARG);
9542 else
9543 SET_LEX_STATE(EXPR_ARG);
9544 return c;
9546 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9547 return tXSTRING_BEG;
9549 case '\'':
9550 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9551 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9552 p->lex.ptok = p->lex.pcur-1;
9553 return tSTRING_BEG;
9555 case '?':
9556 return parse_qmark(p, space_seen);
9558 case '&':
9559 if ((c = nextc(p)) == '&') {
9560 SET_LEX_STATE(EXPR_BEG);
9561 if ((c = nextc(p)) == '=') {
9562 set_yylval_id(idANDOP);
9563 SET_LEX_STATE(EXPR_BEG);
9564 return tOP_ASGN;
9566 pushback(p, c);
9567 return tANDOP;
9569 else if (c == '=') {
9570 set_yylval_id('&');
9571 SET_LEX_STATE(EXPR_BEG);
9572 return tOP_ASGN;
9574 else if (c == '.') {
9575 set_yylval_id(idANDDOT);
9576 SET_LEX_STATE(EXPR_DOT);
9577 return tANDDOT;
9579 pushback(p, c);
9580 if (IS_SPCARG(c)) {
9581 if ((c != ':') ||
9582 (c = peekc_n(p, 1)) == -1 ||
9583 !(c == '\'' || c == '"' ||
9584 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9585 rb_warning0("`&' interpreted as argument prefix");
9587 c = tAMPER;
9589 else if (IS_BEG()) {
9590 c = tAMPER;
9592 else {
9593 c = warn_balanced('&', "&", "argument prefix");
9595 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9596 return c;
9598 case '|':
9599 if ((c = nextc(p)) == '|') {
9600 SET_LEX_STATE(EXPR_BEG);
9601 if ((c = nextc(p)) == '=') {
9602 set_yylval_id(idOROP);
9603 SET_LEX_STATE(EXPR_BEG);
9604 return tOP_ASGN;
9606 pushback(p, c);
9607 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9608 c = '|';
9609 pushback(p, '|');
9610 return c;
9612 return tOROP;
9614 if (c == '=') {
9615 set_yylval_id('|');
9616 SET_LEX_STATE(EXPR_BEG);
9617 return tOP_ASGN;
9619 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9620 pushback(p, c);
9621 return '|';
9623 case '+':
9624 c = nextc(p);
9625 if (IS_AFTER_OPERATOR()) {
9626 SET_LEX_STATE(EXPR_ARG);
9627 if (c == '@') {
9628 return tUPLUS;
9630 pushback(p, c);
9631 return '+';
9633 if (c == '=') {
9634 set_yylval_id('+');
9635 SET_LEX_STATE(EXPR_BEG);
9636 return tOP_ASGN;
9638 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9639 SET_LEX_STATE(EXPR_BEG);
9640 pushback(p, c);
9641 if (c != -1 && ISDIGIT(c)) {
9642 return parse_numeric(p, '+');
9644 return tUPLUS;
9646 SET_LEX_STATE(EXPR_BEG);
9647 pushback(p, c);
9648 return warn_balanced('+', "+", "unary operator");
9650 case '-':
9651 c = nextc(p);
9652 if (IS_AFTER_OPERATOR()) {
9653 SET_LEX_STATE(EXPR_ARG);
9654 if (c == '@') {
9655 return tUMINUS;
9657 pushback(p, c);
9658 return '-';
9660 if (c == '=') {
9661 set_yylval_id('-');
9662 SET_LEX_STATE(EXPR_BEG);
9663 return tOP_ASGN;
9665 if (c == '>') {
9666 SET_LEX_STATE(EXPR_ENDFN);
9667 return tLAMBDA;
9669 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9670 SET_LEX_STATE(EXPR_BEG);
9671 pushback(p, c);
9672 if (c != -1 && ISDIGIT(c)) {
9673 return tUMINUS_NUM;
9675 return tUMINUS;
9677 SET_LEX_STATE(EXPR_BEG);
9678 pushback(p, c);
9679 return warn_balanced('-', "-", "unary operator");
9681 case '.': {
9682 int is_beg = IS_BEG();
9683 SET_LEX_STATE(EXPR_BEG);
9684 if ((c = nextc(p)) == '.') {
9685 if ((c = nextc(p)) == '.') {
9686 if (p->ctxt.in_argdef) {
9687 SET_LEX_STATE(EXPR_ENDARG);
9688 return tBDOT3;
9690 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9691 rb_warn0("... at EOL, should be parenthesized?");
9693 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
9694 if (IS_lex_state_for(last_state, EXPR_LABEL))
9695 return tDOT3;
9697 return is_beg ? tBDOT3 : tDOT3;
9699 pushback(p, c);
9700 return is_beg ? tBDOT2 : tDOT2;
9702 pushback(p, c);
9703 if (c != -1 && ISDIGIT(c)) {
9704 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9705 parse_numeric(p, '.');
9706 if (ISDIGIT(prev)) {
9707 yyerror0("unexpected fraction part after numeric literal");
9709 else {
9710 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9712 SET_LEX_STATE(EXPR_END);
9713 p->lex.ptok = p->lex.pcur;
9714 goto retry;
9716 set_yylval_id('.');
9717 SET_LEX_STATE(EXPR_DOT);
9718 return '.';
9721 case '0': case '1': case '2': case '3': case '4':
9722 case '5': case '6': case '7': case '8': case '9':
9723 return parse_numeric(p, c);
9725 case ')':
9726 COND_POP();
9727 CMDARG_POP();
9728 SET_LEX_STATE(EXPR_ENDFN);
9729 p->lex.paren_nest--;
9730 return c;
9732 case ']':
9733 COND_POP();
9734 CMDARG_POP();
9735 SET_LEX_STATE(EXPR_END);
9736 p->lex.paren_nest--;
9737 return c;
9739 case '}':
9740 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9741 if (!p->lex.brace_nest--) return tSTRING_DEND;
9742 COND_POP();
9743 CMDARG_POP();
9744 SET_LEX_STATE(EXPR_END);
9745 p->lex.paren_nest--;
9746 return c;
9748 case ':':
9749 c = nextc(p);
9750 if (c == ':') {
9751 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9752 SET_LEX_STATE(EXPR_BEG);
9753 return tCOLON3;
9755 set_yylval_id(idCOLON2);
9756 SET_LEX_STATE(EXPR_DOT);
9757 return tCOLON2;
9759 if (IS_END() || ISSPACE(c) || c == '#') {
9760 pushback(p, c);
9761 c = warn_balanced(':', ":", "symbol literal");
9762 SET_LEX_STATE(EXPR_BEG);
9763 return c;
9765 switch (c) {
9766 case '\'':
9767 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9768 break;
9769 case '"':
9770 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9771 break;
9772 default:
9773 pushback(p, c);
9774 break;
9776 SET_LEX_STATE(EXPR_FNAME);
9777 return tSYMBEG;
9779 case '/':
9780 if (IS_BEG()) {
9781 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9782 return tREGEXP_BEG;
9784 if ((c = nextc(p)) == '=') {
9785 set_yylval_id('/');
9786 SET_LEX_STATE(EXPR_BEG);
9787 return tOP_ASGN;
9789 pushback(p, c);
9790 if (IS_SPCARG(c)) {
9791 arg_ambiguous(p, '/');
9792 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9793 return tREGEXP_BEG;
9795 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9796 return warn_balanced('/', "/", "regexp literal");
9798 case '^':
9799 if ((c = nextc(p)) == '=') {
9800 set_yylval_id('^');
9801 SET_LEX_STATE(EXPR_BEG);
9802 return tOP_ASGN;
9804 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9805 pushback(p, c);
9806 return '^';
9808 case ';':
9809 SET_LEX_STATE(EXPR_BEG);
9810 p->command_start = TRUE;
9811 return ';';
9813 case ',':
9814 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9815 return ',';
9817 case '~':
9818 if (IS_AFTER_OPERATOR()) {
9819 if ((c = nextc(p)) != '@') {
9820 pushback(p, c);
9822 SET_LEX_STATE(EXPR_ARG);
9824 else {
9825 SET_LEX_STATE(EXPR_BEG);
9827 return '~';
9829 case '(':
9830 if (IS_BEG()) {
9831 c = tLPAREN;
9833 else if (!space_seen) {
9834 /* foo( ... ) => method call, no ambiguity */
9836 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9837 c = tLPAREN_ARG;
9839 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9840 rb_warning0("parentheses after method name is interpreted as "
9841 "an argument list, not a decomposed argument");
9843 p->lex.paren_nest++;
9844 COND_PUSH(0);
9845 CMDARG_PUSH(0);
9846 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9847 return c;
9849 case '[':
9850 p->lex.paren_nest++;
9851 if (IS_AFTER_OPERATOR()) {
9852 if ((c = nextc(p)) == ']') {
9853 p->lex.paren_nest--;
9854 SET_LEX_STATE(EXPR_ARG);
9855 if ((c = nextc(p)) == '=') {
9856 return tASET;
9858 pushback(p, c);
9859 return tAREF;
9861 pushback(p, c);
9862 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9863 return '[';
9865 else if (IS_BEG()) {
9866 c = tLBRACK;
9868 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9869 c = tLBRACK;
9871 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9872 COND_PUSH(0);
9873 CMDARG_PUSH(0);
9874 return c;
9876 case '{':
9877 ++p->lex.brace_nest;
9878 if (lambda_beginning_p())
9879 c = tLAMBEG;
9880 else if (IS_lex_state(EXPR_LABELED))
9881 c = tLBRACE; /* hash */
9882 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9883 c = '{'; /* block (primary) */
9884 else if (IS_lex_state(EXPR_ENDARG))
9885 c = tLBRACE_ARG; /* block (expr) */
9886 else
9887 c = tLBRACE; /* hash */
9888 if (c != tLBRACE) {
9889 p->command_start = TRUE;
9890 SET_LEX_STATE(EXPR_BEG);
9892 else {
9893 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9895 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9896 COND_PUSH(0);
9897 CMDARG_PUSH(0);
9898 return c;
9900 case '\\':
9901 c = nextc(p);
9902 if (c == '\n') {
9903 space_seen = 1;
9904 dispatch_scan_event(p, tSP);
9905 goto retry; /* skip \\n */
9907 if (c == ' ') return tSP;
9908 if (ISSPACE(c)) return c;
9909 pushback(p, c);
9910 return '\\';
9912 case '%':
9913 return parse_percent(p, space_seen, last_state);
9915 case '$':
9916 return parse_gvar(p, last_state);
9918 case '@':
9919 return parse_atmark(p, last_state);
9921 case '_':
9922 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9923 p->ruby__end__seen = 1;
9924 p->eofp = 1;
9925 #ifndef RIPPER
9926 return -1;
9927 #else
9928 lex_goto_eol(p);
9929 dispatch_scan_event(p, k__END__);
9930 return 0;
9931 #endif
9933 newtok(p);
9934 break;
9936 default:
9937 if (!parser_is_identchar(p)) {
9938 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9939 token_flush(p);
9940 goto retry;
9943 newtok(p);
9944 break;
9947 return parse_ident(p, c, cmd_state);
9950 static enum yytokentype
9951 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9953 enum yytokentype t;
9955 p->lval = lval;
9956 lval->val = Qundef;
9957 t = parser_yylex(p);
9959 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9960 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9961 else
9962 RUBY_SET_YYLLOC(*yylloc);
9964 if (has_delayed_token(p))
9965 dispatch_delayed_token(p, t);
9966 else if (t != 0)
9967 dispatch_scan_event(p, t);
9969 return t;
9972 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9974 static NODE*
9975 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9977 NODE *n = rb_ast_newnode(p->ast, type);
9979 rb_node_init(n, type, a0, a1, a2);
9981 nd_set_loc(n, loc);
9982 nd_set_node_id(n, parser_get_node_id(p));
9983 return n;
9986 static NODE *
9987 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9989 nd->nd_loc = *loc;
9990 nd_set_line(nd, loc->beg_pos.lineno);
9991 return nd;
9994 #ifndef RIPPER
9995 static enum node_type
9996 nodetype(NODE *node) /* for debug */
9998 return (enum node_type)nd_type(node);
10001 static int
10002 nodeline(NODE *node)
10004 return nd_line(node);
10007 static NODE*
10008 newline_node(NODE *node)
10010 if (node) {
10011 node = remove_begin(node);
10012 node->flags |= NODE_FL_NEWLINE;
10014 return node;
10017 static void
10018 fixpos(NODE *node, NODE *orig)
10020 if (!node) return;
10021 if (!orig) return;
10022 nd_set_line(node, nd_line(orig));
10025 static void
10026 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10028 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10031 static void
10032 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10034 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10037 static NODE*
10038 block_append(struct parser_params *p, NODE *head, NODE *tail)
10040 NODE *end, *h = head, *nd;
10042 if (tail == 0) return head;
10044 if (h == 0) return tail;
10045 switch (nd_type(h)) {
10046 case NODE_LIT:
10047 case NODE_STR:
10048 case NODE_SELF:
10049 case NODE_TRUE:
10050 case NODE_FALSE:
10051 case NODE_NIL:
10052 parser_warning(p, h, "unused literal ignored");
10053 return tail;
10054 default:
10055 h = end = NEW_BLOCK(head, &head->nd_loc);
10056 end->nd_end = end;
10057 head = end;
10058 break;
10059 case NODE_BLOCK:
10060 end = h->nd_end;
10061 break;
10064 nd = end->nd_head;
10065 switch (nd_type(nd)) {
10066 case NODE_RETURN:
10067 case NODE_BREAK:
10068 case NODE_NEXT:
10069 case NODE_REDO:
10070 case NODE_RETRY:
10071 if (RTEST(ruby_verbose)) {
10072 parser_warning(p, tail, "statement not reached");
10074 break;
10076 default:
10077 break;
10080 if (!nd_type_p(tail, NODE_BLOCK)) {
10081 tail = NEW_BLOCK(tail, &tail->nd_loc);
10082 tail->nd_end = tail;
10084 end->nd_next = tail;
10085 h->nd_end = tail->nd_end;
10086 nd_set_last_loc(head, nd_last_loc(tail));
10087 return head;
10090 /* append item to the list */
10091 static NODE*
10092 list_append(struct parser_params *p, NODE *list, NODE *item)
10094 NODE *last;
10096 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10097 if (list->nd_next) {
10098 last = list->nd_next->nd_end;
10100 else {
10101 last = list;
10104 list->nd_alen += 1;
10105 last->nd_next = NEW_LIST(item, &item->nd_loc);
10106 list->nd_next->nd_end = last->nd_next;
10108 nd_set_last_loc(list, nd_last_loc(item));
10110 return list;
10113 /* concat two lists */
10114 static NODE*
10115 list_concat(NODE *head, NODE *tail)
10117 NODE *last;
10119 if (head->nd_next) {
10120 last = head->nd_next->nd_end;
10122 else {
10123 last = head;
10126 head->nd_alen += tail->nd_alen;
10127 last->nd_next = tail;
10128 if (tail->nd_next) {
10129 head->nd_next->nd_end = tail->nd_next->nd_end;
10131 else {
10132 head->nd_next->nd_end = tail;
10135 nd_set_last_loc(head, nd_last_loc(tail));
10137 return head;
10140 static int
10141 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10143 if (NIL_P(tail)) return 1;
10144 if (!rb_enc_compatible(head, tail)) {
10145 compile_error(p, "string literal encodings differ (%s / %s)",
10146 rb_enc_name(rb_enc_get(head)),
10147 rb_enc_name(rb_enc_get(tail)));
10148 rb_str_resize(head, 0);
10149 rb_str_resize(tail, 0);
10150 return 0;
10152 rb_str_buf_append(head, tail);
10153 return 1;
10156 static VALUE
10157 string_literal_head(enum node_type htype, NODE *head)
10159 if (htype != NODE_DSTR) return Qfalse;
10160 if (head->nd_next) {
10161 head = head->nd_next->nd_end->nd_head;
10162 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10164 const VALUE lit = head->nd_lit;
10165 ASSUME(lit != Qfalse);
10166 return lit;
10169 /* concat two string literals */
10170 static NODE *
10171 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10173 enum node_type htype;
10174 VALUE lit;
10176 if (!head) return tail;
10177 if (!tail) return head;
10179 htype = nd_type(head);
10180 if (htype == NODE_EVSTR) {
10181 head = new_dstr(p, head, loc);
10182 htype = NODE_DSTR;
10184 if (p->heredoc_indent > 0) {
10185 switch (htype) {
10186 case NODE_STR:
10187 nd_set_type(head, NODE_DSTR);
10188 case NODE_DSTR:
10189 return list_append(p, head, tail);
10190 default:
10191 break;
10194 switch (nd_type(tail)) {
10195 case NODE_STR:
10196 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10197 htype = NODE_STR;
10199 else {
10200 lit = head->nd_lit;
10202 if (htype == NODE_STR) {
10203 if (!literal_concat0(p, lit, tail->nd_lit)) {
10204 error:
10205 rb_discard_node(p, head);
10206 rb_discard_node(p, tail);
10207 return 0;
10209 rb_discard_node(p, tail);
10211 else {
10212 list_append(p, head, tail);
10214 break;
10216 case NODE_DSTR:
10217 if (htype == NODE_STR) {
10218 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10219 goto error;
10220 tail->nd_lit = head->nd_lit;
10221 rb_discard_node(p, head);
10222 head = tail;
10224 else if (NIL_P(tail->nd_lit)) {
10225 append:
10226 head->nd_alen += tail->nd_alen - 1;
10227 if (!head->nd_next) {
10228 head->nd_next = tail->nd_next;
10230 else if (tail->nd_next) {
10231 head->nd_next->nd_end->nd_next = tail->nd_next;
10232 head->nd_next->nd_end = tail->nd_next->nd_end;
10234 rb_discard_node(p, tail);
10236 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10237 if (!literal_concat0(p, lit, tail->nd_lit))
10238 goto error;
10239 tail->nd_lit = Qnil;
10240 goto append;
10242 else {
10243 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10245 break;
10247 case NODE_EVSTR:
10248 if (htype == NODE_STR) {
10249 nd_set_type(head, NODE_DSTR);
10250 head->nd_alen = 1;
10252 list_append(p, head, tail);
10253 break;
10255 return head;
10258 static NODE *
10259 evstr2dstr(struct parser_params *p, NODE *node)
10261 if (nd_type_p(node, NODE_EVSTR)) {
10262 node = new_dstr(p, node, &node->nd_loc);
10264 return node;
10267 static NODE *
10268 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10270 NODE *head = node;
10272 if (node) {
10273 switch (nd_type(node)) {
10274 case NODE_STR:
10275 nd_set_type(node, NODE_DSTR);
10276 return node;
10277 case NODE_DSTR:
10278 break;
10279 case NODE_EVSTR:
10280 return node;
10283 return NEW_EVSTR(head, loc);
10286 static NODE *
10287 new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10289 VALUE lit = STR_NEW0();
10290 NODE *dstr = NEW_DSTR(lit, loc);
10291 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10292 return list_append(p, dstr, node);
10295 static NODE *
10296 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10297 const YYLTYPE *op_loc, const YYLTYPE *loc)
10299 NODE *expr;
10300 value_expr(recv);
10301 value_expr(arg1);
10302 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10303 nd_set_line(expr, op_loc->beg_pos.lineno);
10304 return expr;
10307 static NODE *
10308 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10310 NODE *opcall;
10311 value_expr(recv);
10312 opcall = NEW_OPCALL(recv, id, 0, loc);
10313 nd_set_line(opcall, op_loc->beg_pos.lineno);
10314 return opcall;
10317 static NODE *
10318 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10320 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10321 nd_set_line(qcall, op_loc->beg_pos.lineno);
10322 return qcall;
10325 static NODE*
10326 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10328 NODE *ret;
10329 if (block) block_dup_check(p, args, block);
10330 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10331 if (block) ret = method_add_block(p, ret, block, loc);
10332 fixpos(ret, recv);
10333 return ret;
10336 #define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10337 static NODE*
10338 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10340 NODE *n;
10341 int line = op_loc->beg_pos.lineno;
10343 value_expr(node1);
10344 value_expr(node2);
10345 if (node1 && (n = nd_once_body(node1)) != 0) {
10346 switch (nd_type(n)) {
10347 case NODE_DREGX:
10349 NODE *match = NEW_MATCH2(node1, node2, loc);
10350 nd_set_line(match, line);
10351 return match;
10354 case NODE_LIT:
10355 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10356 const VALUE lit = n->nd_lit;
10357 NODE *match = NEW_MATCH2(node1, node2, loc);
10358 match->nd_args = reg_named_capture_assign(p, lit, loc);
10359 nd_set_line(match, line);
10360 return match;
10365 if (node2 && (n = nd_once_body(node2)) != 0) {
10366 NODE *match3;
10368 switch (nd_type(n)) {
10369 case NODE_LIT:
10370 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10371 /* fallthru */
10372 case NODE_DREGX:
10373 match3 = NEW_MATCH3(node2, node1, loc);
10374 return match3;
10378 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10379 nd_set_line(n, line);
10380 return n;
10383 # if WARN_PAST_SCOPE
10384 static int
10385 past_dvar_p(struct parser_params *p, ID id)
10387 struct vtable *past = p->lvtbl->past;
10388 while (past) {
10389 if (vtable_included(past, id)) return 1;
10390 past = past->prev;
10392 return 0;
10394 # endif
10396 static int
10397 numparam_nested_p(struct parser_params *p)
10399 struct local_vars *local = p->lvtbl;
10400 NODE *outer = local->numparam.outer;
10401 NODE *inner = local->numparam.inner;
10402 if (outer || inner) {
10403 NODE *used = outer ? outer : inner;
10404 compile_error(p, "numbered parameter is already used in\n"
10405 "%s:%d: %s block here",
10406 p->ruby_sourcefile, nd_line(used),
10407 outer ? "outer" : "inner");
10408 parser_show_error_line(p, &used->nd_loc);
10409 return 1;
10411 return 0;
10414 static NODE*
10415 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10417 ID *vidp = NULL;
10418 NODE *node;
10419 switch (id) {
10420 case keyword_self:
10421 return NEW_SELF(loc);
10422 case keyword_nil:
10423 return NEW_NIL(loc);
10424 case keyword_true:
10425 return NEW_TRUE(loc);
10426 case keyword_false:
10427 return NEW_FALSE(loc);
10428 case keyword__FILE__:
10430 VALUE file = p->ruby_sourcefile_string;
10431 if (NIL_P(file))
10432 file = rb_str_new(0, 0);
10433 else
10434 file = rb_str_dup(file);
10435 node = NEW_STR(file, loc);
10436 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10438 return node;
10439 case keyword__LINE__:
10440 return NEW_LIT(INT2FIX(p->tokline), loc);
10441 case keyword__ENCODING__:
10442 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10443 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10444 return node;
10447 switch (id_type(id)) {
10448 case ID_LOCAL:
10449 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10450 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10451 if (id == p->cur_arg) {
10452 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10453 return 0;
10455 if (vidp) *vidp |= LVAR_USED;
10456 node = NEW_DVAR(id, loc);
10457 return node;
10459 if (local_id_ref(p, id, &vidp)) {
10460 if (id == p->cur_arg) {
10461 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10462 return 0;
10464 if (vidp) *vidp |= LVAR_USED;
10465 node = NEW_LVAR(id, loc);
10466 return node;
10468 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10469 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10470 if (numparam_nested_p(p)) return 0;
10471 node = NEW_DVAR(id, loc);
10472 struct local_vars *local = p->lvtbl;
10473 if (!local->numparam.current) local->numparam.current = node;
10474 return node;
10476 # if WARN_PAST_SCOPE
10477 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10478 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10480 # endif
10481 /* method call without arguments */
10482 return NEW_VCALL(id, loc);
10483 case ID_GLOBAL:
10484 return NEW_GVAR(id, loc);
10485 case ID_INSTANCE:
10486 return NEW_IVAR(id, loc);
10487 case ID_CONST:
10488 return NEW_CONST(id, loc);
10489 case ID_CLASS:
10490 return NEW_CVAR(id, loc);
10492 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10493 return 0;
10496 static NODE *
10497 opt_arg_append(NODE *opt_list, NODE *opt)
10499 NODE *opts = opt_list;
10500 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10502 while (opts->nd_next) {
10503 opts = opts->nd_next;
10504 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10506 opts->nd_next = opt;
10508 return opt_list;
10511 static NODE *
10512 kwd_append(NODE *kwlist, NODE *kw)
10514 if (kwlist) {
10515 NODE *kws = kwlist;
10516 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10517 while (kws->nd_next) {
10518 kws = kws->nd_next;
10519 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10521 kws->nd_next = kw;
10523 return kwlist;
10526 static NODE *
10527 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10529 return NEW_DEFINED(remove_begin_all(expr), loc);
10532 static NODE*
10533 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10535 enum node_type type = nd_type(symbol);
10536 switch (type) {
10537 case NODE_DSTR:
10538 nd_set_type(symbol, NODE_DSYM);
10539 break;
10540 case NODE_STR:
10541 nd_set_type(symbol, NODE_LIT);
10542 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10543 break;
10544 default:
10545 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
10547 return list_append(p, symbols, symbol);
10550 static NODE *
10551 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10553 NODE *list, *prev;
10554 VALUE lit;
10556 if (!node) {
10557 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10558 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10559 return node;
10561 switch (nd_type(node)) {
10562 case NODE_STR:
10564 VALUE src = node->nd_lit;
10565 nd_set_type(node, NODE_LIT);
10566 nd_set_loc(node, loc);
10567 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10569 break;
10570 default:
10571 lit = STR_NEW0();
10572 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10573 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10574 /* fall through */
10575 case NODE_DSTR:
10576 nd_set_type(node, NODE_DREGX);
10577 nd_set_loc(node, loc);
10578 node->nd_cflag = options & RE_OPTION_MASK;
10579 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10580 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10581 NODE *frag = list->nd_head;
10582 enum node_type type = nd_type(frag);
10583 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
10584 VALUE tail = frag->nd_lit;
10585 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10586 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10587 if (!literal_concat0(p, lit, tail)) {
10588 return NEW_NIL(loc); /* dummy node on error */
10590 rb_str_resize(tail, 0);
10591 prev->nd_next = list->nd_next;
10592 rb_discard_node(p, list->nd_head);
10593 rb_discard_node(p, list);
10594 list = prev;
10596 else {
10597 prev = list;
10600 else {
10601 prev = 0;
10604 if (!node->nd_next) {
10605 VALUE src = node->nd_lit;
10606 nd_set_type(node, NODE_LIT);
10607 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10609 if (options & RE_OPTION_ONCE) {
10610 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10612 break;
10614 return node;
10617 static NODE *
10618 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10620 if (!k) return 0;
10621 return NEW_KW_ARG(0, (k), loc);
10624 static NODE *
10625 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10627 if (!node) {
10628 VALUE lit = STR_NEW0();
10629 NODE *xstr = NEW_XSTR(lit, loc);
10630 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10631 return xstr;
10633 switch (nd_type(node)) {
10634 case NODE_STR:
10635 nd_set_type(node, NODE_XSTR);
10636 nd_set_loc(node, loc);
10637 break;
10638 case NODE_DSTR:
10639 nd_set_type(node, NODE_DXSTR);
10640 nd_set_loc(node, loc);
10641 break;
10642 default:
10643 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10644 break;
10646 return node;
10649 static void
10650 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10652 VALUE lit;
10654 if (!arg || !p->case_labels) return;
10656 lit = rb_node_case_when_optimizable_literal(arg);
10657 if (lit == Qundef) return;
10658 if (nd_type_p(arg, NODE_STR)) {
10659 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10662 if (NIL_P(p->case_labels)) {
10663 p->case_labels = rb_obj_hide(rb_hash_new());
10665 else {
10666 VALUE line = rb_hash_lookup(p->case_labels, lit);
10667 if (!NIL_P(line)) {
10668 rb_warning1("duplicated `when' clause with line %d is ignored",
10669 WARN_IVAL(line));
10670 return;
10673 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10676 #else /* !RIPPER */
10677 static int
10678 id_is_var(struct parser_params *p, ID id)
10680 if (is_notop_id(id)) {
10681 switch (id & ID_SCOPE_MASK) {
10682 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10683 return 1;
10684 case ID_LOCAL:
10685 if (dyna_in_block(p)) {
10686 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10688 if (local_id(p, id)) return 1;
10689 /* method call without arguments */
10690 return 0;
10693 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10694 return 0;
10697 static VALUE
10698 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10700 VALUE src = 0, err;
10701 int options = 0;
10702 if (ripper_is_node_yylval(re)) {
10703 src = RNODE(re)->nd_cval;
10704 re = RNODE(re)->nd_rval;
10706 if (ripper_is_node_yylval(opt)) {
10707 options = (int)RNODE(opt)->nd_tag;
10708 opt = RNODE(opt)->nd_rval;
10710 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10711 compile_error(p, "%"PRIsVALUE, err);
10713 return dispatch2(regexp_literal, re, opt);
10715 #endif /* !RIPPER */
10717 static inline enum lex_state_e
10718 parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
10720 if (p->debug) {
10721 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
10723 return p->lex.state = ls;
10726 #ifndef RIPPER
10727 static const char rb_parser_lex_state_names[][8] = {
10728 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10729 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10730 "LABEL", "LABELED","FITEM",
10733 static VALUE
10734 append_lex_state_name(enum lex_state_e state, VALUE buf)
10736 int i, sep = 0;
10737 unsigned int mask = 1;
10738 static const char none[] = "NONE";
10740 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10741 if ((unsigned)state & mask) {
10742 if (sep) {
10743 rb_str_cat(buf, "|", 1);
10745 sep = 1;
10746 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10749 if (!sep) {
10750 rb_str_cat(buf, none, sizeof(none)-1);
10752 return buf;
10755 static void
10756 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10758 VALUE mesg = p->debug_buffer;
10760 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10761 p->debug_buffer = Qnil;
10762 rb_io_puts(1, &mesg, out);
10764 if (!NIL_P(str) && RSTRING_LEN(str)) {
10765 rb_io_write(p->debug_output, str);
10769 enum lex_state_e
10770 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10771 enum lex_state_e to, int line)
10773 VALUE mesg;
10774 mesg = rb_str_new_cstr("lex_state: ");
10775 append_lex_state_name(from, mesg);
10776 rb_str_cat_cstr(mesg, " -> ");
10777 append_lex_state_name(to, mesg);
10778 rb_str_catf(mesg, " at line %d\n", line);
10779 flush_debug_buffer(p, p->debug_output, mesg);
10780 return to;
10783 VALUE
10784 rb_parser_lex_state_name(enum lex_state_e state)
10786 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10789 static void
10790 append_bitstack_value(stack_type stack, VALUE mesg)
10792 if (stack == 0) {
10793 rb_str_cat_cstr(mesg, "0");
10795 else {
10796 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10797 for (; mask && !(stack & mask); mask >>= 1) continue;
10798 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10802 void
10803 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10804 const char *name, int line)
10806 VALUE mesg = rb_sprintf("%s: ", name);
10807 append_bitstack_value(stack, mesg);
10808 rb_str_catf(mesg, " at line %d\n", line);
10809 flush_debug_buffer(p, p->debug_output, mesg);
10812 void
10813 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10815 va_list ap;
10816 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10818 va_start(ap, fmt);
10819 rb_str_vcatf(mesg, fmt, ap);
10820 va_end(ap);
10821 yyerror0(RSTRING_PTR(mesg));
10822 RB_GC_GUARD(mesg);
10824 mesg = rb_str_new(0, 0);
10825 append_lex_state_name(p->lex.state, mesg);
10826 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10827 rb_str_resize(mesg, 0);
10828 append_bitstack_value(p->cond_stack, mesg);
10829 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10830 rb_str_resize(mesg, 0);
10831 append_bitstack_value(p->cmdarg_stack, mesg);
10832 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10833 if (p->debug_output == rb_ractor_stdout())
10834 p->debug_output = rb_ractor_stderr();
10835 p->debug = TRUE;
10838 static YYLTYPE *
10839 rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
10841 yylloc->beg_pos.lineno = sourceline;
10842 yylloc->beg_pos.column = beg_pos;
10843 yylloc->end_pos.lineno = sourceline;
10844 yylloc->end_pos.column = end_pos;
10845 return yylloc;
10848 YYLTYPE *
10849 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10851 int sourceline = here->sourceline;
10852 int beg_pos = (int)here->offset - here->quote
10853 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10854 int end_pos = (int)here->offset + here->length + here->quote;
10856 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10859 YYLTYPE *
10860 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10862 int sourceline = p->ruby_sourceline;
10863 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10864 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
10865 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10868 YYLTYPE *
10869 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10871 int sourceline = p->ruby_sourceline;
10872 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10873 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
10874 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
10876 #endif /* !RIPPER */
10878 static int
10879 assignable0(struct parser_params *p, ID id, const char **err)
10881 if (!id) return -1;
10882 switch (id) {
10883 case keyword_self:
10884 *err = "Can't change the value of self";
10885 return -1;
10886 case keyword_nil:
10887 *err = "Can't assign to nil";
10888 return -1;
10889 case keyword_true:
10890 *err = "Can't assign to true";
10891 return -1;
10892 case keyword_false:
10893 *err = "Can't assign to false";
10894 return -1;
10895 case keyword__FILE__:
10896 *err = "Can't assign to __FILE__";
10897 return -1;
10898 case keyword__LINE__:
10899 *err = "Can't assign to __LINE__";
10900 return -1;
10901 case keyword__ENCODING__:
10902 *err = "Can't assign to __ENCODING__";
10903 return -1;
10905 switch (id_type(id)) {
10906 case ID_LOCAL:
10907 if (dyna_in_block(p)) {
10908 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10909 compile_error(p, "Can't assign to numbered parameter _%d",
10910 NUMPARAM_ID_TO_IDX(id));
10911 return -1;
10913 if (dvar_curr(p, id)) return NODE_DASGN;
10914 if (dvar_defined(p, id)) return NODE_DASGN;
10915 if (local_id(p, id)) return NODE_LASGN;
10916 dyna_var(p, id);
10917 return NODE_DASGN;
10919 else {
10920 if (!local_id(p, id)) local_var(p, id);
10921 return NODE_LASGN;
10923 break;
10924 case ID_GLOBAL: return NODE_GASGN;
10925 case ID_INSTANCE: return NODE_IASGN;
10926 case ID_CONST:
10927 if (!p->ctxt.in_def) return NODE_CDECL;
10928 *err = "dynamic constant assignment";
10929 return -1;
10930 case ID_CLASS: return NODE_CVASGN;
10931 default:
10932 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10934 return -1;
10937 #ifndef RIPPER
10938 static NODE*
10939 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10941 const char *err = 0;
10942 int node_type = assignable0(p, id, &err);
10943 switch (node_type) {
10944 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10945 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10946 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10947 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10948 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10949 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10951 if (err) yyerror1(loc, err);
10952 return NEW_BEGIN(0, loc);
10954 #else
10955 static VALUE
10956 assignable(struct parser_params *p, VALUE lhs)
10958 const char *err = 0;
10959 assignable0(p, get_id(lhs), &err);
10960 if (err) lhs = assign_error(p, err, lhs);
10961 return lhs;
10963 #endif
10965 static int
10966 is_private_local_id(ID name)
10968 VALUE s;
10969 if (name == idUScore) return 1;
10970 if (!is_local_id(name)) return 0;
10971 s = rb_id2str(name);
10972 if (!s) return 0;
10973 return RSTRING_PTR(s)[0] == '_';
10976 static int
10977 shadowing_lvar_0(struct parser_params *p, ID name)
10979 if (is_private_local_id(name)) return 1;
10980 if (dyna_in_block(p)) {
10981 if (dvar_curr(p, name)) {
10982 yyerror0("duplicated argument name");
10984 else if (dvar_defined(p, name) || local_id(p, name)) {
10985 vtable_add(p->lvtbl->vars, name);
10986 if (p->lvtbl->used) {
10987 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10989 return 0;
10992 else {
10993 if (local_id(p, name)) {
10994 yyerror0("duplicated argument name");
10997 return 1;
11000 static ID
11001 shadowing_lvar(struct parser_params *p, ID name)
11003 shadowing_lvar_0(p, name);
11004 return name;
11007 static void
11008 new_bv(struct parser_params *p, ID name)
11010 if (!name) return;
11011 if (!is_local_id(name)) {
11012 compile_error(p, "invalid local variable - %"PRIsVALUE,
11013 rb_id2str(name));
11014 return;
11016 if (!shadowing_lvar_0(p, name)) return;
11017 dyna_var(p, name);
11020 #ifndef RIPPER
11021 static NODE *
11022 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11024 return NEW_ATTRASGN(recv, tASET, idx, loc);
11027 static void
11028 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11030 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11031 compile_error(p, "both block arg and actual block given");
11035 static NODE *
11036 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11038 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11039 return NEW_ATTRASGN(recv, id, 0, loc);
11042 static void
11043 rb_backref_error(struct parser_params *p, NODE *node)
11045 switch (nd_type(node)) {
11046 case NODE_NTH_REF:
11047 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11048 break;
11049 case NODE_BACK_REF:
11050 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11051 break;
11054 #else
11055 static VALUE
11056 backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11058 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11059 rb_str_append(mesg, ref->nd_cval);
11060 return dispatch2(assign_error, mesg, expr);
11062 #endif
11064 #ifndef RIPPER
11065 static NODE *
11066 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11068 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11069 switch (nd_type(node1)) {
11070 case NODE_LIST:
11071 return list_append(p, node1, node2);
11072 case NODE_BLOCK_PASS:
11073 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11074 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11075 return node1;
11076 case NODE_ARGSPUSH:
11077 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11078 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11079 nd_set_type(node1, NODE_ARGSCAT);
11080 return node1;
11081 case NODE_ARGSCAT:
11082 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11083 node1->nd_body = list_append(p, node1->nd_body, node2);
11084 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11085 return node1;
11087 return NEW_ARGSPUSH(node1, node2, loc);
11090 static NODE *
11091 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11093 if (!node2) return node1;
11094 switch (nd_type(node1)) {
11095 case NODE_BLOCK_PASS:
11096 if (node1->nd_head)
11097 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11098 else
11099 node1->nd_head = NEW_LIST(node2, loc);
11100 return node1;
11101 case NODE_ARGSPUSH:
11102 if (!nd_type_p(node2, NODE_LIST)) break;
11103 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11104 nd_set_type(node1, NODE_ARGSCAT);
11105 return node1;
11106 case NODE_ARGSCAT:
11107 if (!nd_type_p(node2, NODE_LIST) ||
11108 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11109 node1->nd_body = list_concat(node1->nd_body, node2);
11110 return node1;
11112 return NEW_ARGSCAT(node1, node2, loc);
11115 static NODE *
11116 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11118 NODE *n1;
11119 if ((n1 = splat_array(args)) != 0) {
11120 return list_append(p, n1, last_arg);
11122 return arg_append(p, args, last_arg, loc);
11125 static NODE *
11126 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11128 NODE *n1;
11129 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11130 return list_concat(n1, rest_arg);
11132 return arg_concat(p, args, rest_arg, loc);
11135 static NODE *
11136 splat_array(NODE* node)
11138 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11139 if (nd_type_p(node, NODE_LIST)) return node;
11140 return 0;
11143 static void
11144 mark_lvar_used(struct parser_params *p, NODE *rhs)
11146 ID *vidp = NULL;
11147 if (!rhs) return;
11148 switch (nd_type(rhs)) {
11149 case NODE_LASGN:
11150 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11151 if (vidp) *vidp |= LVAR_USED;
11153 break;
11154 case NODE_DASGN:
11155 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11156 if (vidp) *vidp |= LVAR_USED;
11158 break;
11159 #if 0
11160 case NODE_MASGN:
11161 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11162 mark_lvar_used(p, rhs->nd_head);
11164 break;
11165 #endif
11169 static NODE *
11170 const_decl_path(struct parser_params *p, NODE **dest)
11172 NODE *n = *dest;
11173 if (!nd_type_p(n, NODE_CALL)) {
11174 const YYLTYPE *loc = &n->nd_loc;
11175 VALUE path;
11176 if (n->nd_vid) {
11177 path = rb_id2str(n->nd_vid);
11179 else {
11180 n = n->nd_else;
11181 path = rb_ary_new();
11182 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11183 rb_ary_push(path, rb_id2str(n->nd_mid));
11185 if (n && nd_type_p(n, NODE_CONST)) {
11186 // Const::Name
11187 rb_ary_push(path, rb_id2str(n->nd_vid));
11189 else if (n && nd_type_p(n, NODE_COLON3)) {
11190 // ::Const::Name
11191 rb_ary_push(path, rb_str_new(0, 0));
11193 else {
11194 // expression::Name
11195 rb_ary_push(path, rb_str_new_cstr("..."));
11197 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11198 path = rb_fstring(path);
11200 *dest = n = NEW_LIT(path, loc);
11201 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11203 return n;
11206 extern VALUE rb_mRubyVMFrozenCore;
11208 static NODE *
11209 make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11211 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11213 if (copy) {
11214 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11215 NEW_LIST(value, loc), loc);
11217 else {
11218 return NEW_CALL(fcore, rb_intern("make_shareable"),
11219 NEW_LIST(value, loc), loc);
11223 static NODE *
11224 ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11226 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11227 NODE *args = NEW_LIST(value, loc);
11228 args = list_append(p, args, const_decl_path(p, dest));
11229 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11232 static int is_static_content(NODE *node);
11234 static VALUE
11235 shareable_literal_value(NODE *node)
11237 if (!node) return Qnil;
11238 enum node_type type = nd_type(node);
11239 switch (type) {
11240 case NODE_TRUE:
11241 return Qtrue;
11242 case NODE_FALSE:
11243 return Qfalse;
11244 case NODE_NIL:
11245 return Qnil;
11246 case NODE_LIT:
11247 return node->nd_lit;
11248 default:
11249 return Qundef;
11253 #ifndef SHAREABLE_BARE_EXPRESSION
11254 #define SHAREABLE_BARE_EXPRESSION 1
11255 #endif
11257 static NODE *
11258 shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11259 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11261 # define shareable_literal_constant_next(n) \
11262 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11263 VALUE lit = Qnil;
11265 if (!value) return 0;
11266 enum node_type type = nd_type(value);
11267 switch (type) {
11268 case NODE_TRUE:
11269 case NODE_FALSE:
11270 case NODE_NIL:
11271 case NODE_LIT:
11272 return value;
11274 case NODE_DSTR:
11275 if (shareable == shareable_literal) {
11276 value = NEW_CALL(value, idUMinus, 0, loc);
11278 return value;
11280 case NODE_STR:
11281 lit = rb_fstring(value->nd_lit);
11282 nd_set_type(value, NODE_LIT);
11283 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11284 return value;
11286 case NODE_ZLIST:
11287 lit = rb_ary_new();
11288 OBJ_FREEZE_RAW(lit);
11289 NODE *n = NEW_LIT(lit, loc);
11290 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11291 return n;
11293 case NODE_LIST:
11294 lit = rb_ary_new();
11295 for (NODE *n = value; n; n = n->nd_next) {
11296 NODE *elt = n->nd_head;
11297 if (elt) {
11298 elt = shareable_literal_constant_next(elt);
11299 if (elt) {
11300 n->nd_head = elt;
11302 else if (RTEST(lit)) {
11303 rb_ary_clear(lit);
11304 lit = Qfalse;
11307 if (RTEST(lit)) {
11308 VALUE e = shareable_literal_value(elt);
11309 if (e != Qundef) {
11310 rb_ary_push(lit, e);
11312 else {
11313 rb_ary_clear(lit);
11314 lit = Qnil; /* make shareable at runtime */
11318 break;
11320 case NODE_HASH:
11321 if (!value->nd_brace) return 0;
11322 lit = rb_hash_new();
11323 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11324 NODE *key = n->nd_head;
11325 NODE *val = n->nd_next->nd_head;
11326 if (key) {
11327 key = shareable_literal_constant_next(key);
11328 if (key) {
11329 n->nd_head = key;
11331 else if (RTEST(lit)) {
11332 rb_hash_clear(lit);
11333 lit = Qfalse;
11336 if (val) {
11337 val = shareable_literal_constant_next(val);
11338 if (val) {
11339 n->nd_next->nd_head = val;
11341 else if (RTEST(lit)) {
11342 rb_hash_clear(lit);
11343 lit = Qfalse;
11346 if (RTEST(lit)) {
11347 VALUE k = shareable_literal_value(key);
11348 VALUE v = shareable_literal_value(val);
11349 if (k != Qundef && v != Qundef) {
11350 rb_hash_aset(lit, k, v);
11352 else {
11353 rb_hash_clear(lit);
11354 lit = Qnil; /* make shareable at runtime */
11358 break;
11360 default:
11361 if (shareable == shareable_literal &&
11362 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11363 return ensure_shareable_node(p, dest, value, loc);
11365 return 0;
11368 /* Array or Hash */
11369 if (!lit) return 0;
11370 if (NIL_P(lit)) {
11371 // if shareable_literal, all elements should have been ensured
11372 // as shareable
11373 value = make_shareable_node(p, value, false, loc);
11375 else {
11376 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11377 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11380 return value;
11381 # undef shareable_literal_constant_next
11384 static NODE *
11385 shareable_constant_value(struct parser_params *p, enum shareability shareable,
11386 NODE *lhs, NODE *value, const YYLTYPE *loc)
11388 if (!value) return 0;
11389 switch (shareable) {
11390 case shareable_none:
11391 return value;
11393 case shareable_literal:
11395 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11396 if (lit) return lit;
11397 return value;
11399 break;
11401 case shareable_copy:
11402 case shareable_everything:
11404 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11405 if (lit) return lit;
11406 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11408 break;
11410 default:
11411 UNREACHABLE_RETURN(0);
11415 static NODE *
11416 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11418 if (!lhs) return 0;
11420 switch (nd_type(lhs)) {
11421 case NODE_CDECL:
11422 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11423 /* fallthru */
11425 case NODE_GASGN:
11426 case NODE_IASGN:
11427 case NODE_LASGN:
11428 case NODE_DASGN:
11429 case NODE_MASGN:
11430 case NODE_CVASGN:
11431 lhs->nd_value = rhs;
11432 nd_set_loc(lhs, loc);
11433 break;
11435 case NODE_ATTRASGN:
11436 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11437 nd_set_loc(lhs, loc);
11438 break;
11440 default:
11441 /* should not happen */
11442 break;
11445 return lhs;
11448 static NODE *
11449 value_expr_check(struct parser_params *p, NODE *node)
11451 NODE *void_node = 0, *vn;
11453 if (!node) {
11454 rb_warning0("empty expression");
11456 while (node) {
11457 switch (nd_type(node)) {
11458 case NODE_RETURN:
11459 case NODE_BREAK:
11460 case NODE_NEXT:
11461 case NODE_REDO:
11462 case NODE_RETRY:
11463 return void_node ? void_node : node;
11465 case NODE_CASE3:
11466 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
11467 compile_error(p, "unexpected node");
11468 return NULL;
11470 if (node->nd_body->nd_body) {
11471 return NULL;
11473 /* single line pattern matching */
11474 return void_node ? void_node : node;
11476 case NODE_BLOCK:
11477 while (node->nd_next) {
11478 node = node->nd_next;
11480 node = node->nd_head;
11481 break;
11483 case NODE_BEGIN:
11484 node = node->nd_body;
11485 break;
11487 case NODE_IF:
11488 case NODE_UNLESS:
11489 if (!node->nd_body) {
11490 return NULL;
11492 else if (!node->nd_else) {
11493 return NULL;
11495 vn = value_expr_check(p, node->nd_body);
11496 if (!vn) return NULL;
11497 if (!void_node) void_node = vn;
11498 node = node->nd_else;
11499 break;
11501 case NODE_AND:
11502 case NODE_OR:
11503 node = node->nd_1st;
11504 break;
11506 case NODE_LASGN:
11507 case NODE_DASGN:
11508 case NODE_MASGN:
11509 mark_lvar_used(p, node);
11510 return NULL;
11512 default:
11513 return NULL;
11517 return NULL;
11520 static int
11521 value_expr_gen(struct parser_params *p, NODE *node)
11523 NODE *void_node = value_expr_check(p, node);
11524 if (void_node) {
11525 yyerror1(&void_node->nd_loc, "void value expression");
11526 /* or "control never reach"? */
11527 return FALSE;
11529 return TRUE;
11531 static void
11532 void_expr(struct parser_params *p, NODE *node)
11534 const char *useless = 0;
11536 if (!RTEST(ruby_verbose)) return;
11538 if (!node || !(node = nd_once_body(node))) return;
11539 switch (nd_type(node)) {
11540 case NODE_OPCALL:
11541 switch (node->nd_mid) {
11542 case '+':
11543 case '-':
11544 case '*':
11545 case '/':
11546 case '%':
11547 case tPOW:
11548 case tUPLUS:
11549 case tUMINUS:
11550 case '|':
11551 case '^':
11552 case '&':
11553 case tCMP:
11554 case '>':
11555 case tGEQ:
11556 case '<':
11557 case tLEQ:
11558 case tEQ:
11559 case tNEQ:
11560 useless = rb_id2name(node->nd_mid);
11561 break;
11563 break;
11565 case NODE_LVAR:
11566 case NODE_DVAR:
11567 case NODE_GVAR:
11568 case NODE_IVAR:
11569 case NODE_CVAR:
11570 case NODE_NTH_REF:
11571 case NODE_BACK_REF:
11572 useless = "a variable";
11573 break;
11574 case NODE_CONST:
11575 useless = "a constant";
11576 break;
11577 case NODE_LIT:
11578 case NODE_STR:
11579 case NODE_DSTR:
11580 case NODE_DREGX:
11581 useless = "a literal";
11582 break;
11583 case NODE_COLON2:
11584 case NODE_COLON3:
11585 useless = "::";
11586 break;
11587 case NODE_DOT2:
11588 useless = "..";
11589 break;
11590 case NODE_DOT3:
11591 useless = "...";
11592 break;
11593 case NODE_SELF:
11594 useless = "self";
11595 break;
11596 case NODE_NIL:
11597 useless = "nil";
11598 break;
11599 case NODE_TRUE:
11600 useless = "true";
11601 break;
11602 case NODE_FALSE:
11603 useless = "false";
11604 break;
11605 case NODE_DEFINED:
11606 useless = "defined?";
11607 break;
11610 if (useless) {
11611 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
11615 static NODE *
11616 void_stmts(struct parser_params *p, NODE *node)
11618 NODE *const n = node;
11619 if (!RTEST(ruby_verbose)) return n;
11620 if (!node) return n;
11621 if (!nd_type_p(node, NODE_BLOCK)) return n;
11623 while (node->nd_next) {
11624 void_expr(p, node->nd_head);
11625 node = node->nd_next;
11627 return n;
11630 static NODE *
11631 remove_begin(NODE *node)
11633 NODE **n = &node, *n1 = node;
11634 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
11635 *n = n1 = n1->nd_body;
11637 return node;
11640 static NODE *
11641 remove_begin_all(NODE *node)
11643 NODE **n = &node, *n1 = node;
11644 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
11645 *n = n1 = n1->nd_body;
11647 return node;
11650 static void
11651 reduce_nodes(struct parser_params *p, NODE **body)
11653 NODE *node = *body;
11655 if (!node) {
11656 *body = NEW_NIL(&NULL_LOC);
11657 return;
11659 #define subnodes(n1, n2) \
11660 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
11661 (!node->n2) ? (body = &node->n1, 1) : \
11662 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
11664 while (node) {
11665 int newline = (int)(node->flags & NODE_FL_NEWLINE);
11666 switch (nd_type(node)) {
11667 end:
11668 case NODE_NIL:
11669 *body = 0;
11670 return;
11671 case NODE_RETURN:
11672 *body = node = node->nd_stts;
11673 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11674 continue;
11675 case NODE_BEGIN:
11676 *body = node = node->nd_body;
11677 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11678 continue;
11679 case NODE_BLOCK:
11680 body = &node->nd_end->nd_head;
11681 break;
11682 case NODE_IF:
11683 case NODE_UNLESS:
11684 if (subnodes(nd_body, nd_else)) break;
11685 return;
11686 case NODE_CASE:
11687 body = &node->nd_body;
11688 break;
11689 case NODE_WHEN:
11690 if (!subnodes(nd_body, nd_next)) goto end;
11691 break;
11692 case NODE_ENSURE:
11693 if (!subnodes(nd_head, nd_resq)) goto end;
11694 break;
11695 case NODE_RESCUE:
11696 if (node->nd_else) {
11697 body = &node->nd_resq;
11698 break;
11700 if (!subnodes(nd_head, nd_resq)) goto end;
11701 break;
11702 default:
11703 return;
11705 node = *body;
11706 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11709 #undef subnodes
11712 static int
11713 is_static_content(NODE *node)
11715 if (!node) return 1;
11716 switch (nd_type(node)) {
11717 case NODE_HASH:
11718 if (!(node = node->nd_head)) break;
11719 case NODE_LIST:
11720 do {
11721 if (!is_static_content(node->nd_head)) return 0;
11722 } while ((node = node->nd_next) != 0);
11723 case NODE_LIT:
11724 case NODE_STR:
11725 case NODE_NIL:
11726 case NODE_TRUE:
11727 case NODE_FALSE:
11728 case NODE_ZLIST:
11729 break;
11730 default:
11731 return 0;
11733 return 1;
11736 static int
11737 assign_in_cond(struct parser_params *p, NODE *node)
11739 switch (nd_type(node)) {
11740 case NODE_MASGN:
11741 case NODE_LASGN:
11742 case NODE_DASGN:
11743 case NODE_GASGN:
11744 case NODE_IASGN:
11745 break;
11747 default:
11748 return 0;
11751 if (!node->nd_value) return 1;
11752 if (is_static_content(node->nd_value)) {
11753 /* reports always */
11754 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11756 return 1;
11759 enum cond_type {
11760 COND_IN_OP,
11761 COND_IN_COND,
11762 COND_IN_FF
11765 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11766 switch (t) { \
11767 case COND_IN_OP: break; \
11768 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11769 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11772 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11774 static NODE*
11775 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11777 enum node_type type;
11779 if (node == 0) return 0;
11781 type = nd_type(node);
11782 value_expr(node);
11783 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11784 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11785 ID lineno = rb_intern("$.");
11786 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
11788 return cond0(p, node, COND_IN_FF, loc);
11791 static NODE*
11792 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11794 if (node == 0) return 0;
11795 if (!(node = nd_once_body(node))) return 0;
11796 assign_in_cond(p, node);
11798 switch (nd_type(node)) {
11799 case NODE_DSTR:
11800 case NODE_EVSTR:
11801 case NODE_STR:
11802 SWITCH_BY_COND_TYPE(type, warn, "string ")
11803 break;
11805 case NODE_DREGX:
11806 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11808 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11810 case NODE_AND:
11811 case NODE_OR:
11812 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11813 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11814 break;
11816 case NODE_DOT2:
11817 case NODE_DOT3:
11818 node->nd_beg = range_op(p, node->nd_beg, loc);
11819 node->nd_end = range_op(p, node->nd_end, loc);
11820 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
11821 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
11822 break;
11824 case NODE_DSYM:
11825 warn_symbol:
11826 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
11827 break;
11829 case NODE_LIT:
11830 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11831 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11832 nd_set_type(node, NODE_MATCH);
11834 else if (node->nd_lit == Qtrue ||
11835 node->nd_lit == Qfalse) {
11836 /* booleans are OK, e.g., while true */
11838 else if (SYMBOL_P(node->nd_lit)) {
11839 goto warn_symbol;
11841 else {
11842 SWITCH_BY_COND_TYPE(type, warning, "")
11844 default:
11845 break;
11847 return node;
11850 static NODE*
11851 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11853 if (node == 0) return 0;
11854 return cond0(p, node, COND_IN_COND, loc);
11857 static NODE*
11858 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11860 if (node == 0) return 0;
11861 return cond0(p, node, COND_IN_OP, loc);
11864 static NODE*
11865 new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
11867 YYLTYPE loc = {*pos, *pos};
11868 return NEW_NIL(&loc);
11871 static NODE*
11872 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11874 if (!cc) return right;
11875 cc = cond0(p, cc, COND_IN_COND, loc);
11876 return newline_node(NEW_IF(cc, left, right, loc));
11879 static NODE*
11880 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11882 if (!cc) return right;
11883 cc = cond0(p, cc, COND_IN_COND, loc);
11884 return newline_node(NEW_UNLESS(cc, left, right, loc));
11887 static NODE*
11888 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11889 const YYLTYPE *op_loc, const YYLTYPE *loc)
11891 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11892 NODE *op;
11893 value_expr(left);
11894 if (left && nd_type_p(left, type)) {
11895 NODE *node = left, *second;
11896 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
11897 node = second;
11899 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11900 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11901 left->nd_loc.end_pos = loc->end_pos;
11902 return left;
11904 op = NEW_NODE(type, left, right, 0, loc);
11905 nd_set_line(op, op_loc->beg_pos.lineno);
11906 return op;
11909 static void
11910 no_blockarg(struct parser_params *p, NODE *node)
11912 if (node && nd_type_p(node, NODE_BLOCK_PASS)) {
11913 compile_error(p, "block argument should not be given");
11917 static NODE *
11918 ret_args(struct parser_params *p, NODE *node)
11920 if (node) {
11921 no_blockarg(p, node);
11922 if (nd_type_p(node, NODE_LIST)) {
11923 if (node->nd_next == 0) {
11924 node = node->nd_head;
11926 else {
11927 nd_set_type(node, NODE_VALUES);
11931 return node;
11934 static NODE *
11935 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11937 if (node) no_blockarg(p, node);
11939 return NEW_YIELD(node, loc);
11942 static VALUE
11943 negate_lit(struct parser_params *p, VALUE lit)
11945 if (FIXNUM_P(lit)) {
11946 return LONG2FIX(-FIX2LONG(lit));
11948 if (SPECIAL_CONST_P(lit)) {
11949 #if USE_FLONUM
11950 if (FLONUM_P(lit)) {
11951 return DBL2NUM(-RFLOAT_VALUE(lit));
11953 #endif
11954 goto unknown;
11956 switch (BUILTIN_TYPE(lit)) {
11957 case T_BIGNUM:
11958 BIGNUM_NEGATE(lit);
11959 lit = rb_big_norm(lit);
11960 break;
11961 case T_RATIONAL:
11962 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11963 break;
11964 case T_COMPLEX:
11965 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11966 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11967 break;
11968 case T_FLOAT:
11969 lit = DBL2NUM(-RFLOAT_VALUE(lit));
11970 break;
11971 unknown:
11972 default:
11973 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11974 rb_builtin_class_name(lit));
11975 break;
11977 return lit;
11980 static NODE *
11981 arg_blk_pass(NODE *node1, NODE *node2)
11983 if (node2) {
11984 if (!node1) return node2;
11985 node2->nd_head = node1;
11986 nd_set_first_lineno(node2, nd_first_lineno(node1));
11987 nd_set_first_column(node2, nd_first_column(node1));
11988 return node2;
11990 return node1;
11993 static bool
11994 args_info_empty_p(struct rb_args_info *args)
11996 if (args->pre_args_num) return false;
11997 if (args->post_args_num) return false;
11998 if (args->rest_arg) return false;
11999 if (args->opt_args) return false;
12000 if (args->block_arg) return false;
12001 if (args->kw_args) return false;
12002 if (args->kw_rest_arg) return false;
12003 return true;
12006 static NODE*
12007 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12009 int saved_line = p->ruby_sourceline;
12010 struct rb_args_info *args = tail->nd_ainfo;
12012 if (args->block_arg == idFWD_BLOCK) {
12013 if (rest_arg) {
12014 yyerror1(&tail->nd_loc, "... after rest argument");
12015 return tail;
12017 rest_arg = idFWD_REST;
12020 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12021 args->pre_init = pre_args ? pre_args->nd_next : 0;
12023 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12024 args->post_init = post_args ? post_args->nd_next : 0;
12025 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12027 args->rest_arg = rest_arg;
12029 args->opt_args = opt_args;
12031 args->ruby2_keywords = rest_arg == idFWD_REST;
12033 p->ruby_sourceline = saved_line;
12034 nd_set_loc(tail, loc);
12036 return tail;
12039 static NODE*
12040 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12042 int saved_line = p->ruby_sourceline;
12043 NODE *node;
12044 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12045 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12046 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12047 args->imemo = tmpbuf;
12048 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12049 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12050 if (p->error_p) return node;
12052 args->block_arg = block;
12053 args->kw_args = kw_args;
12055 if (kw_args) {
12057 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12058 * variable order: k1, kr1, k2, &b, internal_id, krest
12059 * #=> <reorder>
12060 * variable order: kr1, k1, k2, internal_id, krest, &b
12062 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12063 struct vtable *vtargs = p->lvtbl->args;
12064 NODE *kwn = kw_args;
12066 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12067 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12068 while (kwn) {
12069 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12070 --kw_vars;
12071 --required_kw_vars;
12072 kwn = kwn->nd_next;
12075 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12076 ID vid = kwn->nd_body->nd_vid;
12077 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12078 *required_kw_vars++ = vid;
12080 else {
12081 *kw_vars++ = vid;
12085 arg_var(p, kw_bits);
12086 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12087 if (block) arg_var(p, block);
12089 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12090 args->kw_rest_arg->nd_cflag = kw_bits;
12092 else if (kw_rest_arg == idNil) {
12093 args->no_kwarg = 1;
12095 else if (kw_rest_arg) {
12096 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12099 p->ruby_sourceline = saved_line;
12100 return node;
12103 static NODE *
12104 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12106 if (max_numparam > NO_PARAM) {
12107 if (!args) {
12108 YYLTYPE loc = RUBY_INIT_YYLLOC();
12109 args = new_args_tail(p, 0, 0, 0, 0);
12110 nd_set_loc(args, &loc);
12112 args->nd_ainfo->pre_args_num = max_numparam;
12114 return args;
12117 static NODE*
12118 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12120 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12122 aryptn->nd_pconst = constant;
12124 if (pre_arg) {
12125 NODE *pre_args = NEW_LIST(pre_arg, loc);
12126 if (apinfo->pre_args) {
12127 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12129 else {
12130 apinfo->pre_args = pre_args;
12133 return aryptn;
12136 static NODE*
12137 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12139 int saved_line = p->ruby_sourceline;
12140 NODE *node;
12141 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12142 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12143 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12144 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12145 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12147 apinfo->pre_args = pre_args;
12149 if (has_rest) {
12150 if (rest_arg) {
12151 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12153 else {
12154 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12157 else {
12158 apinfo->rest_arg = NULL;
12161 apinfo->post_args = post_args;
12163 p->ruby_sourceline = saved_line;
12164 return node;
12167 static NODE*
12168 new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12170 fndptn->nd_pconst = constant;
12172 return fndptn;
12175 static NODE*
12176 new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12178 int saved_line = p->ruby_sourceline;
12179 NODE *node;
12180 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12181 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12182 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12183 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12184 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12186 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12187 fpinfo->args = args;
12188 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12190 p->ruby_sourceline = saved_line;
12191 return node;
12194 static NODE*
12195 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12197 hshptn->nd_pconst = constant;
12198 return hshptn;
12201 static NODE*
12202 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12204 int saved_line = p->ruby_sourceline;
12205 NODE *node, *kw_rest_arg_node;
12207 if (kw_rest_arg == idNil) {
12208 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12210 else if (kw_rest_arg) {
12211 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12213 else {
12214 kw_rest_arg_node = NULL;
12217 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12219 p->ruby_sourceline = saved_line;
12220 return node;
12223 static NODE*
12224 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12226 VALUE lit;
12228 if (!node) {
12229 return NEW_LIT(ID2SYM(idNULL), loc);
12232 switch (nd_type(node)) {
12233 case NODE_DSTR:
12234 nd_set_type(node, NODE_DSYM);
12235 nd_set_loc(node, loc);
12236 break;
12237 case NODE_STR:
12238 lit = node->nd_lit;
12239 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12240 nd_set_type(node, NODE_LIT);
12241 nd_set_loc(node, loc);
12242 break;
12243 default:
12244 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12245 break;
12247 return node;
12250 static int
12251 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12253 NODE *node = (NODE *)v;
12254 NODE **result = (NODE **)h;
12255 node->nd_alen = 2;
12256 node->nd_next->nd_end = node->nd_next;
12257 node->nd_next->nd_next = 0;
12258 if (*result)
12259 list_concat(*result, node);
12260 else
12261 *result = node;
12262 return ST_CONTINUE;
12265 static bool
12266 hash_literal_key_p(VALUE k)
12268 switch (OBJ_BUILTIN_TYPE(k)) {
12269 case T_NODE:
12270 return false;
12271 default:
12272 return true;
12276 static int
12277 literal_cmp(VALUE val, VALUE lit)
12279 if (val == lit) return 0;
12280 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12281 return rb_iseq_cdhash_cmp(val, lit);
12284 static st_index_t
12285 literal_hash(VALUE a)
12287 if (!hash_literal_key_p(a)) return (st_index_t)a;
12288 return rb_iseq_cdhash_hash(a);
12291 static const struct st_hash_type literal_type = {
12292 literal_cmp,
12293 literal_hash,
12296 static NODE *
12297 remove_duplicate_keys(struct parser_params *p, NODE *hash)
12299 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12300 NODE *result = 0;
12301 NODE *last_expr = 0;
12302 rb_code_location_t loc = hash->nd_loc;
12303 while (hash && hash->nd_head && hash->nd_next) {
12304 NODE *head = hash->nd_head;
12305 NODE *value = hash->nd_next;
12306 NODE *next = value->nd_next;
12307 st_data_t key = (st_data_t)head;
12308 st_data_t data;
12309 value->nd_next = 0;
12310 if (nd_type_p(head, NODE_LIT) &&
12311 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12312 NODE *dup_value = ((NODE *)data)->nd_next;
12313 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12314 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12315 head->nd_lit, nd_line(head));
12316 if (dup_value == last_expr) {
12317 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12319 else {
12320 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12323 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12324 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12325 hash = next;
12327 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12328 st_free_table(literal_keys);
12329 if (hash) {
12330 if (!result) result = hash;
12331 else list_concat(result, hash);
12333 result->nd_loc = loc;
12334 return result;
12337 static NODE *
12338 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12340 if (hash) hash = remove_duplicate_keys(p, hash);
12341 return NEW_HASH(hash, loc);
12343 #endif
12345 static void
12346 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12348 if (is_private_local_id(id)) {
12349 return;
12351 if (st_is_member(p->pvtbl, id)) {
12352 yyerror1(loc, "duplicated variable name");
12354 else {
12355 st_insert(p->pvtbl, (st_data_t)id, 0);
12359 static void
12360 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12362 if (!p->pktbl) {
12363 p->pktbl = st_init_numtable();
12365 else if (st_is_member(p->pktbl, key)) {
12366 yyerror1(loc, "duplicated key name");
12367 return;
12369 st_insert(p->pktbl, (st_data_t)key, 0);
12372 #ifndef RIPPER
12373 static NODE *
12374 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12376 return NEW_HASH(hash, loc);
12378 #endif /* !RIPPER */
12380 #ifndef RIPPER
12381 static NODE *
12382 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12384 NODE *asgn;
12386 if (lhs) {
12387 ID vid = lhs->nd_vid;
12388 YYLTYPE lhs_loc = lhs->nd_loc;
12389 int shareable = ctxt.shareable_constant_value;
12390 if (shareable) {
12391 switch (nd_type(lhs)) {
12392 case NODE_CDECL:
12393 case NODE_COLON2:
12394 case NODE_COLON3:
12395 break;
12396 default:
12397 shareable = 0;
12398 break;
12401 if (op == tOROP) {
12402 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12403 lhs->nd_value = rhs;
12404 nd_set_loc(lhs, loc);
12405 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12406 if (is_notop_id(vid)) {
12407 switch (id_type(vid)) {
12408 case ID_GLOBAL:
12409 case ID_INSTANCE:
12410 case ID_CLASS:
12411 asgn->nd_aid = vid;
12415 else if (op == tANDOP) {
12416 if (shareable) {
12417 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_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12423 else {
12424 asgn = lhs;
12425 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12426 if (shareable) {
12427 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12429 asgn->nd_value = rhs;
12430 nd_set_loc(asgn, loc);
12433 else {
12434 asgn = NEW_BEGIN(0, loc);
12436 return asgn;
12439 static NODE *
12440 new_ary_op_assign(struct parser_params *p, NODE *ary,
12441 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
12443 NODE *asgn;
12445 args = make_list(args, args_loc);
12446 if (nd_type_p(args, NODE_BLOCK_PASS)) {
12447 args = NEW_ARGSCAT(args, rhs, loc);
12449 else {
12450 args = arg_concat(p, args, rhs, loc);
12452 asgn = NEW_OP_ASGN1(ary, op, args, loc);
12453 fixpos(asgn, ary);
12454 return asgn;
12457 static NODE *
12458 new_attr_op_assign(struct parser_params *p, NODE *lhs,
12459 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
12461 NODE *asgn;
12463 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
12464 fixpos(asgn, lhs);
12465 return asgn;
12468 static NODE *
12469 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12471 NODE *asgn;
12473 if (lhs) {
12474 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12475 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
12477 else {
12478 asgn = NEW_BEGIN(0, loc);
12480 fixpos(asgn, lhs);
12481 return asgn;
12484 static NODE *
12485 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
12487 if (p->ctxt.in_def) {
12488 yyerror1(loc, "dynamic constant assignment");
12490 return NEW_CDECL(0, 0, (path), loc);
12492 #else
12493 static VALUE
12494 const_decl(struct parser_params *p, VALUE path)
12496 if (p->ctxt.in_def) {
12497 path = assign_error(p, "dynamic constant assignment", path);
12499 return path;
12502 static VALUE
12503 assign_error(struct parser_params *p, const char *mesg, VALUE a)
12505 a = dispatch2(assign_error, ERR_MESG(), a);
12506 ripper_error(p);
12507 return a;
12510 static VALUE
12511 var_field(struct parser_params *p, VALUE a)
12513 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
12515 #endif
12517 #ifndef RIPPER
12518 static NODE *
12519 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
12521 NODE *result = head;
12522 if (rescue) {
12523 NODE *tmp = rescue_else ? rescue_else : rescue;
12524 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
12526 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
12527 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
12529 else if (rescue_else) {
12530 result = block_append(p, result, rescue_else);
12532 if (ensure) {
12533 result = NEW_ENSURE(result, ensure, loc);
12535 fixpos(result, head);
12536 return result;
12538 #endif
12540 static void
12541 warn_unused_var(struct parser_params *p, struct local_vars *local)
12543 int cnt;
12545 if (!local->used) return;
12546 cnt = local->used->pos;
12547 if (cnt != local->vars->pos) {
12548 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
12550 #ifndef RIPPER
12551 ID *v = local->vars->tbl;
12552 ID *u = local->used->tbl;
12553 for (int i = 0; i < cnt; ++i) {
12554 if (!v[i] || (u[i] & LVAR_USED)) continue;
12555 if (is_private_local_id(v[i])) continue;
12556 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
12558 #endif
12561 static void
12562 local_push(struct parser_params *p, int toplevel_scope)
12564 struct local_vars *local;
12565 int inherits_dvars = toplevel_scope && compile_for_eval;
12566 int warn_unused_vars = RTEST(ruby_verbose);
12568 local = ALLOC(struct local_vars);
12569 local->prev = p->lvtbl;
12570 local->args = vtable_alloc(0);
12571 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
12572 #ifndef RIPPER
12573 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
12574 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
12575 local->numparam.outer = 0;
12576 local->numparam.inner = 0;
12577 local->numparam.current = 0;
12578 #endif
12579 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
12581 # if WARN_PAST_SCOPE
12582 local->past = 0;
12583 # endif
12584 CMDARG_PUSH(0);
12585 COND_PUSH(0);
12586 p->lvtbl = local;
12589 static void
12590 local_pop(struct parser_params *p)
12592 struct local_vars *local = p->lvtbl->prev;
12593 if (p->lvtbl->used) {
12594 warn_unused_var(p, p->lvtbl);
12595 vtable_free(p->lvtbl->used);
12597 # if WARN_PAST_SCOPE
12598 while (p->lvtbl->past) {
12599 struct vtable *past = p->lvtbl->past;
12600 p->lvtbl->past = past->prev;
12601 vtable_free(past);
12603 # endif
12604 vtable_free(p->lvtbl->args);
12605 vtable_free(p->lvtbl->vars);
12606 CMDARG_POP();
12607 COND_POP();
12608 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12609 p->lvtbl = local;
12612 #ifndef RIPPER
12613 static rb_ast_id_table_t *
12614 local_tbl(struct parser_params *p)
12616 int cnt_args = vtable_size(p->lvtbl->args);
12617 int cnt_vars = vtable_size(p->lvtbl->vars);
12618 int cnt = cnt_args + cnt_vars;
12619 int i, j;
12620 rb_ast_id_table_t *tbl;
12622 if (cnt <= 0) return 0;
12623 tbl = rb_ast_new_local_table(p->ast, cnt);
12624 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
12625 /* remove IDs duplicated to warn shadowing */
12626 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
12627 ID id = p->lvtbl->vars->tbl[i];
12628 if (!vtable_included(p->lvtbl->args, id)) {
12629 tbl->ids[j++] = id;
12632 if (j < cnt) {
12633 tbl = rb_ast_resize_latest_local_table(p->ast, j);
12636 return tbl;
12639 static NODE*
12640 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
12642 rb_ast_id_table_t *a0;
12643 NODE *n;
12645 a0 = local_tbl(p);
12646 n = NEW_NODE(type, a0, a1, a2, loc);
12647 return n;
12650 #endif
12652 static void
12653 numparam_name(struct parser_params *p, ID id)
12655 if (!NUMPARAM_ID_P(id)) return;
12656 compile_error(p, "_%d is reserved for numbered parameter",
12657 NUMPARAM_ID_TO_IDX(id));
12660 static void
12661 arg_var(struct parser_params *p, ID id)
12663 numparam_name(p, id);
12664 vtable_add(p->lvtbl->args, id);
12667 static void
12668 local_var(struct parser_params *p, ID id)
12670 numparam_name(p, id);
12671 vtable_add(p->lvtbl->vars, id);
12672 if (p->lvtbl->used) {
12673 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
12677 static int
12678 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
12680 struct vtable *vars, *args, *used;
12682 vars = p->lvtbl->vars;
12683 args = p->lvtbl->args;
12684 used = p->lvtbl->used;
12686 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
12687 vars = vars->prev;
12688 args = args->prev;
12689 if (used) used = used->prev;
12692 if (vars && vars->prev == DVARS_INHERIT) {
12693 return rb_local_defined(id, p->parent_iseq);
12695 else if (vtable_included(args, id)) {
12696 return 1;
12698 else {
12699 int i = vtable_included(vars, id);
12700 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
12701 return i != 0;
12705 static int
12706 local_id(struct parser_params *p, ID id)
12708 return local_id_ref(p, id, NULL);
12711 static int
12712 check_forwarding_args(struct parser_params *p)
12714 if (local_id(p, idFWD_REST) &&
12715 #if idFWD_KWREST
12716 local_id(p, idFWD_KWREST) &&
12717 #endif
12718 local_id(p, idFWD_BLOCK)) return TRUE;
12719 compile_error(p, "unexpected ...");
12720 return FALSE;
12723 static void
12724 add_forwarding_args(struct parser_params *p)
12726 arg_var(p, idFWD_REST);
12727 #if idFWD_KWREST
12728 arg_var(p, idFWD_KWREST);
12729 #endif
12730 arg_var(p, idFWD_BLOCK);
12733 #ifndef RIPPER
12734 static NODE *
12735 new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
12737 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, loc), loc);
12738 #if idFWD_KWREST
12739 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
12740 #endif
12741 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
12742 NODE *args = leading ? rest_arg_append(p, leading, splat, argsloc) : splat;
12743 #if idFWD_KWREST
12744 args = arg_append(p, splat, new_hash(p, kwrest, loc), loc);
12745 #endif
12746 return arg_blk_pass(args, block);
12748 #endif
12750 static NODE *
12751 numparam_push(struct parser_params *p)
12753 #ifndef RIPPER
12754 struct local_vars *local = p->lvtbl;
12755 NODE *inner = local->numparam.inner;
12756 if (!local->numparam.outer) {
12757 local->numparam.outer = local->numparam.current;
12759 local->numparam.inner = 0;
12760 local->numparam.current = 0;
12761 return inner;
12762 #else
12763 return 0;
12764 #endif
12767 static void
12768 numparam_pop(struct parser_params *p, NODE *prev_inner)
12770 #ifndef RIPPER
12771 struct local_vars *local = p->lvtbl;
12772 if (prev_inner) {
12773 /* prefer first one */
12774 local->numparam.inner = prev_inner;
12776 else if (local->numparam.current) {
12777 /* current and inner are exclusive */
12778 local->numparam.inner = local->numparam.current;
12780 if (p->max_numparam > NO_PARAM) {
12781 /* current and outer are exclusive */
12782 local->numparam.current = local->numparam.outer;
12783 local->numparam.outer = 0;
12785 else {
12786 /* no numbered parameter */
12787 local->numparam.current = 0;
12789 #endif
12792 static const struct vtable *
12793 dyna_push(struct parser_params *p)
12795 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
12796 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
12797 if (p->lvtbl->used) {
12798 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
12800 return p->lvtbl->args;
12803 static void
12804 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
12806 struct vtable *tmp = *vtblp;
12807 *vtblp = tmp->prev;
12808 # if WARN_PAST_SCOPE
12809 if (p->past_scope_enabled) {
12810 tmp->prev = p->lvtbl->past;
12811 p->lvtbl->past = tmp;
12812 return;
12814 # endif
12815 vtable_free(tmp);
12818 static void
12819 dyna_pop_1(struct parser_params *p)
12821 struct vtable *tmp;
12823 if ((tmp = p->lvtbl->used) != 0) {
12824 warn_unused_var(p, p->lvtbl);
12825 p->lvtbl->used = p->lvtbl->used->prev;
12826 vtable_free(tmp);
12828 dyna_pop_vtable(p, &p->lvtbl->args);
12829 dyna_pop_vtable(p, &p->lvtbl->vars);
12832 static void
12833 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12835 while (p->lvtbl->args != lvargs) {
12836 dyna_pop_1(p);
12837 if (!p->lvtbl->args) {
12838 struct local_vars *local = p->lvtbl->prev;
12839 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12840 p->lvtbl = local;
12843 dyna_pop_1(p);
12846 static int
12847 dyna_in_block(struct parser_params *p)
12849 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12852 static int
12853 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12855 struct vtable *vars, *args, *used;
12856 int i;
12858 args = p->lvtbl->args;
12859 vars = p->lvtbl->vars;
12860 used = p->lvtbl->used;
12862 while (!DVARS_TERMINAL_P(vars)) {
12863 if (vtable_included(args, id)) {
12864 return 1;
12866 if ((i = vtable_included(vars, id)) != 0) {
12867 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12868 return 1;
12870 args = args->prev;
12871 vars = vars->prev;
12872 if (!vidrefp) used = 0;
12873 if (used) used = used->prev;
12876 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
12877 return rb_dvar_defined(id, p->parent_iseq);
12880 return 0;
12883 static int
12884 dvar_defined(struct parser_params *p, ID id)
12886 return dvar_defined_ref(p, id, NULL);
12889 static int
12890 dvar_curr(struct parser_params *p, ID id)
12892 return (vtable_included(p->lvtbl->args, id) ||
12893 vtable_included(p->lvtbl->vars, id));
12896 static void
12897 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12899 compile_error(p,
12900 "regexp encoding option '%c' differs from source encoding '%s'",
12901 c, rb_enc_name(rb_enc_get(str)));
12904 #ifndef RIPPER
12906 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12908 int c = RE_OPTION_ENCODING_IDX(options);
12910 if (c) {
12911 int opt, idx;
12912 rb_char_to_option_kcode(c, &opt, &idx);
12913 if (idx != ENCODING_GET(str) &&
12914 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12915 goto error;
12917 ENCODING_SET(str, idx);
12919 else if (RE_OPTION_ENCODING_NONE(options)) {
12920 if (!ENCODING_IS_ASCII8BIT(str) &&
12921 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12922 c = 'n';
12923 goto error;
12925 rb_enc_associate(str, rb_ascii8bit_encoding());
12927 else if (p->enc == rb_usascii_encoding()) {
12928 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12929 /* raise in re.c */
12930 rb_enc_associate(str, rb_usascii_encoding());
12932 else {
12933 rb_enc_associate(str, rb_ascii8bit_encoding());
12936 return 0;
12938 error:
12939 return c;
12942 static void
12943 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12945 int c = rb_reg_fragment_setenc(p, str, options);
12946 if (c) reg_fragment_enc_error(p, str, c);
12949 static int
12950 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12952 VALUE err;
12953 reg_fragment_setenc(p, str, options);
12954 err = rb_reg_check_preprocess(str);
12955 if (err != Qnil) {
12956 err = rb_obj_as_string(err);
12957 compile_error(p, "%"PRIsVALUE, err);
12958 return 0;
12960 return 1;
12963 typedef struct {
12964 struct parser_params* parser;
12965 rb_encoding *enc;
12966 NODE *succ_block;
12967 const YYLTYPE *loc;
12968 } reg_named_capture_assign_t;
12970 static int
12971 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12972 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12974 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12975 struct parser_params* p = arg->parser;
12976 rb_encoding *enc = arg->enc;
12977 long len = name_end - name;
12978 const char *s = (const char *)name;
12979 ID var;
12980 NODE *node, *succ;
12982 if (!len) return ST_CONTINUE;
12983 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12984 return ST_CONTINUE;
12986 var = intern_cstr(s, len, enc);
12987 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12988 if (!lvar_defined(p, var)) return ST_CONTINUE;
12990 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
12991 succ = arg->succ_block;
12992 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12993 succ = block_append(p, succ, node);
12994 arg->succ_block = succ;
12995 return ST_CONTINUE;
12998 static NODE *
12999 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13001 reg_named_capture_assign_t arg;
13003 arg.parser = p;
13004 arg.enc = rb_enc_get(regexp);
13005 arg.succ_block = 0;
13006 arg.loc = loc;
13007 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13009 if (!arg.succ_block) return 0;
13010 return arg.succ_block->nd_next;
13013 static VALUE
13014 parser_reg_compile(struct parser_params* p, VALUE str, int options)
13016 reg_fragment_setenc(p, str, options);
13017 return rb_parser_reg_compile(p, str, options);
13020 VALUE
13021 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13023 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13026 static VALUE
13027 reg_compile(struct parser_params* p, VALUE str, int options)
13029 VALUE re;
13030 VALUE err;
13032 err = rb_errinfo();
13033 re = parser_reg_compile(p, str, options);
13034 if (NIL_P(re)) {
13035 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13036 rb_set_errinfo(err);
13037 compile_error(p, "%"PRIsVALUE, m);
13038 return Qnil;
13040 return re;
13042 #else
13043 static VALUE
13044 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13046 VALUE err = rb_errinfo();
13047 VALUE re;
13048 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13049 int c = rb_reg_fragment_setenc(p, str, options);
13050 if (c) reg_fragment_enc_error(p, str, c);
13051 re = rb_parser_reg_compile(p, str, options);
13052 if (NIL_P(re)) {
13053 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13054 rb_set_errinfo(err);
13056 return re;
13058 #endif
13060 #ifndef RIPPER
13061 void
13062 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13064 struct parser_params *p;
13065 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13066 p->do_print = print;
13067 p->do_loop = loop;
13068 p->do_chomp = chomp;
13069 p->do_split = split;
13072 static NODE *
13073 parser_append_options(struct parser_params *p, NODE *node)
13075 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13076 const YYLTYPE *const LOC = &default_location;
13078 if (p->do_print) {
13079 NODE *print = NEW_FCALL(rb_intern("print"),
13080 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13081 LOC);
13082 node = block_append(p, node, print);
13085 if (p->do_loop) {
13086 if (p->do_split) {
13087 ID ifs = rb_intern("$;");
13088 ID fields = rb_intern("$F");
13089 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13090 NODE *split = NEW_GASGN(fields,
13091 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13092 rb_intern("split"), args, LOC),
13093 LOC);
13094 node = block_append(p, split, node);
13096 if (p->do_chomp) {
13097 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13098 rb_intern("chomp!"), 0, LOC);
13099 node = block_append(p, chomp, node);
13102 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
13105 return node;
13108 void
13109 rb_init_parse(void)
13111 /* just to suppress unused-function warnings */
13112 (void)nodetype;
13113 (void)nodeline;
13116 static ID
13117 internal_id(struct parser_params *p)
13119 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13121 #endif /* !RIPPER */
13123 static void
13124 parser_initialize(struct parser_params *p)
13126 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13127 p->command_start = TRUE;
13128 p->ruby_sourcefile_string = Qnil;
13129 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13130 p->node_id = 0;
13131 #ifdef RIPPER
13132 p->delayed.token = Qnil;
13133 p->result = Qnil;
13134 p->parsing_thread = Qnil;
13135 #else
13136 p->error_buffer = Qfalse;
13137 #endif
13138 p->debug_buffer = Qnil;
13139 p->debug_output = rb_ractor_stdout();
13140 p->enc = rb_utf8_encoding();
13143 #ifdef RIPPER
13144 #define parser_mark ripper_parser_mark
13145 #define parser_free ripper_parser_free
13146 #endif
13148 static void
13149 parser_mark(void *ptr)
13151 struct parser_params *p = (struct parser_params*)ptr;
13153 rb_gc_mark(p->lex.input);
13154 rb_gc_mark(p->lex.prevline);
13155 rb_gc_mark(p->lex.lastline);
13156 rb_gc_mark(p->lex.nextline);
13157 rb_gc_mark(p->ruby_sourcefile_string);
13158 rb_gc_mark((VALUE)p->lex.strterm);
13159 rb_gc_mark((VALUE)p->ast);
13160 rb_gc_mark(p->case_labels);
13161 #ifndef RIPPER
13162 rb_gc_mark(p->debug_lines);
13163 rb_gc_mark(p->compile_option);
13164 rb_gc_mark(p->error_buffer);
13165 #else
13166 rb_gc_mark(p->delayed.token);
13167 rb_gc_mark(p->value);
13168 rb_gc_mark(p->result);
13169 rb_gc_mark(p->parsing_thread);
13170 #endif
13171 rb_gc_mark(p->debug_buffer);
13172 rb_gc_mark(p->debug_output);
13173 #ifdef YYMALLOC
13174 rb_gc_mark((VALUE)p->heap);
13175 #endif
13178 static void
13179 parser_free(void *ptr)
13181 struct parser_params *p = (struct parser_params*)ptr;
13182 struct local_vars *local, *prev;
13184 if (p->tokenbuf) {
13185 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13187 for (local = p->lvtbl; local; local = prev) {
13188 if (local->vars) xfree(local->vars);
13189 prev = local->prev;
13190 xfree(local);
13193 token_info *ptinfo;
13194 while ((ptinfo = p->token_info) != 0) {
13195 p->token_info = ptinfo->next;
13196 xfree(ptinfo);
13199 xfree(ptr);
13202 static size_t
13203 parser_memsize(const void *ptr)
13205 struct parser_params *p = (struct parser_params*)ptr;
13206 struct local_vars *local;
13207 size_t size = sizeof(*p);
13209 size += p->toksiz;
13210 for (local = p->lvtbl; local; local = local->prev) {
13211 size += sizeof(*local);
13212 if (local->vars) size += local->vars->capa * sizeof(ID);
13214 return size;
13217 static const rb_data_type_t parser_data_type = {
13218 #ifndef RIPPER
13219 "parser",
13220 #else
13221 "ripper",
13222 #endif
13224 parser_mark,
13225 parser_free,
13226 parser_memsize,
13228 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13231 #ifndef RIPPER
13232 #undef rb_reserved_word
13234 const struct kwtable *
13235 rb_reserved_word(const char *str, unsigned int len)
13237 return reserved_word(str, len);
13240 VALUE
13241 rb_parser_new(void)
13243 struct parser_params *p;
13244 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13245 &parser_data_type, p);
13246 parser_initialize(p);
13247 return parser;
13250 VALUE
13251 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13253 struct parser_params *p;
13255 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13256 p->error_buffer = main ? Qfalse : Qnil;
13257 p->parent_iseq = base;
13258 return vparser;
13261 void
13262 rb_parser_keep_script_lines(VALUE vparser)
13264 struct parser_params *p;
13266 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13267 p->keep_script_lines = 1;
13269 #endif
13271 #ifdef RIPPER
13272 #define rb_parser_end_seen_p ripper_parser_end_seen_p
13273 #define rb_parser_encoding ripper_parser_encoding
13274 #define rb_parser_get_yydebug ripper_parser_get_yydebug
13275 #define rb_parser_set_yydebug ripper_parser_set_yydebug
13276 #define rb_parser_get_debug_output ripper_parser_get_debug_output
13277 #define rb_parser_set_debug_output ripper_parser_set_debug_output
13278 static VALUE ripper_parser_end_seen_p(VALUE vparser);
13279 static VALUE ripper_parser_encoding(VALUE vparser);
13280 static VALUE ripper_parser_get_yydebug(VALUE self);
13281 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13282 static VALUE ripper_parser_get_debug_output(VALUE self);
13283 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13286 * call-seq:
13287 * ripper.error? -> Boolean
13289 * Return true if parsed source has errors.
13291 static VALUE
13292 ripper_error_p(VALUE vparser)
13294 struct parser_params *p;
13296 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13297 return RBOOL(p->error_p);
13299 #endif
13302 * call-seq:
13303 * ripper.end_seen? -> Boolean
13305 * Return true if parsed source ended by +\_\_END\_\_+.
13307 VALUE
13308 rb_parser_end_seen_p(VALUE vparser)
13310 struct parser_params *p;
13312 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13313 return RBOOL(p->ruby__end__seen);
13317 * call-seq:
13318 * ripper.encoding -> encoding
13320 * Return encoding of the source.
13322 VALUE
13323 rb_parser_encoding(VALUE vparser)
13325 struct parser_params *p;
13327 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13328 return rb_enc_from_encoding(p->enc);
13331 #ifdef RIPPER
13333 * call-seq:
13334 * ripper.yydebug -> true or false
13336 * Get yydebug.
13338 VALUE
13339 rb_parser_get_yydebug(VALUE self)
13341 struct parser_params *p;
13343 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13344 return RBOOL(p->debug);
13346 #endif
13349 * call-seq:
13350 * ripper.yydebug = flag
13352 * Set yydebug.
13354 VALUE
13355 rb_parser_set_yydebug(VALUE self, VALUE flag)
13357 struct parser_params *p;
13359 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13360 p->debug = RTEST(flag);
13361 return flag;
13365 * call-seq:
13366 * ripper.debug_output -> obj
13368 * Get debug output.
13370 VALUE
13371 rb_parser_get_debug_output(VALUE self)
13373 struct parser_params *p;
13375 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13376 return p->debug_output;
13380 * call-seq:
13381 * ripper.debug_output = obj
13383 * Set debug output.
13385 VALUE
13386 rb_parser_set_debug_output(VALUE self, VALUE output)
13388 struct parser_params *p;
13390 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13391 return p->debug_output = output;
13394 #ifndef RIPPER
13395 #ifdef YYMALLOC
13396 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13397 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13398 * potential memory leak */
13399 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13400 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13401 (new)->cnt = (cnt), (ptr))
13403 void *
13404 rb_parser_malloc(struct parser_params *p, size_t size)
13406 size_t cnt = HEAPCNT(1, size);
13407 rb_imemo_tmpbuf_t *n = NEWHEAP();
13408 void *ptr = xmalloc(size);
13410 return ADD2HEAP(n, cnt, ptr);
13413 void *
13414 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13416 size_t cnt = HEAPCNT(nelem, size);
13417 rb_imemo_tmpbuf_t *n = NEWHEAP();
13418 void *ptr = xcalloc(nelem, size);
13420 return ADD2HEAP(n, cnt, ptr);
13423 void *
13424 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
13426 rb_imemo_tmpbuf_t *n;
13427 size_t cnt = HEAPCNT(1, size);
13429 if (ptr && (n = p->heap) != NULL) {
13430 do {
13431 if (n->ptr == ptr) {
13432 n->ptr = ptr = xrealloc(ptr, size);
13433 if (n->cnt) n->cnt = cnt;
13434 return ptr;
13436 } while ((n = n->next) != NULL);
13438 n = NEWHEAP();
13439 ptr = xrealloc(ptr, size);
13440 return ADD2HEAP(n, cnt, ptr);
13443 void
13444 rb_parser_free(struct parser_params *p, void *ptr)
13446 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
13448 while ((n = *prev) != NULL) {
13449 if (n->ptr == ptr) {
13450 *prev = n->next;
13451 break;
13453 prev = &n->next;
13456 #endif
13458 void
13459 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
13461 va_list ap;
13462 VALUE mesg = p->debug_buffer;
13464 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
13465 va_start(ap, fmt);
13466 rb_str_vcatf(mesg, fmt, ap);
13467 va_end(ap);
13468 if (RSTRING_END(mesg)[-1] == '\n') {
13469 rb_io_write(p->debug_output, mesg);
13470 p->debug_buffer = Qnil;
13474 static void
13475 parser_compile_error(struct parser_params *p, const char *fmt, ...)
13477 va_list ap;
13479 rb_io_flush(p->debug_output);
13480 p->error_p = 1;
13481 va_start(ap, fmt);
13482 p->error_buffer =
13483 rb_syntax_error_append(p->error_buffer,
13484 p->ruby_sourcefile_string,
13485 p->ruby_sourceline,
13486 rb_long2int(p->lex.pcur - p->lex.pbeg),
13487 p->enc, fmt, ap);
13488 va_end(ap);
13491 static size_t
13492 count_char(const char *str, int c)
13494 int n = 0;
13495 while (str[n] == c) ++n;
13496 return n;
13500 * strip enclosing double-quotes, same as the default yytnamerr except
13501 * for that single-quotes matching back-quotes do not stop stripping.
13503 * "\"`class' keyword\"" => "`class' keyword"
13505 RUBY_FUNC_EXPORTED size_t
13506 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
13508 if (*yystr == '"') {
13509 size_t yyn = 0, bquote = 0;
13510 const char *yyp = yystr;
13512 while (*++yyp) {
13513 switch (*yyp) {
13514 case '`':
13515 if (!bquote) {
13516 bquote = count_char(yyp+1, '`') + 1;
13517 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
13518 yyn += bquote;
13519 yyp += bquote - 1;
13520 break;
13522 goto default_char;
13524 case '\'':
13525 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
13526 if (yyres) memcpy(yyres + yyn, yyp, bquote);
13527 yyn += bquote;
13528 yyp += bquote - 1;
13529 bquote = 0;
13530 break;
13532 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
13533 if (yyres) memcpy(yyres + yyn, yyp, 3);
13534 yyn += 3;
13535 yyp += 2;
13536 break;
13538 goto do_not_strip_quotes;
13540 case ',':
13541 goto do_not_strip_quotes;
13543 case '\\':
13544 if (*++yyp != '\\')
13545 goto do_not_strip_quotes;
13546 /* Fall through. */
13547 default_char:
13548 default:
13549 if (yyres)
13550 yyres[yyn] = *yyp;
13551 yyn++;
13552 break;
13554 case '"':
13555 case '\0':
13556 if (yyres)
13557 yyres[yyn] = '\0';
13558 return yyn;
13561 do_not_strip_quotes: ;
13564 if (!yyres) return strlen(yystr);
13566 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
13568 #endif
13570 #ifdef RIPPER
13571 #ifdef RIPPER_DEBUG
13572 /* :nodoc: */
13573 static VALUE
13574 ripper_validate_object(VALUE self, VALUE x)
13576 if (x == Qfalse) return x;
13577 if (x == Qtrue) return x;
13578 if (x == Qnil) return x;
13579 if (x == Qundef)
13580 rb_raise(rb_eArgError, "Qundef given");
13581 if (FIXNUM_P(x)) return x;
13582 if (SYMBOL_P(x)) return x;
13583 switch (BUILTIN_TYPE(x)) {
13584 case T_STRING:
13585 case T_OBJECT:
13586 case T_ARRAY:
13587 case T_BIGNUM:
13588 case T_FLOAT:
13589 case T_COMPLEX:
13590 case T_RATIONAL:
13591 break;
13592 case T_NODE:
13593 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
13594 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
13596 x = ((NODE *)x)->nd_rval;
13597 break;
13598 default:
13599 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
13600 (void *)x, rb_obj_classname(x));
13602 if (!RBASIC_CLASS(x)) {
13603 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
13604 (void *)x, rb_builtin_type_name(TYPE(x)));
13606 return x;
13608 #endif
13610 #define validate(x) ((x) = get_value(x))
13612 static VALUE
13613 ripper_dispatch0(struct parser_params *p, ID mid)
13615 return rb_funcall(p->value, mid, 0);
13618 static VALUE
13619 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
13621 validate(a);
13622 return rb_funcall(p->value, mid, 1, a);
13625 static VALUE
13626 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
13628 validate(a);
13629 validate(b);
13630 return rb_funcall(p->value, mid, 2, a, b);
13633 static VALUE
13634 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
13636 validate(a);
13637 validate(b);
13638 validate(c);
13639 return rb_funcall(p->value, mid, 3, a, b, c);
13642 static VALUE
13643 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
13645 validate(a);
13646 validate(b);
13647 validate(c);
13648 validate(d);
13649 return rb_funcall(p->value, mid, 4, a, b, c, d);
13652 static VALUE
13653 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
13655 validate(a);
13656 validate(b);
13657 validate(c);
13658 validate(d);
13659 validate(e);
13660 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
13663 static VALUE
13664 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
13666 validate(a);
13667 validate(b);
13668 validate(c);
13669 validate(d);
13670 validate(e);
13671 validate(f);
13672 validate(g);
13673 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
13676 static ID
13677 ripper_get_id(VALUE v)
13679 NODE *nd;
13680 if (!RB_TYPE_P(v, T_NODE)) return 0;
13681 nd = (NODE *)v;
13682 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
13683 return nd->nd_vid;
13686 static VALUE
13687 ripper_get_value(VALUE v)
13689 NODE *nd;
13690 if (v == Qundef) return Qnil;
13691 if (!RB_TYPE_P(v, T_NODE)) return v;
13692 nd = (NODE *)v;
13693 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
13694 return nd->nd_rval;
13697 static void
13698 ripper_error(struct parser_params *p)
13700 p->error_p = TRUE;
13703 static void
13704 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
13706 VALUE str;
13707 va_list args;
13709 va_start(args, fmt);
13710 str = rb_vsprintf(fmt, args);
13711 va_end(args);
13712 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
13713 ripper_error(p);
13716 static VALUE
13717 ripper_lex_get_generic(struct parser_params *p, VALUE src)
13719 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
13720 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
13721 rb_raise(rb_eTypeError,
13722 "gets returned %"PRIsVALUE" (expected String or nil)",
13723 rb_obj_class(line));
13725 return line;
13728 static VALUE
13729 ripper_lex_io_get(struct parser_params *p, VALUE src)
13731 return rb_io_gets(src);
13734 static VALUE
13735 ripper_s_allocate(VALUE klass)
13737 struct parser_params *p;
13738 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
13739 &parser_data_type, p);
13740 p->value = self;
13741 return self;
13744 #define ripper_initialized_p(r) ((r)->lex.input != 0)
13747 * call-seq:
13748 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
13750 * Create a new Ripper object.
13751 * _src_ must be a String, an IO, or an Object which has #gets method.
13753 * This method does not starts parsing.
13754 * See also Ripper#parse and Ripper.parse.
13756 static VALUE
13757 ripper_initialize(int argc, VALUE *argv, VALUE self)
13759 struct parser_params *p;
13760 VALUE src, fname, lineno;
13762 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13763 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
13764 if (RB_TYPE_P(src, T_FILE)) {
13765 p->lex.gets = ripper_lex_io_get;
13767 else if (rb_respond_to(src, id_gets)) {
13768 p->lex.gets = ripper_lex_get_generic;
13770 else {
13771 StringValue(src);
13772 p->lex.gets = lex_get_str;
13774 p->lex.input = src;
13775 p->eofp = 0;
13776 if (NIL_P(fname)) {
13777 fname = STR_NEW2("(ripper)");
13778 OBJ_FREEZE(fname);
13780 else {
13781 StringValueCStr(fname);
13782 fname = rb_str_new_frozen(fname);
13784 parser_initialize(p);
13786 p->ruby_sourcefile_string = fname;
13787 p->ruby_sourcefile = RSTRING_PTR(fname);
13788 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
13790 return Qnil;
13793 static VALUE
13794 ripper_parse0(VALUE parser_v)
13796 struct parser_params *p;
13798 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13799 parser_prepare(p);
13800 p->ast = rb_ast_new();
13801 ripper_yyparse((void*)p);
13802 rb_ast_dispose(p->ast);
13803 p->ast = 0;
13804 return p->result;
13807 static VALUE
13808 ripper_ensure(VALUE parser_v)
13810 struct parser_params *p;
13812 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13813 p->parsing_thread = Qnil;
13814 return Qnil;
13818 * call-seq:
13819 * ripper.parse
13821 * Start parsing and returns the value of the root action.
13823 static VALUE
13824 ripper_parse(VALUE self)
13826 struct parser_params *p;
13828 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13829 if (!ripper_initialized_p(p)) {
13830 rb_raise(rb_eArgError, "method called for uninitialized object");
13832 if (!NIL_P(p->parsing_thread)) {
13833 if (p->parsing_thread == rb_thread_current())
13834 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13835 else
13836 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13838 p->parsing_thread = rb_thread_current();
13839 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13841 return p->result;
13845 * call-seq:
13846 * ripper.column -> Integer
13848 * Return column number of current parsing line.
13849 * This number starts from 0.
13851 static VALUE
13852 ripper_column(VALUE self)
13854 struct parser_params *p;
13855 long col;
13857 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13858 if (!ripper_initialized_p(p)) {
13859 rb_raise(rb_eArgError, "method called for uninitialized object");
13861 if (NIL_P(p->parsing_thread)) return Qnil;
13862 col = p->lex.ptok - p->lex.pbeg;
13863 return LONG2NUM(col);
13867 * call-seq:
13868 * ripper.filename -> String
13870 * Return current parsing filename.
13872 static VALUE
13873 ripper_filename(VALUE self)
13875 struct parser_params *p;
13877 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13878 if (!ripper_initialized_p(p)) {
13879 rb_raise(rb_eArgError, "method called for uninitialized object");
13881 return p->ruby_sourcefile_string;
13885 * call-seq:
13886 * ripper.lineno -> Integer
13888 * Return line number of current parsing line.
13889 * This number starts from 1.
13891 static VALUE
13892 ripper_lineno(VALUE self)
13894 struct parser_params *p;
13896 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13897 if (!ripper_initialized_p(p)) {
13898 rb_raise(rb_eArgError, "method called for uninitialized object");
13900 if (NIL_P(p->parsing_thread)) return Qnil;
13901 return INT2NUM(p->ruby_sourceline);
13905 * call-seq:
13906 * ripper.state -> Integer
13908 * Return scanner state of current token.
13910 static VALUE
13911 ripper_state(VALUE self)
13913 struct parser_params *p;
13915 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13916 if (!ripper_initialized_p(p)) {
13917 rb_raise(rb_eArgError, "method called for uninitialized object");
13919 if (NIL_P(p->parsing_thread)) return Qnil;
13920 return INT2NUM(p->lex.state);
13924 * call-seq:
13925 * ripper.token -> String
13927 * Return the current token string.
13929 static VALUE
13930 ripper_token(VALUE self)
13932 struct parser_params *p;
13933 long pos, len;
13935 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13936 if (!ripper_initialized_p(p)) {
13937 rb_raise(rb_eArgError, "method called for uninitialized object");
13939 if (NIL_P(p->parsing_thread)) return Qnil;
13940 pos = p->lex.ptok - p->lex.pbeg;
13941 len = p->lex.pcur - p->lex.ptok;
13942 return rb_str_subseq(p->lex.lastline, pos, len);
13945 #ifdef RIPPER_DEBUG
13946 /* :nodoc: */
13947 static VALUE
13948 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13950 StringValue(msg);
13951 if (obj == Qundef) {
13952 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13954 return Qnil;
13957 /* :nodoc: */
13958 static VALUE
13959 ripper_value(VALUE self, VALUE obj)
13961 return ULONG2NUM(obj);
13963 #endif
13966 * call-seq:
13967 * Ripper.lex_state_name(integer) -> string
13969 * Returns a string representation of lex_state.
13971 static VALUE
13972 ripper_lex_state_name(VALUE self, VALUE state)
13974 return rb_parser_lex_state_name(NUM2INT(state));
13977 void
13978 Init_ripper(void)
13980 ripper_init_eventids1();
13981 ripper_init_eventids2();
13982 id_warn = rb_intern_const("warn");
13983 id_warning = rb_intern_const("warning");
13984 id_gets = rb_intern_const("gets");
13985 id_assoc = rb_intern_const("=>");
13987 (void)yystpcpy; /* may not used in newer bison */
13989 InitVM(ripper);
13992 void
13993 InitVM_ripper(void)
13995 VALUE Ripper;
13997 Ripper = rb_define_class("Ripper", rb_cObject);
13998 /* version of Ripper */
13999 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14000 rb_define_alloc_func(Ripper, ripper_s_allocate);
14001 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14002 rb_define_method(Ripper, "parse", ripper_parse, 0);
14003 rb_define_method(Ripper, "column", ripper_column, 0);
14004 rb_define_method(Ripper, "filename", ripper_filename, 0);
14005 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14006 rb_define_method(Ripper, "state", ripper_state, 0);
14007 rb_define_method(Ripper, "token", ripper_token, 0);
14008 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14009 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14010 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14011 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14012 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14013 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14014 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14015 #ifdef RIPPER_DEBUG
14016 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14017 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14018 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14019 #endif
14021 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14022 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14024 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14026 <% @exprs.each do |expr, desc| -%>
14027 /* <%=desc%> */
14028 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14029 <% end %>
14030 ripper_init_eventids1_table(Ripper);
14031 ripper_init_eventids2_table(Ripper);
14033 # if 0
14034 /* Hack to let RDoc document SCRIPT_LINES__ */
14037 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14038 * after the assignment will be added as an Array of lines with the file
14039 * name as the key.
14041 rb_define_global_const("SCRIPT_LINES__", Qnil);
14042 #endif
14045 #endif /* RIPPER */
14048 * Local variables:
14049 * mode: c
14050 * c-file-style: "ruby"
14051 * End: