Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)
parse.y
1/**********************************************************************
2
3 parse.y -
4
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12%require "3.0"
13
14%{
15
16#if !YYPURE
17# error needs pure parser
18#endif
19#define YYDEBUG 1
20#define YYERROR_VERBOSE 1
21#define YYSTACK_USE_ALLOCA 0
22#define YYLTYPE rb_code_location_t
23#define YYLTYPE_IS_DECLARED 1
24
25#include "ruby/internal/config.h"
26
27#include <ctype.h>
28#include <errno.h>
29#include <stdio.h>
30
31struct lex_context;
32
33#include "internal.h"
34#include "internal/compile.h"
35#include "internal/compilers.h"
36#include "internal/complex.h"
37#include "internal/encoding.h"
38#include "internal/error.h"
39#include "internal/hash.h"
40#include "internal/imemo.h"
41#include "internal/io.h"
42#include "internal/numeric.h"
43#include "internal/parse.h"
44#include "internal/rational.h"
45#include "internal/re.h"
46#include "internal/symbol.h"
47#include "internal/thread.h"
48#include "internal/variable.h"
49#include "node.h"
50#include "probes.h"
51#include "regenc.h"
52#include "ruby/encoding.h"
53#include "ruby/regex.h"
54#include "ruby/ruby.h"
55#include "ruby/st.h"
56#include "ruby/util.h"
57#include "ruby/ractor.h"
58#include "symbol.h"
59
60enum shareability {
61 shareable_none,
62 shareable_literal,
63 shareable_copy,
64 shareable_everything,
65};
66
67struct lex_context {
68 unsigned int in_defined: 1;
69 unsigned int in_kwarg: 1;
70 unsigned int in_argdef: 1;
71 unsigned int in_def: 1;
72 unsigned int in_class: 1;
73 BITFIELD(enum shareability, shareable_constant_value, 2);
74};
75
76#if defined(__GNUC__) && !defined(__clang__)
77// Suppress "parameter passing for argument of type 'struct
78// lex_context' changed" notes. `struct lex_context` is file scope,
79// and has no ABI compatibility issue.
80RBIMPL_WARNING_PUSH()
81RBIMPL_WARNING_IGNORED(-Wpsabi)
82RBIMPL_WARNING_POP()
83// Not sure why effective even after popped.
84#endif
85
86#include "parse.h"
87
88#define NO_LEX_CTXT (struct lex_context){0}
89
90#define AREF(ary, i) RARRAY_AREF(ary, i)
91
92#ifndef WARN_PAST_SCOPE
93# define WARN_PAST_SCOPE 0
94#endif
95
96#define TAB_WIDTH 8
97
98#define yydebug (p->debug) /* disable the global variable definition */
99
100#define YYMALLOC(size) rb_parser_malloc(p, (size))
101#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
102#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
103#define YYFREE(ptr) rb_parser_free(p, (ptr))
104#define YYFPRINTF rb_parser_printf
105#define YY_LOCATION_PRINT(File, loc) \
106 rb_parser_printf(p, "%d.%d-%d.%d", \
107 (loc).beg_pos.lineno, (loc).beg_pos.column,\
108 (loc).end_pos.lineno, (loc).end_pos.column)
109#define YYLLOC_DEFAULT(Current, Rhs, N) \
110 do \
111 if (N) \
112 { \
113 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
114 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
115 } \
116 else \
117 { \
118 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
119 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
120 } \
121 while (0)
122#define YY_(Msgid) \
123 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
124 "nesting too deep" : (Msgid))
125
126#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
127 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
128#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
129 rb_parser_set_location_of_delayed_token(p, &(Current))
130#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
131 rb_parser_set_location_of_heredoc_end(p, &(Current))
132#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
133 rb_parser_set_location_of_dummy_end(p, &(Current))
134#define RUBY_SET_YYLLOC_OF_NONE(Current) \
135 rb_parser_set_location_of_none(p, &(Current))
136#define RUBY_SET_YYLLOC(Current) \
137 rb_parser_set_location(p, &(Current))
138#define RUBY_INIT_YYLLOC() \
139 { \
140 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
141 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
142 }
143
144enum lex_state_bits {
145 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
146 EXPR_END_bit, /* newline significant, +/- is an operator. */
147 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
148 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
149 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
150 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
151 EXPR_MID_bit, /* newline significant, +/- is an operator. */
152 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
153 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
154 EXPR_CLASS_bit, /* immediate after `class', no here document. */
155 EXPR_LABEL_bit, /* flag bit, label is allowed. */
156 EXPR_LABELED_bit, /* flag bit, just after a label. */
157 EXPR_FITEM_bit, /* symbol literal as FNAME. */
158 EXPR_MAX_STATE
159};
160/* examine combinations */
161enum lex_state_e {
162#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
163 DEF_EXPR(BEG),
164 DEF_EXPR(END),
165 DEF_EXPR(ENDARG),
166 DEF_EXPR(ENDFN),
167 DEF_EXPR(ARG),
168 DEF_EXPR(CMDARG),
169 DEF_EXPR(MID),
170 DEF_EXPR(FNAME),
171 DEF_EXPR(DOT),
172 DEF_EXPR(CLASS),
173 DEF_EXPR(LABEL),
174 DEF_EXPR(LABELED),
175 DEF_EXPR(FITEM),
176 EXPR_VALUE = EXPR_BEG,
177 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
178 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
179 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
180 EXPR_NONE = 0
181};
182#define IS_lex_state_for(x, ls) ((x) & (ls))
183#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
184#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
185#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
186
187# define SET_LEX_STATE(ls) \
188 parser_set_lex_state(p, ls, __LINE__)
189static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
190
191typedef VALUE stack_type;
192
193static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
194
195# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
196# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
197# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
198# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
199# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
200
201/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
202 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
203#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
204#define COND_POP() BITSTACK_POP(cond_stack)
205#define COND_P() BITSTACK_SET_P(cond_stack)
206#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
207
208/* A flag to identify keyword_do_block; "do" keyword after command_call.
209 Example: `foo 1, 2 do`. */
210#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
211#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
212#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
213#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
214
215struct vtable {
216 ID *tbl;
217 int pos;
218 int capa;
219 struct vtable *prev;
220};
221
222struct local_vars {
223 struct vtable *args;
224 struct vtable *vars;
225 struct vtable *used;
226# if WARN_PAST_SCOPE
227 struct vtable *past;
228# endif
229 struct local_vars *prev;
230# ifndef RIPPER
231 struct {
232 NODE *outer, *inner, *current;
233 } numparam;
234# endif
235};
236
237enum {
238 ORDINAL_PARAM = -1,
239 NO_PARAM = 0,
240 NUMPARAM_MAX = 9,
241};
242
243#define NUMPARAM_ID_P(id) numparam_id_p(id)
244#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
245#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
246static int
247numparam_id_p(ID id)
248{
249 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
250 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
251 return idx > 0 && idx <= NUMPARAM_MAX;
252}
253static void numparam_name(struct parser_params *p, ID id);
254
255#define DVARS_INHERIT ((void*)1)
256#define DVARS_TOPSCOPE NULL
257#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
258
259typedef struct token_info {
260 const char *token;
261 rb_code_position_t beg;
262 int indent;
263 int nonspc;
264 struct token_info *next;
265} token_info;
266
267typedef struct rb_strterm_struct rb_strterm_t;
268
269/*
270 Structure of Lexer Buffer:
271
272 lex.pbeg lex.ptok lex.pcur lex.pend
273 | | | |
274 |------------+------------+------------|
275 |<---------->|
276 token
277*/
278struct parser_params {
279 rb_imemo_tmpbuf_t *heap;
280
281 YYSTYPE *lval;
282 YYLTYPE *yylloc;
283
284 struct {
285 rb_strterm_t *strterm;
286 VALUE (*gets)(struct parser_params*,VALUE);
287 VALUE input;
288 VALUE lastline;
289 VALUE nextline;
290 const char *pbeg;
291 const char *pcur;
292 const char *pend;
293 const char *ptok;
294 union {
295 long ptr;
296 VALUE (*call)(VALUE, int);
297 } gets_;
298 enum lex_state_e state;
299 /* track the nest level of any parens "()[]{}" */
300 int paren_nest;
301 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
302 int lpar_beg;
303 /* track the nest level of only braces "{}" */
304 int brace_nest;
305 } lex;
306 stack_type cond_stack;
307 stack_type cmdarg_stack;
308 int tokidx;
309 int toksiz;
310 int tokline;
311 int heredoc_end;
312 int heredoc_indent;
313 int heredoc_line_indent;
314 char *tokenbuf;
315 struct local_vars *lvtbl;
316 st_table *pvtbl;
317 st_table *pktbl;
318 int line_count;
319 int ruby_sourceline; /* current line no. */
320 const char *ruby_sourcefile; /* current source file */
321 VALUE ruby_sourcefile_string;
322 rb_encoding *enc;
323 token_info *token_info;
324 VALUE case_labels;
325 VALUE compile_option;
326
327 VALUE debug_buffer;
328 VALUE debug_output;
329
330 struct {
331 VALUE token;
332 int beg_line;
333 int beg_col;
334 int end_line;
335 int end_col;
336 } delayed;
337
338 ID cur_arg;
339
340 rb_ast_t *ast;
341 int node_id;
342
343 int max_numparam;
344
345 struct lex_context ctxt;
346
347 unsigned int command_start:1;
348 unsigned int eofp: 1;
349 unsigned int ruby__end__seen: 1;
350 unsigned int debug: 1;
351 unsigned int has_shebang: 1;
352 unsigned int token_seen: 1;
353 unsigned int token_info_enabled: 1;
354# if WARN_PAST_SCOPE
355 unsigned int past_scope_enabled: 1;
356# endif
357 unsigned int error_p: 1;
358 unsigned int cr_seen: 1;
359
360#ifndef RIPPER
361 /* Ruby core only */
362
363 unsigned int do_print: 1;
364 unsigned int do_loop: 1;
365 unsigned int do_chomp: 1;
366 unsigned int do_split: 1;
367 unsigned int keep_script_lines: 1;
368 unsigned int error_tolerant: 1;
369 unsigned int keep_tokens: 1;
370
371 NODE *eval_tree_begin;
372 NODE *eval_tree;
373 VALUE error_buffer;
374 VALUE debug_lines;
375 const struct rb_iseq_struct *parent_iseq;
376 /* store specific keyword locations to generate dummy end token */
377 VALUE end_expect_token_locations;
378 /* id for terms */
379 int token_id;
380 /* Array for term tokens */
381 VALUE tokens;
382#else
383 /* Ripper only */
384
385 VALUE value;
386 VALUE result;
387 VALUE parsing_thread;
388#endif
389};
390
391#define intern_cstr(n,l,en) rb_intern3(n,l,en)
392
393#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
394#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
395#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
396#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
397#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
398
399static st_table *
400push_pvtbl(struct parser_params *p)
401{
402 st_table *tbl = p->pvtbl;
403 p->pvtbl = st_init_numtable();
404 return tbl;
405}
406
407static void
408pop_pvtbl(struct parser_params *p, st_table *tbl)
409{
410 st_free_table(p->pvtbl);
411 p->pvtbl = tbl;
412}
413
414static st_table *
415push_pktbl(struct parser_params *p)
416{
417 st_table *tbl = p->pktbl;
418 p->pktbl = 0;
419 return tbl;
420}
421
422static void
423pop_pktbl(struct parser_params *p, st_table *tbl)
424{
425 if (p->pktbl) st_free_table(p->pktbl);
426 p->pktbl = tbl;
427}
428
429#ifndef RIPPER
430static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
431
432static void
433debug_end_expect_token_locations(struct parser_params *p, const char *name)
434{
435 if(p->debug) {
436 VALUE mesg = rb_sprintf("%s: ", name);
437 rb_str_catf(mesg, " %"PRIsVALUE"\n", p->end_expect_token_locations);
438 flush_debug_buffer(p, p->debug_output, mesg);
439 }
440}
441
442static void
443push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
444{
445 if(NIL_P(p->end_expect_token_locations)) return;
446 rb_ary_push(p->end_expect_token_locations, rb_ary_new_from_args(2, INT2NUM(pos->lineno), INT2NUM(pos->column)));
447 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
448}
449
450static void
451pop_end_expect_token_locations(struct parser_params *p)
452{
453 if(NIL_P(p->end_expect_token_locations)) return;
454 rb_ary_pop(p->end_expect_token_locations);
455 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
456}
457
458static VALUE
459peek_end_expect_token_locations(struct parser_params *p)
460{
461 if(NIL_P(p->end_expect_token_locations)) return Qnil;
462 return rb_ary_last(0, 0, p->end_expect_token_locations);
463}
464
465static ID
466parser_token2id(enum yytokentype tok)
467{
468 switch ((int) tok) {
469#define TOKEN2ID(tok) case tok: return rb_intern(#tok);
470#define TOKEN2ID2(tok, name) case tok: return rb_intern(name);
471 TOKEN2ID2(' ', "words_sep")
472 TOKEN2ID2('!', "!")
473 TOKEN2ID2('%', "%");
474 TOKEN2ID2('&', "&");
475 TOKEN2ID2('*', "*");
476 TOKEN2ID2('+', "+");
477 TOKEN2ID2('-', "-");
478 TOKEN2ID2('/', "/");
479 TOKEN2ID2('<', "<");
480 TOKEN2ID2('=', "=");
481 TOKEN2ID2('>', ">");
482 TOKEN2ID2('?', "?");
483 TOKEN2ID2('^', "^");
484 TOKEN2ID2('|', "|");
485 TOKEN2ID2('~', "~");
486 TOKEN2ID2(':', ":");
487 TOKEN2ID2(',', ",");
488 TOKEN2ID2('.', ".");
489 TOKEN2ID2(';', ";");
490 TOKEN2ID2('`', "`");
491 TOKEN2ID2('\n', "nl");
492 TOKEN2ID2('{', "{");
493 TOKEN2ID2('}', "}");
494 TOKEN2ID2('[', "[");
495 TOKEN2ID2(']', "]");
496 TOKEN2ID2('(', "(");
497 TOKEN2ID2(')', ")");
498 TOKEN2ID(keyword_class);
499 TOKEN2ID(keyword_module);
500 TOKEN2ID(keyword_def);
501 TOKEN2ID(keyword_undef);
502 TOKEN2ID(keyword_begin);
503 TOKEN2ID(keyword_rescue);
504 TOKEN2ID(keyword_ensure);
505 TOKEN2ID(keyword_end);
506 TOKEN2ID(keyword_if);
507 TOKEN2ID(keyword_unless);
508 TOKEN2ID(keyword_then);
509 TOKEN2ID(keyword_elsif);
510 TOKEN2ID(keyword_else);
511 TOKEN2ID(keyword_case);
512 TOKEN2ID(keyword_when);
513 TOKEN2ID(keyword_while);
514 TOKEN2ID(keyword_until);
515 TOKEN2ID(keyword_for);
516 TOKEN2ID(keyword_break);
517 TOKEN2ID(keyword_next);
518 TOKEN2ID(keyword_redo);
519 TOKEN2ID(keyword_retry);
520 TOKEN2ID(keyword_in);
521 TOKEN2ID(keyword_do);
522 TOKEN2ID(keyword_do_cond);
523 TOKEN2ID(keyword_do_block);
524 TOKEN2ID(keyword_do_LAMBDA);
525 TOKEN2ID(keyword_return);
526 TOKEN2ID(keyword_yield);
527 TOKEN2ID(keyword_super);
528 TOKEN2ID(keyword_self);
529 TOKEN2ID(keyword_nil);
530 TOKEN2ID(keyword_true);
531 TOKEN2ID(keyword_false);
532 TOKEN2ID(keyword_and);
533 TOKEN2ID(keyword_or);
534 TOKEN2ID(keyword_not);
535 TOKEN2ID(modifier_if);
536 TOKEN2ID(modifier_unless);
537 TOKEN2ID(modifier_while);
538 TOKEN2ID(modifier_until);
539 TOKEN2ID(modifier_rescue);
540 TOKEN2ID(keyword_alias);
541 TOKEN2ID(keyword_defined);
542 TOKEN2ID(keyword_BEGIN);
543 TOKEN2ID(keyword_END);
544 TOKEN2ID(keyword__LINE__);
545 TOKEN2ID(keyword__FILE__);
546 TOKEN2ID(keyword__ENCODING__);
547 TOKEN2ID(tIDENTIFIER);
548 TOKEN2ID(tFID);
549 TOKEN2ID(tGVAR);
550 TOKEN2ID(tIVAR);
551 TOKEN2ID(tCONSTANT);
552 TOKEN2ID(tCVAR);
553 TOKEN2ID(tLABEL);
554 TOKEN2ID(tINTEGER);
555 TOKEN2ID(tFLOAT);
556 TOKEN2ID(tRATIONAL);
557 TOKEN2ID(tIMAGINARY);
558 TOKEN2ID(tCHAR);
559 TOKEN2ID(tNTH_REF);
560 TOKEN2ID(tBACK_REF);
561 TOKEN2ID(tSTRING_CONTENT);
562 TOKEN2ID(tREGEXP_END);
563 TOKEN2ID(tDUMNY_END);
564 TOKEN2ID(tSP);
565 TOKEN2ID(tUPLUS);
566 TOKEN2ID(tUMINUS);
567 TOKEN2ID(tPOW);
568 TOKEN2ID(tCMP);
569 TOKEN2ID(tEQ);
570 TOKEN2ID(tEQQ);
571 TOKEN2ID(tNEQ);
572 TOKEN2ID(tGEQ);
573 TOKEN2ID(tLEQ);
574 TOKEN2ID(tANDOP);
575 TOKEN2ID(tOROP);
576 TOKEN2ID(tMATCH);
577 TOKEN2ID(tNMATCH);
578 TOKEN2ID(tDOT2);
579 TOKEN2ID(tDOT3);
580 TOKEN2ID(tBDOT2);
581 TOKEN2ID(tBDOT3);
582 TOKEN2ID(tAREF);
583 TOKEN2ID(tASET);
584 TOKEN2ID(tLSHFT);
585 TOKEN2ID(tRSHFT);
586 TOKEN2ID(tANDDOT);
587 TOKEN2ID(tCOLON2);
588 TOKEN2ID(tCOLON3);
589 TOKEN2ID(tOP_ASGN);
590 TOKEN2ID(tASSOC);
591 TOKEN2ID(tLPAREN);
592 TOKEN2ID(tLPAREN_ARG);
593 TOKEN2ID(tRPAREN);
594 TOKEN2ID(tLBRACK);
595 TOKEN2ID(tLBRACE);
596 TOKEN2ID(tLBRACE_ARG);
597 TOKEN2ID(tSTAR);
598 TOKEN2ID(tDSTAR);
599 TOKEN2ID(tAMPER);
600 TOKEN2ID(tLAMBDA);
601 TOKEN2ID(tSYMBEG);
602 TOKEN2ID(tSTRING_BEG);
603 TOKEN2ID(tXSTRING_BEG);
604 TOKEN2ID(tREGEXP_BEG);
605 TOKEN2ID(tWORDS_BEG);
606 TOKEN2ID(tQWORDS_BEG);
607 TOKEN2ID(tSYMBOLS_BEG);
608 TOKEN2ID(tQSYMBOLS_BEG);
609 TOKEN2ID(tSTRING_END);
610 TOKEN2ID(tSTRING_DEND);
611 TOKEN2ID(tSTRING_DBEG);
612 TOKEN2ID(tSTRING_DVAR);
613 TOKEN2ID(tLAMBEG);
614 TOKEN2ID(tLABEL_END);
615 TOKEN2ID(tIGNORED_NL);
616 TOKEN2ID(tCOMMENT);
617 TOKEN2ID(tEMBDOC_BEG);
618 TOKEN2ID(tEMBDOC);
619 TOKEN2ID(tEMBDOC_END);
620 TOKEN2ID(tHEREDOC_BEG);
621 TOKEN2ID(tHEREDOC_END);
622 TOKEN2ID(k__END__);
623 TOKEN2ID(tLOWEST);
624 TOKEN2ID(tUMINUS_NUM);
625 TOKEN2ID(tLAST_TOKEN);
626#undef TOKEN2ID
627#undef TOKEN2ID2
628 }
629
630 rb_bug("parser_token2id: unknown token %d", tok);
631
632 UNREACHABLE_RETURN(0);
633}
634
635#endif
636
637RBIMPL_ATTR_NONNULL((1, 2, 3))
638static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
639RBIMPL_ATTR_NONNULL((1, 2))
640static int parser_yyerror0(struct parser_params*, const char*);
641#define yyerror0(msg) parser_yyerror0(p, (msg))
642#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
643#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
644#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
645#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
646#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
647#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
648
649static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
650static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
651static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
652static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
653static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
654
655#ifdef RIPPER
656#define compile_for_eval (0)
657#else
658#define compile_for_eval (p->parent_iseq != 0)
659#endif
660
661#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
662
663#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
664#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
665#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
666
667#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
668
669static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
670
671#ifndef RIPPER
672static inline void
673rb_discard_node(struct parser_params *p, NODE *n)
674{
675 rb_ast_delete_node(p->ast, n);
676}
677#endif
678
679#ifdef RIPPER
680static inline VALUE
681add_mark_object(struct parser_params *p, VALUE obj)
682{
683 if (!SPECIAL_CONST_P(obj)
684 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
685 ) {
686 rb_ast_add_mark_object(p->ast, obj);
687 }
688 return obj;
689}
690#else
691static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
692#endif
693
694static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
695#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
696
697/* Make a new temporal node, which should not be appeared in the
698 * result AST and does not have node_id and location. */
699static NODE* node_new_temporal(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2);
700#define NODE_NEW_TEMPORAL(t,a0,a1,a2) node_new_temporal(p, (t),(VALUE)(a0),(VALUE)(a1),(VALUE)(a2))
701
702static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
703
704static int
705parser_get_node_id(struct parser_params *p)
706{
707 int node_id = p->node_id;
708 p->node_id++;
709 return node_id;
710}
711
712#ifndef RIPPER
713static inline void
714set_line_body(NODE *body, int line)
715{
716 if (!body) return;
717 switch (nd_type(body)) {
718 case NODE_RESCUE:
719 case NODE_ENSURE:
720 nd_set_line(body, line);
721 }
722}
723
724#define yyparse ruby_yyparse
725
726static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
727static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
728#define new_nil(loc) NEW_NIL(loc)
729static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
730static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
731static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
732static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
733
734static NODE *newline_node(NODE*);
735static void fixpos(NODE*,NODE*);
736
737static int value_expr_gen(struct parser_params*,NODE*);
738static void void_expr(struct parser_params*,NODE*);
739static NODE *remove_begin(NODE*);
740static NODE *remove_begin_all(NODE*);
741#define value_expr(node) value_expr_gen(p, (node))
742static NODE *void_stmts(struct parser_params*,NODE*);
743static void reduce_nodes(struct parser_params*,NODE**);
744static void block_dup_check(struct parser_params*,NODE*,NODE*);
745
746static NODE *block_append(struct parser_params*,NODE*,NODE*);
747static NODE *list_append(struct parser_params*,NODE*,NODE*);
748static NODE *list_concat(NODE*,NODE*);
749static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
750static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
751static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
752static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
753static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
754static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
755static NODE *evstr2dstr(struct parser_params*,NODE*);
756static NODE *splat_array(NODE*);
757static void mark_lvar_used(struct parser_params *p, NODE *rhs);
758
759static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
760static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
761static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
762static 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);
763static 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;}
764
765static bool args_info_empty_p(struct rb_args_info *args);
766static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
767static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
768static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
769static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
770static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
771static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
772static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
773static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
774
775static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
776static NODE *args_with_numbered(struct parser_params*,NODE*,int);
777
778static VALUE negate_lit(struct parser_params*, VALUE);
779static NODE *ret_args(struct parser_params*,NODE*);
780static NODE *arg_blk_pass(NODE*,NODE*);
781static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
782static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
783
784static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
785static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
786
787static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
788static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
789
790static void rb_backref_error(struct parser_params*,NODE*);
791static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
792
793static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
794static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
795static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
796static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
797static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
798
799static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
800
801static NODE *opt_arg_append(NODE*, NODE*);
802static NODE *kwd_append(NODE*, NODE*);
803
804static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
805static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
806
807static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
808
809static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
810
811#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
812
813static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
814
815static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
816
817static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
818
819static rb_ast_id_table_t *local_tbl(struct parser_params*);
820
821static VALUE reg_compile(struct parser_params*, VALUE, int);
822static void reg_fragment_setenc(struct parser_params*, VALUE, int);
823static int reg_fragment_check(struct parser_params*, VALUE, int);
824static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
825
826static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
827static NODE *heredoc_dedent(struct parser_params*,NODE*);
828
829static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
830
831#define get_id(id) (id)
832#define get_value(val) (val)
833#define get_num(num) (num)
834#else /* RIPPER */
835#define NODE_RIPPER NODE_CDECL
836#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
837
838static inline int ripper_is_node_yylval(VALUE n);
839
840static inline VALUE
841ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
842{
843 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
844 add_mark_object(p, b);
845 add_mark_object(p, c);
846 return NEW_RIPPER(a, b, c, &NULL_LOC);
847}
848
849static inline int
850ripper_is_node_yylval(VALUE n)
851{
852 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
853}
854
855#define value_expr(node) ((void)(node))
856#define remove_begin(node) (node)
857#define void_stmts(p,x) (x)
858#define rb_dvar_defined(id, base) 0
859#define rb_local_defined(id, base) 0
860static ID ripper_get_id(VALUE);
861#define get_id(id) ripper_get_id(id)
862static VALUE ripper_get_value(VALUE);
863#define get_value(val) ripper_get_value(val)
864#define get_num(num) (int)get_id(num)
865static VALUE assignable(struct parser_params*,VALUE);
866static int id_is_var(struct parser_params *p, ID id);
867
868#define method_cond(p,node,loc) (node)
869#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
870#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
871#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
872#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
873
874#define new_nil(loc) Qnil
875
876static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
877
878static VALUE const_decl(struct parser_params *p, VALUE path);
879
880static VALUE var_field(struct parser_params *p, VALUE a);
881static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
882
883static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
884
885static VALUE backref_error(struct parser_params*, NODE *, VALUE);
886#endif /* !RIPPER */
887
888/* forward declaration */
889typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
890
891RUBY_SYMBOL_EXPORT_BEGIN
892VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
893int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
894enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
895VALUE rb_parser_lex_state_name(enum lex_state_e state);
896void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
897PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
898YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
899YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
900YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
901YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
902YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
903YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
904RUBY_SYMBOL_EXPORT_END
905
906static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
907static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
908#ifndef RIPPER
909static ID formal_argument(struct parser_params*, ID);
910#else
911static ID formal_argument(struct parser_params*, VALUE);
912#endif
913static ID shadowing_lvar(struct parser_params*,ID);
914static void new_bv(struct parser_params*,ID);
915
916static void local_push(struct parser_params*,int);
917static void local_pop(struct parser_params*);
918static void local_var(struct parser_params*, ID);
919static void arg_var(struct parser_params*, ID);
920static int local_id(struct parser_params *p, ID id);
921static int local_id_ref(struct parser_params*, ID, ID **);
922#ifndef RIPPER
923static ID internal_id(struct parser_params*);
924static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
925#endif
926static int check_forwarding_args(struct parser_params*);
927static void add_forwarding_args(struct parser_params *p);
928
929static const struct vtable *dyna_push(struct parser_params *);
930static void dyna_pop(struct parser_params*, const struct vtable *);
931static int dyna_in_block(struct parser_params*);
932#define dyna_var(p, id) local_var(p, id)
933static int dvar_defined(struct parser_params*, ID);
934static int dvar_defined_ref(struct parser_params*, ID, ID**);
935static int dvar_curr(struct parser_params*,ID);
936
937static int lvar_defined(struct parser_params*, ID);
938
939static NODE *numparam_push(struct parser_params *p);
940static void numparam_pop(struct parser_params *p, NODE *prev_inner);
941
942#ifdef RIPPER
943# define METHOD_NOT idNOT
944#else
945# define METHOD_NOT '!'
946#endif
947
948#define idFWD_REST '*'
949#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
950#define idFWD_BLOCK '&'
951#define idFWD_ALL idDot3
952#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
953
954#define RE_OPTION_ONCE (1<<16)
955#define RE_OPTION_ENCODING_SHIFT 8
956#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
957#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
958#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
959#define RE_OPTION_MASK 0xff
960#define RE_OPTION_ARG_ENCODING_NONE 32
961
962/* structs for managing terminator of string literal and heredocment */
963typedef struct rb_strterm_literal_struct {
964 union {
965 VALUE dummy;
966 long nest;
967 } u0;
968 union {
969 VALUE dummy;
970 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
971 } u1;
972 union {
973 VALUE dummy;
974 long paren; /* '(' of `%q(...)` */
975 } u2;
976 union {
977 VALUE dummy;
978 long term; /* ')' of `%q(...)` */
979 } u3;
980} rb_strterm_literal_t;
981
982#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
983
984struct rb_strterm_heredoc_struct {
985 VALUE lastline; /* the string of line that contains `<<"END"` */
986 long offset; /* the column of END in `<<"END"` */
987 int sourceline; /* lineno of the line that contains `<<"END"` */
988 unsigned length /* the length of END in `<<"END"` */
989#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
990 : HERETERM_LENGTH_BITS
991# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
992#else
993# define HERETERM_LENGTH_MAX UINT_MAX
994#endif
995 ;
996#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
997 unsigned quote: 1;
998 unsigned func: 8;
999#else
1000 uint8_t quote;
1001 uint8_t func;
1002#endif
1003};
1004STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
1005
1006#define STRTERM_HEREDOC IMEMO_FL_USER0
1007
1008struct rb_strterm_struct {
1009 VALUE flags;
1010 union {
1011 rb_strterm_literal_t literal;
1012 rb_strterm_heredoc_t heredoc;
1013 } u;
1014};
1015
1016#ifndef RIPPER
1017void
1018rb_strterm_mark(VALUE obj)
1019{
1020 rb_strterm_t *strterm = (rb_strterm_t*)obj;
1021 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
1022 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
1023 rb_gc_mark(heredoc->lastline);
1024 }
1025}
1026#endif
1027
1028#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1029size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1030
1031#define TOKEN2ID(tok) ( \
1032 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1033 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1034 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1035 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1036 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1037 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1038 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1039
1040/****** Ripper *******/
1041
1042#ifdef RIPPER
1043#define RIPPER_VERSION "0.1.0"
1044
1045static inline VALUE intern_sym(const char *name);
1046
1047#include "eventids1.c"
1048#include "eventids2.c"
1049
1050static VALUE ripper_dispatch0(struct parser_params*,ID);
1051static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1052static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1053static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1054static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1055static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1056static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1057static void ripper_error(struct parser_params *p);
1058
1059#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1060#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1061#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1062#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1063#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1064#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1065#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))
1066
1067#define yyparse ripper_yyparse
1068
1069#define ID2VAL(id) STATIC_ID2SYM(id)
1070#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1071#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1072
1073#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1074 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1075
1076#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
1077
1078static inline VALUE
1079new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1080{
1081 NODE *t = (NODE *)tail;
1082 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
1083 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
1084}
1085
1086static inline VALUE
1087new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1088{
1089 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
1090 add_mark_object(p, kw_args);
1091 add_mark_object(p, kw_rest_arg);
1092 add_mark_object(p, block);
1093 return (VALUE)t;
1094}
1095
1096static inline VALUE
1097args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1098{
1099 return args;
1100}
1101
1102static VALUE
1103new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1104{
1105 NODE *t = (NODE *)aryptn;
1106 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
1107
1108 if (!NIL_P(pre_arg)) {
1109 if (!NIL_P(pre_args)) {
1110 rb_ary_unshift(pre_args, pre_arg);
1111 }
1112 else {
1113 pre_args = rb_ary_new_from_args(1, pre_arg);
1114 }
1115 }
1116 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1117}
1118
1119static VALUE
1120new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1121{
1122 NODE *t;
1123
1124 if (has_rest) {
1125 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
1126 }
1127 else {
1128 rest_arg = Qnil;
1129 }
1130
1131 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
1132 add_mark_object(p, pre_args);
1133 add_mark_object(p, rest_arg);
1134 add_mark_object(p, post_args);
1135 return (VALUE)t;
1136}
1137
1138static VALUE
1139new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1140{
1141 NODE *t = (NODE *)fndptn;
1142 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
1143
1144 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1145}
1146
1147static VALUE
1148new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1149{
1150 NODE *t;
1151
1152 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
1153 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
1154
1155 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
1156 add_mark_object(p, pre_rest_arg);
1157 add_mark_object(p, args);
1158 add_mark_object(p, post_rest_arg);
1159 return (VALUE)t;
1160}
1161
1162#define new_hash(p,h,l) rb_ary_new_from_args(0)
1163
1164static VALUE
1165new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1166{
1167 return ary;
1168}
1169
1170static VALUE
1171new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1172{
1173 NODE *t = (NODE *)hshptn;
1174 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
1175 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1176}
1177
1178static VALUE
1179new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1180{
1181 NODE *t;
1182 if (kw_rest_arg) {
1183 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1184 }
1185 else {
1186 kw_rest_arg = Qnil;
1187 }
1188 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
1189
1190 add_mark_object(p, kw_args);
1191 add_mark_object(p, kw_rest_arg);
1192 return (VALUE)t;
1193}
1194
1195#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1196
1197static VALUE heredoc_dedent(struct parser_params*,VALUE);
1198
1199#else
1200#define ID2VAL(id) (id)
1201#define TOKEN2VAL(t) ID2VAL(t)
1202#define KWD2EID(t, v) keyword_##t
1203
1204static NODE *
1205set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
1206{
1207 body = remove_begin(body);
1208 reduce_nodes(p, &body);
1209 n->nd_defn = NEW_SCOPE(args, body, loc);
1210 n->nd_loc = *loc;
1211 nd_set_line(n->nd_defn, loc->end_pos.lineno);
1212 set_line_body(body, loc->beg_pos.lineno);
1213 return n;
1214}
1215
1216static NODE *
1217rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1218 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1219{
1220 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1221 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1222 loc.beg_pos = arg_loc->beg_pos;
1223 return NEW_RESCUE(arg, rescue, 0, &loc);
1224}
1225
1226#endif /* RIPPER */
1227
1228static void
1229restore_defun(struct parser_params *p, NODE *name)
1230{
1231 NODE *save = name->nd_next;
1232 YYSTYPE c = {.val = save->nd_cval};
1233 p->cur_arg = name->nd_vid;
1234 p->ctxt.in_def = c.ctxt.in_def;
1235 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
1236 p->max_numparam = (int)save->nd_nth;
1237 numparam_pop(p, save->nd_head);
1238}
1239
1240static void
1241endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
1242{
1243#ifdef RIPPER
1244 defn = defn->nd_defn;
1245#endif
1246 ID mid = defn->nd_mid;
1247 if (is_attrset_id(mid)) {
1248 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1249 }
1250 token_info_drop(p, "def", loc->beg_pos);
1251}
1252
1253#define debug_token_line(p, name, line) if (p->debug) rb_parser_printf(p, name ":%d (%d: %ld|%ld|%ld)\n", line, p->ruby_sourceline, p->lex.ptok - p->lex.pbeg, p->lex.pcur - p->lex.ptok, p->lex.pend - p->lex.pcur)
1254
1255#ifndef RIPPER
1256# define Qnone 0
1257# define Qnull 0
1258# define ifndef_ripper(x) (x)
1259#else
1260# define Qnone Qnil
1261# define Qnull Qundef
1262# define ifndef_ripper(x)
1263#endif
1264
1265# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1266# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1267# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1268# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1269# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1270# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1271# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1272# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1273# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1274# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1275# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1276# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1277# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1278# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1279# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1280# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1281# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1282# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1283# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1284# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1285#ifdef RIPPER
1286static ID id_warn, id_warning, id_gets, id_assoc;
1287# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1288# define WARN_S_L(s,l) STR_NEW(s,l)
1289# define WARN_S(s) STR_NEW2(s)
1290# define WARN_I(i) INT2NUM(i)
1291# define WARN_ID(i) rb_id2str(i)
1292# define WARN_IVAL(i) i
1293# define PRIsWARN "s"
1294# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1295# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1296# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1297# ifdef HAVE_VA_ARGS_MACRO
1298# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1299# else
1300# define WARN_CALL rb_funcall
1301# endif
1302# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1303# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1304# ifdef HAVE_VA_ARGS_MACRO
1305# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1306# else
1307# define WARNING_CALL rb_funcall
1308# endif
1309PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1310# define compile_error ripper_compile_error
1311#else
1312# define WARN_S_L(s,l) s
1313# define WARN_S(s) s
1314# define WARN_I(i) i
1315# define WARN_ID(i) rb_id2name(i)
1316# define WARN_IVAL(i) NUM2INT(i)
1317# define PRIsWARN PRIsVALUE
1318# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1319# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1320# define WARN_CALL rb_compile_warn
1321# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1322# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1323# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1324# define WARNING_CALL rb_compile_warning
1325PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1326# define compile_error parser_compile_error
1327#endif
1328
1329#define WARN_EOL(tok) \
1330 (looking_at_eol_p(p) ? \
1331 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1332 (void)0)
1333static int looking_at_eol_p(struct parser_params *p);
1334%}
1335
1336%expect 0
1337%define api.pure
1338%define parse.error verbose
1339%printer {
1340#ifndef RIPPER
1341 if ($$) {
1342 rb_parser_printf(p, "%s", ruby_node_name(nd_type($$)));
1343 }
1344#else
1345#endif
1346} <node>
1347%printer {
1348#ifndef RIPPER
1349 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1350#else
1351 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1352#endif
1353} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1354%printer {
1355#ifndef RIPPER
1356 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1357#else
1358 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1359#endif
1360} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1361%printer {
1362#ifndef RIPPER
1363 rb_parser_printf(p, "$%ld", $$->nd_nth);
1364#else
1365 rb_parser_printf(p, "%"PRIsVALUE, $$);
1366#endif
1367} tNTH_REF
1368%printer {
1369#ifndef RIPPER
1370 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1371#else
1372 rb_parser_printf(p, "%"PRIsVALUE, $$);
1373#endif
1374} tBACK_REF
1375
1376%lex-param {struct parser_params *p}
1377%parse-param {struct parser_params *p}
1378%initial-action
1379{
1380 RUBY_SET_YYLLOC_OF_NONE(@$);
1381};
1382
1383%union {
1384 VALUE val;
1385 NODE *node;
1386 ID id;
1387 int num;
1388 st_table *tbl;
1389 const struct vtable *vars;
1390 struct rb_strterm_struct *strterm;
1391 struct lex_context ctxt;
1392}
1393
1394%token <id>
1395 keyword_class "`class'"
1396 keyword_module "`module'"
1397 keyword_def "`def'"
1398 keyword_undef "`undef'"
1399 keyword_begin "`begin'"
1400 keyword_rescue "`rescue'"
1401 keyword_ensure "`ensure'"
1402 keyword_end "`end'"
1403 keyword_if "`if'"
1404 keyword_unless "`unless'"
1405 keyword_then "`then'"
1406 keyword_elsif "`elsif'"
1407 keyword_else "`else'"
1408 keyword_case "`case'"
1409 keyword_when "`when'"
1410 keyword_while "`while'"
1411 keyword_until "`until'"
1412 keyword_for "`for'"
1413 keyword_break "`break'"
1414 keyword_next "`next'"
1415 keyword_redo "`redo'"
1416 keyword_retry "`retry'"
1417 keyword_in "`in'"
1418 keyword_do "`do'"
1419 keyword_do_cond "`do' for condition"
1420 keyword_do_block "`do' for block"
1421 keyword_do_LAMBDA "`do' for lambda"
1422 keyword_return "`return'"
1423 keyword_yield "`yield'"
1424 keyword_super "`super'"
1425 keyword_self "`self'"
1426 keyword_nil "`nil'"
1427 keyword_true "`true'"
1428 keyword_false "`false'"
1429 keyword_and "`and'"
1430 keyword_or "`or'"
1431 keyword_not "`not'"
1432 modifier_if "`if' modifier"
1433 modifier_unless "`unless' modifier"
1434 modifier_while "`while' modifier"
1435 modifier_until "`until' modifier"
1436 modifier_rescue "`rescue' modifier"
1437 keyword_alias "`alias'"
1438 keyword_defined "`defined?'"
1439 keyword_BEGIN "`BEGIN'"
1440 keyword_END "`END'"
1441 keyword__LINE__ "`__LINE__'"
1442 keyword__FILE__ "`__FILE__'"
1443 keyword__ENCODING__ "`__ENCODING__'"
1444
1445%token <id> tIDENTIFIER "local variable or method"
1446%token <id> tFID "method"
1447%token <id> tGVAR "global variable"
1448%token <id> tIVAR "instance variable"
1449%token <id> tCONSTANT "constant"
1450%token <id> tCVAR "class variable"
1451%token <id> tLABEL "label"
1452%token <node> tINTEGER "integer literal"
1453%token <node> tFLOAT "float literal"
1454%token <node> tRATIONAL "rational literal"
1455%token <node> tIMAGINARY "imaginary literal"
1456%token <node> tCHAR "char literal"
1457%token <node> tNTH_REF "numbered reference"
1458%token <node> tBACK_REF "back reference"
1459%token <node> tSTRING_CONTENT "literal content"
1460%token <num> tREGEXP_END
1461%token <num> tDUMNY_END "dummy end"
1462
1463%type <node> singleton strings string string1 xstring regexp
1464%type <node> string_contents xstring_contents regexp_contents string_content
1465%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1466%type <node> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1467%type <node> top_compstmt top_stmts top_stmt begin_block
1468%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1469%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1470%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1471%type <node> args call_args opt_call_args
1472%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1473%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1474%type <node> command_rhs arg_rhs
1475%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1476%type <node> f_block_optarg f_block_opt
1477%type <node> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1478%type <node> f_optarg f_marg f_marg_list f_margs f_rest_marg
1479%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1480%type <node> block_param opt_block_param block_param_def f_opt
1481%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1482%type <node> bv_decls opt_bv_decl bvar
1483%type <node> lambda f_larglist lambda_body brace_body do_body
1484%type <node> brace_block cmd_brace_block do_block lhs none fitem
1485%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1486%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1487%type <node> p_expr p_as p_alt p_expr_basic p_find
1488%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1489%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
1490%type <node> p_kwargs p_kwarg p_kw
1491%type <id> keyword_variable user_variable sym operation operation2 operation3
1492%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1493%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1494%type <id> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1495%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var
1496 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1497%token END_OF_INPUT 0 "end-of-input"
1498%token <id> '.'
1499/* escaped chars, should be ignored otherwise */
1500%token <id> '\\' "backslash"
1501%token tSP "escaped space"
1502%token <id> '\t' "escaped horizontal tab"
1503%token <id> '\f' "escaped form feed"
1504%token <id> '\r' "escaped carriage return"
1505%token <id> '\13' "escaped vertical tab"
1506%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1507%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1508%token tPOW RUBY_TOKEN(POW) "**"
1509%token tCMP RUBY_TOKEN(CMP) "<=>"
1510%token tEQ RUBY_TOKEN(EQ) "=="
1511%token tEQQ RUBY_TOKEN(EQQ) "==="
1512%token tNEQ RUBY_TOKEN(NEQ) "!="
1513%token tGEQ RUBY_TOKEN(GEQ) ">="
1514%token tLEQ RUBY_TOKEN(LEQ) "<="
1515%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1516%token tOROP RUBY_TOKEN(OROP) "||"
1517%token tMATCH RUBY_TOKEN(MATCH) "=~"
1518%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1519%token tDOT2 RUBY_TOKEN(DOT2) ".."
1520%token tDOT3 RUBY_TOKEN(DOT3) "..."
1521%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1522%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1523%token tAREF RUBY_TOKEN(AREF) "[]"
1524%token tASET RUBY_TOKEN(ASET) "[]="
1525%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1526%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1527%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1528%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1529%token tCOLON3 ":: at EXPR_BEG"
1530%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1531%token tASSOC "=>"
1532%token tLPAREN "("
1533%token tLPAREN_ARG "( arg"
1534%token tRPAREN ")"
1535%token tLBRACK "["
1536%token tLBRACE "{"
1537%token tLBRACE_ARG "{ arg"
1538%token tSTAR "*"
1539%token tDSTAR "**arg"
1540%token tAMPER "&"
1541%token tLAMBDA "->"
1542%token tSYMBEG "symbol literal"
1543%token tSTRING_BEG "string literal"
1544%token tXSTRING_BEG "backtick literal"
1545%token tREGEXP_BEG "regexp literal"
1546%token tWORDS_BEG "word list"
1547%token tQWORDS_BEG "verbatim word list"
1548%token tSYMBOLS_BEG "symbol list"
1549%token tQSYMBOLS_BEG "verbatim symbol list"
1550%token tSTRING_END "terminator"
1551%token tSTRING_DEND "'}'"
1552%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1553
1554%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
1555%token tHEREDOC_BEG tHEREDOC_END k__END__
1556
1557/*
1558 * precedence table
1559 */
1560
1561%nonassoc tLOWEST
1562%nonassoc tLBRACE_ARG
1563
1564%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1565%left keyword_or keyword_and
1566%right keyword_not
1567%nonassoc keyword_defined
1568%right '=' tOP_ASGN
1569%left modifier_rescue
1570%right '?' ':'
1571%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1572%left tOROP
1573%left tANDOP
1574%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1575%left '>' tGEQ '<' tLEQ
1576%left '|' '^'
1577%left '&'
1578%left tLSHFT tRSHFT
1579%left '+' '-'
1580%left '*' '/' '%'
1581%right tUMINUS_NUM tUMINUS
1582%right tPOW
1583%right '!' '~' tUPLUS
1584
1585%token tLAST_TOKEN
1586
1587%%
1588program : {
1589 SET_LEX_STATE(EXPR_BEG);
1590 local_push(p, ifndef_ripper(1)+0);
1591 }
1592 top_compstmt
1593 {
1594 /*%%%*/
1595 if ($2 && !compile_for_eval) {
1596 NODE *node = $2;
1597 /* last expression should not be void */
1598 if (nd_type_p(node, NODE_BLOCK)) {
1599 while (node->nd_next) {
1600 node = node->nd_next;
1601 }
1602 node = node->nd_head;
1603 }
1604 node = remove_begin(node);
1605 void_expr(p, node);
1606 }
1607 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1608 /*% %*/
1609 /*% ripper[final]: program!($2) %*/
1610 local_pop(p);
1611 }
1612 ;
1613
1614top_compstmt : top_stmts opt_terms
1615 {
1616 $$ = void_stmts(p, $1);
1617 }
1618 ;
1619
1620top_stmts : none
1621 {
1622 /*%%%*/
1623 $$ = NEW_BEGIN(0, &@$);
1624 /*% %*/
1625 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1626 }
1627 | top_stmt
1628 {
1629 /*%%%*/
1630 $$ = newline_node($1);
1631 /*% %*/
1632 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1633 }
1634 | top_stmts terms top_stmt
1635 {
1636 /*%%%*/
1637 $$ = block_append(p, $1, newline_node($3));
1638 /*% %*/
1639 /*% ripper: stmts_add!($1, $3) %*/
1640 }
1641 ;
1642
1643top_stmt : stmt
1644 | keyword_BEGIN begin_block
1645 {
1646 $$ = $2;
1647 }
1648 ;
1649
1650begin_block : '{' top_compstmt '}'
1651 {
1652 /*%%%*/
1653 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1654 NEW_BEGIN($2, &@$));
1655 $$ = NEW_BEGIN(0, &@$);
1656 /*% %*/
1657 /*% ripper: BEGIN!($2) %*/
1658 }
1659 ;
1660
1661bodystmt : compstmt
1662 opt_rescue
1663 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1664 compstmt
1665 opt_ensure
1666 {
1667 /*%%%*/
1668 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1669 /*% %*/
1670 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1671 }
1672 | compstmt
1673 opt_rescue
1674 opt_ensure
1675 {
1676 /*%%%*/
1677 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1678 /*% %*/
1679 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1680 }
1681 ;
1682
1683compstmt : stmts opt_terms
1684 {
1685 $$ = void_stmts(p, $1);
1686 }
1687 ;
1688
1689stmts : none
1690 {
1691 /*%%%*/
1692 $$ = NEW_BEGIN(0, &@$);
1693 /*% %*/
1694 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1695 }
1696 | stmt_or_begin
1697 {
1698 /*%%%*/
1699 $$ = newline_node($1);
1700 /*% %*/
1701 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1702 }
1703 | stmts terms stmt_or_begin
1704 {
1705 /*%%%*/
1706 $$ = block_append(p, $1, newline_node($3));
1707 /*% %*/
1708 /*% ripper: stmts_add!($1, $3) %*/
1709 }
1710 ;
1711
1712stmt_or_begin : stmt
1713 {
1714 $$ = $1;
1715 }
1716 | keyword_BEGIN
1717 {
1718 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1719 }
1720 begin_block
1721 {
1722 $$ = $3;
1723 }
1724 ;
1725
1726stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1727 {
1728 /*%%%*/
1729 $$ = NEW_ALIAS($2, $4, &@$);
1730 /*% %*/
1731 /*% ripper: alias!($2, $4) %*/
1732 }
1733 | keyword_alias tGVAR tGVAR
1734 {
1735 /*%%%*/
1736 $$ = NEW_VALIAS($2, $3, &@$);
1737 /*% %*/
1738 /*% ripper: var_alias!($2, $3) %*/
1739 }
1740 | keyword_alias tGVAR tBACK_REF
1741 {
1742 /*%%%*/
1743 char buf[2];
1744 buf[0] = '$';
1745 buf[1] = (char)$3->nd_nth;
1746 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1747 /*% %*/
1748 /*% ripper: var_alias!($2, $3) %*/
1749 }
1750 | keyword_alias tGVAR tNTH_REF
1751 {
1752 static const char mesg[] = "can't make alias for the number variables";
1753 /*%%%*/
1754 yyerror1(&@3, mesg);
1755 $$ = NEW_BEGIN(0, &@$);
1756 /*% %*/
1757 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1758 }
1759 | keyword_undef undef_list
1760 {
1761 /*%%%*/
1762 $$ = $2;
1763 /*% %*/
1764 /*% ripper: undef!($2) %*/
1765 }
1766 | stmt modifier_if expr_value
1767 {
1768 /*%%%*/
1769 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1770 fixpos($$, $3);
1771 /*% %*/
1772 /*% ripper: if_mod!($3, $1) %*/
1773 }
1774 | stmt modifier_unless expr_value
1775 {
1776 /*%%%*/
1777 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1778 fixpos($$, $3);
1779 /*% %*/
1780 /*% ripper: unless_mod!($3, $1) %*/
1781 }
1782 | stmt modifier_while expr_value
1783 {
1784 /*%%%*/
1785 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1786 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1787 }
1788 else {
1789 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1790 }
1791 /*% %*/
1792 /*% ripper: while_mod!($3, $1) %*/
1793 }
1794 | stmt modifier_until expr_value
1795 {
1796 /*%%%*/
1797 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1798 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1799 }
1800 else {
1801 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1802 }
1803 /*% %*/
1804 /*% ripper: until_mod!($3, $1) %*/
1805 }
1806 | stmt modifier_rescue stmt
1807 {
1808 /*%%%*/
1809 NODE *resq;
1810 YYLTYPE loc = code_loc_gen(&@2, &@3);
1811 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1812 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1813 /*% %*/
1814 /*% ripper: rescue_mod!($1, $3) %*/
1815 }
1816 | keyword_END '{' compstmt '}'
1817 {
1818 if (p->ctxt.in_def) {
1819 rb_warn0("END in method; use at_exit");
1820 }
1821 /*%%%*/
1822 {
1823 NODE *scope = NEW_NODE(
1824 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1825 $$ = NEW_POSTEXE(scope, &@$);
1826 }
1827 /*% %*/
1828 /*% ripper: END!($3) %*/
1829 }
1830 | command_asgn
1831 | mlhs '=' lex_ctxt command_call
1832 {
1833 /*%%%*/
1834 value_expr($4);
1835 $$ = node_assign(p, $1, $4, $3, &@$);
1836 /*% %*/
1837 /*% ripper: massign!($1, $4) %*/
1838 }
1839 | lhs '=' lex_ctxt mrhs
1840 {
1841 /*%%%*/
1842 $$ = node_assign(p, $1, $4, $3, &@$);
1843 /*% %*/
1844 /*% ripper: assign!($1, $4) %*/
1845 }
1846 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1847 {
1848 /*%%%*/
1849 YYLTYPE loc = code_loc_gen(&@5, &@6);
1850 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1851 /*% %*/
1852 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1853 }
1854 | mlhs '=' lex_ctxt mrhs_arg
1855 {
1856 /*%%%*/
1857 $$ = node_assign(p, $1, $4, $3, &@$);
1858 /*% %*/
1859 /*% ripper: massign!($1, $4) %*/
1860 }
1861 | expr
1862 | error
1863 {
1864 /*%%%*/
1865 $$ = NEW_ERROR(&@$);
1866 /*% %*/
1867 }
1868 ;
1869
1870command_asgn : lhs '=' lex_ctxt command_rhs
1871 {
1872 /*%%%*/
1873 $$ = node_assign(p, $1, $4, $3, &@$);
1874 /*% %*/
1875 /*% ripper: assign!($1, $4) %*/
1876 }
1877 | var_lhs tOP_ASGN lex_ctxt command_rhs
1878 {
1879 /*%%%*/
1880 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1881 /*% %*/
1882 /*% ripper: opassign!($1, $2, $4) %*/
1883 }
1884 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1885 {
1886 /*%%%*/
1887 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1888 /*% %*/
1889 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1890
1891 }
1892 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1893 {
1894 /*%%%*/
1895 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1896 /*% %*/
1897 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1898 }
1899 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1900 {
1901 /*%%%*/
1902 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1903 /*% %*/
1904 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1905 }
1906 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1907 {
1908 /*%%%*/
1909 YYLTYPE loc = code_loc_gen(&@1, &@3);
1910 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1911 /*% %*/
1912 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1913 }
1914 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1915 {
1916 /*%%%*/
1917 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1918 /*% %*/
1919 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1920 }
1921 | defn_head f_opt_paren_args '=' command
1922 {
1923 endless_method_name(p, $<node>1, &@1);
1924 restore_defun(p, $<node>1->nd_defn);
1925 /*%%%*/
1926 $$ = set_defun_body(p, $1, $2, $4, &@$);
1927 /*% %*/
1928 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1929 /*% ripper: def!(get_value($1), $2, $4) %*/
1930 local_pop(p);
1931 }
1932 | defn_head f_opt_paren_args '=' command modifier_rescue arg
1933 {
1934 endless_method_name(p, $<node>1, &@1);
1935 restore_defun(p, $<node>1->nd_defn);
1936 /*%%%*/
1937 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1938 $$ = set_defun_body(p, $1, $2, $4, &@$);
1939 /*% %*/
1940 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1941 /*% ripper: def!(get_value($1), $2, $4) %*/
1942 local_pop(p);
1943 }
1944 | defs_head f_opt_paren_args '=' command
1945 {
1946 endless_method_name(p, $<node>1, &@1);
1947 restore_defun(p, $<node>1->nd_defn);
1948 /*%%%*/
1949 $$ = set_defun_body(p, $1, $2, $4, &@$);
1950 /*%
1951 $1 = get_value($1);
1952 %*/
1953 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1954 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1955 local_pop(p);
1956 }
1957 | defs_head f_opt_paren_args '=' command modifier_rescue arg
1958 {
1959 endless_method_name(p, $<node>1, &@1);
1960 restore_defun(p, $<node>1->nd_defn);
1961 /*%%%*/
1962 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1963 $$ = set_defun_body(p, $1, $2, $4, &@$);
1964 /*%
1965 $1 = get_value($1);
1966 %*/
1967 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1968 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1969 local_pop(p);
1970 }
1971 | backref tOP_ASGN lex_ctxt command_rhs
1972 {
1973 /*%%%*/
1974 rb_backref_error(p, $1);
1975 $$ = NEW_BEGIN(0, &@$);
1976 /*% %*/
1977 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1978 }
1979 ;
1980
1981command_rhs : command_call %prec tOP_ASGN
1982 {
1983 value_expr($1);
1984 $$ = $1;
1985 }
1986 | command_call modifier_rescue stmt
1987 {
1988 /*%%%*/
1989 YYLTYPE loc = code_loc_gen(&@2, &@3);
1990 value_expr($1);
1991 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1992 /*% %*/
1993 /*% ripper: rescue_mod!($1, $3) %*/
1994 }
1995 | command_asgn
1996 ;
1997
1998expr : command_call
1999 | expr keyword_and expr
2000 {
2001 $$ = logop(p, idAND, $1, $3, &@2, &@$);
2002 }
2003 | expr keyword_or expr
2004 {
2005 $$ = logop(p, idOR, $1, $3, &@2, &@$);
2006 }
2007 | keyword_not opt_nl expr
2008 {
2009 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2010 }
2011 | '!' command_call
2012 {
2013 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2014 }
2015 | arg tASSOC
2016 {
2017 value_expr($1);
2018 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2019 p->command_start = FALSE;
2020 $<ctxt>2 = p->ctxt;
2021 p->ctxt.in_kwarg = 1;
2022 $<tbl>$ = push_pvtbl(p);
2023 }
2024 {
2025 $<tbl>$ = push_pktbl(p);
2026 }
2027 p_top_expr_body
2028 {
2029 pop_pktbl(p, $<tbl>4);
2030 pop_pvtbl(p, $<tbl>3);
2031 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2032 /*%%%*/
2033 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
2034 /*% %*/
2035 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2036 }
2037 | arg keyword_in
2038 {
2039 value_expr($1);
2040 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2041 p->command_start = FALSE;
2042 $<ctxt>2 = p->ctxt;
2043 p->ctxt.in_kwarg = 1;
2044 $<tbl>$ = push_pvtbl(p);
2045 }
2046 {
2047 $<tbl>$ = push_pktbl(p);
2048 }
2049 p_top_expr_body
2050 {
2051 pop_pktbl(p, $<tbl>4);
2052 pop_pvtbl(p, $<tbl>3);
2053 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2054 /*%%%*/
2055 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
2056 /*% %*/
2057 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2058 }
2059 | arg %prec tLBRACE_ARG
2060 ;
2061
2062def_name : fname
2063 {
2064 ID fname = get_id($1);
2065 ID cur_arg = p->cur_arg;
2066 YYSTYPE c = {.ctxt = p->ctxt};
2067 numparam_name(p, fname);
2068 NODE *save =
2069 NODE_NEW_TEMPORAL(NODE_SELF,
2070 /*head*/numparam_push(p),
2071 /*nth*/p->max_numparam,
2072 /*cval*/c.val);
2073 local_push(p, 0);
2074 p->cur_arg = 0;
2075 p->ctxt.in_def = 1;
2076 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*args*/save, &@$);
2077 /*%%%*/
2078 /*%
2079 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
2080 %*/
2081 }
2082 ;
2083
2084defn_head : k_def def_name
2085 {
2086 $$ = $2;
2087 /*%%%*/
2088 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
2089 /*% %*/
2090 }
2091 ;
2092
2093defs_head : k_def singleton dot_or_colon
2094 {
2095 SET_LEX_STATE(EXPR_FNAME);
2096 p->ctxt.in_argdef = 1;
2097 }
2098 def_name
2099 {
2100 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2101 $$ = $5;
2102 /*%%%*/
2103 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
2104 /*%
2105 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
2106 add_mark_object(p, ary);
2107 $<node>$->nd_rval = ary;
2108 %*/
2109 }
2110 ;
2111
2112expr_value : expr
2113 {
2114 value_expr($1);
2115 $$ = $1;
2116 }
2117 | error
2118 {
2119 /*%%%*/
2120 $$ = NEW_ERROR(&@$);
2121 /*% %*/
2122 }
2123 ;
2124
2125expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2126 {
2127 $$ = $2;
2128 }
2129 ;
2130
2131command_call : command
2132 | block_command
2133 ;
2134
2135block_command : block_call
2136 | block_call call_op2 operation2 command_args
2137 {
2138 /*%%%*/
2139 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2140 /*% %*/
2141 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2142 }
2143 ;
2144
2145cmd_brace_block : tLBRACE_ARG brace_body '}'
2146 {
2147 $$ = $2;
2148 /*%%%*/
2149 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
2150 nd_set_line($$, @1.end_pos.lineno);
2151 /*% %*/
2152 }
2153 ;
2154
2155fcall : operation
2156 {
2157 /*%%%*/
2158 $$ = NEW_FCALL($1, 0, &@$);
2159 nd_set_line($$, p->tokline);
2160 /*% %*/
2161 /*% ripper: $1 %*/
2162 }
2163 ;
2164
2165command : fcall command_args %prec tLOWEST
2166 {
2167 /*%%%*/
2168 $1->nd_args = $2;
2169 nd_set_last_loc($1, @2.end_pos);
2170 $$ = $1;
2171 /*% %*/
2172 /*% ripper: command!($1, $2) %*/
2173 }
2174 | fcall command_args cmd_brace_block
2175 {
2176 /*%%%*/
2177 block_dup_check(p, $2, $3);
2178 $1->nd_args = $2;
2179 $$ = method_add_block(p, $1, $3, &@$);
2180 fixpos($$, $1);
2181 nd_set_last_loc($1, @2.end_pos);
2182 /*% %*/
2183 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2184 }
2185 | primary_value call_op operation2 command_args %prec tLOWEST
2186 {
2187 /*%%%*/
2188 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2189 /*% %*/
2190 /*% ripper: command_call!($1, $2, $3, $4) %*/
2191 }
2192 | primary_value call_op operation2 command_args cmd_brace_block
2193 {
2194 /*%%%*/
2195 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2196 /*% %*/
2197 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2198 }
2199 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2200 {
2201 /*%%%*/
2202 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2203 /*% %*/
2204 /*% ripper: command_call!($1, $2, $3, $4) %*/
2205 }
2206 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2207 {
2208 /*%%%*/
2209 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2210 /*% %*/
2211 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2212 }
2213 | keyword_super command_args
2214 {
2215 /*%%%*/
2216 $$ = NEW_SUPER($2, &@$);
2217 fixpos($$, $2);
2218 /*% %*/
2219 /*% ripper: super!($2) %*/
2220 }
2221 | keyword_yield command_args
2222 {
2223 /*%%%*/
2224 $$ = new_yield(p, $2, &@$);
2225 fixpos($$, $2);
2226 /*% %*/
2227 /*% ripper: yield!($2) %*/
2228 }
2229 | k_return call_args
2230 {
2231 /*%%%*/
2232 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2233 /*% %*/
2234 /*% ripper: return!($2) %*/
2235 }
2236 | keyword_break call_args
2237 {
2238 /*%%%*/
2239 $$ = NEW_BREAK(ret_args(p, $2), &@$);
2240 /*% %*/
2241 /*% ripper: break!($2) %*/
2242 }
2243 | keyword_next call_args
2244 {
2245 /*%%%*/
2246 $$ = NEW_NEXT(ret_args(p, $2), &@$);
2247 /*% %*/
2248 /*% ripper: next!($2) %*/
2249 }
2250 ;
2251
2252mlhs : mlhs_basic
2253 | tLPAREN mlhs_inner rparen
2254 {
2255 /*%%%*/
2256 $$ = $2;
2257 /*% %*/
2258 /*% ripper: mlhs_paren!($2) %*/
2259 }
2260 ;
2261
2262mlhs_inner : mlhs_basic
2263 | tLPAREN mlhs_inner rparen
2264 {
2265 /*%%%*/
2266 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
2267 /*% %*/
2268 /*% ripper: mlhs_paren!($2) %*/
2269 }
2270 ;
2271
2272mlhs_basic : mlhs_head
2273 {
2274 /*%%%*/
2275 $$ = NEW_MASGN($1, 0, &@$);
2276 /*% %*/
2277 /*% ripper: $1 %*/
2278 }
2279 | mlhs_head mlhs_item
2280 {
2281 /*%%%*/
2282 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2283 /*% %*/
2284 /*% ripper: mlhs_add!($1, $2) %*/
2285 }
2286 | mlhs_head tSTAR mlhs_node
2287 {
2288 /*%%%*/
2289 $$ = NEW_MASGN($1, $3, &@$);
2290 /*% %*/
2291 /*% ripper: mlhs_add_star!($1, $3) %*/
2292 }
2293 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2294 {
2295 /*%%%*/
2296 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2297 /*% %*/
2298 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2299 }
2300 | mlhs_head tSTAR
2301 {
2302 /*%%%*/
2303 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2304 /*% %*/
2305 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2306 }
2307 | mlhs_head tSTAR ',' mlhs_post
2308 {
2309 /*%%%*/
2310 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2311 /*% %*/
2312 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2313 }
2314 | tSTAR mlhs_node
2315 {
2316 /*%%%*/
2317 $$ = NEW_MASGN(0, $2, &@$);
2318 /*% %*/
2319 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2320 }
2321 | tSTAR mlhs_node ',' mlhs_post
2322 {
2323 /*%%%*/
2324 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2325 /*% %*/
2326 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2327 }
2328 | tSTAR
2329 {
2330 /*%%%*/
2331 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2332 /*% %*/
2333 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2334 }
2335 | tSTAR ',' mlhs_post
2336 {
2337 /*%%%*/
2338 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2339 /*% %*/
2340 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2341 }
2342 ;
2343
2344mlhs_item : mlhs_node
2345 | tLPAREN mlhs_inner rparen
2346 {
2347 /*%%%*/
2348 $$ = $2;
2349 /*% %*/
2350 /*% ripper: mlhs_paren!($2) %*/
2351 }
2352 ;
2353
2354mlhs_head : mlhs_item ','
2355 {
2356 /*%%%*/
2357 $$ = NEW_LIST($1, &@1);
2358 /*% %*/
2359 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2360 }
2361 | mlhs_head mlhs_item ','
2362 {
2363 /*%%%*/
2364 $$ = list_append(p, $1, $2);
2365 /*% %*/
2366 /*% ripper: mlhs_add!($1, $2) %*/
2367 }
2368 ;
2369
2370mlhs_post : mlhs_item
2371 {
2372 /*%%%*/
2373 $$ = NEW_LIST($1, &@$);
2374 /*% %*/
2375 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2376 }
2377 | mlhs_post ',' mlhs_item
2378 {
2379 /*%%%*/
2380 $$ = list_append(p, $1, $3);
2381 /*% %*/
2382 /*% ripper: mlhs_add!($1, $3) %*/
2383 }
2384 ;
2385
2386mlhs_node : user_variable
2387 {
2388 /*%%%*/
2389 $$ = assignable(p, $1, 0, &@$);
2390 /*% %*/
2391 /*% ripper: assignable(p, var_field(p, $1)) %*/
2392 }
2393 | keyword_variable
2394 {
2395 /*%%%*/
2396 $$ = assignable(p, $1, 0, &@$);
2397 /*% %*/
2398 /*% ripper: assignable(p, var_field(p, $1)) %*/
2399 }
2400 | primary_value '[' opt_call_args rbracket
2401 {
2402 /*%%%*/
2403 $$ = aryset(p, $1, $3, &@$);
2404 /*% %*/
2405 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2406 }
2407 | primary_value call_op tIDENTIFIER
2408 {
2409 if ($2 == tANDDOT) {
2410 yyerror1(&@2, "&. inside multiple assignment destination");
2411 }
2412 /*%%%*/
2413 $$ = attrset(p, $1, $2, $3, &@$);
2414 /*% %*/
2415 /*% ripper: field!($1, $2, $3) %*/
2416 }
2417 | primary_value tCOLON2 tIDENTIFIER
2418 {
2419 /*%%%*/
2420 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2421 /*% %*/
2422 /*% ripper: const_path_field!($1, $3) %*/
2423 }
2424 | primary_value call_op tCONSTANT
2425 {
2426 if ($2 == tANDDOT) {
2427 yyerror1(&@2, "&. inside multiple assignment destination");
2428 }
2429 /*%%%*/
2430 $$ = attrset(p, $1, $2, $3, &@$);
2431 /*% %*/
2432 /*% ripper: field!($1, $2, $3) %*/
2433 }
2434 | primary_value tCOLON2 tCONSTANT
2435 {
2436 /*%%%*/
2437 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2438 /*% %*/
2439 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2440 }
2441 | tCOLON3 tCONSTANT
2442 {
2443 /*%%%*/
2444 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2445 /*% %*/
2446 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2447 }
2448 | backref
2449 {
2450 /*%%%*/
2451 rb_backref_error(p, $1);
2452 $$ = NEW_BEGIN(0, &@$);
2453 /*% %*/
2454 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2455 }
2456 ;
2457
2458lhs : user_variable
2459 {
2460 /*%%%*/
2461 $$ = assignable(p, $1, 0, &@$);
2462 /*% %*/
2463 /*% ripper: assignable(p, var_field(p, $1)) %*/
2464 }
2465 | keyword_variable
2466 {
2467 /*%%%*/
2468 $$ = assignable(p, $1, 0, &@$);
2469 /*% %*/
2470 /*% ripper: assignable(p, var_field(p, $1)) %*/
2471 }
2472 | primary_value '[' opt_call_args rbracket
2473 {
2474 /*%%%*/
2475 $$ = aryset(p, $1, $3, &@$);
2476 /*% %*/
2477 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2478 }
2479 | primary_value call_op tIDENTIFIER
2480 {
2481 /*%%%*/
2482 $$ = attrset(p, $1, $2, $3, &@$);
2483 /*% %*/
2484 /*% ripper: field!($1, $2, $3) %*/
2485 }
2486 | primary_value tCOLON2 tIDENTIFIER
2487 {
2488 /*%%%*/
2489 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2490 /*% %*/
2491 /*% ripper: field!($1, $2, $3) %*/
2492 }
2493 | primary_value call_op tCONSTANT
2494 {
2495 /*%%%*/
2496 $$ = attrset(p, $1, $2, $3, &@$);
2497 /*% %*/
2498 /*% ripper: field!($1, $2, $3) %*/
2499 }
2500 | primary_value tCOLON2 tCONSTANT
2501 {
2502 /*%%%*/
2503 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2504 /*% %*/
2505 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2506 }
2507 | tCOLON3 tCONSTANT
2508 {
2509 /*%%%*/
2510 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2511 /*% %*/
2512 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2513 }
2514 | backref
2515 {
2516 /*%%%*/
2517 rb_backref_error(p, $1);
2518 $$ = NEW_BEGIN(0, &@$);
2519 /*% %*/
2520 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2521 }
2522 ;
2523
2524cname : tIDENTIFIER
2525 {
2526 static const char mesg[] = "class/module name must be CONSTANT";
2527 /*%%%*/
2528 yyerror1(&@1, mesg);
2529 /*% %*/
2530 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2531 }
2532 | tCONSTANT
2533 ;
2534
2535cpath : tCOLON3 cname
2536 {
2537 /*%%%*/
2538 $$ = NEW_COLON3($2, &@$);
2539 /*% %*/
2540 /*% ripper: top_const_ref!($2) %*/
2541 }
2542 | cname
2543 {
2544 /*%%%*/
2545 $$ = NEW_COLON2(0, $$, &@$);
2546 /*% %*/
2547 /*% ripper: const_ref!($1) %*/
2548 }
2549 | primary_value tCOLON2 cname
2550 {
2551 /*%%%*/
2552 $$ = NEW_COLON2($1, $3, &@$);
2553 /*% %*/
2554 /*% ripper: const_path_ref!($1, $3) %*/
2555 }
2556 ;
2557
2558fname : tIDENTIFIER
2559 | tCONSTANT
2560 | tFID
2561 | op
2562 {
2563 SET_LEX_STATE(EXPR_ENDFN);
2564 $$ = $1;
2565 }
2566 | reswords
2567 ;
2568
2569fitem : fname
2570 {
2571 /*%%%*/
2572 $$ = NEW_LIT(ID2SYM($1), &@$);
2573 /*% %*/
2574 /*% ripper: symbol_literal!($1) %*/
2575 }
2576 | symbol
2577 ;
2578
2579undef_list : fitem
2580 {
2581 /*%%%*/
2582 $$ = NEW_UNDEF($1, &@$);
2583 /*% %*/
2584 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2585 }
2586 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2587 {
2588 /*%%%*/
2589 NODE *undef = NEW_UNDEF($4, &@4);
2590 $$ = block_append(p, $1, undef);
2591 /*% %*/
2592 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2593 }
2594 ;
2595
2596op : '|' { ifndef_ripper($$ = '|'); }
2597 | '^' { ifndef_ripper($$ = '^'); }
2598 | '&' { ifndef_ripper($$ = '&'); }
2599 | tCMP { ifndef_ripper($$ = tCMP); }
2600 | tEQ { ifndef_ripper($$ = tEQ); }
2601 | tEQQ { ifndef_ripper($$ = tEQQ); }
2602 | tMATCH { ifndef_ripper($$ = tMATCH); }
2603 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2604 | '>' { ifndef_ripper($$ = '>'); }
2605 | tGEQ { ifndef_ripper($$ = tGEQ); }
2606 | '<' { ifndef_ripper($$ = '<'); }
2607 | tLEQ { ifndef_ripper($$ = tLEQ); }
2608 | tNEQ { ifndef_ripper($$ = tNEQ); }
2609 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2610 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2611 | '+' { ifndef_ripper($$ = '+'); }
2612 | '-' { ifndef_ripper($$ = '-'); }
2613 | '*' { ifndef_ripper($$ = '*'); }
2614 | tSTAR { ifndef_ripper($$ = '*'); }
2615 | '/' { ifndef_ripper($$ = '/'); }
2616 | '%' { ifndef_ripper($$ = '%'); }
2617 | tPOW { ifndef_ripper($$ = tPOW); }
2618 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2619 | '!' { ifndef_ripper($$ = '!'); }
2620 | '~' { ifndef_ripper($$ = '~'); }
2621 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2622 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2623 | tAREF { ifndef_ripper($$ = tAREF); }
2624 | tASET { ifndef_ripper($$ = tASET); }
2625 | '`' { ifndef_ripper($$ = '`'); }
2626 ;
2627
2628reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2629 | keyword_BEGIN | keyword_END
2630 | keyword_alias | keyword_and | keyword_begin
2631 | keyword_break | keyword_case | keyword_class | keyword_def
2632 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2633 | keyword_end | keyword_ensure | keyword_false
2634 | keyword_for | keyword_in | keyword_module | keyword_next
2635 | keyword_nil | keyword_not | keyword_or | keyword_redo
2636 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2637 | keyword_super | keyword_then | keyword_true | keyword_undef
2638 | keyword_when | keyword_yield | keyword_if | keyword_unless
2639 | keyword_while | keyword_until
2640 ;
2641
2642arg : lhs '=' lex_ctxt arg_rhs
2643 {
2644 /*%%%*/
2645 $$ = node_assign(p, $1, $4, $3, &@$);
2646 /*% %*/
2647 /*% ripper: assign!($1, $4) %*/
2648 }
2649 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2650 {
2651 /*%%%*/
2652 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2653 /*% %*/
2654 /*% ripper: opassign!($1, $2, $4) %*/
2655 }
2656 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2657 {
2658 /*%%%*/
2659 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2660 /*% %*/
2661 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2662 }
2663 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2664 {
2665 /*%%%*/
2666 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2667 /*% %*/
2668 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2669 }
2670 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2671 {
2672 /*%%%*/
2673 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2674 /*% %*/
2675 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2676 }
2677 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2678 {
2679 /*%%%*/
2680 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2681 /*% %*/
2682 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2683 }
2684 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2685 {
2686 /*%%%*/
2687 YYLTYPE loc = code_loc_gen(&@1, &@3);
2688 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2689 /*% %*/
2690 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2691 }
2692 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2693 {
2694 /*%%%*/
2695 YYLTYPE loc = code_loc_gen(&@1, &@2);
2696 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2697 /*% %*/
2698 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2699 }
2700 | backref tOP_ASGN lex_ctxt arg_rhs
2701 {
2702 /*%%%*/
2703 rb_backref_error(p, $1);
2704 $$ = NEW_BEGIN(0, &@$);
2705 /*% %*/
2706 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2707 }
2708 | arg tDOT2 arg
2709 {
2710 /*%%%*/
2711 value_expr($1);
2712 value_expr($3);
2713 $$ = NEW_DOT2($1, $3, &@$);
2714 /*% %*/
2715 /*% ripper: dot2!($1, $3) %*/
2716 }
2717 | arg tDOT3 arg
2718 {
2719 /*%%%*/
2720 value_expr($1);
2721 value_expr($3);
2722 $$ = NEW_DOT3($1, $3, &@$);
2723 /*% %*/
2724 /*% ripper: dot3!($1, $3) %*/
2725 }
2726 | arg tDOT2
2727 {
2728 /*%%%*/
2729 value_expr($1);
2730 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2731 /*% %*/
2732 /*% ripper: dot2!($1, Qnil) %*/
2733 }
2734 | arg tDOT3
2735 {
2736 /*%%%*/
2737 value_expr($1);
2738 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2739 /*% %*/
2740 /*% ripper: dot3!($1, Qnil) %*/
2741 }
2742 | tBDOT2 arg
2743 {
2744 /*%%%*/
2745 value_expr($2);
2746 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2747 /*% %*/
2748 /*% ripper: dot2!(Qnil, $2) %*/
2749 }
2750 | tBDOT3 arg
2751 {
2752 /*%%%*/
2753 value_expr($2);
2754 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2755 /*% %*/
2756 /*% ripper: dot3!(Qnil, $2) %*/
2757 }
2758 | arg '+' arg
2759 {
2760 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2761 }
2762 | arg '-' arg
2763 {
2764 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2765 }
2766 | arg '*' arg
2767 {
2768 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2769 }
2770 | arg '/' arg
2771 {
2772 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2773 }
2774 | arg '%' arg
2775 {
2776 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2777 }
2778 | arg tPOW arg
2779 {
2780 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2781 }
2782 | tUMINUS_NUM simple_numeric tPOW arg
2783 {
2784 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2785 }
2786 | tUPLUS arg
2787 {
2788 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2789 }
2790 | tUMINUS arg
2791 {
2792 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2793 }
2794 | arg '|' arg
2795 {
2796 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2797 }
2798 | arg '^' arg
2799 {
2800 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2801 }
2802 | arg '&' arg
2803 {
2804 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2805 }
2806 | arg tCMP arg
2807 {
2808 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2809 }
2810 | rel_expr %prec tCMP
2811 | arg tEQ arg
2812 {
2813 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2814 }
2815 | arg tEQQ arg
2816 {
2817 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2818 }
2819 | arg tNEQ arg
2820 {
2821 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2822 }
2823 | arg tMATCH arg
2824 {
2825 $$ = match_op(p, $1, $3, &@2, &@$);
2826 }
2827 | arg tNMATCH arg
2828 {
2829 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2830 }
2831 | '!' arg
2832 {
2833 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2834 }
2835 | '~' arg
2836 {
2837 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2838 }
2839 | arg tLSHFT arg
2840 {
2841 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2842 }
2843 | arg tRSHFT arg
2844 {
2845 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2846 }
2847 | arg tANDOP arg
2848 {
2849 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2850 }
2851 | arg tOROP arg
2852 {
2853 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2854 }
2855 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2856 {
2857 p->ctxt.in_defined = 0;
2858 $$ = new_defined(p, $4, &@$);
2859 }
2860 | arg '?' arg opt_nl ':' arg
2861 {
2862 /*%%%*/
2863 value_expr($1);
2864 $$ = new_if(p, $1, $3, $6, &@$);
2865 fixpos($$, $1);
2866 /*% %*/
2867 /*% ripper: ifop!($1, $3, $6) %*/
2868 }
2869 | defn_head f_opt_paren_args '=' arg
2870 {
2871 endless_method_name(p, $<node>1, &@1);
2872 restore_defun(p, $<node>1->nd_defn);
2873 /*%%%*/
2874 $$ = set_defun_body(p, $1, $2, $4, &@$);
2875 /*% %*/
2876 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2877 /*% ripper: def!(get_value($1), $2, $4) %*/
2878 local_pop(p);
2879 }
2880 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2881 {
2882 endless_method_name(p, $<node>1, &@1);
2883 restore_defun(p, $<node>1->nd_defn);
2884 /*%%%*/
2885 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2886 $$ = set_defun_body(p, $1, $2, $4, &@$);
2887 /*% %*/
2888 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2889 /*% ripper: def!(get_value($1), $2, $4) %*/
2890 local_pop(p);
2891 }
2892 | defs_head f_opt_paren_args '=' arg
2893 {
2894 endless_method_name(p, $<node>1, &@1);
2895 restore_defun(p, $<node>1->nd_defn);
2896 /*%%%*/
2897 $$ = set_defun_body(p, $1, $2, $4, &@$);
2898 /*%
2899 $1 = get_value($1);
2900 %*/
2901 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2902 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2903 local_pop(p);
2904 }
2905 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2906 {
2907 endless_method_name(p, $<node>1, &@1);
2908 restore_defun(p, $<node>1->nd_defn);
2909 /*%%%*/
2910 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2911 $$ = set_defun_body(p, $1, $2, $4, &@$);
2912 /*%
2913 $1 = get_value($1);
2914 %*/
2915 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2916 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2917 local_pop(p);
2918 }
2919 | primary
2920 {
2921 $$ = $1;
2922 }
2923 ;
2924
2925relop : '>' {$$ = '>';}
2926 | '<' {$$ = '<';}
2927 | tGEQ {$$ = idGE;}
2928 | tLEQ {$$ = idLE;}
2929 ;
2930
2931rel_expr : arg relop arg %prec '>'
2932 {
2933 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2934 }
2935 | rel_expr relop arg %prec '>'
2936 {
2937 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2938 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2939 }
2940 ;
2941
2942lex_ctxt : none
2943 {
2944 $$ = p->ctxt;
2945 }
2946 ;
2947
2948arg_value : arg
2949 {
2950 value_expr($1);
2951 $$ = $1;
2952 }
2953 ;
2954
2955aref_args : none
2956 | args trailer
2957 {
2958 $$ = $1;
2959 }
2960 | args ',' assocs trailer
2961 {
2962 /*%%%*/
2963 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2964 /*% %*/
2965 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2966 }
2967 | assocs trailer
2968 {
2969 /*%%%*/
2970 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2971 /*% %*/
2972 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2973 }
2974 ;
2975
2976arg_rhs : arg %prec tOP_ASGN
2977 {
2978 value_expr($1);
2979 $$ = $1;
2980 }
2981 | arg modifier_rescue arg
2982 {
2983 /*%%%*/
2984 value_expr($1);
2985 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2986 /*% %*/
2987 /*% ripper: rescue_mod!($1, $3) %*/
2988 }
2989 ;
2990
2991paren_args : '(' opt_call_args rparen
2992 {
2993 /*%%%*/
2994 $$ = $2;
2995 /*% %*/
2996 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2997 }
2998 | '(' args ',' args_forward rparen
2999 {
3000 if (!check_forwarding_args(p)) {
3001 $$ = Qnone;
3002 }
3003 else {
3004 /*%%%*/
3005 $$ = new_args_forward_call(p, $2, &@4, &@$);
3006 /*% %*/
3007 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
3008 }
3009 }
3010 | '(' args_forward rparen
3011 {
3012 if (!check_forwarding_args(p)) {
3013 $$ = Qnone;
3014 }
3015 else {
3016 /*%%%*/
3017 $$ = new_args_forward_call(p, 0, &@2, &@$);
3018 /*% %*/
3019 /*% ripper: arg_paren!($2) %*/
3020 }
3021 }
3022 ;
3023
3024opt_paren_args : none
3025 | paren_args
3026 ;
3027
3028opt_call_args : none
3029 | call_args
3030 | args ','
3031 {
3032 $$ = $1;
3033 }
3034 | args ',' assocs ','
3035 {
3036 /*%%%*/
3037 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3038 /*% %*/
3039 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3040 }
3041 | assocs ','
3042 {
3043 /*%%%*/
3044 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3045 /*% %*/
3046 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3047 }
3048 ;
3049
3050call_args : command
3051 {
3052 /*%%%*/
3053 value_expr($1);
3054 $$ = NEW_LIST($1, &@$);
3055 /*% %*/
3056 /*% ripper: args_add!(args_new!, $1) %*/
3057 }
3058 | args opt_block_arg
3059 {
3060 /*%%%*/
3061 $$ = arg_blk_pass($1, $2);
3062 /*% %*/
3063 /*% ripper: args_add_block!($1, $2) %*/
3064 }
3065 | assocs opt_block_arg
3066 {
3067 /*%%%*/
3068 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3069 $$ = arg_blk_pass($$, $2);
3070 /*% %*/
3071 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3072 }
3073 | args ',' assocs opt_block_arg
3074 {
3075 /*%%%*/
3076 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3077 $$ = arg_blk_pass($$, $4);
3078 /*% %*/
3079 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3080 }
3081 | block_arg
3082 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3083 ;
3084
3085command_args : {
3086 /* If call_args starts with a open paren '(' or '[',
3087 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3088 * but the push must be done after CMDARG_PUSH(1).
3089 * So this code makes them consistent by first cancelling
3090 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3091 * and finally redoing CMDARG_PUSH(0).
3092 */
3093 int lookahead = 0;
3094 switch (yychar) {
3095 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3096 lookahead = 1;
3097 }
3098 if (lookahead) CMDARG_POP();
3099 CMDARG_PUSH(1);
3100 if (lookahead) CMDARG_PUSH(0);
3101 }
3102 call_args
3103 {
3104 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3105 * but the push must be done after CMDARG_POP() in the parser.
3106 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3107 * CMDARG_POP() to pop 1 pushed by command_args,
3108 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3109 */
3110 int lookahead = 0;
3111 switch (yychar) {
3112 case tLBRACE_ARG:
3113 lookahead = 1;
3114 }
3115 if (lookahead) CMDARG_POP();
3116 CMDARG_POP();
3117 if (lookahead) CMDARG_PUSH(0);
3118 $$ = $2;
3119 }
3120 ;
3121
3122block_arg : tAMPER arg_value
3123 {
3124 /*%%%*/
3125 $$ = NEW_BLOCK_PASS($2, &@$);
3126 /*% %*/
3127 /*% ripper: $2 %*/
3128 }
3129 | tAMPER
3130 {
3131 if (!local_id(p, idFWD_BLOCK)) {
3132 compile_error(p, "no anonymous block parameter");
3133 }
3134 /*%%%*/
3135 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3136 /*% %*/
3137 /*% ripper: Qnil %*/
3138 }
3139 ;
3140
3141opt_block_arg : ',' block_arg
3142 {
3143 $$ = $2;
3144 }
3145 | none
3146 {
3147 $$ = 0;
3148 }
3149 ;
3150
3151/* value */
3152args : arg_value
3153 {
3154 /*%%%*/
3155 $$ = NEW_LIST($1, &@$);
3156 /*% %*/
3157 /*% ripper: args_add!(args_new!, $1) %*/
3158 }
3159 | tSTAR arg_value
3160 {
3161 /*%%%*/
3162 $$ = NEW_SPLAT($2, &@$);
3163 /*% %*/
3164 /*% ripper: args_add_star!(args_new!, $2) %*/
3165 }
3166 | tSTAR
3167 {
3168 if (!local_id(p, idFWD_REST) ||
3169 local_id(p, idFWD_ALL)) {
3170 compile_error(p, "no anonymous rest parameter");
3171 }
3172 /*%%%*/
3173 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@1), &@$);
3174 /*% %*/
3175 /*% ripper: args_add_star!(args_new!, Qnil) %*/
3176 }
3177 | args ',' arg_value
3178 {
3179 /*%%%*/
3180 $$ = last_arg_append(p, $1, $3, &@$);
3181 /*% %*/
3182 /*% ripper: args_add!($1, $3) %*/
3183 }
3184 | args ',' tSTAR arg_value
3185 {
3186 /*%%%*/
3187 $$ = rest_arg_append(p, $1, $4, &@$);
3188 /*% %*/
3189 /*% ripper: args_add_star!($1, $4) %*/
3190 }
3191 | args ',' tSTAR
3192 {
3193 if (!local_id(p, idFWD_REST) ||
3194 local_id(p, idFWD_ALL)) {
3195 compile_error(p, "no anonymous rest parameter");
3196 }
3197 /*%%%*/
3198 $$ = rest_arg_append(p, $1, NEW_LVAR(idFWD_REST, &@3), &@$);
3199 /*% %*/
3200 /*% ripper: args_add_star!($1, Qnil) %*/
3201 }
3202 ;
3203
3204/* value */
3205mrhs_arg : mrhs
3206 | arg_value
3207 ;
3208
3209/* value */
3210mrhs : args ',' arg_value
3211 {
3212 /*%%%*/
3213 $$ = last_arg_append(p, $1, $3, &@$);
3214 /*% %*/
3215 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3216 }
3217 | args ',' tSTAR arg_value
3218 {
3219 /*%%%*/
3220 $$ = rest_arg_append(p, $1, $4, &@$);
3221 /*% %*/
3222 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3223 }
3224 | tSTAR arg_value
3225 {
3226 /*%%%*/
3227 $$ = NEW_SPLAT($2, &@$);
3228 /*% %*/
3229 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3230 }
3231 ;
3232
3233primary : literal
3234 | strings
3235 | xstring
3236 | regexp
3237 | words
3238 | qwords
3239 | symbols
3240 | qsymbols
3241 | var_ref
3242 | backref
3243 | tFID
3244 {
3245 /*%%%*/
3246 $$ = NEW_FCALL($1, 0, &@$);
3247 /*% %*/
3248 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3249 }
3250 | k_begin
3251 {
3252 CMDARG_PUSH(0);
3253 }
3254 bodystmt
3255 k_end
3256 {
3257 CMDARG_POP();
3258 /*%%%*/
3259 set_line_body($3, @1.end_pos.lineno);
3260 $$ = NEW_BEGIN($3, &@$);
3261 nd_set_line($$, @1.end_pos.lineno);
3262 /*% %*/
3263 /*% ripper: begin!($3) %*/
3264 }
3265 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
3266 {
3267 /*%%%*/
3268 $$ = NEW_BEGIN(0, &@$);
3269 /*% %*/
3270 /*% ripper: paren!(0) %*/
3271 }
3272 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
3273 {
3274 /*%%%*/
3275 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3276 $$ = $2;
3277 /*% %*/
3278 /*% ripper: paren!($2) %*/
3279 }
3280 | tLPAREN compstmt ')'
3281 {
3282 /*%%%*/
3283 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3284 $$ = $2;
3285 /*% %*/
3286 /*% ripper: paren!($2) %*/
3287 }
3288 | primary_value tCOLON2 tCONSTANT
3289 {
3290 /*%%%*/
3291 $$ = NEW_COLON2($1, $3, &@$);
3292 /*% %*/
3293 /*% ripper: const_path_ref!($1, $3) %*/
3294 }
3295 | tCOLON3 tCONSTANT
3296 {
3297 /*%%%*/
3298 $$ = NEW_COLON3($2, &@$);
3299 /*% %*/
3300 /*% ripper: top_const_ref!($2) %*/
3301 }
3302 | tLBRACK aref_args ']'
3303 {
3304 /*%%%*/
3305 $$ = make_list($2, &@$);
3306 /*% %*/
3307 /*% ripper: array!(escape_Qundef($2)) %*/
3308 }
3309 | tLBRACE assoc_list '}'
3310 {
3311 /*%%%*/
3312 $$ = new_hash(p, $2, &@$);
3313 $$->nd_brace = TRUE;
3314 /*% %*/
3315 /*% ripper: hash!(escape_Qundef($2)) %*/
3316 }
3317 | k_return
3318 {
3319 /*%%%*/
3320 $$ = NEW_RETURN(0, &@$);
3321 /*% %*/
3322 /*% ripper: return0! %*/
3323 }
3324 | keyword_yield '(' call_args rparen
3325 {
3326 /*%%%*/
3327 $$ = new_yield(p, $3, &@$);
3328 /*% %*/
3329 /*% ripper: yield!(paren!($3)) %*/
3330 }
3331 | keyword_yield '(' rparen
3332 {
3333 /*%%%*/
3334 $$ = NEW_YIELD(0, &@$);
3335 /*% %*/
3336 /*% ripper: yield!(paren!(args_new!)) %*/
3337 }
3338 | keyword_yield
3339 {
3340 /*%%%*/
3341 $$ = NEW_YIELD(0, &@$);
3342 /*% %*/
3343 /*% ripper: yield0! %*/
3344 }
3345 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3346 {
3347 p->ctxt.in_defined = 0;
3348 $$ = new_defined(p, $5, &@$);
3349 }
3350 | keyword_not '(' expr rparen
3351 {
3352 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3353 }
3354 | keyword_not '(' rparen
3355 {
3356 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3357 }
3358 | fcall brace_block
3359 {
3360 /*%%%*/
3361 $$ = method_add_block(p, $1, $2, &@$);
3362 /*% %*/
3363 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3364 }
3365 | method_call
3366 | method_call brace_block
3367 {
3368 /*%%%*/
3369 block_dup_check(p, $1->nd_args, $2);
3370 $$ = method_add_block(p, $1, $2, &@$);
3371 /*% %*/
3372 /*% ripper: method_add_block!($1, $2) %*/
3373 }
3374 | lambda
3375 | k_if expr_value then
3376 compstmt
3377 if_tail
3378 k_end
3379 {
3380 /*%%%*/
3381 $$ = new_if(p, $2, $4, $5, &@$);
3382 fixpos($$, $2);
3383 /*% %*/
3384 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3385 }
3386 | k_unless expr_value then
3387 compstmt
3388 opt_else
3389 k_end
3390 {
3391 /*%%%*/
3392 $$ = new_unless(p, $2, $4, $5, &@$);
3393 fixpos($$, $2);
3394 /*% %*/
3395 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3396 }
3397 | k_while expr_value_do
3398 compstmt
3399 k_end
3400 {
3401 /*%%%*/
3402 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3403 fixpos($$, $2);
3404 /*% %*/
3405 /*% ripper: while!($2, $3) %*/
3406 }
3407 | k_until expr_value_do
3408 compstmt
3409 k_end
3410 {
3411 /*%%%*/
3412 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3413 fixpos($$, $2);
3414 /*% %*/
3415 /*% ripper: until!($2, $3) %*/
3416 }
3417 | k_case expr_value opt_terms
3418 {
3419 $<val>$ = p->case_labels;
3420 p->case_labels = Qnil;
3421 }
3422 case_body
3423 k_end
3424 {
3425 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3426 p->case_labels = $<val>4;
3427 /*%%%*/
3428 $$ = NEW_CASE($2, $5, &@$);
3429 fixpos($$, $2);
3430 /*% %*/
3431 /*% ripper: case!($2, $5) %*/
3432 }
3433 | k_case opt_terms
3434 {
3435 $<val>$ = p->case_labels;
3436 p->case_labels = 0;
3437 }
3438 case_body
3439 k_end
3440 {
3441 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3442 p->case_labels = $<val>3;
3443 /*%%%*/
3444 $$ = NEW_CASE2($4, &@$);
3445 /*% %*/
3446 /*% ripper: case!(Qnil, $4) %*/
3447 }
3448 | k_case expr_value opt_terms
3449 p_case_body
3450 k_end
3451 {
3452 /*%%%*/
3453 $$ = NEW_CASE3($2, $4, &@$);
3454 /*% %*/
3455 /*% ripper: case!($2, $4) %*/
3456 }
3457 | k_for for_var keyword_in expr_value_do
3458 compstmt
3459 k_end
3460 {
3461 /*%%%*/
3462 /*
3463 * for a, b, c in e
3464 * #=>
3465 * e.each{|*x| a, b, c = x}
3466 *
3467 * for a in e
3468 * #=>
3469 * e.each{|x| a, = x}
3470 */
3471 ID id = internal_id(p);
3472 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3473 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3474 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3475 tbl->ids[0] = id; /* internal id */
3476
3477 switch (nd_type($2)) {
3478 case NODE_LASGN:
3479 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3480 $2->nd_value = internal_var;
3481 id = 0;
3482 m->nd_plen = 1;
3483 m->nd_next = $2;
3484 break;
3485 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3486 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3487 break;
3488 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3489 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3490 }
3491 /* {|*internal_id| <m> = internal_id; ... } */
3492 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3493 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3494 $$ = NEW_FOR($4, scope, &@$);
3495 fixpos($$, $2);
3496 /*% %*/
3497 /*% ripper: for!($2, $4, $5) %*/
3498 }
3499 | k_class cpath superclass
3500 {
3501 if (p->ctxt.in_def) {
3502 YYLTYPE loc = code_loc_gen(&@1, &@2);
3503 yyerror1(&loc, "class definition in method body");
3504 }
3505 p->ctxt.in_class = 1;
3506 local_push(p, 0);
3507 }
3508 bodystmt
3509 k_end
3510 {
3511 /*%%%*/
3512 $$ = NEW_CLASS($2, $5, $3, &@$);
3513 nd_set_line($$->nd_body, @6.end_pos.lineno);
3514 set_line_body($5, @3.end_pos.lineno);
3515 nd_set_line($$, @3.end_pos.lineno);
3516 /*% %*/
3517 /*% ripper: class!($2, $3, $5) %*/
3518 local_pop(p);
3519 p->ctxt.in_class = $<ctxt>1.in_class;
3520 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3521 }
3522 | k_class tLSHFT expr
3523 {
3524 p->ctxt.in_def = 0;
3525 p->ctxt.in_class = 0;
3526 local_push(p, 0);
3527 }
3528 term
3529 bodystmt
3530 k_end
3531 {
3532 /*%%%*/
3533 $$ = NEW_SCLASS($3, $6, &@$);
3534 nd_set_line($$->nd_body, @7.end_pos.lineno);
3535 set_line_body($6, nd_line($3));
3536 fixpos($$, $3);
3537 /*% %*/
3538 /*% ripper: sclass!($3, $6) %*/
3539 local_pop(p);
3540 p->ctxt.in_def = $<ctxt>1.in_def;
3541 p->ctxt.in_class = $<ctxt>1.in_class;
3542 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3543 }
3544 | k_module cpath
3545 {
3546 if (p->ctxt.in_def) {
3547 YYLTYPE loc = code_loc_gen(&@1, &@2);
3548 yyerror1(&loc, "module definition in method body");
3549 }
3550 p->ctxt.in_class = 1;
3551 local_push(p, 0);
3552 }
3553 bodystmt
3554 k_end
3555 {
3556 /*%%%*/
3557 $$ = NEW_MODULE($2, $4, &@$);
3558 nd_set_line($$->nd_body, @5.end_pos.lineno);
3559 set_line_body($4, @2.end_pos.lineno);
3560 nd_set_line($$, @2.end_pos.lineno);
3561 /*% %*/
3562 /*% ripper: module!($2, $4) %*/
3563 local_pop(p);
3564 p->ctxt.in_class = $<ctxt>1.in_class;
3565 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3566 }
3567 | defn_head
3568 f_arglist
3569 {
3570 /*%%%*/
3571 push_end_expect_token_locations(p, &@1.beg_pos);
3572 /*% %*/
3573 }
3574 bodystmt
3575 k_end
3576 {
3577 restore_defun(p, $<node>1->nd_defn);
3578 /*%%%*/
3579 $$ = set_defun_body(p, $1, $2, $4, &@$);
3580 /*% %*/
3581 /*% ripper: def!(get_value($1), $2, $4) %*/
3582 local_pop(p);
3583 }
3584 | defs_head
3585 f_arglist
3586 {
3587 /*%%%*/
3588 push_end_expect_token_locations(p, &@1.beg_pos);
3589 /*% %*/
3590 }
3591 bodystmt
3592 k_end
3593 {
3594 restore_defun(p, $<node>1->nd_defn);
3595 /*%%%*/
3596 $$ = set_defun_body(p, $1, $2, $4, &@$);
3597 /*%
3598 $1 = get_value($1);
3599 %*/
3600 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
3601 local_pop(p);
3602 }
3603 | keyword_break
3604 {
3605 /*%%%*/
3606 $$ = NEW_BREAK(0, &@$);
3607 /*% %*/
3608 /*% ripper: break!(args_new!) %*/
3609 }
3610 | keyword_next
3611 {
3612 /*%%%*/
3613 $$ = NEW_NEXT(0, &@$);
3614 /*% %*/
3615 /*% ripper: next!(args_new!) %*/
3616 }
3617 | keyword_redo
3618 {
3619 /*%%%*/
3620 $$ = NEW_REDO(&@$);
3621 /*% %*/
3622 /*% ripper: redo! %*/
3623 }
3624 | keyword_retry
3625 {
3626 /*%%%*/
3627 $$ = NEW_RETRY(&@$);
3628 /*% %*/
3629 /*% ripper: retry! %*/
3630 }
3631 ;
3632
3633primary_value : primary
3634 {
3635 value_expr($1);
3636 $$ = $1;
3637 }
3638 ;
3639
3640k_begin : keyword_begin
3641 {
3642 token_info_push(p, "begin", &@$);
3643 /*%%%*/
3644 push_end_expect_token_locations(p, &@1.beg_pos);
3645 /*% %*/
3646 }
3647 ;
3648
3649k_if : keyword_if
3650 {
3651 WARN_EOL("if");
3652 token_info_push(p, "if", &@$);
3653 if (p->token_info && p->token_info->nonspc &&
3654 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3655 const char *tok = p->lex.ptok - rb_strlen_lit("if");
3656 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3657 beg += rb_strlen_lit("else");
3658 while (beg < tok && ISSPACE(*beg)) beg++;
3659 if (beg == tok) {
3660 p->token_info->nonspc = 0;
3661 }
3662 }
3663 /*%%%*/
3664 push_end_expect_token_locations(p, &@1.beg_pos);
3665 /*% %*/
3666 }
3667 ;
3668
3669k_unless : keyword_unless
3670 {
3671 token_info_push(p, "unless", &@$);
3672 /*%%%*/
3673 push_end_expect_token_locations(p, &@1.beg_pos);
3674 /*% %*/
3675 }
3676 ;
3677
3678k_while : keyword_while
3679 {
3680 token_info_push(p, "while", &@$);
3681 /*%%%*/
3682 push_end_expect_token_locations(p, &@1.beg_pos);
3683 /*% %*/
3684 }
3685 ;
3686
3687k_until : keyword_until
3688 {
3689 token_info_push(p, "until", &@$);
3690 /*%%%*/
3691 push_end_expect_token_locations(p, &@1.beg_pos);
3692 /*% %*/
3693 }
3694 ;
3695
3696k_case : keyword_case
3697 {
3698 token_info_push(p, "case", &@$);
3699 /*%%%*/
3700 push_end_expect_token_locations(p, &@1.beg_pos);
3701 /*% %*/
3702 }
3703 ;
3704
3705k_for : keyword_for
3706 {
3707 token_info_push(p, "for", &@$);
3708 /*%%%*/
3709 push_end_expect_token_locations(p, &@1.beg_pos);
3710 /*% %*/
3711 }
3712 ;
3713
3714k_class : keyword_class
3715 {
3716 token_info_push(p, "class", &@$);
3717 $<ctxt>$ = p->ctxt;
3718 /*%%%*/
3719 push_end_expect_token_locations(p, &@1.beg_pos);
3720 /*% %*/
3721 }
3722 ;
3723
3724k_module : keyword_module
3725 {
3726 token_info_push(p, "module", &@$);
3727 $<ctxt>$ = p->ctxt;
3728 /*%%%*/
3729 push_end_expect_token_locations(p, &@1.beg_pos);
3730 /*% %*/
3731 }
3732 ;
3733
3734k_def : keyword_def
3735 {
3736 token_info_push(p, "def", &@$);
3737 p->ctxt.in_argdef = 1;
3738 }
3739 ;
3740
3741k_do : keyword_do
3742 {
3743 token_info_push(p, "do", &@$);
3744 /*%%%*/
3745 push_end_expect_token_locations(p, &@1.beg_pos);
3746 /*% %*/
3747
3748 }
3749 ;
3750
3751k_do_block : keyword_do_block
3752 {
3753 token_info_push(p, "do", &@$);
3754 /*%%%*/
3755 push_end_expect_token_locations(p, &@1.beg_pos);
3756 /*% %*/
3757 }
3758 ;
3759
3760k_rescue : keyword_rescue
3761 {
3762 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3763 }
3764 ;
3765
3766k_ensure : keyword_ensure
3767 {
3768 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3769 }
3770 ;
3771
3772k_when : keyword_when
3773 {
3774 token_info_warn(p, "when", p->token_info, 0, &@$);
3775 }
3776 ;
3777
3778k_else : keyword_else
3779 {
3780 token_info *ptinfo_beg = p->token_info;
3781 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3782 token_info_warn(p, "else", p->token_info, same, &@$);
3783 if (same) {
3784 token_info e;
3785 e.next = ptinfo_beg->next;
3786 e.token = "else";
3787 token_info_setup(&e, p->lex.pbeg, &@$);
3788 if (!e.nonspc) *ptinfo_beg = e;
3789 }
3790 }
3791 ;
3792
3793k_elsif : keyword_elsif
3794 {
3795 WARN_EOL("elsif");
3796 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3797 }
3798 ;
3799
3800k_end : keyword_end
3801 {
3802 token_info_pop(p, "end", &@$);
3803 /*%%%*/
3804 pop_end_expect_token_locations(p);
3805 /*% %*/
3806 }
3807 | tDUMNY_END
3808 {
3809 compile_error(p, "syntax error, unexpected end-of-input");
3810 }
3811 ;
3812
3813k_return : keyword_return
3814 {
3815 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3816 yyerror1(&@1, "Invalid return in class/module body");
3817 }
3818 ;
3819
3820then : term
3821 | keyword_then
3822 | term keyword_then
3823 ;
3824
3825do : term
3826 | keyword_do_cond
3827 ;
3828
3829if_tail : opt_else
3830 | k_elsif expr_value then
3831 compstmt
3832 if_tail
3833 {
3834 /*%%%*/
3835 $$ = new_if(p, $2, $4, $5, &@$);
3836 fixpos($$, $2);
3837 /*% %*/
3838 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3839 }
3840 ;
3841
3842opt_else : none
3843 | k_else compstmt
3844 {
3845 /*%%%*/
3846 $$ = $2;
3847 /*% %*/
3848 /*% ripper: else!($2) %*/
3849 }
3850 ;
3851
3852for_var : lhs
3853 | mlhs
3854 ;
3855
3856f_marg : f_norm_arg
3857 {
3858 /*%%%*/
3859 $$ = assignable(p, $1, 0, &@$);
3860 mark_lvar_used(p, $$);
3861 /*% %*/
3862 /*% ripper: assignable(p, $1) %*/
3863 }
3864 | tLPAREN f_margs rparen
3865 {
3866 /*%%%*/
3867 $$ = $2;
3868 /*% %*/
3869 /*% ripper: mlhs_paren!($2) %*/
3870 }
3871 ;
3872
3873f_marg_list : f_marg
3874 {
3875 /*%%%*/
3876 $$ = NEW_LIST($1, &@$);
3877 /*% %*/
3878 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3879 }
3880 | f_marg_list ',' f_marg
3881 {
3882 /*%%%*/
3883 $$ = list_append(p, $1, $3);
3884 /*% %*/
3885 /*% ripper: mlhs_add!($1, $3) %*/
3886 }
3887 ;
3888
3889f_margs : f_marg_list
3890 {
3891 /*%%%*/
3892 $$ = NEW_MASGN($1, 0, &@$);
3893 /*% %*/
3894 /*% ripper: $1 %*/
3895 }
3896 | f_marg_list ',' f_rest_marg
3897 {
3898 /*%%%*/
3899 $$ = NEW_MASGN($1, $3, &@$);
3900 /*% %*/
3901 /*% ripper: mlhs_add_star!($1, $3) %*/
3902 }
3903 | f_marg_list ',' f_rest_marg ',' f_marg_list
3904 {
3905 /*%%%*/
3906 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3907 /*% %*/
3908 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3909 }
3910 | f_rest_marg
3911 {
3912 /*%%%*/
3913 $$ = NEW_MASGN(0, $1, &@$);
3914 /*% %*/
3915 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3916 }
3917 | f_rest_marg ',' f_marg_list
3918 {
3919 /*%%%*/
3920 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3921 /*% %*/
3922 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3923 }
3924 ;
3925
3926f_rest_marg : tSTAR f_norm_arg
3927 {
3928 /*%%%*/
3929 $$ = assignable(p, $2, 0, &@$);
3930 mark_lvar_used(p, $$);
3931 /*% %*/
3932 /*% ripper: assignable(p, $2) %*/
3933 }
3934 | tSTAR
3935 {
3936 /*%%%*/
3937 $$ = NODE_SPECIAL_NO_NAME_REST;
3938 /*% %*/
3939 /*% ripper: Qnil %*/
3940 }
3941 ;
3942
3943f_any_kwrest : f_kwrest
3944 | f_no_kwarg {$$ = ID2VAL(idNil);}
3945 ;
3946
3947f_eq : {p->ctxt.in_argdef = 0;} '=';
3948
3949block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3950 {
3951 $$ = new_args_tail(p, $1, $3, $4, &@3);
3952 }
3953 | f_block_kwarg opt_f_block_arg
3954 {
3955 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3956 }
3957 | f_any_kwrest opt_f_block_arg
3958 {
3959 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3960 }
3961 | f_block_arg
3962 {
3963 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3964 }
3965 ;
3966
3967opt_block_args_tail : ',' block_args_tail
3968 {
3969 $$ = $2;
3970 }
3971 | /* none */
3972 {
3973 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3974 }
3975 ;
3976
3977excessed_comma : ','
3978 {
3979 /* magic number for rest_id in iseq_set_arguments() */
3980 /*%%%*/
3981 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3982 /*% %*/
3983 /*% ripper: excessed_comma! %*/
3984 }
3985 ;
3986
3987block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3988 {
3989 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3990 }
3991 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3992 {
3993 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3994 }
3995 | f_arg ',' f_block_optarg opt_block_args_tail
3996 {
3997 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3998 }
3999 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
4000 {
4001 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4002 }
4003 | f_arg ',' f_rest_arg opt_block_args_tail
4004 {
4005 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4006 }
4007 | f_arg excessed_comma
4008 {
4009 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
4010 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
4011 }
4012 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4013 {
4014 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4015 }
4016 | f_arg opt_block_args_tail
4017 {
4018 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4019 }
4020 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4021 {
4022 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4023 }
4024 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4025 {
4026 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4027 }
4028 | f_block_optarg opt_block_args_tail
4029 {
4030 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4031 }
4032 | f_block_optarg ',' f_arg opt_block_args_tail
4033 {
4034 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4035 }
4036 | f_rest_arg opt_block_args_tail
4037 {
4038 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4039 }
4040 | f_rest_arg ',' f_arg opt_block_args_tail
4041 {
4042 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4043 }
4044 | block_args_tail
4045 {
4046 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4047 }
4048 ;
4049
4050opt_block_param : none
4051 | block_param_def
4052 {
4053 p->command_start = TRUE;
4054 }
4055 ;
4056
4057block_param_def : '|' opt_bv_decl '|'
4058 {
4059 p->cur_arg = 0;
4060 p->max_numparam = ORDINAL_PARAM;
4061 p->ctxt.in_argdef = 0;
4062 /*%%%*/
4063 $$ = 0;
4064 /*% %*/
4065 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
4066 }
4067 | '|' block_param opt_bv_decl '|'
4068 {
4069 p->cur_arg = 0;
4070 p->max_numparam = ORDINAL_PARAM;
4071 p->ctxt.in_argdef = 0;
4072 /*%%%*/
4073 $$ = $2;
4074 /*% %*/
4075 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
4076 }
4077 ;
4078
4079
4080opt_bv_decl : opt_nl
4081 {
4082 $$ = 0;
4083 }
4084 | opt_nl ';' bv_decls opt_nl
4085 {
4086 /*%%%*/
4087 $$ = 0;
4088 /*% %*/
4089 /*% ripper: $3 %*/
4090 }
4091 ;
4092
4093bv_decls : bvar
4094 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4095 | bv_decls ',' bvar
4096 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4097 ;
4098
4099bvar : tIDENTIFIER
4100 {
4101 new_bv(p, get_id($1));
4102 /*% ripper: get_value($1) %*/
4103 }
4104 | f_bad_arg
4105 {
4106 $$ = 0;
4107 }
4108 ;
4109
4110lambda : tLAMBDA
4111 {
4112 token_info_push(p, "->", &@1);
4113 $<vars>1 = dyna_push(p);
4114 $<num>$ = p->lex.lpar_beg;
4115 p->lex.lpar_beg = p->lex.paren_nest;
4116 }
4117 {
4118 $<num>$ = p->max_numparam;
4119 p->max_numparam = 0;
4120 }
4121 {
4122 $<node>$ = numparam_push(p);
4123 }
4124 f_larglist
4125 {
4126 CMDARG_PUSH(0);
4127 }
4128 lambda_body
4129 {
4130 int max_numparam = p->max_numparam;
4131 p->lex.lpar_beg = $<num>2;
4132 p->max_numparam = $<num>3;
4133 CMDARG_POP();
4134 $5 = args_with_numbered(p, $5, max_numparam);
4135 /*%%%*/
4136 {
4137 YYLTYPE loc = code_loc_gen(&@5, &@7);
4138 $$ = NEW_LAMBDA($5, $7, &loc);
4139 nd_set_line($$->nd_body, @7.end_pos.lineno);
4140 nd_set_line($$, @5.end_pos.lineno);
4141 nd_set_first_loc($$, @1.beg_pos);
4142 }
4143 /*% %*/
4144 /*% ripper: lambda!($5, $7) %*/
4145 numparam_pop(p, $<node>4);
4146 dyna_pop(p, $<vars>1);
4147 }
4148 ;
4149
4150f_larglist : '(' f_args opt_bv_decl ')'
4151 {
4152 p->ctxt.in_argdef = 0;
4153 /*%%%*/
4154 $$ = $2;
4155 p->max_numparam = ORDINAL_PARAM;
4156 /*% %*/
4157 /*% ripper: paren!($2) %*/
4158 }
4159 | f_args
4160 {
4161 p->ctxt.in_argdef = 0;
4162 /*%%%*/
4163 if (!args_info_empty_p($1->nd_ainfo))
4164 p->max_numparam = ORDINAL_PARAM;
4165 /*% %*/
4166 $$ = $1;
4167 }
4168 ;
4169
4170lambda_body : tLAMBEG compstmt '}'
4171 {
4172 token_info_pop(p, "}", &@3);
4173 $$ = $2;
4174 }
4175 | keyword_do_LAMBDA
4176 {
4177 /*%%%*/
4178 push_end_expect_token_locations(p, &@1.beg_pos);
4179 /*% %*/
4180 }
4181 bodystmt k_end
4182 {
4183 $$ = $3;
4184 }
4185 ;
4186
4187do_block : k_do_block do_body k_end
4188 {
4189 $$ = $2;
4190 /*%%%*/
4191 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4192 nd_set_line($$, @1.end_pos.lineno);
4193 /*% %*/
4194 }
4195 ;
4196
4197block_call : command do_block
4198 {
4199 /*%%%*/
4200 if (nd_type_p($1, NODE_YIELD)) {
4201 compile_error(p, "block given to yield");
4202 }
4203 else {
4204 block_dup_check(p, $1->nd_args, $2);
4205 }
4206 $$ = method_add_block(p, $1, $2, &@$);
4207 fixpos($$, $1);
4208 /*% %*/
4209 /*% ripper: method_add_block!($1, $2) %*/
4210 }
4211 | block_call call_op2 operation2 opt_paren_args
4212 {
4213 /*%%%*/
4214 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4215 /*% %*/
4216 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4217 }
4218 | block_call call_op2 operation2 opt_paren_args brace_block
4219 {
4220 /*%%%*/
4221 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4222 /*% %*/
4223 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4224 }
4225 | block_call call_op2 operation2 command_args do_block
4226 {
4227 /*%%%*/
4228 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4229 /*% %*/
4230 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4231 }
4232 ;
4233
4234method_call : fcall paren_args
4235 {
4236 /*%%%*/
4237 $$ = $1;
4238 $$->nd_args = $2;
4239 nd_set_last_loc($1, @2.end_pos);
4240 /*% %*/
4241 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4242 }
4243 | primary_value call_op operation2 opt_paren_args
4244 {
4245 /*%%%*/
4246 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4247 nd_set_line($$, @3.end_pos.lineno);
4248 /*% %*/
4249 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4250 }
4251 | primary_value tCOLON2 operation2 paren_args
4252 {
4253 /*%%%*/
4254 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4255 nd_set_line($$, @3.end_pos.lineno);
4256 /*% %*/
4257 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4258 }
4259 | primary_value tCOLON2 operation3
4260 {
4261 /*%%%*/
4262 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4263 /*% %*/
4264 /*% ripper: call!($1, $2, $3) %*/
4265 }
4266 | primary_value call_op paren_args
4267 {
4268 /*%%%*/
4269 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4270 nd_set_line($$, @2.end_pos.lineno);
4271 /*% %*/
4272 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4273 }
4274 | primary_value tCOLON2 paren_args
4275 {
4276 /*%%%*/
4277 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4278 nd_set_line($$, @2.end_pos.lineno);
4279 /*% %*/
4280 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4281 }
4282 | keyword_super paren_args
4283 {
4284 /*%%%*/
4285 $$ = NEW_SUPER($2, &@$);
4286 /*% %*/
4287 /*% ripper: super!($2) %*/
4288 }
4289 | keyword_super
4290 {
4291 /*%%%*/
4292 $$ = NEW_ZSUPER(&@$);
4293 /*% %*/
4294 /*% ripper: zsuper! %*/
4295 }
4296 | primary_value '[' opt_call_args rbracket
4297 {
4298 /*%%%*/
4299 if ($1 && nd_type_p($1, NODE_SELF))
4300 $$ = NEW_FCALL(tAREF, $3, &@$);
4301 else
4302 $$ = NEW_CALL($1, tAREF, $3, &@$);
4303 fixpos($$, $1);
4304 /*% %*/
4305 /*% ripper: aref!($1, escape_Qundef($3)) %*/
4306 }
4307 ;
4308
4309brace_block : '{' brace_body '}'
4310 {
4311 $$ = $2;
4312 /*%%%*/
4313 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4314 nd_set_line($$, @1.end_pos.lineno);
4315 /*% %*/
4316 }
4317 | k_do do_body k_end
4318 {
4319 $$ = $2;
4320 /*%%%*/
4321 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4322 nd_set_line($$, @1.end_pos.lineno);
4323 /*% %*/
4324 }
4325 ;
4326
4327brace_body : {$<vars>$ = dyna_push(p);}
4328 {
4329 $<num>$ = p->max_numparam;
4330 p->max_numparam = 0;
4331 }
4332 {
4333 $<node>$ = numparam_push(p);
4334 }
4335 opt_block_param compstmt
4336 {
4337 int max_numparam = p->max_numparam;
4338 p->max_numparam = $<num>2;
4339 $4 = args_with_numbered(p, $4, max_numparam);
4340 /*%%%*/
4341 $$ = NEW_ITER($4, $5, &@$);
4342 /*% %*/
4343 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
4344 numparam_pop(p, $<node>3);
4345 dyna_pop(p, $<vars>1);
4346 }
4347 ;
4348
4349do_body : {$<vars>$ = dyna_push(p);}
4350 {
4351 $<num>$ = p->max_numparam;
4352 p->max_numparam = 0;
4353 }
4354 {
4355 $<node>$ = numparam_push(p);
4356 CMDARG_PUSH(0);
4357 }
4358 opt_block_param bodystmt
4359 {
4360 int max_numparam = p->max_numparam;
4361 p->max_numparam = $<num>2;
4362 $4 = args_with_numbered(p, $4, max_numparam);
4363 /*%%%*/
4364 $$ = NEW_ITER($4, $5, &@$);
4365 /*% %*/
4366 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4367 CMDARG_POP();
4368 numparam_pop(p, $<node>3);
4369 dyna_pop(p, $<vars>1);
4370 }
4371 ;
4372
4373case_args : arg_value
4374 {
4375 /*%%%*/
4376 check_literal_when(p, $1, &@1);
4377 $$ = NEW_LIST($1, &@$);
4378 /*% %*/
4379 /*% ripper: args_add!(args_new!, $1) %*/
4380 }
4381 | tSTAR arg_value
4382 {
4383 /*%%%*/
4384 $$ = NEW_SPLAT($2, &@$);
4385 /*% %*/
4386 /*% ripper: args_add_star!(args_new!, $2) %*/
4387 }
4388 | case_args ',' arg_value
4389 {
4390 /*%%%*/
4391 check_literal_when(p, $3, &@3);
4392 $$ = last_arg_append(p, $1, $3, &@$);
4393 /*% %*/
4394 /*% ripper: args_add!($1, $3) %*/
4395 }
4396 | case_args ',' tSTAR arg_value
4397 {
4398 /*%%%*/
4399 $$ = rest_arg_append(p, $1, $4, &@$);
4400 /*% %*/
4401 /*% ripper: args_add_star!($1, $4) %*/
4402 }
4403 ;
4404
4405case_body : k_when case_args then
4406 compstmt
4407 cases
4408 {
4409 /*%%%*/
4410 $$ = NEW_WHEN($2, $4, $5, &@$);
4411 fixpos($$, $2);
4412 /*% %*/
4413 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4414 }
4415 ;
4416
4417cases : opt_else
4418 | case_body
4419 ;
4420
4421p_case_body : keyword_in
4422 {
4423 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4424 p->command_start = FALSE;
4425 $<ctxt>1 = p->ctxt;
4426 p->ctxt.in_kwarg = 1;
4427 $<tbl>$ = push_pvtbl(p);
4428 }
4429 {
4430 $<tbl>$ = push_pktbl(p);
4431 }
4432 p_top_expr then
4433 {
4434 pop_pktbl(p, $<tbl>3);
4435 pop_pvtbl(p, $<tbl>2);
4436 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4437 }
4438 compstmt
4439 p_cases
4440 {
4441 /*%%%*/
4442 $$ = NEW_IN($4, $7, $8, &@$);
4443 /*% %*/
4444 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4445 }
4446 ;
4447
4448p_cases : opt_else
4449 | p_case_body
4450 ;
4451
4452p_top_expr : p_top_expr_body
4453 | p_top_expr_body modifier_if expr_value
4454 {
4455 /*%%%*/
4456 $$ = new_if(p, $3, $1, 0, &@$);
4457 fixpos($$, $3);
4458 /*% %*/
4459 /*% ripper: if_mod!($3, $1) %*/
4460 }
4461 | p_top_expr_body modifier_unless expr_value
4462 {
4463 /*%%%*/
4464 $$ = new_unless(p, $3, $1, 0, &@$);
4465 fixpos($$, $3);
4466 /*% %*/
4467 /*% ripper: unless_mod!($3, $1) %*/
4468 }
4469 ;
4470
4471p_top_expr_body : p_expr
4472 | p_expr ','
4473 {
4474 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4475 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4476 }
4477 | p_expr ',' p_args
4478 {
4479 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4480 /*%%%*/
4481 nd_set_first_loc($$, @1.beg_pos);
4482 /*%
4483 %*/
4484 }
4485 | p_find
4486 {
4487 $$ = new_find_pattern(p, Qnone, $1, &@$);
4488 }
4489 | p_args_tail
4490 {
4491 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4492 }
4493 | p_kwargs
4494 {
4495 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4496 }
4497 ;
4498
4499p_expr : p_as
4500 ;
4501
4502p_as : p_expr tASSOC p_variable
4503 {
4504 /*%%%*/
4505 NODE *n = NEW_LIST($1, &@$);
4506 n = list_append(p, n, $3);
4507 $$ = new_hash(p, n, &@$);
4508 /*% %*/
4509 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4510 }
4511 | p_alt
4512 ;
4513
4514p_alt : p_alt '|' p_expr_basic
4515 {
4516 /*%%%*/
4517 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4518 /*% %*/
4519 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4520 }
4521 | p_expr_basic
4522 ;
4523
4524p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4525p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4526
4527p_expr_basic : p_value
4528 | p_variable
4529 | p_const p_lparen p_args rparen
4530 {
4531 pop_pktbl(p, $<tbl>2);
4532 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4533 /*%%%*/
4534 nd_set_first_loc($$, @1.beg_pos);
4535 /*%
4536 %*/
4537 }
4538 | p_const p_lparen p_find rparen
4539 {
4540 pop_pktbl(p, $<tbl>2);
4541 $$ = new_find_pattern(p, $1, $3, &@$);
4542 /*%%%*/
4543 nd_set_first_loc($$, @1.beg_pos);
4544 /*%
4545 %*/
4546 }
4547 | p_const p_lparen p_kwargs rparen
4548 {
4549 pop_pktbl(p, $<tbl>2);
4550 $$ = new_hash_pattern(p, $1, $3, &@$);
4551 /*%%%*/
4552 nd_set_first_loc($$, @1.beg_pos);
4553 /*%
4554 %*/
4555 }
4556 | p_const '(' rparen
4557 {
4558 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4559 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4560 }
4561 | p_const p_lbracket p_args rbracket
4562 {
4563 pop_pktbl(p, $<tbl>2);
4564 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4565 /*%%%*/
4566 nd_set_first_loc($$, @1.beg_pos);
4567 /*%
4568 %*/
4569 }
4570 | p_const p_lbracket p_find rbracket
4571 {
4572 pop_pktbl(p, $<tbl>2);
4573 $$ = new_find_pattern(p, $1, $3, &@$);
4574 /*%%%*/
4575 nd_set_first_loc($$, @1.beg_pos);
4576 /*%
4577 %*/
4578 }
4579 | p_const p_lbracket p_kwargs rbracket
4580 {
4581 pop_pktbl(p, $<tbl>2);
4582 $$ = new_hash_pattern(p, $1, $3, &@$);
4583 /*%%%*/
4584 nd_set_first_loc($$, @1.beg_pos);
4585 /*%
4586 %*/
4587 }
4588 | p_const '[' rbracket
4589 {
4590 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4591 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4592 }
4593 | tLBRACK p_args rbracket
4594 {
4595 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4596 }
4597 | tLBRACK p_find rbracket
4598 {
4599 $$ = new_find_pattern(p, Qnone, $2, &@$);
4600 }
4601 | tLBRACK rbracket
4602 {
4603 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4604 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4605 }
4606 | tLBRACE
4607 {
4608 $<tbl>$ = push_pktbl(p);
4609 $<ctxt>1 = p->ctxt;
4610 p->ctxt.in_kwarg = 0;
4611 }
4612 p_kwargs rbrace
4613 {
4614 pop_pktbl(p, $<tbl>2);
4615 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4616 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4617 }
4618 | tLBRACE rbrace
4619 {
4620 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4621 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4622 }
4623 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4624 {
4625 pop_pktbl(p, $<tbl>2);
4626 $$ = $3;
4627 }
4628 ;
4629
4630p_args : p_expr
4631 {
4632 /*%%%*/
4633 NODE *pre_args = NEW_LIST($1, &@$);
4634 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4635 /*%
4636 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4637 %*/
4638 }
4639 | p_args_head
4640 {
4641 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4642 }
4643 | p_args_head p_arg
4644 {
4645 /*%%%*/
4646 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4647 /*%
4648 VALUE pre_args = rb_ary_concat($1, get_value($2));
4649 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4650 %*/
4651 }
4652 | p_args_head p_rest
4653 {
4654 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
4655 }
4656 | p_args_head p_rest ',' p_args_post
4657 {
4658 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
4659 }
4660 | p_args_tail
4661 ;
4662
4663p_args_head : p_arg ','
4664 {
4665 $$ = $1;
4666 }
4667 | p_args_head p_arg ','
4668 {
4669 /*%%%*/
4670 $$ = list_concat($1, $2);
4671 /*% %*/
4672 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4673 }
4674 ;
4675
4676p_args_tail : p_rest
4677 {
4678 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4679 }
4680 | p_rest ',' p_args_post
4681 {
4682 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4683 }
4684 ;
4685
4686p_find : p_rest ',' p_args_post ',' p_rest
4687 {
4688 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4689 }
4690 ;
4691
4692
4693p_rest : tSTAR tIDENTIFIER
4694 {
4695 $$ = $2;
4696 }
4697 | tSTAR
4698 {
4699 $$ = 0;
4700 }
4701 ;
4702
4703p_args_post : p_arg
4704 | p_args_post ',' p_arg
4705 {
4706 /*%%%*/
4707 $$ = list_concat($1, $3);
4708 /*% %*/
4709 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4710 }
4711 ;
4712
4713p_arg : p_expr
4714 {
4715 /*%%%*/
4716 $$ = NEW_LIST($1, &@$);
4717 /*% %*/
4718 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4719 }
4720 ;
4721
4722p_kwargs : p_kwarg ',' p_any_kwrest
4723 {
4724 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4725 }
4726 | p_kwarg
4727 {
4728 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4729 }
4730 | p_kwarg ','
4731 {
4732 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4733 }
4734 | p_any_kwrest
4735 {
4736 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4737 }
4738 ;
4739
4740p_kwarg : p_kw
4741 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4742 | p_kwarg ',' p_kw
4743 {
4744 /*%%%*/
4745 $$ = list_concat($1, $3);
4746 /*% %*/
4747 /*% ripper: rb_ary_push($1, $3) %*/
4748 }
4749 ;
4750
4751p_kw : p_kw_label p_expr
4752 {
4753 error_duplicate_pattern_key(p, get_id($1), &@1);
4754 /*%%%*/
4755 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
4756 /*% %*/
4757 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4758 }
4759 | p_kw_label
4760 {
4761 error_duplicate_pattern_key(p, get_id($1), &@1);
4762 if ($1 && !is_local_id(get_id($1))) {
4763 yyerror1(&@1, "key must be valid as local variables");
4764 }
4765 error_duplicate_pattern_variable(p, get_id($1), &@1);
4766 /*%%%*/
4767 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4768 /*% %*/
4769 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4770 }
4771 ;
4772
4773p_kw_label : tLABEL
4774 | tSTRING_BEG string_contents tLABEL_END
4775 {
4776 YYLTYPE loc = code_loc_gen(&@1, &@3);
4777 /*%%%*/
4778 if (!$2 || nd_type_p($2, NODE_STR)) {
4779 NODE *node = dsym_node(p, $2, &loc);
4780 $$ = SYM2ID(node->nd_lit);
4781 }
4782 /*%
4783 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4784 VALUE label = RNODE($2)->nd_cval;
4785 VALUE rval = RNODE($2)->nd_rval;
4786 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4787 RNODE($$)->nd_loc = loc;
4788 }
4789 %*/
4790 else {
4791 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4792 $$ = 0;
4793 }
4794 }
4795 ;
4796
4797p_kwrest : kwrest_mark tIDENTIFIER
4798 {
4799 $$ = $2;
4800 }
4801 | kwrest_mark
4802 {
4803 $$ = 0;
4804 }
4805 ;
4806
4807p_kwnorest : kwrest_mark keyword_nil
4808 {
4809 $$ = 0;
4810 }
4811 ;
4812
4813p_any_kwrest : p_kwrest
4814 | p_kwnorest {$$ = ID2VAL(idNil);}
4815 ;
4816
4817p_value : p_primitive
4818 | p_primitive tDOT2 p_primitive
4819 {
4820 /*%%%*/
4821 value_expr($1);
4822 value_expr($3);
4823 $$ = NEW_DOT2($1, $3, &@$);
4824 /*% %*/
4825 /*% ripper: dot2!($1, $3) %*/
4826 }
4827 | p_primitive tDOT3 p_primitive
4828 {
4829 /*%%%*/
4830 value_expr($1);
4831 value_expr($3);
4832 $$ = NEW_DOT3($1, $3, &@$);
4833 /*% %*/
4834 /*% ripper: dot3!($1, $3) %*/
4835 }
4836 | p_primitive tDOT2
4837 {
4838 /*%%%*/
4839 value_expr($1);
4840 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4841 /*% %*/
4842 /*% ripper: dot2!($1, Qnil) %*/
4843 }
4844 | p_primitive tDOT3
4845 {
4846 /*%%%*/
4847 value_expr($1);
4848 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4849 /*% %*/
4850 /*% ripper: dot3!($1, Qnil) %*/
4851 }
4852 | p_var_ref
4853 | p_expr_ref
4854 | p_const
4855 | tBDOT2 p_primitive
4856 {
4857 /*%%%*/
4858 value_expr($2);
4859 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4860 /*% %*/
4861 /*% ripper: dot2!(Qnil, $2) %*/
4862 }
4863 | tBDOT3 p_primitive
4864 {
4865 /*%%%*/
4866 value_expr($2);
4867 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4868 /*% %*/
4869 /*% ripper: dot3!(Qnil, $2) %*/
4870 }
4871 ;
4872
4873p_primitive : literal
4874 | strings
4875 | xstring
4876 | regexp
4877 | words
4878 | qwords
4879 | symbols
4880 | qsymbols
4881 | keyword_variable
4882 {
4883 /*%%%*/
4884 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4885 /*% %*/
4886 /*% ripper: var_ref!($1) %*/
4887 }
4888 | lambda
4889 ;
4890
4891p_variable : tIDENTIFIER
4892 {
4893 /*%%%*/
4894 error_duplicate_pattern_variable(p, $1, &@1);
4895 $$ = assignable(p, $1, 0, &@$);
4896 /*% %*/
4897 /*% ripper: assignable(p, var_field(p, $1)) %*/
4898 }
4899 ;
4900
4901p_var_ref : '^' tIDENTIFIER
4902 {
4903 /*%%%*/
4904 NODE *n = gettable(p, $2, &@$);
4905 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4906 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4907 }
4908 $$ = n;
4909 /*% %*/
4910 /*% ripper: var_ref!($2) %*/
4911 }
4912 | '^' nonlocal_var
4913 {
4914 /*%%%*/
4915 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4916 /*% %*/
4917 /*% ripper: var_ref!($2) %*/
4918 }
4919 ;
4920
4921p_expr_ref : '^' tLPAREN expr_value rparen
4922 {
4923 /*%%%*/
4924 $$ = NEW_BEGIN($3, &@$);
4925 /*% %*/
4926 /*% ripper: begin!($3) %*/
4927 }
4928 ;
4929
4930p_const : tCOLON3 cname
4931 {
4932 /*%%%*/
4933 $$ = NEW_COLON3($2, &@$);
4934 /*% %*/
4935 /*% ripper: top_const_ref!($2) %*/
4936 }
4937 | p_const tCOLON2 cname
4938 {
4939 /*%%%*/
4940 $$ = NEW_COLON2($1, $3, &@$);
4941 /*% %*/
4942 /*% ripper: const_path_ref!($1, $3) %*/
4943 }
4944 | tCONSTANT
4945 {
4946 /*%%%*/
4947 $$ = gettable(p, $1, &@$);
4948 /*% %*/
4949 /*% ripper: var_ref!($1) %*/
4950 }
4951 ;
4952
4953opt_rescue : k_rescue exc_list exc_var then
4954 compstmt
4955 opt_rescue
4956 {
4957 /*%%%*/
4958 $$ = NEW_RESBODY($2,
4959 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4960 $6, &@$);
4961
4962 if ($2) {
4963 fixpos($$, $2);
4964 }
4965 else if ($3) {
4966 fixpos($$, $3);
4967 }
4968 else {
4969 fixpos($$, $5);
4970 }
4971 /*% %*/
4972 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4973 }
4974 | none
4975 ;
4976
4977exc_list : arg_value
4978 {
4979 /*%%%*/
4980 $$ = NEW_LIST($1, &@$);
4981 /*% %*/
4982 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4983 }
4984 | mrhs
4985 {
4986 /*%%%*/
4987 if (!($$ = splat_array($1))) $$ = $1;
4988 /*% %*/
4989 /*% ripper: $1 %*/
4990 }
4991 | none
4992 ;
4993
4994exc_var : tASSOC lhs
4995 {
4996 $$ = $2;
4997 }
4998 | none
4999 ;
5000
5001opt_ensure : k_ensure compstmt
5002 {
5003 /*%%%*/
5004 $$ = $2;
5005 /*% %*/
5006 /*% ripper: ensure!($2) %*/
5007 }
5008 | none
5009 ;
5010
5011literal : numeric
5012 | symbol
5013 ;
5014
5015strings : string
5016 {
5017 /*%%%*/
5018 NODE *node = $1;
5019 if (!node) {
5020 node = NEW_STR(STR_NEW0(), &@$);
5021 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
5022 }
5023 else {
5024 node = evstr2dstr(p, node);
5025 }
5026 $$ = node;
5027 /*% %*/
5028 /*% ripper: $1 %*/
5029 }
5030 ;
5031
5032string : tCHAR
5033 | string1
5034 | string string1
5035 {
5036 /*%%%*/
5037 $$ = literal_concat(p, $1, $2, &@$);
5038 /*% %*/
5039 /*% ripper: string_concat!($1, $2) %*/
5040 }
5041 ;
5042
5043string1 : tSTRING_BEG string_contents tSTRING_END
5044 {
5045 /*%%%*/
5046 $$ = heredoc_dedent(p, $2);
5047 if ($$) nd_set_loc($$, &@$);
5048 /*% %*/
5049 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5050 }
5051 ;
5052
5053xstring : tXSTRING_BEG xstring_contents tSTRING_END
5054 {
5055 /*%%%*/
5056 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5057 /*% %*/
5058 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5059 }
5060 ;
5061
5062regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5063 {
5064 $$ = new_regexp(p, $2, $3, &@$);
5065 }
5066 ;
5067
5068words : tWORDS_BEG ' ' word_list tSTRING_END
5069 {
5070 /*%%%*/
5071 $$ = make_list($3, &@$);
5072 /*% %*/
5073 /*% ripper: array!($3) %*/
5074 }
5075 ;
5076
5077word_list : /* none */
5078 {
5079 /*%%%*/
5080 $$ = 0;
5081 /*% %*/
5082 /*% ripper: words_new! %*/
5083 }
5084 | word_list word ' '
5085 {
5086 /*%%%*/
5087 $$ = list_append(p, $1, evstr2dstr(p, $2));
5088 /*% %*/
5089 /*% ripper: words_add!($1, $2) %*/
5090 }
5091 ;
5092
5093word : string_content
5094 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5095 | word string_content
5096 {
5097 /*%%%*/
5098 $$ = literal_concat(p, $1, $2, &@$);
5099 /*% %*/
5100 /*% ripper: word_add!($1, $2) %*/
5101 }
5102 ;
5103
5104symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
5105 {
5106 /*%%%*/
5107 $$ = make_list($3, &@$);
5108 /*% %*/
5109 /*% ripper: array!($3) %*/
5110 }
5111 ;
5112
5113symbol_list : /* none */
5114 {
5115 /*%%%*/
5116 $$ = 0;
5117 /*% %*/
5118 /*% ripper: symbols_new! %*/
5119 }
5120 | symbol_list word ' '
5121 {
5122 /*%%%*/
5123 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5124 /*% %*/
5125 /*% ripper: symbols_add!($1, $2) %*/
5126 }
5127 ;
5128
5129qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
5130 {
5131 /*%%%*/
5132 $$ = make_list($3, &@$);
5133 /*% %*/
5134 /*% ripper: array!($3) %*/
5135 }
5136 ;
5137
5138qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
5139 {
5140 /*%%%*/
5141 $$ = make_list($3, &@$);
5142 /*% %*/
5143 /*% ripper: array!($3) %*/
5144 }
5145 ;
5146
5147qword_list : /* none */
5148 {
5149 /*%%%*/
5150 $$ = 0;
5151 /*% %*/
5152 /*% ripper: qwords_new! %*/
5153 }
5154 | qword_list tSTRING_CONTENT ' '
5155 {
5156 /*%%%*/
5157 $$ = list_append(p, $1, $2);
5158 /*% %*/
5159 /*% ripper: qwords_add!($1, $2) %*/
5160 }
5161 ;
5162
5163qsym_list : /* none */
5164 {
5165 /*%%%*/
5166 $$ = 0;
5167 /*% %*/
5168 /*% ripper: qsymbols_new! %*/
5169 }
5170 | qsym_list tSTRING_CONTENT ' '
5171 {
5172 /*%%%*/
5173 $$ = symbol_append(p, $1, $2);
5174 /*% %*/
5175 /*% ripper: qsymbols_add!($1, $2) %*/
5176 }
5177 ;
5178
5179string_contents : /* none */
5180 {
5181 /*%%%*/
5182 $$ = 0;
5183 /*% %*/
5184 /*% ripper: string_content! %*/
5185 /*%%%*/
5186 /*%
5187 $$ = ripper_new_yylval(p, 0, $$, 0);
5188 %*/
5189 }
5190 | string_contents string_content
5191 {
5192 /*%%%*/
5193 $$ = literal_concat(p, $1, $2, &@$);
5194 /*% %*/
5195 /*% ripper: string_add!($1, $2) %*/
5196 /*%%%*/
5197 /*%
5198 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
5199 !RNODE($1)->nd_cval) {
5200 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
5201 RNODE($1)->nd_rval = add_mark_object(p, $$);
5202 $$ = $1;
5203 }
5204 %*/
5205 }
5206 ;
5207
5208xstring_contents: /* none */
5209 {
5210 /*%%%*/
5211 $$ = 0;
5212 /*% %*/
5213 /*% ripper: xstring_new! %*/
5214 }
5215 | xstring_contents string_content
5216 {
5217 /*%%%*/
5218 $$ = literal_concat(p, $1, $2, &@$);
5219 /*% %*/
5220 /*% ripper: xstring_add!($1, $2) %*/
5221 }
5222 ;
5223
5224regexp_contents: /* none */
5225 {
5226 /*%%%*/
5227 $$ = 0;
5228 /*% %*/
5229 /*% ripper: regexp_new! %*/
5230 /*%%%*/
5231 /*%
5232 $$ = ripper_new_yylval(p, 0, $$, 0);
5233 %*/
5234 }
5235 | regexp_contents string_content
5236 {
5237 /*%%%*/
5238 NODE *head = $1, *tail = $2;
5239 if (!head) {
5240 $$ = tail;
5241 }
5242 else if (!tail) {
5243 $$ = head;
5244 }
5245 else {
5246 switch (nd_type(head)) {
5247 case NODE_STR:
5248 nd_set_type(head, NODE_DSTR);
5249 break;
5250 case NODE_DSTR:
5251 break;
5252 default:
5253 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5254 break;
5255 }
5256 $$ = list_append(p, head, tail);
5257 }
5258 /*%
5259 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5260 if (ripper_is_node_yylval(n1)) {
5261 s1 = RNODE(n1)->nd_cval;
5262 n1 = RNODE(n1)->nd_rval;
5263 }
5264 if (ripper_is_node_yylval(n2)) {
5265 s2 = RNODE(n2)->nd_cval;
5266 n2 = RNODE(n2)->nd_rval;
5267 }
5268 $$ = dispatch2(regexp_add, n1, n2);
5269 if (!s1 && s2) {
5270 $$ = ripper_new_yylval(p, 0, $$, s2);
5271 }
5272 %*/
5273 }
5274 ;
5275
5276string_content : tSTRING_CONTENT
5277 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5278 | tSTRING_DVAR
5279 {
5280 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5281 $<strterm>$ = p->lex.strterm;
5282 p->lex.strterm = 0;
5283 SET_LEX_STATE(EXPR_BEG);
5284 }
5285 string_dvar
5286 {
5287 p->lex.strterm = $<strterm>2;
5288 /*%%%*/
5289 $$ = NEW_EVSTR($3, &@$);
5290 nd_set_line($$, @3.end_pos.lineno);
5291 /*% %*/
5292 /*% ripper: string_dvar!($3) %*/
5293 }
5294 | tSTRING_DBEG
5295 {
5296 CMDARG_PUSH(0);
5297 COND_PUSH(0);
5298 }
5299 {
5300 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5301 $<strterm>$ = p->lex.strterm;
5302 p->lex.strterm = 0;
5303 }
5304 {
5305 $<num>$ = p->lex.state;
5306 SET_LEX_STATE(EXPR_BEG);
5307 }
5308 {
5309 $<num>$ = p->lex.brace_nest;
5310 p->lex.brace_nest = 0;
5311 }
5312 {
5313 $<num>$ = p->heredoc_indent;
5314 p->heredoc_indent = 0;
5315 }
5316 compstmt tSTRING_DEND
5317 {
5318 COND_POP();
5319 CMDARG_POP();
5320 p->lex.strterm = $<strterm>3;
5321 SET_LEX_STATE($<num>4);
5322 p->lex.brace_nest = $<num>5;
5323 p->heredoc_indent = $<num>6;
5324 p->heredoc_line_indent = -1;
5325 /*%%%*/
5326 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
5327 $$ = new_evstr(p, $7, &@$);
5328 /*% %*/
5329 /*% ripper: string_embexpr!($7) %*/
5330 }
5331 ;
5332
5333string_dvar : tGVAR
5334 {
5335 /*%%%*/
5336 $$ = NEW_GVAR($1, &@$);
5337 /*% %*/
5338 /*% ripper: var_ref!($1) %*/
5339 }
5340 | tIVAR
5341 {
5342 /*%%%*/
5343 $$ = NEW_IVAR($1, &@$);
5344 /*% %*/
5345 /*% ripper: var_ref!($1) %*/
5346 }
5347 | tCVAR
5348 {
5349 /*%%%*/
5350 $$ = NEW_CVAR($1, &@$);
5351 /*% %*/
5352 /*% ripper: var_ref!($1) %*/
5353 }
5354 | backref
5355 ;
5356
5357symbol : ssym
5358 | dsym
5359 ;
5360
5361ssym : tSYMBEG sym
5362 {
5363 SET_LEX_STATE(EXPR_END);
5364 /*%%%*/
5365 $$ = NEW_LIT(ID2SYM($2), &@$);
5366 /*% %*/
5367 /*% ripper: symbol_literal!(symbol!($2)) %*/
5368 }
5369 ;
5370
5371sym : fname
5372 | nonlocal_var
5373 ;
5374
5375dsym : tSYMBEG string_contents tSTRING_END
5376 {
5377 SET_LEX_STATE(EXPR_END);
5378 /*%%%*/
5379 $$ = dsym_node(p, $2, &@$);
5380 /*% %*/
5381 /*% ripper: dyna_symbol!($2) %*/
5382 }
5383 ;
5384
5385numeric : simple_numeric
5386 | tUMINUS_NUM simple_numeric %prec tLOWEST
5387 {
5388 /*%%%*/
5389 $$ = $2;
5390 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5391 /*% %*/
5392 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5393 }
5394 ;
5395
5396simple_numeric : tINTEGER
5397 | tFLOAT
5398 | tRATIONAL
5399 | tIMAGINARY
5400 ;
5401
5402nonlocal_var : tIVAR
5403 | tGVAR
5404 | tCVAR
5405 ;
5406
5407user_variable : tIDENTIFIER
5408 | tCONSTANT
5409 | nonlocal_var
5410 ;
5411
5412keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5413 | keyword_self {$$ = KWD2EID(self, $1);}
5414 | keyword_true {$$ = KWD2EID(true, $1);}
5415 | keyword_false {$$ = KWD2EID(false, $1);}
5416 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5417 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5418 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5419 ;
5420
5421var_ref : user_variable
5422 {
5423 /*%%%*/
5424 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5425 /*%
5426 if (id_is_var(p, get_id($1))) {
5427 $$ = dispatch1(var_ref, $1);
5428 }
5429 else {
5430 $$ = dispatch1(vcall, $1);
5431 }
5432 %*/
5433 }
5434 | keyword_variable
5435 {
5436 /*%%%*/
5437 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5438 /*% %*/
5439 /*% ripper: var_ref!($1) %*/
5440 }
5441 ;
5442
5443var_lhs : user_variable
5444 {
5445 /*%%%*/
5446 $$ = assignable(p, $1, 0, &@$);
5447 /*% %*/
5448 /*% ripper: assignable(p, var_field(p, $1)) %*/
5449 }
5450 | keyword_variable
5451 {
5452 /*%%%*/
5453 $$ = assignable(p, $1, 0, &@$);
5454 /*% %*/
5455 /*% ripper: assignable(p, var_field(p, $1)) %*/
5456 }
5457 ;
5458
5459backref : tNTH_REF
5460 | tBACK_REF
5461 ;
5462
5463superclass : '<'
5464 {
5465 SET_LEX_STATE(EXPR_BEG);
5466 p->command_start = TRUE;
5467 }
5468 expr_value term
5469 {
5470 $$ = $3;
5471 }
5472 | /* none */
5473 {
5474 /*%%%*/
5475 $$ = 0;
5476 /*% %*/
5477 /*% ripper: Qnil %*/
5478 }
5479 ;
5480
5481f_opt_paren_args: f_paren_args
5482 | none
5483 {
5484 p->ctxt.in_argdef = 0;
5485 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5486 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5487 }
5488 ;
5489
5490f_paren_args : '(' f_args rparen
5491 {
5492 /*%%%*/
5493 $$ = $2;
5494 /*% %*/
5495 /*% ripper: paren!($2) %*/
5496 SET_LEX_STATE(EXPR_BEG);
5497 p->command_start = TRUE;
5498 p->ctxt.in_argdef = 0;
5499 }
5500 ;
5501
5502f_arglist : f_paren_args
5503 | {
5504 $<ctxt>$ = p->ctxt;
5505 p->ctxt.in_kwarg = 1;
5506 p->ctxt.in_argdef = 1;
5507 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5508 }
5509 f_args term
5510 {
5511 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5512 p->ctxt.in_argdef = 0;
5513 $$ = $2;
5514 SET_LEX_STATE(EXPR_BEG);
5515 p->command_start = TRUE;
5516 }
5517 ;
5518
5519args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5520 {
5521 $$ = new_args_tail(p, $1, $3, $4, &@3);
5522 }
5523 | f_kwarg opt_f_block_arg
5524 {
5525 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5526 }
5527 | f_any_kwrest opt_f_block_arg
5528 {
5529 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5530 }
5531 | f_block_arg
5532 {
5533 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5534 }
5535 | args_forward
5536 {
5537 add_forwarding_args(p);
5538 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5539 /*%%%*/
5540 ($$->nd_ainfo)->forwarding = 1;
5541 /*% %*/
5542 }
5543 ;
5544
5545opt_args_tail : ',' args_tail
5546 {
5547 $$ = $2;
5548 }
5549 | /* none */
5550 {
5551 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5552 }
5553 ;
5554
5555f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5556 {
5557 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5558 }
5559 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5560 {
5561 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5562 }
5563 | f_arg ',' f_optarg opt_args_tail
5564 {
5565 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5566 }
5567 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5568 {
5569 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5570 }
5571 | f_arg ',' f_rest_arg opt_args_tail
5572 {
5573 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5574 }
5575 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5576 {
5577 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5578 }
5579 | f_arg opt_args_tail
5580 {
5581 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5582 }
5583 | f_optarg ',' f_rest_arg opt_args_tail
5584 {
5585 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5586 }
5587 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5588 {
5589 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5590 }
5591 | f_optarg opt_args_tail
5592 {
5593 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5594 }
5595 | f_optarg ',' f_arg opt_args_tail
5596 {
5597 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5598 }
5599 | f_rest_arg opt_args_tail
5600 {
5601 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5602 }
5603 | f_rest_arg ',' f_arg opt_args_tail
5604 {
5605 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5606 }
5607 | args_tail
5608 {
5609 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5610 }
5611 | /* none */
5612 {
5613 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5614 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5615 }
5616 ;
5617
5618args_forward : tBDOT3
5619 {
5620 /*%%%*/
5621#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
5622 $$ = 0;
5623#else
5624 $$ = idFWD_KWREST;
5625#endif
5626 /*% %*/
5627 /*% ripper: args_forward! %*/
5628 }
5629 ;
5630
5631f_bad_arg : tCONSTANT
5632 {
5633 static const char mesg[] = "formal argument cannot be a constant";
5634 /*%%%*/
5635 yyerror1(&@1, mesg);
5636 $$ = 0;
5637 /*% %*/
5638 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5639 }
5640 | tIVAR
5641 {
5642 static const char mesg[] = "formal argument cannot be an instance variable";
5643 /*%%%*/
5644 yyerror1(&@1, mesg);
5645 $$ = 0;
5646 /*% %*/
5647 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5648 }
5649 | tGVAR
5650 {
5651 static const char mesg[] = "formal argument cannot be a global variable";
5652 /*%%%*/
5653 yyerror1(&@1, mesg);
5654 $$ = 0;
5655 /*% %*/
5656 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5657 }
5658 | tCVAR
5659 {
5660 static const char mesg[] = "formal argument cannot be a class variable";
5661 /*%%%*/
5662 yyerror1(&@1, mesg);
5663 $$ = 0;
5664 /*% %*/
5665 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5666 }
5667 ;
5668
5669f_norm_arg : f_bad_arg
5670 | tIDENTIFIER
5671 {
5672 formal_argument(p, $1);
5673 p->max_numparam = ORDINAL_PARAM;
5674 $$ = $1;
5675 }
5676 ;
5677
5678f_arg_asgn : f_norm_arg
5679 {
5680 ID id = get_id($1);
5681 arg_var(p, id);
5682 p->cur_arg = id;
5683 $$ = $1;
5684 }
5685 ;
5686
5687f_arg_item : f_arg_asgn
5688 {
5689 p->cur_arg = 0;
5690 /*%%%*/
5691 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5692 /*% %*/
5693 /*% ripper: get_value($1) %*/
5694 }
5695 | tLPAREN f_margs rparen
5696 {
5697 /*%%%*/
5698 ID tid = internal_id(p);
5699 YYLTYPE loc;
5700 loc.beg_pos = @2.beg_pos;
5701 loc.end_pos = @2.beg_pos;
5702 arg_var(p, tid);
5703 if (dyna_in_block(p)) {
5704 $2->nd_value = NEW_DVAR(tid, &loc);
5705 }
5706 else {
5707 $2->nd_value = NEW_LVAR(tid, &loc);
5708 }
5709 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5710 $$->nd_next = $2;
5711 /*% %*/
5712 /*% ripper: mlhs_paren!($2) %*/
5713 }
5714 ;
5715
5716f_arg : f_arg_item
5717 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5718 | f_arg ',' f_arg_item
5719 {
5720 /*%%%*/
5721 $$ = $1;
5722 $$->nd_plen++;
5723 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5724 rb_discard_node(p, $3);
5725 /*% %*/
5726 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5727 }
5728 ;
5729
5730
5731f_label : tLABEL
5732 {
5733 arg_var(p, formal_argument(p, $1));
5734 p->cur_arg = get_id($1);
5735 p->max_numparam = ORDINAL_PARAM;
5736 p->ctxt.in_argdef = 0;
5737 $$ = $1;
5738 }
5739 ;
5740
5741f_kw : f_label arg_value
5742 {
5743 p->cur_arg = 0;
5744 p->ctxt.in_argdef = 1;
5745 /*%%%*/
5746 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5747 /*% %*/
5748 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5749 }
5750 | f_label
5751 {
5752 p->cur_arg = 0;
5753 p->ctxt.in_argdef = 1;
5754 /*%%%*/
5755 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5756 /*% %*/
5757 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5758 }
5759 ;
5760
5761f_block_kw : f_label primary_value
5762 {
5763 p->ctxt.in_argdef = 1;
5764 /*%%%*/
5765 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5766 /*% %*/
5767 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5768 }
5769 | f_label
5770 {
5771 p->ctxt.in_argdef = 1;
5772 /*%%%*/
5773 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5774 /*% %*/
5775 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5776 }
5777 ;
5778
5779f_block_kwarg : f_block_kw
5780 {
5781 /*%%%*/
5782 $$ = $1;
5783 /*% %*/
5784 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5785 }
5786 | f_block_kwarg ',' f_block_kw
5787 {
5788 /*%%%*/
5789 $$ = kwd_append($1, $3);
5790 /*% %*/
5791 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5792 }
5793 ;
5794
5795
5796f_kwarg : f_kw
5797 {
5798 /*%%%*/
5799 $$ = $1;
5800 /*% %*/
5801 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5802 }
5803 | f_kwarg ',' f_kw
5804 {
5805 /*%%%*/
5806 $$ = kwd_append($1, $3);
5807 /*% %*/
5808 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5809 }
5810 ;
5811
5812kwrest_mark : tPOW
5813 | tDSTAR
5814 ;
5815
5816f_no_kwarg : p_kwnorest
5817 {
5818 /*%%%*/
5819 /*% %*/
5820 /*% ripper: nokw_param!(Qnil) %*/
5821 }
5822 ;
5823
5824f_kwrest : kwrest_mark tIDENTIFIER
5825 {
5826 arg_var(p, shadowing_lvar(p, get_id($2)));
5827 /*%%%*/
5828 $$ = $2;
5829 /*% %*/
5830 /*% ripper: kwrest_param!($2) %*/
5831 }
5832 | kwrest_mark
5833 {
5834 arg_var(p, idFWD_KWREST);
5835 /*%%%*/
5836 $$ = idFWD_KWREST;
5837 /*% %*/
5838 /*% ripper: kwrest_param!(Qnil) %*/
5839 }
5840 ;
5841
5842f_opt : f_arg_asgn f_eq arg_value
5843 {
5844 p->cur_arg = 0;
5845 p->ctxt.in_argdef = 1;
5846 /*%%%*/
5847 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5848 /*% %*/
5849 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5850 }
5851 ;
5852
5853f_block_opt : f_arg_asgn f_eq primary_value
5854 {
5855 p->cur_arg = 0;
5856 p->ctxt.in_argdef = 1;
5857 /*%%%*/
5858 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5859 /*% %*/
5860 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5861 }
5862 ;
5863
5864f_block_optarg : f_block_opt
5865 {
5866 /*%%%*/
5867 $$ = $1;
5868 /*% %*/
5869 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5870 }
5871 | f_block_optarg ',' f_block_opt
5872 {
5873 /*%%%*/
5874 $$ = opt_arg_append($1, $3);
5875 /*% %*/
5876 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5877 }
5878 ;
5879
5880f_optarg : f_opt
5881 {
5882 /*%%%*/
5883 $$ = $1;
5884 /*% %*/
5885 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5886 }
5887 | f_optarg ',' f_opt
5888 {
5889 /*%%%*/
5890 $$ = opt_arg_append($1, $3);
5891 /*% %*/
5892 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5893 }
5894 ;
5895
5896restarg_mark : '*'
5897 | tSTAR
5898 ;
5899
5900f_rest_arg : restarg_mark tIDENTIFIER
5901 {
5902 arg_var(p, shadowing_lvar(p, get_id($2)));
5903 /*%%%*/
5904 $$ = $2;
5905 /*% %*/
5906 /*% ripper: rest_param!($2) %*/
5907 }
5908 | restarg_mark
5909 {
5910 arg_var(p, idFWD_REST);
5911 /*%%%*/
5912 $$ = idFWD_REST;
5913 /*% %*/
5914 /*% ripper: rest_param!(Qnil) %*/
5915 }
5916 ;
5917
5918blkarg_mark : '&'
5919 | tAMPER
5920 ;
5921
5922f_block_arg : blkarg_mark tIDENTIFIER
5923 {
5924 arg_var(p, shadowing_lvar(p, get_id($2)));
5925 /*%%%*/
5926 $$ = $2;
5927 /*% %*/
5928 /*% ripper: blockarg!($2) %*/
5929 }
5930 | blkarg_mark
5931 {
5932 arg_var(p, idFWD_BLOCK);
5933 /*%%%*/
5934 $$ = idFWD_BLOCK;
5935 /*% %*/
5936 /*% ripper: blockarg!(Qnil) %*/
5937 }
5938 ;
5939
5940opt_f_block_arg : ',' f_block_arg
5941 {
5942 $$ = $2;
5943 }
5944 | none
5945 {
5946 $$ = Qnull;
5947 }
5948 ;
5949
5950singleton : var_ref
5951 {
5952 value_expr($1);
5953 $$ = $1;
5954 }
5955 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5956 {
5957 /*%%%*/
5958 switch (nd_type($3)) {
5959 case NODE_STR:
5960 case NODE_DSTR:
5961 case NODE_XSTR:
5962 case NODE_DXSTR:
5963 case NODE_DREGX:
5964 case NODE_LIT:
5965 case NODE_LIST:
5966 case NODE_ZLIST:
5967 yyerror1(&@3, "can't define singleton method for literals");
5968 break;
5969 default:
5970 value_expr($3);
5971 break;
5972 }
5973 $$ = $3;
5974 /*% %*/
5975 /*% ripper: paren!($3) %*/
5976 }
5977 ;
5978
5979assoc_list : none
5980 | assocs trailer
5981 {
5982 /*%%%*/
5983 $$ = $1;
5984 /*% %*/
5985 /*% ripper: assoclist_from_args!($1) %*/
5986 }
5987 ;
5988
5989assocs : assoc
5990 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5991 | assocs ',' assoc
5992 {
5993 /*%%%*/
5994 NODE *assocs = $1;
5995 NODE *tail = $3;
5996 if (!assocs) {
5997 assocs = tail;
5998 }
5999 else if (tail) {
6000 if (assocs->nd_head &&
6001 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
6002 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
6003 /* DSTAR */
6004 tail = tail->nd_next->nd_head->nd_head;
6005 }
6006 assocs = list_concat(assocs, tail);
6007 }
6008 $$ = assocs;
6009 /*% %*/
6010 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6011 }
6012 ;
6013
6014assoc : arg_value tASSOC arg_value
6015 {
6016 /*%%%*/
6017 if (nd_type_p($1, NODE_STR)) {
6018 nd_set_type($1, NODE_LIT);
6019 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
6020 }
6021 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6022 /*% %*/
6023 /*% ripper: assoc_new!($1, $3) %*/
6024 }
6025 | tLABEL arg_value
6026 {
6027 /*%%%*/
6028 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6029 /*% %*/
6030 /*% ripper: assoc_new!($1, $2) %*/
6031 }
6032 | tLABEL
6033 {
6034 /*%%%*/
6035 NODE *val = gettable(p, $1, &@$);
6036 if (!val) val = NEW_BEGIN(0, &@$);
6037 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6038 /*% %*/
6039 /*% ripper: assoc_new!($1, Qnil) %*/
6040 }
6041 | tSTRING_BEG string_contents tLABEL_END arg_value
6042 {
6043 /*%%%*/
6044 YYLTYPE loc = code_loc_gen(&@1, &@3);
6045 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6046 /*% %*/
6047 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6048 }
6049 | tDSTAR arg_value
6050 {
6051 /*%%%*/
6052 if (nd_type_p($2, NODE_HASH) &&
6053 !($2->nd_head && $2->nd_head->nd_alen)) {
6054 static VALUE empty_hash;
6055 if (!empty_hash) {
6056 empty_hash = rb_obj_freeze(rb_hash_new());
6057 rb_gc_register_mark_object(empty_hash);
6058 }
6059 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6060 }
6061 else
6062 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6063 /*% %*/
6064 /*% ripper: assoc_splat!($2) %*/
6065 }
6066 | tDSTAR
6067 {
6068 if (!local_id(p, idFWD_KWREST) ||
6069 local_id(p, idFWD_ALL)) {
6070 compile_error(p, "no anonymous keyword rest parameter");
6071 }
6072 /*%%%*/
6073 $$ = list_append(p, NEW_LIST(0, &@$),
6074 NEW_LVAR(idFWD_KWREST, &@$));
6075 /*% %*/
6076 /*% ripper: assoc_splat!(Qnil) %*/
6077 }
6078 ;
6079
6080operation : tIDENTIFIER
6081 | tCONSTANT
6082 | tFID
6083 ;
6084
6085operation2 : operation
6086 | op
6087 ;
6088
6089operation3 : tIDENTIFIER
6090 | tFID
6091 | op
6092 ;
6093
6094dot_or_colon : '.'
6095 | tCOLON2
6096 ;
6097
6098call_op : '.'
6099 | tANDDOT
6100 ;
6101
6102call_op2 : call_op
6103 | tCOLON2
6104 ;
6105
6106opt_terms : /* none */
6107 | terms
6108 ;
6109
6110opt_nl : /* none */
6111 | '\n'
6112 ;
6113
6114rparen : opt_nl ')'
6115 ;
6116
6117rbracket : opt_nl ']'
6118 ;
6119
6120rbrace : opt_nl '}'
6121 ;
6122
6123trailer : opt_nl
6124 | ','
6125 ;
6126
6127term : ';' {yyerrok;token_flush(p);}
6128 | '\n'
6129 {
6130 @$.end_pos = @$.beg_pos;
6131 token_flush(p);
6132 }
6133 ;
6134
6135terms : term
6136 | terms ';' {yyerrok;}
6137 ;
6138
6139none : /* none */
6140 {
6141 $$ = Qnull;
6142 }
6143 ;
6144%%
6145# undef p
6146# undef yylex
6147# undef yylval
6148# define yylval (*p->lval)
6149
6150static int regx_options(struct parser_params*);
6151static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6152static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6153static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6154static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6155
6156#ifndef RIPPER
6157# define set_yylval_node(x) { \
6158 YYLTYPE _cur_loc; \
6159 rb_parser_set_location(p, &_cur_loc); \
6160 yylval.node = (x); \
6161}
6162# define set_yylval_str(x) \
6163do { \
6164 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6165 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6166} while(0)
6167# define set_yylval_literal(x) \
6168do { \
6169 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6170 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6171} while(0)
6172# define set_yylval_num(x) (yylval.num = (x))
6173# define set_yylval_id(x) (yylval.id = (x))
6174# define set_yylval_name(x) (yylval.id = (x))
6175# define yylval_id() (yylval.id)
6176#else
6177static inline VALUE
6178ripper_yylval_id(struct parser_params *p, ID x)
6179{
6180 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6181}
6182# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6183# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6184# define set_yylval_id(x) (void)(x)
6185# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6186# define set_yylval_literal(x) add_mark_object(p, (x))
6187# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6188# define yylval_id() yylval.id
6189# define _cur_loc NULL_LOC /* dummy */
6190#endif
6191
6192#define set_yylval_noname() set_yylval_id(keyword_nil)
6193#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6194
6195#ifndef RIPPER
6196#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6197#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6198
6199static bool
6200parser_has_token(struct parser_params *p)
6201{
6202 if (p->keep_tokens && (p->lex.pcur < p->lex.ptok)) rb_bug("lex.pcur < lex.ptok. (line: %d) %ld|%ld|%ld", p->ruby_sourceline, p->lex.ptok - p->lex.pbeg, p->lex.pcur - p->lex.ptok, p->lex.pend - p->lex.pcur);
6203 return p->lex.pcur > p->lex.ptok;
6204}
6205
6206static VALUE
6207code_loc_to_ary(const rb_code_location_t *loc)
6208{
6209 VALUE ary = rb_ary_new_from_args(4,
6210 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6211 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6212 rb_obj_freeze(ary);
6213
6214 return ary;
6215}
6216
6217static void
6218parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6219{
6220 VALUE ary;
6221 int token_id;
6222
6223 ary = rb_ary_new2(4);
6224 token_id = p->token_id;
6225 rb_ary_push(ary, INT2FIX(token_id));
6226 rb_ary_push(ary, ID2SYM(parser_token2id(t)));
6227 rb_ary_push(ary, str);
6228 rb_ary_push(ary, code_loc_to_ary(p->yylloc));
6229 rb_obj_freeze(ary);
6230 rb_ary_push(p->tokens, ary);
6231 p->token_id++;
6232
6233 if (p->debug) {
6234 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6235 }
6236}
6237
6238static void
6239parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6240{
6241 debug_token_line(p, "parser_dispatch_scan_event", line);
6242
6243 if (!parser_has_token(p)) return;
6244
6245 RUBY_SET_YYLLOC(*p->yylloc);
6246
6247 if (p->keep_tokens) {
6248 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6249 parser_append_tokens(p, str, t, line);
6250 }
6251
6252 token_flush(p);
6253}
6254
6255#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6256static void
6257parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6258{
6259 int saved_line = p->ruby_sourceline;
6260 const char *saved_tokp = p->lex.ptok;
6261
6262 debug_token_line(p, "parser_dispatch_delayed_token", line);
6263
6264 if (!has_delayed_token(p)) return;
6265
6266 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6267
6268 if (p->keep_tokens) {
6269 p->ruby_sourceline = p->delayed.beg_line;
6270 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6271 parser_append_tokens(p, p->delayed.token, t, line);
6272 p->ruby_sourceline = saved_line;
6273 p->lex.ptok = saved_tokp;
6274 }
6275
6276 p->delayed.token = Qnil;
6277}
6278#else
6279#define literal_flush(p, ptr) ((void)(ptr))
6280
6281#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
6282
6283static inline VALUE
6284intern_sym(const char *name)
6285{
6286 ID id = rb_intern_const(name);
6287 return ID2SYM(id);
6288}
6289
6290static int
6291ripper_has_scan_event(struct parser_params *p)
6292{
6293 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6294 return p->lex.pcur > p->lex.ptok;
6295}
6296
6297static VALUE
6298ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6299{
6300 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6301 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6302 RUBY_SET_YYLLOC(*p->yylloc);
6303 token_flush(p);
6304 return rval;
6305}
6306
6307static void
6308ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6309{
6310 if (!ripper_has_scan_event(p)) return;
6311 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6312}
6313#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6314
6315static void
6316ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6317{
6318 int saved_line = p->ruby_sourceline;
6319 const char *saved_tokp = p->lex.ptok;
6320
6321 if (!has_delayed_token(p)) return;
6322 p->ruby_sourceline = p->delayed.beg_line;
6323 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6324 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6325 p->delayed.token = Qnil;
6326 p->ruby_sourceline = saved_line;
6327 p->lex.ptok = saved_tokp;
6328}
6329#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6330#endif /* RIPPER */
6331
6332static inline int
6333is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6334{
6335 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6336}
6337
6338static inline int
6339parser_is_identchar(struct parser_params *p)
6340{
6341 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
6342}
6343
6344static inline int
6345parser_isascii(struct parser_params *p)
6346{
6347 return ISASCII(*(p->lex.pcur-1));
6348}
6349
6350static void
6351token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6352{
6353 int column = 1, nonspc = 0, i;
6354 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6355 if (*ptr == '\t') {
6356 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6357 }
6358 column++;
6359 if (*ptr != ' ' && *ptr != '\t') {
6360 nonspc = 1;
6361 }
6362 }
6363
6364 ptinfo->beg = loc->beg_pos;
6365 ptinfo->indent = column;
6366 ptinfo->nonspc = nonspc;
6367}
6368
6369static void
6370token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6371{
6372 token_info *ptinfo;
6373
6374 if (!p->token_info_enabled) return;
6375 ptinfo = ALLOC(token_info);
6376 ptinfo->token = token;
6377 ptinfo->next = p->token_info;
6378 token_info_setup(ptinfo, p->lex.pbeg, loc);
6379
6380 p->token_info = ptinfo;
6381}
6382
6383static void
6384token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6385{
6386 token_info *ptinfo_beg = p->token_info;
6387
6388 if (!ptinfo_beg) return;
6389 p->token_info = ptinfo_beg->next;
6390
6391 /* indentation check of matched keywords (begin..end, if..end, etc.) */
6392 token_info_warn(p, token, ptinfo_beg, 1, loc);
6393 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6394}
6395
6396static void
6397token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
6398{
6399 token_info *ptinfo_beg = p->token_info;
6400
6401 if (!ptinfo_beg) return;
6402 p->token_info = ptinfo_beg->next;
6403
6404 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
6405 ptinfo_beg->beg.column != beg_pos.column ||
6406 strcmp(ptinfo_beg->token, token)) {
6407 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
6408 beg_pos.lineno, beg_pos.column, token,
6409 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
6410 ptinfo_beg->token);
6411 }
6412
6413 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6414}
6415
6416static void
6417token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
6418{
6419 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
6420 if (!p->token_info_enabled) return;
6421 if (!ptinfo_beg) return;
6422 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
6423 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
6424 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
6425 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
6426 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
6427 rb_warn3L(ptinfo_end->beg.lineno,
6428 "mismatched indentations at '%s' with '%s' at %d",
6429 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
6430}
6431
6432static int
6433parser_precise_mbclen(struct parser_params *p, const char *ptr)
6434{
6435 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
6436 if (!MBCLEN_CHARFOUND_P(len)) {
6437 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
6438 return -1;
6439 }
6440 return len;
6441}
6442
6443#ifndef RIPPER
6444static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6445
6446static inline void
6447parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6448{
6449 VALUE str;
6450 int lineno = p->ruby_sourceline;
6451 if (!yylloc) {
6452 return;
6453 }
6454 else if (yylloc->beg_pos.lineno == lineno) {
6455 str = p->lex.lastline;
6456 }
6457 else {
6458 return;
6459 }
6460 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6461}
6462
6463static int
6464parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6465{
6466#if 0
6467 YYLTYPE current;
6468
6469 if (!yylloc) {
6470 yylloc = RUBY_SET_YYLLOC(current);
6471 }
6472 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6473 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6474 yylloc = 0;
6475 }
6476#endif
6477 compile_error(p, "%s", msg);
6478 parser_show_error_line(p, yylloc);
6479 return 0;
6480}
6481
6482static int
6483parser_yyerror0(struct parser_params *p, const char *msg)
6484{
6485 YYLTYPE current;
6486 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6487}
6488
6489static void
6490ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6491{
6492 VALUE mesg;
6493 const int max_line_margin = 30;
6494 const char *ptr, *ptr_end, *pt, *pb;
6495 const char *pre = "", *post = "", *pend;
6496 const char *code = "", *caret = "";
6497 const char *lim;
6498 const char *const pbeg = RSTRING_PTR(str);
6499 char *buf;
6500 long len;
6501 int i;
6502
6503 if (!yylloc) return;
6504 pend = RSTRING_END(str);
6505 if (pend > pbeg && pend[-1] == '\n') {
6506 if (--pend > pbeg && pend[-1] == '\r') --pend;
6507 }
6508
6509 pt = pend;
6510 if (lineno == yylloc->end_pos.lineno &&
6511 (pend - pbeg) > yylloc->end_pos.column) {
6512 pt = pbeg + yylloc->end_pos.column;
6513 }
6514
6515 ptr = ptr_end = pt;
6516 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6517 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6518
6519 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6520 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6521
6522 len = ptr_end - ptr;
6523 if (len > 4) {
6524 if (ptr > pbeg) {
6525 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6526 if (ptr > pbeg) pre = "...";
6527 }
6528 if (ptr_end < pend) {
6529 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6530 if (ptr_end < pend) post = "...";
6531 }
6532 }
6533 pb = pbeg;
6534 if (lineno == yylloc->beg_pos.lineno) {
6535 pb += yylloc->beg_pos.column;
6536 if (pb > pt) pb = pt;
6537 }
6538 if (pb < ptr) pb = ptr;
6539 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6540 return;
6541 }
6542 if (RTEST(errbuf)) {
6543 mesg = rb_attr_get(errbuf, idMesg);
6544 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6545 rb_str_cat_cstr(mesg, "\n");
6546 }
6547 else {
6548 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6549 }
6550 if (!errbuf && rb_stderr_tty_p()) {
6551#define CSI_BEGIN "\033["
6552#define CSI_SGR "m"
6553 rb_str_catf(mesg,
6554 CSI_BEGIN""CSI_SGR"%s" /* pre */
6555 CSI_BEGIN"1"CSI_SGR"%.*s"
6556 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6557 CSI_BEGIN";1"CSI_SGR"%.*s"
6558 CSI_BEGIN""CSI_SGR"%s" /* post */
6559 "\n",
6560 pre,
6561 (int)(pb - ptr), ptr,
6562 (int)(pt - pb), pb,
6563 (int)(ptr_end - pt), pt,
6564 post);
6565 }
6566 else {
6567 char *p2;
6568
6569 len = ptr_end - ptr;
6570 lim = pt < pend ? pt : pend;
6571 i = (int)(lim - ptr);
6572 buf = ALLOCA_N(char, i+2);
6573 code = ptr;
6574 caret = p2 = buf;
6575 if (ptr <= pb) {
6576 while (ptr < pb) {
6577 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6578 }
6579 *p2++ = '^';
6580 ptr++;
6581 }
6582 if (lim > ptr) {
6583 memset(p2, '~', (lim - ptr));
6584 p2 += (lim - ptr);
6585 }
6586 *p2 = '\0';
6587 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6588 pre, (int)len, code, post,
6589 pre, caret);
6590 }
6591 if (!errbuf) rb_write_error_str(mesg);
6592}
6593#else
6594static int
6595parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6596{
6597 const char *pcur = 0, *ptok = 0;
6598 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6599 p->ruby_sourceline == yylloc->end_pos.lineno) {
6600 pcur = p->lex.pcur;
6601 ptok = p->lex.ptok;
6602 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6603 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6604 }
6605 parser_yyerror0(p, msg);
6606 if (pcur) {
6607 p->lex.ptok = ptok;
6608 p->lex.pcur = pcur;
6609 }
6610 return 0;
6611}
6612
6613static int
6614parser_yyerror0(struct parser_params *p, const char *msg)
6615{
6616 dispatch1(parse_error, STR_NEW2(msg));
6617 ripper_error(p);
6618 return 0;
6619}
6620
6621static inline void
6622parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6623{
6624}
6625#endif /* !RIPPER */
6626
6627#ifndef RIPPER
6628static int
6629vtable_size(const struct vtable *tbl)
6630{
6631 if (!DVARS_TERMINAL_P(tbl)) {
6632 return tbl->pos;
6633 }
6634 else {
6635 return 0;
6636 }
6637}
6638#endif
6639
6640static struct vtable *
6641vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6642{
6643 struct vtable *tbl = ALLOC(struct vtable);
6644 tbl->pos = 0;
6645 tbl->capa = 8;
6646 tbl->tbl = ALLOC_N(ID, tbl->capa);
6647 tbl->prev = prev;
6648#ifndef RIPPER
6649 if (p->debug) {
6650 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6651 }
6652#endif
6653 return tbl;
6654}
6655#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6656
6657static void
6658vtable_free_gen(struct parser_params *p, int line, const char *name,
6659 struct vtable *tbl)
6660{
6661#ifndef RIPPER
6662 if (p->debug) {
6663 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6664 }
6665#endif
6666 if (!DVARS_TERMINAL_P(tbl)) {
6667 if (tbl->tbl) {
6668 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6669 }
6670 ruby_sized_xfree(tbl, sizeof(*tbl));
6671 }
6672}
6673#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6674
6675static void
6676vtable_add_gen(struct parser_params *p, int line, const char *name,
6677 struct vtable *tbl, ID id)
6678{
6679#ifndef RIPPER
6680 if (p->debug) {
6681 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6682 line, name, (void *)tbl, rb_id2name(id));
6683 }
6684#endif
6685 if (DVARS_TERMINAL_P(tbl)) {
6686 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6687 return;
6688 }
6689 if (tbl->pos == tbl->capa) {
6690 tbl->capa = tbl->capa * 2;
6691 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6692 }
6693 tbl->tbl[tbl->pos++] = id;
6694}
6695#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6696
6697#ifndef RIPPER
6698static void
6699vtable_pop_gen(struct parser_params *p, int line, const char *name,
6700 struct vtable *tbl, int n)
6701{
6702 if (p->debug) {
6703 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6704 line, name, (void *)tbl, n);
6705 }
6706 if (tbl->pos < n) {
6707 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6708 return;
6709 }
6710 tbl->pos -= n;
6711}
6712#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6713#endif
6714
6715static int
6716vtable_included(const struct vtable * tbl, ID id)
6717{
6718 int i;
6719
6720 if (!DVARS_TERMINAL_P(tbl)) {
6721 for (i = 0; i < tbl->pos; i++) {
6722 if (tbl->tbl[i] == id) {
6723 return i+1;
6724 }
6725 }
6726 }
6727 return 0;
6728}
6729
6730static void parser_prepare(struct parser_params *p);
6731
6732#ifndef RIPPER
6733static NODE *parser_append_options(struct parser_params *p, NODE *node);
6734
6735static VALUE
6736debug_lines(VALUE fname)
6737{
6738 ID script_lines;
6739 CONST_ID(script_lines, "SCRIPT_LINES__");
6740 if (rb_const_defined_at(rb_cObject, script_lines)) {
6741 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6742 if (RB_TYPE_P(hash, T_HASH)) {
6743 VALUE lines = rb_ary_new();
6744 rb_hash_aset(hash, fname, lines);
6745 return lines;
6746 }
6747 }
6748 return 0;
6749}
6750
6751static int
6752e_option_supplied(struct parser_params *p)
6753{
6754 return strcmp(p->ruby_sourcefile, "-e") == 0;
6755}
6756
6757static VALUE
6758yycompile0(VALUE arg)
6759{
6760 int n;
6761 NODE *tree;
6762 struct parser_params *p = (struct parser_params *)arg;
6763 VALUE cov = Qfalse;
6764
6765 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6766 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6767 if (p->debug_lines && p->ruby_sourceline > 0) {
6768 VALUE str = rb_default_rs;
6769 n = p->ruby_sourceline;
6770 do {
6771 rb_ary_push(p->debug_lines, str);
6772 } while (--n);
6773 }
6774
6775 if (!e_option_supplied(p)) {
6776 cov = Qtrue;
6777 }
6778 }
6779
6780 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6781 if (!p->debug_lines) {
6782 p->debug_lines = rb_ary_new();
6783 }
6784
6785 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6786 }
6787
6788 parser_prepare(p);
6789#define RUBY_DTRACE_PARSE_HOOK(name) \
6790 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6791 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6792 }
6793 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6794 n = yyparse(p);
6795 RUBY_DTRACE_PARSE_HOOK(END);
6796 p->debug_lines = 0;
6797
6798 p->lex.strterm = 0;
6799 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6800 if (n || p->error_p) {
6801 VALUE mesg = p->error_buffer;
6802 if (!mesg) {
6803 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6804 }
6805 if (!p->error_tolerant) {
6806 rb_set_errinfo(mesg);
6807 return FALSE;
6808 }
6809 }
6810 tree = p->eval_tree;
6811 if (!tree) {
6812 tree = NEW_NIL(&NULL_LOC);
6813 }
6814 else {
6815 VALUE opt = p->compile_option;
6816 VALUE tokens = p->tokens;
6817 NODE *prelude;
6818 NODE *body = parser_append_options(p, tree->nd_body);
6819 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6820 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6821 prelude = block_append(p, p->eval_tree_begin, body);
6822 tree->nd_body = prelude;
6823 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6824 if (p->keep_tokens) {
6825 rb_obj_freeze(tokens);
6826 rb_ast_set_tokens(p->ast, tokens);
6827 }
6828 }
6829 p->ast->body.root = tree;
6830 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6831 return TRUE;
6832}
6833
6834static rb_ast_t *
6835yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6836{
6837 rb_ast_t *ast;
6838 if (NIL_P(fname)) {
6839 p->ruby_sourcefile_string = Qnil;
6840 p->ruby_sourcefile = "(none)";
6841 }
6842 else {
6843 p->ruby_sourcefile_string = rb_fstring(fname);
6844 p->ruby_sourcefile = StringValueCStr(fname);
6845 }
6846 p->ruby_sourceline = line - 1;
6847
6848 p->lvtbl = NULL;
6849
6850 p->ast = ast = rb_ast_new();
6851 rb_suppress_tracing(yycompile0, (VALUE)p);
6852 p->ast = 0;
6853 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6854
6855 while (p->lvtbl) {
6856 local_pop(p);
6857 }
6858
6859 return ast;
6860}
6861#endif /* !RIPPER */
6862
6863static rb_encoding *
6864must_be_ascii_compatible(VALUE s)
6865{
6866 rb_encoding *enc = rb_enc_get(s);
6867 if (!rb_enc_asciicompat(enc)) {
6868 rb_raise(rb_eArgError, "invalid source encoding");
6869 }
6870 return enc;
6871}
6872
6873static VALUE
6874lex_get_str(struct parser_params *p, VALUE s)
6875{
6876 char *beg, *end, *start;
6877 long len;
6878
6879 beg = RSTRING_PTR(s);
6880 len = RSTRING_LEN(s);
6881 start = beg;
6882 if (p->lex.gets_.ptr) {
6883 if (len == p->lex.gets_.ptr) return Qnil;
6884 beg += p->lex.gets_.ptr;
6885 len -= p->lex.gets_.ptr;
6886 }
6887 end = memchr(beg, '\n', len);
6888 if (end) len = ++end - beg;
6889 p->lex.gets_.ptr += len;
6890 return rb_str_subseq(s, beg - start, len);
6891}
6892
6893static VALUE
6894lex_getline(struct parser_params *p)
6895{
6896 VALUE line = (*p->lex.gets)(p, p->lex.input);
6897 if (NIL_P(line)) return line;
6898 must_be_ascii_compatible(line);
6899 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6900 p->line_count++;
6901 return line;
6902}
6903
6904static const rb_data_type_t parser_data_type;
6905
6906#ifndef RIPPER
6907static rb_ast_t*
6908parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6909{
6910 struct parser_params *p;
6911
6912 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6913
6914 p->lex.gets = lex_get_str;
6915 p->lex.gets_.ptr = 0;
6916 p->lex.input = rb_str_new_frozen(s);
6917 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6918
6919 return yycompile(vparser, p, fname, line);
6920}
6921
6922rb_ast_t*
6923rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6924{
6925 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6926}
6927
6928rb_ast_t*
6929rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6930{
6931 must_be_ascii_compatible(s);
6932 return parser_compile_string(vparser, f, s, line);
6933}
6934
6935VALUE rb_io_gets_internal(VALUE io);
6936
6937static VALUE
6938lex_io_gets(struct parser_params *p, VALUE io)
6939{
6940 return rb_io_gets_internal(io);
6941}
6942
6943rb_ast_t*
6944rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6945{
6946 struct parser_params *p;
6947
6948 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6949
6950 p->lex.gets = lex_io_gets;
6951 p->lex.input = file;
6952 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6953
6954 return yycompile(vparser, p, fname, start);
6955}
6956
6957static VALUE
6958lex_generic_gets(struct parser_params *p, VALUE input)
6959{
6960 return (*p->lex.gets_.call)(input, p->line_count);
6961}
6962
6963rb_ast_t*
6964rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6965{
6966 struct parser_params *p;
6967
6968 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6969
6970 p->lex.gets = lex_generic_gets;
6971 p->lex.gets_.call = lex_gets;
6972 p->lex.input = input;
6973 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6974
6975 return yycompile(vparser, p, fname, start);
6976}
6977#endif /* !RIPPER */
6978
6979#define STR_FUNC_ESCAPE 0x01
6980#define STR_FUNC_EXPAND 0x02
6981#define STR_FUNC_REGEXP 0x04
6982#define STR_FUNC_QWORDS 0x08
6983#define STR_FUNC_SYMBOL 0x10
6984#define STR_FUNC_INDENT 0x20
6985#define STR_FUNC_LABEL 0x40
6986#define STR_FUNC_LIST 0x4000
6987#define STR_FUNC_TERM 0x8000
6988
6989enum string_type {
6990 str_label = STR_FUNC_LABEL,
6991 str_squote = (0),
6992 str_dquote = (STR_FUNC_EXPAND),
6993 str_xquote = (STR_FUNC_EXPAND),
6994 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6995 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6996 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6997 str_ssym = (STR_FUNC_SYMBOL),
6998 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6999};
7000
7001static VALUE
7002parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7003{
7004 VALUE str;
7005
7006 str = rb_enc_str_new(ptr, len, enc);
7007 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7008 if (is_ascii_string(str)) {
7009 }
7010 else if (rb_is_usascii_enc(enc0) && enc != rb_utf8_encoding()) {
7011 rb_enc_associate(str, rb_ascii8bit_encoding());
7012 }
7013 }
7014
7015 return str;
7016}
7017
7018#define peek(p,c) peek_n(p, (c), 0)
7019#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7020#define peekc(p) peekc_n(p, 0)
7021#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7022
7023static void
7024add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7025{
7026#ifndef RIPPER
7027 debug_token_line(p, "add_delayed_token", line);
7028#endif
7029
7030 if (tok < end) {
7031 if (!has_delayed_token(p)) {
7032 p->delayed.token = rb_str_buf_new(end - tok);
7033 rb_enc_associate(p->delayed.token, p->enc);
7034 p->delayed.beg_line = p->ruby_sourceline;
7035 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7036 }
7037 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7038 p->delayed.end_line = p->ruby_sourceline;
7039 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7040 p->lex.ptok = end;
7041 }
7042}
7043
7044static int
7045nextline(struct parser_params *p, int set_encoding)
7046{
7047 VALUE v = p->lex.nextline;
7048 p->lex.nextline = 0;
7049 if (!v) {
7050 if (p->eofp)
7051 return -1;
7052
7053 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
7054 goto end_of_input;
7055 }
7056
7057 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7058 end_of_input:
7059 p->eofp = 1;
7060 lex_goto_eol(p);
7061 return -1;
7062 }
7063#ifndef RIPPER
7064 if (p->debug_lines) {
7065 if (set_encoding) rb_enc_associate(v, p->enc);
7066 rb_ary_push(p->debug_lines, v);
7067 }
7068#endif
7069 p->cr_seen = FALSE;
7070 }
7071 else if (NIL_P(v)) {
7072 /* after here-document without terminator */
7073 goto end_of_input;
7074 }
7075 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7076 if (p->heredoc_end > 0) {
7077 p->ruby_sourceline = p->heredoc_end;
7078 p->heredoc_end = 0;
7079 }
7080 p->ruby_sourceline++;
7081 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7082 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7083 token_flush(p);
7084 p->lex.lastline = v;
7085 return 0;
7086}
7087
7088static int
7089parser_cr(struct parser_params *p, int c)
7090{
7091 if (peek(p, '\n')) {
7092 p->lex.pcur++;
7093 c = '\n';
7094 }
7095 return c;
7096}
7097
7098static inline int
7099nextc0(struct parser_params *p, int set_encoding)
7100{
7101 int c;
7102
7103 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
7104 if (nextline(p, set_encoding)) return -1;
7105 }
7106 c = (unsigned char)*p->lex.pcur++;
7107 if (UNLIKELY(c == '\r')) {
7108 c = parser_cr(p, c);
7109 }
7110
7111 return c;
7112}
7113#define nextc(p) nextc0(p, TRUE)
7114
7115static void
7116pushback(struct parser_params *p, int c)
7117{
7118 if (c == -1) return;
7119 p->lex.pcur--;
7120 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7121 p->lex.pcur--;
7122 }
7123}
7124
7125#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7126
7127#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7128#define tok(p) (p)->tokenbuf
7129#define toklen(p) (p)->tokidx
7130
7131static int
7132looking_at_eol_p(struct parser_params *p)
7133{
7134 const char *ptr = p->lex.pcur;
7135 while (ptr < p->lex.pend) {
7136 int c = (unsigned char)*ptr++;
7137 int eol = (c == '\n' || c == '#');
7138 if (eol || !ISSPACE(c)) {
7139 return eol;
7140 }
7141 }
7142 return TRUE;
7143}
7144
7145static char*
7146newtok(struct parser_params *p)
7147{
7148 p->tokidx = 0;
7149 p->tokline = p->ruby_sourceline;
7150 if (!p->tokenbuf) {
7151 p->toksiz = 60;
7152 p->tokenbuf = ALLOC_N(char, 60);
7153 }
7154 if (p->toksiz > 4096) {
7155 p->toksiz = 60;
7156 REALLOC_N(p->tokenbuf, char, 60);
7157 }
7158 return p->tokenbuf;
7159}
7160
7161static char *
7162tokspace(struct parser_params *p, int n)
7163{
7164 p->tokidx += n;
7165
7166 if (p->tokidx >= p->toksiz) {
7167 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7168 REALLOC_N(p->tokenbuf, char, p->toksiz);
7169 }
7170 return &p->tokenbuf[p->tokidx-n];
7171}
7172
7173static void
7174tokadd(struct parser_params *p, int c)
7175{
7176 p->tokenbuf[p->tokidx++] = (char)c;
7177 if (p->tokidx >= p->toksiz) {
7178 p->toksiz *= 2;
7179 REALLOC_N(p->tokenbuf, char, p->toksiz);
7180 }
7181}
7182
7183static int
7184tok_hex(struct parser_params *p, size_t *numlen)
7185{
7186 int c;
7187
7188 c = scan_hex(p->lex.pcur, 2, numlen);
7189 if (!*numlen) {
7190 yyerror0("invalid hex escape");
7191 token_flush(p);
7192 return 0;
7193 }
7194 p->lex.pcur += *numlen;
7195 return c;
7196}
7197
7198#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7199
7200static int
7201escaped_control_code(int c)
7202{
7203 int c2 = 0;
7204 switch (c) {
7205 case ' ':
7206 c2 = 's';
7207 break;
7208 case '\n':
7209 c2 = 'n';
7210 break;
7211 case '\t':
7212 c2 = 't';
7213 break;
7214 case '\v':
7215 c2 = 'v';
7216 break;
7217 case '\r':
7218 c2 = 'r';
7219 break;
7220 case '\f':
7221 c2 = 'f';
7222 break;
7223 }
7224 return c2;
7225}
7226
7227#define WARN_SPACE_CHAR(c, prefix) \
7228 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7229
7230static int
7231tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7232 int regexp_literal, int wide)
7233{
7234 size_t numlen;
7235 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7236 p->lex.pcur += numlen;
7237 if (p->lex.strterm == NULL ||
7238 (p->lex.strterm->flags & STRTERM_HEREDOC) ||
7239 (p->lex.strterm->u.literal.u1.func != str_regexp)) {
7240 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7241 literal_flush(p, p->lex.pcur);
7242 yyerror0("invalid Unicode escape");
7243 return wide && numlen > 0;
7244 }
7245 if (codepoint > 0x10ffff) {
7246 literal_flush(p, p->lex.pcur);
7247 yyerror0("invalid Unicode codepoint (too large)");
7248 return wide;
7249 }
7250 if ((codepoint & 0xfffff800) == 0xd800) {
7251 literal_flush(p, p->lex.pcur);
7252 yyerror0("invalid Unicode codepoint");
7253 return wide;
7254 }
7255 }
7256 if (regexp_literal) {
7257 tokcopy(p, (int)numlen);
7258 }
7259 else if (codepoint >= 0x80) {
7260 rb_encoding *utf8 = rb_utf8_encoding();
7261 if (*encp && utf8 != *encp) {
7262 YYLTYPE loc = RUBY_INIT_YYLLOC();
7263 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7264 parser_show_error_line(p, &loc);
7265 return wide;
7266 }
7267 *encp = utf8;
7268 tokaddmbc(p, codepoint, *encp);
7269 }
7270 else {
7271 tokadd(p, codepoint);
7272 }
7273 return TRUE;
7274}
7275
7276static int tokadd_mbchar(struct parser_params *p, int c);
7277
7278static int
7279tokskip_mbchar(struct parser_params *p)
7280{
7281 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7282 if (len > 0) {
7283 p->lex.pcur += len - 1;
7284 }
7285 return len;
7286}
7287
7288/* return value is for ?\u3042 */
7289static void
7290tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7291 int term, int symbol_literal, int regexp_literal)
7292{
7293 /*
7294 * If `term` is not -1, then we allow multiple codepoints in \u{}
7295 * upto `term` byte, otherwise we're parsing a character literal.
7296 * And then add the codepoints to the current token.
7297 */
7298 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7299
7300 const int open_brace = '{', close_brace = '}';
7301
7302 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7303
7304 if (peek(p, open_brace)) { /* handle \u{...} form */
7305 if (regexp_literal && p->lex.strterm->u.literal.u1.func == str_regexp) {
7306 /*
7307 * Skip parsing validation code and copy bytes as-is until term or
7308 * closing brace, in order to correctly handle extended regexps where
7309 * invalid unicode escapes are allowed in comments. The regexp parser
7310 * does its own validation and will catch any issues.
7311 */
7312 tokadd(p, open_brace);
7313 while (++p->lex.pcur < p->lex.pend) {
7314 int c = peekc(p);
7315 if (c == close_brace) {
7316 tokadd(p, c);
7317 ++p->lex.pcur;
7318 break;
7319 }
7320 else if (c == term) {
7321 break;
7322 }
7323 if (c == '\\' && p->lex.pcur + 1 < p->lex.pend) {
7324 tokadd(p, c);
7325 c = *++p->lex.pcur;
7326 }
7327 tokadd_mbchar(p, c);
7328 }
7329 }
7330 else {
7331 const char *second = NULL;
7332 int c, last = nextc(p);
7333 if (p->lex.pcur >= p->lex.pend) goto unterminated;
7334 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
7335 while (c != close_brace) {
7336 if (c == term) goto unterminated;
7337 if (second == multiple_codepoints)
7338 second = p->lex.pcur;
7339 if (regexp_literal) tokadd(p, last);
7340 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7341 break;
7342 }
7343 while (ISSPACE(c = *p->lex.pcur)) {
7344 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
7345 last = c;
7346 }
7347 if (term == -1 && !second)
7348 second = multiple_codepoints;
7349 }
7350
7351 if (c != close_brace) {
7352 unterminated:
7353 token_flush(p);
7354 yyerror0("unterminated Unicode escape");
7355 return;
7356 }
7357 if (second && second != multiple_codepoints) {
7358 const char *pcur = p->lex.pcur;
7359 p->lex.pcur = second;
7360 dispatch_scan_event(p, tSTRING_CONTENT);
7361 token_flush(p);
7362 p->lex.pcur = pcur;
7363 yyerror0(multiple_codepoints);
7364 token_flush(p);
7365 }
7366
7367 if (regexp_literal) tokadd(p, close_brace);
7368 nextc(p);
7369 }
7370 }
7371 else { /* handle \uxxxx form */
7372 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7373 token_flush(p);
7374 return;
7375 }
7376 }
7377}
7378
7379#define ESCAPE_CONTROL 1
7380#define ESCAPE_META 2
7381
7382static int
7383read_escape(struct parser_params *p, int flags, rb_encoding **encp)
7384{
7385 int c;
7386 size_t numlen;
7387
7388 switch (c = nextc(p)) {
7389 case '\\': /* Backslash */
7390 return c;
7391
7392 case 'n': /* newline */
7393 return '\n';
7394
7395 case 't': /* horizontal tab */
7396 return '\t';
7397
7398 case 'r': /* carriage-return */
7399 return '\r';
7400
7401 case 'f': /* form-feed */
7402 return '\f';
7403
7404 case 'v': /* vertical tab */
7405 return '\13';
7406
7407 case 'a': /* alarm(bell) */
7408 return '\007';
7409
7410 case 'e': /* escape */
7411 return 033;
7412
7413 case '0': case '1': case '2': case '3': /* octal constant */
7414 case '4': case '5': case '6': case '7':
7415 pushback(p, c);
7416 c = scan_oct(p->lex.pcur, 3, &numlen);
7417 p->lex.pcur += numlen;
7418 return c;
7419
7420 case 'x': /* hex constant */
7421 c = tok_hex(p, &numlen);
7422 if (numlen == 0) return 0;
7423 return c;
7424
7425 case 'b': /* backspace */
7426 return '\010';
7427
7428 case 's': /* space */
7429 return ' ';
7430
7431 case 'M':
7432 if (flags & ESCAPE_META) goto eof;
7433 if ((c = nextc(p)) != '-') {
7434 goto eof;
7435 }
7436 if ((c = nextc(p)) == '\\') {
7437 switch (peekc(p)) {
7438 case 'u': case 'U':
7439 nextc(p);
7440 goto eof;
7441 }
7442 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
7443 }
7444 else if (c == -1) goto eof;
7445 else if (!ISASCII(c)) {
7446 tokskip_mbchar(p);
7447 goto eof;
7448 }
7449 else {
7450 int c2 = escaped_control_code(c);
7451 if (c2) {
7452 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
7453 WARN_SPACE_CHAR(c2, "\\M-");
7454 }
7455 else {
7456 WARN_SPACE_CHAR(c2, "\\C-\\M-");
7457 }
7458 }
7459 else if (ISCNTRL(c)) goto eof;
7460 return ((c & 0xff) | 0x80);
7461 }
7462
7463 case 'C':
7464 if ((c = nextc(p)) != '-') {
7465 goto eof;
7466 }
7467 case 'c':
7468 if (flags & ESCAPE_CONTROL) goto eof;
7469 if ((c = nextc(p))== '\\') {
7470 switch (peekc(p)) {
7471 case 'u': case 'U':
7472 nextc(p);
7473 goto eof;
7474 }
7475 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
7476 }
7477 else if (c == '?')
7478 return 0177;
7479 else if (c == -1) goto eof;
7480 else if (!ISASCII(c)) {
7481 tokskip_mbchar(p);
7482 goto eof;
7483 }
7484 else {
7485 int c2 = escaped_control_code(c);
7486 if (c2) {
7487 if (ISCNTRL(c)) {
7488 if (flags & ESCAPE_META) {
7489 WARN_SPACE_CHAR(c2, "\\M-");
7490 }
7491 else {
7492 WARN_SPACE_CHAR(c2, "");
7493 }
7494 }
7495 else {
7496 if (flags & ESCAPE_META) {
7497 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7498 }
7499 else {
7500 WARN_SPACE_CHAR(c2, "\\C-");
7501 }
7502 }
7503 }
7504 else if (ISCNTRL(c)) goto eof;
7505 }
7506 return c & 0x9f;
7507
7508 eof:
7509 case -1:
7510 yyerror0("Invalid escape character syntax");
7511 dispatch_scan_event(p, tSTRING_CONTENT);
7512 return '\0';
7513
7514 default:
7515 return c;
7516 }
7517}
7518
7519static void
7520tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7521{
7522 int len = rb_enc_codelen(c, enc);
7523 rb_enc_mbcput(c, tokspace(p, len), enc);
7524}
7525
7526static int
7527tokadd_escape(struct parser_params *p, rb_encoding **encp)
7528{
7529 int c;
7530 size_t numlen;
7531
7532 switch (c = nextc(p)) {
7533 case '\n':
7534 return 0; /* just ignore */
7535
7536 case '0': case '1': case '2': case '3': /* octal constant */
7537 case '4': case '5': case '6': case '7':
7538 {
7539 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7540 if (numlen == 0) goto eof;
7541 p->lex.pcur += numlen;
7542 tokcopy(p, (int)numlen + 1);
7543 }
7544 return 0;
7545
7546 case 'x': /* hex constant */
7547 {
7548 tok_hex(p, &numlen);
7549 if (numlen == 0) return -1;
7550 tokcopy(p, (int)numlen + 2);
7551 }
7552 return 0;
7553
7554 eof:
7555 case -1:
7556 yyerror0("Invalid escape character syntax");
7557 token_flush(p);
7558 return -1;
7559
7560 default:
7561 tokadd(p, '\\');
7562 tokadd(p, c);
7563 }
7564 return 0;
7565}
7566
7567static int
7568regx_options(struct parser_params *p)
7569{
7570 int kcode = 0;
7571 int kopt = 0;
7572 int options = 0;
7573 int c, opt, kc;
7574
7575 newtok(p);
7576 while (c = nextc(p), ISALPHA(c)) {
7577 if (c == 'o') {
7578 options |= RE_OPTION_ONCE;
7579 }
7580 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7581 if (kc >= 0) {
7582 if (kc != rb_ascii8bit_encindex()) kcode = c;
7583 kopt = opt;
7584 }
7585 else {
7586 options |= opt;
7587 }
7588 }
7589 else {
7590 tokadd(p, c);
7591 }
7592 }
7593 options |= kopt;
7594 pushback(p, c);
7595 if (toklen(p)) {
7596 YYLTYPE loc = RUBY_INIT_YYLLOC();
7597 tokfix(p);
7598 compile_error(p, "unknown regexp option%s - %*s",
7599 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7600 parser_show_error_line(p, &loc);
7601 }
7602 return options | RE_OPTION_ENCODING(kcode);
7603}
7604
7605static int
7606tokadd_mbchar(struct parser_params *p, int c)
7607{
7608 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7609 if (len < 0) return -1;
7610 tokadd(p, c);
7611 p->lex.pcur += --len;
7612 if (len > 0) tokcopy(p, len);
7613 return c;
7614}
7615
7616static inline int
7617simple_re_meta(int c)
7618{
7619 switch (c) {
7620 case '$': case '*': case '+': case '.':
7621 case '?': case '^': case '|':
7622 case ')': case ']': case '}': case '>':
7623 return TRUE;
7624 default:
7625 return FALSE;
7626 }
7627}
7628
7629static int
7630parser_update_heredoc_indent(struct parser_params *p, int c)
7631{
7632 if (p->heredoc_line_indent == -1) {
7633 if (c == '\n') p->heredoc_line_indent = 0;
7634 }
7635 else {
7636 if (c == ' ') {
7637 p->heredoc_line_indent++;
7638 return TRUE;
7639 }
7640 else if (c == '\t') {
7641 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7642 p->heredoc_line_indent = w * TAB_WIDTH;
7643 return TRUE;
7644 }
7645 else if (c != '\n') {
7646 if (p->heredoc_indent > p->heredoc_line_indent) {
7647 p->heredoc_indent = p->heredoc_line_indent;
7648 }
7649 p->heredoc_line_indent = -1;
7650 }
7651 }
7652 return FALSE;
7653}
7654
7655static void
7656parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7657{
7658 YYLTYPE loc = RUBY_INIT_YYLLOC();
7659 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7660 compile_error(p, "%s mixed within %s source", n1, n2);
7661 parser_show_error_line(p, &loc);
7662}
7663
7664static void
7665parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7666{
7667 const char *pos = p->lex.pcur;
7668 p->lex.pcur = beg;
7669 parser_mixed_error(p, enc1, enc2);
7670 p->lex.pcur = pos;
7671}
7672
7673static int
7674tokadd_string(struct parser_params *p,
7675 int func, int term, int paren, long *nest,
7676 rb_encoding **encp, rb_encoding **enc)
7677{
7678 int c;
7679 bool erred = false;
7680#ifdef RIPPER
7681 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
7682 int top_of_line = FALSE;
7683#endif
7684
7685#define mixed_error(enc1, enc2) \
7686 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7687#define mixed_escape(beg, enc1, enc2) \
7688 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7689
7690 while ((c = nextc(p)) != -1) {
7691 if (p->heredoc_indent > 0) {
7692 parser_update_heredoc_indent(p, c);
7693 }
7694#ifdef RIPPER
7695 if (top_of_line && heredoc_end == p->ruby_sourceline) {
7696 pushback(p, c);
7697 break;
7698 }
7699#endif
7700
7701 if (paren && c == paren) {
7702 ++*nest;
7703 }
7704 else if (c == term) {
7705 if (!nest || !*nest) {
7706 pushback(p, c);
7707 break;
7708 }
7709 --*nest;
7710 }
7711 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7712 unsigned char c2 = *p->lex.pcur;
7713 if (c2 == '$' || c2 == '@' || c2 == '{') {
7714 pushback(p, c);
7715 break;
7716 }
7717 }
7718 else if (c == '\\') {
7719 c = nextc(p);
7720 switch (c) {
7721 case '\n':
7722 if (func & STR_FUNC_QWORDS) break;
7723 if (func & STR_FUNC_EXPAND) {
7724 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7725 continue;
7726 if (c == term) {
7727 c = '\\';
7728 goto terminate;
7729 }
7730 }
7731 tokadd(p, '\\');
7732 break;
7733
7734 case '\\':
7735 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7736 break;
7737
7738 case 'u':
7739 if ((func & STR_FUNC_EXPAND) == 0) {
7740 tokadd(p, '\\');
7741 break;
7742 }
7743 tokadd_utf8(p, enc, term,
7744 func & STR_FUNC_SYMBOL,
7745 func & STR_FUNC_REGEXP);
7746 continue;
7747
7748 default:
7749 if (c == -1) return -1;
7750 if (!ISASCII(c)) {
7751 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7752 goto non_ascii;
7753 }
7754 if (func & STR_FUNC_REGEXP) {
7755 switch (c) {
7756 case 'c':
7757 case 'C':
7758 case 'M': {
7759 pushback(p, c);
7760 c = read_escape(p, 0, enc);
7761
7762 int i;
7763 char escbuf[5];
7764 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7765 for (i = 0; i < 4; i++) {
7766 tokadd(p, escbuf[i]);
7767 }
7768 continue;
7769 }
7770 }
7771
7772 if (c == term && !simple_re_meta(c)) {
7773 tokadd(p, c);
7774 continue;
7775 }
7776 pushback(p, c);
7777 if ((c = tokadd_escape(p, enc)) < 0)
7778 return -1;
7779 if (*enc && *enc != *encp) {
7780 mixed_escape(p->lex.ptok+2, *enc, *encp);
7781 }
7782 continue;
7783 }
7784 else if (func & STR_FUNC_EXPAND) {
7785 pushback(p, c);
7786 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7787 c = read_escape(p, 0, enc);
7788 }
7789 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7790 /* ignore backslashed spaces in %w */
7791 }
7792 else if (c != term && !(paren && c == paren)) {
7793 tokadd(p, '\\');
7794 pushback(p, c);
7795 continue;
7796 }
7797 }
7798 }
7799 else if (!parser_isascii(p)) {
7800 non_ascii:
7801 if (!*enc) {
7802 *enc = *encp;
7803 }
7804 else if (*enc != *encp) {
7805 mixed_error(*enc, *encp);
7806 continue;
7807 }
7808 if (tokadd_mbchar(p, c) == -1) return -1;
7809 continue;
7810 }
7811 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7812 pushback(p, c);
7813 break;
7814 }
7815 if (c & 0x80) {
7816 if (!*enc) {
7817 *enc = *encp;
7818 }
7819 else if (*enc != *encp) {
7820 mixed_error(*enc, *encp);
7821 continue;
7822 }
7823 }
7824 tokadd(p, c);
7825#ifdef RIPPER
7826 top_of_line = (c == '\n');
7827#endif
7828 }
7829 terminate:
7830 if (*enc) *encp = *enc;
7831 return c;
7832}
7833
7834static inline rb_strterm_t *
7835new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7836{
7837 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7838}
7839
7840/* imemo_parser_strterm for literal */
7841#define NEW_STRTERM(func, term, paren) \
7842 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7843
7844#ifdef RIPPER
7845static void
7846flush_string_content(struct parser_params *p, rb_encoding *enc)
7847{
7848 VALUE content = yylval.val;
7849 if (!ripper_is_node_yylval(content))
7850 content = ripper_new_yylval(p, 0, 0, content);
7851 if (has_delayed_token(p)) {
7852 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7853 if (len > 0) {
7854 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7855 }
7856 dispatch_delayed_token(p, tSTRING_CONTENT);
7857 p->lex.ptok = p->lex.pcur;
7858 RNODE(content)->nd_rval = yylval.val;
7859 }
7860 dispatch_scan_event(p, tSTRING_CONTENT);
7861 if (yylval.val != content)
7862 RNODE(content)->nd_rval = yylval.val;
7863 yylval.val = content;
7864}
7865#else
7866static void
7867flush_string_content(struct parser_params *p, rb_encoding *enc)
7868{
7869 if (has_delayed_token(p)) {
7870 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7871 if (len > 0) {
7872 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7873 p->delayed.end_line = p->ruby_sourceline;
7874 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
7875 }
7876 dispatch_delayed_token(p, tSTRING_CONTENT);
7877 p->lex.ptok = p->lex.pcur;
7878 }
7879 dispatch_scan_event(p, tSTRING_CONTENT);
7880}
7881#endif
7882
7883RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7884/* this can be shared with ripper, since it's independent from struct
7885 * parser_params. */
7886#ifndef RIPPER
7887#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7888#define SPECIAL_PUNCT(idx) ( \
7889 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7890 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7891 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7892 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7893 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7894 BIT('0', idx))
7895const unsigned int ruby_global_name_punct_bits[] = {
7896 SPECIAL_PUNCT(0),
7897 SPECIAL_PUNCT(1),
7898 SPECIAL_PUNCT(2),
7899};
7900#undef BIT
7901#undef SPECIAL_PUNCT
7902#endif
7903
7904static enum yytokentype
7905parser_peek_variable_name(struct parser_params *p)
7906{
7907 int c;
7908 const char *ptr = p->lex.pcur;
7909
7910 if (ptr + 1 >= p->lex.pend) return 0;
7911 c = *ptr++;
7912 switch (c) {
7913 case '$':
7914 if ((c = *ptr) == '-') {
7915 if (++ptr >= p->lex.pend) return 0;
7916 c = *ptr;
7917 }
7918 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7919 return tSTRING_DVAR;
7920 }
7921 break;
7922 case '@':
7923 if ((c = *ptr) == '@') {
7924 if (++ptr >= p->lex.pend) return 0;
7925 c = *ptr;
7926 }
7927 break;
7928 case '{':
7929 p->lex.pcur = ptr;
7930 p->command_start = TRUE;
7931 return tSTRING_DBEG;
7932 default:
7933 return 0;
7934 }
7935 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7936 return tSTRING_DVAR;
7937 return 0;
7938}
7939
7940#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7941#define IS_END() IS_lex_state(EXPR_END_ANY)
7942#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7943#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7944#define IS_LABEL_POSSIBLE() (\
7945 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7946 IS_ARG())
7947#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7948#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7949
7950static inline enum yytokentype
7951parser_string_term(struct parser_params *p, int func)
7952{
7953 p->lex.strterm = 0;
7954 if (func & STR_FUNC_REGEXP) {
7955 set_yylval_num(regx_options(p));
7956 dispatch_scan_event(p, tREGEXP_END);
7957 SET_LEX_STATE(EXPR_END);
7958 return tREGEXP_END;
7959 }
7960 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7961 nextc(p);
7962 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7963 return tLABEL_END;
7964 }
7965 SET_LEX_STATE(EXPR_END);
7966 return tSTRING_END;
7967}
7968
7969static enum yytokentype
7970parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7971{
7972 int func = (int)quote->u1.func;
7973 int term = (int)quote->u3.term;
7974 int paren = (int)quote->u2.paren;
7975 int c, space = 0;
7976 rb_encoding *enc = p->enc;
7977 rb_encoding *base_enc = 0;
7978 VALUE lit;
7979
7980 if (func & STR_FUNC_TERM) {
7981 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7982 SET_LEX_STATE(EXPR_END);
7983 p->lex.strterm = 0;
7984 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7985 }
7986 c = nextc(p);
7987 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7988 do {c = nextc(p);} while (ISSPACE(c));
7989 space = 1;
7990 }
7991 if (func & STR_FUNC_LIST) {
7992 quote->u1.func &= ~STR_FUNC_LIST;
7993 space = 1;
7994 }
7995 if (c == term && !quote->u0.nest) {
7996 if (func & STR_FUNC_QWORDS) {
7997 quote->u1.func |= STR_FUNC_TERM;
7998 pushback(p, c); /* dispatch the term at tSTRING_END */
7999 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8000 return ' ';
8001 }
8002 return parser_string_term(p, func);
8003 }
8004 if (space) {
8005 pushback(p, c);
8006 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8007 return ' ';
8008 }
8009 newtok(p);
8010 if ((func & STR_FUNC_EXPAND) && c == '#') {
8011 int t = parser_peek_variable_name(p);
8012 if (t) return t;
8013 tokadd(p, '#');
8014 c = nextc(p);
8015 }
8016 pushback(p, c);
8017 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
8018 &enc, &base_enc) == -1) {
8019 if (p->eofp) {
8020#ifndef RIPPER
8021# define unterminated_literal(mesg) yyerror0(mesg)
8022#else
8023# define unterminated_literal(mesg) compile_error(p, mesg)
8024#endif
8025 literal_flush(p, p->lex.pcur);
8026 if (func & STR_FUNC_QWORDS) {
8027 /* no content to add, bailing out here */
8028 unterminated_literal("unterminated list meets end of file");
8029 p->lex.strterm = 0;
8030 return tSTRING_END;
8031 }
8032 if (func & STR_FUNC_REGEXP) {
8033 unterminated_literal("unterminated regexp meets end of file");
8034 }
8035 else {
8036 unterminated_literal("unterminated string meets end of file");
8037 }
8038 quote->u1.func |= STR_FUNC_TERM;
8039 }
8040 }
8041
8042 tokfix(p);
8043 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8044 set_yylval_str(lit);
8045 flush_string_content(p, enc);
8046
8047 return tSTRING_CONTENT;
8048}
8049
8050static enum yytokentype
8051heredoc_identifier(struct parser_params *p)
8052{
8053 /*
8054 * term_len is length of `<<"END"` except `END`,
8055 * in this case term_len is 4 (<, <, " and ").
8056 */
8057 long len, offset = p->lex.pcur - p->lex.pbeg;
8058 int c = nextc(p), term, func = 0, quote = 0;
8059 enum yytokentype token = tSTRING_BEG;
8060 int indent = 0;
8061
8062 if (c == '-') {
8063 c = nextc(p);
8064 func = STR_FUNC_INDENT;
8065 offset++;
8066 }
8067 else if (c == '~') {
8068 c = nextc(p);
8069 func = STR_FUNC_INDENT;
8070 offset++;
8071 indent = INT_MAX;
8072 }
8073 switch (c) {
8074 case '\'':
8075 func |= str_squote; goto quoted;
8076 case '"':
8077 func |= str_dquote; goto quoted;
8078 case '`':
8079 token = tXSTRING_BEG;
8080 func |= str_xquote; goto quoted;
8081
8082 quoted:
8083 quote++;
8084 offset++;
8085 term = c;
8086 len = 0;
8087 while ((c = nextc(p)) != term) {
8088 if (c == -1 || c == '\r' || c == '\n') {
8089 yyerror0("unterminated here document identifier");
8090 return -1;
8091 }
8092 }
8093 break;
8094
8095 default:
8096 if (!parser_is_identchar(p)) {
8097 pushback(p, c);
8098 if (func & STR_FUNC_INDENT) {
8099 pushback(p, indent > 0 ? '~' : '-');
8100 }
8101 return 0;
8102 }
8103 func |= str_dquote;
8104 do {
8105 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8106 if (n < 0) return 0;
8107 p->lex.pcur += --n;
8108 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8109 pushback(p, c);
8110 break;
8111 }
8112
8113 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8114 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8115 yyerror0("too long here document identifier");
8116 dispatch_scan_event(p, tHEREDOC_BEG);
8117 lex_goto_eol(p);
8118
8119 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
8120 p->lex.strterm->flags |= STRTERM_HEREDOC;
8121 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8122 here->offset = offset;
8123 here->sourceline = p->ruby_sourceline;
8124 here->length = (int)len;
8125 here->quote = quote;
8126 here->func = func;
8127
8128 token_flush(p);
8129 p->heredoc_indent = indent;
8130 p->heredoc_line_indent = 0;
8131 return token;
8132}
8133
8134static void
8135heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8136{
8137 VALUE line;
8138
8139 p->lex.strterm = 0;
8140 line = here->lastline;
8141 p->lex.lastline = line;
8142 p->lex.pbeg = RSTRING_PTR(line);
8143 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8144 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8145 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8146 p->heredoc_end = p->ruby_sourceline;
8147 p->ruby_sourceline = (int)here->sourceline;
8148 if (p->eofp) p->lex.nextline = Qnil;
8149 p->eofp = 0;
8150}
8151
8152static int
8153dedent_string(VALUE string, int width)
8154{
8155 char *str;
8156 long len;
8157 int i, col = 0;
8158
8159 RSTRING_GETMEM(string, str, len);
8160 for (i = 0; i < len && col < width; i++) {
8161 if (str[i] == ' ') {
8162 col++;
8163 }
8164 else if (str[i] == '\t') {
8165 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8166 if (n > width) break;
8167 col = n;
8168 }
8169 else {
8170 break;
8171 }
8172 }
8173 if (!i) return 0;
8174 rb_str_modify(string);
8175 str = RSTRING_PTR(string);
8176 if (RSTRING_LEN(string) != len)
8177 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8178 MEMMOVE(str, str + i, char, len - i);
8179 rb_str_set_len(string, len - i);
8180 return i;
8181}
8182
8183#ifndef RIPPER
8184static NODE *
8185heredoc_dedent(struct parser_params *p, NODE *root)
8186{
8187 NODE *node, *str_node, *prev_node;
8188 int indent = p->heredoc_indent;
8189 VALUE prev_lit = 0;
8190
8191 if (indent <= 0) return root;
8192 p->heredoc_indent = 0;
8193 if (!root) return root;
8194
8195 prev_node = node = str_node = root;
8196 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
8197
8198 while (str_node) {
8199 VALUE lit = str_node->nd_lit;
8200 if (str_node->flags & NODE_FL_NEWLINE) {
8201 dedent_string(lit, indent);
8202 }
8203 if (!prev_lit) {
8204 prev_lit = lit;
8205 }
8206 else if (!literal_concat0(p, prev_lit, lit)) {
8207 return 0;
8208 }
8209 else {
8210 NODE *end = node->nd_end;
8211 node = prev_node->nd_next = node->nd_next;
8212 if (!node) {
8213 if (nd_type_p(prev_node, NODE_DSTR))
8214 nd_set_type(prev_node, NODE_STR);
8215 break;
8216 }
8217 node->nd_end = end;
8218 goto next_str;
8219 }
8220
8221 str_node = 0;
8222 while ((node = (prev_node = node)->nd_next) != 0) {
8223 next_str:
8224 if (!nd_type_p(node, NODE_LIST)) break;
8225 if ((str_node = node->nd_head) != 0) {
8226 enum node_type type = nd_type(str_node);
8227 if (type == NODE_STR || type == NODE_DSTR) break;
8228 prev_lit = 0;
8229 str_node = 0;
8230 }
8231 }
8232 }
8233 return root;
8234}
8235#else /* RIPPER */
8236static VALUE
8237heredoc_dedent(struct parser_params *p, VALUE array)
8238{
8239 int indent = p->heredoc_indent;
8240
8241 if (indent <= 0) return array;
8242 p->heredoc_indent = 0;
8243 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8244 return array;
8245}
8246
8247/*
8248 * call-seq:
8249 * Ripper.dedent_string(input, width) -> Integer
8250 *
8251 * USE OF RIPPER LIBRARY ONLY.
8252 *
8253 * Strips up to +width+ leading whitespaces from +input+,
8254 * and returns the stripped column width.
8255 */
8256static VALUE
8257parser_dedent_string(VALUE self, VALUE input, VALUE width)
8258{
8259 int wid, col;
8260
8261 StringValue(input);
8262 wid = NUM2UINT(width);
8263 col = dedent_string(input, wid);
8264 return INT2NUM(col);
8265}
8266#endif
8267
8268static int
8269whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8270{
8271 const char *ptr = p->lex.pbeg;
8272 long n;
8273
8274 if (indent) {
8275 while (*ptr && ISSPACE(*ptr)) ptr++;
8276 }
8277 n = p->lex.pend - (ptr + len);
8278 if (n < 0) return FALSE;
8279 if (n > 0 && ptr[len] != '\n') {
8280 if (ptr[len] != '\r') return FALSE;
8281 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
8282 }
8283 return strncmp(eos, ptr, len) == 0;
8284}
8285
8286static int
8287word_match_p(struct parser_params *p, const char *word, long len)
8288{
8289 if (strncmp(p->lex.pcur, word, len)) return 0;
8290 if (p->lex.pcur + len == p->lex.pend) return 1;
8291 int c = (unsigned char)p->lex.pcur[len];
8292 if (ISSPACE(c)) return 1;
8293 switch (c) {
8294 case '\0': case '\004': case '\032': return 1;
8295 }
8296 return 0;
8297}
8298
8299#define NUM_SUFFIX_R (1<<0)
8300#define NUM_SUFFIX_I (1<<1)
8301#define NUM_SUFFIX_ALL 3
8302
8303static int
8304number_literal_suffix(struct parser_params *p, int mask)
8305{
8306 int c, result = 0;
8307 const char *lastp = p->lex.pcur;
8308
8309 while ((c = nextc(p)) != -1) {
8310 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8311 result |= (mask & NUM_SUFFIX_I);
8312 mask &= ~NUM_SUFFIX_I;
8313 /* r after i, rational of complex is disallowed */
8314 mask &= ~NUM_SUFFIX_R;
8315 continue;
8316 }
8317 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8318 result |= (mask & NUM_SUFFIX_R);
8319 mask &= ~NUM_SUFFIX_R;
8320 continue;
8321 }
8322 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8323 p->lex.pcur = lastp;
8324 literal_flush(p, p->lex.pcur);
8325 return 0;
8326 }
8327 pushback(p, c);
8328 break;
8329 }
8330 return result;
8331}
8332
8333static enum yytokentype
8334set_number_literal(struct parser_params *p, VALUE v,
8335 enum yytokentype type, int suffix)
8336{
8337 if (suffix & NUM_SUFFIX_I) {
8338 v = rb_complex_raw(INT2FIX(0), v);
8339 type = tIMAGINARY;
8340 }
8341 set_yylval_literal(v);
8342 SET_LEX_STATE(EXPR_END);
8343 return type;
8344}
8345
8346static enum yytokentype
8347set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8348{
8349 enum yytokentype type = tINTEGER;
8350 if (suffix & NUM_SUFFIX_R) {
8351 v = rb_rational_raw1(v);
8352 type = tRATIONAL;
8353 }
8354 return set_number_literal(p, v, type, suffix);
8355}
8356
8357#ifdef RIPPER
8358static void
8359dispatch_heredoc_end(struct parser_params *p)
8360{
8361 VALUE str;
8362 if (has_delayed_token(p))
8363 dispatch_delayed_token(p, tSTRING_CONTENT);
8364 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8365 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8366 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8367 lex_goto_eol(p);
8368 token_flush(p);
8369}
8370
8371#else
8372#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8373static void
8374parser_dispatch_heredoc_end(struct parser_params *p, int line)
8375{
8376 if (has_delayed_token(p))
8377 dispatch_delayed_token(p, tSTRING_CONTENT);
8378
8379 if (p->keep_tokens) {
8380 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8381 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8382 parser_append_tokens(p, str, tHEREDOC_END, line);
8383 }
8384
8385 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8386 lex_goto_eol(p);
8387 token_flush(p);
8388}
8389#endif
8390
8391static enum yytokentype
8392here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8393{
8394 int c, func, indent = 0;
8395 const char *eos, *ptr, *ptr_end;
8396 long len;
8397 VALUE str = 0;
8398 rb_encoding *enc = p->enc;
8399 rb_encoding *base_enc = 0;
8400 int bol;
8401
8402 eos = RSTRING_PTR(here->lastline) + here->offset;
8403 len = here->length;
8404 indent = (func = here->func) & STR_FUNC_INDENT;
8405
8406 if ((c = nextc(p)) == -1) {
8407 error:
8408#ifdef RIPPER
8409 if (!has_delayed_token(p)) {
8410 dispatch_scan_event(p, tSTRING_CONTENT);
8411 }
8412 else {
8413 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
8414 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
8415 int cr = ENC_CODERANGE_UNKNOWN;
8416 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
8417 if (cr != ENC_CODERANGE_7BIT &&
8418 rb_is_usascii_enc(p->enc) &&
8419 enc != rb_utf8_encoding()) {
8420 enc = rb_ascii8bit_encoding();
8421 }
8422 }
8423 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8424 }
8425 dispatch_delayed_token(p, tSTRING_CONTENT);
8426 }
8427 lex_goto_eol(p);
8428#endif
8429 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8430 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
8431 (int)len, eos);
8432 token_flush(p);
8433 p->lex.strterm = 0;
8434 SET_LEX_STATE(EXPR_END);
8435 return tSTRING_END;
8436 }
8437 bol = was_bol(p);
8438 if (!bol) {
8439 /* not beginning of line, cannot be the terminator */
8440 }
8441 else if (p->heredoc_line_indent == -1) {
8442 /* `heredoc_line_indent == -1` means
8443 * - "after an interpolation in the same line", or
8444 * - "in a continuing line"
8445 */
8446 p->heredoc_line_indent = 0;
8447 }
8448 else if (whole_match_p(p, eos, len, indent)) {
8449 dispatch_heredoc_end(p);
8450 restore:
8451 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8452 token_flush(p);
8453 p->lex.strterm = 0;
8454 SET_LEX_STATE(EXPR_END);
8455 return tSTRING_END;
8456 }
8457
8458 if (!(func & STR_FUNC_EXPAND)) {
8459 do {
8460 ptr = RSTRING_PTR(p->lex.lastline);
8461 ptr_end = p->lex.pend;
8462 if (ptr_end > ptr) {
8463 switch (ptr_end[-1]) {
8464 case '\n':
8465 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
8466 ptr_end++;
8467 break;
8468 }
8469 case '\r':
8470 --ptr_end;
8471 }
8472 }
8473
8474 if (p->heredoc_indent > 0) {
8475 long i = 0;
8476 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
8477 i++;
8478 p->heredoc_line_indent = 0;
8479 }
8480
8481 if (str)
8482 rb_str_cat(str, ptr, ptr_end - ptr);
8483 else
8484 str = STR_NEW(ptr, ptr_end - ptr);
8485 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
8486 lex_goto_eol(p);
8487 if (p->heredoc_indent > 0) {
8488 goto flush_str;
8489 }
8490 if (nextc(p) == -1) {
8491 if (str) {
8492 str = 0;
8493 }
8494 goto error;
8495 }
8496 } while (!whole_match_p(p, eos, len, indent));
8497 }
8498 else {
8499 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
8500 newtok(p);
8501 if (c == '#') {
8502 int t = parser_peek_variable_name(p);
8503 if (p->heredoc_line_indent != -1) {
8504 if (p->heredoc_indent > p->heredoc_line_indent) {
8505 p->heredoc_indent = p->heredoc_line_indent;
8506 }
8507 p->heredoc_line_indent = -1;
8508 }
8509 if (t) return t;
8510 tokadd(p, '#');
8511 c = nextc(p);
8512 }
8513 do {
8514 pushback(p, c);
8515 enc = p->enc;
8516 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
8517 if (p->eofp) goto error;
8518 goto restore;
8519 }
8520 if (c != '\n') {
8521 if (c == '\\') p->heredoc_line_indent = -1;
8522 flush:
8523 str = STR_NEW3(tok(p), toklen(p), enc, func);
8524 flush_str:
8525 set_yylval_str(str);
8526#ifndef RIPPER
8527 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8528#endif
8529 flush_string_content(p, enc);
8530 return tSTRING_CONTENT;
8531 }
8532 tokadd(p, nextc(p));
8533 if (p->heredoc_indent > 0) {
8534 lex_goto_eol(p);
8535 goto flush;
8536 }
8537 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8538 if ((c = nextc(p)) == -1) goto error;
8539 } while (!whole_match_p(p, eos, len, indent));
8540 str = STR_NEW3(tok(p), toklen(p), enc, func);
8541 }
8542 dispatch_heredoc_end(p);
8543#ifdef RIPPER
8544 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8545 yylval.val, str);
8546#endif
8547 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8548 token_flush(p);
8549 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8550 set_yylval_str(str);
8551#ifndef RIPPER
8552 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8553#endif
8554 return tSTRING_CONTENT;
8555}
8556
8557#include "lex.c"
8558
8559static int
8560arg_ambiguous(struct parser_params *p, char c)
8561{
8562#ifndef RIPPER
8563 if (c == '/') {
8564 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8565 }
8566 else {
8567 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8568 }
8569#else
8570 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8571#endif
8572 return TRUE;
8573}
8574
8575static ID
8576#ifndef RIPPER
8577formal_argument(struct parser_params *p, ID lhs)
8578#else
8579formal_argument(struct parser_params *p, VALUE lhs)
8580#endif
8581{
8582 ID id = get_id(lhs);
8583
8584 switch (id_type(id)) {
8585 case ID_LOCAL:
8586 break;
8587#ifndef RIPPER
8588# define ERR(mesg) yyerror0(mesg)
8589#else
8590# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8591#endif
8592 case ID_CONST:
8593 ERR("formal argument cannot be a constant");
8594 return 0;
8595 case ID_INSTANCE:
8596 ERR("formal argument cannot be an instance variable");
8597 return 0;
8598 case ID_GLOBAL:
8599 ERR("formal argument cannot be a global variable");
8600 return 0;
8601 case ID_CLASS:
8602 ERR("formal argument cannot be a class variable");
8603 return 0;
8604 default:
8605 ERR("formal argument must be local variable");
8606 return 0;
8607#undef ERR
8608 }
8609 shadowing_lvar(p, id);
8610 return lhs;
8611}
8612
8613static int
8614lvar_defined(struct parser_params *p, ID id)
8615{
8616 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8617}
8618
8619/* emacsen -*- hack */
8620static long
8621parser_encode_length(struct parser_params *p, const char *name, long len)
8622{
8623 long nlen;
8624
8625 if (len > 5 && name[nlen = len - 5] == '-') {
8626 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8627 return nlen;
8628 }
8629 if (len > 4 && name[nlen = len - 4] == '-') {
8630 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8631 return nlen;
8632 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8633 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8634 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8635 return nlen;
8636 }
8637 return len;
8638}
8639
8640static void
8641parser_set_encode(struct parser_params *p, const char *name)
8642{
8643 int idx = rb_enc_find_index(name);
8644 rb_encoding *enc;
8645 VALUE excargs[3];
8646
8647 if (idx < 0) {
8648 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8649 error:
8650 excargs[0] = rb_eArgError;
8651 excargs[2] = rb_make_backtrace();
8652 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8653 rb_exc_raise(rb_make_exception(3, excargs));
8654 }
8655 enc = rb_enc_from_index(idx);
8656 if (!rb_enc_asciicompat(enc)) {
8657 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8658 goto error;
8659 }
8660 p->enc = enc;
8661#ifndef RIPPER
8662 if (p->debug_lines) {
8663 VALUE lines = p->debug_lines;
8664 long i, n = RARRAY_LEN(lines);
8665 for (i = 0; i < n; ++i) {
8666 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8667 }
8668 }
8669#endif
8670}
8671
8672static int
8673comment_at_top(struct parser_params *p)
8674{
8675 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8676 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8677 while (ptr < ptr_end) {
8678 if (!ISSPACE(*ptr)) return 0;
8679 ptr++;
8680 }
8681 return 1;
8682}
8683
8684typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8685typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8686
8687static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8688
8689static void
8690magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8691{
8692 if (!comment_at_top(p)) {
8693 return;
8694 }
8695 parser_set_encode(p, val);
8696}
8697
8698static int
8699parser_get_bool(struct parser_params *p, const char *name, const char *val)
8700{
8701 switch (*val) {
8702 case 't': case 'T':
8703 if (STRCASECMP(val, "true") == 0) {
8704 return TRUE;
8705 }
8706 break;
8707 case 'f': case 'F':
8708 if (STRCASECMP(val, "false") == 0) {
8709 return FALSE;
8710 }
8711 break;
8712 }
8713 return parser_invalid_pragma_value(p, name, val);
8714}
8715
8716static int
8717parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8718{
8719 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8720 return -1;
8721}
8722
8723static void
8724parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8725{
8726 int b = parser_get_bool(p, name, val);
8727 if (b >= 0) p->token_info_enabled = b;
8728}
8729
8730static void
8731parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8732{
8733 int b;
8734
8735 if (p->token_seen) {
8736 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8737 return;
8738 }
8739
8740 b = parser_get_bool(p, name, val);
8741 if (b < 0) return;
8742
8743 if (!p->compile_option)
8744 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8745 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8746 RBOOL(b));
8747}
8748
8749static void
8750parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8751{
8752 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8753 if (*s == ' ' || *s == '\t') continue;
8754 if (*s == '#') break;
8755 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8756 return;
8757 }
8758
8759 switch (*val) {
8760 case 'n': case 'N':
8761 if (STRCASECMP(val, "none") == 0) {
8762 p->ctxt.shareable_constant_value = shareable_none;
8763 return;
8764 }
8765 break;
8766 case 'l': case 'L':
8767 if (STRCASECMP(val, "literal") == 0) {
8768 p->ctxt.shareable_constant_value = shareable_literal;
8769 return;
8770 }
8771 break;
8772 case 'e': case 'E':
8773 if (STRCASECMP(val, "experimental_copy") == 0) {
8774 p->ctxt.shareable_constant_value = shareable_copy;
8775 return;
8776 }
8777 if (STRCASECMP(val, "experimental_everything") == 0) {
8778 p->ctxt.shareable_constant_value = shareable_everything;
8779 return;
8780 }
8781 break;
8782 }
8783 parser_invalid_pragma_value(p, name, val);
8784}
8785
8786# if WARN_PAST_SCOPE
8787static void
8788parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8789{
8790 int b = parser_get_bool(p, name, val);
8791 if (b >= 0) p->past_scope_enabled = b;
8792}
8793# endif
8794
8795struct magic_comment {
8796 const char *name;
8797 rb_magic_comment_setter_t func;
8798 rb_magic_comment_length_t length;
8799};
8800
8801static const struct magic_comment magic_comments[] = {
8802 {"coding", magic_comment_encoding, parser_encode_length},
8803 {"encoding", magic_comment_encoding, parser_encode_length},
8804 {"frozen_string_literal", parser_set_compile_option_flag},
8805 {"shareable_constant_value", parser_set_shareable_constant_value},
8806 {"warn_indent", parser_set_token_info},
8807# if WARN_PAST_SCOPE
8808 {"warn_past_scope", parser_set_past_scope},
8809# endif
8810};
8811
8812static const char *
8813magic_comment_marker(const char *str, long len)
8814{
8815 long i = 2;
8816
8817 while (i < len) {
8818 switch (str[i]) {
8819 case '-':
8820 if (str[i-1] == '*' && str[i-2] == '-') {
8821 return str + i + 1;
8822 }
8823 i += 2;
8824 break;
8825 case '*':
8826 if (i + 1 >= len) return 0;
8827 if (str[i+1] != '-') {
8828 i += 4;
8829 }
8830 else if (str[i-1] != '-') {
8831 i += 2;
8832 }
8833 else {
8834 return str + i + 2;
8835 }
8836 break;
8837 default:
8838 i += 3;
8839 break;
8840 }
8841 }
8842 return 0;
8843}
8844
8845static int
8846parser_magic_comment(struct parser_params *p, const char *str, long len)
8847{
8848 int indicator = 0;
8849 VALUE name = 0, val = 0;
8850 const char *beg, *end, *vbeg, *vend;
8851#define str_copy(_s, _p, _n) ((_s) \
8852 ? (void)(rb_str_resize((_s), (_n)), \
8853 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8854 : (void)((_s) = STR_NEW((_p), (_n))))
8855
8856 if (len <= 7) return FALSE;
8857 if (!!(beg = magic_comment_marker(str, len))) {
8858 if (!(end = magic_comment_marker(beg, str + len - beg)))
8859 return FALSE;
8860 indicator = TRUE;
8861 str = beg;
8862 len = end - beg - 3;
8863 }
8864
8865 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8866 while (len > 0) {
8867 const struct magic_comment *mc = magic_comments;
8868 char *s;
8869 int i;
8870 long n = 0;
8871
8872 for (; len > 0 && *str; str++, --len) {
8873 switch (*str) {
8874 case '\'': case '"': case ':': case ';':
8875 continue;
8876 }
8877 if (!ISSPACE(*str)) break;
8878 }
8879 for (beg = str; len > 0; str++, --len) {
8880 switch (*str) {
8881 case '\'': case '"': case ':': case ';':
8882 break;
8883 default:
8884 if (ISSPACE(*str)) break;
8885 continue;
8886 }
8887 break;
8888 }
8889 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8890 if (!len) break;
8891 if (*str != ':') {
8892 if (!indicator) return FALSE;
8893 continue;
8894 }
8895
8896 do str++; while (--len > 0 && ISSPACE(*str));
8897 if (!len) break;
8898 if (*str == '"') {
8899 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8900 if (*str == '\\') {
8901 --len;
8902 ++str;
8903 }
8904 }
8905 vend = str;
8906 if (len) {
8907 --len;
8908 ++str;
8909 }
8910 }
8911 else {
8912 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8913 vend = str;
8914 }
8915 if (indicator) {
8916 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8917 }
8918 else {
8919 while (len > 0 && (ISSPACE(*str))) --len, str++;
8920 if (len) return FALSE;
8921 }
8922
8923 n = end - beg;
8924 str_copy(name, beg, n);
8925 s = RSTRING_PTR(name);
8926 for (i = 0; i < n; ++i) {
8927 if (s[i] == '-') s[i] = '_';
8928 }
8929 do {
8930 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8931 n = vend - vbeg;
8932 if (mc->length) {
8933 n = (*mc->length)(p, vbeg, n);
8934 }
8935 str_copy(val, vbeg, n);
8936 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8937 break;
8938 }
8939 } while (++mc < magic_comments + numberof(magic_comments));
8940#ifdef RIPPER
8941 str_copy(val, vbeg, vend - vbeg);
8942 dispatch2(magic_comment, name, val);
8943#endif
8944 }
8945
8946 return TRUE;
8947}
8948
8949static void
8950set_file_encoding(struct parser_params *p, const char *str, const char *send)
8951{
8952 int sep = 0;
8953 const char *beg = str;
8954 VALUE s;
8955
8956 for (;;) {
8957 if (send - str <= 6) return;
8958 switch (str[6]) {
8959 case 'C': case 'c': str += 6; continue;
8960 case 'O': case 'o': str += 5; continue;
8961 case 'D': case 'd': str += 4; continue;
8962 case 'I': case 'i': str += 3; continue;
8963 case 'N': case 'n': str += 2; continue;
8964 case 'G': case 'g': str += 1; continue;
8965 case '=': case ':':
8966 sep = 1;
8967 str += 6;
8968 break;
8969 default:
8970 str += 6;
8971 if (ISSPACE(*str)) break;
8972 continue;
8973 }
8974 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8975 sep = 0;
8976 }
8977 for (;;) {
8978 do {
8979 if (++str >= send) return;
8980 } while (ISSPACE(*str));
8981 if (sep) break;
8982 if (*str != '=' && *str != ':') return;
8983 sep = 1;
8984 str++;
8985 }
8986 beg = str;
8987 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8988 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8989 parser_set_encode(p, RSTRING_PTR(s));
8990 rb_str_resize(s, 0);
8991}
8992
8993static void
8994parser_prepare(struct parser_params *p)
8995{
8996 int c = nextc0(p, FALSE);
8997 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8998 switch (c) {
8999 case '#':
9000 if (peek(p, '!')) p->has_shebang = 1;
9001 break;
9002 case 0xef: /* UTF-8 BOM marker */
9003 if (p->lex.pend - p->lex.pcur >= 2 &&
9004 (unsigned char)p->lex.pcur[0] == 0xbb &&
9005 (unsigned char)p->lex.pcur[1] == 0xbf) {
9006 p->enc = rb_utf8_encoding();
9007 p->lex.pcur += 2;
9008#ifndef RIPPER
9009 if (p->debug_lines) {
9010 rb_enc_associate(p->lex.lastline, p->enc);
9011 }
9012#endif
9013 p->lex.pbeg = p->lex.pcur;
9014 return;
9015 }
9016 break;
9017 case EOF:
9018 return;
9019 }
9020 pushback(p, c);
9021 p->enc = rb_enc_get(p->lex.lastline);
9022}
9023
9024#ifndef RIPPER
9025#define ambiguous_operator(tok, op, syn) ( \
9026 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9027 rb_warning0("even though it seems like "syn""))
9028#else
9029#define ambiguous_operator(tok, op, syn) \
9030 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9031#endif
9032#define warn_balanced(tok, op, syn) ((void) \
9033 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9034 space_seen && !ISSPACE(c) && \
9035 (ambiguous_operator(tok, op, syn), 0)), \
9036 (enum yytokentype)(tok))
9037
9038static VALUE
9039parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9040{
9041 VALUE v;
9042 char *point = &str[seen_point];
9043 size_t fraclen = len-seen_point-1;
9044 memmove(point, point+1, fraclen+1);
9045 v = rb_cstr_to_inum(str, 10, FALSE);
9046 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9047}
9048
9049static enum yytokentype
9050no_digits(struct parser_params *p)
9051{
9052 yyerror0("numeric literal without digits");
9053 if (peek(p, '_')) nextc(p);
9054 /* dummy 0, for tUMINUS_NUM at numeric */
9055 return set_integer_literal(p, INT2FIX(0), 0);
9056}
9057
9058static enum yytokentype
9059parse_numeric(struct parser_params *p, int c)
9060{
9061 int is_float, seen_point, seen_e, nondigit;
9062 int suffix;
9063
9064 is_float = seen_point = seen_e = nondigit = 0;
9065 SET_LEX_STATE(EXPR_END);
9066 newtok(p);
9067 if (c == '-' || c == '+') {
9068 tokadd(p, c);
9069 c = nextc(p);
9070 }
9071 if (c == '0') {
9072 int start = toklen(p);
9073 c = nextc(p);
9074 if (c == 'x' || c == 'X') {
9075 /* hexadecimal */
9076 c = nextc(p);
9077 if (c != -1 && ISXDIGIT(c)) {
9078 do {
9079 if (c == '_') {
9080 if (nondigit) break;
9081 nondigit = c;
9082 continue;
9083 }
9084 if (!ISXDIGIT(c)) break;
9085 nondigit = 0;
9086 tokadd(p, c);
9087 } while ((c = nextc(p)) != -1);
9088 }
9089 pushback(p, c);
9090 tokfix(p);
9091 if (toklen(p) == start) {
9092 return no_digits(p);
9093 }
9094 else if (nondigit) goto trailing_uc;
9095 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9096 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9097 }
9098 if (c == 'b' || c == 'B') {
9099 /* binary */
9100 c = nextc(p);
9101 if (c == '0' || c == '1') {
9102 do {
9103 if (c == '_') {
9104 if (nondigit) break;
9105 nondigit = c;
9106 continue;
9107 }
9108 if (c != '0' && c != '1') break;
9109 nondigit = 0;
9110 tokadd(p, c);
9111 } while ((c = nextc(p)) != -1);
9112 }
9113 pushback(p, c);
9114 tokfix(p);
9115 if (toklen(p) == start) {
9116 return no_digits(p);
9117 }
9118 else if (nondigit) goto trailing_uc;
9119 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9120 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9121 }
9122 if (c == 'd' || c == 'D') {
9123 /* decimal */
9124 c = nextc(p);
9125 if (c != -1 && ISDIGIT(c)) {
9126 do {
9127 if (c == '_') {
9128 if (nondigit) break;
9129 nondigit = c;
9130 continue;
9131 }
9132 if (!ISDIGIT(c)) break;
9133 nondigit = 0;
9134 tokadd(p, c);
9135 } while ((c = nextc(p)) != -1);
9136 }
9137 pushback(p, c);
9138 tokfix(p);
9139 if (toklen(p) == start) {
9140 return no_digits(p);
9141 }
9142 else if (nondigit) goto trailing_uc;
9143 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9144 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9145 }
9146 if (c == '_') {
9147 /* 0_0 */
9148 goto octal_number;
9149 }
9150 if (c == 'o' || c == 'O') {
9151 /* prefixed octal */
9152 c = nextc(p);
9153 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9154 return no_digits(p);
9155 }
9156 }
9157 if (c >= '0' && c <= '7') {
9158 /* octal */
9159 octal_number:
9160 do {
9161 if (c == '_') {
9162 if (nondigit) break;
9163 nondigit = c;
9164 continue;
9165 }
9166 if (c < '0' || c > '9') break;
9167 if (c > '7') goto invalid_octal;
9168 nondigit = 0;
9169 tokadd(p, c);
9170 } while ((c = nextc(p)) != -1);
9171 if (toklen(p) > start) {
9172 pushback(p, c);
9173 tokfix(p);
9174 if (nondigit) goto trailing_uc;
9175 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9176 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9177 }
9178 if (nondigit) {
9179 pushback(p, c);
9180 goto trailing_uc;
9181 }
9182 }
9183 if (c > '7' && c <= '9') {
9184 invalid_octal:
9185 yyerror0("Invalid octal digit");
9186 }
9187 else if (c == '.' || c == 'e' || c == 'E') {
9188 tokadd(p, '0');
9189 }
9190 else {
9191 pushback(p, c);
9192 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9193 return set_integer_literal(p, INT2FIX(0), suffix);
9194 }
9195 }
9196
9197 for (;;) {
9198 switch (c) {
9199 case '0': case '1': case '2': case '3': case '4':
9200 case '5': case '6': case '7': case '8': case '9':
9201 nondigit = 0;
9202 tokadd(p, c);
9203 break;
9204
9205 case '.':
9206 if (nondigit) goto trailing_uc;
9207 if (seen_point || seen_e) {
9208 goto decode_num;
9209 }
9210 else {
9211 int c0 = nextc(p);
9212 if (c0 == -1 || !ISDIGIT(c0)) {
9213 pushback(p, c0);
9214 goto decode_num;
9215 }
9216 c = c0;
9217 }
9218 seen_point = toklen(p);
9219 tokadd(p, '.');
9220 tokadd(p, c);
9221 is_float++;
9222 nondigit = 0;
9223 break;
9224
9225 case 'e':
9226 case 'E':
9227 if (nondigit) {
9228 pushback(p, c);
9229 c = nondigit;
9230 goto decode_num;
9231 }
9232 if (seen_e) {
9233 goto decode_num;
9234 }
9235 nondigit = c;
9236 c = nextc(p);
9237 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9238 pushback(p, c);
9239 nondigit = 0;
9240 goto decode_num;
9241 }
9242 tokadd(p, nondigit);
9243 seen_e++;
9244 is_float++;
9245 tokadd(p, c);
9246 nondigit = (c == '-' || c == '+') ? c : 0;
9247 break;
9248
9249 case '_': /* `_' in number just ignored */
9250 if (nondigit) goto decode_num;
9251 nondigit = c;
9252 break;
9253
9254 default:
9255 goto decode_num;
9256 }
9257 c = nextc(p);
9258 }
9259
9260 decode_num:
9261 pushback(p, c);
9262 if (nondigit) {
9263 trailing_uc:
9264 literal_flush(p, p->lex.pcur - 1);
9265 YYLTYPE loc = RUBY_INIT_YYLLOC();
9266 compile_error(p, "trailing `%c' in number", nondigit);
9267 parser_show_error_line(p, &loc);
9268 }
9269 tokfix(p);
9270 if (is_float) {
9271 enum yytokentype type = tFLOAT;
9272 VALUE v;
9273
9274 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9275 if (suffix & NUM_SUFFIX_R) {
9276 type = tRATIONAL;
9277 v = parse_rational(p, tok(p), toklen(p), seen_point);
9278 }
9279 else {
9280 double d = strtod(tok(p), 0);
9281 if (errno == ERANGE) {
9282 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9283 errno = 0;
9284 }
9285 v = DBL2NUM(d);
9286 }
9287 return set_number_literal(p, v, type, suffix);
9288 }
9289 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9290 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9291}
9292
9293static enum yytokentype
9294parse_qmark(struct parser_params *p, int space_seen)
9295{
9296 rb_encoding *enc;
9297 register int c;
9298 VALUE lit;
9299
9300 if (IS_END()) {
9301 SET_LEX_STATE(EXPR_VALUE);
9302 return '?';
9303 }
9304 c = nextc(p);
9305 if (c == -1) {
9306 compile_error(p, "incomplete character syntax");
9307 return 0;
9308 }
9309 if (rb_enc_isspace(c, p->enc)) {
9310 if (!IS_ARG()) {
9311 int c2 = escaped_control_code(c);
9312 if (c2) {
9313 WARN_SPACE_CHAR(c2, "?");
9314 }
9315 }
9316 ternary:
9317 pushback(p, c);
9318 SET_LEX_STATE(EXPR_VALUE);
9319 return '?';
9320 }
9321 newtok(p);
9322 enc = p->enc;
9323 if (!parser_isascii(p)) {
9324 if (tokadd_mbchar(p, c) == -1) return 0;
9325 }
9326 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9327 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
9328 if (space_seen) {
9329 const char *start = p->lex.pcur - 1, *ptr = start;
9330 do {
9331 int n = parser_precise_mbclen(p, ptr);
9332 if (n < 0) return -1;
9333 ptr += n;
9334 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
9335 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9336 " a conditional operator, put a space after `?'",
9337 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9338 }
9339 goto ternary;
9340 }
9341 else if (c == '\\') {
9342 if (peek(p, 'u')) {
9343 nextc(p);
9344 enc = rb_utf8_encoding();
9345 tokadd_utf8(p, &enc, -1, 0, 0);
9346 }
9347 else if (!ISASCII(c = peekc(p))) {
9348 nextc(p);
9349 if (tokadd_mbchar(p, c) == -1) return 0;
9350 }
9351 else {
9352 c = read_escape(p, 0, &enc);
9353 tokadd(p, c);
9354 }
9355 }
9356 else {
9357 tokadd(p, c);
9358 }
9359 tokfix(p);
9360 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9361 set_yylval_str(lit);
9362 SET_LEX_STATE(EXPR_END);
9363 return tCHAR;
9364}
9365
9366static enum yytokentype
9367parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9368{
9369 register int c;
9370 const char *ptok = p->lex.pcur;
9371
9372 if (IS_BEG()) {
9373 int term;
9374 int paren;
9375
9376 c = nextc(p);
9377 quotation:
9378 if (c == -1) goto unterminated;
9379 if (!ISALNUM(c)) {
9380 term = c;
9381 if (!ISASCII(c)) goto unknown;
9382 c = 'Q';
9383 }
9384 else {
9385 term = nextc(p);
9386 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9387 unknown:
9388 pushback(p, term);
9389 c = parser_precise_mbclen(p, p->lex.pcur);
9390 if (c < 0) return 0;
9391 p->lex.pcur += c;
9392 yyerror0("unknown type of %string");
9393 return 0;
9394 }
9395 }
9396 if (term == -1) {
9397 unterminated:
9398 compile_error(p, "unterminated quoted string meets end of file");
9399 return 0;
9400 }
9401 paren = term;
9402 if (term == '(') term = ')';
9403 else if (term == '[') term = ']';
9404 else if (term == '{') term = '}';
9405 else if (term == '<') term = '>';
9406 else paren = 0;
9407
9408 p->lex.ptok = ptok-1;
9409 switch (c) {
9410 case 'Q':
9411 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9412 return tSTRING_BEG;
9413
9414 case 'q':
9415 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
9416 return tSTRING_BEG;
9417
9418 case 'W':
9419 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9420 return tWORDS_BEG;
9421
9422 case 'w':
9423 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9424 return tQWORDS_BEG;
9425
9426 case 'I':
9427 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9428 return tSYMBOLS_BEG;
9429
9430 case 'i':
9431 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9432 return tQSYMBOLS_BEG;
9433
9434 case 'x':
9435 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
9436 return tXSTRING_BEG;
9437
9438 case 'r':
9439 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
9440 return tREGEXP_BEG;
9441
9442 case 's':
9443 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
9444 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
9445 return tSYMBEG;
9446
9447 default:
9448 yyerror0("unknown type of %string");
9449 return 0;
9450 }
9451 }
9452 if ((c = nextc(p)) == '=') {
9453 set_yylval_id('%');
9454 SET_LEX_STATE(EXPR_BEG);
9455 return tOP_ASGN;
9456 }
9457 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
9458 goto quotation;
9459 }
9460 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9461 pushback(p, c);
9462 return warn_balanced('%', "%%", "string literal");
9463}
9464
9465static int
9466tokadd_ident(struct parser_params *p, int c)
9467{
9468 do {
9469 if (tokadd_mbchar(p, c) == -1) return -1;
9470 c = nextc(p);
9471 } while (parser_is_identchar(p));
9472 pushback(p, c);
9473 return 0;
9474}
9475
9476static ID
9477tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
9478{
9479 ID ident = TOK_INTERN();
9480
9481 set_yylval_name(ident);
9482
9483 return ident;
9484}
9485
9486static int
9487parse_numvar(struct parser_params *p)
9488{
9489 size_t len;
9490 int overflow;
9491 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
9492 const unsigned long nth_ref_max =
9493 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
9494 /* NTH_REF is left-shifted to be ORed with back-ref flag and
9495 * turned into a Fixnum, in compile.c */
9496
9497 if (overflow || n > nth_ref_max) {
9498 /* compile_error()? */
9499 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
9500 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
9501 }
9502 else {
9503 return (int)n;
9504 }
9505}
9506
9507static enum yytokentype
9508parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
9509{
9510 const char *ptr = p->lex.pcur;
9511 register int c;
9512
9513 SET_LEX_STATE(EXPR_END);
9514 p->lex.ptok = ptr - 1; /* from '$' */
9515 newtok(p);
9516 c = nextc(p);
9517 switch (c) {
9518 case '_': /* $_: last read line string */
9519 c = nextc(p);
9520 if (parser_is_identchar(p)) {
9521 tokadd(p, '$');
9522 tokadd(p, '_');
9523 break;
9524 }
9525 pushback(p, c);
9526 c = '_';
9527 /* fall through */
9528 case '~': /* $~: match-data */
9529 case '*': /* $*: argv */
9530 case '$': /* $$: pid */
9531 case '?': /* $?: last status */
9532 case '!': /* $!: error string */
9533 case '@': /* $@: error position */
9534 case '/': /* $/: input record separator */
9535 case '\\': /* $\: output record separator */
9536 case ';': /* $;: field separator */
9537 case ',': /* $,: output field separator */
9538 case '.': /* $.: last read line number */
9539 case '=': /* $=: ignorecase */
9540 case ':': /* $:: load path */
9541 case '<': /* $<: reading filename */
9542 case '>': /* $>: default output handle */
9543 case '\"': /* $": already loaded files */
9544 tokadd(p, '$');
9545 tokadd(p, c);
9546 goto gvar;
9547
9548 case '-':
9549 tokadd(p, '$');
9550 tokadd(p, c);
9551 c = nextc(p);
9552 if (parser_is_identchar(p)) {
9553 if (tokadd_mbchar(p, c) == -1) return 0;
9554 }
9555 else {
9556 pushback(p, c);
9557 pushback(p, '-');
9558 return '$';
9559 }
9560 gvar:
9561 set_yylval_name(TOK_INTERN());
9562 return tGVAR;
9563
9564 case '&': /* $&: last match */
9565 case '`': /* $`: string before last match */
9566 case '\'': /* $': string after last match */
9567 case '+': /* $+: string matches last paren. */
9568 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9569 tokadd(p, '$');
9570 tokadd(p, c);
9571 goto gvar;
9572 }
9573 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9574 return tBACK_REF;
9575
9576 case '1': case '2': case '3':
9577 case '4': case '5': case '6':
9578 case '7': case '8': case '9':
9579 tokadd(p, '$');
9580 do {
9581 tokadd(p, c);
9582 c = nextc(p);
9583 } while (c != -1 && ISDIGIT(c));
9584 pushback(p, c);
9585 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9586 tokfix(p);
9587 c = parse_numvar(p);
9588 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9589 return tNTH_REF;
9590
9591 default:
9592 if (!parser_is_identchar(p)) {
9593 YYLTYPE loc = RUBY_INIT_YYLLOC();
9594 if (c == -1 || ISSPACE(c)) {
9595 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9596 }
9597 else {
9598 pushback(p, c);
9599 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9600 }
9601 parser_show_error_line(p, &loc);
9602 set_yylval_noname();
9603 return tGVAR;
9604 }
9605 /* fall through */
9606 case '0':
9607 tokadd(p, '$');
9608 }
9609
9610 if (tokadd_ident(p, c)) return 0;
9611 SET_LEX_STATE(EXPR_END);
9612 tokenize_ident(p, last_state);
9613 return tGVAR;
9614}
9615
9616#ifndef RIPPER
9617static bool
9618parser_numbered_param(struct parser_params *p, int n)
9619{
9620 if (n < 0) return false;
9621
9622 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9623 return false;
9624 }
9625 if (p->max_numparam == ORDINAL_PARAM) {
9626 compile_error(p, "ordinary parameter is defined");
9627 return false;
9628 }
9629 struct vtable *args = p->lvtbl->args;
9630 if (p->max_numparam < n) {
9631 p->max_numparam = n;
9632 }
9633 while (n > args->pos) {
9634 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9635 }
9636 return true;
9637}
9638#endif
9639
9640static enum yytokentype
9641parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9642{
9643 const char *ptr = p->lex.pcur;
9644 enum yytokentype result = tIVAR;
9645 register int c = nextc(p);
9646 YYLTYPE loc;
9647
9648 p->lex.ptok = ptr - 1; /* from '@' */
9649 newtok(p);
9650 tokadd(p, '@');
9651 if (c == '@') {
9652 result = tCVAR;
9653 tokadd(p, '@');
9654 c = nextc(p);
9655 }
9656 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9657 if (c == -1 || !parser_is_identchar(p)) {
9658 pushback(p, c);
9659 RUBY_SET_YYLLOC(loc);
9660 if (result == tIVAR) {
9661 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9662 }
9663 else {
9664 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9665 }
9666 parser_show_error_line(p, &loc);
9667 set_yylval_noname();
9668 SET_LEX_STATE(EXPR_END);
9669 return result;
9670 }
9671 else if (ISDIGIT(c)) {
9672 pushback(p, c);
9673 RUBY_SET_YYLLOC(loc);
9674 if (result == tIVAR) {
9675 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9676 }
9677 else {
9678 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9679 }
9680 parser_show_error_line(p, &loc);
9681 set_yylval_noname();
9682 SET_LEX_STATE(EXPR_END);
9683 return result;
9684 }
9685
9686 if (tokadd_ident(p, c)) return 0;
9687 tokenize_ident(p, last_state);
9688 return result;
9689}
9690
9691static enum yytokentype
9692parse_ident(struct parser_params *p, int c, int cmd_state)
9693{
9694 enum yytokentype result;
9695 int mb = ENC_CODERANGE_7BIT;
9696 const enum lex_state_e last_state = p->lex.state;
9697 ID ident;
9698 int enforce_keyword_end = 0;
9699
9700 do {
9701 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9702 if (tokadd_mbchar(p, c) == -1) return 0;
9703 c = nextc(p);
9704 } while (parser_is_identchar(p));
9705 if ((c == '!' || c == '?') && !peek(p, '=')) {
9706 result = tFID;
9707 tokadd(p, c);
9708 }
9709 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9710 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9711 result = tIDENTIFIER;
9712 tokadd(p, c);
9713 }
9714 else {
9715 result = tCONSTANT; /* assume provisionally */
9716 pushback(p, c);
9717 }
9718 tokfix(p);
9719
9720 if (IS_LABEL_POSSIBLE()) {
9721 if (IS_LABEL_SUFFIX(0)) {
9722 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9723 nextc(p);
9724 set_yylval_name(TOK_INTERN());
9725 return tLABEL;
9726 }
9727 }
9728
9729#ifndef RIPPER
9730 if (!NIL_P(peek_end_expect_token_locations(p))) {
9731 VALUE end_loc;
9732 int lineno, column;
9733 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
9734
9735 end_loc = peek_end_expect_token_locations(p);
9736 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
9737 column = NUM2INT(rb_ary_entry(end_loc, 1));
9738
9739 if (p->debug) {
9740 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
9741 p->ruby_sourceline, beg_pos, lineno, column);
9742 }
9743
9744 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
9745 const struct kwtable *kw;
9746
9747 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
9748 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
9749 enforce_keyword_end = 1;
9750 }
9751 }
9752 }
9753#endif
9754
9755 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
9756 const struct kwtable *kw;
9757
9758 /* See if it is a reserved word. */
9759 kw = rb_reserved_word(tok(p), toklen(p));
9760 if (kw) {
9761 enum lex_state_e state = p->lex.state;
9762 if (IS_lex_state_for(state, EXPR_FNAME)) {
9763 SET_LEX_STATE(EXPR_ENDFN);
9764 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9765 return kw->id[0];
9766 }
9767 SET_LEX_STATE(kw->state);
9768 if (IS_lex_state(EXPR_BEG)) {
9769 p->command_start = TRUE;
9770 }
9771 if (kw->id[0] == keyword_do) {
9772 if (lambda_beginning_p()) {
9773 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9774 return keyword_do_LAMBDA;
9775 }
9776 if (COND_P()) return keyword_do_cond;
9777 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9778 return keyword_do_block;
9779 return keyword_do;
9780 }
9781 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
9782 return kw->id[0];
9783 else {
9784 if (kw->id[0] != kw->id[1])
9785 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9786 return kw->id[1];
9787 }
9788 }
9789 }
9790
9791 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9792 if (cmd_state) {
9793 SET_LEX_STATE(EXPR_CMDARG);
9794 }
9795 else {
9796 SET_LEX_STATE(EXPR_ARG);
9797 }
9798 }
9799 else if (p->lex.state == EXPR_FNAME) {
9800 SET_LEX_STATE(EXPR_ENDFN);
9801 }
9802 else {
9803 SET_LEX_STATE(EXPR_END);
9804 }
9805
9806 ident = tokenize_ident(p, last_state);
9807 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9808 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9809 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9810 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
9811 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9812 }
9813 return result;
9814}
9815
9816static void
9817warn_cr(struct parser_params *p)
9818{
9819 if (!p->cr_seen) {
9820 p->cr_seen = TRUE;
9821 /* carried over with p->lex.nextline for nextc() */
9822 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9823 }
9824}
9825
9826static enum yytokentype
9827parser_yylex(struct parser_params *p)
9828{
9829 register int c;
9830 int space_seen = 0;
9831 int cmd_state;
9832 int label;
9833 enum lex_state_e last_state;
9834 int fallthru = FALSE;
9835 int token_seen = p->token_seen;
9836
9837 if (p->lex.strterm) {
9838 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9839 token_flush(p);
9840 return here_document(p, &p->lex.strterm->u.heredoc);
9841 }
9842 else {
9843 token_flush(p);
9844 return parse_string(p, &p->lex.strterm->u.literal);
9845 }
9846 }
9847 cmd_state = p->command_start;
9848 p->command_start = FALSE;
9849 p->token_seen = TRUE;
9850#ifndef RIPPER
9851 token_flush(p);
9852#endif
9853 retry:
9854 last_state = p->lex.state;
9855 switch (c = nextc(p)) {
9856 case '\0': /* NUL */
9857 case '\004': /* ^D */
9858 case '\032': /* ^Z */
9859 case -1: /* end of script. */
9860 p->eofp = 1;
9861#ifndef RIPPER
9862 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
9863 pop_end_expect_token_locations(p);
9864 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
9865 return tDUMNY_END;
9866 }
9867#endif
9868 /* Set location for end-of-input because dispatch_scan_event is not called. */
9869 RUBY_SET_YYLLOC(*p->yylloc);
9870 return 0;
9871
9872 /* white spaces */
9873 case '\r':
9874 warn_cr(p);
9875 /* fall through */
9876 case ' ': case '\t': case '\f':
9877 case '\13': /* '\v' */
9878 space_seen = 1;
9879 while ((c = nextc(p))) {
9880 switch (c) {
9881 case '\r':
9882 warn_cr(p);
9883 /* fall through */
9884 case ' ': case '\t': case '\f':
9885 case '\13': /* '\v' */
9886 break;
9887 default:
9888 goto outofloop;
9889 }
9890 }
9891 outofloop:
9892 pushback(p, c);
9893 dispatch_scan_event(p, tSP);
9894#ifndef RIPPER
9895 token_flush(p);
9896#endif
9897 goto retry;
9898
9899 case '#': /* it's a comment */
9900 p->token_seen = token_seen;
9901 /* no magic_comment in shebang line */
9902 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9903 if (comment_at_top(p)) {
9904 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9905 }
9906 }
9907 lex_goto_eol(p);
9908 dispatch_scan_event(p, tCOMMENT);
9909 fallthru = TRUE;
9910 /* fall through */
9911 case '\n':
9912 p->token_seen = token_seen;
9913 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9914 !IS_lex_state(EXPR_LABELED));
9915 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9916 if (!fallthru) {
9917 dispatch_scan_event(p, tIGNORED_NL);
9918 }
9919 fallthru = FALSE;
9920 if (!c && p->ctxt.in_kwarg) {
9921 goto normal_newline;
9922 }
9923 goto retry;
9924 }
9925 while (1) {
9926 switch (c = nextc(p)) {
9927 case ' ': case '\t': case '\f': case '\r':
9928 case '\13': /* '\v' */
9929 space_seen = 1;
9930 break;
9931 case '#':
9932 pushback(p, c);
9933 if (space_seen) {
9934 dispatch_scan_event(p, tSP);
9935 token_flush(p);
9936 }
9937 goto retry;
9938 case '&':
9939 case '.': {
9940 dispatch_delayed_token(p, tIGNORED_NL);
9941 if (peek(p, '.') == (c == '&')) {
9942 pushback(p, c);
9943 dispatch_scan_event(p, tSP);
9944 goto retry;
9945 }
9946 }
9947 default:
9948 p->ruby_sourceline--;
9949 p->lex.nextline = p->lex.lastline;
9950 case -1: /* EOF no decrement*/
9951 lex_goto_eol(p);
9952 if (c != -1) {
9953 p->lex.ptok = p->lex.pcur;
9954 }
9955 goto normal_newline;
9956 }
9957 }
9958 normal_newline:
9959 p->command_start = TRUE;
9960 SET_LEX_STATE(EXPR_BEG);
9961 return '\n';
9962
9963 case '*':
9964 if ((c = nextc(p)) == '*') {
9965 if ((c = nextc(p)) == '=') {
9966 set_yylval_id(idPow);
9967 SET_LEX_STATE(EXPR_BEG);
9968 return tOP_ASGN;
9969 }
9970 pushback(p, c);
9971 if (IS_SPCARG(c)) {
9972 rb_warning0("`**' interpreted as argument prefix");
9973 c = tDSTAR;
9974 }
9975 else if (IS_BEG()) {
9976 c = tDSTAR;
9977 }
9978 else {
9979 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9980 }
9981 }
9982 else {
9983 if (c == '=') {
9984 set_yylval_id('*');
9985 SET_LEX_STATE(EXPR_BEG);
9986 return tOP_ASGN;
9987 }
9988 pushback(p, c);
9989 if (IS_SPCARG(c)) {
9990 rb_warning0("`*' interpreted as argument prefix");
9991 c = tSTAR;
9992 }
9993 else if (IS_BEG()) {
9994 c = tSTAR;
9995 }
9996 else {
9997 c = warn_balanced('*', "*", "argument prefix");
9998 }
9999 }
10000 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10001 return c;
10002
10003 case '!':
10004 c = nextc(p);
10005 if (IS_AFTER_OPERATOR()) {
10006 SET_LEX_STATE(EXPR_ARG);
10007 if (c == '@') {
10008 return '!';
10009 }
10010 }
10011 else {
10012 SET_LEX_STATE(EXPR_BEG);
10013 }
10014 if (c == '=') {
10015 return tNEQ;
10016 }
10017 if (c == '~') {
10018 return tNMATCH;
10019 }
10020 pushback(p, c);
10021 return '!';
10022
10023 case '=':
10024 if (was_bol(p)) {
10025 /* skip embedded rd document */
10026 if (word_match_p(p, "begin", 5)) {
10027 int first_p = TRUE;
10028
10029 lex_goto_eol(p);
10030 dispatch_scan_event(p, tEMBDOC_BEG);
10031 for (;;) {
10032 lex_goto_eol(p);
10033 if (!first_p) {
10034 dispatch_scan_event(p, tEMBDOC);
10035 }
10036 first_p = FALSE;
10037 c = nextc(p);
10038 if (c == -1) {
10039 compile_error(p, "embedded document meets end of file");
10040 return 0;
10041 }
10042 if (c == '=' && word_match_p(p, "end", 3)) {
10043 break;
10044 }
10045 pushback(p, c);
10046 }
10047 lex_goto_eol(p);
10048 dispatch_scan_event(p, tEMBDOC_END);
10049 goto retry;
10050 }
10051 }
10052
10053 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10054 if ((c = nextc(p)) == '=') {
10055 if ((c = nextc(p)) == '=') {
10056 return tEQQ;
10057 }
10058 pushback(p, c);
10059 return tEQ;
10060 }
10061 if (c == '~') {
10062 return tMATCH;
10063 }
10064 else if (c == '>') {
10065 return tASSOC;
10066 }
10067 pushback(p, c);
10068 return '=';
10069
10070 case '<':
10071 c = nextc(p);
10072 if (c == '<' &&
10073 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10074 !IS_END() &&
10075 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10076 int token = heredoc_identifier(p);
10077 if (token) return token < 0 ? 0 : token;
10078 }
10079 if (IS_AFTER_OPERATOR()) {
10080 SET_LEX_STATE(EXPR_ARG);
10081 }
10082 else {
10083 if (IS_lex_state(EXPR_CLASS))
10084 p->command_start = TRUE;
10085 SET_LEX_STATE(EXPR_BEG);
10086 }
10087 if (c == '=') {
10088 if ((c = nextc(p)) == '>') {
10089 return tCMP;
10090 }
10091 pushback(p, c);
10092 return tLEQ;
10093 }
10094 if (c == '<') {
10095 if ((c = nextc(p)) == '=') {
10096 set_yylval_id(idLTLT);
10097 SET_LEX_STATE(EXPR_BEG);
10098 return tOP_ASGN;
10099 }
10100 pushback(p, c);
10101 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10102 }
10103 pushback(p, c);
10104 return '<';
10105
10106 case '>':
10107 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10108 if ((c = nextc(p)) == '=') {
10109 return tGEQ;
10110 }
10111 if (c == '>') {
10112 if ((c = nextc(p)) == '=') {
10113 set_yylval_id(idGTGT);
10114 SET_LEX_STATE(EXPR_BEG);
10115 return tOP_ASGN;
10116 }
10117 pushback(p, c);
10118 return tRSHFT;
10119 }
10120 pushback(p, c);
10121 return '>';
10122
10123 case '"':
10124 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10125 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10126 p->lex.ptok = p->lex.pcur-1;
10127 return tSTRING_BEG;
10128
10129 case '`':
10130 if (IS_lex_state(EXPR_FNAME)) {
10131 SET_LEX_STATE(EXPR_ENDFN);
10132 return c;
10133 }
10134 if (IS_lex_state(EXPR_DOT)) {
10135 if (cmd_state)
10136 SET_LEX_STATE(EXPR_CMDARG);
10137 else
10138 SET_LEX_STATE(EXPR_ARG);
10139 return c;
10140 }
10141 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10142 return tXSTRING_BEG;
10143
10144 case '\'':
10145 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10146 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10147 p->lex.ptok = p->lex.pcur-1;
10148 return tSTRING_BEG;
10149
10150 case '?':
10151 return parse_qmark(p, space_seen);
10152
10153 case '&':
10154 if ((c = nextc(p)) == '&') {
10155 SET_LEX_STATE(EXPR_BEG);
10156 if ((c = nextc(p)) == '=') {
10157 set_yylval_id(idANDOP);
10158 SET_LEX_STATE(EXPR_BEG);
10159 return tOP_ASGN;
10160 }
10161 pushback(p, c);
10162 return tANDOP;
10163 }
10164 else if (c == '=') {
10165 set_yylval_id('&');
10166 SET_LEX_STATE(EXPR_BEG);
10167 return tOP_ASGN;
10168 }
10169 else if (c == '.') {
10170 set_yylval_id(idANDDOT);
10171 SET_LEX_STATE(EXPR_DOT);
10172 return tANDDOT;
10173 }
10174 pushback(p, c);
10175 if (IS_SPCARG(c)) {
10176 if ((c != ':') ||
10177 (c = peekc_n(p, 1)) == -1 ||
10178 !(c == '\'' || c == '"' ||
10179 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
10180 rb_warning0("`&' interpreted as argument prefix");
10181 }
10182 c = tAMPER;
10183 }
10184 else if (IS_BEG()) {
10185 c = tAMPER;
10186 }
10187 else {
10188 c = warn_balanced('&', "&", "argument prefix");
10189 }
10190 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10191 return c;
10192
10193 case '|':
10194 if ((c = nextc(p)) == '|') {
10195 SET_LEX_STATE(EXPR_BEG);
10196 if ((c = nextc(p)) == '=') {
10197 set_yylval_id(idOROP);
10198 SET_LEX_STATE(EXPR_BEG);
10199 return tOP_ASGN;
10200 }
10201 pushback(p, c);
10202 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10203 c = '|';
10204 pushback(p, '|');
10205 return c;
10206 }
10207 return tOROP;
10208 }
10209 if (c == '=') {
10210 set_yylval_id('|');
10211 SET_LEX_STATE(EXPR_BEG);
10212 return tOP_ASGN;
10213 }
10214 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10215 pushback(p, c);
10216 return '|';
10217
10218 case '+':
10219 c = nextc(p);
10220 if (IS_AFTER_OPERATOR()) {
10221 SET_LEX_STATE(EXPR_ARG);
10222 if (c == '@') {
10223 return tUPLUS;
10224 }
10225 pushback(p, c);
10226 return '+';
10227 }
10228 if (c == '=') {
10229 set_yylval_id('+');
10230 SET_LEX_STATE(EXPR_BEG);
10231 return tOP_ASGN;
10232 }
10233 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10234 SET_LEX_STATE(EXPR_BEG);
10235 pushback(p, c);
10236 if (c != -1 && ISDIGIT(c)) {
10237 return parse_numeric(p, '+');
10238 }
10239 return tUPLUS;
10240 }
10241 SET_LEX_STATE(EXPR_BEG);
10242 pushback(p, c);
10243 return warn_balanced('+', "+", "unary operator");
10244
10245 case '-':
10246 c = nextc(p);
10247 if (IS_AFTER_OPERATOR()) {
10248 SET_LEX_STATE(EXPR_ARG);
10249 if (c == '@') {
10250 return tUMINUS;
10251 }
10252 pushback(p, c);
10253 return '-';
10254 }
10255 if (c == '=') {
10256 set_yylval_id('-');
10257 SET_LEX_STATE(EXPR_BEG);
10258 return tOP_ASGN;
10259 }
10260 if (c == '>') {
10261 SET_LEX_STATE(EXPR_ENDFN);
10262 return tLAMBDA;
10263 }
10264 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10265 SET_LEX_STATE(EXPR_BEG);
10266 pushback(p, c);
10267 if (c != -1 && ISDIGIT(c)) {
10268 return tUMINUS_NUM;
10269 }
10270 return tUMINUS;
10271 }
10272 SET_LEX_STATE(EXPR_BEG);
10273 pushback(p, c);
10274 return warn_balanced('-', "-", "unary operator");
10275
10276 case '.': {
10277 int is_beg = IS_BEG();
10278 SET_LEX_STATE(EXPR_BEG);
10279 if ((c = nextc(p)) == '.') {
10280 if ((c = nextc(p)) == '.') {
10281 if (p->ctxt.in_argdef) {
10282 SET_LEX_STATE(EXPR_ENDARG);
10283 return tBDOT3;
10284 }
10285 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10286 rb_warn0("... at EOL, should be parenthesized?");
10287 }
10288 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10289 if (IS_lex_state_for(last_state, EXPR_LABEL))
10290 return tDOT3;
10291 }
10292 return is_beg ? tBDOT3 : tDOT3;
10293 }
10294 pushback(p, c);
10295 return is_beg ? tBDOT2 : tDOT2;
10296 }
10297 pushback(p, c);
10298 if (c != -1 && ISDIGIT(c)) {
10299 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10300 parse_numeric(p, '.');
10301 if (ISDIGIT(prev)) {
10302 yyerror0("unexpected fraction part after numeric literal");
10303 }
10304 else {
10305 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10306 }
10307 SET_LEX_STATE(EXPR_END);
10308 p->lex.ptok = p->lex.pcur;
10309 goto retry;
10310 }
10311 set_yylval_id('.');
10312 SET_LEX_STATE(EXPR_DOT);
10313 return '.';
10314 }
10315
10316 case '0': case '1': case '2': case '3': case '4':
10317 case '5': case '6': case '7': case '8': case '9':
10318 return parse_numeric(p, c);
10319
10320 case ')':
10321 COND_POP();
10322 CMDARG_POP();
10323 SET_LEX_STATE(EXPR_ENDFN);
10324 p->lex.paren_nest--;
10325 return c;
10326
10327 case ']':
10328 COND_POP();
10329 CMDARG_POP();
10330 SET_LEX_STATE(EXPR_END);
10331 p->lex.paren_nest--;
10332 return c;
10333
10334 case '}':
10335 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10336 if (!p->lex.brace_nest--) return tSTRING_DEND;
10337 COND_POP();
10338 CMDARG_POP();
10339 SET_LEX_STATE(EXPR_END);
10340 p->lex.paren_nest--;
10341 return c;
10342
10343 case ':':
10344 c = nextc(p);
10345 if (c == ':') {
10346 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10347 SET_LEX_STATE(EXPR_BEG);
10348 return tCOLON3;
10349 }
10350 set_yylval_id(idCOLON2);
10351 SET_LEX_STATE(EXPR_DOT);
10352 return tCOLON2;
10353 }
10354 if (IS_END() || ISSPACE(c) || c == '#') {
10355 pushback(p, c);
10356 c = warn_balanced(':', ":", "symbol literal");
10357 SET_LEX_STATE(EXPR_BEG);
10358 return c;
10359 }
10360 switch (c) {
10361 case '\'':
10362 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10363 break;
10364 case '"':
10365 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10366 break;
10367 default:
10368 pushback(p, c);
10369 break;
10370 }
10371 SET_LEX_STATE(EXPR_FNAME);
10372 return tSYMBEG;
10373
10374 case '/':
10375 if (IS_BEG()) {
10376 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10377 return tREGEXP_BEG;
10378 }
10379 if ((c = nextc(p)) == '=') {
10380 set_yylval_id('/');
10381 SET_LEX_STATE(EXPR_BEG);
10382 return tOP_ASGN;
10383 }
10384 pushback(p, c);
10385 if (IS_SPCARG(c)) {
10386 arg_ambiguous(p, '/');
10387 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10388 return tREGEXP_BEG;
10389 }
10390 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10391 return warn_balanced('/', "/", "regexp literal");
10392
10393 case '^':
10394 if ((c = nextc(p)) == '=') {
10395 set_yylval_id('^');
10396 SET_LEX_STATE(EXPR_BEG);
10397 return tOP_ASGN;
10398 }
10399 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10400 pushback(p, c);
10401 return '^';
10402
10403 case ';':
10404 SET_LEX_STATE(EXPR_BEG);
10405 p->command_start = TRUE;
10406 return ';';
10407
10408 case ',':
10409 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10410 return ',';
10411
10412 case '~':
10413 if (IS_AFTER_OPERATOR()) {
10414 if ((c = nextc(p)) != '@') {
10415 pushback(p, c);
10416 }
10417 SET_LEX_STATE(EXPR_ARG);
10418 }
10419 else {
10420 SET_LEX_STATE(EXPR_BEG);
10421 }
10422 return '~';
10423
10424 case '(':
10425 if (IS_BEG()) {
10426 c = tLPAREN;
10427 }
10428 else if (!space_seen) {
10429 /* foo( ... ) => method call, no ambiguity */
10430 }
10431 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
10432 c = tLPAREN_ARG;
10433 }
10434 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
10435 rb_warning0("parentheses after method name is interpreted as "
10436 "an argument list, not a decomposed argument");
10437 }
10438 p->lex.paren_nest++;
10439 COND_PUSH(0);
10440 CMDARG_PUSH(0);
10441 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10442 return c;
10443
10444 case '[':
10445 p->lex.paren_nest++;
10446 if (IS_AFTER_OPERATOR()) {
10447 if ((c = nextc(p)) == ']') {
10448 p->lex.paren_nest--;
10449 SET_LEX_STATE(EXPR_ARG);
10450 if ((c = nextc(p)) == '=') {
10451 return tASET;
10452 }
10453 pushback(p, c);
10454 return tAREF;
10455 }
10456 pushback(p, c);
10457 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
10458 return '[';
10459 }
10460 else if (IS_BEG()) {
10461 c = tLBRACK;
10462 }
10463 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
10464 c = tLBRACK;
10465 }
10466 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10467 COND_PUSH(0);
10468 CMDARG_PUSH(0);
10469 return c;
10470
10471 case '{':
10472 ++p->lex.brace_nest;
10473 if (lambda_beginning_p())
10474 c = tLAMBEG;
10475 else if (IS_lex_state(EXPR_LABELED))
10476 c = tLBRACE; /* hash */
10477 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
10478 c = '{'; /* block (primary) */
10479 else if (IS_lex_state(EXPR_ENDARG))
10480 c = tLBRACE_ARG; /* block (expr) */
10481 else
10482 c = tLBRACE; /* hash */
10483 if (c != tLBRACE) {
10484 p->command_start = TRUE;
10485 SET_LEX_STATE(EXPR_BEG);
10486 }
10487 else {
10488 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10489 }
10490 ++p->lex.paren_nest; /* after lambda_beginning_p() */
10491 COND_PUSH(0);
10492 CMDARG_PUSH(0);
10493 return c;
10494
10495 case '\\':
10496 c = nextc(p);
10497 if (c == '\n') {
10498 space_seen = 1;
10499 dispatch_scan_event(p, tSP);
10500 goto retry; /* skip \\n */
10501 }
10502 if (c == ' ') return tSP;
10503 if (ISSPACE(c)) return c;
10504 pushback(p, c);
10505 return '\\';
10506
10507 case '%':
10508 return parse_percent(p, space_seen, last_state);
10509
10510 case '$':
10511 return parse_gvar(p, last_state);
10512
10513 case '@':
10514 return parse_atmark(p, last_state);
10515
10516 case '_':
10517 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
10518 p->ruby__end__seen = 1;
10519 p->eofp = 1;
10520#ifndef RIPPER
10521 return -1;
10522#else
10523 lex_goto_eol(p);
10524 dispatch_scan_event(p, k__END__);
10525 return 0;
10526#endif
10527 }
10528 newtok(p);
10529 break;
10530
10531 default:
10532 if (!parser_is_identchar(p)) {
10533 compile_error(p, "Invalid char `\\x%02X' in expression", c);
10534 token_flush(p);
10535 goto retry;
10536 }
10537
10538 newtok(p);
10539 break;
10540 }
10541
10542 return parse_ident(p, c, cmd_state);
10543}
10544
10545static enum yytokentype
10546yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
10547{
10548 enum yytokentype t;
10549
10550 p->lval = lval;
10551 lval->val = Qundef;
10552 p->yylloc = yylloc;
10553
10554 t = parser_yylex(p);
10555
10556 if (has_delayed_token(p))
10557 dispatch_delayed_token(p, t);
10558 else if (t != 0)
10559 dispatch_scan_event(p, t);
10560
10561 return t;
10562}
10563
10564#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
10565
10566static NODE*
10567node_new_temporal(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
10568{
10569 NODE *n = rb_ast_newnode(p->ast, type);
10570
10571 rb_node_init(n, type, a0, a1, a2);
10572 return n;
10573}
10574
10575static NODE*
10576node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
10577{
10578 NODE *n = node_new_temporal(p, type, a0, a1, a2);
10579
10580 nd_set_loc(n, loc);
10581 nd_set_node_id(n, parser_get_node_id(p));
10582 return n;
10583}
10584
10585static NODE *
10586nd_set_loc(NODE *nd, const YYLTYPE *loc)
10587{
10588 nd->nd_loc = *loc;
10589 nd_set_line(nd, loc->beg_pos.lineno);
10590 return nd;
10591}
10592
10593#ifndef RIPPER
10594static enum node_type
10595nodetype(NODE *node) /* for debug */
10596{
10597 return (enum node_type)nd_type(node);
10598}
10599
10600static int
10601nodeline(NODE *node)
10602{
10603 return nd_line(node);
10604}
10605
10606static NODE*
10607newline_node(NODE *node)
10608{
10609 if (node) {
10610 node = remove_begin(node);
10611 node->flags |= NODE_FL_NEWLINE;
10612 }
10613 return node;
10614}
10615
10616static void
10617fixpos(NODE *node, NODE *orig)
10618{
10619 if (!node) return;
10620 if (!orig) return;
10621 nd_set_line(node, nd_line(orig));
10622}
10623
10624static void
10625parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10626{
10627 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10628}
10629
10630static void
10631parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10632{
10633 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10634}
10635
10636static NODE*
10637block_append(struct parser_params *p, NODE *head, NODE *tail)
10638{
10639 NODE *end, *h = head, *nd;
10640
10641 if (tail == 0) return head;
10642
10643 if (h == 0) return tail;
10644 switch (nd_type(h)) {
10645 case NODE_LIT:
10646 case NODE_STR:
10647 case NODE_SELF:
10648 case NODE_TRUE:
10649 case NODE_FALSE:
10650 case NODE_NIL:
10651 parser_warning(p, h, "unused literal ignored");
10652 return tail;
10653 default:
10654 h = end = NEW_BLOCK(head, &head->nd_loc);
10655 end->nd_end = end;
10656 head = end;
10657 break;
10658 case NODE_BLOCK:
10659 end = h->nd_end;
10660 break;
10661 }
10662
10663 nd = end->nd_head;
10664 switch (nd_type(nd)) {
10665 case NODE_RETURN:
10666 case NODE_BREAK:
10667 case NODE_NEXT:
10668 case NODE_REDO:
10669 case NODE_RETRY:
10670 if (RTEST(ruby_verbose)) {
10671 parser_warning(p, tail, "statement not reached");
10672 }
10673 break;
10674
10675 default:
10676 break;
10677 }
10678
10679 if (!nd_type_p(tail, NODE_BLOCK)) {
10680 tail = NEW_BLOCK(tail, &tail->nd_loc);
10681 tail->nd_end = tail;
10682 }
10683 end->nd_next = tail;
10684 h->nd_end = tail->nd_end;
10685 nd_set_last_loc(head, nd_last_loc(tail));
10686 return head;
10687}
10688
10689/* append item to the list */
10690static NODE*
10691list_append(struct parser_params *p, NODE *list, NODE *item)
10692{
10693 NODE *last;
10694
10695 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10696 if (list->nd_next) {
10697 last = list->nd_next->nd_end;
10698 }
10699 else {
10700 last = list;
10701 }
10702
10703 list->nd_alen += 1;
10704 last->nd_next = NEW_LIST(item, &item->nd_loc);
10705 list->nd_next->nd_end = last->nd_next;
10706
10707 nd_set_last_loc(list, nd_last_loc(item));
10708
10709 return list;
10710}
10711
10712/* concat two lists */
10713static NODE*
10714list_concat(NODE *head, NODE *tail)
10715{
10716 NODE *last;
10717
10718 if (head->nd_next) {
10719 last = head->nd_next->nd_end;
10720 }
10721 else {
10722 last = head;
10723 }
10724
10725 head->nd_alen += tail->nd_alen;
10726 last->nd_next = tail;
10727 if (tail->nd_next) {
10728 head->nd_next->nd_end = tail->nd_next->nd_end;
10729 }
10730 else {
10731 head->nd_next->nd_end = tail;
10732 }
10733
10734 nd_set_last_loc(head, nd_last_loc(tail));
10735
10736 return head;
10737}
10738
10739static int
10740literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10741{
10742 if (NIL_P(tail)) return 1;
10743 if (!rb_enc_compatible(head, tail)) {
10744 compile_error(p, "string literal encodings differ (%s / %s)",
10745 rb_enc_name(rb_enc_get(head)),
10746 rb_enc_name(rb_enc_get(tail)));
10747 rb_str_resize(head, 0);
10748 rb_str_resize(tail, 0);
10749 return 0;
10750 }
10751 rb_str_buf_append(head, tail);
10752 return 1;
10753}
10754
10755static VALUE
10756string_literal_head(enum node_type htype, NODE *head)
10757{
10758 if (htype != NODE_DSTR) return Qfalse;
10759 if (head->nd_next) {
10760 head = head->nd_next->nd_end->nd_head;
10761 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10762 }
10763 const VALUE lit = head->nd_lit;
10764 ASSUME(lit != Qfalse);
10765 return lit;
10766}
10767
10768/* concat two string literals */
10769static NODE *
10770literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10771{
10772 enum node_type htype;
10773 VALUE lit;
10774
10775 if (!head) return tail;
10776 if (!tail) return head;
10777
10778 htype = nd_type(head);
10779 if (htype == NODE_EVSTR) {
10780 head = new_dstr(p, head, loc);
10781 htype = NODE_DSTR;
10782 }
10783 if (p->heredoc_indent > 0) {
10784 switch (htype) {
10785 case NODE_STR:
10786 nd_set_type(head, NODE_DSTR);
10787 case NODE_DSTR:
10788 return list_append(p, head, tail);
10789 default:
10790 break;
10791 }
10792 }
10793 switch (nd_type(tail)) {
10794 case NODE_STR:
10795 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10796 htype = NODE_STR;
10797 }
10798 else {
10799 lit = head->nd_lit;
10800 }
10801 if (htype == NODE_STR) {
10802 if (!literal_concat0(p, lit, tail->nd_lit)) {
10803 error:
10804 rb_discard_node(p, head);
10805 rb_discard_node(p, tail);
10806 return 0;
10807 }
10808 rb_discard_node(p, tail);
10809 }
10810 else {
10811 list_append(p, head, tail);
10812 }
10813 break;
10814
10815 case NODE_DSTR:
10816 if (htype == NODE_STR) {
10817 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10818 goto error;
10819 tail->nd_lit = head->nd_lit;
10820 rb_discard_node(p, head);
10821 head = tail;
10822 }
10823 else if (NIL_P(tail->nd_lit)) {
10824 append:
10825 head->nd_alen += tail->nd_alen - 1;
10826 if (!head->nd_next) {
10827 head->nd_next = tail->nd_next;
10828 }
10829 else if (tail->nd_next) {
10830 head->nd_next->nd_end->nd_next = tail->nd_next;
10831 head->nd_next->nd_end = tail->nd_next->nd_end;
10832 }
10833 rb_discard_node(p, tail);
10834 }
10835 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10836 if (!literal_concat0(p, lit, tail->nd_lit))
10837 goto error;
10838 tail->nd_lit = Qnil;
10839 goto append;
10840 }
10841 else {
10842 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10843 }
10844 break;
10845
10846 case NODE_EVSTR:
10847 if (htype == NODE_STR) {
10848 nd_set_type(head, NODE_DSTR);
10849 head->nd_alen = 1;
10850 }
10851 list_append(p, head, tail);
10852 break;
10853 }
10854 return head;
10855}
10856
10857static NODE *
10858evstr2dstr(struct parser_params *p, NODE *node)
10859{
10860 if (nd_type_p(node, NODE_EVSTR)) {
10861 node = new_dstr(p, node, &node->nd_loc);
10862 }
10863 return node;
10864}
10865
10866static NODE *
10867new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10868{
10869 NODE *head = node;
10870
10871 if (node) {
10872 switch (nd_type(node)) {
10873 case NODE_STR:
10874 nd_set_type(node, NODE_DSTR);
10875 return node;
10876 case NODE_DSTR:
10877 break;
10878 case NODE_EVSTR:
10879 return node;
10880 }
10881 }
10882 return NEW_EVSTR(head, loc);
10883}
10884
10885static NODE *
10886new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10887{
10888 VALUE lit = STR_NEW0();
10889 NODE *dstr = NEW_DSTR(lit, loc);
10890 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10891 return list_append(p, dstr, node);
10892}
10893
10894static NODE *
10895call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10896 const YYLTYPE *op_loc, const YYLTYPE *loc)
10897{
10898 NODE *expr;
10899 value_expr(recv);
10900 value_expr(arg1);
10901 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10902 nd_set_line(expr, op_loc->beg_pos.lineno);
10903 return expr;
10904}
10905
10906static NODE *
10907call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10908{
10909 NODE *opcall;
10910 value_expr(recv);
10911 opcall = NEW_OPCALL(recv, id, 0, loc);
10912 nd_set_line(opcall, op_loc->beg_pos.lineno);
10913 return opcall;
10914}
10915
10916static NODE *
10917new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10918{
10919 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10920 nd_set_line(qcall, op_loc->beg_pos.lineno);
10921 return qcall;
10922}
10923
10924static NODE*
10925new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10926{
10927 NODE *ret;
10928 if (block) block_dup_check(p, args, block);
10929 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10930 if (block) ret = method_add_block(p, ret, block, loc);
10931 fixpos(ret, recv);
10932 return ret;
10933}
10934
10935#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10936static NODE*
10937match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10938{
10939 NODE *n;
10940 int line = op_loc->beg_pos.lineno;
10941
10942 value_expr(node1);
10943 value_expr(node2);
10944 if (node1 && (n = nd_once_body(node1)) != 0) {
10945 switch (nd_type(n)) {
10946 case NODE_DREGX:
10947 {
10948 NODE *match = NEW_MATCH2(node1, node2, loc);
10949 nd_set_line(match, line);
10950 return match;
10951 }
10952
10953 case NODE_LIT:
10954 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10955 const VALUE lit = n->nd_lit;
10956 NODE *match = NEW_MATCH2(node1, node2, loc);
10957 match->nd_args = reg_named_capture_assign(p, lit, loc);
10958 nd_set_line(match, line);
10959 return match;
10960 }
10961 }
10962 }
10963
10964 if (node2 && (n = nd_once_body(node2)) != 0) {
10965 NODE *match3;
10966
10967 switch (nd_type(n)) {
10968 case NODE_LIT:
10969 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10970 /* fallthru */
10971 case NODE_DREGX:
10972 match3 = NEW_MATCH3(node2, node1, loc);
10973 return match3;
10974 }
10975 }
10976
10977 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10978 nd_set_line(n, line);
10979 return n;
10980}
10981
10982# if WARN_PAST_SCOPE
10983static int
10984past_dvar_p(struct parser_params *p, ID id)
10985{
10986 struct vtable *past = p->lvtbl->past;
10987 while (past) {
10988 if (vtable_included(past, id)) return 1;
10989 past = past->prev;
10990 }
10991 return 0;
10992}
10993# endif
10994
10995static int
10996numparam_nested_p(struct parser_params *p)
10997{
10998 struct local_vars *local = p->lvtbl;
10999 NODE *outer = local->numparam.outer;
11000 NODE *inner = local->numparam.inner;
11001 if (outer || inner) {
11002 NODE *used = outer ? outer : inner;
11003 compile_error(p, "numbered parameter is already used in\n"
11004 "%s:%d: %s block here",
11005 p->ruby_sourcefile, nd_line(used),
11006 outer ? "outer" : "inner");
11007 parser_show_error_line(p, &used->nd_loc);
11008 return 1;
11009 }
11010 return 0;
11011}
11012
11013static NODE*
11014gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
11015{
11016 ID *vidp = NULL;
11017 NODE *node;
11018 switch (id) {
11019 case keyword_self:
11020 return NEW_SELF(loc);
11021 case keyword_nil:
11022 return NEW_NIL(loc);
11023 case keyword_true:
11024 return NEW_TRUE(loc);
11025 case keyword_false:
11026 return NEW_FALSE(loc);
11027 case keyword__FILE__:
11028 {
11029 VALUE file = p->ruby_sourcefile_string;
11030 if (NIL_P(file))
11031 file = rb_str_new(0, 0);
11032 else
11033 file = rb_str_dup(file);
11034 node = NEW_STR(file, loc);
11035 RB_OBJ_WRITTEN(p->ast, Qnil, file);
11036 }
11037 return node;
11038 case keyword__LINE__:
11039 return NEW_LIT(INT2FIX(p->tokline), loc);
11040 case keyword__ENCODING__:
11041 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
11042 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11043 return node;
11044
11045 }
11046 switch (id_type(id)) {
11047 case ID_LOCAL:
11048 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
11049 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
11050 if (id == p->cur_arg) {
11051 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11052 return 0;
11053 }
11054 if (vidp) *vidp |= LVAR_USED;
11055 node = NEW_DVAR(id, loc);
11056 return node;
11057 }
11058 if (local_id_ref(p, id, &vidp)) {
11059 if (id == p->cur_arg) {
11060 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11061 return 0;
11062 }
11063 if (vidp) *vidp |= LVAR_USED;
11064 node = NEW_LVAR(id, loc);
11065 return node;
11066 }
11067 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
11068 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
11069 if (numparam_nested_p(p)) return 0;
11070 node = NEW_DVAR(id, loc);
11071 struct local_vars *local = p->lvtbl;
11072 if (!local->numparam.current) local->numparam.current = node;
11073 return node;
11074 }
11075# if WARN_PAST_SCOPE
11076 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
11077 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
11078 }
11079# endif
11080 /* method call without arguments */
11081 return NEW_VCALL(id, loc);
11082 case ID_GLOBAL:
11083 return NEW_GVAR(id, loc);
11084 case ID_INSTANCE:
11085 return NEW_IVAR(id, loc);
11086 case ID_CONST:
11087 return NEW_CONST(id, loc);
11088 case ID_CLASS:
11089 return NEW_CVAR(id, loc);
11090 }
11091 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11092 return 0;
11093}
11094
11095static NODE *
11096opt_arg_append(NODE *opt_list, NODE *opt)
11097{
11098 NODE *opts = opt_list;
11099 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11100
11101 while (opts->nd_next) {
11102 opts = opts->nd_next;
11103 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11104 }
11105 opts->nd_next = opt;
11106
11107 return opt_list;
11108}
11109
11110static NODE *
11111kwd_append(NODE *kwlist, NODE *kw)
11112{
11113 if (kwlist) {
11114 opt_arg_append(kwlist, kw);
11115 }
11116 return kwlist;
11117}
11118
11119static NODE *
11120new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
11121{
11122 return NEW_DEFINED(remove_begin_all(expr), loc);
11123}
11124
11125static NODE*
11126symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
11127{
11128 enum node_type type = nd_type(symbol);
11129 switch (type) {
11130 case NODE_DSTR:
11131 nd_set_type(symbol, NODE_DSYM);
11132 break;
11133 case NODE_STR:
11134 nd_set_type(symbol, NODE_LIT);
11135 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
11136 break;
11137 default:
11138 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
11139 }
11140 return list_append(p, symbols, symbol);
11141}
11142
11143static NODE *
11144new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
11145{
11146 NODE *list, *prev;
11147 VALUE lit;
11148
11149 if (!node) {
11150 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
11151 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11152 return node;
11153 }
11154 switch (nd_type(node)) {
11155 case NODE_STR:
11156 {
11157 VALUE src = node->nd_lit;
11158 nd_set_type(node, NODE_LIT);
11159 nd_set_loc(node, loc);
11160 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11161 }
11162 break;
11163 default:
11164 lit = STR_NEW0();
11165 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
11166 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11167 /* fall through */
11168 case NODE_DSTR:
11169 nd_set_type(node, NODE_DREGX);
11170 nd_set_loc(node, loc);
11171 node->nd_cflag = options & RE_OPTION_MASK;
11172 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
11173 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
11174 NODE *frag = list->nd_head;
11175 enum node_type type = nd_type(frag);
11176 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
11177 VALUE tail = frag->nd_lit;
11178 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
11179 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
11180 if (!literal_concat0(p, lit, tail)) {
11181 return NEW_NIL(loc); /* dummy node on error */
11182 }
11183 rb_str_resize(tail, 0);
11184 prev->nd_next = list->nd_next;
11185 rb_discard_node(p, list->nd_head);
11186 rb_discard_node(p, list);
11187 list = prev;
11188 }
11189 else {
11190 prev = list;
11191 }
11192 }
11193 else {
11194 prev = 0;
11195 }
11196 }
11197 if (!node->nd_next) {
11198 VALUE src = node->nd_lit;
11199 nd_set_type(node, NODE_LIT);
11200 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11201 }
11202 if (options & RE_OPTION_ONCE) {
11203 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
11204 }
11205 break;
11206 }
11207 return node;
11208}
11209
11210static NODE *
11211new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
11212{
11213 if (!k) return 0;
11214 return NEW_KW_ARG(0, (k), loc);
11215}
11216
11217static NODE *
11218new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11219{
11220 if (!node) {
11221 VALUE lit = STR_NEW0();
11222 NODE *xstr = NEW_XSTR(lit, loc);
11223 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11224 return xstr;
11225 }
11226 switch (nd_type(node)) {
11227 case NODE_STR:
11228 nd_set_type(node, NODE_XSTR);
11229 nd_set_loc(node, loc);
11230 break;
11231 case NODE_DSTR:
11232 nd_set_type(node, NODE_DXSTR);
11233 nd_set_loc(node, loc);
11234 break;
11235 default:
11236 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
11237 break;
11238 }
11239 return node;
11240}
11241
11242static void
11243check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
11244{
11245 VALUE lit;
11246
11247 if (!arg || !p->case_labels) return;
11248
11249 lit = rb_node_case_when_optimizable_literal(arg);
11250 if (UNDEF_P(lit)) return;
11251 if (nd_type_p(arg, NODE_STR)) {
11252 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
11253 }
11254
11255 if (NIL_P(p->case_labels)) {
11256 p->case_labels = rb_obj_hide(rb_hash_new());
11257 }
11258 else {
11259 VALUE line = rb_hash_lookup(p->case_labels, lit);
11260 if (!NIL_P(line)) {
11261 rb_warning1("duplicated `when' clause with line %d is ignored",
11262 WARN_IVAL(line));
11263 return;
11264 }
11265 }
11266 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
11267}
11268
11269#else /* !RIPPER */
11270static int
11271id_is_var(struct parser_params *p, ID id)
11272{
11273 if (is_notop_id(id)) {
11274 switch (id & ID_SCOPE_MASK) {
11275 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
11276 return 1;
11277 case ID_LOCAL:
11278 if (dyna_in_block(p)) {
11279 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
11280 }
11281 if (local_id(p, id)) return 1;
11282 /* method call without arguments */
11283 return 0;
11284 }
11285 }
11286 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11287 return 0;
11288}
11289
11290static VALUE
11291new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
11292{
11293 VALUE src = 0, err;
11294 int options = 0;
11295 if (ripper_is_node_yylval(re)) {
11296 src = RNODE(re)->nd_cval;
11297 re = RNODE(re)->nd_rval;
11298 }
11299 if (ripper_is_node_yylval(opt)) {
11300 options = (int)RNODE(opt)->nd_tag;
11301 opt = RNODE(opt)->nd_rval;
11302 }
11303 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
11304 compile_error(p, "%"PRIsVALUE, err);
11305 }
11306 return dispatch2(regexp_literal, re, opt);
11307}
11308#endif /* !RIPPER */
11309
11310static inline enum lex_state_e
11311parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
11312{
11313 if (p->debug) {
11314 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
11315 }
11316 return p->lex.state = ls;
11317}
11318
11319#ifndef RIPPER
11320static const char rb_parser_lex_state_names[][8] = {
11321 "BEG", "END", "ENDARG", "ENDFN", "ARG",
11322 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
11323 "LABEL", "LABELED","FITEM",
11324};
11325
11326static VALUE
11327append_lex_state_name(enum lex_state_e state, VALUE buf)
11328{
11329 int i, sep = 0;
11330 unsigned int mask = 1;
11331 static const char none[] = "NONE";
11332
11333 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
11334 if ((unsigned)state & mask) {
11335 if (sep) {
11336 rb_str_cat(buf, "|", 1);
11337 }
11338 sep = 1;
11339 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
11340 }
11341 }
11342 if (!sep) {
11343 rb_str_cat(buf, none, sizeof(none)-1);
11344 }
11345 return buf;
11346}
11347
11348static void
11349flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
11350{
11351 VALUE mesg = p->debug_buffer;
11352
11353 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
11354 p->debug_buffer = Qnil;
11355 rb_io_puts(1, &mesg, out);
11356 }
11357 if (!NIL_P(str) && RSTRING_LEN(str)) {
11358 rb_io_write(p->debug_output, str);
11359 }
11360}
11361
11362enum lex_state_e
11363rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
11364 enum lex_state_e to, int line)
11365{
11366 VALUE mesg;
11367 mesg = rb_str_new_cstr("lex_state: ");
11368 append_lex_state_name(from, mesg);
11369 rb_str_cat_cstr(mesg, " -> ");
11370 append_lex_state_name(to, mesg);
11371 rb_str_catf(mesg, " at line %d\n", line);
11372 flush_debug_buffer(p, p->debug_output, mesg);
11373 return to;
11374}
11375
11376VALUE
11377rb_parser_lex_state_name(enum lex_state_e state)
11378{
11379 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
11380}
11381
11382static void
11383append_bitstack_value(stack_type stack, VALUE mesg)
11384{
11385 if (stack == 0) {
11386 rb_str_cat_cstr(mesg, "0");
11387 }
11388 else {
11389 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
11390 for (; mask && !(stack & mask); mask >>= 1) continue;
11391 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
11392 }
11393}
11394
11395void
11396rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
11397 const char *name, int line)
11398{
11399 VALUE mesg = rb_sprintf("%s: ", name);
11400 append_bitstack_value(stack, mesg);
11401 rb_str_catf(mesg, " at line %d\n", line);
11402 flush_debug_buffer(p, p->debug_output, mesg);
11403}
11404
11405void
11406rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
11407{
11408 va_list ap;
11409 VALUE mesg = rb_str_new_cstr("internal parser error: ");
11410
11411 va_start(ap, fmt);
11412 rb_str_vcatf(mesg, fmt, ap);
11413 va_end(ap);
11414 yyerror0(RSTRING_PTR(mesg));
11415 RB_GC_GUARD(mesg);
11416
11417 mesg = rb_str_new(0, 0);
11418 append_lex_state_name(p->lex.state, mesg);
11419 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
11420 rb_str_resize(mesg, 0);
11421 append_bitstack_value(p->cond_stack, mesg);
11422 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
11423 rb_str_resize(mesg, 0);
11424 append_bitstack_value(p->cmdarg_stack, mesg);
11425 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
11426 if (p->debug_output == rb_ractor_stdout())
11427 p->debug_output = rb_ractor_stderr();
11428 p->debug = TRUE;
11429}
11430
11431static YYLTYPE *
11432rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
11433{
11434 yylloc->beg_pos.lineno = sourceline;
11435 yylloc->beg_pos.column = beg_pos;
11436 yylloc->end_pos.lineno = sourceline;
11437 yylloc->end_pos.column = end_pos;
11438 return yylloc;
11439}
11440
11441YYLTYPE *
11442rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
11443{
11444 int sourceline = here->sourceline;
11445 int beg_pos = (int)here->offset - here->quote
11446 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
11447 int end_pos = (int)here->offset + here->length + here->quote;
11448
11449 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11450}
11451
11452YYLTYPE *
11453rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
11454{
11455 yylloc->beg_pos.lineno = p->delayed.beg_line;
11456 yylloc->beg_pos.column = p->delayed.beg_col;
11457 yylloc->end_pos.lineno = p->delayed.end_line;
11458 yylloc->end_pos.column = p->delayed.end_col;
11459
11460 return yylloc;
11461}
11462
11463YYLTYPE *
11464rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
11465{
11466 int sourceline = p->ruby_sourceline;
11467 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11468 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
11469 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11470}
11471
11472YYLTYPE *
11473rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
11474{
11475 yylloc->end_pos = yylloc->beg_pos;
11476
11477 return yylloc;
11478}
11479
11480YYLTYPE *
11481rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
11482{
11483 int sourceline = p->ruby_sourceline;
11484 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11485 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
11486 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11487}
11488
11489YYLTYPE *
11490rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
11491{
11492 int sourceline = p->ruby_sourceline;
11493 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11494 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
11495 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11496}
11497#endif /* !RIPPER */
11498
11499static int
11500assignable0(struct parser_params *p, ID id, const char **err)
11501{
11502 if (!id) return -1;
11503 switch (id) {
11504 case keyword_self:
11505 *err = "Can't change the value of self";
11506 return -1;
11507 case keyword_nil:
11508 *err = "Can't assign to nil";
11509 return -1;
11510 case keyword_true:
11511 *err = "Can't assign to true";
11512 return -1;
11513 case keyword_false:
11514 *err = "Can't assign to false";
11515 return -1;
11516 case keyword__FILE__:
11517 *err = "Can't assign to __FILE__";
11518 return -1;
11519 case keyword__LINE__:
11520 *err = "Can't assign to __LINE__";
11521 return -1;
11522 case keyword__ENCODING__:
11523 *err = "Can't assign to __ENCODING__";
11524 return -1;
11525 }
11526 switch (id_type(id)) {
11527 case ID_LOCAL:
11528 if (dyna_in_block(p)) {
11529 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
11530 compile_error(p, "Can't assign to numbered parameter _%d",
11531 NUMPARAM_ID_TO_IDX(id));
11532 return -1;
11533 }
11534 if (dvar_curr(p, id)) return NODE_DASGN;
11535 if (dvar_defined(p, id)) return NODE_DASGN;
11536 if (local_id(p, id)) return NODE_LASGN;
11537 dyna_var(p, id);
11538 return NODE_DASGN;
11539 }
11540 else {
11541 if (!local_id(p, id)) local_var(p, id);
11542 return NODE_LASGN;
11543 }
11544 break;
11545 case ID_GLOBAL: return NODE_GASGN;
11546 case ID_INSTANCE: return NODE_IASGN;
11547 case ID_CONST:
11548 if (!p->ctxt.in_def) return NODE_CDECL;
11549 *err = "dynamic constant assignment";
11550 return -1;
11551 case ID_CLASS: return NODE_CVASGN;
11552 default:
11553 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
11554 }
11555 return -1;
11556}
11557
11558#ifndef RIPPER
11559static NODE*
11560assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
11561{
11562 const char *err = 0;
11563 int node_type = assignable0(p, id, &err);
11564 switch (node_type) {
11565 case NODE_DASGN: return NEW_DASGN(id, val, loc);
11566 case NODE_LASGN: return NEW_LASGN(id, val, loc);
11567 case NODE_GASGN: return NEW_GASGN(id, val, loc);
11568 case NODE_IASGN: return NEW_IASGN(id, val, loc);
11569 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
11570 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
11571 }
11572 if (err) yyerror1(loc, err);
11573 return NEW_BEGIN(0, loc);
11574}
11575#else
11576static VALUE
11577assignable(struct parser_params *p, VALUE lhs)
11578{
11579 const char *err = 0;
11580 assignable0(p, get_id(lhs), &err);
11581 if (err) lhs = assign_error(p, err, lhs);
11582 return lhs;
11583}
11584#endif
11585
11586static int
11587is_private_local_id(ID name)
11588{
11589 VALUE s;
11590 if (name == idUScore) return 1;
11591 if (!is_local_id(name)) return 0;
11592 s = rb_id2str(name);
11593 if (!s) return 0;
11594 return RSTRING_PTR(s)[0] == '_';
11595}
11596
11597static int
11598shadowing_lvar_0(struct parser_params *p, ID name)
11599{
11600 if (dyna_in_block(p)) {
11601 if (dvar_curr(p, name)) {
11602 if (is_private_local_id(name)) return 1;
11603 yyerror0("duplicated argument name");
11604 }
11605 else if (dvar_defined(p, name) || local_id(p, name)) {
11606 vtable_add(p->lvtbl->vars, name);
11607 if (p->lvtbl->used) {
11608 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11609 }
11610 return 0;
11611 }
11612 }
11613 else {
11614 if (local_id(p, name)) {
11615 if (is_private_local_id(name)) return 1;
11616 yyerror0("duplicated argument name");
11617 }
11618 }
11619 return 1;
11620}
11621
11622static ID
11623shadowing_lvar(struct parser_params *p, ID name)
11624{
11625 shadowing_lvar_0(p, name);
11626 return name;
11627}
11628
11629static void
11630new_bv(struct parser_params *p, ID name)
11631{
11632 if (!name) return;
11633 if (!is_local_id(name)) {
11634 compile_error(p, "invalid local variable - %"PRIsVALUE,
11635 rb_id2str(name));
11636 return;
11637 }
11638 if (!shadowing_lvar_0(p, name)) return;
11639 dyna_var(p, name);
11640}
11641
11642#ifndef RIPPER
11643static NODE *
11644aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11645{
11646 return NEW_ATTRASGN(recv, tASET, idx, loc);
11647}
11648
11649static void
11650block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11651{
11652 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11653 compile_error(p, "both block arg and actual block given");
11654 }
11655}
11656
11657static NODE *
11658attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11659{
11660 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11661 return NEW_ATTRASGN(recv, id, 0, loc);
11662}
11663
11664static void
11665rb_backref_error(struct parser_params *p, NODE *node)
11666{
11667 switch (nd_type(node)) {
11668 case NODE_NTH_REF:
11669 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11670 break;
11671 case NODE_BACK_REF:
11672 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11673 break;
11674 }
11675}
11676#else
11677static VALUE
11678backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11679{
11680 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11681 rb_str_append(mesg, ref->nd_cval);
11682 return dispatch2(assign_error, mesg, expr);
11683}
11684#endif
11685
11686#ifndef RIPPER
11687static NODE *
11688arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11689{
11690 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11691 switch (nd_type(node1)) {
11692 case NODE_LIST:
11693 return list_append(p, node1, node2);
11694 case NODE_BLOCK_PASS:
11695 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11696 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11697 return node1;
11698 case NODE_ARGSPUSH:
11699 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11700 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11701 nd_set_type(node1, NODE_ARGSCAT);
11702 return node1;
11703 case NODE_ARGSCAT:
11704 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11705 node1->nd_body = list_append(p, node1->nd_body, node2);
11706 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11707 return node1;
11708 }
11709 return NEW_ARGSPUSH(node1, node2, loc);
11710}
11711
11712static NODE *
11713arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11714{
11715 if (!node2) return node1;
11716 switch (nd_type(node1)) {
11717 case NODE_BLOCK_PASS:
11718 if (node1->nd_head)
11719 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11720 else
11721 node1->nd_head = NEW_LIST(node2, loc);
11722 return node1;
11723 case NODE_ARGSPUSH:
11724 if (!nd_type_p(node2, NODE_LIST)) break;
11725 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11726 nd_set_type(node1, NODE_ARGSCAT);
11727 return node1;
11728 case NODE_ARGSCAT:
11729 if (!nd_type_p(node2, NODE_LIST) ||
11730 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11731 node1->nd_body = list_concat(node1->nd_body, node2);
11732 return node1;
11733 }
11734 return NEW_ARGSCAT(node1, node2, loc);
11735}
11736
11737static NODE *
11738last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11739{
11740 NODE *n1;
11741 if ((n1 = splat_array(args)) != 0) {
11742 return list_append(p, n1, last_arg);
11743 }
11744 return arg_append(p, args, last_arg, loc);
11745}
11746
11747static NODE *
11748rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11749{
11750 NODE *n1;
11751 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11752 return list_concat(n1, rest_arg);
11753 }
11754 return arg_concat(p, args, rest_arg, loc);
11755}
11756
11757static NODE *
11758splat_array(NODE* node)
11759{
11760 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11761 if (nd_type_p(node, NODE_LIST)) return node;
11762 return 0;
11763}
11764
11765static void
11766mark_lvar_used(struct parser_params *p, NODE *rhs)
11767{
11768 ID *vidp = NULL;
11769 if (!rhs) return;
11770 switch (nd_type(rhs)) {
11771 case NODE_LASGN:
11772 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11773 if (vidp) *vidp |= LVAR_USED;
11774 }
11775 break;
11776 case NODE_DASGN:
11777 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11778 if (vidp) *vidp |= LVAR_USED;
11779 }
11780 break;
11781#if 0
11782 case NODE_MASGN:
11783 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11784 mark_lvar_used(p, rhs->nd_head);
11785 }
11786 break;
11787#endif
11788 }
11789}
11790
11791static NODE *
11792const_decl_path(struct parser_params *p, NODE **dest)
11793{
11794 NODE *n = *dest;
11795 if (!nd_type_p(n, NODE_CALL)) {
11796 const YYLTYPE *loc = &n->nd_loc;
11797 VALUE path;
11798 if (n->nd_vid) {
11799 path = rb_id2str(n->nd_vid);
11800 }
11801 else {
11802 n = n->nd_else;
11803 path = rb_ary_new();
11804 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11805 rb_ary_push(path, rb_id2str(n->nd_mid));
11806 }
11807 if (n && nd_type_p(n, NODE_CONST)) {
11808 // Const::Name
11809 rb_ary_push(path, rb_id2str(n->nd_vid));
11810 }
11811 else if (n && nd_type_p(n, NODE_COLON3)) {
11812 // ::Const::Name
11813 rb_ary_push(path, rb_str_new(0, 0));
11814 }
11815 else {
11816 // expression::Name
11817 rb_ary_push(path, rb_str_new_cstr("..."));
11818 }
11819 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11820 path = rb_fstring(path);
11821 }
11822 *dest = n = NEW_LIT(path, loc);
11823 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11824 }
11825 return n;
11826}
11827
11828extern VALUE rb_mRubyVMFrozenCore;
11829
11830static NODE *
11831make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11832{
11833 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11834
11835 if (copy) {
11836 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11837 NEW_LIST(value, loc), loc);
11838 }
11839 else {
11840 return NEW_CALL(fcore, rb_intern("make_shareable"),
11841 NEW_LIST(value, loc), loc);
11842 }
11843}
11844
11845static NODE *
11846ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11847{
11848 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11849 NODE *args = NEW_LIST(value, loc);
11850 args = list_append(p, args, const_decl_path(p, dest));
11851 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11852}
11853
11854static int is_static_content(NODE *node);
11855
11856static VALUE
11857shareable_literal_value(NODE *node)
11858{
11859 if (!node) return Qnil;
11860 enum node_type type = nd_type(node);
11861 switch (type) {
11862 case NODE_TRUE:
11863 return Qtrue;
11864 case NODE_FALSE:
11865 return Qfalse;
11866 case NODE_NIL:
11867 return Qnil;
11868 case NODE_LIT:
11869 return node->nd_lit;
11870 default:
11871 return Qundef;
11872 }
11873}
11874
11875#ifndef SHAREABLE_BARE_EXPRESSION
11876#define SHAREABLE_BARE_EXPRESSION 1
11877#endif
11878
11879static NODE *
11880shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11881 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11882{
11883# define shareable_literal_constant_next(n) \
11884 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11885 VALUE lit = Qnil;
11886
11887 if (!value) return 0;
11888 enum node_type type = nd_type(value);
11889 switch (type) {
11890 case NODE_TRUE:
11891 case NODE_FALSE:
11892 case NODE_NIL:
11893 case NODE_LIT:
11894 return value;
11895
11896 case NODE_DSTR:
11897 if (shareable == shareable_literal) {
11898 value = NEW_CALL(value, idUMinus, 0, loc);
11899 }
11900 return value;
11901
11902 case NODE_STR:
11903 lit = rb_fstring(value->nd_lit);
11904 nd_set_type(value, NODE_LIT);
11905 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11906 return value;
11907
11908 case NODE_ZLIST:
11909 lit = rb_ary_new();
11910 OBJ_FREEZE_RAW(lit);
11911 NODE *n = NEW_LIT(lit, loc);
11912 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11913 return n;
11914
11915 case NODE_LIST:
11916 lit = rb_ary_new();
11917 for (NODE *n = value; n; n = n->nd_next) {
11918 NODE *elt = n->nd_head;
11919 if (elt) {
11920 elt = shareable_literal_constant_next(elt);
11921 if (elt) {
11922 n->nd_head = elt;
11923 }
11924 else if (RTEST(lit)) {
11925 rb_ary_clear(lit);
11926 lit = Qfalse;
11927 }
11928 }
11929 if (RTEST(lit)) {
11930 VALUE e = shareable_literal_value(elt);
11931 if (!UNDEF_P(e)) {
11932 rb_ary_push(lit, e);
11933 }
11934 else {
11935 rb_ary_clear(lit);
11936 lit = Qnil; /* make shareable at runtime */
11937 }
11938 }
11939 }
11940 break;
11941
11942 case NODE_HASH:
11943 if (!value->nd_brace) return 0;
11944 lit = rb_hash_new();
11945 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11946 NODE *key = n->nd_head;
11947 NODE *val = n->nd_next->nd_head;
11948 if (key) {
11949 key = shareable_literal_constant_next(key);
11950 if (key) {
11951 n->nd_head = key;
11952 }
11953 else if (RTEST(lit)) {
11954 rb_hash_clear(lit);
11955 lit = Qfalse;
11956 }
11957 }
11958 if (val) {
11959 val = shareable_literal_constant_next(val);
11960 if (val) {
11961 n->nd_next->nd_head = val;
11962 }
11963 else if (RTEST(lit)) {
11964 rb_hash_clear(lit);
11965 lit = Qfalse;
11966 }
11967 }
11968 if (RTEST(lit)) {
11969 VALUE k = shareable_literal_value(key);
11970 VALUE v = shareable_literal_value(val);
11971 if (!UNDEF_P(k) && !UNDEF_P(v)) {
11972 rb_hash_aset(lit, k, v);
11973 }
11974 else {
11975 rb_hash_clear(lit);
11976 lit = Qnil; /* make shareable at runtime */
11977 }
11978 }
11979 }
11980 break;
11981
11982 default:
11983 if (shareable == shareable_literal &&
11984 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11985 return ensure_shareable_node(p, dest, value, loc);
11986 }
11987 return 0;
11988 }
11989
11990 /* Array or Hash */
11991 if (!lit) return 0;
11992 if (NIL_P(lit)) {
11993 // if shareable_literal, all elements should have been ensured
11994 // as shareable
11995 value = make_shareable_node(p, value, false, loc);
11996 }
11997 else {
11998 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11999 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
12000 }
12001
12002 return value;
12003# undef shareable_literal_constant_next
12004}
12005
12006static NODE *
12007shareable_constant_value(struct parser_params *p, enum shareability shareable,
12008 NODE *lhs, NODE *value, const YYLTYPE *loc)
12009{
12010 if (!value) return 0;
12011 switch (shareable) {
12012 case shareable_none:
12013 return value;
12014
12015 case shareable_literal:
12016 {
12017 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
12018 if (lit) return lit;
12019 return value;
12020 }
12021 break;
12022
12023 case shareable_copy:
12024 case shareable_everything:
12025 {
12026 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
12027 if (lit) return lit;
12028 return make_shareable_node(p, value, shareable == shareable_copy, loc);
12029 }
12030 break;
12031
12032 default:
12033 UNREACHABLE_RETURN(0);
12034 }
12035}
12036
12037static NODE *
12038node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12039{
12040 if (!lhs) return 0;
12041
12042 switch (nd_type(lhs)) {
12043 case NODE_CDECL:
12044 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12045 /* fallthru */
12046
12047 case NODE_GASGN:
12048 case NODE_IASGN:
12049 case NODE_LASGN:
12050 case NODE_DASGN:
12051 case NODE_MASGN:
12052 case NODE_CVASGN:
12053 lhs->nd_value = rhs;
12054 nd_set_loc(lhs, loc);
12055 break;
12056
12057 case NODE_ATTRASGN:
12058 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
12059 nd_set_loc(lhs, loc);
12060 break;
12061
12062 default:
12063 /* should not happen */
12064 break;
12065 }
12066
12067 return lhs;
12068}
12069
12070static NODE *
12071value_expr_check(struct parser_params *p, NODE *node)
12072{
12073 NODE *void_node = 0, *vn;
12074
12075 if (!node) {
12076 rb_warning0("empty expression");
12077 }
12078 while (node) {
12079 switch (nd_type(node)) {
12080 case NODE_RETURN:
12081 case NODE_BREAK:
12082 case NODE_NEXT:
12083 case NODE_REDO:
12084 case NODE_RETRY:
12085 return void_node ? void_node : node;
12086
12087 case NODE_CASE3:
12088 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
12089 compile_error(p, "unexpected node");
12090 return NULL;
12091 }
12092 if (node->nd_body->nd_body) {
12093 return NULL;
12094 }
12095 /* single line pattern matching */
12096 return void_node ? void_node : node;
12097
12098 case NODE_BLOCK:
12099 while (node->nd_next) {
12100 node = node->nd_next;
12101 }
12102 node = node->nd_head;
12103 break;
12104
12105 case NODE_BEGIN:
12106 node = node->nd_body;
12107 break;
12108
12109 case NODE_IF:
12110 case NODE_UNLESS:
12111 if (!node->nd_body) {
12112 return NULL;
12113 }
12114 else if (!node->nd_else) {
12115 return NULL;
12116 }
12117 vn = value_expr_check(p, node->nd_body);
12118 if (!vn) return NULL;
12119 if (!void_node) void_node = vn;
12120 node = node->nd_else;
12121 break;
12122
12123 case NODE_AND:
12124 case NODE_OR:
12125 node = node->nd_1st;
12126 break;
12127
12128 case NODE_LASGN:
12129 case NODE_DASGN:
12130 case NODE_MASGN:
12131 mark_lvar_used(p, node);
12132 return NULL;
12133
12134 default:
12135 return NULL;
12136 }
12137 }
12138
12139 return NULL;
12140}
12141
12142static int
12143value_expr_gen(struct parser_params *p, NODE *node)
12144{
12145 NODE *void_node = value_expr_check(p, node);
12146 if (void_node) {
12147 yyerror1(&void_node->nd_loc, "void value expression");
12148 /* or "control never reach"? */
12149 return FALSE;
12150 }
12151 return TRUE;
12152}
12153static void
12154void_expr(struct parser_params *p, NODE *node)
12155{
12156 const char *useless = 0;
12157
12158 if (!RTEST(ruby_verbose)) return;
12159
12160 if (!node || !(node = nd_once_body(node))) return;
12161 switch (nd_type(node)) {
12162 case NODE_OPCALL:
12163 switch (node->nd_mid) {
12164 case '+':
12165 case '-':
12166 case '*':
12167 case '/':
12168 case '%':
12169 case tPOW:
12170 case tUPLUS:
12171 case tUMINUS:
12172 case '|':
12173 case '^':
12174 case '&':
12175 case tCMP:
12176 case '>':
12177 case tGEQ:
12178 case '<':
12179 case tLEQ:
12180 case tEQ:
12181 case tNEQ:
12182 useless = rb_id2name(node->nd_mid);
12183 break;
12184 }
12185 break;
12186
12187 case NODE_LVAR:
12188 case NODE_DVAR:
12189 case NODE_GVAR:
12190 case NODE_IVAR:
12191 case NODE_CVAR:
12192 case NODE_NTH_REF:
12193 case NODE_BACK_REF:
12194 useless = "a variable";
12195 break;
12196 case NODE_CONST:
12197 useless = "a constant";
12198 break;
12199 case NODE_LIT:
12200 case NODE_STR:
12201 case NODE_DSTR:
12202 case NODE_DREGX:
12203 useless = "a literal";
12204 break;
12205 case NODE_COLON2:
12206 case NODE_COLON3:
12207 useless = "::";
12208 break;
12209 case NODE_DOT2:
12210 useless = "..";
12211 break;
12212 case NODE_DOT3:
12213 useless = "...";
12214 break;
12215 case NODE_SELF:
12216 useless = "self";
12217 break;
12218 case NODE_NIL:
12219 useless = "nil";
12220 break;
12221 case NODE_TRUE:
12222 useless = "true";
12223 break;
12224 case NODE_FALSE:
12225 useless = "false";
12226 break;
12227 case NODE_DEFINED:
12228 useless = "defined?";
12229 break;
12230 }
12231
12232 if (useless) {
12233 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
12234 }
12235}
12236
12237static NODE *
12238void_stmts(struct parser_params *p, NODE *node)
12239{
12240 NODE *const n = node;
12241 if (!RTEST(ruby_verbose)) return n;
12242 if (!node) return n;
12243 if (!nd_type_p(node, NODE_BLOCK)) return n;
12244
12245 while (node->nd_next) {
12246 void_expr(p, node->nd_head);
12247 node = node->nd_next;
12248 }
12249 return n;
12250}
12251
12252static NODE *
12253remove_begin(NODE *node)
12254{
12255 NODE **n = &node, *n1 = node;
12256 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
12257 *n = n1 = n1->nd_body;
12258 }
12259 return node;
12260}
12261
12262static NODE *
12263remove_begin_all(NODE *node)
12264{
12265 NODE **n = &node, *n1 = node;
12266 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
12267 *n = n1 = n1->nd_body;
12268 }
12269 return node;
12270}
12271
12272static void
12273reduce_nodes(struct parser_params *p, NODE **body)
12274{
12275 NODE *node = *body;
12276
12277 if (!node) {
12278 *body = NEW_NIL(&NULL_LOC);
12279 return;
12280 }
12281#define subnodes(n1, n2) \
12282 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
12283 (!node->n2) ? (body = &node->n1, 1) : \
12284 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
12285
12286 while (node) {
12287 int newline = (int)(node->flags & NODE_FL_NEWLINE);
12288 switch (nd_type(node)) {
12289 end:
12290 case NODE_NIL:
12291 *body = 0;
12292 return;
12293 case NODE_RETURN:
12294 *body = node = node->nd_stts;
12295 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12296 continue;
12297 case NODE_BEGIN:
12298 *body = node = node->nd_body;
12299 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12300 continue;
12301 case NODE_BLOCK:
12302 body = &node->nd_end->nd_head;
12303 break;
12304 case NODE_IF:
12305 case NODE_UNLESS:
12306 if (subnodes(nd_body, nd_else)) break;
12307 return;
12308 case NODE_CASE:
12309 body = &node->nd_body;
12310 break;
12311 case NODE_WHEN:
12312 if (!subnodes(nd_body, nd_next)) goto end;
12313 break;
12314 case NODE_ENSURE:
12315 if (!subnodes(nd_head, nd_resq)) goto end;
12316 break;
12317 case NODE_RESCUE:
12318 if (node->nd_else) {
12319 body = &node->nd_resq;
12320 break;
12321 }
12322 if (!subnodes(nd_head, nd_resq)) goto end;
12323 break;
12324 default:
12325 return;
12326 }
12327 node = *body;
12328 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12329 }
12330
12331#undef subnodes
12332}
12333
12334static int
12335is_static_content(NODE *node)
12336{
12337 if (!node) return 1;
12338 switch (nd_type(node)) {
12339 case NODE_HASH:
12340 if (!(node = node->nd_head)) break;
12341 case NODE_LIST:
12342 do {
12343 if (!is_static_content(node->nd_head)) return 0;
12344 } while ((node = node->nd_next) != 0);
12345 case NODE_LIT:
12346 case NODE_STR:
12347 case NODE_NIL:
12348 case NODE_TRUE:
12349 case NODE_FALSE:
12350 case NODE_ZLIST:
12351 break;
12352 default:
12353 return 0;
12354 }
12355 return 1;
12356}
12357
12358static int
12359assign_in_cond(struct parser_params *p, NODE *node)
12360{
12361 switch (nd_type(node)) {
12362 case NODE_MASGN:
12363 case NODE_LASGN:
12364 case NODE_DASGN:
12365 case NODE_GASGN:
12366 case NODE_IASGN:
12367 break;
12368
12369 default:
12370 return 0;
12371 }
12372
12373 if (!node->nd_value) return 1;
12374 if (is_static_content(node->nd_value)) {
12375 /* reports always */
12376 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
12377 }
12378 return 1;
12379}
12380
12381enum cond_type {
12382 COND_IN_OP,
12383 COND_IN_COND,
12384 COND_IN_FF
12385};
12386
12387#define SWITCH_BY_COND_TYPE(t, w, arg) \
12388 switch (t) { \
12389 case COND_IN_OP: break; \
12390 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
12391 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
12392 }
12393
12394static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
12395
12396static NODE*
12397range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12398{
12399 enum node_type type;
12400
12401 if (node == 0) return 0;
12402
12403 type = nd_type(node);
12404 value_expr(node);
12405 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
12406 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
12407 ID lineno = rb_intern("$.");
12408 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
12409 }
12410 return cond0(p, node, COND_IN_FF, loc);
12411}
12412
12413static NODE*
12414cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
12415{
12416 if (node == 0) return 0;
12417 if (!(node = nd_once_body(node))) return 0;
12418 assign_in_cond(p, node);
12419
12420 switch (nd_type(node)) {
12421 case NODE_DSTR:
12422 case NODE_EVSTR:
12423 case NODE_STR:
12424 SWITCH_BY_COND_TYPE(type, warn, "string ")
12425 break;
12426
12427 case NODE_DREGX:
12428 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
12429
12430 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
12431
12432 case NODE_AND:
12433 case NODE_OR:
12434 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
12435 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
12436 break;
12437
12438 case NODE_DOT2:
12439 case NODE_DOT3:
12440 node->nd_beg = range_op(p, node->nd_beg, loc);
12441 node->nd_end = range_op(p, node->nd_end, loc);
12442 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
12443 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
12444 break;
12445
12446 case NODE_DSYM:
12447 warn_symbol:
12448 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
12449 break;
12450
12451 case NODE_LIT:
12452 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
12453 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
12454 nd_set_type(node, NODE_MATCH);
12455 }
12456 else if (node->nd_lit == Qtrue ||
12457 node->nd_lit == Qfalse) {
12458 /* booleans are OK, e.g., while true */
12459 }
12460 else if (SYMBOL_P(node->nd_lit)) {
12461 goto warn_symbol;
12462 }
12463 else {
12464 SWITCH_BY_COND_TYPE(type, warning, "")
12465 }
12466 default:
12467 break;
12468 }
12469 return node;
12470}
12471
12472static NODE*
12473cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12474{
12475 if (node == 0) return 0;
12476 return cond0(p, node, COND_IN_COND, loc);
12477}
12478
12479static NODE*
12480method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12481{
12482 if (node == 0) return 0;
12483 return cond0(p, node, COND_IN_OP, loc);
12484}
12485
12486static NODE*
12487new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
12488{
12489 YYLTYPE loc = {*pos, *pos};
12490 return NEW_NIL(&loc);
12491}
12492
12493static NODE*
12494new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12495{
12496 if (!cc) return right;
12497 cc = cond0(p, cc, COND_IN_COND, loc);
12498 return newline_node(NEW_IF(cc, left, right, loc));
12499}
12500
12501static NODE*
12502new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12503{
12504 if (!cc) return right;
12505 cc = cond0(p, cc, COND_IN_COND, loc);
12506 return newline_node(NEW_UNLESS(cc, left, right, loc));
12507}
12508
12509static NODE*
12510logop(struct parser_params *p, ID id, NODE *left, NODE *right,
12511 const YYLTYPE *op_loc, const YYLTYPE *loc)
12512{
12513 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
12514 NODE *op;
12515 value_expr(left);
12516 if (left && nd_type_p(left, type)) {
12517 NODE *node = left, *second;
12518 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
12519 node = second;
12520 }
12521 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
12522 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
12523 left->nd_loc.end_pos = loc->end_pos;
12524 return left;
12525 }
12526 op = NEW_NODE(type, left, right, 0, loc);
12527 nd_set_line(op, op_loc->beg_pos.lineno);
12528 return op;
12529}
12530
12531static void
12532no_blockarg(struct parser_params *p, NODE *node)
12533{
12534 if (nd_type_p(node, NODE_BLOCK_PASS)) {
12535 compile_error(p, "block argument should not be given");
12536 }
12537}
12538
12539static NODE *
12540ret_args(struct parser_params *p, NODE *node)
12541{
12542 if (node) {
12543 no_blockarg(p, node);
12544 if (nd_type_p(node, NODE_LIST)) {
12545 if (node->nd_next == 0) {
12546 node = node->nd_head;
12547 }
12548 else {
12549 nd_set_type(node, NODE_VALUES);
12550 }
12551 }
12552 }
12553 return node;
12554}
12555
12556static NODE *
12557new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12558{
12559 if (node) no_blockarg(p, node);
12560
12561 return NEW_YIELD(node, loc);
12562}
12563
12564static VALUE
12565negate_lit(struct parser_params *p, VALUE lit)
12566{
12567 if (FIXNUM_P(lit)) {
12568 return LONG2FIX(-FIX2LONG(lit));
12569 }
12570 if (SPECIAL_CONST_P(lit)) {
12571#if USE_FLONUM
12572 if (FLONUM_P(lit)) {
12573 return DBL2NUM(-RFLOAT_VALUE(lit));
12574 }
12575#endif
12576 goto unknown;
12577 }
12578 switch (BUILTIN_TYPE(lit)) {
12579 case T_BIGNUM:
12580 BIGNUM_NEGATE(lit);
12581 lit = rb_big_norm(lit);
12582 break;
12583 case T_RATIONAL:
12584 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
12585 break;
12586 case T_COMPLEX:
12587 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
12588 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
12589 break;
12590 case T_FLOAT:
12591 lit = DBL2NUM(-RFLOAT_VALUE(lit));
12592 break;
12593 unknown:
12594 default:
12595 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
12596 rb_builtin_class_name(lit));
12597 break;
12598 }
12599 return lit;
12600}
12601
12602static NODE *
12603arg_blk_pass(NODE *node1, NODE *node2)
12604{
12605 if (node2) {
12606 if (!node1) return node2;
12607 node2->nd_head = node1;
12608 nd_set_first_lineno(node2, nd_first_lineno(node1));
12609 nd_set_first_column(node2, nd_first_column(node1));
12610 return node2;
12611 }
12612 return node1;
12613}
12614
12615static bool
12616args_info_empty_p(struct rb_args_info *args)
12617{
12618 if (args->pre_args_num) return false;
12619 if (args->post_args_num) return false;
12620 if (args->rest_arg) return false;
12621 if (args->opt_args) return false;
12622 if (args->block_arg) return false;
12623 if (args->kw_args) return false;
12624 if (args->kw_rest_arg) return false;
12625 return true;
12626}
12627
12628static NODE*
12629new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12630{
12631 int saved_line = p->ruby_sourceline;
12632 struct rb_args_info *args = tail->nd_ainfo;
12633
12634 if (args->forwarding) {
12635 if (rest_arg) {
12636 yyerror1(&tail->nd_loc, "... after rest argument");
12637 return tail;
12638 }
12639 rest_arg = idFWD_REST;
12640 }
12641
12642 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12643 args->pre_init = pre_args ? pre_args->nd_next : 0;
12644
12645 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12646 args->post_init = post_args ? post_args->nd_next : 0;
12647 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12648
12649 args->rest_arg = rest_arg;
12650
12651 args->opt_args = opt_args;
12652
12653#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
12654 args->ruby2_keywords = args->forwarding;
12655#else
12656 args->ruby2_keywords = 0;
12657#endif
12658
12659 p->ruby_sourceline = saved_line;
12660 nd_set_loc(tail, loc);
12661
12662 return tail;
12663}
12664
12665static NODE*
12666new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12667{
12668 int saved_line = p->ruby_sourceline;
12669 NODE *node;
12670 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12671 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12672 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12673 args->imemo = tmpbuf;
12674 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12675 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12676 if (p->error_p) return node;
12677
12678 args->block_arg = block;
12679 args->kw_args = kw_args;
12680
12681 if (kw_args) {
12682 /*
12683 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12684 * variable order: k1, kr1, k2, &b, internal_id, krest
12685 * #=> <reorder>
12686 * variable order: kr1, k1, k2, internal_id, krest, &b
12687 */
12688 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12689 struct vtable *vtargs = p->lvtbl->args;
12690 NODE *kwn = kw_args;
12691
12692 if (block) block = vtargs->tbl[vtargs->pos-1];
12693 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12694 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12695 while (kwn) {
12696 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12697 --kw_vars;
12698 --required_kw_vars;
12699 kwn = kwn->nd_next;
12700 }
12701
12702 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12703 ID vid = kwn->nd_body->nd_vid;
12704 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12705 *required_kw_vars++ = vid;
12706 }
12707 else {
12708 *kw_vars++ = vid;
12709 }
12710 }
12711
12712 arg_var(p, kw_bits);
12713 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12714 if (block) arg_var(p, block);
12715
12716 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12717 args->kw_rest_arg->nd_cflag = kw_bits;
12718 }
12719 else if (kw_rest_arg == idNil) {
12720 args->no_kwarg = 1;
12721 }
12722 else if (kw_rest_arg) {
12723 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12724 }
12725
12726 p->ruby_sourceline = saved_line;
12727 return node;
12728}
12729
12730static NODE *
12731args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12732{
12733 if (max_numparam > NO_PARAM) {
12734 if (!args) {
12735 YYLTYPE loc = RUBY_INIT_YYLLOC();
12736 args = new_args_tail(p, 0, 0, 0, 0);
12737 nd_set_loc(args, &loc);
12738 }
12739 args->nd_ainfo->pre_args_num = max_numparam;
12740 }
12741 return args;
12742}
12743
12744static NODE*
12745new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12746{
12747 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12748
12749 aryptn->nd_pconst = constant;
12750
12751 if (pre_arg) {
12752 NODE *pre_args = NEW_LIST(pre_arg, loc);
12753 if (apinfo->pre_args) {
12754 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12755 }
12756 else {
12757 apinfo->pre_args = pre_args;
12758 }
12759 }
12760 return aryptn;
12761}
12762
12763static NODE*
12764new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12765{
12766 int saved_line = p->ruby_sourceline;
12767 NODE *node;
12768 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12769 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12770 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12771 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12772 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12773
12774 apinfo->pre_args = pre_args;
12775
12776 if (has_rest) {
12777 if (rest_arg) {
12778 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12779 }
12780 else {
12781 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12782 }
12783 }
12784 else {
12785 apinfo->rest_arg = NULL;
12786 }
12787
12788 apinfo->post_args = post_args;
12789
12790 p->ruby_sourceline = saved_line;
12791 return node;
12792}
12793
12794static NODE*
12795new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12796{
12797 fndptn->nd_pconst = constant;
12798
12799 return fndptn;
12800}
12801
12802static NODE*
12803new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12804{
12805 int saved_line = p->ruby_sourceline;
12806 NODE *node;
12807 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12808 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12809 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12810 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12811 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12812
12813 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12814 fpinfo->args = args;
12815 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12816
12817 p->ruby_sourceline = saved_line;
12818 return node;
12819}
12820
12821static NODE*
12822new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12823{
12824 hshptn->nd_pconst = constant;
12825 return hshptn;
12826}
12827
12828static NODE*
12829new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12830{
12831 int saved_line = p->ruby_sourceline;
12832 NODE *node, *kw_rest_arg_node;
12833
12834 if (kw_rest_arg == idNil) {
12835 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12836 }
12837 else if (kw_rest_arg) {
12838 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12839 }
12840 else {
12841 kw_rest_arg_node = NULL;
12842 }
12843
12844 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12845
12846 p->ruby_sourceline = saved_line;
12847 return node;
12848}
12849
12850static NODE*
12851dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12852{
12853 VALUE lit;
12854
12855 if (!node) {
12856 return NEW_LIT(ID2SYM(idNULL), loc);
12857 }
12858
12859 switch (nd_type(node)) {
12860 case NODE_DSTR:
12861 nd_set_type(node, NODE_DSYM);
12862 nd_set_loc(node, loc);
12863 break;
12864 case NODE_STR:
12865 lit = node->nd_lit;
12866 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12867 nd_set_type(node, NODE_LIT);
12868 nd_set_loc(node, loc);
12869 break;
12870 default:
12871 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12872 break;
12873 }
12874 return node;
12875}
12876
12877static int
12878append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12879{
12880 NODE *node = (NODE *)v;
12881 NODE **result = (NODE **)h;
12882 node->nd_alen = 2;
12883 node->nd_next->nd_end = node->nd_next;
12884 node->nd_next->nd_next = 0;
12885 if (*result)
12886 list_concat(*result, node);
12887 else
12888 *result = node;
12889 return ST_CONTINUE;
12890}
12891
12892static bool
12893hash_literal_key_p(VALUE k)
12894{
12895 switch (OBJ_BUILTIN_TYPE(k)) {
12896 case T_NODE:
12897 return false;
12898 default:
12899 return true;
12900 }
12901}
12902
12903static int
12904literal_cmp(VALUE val, VALUE lit)
12905{
12906 if (val == lit) return 0;
12907 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12908 return rb_iseq_cdhash_cmp(val, lit);
12909}
12910
12911static st_index_t
12912literal_hash(VALUE a)
12913{
12914 if (!hash_literal_key_p(a)) return (st_index_t)a;
12915 return rb_iseq_cdhash_hash(a);
12916}
12917
12918static const struct st_hash_type literal_type = {
12919 literal_cmp,
12920 literal_hash,
12921};
12922
12923static NODE *
12924remove_duplicate_keys(struct parser_params *p, NODE *hash)
12925{
12926 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12927 NODE *result = 0;
12928 NODE *last_expr = 0;
12929 rb_code_location_t loc = hash->nd_loc;
12930 while (hash && hash->nd_head && hash->nd_next) {
12931 NODE *head = hash->nd_head;
12932 NODE *value = hash->nd_next;
12933 NODE *next = value->nd_next;
12934 st_data_t key = (st_data_t)head;
12935 st_data_t data;
12936 value->nd_next = 0;
12937 if (nd_type_p(head, NODE_LIT) &&
12938 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12939 NODE *dup_value = ((NODE *)data)->nd_next;
12940 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12941 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12942 head->nd_lit, nd_line(head));
12943 if (dup_value == last_expr) {
12944 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12945 }
12946 else {
12947 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12948 }
12949 }
12950 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12951 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12952 hash = next;
12953 }
12954 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12955 st_free_table(literal_keys);
12956 if (hash) {
12957 if (!result) result = hash;
12958 else list_concat(result, hash);
12959 }
12960 result->nd_loc = loc;
12961 return result;
12962}
12963
12964static NODE *
12965new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12966{
12967 if (hash) hash = remove_duplicate_keys(p, hash);
12968 return NEW_HASH(hash, loc);
12969}
12970#endif
12971
12972static void
12973error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12974{
12975 if (is_private_local_id(id)) {
12976 return;
12977 }
12978 if (st_is_member(p->pvtbl, id)) {
12979 yyerror1(loc, "duplicated variable name");
12980 }
12981 else {
12982 st_insert(p->pvtbl, (st_data_t)id, 0);
12983 }
12984}
12985
12986static void
12987error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12988{
12989 if (!p->pktbl) {
12990 p->pktbl = st_init_numtable();
12991 }
12992 else if (st_is_member(p->pktbl, key)) {
12993 yyerror1(loc, "duplicated key name");
12994 return;
12995 }
12996 st_insert(p->pktbl, (st_data_t)key, 0);
12997}
12998
12999#ifndef RIPPER
13000static NODE *
13001new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
13002{
13003 return NEW_HASH(hash, loc);
13004}
13005#endif /* !RIPPER */
13006
13007#ifndef RIPPER
13008static NODE *
13009new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13010{
13011 NODE *asgn;
13012
13013 if (lhs) {
13014 ID vid = lhs->nd_vid;
13015 YYLTYPE lhs_loc = lhs->nd_loc;
13016 int shareable = ctxt.shareable_constant_value;
13017 if (shareable) {
13018 switch (nd_type(lhs)) {
13019 case NODE_CDECL:
13020 case NODE_COLON2:
13021 case NODE_COLON3:
13022 break;
13023 default:
13024 shareable = 0;
13025 break;
13026 }
13027 }
13028 if (op == tOROP) {
13029 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13030 lhs->nd_value = rhs;
13031 nd_set_loc(lhs, loc);
13032 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
13033 if (is_notop_id(vid)) {
13034 switch (id_type(vid)) {
13035 case ID_GLOBAL:
13036 case ID_INSTANCE:
13037 case ID_CLASS:
13038 asgn->nd_aid = vid;
13039 }
13040 }
13041 }
13042 else if (op == tANDOP) {
13043 if (shareable) {
13044 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13045 }
13046 lhs->nd_value = rhs;
13047 nd_set_loc(lhs, loc);
13048 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
13049 }
13050 else {
13051 asgn = lhs;
13052 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
13053 if (shareable) {
13054 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13055 }
13056 asgn->nd_value = rhs;
13057 nd_set_loc(asgn, loc);
13058 }
13059 }
13060 else {
13061 asgn = NEW_BEGIN(0, loc);
13062 }
13063 return asgn;
13064}
13065
13066static NODE *
13067new_ary_op_assign(struct parser_params *p, NODE *ary,
13068 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
13069{
13070 NODE *asgn;
13071
13072 args = make_list(args, args_loc);
13073 if (nd_type_p(args, NODE_BLOCK_PASS)) {
13074 args = NEW_ARGSCAT(args, rhs, loc);
13075 }
13076 else {
13077 args = arg_concat(p, args, rhs, loc);
13078 }
13079 asgn = NEW_OP_ASGN1(ary, op, args, loc);
13080 fixpos(asgn, ary);
13081 return asgn;
13082}
13083
13084static NODE *
13085new_attr_op_assign(struct parser_params *p, NODE *lhs,
13086 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
13087{
13088 NODE *asgn;
13089
13090 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
13091 fixpos(asgn, lhs);
13092 return asgn;
13093}
13094
13095static NODE *
13096new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13097{
13098 NODE *asgn;
13099
13100 if (lhs) {
13101 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13102 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
13103 }
13104 else {
13105 asgn = NEW_BEGIN(0, loc);
13106 }
13107 fixpos(asgn, lhs);
13108 return asgn;
13109}
13110
13111static NODE *
13112const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
13113{
13114 if (p->ctxt.in_def) {
13115 yyerror1(loc, "dynamic constant assignment");
13116 }
13117 return NEW_CDECL(0, 0, (path), loc);
13118}
13119#else
13120static VALUE
13121const_decl(struct parser_params *p, VALUE path)
13122{
13123 if (p->ctxt.in_def) {
13124 path = assign_error(p, "dynamic constant assignment", path);
13125 }
13126 return path;
13127}
13128
13129static VALUE
13130assign_error(struct parser_params *p, const char *mesg, VALUE a)
13131{
13132 a = dispatch2(assign_error, ERR_MESG(), a);
13133 ripper_error(p);
13134 return a;
13135}
13136
13137static VALUE
13138var_field(struct parser_params *p, VALUE a)
13139{
13140 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
13141}
13142#endif
13143
13144#ifndef RIPPER
13145static NODE *
13146new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
13147{
13148 NODE *result = head;
13149 if (rescue) {
13150 NODE *tmp = rescue_else ? rescue_else : rescue;
13151 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
13152
13153 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
13154 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
13155 }
13156 else if (rescue_else) {
13157 result = block_append(p, result, rescue_else);
13158 }
13159 if (ensure) {
13160 result = NEW_ENSURE(result, ensure, loc);
13161 }
13162 fixpos(result, head);
13163 return result;
13164}
13165#endif
13166
13167static void
13168warn_unused_var(struct parser_params *p, struct local_vars *local)
13169{
13170 int cnt;
13171
13172 if (!local->used) return;
13173 cnt = local->used->pos;
13174 if (cnt != local->vars->pos) {
13175 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
13176 }
13177#ifndef RIPPER
13178 ID *v = local->vars->tbl;
13179 ID *u = local->used->tbl;
13180 for (int i = 0; i < cnt; ++i) {
13181 if (!v[i] || (u[i] & LVAR_USED)) continue;
13182 if (is_private_local_id(v[i])) continue;
13183 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
13184 }
13185#endif
13186}
13187
13188static void
13189local_push(struct parser_params *p, int toplevel_scope)
13190{
13191 struct local_vars *local;
13192 int inherits_dvars = toplevel_scope && compile_for_eval;
13193 int warn_unused_vars = RTEST(ruby_verbose);
13194
13195 local = ALLOC(struct local_vars);
13196 local->prev = p->lvtbl;
13197 local->args = vtable_alloc(0);
13198 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
13199#ifndef RIPPER
13200 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
13201 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
13202 local->numparam.outer = 0;
13203 local->numparam.inner = 0;
13204 local->numparam.current = 0;
13205#endif
13206 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
13207
13208# if WARN_PAST_SCOPE
13209 local->past = 0;
13210# endif
13211 CMDARG_PUSH(0);
13212 COND_PUSH(0);
13213 p->lvtbl = local;
13214}
13215
13216static void
13217vtable_chain_free(struct parser_params *p, struct vtable *table)
13218{
13219 while (!DVARS_TERMINAL_P(table)) {
13220 struct vtable *cur_table = table;
13221 table = cur_table->prev;
13222 vtable_free(cur_table);
13223 }
13224}
13225
13226static void
13227local_free(struct parser_params *p, struct local_vars *local)
13228{
13229 vtable_chain_free(p, local->used);
13230
13231# if WARN_PAST_SCOPE
13232 vtable_chain_free(p, local->past);
13233# endif
13234
13235 vtable_chain_free(p, local->args);
13236 vtable_chain_free(p, local->vars);
13237
13238 ruby_sized_xfree(local, sizeof(struct local_vars));
13239}
13240
13241static void
13242local_pop(struct parser_params *p)
13243{
13244 struct local_vars *local = p->lvtbl->prev;
13245 if (p->lvtbl->used) {
13246 warn_unused_var(p, p->lvtbl);
13247 }
13248
13249 local_free(p, p->lvtbl);
13250 p->lvtbl = local;
13251
13252 CMDARG_POP();
13253 COND_POP();
13254}
13255
13256#ifndef RIPPER
13257static rb_ast_id_table_t *
13258local_tbl(struct parser_params *p)
13259{
13260 int cnt_args = vtable_size(p->lvtbl->args);
13261 int cnt_vars = vtable_size(p->lvtbl->vars);
13262 int cnt = cnt_args + cnt_vars;
13263 int i, j;
13264 rb_ast_id_table_t *tbl;
13265
13266 if (cnt <= 0) return 0;
13267 tbl = rb_ast_new_local_table(p->ast, cnt);
13268 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
13269 /* remove IDs duplicated to warn shadowing */
13270 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
13271 ID id = p->lvtbl->vars->tbl[i];
13272 if (!vtable_included(p->lvtbl->args, id)) {
13273 tbl->ids[j++] = id;
13274 }
13275 }
13276 if (j < cnt) {
13277 tbl = rb_ast_resize_latest_local_table(p->ast, j);
13278 }
13279
13280 return tbl;
13281}
13282
13283static NODE*
13284node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
13285{
13286 rb_ast_id_table_t *a0;
13287 NODE *n;
13288
13289 a0 = local_tbl(p);
13290 n = NEW_NODE(type, a0, a1, a2, loc);
13291 return n;
13292}
13293
13294#endif
13295
13296static void
13297numparam_name(struct parser_params *p, ID id)
13298{
13299 if (!NUMPARAM_ID_P(id)) return;
13300 compile_error(p, "_%d is reserved for numbered parameter",
13301 NUMPARAM_ID_TO_IDX(id));
13302}
13303
13304static void
13305arg_var(struct parser_params *p, ID id)
13306{
13307 numparam_name(p, id);
13308 vtable_add(p->lvtbl->args, id);
13309}
13310
13311static void
13312local_var(struct parser_params *p, ID id)
13313{
13314 numparam_name(p, id);
13315 vtable_add(p->lvtbl->vars, id);
13316 if (p->lvtbl->used) {
13317 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
13318 }
13319}
13320
13321static int
13322local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
13323{
13324 struct vtable *vars, *args, *used;
13325
13326 vars = p->lvtbl->vars;
13327 args = p->lvtbl->args;
13328 used = p->lvtbl->used;
13329
13330 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
13331 vars = vars->prev;
13332 args = args->prev;
13333 if (used) used = used->prev;
13334 }
13335
13336 if (vars && vars->prev == DVARS_INHERIT) {
13337 return rb_local_defined(id, p->parent_iseq);
13338 }
13339 else if (vtable_included(args, id)) {
13340 return 1;
13341 }
13342 else {
13343 int i = vtable_included(vars, id);
13344 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
13345 return i != 0;
13346 }
13347}
13348
13349static int
13350local_id(struct parser_params *p, ID id)
13351{
13352 return local_id_ref(p, id, NULL);
13353}
13354
13355static int
13356check_forwarding_args(struct parser_params *p)
13357{
13358 if (local_id(p, idFWD_ALL)) return TRUE;
13359 compile_error(p, "unexpected ...");
13360 return FALSE;
13361}
13362
13363static void
13364add_forwarding_args(struct parser_params *p)
13365{
13366 arg_var(p, idFWD_REST);
13367#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13368 arg_var(p, idFWD_KWREST);
13369#endif
13370 arg_var(p, idFWD_BLOCK);
13371 arg_var(p, idFWD_ALL);
13372}
13373
13374#ifndef RIPPER
13375static NODE *
13376new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
13377{
13378 NODE *rest = NEW_LVAR(idFWD_REST, loc);
13379#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13380 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
13381#endif
13382 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
13383 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
13384#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13385 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
13386#endif
13387 return arg_blk_pass(args, block);
13388}
13389#endif
13390
13391static NODE *
13392numparam_push(struct parser_params *p)
13393{
13394#ifndef RIPPER
13395 struct local_vars *local = p->lvtbl;
13396 NODE *inner = local->numparam.inner;
13397 if (!local->numparam.outer) {
13398 local->numparam.outer = local->numparam.current;
13399 }
13400 local->numparam.inner = 0;
13401 local->numparam.current = 0;
13402 return inner;
13403#else
13404 return 0;
13405#endif
13406}
13407
13408static void
13409numparam_pop(struct parser_params *p, NODE *prev_inner)
13410{
13411#ifndef RIPPER
13412 struct local_vars *local = p->lvtbl;
13413 if (prev_inner) {
13414 /* prefer first one */
13415 local->numparam.inner = prev_inner;
13416 }
13417 else if (local->numparam.current) {
13418 /* current and inner are exclusive */
13419 local->numparam.inner = local->numparam.current;
13420 }
13421 if (p->max_numparam > NO_PARAM) {
13422 /* current and outer are exclusive */
13423 local->numparam.current = local->numparam.outer;
13424 local->numparam.outer = 0;
13425 }
13426 else {
13427 /* no numbered parameter */
13428 local->numparam.current = 0;
13429 }
13430#endif
13431}
13432
13433static const struct vtable *
13434dyna_push(struct parser_params *p)
13435{
13436 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
13437 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
13438 if (p->lvtbl->used) {
13439 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
13440 }
13441 return p->lvtbl->args;
13442}
13443
13444static void
13445dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
13446{
13447 struct vtable *tmp = *vtblp;
13448 *vtblp = tmp->prev;
13449# if WARN_PAST_SCOPE
13450 if (p->past_scope_enabled) {
13451 tmp->prev = p->lvtbl->past;
13452 p->lvtbl->past = tmp;
13453 return;
13454 }
13455# endif
13456 vtable_free(tmp);
13457}
13458
13459static void
13460dyna_pop_1(struct parser_params *p)
13461{
13462 struct vtable *tmp;
13463
13464 if ((tmp = p->lvtbl->used) != 0) {
13465 warn_unused_var(p, p->lvtbl);
13466 p->lvtbl->used = p->lvtbl->used->prev;
13467 vtable_free(tmp);
13468 }
13469 dyna_pop_vtable(p, &p->lvtbl->args);
13470 dyna_pop_vtable(p, &p->lvtbl->vars);
13471}
13472
13473static void
13474dyna_pop(struct parser_params *p, const struct vtable *lvargs)
13475{
13476 while (p->lvtbl->args != lvargs) {
13477 dyna_pop_1(p);
13478 if (!p->lvtbl->args) {
13479 struct local_vars *local = p->lvtbl->prev;
13480 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13481 p->lvtbl = local;
13482 }
13483 }
13484 dyna_pop_1(p);
13485}
13486
13487static int
13488dyna_in_block(struct parser_params *p)
13489{
13490 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
13491}
13492
13493static int
13494dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
13495{
13496 struct vtable *vars, *args, *used;
13497 int i;
13498
13499 args = p->lvtbl->args;
13500 vars = p->lvtbl->vars;
13501 used = p->lvtbl->used;
13502
13503 while (!DVARS_TERMINAL_P(vars)) {
13504 if (vtable_included(args, id)) {
13505 return 1;
13506 }
13507 if ((i = vtable_included(vars, id)) != 0) {
13508 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
13509 return 1;
13510 }
13511 args = args->prev;
13512 vars = vars->prev;
13513 if (!vidrefp) used = 0;
13514 if (used) used = used->prev;
13515 }
13516
13517 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
13518 return rb_dvar_defined(id, p->parent_iseq);
13519 }
13520
13521 return 0;
13522}
13523
13524static int
13525dvar_defined(struct parser_params *p, ID id)
13526{
13527 return dvar_defined_ref(p, id, NULL);
13528}
13529
13530static int
13531dvar_curr(struct parser_params *p, ID id)
13532{
13533 return (vtable_included(p->lvtbl->args, id) ||
13534 vtable_included(p->lvtbl->vars, id));
13535}
13536
13537static void
13538reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
13539{
13540 compile_error(p,
13541 "regexp encoding option '%c' differs from source encoding '%s'",
13542 c, rb_enc_name(rb_enc_get(str)));
13543}
13544
13545#ifndef RIPPER
13546int
13547rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13548{
13549 int c = RE_OPTION_ENCODING_IDX(options);
13550
13551 if (c) {
13552 int opt, idx;
13553 rb_char_to_option_kcode(c, &opt, &idx);
13554 if (idx != ENCODING_GET(str) &&
13555 !is_ascii_string(str)) {
13556 goto error;
13557 }
13558 ENCODING_SET(str, idx);
13559 }
13560 else if (RE_OPTION_ENCODING_NONE(options)) {
13561 if (!ENCODING_IS_ASCII8BIT(str) &&
13562 !is_ascii_string(str)) {
13563 c = 'n';
13564 goto error;
13565 }
13566 rb_enc_associate(str, rb_ascii8bit_encoding());
13567 }
13568 else if (rb_is_usascii_enc(p->enc)) {
13569 if (!is_ascii_string(str)) {
13570 /* raise in re.c */
13571 rb_enc_associate(str, rb_usascii_encoding());
13572 }
13573 else {
13574 rb_enc_associate(str, rb_ascii8bit_encoding());
13575 }
13576 }
13577 return 0;
13578
13579 error:
13580 return c;
13581}
13582
13583static void
13584reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13585{
13586 int c = rb_reg_fragment_setenc(p, str, options);
13587 if (c) reg_fragment_enc_error(p, str, c);
13588}
13589
13590static int
13591reg_fragment_check(struct parser_params* p, VALUE str, int options)
13592{
13593 VALUE err;
13594 reg_fragment_setenc(p, str, options);
13595 err = rb_reg_check_preprocess(str);
13596 if (err != Qnil) {
13597 err = rb_obj_as_string(err);
13598 compile_error(p, "%"PRIsVALUE, err);
13599 return 0;
13600 }
13601 return 1;
13602}
13603
13604typedef struct {
13605 struct parser_params* parser;
13606 rb_encoding *enc;
13607 NODE *succ_block;
13608 const YYLTYPE *loc;
13609} reg_named_capture_assign_t;
13610
13611static int
13612reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
13613 int back_num, int *back_refs, OnigRegex regex, void *arg0)
13614{
13615 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
13616 struct parser_params* p = arg->parser;
13617 rb_encoding *enc = arg->enc;
13618 long len = name_end - name;
13619 const char *s = (const char *)name;
13620 ID var;
13621 NODE *node, *succ;
13622
13623 if (!len) return ST_CONTINUE;
13624 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13625 return ST_CONTINUE;
13626
13627 var = intern_cstr(s, len, enc);
13628 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13629 if (!lvar_defined(p, var)) return ST_CONTINUE;
13630 }
13631 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13632 succ = arg->succ_block;
13633 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13634 succ = block_append(p, succ, node);
13635 arg->succ_block = succ;
13636 return ST_CONTINUE;
13637}
13638
13639static NODE *
13640reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13641{
13642 reg_named_capture_assign_t arg;
13643
13644 arg.parser = p;
13645 arg.enc = rb_enc_get(regexp);
13646 arg.succ_block = 0;
13647 arg.loc = loc;
13648 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13649
13650 if (!arg.succ_block) return 0;
13651 return arg.succ_block->nd_next;
13652}
13653
13654static VALUE
13655parser_reg_compile(struct parser_params* p, VALUE str, int options)
13656{
13657 reg_fragment_setenc(p, str, options);
13658 return rb_parser_reg_compile(p, str, options);
13659}
13660
13661VALUE
13662rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13663{
13664 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13665}
13666
13667static VALUE
13668reg_compile(struct parser_params* p, VALUE str, int options)
13669{
13670 VALUE re;
13671 VALUE err;
13672
13673 err = rb_errinfo();
13674 re = parser_reg_compile(p, str, options);
13675 if (NIL_P(re)) {
13676 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13677 rb_set_errinfo(err);
13678 compile_error(p, "%"PRIsVALUE, m);
13679 return Qnil;
13680 }
13681 return re;
13682}
13683#else
13684static VALUE
13685parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13686{
13687 VALUE err = rb_errinfo();
13688 VALUE re;
13689 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13690 int c = rb_reg_fragment_setenc(p, str, options);
13691 if (c) reg_fragment_enc_error(p, str, c);
13692 re = rb_parser_reg_compile(p, str, options);
13693 if (NIL_P(re)) {
13694 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13695 rb_set_errinfo(err);
13696 }
13697 return re;
13698}
13699#endif
13700
13701#ifndef RIPPER
13702void
13703rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13704{
13705 struct parser_params *p;
13706 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13707 p->do_print = print;
13708 p->do_loop = loop;
13709 p->do_chomp = chomp;
13710 p->do_split = split;
13711}
13712
13713static NODE *
13714parser_append_options(struct parser_params *p, NODE *node)
13715{
13716 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13717 const YYLTYPE *const LOC = &default_location;
13718
13719 if (p->do_print) {
13720 NODE *print = NEW_FCALL(rb_intern("print"),
13721 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13722 LOC);
13723 node = block_append(p, node, print);
13724 }
13725
13726 if (p->do_loop) {
13727 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
13728
13729 if (p->do_split) {
13730 ID ifs = rb_intern("$;");
13731 ID fields = rb_intern("$F");
13732 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13733 NODE *split = NEW_GASGN(fields,
13734 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13735 rb_intern("split"), args, LOC),
13736 LOC);
13737 node = block_append(p, split, node);
13738 }
13739 if (p->do_chomp) {
13740 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
13741 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
13742 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
13743 }
13744
13745 node = NEW_WHILE(NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
13746 }
13747
13748 return node;
13749}
13750
13751void
13752rb_init_parse(void)
13753{
13754 /* just to suppress unused-function warnings */
13755 (void)nodetype;
13756 (void)nodeline;
13757}
13758
13759static ID
13760internal_id(struct parser_params *p)
13761{
13762 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13763}
13764#endif /* !RIPPER */
13765
13766static void
13767parser_initialize(struct parser_params *p)
13768{
13769 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13770 p->command_start = TRUE;
13771 p->ruby_sourcefile_string = Qnil;
13772 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13773 p->node_id = 0;
13774 p->delayed.token = Qnil;
13775#ifdef RIPPER
13776 p->result = Qnil;
13777 p->parsing_thread = Qnil;
13778#else
13779 p->error_buffer = Qfalse;
13780 p->end_expect_token_locations = Qnil;
13781 p->token_id = 0;
13782 p->tokens = Qnil;
13783#endif
13784 p->debug_buffer = Qnil;
13785 p->debug_output = rb_ractor_stdout();
13786 p->enc = rb_utf8_encoding();
13787}
13788
13789#ifdef RIPPER
13790#define parser_mark ripper_parser_mark
13791#define parser_free ripper_parser_free
13792#endif
13793
13794static void
13795parser_mark(void *ptr)
13796{
13797 struct parser_params *p = (struct parser_params*)ptr;
13798
13799 rb_gc_mark(p->lex.input);
13800 rb_gc_mark(p->lex.lastline);
13801 rb_gc_mark(p->lex.nextline);
13802 rb_gc_mark(p->ruby_sourcefile_string);
13803 rb_gc_mark((VALUE)p->lex.strterm);
13804 rb_gc_mark((VALUE)p->ast);
13805 rb_gc_mark(p->case_labels);
13806 rb_gc_mark(p->delayed.token);
13807#ifndef RIPPER
13808 rb_gc_mark(p->debug_lines);
13809 rb_gc_mark(p->compile_option);
13810 rb_gc_mark(p->error_buffer);
13811 rb_gc_mark(p->end_expect_token_locations);
13812 rb_gc_mark(p->tokens);
13813#else
13814 rb_gc_mark(p->value);
13815 rb_gc_mark(p->result);
13816 rb_gc_mark(p->parsing_thread);
13817#endif
13818 rb_gc_mark(p->debug_buffer);
13819 rb_gc_mark(p->debug_output);
13820#ifdef YYMALLOC
13821 rb_gc_mark((VALUE)p->heap);
13822#endif
13823}
13824
13825static void
13826parser_free(void *ptr)
13827{
13828 struct parser_params *p = (struct parser_params*)ptr;
13829 struct local_vars *local, *prev;
13830
13831 if (p->tokenbuf) {
13832 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13833 }
13834
13835 for (local = p->lvtbl; local; local = prev) {
13836 prev = local->prev;
13837 local_free(p, local);
13838 }
13839
13840 {
13841 token_info *ptinfo;
13842 while ((ptinfo = p->token_info) != 0) {
13843 p->token_info = ptinfo->next;
13844 xfree(ptinfo);
13845 }
13846 }
13847 xfree(ptr);
13848}
13849
13850static size_t
13851parser_memsize(const void *ptr)
13852{
13853 struct parser_params *p = (struct parser_params*)ptr;
13854 struct local_vars *local;
13855 size_t size = sizeof(*p);
13856
13857 size += p->toksiz;
13858 for (local = p->lvtbl; local; local = local->prev) {
13859 size += sizeof(*local);
13860 if (local->vars) size += local->vars->capa * sizeof(ID);
13861 }
13862 return size;
13863}
13864
13865static const rb_data_type_t parser_data_type = {
13866#ifndef RIPPER
13867 "parser",
13868#else
13869 "ripper",
13870#endif
13871 {
13872 parser_mark,
13873 parser_free,
13874 parser_memsize,
13875 },
13876 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13877};
13878
13879#ifndef RIPPER
13880#undef rb_reserved_word
13881
13882const struct kwtable *
13883rb_reserved_word(const char *str, unsigned int len)
13884{
13885 return reserved_word(str, len);
13886}
13887
13888VALUE
13889rb_parser_new(void)
13890{
13891 struct parser_params *p;
13892 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13893 &parser_data_type, p);
13894 parser_initialize(p);
13895 return parser;
13896}
13897
13898VALUE
13899rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13900{
13901 struct parser_params *p;
13902
13903 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13904 p->error_buffer = main ? Qfalse : Qnil;
13905 p->parent_iseq = base;
13906 return vparser;
13907}
13908
13909void
13910rb_parser_keep_script_lines(VALUE vparser)
13911{
13912 struct parser_params *p;
13913
13914 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13915 p->keep_script_lines = 1;
13916}
13917
13918void
13919rb_parser_error_tolerant(VALUE vparser)
13920{
13921 struct parser_params *p;
13922
13923 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13924 p->error_tolerant = 1;
13925 p->end_expect_token_locations = rb_ary_new();
13926}
13927
13928void
13929rb_parser_keep_tokens(VALUE vparser)
13930{
13931 struct parser_params *p;
13932
13933 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13934 p->keep_tokens = 1;
13935 p->tokens = rb_ary_new();
13936}
13937
13938#endif
13939
13940#ifdef RIPPER
13941#define rb_parser_end_seen_p ripper_parser_end_seen_p
13942#define rb_parser_encoding ripper_parser_encoding
13943#define rb_parser_get_yydebug ripper_parser_get_yydebug
13944#define rb_parser_set_yydebug ripper_parser_set_yydebug
13945#define rb_parser_get_debug_output ripper_parser_get_debug_output
13946#define rb_parser_set_debug_output ripper_parser_set_debug_output
13947static VALUE ripper_parser_end_seen_p(VALUE vparser);
13948static VALUE ripper_parser_encoding(VALUE vparser);
13949static VALUE ripper_parser_get_yydebug(VALUE self);
13950static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13951static VALUE ripper_parser_get_debug_output(VALUE self);
13952static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13953
13954/*
13955 * call-seq:
13956 * ripper.error? -> Boolean
13957 *
13958 * Return true if parsed source has errors.
13959 */
13960static VALUE
13961ripper_error_p(VALUE vparser)
13962{
13963 struct parser_params *p;
13964
13965 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13966 return RBOOL(p->error_p);
13967}
13968#endif
13969
13970/*
13971 * call-seq:
13972 * ripper.end_seen? -> Boolean
13973 *
13974 * Return true if parsed source ended by +\_\_END\_\_+.
13975 */
13976VALUE
13977rb_parser_end_seen_p(VALUE vparser)
13978{
13979 struct parser_params *p;
13980
13981 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13982 return RBOOL(p->ruby__end__seen);
13983}
13984
13985/*
13986 * call-seq:
13987 * ripper.encoding -> encoding
13988 *
13989 * Return encoding of the source.
13990 */
13991VALUE
13992rb_parser_encoding(VALUE vparser)
13993{
13994 struct parser_params *p;
13995
13996 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13997 return rb_enc_from_encoding(p->enc);
13998}
13999
14000#ifdef RIPPER
14001/*
14002 * call-seq:
14003 * ripper.yydebug -> true or false
14004 *
14005 * Get yydebug.
14006 */
14007VALUE
14008rb_parser_get_yydebug(VALUE self)
14009{
14010 struct parser_params *p;
14011
14012 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14013 return RBOOL(p->debug);
14014}
14015#endif
14016
14017/*
14018 * call-seq:
14019 * ripper.yydebug = flag
14020 *
14021 * Set yydebug.
14022 */
14023VALUE
14024rb_parser_set_yydebug(VALUE self, VALUE flag)
14025{
14026 struct parser_params *p;
14027
14028 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14029 p->debug = RTEST(flag);
14030 return flag;
14031}
14032
14033/*
14034 * call-seq:
14035 * ripper.debug_output -> obj
14036 *
14037 * Get debug output.
14038 */
14039VALUE
14040rb_parser_get_debug_output(VALUE self)
14041{
14042 struct parser_params *p;
14043
14044 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14045 return p->debug_output;
14046}
14047
14048/*
14049 * call-seq:
14050 * ripper.debug_output = obj
14051 *
14052 * Set debug output.
14053 */
14054VALUE
14055rb_parser_set_debug_output(VALUE self, VALUE output)
14056{
14057 struct parser_params *p;
14058
14059 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14060 return p->debug_output = output;
14061}
14062
14063#ifndef RIPPER
14064#ifdef YYMALLOC
14065#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
14066/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
14067 * potential memory leak */
14068#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
14069#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
14070 (new)->cnt = (cnt), (ptr))
14071
14072void *
14073rb_parser_malloc(struct parser_params *p, size_t size)
14074{
14075 size_t cnt = HEAPCNT(1, size);
14076 rb_imemo_tmpbuf_t *n = NEWHEAP();
14077 void *ptr = xmalloc(size);
14078
14079 return ADD2HEAP(n, cnt, ptr);
14080}
14081
14082void *
14083rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
14084{
14085 size_t cnt = HEAPCNT(nelem, size);
14086 rb_imemo_tmpbuf_t *n = NEWHEAP();
14087 void *ptr = xcalloc(nelem, size);
14088
14089 return ADD2HEAP(n, cnt, ptr);
14090}
14091
14092void *
14093rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
14094{
14095 rb_imemo_tmpbuf_t *n;
14096 size_t cnt = HEAPCNT(1, size);
14097
14098 if (ptr && (n = p->heap) != NULL) {
14099 do {
14100 if (n->ptr == ptr) {
14101 n->ptr = ptr = xrealloc(ptr, size);
14102 if (n->cnt) n->cnt = cnt;
14103 return ptr;
14104 }
14105 } while ((n = n->next) != NULL);
14106 }
14107 n = NEWHEAP();
14108 ptr = xrealloc(ptr, size);
14109 return ADD2HEAP(n, cnt, ptr);
14110}
14111
14112void
14113rb_parser_free(struct parser_params *p, void *ptr)
14114{
14115 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
14116
14117 while ((n = *prev) != NULL) {
14118 if (n->ptr == ptr) {
14119 *prev = n->next;
14120 break;
14121 }
14122 prev = &n->next;
14123 }
14124}
14125#endif
14126
14127void
14128rb_parser_printf(struct parser_params *p, const char *fmt, ...)
14129{
14130 va_list ap;
14131 VALUE mesg = p->debug_buffer;
14132
14133 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
14134 va_start(ap, fmt);
14135 rb_str_vcatf(mesg, fmt, ap);
14136 va_end(ap);
14137 if (RSTRING_END(mesg)[-1] == '\n') {
14138 rb_io_write(p->debug_output, mesg);
14139 p->debug_buffer = Qnil;
14140 }
14141}
14142
14143static void
14144parser_compile_error(struct parser_params *p, const char *fmt, ...)
14145{
14146 va_list ap;
14147
14148 rb_io_flush(p->debug_output);
14149 p->error_p = 1;
14150 va_start(ap, fmt);
14151 p->error_buffer =
14152 rb_syntax_error_append(p->error_buffer,
14153 p->ruby_sourcefile_string,
14154 p->ruby_sourceline,
14155 rb_long2int(p->lex.pcur - p->lex.pbeg),
14156 p->enc, fmt, ap);
14157 va_end(ap);
14158}
14159
14160static size_t
14161count_char(const char *str, int c)
14162{
14163 int n = 0;
14164 while (str[n] == c) ++n;
14165 return n;
14166}
14167
14168/*
14169 * strip enclosing double-quotes, same as the default yytnamerr except
14170 * for that single-quotes matching back-quotes do not stop stripping.
14171 *
14172 * "\"`class' keyword\"" => "`class' keyword"
14173 */
14174RUBY_FUNC_EXPORTED size_t
14175rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
14176{
14177 if (*yystr == '"') {
14178 size_t yyn = 0, bquote = 0;
14179 const char *yyp = yystr;
14180
14181 while (*++yyp) {
14182 switch (*yyp) {
14183 case '`':
14184 if (!bquote) {
14185 bquote = count_char(yyp+1, '`') + 1;
14186 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
14187 yyn += bquote;
14188 yyp += bquote - 1;
14189 break;
14190 }
14191 goto default_char;
14192
14193 case '\'':
14194 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
14195 if (yyres) memcpy(yyres + yyn, yyp, bquote);
14196 yyn += bquote;
14197 yyp += bquote - 1;
14198 bquote = 0;
14199 break;
14200 }
14201 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
14202 if (yyres) memcpy(yyres + yyn, yyp, 3);
14203 yyn += 3;
14204 yyp += 2;
14205 break;
14206 }
14207 goto do_not_strip_quotes;
14208
14209 case ',':
14210 goto do_not_strip_quotes;
14211
14212 case '\\':
14213 if (*++yyp != '\\')
14214 goto do_not_strip_quotes;
14215 /* Fall through. */
14216 default_char:
14217 default:
14218 if (yyres)
14219 yyres[yyn] = *yyp;
14220 yyn++;
14221 break;
14222
14223 case '"':
14224 case '\0':
14225 if (yyres)
14226 yyres[yyn] = '\0';
14227 return yyn;
14228 }
14229 }
14230 do_not_strip_quotes: ;
14231 }
14232
14233 if (!yyres) return strlen(yystr);
14234
14235 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
14236}
14237#endif
14238
14239#ifdef RIPPER
14240#ifdef RIPPER_DEBUG
14241/* :nodoc: */
14242static VALUE
14243ripper_validate_object(VALUE self, VALUE x)
14244{
14245 if (x == Qfalse) return x;
14246 if (x == Qtrue) return x;
14247 if (NIL_P(x)) return x;
14248 if (UNDEF_P(x))
14249 rb_raise(rb_eArgError, "Qundef given");
14250 if (FIXNUM_P(x)) return x;
14251 if (SYMBOL_P(x)) return x;
14252 switch (BUILTIN_TYPE(x)) {
14253 case T_STRING:
14254 case T_OBJECT:
14255 case T_ARRAY:
14256 case T_BIGNUM:
14257 case T_FLOAT:
14258 case T_COMPLEX:
14259 case T_RATIONAL:
14260 break;
14261 case T_NODE:
14262 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
14263 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
14264 }
14265 x = ((NODE *)x)->nd_rval;
14266 break;
14267 default:
14268 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
14269 (void *)x, rb_obj_classname(x));
14270 }
14271 if (!RBASIC_CLASS(x)) {
14272 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
14273 (void *)x, rb_builtin_type_name(TYPE(x)));
14274 }
14275 return x;
14276}
14277#endif
14278
14279#define validate(x) ((x) = get_value(x))
14280
14281static VALUE
14282ripper_dispatch0(struct parser_params *p, ID mid)
14283{
14284 return rb_funcall(p->value, mid, 0);
14285}
14286
14287static VALUE
14288ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
14289{
14290 validate(a);
14291 return rb_funcall(p->value, mid, 1, a);
14292}
14293
14294static VALUE
14295ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
14296{
14297 validate(a);
14298 validate(b);
14299 return rb_funcall(p->value, mid, 2, a, b);
14300}
14301
14302static VALUE
14303ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
14304{
14305 validate(a);
14306 validate(b);
14307 validate(c);
14308 return rb_funcall(p->value, mid, 3, a, b, c);
14309}
14310
14311static VALUE
14312ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
14313{
14314 validate(a);
14315 validate(b);
14316 validate(c);
14317 validate(d);
14318 return rb_funcall(p->value, mid, 4, a, b, c, d);
14319}
14320
14321static VALUE
14322ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
14323{
14324 validate(a);
14325 validate(b);
14326 validate(c);
14327 validate(d);
14328 validate(e);
14329 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
14330}
14331
14332static VALUE
14333ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
14334{
14335 validate(a);
14336 validate(b);
14337 validate(c);
14338 validate(d);
14339 validate(e);
14340 validate(f);
14341 validate(g);
14342 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
14343}
14344
14345static ID
14346ripper_get_id(VALUE v)
14347{
14348 NODE *nd;
14349 if (!RB_TYPE_P(v, T_NODE)) return 0;
14350 nd = (NODE *)v;
14351 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
14352 return nd->nd_vid;
14353}
14354
14355static VALUE
14356ripper_get_value(VALUE v)
14357{
14358 NODE *nd;
14359 if (UNDEF_P(v)) return Qnil;
14360 if (!RB_TYPE_P(v, T_NODE)) return v;
14361 nd = (NODE *)v;
14362 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
14363 return nd->nd_rval;
14364}
14365
14366static void
14367ripper_error(struct parser_params *p)
14368{
14369 p->error_p = TRUE;
14370}
14371
14372static void
14373ripper_compile_error(struct parser_params *p, const char *fmt, ...)
14374{
14375 VALUE str;
14376 va_list args;
14377
14378 va_start(args, fmt);
14379 str = rb_vsprintf(fmt, args);
14380 va_end(args);
14381 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
14382 ripper_error(p);
14383}
14384
14385static VALUE
14386ripper_lex_get_generic(struct parser_params *p, VALUE src)
14387{
14388 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
14389 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
14390 rb_raise(rb_eTypeError,
14391 "gets returned %"PRIsVALUE" (expected String or nil)",
14392 rb_obj_class(line));
14393 }
14394 return line;
14395}
14396
14397static VALUE
14398ripper_lex_io_get(struct parser_params *p, VALUE src)
14399{
14400 return rb_io_gets(src);
14401}
14402
14403static VALUE
14404ripper_s_allocate(VALUE klass)
14405{
14406 struct parser_params *p;
14407 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
14408 &parser_data_type, p);
14409 p->value = self;
14410 return self;
14411}
14412
14413#define ripper_initialized_p(r) ((r)->lex.input != 0)
14414
14415/*
14416 * call-seq:
14417 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
14418 *
14419 * Create a new Ripper object.
14420 * _src_ must be a String, an IO, or an Object which has #gets method.
14421 *
14422 * This method does not starts parsing.
14423 * See also Ripper#parse and Ripper.parse.
14424 */
14425static VALUE
14426ripper_initialize(int argc, VALUE *argv, VALUE self)
14427{
14428 struct parser_params *p;
14429 VALUE src, fname, lineno;
14430
14431 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14432 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
14433 if (RB_TYPE_P(src, T_FILE)) {
14434 p->lex.gets = ripper_lex_io_get;
14435 }
14436 else if (rb_respond_to(src, id_gets)) {
14437 p->lex.gets = ripper_lex_get_generic;
14438 }
14439 else {
14440 StringValue(src);
14441 p->lex.gets = lex_get_str;
14442 }
14443 p->lex.input = src;
14444 p->eofp = 0;
14445 if (NIL_P(fname)) {
14446 fname = STR_NEW2("(ripper)");
14447 OBJ_FREEZE(fname);
14448 }
14449 else {
14450 StringValueCStr(fname);
14451 fname = rb_str_new_frozen(fname);
14452 }
14453 parser_initialize(p);
14454
14455 p->ruby_sourcefile_string = fname;
14456 p->ruby_sourcefile = RSTRING_PTR(fname);
14457 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
14458
14459 return Qnil;
14460}
14461
14462static VALUE
14463ripper_parse0(VALUE parser_v)
14464{
14465 struct parser_params *p;
14466
14467 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14468 parser_prepare(p);
14469 p->ast = rb_ast_new();
14470 ripper_yyparse((void*)p);
14471 rb_ast_dispose(p->ast);
14472 p->ast = 0;
14473 return p->result;
14474}
14475
14476static VALUE
14477ripper_ensure(VALUE parser_v)
14478{
14479 struct parser_params *p;
14480
14481 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14482 p->parsing_thread = Qnil;
14483 return Qnil;
14484}
14485
14486/*
14487 * call-seq:
14488 * ripper.parse
14489 *
14490 * Start parsing and returns the value of the root action.
14491 */
14492static VALUE
14493ripper_parse(VALUE self)
14494{
14495 struct parser_params *p;
14496
14497 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14498 if (!ripper_initialized_p(p)) {
14499 rb_raise(rb_eArgError, "method called for uninitialized object");
14500 }
14501 if (!NIL_P(p->parsing_thread)) {
14502 if (p->parsing_thread == rb_thread_current())
14503 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
14504 else
14505 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
14506 }
14507 p->parsing_thread = rb_thread_current();
14508 rb_ensure(ripper_parse0, self, ripper_ensure, self);
14509
14510 return p->result;
14511}
14512
14513/*
14514 * call-seq:
14515 * ripper.column -> Integer
14516 *
14517 * Return column number of current parsing line.
14518 * This number starts from 0.
14519 */
14520static VALUE
14521ripper_column(VALUE self)
14522{
14523 struct parser_params *p;
14524 long col;
14525
14526 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14527 if (!ripper_initialized_p(p)) {
14528 rb_raise(rb_eArgError, "method called for uninitialized object");
14529 }
14530 if (NIL_P(p->parsing_thread)) return Qnil;
14531 col = p->lex.ptok - p->lex.pbeg;
14532 return LONG2NUM(col);
14533}
14534
14535/*
14536 * call-seq:
14537 * ripper.filename -> String
14538 *
14539 * Return current parsing filename.
14540 */
14541static VALUE
14542ripper_filename(VALUE self)
14543{
14544 struct parser_params *p;
14545
14546 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14547 if (!ripper_initialized_p(p)) {
14548 rb_raise(rb_eArgError, "method called for uninitialized object");
14549 }
14550 return p->ruby_sourcefile_string;
14551}
14552
14553/*
14554 * call-seq:
14555 * ripper.lineno -> Integer
14556 *
14557 * Return line number of current parsing line.
14558 * This number starts from 1.
14559 */
14560static VALUE
14561ripper_lineno(VALUE self)
14562{
14563 struct parser_params *p;
14564
14565 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14566 if (!ripper_initialized_p(p)) {
14567 rb_raise(rb_eArgError, "method called for uninitialized object");
14568 }
14569 if (NIL_P(p->parsing_thread)) return Qnil;
14570 return INT2NUM(p->ruby_sourceline);
14571}
14572
14573/*
14574 * call-seq:
14575 * ripper.state -> Integer
14576 *
14577 * Return scanner state of current token.
14578 */
14579static VALUE
14580ripper_state(VALUE self)
14581{
14582 struct parser_params *p;
14583
14584 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14585 if (!ripper_initialized_p(p)) {
14586 rb_raise(rb_eArgError, "method called for uninitialized object");
14587 }
14588 if (NIL_P(p->parsing_thread)) return Qnil;
14589 return INT2NUM(p->lex.state);
14590}
14591
14592/*
14593 * call-seq:
14594 * ripper.token -> String
14595 *
14596 * Return the current token string.
14597 */
14598static VALUE
14599ripper_token(VALUE self)
14600{
14601 struct parser_params *p;
14602 long pos, len;
14603
14604 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14605 if (!ripper_initialized_p(p)) {
14606 rb_raise(rb_eArgError, "method called for uninitialized object");
14607 }
14608 if (NIL_P(p->parsing_thread)) return Qnil;
14609 pos = p->lex.ptok - p->lex.pbeg;
14610 len = p->lex.pcur - p->lex.ptok;
14611 return rb_str_subseq(p->lex.lastline, pos, len);
14612}
14613
14614#ifdef RIPPER_DEBUG
14615/* :nodoc: */
14616static VALUE
14617ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
14618{
14619 StringValue(msg);
14620 if (UNDEF_P(obj)) {
14621 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
14622 }
14623 return Qnil;
14624}
14625
14626/* :nodoc: */
14627static VALUE
14628ripper_value(VALUE self, VALUE obj)
14629{
14630 return ULONG2NUM(obj);
14631}
14632#endif
14633
14634/*
14635 * call-seq:
14636 * Ripper.lex_state_name(integer) -> string
14637 *
14638 * Returns a string representation of lex_state.
14639 */
14640static VALUE
14641ripper_lex_state_name(VALUE self, VALUE state)
14642{
14643 return rb_parser_lex_state_name(NUM2INT(state));
14644}
14645
14646void
14647Init_ripper(void)
14648{
14649 ripper_init_eventids1();
14650 ripper_init_eventids2();
14651 id_warn = rb_intern_const("warn");
14652 id_warning = rb_intern_const("warning");
14653 id_gets = rb_intern_const("gets");
14654 id_assoc = rb_intern_const("=>");
14655
14656 (void)yystpcpy; /* may not used in newer bison */
14657
14658 InitVM(ripper);
14659}
14660
14661void
14662InitVM_ripper(void)
14663{
14664 VALUE Ripper;
14665
14666 Ripper = rb_define_class("Ripper", rb_cObject);
14667 /* version of Ripper */
14668 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14669 rb_define_alloc_func(Ripper, ripper_s_allocate);
14670 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14671 rb_define_method(Ripper, "parse", ripper_parse, 0);
14672 rb_define_method(Ripper, "column", ripper_column, 0);
14673 rb_define_method(Ripper, "filename", ripper_filename, 0);
14674 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14675 rb_define_method(Ripper, "state", ripper_state, 0);
14676 rb_define_method(Ripper, "token", ripper_token, 0);
14677 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14678 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14679 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14680 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14681 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14682 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14683 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14684#ifdef RIPPER_DEBUG
14685 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14686 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14687 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14688#endif
14689
14690 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14691 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14692
14693 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14694
14695<% @exprs.each do |expr, desc| -%>
14696 /* <%=desc%> */
14697 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14698<% end %>
14699 ripper_init_eventids1_table(Ripper);
14700 ripper_init_eventids2_table(Ripper);
14701
14702# if 0
14703 /* Hack to let RDoc document SCRIPT_LINES__ */
14704
14705 /*
14706 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14707 * after the assignment will be added as an Array of lines with the file
14708 * name as the key.
14709 */
14710 rb_define_global_const("SCRIPT_LINES__", Qnil);
14711#endif
14712
14713}
14714#endif /* RIPPER */
14715
14716/*
14717 * Local variables:
14718 * mode: c
14719 * c-file-style: "ruby"
14720 * End:
14721 */