Ruby 3.2.2p53 (2023-03-30 revision e51014f9c05aa65cbf203442d37fef7c12390015)
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
697static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
698
699static int
700parser_get_node_id(struct parser_params *p)
701{
702 int node_id = p->node_id;
703 p->node_id++;
704 return node_id;
705}
706
707#ifndef RIPPER
708static inline void
709set_line_body(NODE *body, int line)
710{
711 if (!body) return;
712 switch (nd_type(body)) {
713 case NODE_RESCUE:
714 case NODE_ENSURE:
715 nd_set_line(body, line);
716 }
717}
718
719#define yyparse ruby_yyparse
720
721static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
722static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
723#define new_nil(loc) NEW_NIL(loc)
724static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
725static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
726static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
727static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
728
729static NODE *newline_node(NODE*);
730static void fixpos(NODE*,NODE*);
731
732static int value_expr_gen(struct parser_params*,NODE*);
733static void void_expr(struct parser_params*,NODE*);
734static NODE *remove_begin(NODE*);
735static NODE *remove_begin_all(NODE*);
736#define value_expr(node) value_expr_gen(p, (node))
737static NODE *void_stmts(struct parser_params*,NODE*);
738static void reduce_nodes(struct parser_params*,NODE**);
739static void block_dup_check(struct parser_params*,NODE*,NODE*);
740
741static NODE *block_append(struct parser_params*,NODE*,NODE*);
742static NODE *list_append(struct parser_params*,NODE*,NODE*);
743static NODE *list_concat(NODE*,NODE*);
744static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
745static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
746static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
747static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
748static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
749static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
750static NODE *evstr2dstr(struct parser_params*,NODE*);
751static NODE *splat_array(NODE*);
752static void mark_lvar_used(struct parser_params *p, NODE *rhs);
753
754static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
755static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
756static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
757static 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);
758static 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;}
759
760static bool args_info_empty_p(struct rb_args_info *args);
761static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
762static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
763static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
764static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
765static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
766static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
767static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
768static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
769
770static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
771static NODE *args_with_numbered(struct parser_params*,NODE*,int);
772
773static VALUE negate_lit(struct parser_params*, VALUE);
774static NODE *ret_args(struct parser_params*,NODE*);
775static NODE *arg_blk_pass(NODE*,NODE*);
776static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
777static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
778
779static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
780static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
781
782static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
783static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
784
785static void rb_backref_error(struct parser_params*,NODE*);
786static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
787
788static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
789static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
790static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
791static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
792static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
793
794static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
795
796static NODE *opt_arg_append(NODE*, NODE*);
797static NODE *kwd_append(NODE*, NODE*);
798
799static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
800static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
801
802static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
803
804static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
805
806#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
807
808static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
809
810static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
811
812static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
813
814static rb_ast_id_table_t *local_tbl(struct parser_params*);
815
816static VALUE reg_compile(struct parser_params*, VALUE, int);
817static void reg_fragment_setenc(struct parser_params*, VALUE, int);
818static int reg_fragment_check(struct parser_params*, VALUE, int);
819static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
820
821static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
822static NODE *heredoc_dedent(struct parser_params*,NODE*);
823
824static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
825
826#define get_id(id) (id)
827#define get_value(val) (val)
828#define get_num(num) (num)
829#else /* RIPPER */
830#define NODE_RIPPER NODE_CDECL
831#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
832
833static inline int ripper_is_node_yylval(VALUE n);
834
835static inline VALUE
836ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
837{
838 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
839 add_mark_object(p, b);
840 add_mark_object(p, c);
841 return NEW_RIPPER(a, b, c, &NULL_LOC);
842}
843
844static inline int
845ripper_is_node_yylval(VALUE n)
846{
847 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
848}
849
850#define value_expr(node) ((void)(node))
851#define remove_begin(node) (node)
852#define void_stmts(p,x) (x)
853#define rb_dvar_defined(id, base) 0
854#define rb_local_defined(id, base) 0
855static ID ripper_get_id(VALUE);
856#define get_id(id) ripper_get_id(id)
857static VALUE ripper_get_value(VALUE);
858#define get_value(val) ripper_get_value(val)
859#define get_num(num) (int)get_id(num)
860static VALUE assignable(struct parser_params*,VALUE);
861static int id_is_var(struct parser_params *p, ID id);
862
863#define method_cond(p,node,loc) (node)
864#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
865#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
866#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
867#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
868
869#define new_nil(loc) Qnil
870
871static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
872
873static VALUE const_decl(struct parser_params *p, VALUE path);
874
875static VALUE var_field(struct parser_params *p, VALUE a);
876static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
877
878static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
879
880static VALUE backref_error(struct parser_params*, NODE *, VALUE);
881#endif /* !RIPPER */
882
883/* forward declaration */
884typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
885
886RUBY_SYMBOL_EXPORT_BEGIN
887VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
888int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
889enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
890VALUE rb_parser_lex_state_name(enum lex_state_e state);
891void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
892PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
893YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
894YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
895YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
896YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
897YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
898YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
899RUBY_SYMBOL_EXPORT_END
900
901static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
902static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
903#ifndef RIPPER
904static ID formal_argument(struct parser_params*, ID);
905#else
906static ID formal_argument(struct parser_params*, VALUE);
907#endif
908static ID shadowing_lvar(struct parser_params*,ID);
909static void new_bv(struct parser_params*,ID);
910
911static void local_push(struct parser_params*,int);
912static void local_pop(struct parser_params*);
913static void local_var(struct parser_params*, ID);
914static void arg_var(struct parser_params*, ID);
915static int local_id(struct parser_params *p, ID id);
916static int local_id_ref(struct parser_params*, ID, ID **);
917#ifndef RIPPER
918static ID internal_id(struct parser_params*);
919static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
920#endif
921static int check_forwarding_args(struct parser_params*);
922static void add_forwarding_args(struct parser_params *p);
923
924static const struct vtable *dyna_push(struct parser_params *);
925static void dyna_pop(struct parser_params*, const struct vtable *);
926static int dyna_in_block(struct parser_params*);
927#define dyna_var(p, id) local_var(p, id)
928static int dvar_defined(struct parser_params*, ID);
929static int dvar_defined_ref(struct parser_params*, ID, ID**);
930static int dvar_curr(struct parser_params*,ID);
931
932static int lvar_defined(struct parser_params*, ID);
933
934static NODE *numparam_push(struct parser_params *p);
935static void numparam_pop(struct parser_params *p, NODE *prev_inner);
936
937#ifdef RIPPER
938# define METHOD_NOT idNOT
939#else
940# define METHOD_NOT '!'
941#endif
942
943#define idFWD_REST '*'
944#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
945#define idFWD_BLOCK '&'
946#define idFWD_ALL idDot3
947#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
948
949#define RE_OPTION_ONCE (1<<16)
950#define RE_OPTION_ENCODING_SHIFT 8
951#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
952#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
953#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
954#define RE_OPTION_MASK 0xff
955#define RE_OPTION_ARG_ENCODING_NONE 32
956
957/* structs for managing terminator of string literal and heredocment */
958typedef struct rb_strterm_literal_struct {
959 union {
960 VALUE dummy;
961 long nest;
962 } u0;
963 union {
964 VALUE dummy;
965 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
966 } u1;
967 union {
968 VALUE dummy;
969 long paren; /* '(' of `%q(...)` */
970 } u2;
971 union {
972 VALUE dummy;
973 long term; /* ')' of `%q(...)` */
974 } u3;
975} rb_strterm_literal_t;
976
977#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
978
979struct rb_strterm_heredoc_struct {
980 VALUE lastline; /* the string of line that contains `<<"END"` */
981 long offset; /* the column of END in `<<"END"` */
982 int sourceline; /* lineno of the line that contains `<<"END"` */
983 unsigned length /* the length of END in `<<"END"` */
984#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
985 : HERETERM_LENGTH_BITS
986# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
987#else
988# define HERETERM_LENGTH_MAX UINT_MAX
989#endif
990 ;
991#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
992 unsigned quote: 1;
993 unsigned func: 8;
994#else
995 uint8_t quote;
996 uint8_t func;
997#endif
998};
999STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
1000
1001#define STRTERM_HEREDOC IMEMO_FL_USER0
1002
1003struct rb_strterm_struct {
1004 VALUE flags;
1005 union {
1006 rb_strterm_literal_t literal;
1007 rb_strterm_heredoc_t heredoc;
1008 } u;
1009};
1010
1011#ifndef RIPPER
1012void
1013rb_strterm_mark(VALUE obj)
1014{
1015 rb_strterm_t *strterm = (rb_strterm_t*)obj;
1016 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
1017 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
1018 rb_gc_mark(heredoc->lastline);
1019 }
1020}
1021#endif
1022
1023#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1024size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1025
1026#define TOKEN2ID(tok) ( \
1027 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1028 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1029 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1030 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1031 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1032 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1033 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1034
1035/****** Ripper *******/
1036
1037#ifdef RIPPER
1038#define RIPPER_VERSION "0.1.0"
1039
1040static inline VALUE intern_sym(const char *name);
1041
1042#include "eventids1.c"
1043#include "eventids2.c"
1044
1045static VALUE ripper_dispatch0(struct parser_params*,ID);
1046static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1047static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1048static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1049static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1050static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1051static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1052static void ripper_error(struct parser_params *p);
1053
1054#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1055#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1056#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1057#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1058#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1059#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1060#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))
1061
1062#define yyparse ripper_yyparse
1063
1064#define ID2VAL(id) STATIC_ID2SYM(id)
1065#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1066#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1067
1068#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1069 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1070
1071#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
1072
1073static inline VALUE
1074new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1075{
1076 NODE *t = (NODE *)tail;
1077 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
1078 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
1079}
1080
1081static inline VALUE
1082new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1083{
1084 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
1085 add_mark_object(p, kw_args);
1086 add_mark_object(p, kw_rest_arg);
1087 add_mark_object(p, block);
1088 return (VALUE)t;
1089}
1090
1091static inline VALUE
1092args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1093{
1094 return args;
1095}
1096
1097static VALUE
1098new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1099{
1100 NODE *t = (NODE *)aryptn;
1101 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
1102
1103 if (!NIL_P(pre_arg)) {
1104 if (!NIL_P(pre_args)) {
1105 rb_ary_unshift(pre_args, pre_arg);
1106 }
1107 else {
1108 pre_args = rb_ary_new_from_args(1, pre_arg);
1109 }
1110 }
1111 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1112}
1113
1114static VALUE
1115new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1116{
1117 NODE *t;
1118
1119 if (has_rest) {
1120 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
1121 }
1122 else {
1123 rest_arg = Qnil;
1124 }
1125
1126 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
1127 add_mark_object(p, pre_args);
1128 add_mark_object(p, rest_arg);
1129 add_mark_object(p, post_args);
1130 return (VALUE)t;
1131}
1132
1133static VALUE
1134new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1135{
1136 NODE *t = (NODE *)fndptn;
1137 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
1138
1139 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1140}
1141
1142static VALUE
1143new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1144{
1145 NODE *t;
1146
1147 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
1148 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
1149
1150 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
1151 add_mark_object(p, pre_rest_arg);
1152 add_mark_object(p, args);
1153 add_mark_object(p, post_rest_arg);
1154 return (VALUE)t;
1155}
1156
1157#define new_hash(p,h,l) rb_ary_new_from_args(0)
1158
1159static VALUE
1160new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1161{
1162 return ary;
1163}
1164
1165static VALUE
1166new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1167{
1168 NODE *t = (NODE *)hshptn;
1169 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
1170 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1171}
1172
1173static VALUE
1174new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1175{
1176 NODE *t;
1177 if (kw_rest_arg) {
1178 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1179 }
1180 else {
1181 kw_rest_arg = Qnil;
1182 }
1183 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
1184
1185 add_mark_object(p, kw_args);
1186 add_mark_object(p, kw_rest_arg);
1187 return (VALUE)t;
1188}
1189
1190#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1191
1192static VALUE heredoc_dedent(struct parser_params*,VALUE);
1193
1194#else
1195#define ID2VAL(id) (id)
1196#define TOKEN2VAL(t) ID2VAL(t)
1197#define KWD2EID(t, v) keyword_##t
1198
1199static NODE *
1200set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
1201{
1202 body = remove_begin(body);
1203 reduce_nodes(p, &body);
1204 n->nd_defn = NEW_SCOPE(args, body, loc);
1205 n->nd_loc = *loc;
1206 nd_set_line(n->nd_defn, loc->end_pos.lineno);
1207 set_line_body(body, loc->beg_pos.lineno);
1208 return n;
1209}
1210
1211static NODE *
1212rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1213 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1214{
1215 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1216 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1217 loc.beg_pos = arg_loc->beg_pos;
1218 return NEW_RESCUE(arg, rescue, 0, &loc);
1219}
1220
1221#endif /* RIPPER */
1222
1223static void
1224restore_defun(struct parser_params *p, NODE *name)
1225{
1226 YYSTYPE c = {.val = name->nd_cval};
1227 p->cur_arg = name->nd_vid;
1228 p->ctxt.in_def = c.ctxt.in_def;
1229 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
1230}
1231
1232static void
1233endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
1234{
1235#ifdef RIPPER
1236 defn = defn->nd_defn;
1237#endif
1238 ID mid = defn->nd_mid;
1239 if (is_attrset_id(mid)) {
1240 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1241 }
1242 token_info_drop(p, "def", loc->beg_pos);
1243}
1244
1245#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)
1246
1247#ifndef RIPPER
1248# define Qnone 0
1249# define Qnull 0
1250# define ifndef_ripper(x) (x)
1251#else
1252# define Qnone Qnil
1253# define Qnull Qundef
1254# define ifndef_ripper(x)
1255#endif
1256
1257# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1258# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1259# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1260# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1261# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1262# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1263# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1264# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1265# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1266# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1267# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1268# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1269# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1270# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1271# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1272# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1273# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1274# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1275# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1276# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1277#ifdef RIPPER
1278static ID id_warn, id_warning, id_gets, id_assoc;
1279# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1280# define WARN_S_L(s,l) STR_NEW(s,l)
1281# define WARN_S(s) STR_NEW2(s)
1282# define WARN_I(i) INT2NUM(i)
1283# define WARN_ID(i) rb_id2str(i)
1284# define WARN_IVAL(i) i
1285# define PRIsWARN "s"
1286# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1287# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1288# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1289# ifdef HAVE_VA_ARGS_MACRO
1290# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1291# else
1292# define WARN_CALL rb_funcall
1293# endif
1294# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1295# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1296# ifdef HAVE_VA_ARGS_MACRO
1297# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1298# else
1299# define WARNING_CALL rb_funcall
1300# endif
1301PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1302# define compile_error ripper_compile_error
1303#else
1304# define WARN_S_L(s,l) s
1305# define WARN_S(s) s
1306# define WARN_I(i) i
1307# define WARN_ID(i) rb_id2name(i)
1308# define WARN_IVAL(i) NUM2INT(i)
1309# define PRIsWARN PRIsVALUE
1310# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1311# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1312# define WARN_CALL rb_compile_warn
1313# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1314# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1315# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1316# define WARNING_CALL rb_compile_warning
1317PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1318# define compile_error parser_compile_error
1319#endif
1320
1321#define WARN_EOL(tok) \
1322 (looking_at_eol_p(p) ? \
1323 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1324 (void)0)
1325static int looking_at_eol_p(struct parser_params *p);
1326%}
1327
1328%expect 0
1329%define api.pure
1330%define parse.error verbose
1331%printer {
1332#ifndef RIPPER
1333 if ($$) {
1334 rb_parser_printf(p, "%s", ruby_node_name(nd_type($$)));
1335 }
1336#else
1337#endif
1338} <node>
1339%printer {
1340#ifndef RIPPER
1341 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1342#else
1343 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1344#endif
1345} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1346%printer {
1347#ifndef RIPPER
1348 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1349#else
1350 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1351#endif
1352} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1353%printer {
1354#ifndef RIPPER
1355 rb_parser_printf(p, "$%ld", $$->nd_nth);
1356#else
1357 rb_parser_printf(p, "%"PRIsVALUE, $$);
1358#endif
1359} tNTH_REF
1360%printer {
1361#ifndef RIPPER
1362 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1363#else
1364 rb_parser_printf(p, "%"PRIsVALUE, $$);
1365#endif
1366} tBACK_REF
1367
1368%lex-param {struct parser_params *p}
1369%parse-param {struct parser_params *p}
1370%initial-action
1371{
1372 RUBY_SET_YYLLOC_OF_NONE(@$);
1373};
1374
1375%union {
1376 VALUE val;
1377 NODE *node;
1378 ID id;
1379 int num;
1380 st_table *tbl;
1381 const struct vtable *vars;
1382 struct rb_strterm_struct *strterm;
1383 struct lex_context ctxt;
1384}
1385
1386%token <id>
1387 keyword_class "`class'"
1388 keyword_module "`module'"
1389 keyword_def "`def'"
1390 keyword_undef "`undef'"
1391 keyword_begin "`begin'"
1392 keyword_rescue "`rescue'"
1393 keyword_ensure "`ensure'"
1394 keyword_end "`end'"
1395 keyword_if "`if'"
1396 keyword_unless "`unless'"
1397 keyword_then "`then'"
1398 keyword_elsif "`elsif'"
1399 keyword_else "`else'"
1400 keyword_case "`case'"
1401 keyword_when "`when'"
1402 keyword_while "`while'"
1403 keyword_until "`until'"
1404 keyword_for "`for'"
1405 keyword_break "`break'"
1406 keyword_next "`next'"
1407 keyword_redo "`redo'"
1408 keyword_retry "`retry'"
1409 keyword_in "`in'"
1410 keyword_do "`do'"
1411 keyword_do_cond "`do' for condition"
1412 keyword_do_block "`do' for block"
1413 keyword_do_LAMBDA "`do' for lambda"
1414 keyword_return "`return'"
1415 keyword_yield "`yield'"
1416 keyword_super "`super'"
1417 keyword_self "`self'"
1418 keyword_nil "`nil'"
1419 keyword_true "`true'"
1420 keyword_false "`false'"
1421 keyword_and "`and'"
1422 keyword_or "`or'"
1423 keyword_not "`not'"
1424 modifier_if "`if' modifier"
1425 modifier_unless "`unless' modifier"
1426 modifier_while "`while' modifier"
1427 modifier_until "`until' modifier"
1428 modifier_rescue "`rescue' modifier"
1429 keyword_alias "`alias'"
1430 keyword_defined "`defined?'"
1431 keyword_BEGIN "`BEGIN'"
1432 keyword_END "`END'"
1433 keyword__LINE__ "`__LINE__'"
1434 keyword__FILE__ "`__FILE__'"
1435 keyword__ENCODING__ "`__ENCODING__'"
1436
1437%token <id> tIDENTIFIER "local variable or method"
1438%token <id> tFID "method"
1439%token <id> tGVAR "global variable"
1440%token <id> tIVAR "instance variable"
1441%token <id> tCONSTANT "constant"
1442%token <id> tCVAR "class variable"
1443%token <id> tLABEL "label"
1444%token <node> tINTEGER "integer literal"
1445%token <node> tFLOAT "float literal"
1446%token <node> tRATIONAL "rational literal"
1447%token <node> tIMAGINARY "imaginary literal"
1448%token <node> tCHAR "char literal"
1449%token <node> tNTH_REF "numbered reference"
1450%token <node> tBACK_REF "back reference"
1451%token <node> tSTRING_CONTENT "literal content"
1452%token <num> tREGEXP_END
1453%token <num> tDUMNY_END "dummy end"
1454
1455%type <node> singleton strings string string1 xstring regexp
1456%type <node> string_contents xstring_contents regexp_contents string_content
1457%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1458%type <node> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1459%type <node> top_compstmt top_stmts top_stmt begin_block
1460%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1461%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1462%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1463%type <node> args call_args opt_call_args
1464%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1465%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1466%type <node> command_rhs arg_rhs
1467%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1468%type <node> f_block_optarg f_block_opt
1469%type <node> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1470%type <node> f_optarg f_marg f_marg_list f_margs f_rest_marg
1471%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1472%type <node> block_param opt_block_param block_param_def f_opt
1473%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1474%type <node> bv_decls opt_bv_decl bvar
1475%type <node> lambda f_larglist lambda_body brace_body do_body
1476%type <node> brace_block cmd_brace_block do_block lhs none fitem
1477%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1478%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1479%type <node> p_expr p_as p_alt p_expr_basic p_find
1480%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1481%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
1482%type <node> p_kwargs p_kwarg p_kw
1483%type <id> keyword_variable user_variable sym operation operation2 operation3
1484%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1485%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1486%type <id> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1487%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var
1488 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1489%token END_OF_INPUT 0 "end-of-input"
1490%token <id> '.'
1491/* escaped chars, should be ignored otherwise */
1492%token <id> '\\' "backslash"
1493%token tSP "escaped space"
1494%token <id> '\t' "escaped horizontal tab"
1495%token <id> '\f' "escaped form feed"
1496%token <id> '\r' "escaped carriage return"
1497%token <id> '\13' "escaped vertical tab"
1498%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1499%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1500%token tPOW RUBY_TOKEN(POW) "**"
1501%token tCMP RUBY_TOKEN(CMP) "<=>"
1502%token tEQ RUBY_TOKEN(EQ) "=="
1503%token tEQQ RUBY_TOKEN(EQQ) "==="
1504%token tNEQ RUBY_TOKEN(NEQ) "!="
1505%token tGEQ RUBY_TOKEN(GEQ) ">="
1506%token tLEQ RUBY_TOKEN(LEQ) "<="
1507%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1508%token tOROP RUBY_TOKEN(OROP) "||"
1509%token tMATCH RUBY_TOKEN(MATCH) "=~"
1510%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1511%token tDOT2 RUBY_TOKEN(DOT2) ".."
1512%token tDOT3 RUBY_TOKEN(DOT3) "..."
1513%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1514%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1515%token tAREF RUBY_TOKEN(AREF) "[]"
1516%token tASET RUBY_TOKEN(ASET) "[]="
1517%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1518%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1519%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1520%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1521%token tCOLON3 ":: at EXPR_BEG"
1522%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1523%token tASSOC "=>"
1524%token tLPAREN "("
1525%token tLPAREN_ARG "( arg"
1526%token tRPAREN ")"
1527%token tLBRACK "["
1528%token tLBRACE "{"
1529%token tLBRACE_ARG "{ arg"
1530%token tSTAR "*"
1531%token tDSTAR "**arg"
1532%token tAMPER "&"
1533%token tLAMBDA "->"
1534%token tSYMBEG "symbol literal"
1535%token tSTRING_BEG "string literal"
1536%token tXSTRING_BEG "backtick literal"
1537%token tREGEXP_BEG "regexp literal"
1538%token tWORDS_BEG "word list"
1539%token tQWORDS_BEG "verbatim word list"
1540%token tSYMBOLS_BEG "symbol list"
1541%token tQSYMBOLS_BEG "verbatim symbol list"
1542%token tSTRING_END "terminator"
1543%token tSTRING_DEND "'}'"
1544%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1545
1546%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
1547%token tHEREDOC_BEG tHEREDOC_END k__END__
1548
1549/*
1550 * precedence table
1551 */
1552
1553%nonassoc tLOWEST
1554%nonassoc tLBRACE_ARG
1555
1556%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1557%left keyword_or keyword_and
1558%right keyword_not
1559%nonassoc keyword_defined
1560%right '=' tOP_ASGN
1561%left modifier_rescue
1562%right '?' ':'
1563%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1564%left tOROP
1565%left tANDOP
1566%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1567%left '>' tGEQ '<' tLEQ
1568%left '|' '^'
1569%left '&'
1570%left tLSHFT tRSHFT
1571%left '+' '-'
1572%left '*' '/' '%'
1573%right tUMINUS_NUM tUMINUS
1574%right tPOW
1575%right '!' '~' tUPLUS
1576
1577%token tLAST_TOKEN
1578
1579%%
1580program : {
1581 SET_LEX_STATE(EXPR_BEG);
1582 local_push(p, ifndef_ripper(1)+0);
1583 }
1584 top_compstmt
1585 {
1586 /*%%%*/
1587 if ($2 && !compile_for_eval) {
1588 NODE *node = $2;
1589 /* last expression should not be void */
1590 if (nd_type_p(node, NODE_BLOCK)) {
1591 while (node->nd_next) {
1592 node = node->nd_next;
1593 }
1594 node = node->nd_head;
1595 }
1596 node = remove_begin(node);
1597 void_expr(p, node);
1598 }
1599 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1600 /*% %*/
1601 /*% ripper[final]: program!($2) %*/
1602 local_pop(p);
1603 }
1604 ;
1605
1606top_compstmt : top_stmts opt_terms
1607 {
1608 $$ = void_stmts(p, $1);
1609 }
1610 ;
1611
1612top_stmts : none
1613 {
1614 /*%%%*/
1615 $$ = NEW_BEGIN(0, &@$);
1616 /*% %*/
1617 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1618 }
1619 | top_stmt
1620 {
1621 /*%%%*/
1622 $$ = newline_node($1);
1623 /*% %*/
1624 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1625 }
1626 | top_stmts terms top_stmt
1627 {
1628 /*%%%*/
1629 $$ = block_append(p, $1, newline_node($3));
1630 /*% %*/
1631 /*% ripper: stmts_add!($1, $3) %*/
1632 }
1633 ;
1634
1635top_stmt : stmt
1636 | keyword_BEGIN begin_block
1637 {
1638 $$ = $2;
1639 }
1640 ;
1641
1642begin_block : '{' top_compstmt '}'
1643 {
1644 /*%%%*/
1645 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1646 NEW_BEGIN($2, &@$));
1647 $$ = NEW_BEGIN(0, &@$);
1648 /*% %*/
1649 /*% ripper: BEGIN!($2) %*/
1650 }
1651 ;
1652
1653bodystmt : compstmt
1654 opt_rescue
1655 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1656 compstmt
1657 opt_ensure
1658 {
1659 /*%%%*/
1660 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1661 /*% %*/
1662 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1663 }
1664 | compstmt
1665 opt_rescue
1666 opt_ensure
1667 {
1668 /*%%%*/
1669 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1670 /*% %*/
1671 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1672 }
1673 ;
1674
1675compstmt : stmts opt_terms
1676 {
1677 $$ = void_stmts(p, $1);
1678 }
1679 ;
1680
1681stmts : none
1682 {
1683 /*%%%*/
1684 $$ = NEW_BEGIN(0, &@$);
1685 /*% %*/
1686 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1687 }
1688 | stmt_or_begin
1689 {
1690 /*%%%*/
1691 $$ = newline_node($1);
1692 /*% %*/
1693 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1694 }
1695 | stmts terms stmt_or_begin
1696 {
1697 /*%%%*/
1698 $$ = block_append(p, $1, newline_node($3));
1699 /*% %*/
1700 /*% ripper: stmts_add!($1, $3) %*/
1701 }
1702 ;
1703
1704stmt_or_begin : stmt
1705 {
1706 $$ = $1;
1707 }
1708 | keyword_BEGIN
1709 {
1710 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1711 }
1712 begin_block
1713 {
1714 $$ = $3;
1715 }
1716 ;
1717
1718stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1719 {
1720 /*%%%*/
1721 $$ = NEW_ALIAS($2, $4, &@$);
1722 /*% %*/
1723 /*% ripper: alias!($2, $4) %*/
1724 }
1725 | keyword_alias tGVAR tGVAR
1726 {
1727 /*%%%*/
1728 $$ = NEW_VALIAS($2, $3, &@$);
1729 /*% %*/
1730 /*% ripper: var_alias!($2, $3) %*/
1731 }
1732 | keyword_alias tGVAR tBACK_REF
1733 {
1734 /*%%%*/
1735 char buf[2];
1736 buf[0] = '$';
1737 buf[1] = (char)$3->nd_nth;
1738 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1739 /*% %*/
1740 /*% ripper: var_alias!($2, $3) %*/
1741 }
1742 | keyword_alias tGVAR tNTH_REF
1743 {
1744 static const char mesg[] = "can't make alias for the number variables";
1745 /*%%%*/
1746 yyerror1(&@3, mesg);
1747 $$ = NEW_BEGIN(0, &@$);
1748 /*% %*/
1749 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1750 }
1751 | keyword_undef undef_list
1752 {
1753 /*%%%*/
1754 $$ = $2;
1755 /*% %*/
1756 /*% ripper: undef!($2) %*/
1757 }
1758 | stmt modifier_if expr_value
1759 {
1760 /*%%%*/
1761 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1762 fixpos($$, $3);
1763 /*% %*/
1764 /*% ripper: if_mod!($3, $1) %*/
1765 }
1766 | stmt modifier_unless expr_value
1767 {
1768 /*%%%*/
1769 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1770 fixpos($$, $3);
1771 /*% %*/
1772 /*% ripper: unless_mod!($3, $1) %*/
1773 }
1774 | stmt modifier_while expr_value
1775 {
1776 /*%%%*/
1777 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1778 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1779 }
1780 else {
1781 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1782 }
1783 /*% %*/
1784 /*% ripper: while_mod!($3, $1) %*/
1785 }
1786 | stmt modifier_until expr_value
1787 {
1788 /*%%%*/
1789 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1790 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1791 }
1792 else {
1793 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1794 }
1795 /*% %*/
1796 /*% ripper: until_mod!($3, $1) %*/
1797 }
1798 | stmt modifier_rescue stmt
1799 {
1800 /*%%%*/
1801 NODE *resq;
1802 YYLTYPE loc = code_loc_gen(&@2, &@3);
1803 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1804 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1805 /*% %*/
1806 /*% ripper: rescue_mod!($1, $3) %*/
1807 }
1808 | keyword_END '{' compstmt '}'
1809 {
1810 if (p->ctxt.in_def) {
1811 rb_warn0("END in method; use at_exit");
1812 }
1813 /*%%%*/
1814 {
1815 NODE *scope = NEW_NODE(
1816 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1817 $$ = NEW_POSTEXE(scope, &@$);
1818 }
1819 /*% %*/
1820 /*% ripper: END!($3) %*/
1821 }
1822 | command_asgn
1823 | mlhs '=' lex_ctxt command_call
1824 {
1825 /*%%%*/
1826 value_expr($4);
1827 $$ = node_assign(p, $1, $4, $3, &@$);
1828 /*% %*/
1829 /*% ripper: massign!($1, $4) %*/
1830 }
1831 | lhs '=' lex_ctxt mrhs
1832 {
1833 /*%%%*/
1834 $$ = node_assign(p, $1, $4, $3, &@$);
1835 /*% %*/
1836 /*% ripper: assign!($1, $4) %*/
1837 }
1838 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1839 {
1840 /*%%%*/
1841 YYLTYPE loc = code_loc_gen(&@5, &@6);
1842 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1843 /*% %*/
1844 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1845 }
1846 | mlhs '=' lex_ctxt mrhs_arg
1847 {
1848 /*%%%*/
1849 $$ = node_assign(p, $1, $4, $3, &@$);
1850 /*% %*/
1851 /*% ripper: massign!($1, $4) %*/
1852 }
1853 | expr
1854 | error
1855 {
1856 /*%%%*/
1857 $$ = NEW_ERROR(&@$);
1858 /*% %*/
1859 }
1860 ;
1861
1862command_asgn : lhs '=' lex_ctxt command_rhs
1863 {
1864 /*%%%*/
1865 $$ = node_assign(p, $1, $4, $3, &@$);
1866 /*% %*/
1867 /*% ripper: assign!($1, $4) %*/
1868 }
1869 | var_lhs tOP_ASGN lex_ctxt command_rhs
1870 {
1871 /*%%%*/
1872 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1873 /*% %*/
1874 /*% ripper: opassign!($1, $2, $4) %*/
1875 }
1876 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1877 {
1878 /*%%%*/
1879 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1880 /*% %*/
1881 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1882
1883 }
1884 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1885 {
1886 /*%%%*/
1887 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1888 /*% %*/
1889 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1890 }
1891 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1892 {
1893 /*%%%*/
1894 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1895 /*% %*/
1896 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1897 }
1898 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1899 {
1900 /*%%%*/
1901 YYLTYPE loc = code_loc_gen(&@1, &@3);
1902 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1903 /*% %*/
1904 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1905 }
1906 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1907 {
1908 /*%%%*/
1909 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1910 /*% %*/
1911 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
1912 }
1913 | defn_head f_opt_paren_args '=' command
1914 {
1915 endless_method_name(p, $<node>1, &@1);
1916 restore_defun(p, $<node>1->nd_defn);
1917 /*%%%*/
1918 $$ = set_defun_body(p, $1, $2, $4, &@$);
1919 /*% %*/
1920 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1921 /*% ripper: def!(get_value($1), $2, $4) %*/
1922 local_pop(p);
1923 }
1924 | defn_head f_opt_paren_args '=' command modifier_rescue arg
1925 {
1926 endless_method_name(p, $<node>1, &@1);
1927 restore_defun(p, $<node>1->nd_defn);
1928 /*%%%*/
1929 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1930 $$ = set_defun_body(p, $1, $2, $4, &@$);
1931 /*% %*/
1932 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1933 /*% ripper: def!(get_value($1), $2, $4) %*/
1934 local_pop(p);
1935 }
1936 | defs_head f_opt_paren_args '=' command
1937 {
1938 endless_method_name(p, $<node>1, &@1);
1939 restore_defun(p, $<node>1->nd_defn);
1940 /*%%%*/
1941 $$ = set_defun_body(p, $1, $2, $4, &@$);
1942 /*%
1943 $1 = get_value($1);
1944 %*/
1945 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1946 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1947 local_pop(p);
1948 }
1949 | defs_head f_opt_paren_args '=' command modifier_rescue arg
1950 {
1951 endless_method_name(p, $<node>1, &@1);
1952 restore_defun(p, $<node>1->nd_defn);
1953 /*%%%*/
1954 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1955 $$ = set_defun_body(p, $1, $2, $4, &@$);
1956 /*%
1957 $1 = get_value($1);
1958 %*/
1959 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1960 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1961 local_pop(p);
1962 }
1963 | backref tOP_ASGN lex_ctxt command_rhs
1964 {
1965 /*%%%*/
1966 rb_backref_error(p, $1);
1967 $$ = NEW_BEGIN(0, &@$);
1968 /*% %*/
1969 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1970 }
1971 ;
1972
1973command_rhs : command_call %prec tOP_ASGN
1974 {
1975 value_expr($1);
1976 $$ = $1;
1977 }
1978 | command_call modifier_rescue stmt
1979 {
1980 /*%%%*/
1981 YYLTYPE loc = code_loc_gen(&@2, &@3);
1982 value_expr($1);
1983 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1984 /*% %*/
1985 /*% ripper: rescue_mod!($1, $3) %*/
1986 }
1987 | command_asgn
1988 ;
1989
1990expr : command_call
1991 | expr keyword_and expr
1992 {
1993 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1994 }
1995 | expr keyword_or expr
1996 {
1997 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1998 }
1999 | keyword_not opt_nl expr
2000 {
2001 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2002 }
2003 | '!' command_call
2004 {
2005 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2006 }
2007 | arg tASSOC
2008 {
2009 value_expr($1);
2010 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2011 p->command_start = FALSE;
2012 $<ctxt>2 = p->ctxt;
2013 p->ctxt.in_kwarg = 1;
2014 $<tbl>$ = push_pvtbl(p);
2015 }
2016 {
2017 $<tbl>$ = push_pktbl(p);
2018 }
2019 p_top_expr_body
2020 {
2021 pop_pktbl(p, $<tbl>4);
2022 pop_pvtbl(p, $<tbl>3);
2023 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2024 /*%%%*/
2025 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
2026 /*% %*/
2027 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2028 }
2029 | arg keyword_in
2030 {
2031 value_expr($1);
2032 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2033 p->command_start = FALSE;
2034 $<ctxt>2 = p->ctxt;
2035 p->ctxt.in_kwarg = 1;
2036 $<tbl>$ = push_pvtbl(p);
2037 }
2038 {
2039 $<tbl>$ = push_pktbl(p);
2040 }
2041 p_top_expr_body
2042 {
2043 pop_pktbl(p, $<tbl>4);
2044 pop_pvtbl(p, $<tbl>3);
2045 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2046 /*%%%*/
2047 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
2048 /*% %*/
2049 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2050 }
2051 | arg %prec tLBRACE_ARG
2052 ;
2053
2054def_name : fname
2055 {
2056 ID fname = get_id($1);
2057 ID cur_arg = p->cur_arg;
2058 YYSTYPE c = {.ctxt = p->ctxt};
2059 numparam_name(p, fname);
2060 local_push(p, 0);
2061 p->cur_arg = 0;
2062 p->ctxt.in_def = 1;
2063 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
2064 /*%%%*/
2065 /*%
2066 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
2067 %*/
2068 }
2069 ;
2070
2071defn_head : k_def def_name
2072 {
2073 $$ = $2;
2074 /*%%%*/
2075 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
2076 /*% %*/
2077 }
2078 ;
2079
2080defs_head : k_def singleton dot_or_colon
2081 {
2082 SET_LEX_STATE(EXPR_FNAME);
2083 p->ctxt.in_argdef = 1;
2084 }
2085 def_name
2086 {
2087 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2088 $$ = $5;
2089 /*%%%*/
2090 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
2091 /*%
2092 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
2093 add_mark_object(p, ary);
2094 $<node>$->nd_rval = ary;
2095 %*/
2096 }
2097 ;
2098
2099expr_value : expr
2100 {
2101 value_expr($1);
2102 $$ = $1;
2103 }
2104 | error
2105 {
2106 /*%%%*/
2107 $$ = NEW_ERROR(&@$);
2108 /*% %*/
2109 }
2110 ;
2111
2112expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2113 {
2114 $$ = $2;
2115 }
2116 ;
2117
2118command_call : command
2119 | block_command
2120 ;
2121
2122block_command : block_call
2123 | block_call call_op2 operation2 command_args
2124 {
2125 /*%%%*/
2126 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2127 /*% %*/
2128 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2129 }
2130 ;
2131
2132cmd_brace_block : tLBRACE_ARG brace_body '}'
2133 {
2134 $$ = $2;
2135 /*%%%*/
2136 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
2137 nd_set_line($$, @1.end_pos.lineno);
2138 /*% %*/
2139 }
2140 ;
2141
2142fcall : operation
2143 {
2144 /*%%%*/
2145 $$ = NEW_FCALL($1, 0, &@$);
2146 nd_set_line($$, p->tokline);
2147 /*% %*/
2148 /*% ripper: $1 %*/
2149 }
2150 ;
2151
2152command : fcall command_args %prec tLOWEST
2153 {
2154 /*%%%*/
2155 $1->nd_args = $2;
2156 nd_set_last_loc($1, @2.end_pos);
2157 $$ = $1;
2158 /*% %*/
2159 /*% ripper: command!($1, $2) %*/
2160 }
2161 | fcall command_args cmd_brace_block
2162 {
2163 /*%%%*/
2164 block_dup_check(p, $2, $3);
2165 $1->nd_args = $2;
2166 $$ = method_add_block(p, $1, $3, &@$);
2167 fixpos($$, $1);
2168 nd_set_last_loc($1, @2.end_pos);
2169 /*% %*/
2170 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2171 }
2172 | primary_value call_op operation2 command_args %prec tLOWEST
2173 {
2174 /*%%%*/
2175 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2176 /*% %*/
2177 /*% ripper: command_call!($1, $2, $3, $4) %*/
2178 }
2179 | primary_value call_op operation2 command_args cmd_brace_block
2180 {
2181 /*%%%*/
2182 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2183 /*% %*/
2184 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2185 }
2186 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2187 {
2188 /*%%%*/
2189 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2190 /*% %*/
2191 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
2192 }
2193 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2194 {
2195 /*%%%*/
2196 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2197 /*% %*/
2198 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
2199 }
2200 | keyword_super command_args
2201 {
2202 /*%%%*/
2203 $$ = NEW_SUPER($2, &@$);
2204 fixpos($$, $2);
2205 /*% %*/
2206 /*% ripper: super!($2) %*/
2207 }
2208 | keyword_yield command_args
2209 {
2210 /*%%%*/
2211 $$ = new_yield(p, $2, &@$);
2212 fixpos($$, $2);
2213 /*% %*/
2214 /*% ripper: yield!($2) %*/
2215 }
2216 | k_return call_args
2217 {
2218 /*%%%*/
2219 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2220 /*% %*/
2221 /*% ripper: return!($2) %*/
2222 }
2223 | keyword_break call_args
2224 {
2225 /*%%%*/
2226 $$ = NEW_BREAK(ret_args(p, $2), &@$);
2227 /*% %*/
2228 /*% ripper: break!($2) %*/
2229 }
2230 | keyword_next call_args
2231 {
2232 /*%%%*/
2233 $$ = NEW_NEXT(ret_args(p, $2), &@$);
2234 /*% %*/
2235 /*% ripper: next!($2) %*/
2236 }
2237 ;
2238
2239mlhs : mlhs_basic
2240 | tLPAREN mlhs_inner rparen
2241 {
2242 /*%%%*/
2243 $$ = $2;
2244 /*% %*/
2245 /*% ripper: mlhs_paren!($2) %*/
2246 }
2247 ;
2248
2249mlhs_inner : mlhs_basic
2250 | tLPAREN mlhs_inner rparen
2251 {
2252 /*%%%*/
2253 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
2254 /*% %*/
2255 /*% ripper: mlhs_paren!($2) %*/
2256 }
2257 ;
2258
2259mlhs_basic : mlhs_head
2260 {
2261 /*%%%*/
2262 $$ = NEW_MASGN($1, 0, &@$);
2263 /*% %*/
2264 /*% ripper: $1 %*/
2265 }
2266 | mlhs_head mlhs_item
2267 {
2268 /*%%%*/
2269 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2270 /*% %*/
2271 /*% ripper: mlhs_add!($1, $2) %*/
2272 }
2273 | mlhs_head tSTAR mlhs_node
2274 {
2275 /*%%%*/
2276 $$ = NEW_MASGN($1, $3, &@$);
2277 /*% %*/
2278 /*% ripper: mlhs_add_star!($1, $3) %*/
2279 }
2280 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2281 {
2282 /*%%%*/
2283 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2284 /*% %*/
2285 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2286 }
2287 | mlhs_head tSTAR
2288 {
2289 /*%%%*/
2290 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2291 /*% %*/
2292 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2293 }
2294 | mlhs_head tSTAR ',' mlhs_post
2295 {
2296 /*%%%*/
2297 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2298 /*% %*/
2299 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2300 }
2301 | tSTAR mlhs_node
2302 {
2303 /*%%%*/
2304 $$ = NEW_MASGN(0, $2, &@$);
2305 /*% %*/
2306 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2307 }
2308 | tSTAR mlhs_node ',' mlhs_post
2309 {
2310 /*%%%*/
2311 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2312 /*% %*/
2313 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2314 }
2315 | tSTAR
2316 {
2317 /*%%%*/
2318 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2319 /*% %*/
2320 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2321 }
2322 | tSTAR ',' mlhs_post
2323 {
2324 /*%%%*/
2325 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2326 /*% %*/
2327 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2328 }
2329 ;
2330
2331mlhs_item : mlhs_node
2332 | tLPAREN mlhs_inner rparen
2333 {
2334 /*%%%*/
2335 $$ = $2;
2336 /*% %*/
2337 /*% ripper: mlhs_paren!($2) %*/
2338 }
2339 ;
2340
2341mlhs_head : mlhs_item ','
2342 {
2343 /*%%%*/
2344 $$ = NEW_LIST($1, &@1);
2345 /*% %*/
2346 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2347 }
2348 | mlhs_head mlhs_item ','
2349 {
2350 /*%%%*/
2351 $$ = list_append(p, $1, $2);
2352 /*% %*/
2353 /*% ripper: mlhs_add!($1, $2) %*/
2354 }
2355 ;
2356
2357mlhs_post : mlhs_item
2358 {
2359 /*%%%*/
2360 $$ = NEW_LIST($1, &@$);
2361 /*% %*/
2362 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2363 }
2364 | mlhs_post ',' mlhs_item
2365 {
2366 /*%%%*/
2367 $$ = list_append(p, $1, $3);
2368 /*% %*/
2369 /*% ripper: mlhs_add!($1, $3) %*/
2370 }
2371 ;
2372
2373mlhs_node : user_variable
2374 {
2375 /*%%%*/
2376 $$ = assignable(p, $1, 0, &@$);
2377 /*% %*/
2378 /*% ripper: assignable(p, var_field(p, $1)) %*/
2379 }
2380 | keyword_variable
2381 {
2382 /*%%%*/
2383 $$ = assignable(p, $1, 0, &@$);
2384 /*% %*/
2385 /*% ripper: assignable(p, var_field(p, $1)) %*/
2386 }
2387 | primary_value '[' opt_call_args rbracket
2388 {
2389 /*%%%*/
2390 $$ = aryset(p, $1, $3, &@$);
2391 /*% %*/
2392 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2393 }
2394 | primary_value call_op tIDENTIFIER
2395 {
2396 if ($2 == tANDDOT) {
2397 yyerror1(&@2, "&. inside multiple assignment destination");
2398 }
2399 /*%%%*/
2400 $$ = attrset(p, $1, $2, $3, &@$);
2401 /*% %*/
2402 /*% ripper: field!($1, $2, $3) %*/
2403 }
2404 | primary_value tCOLON2 tIDENTIFIER
2405 {
2406 /*%%%*/
2407 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2408 /*% %*/
2409 /*% ripper: const_path_field!($1, $3) %*/
2410 }
2411 | primary_value call_op tCONSTANT
2412 {
2413 if ($2 == tANDDOT) {
2414 yyerror1(&@2, "&. inside multiple assignment destination");
2415 }
2416 /*%%%*/
2417 $$ = attrset(p, $1, $2, $3, &@$);
2418 /*% %*/
2419 /*% ripper: field!($1, $2, $3) %*/
2420 }
2421 | primary_value tCOLON2 tCONSTANT
2422 {
2423 /*%%%*/
2424 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2425 /*% %*/
2426 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2427 }
2428 | tCOLON3 tCONSTANT
2429 {
2430 /*%%%*/
2431 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2432 /*% %*/
2433 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2434 }
2435 | backref
2436 {
2437 /*%%%*/
2438 rb_backref_error(p, $1);
2439 $$ = NEW_BEGIN(0, &@$);
2440 /*% %*/
2441 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2442 }
2443 ;
2444
2445lhs : user_variable
2446 {
2447 /*%%%*/
2448 $$ = assignable(p, $1, 0, &@$);
2449 /*% %*/
2450 /*% ripper: assignable(p, var_field(p, $1)) %*/
2451 }
2452 | keyword_variable
2453 {
2454 /*%%%*/
2455 $$ = assignable(p, $1, 0, &@$);
2456 /*% %*/
2457 /*% ripper: assignable(p, var_field(p, $1)) %*/
2458 }
2459 | primary_value '[' opt_call_args rbracket
2460 {
2461 /*%%%*/
2462 $$ = aryset(p, $1, $3, &@$);
2463 /*% %*/
2464 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2465 }
2466 | primary_value call_op tIDENTIFIER
2467 {
2468 /*%%%*/
2469 $$ = attrset(p, $1, $2, $3, &@$);
2470 /*% %*/
2471 /*% ripper: field!($1, $2, $3) %*/
2472 }
2473 | primary_value tCOLON2 tIDENTIFIER
2474 {
2475 /*%%%*/
2476 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2477 /*% %*/
2478 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
2479 }
2480 | primary_value call_op tCONSTANT
2481 {
2482 /*%%%*/
2483 $$ = attrset(p, $1, $2, $3, &@$);
2484 /*% %*/
2485 /*% ripper: field!($1, $2, $3) %*/
2486 }
2487 | primary_value tCOLON2 tCONSTANT
2488 {
2489 /*%%%*/
2490 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2491 /*% %*/
2492 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2493 }
2494 | tCOLON3 tCONSTANT
2495 {
2496 /*%%%*/
2497 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2498 /*% %*/
2499 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2500 }
2501 | backref
2502 {
2503 /*%%%*/
2504 rb_backref_error(p, $1);
2505 $$ = NEW_BEGIN(0, &@$);
2506 /*% %*/
2507 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2508 }
2509 ;
2510
2511cname : tIDENTIFIER
2512 {
2513 static const char mesg[] = "class/module name must be CONSTANT";
2514 /*%%%*/
2515 yyerror1(&@1, mesg);
2516 /*% %*/
2517 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2518 }
2519 | tCONSTANT
2520 ;
2521
2522cpath : tCOLON3 cname
2523 {
2524 /*%%%*/
2525 $$ = NEW_COLON3($2, &@$);
2526 /*% %*/
2527 /*% ripper: top_const_ref!($2) %*/
2528 }
2529 | cname
2530 {
2531 /*%%%*/
2532 $$ = NEW_COLON2(0, $$, &@$);
2533 /*% %*/
2534 /*% ripper: const_ref!($1) %*/
2535 }
2536 | primary_value tCOLON2 cname
2537 {
2538 /*%%%*/
2539 $$ = NEW_COLON2($1, $3, &@$);
2540 /*% %*/
2541 /*% ripper: const_path_ref!($1, $3) %*/
2542 }
2543 ;
2544
2545fname : tIDENTIFIER
2546 | tCONSTANT
2547 | tFID
2548 | op
2549 {
2550 SET_LEX_STATE(EXPR_ENDFN);
2551 $$ = $1;
2552 }
2553 | reswords
2554 ;
2555
2556fitem : fname
2557 {
2558 /*%%%*/
2559 $$ = NEW_LIT(ID2SYM($1), &@$);
2560 /*% %*/
2561 /*% ripper: symbol_literal!($1) %*/
2562 }
2563 | symbol
2564 ;
2565
2566undef_list : fitem
2567 {
2568 /*%%%*/
2569 $$ = NEW_UNDEF($1, &@$);
2570 /*% %*/
2571 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2572 }
2573 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2574 {
2575 /*%%%*/
2576 NODE *undef = NEW_UNDEF($4, &@4);
2577 $$ = block_append(p, $1, undef);
2578 /*% %*/
2579 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2580 }
2581 ;
2582
2583op : '|' { ifndef_ripper($$ = '|'); }
2584 | '^' { ifndef_ripper($$ = '^'); }
2585 | '&' { ifndef_ripper($$ = '&'); }
2586 | tCMP { ifndef_ripper($$ = tCMP); }
2587 | tEQ { ifndef_ripper($$ = tEQ); }
2588 | tEQQ { ifndef_ripper($$ = tEQQ); }
2589 | tMATCH { ifndef_ripper($$ = tMATCH); }
2590 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2591 | '>' { ifndef_ripper($$ = '>'); }
2592 | tGEQ { ifndef_ripper($$ = tGEQ); }
2593 | '<' { ifndef_ripper($$ = '<'); }
2594 | tLEQ { ifndef_ripper($$ = tLEQ); }
2595 | tNEQ { ifndef_ripper($$ = tNEQ); }
2596 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2597 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2598 | '+' { ifndef_ripper($$ = '+'); }
2599 | '-' { ifndef_ripper($$ = '-'); }
2600 | '*' { ifndef_ripper($$ = '*'); }
2601 | tSTAR { ifndef_ripper($$ = '*'); }
2602 | '/' { ifndef_ripper($$ = '/'); }
2603 | '%' { ifndef_ripper($$ = '%'); }
2604 | tPOW { ifndef_ripper($$ = tPOW); }
2605 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2606 | '!' { ifndef_ripper($$ = '!'); }
2607 | '~' { ifndef_ripper($$ = '~'); }
2608 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2609 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2610 | tAREF { ifndef_ripper($$ = tAREF); }
2611 | tASET { ifndef_ripper($$ = tASET); }
2612 | '`' { ifndef_ripper($$ = '`'); }
2613 ;
2614
2615reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2616 | keyword_BEGIN | keyword_END
2617 | keyword_alias | keyword_and | keyword_begin
2618 | keyword_break | keyword_case | keyword_class | keyword_def
2619 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2620 | keyword_end | keyword_ensure | keyword_false
2621 | keyword_for | keyword_in | keyword_module | keyword_next
2622 | keyword_nil | keyword_not | keyword_or | keyword_redo
2623 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2624 | keyword_super | keyword_then | keyword_true | keyword_undef
2625 | keyword_when | keyword_yield | keyword_if | keyword_unless
2626 | keyword_while | keyword_until
2627 ;
2628
2629arg : lhs '=' lex_ctxt arg_rhs
2630 {
2631 /*%%%*/
2632 $$ = node_assign(p, $1, $4, $3, &@$);
2633 /*% %*/
2634 /*% ripper: assign!($1, $4) %*/
2635 }
2636 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2637 {
2638 /*%%%*/
2639 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2640 /*% %*/
2641 /*% ripper: opassign!($1, $2, $4) %*/
2642 }
2643 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2644 {
2645 /*%%%*/
2646 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2647 /*% %*/
2648 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2649 }
2650 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2651 {
2652 /*%%%*/
2653 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2654 /*% %*/
2655 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2656 }
2657 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2658 {
2659 /*%%%*/
2660 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2661 /*% %*/
2662 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2663 }
2664 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2665 {
2666 /*%%%*/
2667 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2668 /*% %*/
2669 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/
2670 }
2671 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2672 {
2673 /*%%%*/
2674 YYLTYPE loc = code_loc_gen(&@1, &@3);
2675 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2676 /*% %*/
2677 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2678 }
2679 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2680 {
2681 /*%%%*/
2682 YYLTYPE loc = code_loc_gen(&@1, &@2);
2683 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2684 /*% %*/
2685 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2686 }
2687 | backref tOP_ASGN lex_ctxt arg_rhs
2688 {
2689 /*%%%*/
2690 rb_backref_error(p, $1);
2691 $$ = NEW_BEGIN(0, &@$);
2692 /*% %*/
2693 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2694 }
2695 | arg tDOT2 arg
2696 {
2697 /*%%%*/
2698 value_expr($1);
2699 value_expr($3);
2700 $$ = NEW_DOT2($1, $3, &@$);
2701 /*% %*/
2702 /*% ripper: dot2!($1, $3) %*/
2703 }
2704 | arg tDOT3 arg
2705 {
2706 /*%%%*/
2707 value_expr($1);
2708 value_expr($3);
2709 $$ = NEW_DOT3($1, $3, &@$);
2710 /*% %*/
2711 /*% ripper: dot3!($1, $3) %*/
2712 }
2713 | arg tDOT2
2714 {
2715 /*%%%*/
2716 value_expr($1);
2717 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2718 /*% %*/
2719 /*% ripper: dot2!($1, Qnil) %*/
2720 }
2721 | arg tDOT3
2722 {
2723 /*%%%*/
2724 value_expr($1);
2725 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2726 /*% %*/
2727 /*% ripper: dot3!($1, Qnil) %*/
2728 }
2729 | tBDOT2 arg
2730 {
2731 /*%%%*/
2732 value_expr($2);
2733 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2734 /*% %*/
2735 /*% ripper: dot2!(Qnil, $2) %*/
2736 }
2737 | tBDOT3 arg
2738 {
2739 /*%%%*/
2740 value_expr($2);
2741 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2742 /*% %*/
2743 /*% ripper: dot3!(Qnil, $2) %*/
2744 }
2745 | arg '+' arg
2746 {
2747 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2748 }
2749 | arg '-' arg
2750 {
2751 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2752 }
2753 | arg '*' arg
2754 {
2755 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2756 }
2757 | arg '/' arg
2758 {
2759 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2760 }
2761 | arg '%' arg
2762 {
2763 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2764 }
2765 | arg tPOW arg
2766 {
2767 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2768 }
2769 | tUMINUS_NUM simple_numeric tPOW arg
2770 {
2771 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2772 }
2773 | tUPLUS arg
2774 {
2775 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2776 }
2777 | tUMINUS arg
2778 {
2779 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2780 }
2781 | arg '|' arg
2782 {
2783 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2784 }
2785 | arg '^' arg
2786 {
2787 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2788 }
2789 | arg '&' arg
2790 {
2791 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2792 }
2793 | arg tCMP arg
2794 {
2795 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2796 }
2797 | rel_expr %prec tCMP
2798 | arg tEQ arg
2799 {
2800 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2801 }
2802 | arg tEQQ arg
2803 {
2804 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2805 }
2806 | arg tNEQ arg
2807 {
2808 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2809 }
2810 | arg tMATCH arg
2811 {
2812 $$ = match_op(p, $1, $3, &@2, &@$);
2813 }
2814 | arg tNMATCH arg
2815 {
2816 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2817 }
2818 | '!' arg
2819 {
2820 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2821 }
2822 | '~' arg
2823 {
2824 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2825 }
2826 | arg tLSHFT arg
2827 {
2828 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2829 }
2830 | arg tRSHFT arg
2831 {
2832 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2833 }
2834 | arg tANDOP arg
2835 {
2836 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2837 }
2838 | arg tOROP arg
2839 {
2840 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2841 }
2842 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2843 {
2844 p->ctxt.in_defined = 0;
2845 $$ = new_defined(p, $4, &@$);
2846 }
2847 | arg '?' arg opt_nl ':' arg
2848 {
2849 /*%%%*/
2850 value_expr($1);
2851 $$ = new_if(p, $1, $3, $6, &@$);
2852 fixpos($$, $1);
2853 /*% %*/
2854 /*% ripper: ifop!($1, $3, $6) %*/
2855 }
2856 | defn_head f_opt_paren_args '=' arg
2857 {
2858 endless_method_name(p, $<node>1, &@1);
2859 restore_defun(p, $<node>1->nd_defn);
2860 /*%%%*/
2861 $$ = set_defun_body(p, $1, $2, $4, &@$);
2862 /*% %*/
2863 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2864 /*% ripper: def!(get_value($1), $2, $4) %*/
2865 local_pop(p);
2866 }
2867 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2868 {
2869 endless_method_name(p, $<node>1, &@1);
2870 restore_defun(p, $<node>1->nd_defn);
2871 /*%%%*/
2872 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2873 $$ = set_defun_body(p, $1, $2, $4, &@$);
2874 /*% %*/
2875 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2876 /*% ripper: def!(get_value($1), $2, $4) %*/
2877 local_pop(p);
2878 }
2879 | defs_head f_opt_paren_args '=' arg
2880 {
2881 endless_method_name(p, $<node>1, &@1);
2882 restore_defun(p, $<node>1->nd_defn);
2883 /*%%%*/
2884 $$ = set_defun_body(p, $1, $2, $4, &@$);
2885 /*%
2886 $1 = get_value($1);
2887 %*/
2888 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2889 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2890 local_pop(p);
2891 }
2892 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2893 {
2894 endless_method_name(p, $<node>1, &@1);
2895 restore_defun(p, $<node>1->nd_defn);
2896 /*%%%*/
2897 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2898 $$ = set_defun_body(p, $1, $2, $4, &@$);
2899 /*%
2900 $1 = get_value($1);
2901 %*/
2902 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2903 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2904 local_pop(p);
2905 }
2906 | primary
2907 {
2908 $$ = $1;
2909 }
2910 ;
2911
2912relop : '>' {$$ = '>';}
2913 | '<' {$$ = '<';}
2914 | tGEQ {$$ = idGE;}
2915 | tLEQ {$$ = idLE;}
2916 ;
2917
2918rel_expr : arg relop arg %prec '>'
2919 {
2920 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2921 }
2922 | rel_expr relop arg %prec '>'
2923 {
2924 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2925 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2926 }
2927 ;
2928
2929lex_ctxt : none
2930 {
2931 $$ = p->ctxt;
2932 }
2933 ;
2934
2935arg_value : arg
2936 {
2937 value_expr($1);
2938 $$ = $1;
2939 }
2940 ;
2941
2942aref_args : none
2943 | args trailer
2944 {
2945 $$ = $1;
2946 }
2947 | args ',' assocs trailer
2948 {
2949 /*%%%*/
2950 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2951 /*% %*/
2952 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2953 }
2954 | assocs trailer
2955 {
2956 /*%%%*/
2957 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2958 /*% %*/
2959 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2960 }
2961 ;
2962
2963arg_rhs : arg %prec tOP_ASGN
2964 {
2965 value_expr($1);
2966 $$ = $1;
2967 }
2968 | arg modifier_rescue arg
2969 {
2970 /*%%%*/
2971 value_expr($1);
2972 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2973 /*% %*/
2974 /*% ripper: rescue_mod!($1, $3) %*/
2975 }
2976 ;
2977
2978paren_args : '(' opt_call_args rparen
2979 {
2980 /*%%%*/
2981 $$ = $2;
2982 /*% %*/
2983 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2984 }
2985 | '(' args ',' args_forward rparen
2986 {
2987 if (!check_forwarding_args(p)) {
2988 $$ = Qnone;
2989 }
2990 else {
2991 /*%%%*/
2992 $$ = new_args_forward_call(p, $2, &@4, &@$);
2993 /*% %*/
2994 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
2995 }
2996 }
2997 | '(' args_forward rparen
2998 {
2999 if (!check_forwarding_args(p)) {
3000 $$ = Qnone;
3001 }
3002 else {
3003 /*%%%*/
3004 $$ = new_args_forward_call(p, 0, &@2, &@$);
3005 /*% %*/
3006 /*% ripper: arg_paren!($2) %*/
3007 }
3008 }
3009 ;
3010
3011opt_paren_args : none
3012 | paren_args
3013 ;
3014
3015opt_call_args : none
3016 | call_args
3017 | args ','
3018 {
3019 $$ = $1;
3020 }
3021 | args ',' assocs ','
3022 {
3023 /*%%%*/
3024 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3025 /*% %*/
3026 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3027 }
3028 | assocs ','
3029 {
3030 /*%%%*/
3031 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3032 /*% %*/
3033 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3034 }
3035 ;
3036
3037call_args : command
3038 {
3039 /*%%%*/
3040 value_expr($1);
3041 $$ = NEW_LIST($1, &@$);
3042 /*% %*/
3043 /*% ripper: args_add!(args_new!, $1) %*/
3044 }
3045 | args opt_block_arg
3046 {
3047 /*%%%*/
3048 $$ = arg_blk_pass($1, $2);
3049 /*% %*/
3050 /*% ripper: args_add_block!($1, $2) %*/
3051 }
3052 | assocs opt_block_arg
3053 {
3054 /*%%%*/
3055 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3056 $$ = arg_blk_pass($$, $2);
3057 /*% %*/
3058 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3059 }
3060 | args ',' assocs opt_block_arg
3061 {
3062 /*%%%*/
3063 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3064 $$ = arg_blk_pass($$, $4);
3065 /*% %*/
3066 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3067 }
3068 | block_arg
3069 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3070 ;
3071
3072command_args : {
3073 /* If call_args starts with a open paren '(' or '[',
3074 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3075 * but the push must be done after CMDARG_PUSH(1).
3076 * So this code makes them consistent by first cancelling
3077 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3078 * and finally redoing CMDARG_PUSH(0).
3079 */
3080 int lookahead = 0;
3081 switch (yychar) {
3082 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3083 lookahead = 1;
3084 }
3085 if (lookahead) CMDARG_POP();
3086 CMDARG_PUSH(1);
3087 if (lookahead) CMDARG_PUSH(0);
3088 }
3089 call_args
3090 {
3091 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3092 * but the push must be done after CMDARG_POP() in the parser.
3093 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3094 * CMDARG_POP() to pop 1 pushed by command_args,
3095 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3096 */
3097 int lookahead = 0;
3098 switch (yychar) {
3099 case tLBRACE_ARG:
3100 lookahead = 1;
3101 }
3102 if (lookahead) CMDARG_POP();
3103 CMDARG_POP();
3104 if (lookahead) CMDARG_PUSH(0);
3105 $$ = $2;
3106 }
3107 ;
3108
3109block_arg : tAMPER arg_value
3110 {
3111 /*%%%*/
3112 $$ = NEW_BLOCK_PASS($2, &@$);
3113 /*% %*/
3114 /*% ripper: $2 %*/
3115 }
3116 | tAMPER
3117 {
3118 if (!local_id(p, idFWD_BLOCK)) {
3119 compile_error(p, "no anonymous block parameter");
3120 }
3121 /*%%%*/
3122 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3123 /*% %*/
3124 /*% ripper: Qnil %*/
3125 }
3126 ;
3127
3128opt_block_arg : ',' block_arg
3129 {
3130 $$ = $2;
3131 }
3132 | none
3133 {
3134 $$ = 0;
3135 }
3136 ;
3137
3138/* value */
3139args : arg_value
3140 {
3141 /*%%%*/
3142 $$ = NEW_LIST($1, &@$);
3143 /*% %*/
3144 /*% ripper: args_add!(args_new!, $1) %*/
3145 }
3146 | tSTAR arg_value
3147 {
3148 /*%%%*/
3149 $$ = NEW_SPLAT($2, &@$);
3150 /*% %*/
3151 /*% ripper: args_add_star!(args_new!, $2) %*/
3152 }
3153 | tSTAR
3154 {
3155 if (!local_id(p, idFWD_REST) ||
3156 local_id(p, idFWD_ALL)) {
3157 compile_error(p, "no anonymous rest parameter");
3158 }
3159 /*%%%*/
3160 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@1), &@$);
3161 /*% %*/
3162 /*% ripper: args_add_star!(args_new!, Qnil) %*/
3163 }
3164 | args ',' arg_value
3165 {
3166 /*%%%*/
3167 $$ = last_arg_append(p, $1, $3, &@$);
3168 /*% %*/
3169 /*% ripper: args_add!($1, $3) %*/
3170 }
3171 | args ',' tSTAR arg_value
3172 {
3173 /*%%%*/
3174 $$ = rest_arg_append(p, $1, $4, &@$);
3175 /*% %*/
3176 /*% ripper: args_add_star!($1, $4) %*/
3177 }
3178 | args ',' tSTAR
3179 {
3180 if (!local_id(p, idFWD_REST) ||
3181 local_id(p, idFWD_ALL)) {
3182 compile_error(p, "no anonymous rest parameter");
3183 }
3184 /*%%%*/
3185 $$ = rest_arg_append(p, $1, NEW_LVAR(idFWD_REST, &@3), &@$);
3186 /*% %*/
3187 /*% ripper: args_add_star!($1, Qnil) %*/
3188 }
3189 ;
3190
3191/* value */
3192mrhs_arg : mrhs
3193 | arg_value
3194 ;
3195
3196/* value */
3197mrhs : args ',' arg_value
3198 {
3199 /*%%%*/
3200 $$ = last_arg_append(p, $1, $3, &@$);
3201 /*% %*/
3202 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3203 }
3204 | args ',' tSTAR arg_value
3205 {
3206 /*%%%*/
3207 $$ = rest_arg_append(p, $1, $4, &@$);
3208 /*% %*/
3209 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3210 }
3211 | tSTAR arg_value
3212 {
3213 /*%%%*/
3214 $$ = NEW_SPLAT($2, &@$);
3215 /*% %*/
3216 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3217 }
3218 ;
3219
3220primary : literal
3221 | strings
3222 | xstring
3223 | regexp
3224 | words
3225 | qwords
3226 | symbols
3227 | qsymbols
3228 | var_ref
3229 | backref
3230 | tFID
3231 {
3232 /*%%%*/
3233 $$ = NEW_FCALL($1, 0, &@$);
3234 /*% %*/
3235 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3236 }
3237 | k_begin
3238 {
3239 CMDARG_PUSH(0);
3240 }
3241 bodystmt
3242 k_end
3243 {
3244 CMDARG_POP();
3245 /*%%%*/
3246 set_line_body($3, @1.end_pos.lineno);
3247 $$ = NEW_BEGIN($3, &@$);
3248 nd_set_line($$, @1.end_pos.lineno);
3249 /*% %*/
3250 /*% ripper: begin!($3) %*/
3251 }
3252 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
3253 {
3254 /*%%%*/
3255 $$ = NEW_BEGIN(0, &@$);
3256 /*% %*/
3257 /*% ripper: paren!(0) %*/
3258 }
3259 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
3260 {
3261 /*%%%*/
3262 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3263 $$ = $2;
3264 /*% %*/
3265 /*% ripper: paren!($2) %*/
3266 }
3267 | tLPAREN compstmt ')'
3268 {
3269 /*%%%*/
3270 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3271 $$ = $2;
3272 /*% %*/
3273 /*% ripper: paren!($2) %*/
3274 }
3275 | primary_value tCOLON2 tCONSTANT
3276 {
3277 /*%%%*/
3278 $$ = NEW_COLON2($1, $3, &@$);
3279 /*% %*/
3280 /*% ripper: const_path_ref!($1, $3) %*/
3281 }
3282 | tCOLON3 tCONSTANT
3283 {
3284 /*%%%*/
3285 $$ = NEW_COLON3($2, &@$);
3286 /*% %*/
3287 /*% ripper: top_const_ref!($2) %*/
3288 }
3289 | tLBRACK aref_args ']'
3290 {
3291 /*%%%*/
3292 $$ = make_list($2, &@$);
3293 /*% %*/
3294 /*% ripper: array!(escape_Qundef($2)) %*/
3295 }
3296 | tLBRACE assoc_list '}'
3297 {
3298 /*%%%*/
3299 $$ = new_hash(p, $2, &@$);
3300 $$->nd_brace = TRUE;
3301 /*% %*/
3302 /*% ripper: hash!(escape_Qundef($2)) %*/
3303 }
3304 | k_return
3305 {
3306 /*%%%*/
3307 $$ = NEW_RETURN(0, &@$);
3308 /*% %*/
3309 /*% ripper: return0! %*/
3310 }
3311 | keyword_yield '(' call_args rparen
3312 {
3313 /*%%%*/
3314 $$ = new_yield(p, $3, &@$);
3315 /*% %*/
3316 /*% ripper: yield!(paren!($3)) %*/
3317 }
3318 | keyword_yield '(' rparen
3319 {
3320 /*%%%*/
3321 $$ = NEW_YIELD(0, &@$);
3322 /*% %*/
3323 /*% ripper: yield!(paren!(args_new!)) %*/
3324 }
3325 | keyword_yield
3326 {
3327 /*%%%*/
3328 $$ = NEW_YIELD(0, &@$);
3329 /*% %*/
3330 /*% ripper: yield0! %*/
3331 }
3332 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3333 {
3334 p->ctxt.in_defined = 0;
3335 $$ = new_defined(p, $5, &@$);
3336 }
3337 | keyword_not '(' expr rparen
3338 {
3339 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3340 }
3341 | keyword_not '(' rparen
3342 {
3343 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3344 }
3345 | fcall brace_block
3346 {
3347 /*%%%*/
3348 $$ = method_add_block(p, $1, $2, &@$);
3349 /*% %*/
3350 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3351 }
3352 | method_call
3353 | method_call brace_block
3354 {
3355 /*%%%*/
3356 block_dup_check(p, $1->nd_args, $2);
3357 $$ = method_add_block(p, $1, $2, &@$);
3358 /*% %*/
3359 /*% ripper: method_add_block!($1, $2) %*/
3360 }
3361 | lambda
3362 | k_if expr_value then
3363 compstmt
3364 if_tail
3365 k_end
3366 {
3367 /*%%%*/
3368 $$ = new_if(p, $2, $4, $5, &@$);
3369 fixpos($$, $2);
3370 /*% %*/
3371 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3372 }
3373 | k_unless expr_value then
3374 compstmt
3375 opt_else
3376 k_end
3377 {
3378 /*%%%*/
3379 $$ = new_unless(p, $2, $4, $5, &@$);
3380 fixpos($$, $2);
3381 /*% %*/
3382 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3383 }
3384 | k_while expr_value_do
3385 compstmt
3386 k_end
3387 {
3388 /*%%%*/
3389 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3390 fixpos($$, $2);
3391 /*% %*/
3392 /*% ripper: while!($2, $3) %*/
3393 }
3394 | k_until expr_value_do
3395 compstmt
3396 k_end
3397 {
3398 /*%%%*/
3399 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3400 fixpos($$, $2);
3401 /*% %*/
3402 /*% ripper: until!($2, $3) %*/
3403 }
3404 | k_case expr_value opt_terms
3405 {
3406 $<val>$ = p->case_labels;
3407 p->case_labels = Qnil;
3408 }
3409 case_body
3410 k_end
3411 {
3412 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3413 p->case_labels = $<val>4;
3414 /*%%%*/
3415 $$ = NEW_CASE($2, $5, &@$);
3416 fixpos($$, $2);
3417 /*% %*/
3418 /*% ripper: case!($2, $5) %*/
3419 }
3420 | k_case opt_terms
3421 {
3422 $<val>$ = p->case_labels;
3423 p->case_labels = 0;
3424 }
3425 case_body
3426 k_end
3427 {
3428 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3429 p->case_labels = $<val>3;
3430 /*%%%*/
3431 $$ = NEW_CASE2($4, &@$);
3432 /*% %*/
3433 /*% ripper: case!(Qnil, $4) %*/
3434 }
3435 | k_case expr_value opt_terms
3436 p_case_body
3437 k_end
3438 {
3439 /*%%%*/
3440 $$ = NEW_CASE3($2, $4, &@$);
3441 /*% %*/
3442 /*% ripper: case!($2, $4) %*/
3443 }
3444 | k_for for_var keyword_in expr_value_do
3445 compstmt
3446 k_end
3447 {
3448 /*%%%*/
3449 /*
3450 * for a, b, c in e
3451 * #=>
3452 * e.each{|*x| a, b, c = x}
3453 *
3454 * for a in e
3455 * #=>
3456 * e.each{|x| a, = x}
3457 */
3458 ID id = internal_id(p);
3459 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3460 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3461 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3462 tbl->ids[0] = id; /* internal id */
3463
3464 switch (nd_type($2)) {
3465 case NODE_LASGN:
3466 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3467 $2->nd_value = internal_var;
3468 id = 0;
3469 m->nd_plen = 1;
3470 m->nd_next = $2;
3471 break;
3472 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3473 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3474 break;
3475 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3476 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3477 }
3478 /* {|*internal_id| <m> = internal_id; ... } */
3479 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3480 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3481 $$ = NEW_FOR($4, scope, &@$);
3482 fixpos($$, $2);
3483 /*% %*/
3484 /*% ripper: for!($2, $4, $5) %*/
3485 }
3486 | k_class cpath superclass
3487 {
3488 if (p->ctxt.in_def) {
3489 YYLTYPE loc = code_loc_gen(&@1, &@2);
3490 yyerror1(&loc, "class definition in method body");
3491 }
3492 p->ctxt.in_class = 1;
3493 local_push(p, 0);
3494 }
3495 bodystmt
3496 k_end
3497 {
3498 /*%%%*/
3499 $$ = NEW_CLASS($2, $5, $3, &@$);
3500 nd_set_line($$->nd_body, @6.end_pos.lineno);
3501 set_line_body($5, @3.end_pos.lineno);
3502 nd_set_line($$, @3.end_pos.lineno);
3503 /*% %*/
3504 /*% ripper: class!($2, $3, $5) %*/
3505 local_pop(p);
3506 p->ctxt.in_class = $<ctxt>1.in_class;
3507 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3508 }
3509 | k_class tLSHFT expr
3510 {
3511 p->ctxt.in_def = 0;
3512 p->ctxt.in_class = 0;
3513 local_push(p, 0);
3514 }
3515 term
3516 bodystmt
3517 k_end
3518 {
3519 /*%%%*/
3520 $$ = NEW_SCLASS($3, $6, &@$);
3521 nd_set_line($$->nd_body, @7.end_pos.lineno);
3522 set_line_body($6, nd_line($3));
3523 fixpos($$, $3);
3524 /*% %*/
3525 /*% ripper: sclass!($3, $6) %*/
3526 local_pop(p);
3527 p->ctxt.in_def = $<ctxt>1.in_def;
3528 p->ctxt.in_class = $<ctxt>1.in_class;
3529 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3530 }
3531 | k_module cpath
3532 {
3533 if (p->ctxt.in_def) {
3534 YYLTYPE loc = code_loc_gen(&@1, &@2);
3535 yyerror1(&loc, "module definition in method body");
3536 }
3537 p->ctxt.in_class = 1;
3538 local_push(p, 0);
3539 }
3540 bodystmt
3541 k_end
3542 {
3543 /*%%%*/
3544 $$ = NEW_MODULE($2, $4, &@$);
3545 nd_set_line($$->nd_body, @5.end_pos.lineno);
3546 set_line_body($4, @2.end_pos.lineno);
3547 nd_set_line($$, @2.end_pos.lineno);
3548 /*% %*/
3549 /*% ripper: module!($2, $4) %*/
3550 local_pop(p);
3551 p->ctxt.in_class = $<ctxt>1.in_class;
3552 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3553 }
3554 | defn_head
3555 f_arglist
3556 {
3557 /*%%%*/
3558 push_end_expect_token_locations(p, &@1.beg_pos);
3559 /*% %*/
3560 }
3561 bodystmt
3562 k_end
3563 {
3564 restore_defun(p, $<node>1->nd_defn);
3565 /*%%%*/
3566 $$ = set_defun_body(p, $1, $2, $4, &@$);
3567 /*% %*/
3568 /*% ripper: def!(get_value($1), $2, $4) %*/
3569 local_pop(p);
3570 }
3571 | defs_head
3572 f_arglist
3573 {
3574 /*%%%*/
3575 push_end_expect_token_locations(p, &@1.beg_pos);
3576 /*% %*/
3577 }
3578 bodystmt
3579 k_end
3580 {
3581 restore_defun(p, $<node>1->nd_defn);
3582 /*%%%*/
3583 $$ = set_defun_body(p, $1, $2, $4, &@$);
3584 /*%
3585 $1 = get_value($1);
3586 %*/
3587 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
3588 local_pop(p);
3589 }
3590 | keyword_break
3591 {
3592 /*%%%*/
3593 $$ = NEW_BREAK(0, &@$);
3594 /*% %*/
3595 /*% ripper: break!(args_new!) %*/
3596 }
3597 | keyword_next
3598 {
3599 /*%%%*/
3600 $$ = NEW_NEXT(0, &@$);
3601 /*% %*/
3602 /*% ripper: next!(args_new!) %*/
3603 }
3604 | keyword_redo
3605 {
3606 /*%%%*/
3607 $$ = NEW_REDO(&@$);
3608 /*% %*/
3609 /*% ripper: redo! %*/
3610 }
3611 | keyword_retry
3612 {
3613 /*%%%*/
3614 $$ = NEW_RETRY(&@$);
3615 /*% %*/
3616 /*% ripper: retry! %*/
3617 }
3618 ;
3619
3620primary_value : primary
3621 {
3622 value_expr($1);
3623 $$ = $1;
3624 }
3625 ;
3626
3627k_begin : keyword_begin
3628 {
3629 token_info_push(p, "begin", &@$);
3630 /*%%%*/
3631 push_end_expect_token_locations(p, &@1.beg_pos);
3632 /*% %*/
3633 }
3634 ;
3635
3636k_if : keyword_if
3637 {
3638 WARN_EOL("if");
3639 token_info_push(p, "if", &@$);
3640 if (p->token_info && p->token_info->nonspc &&
3641 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3642 const char *tok = p->lex.ptok - rb_strlen_lit("if");
3643 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3644 beg += rb_strlen_lit("else");
3645 while (beg < tok && ISSPACE(*beg)) beg++;
3646 if (beg == tok) {
3647 p->token_info->nonspc = 0;
3648 }
3649 }
3650 /*%%%*/
3651 push_end_expect_token_locations(p, &@1.beg_pos);
3652 /*% %*/
3653 }
3654 ;
3655
3656k_unless : keyword_unless
3657 {
3658 token_info_push(p, "unless", &@$);
3659 /*%%%*/
3660 push_end_expect_token_locations(p, &@1.beg_pos);
3661 /*% %*/
3662 }
3663 ;
3664
3665k_while : keyword_while
3666 {
3667 token_info_push(p, "while", &@$);
3668 /*%%%*/
3669 push_end_expect_token_locations(p, &@1.beg_pos);
3670 /*% %*/
3671 }
3672 ;
3673
3674k_until : keyword_until
3675 {
3676 token_info_push(p, "until", &@$);
3677 /*%%%*/
3678 push_end_expect_token_locations(p, &@1.beg_pos);
3679 /*% %*/
3680 }
3681 ;
3682
3683k_case : keyword_case
3684 {
3685 token_info_push(p, "case", &@$);
3686 /*%%%*/
3687 push_end_expect_token_locations(p, &@1.beg_pos);
3688 /*% %*/
3689 }
3690 ;
3691
3692k_for : keyword_for
3693 {
3694 token_info_push(p, "for", &@$);
3695 /*%%%*/
3696 push_end_expect_token_locations(p, &@1.beg_pos);
3697 /*% %*/
3698 }
3699 ;
3700
3701k_class : keyword_class
3702 {
3703 token_info_push(p, "class", &@$);
3704 $<ctxt>$ = p->ctxt;
3705 /*%%%*/
3706 push_end_expect_token_locations(p, &@1.beg_pos);
3707 /*% %*/
3708 }
3709 ;
3710
3711k_module : keyword_module
3712 {
3713 token_info_push(p, "module", &@$);
3714 $<ctxt>$ = p->ctxt;
3715 /*%%%*/
3716 push_end_expect_token_locations(p, &@1.beg_pos);
3717 /*% %*/
3718 }
3719 ;
3720
3721k_def : keyword_def
3722 {
3723 token_info_push(p, "def", &@$);
3724 p->ctxt.in_argdef = 1;
3725 }
3726 ;
3727
3728k_do : keyword_do
3729 {
3730 token_info_push(p, "do", &@$);
3731 /*%%%*/
3732 push_end_expect_token_locations(p, &@1.beg_pos);
3733 /*% %*/
3734
3735 }
3736 ;
3737
3738k_do_block : keyword_do_block
3739 {
3740 token_info_push(p, "do", &@$);
3741 /*%%%*/
3742 push_end_expect_token_locations(p, &@1.beg_pos);
3743 /*% %*/
3744 }
3745 ;
3746
3747k_rescue : keyword_rescue
3748 {
3749 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3750 }
3751 ;
3752
3753k_ensure : keyword_ensure
3754 {
3755 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3756 }
3757 ;
3758
3759k_when : keyword_when
3760 {
3761 token_info_warn(p, "when", p->token_info, 0, &@$);
3762 }
3763 ;
3764
3765k_else : keyword_else
3766 {
3767 token_info *ptinfo_beg = p->token_info;
3768 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3769 token_info_warn(p, "else", p->token_info, same, &@$);
3770 if (same) {
3771 token_info e;
3772 e.next = ptinfo_beg->next;
3773 e.token = "else";
3774 token_info_setup(&e, p->lex.pbeg, &@$);
3775 if (!e.nonspc) *ptinfo_beg = e;
3776 }
3777 }
3778 ;
3779
3780k_elsif : keyword_elsif
3781 {
3782 WARN_EOL("elsif");
3783 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3784 }
3785 ;
3786
3787k_end : keyword_end
3788 {
3789 token_info_pop(p, "end", &@$);
3790 /*%%%*/
3791 pop_end_expect_token_locations(p);
3792 /*% %*/
3793 }
3794 | tDUMNY_END
3795 {
3796 compile_error(p, "syntax error, unexpected end-of-input");
3797 }
3798 ;
3799
3800k_return : keyword_return
3801 {
3802 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3803 yyerror1(&@1, "Invalid return in class/module body");
3804 }
3805 ;
3806
3807then : term
3808 | keyword_then
3809 | term keyword_then
3810 ;
3811
3812do : term
3813 | keyword_do_cond
3814 ;
3815
3816if_tail : opt_else
3817 | k_elsif expr_value then
3818 compstmt
3819 if_tail
3820 {
3821 /*%%%*/
3822 $$ = new_if(p, $2, $4, $5, &@$);
3823 fixpos($$, $2);
3824 /*% %*/
3825 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3826 }
3827 ;
3828
3829opt_else : none
3830 | k_else compstmt
3831 {
3832 /*%%%*/
3833 $$ = $2;
3834 /*% %*/
3835 /*% ripper: else!($2) %*/
3836 }
3837 ;
3838
3839for_var : lhs
3840 | mlhs
3841 ;
3842
3843f_marg : f_norm_arg
3844 {
3845 /*%%%*/
3846 $$ = assignable(p, $1, 0, &@$);
3847 mark_lvar_used(p, $$);
3848 /*% %*/
3849 /*% ripper: assignable(p, $1) %*/
3850 }
3851 | tLPAREN f_margs rparen
3852 {
3853 /*%%%*/
3854 $$ = $2;
3855 /*% %*/
3856 /*% ripper: mlhs_paren!($2) %*/
3857 }
3858 ;
3859
3860f_marg_list : f_marg
3861 {
3862 /*%%%*/
3863 $$ = NEW_LIST($1, &@$);
3864 /*% %*/
3865 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3866 }
3867 | f_marg_list ',' f_marg
3868 {
3869 /*%%%*/
3870 $$ = list_append(p, $1, $3);
3871 /*% %*/
3872 /*% ripper: mlhs_add!($1, $3) %*/
3873 }
3874 ;
3875
3876f_margs : f_marg_list
3877 {
3878 /*%%%*/
3879 $$ = NEW_MASGN($1, 0, &@$);
3880 /*% %*/
3881 /*% ripper: $1 %*/
3882 }
3883 | f_marg_list ',' f_rest_marg
3884 {
3885 /*%%%*/
3886 $$ = NEW_MASGN($1, $3, &@$);
3887 /*% %*/
3888 /*% ripper: mlhs_add_star!($1, $3) %*/
3889 }
3890 | f_marg_list ',' f_rest_marg ',' f_marg_list
3891 {
3892 /*%%%*/
3893 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3894 /*% %*/
3895 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3896 }
3897 | f_rest_marg
3898 {
3899 /*%%%*/
3900 $$ = NEW_MASGN(0, $1, &@$);
3901 /*% %*/
3902 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3903 }
3904 | f_rest_marg ',' f_marg_list
3905 {
3906 /*%%%*/
3907 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3908 /*% %*/
3909 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3910 }
3911 ;
3912
3913f_rest_marg : tSTAR f_norm_arg
3914 {
3915 /*%%%*/
3916 $$ = assignable(p, $2, 0, &@$);
3917 mark_lvar_used(p, $$);
3918 /*% %*/
3919 /*% ripper: assignable(p, $2) %*/
3920 }
3921 | tSTAR
3922 {
3923 /*%%%*/
3924 $$ = NODE_SPECIAL_NO_NAME_REST;
3925 /*% %*/
3926 /*% ripper: Qnil %*/
3927 }
3928 ;
3929
3930f_any_kwrest : f_kwrest
3931 | f_no_kwarg {$$ = ID2VAL(idNil);}
3932 ;
3933
3934f_eq : {p->ctxt.in_argdef = 0;} '=';
3935
3936block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3937 {
3938 $$ = new_args_tail(p, $1, $3, $4, &@3);
3939 }
3940 | f_block_kwarg opt_f_block_arg
3941 {
3942 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3943 }
3944 | f_any_kwrest opt_f_block_arg
3945 {
3946 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3947 }
3948 | f_block_arg
3949 {
3950 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3951 }
3952 ;
3953
3954opt_block_args_tail : ',' block_args_tail
3955 {
3956 $$ = $2;
3957 }
3958 | /* none */
3959 {
3960 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3961 }
3962 ;
3963
3964excessed_comma : ','
3965 {
3966 /* magic number for rest_id in iseq_set_arguments() */
3967 /*%%%*/
3968 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3969 /*% %*/
3970 /*% ripper: excessed_comma! %*/
3971 }
3972 ;
3973
3974block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3975 {
3976 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3977 }
3978 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3979 {
3980 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3981 }
3982 | f_arg ',' f_block_optarg opt_block_args_tail
3983 {
3984 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3985 }
3986 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3987 {
3988 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3989 }
3990 | f_arg ',' f_rest_arg opt_block_args_tail
3991 {
3992 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3993 }
3994 | f_arg excessed_comma
3995 {
3996 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3997 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3998 }
3999 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4000 {
4001 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4002 }
4003 | f_arg opt_block_args_tail
4004 {
4005 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4006 }
4007 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4008 {
4009 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4010 }
4011 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4012 {
4013 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4014 }
4015 | f_block_optarg opt_block_args_tail
4016 {
4017 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4018 }
4019 | f_block_optarg ',' f_arg opt_block_args_tail
4020 {
4021 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4022 }
4023 | f_rest_arg opt_block_args_tail
4024 {
4025 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4026 }
4027 | f_rest_arg ',' f_arg opt_block_args_tail
4028 {
4029 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4030 }
4031 | block_args_tail
4032 {
4033 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4034 }
4035 ;
4036
4037opt_block_param : none
4038 | block_param_def
4039 {
4040 p->command_start = TRUE;
4041 }
4042 ;
4043
4044block_param_def : '|' opt_bv_decl '|'
4045 {
4046 p->cur_arg = 0;
4047 p->max_numparam = ORDINAL_PARAM;
4048 p->ctxt.in_argdef = 0;
4049 /*%%%*/
4050 $$ = 0;
4051 /*% %*/
4052 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
4053 }
4054 | '|' block_param opt_bv_decl '|'
4055 {
4056 p->cur_arg = 0;
4057 p->max_numparam = ORDINAL_PARAM;
4058 p->ctxt.in_argdef = 0;
4059 /*%%%*/
4060 $$ = $2;
4061 /*% %*/
4062 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
4063 }
4064 ;
4065
4066
4067opt_bv_decl : opt_nl
4068 {
4069 $$ = 0;
4070 }
4071 | opt_nl ';' bv_decls opt_nl
4072 {
4073 /*%%%*/
4074 $$ = 0;
4075 /*% %*/
4076 /*% ripper: $3 %*/
4077 }
4078 ;
4079
4080bv_decls : bvar
4081 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4082 | bv_decls ',' bvar
4083 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4084 ;
4085
4086bvar : tIDENTIFIER
4087 {
4088 new_bv(p, get_id($1));
4089 /*% ripper: get_value($1) %*/
4090 }
4091 | f_bad_arg
4092 {
4093 $$ = 0;
4094 }
4095 ;
4096
4097lambda : tLAMBDA
4098 {
4099 token_info_push(p, "->", &@1);
4100 $<vars>1 = dyna_push(p);
4101 $<num>$ = p->lex.lpar_beg;
4102 p->lex.lpar_beg = p->lex.paren_nest;
4103 }
4104 {
4105 $<num>$ = p->max_numparam;
4106 p->max_numparam = 0;
4107 }
4108 {
4109 $<node>$ = numparam_push(p);
4110 }
4111 f_larglist
4112 {
4113 CMDARG_PUSH(0);
4114 }
4115 lambda_body
4116 {
4117 int max_numparam = p->max_numparam;
4118 p->lex.lpar_beg = $<num>2;
4119 p->max_numparam = $<num>3;
4120 CMDARG_POP();
4121 $5 = args_with_numbered(p, $5, max_numparam);
4122 /*%%%*/
4123 {
4124 YYLTYPE loc = code_loc_gen(&@5, &@7);
4125 $$ = NEW_LAMBDA($5, $7, &loc);
4126 nd_set_line($$->nd_body, @7.end_pos.lineno);
4127 nd_set_line($$, @5.end_pos.lineno);
4128 nd_set_first_loc($$, @1.beg_pos);
4129 }
4130 /*% %*/
4131 /*% ripper: lambda!($5, $7) %*/
4132 numparam_pop(p, $<node>4);
4133 dyna_pop(p, $<vars>1);
4134 }
4135 ;
4136
4137f_larglist : '(' f_args opt_bv_decl ')'
4138 {
4139 p->ctxt.in_argdef = 0;
4140 /*%%%*/
4141 $$ = $2;
4142 p->max_numparam = ORDINAL_PARAM;
4143 /*% %*/
4144 /*% ripper: paren!($2) %*/
4145 }
4146 | f_args
4147 {
4148 p->ctxt.in_argdef = 0;
4149 /*%%%*/
4150 if (!args_info_empty_p($1->nd_ainfo))
4151 p->max_numparam = ORDINAL_PARAM;
4152 /*% %*/
4153 $$ = $1;
4154 }
4155 ;
4156
4157lambda_body : tLAMBEG compstmt '}'
4158 {
4159 token_info_pop(p, "}", &@3);
4160 $$ = $2;
4161 }
4162 | keyword_do_LAMBDA
4163 {
4164 /*%%%*/
4165 push_end_expect_token_locations(p, &@1.beg_pos);
4166 /*% %*/
4167 }
4168 bodystmt k_end
4169 {
4170 $$ = $3;
4171 }
4172 ;
4173
4174do_block : k_do_block do_body k_end
4175 {
4176 $$ = $2;
4177 /*%%%*/
4178 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4179 nd_set_line($$, @1.end_pos.lineno);
4180 /*% %*/
4181 }
4182 ;
4183
4184block_call : command do_block
4185 {
4186 /*%%%*/
4187 if (nd_type_p($1, NODE_YIELD)) {
4188 compile_error(p, "block given to yield");
4189 }
4190 else {
4191 block_dup_check(p, $1->nd_args, $2);
4192 }
4193 $$ = method_add_block(p, $1, $2, &@$);
4194 fixpos($$, $1);
4195 /*% %*/
4196 /*% ripper: method_add_block!($1, $2) %*/
4197 }
4198 | block_call call_op2 operation2 opt_paren_args
4199 {
4200 /*%%%*/
4201 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4202 /*% %*/
4203 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4204 }
4205 | block_call call_op2 operation2 opt_paren_args brace_block
4206 {
4207 /*%%%*/
4208 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4209 /*% %*/
4210 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4211 }
4212 | block_call call_op2 operation2 command_args do_block
4213 {
4214 /*%%%*/
4215 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4216 /*% %*/
4217 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4218 }
4219 ;
4220
4221method_call : fcall paren_args
4222 {
4223 /*%%%*/
4224 $$ = $1;
4225 $$->nd_args = $2;
4226 nd_set_last_loc($1, @2.end_pos);
4227 /*% %*/
4228 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4229 }
4230 | primary_value call_op operation2 opt_paren_args
4231 {
4232 /*%%%*/
4233 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4234 nd_set_line($$, @3.end_pos.lineno);
4235 /*% %*/
4236 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4237 }
4238 | primary_value tCOLON2 operation2 paren_args
4239 {
4240 /*%%%*/
4241 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4242 nd_set_line($$, @3.end_pos.lineno);
4243 /*% %*/
4244 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
4245 }
4246 | primary_value tCOLON2 operation3
4247 {
4248 /*%%%*/
4249 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4250 /*% %*/
4251 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
4252 }
4253 | primary_value call_op paren_args
4254 {
4255 /*%%%*/
4256 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4257 nd_set_line($$, @2.end_pos.lineno);
4258 /*% %*/
4259 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4260 }
4261 | primary_value tCOLON2 paren_args
4262 {
4263 /*%%%*/
4264 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4265 nd_set_line($$, @2.end_pos.lineno);
4266 /*% %*/
4267 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
4268 }
4269 | keyword_super paren_args
4270 {
4271 /*%%%*/
4272 $$ = NEW_SUPER($2, &@$);
4273 /*% %*/
4274 /*% ripper: super!($2) %*/
4275 }
4276 | keyword_super
4277 {
4278 /*%%%*/
4279 $$ = NEW_ZSUPER(&@$);
4280 /*% %*/
4281 /*% ripper: zsuper! %*/
4282 }
4283 | primary_value '[' opt_call_args rbracket
4284 {
4285 /*%%%*/
4286 if ($1 && nd_type_p($1, NODE_SELF))
4287 $$ = NEW_FCALL(tAREF, $3, &@$);
4288 else
4289 $$ = NEW_CALL($1, tAREF, $3, &@$);
4290 fixpos($$, $1);
4291 /*% %*/
4292 /*% ripper: aref!($1, escape_Qundef($3)) %*/
4293 }
4294 ;
4295
4296brace_block : '{' brace_body '}'
4297 {
4298 $$ = $2;
4299 /*%%%*/
4300 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4301 nd_set_line($$, @1.end_pos.lineno);
4302 /*% %*/
4303 }
4304 | k_do do_body k_end
4305 {
4306 $$ = $2;
4307 /*%%%*/
4308 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4309 nd_set_line($$, @1.end_pos.lineno);
4310 /*% %*/
4311 }
4312 ;
4313
4314brace_body : {$<vars>$ = dyna_push(p);}
4315 {
4316 $<num>$ = p->max_numparam;
4317 p->max_numparam = 0;
4318 }
4319 {
4320 $<node>$ = numparam_push(p);
4321 }
4322 opt_block_param compstmt
4323 {
4324 int max_numparam = p->max_numparam;
4325 p->max_numparam = $<num>2;
4326 $4 = args_with_numbered(p, $4, max_numparam);
4327 /*%%%*/
4328 $$ = NEW_ITER($4, $5, &@$);
4329 /*% %*/
4330 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
4331 numparam_pop(p, $<node>3);
4332 dyna_pop(p, $<vars>1);
4333 }
4334 ;
4335
4336do_body : {$<vars>$ = dyna_push(p);}
4337 {
4338 $<num>$ = p->max_numparam;
4339 p->max_numparam = 0;
4340 }
4341 {
4342 $<node>$ = numparam_push(p);
4343 CMDARG_PUSH(0);
4344 }
4345 opt_block_param bodystmt
4346 {
4347 int max_numparam = p->max_numparam;
4348 p->max_numparam = $<num>2;
4349 $4 = args_with_numbered(p, $4, max_numparam);
4350 /*%%%*/
4351 $$ = NEW_ITER($4, $5, &@$);
4352 /*% %*/
4353 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4354 CMDARG_POP();
4355 numparam_pop(p, $<node>3);
4356 dyna_pop(p, $<vars>1);
4357 }
4358 ;
4359
4360case_args : arg_value
4361 {
4362 /*%%%*/
4363 check_literal_when(p, $1, &@1);
4364 $$ = NEW_LIST($1, &@$);
4365 /*% %*/
4366 /*% ripper: args_add!(args_new!, $1) %*/
4367 }
4368 | tSTAR arg_value
4369 {
4370 /*%%%*/
4371 $$ = NEW_SPLAT($2, &@$);
4372 /*% %*/
4373 /*% ripper: args_add_star!(args_new!, $2) %*/
4374 }
4375 | case_args ',' arg_value
4376 {
4377 /*%%%*/
4378 check_literal_when(p, $3, &@3);
4379 $$ = last_arg_append(p, $1, $3, &@$);
4380 /*% %*/
4381 /*% ripper: args_add!($1, $3) %*/
4382 }
4383 | case_args ',' tSTAR arg_value
4384 {
4385 /*%%%*/
4386 $$ = rest_arg_append(p, $1, $4, &@$);
4387 /*% %*/
4388 /*% ripper: args_add_star!($1, $4) %*/
4389 }
4390 ;
4391
4392case_body : k_when case_args then
4393 compstmt
4394 cases
4395 {
4396 /*%%%*/
4397 $$ = NEW_WHEN($2, $4, $5, &@$);
4398 fixpos($$, $2);
4399 /*% %*/
4400 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4401 }
4402 ;
4403
4404cases : opt_else
4405 | case_body
4406 ;
4407
4408p_case_body : keyword_in
4409 {
4410 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4411 p->command_start = FALSE;
4412 $<ctxt>1 = p->ctxt;
4413 p->ctxt.in_kwarg = 1;
4414 $<tbl>$ = push_pvtbl(p);
4415 }
4416 {
4417 $<tbl>$ = push_pktbl(p);
4418 }
4419 p_top_expr then
4420 {
4421 pop_pktbl(p, $<tbl>3);
4422 pop_pvtbl(p, $<tbl>2);
4423 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4424 }
4425 compstmt
4426 p_cases
4427 {
4428 /*%%%*/
4429 $$ = NEW_IN($4, $7, $8, &@$);
4430 /*% %*/
4431 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4432 }
4433 ;
4434
4435p_cases : opt_else
4436 | p_case_body
4437 ;
4438
4439p_top_expr : p_top_expr_body
4440 | p_top_expr_body modifier_if expr_value
4441 {
4442 /*%%%*/
4443 $$ = new_if(p, $3, $1, 0, &@$);
4444 fixpos($$, $3);
4445 /*% %*/
4446 /*% ripper: if_mod!($3, $1) %*/
4447 }
4448 | p_top_expr_body modifier_unless expr_value
4449 {
4450 /*%%%*/
4451 $$ = new_unless(p, $3, $1, 0, &@$);
4452 fixpos($$, $3);
4453 /*% %*/
4454 /*% ripper: unless_mod!($3, $1) %*/
4455 }
4456 ;
4457
4458p_top_expr_body : p_expr
4459 | p_expr ','
4460 {
4461 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4462 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4463 }
4464 | p_expr ',' p_args
4465 {
4466 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4467 /*%%%*/
4468 nd_set_first_loc($$, @1.beg_pos);
4469 /*%
4470 %*/
4471 }
4472 | p_find
4473 {
4474 $$ = new_find_pattern(p, Qnone, $1, &@$);
4475 }
4476 | p_args_tail
4477 {
4478 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4479 }
4480 | p_kwargs
4481 {
4482 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4483 }
4484 ;
4485
4486p_expr : p_as
4487 ;
4488
4489p_as : p_expr tASSOC p_variable
4490 {
4491 /*%%%*/
4492 NODE *n = NEW_LIST($1, &@$);
4493 n = list_append(p, n, $3);
4494 $$ = new_hash(p, n, &@$);
4495 /*% %*/
4496 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4497 }
4498 | p_alt
4499 ;
4500
4501p_alt : p_alt '|' p_expr_basic
4502 {
4503 /*%%%*/
4504 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4505 /*% %*/
4506 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4507 }
4508 | p_expr_basic
4509 ;
4510
4511p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4512p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4513
4514p_expr_basic : p_value
4515 | p_variable
4516 | p_const p_lparen p_args rparen
4517 {
4518 pop_pktbl(p, $<tbl>2);
4519 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4520 /*%%%*/
4521 nd_set_first_loc($$, @1.beg_pos);
4522 /*%
4523 %*/
4524 }
4525 | p_const p_lparen p_find rparen
4526 {
4527 pop_pktbl(p, $<tbl>2);
4528 $$ = new_find_pattern(p, $1, $3, &@$);
4529 /*%%%*/
4530 nd_set_first_loc($$, @1.beg_pos);
4531 /*%
4532 %*/
4533 }
4534 | p_const p_lparen p_kwargs rparen
4535 {
4536 pop_pktbl(p, $<tbl>2);
4537 $$ = new_hash_pattern(p, $1, $3, &@$);
4538 /*%%%*/
4539 nd_set_first_loc($$, @1.beg_pos);
4540 /*%
4541 %*/
4542 }
4543 | p_const '(' rparen
4544 {
4545 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4546 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4547 }
4548 | p_const p_lbracket p_args rbracket
4549 {
4550 pop_pktbl(p, $<tbl>2);
4551 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4552 /*%%%*/
4553 nd_set_first_loc($$, @1.beg_pos);
4554 /*%
4555 %*/
4556 }
4557 | p_const p_lbracket p_find rbracket
4558 {
4559 pop_pktbl(p, $<tbl>2);
4560 $$ = new_find_pattern(p, $1, $3, &@$);
4561 /*%%%*/
4562 nd_set_first_loc($$, @1.beg_pos);
4563 /*%
4564 %*/
4565 }
4566 | p_const p_lbracket p_kwargs rbracket
4567 {
4568 pop_pktbl(p, $<tbl>2);
4569 $$ = new_hash_pattern(p, $1, $3, &@$);
4570 /*%%%*/
4571 nd_set_first_loc($$, @1.beg_pos);
4572 /*%
4573 %*/
4574 }
4575 | p_const '[' rbracket
4576 {
4577 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4578 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4579 }
4580 | tLBRACK p_args rbracket
4581 {
4582 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4583 }
4584 | tLBRACK p_find rbracket
4585 {
4586 $$ = new_find_pattern(p, Qnone, $2, &@$);
4587 }
4588 | tLBRACK rbracket
4589 {
4590 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4591 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4592 }
4593 | tLBRACE
4594 {
4595 $<tbl>$ = push_pktbl(p);
4596 $<ctxt>1 = p->ctxt;
4597 p->ctxt.in_kwarg = 0;
4598 }
4599 p_kwargs rbrace
4600 {
4601 pop_pktbl(p, $<tbl>2);
4602 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4603 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4604 }
4605 | tLBRACE rbrace
4606 {
4607 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4608 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4609 }
4610 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4611 {
4612 pop_pktbl(p, $<tbl>2);
4613 $$ = $3;
4614 }
4615 ;
4616
4617p_args : p_expr
4618 {
4619 /*%%%*/
4620 NODE *pre_args = NEW_LIST($1, &@$);
4621 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4622 /*%
4623 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4624 %*/
4625 }
4626 | p_args_head
4627 {
4628 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4629 }
4630 | p_args_head p_arg
4631 {
4632 /*%%%*/
4633 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4634 /*%
4635 VALUE pre_args = rb_ary_concat($1, get_value($2));
4636 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4637 %*/
4638 }
4639 | p_args_head p_rest
4640 {
4641 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
4642 }
4643 | p_args_head p_rest ',' p_args_post
4644 {
4645 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
4646 }
4647 | p_args_tail
4648 ;
4649
4650p_args_head : p_arg ','
4651 {
4652 $$ = $1;
4653 }
4654 | p_args_head p_arg ','
4655 {
4656 /*%%%*/
4657 $$ = list_concat($1, $2);
4658 /*% %*/
4659 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4660 }
4661 ;
4662
4663p_args_tail : p_rest
4664 {
4665 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4666 }
4667 | p_rest ',' p_args_post
4668 {
4669 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4670 }
4671 ;
4672
4673p_find : p_rest ',' p_args_post ',' p_rest
4674 {
4675 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4676 }
4677 ;
4678
4679
4680p_rest : tSTAR tIDENTIFIER
4681 {
4682 $$ = $2;
4683 }
4684 | tSTAR
4685 {
4686 $$ = 0;
4687 }
4688 ;
4689
4690p_args_post : p_arg
4691 | p_args_post ',' p_arg
4692 {
4693 /*%%%*/
4694 $$ = list_concat($1, $3);
4695 /*% %*/
4696 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4697 }
4698 ;
4699
4700p_arg : p_expr
4701 {
4702 /*%%%*/
4703 $$ = NEW_LIST($1, &@$);
4704 /*% %*/
4705 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4706 }
4707 ;
4708
4709p_kwargs : p_kwarg ',' p_any_kwrest
4710 {
4711 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4712 }
4713 | p_kwarg
4714 {
4715 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4716 }
4717 | p_kwarg ','
4718 {
4719 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4720 }
4721 | p_any_kwrest
4722 {
4723 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4724 }
4725 ;
4726
4727p_kwarg : p_kw
4728 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4729 | p_kwarg ',' p_kw
4730 {
4731 /*%%%*/
4732 $$ = list_concat($1, $3);
4733 /*% %*/
4734 /*% ripper: rb_ary_push($1, $3) %*/
4735 }
4736 ;
4737
4738p_kw : p_kw_label p_expr
4739 {
4740 error_duplicate_pattern_key(p, get_id($1), &@1);
4741 /*%%%*/
4742 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
4743 /*% %*/
4744 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4745 }
4746 | p_kw_label
4747 {
4748 error_duplicate_pattern_key(p, get_id($1), &@1);
4749 if ($1 && !is_local_id(get_id($1))) {
4750 yyerror1(&@1, "key must be valid as local variables");
4751 }
4752 error_duplicate_pattern_variable(p, get_id($1), &@1);
4753 /*%%%*/
4754 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4755 /*% %*/
4756 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4757 }
4758 ;
4759
4760p_kw_label : tLABEL
4761 | tSTRING_BEG string_contents tLABEL_END
4762 {
4763 YYLTYPE loc = code_loc_gen(&@1, &@3);
4764 /*%%%*/
4765 if (!$2 || nd_type_p($2, NODE_STR)) {
4766 NODE *node = dsym_node(p, $2, &loc);
4767 $$ = SYM2ID(node->nd_lit);
4768 }
4769 /*%
4770 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4771 VALUE label = RNODE($2)->nd_cval;
4772 VALUE rval = RNODE($2)->nd_rval;
4773 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4774 RNODE($$)->nd_loc = loc;
4775 }
4776 %*/
4777 else {
4778 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4779 $$ = 0;
4780 }
4781 }
4782 ;
4783
4784p_kwrest : kwrest_mark tIDENTIFIER
4785 {
4786 $$ = $2;
4787 }
4788 | kwrest_mark
4789 {
4790 $$ = 0;
4791 }
4792 ;
4793
4794p_kwnorest : kwrest_mark keyword_nil
4795 {
4796 $$ = 0;
4797 }
4798 ;
4799
4800p_any_kwrest : p_kwrest
4801 | p_kwnorest {$$ = ID2VAL(idNil);}
4802 ;
4803
4804p_value : p_primitive
4805 | p_primitive tDOT2 p_primitive
4806 {
4807 /*%%%*/
4808 value_expr($1);
4809 value_expr($3);
4810 $$ = NEW_DOT2($1, $3, &@$);
4811 /*% %*/
4812 /*% ripper: dot2!($1, $3) %*/
4813 }
4814 | p_primitive tDOT3 p_primitive
4815 {
4816 /*%%%*/
4817 value_expr($1);
4818 value_expr($3);
4819 $$ = NEW_DOT3($1, $3, &@$);
4820 /*% %*/
4821 /*% ripper: dot3!($1, $3) %*/
4822 }
4823 | p_primitive tDOT2
4824 {
4825 /*%%%*/
4826 value_expr($1);
4827 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4828 /*% %*/
4829 /*% ripper: dot2!($1, Qnil) %*/
4830 }
4831 | p_primitive tDOT3
4832 {
4833 /*%%%*/
4834 value_expr($1);
4835 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4836 /*% %*/
4837 /*% ripper: dot3!($1, Qnil) %*/
4838 }
4839 | p_var_ref
4840 | p_expr_ref
4841 | p_const
4842 | tBDOT2 p_primitive
4843 {
4844 /*%%%*/
4845 value_expr($2);
4846 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4847 /*% %*/
4848 /*% ripper: dot2!(Qnil, $2) %*/
4849 }
4850 | tBDOT3 p_primitive
4851 {
4852 /*%%%*/
4853 value_expr($2);
4854 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4855 /*% %*/
4856 /*% ripper: dot3!(Qnil, $2) %*/
4857 }
4858 ;
4859
4860p_primitive : literal
4861 | strings
4862 | xstring
4863 | regexp
4864 | words
4865 | qwords
4866 | symbols
4867 | qsymbols
4868 | keyword_variable
4869 {
4870 /*%%%*/
4871 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4872 /*% %*/
4873 /*% ripper: var_ref!($1) %*/
4874 }
4875 | lambda
4876 ;
4877
4878p_variable : tIDENTIFIER
4879 {
4880 /*%%%*/
4881 error_duplicate_pattern_variable(p, $1, &@1);
4882 $$ = assignable(p, $1, 0, &@$);
4883 /*% %*/
4884 /*% ripper: assignable(p, var_field(p, $1)) %*/
4885 }
4886 ;
4887
4888p_var_ref : '^' tIDENTIFIER
4889 {
4890 /*%%%*/
4891 NODE *n = gettable(p, $2, &@$);
4892 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4893 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4894 }
4895 $$ = n;
4896 /*% %*/
4897 /*% ripper: var_ref!($2) %*/
4898 }
4899 | '^' nonlocal_var
4900 {
4901 /*%%%*/
4902 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4903 /*% %*/
4904 /*% ripper: var_ref!($2) %*/
4905 }
4906 ;
4907
4908p_expr_ref : '^' tLPAREN expr_value rparen
4909 {
4910 /*%%%*/
4911 $$ = NEW_BEGIN($3, &@$);
4912 /*% %*/
4913 /*% ripper: begin!($3) %*/
4914 }
4915 ;
4916
4917p_const : tCOLON3 cname
4918 {
4919 /*%%%*/
4920 $$ = NEW_COLON3($2, &@$);
4921 /*% %*/
4922 /*% ripper: top_const_ref!($2) %*/
4923 }
4924 | p_const tCOLON2 cname
4925 {
4926 /*%%%*/
4927 $$ = NEW_COLON2($1, $3, &@$);
4928 /*% %*/
4929 /*% ripper: const_path_ref!($1, $3) %*/
4930 }
4931 | tCONSTANT
4932 {
4933 /*%%%*/
4934 $$ = gettable(p, $1, &@$);
4935 /*% %*/
4936 /*% ripper: var_ref!($1) %*/
4937 }
4938 ;
4939
4940opt_rescue : k_rescue exc_list exc_var then
4941 compstmt
4942 opt_rescue
4943 {
4944 /*%%%*/
4945 $$ = NEW_RESBODY($2,
4946 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4947 $6, &@$);
4948
4949 if ($2) {
4950 fixpos($$, $2);
4951 }
4952 else if ($3) {
4953 fixpos($$, $3);
4954 }
4955 else {
4956 fixpos($$, $5);
4957 }
4958 /*% %*/
4959 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4960 }
4961 | none
4962 ;
4963
4964exc_list : arg_value
4965 {
4966 /*%%%*/
4967 $$ = NEW_LIST($1, &@$);
4968 /*% %*/
4969 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4970 }
4971 | mrhs
4972 {
4973 /*%%%*/
4974 if (!($$ = splat_array($1))) $$ = $1;
4975 /*% %*/
4976 /*% ripper: $1 %*/
4977 }
4978 | none
4979 ;
4980
4981exc_var : tASSOC lhs
4982 {
4983 $$ = $2;
4984 }
4985 | none
4986 ;
4987
4988opt_ensure : k_ensure compstmt
4989 {
4990 /*%%%*/
4991 $$ = $2;
4992 /*% %*/
4993 /*% ripper: ensure!($2) %*/
4994 }
4995 | none
4996 ;
4997
4998literal : numeric
4999 | symbol
5000 ;
5001
5002strings : string
5003 {
5004 /*%%%*/
5005 NODE *node = $1;
5006 if (!node) {
5007 node = NEW_STR(STR_NEW0(), &@$);
5008 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
5009 }
5010 else {
5011 node = evstr2dstr(p, node);
5012 }
5013 $$ = node;
5014 /*% %*/
5015 /*% ripper: $1 %*/
5016 }
5017 ;
5018
5019string : tCHAR
5020 | string1
5021 | string string1
5022 {
5023 /*%%%*/
5024 $$ = literal_concat(p, $1, $2, &@$);
5025 /*% %*/
5026 /*% ripper: string_concat!($1, $2) %*/
5027 }
5028 ;
5029
5030string1 : tSTRING_BEG string_contents tSTRING_END
5031 {
5032 /*%%%*/
5033 $$ = heredoc_dedent(p, $2);
5034 if ($$) nd_set_loc($$, &@$);
5035 /*% %*/
5036 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5037 }
5038 ;
5039
5040xstring : tXSTRING_BEG xstring_contents tSTRING_END
5041 {
5042 /*%%%*/
5043 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5044 /*% %*/
5045 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5046 }
5047 ;
5048
5049regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5050 {
5051 $$ = new_regexp(p, $2, $3, &@$);
5052 }
5053 ;
5054
5055words : tWORDS_BEG ' ' word_list tSTRING_END
5056 {
5057 /*%%%*/
5058 $$ = make_list($3, &@$);
5059 /*% %*/
5060 /*% ripper: array!($3) %*/
5061 }
5062 ;
5063
5064word_list : /* none */
5065 {
5066 /*%%%*/
5067 $$ = 0;
5068 /*% %*/
5069 /*% ripper: words_new! %*/
5070 }
5071 | word_list word ' '
5072 {
5073 /*%%%*/
5074 $$ = list_append(p, $1, evstr2dstr(p, $2));
5075 /*% %*/
5076 /*% ripper: words_add!($1, $2) %*/
5077 }
5078 ;
5079
5080word : string_content
5081 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5082 | word string_content
5083 {
5084 /*%%%*/
5085 $$ = literal_concat(p, $1, $2, &@$);
5086 /*% %*/
5087 /*% ripper: word_add!($1, $2) %*/
5088 }
5089 ;
5090
5091symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
5092 {
5093 /*%%%*/
5094 $$ = make_list($3, &@$);
5095 /*% %*/
5096 /*% ripper: array!($3) %*/
5097 }
5098 ;
5099
5100symbol_list : /* none */
5101 {
5102 /*%%%*/
5103 $$ = 0;
5104 /*% %*/
5105 /*% ripper: symbols_new! %*/
5106 }
5107 | symbol_list word ' '
5108 {
5109 /*%%%*/
5110 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5111 /*% %*/
5112 /*% ripper: symbols_add!($1, $2) %*/
5113 }
5114 ;
5115
5116qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
5117 {
5118 /*%%%*/
5119 $$ = make_list($3, &@$);
5120 /*% %*/
5121 /*% ripper: array!($3) %*/
5122 }
5123 ;
5124
5125qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
5126 {
5127 /*%%%*/
5128 $$ = make_list($3, &@$);
5129 /*% %*/
5130 /*% ripper: array!($3) %*/
5131 }
5132 ;
5133
5134qword_list : /* none */
5135 {
5136 /*%%%*/
5137 $$ = 0;
5138 /*% %*/
5139 /*% ripper: qwords_new! %*/
5140 }
5141 | qword_list tSTRING_CONTENT ' '
5142 {
5143 /*%%%*/
5144 $$ = list_append(p, $1, $2);
5145 /*% %*/
5146 /*% ripper: qwords_add!($1, $2) %*/
5147 }
5148 ;
5149
5150qsym_list : /* none */
5151 {
5152 /*%%%*/
5153 $$ = 0;
5154 /*% %*/
5155 /*% ripper: qsymbols_new! %*/
5156 }
5157 | qsym_list tSTRING_CONTENT ' '
5158 {
5159 /*%%%*/
5160 $$ = symbol_append(p, $1, $2);
5161 /*% %*/
5162 /*% ripper: qsymbols_add!($1, $2) %*/
5163 }
5164 ;
5165
5166string_contents : /* none */
5167 {
5168 /*%%%*/
5169 $$ = 0;
5170 /*% %*/
5171 /*% ripper: string_content! %*/
5172 /*%%%*/
5173 /*%
5174 $$ = ripper_new_yylval(p, 0, $$, 0);
5175 %*/
5176 }
5177 | string_contents string_content
5178 {
5179 /*%%%*/
5180 $$ = literal_concat(p, $1, $2, &@$);
5181 /*% %*/
5182 /*% ripper: string_add!($1, $2) %*/
5183 /*%%%*/
5184 /*%
5185 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
5186 !RNODE($1)->nd_cval) {
5187 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
5188 RNODE($1)->nd_rval = add_mark_object(p, $$);
5189 $$ = $1;
5190 }
5191 %*/
5192 }
5193 ;
5194
5195xstring_contents: /* none */
5196 {
5197 /*%%%*/
5198 $$ = 0;
5199 /*% %*/
5200 /*% ripper: xstring_new! %*/
5201 }
5202 | xstring_contents string_content
5203 {
5204 /*%%%*/
5205 $$ = literal_concat(p, $1, $2, &@$);
5206 /*% %*/
5207 /*% ripper: xstring_add!($1, $2) %*/
5208 }
5209 ;
5210
5211regexp_contents: /* none */
5212 {
5213 /*%%%*/
5214 $$ = 0;
5215 /*% %*/
5216 /*% ripper: regexp_new! %*/
5217 /*%%%*/
5218 /*%
5219 $$ = ripper_new_yylval(p, 0, $$, 0);
5220 %*/
5221 }
5222 | regexp_contents string_content
5223 {
5224 /*%%%*/
5225 NODE *head = $1, *tail = $2;
5226 if (!head) {
5227 $$ = tail;
5228 }
5229 else if (!tail) {
5230 $$ = head;
5231 }
5232 else {
5233 switch (nd_type(head)) {
5234 case NODE_STR:
5235 nd_set_type(head, NODE_DSTR);
5236 break;
5237 case NODE_DSTR:
5238 break;
5239 default:
5240 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5241 break;
5242 }
5243 $$ = list_append(p, head, tail);
5244 }
5245 /*%
5246 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5247 if (ripper_is_node_yylval(n1)) {
5248 s1 = RNODE(n1)->nd_cval;
5249 n1 = RNODE(n1)->nd_rval;
5250 }
5251 if (ripper_is_node_yylval(n2)) {
5252 s2 = RNODE(n2)->nd_cval;
5253 n2 = RNODE(n2)->nd_rval;
5254 }
5255 $$ = dispatch2(regexp_add, n1, n2);
5256 if (!s1 && s2) {
5257 $$ = ripper_new_yylval(p, 0, $$, s2);
5258 }
5259 %*/
5260 }
5261 ;
5262
5263string_content : tSTRING_CONTENT
5264 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5265 | tSTRING_DVAR
5266 {
5267 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5268 $<strterm>$ = p->lex.strterm;
5269 p->lex.strterm = 0;
5270 SET_LEX_STATE(EXPR_BEG);
5271 }
5272 string_dvar
5273 {
5274 p->lex.strterm = $<strterm>2;
5275 /*%%%*/
5276 $$ = NEW_EVSTR($3, &@$);
5277 nd_set_line($$, @3.end_pos.lineno);
5278 /*% %*/
5279 /*% ripper: string_dvar!($3) %*/
5280 }
5281 | tSTRING_DBEG
5282 {
5283 CMDARG_PUSH(0);
5284 COND_PUSH(0);
5285 }
5286 {
5287 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5288 $<strterm>$ = p->lex.strterm;
5289 p->lex.strterm = 0;
5290 }
5291 {
5292 $<num>$ = p->lex.state;
5293 SET_LEX_STATE(EXPR_BEG);
5294 }
5295 {
5296 $<num>$ = p->lex.brace_nest;
5297 p->lex.brace_nest = 0;
5298 }
5299 {
5300 $<num>$ = p->heredoc_indent;
5301 p->heredoc_indent = 0;
5302 }
5303 compstmt tSTRING_DEND
5304 {
5305 COND_POP();
5306 CMDARG_POP();
5307 p->lex.strterm = $<strterm>3;
5308 SET_LEX_STATE($<num>4);
5309 p->lex.brace_nest = $<num>5;
5310 p->heredoc_indent = $<num>6;
5311 p->heredoc_line_indent = -1;
5312 /*%%%*/
5313 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
5314 $$ = new_evstr(p, $7, &@$);
5315 /*% %*/
5316 /*% ripper: string_embexpr!($7) %*/
5317 }
5318 ;
5319
5320string_dvar : tGVAR
5321 {
5322 /*%%%*/
5323 $$ = NEW_GVAR($1, &@$);
5324 /*% %*/
5325 /*% ripper: var_ref!($1) %*/
5326 }
5327 | tIVAR
5328 {
5329 /*%%%*/
5330 $$ = NEW_IVAR($1, &@$);
5331 /*% %*/
5332 /*% ripper: var_ref!($1) %*/
5333 }
5334 | tCVAR
5335 {
5336 /*%%%*/
5337 $$ = NEW_CVAR($1, &@$);
5338 /*% %*/
5339 /*% ripper: var_ref!($1) %*/
5340 }
5341 | backref
5342 ;
5343
5344symbol : ssym
5345 | dsym
5346 ;
5347
5348ssym : tSYMBEG sym
5349 {
5350 SET_LEX_STATE(EXPR_END);
5351 /*%%%*/
5352 $$ = NEW_LIT(ID2SYM($2), &@$);
5353 /*% %*/
5354 /*% ripper: symbol_literal!(symbol!($2)) %*/
5355 }
5356 ;
5357
5358sym : fname
5359 | nonlocal_var
5360 ;
5361
5362dsym : tSYMBEG string_contents tSTRING_END
5363 {
5364 SET_LEX_STATE(EXPR_END);
5365 /*%%%*/
5366 $$ = dsym_node(p, $2, &@$);
5367 /*% %*/
5368 /*% ripper: dyna_symbol!($2) %*/
5369 }
5370 ;
5371
5372numeric : simple_numeric
5373 | tUMINUS_NUM simple_numeric %prec tLOWEST
5374 {
5375 /*%%%*/
5376 $$ = $2;
5377 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5378 /*% %*/
5379 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5380 }
5381 ;
5382
5383simple_numeric : tINTEGER
5384 | tFLOAT
5385 | tRATIONAL
5386 | tIMAGINARY
5387 ;
5388
5389nonlocal_var : tIVAR
5390 | tGVAR
5391 | tCVAR
5392 ;
5393
5394user_variable : tIDENTIFIER
5395 | tCONSTANT
5396 | nonlocal_var
5397 ;
5398
5399keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5400 | keyword_self {$$ = KWD2EID(self, $1);}
5401 | keyword_true {$$ = KWD2EID(true, $1);}
5402 | keyword_false {$$ = KWD2EID(false, $1);}
5403 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5404 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5405 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5406 ;
5407
5408var_ref : user_variable
5409 {
5410 /*%%%*/
5411 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5412 /*%
5413 if (id_is_var(p, get_id($1))) {
5414 $$ = dispatch1(var_ref, $1);
5415 }
5416 else {
5417 $$ = dispatch1(vcall, $1);
5418 }
5419 %*/
5420 }
5421 | keyword_variable
5422 {
5423 /*%%%*/
5424 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5425 /*% %*/
5426 /*% ripper: var_ref!($1) %*/
5427 }
5428 ;
5429
5430var_lhs : user_variable
5431 {
5432 /*%%%*/
5433 $$ = assignable(p, $1, 0, &@$);
5434 /*% %*/
5435 /*% ripper: assignable(p, var_field(p, $1)) %*/
5436 }
5437 | keyword_variable
5438 {
5439 /*%%%*/
5440 $$ = assignable(p, $1, 0, &@$);
5441 /*% %*/
5442 /*% ripper: assignable(p, var_field(p, $1)) %*/
5443 }
5444 ;
5445
5446backref : tNTH_REF
5447 | tBACK_REF
5448 ;
5449
5450superclass : '<'
5451 {
5452 SET_LEX_STATE(EXPR_BEG);
5453 p->command_start = TRUE;
5454 }
5455 expr_value term
5456 {
5457 $$ = $3;
5458 }
5459 | /* none */
5460 {
5461 /*%%%*/
5462 $$ = 0;
5463 /*% %*/
5464 /*% ripper: Qnil %*/
5465 }
5466 ;
5467
5468f_opt_paren_args: f_paren_args
5469 | none
5470 {
5471 p->ctxt.in_argdef = 0;
5472 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5473 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5474 }
5475 ;
5476
5477f_paren_args : '(' f_args rparen
5478 {
5479 /*%%%*/
5480 $$ = $2;
5481 /*% %*/
5482 /*% ripper: paren!($2) %*/
5483 SET_LEX_STATE(EXPR_BEG);
5484 p->command_start = TRUE;
5485 p->ctxt.in_argdef = 0;
5486 }
5487 ;
5488
5489f_arglist : f_paren_args
5490 | {
5491 $<ctxt>$ = p->ctxt;
5492 p->ctxt.in_kwarg = 1;
5493 p->ctxt.in_argdef = 1;
5494 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5495 }
5496 f_args term
5497 {
5498 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5499 p->ctxt.in_argdef = 0;
5500 $$ = $2;
5501 SET_LEX_STATE(EXPR_BEG);
5502 p->command_start = TRUE;
5503 }
5504 ;
5505
5506args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5507 {
5508 $$ = new_args_tail(p, $1, $3, $4, &@3);
5509 }
5510 | f_kwarg opt_f_block_arg
5511 {
5512 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5513 }
5514 | f_any_kwrest opt_f_block_arg
5515 {
5516 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5517 }
5518 | f_block_arg
5519 {
5520 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5521 }
5522 | args_forward
5523 {
5524 add_forwarding_args(p);
5525 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5526 /*%%%*/
5527 ($$->nd_ainfo)->forwarding = 1;
5528 /*% %*/
5529 }
5530 ;
5531
5532opt_args_tail : ',' args_tail
5533 {
5534 $$ = $2;
5535 }
5536 | /* none */
5537 {
5538 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5539 }
5540 ;
5541
5542f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5543 {
5544 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5545 }
5546 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5547 {
5548 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5549 }
5550 | f_arg ',' f_optarg opt_args_tail
5551 {
5552 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5553 }
5554 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5555 {
5556 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5557 }
5558 | f_arg ',' f_rest_arg opt_args_tail
5559 {
5560 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5561 }
5562 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5563 {
5564 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5565 }
5566 | f_arg opt_args_tail
5567 {
5568 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5569 }
5570 | f_optarg ',' f_rest_arg opt_args_tail
5571 {
5572 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5573 }
5574 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5575 {
5576 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5577 }
5578 | f_optarg opt_args_tail
5579 {
5580 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5581 }
5582 | f_optarg ',' f_arg opt_args_tail
5583 {
5584 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5585 }
5586 | f_rest_arg opt_args_tail
5587 {
5588 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5589 }
5590 | f_rest_arg ',' f_arg opt_args_tail
5591 {
5592 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5593 }
5594 | args_tail
5595 {
5596 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5597 }
5598 | /* none */
5599 {
5600 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5601 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5602 }
5603 ;
5604
5605args_forward : tBDOT3
5606 {
5607 /*%%%*/
5608#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
5609 $$ = 0;
5610#else
5611 $$ = idFWD_KWREST;
5612#endif
5613 /*% %*/
5614 /*% ripper: args_forward! %*/
5615 }
5616 ;
5617
5618f_bad_arg : tCONSTANT
5619 {
5620 static const char mesg[] = "formal argument cannot be a constant";
5621 /*%%%*/
5622 yyerror1(&@1, mesg);
5623 $$ = 0;
5624 /*% %*/
5625 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5626 }
5627 | tIVAR
5628 {
5629 static const char mesg[] = "formal argument cannot be an instance variable";
5630 /*%%%*/
5631 yyerror1(&@1, mesg);
5632 $$ = 0;
5633 /*% %*/
5634 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5635 }
5636 | tGVAR
5637 {
5638 static const char mesg[] = "formal argument cannot be a global variable";
5639 /*%%%*/
5640 yyerror1(&@1, mesg);
5641 $$ = 0;
5642 /*% %*/
5643 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5644 }
5645 | tCVAR
5646 {
5647 static const char mesg[] = "formal argument cannot be a class variable";
5648 /*%%%*/
5649 yyerror1(&@1, mesg);
5650 $$ = 0;
5651 /*% %*/
5652 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5653 }
5654 ;
5655
5656f_norm_arg : f_bad_arg
5657 | tIDENTIFIER
5658 {
5659 formal_argument(p, $1);
5660 p->max_numparam = ORDINAL_PARAM;
5661 $$ = $1;
5662 }
5663 ;
5664
5665f_arg_asgn : f_norm_arg
5666 {
5667 ID id = get_id($1);
5668 arg_var(p, id);
5669 p->cur_arg = id;
5670 $$ = $1;
5671 }
5672 ;
5673
5674f_arg_item : f_arg_asgn
5675 {
5676 p->cur_arg = 0;
5677 /*%%%*/
5678 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5679 /*% %*/
5680 /*% ripper: get_value($1) %*/
5681 }
5682 | tLPAREN f_margs rparen
5683 {
5684 /*%%%*/
5685 ID tid = internal_id(p);
5686 YYLTYPE loc;
5687 loc.beg_pos = @2.beg_pos;
5688 loc.end_pos = @2.beg_pos;
5689 arg_var(p, tid);
5690 if (dyna_in_block(p)) {
5691 $2->nd_value = NEW_DVAR(tid, &loc);
5692 }
5693 else {
5694 $2->nd_value = NEW_LVAR(tid, &loc);
5695 }
5696 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5697 $$->nd_next = $2;
5698 /*% %*/
5699 /*% ripper: mlhs_paren!($2) %*/
5700 }
5701 ;
5702
5703f_arg : f_arg_item
5704 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5705 | f_arg ',' f_arg_item
5706 {
5707 /*%%%*/
5708 $$ = $1;
5709 $$->nd_plen++;
5710 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5711 rb_discard_node(p, $3);
5712 /*% %*/
5713 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5714 }
5715 ;
5716
5717
5718f_label : tLABEL
5719 {
5720 arg_var(p, formal_argument(p, $1));
5721 p->cur_arg = get_id($1);
5722 p->max_numparam = ORDINAL_PARAM;
5723 p->ctxt.in_argdef = 0;
5724 $$ = $1;
5725 }
5726 ;
5727
5728f_kw : f_label arg_value
5729 {
5730 p->cur_arg = 0;
5731 p->ctxt.in_argdef = 1;
5732 /*%%%*/
5733 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5734 /*% %*/
5735 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5736 }
5737 | f_label
5738 {
5739 p->cur_arg = 0;
5740 p->ctxt.in_argdef = 1;
5741 /*%%%*/
5742 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5743 /*% %*/
5744 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5745 }
5746 ;
5747
5748f_block_kw : f_label primary_value
5749 {
5750 p->ctxt.in_argdef = 1;
5751 /*%%%*/
5752 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5753 /*% %*/
5754 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5755 }
5756 | f_label
5757 {
5758 p->ctxt.in_argdef = 1;
5759 /*%%%*/
5760 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5761 /*% %*/
5762 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5763 }
5764 ;
5765
5766f_block_kwarg : f_block_kw
5767 {
5768 /*%%%*/
5769 $$ = $1;
5770 /*% %*/
5771 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5772 }
5773 | f_block_kwarg ',' f_block_kw
5774 {
5775 /*%%%*/
5776 $$ = kwd_append($1, $3);
5777 /*% %*/
5778 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5779 }
5780 ;
5781
5782
5783f_kwarg : f_kw
5784 {
5785 /*%%%*/
5786 $$ = $1;
5787 /*% %*/
5788 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5789 }
5790 | f_kwarg ',' f_kw
5791 {
5792 /*%%%*/
5793 $$ = kwd_append($1, $3);
5794 /*% %*/
5795 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5796 }
5797 ;
5798
5799kwrest_mark : tPOW
5800 | tDSTAR
5801 ;
5802
5803f_no_kwarg : p_kwnorest
5804 {
5805 /*%%%*/
5806 /*% %*/
5807 /*% ripper: nokw_param!(Qnil) %*/
5808 }
5809 ;
5810
5811f_kwrest : kwrest_mark tIDENTIFIER
5812 {
5813 arg_var(p, shadowing_lvar(p, get_id($2)));
5814 /*%%%*/
5815 $$ = $2;
5816 /*% %*/
5817 /*% ripper: kwrest_param!($2) %*/
5818 }
5819 | kwrest_mark
5820 {
5821 arg_var(p, idFWD_KWREST);
5822 /*%%%*/
5823 $$ = idFWD_KWREST;
5824 /*% %*/
5825 /*% ripper: kwrest_param!(Qnil) %*/
5826 }
5827 ;
5828
5829f_opt : f_arg_asgn f_eq arg_value
5830 {
5831 p->cur_arg = 0;
5832 p->ctxt.in_argdef = 1;
5833 /*%%%*/
5834 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5835 /*% %*/
5836 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5837 }
5838 ;
5839
5840f_block_opt : f_arg_asgn f_eq primary_value
5841 {
5842 p->cur_arg = 0;
5843 p->ctxt.in_argdef = 1;
5844 /*%%%*/
5845 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5846 /*% %*/
5847 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5848 }
5849 ;
5850
5851f_block_optarg : f_block_opt
5852 {
5853 /*%%%*/
5854 $$ = $1;
5855 /*% %*/
5856 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5857 }
5858 | f_block_optarg ',' f_block_opt
5859 {
5860 /*%%%*/
5861 $$ = opt_arg_append($1, $3);
5862 /*% %*/
5863 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5864 }
5865 ;
5866
5867f_optarg : f_opt
5868 {
5869 /*%%%*/
5870 $$ = $1;
5871 /*% %*/
5872 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5873 }
5874 | f_optarg ',' f_opt
5875 {
5876 /*%%%*/
5877 $$ = opt_arg_append($1, $3);
5878 /*% %*/
5879 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5880 }
5881 ;
5882
5883restarg_mark : '*'
5884 | tSTAR
5885 ;
5886
5887f_rest_arg : restarg_mark tIDENTIFIER
5888 {
5889 arg_var(p, shadowing_lvar(p, get_id($2)));
5890 /*%%%*/
5891 $$ = $2;
5892 /*% %*/
5893 /*% ripper: rest_param!($2) %*/
5894 }
5895 | restarg_mark
5896 {
5897 arg_var(p, idFWD_REST);
5898 /*%%%*/
5899 $$ = idFWD_REST;
5900 /*% %*/
5901 /*% ripper: rest_param!(Qnil) %*/
5902 }
5903 ;
5904
5905blkarg_mark : '&'
5906 | tAMPER
5907 ;
5908
5909f_block_arg : blkarg_mark tIDENTIFIER
5910 {
5911 arg_var(p, shadowing_lvar(p, get_id($2)));
5912 /*%%%*/
5913 $$ = $2;
5914 /*% %*/
5915 /*% ripper: blockarg!($2) %*/
5916 }
5917 | blkarg_mark
5918 {
5919 arg_var(p, idFWD_BLOCK);
5920 /*%%%*/
5921 $$ = idFWD_BLOCK;
5922 /*% %*/
5923 /*% ripper: blockarg!(Qnil) %*/
5924 }
5925 ;
5926
5927opt_f_block_arg : ',' f_block_arg
5928 {
5929 $$ = $2;
5930 }
5931 | none
5932 {
5933 $$ = Qnull;
5934 }
5935 ;
5936
5937singleton : var_ref
5938 {
5939 value_expr($1);
5940 $$ = $1;
5941 }
5942 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5943 {
5944 /*%%%*/
5945 switch (nd_type($3)) {
5946 case NODE_STR:
5947 case NODE_DSTR:
5948 case NODE_XSTR:
5949 case NODE_DXSTR:
5950 case NODE_DREGX:
5951 case NODE_LIT:
5952 case NODE_LIST:
5953 case NODE_ZLIST:
5954 yyerror1(&@3, "can't define singleton method for literals");
5955 break;
5956 default:
5957 value_expr($3);
5958 break;
5959 }
5960 $$ = $3;
5961 /*% %*/
5962 /*% ripper: paren!($3) %*/
5963 }
5964 ;
5965
5966assoc_list : none
5967 | assocs trailer
5968 {
5969 /*%%%*/
5970 $$ = $1;
5971 /*% %*/
5972 /*% ripper: assoclist_from_args!($1) %*/
5973 }
5974 ;
5975
5976assocs : assoc
5977 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5978 | assocs ',' assoc
5979 {
5980 /*%%%*/
5981 NODE *assocs = $1;
5982 NODE *tail = $3;
5983 if (!assocs) {
5984 assocs = tail;
5985 }
5986 else if (tail) {
5987 if (assocs->nd_head &&
5988 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
5989 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
5990 /* DSTAR */
5991 tail = tail->nd_next->nd_head->nd_head;
5992 }
5993 assocs = list_concat(assocs, tail);
5994 }
5995 $$ = assocs;
5996 /*% %*/
5997 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5998 }
5999 ;
6000
6001assoc : arg_value tASSOC arg_value
6002 {
6003 /*%%%*/
6004 if (nd_type_p($1, NODE_STR)) {
6005 nd_set_type($1, NODE_LIT);
6006 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
6007 }
6008 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6009 /*% %*/
6010 /*% ripper: assoc_new!($1, $3) %*/
6011 }
6012 | tLABEL arg_value
6013 {
6014 /*%%%*/
6015 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6016 /*% %*/
6017 /*% ripper: assoc_new!($1, $2) %*/
6018 }
6019 | tLABEL
6020 {
6021 /*%%%*/
6022 NODE *val = gettable(p, $1, &@$);
6023 if (!val) val = NEW_BEGIN(0, &@$);
6024 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6025 /*% %*/
6026 /*% ripper: assoc_new!($1, Qnil) %*/
6027 }
6028 | tSTRING_BEG string_contents tLABEL_END arg_value
6029 {
6030 /*%%%*/
6031 YYLTYPE loc = code_loc_gen(&@1, &@3);
6032 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6033 /*% %*/
6034 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6035 }
6036 | tDSTAR arg_value
6037 {
6038 /*%%%*/
6039 if (nd_type_p($2, NODE_HASH) &&
6040 !($2->nd_head && $2->nd_head->nd_alen)) {
6041 static VALUE empty_hash;
6042 if (!empty_hash) {
6043 empty_hash = rb_obj_freeze(rb_hash_new());
6044 rb_gc_register_mark_object(empty_hash);
6045 }
6046 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6047 }
6048 else
6049 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6050 /*% %*/
6051 /*% ripper: assoc_splat!($2) %*/
6052 }
6053 | tDSTAR
6054 {
6055 if (!local_id(p, idFWD_KWREST) ||
6056 local_id(p, idFWD_ALL)) {
6057 compile_error(p, "no anonymous keyword rest parameter");
6058 }
6059 /*%%%*/
6060 $$ = list_append(p, NEW_LIST(0, &@$),
6061 NEW_LVAR(idFWD_KWREST, &@$));
6062 /*% %*/
6063 /*% ripper: assoc_splat!(Qnil) %*/
6064 }
6065 ;
6066
6067operation : tIDENTIFIER
6068 | tCONSTANT
6069 | tFID
6070 ;
6071
6072operation2 : operation
6073 | op
6074 ;
6075
6076operation3 : tIDENTIFIER
6077 | tFID
6078 | op
6079 ;
6080
6081dot_or_colon : '.'
6082 | tCOLON2
6083 ;
6084
6085call_op : '.'
6086 | tANDDOT
6087 ;
6088
6089call_op2 : call_op
6090 | tCOLON2
6091 ;
6092
6093opt_terms : /* none */
6094 | terms
6095 ;
6096
6097opt_nl : /* none */
6098 | '\n'
6099 ;
6100
6101rparen : opt_nl ')'
6102 ;
6103
6104rbracket : opt_nl ']'
6105 ;
6106
6107rbrace : opt_nl '}'
6108 ;
6109
6110trailer : opt_nl
6111 | ','
6112 ;
6113
6114term : ';' {yyerrok;token_flush(p);}
6115 | '\n'
6116 {
6117 @$.end_pos = @$.beg_pos;
6118 token_flush(p);
6119 }
6120 ;
6121
6122terms : term
6123 | terms ';' {yyerrok;}
6124 ;
6125
6126none : /* none */
6127 {
6128 $$ = Qnull;
6129 }
6130 ;
6131%%
6132# undef p
6133# undef yylex
6134# undef yylval
6135# define yylval (*p->lval)
6136
6137static int regx_options(struct parser_params*);
6138static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6139static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6140static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6141static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6142
6143#ifndef RIPPER
6144# define set_yylval_node(x) { \
6145 YYLTYPE _cur_loc; \
6146 rb_parser_set_location(p, &_cur_loc); \
6147 yylval.node = (x); \
6148}
6149# define set_yylval_str(x) \
6150do { \
6151 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6152 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6153} while(0)
6154# define set_yylval_literal(x) \
6155do { \
6156 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6157 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6158} while(0)
6159# define set_yylval_num(x) (yylval.num = (x))
6160# define set_yylval_id(x) (yylval.id = (x))
6161# define set_yylval_name(x) (yylval.id = (x))
6162# define yylval_id() (yylval.id)
6163#else
6164static inline VALUE
6165ripper_yylval_id(struct parser_params *p, ID x)
6166{
6167 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6168}
6169# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6170# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6171# define set_yylval_id(x) (void)(x)
6172# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6173# define set_yylval_literal(x) add_mark_object(p, (x))
6174# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6175# define yylval_id() yylval.id
6176# define _cur_loc NULL_LOC /* dummy */
6177#endif
6178
6179#define set_yylval_noname() set_yylval_id(keyword_nil)
6180#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6181
6182#ifndef RIPPER
6183#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6184#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6185
6186static bool
6187parser_has_token(struct parser_params *p)
6188{
6189 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);
6190 return p->lex.pcur > p->lex.ptok;
6191}
6192
6193static VALUE
6194code_loc_to_ary(const rb_code_location_t *loc)
6195{
6196 VALUE ary = rb_ary_new_from_args(4,
6197 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6198 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6199 rb_obj_freeze(ary);
6200
6201 return ary;
6202}
6203
6204static void
6205parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6206{
6207 VALUE ary;
6208 int token_id;
6209
6210 ary = rb_ary_new2(4);
6211 token_id = p->token_id;
6212 rb_ary_push(ary, INT2FIX(token_id));
6213 rb_ary_push(ary, ID2SYM(parser_token2id(t)));
6214 rb_ary_push(ary, str);
6215 rb_ary_push(ary, code_loc_to_ary(p->yylloc));
6216 rb_obj_freeze(ary);
6217 rb_ary_push(p->tokens, ary);
6218 p->token_id++;
6219
6220 if (p->debug) {
6221 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6222 }
6223}
6224
6225static void
6226parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6227{
6228 debug_token_line(p, "parser_dispatch_scan_event", line);
6229
6230 if (!parser_has_token(p)) return;
6231
6232 RUBY_SET_YYLLOC(*p->yylloc);
6233
6234 if (p->keep_tokens) {
6235 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6236 parser_append_tokens(p, str, t, line);
6237 }
6238
6239 token_flush(p);
6240}
6241
6242#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6243static void
6244parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6245{
6246 int saved_line = p->ruby_sourceline;
6247 const char *saved_tokp = p->lex.ptok;
6248
6249 debug_token_line(p, "parser_dispatch_delayed_token", line);
6250
6251 if (!has_delayed_token(p)) return;
6252
6253 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6254
6255 if (p->keep_tokens) {
6256 p->ruby_sourceline = p->delayed.beg_line;
6257 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6258 parser_append_tokens(p, p->delayed.token, t, line);
6259 p->ruby_sourceline = saved_line;
6260 p->lex.ptok = saved_tokp;
6261 }
6262
6263 p->delayed.token = Qnil;
6264}
6265#else
6266#define literal_flush(p, ptr) ((void)(ptr))
6267
6268#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
6269
6270static inline VALUE
6271intern_sym(const char *name)
6272{
6273 ID id = rb_intern_const(name);
6274 return ID2SYM(id);
6275}
6276
6277static int
6278ripper_has_scan_event(struct parser_params *p)
6279{
6280 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6281 return p->lex.pcur > p->lex.ptok;
6282}
6283
6284static VALUE
6285ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6286{
6287 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6288 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6289 RUBY_SET_YYLLOC(*p->yylloc);
6290 token_flush(p);
6291 return rval;
6292}
6293
6294static void
6295ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6296{
6297 if (!ripper_has_scan_event(p)) return;
6298 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6299}
6300#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6301
6302static void
6303ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6304{
6305 int saved_line = p->ruby_sourceline;
6306 const char *saved_tokp = p->lex.ptok;
6307
6308 if (!has_delayed_token(p)) return;
6309 p->ruby_sourceline = p->delayed.beg_line;
6310 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6311 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6312 p->delayed.token = Qnil;
6313 p->ruby_sourceline = saved_line;
6314 p->lex.ptok = saved_tokp;
6315}
6316#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6317#endif /* RIPPER */
6318
6319static inline int
6320is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6321{
6322 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6323}
6324
6325static inline int
6326parser_is_identchar(struct parser_params *p)
6327{
6328 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
6329}
6330
6331static inline int
6332parser_isascii(struct parser_params *p)
6333{
6334 return ISASCII(*(p->lex.pcur-1));
6335}
6336
6337static void
6338token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6339{
6340 int column = 1, nonspc = 0, i;
6341 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6342 if (*ptr == '\t') {
6343 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6344 }
6345 column++;
6346 if (*ptr != ' ' && *ptr != '\t') {
6347 nonspc = 1;
6348 }
6349 }
6350
6351 ptinfo->beg = loc->beg_pos;
6352 ptinfo->indent = column;
6353 ptinfo->nonspc = nonspc;
6354}
6355
6356static void
6357token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6358{
6359 token_info *ptinfo;
6360
6361 if (!p->token_info_enabled) return;
6362 ptinfo = ALLOC(token_info);
6363 ptinfo->token = token;
6364 ptinfo->next = p->token_info;
6365 token_info_setup(ptinfo, p->lex.pbeg, loc);
6366
6367 p->token_info = ptinfo;
6368}
6369
6370static void
6371token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6372{
6373 token_info *ptinfo_beg = p->token_info;
6374
6375 if (!ptinfo_beg) return;
6376 p->token_info = ptinfo_beg->next;
6377
6378 /* indentation check of matched keywords (begin..end, if..end, etc.) */
6379 token_info_warn(p, token, ptinfo_beg, 1, loc);
6380 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6381}
6382
6383static void
6384token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
6385{
6386 token_info *ptinfo_beg = p->token_info;
6387
6388 if (!ptinfo_beg) return;
6389 p->token_info = ptinfo_beg->next;
6390
6391 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
6392 ptinfo_beg->beg.column != beg_pos.column ||
6393 strcmp(ptinfo_beg->token, token)) {
6394 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
6395 beg_pos.lineno, beg_pos.column, token,
6396 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
6397 ptinfo_beg->token);
6398 }
6399
6400 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6401}
6402
6403static void
6404token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
6405{
6406 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
6407 if (!p->token_info_enabled) return;
6408 if (!ptinfo_beg) return;
6409 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
6410 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
6411 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
6412 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
6413 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
6414 rb_warn3L(ptinfo_end->beg.lineno,
6415 "mismatched indentations at '%s' with '%s' at %d",
6416 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
6417}
6418
6419static int
6420parser_precise_mbclen(struct parser_params *p, const char *ptr)
6421{
6422 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
6423 if (!MBCLEN_CHARFOUND_P(len)) {
6424 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
6425 return -1;
6426 }
6427 return len;
6428}
6429
6430#ifndef RIPPER
6431static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6432
6433static inline void
6434parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6435{
6436 VALUE str;
6437 int lineno = p->ruby_sourceline;
6438 if (!yylloc) {
6439 return;
6440 }
6441 else if (yylloc->beg_pos.lineno == lineno) {
6442 str = p->lex.lastline;
6443 }
6444 else {
6445 return;
6446 }
6447 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6448}
6449
6450static int
6451parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6452{
6453#if 0
6454 YYLTYPE current;
6455
6456 if (!yylloc) {
6457 yylloc = RUBY_SET_YYLLOC(current);
6458 }
6459 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6460 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6461 yylloc = 0;
6462 }
6463#endif
6464 compile_error(p, "%s", msg);
6465 parser_show_error_line(p, yylloc);
6466 return 0;
6467}
6468
6469static int
6470parser_yyerror0(struct parser_params *p, const char *msg)
6471{
6472 YYLTYPE current;
6473 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6474}
6475
6476static void
6477ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6478{
6479 VALUE mesg;
6480 const int max_line_margin = 30;
6481 const char *ptr, *ptr_end, *pt, *pb;
6482 const char *pre = "", *post = "", *pend;
6483 const char *code = "", *caret = "";
6484 const char *lim;
6485 const char *const pbeg = RSTRING_PTR(str);
6486 char *buf;
6487 long len;
6488 int i;
6489
6490 if (!yylloc) return;
6491 pend = RSTRING_END(str);
6492 if (pend > pbeg && pend[-1] == '\n') {
6493 if (--pend > pbeg && pend[-1] == '\r') --pend;
6494 }
6495
6496 pt = pend;
6497 if (lineno == yylloc->end_pos.lineno &&
6498 (pend - pbeg) > yylloc->end_pos.column) {
6499 pt = pbeg + yylloc->end_pos.column;
6500 }
6501
6502 ptr = ptr_end = pt;
6503 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6504 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6505
6506 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6507 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6508
6509 len = ptr_end - ptr;
6510 if (len > 4) {
6511 if (ptr > pbeg) {
6512 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6513 if (ptr > pbeg) pre = "...";
6514 }
6515 if (ptr_end < pend) {
6516 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6517 if (ptr_end < pend) post = "...";
6518 }
6519 }
6520 pb = pbeg;
6521 if (lineno == yylloc->beg_pos.lineno) {
6522 pb += yylloc->beg_pos.column;
6523 if (pb > pt) pb = pt;
6524 }
6525 if (pb < ptr) pb = ptr;
6526 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6527 return;
6528 }
6529 if (RTEST(errbuf)) {
6530 mesg = rb_attr_get(errbuf, idMesg);
6531 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6532 rb_str_cat_cstr(mesg, "\n");
6533 }
6534 else {
6535 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6536 }
6537 if (!errbuf && rb_stderr_tty_p()) {
6538#define CSI_BEGIN "\033["
6539#define CSI_SGR "m"
6540 rb_str_catf(mesg,
6541 CSI_BEGIN""CSI_SGR"%s" /* pre */
6542 CSI_BEGIN"1"CSI_SGR"%.*s"
6543 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6544 CSI_BEGIN";1"CSI_SGR"%.*s"
6545 CSI_BEGIN""CSI_SGR"%s" /* post */
6546 "\n",
6547 pre,
6548 (int)(pb - ptr), ptr,
6549 (int)(pt - pb), pb,
6550 (int)(ptr_end - pt), pt,
6551 post);
6552 }
6553 else {
6554 char *p2;
6555
6556 len = ptr_end - ptr;
6557 lim = pt < pend ? pt : pend;
6558 i = (int)(lim - ptr);
6559 buf = ALLOCA_N(char, i+2);
6560 code = ptr;
6561 caret = p2 = buf;
6562 if (ptr <= pb) {
6563 while (ptr < pb) {
6564 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6565 }
6566 *p2++ = '^';
6567 ptr++;
6568 }
6569 if (lim > ptr) {
6570 memset(p2, '~', (lim - ptr));
6571 p2 += (lim - ptr);
6572 }
6573 *p2 = '\0';
6574 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6575 pre, (int)len, code, post,
6576 pre, caret);
6577 }
6578 if (!errbuf) rb_write_error_str(mesg);
6579}
6580#else
6581static int
6582parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6583{
6584 const char *pcur = 0, *ptok = 0;
6585 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6586 p->ruby_sourceline == yylloc->end_pos.lineno) {
6587 pcur = p->lex.pcur;
6588 ptok = p->lex.ptok;
6589 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6590 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6591 }
6592 parser_yyerror0(p, msg);
6593 if (pcur) {
6594 p->lex.ptok = ptok;
6595 p->lex.pcur = pcur;
6596 }
6597 return 0;
6598}
6599
6600static int
6601parser_yyerror0(struct parser_params *p, const char *msg)
6602{
6603 dispatch1(parse_error, STR_NEW2(msg));
6604 ripper_error(p);
6605 return 0;
6606}
6607
6608static inline void
6609parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6610{
6611}
6612#endif /* !RIPPER */
6613
6614#ifndef RIPPER
6615static int
6616vtable_size(const struct vtable *tbl)
6617{
6618 if (!DVARS_TERMINAL_P(tbl)) {
6619 return tbl->pos;
6620 }
6621 else {
6622 return 0;
6623 }
6624}
6625#endif
6626
6627static struct vtable *
6628vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6629{
6630 struct vtable *tbl = ALLOC(struct vtable);
6631 tbl->pos = 0;
6632 tbl->capa = 8;
6633 tbl->tbl = ALLOC_N(ID, tbl->capa);
6634 tbl->prev = prev;
6635#ifndef RIPPER
6636 if (p->debug) {
6637 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6638 }
6639#endif
6640 return tbl;
6641}
6642#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6643
6644static void
6645vtable_free_gen(struct parser_params *p, int line, const char *name,
6646 struct vtable *tbl)
6647{
6648#ifndef RIPPER
6649 if (p->debug) {
6650 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6651 }
6652#endif
6653 if (!DVARS_TERMINAL_P(tbl)) {
6654 if (tbl->tbl) {
6655 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6656 }
6657 ruby_sized_xfree(tbl, sizeof(*tbl));
6658 }
6659}
6660#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6661
6662static void
6663vtable_add_gen(struct parser_params *p, int line, const char *name,
6664 struct vtable *tbl, ID id)
6665{
6666#ifndef RIPPER
6667 if (p->debug) {
6668 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6669 line, name, (void *)tbl, rb_id2name(id));
6670 }
6671#endif
6672 if (DVARS_TERMINAL_P(tbl)) {
6673 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6674 return;
6675 }
6676 if (tbl->pos == tbl->capa) {
6677 tbl->capa = tbl->capa * 2;
6678 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6679 }
6680 tbl->tbl[tbl->pos++] = id;
6681}
6682#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6683
6684#ifndef RIPPER
6685static void
6686vtable_pop_gen(struct parser_params *p, int line, const char *name,
6687 struct vtable *tbl, int n)
6688{
6689 if (p->debug) {
6690 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6691 line, name, (void *)tbl, n);
6692 }
6693 if (tbl->pos < n) {
6694 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6695 return;
6696 }
6697 tbl->pos -= n;
6698}
6699#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6700#endif
6701
6702static int
6703vtable_included(const struct vtable * tbl, ID id)
6704{
6705 int i;
6706
6707 if (!DVARS_TERMINAL_P(tbl)) {
6708 for (i = 0; i < tbl->pos; i++) {
6709 if (tbl->tbl[i] == id) {
6710 return i+1;
6711 }
6712 }
6713 }
6714 return 0;
6715}
6716
6717static void parser_prepare(struct parser_params *p);
6718
6719#ifndef RIPPER
6720static NODE *parser_append_options(struct parser_params *p, NODE *node);
6721
6722static VALUE
6723debug_lines(VALUE fname)
6724{
6725 ID script_lines;
6726 CONST_ID(script_lines, "SCRIPT_LINES__");
6727 if (rb_const_defined_at(rb_cObject, script_lines)) {
6728 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6729 if (RB_TYPE_P(hash, T_HASH)) {
6730 VALUE lines = rb_ary_new();
6731 rb_hash_aset(hash, fname, lines);
6732 return lines;
6733 }
6734 }
6735 return 0;
6736}
6737
6738static int
6739e_option_supplied(struct parser_params *p)
6740{
6741 return strcmp(p->ruby_sourcefile, "-e") == 0;
6742}
6743
6744static VALUE
6745yycompile0(VALUE arg)
6746{
6747 int n;
6748 NODE *tree;
6749 struct parser_params *p = (struct parser_params *)arg;
6750 VALUE cov = Qfalse;
6751
6752 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6753 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6754 if (p->debug_lines && p->ruby_sourceline > 0) {
6755 VALUE str = rb_default_rs;
6756 n = p->ruby_sourceline;
6757 do {
6758 rb_ary_push(p->debug_lines, str);
6759 } while (--n);
6760 }
6761
6762 if (!e_option_supplied(p)) {
6763 cov = Qtrue;
6764 }
6765 }
6766
6767 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6768 if (!p->debug_lines) {
6769 p->debug_lines = rb_ary_new();
6770 }
6771
6772 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6773 }
6774
6775 parser_prepare(p);
6776#define RUBY_DTRACE_PARSE_HOOK(name) \
6777 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6778 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6779 }
6780 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6781 n = yyparse(p);
6782 RUBY_DTRACE_PARSE_HOOK(END);
6783 p->debug_lines = 0;
6784
6785 p->lex.strterm = 0;
6786 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6787 if (n || p->error_p) {
6788 VALUE mesg = p->error_buffer;
6789 if (!mesg) {
6790 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6791 }
6792 if (!p->error_tolerant) {
6793 rb_set_errinfo(mesg);
6794 return FALSE;
6795 }
6796 }
6797 tree = p->eval_tree;
6798 if (!tree) {
6799 tree = NEW_NIL(&NULL_LOC);
6800 }
6801 else {
6802 VALUE opt = p->compile_option;
6803 VALUE tokens = p->tokens;
6804 NODE *prelude;
6805 NODE *body = parser_append_options(p, tree->nd_body);
6806 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6807 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6808 prelude = block_append(p, p->eval_tree_begin, body);
6809 tree->nd_body = prelude;
6810 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6811 if (p->keep_tokens) {
6812 rb_obj_freeze(tokens);
6813 rb_ast_set_tokens(p->ast, tokens);
6814 }
6815 }
6816 p->ast->body.root = tree;
6817 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6818 return TRUE;
6819}
6820
6821static rb_ast_t *
6822yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6823{
6824 rb_ast_t *ast;
6825 if (NIL_P(fname)) {
6826 p->ruby_sourcefile_string = Qnil;
6827 p->ruby_sourcefile = "(none)";
6828 }
6829 else {
6830 p->ruby_sourcefile_string = rb_fstring(fname);
6831 p->ruby_sourcefile = StringValueCStr(fname);
6832 }
6833 p->ruby_sourceline = line - 1;
6834
6835 p->lvtbl = NULL;
6836
6837 p->ast = ast = rb_ast_new();
6838 rb_suppress_tracing(yycompile0, (VALUE)p);
6839 p->ast = 0;
6840 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6841
6842 while (p->lvtbl) {
6843 local_pop(p);
6844 }
6845
6846 return ast;
6847}
6848#endif /* !RIPPER */
6849
6850static rb_encoding *
6851must_be_ascii_compatible(VALUE s)
6852{
6853 rb_encoding *enc = rb_enc_get(s);
6854 if (!rb_enc_asciicompat(enc)) {
6855 rb_raise(rb_eArgError, "invalid source encoding");
6856 }
6857 return enc;
6858}
6859
6860static VALUE
6861lex_get_str(struct parser_params *p, VALUE s)
6862{
6863 char *beg, *end, *start;
6864 long len;
6865
6866 beg = RSTRING_PTR(s);
6867 len = RSTRING_LEN(s);
6868 start = beg;
6869 if (p->lex.gets_.ptr) {
6870 if (len == p->lex.gets_.ptr) return Qnil;
6871 beg += p->lex.gets_.ptr;
6872 len -= p->lex.gets_.ptr;
6873 }
6874 end = memchr(beg, '\n', len);
6875 if (end) len = ++end - beg;
6876 p->lex.gets_.ptr += len;
6877 return rb_str_subseq(s, beg - start, len);
6878}
6879
6880static VALUE
6881lex_getline(struct parser_params *p)
6882{
6883 VALUE line = (*p->lex.gets)(p, p->lex.input);
6884 if (NIL_P(line)) return line;
6885 must_be_ascii_compatible(line);
6886 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6887 p->line_count++;
6888 return line;
6889}
6890
6891static const rb_data_type_t parser_data_type;
6892
6893#ifndef RIPPER
6894static rb_ast_t*
6895parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6896{
6897 struct parser_params *p;
6898
6899 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6900
6901 p->lex.gets = lex_get_str;
6902 p->lex.gets_.ptr = 0;
6903 p->lex.input = rb_str_new_frozen(s);
6904 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6905
6906 return yycompile(vparser, p, fname, line);
6907}
6908
6909rb_ast_t*
6910rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6911{
6912 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6913}
6914
6915rb_ast_t*
6916rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6917{
6918 must_be_ascii_compatible(s);
6919 return parser_compile_string(vparser, f, s, line);
6920}
6921
6922VALUE rb_io_gets_internal(VALUE io);
6923
6924static VALUE
6925lex_io_gets(struct parser_params *p, VALUE io)
6926{
6927 return rb_io_gets_internal(io);
6928}
6929
6930rb_ast_t*
6931rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6932{
6933 struct parser_params *p;
6934
6935 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6936
6937 p->lex.gets = lex_io_gets;
6938 p->lex.input = file;
6939 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6940
6941 return yycompile(vparser, p, fname, start);
6942}
6943
6944static VALUE
6945lex_generic_gets(struct parser_params *p, VALUE input)
6946{
6947 return (*p->lex.gets_.call)(input, p->line_count);
6948}
6949
6950rb_ast_t*
6951rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6952{
6953 struct parser_params *p;
6954
6955 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6956
6957 p->lex.gets = lex_generic_gets;
6958 p->lex.gets_.call = lex_gets;
6959 p->lex.input = input;
6960 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6961
6962 return yycompile(vparser, p, fname, start);
6963}
6964#endif /* !RIPPER */
6965
6966#define STR_FUNC_ESCAPE 0x01
6967#define STR_FUNC_EXPAND 0x02
6968#define STR_FUNC_REGEXP 0x04
6969#define STR_FUNC_QWORDS 0x08
6970#define STR_FUNC_SYMBOL 0x10
6971#define STR_FUNC_INDENT 0x20
6972#define STR_FUNC_LABEL 0x40
6973#define STR_FUNC_LIST 0x4000
6974#define STR_FUNC_TERM 0x8000
6975
6976enum string_type {
6977 str_label = STR_FUNC_LABEL,
6978 str_squote = (0),
6979 str_dquote = (STR_FUNC_EXPAND),
6980 str_xquote = (STR_FUNC_EXPAND),
6981 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6982 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6983 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6984 str_ssym = (STR_FUNC_SYMBOL),
6985 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6986};
6987
6988static VALUE
6989parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6990{
6991 VALUE str;
6992
6993 str = rb_enc_str_new(ptr, len, enc);
6994 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6995 if (is_ascii_string(str)) {
6996 }
6997 else if (rb_is_usascii_enc(enc0) && enc != rb_utf8_encoding()) {
6998 rb_enc_associate(str, rb_ascii8bit_encoding());
6999 }
7000 }
7001
7002 return str;
7003}
7004
7005#define peek(p,c) peek_n(p, (c), 0)
7006#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7007#define peekc(p) peekc_n(p, 0)
7008#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7009
7010static void
7011add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7012{
7013#ifndef RIPPER
7014 debug_token_line(p, "add_delayed_token", line);
7015#endif
7016
7017 if (tok < end) {
7018 if (!has_delayed_token(p)) {
7019 p->delayed.token = rb_str_buf_new(end - tok);
7020 rb_enc_associate(p->delayed.token, p->enc);
7021 p->delayed.beg_line = p->ruby_sourceline;
7022 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7023 }
7024 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7025 p->delayed.end_line = p->ruby_sourceline;
7026 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7027 p->lex.ptok = end;
7028 }
7029}
7030
7031static int
7032nextline(struct parser_params *p, int set_encoding)
7033{
7034 VALUE v = p->lex.nextline;
7035 p->lex.nextline = 0;
7036 if (!v) {
7037 if (p->eofp)
7038 return -1;
7039
7040 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
7041 goto end_of_input;
7042 }
7043
7044 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7045 end_of_input:
7046 p->eofp = 1;
7047 lex_goto_eol(p);
7048 return -1;
7049 }
7050#ifndef RIPPER
7051 if (p->debug_lines) {
7052 if (set_encoding) rb_enc_associate(v, p->enc);
7053 rb_ary_push(p->debug_lines, v);
7054 }
7055#endif
7056 p->cr_seen = FALSE;
7057 }
7058 else if (NIL_P(v)) {
7059 /* after here-document without terminator */
7060 goto end_of_input;
7061 }
7062 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7063 if (p->heredoc_end > 0) {
7064 p->ruby_sourceline = p->heredoc_end;
7065 p->heredoc_end = 0;
7066 }
7067 p->ruby_sourceline++;
7068 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7069 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7070 token_flush(p);
7071 p->lex.lastline = v;
7072 return 0;
7073}
7074
7075static int
7076parser_cr(struct parser_params *p, int c)
7077{
7078 if (peek(p, '\n')) {
7079 p->lex.pcur++;
7080 c = '\n';
7081 }
7082 return c;
7083}
7084
7085static inline int
7086nextc0(struct parser_params *p, int set_encoding)
7087{
7088 int c;
7089
7090 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
7091 if (nextline(p, set_encoding)) return -1;
7092 }
7093 c = (unsigned char)*p->lex.pcur++;
7094 if (UNLIKELY(c == '\r')) {
7095 c = parser_cr(p, c);
7096 }
7097
7098 return c;
7099}
7100#define nextc(p) nextc0(p, TRUE)
7101
7102static void
7103pushback(struct parser_params *p, int c)
7104{
7105 if (c == -1) return;
7106 p->lex.pcur--;
7107 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7108 p->lex.pcur--;
7109 }
7110}
7111
7112#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7113
7114#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7115#define tok(p) (p)->tokenbuf
7116#define toklen(p) (p)->tokidx
7117
7118static int
7119looking_at_eol_p(struct parser_params *p)
7120{
7121 const char *ptr = p->lex.pcur;
7122 while (ptr < p->lex.pend) {
7123 int c = (unsigned char)*ptr++;
7124 int eol = (c == '\n' || c == '#');
7125 if (eol || !ISSPACE(c)) {
7126 return eol;
7127 }
7128 }
7129 return TRUE;
7130}
7131
7132static char*
7133newtok(struct parser_params *p)
7134{
7135 p->tokidx = 0;
7136 p->tokline = p->ruby_sourceline;
7137 if (!p->tokenbuf) {
7138 p->toksiz = 60;
7139 p->tokenbuf = ALLOC_N(char, 60);
7140 }
7141 if (p->toksiz > 4096) {
7142 p->toksiz = 60;
7143 REALLOC_N(p->tokenbuf, char, 60);
7144 }
7145 return p->tokenbuf;
7146}
7147
7148static char *
7149tokspace(struct parser_params *p, int n)
7150{
7151 p->tokidx += n;
7152
7153 if (p->tokidx >= p->toksiz) {
7154 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7155 REALLOC_N(p->tokenbuf, char, p->toksiz);
7156 }
7157 return &p->tokenbuf[p->tokidx-n];
7158}
7159
7160static void
7161tokadd(struct parser_params *p, int c)
7162{
7163 p->tokenbuf[p->tokidx++] = (char)c;
7164 if (p->tokidx >= p->toksiz) {
7165 p->toksiz *= 2;
7166 REALLOC_N(p->tokenbuf, char, p->toksiz);
7167 }
7168}
7169
7170static int
7171tok_hex(struct parser_params *p, size_t *numlen)
7172{
7173 int c;
7174
7175 c = scan_hex(p->lex.pcur, 2, numlen);
7176 if (!*numlen) {
7177 yyerror0("invalid hex escape");
7178 token_flush(p);
7179 return 0;
7180 }
7181 p->lex.pcur += *numlen;
7182 return c;
7183}
7184
7185#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7186
7187static int
7188escaped_control_code(int c)
7189{
7190 int c2 = 0;
7191 switch (c) {
7192 case ' ':
7193 c2 = 's';
7194 break;
7195 case '\n':
7196 c2 = 'n';
7197 break;
7198 case '\t':
7199 c2 = 't';
7200 break;
7201 case '\v':
7202 c2 = 'v';
7203 break;
7204 case '\r':
7205 c2 = 'r';
7206 break;
7207 case '\f':
7208 c2 = 'f';
7209 break;
7210 }
7211 return c2;
7212}
7213
7214#define WARN_SPACE_CHAR(c, prefix) \
7215 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7216
7217static int
7218tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7219 int regexp_literal, int wide)
7220{
7221 size_t numlen;
7222 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7223 p->lex.pcur += numlen;
7224 if (p->lex.strterm == NULL ||
7225 (p->lex.strterm->flags & STRTERM_HEREDOC) ||
7226 (p->lex.strterm->u.literal.u1.func != str_regexp)) {
7227 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7228 literal_flush(p, p->lex.pcur);
7229 yyerror0("invalid Unicode escape");
7230 return wide && numlen > 0;
7231 }
7232 if (codepoint > 0x10ffff) {
7233 literal_flush(p, p->lex.pcur);
7234 yyerror0("invalid Unicode codepoint (too large)");
7235 return wide;
7236 }
7237 if ((codepoint & 0xfffff800) == 0xd800) {
7238 literal_flush(p, p->lex.pcur);
7239 yyerror0("invalid Unicode codepoint");
7240 return wide;
7241 }
7242 }
7243 if (regexp_literal) {
7244 tokcopy(p, (int)numlen);
7245 }
7246 else if (codepoint >= 0x80) {
7247 rb_encoding *utf8 = rb_utf8_encoding();
7248 if (*encp && utf8 != *encp) {
7249 YYLTYPE loc = RUBY_INIT_YYLLOC();
7250 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7251 parser_show_error_line(p, &loc);
7252 return wide;
7253 }
7254 *encp = utf8;
7255 tokaddmbc(p, codepoint, *encp);
7256 }
7257 else {
7258 tokadd(p, codepoint);
7259 }
7260 return TRUE;
7261}
7262
7263/* return value is for ?\u3042 */
7264static void
7265tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7266 int term, int symbol_literal, int regexp_literal)
7267{
7268 /*
7269 * If `term` is not -1, then we allow multiple codepoints in \u{}
7270 * upto `term` byte, otherwise we're parsing a character literal.
7271 * And then add the codepoints to the current token.
7272 */
7273 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7274
7275 const int open_brace = '{', close_brace = '}';
7276
7277 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7278
7279 if (peek(p, open_brace)) { /* handle \u{...} form */
7280 const char *second = NULL;
7281 int c, last = nextc(p);
7282 if (p->lex.pcur >= p->lex.pend) goto unterminated;
7283 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
7284 while (c != close_brace) {
7285 if (c == term) goto unterminated;
7286 if (second == multiple_codepoints)
7287 second = p->lex.pcur;
7288 if (regexp_literal) tokadd(p, last);
7289 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7290 break;
7291 }
7292 while (ISSPACE(c = *p->lex.pcur)) {
7293 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
7294 last = c;
7295 }
7296 if (term == -1 && !second)
7297 second = multiple_codepoints;
7298 }
7299
7300 if (c != close_brace) {
7301 unterminated:
7302 token_flush(p);
7303 yyerror0("unterminated Unicode escape");
7304 return;
7305 }
7306 if (second && second != multiple_codepoints) {
7307 const char *pcur = p->lex.pcur;
7308 p->lex.pcur = second;
7309 dispatch_scan_event(p, tSTRING_CONTENT);
7310 token_flush(p);
7311 p->lex.pcur = pcur;
7312 yyerror0(multiple_codepoints);
7313 token_flush(p);
7314 }
7315
7316 if (regexp_literal) tokadd(p, close_brace);
7317 nextc(p);
7318 }
7319 else { /* handle \uxxxx form */
7320 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7321 token_flush(p);
7322 return;
7323 }
7324 }
7325}
7326
7327#define ESCAPE_CONTROL 1
7328#define ESCAPE_META 2
7329
7330static int
7331read_escape(struct parser_params *p, int flags, rb_encoding **encp)
7332{
7333 int c;
7334 size_t numlen;
7335
7336 switch (c = nextc(p)) {
7337 case '\\': /* Backslash */
7338 return c;
7339
7340 case 'n': /* newline */
7341 return '\n';
7342
7343 case 't': /* horizontal tab */
7344 return '\t';
7345
7346 case 'r': /* carriage-return */
7347 return '\r';
7348
7349 case 'f': /* form-feed */
7350 return '\f';
7351
7352 case 'v': /* vertical tab */
7353 return '\13';
7354
7355 case 'a': /* alarm(bell) */
7356 return '\007';
7357
7358 case 'e': /* escape */
7359 return 033;
7360
7361 case '0': case '1': case '2': case '3': /* octal constant */
7362 case '4': case '5': case '6': case '7':
7363 pushback(p, c);
7364 c = scan_oct(p->lex.pcur, 3, &numlen);
7365 p->lex.pcur += numlen;
7366 return c;
7367
7368 case 'x': /* hex constant */
7369 c = tok_hex(p, &numlen);
7370 if (numlen == 0) return 0;
7371 return c;
7372
7373 case 'b': /* backspace */
7374 return '\010';
7375
7376 case 's': /* space */
7377 return ' ';
7378
7379 case 'M':
7380 if (flags & ESCAPE_META) goto eof;
7381 if ((c = nextc(p)) != '-') {
7382 goto eof;
7383 }
7384 if ((c = nextc(p)) == '\\') {
7385 switch (peekc(p)) {
7386 case 'u': case 'U':
7387 nextc(p);
7388 goto eof;
7389 }
7390 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
7391 }
7392 else if (c == -1 || !ISASCII(c)) goto eof;
7393 else {
7394 int c2 = escaped_control_code(c);
7395 if (c2) {
7396 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
7397 WARN_SPACE_CHAR(c2, "\\M-");
7398 }
7399 else {
7400 WARN_SPACE_CHAR(c2, "\\C-\\M-");
7401 }
7402 }
7403 else if (ISCNTRL(c)) goto eof;
7404 return ((c & 0xff) | 0x80);
7405 }
7406
7407 case 'C':
7408 if ((c = nextc(p)) != '-') {
7409 goto eof;
7410 }
7411 case 'c':
7412 if (flags & ESCAPE_CONTROL) goto eof;
7413 if ((c = nextc(p))== '\\') {
7414 switch (peekc(p)) {
7415 case 'u': case 'U':
7416 nextc(p);
7417 goto eof;
7418 }
7419 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
7420 }
7421 else if (c == '?')
7422 return 0177;
7423 else if (c == -1 || !ISASCII(c)) goto eof;
7424 else {
7425 int c2 = escaped_control_code(c);
7426 if (c2) {
7427 if (ISCNTRL(c)) {
7428 if (flags & ESCAPE_META) {
7429 WARN_SPACE_CHAR(c2, "\\M-");
7430 }
7431 else {
7432 WARN_SPACE_CHAR(c2, "");
7433 }
7434 }
7435 else {
7436 if (flags & ESCAPE_META) {
7437 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7438 }
7439 else {
7440 WARN_SPACE_CHAR(c2, "\\C-");
7441 }
7442 }
7443 }
7444 else if (ISCNTRL(c)) goto eof;
7445 }
7446 return c & 0x9f;
7447
7448 eof:
7449 case -1:
7450 yyerror0("Invalid escape character syntax");
7451 token_flush(p);
7452 return '\0';
7453
7454 default:
7455 return c;
7456 }
7457}
7458
7459static void
7460tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7461{
7462 int len = rb_enc_codelen(c, enc);
7463 rb_enc_mbcput(c, tokspace(p, len), enc);
7464}
7465
7466static int
7467tokadd_escape(struct parser_params *p, rb_encoding **encp)
7468{
7469 int c;
7470 size_t numlen;
7471
7472 switch (c = nextc(p)) {
7473 case '\n':
7474 return 0; /* just ignore */
7475
7476 case '0': case '1': case '2': case '3': /* octal constant */
7477 case '4': case '5': case '6': case '7':
7478 {
7479 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7480 if (numlen == 0) goto eof;
7481 p->lex.pcur += numlen;
7482 tokcopy(p, (int)numlen + 1);
7483 }
7484 return 0;
7485
7486 case 'x': /* hex constant */
7487 {
7488 tok_hex(p, &numlen);
7489 if (numlen == 0) return -1;
7490 tokcopy(p, (int)numlen + 2);
7491 }
7492 return 0;
7493
7494 eof:
7495 case -1:
7496 yyerror0("Invalid escape character syntax");
7497 token_flush(p);
7498 return -1;
7499
7500 default:
7501 tokadd(p, '\\');
7502 tokadd(p, c);
7503 }
7504 return 0;
7505}
7506
7507static int
7508regx_options(struct parser_params *p)
7509{
7510 int kcode = 0;
7511 int kopt = 0;
7512 int options = 0;
7513 int c, opt, kc;
7514
7515 newtok(p);
7516 while (c = nextc(p), ISALPHA(c)) {
7517 if (c == 'o') {
7518 options |= RE_OPTION_ONCE;
7519 }
7520 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7521 if (kc >= 0) {
7522 if (kc != rb_ascii8bit_encindex()) kcode = c;
7523 kopt = opt;
7524 }
7525 else {
7526 options |= opt;
7527 }
7528 }
7529 else {
7530 tokadd(p, c);
7531 }
7532 }
7533 options |= kopt;
7534 pushback(p, c);
7535 if (toklen(p)) {
7536 YYLTYPE loc = RUBY_INIT_YYLLOC();
7537 tokfix(p);
7538 compile_error(p, "unknown regexp option%s - %*s",
7539 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7540 parser_show_error_line(p, &loc);
7541 }
7542 return options | RE_OPTION_ENCODING(kcode);
7543}
7544
7545static int
7546tokadd_mbchar(struct parser_params *p, int c)
7547{
7548 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7549 if (len < 0) return -1;
7550 tokadd(p, c);
7551 p->lex.pcur += --len;
7552 if (len > 0) tokcopy(p, len);
7553 return c;
7554}
7555
7556static inline int
7557simple_re_meta(int c)
7558{
7559 switch (c) {
7560 case '$': case '*': case '+': case '.':
7561 case '?': case '^': case '|':
7562 case ')': case ']': case '}': case '>':
7563 return TRUE;
7564 default:
7565 return FALSE;
7566 }
7567}
7568
7569static int
7570parser_update_heredoc_indent(struct parser_params *p, int c)
7571{
7572 if (p->heredoc_line_indent == -1) {
7573 if (c == '\n') p->heredoc_line_indent = 0;
7574 }
7575 else {
7576 if (c == ' ') {
7577 p->heredoc_line_indent++;
7578 return TRUE;
7579 }
7580 else if (c == '\t') {
7581 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7582 p->heredoc_line_indent = w * TAB_WIDTH;
7583 return TRUE;
7584 }
7585 else if (c != '\n') {
7586 if (p->heredoc_indent > p->heredoc_line_indent) {
7587 p->heredoc_indent = p->heredoc_line_indent;
7588 }
7589 p->heredoc_line_indent = -1;
7590 }
7591 }
7592 return FALSE;
7593}
7594
7595static void
7596parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7597{
7598 YYLTYPE loc = RUBY_INIT_YYLLOC();
7599 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7600 compile_error(p, "%s mixed within %s source", n1, n2);
7601 parser_show_error_line(p, &loc);
7602}
7603
7604static void
7605parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7606{
7607 const char *pos = p->lex.pcur;
7608 p->lex.pcur = beg;
7609 parser_mixed_error(p, enc1, enc2);
7610 p->lex.pcur = pos;
7611}
7612
7613static int
7614tokadd_string(struct parser_params *p,
7615 int func, int term, int paren, long *nest,
7616 rb_encoding **encp, rb_encoding **enc)
7617{
7618 int c;
7619 bool erred = false;
7620#ifdef RIPPER
7621 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
7622 int top_of_line = FALSE;
7623#endif
7624
7625#define mixed_error(enc1, enc2) \
7626 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7627#define mixed_escape(beg, enc1, enc2) \
7628 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7629
7630 while ((c = nextc(p)) != -1) {
7631 if (p->heredoc_indent > 0) {
7632 parser_update_heredoc_indent(p, c);
7633 }
7634#ifdef RIPPER
7635 if (top_of_line && heredoc_end == p->ruby_sourceline) {
7636 pushback(p, c);
7637 break;
7638 }
7639#endif
7640
7641 if (paren && c == paren) {
7642 ++*nest;
7643 }
7644 else if (c == term) {
7645 if (!nest || !*nest) {
7646 pushback(p, c);
7647 break;
7648 }
7649 --*nest;
7650 }
7651 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7652 int c2 = *p->lex.pcur;
7653 if (c2 == '$' || c2 == '@' || c2 == '{') {
7654 pushback(p, c);
7655 break;
7656 }
7657 }
7658 else if (c == '\\') {
7659 c = nextc(p);
7660 switch (c) {
7661 case '\n':
7662 if (func & STR_FUNC_QWORDS) break;
7663 if (func & STR_FUNC_EXPAND) {
7664 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7665 continue;
7666 if (c == term) {
7667 c = '\\';
7668 goto terminate;
7669 }
7670 }
7671 tokadd(p, '\\');
7672 break;
7673
7674 case '\\':
7675 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7676 break;
7677
7678 case 'u':
7679 if ((func & STR_FUNC_EXPAND) == 0) {
7680 tokadd(p, '\\');
7681 break;
7682 }
7683 tokadd_utf8(p, enc, term,
7684 func & STR_FUNC_SYMBOL,
7685 func & STR_FUNC_REGEXP);
7686 continue;
7687
7688 default:
7689 if (c == -1) return -1;
7690 if (!ISASCII(c)) {
7691 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7692 goto non_ascii;
7693 }
7694 if (func & STR_FUNC_REGEXP) {
7695 switch (c) {
7696 case 'c':
7697 case 'C':
7698 case 'M': {
7699 pushback(p, c);
7700 c = read_escape(p, 0, enc);
7701
7702 int i;
7703 char escbuf[5];
7704 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7705 for (i = 0; i < 4; i++) {
7706 tokadd(p, escbuf[i]);
7707 }
7708 continue;
7709 }
7710 }
7711
7712 if (c == term && !simple_re_meta(c)) {
7713 tokadd(p, c);
7714 continue;
7715 }
7716 pushback(p, c);
7717 if ((c = tokadd_escape(p, enc)) < 0)
7718 return -1;
7719 if (*enc && *enc != *encp) {
7720 mixed_escape(p->lex.ptok+2, *enc, *encp);
7721 }
7722 continue;
7723 }
7724 else if (func & STR_FUNC_EXPAND) {
7725 pushback(p, c);
7726 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7727 c = read_escape(p, 0, enc);
7728 }
7729 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7730 /* ignore backslashed spaces in %w */
7731 }
7732 else if (c != term && !(paren && c == paren)) {
7733 tokadd(p, '\\');
7734 pushback(p, c);
7735 continue;
7736 }
7737 }
7738 }
7739 else if (!parser_isascii(p)) {
7740 non_ascii:
7741 if (!*enc) {
7742 *enc = *encp;
7743 }
7744 else if (*enc != *encp) {
7745 mixed_error(*enc, *encp);
7746 continue;
7747 }
7748 if (tokadd_mbchar(p, c) == -1) return -1;
7749 continue;
7750 }
7751 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7752 pushback(p, c);
7753 break;
7754 }
7755 if (c & 0x80) {
7756 if (!*enc) {
7757 *enc = *encp;
7758 }
7759 else if (*enc != *encp) {
7760 mixed_error(*enc, *encp);
7761 continue;
7762 }
7763 }
7764 tokadd(p, c);
7765#ifdef RIPPER
7766 top_of_line = (c == '\n');
7767#endif
7768 }
7769 terminate:
7770 if (*enc) *encp = *enc;
7771 return c;
7772}
7773
7774static inline rb_strterm_t *
7775new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7776{
7777 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7778}
7779
7780/* imemo_parser_strterm for literal */
7781#define NEW_STRTERM(func, term, paren) \
7782 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7783
7784#ifdef RIPPER
7785static void
7786flush_string_content(struct parser_params *p, rb_encoding *enc)
7787{
7788 VALUE content = yylval.val;
7789 if (!ripper_is_node_yylval(content))
7790 content = ripper_new_yylval(p, 0, 0, content);
7791 if (has_delayed_token(p)) {
7792 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7793 if (len > 0) {
7794 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7795 }
7796 dispatch_delayed_token(p, tSTRING_CONTENT);
7797 p->lex.ptok = p->lex.pcur;
7798 RNODE(content)->nd_rval = yylval.val;
7799 }
7800 dispatch_scan_event(p, tSTRING_CONTENT);
7801 if (yylval.val != content)
7802 RNODE(content)->nd_rval = yylval.val;
7803 yylval.val = content;
7804}
7805#else
7806static void
7807flush_string_content(struct parser_params *p, rb_encoding *enc)
7808{
7809 if (has_delayed_token(p)) {
7810 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7811 if (len > 0) {
7812 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7813 p->delayed.end_line = p->ruby_sourceline;
7814 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
7815 }
7816 dispatch_delayed_token(p, tSTRING_CONTENT);
7817 p->lex.ptok = p->lex.pcur;
7818 }
7819 dispatch_scan_event(p, tSTRING_CONTENT);
7820}
7821#endif
7822
7823RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7824/* this can be shared with ripper, since it's independent from struct
7825 * parser_params. */
7826#ifndef RIPPER
7827#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7828#define SPECIAL_PUNCT(idx) ( \
7829 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7830 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7831 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7832 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7833 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7834 BIT('0', idx))
7835const unsigned int ruby_global_name_punct_bits[] = {
7836 SPECIAL_PUNCT(0),
7837 SPECIAL_PUNCT(1),
7838 SPECIAL_PUNCT(2),
7839};
7840#undef BIT
7841#undef SPECIAL_PUNCT
7842#endif
7843
7844static enum yytokentype
7845parser_peek_variable_name(struct parser_params *p)
7846{
7847 int c;
7848 const char *ptr = p->lex.pcur;
7849
7850 if (ptr + 1 >= p->lex.pend) return 0;
7851 c = *ptr++;
7852 switch (c) {
7853 case '$':
7854 if ((c = *ptr) == '-') {
7855 if (++ptr >= p->lex.pend) return 0;
7856 c = *ptr;
7857 }
7858 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7859 return tSTRING_DVAR;
7860 }
7861 break;
7862 case '@':
7863 if ((c = *ptr) == '@') {
7864 if (++ptr >= p->lex.pend) return 0;
7865 c = *ptr;
7866 }
7867 break;
7868 case '{':
7869 p->lex.pcur = ptr;
7870 p->command_start = TRUE;
7871 return tSTRING_DBEG;
7872 default:
7873 return 0;
7874 }
7875 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7876 return tSTRING_DVAR;
7877 return 0;
7878}
7879
7880#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7881#define IS_END() IS_lex_state(EXPR_END_ANY)
7882#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7883#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7884#define IS_LABEL_POSSIBLE() (\
7885 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7886 IS_ARG())
7887#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7888#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7889
7890static inline enum yytokentype
7891parser_string_term(struct parser_params *p, int func)
7892{
7893 p->lex.strterm = 0;
7894 if (func & STR_FUNC_REGEXP) {
7895 set_yylval_num(regx_options(p));
7896 dispatch_scan_event(p, tREGEXP_END);
7897 SET_LEX_STATE(EXPR_END);
7898 return tREGEXP_END;
7899 }
7900 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7901 nextc(p);
7902 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7903 return tLABEL_END;
7904 }
7905 SET_LEX_STATE(EXPR_END);
7906 return tSTRING_END;
7907}
7908
7909static enum yytokentype
7910parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7911{
7912 int func = (int)quote->u1.func;
7913 int term = (int)quote->u3.term;
7914 int paren = (int)quote->u2.paren;
7915 int c, space = 0;
7916 rb_encoding *enc = p->enc;
7917 rb_encoding *base_enc = 0;
7918 VALUE lit;
7919
7920 if (func & STR_FUNC_TERM) {
7921 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7922 SET_LEX_STATE(EXPR_END);
7923 p->lex.strterm = 0;
7924 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7925 }
7926 c = nextc(p);
7927 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7928 do {c = nextc(p);} while (ISSPACE(c));
7929 space = 1;
7930 }
7931 if (func & STR_FUNC_LIST) {
7932 quote->u1.func &= ~STR_FUNC_LIST;
7933 space = 1;
7934 }
7935 if (c == term && !quote->u0.nest) {
7936 if (func & STR_FUNC_QWORDS) {
7937 quote->u1.func |= STR_FUNC_TERM;
7938 pushback(p, c); /* dispatch the term at tSTRING_END */
7939 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7940 return ' ';
7941 }
7942 return parser_string_term(p, func);
7943 }
7944 if (space) {
7945 pushback(p, c);
7946 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7947 return ' ';
7948 }
7949 newtok(p);
7950 if ((func & STR_FUNC_EXPAND) && c == '#') {
7951 int t = parser_peek_variable_name(p);
7952 if (t) return t;
7953 tokadd(p, '#');
7954 c = nextc(p);
7955 }
7956 pushback(p, c);
7957 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7958 &enc, &base_enc) == -1) {
7959 if (p->eofp) {
7960#ifndef RIPPER
7961# define unterminated_literal(mesg) yyerror0(mesg)
7962#else
7963# define unterminated_literal(mesg) compile_error(p, mesg)
7964#endif
7965 literal_flush(p, p->lex.pcur);
7966 if (func & STR_FUNC_QWORDS) {
7967 /* no content to add, bailing out here */
7968 unterminated_literal("unterminated list meets end of file");
7969 p->lex.strterm = 0;
7970 return tSTRING_END;
7971 }
7972 if (func & STR_FUNC_REGEXP) {
7973 unterminated_literal("unterminated regexp meets end of file");
7974 }
7975 else {
7976 unterminated_literal("unterminated string meets end of file");
7977 }
7978 quote->u1.func |= STR_FUNC_TERM;
7979 }
7980 }
7981
7982 tokfix(p);
7983 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7984 set_yylval_str(lit);
7985 flush_string_content(p, enc);
7986
7987 return tSTRING_CONTENT;
7988}
7989
7990static enum yytokentype
7991heredoc_identifier(struct parser_params *p)
7992{
7993 /*
7994 * term_len is length of `<<"END"` except `END`,
7995 * in this case term_len is 4 (<, <, " and ").
7996 */
7997 long len, offset = p->lex.pcur - p->lex.pbeg;
7998 int c = nextc(p), term, func = 0, quote = 0;
7999 enum yytokentype token = tSTRING_BEG;
8000 int indent = 0;
8001
8002 if (c == '-') {
8003 c = nextc(p);
8004 func = STR_FUNC_INDENT;
8005 offset++;
8006 }
8007 else if (c == '~') {
8008 c = nextc(p);
8009 func = STR_FUNC_INDENT;
8010 offset++;
8011 indent = INT_MAX;
8012 }
8013 switch (c) {
8014 case '\'':
8015 func |= str_squote; goto quoted;
8016 case '"':
8017 func |= str_dquote; goto quoted;
8018 case '`':
8019 token = tXSTRING_BEG;
8020 func |= str_xquote; goto quoted;
8021
8022 quoted:
8023 quote++;
8024 offset++;
8025 term = c;
8026 len = 0;
8027 while ((c = nextc(p)) != term) {
8028 if (c == -1 || c == '\r' || c == '\n') {
8029 yyerror0("unterminated here document identifier");
8030 return -1;
8031 }
8032 }
8033 break;
8034
8035 default:
8036 if (!parser_is_identchar(p)) {
8037 pushback(p, c);
8038 if (func & STR_FUNC_INDENT) {
8039 pushback(p, indent > 0 ? '~' : '-');
8040 }
8041 return 0;
8042 }
8043 func |= str_dquote;
8044 do {
8045 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8046 if (n < 0) return 0;
8047 p->lex.pcur += --n;
8048 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8049 pushback(p, c);
8050 break;
8051 }
8052
8053 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8054 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8055 yyerror0("too long here document identifier");
8056 dispatch_scan_event(p, tHEREDOC_BEG);
8057 lex_goto_eol(p);
8058
8059 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
8060 p->lex.strterm->flags |= STRTERM_HEREDOC;
8061 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8062 here->offset = offset;
8063 here->sourceline = p->ruby_sourceline;
8064 here->length = (int)len;
8065 here->quote = quote;
8066 here->func = func;
8067
8068 token_flush(p);
8069 p->heredoc_indent = indent;
8070 p->heredoc_line_indent = 0;
8071 return token;
8072}
8073
8074static void
8075heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8076{
8077 VALUE line;
8078
8079 p->lex.strterm = 0;
8080 line = here->lastline;
8081 p->lex.lastline = line;
8082 p->lex.pbeg = RSTRING_PTR(line);
8083 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8084 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8085 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8086 p->heredoc_end = p->ruby_sourceline;
8087 p->ruby_sourceline = (int)here->sourceline;
8088 if (p->eofp) p->lex.nextline = Qnil;
8089 p->eofp = 0;
8090}
8091
8092static int
8093dedent_string(VALUE string, int width)
8094{
8095 char *str;
8096 long len;
8097 int i, col = 0;
8098
8099 RSTRING_GETMEM(string, str, len);
8100 for (i = 0; i < len && col < width; i++) {
8101 if (str[i] == ' ') {
8102 col++;
8103 }
8104 else if (str[i] == '\t') {
8105 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8106 if (n > width) break;
8107 col = n;
8108 }
8109 else {
8110 break;
8111 }
8112 }
8113 if (!i) return 0;
8114 rb_str_modify(string);
8115 str = RSTRING_PTR(string);
8116 if (RSTRING_LEN(string) != len)
8117 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8118 MEMMOVE(str, str + i, char, len - i);
8119 rb_str_set_len(string, len - i);
8120 return i;
8121}
8122
8123#ifndef RIPPER
8124static NODE *
8125heredoc_dedent(struct parser_params *p, NODE *root)
8126{
8127 NODE *node, *str_node, *prev_node;
8128 int indent = p->heredoc_indent;
8129 VALUE prev_lit = 0;
8130
8131 if (indent <= 0) return root;
8132 p->heredoc_indent = 0;
8133 if (!root) return root;
8134
8135 prev_node = node = str_node = root;
8136 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
8137
8138 while (str_node) {
8139 VALUE lit = str_node->nd_lit;
8140 if (str_node->flags & NODE_FL_NEWLINE) {
8141 dedent_string(lit, indent);
8142 }
8143 if (!prev_lit) {
8144 prev_lit = lit;
8145 }
8146 else if (!literal_concat0(p, prev_lit, lit)) {
8147 return 0;
8148 }
8149 else {
8150 NODE *end = node->nd_end;
8151 node = prev_node->nd_next = node->nd_next;
8152 if (!node) {
8153 if (nd_type_p(prev_node, NODE_DSTR))
8154 nd_set_type(prev_node, NODE_STR);
8155 break;
8156 }
8157 node->nd_end = end;
8158 goto next_str;
8159 }
8160
8161 str_node = 0;
8162 while ((node = (prev_node = node)->nd_next) != 0) {
8163 next_str:
8164 if (!nd_type_p(node, NODE_LIST)) break;
8165 if ((str_node = node->nd_head) != 0) {
8166 enum node_type type = nd_type(str_node);
8167 if (type == NODE_STR || type == NODE_DSTR) break;
8168 prev_lit = 0;
8169 str_node = 0;
8170 }
8171 }
8172 }
8173 return root;
8174}
8175#else /* RIPPER */
8176static VALUE
8177heredoc_dedent(struct parser_params *p, VALUE array)
8178{
8179 int indent = p->heredoc_indent;
8180
8181 if (indent <= 0) return array;
8182 p->heredoc_indent = 0;
8183 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8184 return array;
8185}
8186
8187/*
8188 * call-seq:
8189 * Ripper.dedent_string(input, width) -> Integer
8190 *
8191 * USE OF RIPPER LIBRARY ONLY.
8192 *
8193 * Strips up to +width+ leading whitespaces from +input+,
8194 * and returns the stripped column width.
8195 */
8196static VALUE
8197parser_dedent_string(VALUE self, VALUE input, VALUE width)
8198{
8199 int wid, col;
8200
8201 StringValue(input);
8202 wid = NUM2UINT(width);
8203 col = dedent_string(input, wid);
8204 return INT2NUM(col);
8205}
8206#endif
8207
8208static int
8209whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8210{
8211 const char *ptr = p->lex.pbeg;
8212 long n;
8213
8214 if (indent) {
8215 while (*ptr && ISSPACE(*ptr)) ptr++;
8216 }
8217 n = p->lex.pend - (ptr + len);
8218 if (n < 0) return FALSE;
8219 if (n > 0 && ptr[len] != '\n') {
8220 if (ptr[len] != '\r') return FALSE;
8221 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
8222 }
8223 return strncmp(eos, ptr, len) == 0;
8224}
8225
8226static int
8227word_match_p(struct parser_params *p, const char *word, long len)
8228{
8229 if (strncmp(p->lex.pcur, word, len)) return 0;
8230 if (p->lex.pcur + len == p->lex.pend) return 1;
8231 int c = (unsigned char)p->lex.pcur[len];
8232 if (ISSPACE(c)) return 1;
8233 switch (c) {
8234 case '\0': case '\004': case '\032': return 1;
8235 }
8236 return 0;
8237}
8238
8239#define NUM_SUFFIX_R (1<<0)
8240#define NUM_SUFFIX_I (1<<1)
8241#define NUM_SUFFIX_ALL 3
8242
8243static int
8244number_literal_suffix(struct parser_params *p, int mask)
8245{
8246 int c, result = 0;
8247 const char *lastp = p->lex.pcur;
8248
8249 while ((c = nextc(p)) != -1) {
8250 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8251 result |= (mask & NUM_SUFFIX_I);
8252 mask &= ~NUM_SUFFIX_I;
8253 /* r after i, rational of complex is disallowed */
8254 mask &= ~NUM_SUFFIX_R;
8255 continue;
8256 }
8257 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8258 result |= (mask & NUM_SUFFIX_R);
8259 mask &= ~NUM_SUFFIX_R;
8260 continue;
8261 }
8262 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8263 p->lex.pcur = lastp;
8264 literal_flush(p, p->lex.pcur);
8265 return 0;
8266 }
8267 pushback(p, c);
8268 break;
8269 }
8270 return result;
8271}
8272
8273static enum yytokentype
8274set_number_literal(struct parser_params *p, VALUE v,
8275 enum yytokentype type, int suffix)
8276{
8277 if (suffix & NUM_SUFFIX_I) {
8278 v = rb_complex_raw(INT2FIX(0), v);
8279 type = tIMAGINARY;
8280 }
8281 set_yylval_literal(v);
8282 SET_LEX_STATE(EXPR_END);
8283 return type;
8284}
8285
8286static enum yytokentype
8287set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8288{
8289 enum yytokentype type = tINTEGER;
8290 if (suffix & NUM_SUFFIX_R) {
8291 v = rb_rational_raw1(v);
8292 type = tRATIONAL;
8293 }
8294 return set_number_literal(p, v, type, suffix);
8295}
8296
8297#ifdef RIPPER
8298static void
8299dispatch_heredoc_end(struct parser_params *p)
8300{
8301 VALUE str;
8302 if (has_delayed_token(p))
8303 dispatch_delayed_token(p, tSTRING_CONTENT);
8304 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8305 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8306 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8307 lex_goto_eol(p);
8308 token_flush(p);
8309}
8310
8311#else
8312#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8313static void
8314parser_dispatch_heredoc_end(struct parser_params *p, int line)
8315{
8316 if (has_delayed_token(p))
8317 dispatch_delayed_token(p, tSTRING_CONTENT);
8318
8319 if (p->keep_tokens) {
8320 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8321 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8322 parser_append_tokens(p, str, tHEREDOC_END, line);
8323 }
8324
8325 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8326 lex_goto_eol(p);
8327 token_flush(p);
8328}
8329#endif
8330
8331static enum yytokentype
8332here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8333{
8334 int c, func, indent = 0;
8335 const char *eos, *ptr, *ptr_end;
8336 long len;
8337 VALUE str = 0;
8338 rb_encoding *enc = p->enc;
8339 rb_encoding *base_enc = 0;
8340 int bol;
8341
8342 eos = RSTRING_PTR(here->lastline) + here->offset;
8343 len = here->length;
8344 indent = (func = here->func) & STR_FUNC_INDENT;
8345
8346 if ((c = nextc(p)) == -1) {
8347 error:
8348#ifdef RIPPER
8349 if (!has_delayed_token(p)) {
8350 dispatch_scan_event(p, tSTRING_CONTENT);
8351 }
8352 else {
8353 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
8354 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
8355 int cr = ENC_CODERANGE_UNKNOWN;
8356 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
8357 if (cr != ENC_CODERANGE_7BIT &&
8358 rb_is_usascii_enc(p->enc) &&
8359 enc != rb_utf8_encoding()) {
8360 enc = rb_ascii8bit_encoding();
8361 }
8362 }
8363 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8364 }
8365 dispatch_delayed_token(p, tSTRING_CONTENT);
8366 }
8367 lex_goto_eol(p);
8368#endif
8369 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8370 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
8371 (int)len, eos);
8372 token_flush(p);
8373 p->lex.strterm = 0;
8374 SET_LEX_STATE(EXPR_END);
8375 return tSTRING_END;
8376 }
8377 bol = was_bol(p);
8378 if (!bol) {
8379 /* not beginning of line, cannot be the terminator */
8380 }
8381 else if (p->heredoc_line_indent == -1) {
8382 /* `heredoc_line_indent == -1` means
8383 * - "after an interpolation in the same line", or
8384 * - "in a continuing line"
8385 */
8386 p->heredoc_line_indent = 0;
8387 }
8388 else if (whole_match_p(p, eos, len, indent)) {
8389 dispatch_heredoc_end(p);
8390 restore:
8391 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8392 token_flush(p);
8393 p->lex.strterm = 0;
8394 SET_LEX_STATE(EXPR_END);
8395 return tSTRING_END;
8396 }
8397
8398 if (!(func & STR_FUNC_EXPAND)) {
8399 do {
8400 ptr = RSTRING_PTR(p->lex.lastline);
8401 ptr_end = p->lex.pend;
8402 if (ptr_end > ptr) {
8403 switch (ptr_end[-1]) {
8404 case '\n':
8405 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
8406 ptr_end++;
8407 break;
8408 }
8409 case '\r':
8410 --ptr_end;
8411 }
8412 }
8413
8414 if (p->heredoc_indent > 0) {
8415 long i = 0;
8416 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
8417 i++;
8418 p->heredoc_line_indent = 0;
8419 }
8420
8421 if (str)
8422 rb_str_cat(str, ptr, ptr_end - ptr);
8423 else
8424 str = STR_NEW(ptr, ptr_end - ptr);
8425 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
8426 lex_goto_eol(p);
8427 if (p->heredoc_indent > 0) {
8428 goto flush_str;
8429 }
8430 if (nextc(p) == -1) {
8431 if (str) {
8432 str = 0;
8433 }
8434 goto error;
8435 }
8436 } while (!whole_match_p(p, eos, len, indent));
8437 }
8438 else {
8439 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
8440 newtok(p);
8441 if (c == '#') {
8442 int t = parser_peek_variable_name(p);
8443 if (p->heredoc_line_indent != -1) {
8444 if (p->heredoc_indent > p->heredoc_line_indent) {
8445 p->heredoc_indent = p->heredoc_line_indent;
8446 }
8447 p->heredoc_line_indent = -1;
8448 }
8449 if (t) return t;
8450 tokadd(p, '#');
8451 c = nextc(p);
8452 }
8453 do {
8454 pushback(p, c);
8455 enc = p->enc;
8456 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
8457 if (p->eofp) goto error;
8458 goto restore;
8459 }
8460 if (c != '\n') {
8461 if (c == '\\') p->heredoc_line_indent = -1;
8462 flush:
8463 str = STR_NEW3(tok(p), toklen(p), enc, func);
8464 flush_str:
8465 set_yylval_str(str);
8466#ifndef RIPPER
8467 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8468#endif
8469 flush_string_content(p, enc);
8470 return tSTRING_CONTENT;
8471 }
8472 tokadd(p, nextc(p));
8473 if (p->heredoc_indent > 0) {
8474 lex_goto_eol(p);
8475 goto flush;
8476 }
8477 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8478 if ((c = nextc(p)) == -1) goto error;
8479 } while (!whole_match_p(p, eos, len, indent));
8480 str = STR_NEW3(tok(p), toklen(p), enc, func);
8481 }
8482 dispatch_heredoc_end(p);
8483#ifdef RIPPER
8484 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8485 yylval.val, str);
8486#endif
8487 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8488 token_flush(p);
8489 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8490 set_yylval_str(str);
8491#ifndef RIPPER
8492 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8493#endif
8494 return tSTRING_CONTENT;
8495}
8496
8497#include "lex.c"
8498
8499static int
8500arg_ambiguous(struct parser_params *p, char c)
8501{
8502#ifndef RIPPER
8503 if (c == '/') {
8504 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8505 }
8506 else {
8507 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8508 }
8509#else
8510 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8511#endif
8512 return TRUE;
8513}
8514
8515static ID
8516#ifndef RIPPER
8517formal_argument(struct parser_params *p, ID lhs)
8518#else
8519formal_argument(struct parser_params *p, VALUE lhs)
8520#endif
8521{
8522 ID id = get_id(lhs);
8523
8524 switch (id_type(id)) {
8525 case ID_LOCAL:
8526 break;
8527#ifndef RIPPER
8528# define ERR(mesg) yyerror0(mesg)
8529#else
8530# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8531#endif
8532 case ID_CONST:
8533 ERR("formal argument cannot be a constant");
8534 return 0;
8535 case ID_INSTANCE:
8536 ERR("formal argument cannot be an instance variable");
8537 return 0;
8538 case ID_GLOBAL:
8539 ERR("formal argument cannot be a global variable");
8540 return 0;
8541 case ID_CLASS:
8542 ERR("formal argument cannot be a class variable");
8543 return 0;
8544 default:
8545 ERR("formal argument must be local variable");
8546 return 0;
8547#undef ERR
8548 }
8549 shadowing_lvar(p, id);
8550 return lhs;
8551}
8552
8553static int
8554lvar_defined(struct parser_params *p, ID id)
8555{
8556 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8557}
8558
8559/* emacsen -*- hack */
8560static long
8561parser_encode_length(struct parser_params *p, const char *name, long len)
8562{
8563 long nlen;
8564
8565 if (len > 5 && name[nlen = len - 5] == '-') {
8566 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8567 return nlen;
8568 }
8569 if (len > 4 && name[nlen = len - 4] == '-') {
8570 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8571 return nlen;
8572 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8573 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8574 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8575 return nlen;
8576 }
8577 return len;
8578}
8579
8580static void
8581parser_set_encode(struct parser_params *p, const char *name)
8582{
8583 int idx = rb_enc_find_index(name);
8584 rb_encoding *enc;
8585 VALUE excargs[3];
8586
8587 if (idx < 0) {
8588 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8589 error:
8590 excargs[0] = rb_eArgError;
8591 excargs[2] = rb_make_backtrace();
8592 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8593 rb_exc_raise(rb_make_exception(3, excargs));
8594 }
8595 enc = rb_enc_from_index(idx);
8596 if (!rb_enc_asciicompat(enc)) {
8597 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8598 goto error;
8599 }
8600 p->enc = enc;
8601#ifndef RIPPER
8602 if (p->debug_lines) {
8603 VALUE lines = p->debug_lines;
8604 long i, n = RARRAY_LEN(lines);
8605 for (i = 0; i < n; ++i) {
8606 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8607 }
8608 }
8609#endif
8610}
8611
8612static int
8613comment_at_top(struct parser_params *p)
8614{
8615 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8616 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8617 while (ptr < ptr_end) {
8618 if (!ISSPACE(*ptr)) return 0;
8619 ptr++;
8620 }
8621 return 1;
8622}
8623
8624typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8625typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8626
8627static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8628
8629static void
8630magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8631{
8632 if (!comment_at_top(p)) {
8633 return;
8634 }
8635 parser_set_encode(p, val);
8636}
8637
8638static int
8639parser_get_bool(struct parser_params *p, const char *name, const char *val)
8640{
8641 switch (*val) {
8642 case 't': case 'T':
8643 if (STRCASECMP(val, "true") == 0) {
8644 return TRUE;
8645 }
8646 break;
8647 case 'f': case 'F':
8648 if (STRCASECMP(val, "false") == 0) {
8649 return FALSE;
8650 }
8651 break;
8652 }
8653 return parser_invalid_pragma_value(p, name, val);
8654}
8655
8656static int
8657parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8658{
8659 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8660 return -1;
8661}
8662
8663static void
8664parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8665{
8666 int b = parser_get_bool(p, name, val);
8667 if (b >= 0) p->token_info_enabled = b;
8668}
8669
8670static void
8671parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8672{
8673 int b;
8674
8675 if (p->token_seen) {
8676 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8677 return;
8678 }
8679
8680 b = parser_get_bool(p, name, val);
8681 if (b < 0) return;
8682
8683 if (!p->compile_option)
8684 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8685 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8686 RBOOL(b));
8687}
8688
8689static void
8690parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8691{
8692 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8693 if (*s == ' ' || *s == '\t') continue;
8694 if (*s == '#') break;
8695 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8696 return;
8697 }
8698
8699 switch (*val) {
8700 case 'n': case 'N':
8701 if (STRCASECMP(val, "none") == 0) {
8702 p->ctxt.shareable_constant_value = shareable_none;
8703 return;
8704 }
8705 break;
8706 case 'l': case 'L':
8707 if (STRCASECMP(val, "literal") == 0) {
8708 p->ctxt.shareable_constant_value = shareable_literal;
8709 return;
8710 }
8711 break;
8712 case 'e': case 'E':
8713 if (STRCASECMP(val, "experimental_copy") == 0) {
8714 p->ctxt.shareable_constant_value = shareable_copy;
8715 return;
8716 }
8717 if (STRCASECMP(val, "experimental_everything") == 0) {
8718 p->ctxt.shareable_constant_value = shareable_everything;
8719 return;
8720 }
8721 break;
8722 }
8723 parser_invalid_pragma_value(p, name, val);
8724}
8725
8726# if WARN_PAST_SCOPE
8727static void
8728parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8729{
8730 int b = parser_get_bool(p, name, val);
8731 if (b >= 0) p->past_scope_enabled = b;
8732}
8733# endif
8734
8735struct magic_comment {
8736 const char *name;
8737 rb_magic_comment_setter_t func;
8738 rb_magic_comment_length_t length;
8739};
8740
8741static const struct magic_comment magic_comments[] = {
8742 {"coding", magic_comment_encoding, parser_encode_length},
8743 {"encoding", magic_comment_encoding, parser_encode_length},
8744 {"frozen_string_literal", parser_set_compile_option_flag},
8745 {"shareable_constant_value", parser_set_shareable_constant_value},
8746 {"warn_indent", parser_set_token_info},
8747# if WARN_PAST_SCOPE
8748 {"warn_past_scope", parser_set_past_scope},
8749# endif
8750};
8751
8752static const char *
8753magic_comment_marker(const char *str, long len)
8754{
8755 long i = 2;
8756
8757 while (i < len) {
8758 switch (str[i]) {
8759 case '-':
8760 if (str[i-1] == '*' && str[i-2] == '-') {
8761 return str + i + 1;
8762 }
8763 i += 2;
8764 break;
8765 case '*':
8766 if (i + 1 >= len) return 0;
8767 if (str[i+1] != '-') {
8768 i += 4;
8769 }
8770 else if (str[i-1] != '-') {
8771 i += 2;
8772 }
8773 else {
8774 return str + i + 2;
8775 }
8776 break;
8777 default:
8778 i += 3;
8779 break;
8780 }
8781 }
8782 return 0;
8783}
8784
8785static int
8786parser_magic_comment(struct parser_params *p, const char *str, long len)
8787{
8788 int indicator = 0;
8789 VALUE name = 0, val = 0;
8790 const char *beg, *end, *vbeg, *vend;
8791#define str_copy(_s, _p, _n) ((_s) \
8792 ? (void)(rb_str_resize((_s), (_n)), \
8793 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8794 : (void)((_s) = STR_NEW((_p), (_n))))
8795
8796 if (len <= 7) return FALSE;
8797 if (!!(beg = magic_comment_marker(str, len))) {
8798 if (!(end = magic_comment_marker(beg, str + len - beg)))
8799 return FALSE;
8800 indicator = TRUE;
8801 str = beg;
8802 len = end - beg - 3;
8803 }
8804
8805 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8806 while (len > 0) {
8807 const struct magic_comment *mc = magic_comments;
8808 char *s;
8809 int i;
8810 long n = 0;
8811
8812 for (; len > 0 && *str; str++, --len) {
8813 switch (*str) {
8814 case '\'': case '"': case ':': case ';':
8815 continue;
8816 }
8817 if (!ISSPACE(*str)) break;
8818 }
8819 for (beg = str; len > 0; str++, --len) {
8820 switch (*str) {
8821 case '\'': case '"': case ':': case ';':
8822 break;
8823 default:
8824 if (ISSPACE(*str)) break;
8825 continue;
8826 }
8827 break;
8828 }
8829 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8830 if (!len) break;
8831 if (*str != ':') {
8832 if (!indicator) return FALSE;
8833 continue;
8834 }
8835
8836 do str++; while (--len > 0 && ISSPACE(*str));
8837 if (!len) break;
8838 if (*str == '"') {
8839 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8840 if (*str == '\\') {
8841 --len;
8842 ++str;
8843 }
8844 }
8845 vend = str;
8846 if (len) {
8847 --len;
8848 ++str;
8849 }
8850 }
8851 else {
8852 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8853 vend = str;
8854 }
8855 if (indicator) {
8856 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8857 }
8858 else {
8859 while (len > 0 && (ISSPACE(*str))) --len, str++;
8860 if (len) return FALSE;
8861 }
8862
8863 n = end - beg;
8864 str_copy(name, beg, n);
8865 s = RSTRING_PTR(name);
8866 for (i = 0; i < n; ++i) {
8867 if (s[i] == '-') s[i] = '_';
8868 }
8869 do {
8870 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8871 n = vend - vbeg;
8872 if (mc->length) {
8873 n = (*mc->length)(p, vbeg, n);
8874 }
8875 str_copy(val, vbeg, n);
8876 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8877 break;
8878 }
8879 } while (++mc < magic_comments + numberof(magic_comments));
8880#ifdef RIPPER
8881 str_copy(val, vbeg, vend - vbeg);
8882 dispatch2(magic_comment, name, val);
8883#endif
8884 }
8885
8886 return TRUE;
8887}
8888
8889static void
8890set_file_encoding(struct parser_params *p, const char *str, const char *send)
8891{
8892 int sep = 0;
8893 const char *beg = str;
8894 VALUE s;
8895
8896 for (;;) {
8897 if (send - str <= 6) return;
8898 switch (str[6]) {
8899 case 'C': case 'c': str += 6; continue;
8900 case 'O': case 'o': str += 5; continue;
8901 case 'D': case 'd': str += 4; continue;
8902 case 'I': case 'i': str += 3; continue;
8903 case 'N': case 'n': str += 2; continue;
8904 case 'G': case 'g': str += 1; continue;
8905 case '=': case ':':
8906 sep = 1;
8907 str += 6;
8908 break;
8909 default:
8910 str += 6;
8911 if (ISSPACE(*str)) break;
8912 continue;
8913 }
8914 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8915 sep = 0;
8916 }
8917 for (;;) {
8918 do {
8919 if (++str >= send) return;
8920 } while (ISSPACE(*str));
8921 if (sep) break;
8922 if (*str != '=' && *str != ':') return;
8923 sep = 1;
8924 str++;
8925 }
8926 beg = str;
8927 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8928 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8929 parser_set_encode(p, RSTRING_PTR(s));
8930 rb_str_resize(s, 0);
8931}
8932
8933static void
8934parser_prepare(struct parser_params *p)
8935{
8936 int c = nextc0(p, FALSE);
8937 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8938 switch (c) {
8939 case '#':
8940 if (peek(p, '!')) p->has_shebang = 1;
8941 break;
8942 case 0xef: /* UTF-8 BOM marker */
8943 if (p->lex.pend - p->lex.pcur >= 2 &&
8944 (unsigned char)p->lex.pcur[0] == 0xbb &&
8945 (unsigned char)p->lex.pcur[1] == 0xbf) {
8946 p->enc = rb_utf8_encoding();
8947 p->lex.pcur += 2;
8948#ifndef RIPPER
8949 if (p->debug_lines) {
8950 rb_enc_associate(p->lex.lastline, p->enc);
8951 }
8952#endif
8953 p->lex.pbeg = p->lex.pcur;
8954 return;
8955 }
8956 break;
8957 case EOF:
8958 return;
8959 }
8960 pushback(p, c);
8961 p->enc = rb_enc_get(p->lex.lastline);
8962}
8963
8964#ifndef RIPPER
8965#define ambiguous_operator(tok, op, syn) ( \
8966 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8967 rb_warning0("even though it seems like "syn""))
8968#else
8969#define ambiguous_operator(tok, op, syn) \
8970 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8971#endif
8972#define warn_balanced(tok, op, syn) ((void) \
8973 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8974 space_seen && !ISSPACE(c) && \
8975 (ambiguous_operator(tok, op, syn), 0)), \
8976 (enum yytokentype)(tok))
8977
8978static VALUE
8979parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8980{
8981 VALUE v;
8982 char *point = &str[seen_point];
8983 size_t fraclen = len-seen_point-1;
8984 memmove(point, point+1, fraclen+1);
8985 v = rb_cstr_to_inum(str, 10, FALSE);
8986 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8987}
8988
8989static enum yytokentype
8990no_digits(struct parser_params *p)
8991{
8992 yyerror0("numeric literal without digits");
8993 if (peek(p, '_')) nextc(p);
8994 /* dummy 0, for tUMINUS_NUM at numeric */
8995 return set_integer_literal(p, INT2FIX(0), 0);
8996}
8997
8998static enum yytokentype
8999parse_numeric(struct parser_params *p, int c)
9000{
9001 int is_float, seen_point, seen_e, nondigit;
9002 int suffix;
9003
9004 is_float = seen_point = seen_e = nondigit = 0;
9005 SET_LEX_STATE(EXPR_END);
9006 newtok(p);
9007 if (c == '-' || c == '+') {
9008 tokadd(p, c);
9009 c = nextc(p);
9010 }
9011 if (c == '0') {
9012 int start = toklen(p);
9013 c = nextc(p);
9014 if (c == 'x' || c == 'X') {
9015 /* hexadecimal */
9016 c = nextc(p);
9017 if (c != -1 && ISXDIGIT(c)) {
9018 do {
9019 if (c == '_') {
9020 if (nondigit) break;
9021 nondigit = c;
9022 continue;
9023 }
9024 if (!ISXDIGIT(c)) break;
9025 nondigit = 0;
9026 tokadd(p, c);
9027 } while ((c = nextc(p)) != -1);
9028 }
9029 pushback(p, c);
9030 tokfix(p);
9031 if (toklen(p) == start) {
9032 return no_digits(p);
9033 }
9034 else if (nondigit) goto trailing_uc;
9035 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9036 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9037 }
9038 if (c == 'b' || c == 'B') {
9039 /* binary */
9040 c = nextc(p);
9041 if (c == '0' || c == '1') {
9042 do {
9043 if (c == '_') {
9044 if (nondigit) break;
9045 nondigit = c;
9046 continue;
9047 }
9048 if (c != '0' && c != '1') break;
9049 nondigit = 0;
9050 tokadd(p, c);
9051 } while ((c = nextc(p)) != -1);
9052 }
9053 pushback(p, c);
9054 tokfix(p);
9055 if (toklen(p) == start) {
9056 return no_digits(p);
9057 }
9058 else if (nondigit) goto trailing_uc;
9059 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9060 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9061 }
9062 if (c == 'd' || c == 'D') {
9063 /* decimal */
9064 c = nextc(p);
9065 if (c != -1 && ISDIGIT(c)) {
9066 do {
9067 if (c == '_') {
9068 if (nondigit) break;
9069 nondigit = c;
9070 continue;
9071 }
9072 if (!ISDIGIT(c)) break;
9073 nondigit = 0;
9074 tokadd(p, c);
9075 } while ((c = nextc(p)) != -1);
9076 }
9077 pushback(p, c);
9078 tokfix(p);
9079 if (toklen(p) == start) {
9080 return no_digits(p);
9081 }
9082 else if (nondigit) goto trailing_uc;
9083 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9084 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9085 }
9086 if (c == '_') {
9087 /* 0_0 */
9088 goto octal_number;
9089 }
9090 if (c == 'o' || c == 'O') {
9091 /* prefixed octal */
9092 c = nextc(p);
9093 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9094 return no_digits(p);
9095 }
9096 }
9097 if (c >= '0' && c <= '7') {
9098 /* octal */
9099 octal_number:
9100 do {
9101 if (c == '_') {
9102 if (nondigit) break;
9103 nondigit = c;
9104 continue;
9105 }
9106 if (c < '0' || c > '9') break;
9107 if (c > '7') goto invalid_octal;
9108 nondigit = 0;
9109 tokadd(p, c);
9110 } while ((c = nextc(p)) != -1);
9111 if (toklen(p) > start) {
9112 pushback(p, c);
9113 tokfix(p);
9114 if (nondigit) goto trailing_uc;
9115 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9116 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9117 }
9118 if (nondigit) {
9119 pushback(p, c);
9120 goto trailing_uc;
9121 }
9122 }
9123 if (c > '7' && c <= '9') {
9124 invalid_octal:
9125 yyerror0("Invalid octal digit");
9126 }
9127 else if (c == '.' || c == 'e' || c == 'E') {
9128 tokadd(p, '0');
9129 }
9130 else {
9131 pushback(p, c);
9132 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9133 return set_integer_literal(p, INT2FIX(0), suffix);
9134 }
9135 }
9136
9137 for (;;) {
9138 switch (c) {
9139 case '0': case '1': case '2': case '3': case '4':
9140 case '5': case '6': case '7': case '8': case '9':
9141 nondigit = 0;
9142 tokadd(p, c);
9143 break;
9144
9145 case '.':
9146 if (nondigit) goto trailing_uc;
9147 if (seen_point || seen_e) {
9148 goto decode_num;
9149 }
9150 else {
9151 int c0 = nextc(p);
9152 if (c0 == -1 || !ISDIGIT(c0)) {
9153 pushback(p, c0);
9154 goto decode_num;
9155 }
9156 c = c0;
9157 }
9158 seen_point = toklen(p);
9159 tokadd(p, '.');
9160 tokadd(p, c);
9161 is_float++;
9162 nondigit = 0;
9163 break;
9164
9165 case 'e':
9166 case 'E':
9167 if (nondigit) {
9168 pushback(p, c);
9169 c = nondigit;
9170 goto decode_num;
9171 }
9172 if (seen_e) {
9173 goto decode_num;
9174 }
9175 nondigit = c;
9176 c = nextc(p);
9177 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9178 pushback(p, c);
9179 nondigit = 0;
9180 goto decode_num;
9181 }
9182 tokadd(p, nondigit);
9183 seen_e++;
9184 is_float++;
9185 tokadd(p, c);
9186 nondigit = (c == '-' || c == '+') ? c : 0;
9187 break;
9188
9189 case '_': /* `_' in number just ignored */
9190 if (nondigit) goto decode_num;
9191 nondigit = c;
9192 break;
9193
9194 default:
9195 goto decode_num;
9196 }
9197 c = nextc(p);
9198 }
9199
9200 decode_num:
9201 pushback(p, c);
9202 if (nondigit) {
9203 trailing_uc:
9204 literal_flush(p, p->lex.pcur - 1);
9205 YYLTYPE loc = RUBY_INIT_YYLLOC();
9206 compile_error(p, "trailing `%c' in number", nondigit);
9207 parser_show_error_line(p, &loc);
9208 }
9209 tokfix(p);
9210 if (is_float) {
9211 enum yytokentype type = tFLOAT;
9212 VALUE v;
9213
9214 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9215 if (suffix & NUM_SUFFIX_R) {
9216 type = tRATIONAL;
9217 v = parse_rational(p, tok(p), toklen(p), seen_point);
9218 }
9219 else {
9220 double d = strtod(tok(p), 0);
9221 if (errno == ERANGE) {
9222 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9223 errno = 0;
9224 }
9225 v = DBL2NUM(d);
9226 }
9227 return set_number_literal(p, v, type, suffix);
9228 }
9229 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9230 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9231}
9232
9233static enum yytokentype
9234parse_qmark(struct parser_params *p, int space_seen)
9235{
9236 rb_encoding *enc;
9237 register int c;
9238 VALUE lit;
9239
9240 if (IS_END()) {
9241 SET_LEX_STATE(EXPR_VALUE);
9242 return '?';
9243 }
9244 c = nextc(p);
9245 if (c == -1) {
9246 compile_error(p, "incomplete character syntax");
9247 return 0;
9248 }
9249 if (rb_enc_isspace(c, p->enc)) {
9250 if (!IS_ARG()) {
9251 int c2 = escaped_control_code(c);
9252 if (c2) {
9253 WARN_SPACE_CHAR(c2, "?");
9254 }
9255 }
9256 ternary:
9257 pushback(p, c);
9258 SET_LEX_STATE(EXPR_VALUE);
9259 return '?';
9260 }
9261 newtok(p);
9262 enc = p->enc;
9263 if (!parser_isascii(p)) {
9264 if (tokadd_mbchar(p, c) == -1) return 0;
9265 }
9266 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9267 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
9268 if (space_seen) {
9269 const char *start = p->lex.pcur - 1, *ptr = start;
9270 do {
9271 int n = parser_precise_mbclen(p, ptr);
9272 if (n < 0) return -1;
9273 ptr += n;
9274 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
9275 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9276 " a conditional operator, put a space after `?'",
9277 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9278 }
9279 goto ternary;
9280 }
9281 else if (c == '\\') {
9282 if (peek(p, 'u')) {
9283 nextc(p);
9284 enc = rb_utf8_encoding();
9285 tokadd_utf8(p, &enc, -1, 0, 0);
9286 }
9287 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
9288 nextc(p);
9289 if (tokadd_mbchar(p, c) == -1) return 0;
9290 }
9291 else {
9292 c = read_escape(p, 0, &enc);
9293 tokadd(p, c);
9294 }
9295 }
9296 else {
9297 tokadd(p, c);
9298 }
9299 tokfix(p);
9300 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9301 set_yylval_str(lit);
9302 SET_LEX_STATE(EXPR_END);
9303 return tCHAR;
9304}
9305
9306static enum yytokentype
9307parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9308{
9309 register int c;
9310 const char *ptok = p->lex.pcur;
9311
9312 if (IS_BEG()) {
9313 int term;
9314 int paren;
9315
9316 c = nextc(p);
9317 quotation:
9318 if (c == -1) goto unterminated;
9319 if (!ISALNUM(c)) {
9320 term = c;
9321 if (!ISASCII(c)) goto unknown;
9322 c = 'Q';
9323 }
9324 else {
9325 term = nextc(p);
9326 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9327 unknown:
9328 pushback(p, term);
9329 c = parser_precise_mbclen(p, p->lex.pcur);
9330 if (c < 0) return 0;
9331 p->lex.pcur += c;
9332 yyerror0("unknown type of %string");
9333 return 0;
9334 }
9335 }
9336 if (term == -1) {
9337 unterminated:
9338 compile_error(p, "unterminated quoted string meets end of file");
9339 return 0;
9340 }
9341 paren = term;
9342 if (term == '(') term = ')';
9343 else if (term == '[') term = ']';
9344 else if (term == '{') term = '}';
9345 else if (term == '<') term = '>';
9346 else paren = 0;
9347
9348 p->lex.ptok = ptok-1;
9349 switch (c) {
9350 case 'Q':
9351 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9352 return tSTRING_BEG;
9353
9354 case 'q':
9355 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
9356 return tSTRING_BEG;
9357
9358 case 'W':
9359 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9360 return tWORDS_BEG;
9361
9362 case 'w':
9363 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9364 return tQWORDS_BEG;
9365
9366 case 'I':
9367 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9368 return tSYMBOLS_BEG;
9369
9370 case 'i':
9371 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9372 return tQSYMBOLS_BEG;
9373
9374 case 'x':
9375 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
9376 return tXSTRING_BEG;
9377
9378 case 'r':
9379 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
9380 return tREGEXP_BEG;
9381
9382 case 's':
9383 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
9384 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
9385 return tSYMBEG;
9386
9387 default:
9388 yyerror0("unknown type of %string");
9389 return 0;
9390 }
9391 }
9392 if ((c = nextc(p)) == '=') {
9393 set_yylval_id('%');
9394 SET_LEX_STATE(EXPR_BEG);
9395 return tOP_ASGN;
9396 }
9397 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
9398 goto quotation;
9399 }
9400 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9401 pushback(p, c);
9402 return warn_balanced('%', "%%", "string literal");
9403}
9404
9405static int
9406tokadd_ident(struct parser_params *p, int c)
9407{
9408 do {
9409 if (tokadd_mbchar(p, c) == -1) return -1;
9410 c = nextc(p);
9411 } while (parser_is_identchar(p));
9412 pushback(p, c);
9413 return 0;
9414}
9415
9416static ID
9417tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
9418{
9419 ID ident = TOK_INTERN();
9420
9421 set_yylval_name(ident);
9422
9423 return ident;
9424}
9425
9426static int
9427parse_numvar(struct parser_params *p)
9428{
9429 size_t len;
9430 int overflow;
9431 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
9432 const unsigned long nth_ref_max =
9433 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
9434 /* NTH_REF is left-shifted to be ORed with back-ref flag and
9435 * turned into a Fixnum, in compile.c */
9436
9437 if (overflow || n > nth_ref_max) {
9438 /* compile_error()? */
9439 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
9440 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
9441 }
9442 else {
9443 return (int)n;
9444 }
9445}
9446
9447static enum yytokentype
9448parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
9449{
9450 const char *ptr = p->lex.pcur;
9451 register int c;
9452
9453 SET_LEX_STATE(EXPR_END);
9454 p->lex.ptok = ptr - 1; /* from '$' */
9455 newtok(p);
9456 c = nextc(p);
9457 switch (c) {
9458 case '_': /* $_: last read line string */
9459 c = nextc(p);
9460 if (parser_is_identchar(p)) {
9461 tokadd(p, '$');
9462 tokadd(p, '_');
9463 break;
9464 }
9465 pushback(p, c);
9466 c = '_';
9467 /* fall through */
9468 case '~': /* $~: match-data */
9469 case '*': /* $*: argv */
9470 case '$': /* $$: pid */
9471 case '?': /* $?: last status */
9472 case '!': /* $!: error string */
9473 case '@': /* $@: error position */
9474 case '/': /* $/: input record separator */
9475 case '\\': /* $\: output record separator */
9476 case ';': /* $;: field separator */
9477 case ',': /* $,: output field separator */
9478 case '.': /* $.: last read line number */
9479 case '=': /* $=: ignorecase */
9480 case ':': /* $:: load path */
9481 case '<': /* $<: reading filename */
9482 case '>': /* $>: default output handle */
9483 case '\"': /* $": already loaded files */
9484 tokadd(p, '$');
9485 tokadd(p, c);
9486 goto gvar;
9487
9488 case '-':
9489 tokadd(p, '$');
9490 tokadd(p, c);
9491 c = nextc(p);
9492 if (parser_is_identchar(p)) {
9493 if (tokadd_mbchar(p, c) == -1) return 0;
9494 }
9495 else {
9496 pushback(p, c);
9497 pushback(p, '-');
9498 return '$';
9499 }
9500 gvar:
9501 set_yylval_name(TOK_INTERN());
9502 return tGVAR;
9503
9504 case '&': /* $&: last match */
9505 case '`': /* $`: string before last match */
9506 case '\'': /* $': string after last match */
9507 case '+': /* $+: string matches last paren. */
9508 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9509 tokadd(p, '$');
9510 tokadd(p, c);
9511 goto gvar;
9512 }
9513 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9514 return tBACK_REF;
9515
9516 case '1': case '2': case '3':
9517 case '4': case '5': case '6':
9518 case '7': case '8': case '9':
9519 tokadd(p, '$');
9520 do {
9521 tokadd(p, c);
9522 c = nextc(p);
9523 } while (c != -1 && ISDIGIT(c));
9524 pushback(p, c);
9525 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9526 tokfix(p);
9527 c = parse_numvar(p);
9528 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9529 return tNTH_REF;
9530
9531 default:
9532 if (!parser_is_identchar(p)) {
9533 YYLTYPE loc = RUBY_INIT_YYLLOC();
9534 if (c == -1 || ISSPACE(c)) {
9535 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9536 }
9537 else {
9538 pushback(p, c);
9539 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9540 }
9541 parser_show_error_line(p, &loc);
9542 set_yylval_noname();
9543 return tGVAR;
9544 }
9545 /* fall through */
9546 case '0':
9547 tokadd(p, '$');
9548 }
9549
9550 if (tokadd_ident(p, c)) return 0;
9551 SET_LEX_STATE(EXPR_END);
9552 tokenize_ident(p, last_state);
9553 return tGVAR;
9554}
9555
9556#ifndef RIPPER
9557static bool
9558parser_numbered_param(struct parser_params *p, int n)
9559{
9560 if (n < 0) return false;
9561
9562 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9563 return false;
9564 }
9565 if (p->max_numparam == ORDINAL_PARAM) {
9566 compile_error(p, "ordinary parameter is defined");
9567 return false;
9568 }
9569 struct vtable *args = p->lvtbl->args;
9570 if (p->max_numparam < n) {
9571 p->max_numparam = n;
9572 }
9573 while (n > args->pos) {
9574 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9575 }
9576 return true;
9577}
9578#endif
9579
9580static enum yytokentype
9581parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9582{
9583 const char *ptr = p->lex.pcur;
9584 enum yytokentype result = tIVAR;
9585 register int c = nextc(p);
9586 YYLTYPE loc;
9587
9588 p->lex.ptok = ptr - 1; /* from '@' */
9589 newtok(p);
9590 tokadd(p, '@');
9591 if (c == '@') {
9592 result = tCVAR;
9593 tokadd(p, '@');
9594 c = nextc(p);
9595 }
9596 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9597 if (c == -1 || !parser_is_identchar(p)) {
9598 pushback(p, c);
9599 RUBY_SET_YYLLOC(loc);
9600 if (result == tIVAR) {
9601 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9602 }
9603 else {
9604 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9605 }
9606 parser_show_error_line(p, &loc);
9607 set_yylval_noname();
9608 SET_LEX_STATE(EXPR_END);
9609 return result;
9610 }
9611 else if (ISDIGIT(c)) {
9612 pushback(p, c);
9613 RUBY_SET_YYLLOC(loc);
9614 if (result == tIVAR) {
9615 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9616 }
9617 else {
9618 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9619 }
9620 parser_show_error_line(p, &loc);
9621 set_yylval_noname();
9622 SET_LEX_STATE(EXPR_END);
9623 return result;
9624 }
9625
9626 if (tokadd_ident(p, c)) return 0;
9627 tokenize_ident(p, last_state);
9628 return result;
9629}
9630
9631static enum yytokentype
9632parse_ident(struct parser_params *p, int c, int cmd_state)
9633{
9634 enum yytokentype result;
9635 int mb = ENC_CODERANGE_7BIT;
9636 const enum lex_state_e last_state = p->lex.state;
9637 ID ident;
9638 int enforce_keyword_end = 0;
9639
9640 do {
9641 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9642 if (tokadd_mbchar(p, c) == -1) return 0;
9643 c = nextc(p);
9644 } while (parser_is_identchar(p));
9645 if ((c == '!' || c == '?') && !peek(p, '=')) {
9646 result = tFID;
9647 tokadd(p, c);
9648 }
9649 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9650 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9651 result = tIDENTIFIER;
9652 tokadd(p, c);
9653 }
9654 else {
9655 result = tCONSTANT; /* assume provisionally */
9656 pushback(p, c);
9657 }
9658 tokfix(p);
9659
9660 if (IS_LABEL_POSSIBLE()) {
9661 if (IS_LABEL_SUFFIX(0)) {
9662 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9663 nextc(p);
9664 set_yylval_name(TOK_INTERN());
9665 return tLABEL;
9666 }
9667 }
9668
9669#ifndef RIPPER
9670 if (!NIL_P(peek_end_expect_token_locations(p))) {
9671 VALUE end_loc;
9672 int lineno, column;
9673 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
9674
9675 end_loc = peek_end_expect_token_locations(p);
9676 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
9677 column = NUM2INT(rb_ary_entry(end_loc, 1));
9678
9679 if (p->debug) {
9680 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
9681 p->ruby_sourceline, beg_pos, lineno, column);
9682 }
9683
9684 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
9685 const struct kwtable *kw;
9686
9687 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
9688 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
9689 enforce_keyword_end = 1;
9690 }
9691 }
9692 }
9693#endif
9694
9695 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
9696 const struct kwtable *kw;
9697
9698 /* See if it is a reserved word. */
9699 kw = rb_reserved_word(tok(p), toklen(p));
9700 if (kw) {
9701 enum lex_state_e state = p->lex.state;
9702 if (IS_lex_state_for(state, EXPR_FNAME)) {
9703 SET_LEX_STATE(EXPR_ENDFN);
9704 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9705 return kw->id[0];
9706 }
9707 SET_LEX_STATE(kw->state);
9708 if (IS_lex_state(EXPR_BEG)) {
9709 p->command_start = TRUE;
9710 }
9711 if (kw->id[0] == keyword_do) {
9712 if (lambda_beginning_p()) {
9713 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9714 return keyword_do_LAMBDA;
9715 }
9716 if (COND_P()) return keyword_do_cond;
9717 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9718 return keyword_do_block;
9719 return keyword_do;
9720 }
9721 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
9722 return kw->id[0];
9723 else {
9724 if (kw->id[0] != kw->id[1])
9725 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9726 return kw->id[1];
9727 }
9728 }
9729 }
9730
9731 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9732 if (cmd_state) {
9733 SET_LEX_STATE(EXPR_CMDARG);
9734 }
9735 else {
9736 SET_LEX_STATE(EXPR_ARG);
9737 }
9738 }
9739 else if (p->lex.state == EXPR_FNAME) {
9740 SET_LEX_STATE(EXPR_ENDFN);
9741 }
9742 else {
9743 SET_LEX_STATE(EXPR_END);
9744 }
9745
9746 ident = tokenize_ident(p, last_state);
9747 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9748 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9749 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9750 lvar_defined(p, ident)) {
9751 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9752 }
9753 return result;
9754}
9755
9756static void
9757warn_cr(struct parser_params *p)
9758{
9759 if (!p->cr_seen) {
9760 p->cr_seen = TRUE;
9761 /* carried over with p->lex.nextline for nextc() */
9762 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9763 }
9764}
9765
9766static enum yytokentype
9767parser_yylex(struct parser_params *p)
9768{
9769 register int c;
9770 int space_seen = 0;
9771 int cmd_state;
9772 int label;
9773 enum lex_state_e last_state;
9774 int fallthru = FALSE;
9775 int token_seen = p->token_seen;
9776
9777 if (p->lex.strterm) {
9778 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9779 token_flush(p);
9780 return here_document(p, &p->lex.strterm->u.heredoc);
9781 }
9782 else {
9783 token_flush(p);
9784 return parse_string(p, &p->lex.strterm->u.literal);
9785 }
9786 }
9787 cmd_state = p->command_start;
9788 p->command_start = FALSE;
9789 p->token_seen = TRUE;
9790#ifndef RIPPER
9791 token_flush(p);
9792#endif
9793 retry:
9794 last_state = p->lex.state;
9795 switch (c = nextc(p)) {
9796 case '\0': /* NUL */
9797 case '\004': /* ^D */
9798 case '\032': /* ^Z */
9799 case -1: /* end of script. */
9800 p->eofp = 1;
9801#ifndef RIPPER
9802 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
9803 pop_end_expect_token_locations(p);
9804 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
9805 return tDUMNY_END;
9806 }
9807#endif
9808 /* Set location for end-of-input because dispatch_scan_event is not called. */
9809 RUBY_SET_YYLLOC(*p->yylloc);
9810 return 0;
9811
9812 /* white spaces */
9813 case '\r':
9814 warn_cr(p);
9815 /* fall through */
9816 case ' ': case '\t': case '\f':
9817 case '\13': /* '\v' */
9818 space_seen = 1;
9819 while ((c = nextc(p))) {
9820 switch (c) {
9821 case '\r':
9822 warn_cr(p);
9823 /* fall through */
9824 case ' ': case '\t': case '\f':
9825 case '\13': /* '\v' */
9826 break;
9827 default:
9828 goto outofloop;
9829 }
9830 }
9831 outofloop:
9832 pushback(p, c);
9833 dispatch_scan_event(p, tSP);
9834#ifndef RIPPER
9835 token_flush(p);
9836#endif
9837 goto retry;
9838
9839 case '#': /* it's a comment */
9840 p->token_seen = token_seen;
9841 /* no magic_comment in shebang line */
9842 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9843 if (comment_at_top(p)) {
9844 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9845 }
9846 }
9847 lex_goto_eol(p);
9848 dispatch_scan_event(p, tCOMMENT);
9849 fallthru = TRUE;
9850 /* fall through */
9851 case '\n':
9852 p->token_seen = token_seen;
9853 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9854 !IS_lex_state(EXPR_LABELED));
9855 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9856 if (!fallthru) {
9857 dispatch_scan_event(p, tIGNORED_NL);
9858 }
9859 fallthru = FALSE;
9860 if (!c && p->ctxt.in_kwarg) {
9861 goto normal_newline;
9862 }
9863 goto retry;
9864 }
9865 while (1) {
9866 switch (c = nextc(p)) {
9867 case ' ': case '\t': case '\f': case '\r':
9868 case '\13': /* '\v' */
9869 space_seen = 1;
9870 break;
9871 case '#':
9872 pushback(p, c);
9873 if (space_seen) {
9874 dispatch_scan_event(p, tSP);
9875 token_flush(p);
9876 }
9877 goto retry;
9878 case '&':
9879 case '.': {
9880 dispatch_delayed_token(p, tIGNORED_NL);
9881 if (peek(p, '.') == (c == '&')) {
9882 pushback(p, c);
9883 dispatch_scan_event(p, tSP);
9884 goto retry;
9885 }
9886 }
9887 default:
9888 p->ruby_sourceline--;
9889 p->lex.nextline = p->lex.lastline;
9890 case -1: /* EOF no decrement*/
9891 lex_goto_eol(p);
9892 if (c != -1) {
9893 p->lex.ptok = p->lex.pcur;
9894 }
9895 goto normal_newline;
9896 }
9897 }
9898 normal_newline:
9899 p->command_start = TRUE;
9900 SET_LEX_STATE(EXPR_BEG);
9901 return '\n';
9902
9903 case '*':
9904 if ((c = nextc(p)) == '*') {
9905 if ((c = nextc(p)) == '=') {
9906 set_yylval_id(idPow);
9907 SET_LEX_STATE(EXPR_BEG);
9908 return tOP_ASGN;
9909 }
9910 pushback(p, c);
9911 if (IS_SPCARG(c)) {
9912 rb_warning0("`**' interpreted as argument prefix");
9913 c = tDSTAR;
9914 }
9915 else if (IS_BEG()) {
9916 c = tDSTAR;
9917 }
9918 else {
9919 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9920 }
9921 }
9922 else {
9923 if (c == '=') {
9924 set_yylval_id('*');
9925 SET_LEX_STATE(EXPR_BEG);
9926 return tOP_ASGN;
9927 }
9928 pushback(p, c);
9929 if (IS_SPCARG(c)) {
9930 rb_warning0("`*' interpreted as argument prefix");
9931 c = tSTAR;
9932 }
9933 else if (IS_BEG()) {
9934 c = tSTAR;
9935 }
9936 else {
9937 c = warn_balanced('*', "*", "argument prefix");
9938 }
9939 }
9940 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9941 return c;
9942
9943 case '!':
9944 c = nextc(p);
9945 if (IS_AFTER_OPERATOR()) {
9946 SET_LEX_STATE(EXPR_ARG);
9947 if (c == '@') {
9948 return '!';
9949 }
9950 }
9951 else {
9952 SET_LEX_STATE(EXPR_BEG);
9953 }
9954 if (c == '=') {
9955 return tNEQ;
9956 }
9957 if (c == '~') {
9958 return tNMATCH;
9959 }
9960 pushback(p, c);
9961 return '!';
9962
9963 case '=':
9964 if (was_bol(p)) {
9965 /* skip embedded rd document */
9966 if (word_match_p(p, "begin", 5)) {
9967 int first_p = TRUE;
9968
9969 lex_goto_eol(p);
9970 dispatch_scan_event(p, tEMBDOC_BEG);
9971 for (;;) {
9972 lex_goto_eol(p);
9973 if (!first_p) {
9974 dispatch_scan_event(p, tEMBDOC);
9975 }
9976 first_p = FALSE;
9977 c = nextc(p);
9978 if (c == -1) {
9979 compile_error(p, "embedded document meets end of file");
9980 return 0;
9981 }
9982 if (c == '=' && word_match_p(p, "end", 3)) {
9983 break;
9984 }
9985 pushback(p, c);
9986 }
9987 lex_goto_eol(p);
9988 dispatch_scan_event(p, tEMBDOC_END);
9989 goto retry;
9990 }
9991 }
9992
9993 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9994 if ((c = nextc(p)) == '=') {
9995 if ((c = nextc(p)) == '=') {
9996 return tEQQ;
9997 }
9998 pushback(p, c);
9999 return tEQ;
10000 }
10001 if (c == '~') {
10002 return tMATCH;
10003 }
10004 else if (c == '>') {
10005 return tASSOC;
10006 }
10007 pushback(p, c);
10008 return '=';
10009
10010 case '<':
10011 c = nextc(p);
10012 if (c == '<' &&
10013 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10014 !IS_END() &&
10015 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10016 int token = heredoc_identifier(p);
10017 if (token) return token < 0 ? 0 : token;
10018 }
10019 if (IS_AFTER_OPERATOR()) {
10020 SET_LEX_STATE(EXPR_ARG);
10021 }
10022 else {
10023 if (IS_lex_state(EXPR_CLASS))
10024 p->command_start = TRUE;
10025 SET_LEX_STATE(EXPR_BEG);
10026 }
10027 if (c == '=') {
10028 if ((c = nextc(p)) == '>') {
10029 return tCMP;
10030 }
10031 pushback(p, c);
10032 return tLEQ;
10033 }
10034 if (c == '<') {
10035 if ((c = nextc(p)) == '=') {
10036 set_yylval_id(idLTLT);
10037 SET_LEX_STATE(EXPR_BEG);
10038 return tOP_ASGN;
10039 }
10040 pushback(p, c);
10041 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10042 }
10043 pushback(p, c);
10044 return '<';
10045
10046 case '>':
10047 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10048 if ((c = nextc(p)) == '=') {
10049 return tGEQ;
10050 }
10051 if (c == '>') {
10052 if ((c = nextc(p)) == '=') {
10053 set_yylval_id(idGTGT);
10054 SET_LEX_STATE(EXPR_BEG);
10055 return tOP_ASGN;
10056 }
10057 pushback(p, c);
10058 return tRSHFT;
10059 }
10060 pushback(p, c);
10061 return '>';
10062
10063 case '"':
10064 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10065 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10066 p->lex.ptok = p->lex.pcur-1;
10067 return tSTRING_BEG;
10068
10069 case '`':
10070 if (IS_lex_state(EXPR_FNAME)) {
10071 SET_LEX_STATE(EXPR_ENDFN);
10072 return c;
10073 }
10074 if (IS_lex_state(EXPR_DOT)) {
10075 if (cmd_state)
10076 SET_LEX_STATE(EXPR_CMDARG);
10077 else
10078 SET_LEX_STATE(EXPR_ARG);
10079 return c;
10080 }
10081 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10082 return tXSTRING_BEG;
10083
10084 case '\'':
10085 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10086 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10087 p->lex.ptok = p->lex.pcur-1;
10088 return tSTRING_BEG;
10089
10090 case '?':
10091 return parse_qmark(p, space_seen);
10092
10093 case '&':
10094 if ((c = nextc(p)) == '&') {
10095 SET_LEX_STATE(EXPR_BEG);
10096 if ((c = nextc(p)) == '=') {
10097 set_yylval_id(idANDOP);
10098 SET_LEX_STATE(EXPR_BEG);
10099 return tOP_ASGN;
10100 }
10101 pushback(p, c);
10102 return tANDOP;
10103 }
10104 else if (c == '=') {
10105 set_yylval_id('&');
10106 SET_LEX_STATE(EXPR_BEG);
10107 return tOP_ASGN;
10108 }
10109 else if (c == '.') {
10110 set_yylval_id(idANDDOT);
10111 SET_LEX_STATE(EXPR_DOT);
10112 return tANDDOT;
10113 }
10114 pushback(p, c);
10115 if (IS_SPCARG(c)) {
10116 if ((c != ':') ||
10117 (c = peekc_n(p, 1)) == -1 ||
10118 !(c == '\'' || c == '"' ||
10119 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
10120 rb_warning0("`&' interpreted as argument prefix");
10121 }
10122 c = tAMPER;
10123 }
10124 else if (IS_BEG()) {
10125 c = tAMPER;
10126 }
10127 else {
10128 c = warn_balanced('&', "&", "argument prefix");
10129 }
10130 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10131 return c;
10132
10133 case '|':
10134 if ((c = nextc(p)) == '|') {
10135 SET_LEX_STATE(EXPR_BEG);
10136 if ((c = nextc(p)) == '=') {
10137 set_yylval_id(idOROP);
10138 SET_LEX_STATE(EXPR_BEG);
10139 return tOP_ASGN;
10140 }
10141 pushback(p, c);
10142 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10143 c = '|';
10144 pushback(p, '|');
10145 return c;
10146 }
10147 return tOROP;
10148 }
10149 if (c == '=') {
10150 set_yylval_id('|');
10151 SET_LEX_STATE(EXPR_BEG);
10152 return tOP_ASGN;
10153 }
10154 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10155 pushback(p, c);
10156 return '|';
10157
10158 case '+':
10159 c = nextc(p);
10160 if (IS_AFTER_OPERATOR()) {
10161 SET_LEX_STATE(EXPR_ARG);
10162 if (c == '@') {
10163 return tUPLUS;
10164 }
10165 pushback(p, c);
10166 return '+';
10167 }
10168 if (c == '=') {
10169 set_yylval_id('+');
10170 SET_LEX_STATE(EXPR_BEG);
10171 return tOP_ASGN;
10172 }
10173 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10174 SET_LEX_STATE(EXPR_BEG);
10175 pushback(p, c);
10176 if (c != -1 && ISDIGIT(c)) {
10177 return parse_numeric(p, '+');
10178 }
10179 return tUPLUS;
10180 }
10181 SET_LEX_STATE(EXPR_BEG);
10182 pushback(p, c);
10183 return warn_balanced('+', "+", "unary operator");
10184
10185 case '-':
10186 c = nextc(p);
10187 if (IS_AFTER_OPERATOR()) {
10188 SET_LEX_STATE(EXPR_ARG);
10189 if (c == '@') {
10190 return tUMINUS;
10191 }
10192 pushback(p, c);
10193 return '-';
10194 }
10195 if (c == '=') {
10196 set_yylval_id('-');
10197 SET_LEX_STATE(EXPR_BEG);
10198 return tOP_ASGN;
10199 }
10200 if (c == '>') {
10201 SET_LEX_STATE(EXPR_ENDFN);
10202 return tLAMBDA;
10203 }
10204 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10205 SET_LEX_STATE(EXPR_BEG);
10206 pushback(p, c);
10207 if (c != -1 && ISDIGIT(c)) {
10208 return tUMINUS_NUM;
10209 }
10210 return tUMINUS;
10211 }
10212 SET_LEX_STATE(EXPR_BEG);
10213 pushback(p, c);
10214 return warn_balanced('-', "-", "unary operator");
10215
10216 case '.': {
10217 int is_beg = IS_BEG();
10218 SET_LEX_STATE(EXPR_BEG);
10219 if ((c = nextc(p)) == '.') {
10220 if ((c = nextc(p)) == '.') {
10221 if (p->ctxt.in_argdef) {
10222 SET_LEX_STATE(EXPR_ENDARG);
10223 return tBDOT3;
10224 }
10225 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10226 rb_warn0("... at EOL, should be parenthesized?");
10227 }
10228 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10229 if (IS_lex_state_for(last_state, EXPR_LABEL))
10230 return tDOT3;
10231 }
10232 return is_beg ? tBDOT3 : tDOT3;
10233 }
10234 pushback(p, c);
10235 return is_beg ? tBDOT2 : tDOT2;
10236 }
10237 pushback(p, c);
10238 if (c != -1 && ISDIGIT(c)) {
10239 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10240 parse_numeric(p, '.');
10241 if (ISDIGIT(prev)) {
10242 yyerror0("unexpected fraction part after numeric literal");
10243 }
10244 else {
10245 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10246 }
10247 SET_LEX_STATE(EXPR_END);
10248 p->lex.ptok = p->lex.pcur;
10249 goto retry;
10250 }
10251 set_yylval_id('.');
10252 SET_LEX_STATE(EXPR_DOT);
10253 return '.';
10254 }
10255
10256 case '0': case '1': case '2': case '3': case '4':
10257 case '5': case '6': case '7': case '8': case '9':
10258 return parse_numeric(p, c);
10259
10260 case ')':
10261 COND_POP();
10262 CMDARG_POP();
10263 SET_LEX_STATE(EXPR_ENDFN);
10264 p->lex.paren_nest--;
10265 return c;
10266
10267 case ']':
10268 COND_POP();
10269 CMDARG_POP();
10270 SET_LEX_STATE(EXPR_END);
10271 p->lex.paren_nest--;
10272 return c;
10273
10274 case '}':
10275 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10276 if (!p->lex.brace_nest--) return tSTRING_DEND;
10277 COND_POP();
10278 CMDARG_POP();
10279 SET_LEX_STATE(EXPR_END);
10280 p->lex.paren_nest--;
10281 return c;
10282
10283 case ':':
10284 c = nextc(p);
10285 if (c == ':') {
10286 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10287 SET_LEX_STATE(EXPR_BEG);
10288 return tCOLON3;
10289 }
10290 set_yylval_id(idCOLON2);
10291 SET_LEX_STATE(EXPR_DOT);
10292 return tCOLON2;
10293 }
10294 if (IS_END() || ISSPACE(c) || c == '#') {
10295 pushback(p, c);
10296 c = warn_balanced(':', ":", "symbol literal");
10297 SET_LEX_STATE(EXPR_BEG);
10298 return c;
10299 }
10300 switch (c) {
10301 case '\'':
10302 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10303 break;
10304 case '"':
10305 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10306 break;
10307 default:
10308 pushback(p, c);
10309 break;
10310 }
10311 SET_LEX_STATE(EXPR_FNAME);
10312 return tSYMBEG;
10313
10314 case '/':
10315 if (IS_BEG()) {
10316 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10317 return tREGEXP_BEG;
10318 }
10319 if ((c = nextc(p)) == '=') {
10320 set_yylval_id('/');
10321 SET_LEX_STATE(EXPR_BEG);
10322 return tOP_ASGN;
10323 }
10324 pushback(p, c);
10325 if (IS_SPCARG(c)) {
10326 arg_ambiguous(p, '/');
10327 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10328 return tREGEXP_BEG;
10329 }
10330 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10331 return warn_balanced('/', "/", "regexp literal");
10332
10333 case '^':
10334 if ((c = nextc(p)) == '=') {
10335 set_yylval_id('^');
10336 SET_LEX_STATE(EXPR_BEG);
10337 return tOP_ASGN;
10338 }
10339 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10340 pushback(p, c);
10341 return '^';
10342
10343 case ';':
10344 SET_LEX_STATE(EXPR_BEG);
10345 p->command_start = TRUE;
10346 return ';';
10347
10348 case ',':
10349 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10350 return ',';
10351
10352 case '~':
10353 if (IS_AFTER_OPERATOR()) {
10354 if ((c = nextc(p)) != '@') {
10355 pushback(p, c);
10356 }
10357 SET_LEX_STATE(EXPR_ARG);
10358 }
10359 else {
10360 SET_LEX_STATE(EXPR_BEG);
10361 }
10362 return '~';
10363
10364 case '(':
10365 if (IS_BEG()) {
10366 c = tLPAREN;
10367 }
10368 else if (!space_seen) {
10369 /* foo( ... ) => method call, no ambiguity */
10370 }
10371 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
10372 c = tLPAREN_ARG;
10373 }
10374 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
10375 rb_warning0("parentheses after method name is interpreted as "
10376 "an argument list, not a decomposed argument");
10377 }
10378 p->lex.paren_nest++;
10379 COND_PUSH(0);
10380 CMDARG_PUSH(0);
10381 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10382 return c;
10383
10384 case '[':
10385 p->lex.paren_nest++;
10386 if (IS_AFTER_OPERATOR()) {
10387 if ((c = nextc(p)) == ']') {
10388 p->lex.paren_nest--;
10389 SET_LEX_STATE(EXPR_ARG);
10390 if ((c = nextc(p)) == '=') {
10391 return tASET;
10392 }
10393 pushback(p, c);
10394 return tAREF;
10395 }
10396 pushback(p, c);
10397 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
10398 return '[';
10399 }
10400 else if (IS_BEG()) {
10401 c = tLBRACK;
10402 }
10403 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
10404 c = tLBRACK;
10405 }
10406 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10407 COND_PUSH(0);
10408 CMDARG_PUSH(0);
10409 return c;
10410
10411 case '{':
10412 ++p->lex.brace_nest;
10413 if (lambda_beginning_p())
10414 c = tLAMBEG;
10415 else if (IS_lex_state(EXPR_LABELED))
10416 c = tLBRACE; /* hash */
10417 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
10418 c = '{'; /* block (primary) */
10419 else if (IS_lex_state(EXPR_ENDARG))
10420 c = tLBRACE_ARG; /* block (expr) */
10421 else
10422 c = tLBRACE; /* hash */
10423 if (c != tLBRACE) {
10424 p->command_start = TRUE;
10425 SET_LEX_STATE(EXPR_BEG);
10426 }
10427 else {
10428 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10429 }
10430 ++p->lex.paren_nest; /* after lambda_beginning_p() */
10431 COND_PUSH(0);
10432 CMDARG_PUSH(0);
10433 return c;
10434
10435 case '\\':
10436 c = nextc(p);
10437 if (c == '\n') {
10438 space_seen = 1;
10439 dispatch_scan_event(p, tSP);
10440 goto retry; /* skip \\n */
10441 }
10442 if (c == ' ') return tSP;
10443 if (ISSPACE(c)) return c;
10444 pushback(p, c);
10445 return '\\';
10446
10447 case '%':
10448 return parse_percent(p, space_seen, last_state);
10449
10450 case '$':
10451 return parse_gvar(p, last_state);
10452
10453 case '@':
10454 return parse_atmark(p, last_state);
10455
10456 case '_':
10457 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
10458 p->ruby__end__seen = 1;
10459 p->eofp = 1;
10460#ifndef RIPPER
10461 return -1;
10462#else
10463 lex_goto_eol(p);
10464 dispatch_scan_event(p, k__END__);
10465 return 0;
10466#endif
10467 }
10468 newtok(p);
10469 break;
10470
10471 default:
10472 if (!parser_is_identchar(p)) {
10473 compile_error(p, "Invalid char `\\x%02X' in expression", c);
10474 token_flush(p);
10475 goto retry;
10476 }
10477
10478 newtok(p);
10479 break;
10480 }
10481
10482 return parse_ident(p, c, cmd_state);
10483}
10484
10485static enum yytokentype
10486yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
10487{
10488 enum yytokentype t;
10489
10490 p->lval = lval;
10491 lval->val = Qundef;
10492 p->yylloc = yylloc;
10493
10494 t = parser_yylex(p);
10495
10496 if (has_delayed_token(p))
10497 dispatch_delayed_token(p, t);
10498 else if (t != 0)
10499 dispatch_scan_event(p, t);
10500
10501 return t;
10502}
10503
10504#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
10505
10506static NODE*
10507node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
10508{
10509 NODE *n = rb_ast_newnode(p->ast, type);
10510
10511 rb_node_init(n, type, a0, a1, a2);
10512
10513 nd_set_loc(n, loc);
10514 nd_set_node_id(n, parser_get_node_id(p));
10515 return n;
10516}
10517
10518static NODE *
10519nd_set_loc(NODE *nd, const YYLTYPE *loc)
10520{
10521 nd->nd_loc = *loc;
10522 nd_set_line(nd, loc->beg_pos.lineno);
10523 return nd;
10524}
10525
10526#ifndef RIPPER
10527static enum node_type
10528nodetype(NODE *node) /* for debug */
10529{
10530 return (enum node_type)nd_type(node);
10531}
10532
10533static int
10534nodeline(NODE *node)
10535{
10536 return nd_line(node);
10537}
10538
10539static NODE*
10540newline_node(NODE *node)
10541{
10542 if (node) {
10543 node = remove_begin(node);
10544 node->flags |= NODE_FL_NEWLINE;
10545 }
10546 return node;
10547}
10548
10549static void
10550fixpos(NODE *node, NODE *orig)
10551{
10552 if (!node) return;
10553 if (!orig) return;
10554 nd_set_line(node, nd_line(orig));
10555}
10556
10557static void
10558parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10559{
10560 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10561}
10562
10563static void
10564parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10565{
10566 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10567}
10568
10569static NODE*
10570block_append(struct parser_params *p, NODE *head, NODE *tail)
10571{
10572 NODE *end, *h = head, *nd;
10573
10574 if (tail == 0) return head;
10575
10576 if (h == 0) return tail;
10577 switch (nd_type(h)) {
10578 case NODE_LIT:
10579 case NODE_STR:
10580 case NODE_SELF:
10581 case NODE_TRUE:
10582 case NODE_FALSE:
10583 case NODE_NIL:
10584 parser_warning(p, h, "unused literal ignored");
10585 return tail;
10586 default:
10587 h = end = NEW_BLOCK(head, &head->nd_loc);
10588 end->nd_end = end;
10589 head = end;
10590 break;
10591 case NODE_BLOCK:
10592 end = h->nd_end;
10593 break;
10594 }
10595
10596 nd = end->nd_head;
10597 switch (nd_type(nd)) {
10598 case NODE_RETURN:
10599 case NODE_BREAK:
10600 case NODE_NEXT:
10601 case NODE_REDO:
10602 case NODE_RETRY:
10603 if (RTEST(ruby_verbose)) {
10604 parser_warning(p, tail, "statement not reached");
10605 }
10606 break;
10607
10608 default:
10609 break;
10610 }
10611
10612 if (!nd_type_p(tail, NODE_BLOCK)) {
10613 tail = NEW_BLOCK(tail, &tail->nd_loc);
10614 tail->nd_end = tail;
10615 }
10616 end->nd_next = tail;
10617 h->nd_end = tail->nd_end;
10618 nd_set_last_loc(head, nd_last_loc(tail));
10619 return head;
10620}
10621
10622/* append item to the list */
10623static NODE*
10624list_append(struct parser_params *p, NODE *list, NODE *item)
10625{
10626 NODE *last;
10627
10628 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10629 if (list->nd_next) {
10630 last = list->nd_next->nd_end;
10631 }
10632 else {
10633 last = list;
10634 }
10635
10636 list->nd_alen += 1;
10637 last->nd_next = NEW_LIST(item, &item->nd_loc);
10638 list->nd_next->nd_end = last->nd_next;
10639
10640 nd_set_last_loc(list, nd_last_loc(item));
10641
10642 return list;
10643}
10644
10645/* concat two lists */
10646static NODE*
10647list_concat(NODE *head, NODE *tail)
10648{
10649 NODE *last;
10650
10651 if (head->nd_next) {
10652 last = head->nd_next->nd_end;
10653 }
10654 else {
10655 last = head;
10656 }
10657
10658 head->nd_alen += tail->nd_alen;
10659 last->nd_next = tail;
10660 if (tail->nd_next) {
10661 head->nd_next->nd_end = tail->nd_next->nd_end;
10662 }
10663 else {
10664 head->nd_next->nd_end = tail;
10665 }
10666
10667 nd_set_last_loc(head, nd_last_loc(tail));
10668
10669 return head;
10670}
10671
10672static int
10673literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10674{
10675 if (NIL_P(tail)) return 1;
10676 if (!rb_enc_compatible(head, tail)) {
10677 compile_error(p, "string literal encodings differ (%s / %s)",
10678 rb_enc_name(rb_enc_get(head)),
10679 rb_enc_name(rb_enc_get(tail)));
10680 rb_str_resize(head, 0);
10681 rb_str_resize(tail, 0);
10682 return 0;
10683 }
10684 rb_str_buf_append(head, tail);
10685 return 1;
10686}
10687
10688static VALUE
10689string_literal_head(enum node_type htype, NODE *head)
10690{
10691 if (htype != NODE_DSTR) return Qfalse;
10692 if (head->nd_next) {
10693 head = head->nd_next->nd_end->nd_head;
10694 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10695 }
10696 const VALUE lit = head->nd_lit;
10697 ASSUME(lit != Qfalse);
10698 return lit;
10699}
10700
10701/* concat two string literals */
10702static NODE *
10703literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10704{
10705 enum node_type htype;
10706 VALUE lit;
10707
10708 if (!head) return tail;
10709 if (!tail) return head;
10710
10711 htype = nd_type(head);
10712 if (htype == NODE_EVSTR) {
10713 head = new_dstr(p, head, loc);
10714 htype = NODE_DSTR;
10715 }
10716 if (p->heredoc_indent > 0) {
10717 switch (htype) {
10718 case NODE_STR:
10719 nd_set_type(head, NODE_DSTR);
10720 case NODE_DSTR:
10721 return list_append(p, head, tail);
10722 default:
10723 break;
10724 }
10725 }
10726 switch (nd_type(tail)) {
10727 case NODE_STR:
10728 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10729 htype = NODE_STR;
10730 }
10731 else {
10732 lit = head->nd_lit;
10733 }
10734 if (htype == NODE_STR) {
10735 if (!literal_concat0(p, lit, tail->nd_lit)) {
10736 error:
10737 rb_discard_node(p, head);
10738 rb_discard_node(p, tail);
10739 return 0;
10740 }
10741 rb_discard_node(p, tail);
10742 }
10743 else {
10744 list_append(p, head, tail);
10745 }
10746 break;
10747
10748 case NODE_DSTR:
10749 if (htype == NODE_STR) {
10750 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10751 goto error;
10752 tail->nd_lit = head->nd_lit;
10753 rb_discard_node(p, head);
10754 head = tail;
10755 }
10756 else if (NIL_P(tail->nd_lit)) {
10757 append:
10758 head->nd_alen += tail->nd_alen - 1;
10759 if (!head->nd_next) {
10760 head->nd_next = tail->nd_next;
10761 }
10762 else if (tail->nd_next) {
10763 head->nd_next->nd_end->nd_next = tail->nd_next;
10764 head->nd_next->nd_end = tail->nd_next->nd_end;
10765 }
10766 rb_discard_node(p, tail);
10767 }
10768 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10769 if (!literal_concat0(p, lit, tail->nd_lit))
10770 goto error;
10771 tail->nd_lit = Qnil;
10772 goto append;
10773 }
10774 else {
10775 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10776 }
10777 break;
10778
10779 case NODE_EVSTR:
10780 if (htype == NODE_STR) {
10781 nd_set_type(head, NODE_DSTR);
10782 head->nd_alen = 1;
10783 }
10784 list_append(p, head, tail);
10785 break;
10786 }
10787 return head;
10788}
10789
10790static NODE *
10791evstr2dstr(struct parser_params *p, NODE *node)
10792{
10793 if (nd_type_p(node, NODE_EVSTR)) {
10794 node = new_dstr(p, node, &node->nd_loc);
10795 }
10796 return node;
10797}
10798
10799static NODE *
10800new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10801{
10802 NODE *head = node;
10803
10804 if (node) {
10805 switch (nd_type(node)) {
10806 case NODE_STR:
10807 nd_set_type(node, NODE_DSTR);
10808 return node;
10809 case NODE_DSTR:
10810 break;
10811 case NODE_EVSTR:
10812 return node;
10813 }
10814 }
10815 return NEW_EVSTR(head, loc);
10816}
10817
10818static NODE *
10819new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10820{
10821 VALUE lit = STR_NEW0();
10822 NODE *dstr = NEW_DSTR(lit, loc);
10823 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10824 return list_append(p, dstr, node);
10825}
10826
10827static NODE *
10828call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10829 const YYLTYPE *op_loc, const YYLTYPE *loc)
10830{
10831 NODE *expr;
10832 value_expr(recv);
10833 value_expr(arg1);
10834 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10835 nd_set_line(expr, op_loc->beg_pos.lineno);
10836 return expr;
10837}
10838
10839static NODE *
10840call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10841{
10842 NODE *opcall;
10843 value_expr(recv);
10844 opcall = NEW_OPCALL(recv, id, 0, loc);
10845 nd_set_line(opcall, op_loc->beg_pos.lineno);
10846 return opcall;
10847}
10848
10849static NODE *
10850new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10851{
10852 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10853 nd_set_line(qcall, op_loc->beg_pos.lineno);
10854 return qcall;
10855}
10856
10857static NODE*
10858new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10859{
10860 NODE *ret;
10861 if (block) block_dup_check(p, args, block);
10862 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10863 if (block) ret = method_add_block(p, ret, block, loc);
10864 fixpos(ret, recv);
10865 return ret;
10866}
10867
10868#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10869static NODE*
10870match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10871{
10872 NODE *n;
10873 int line = op_loc->beg_pos.lineno;
10874
10875 value_expr(node1);
10876 value_expr(node2);
10877 if (node1 && (n = nd_once_body(node1)) != 0) {
10878 switch (nd_type(n)) {
10879 case NODE_DREGX:
10880 {
10881 NODE *match = NEW_MATCH2(node1, node2, loc);
10882 nd_set_line(match, line);
10883 return match;
10884 }
10885
10886 case NODE_LIT:
10887 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10888 const VALUE lit = n->nd_lit;
10889 NODE *match = NEW_MATCH2(node1, node2, loc);
10890 match->nd_args = reg_named_capture_assign(p, lit, loc);
10891 nd_set_line(match, line);
10892 return match;
10893 }
10894 }
10895 }
10896
10897 if (node2 && (n = nd_once_body(node2)) != 0) {
10898 NODE *match3;
10899
10900 switch (nd_type(n)) {
10901 case NODE_LIT:
10902 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10903 /* fallthru */
10904 case NODE_DREGX:
10905 match3 = NEW_MATCH3(node2, node1, loc);
10906 return match3;
10907 }
10908 }
10909
10910 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10911 nd_set_line(n, line);
10912 return n;
10913}
10914
10915# if WARN_PAST_SCOPE
10916static int
10917past_dvar_p(struct parser_params *p, ID id)
10918{
10919 struct vtable *past = p->lvtbl->past;
10920 while (past) {
10921 if (vtable_included(past, id)) return 1;
10922 past = past->prev;
10923 }
10924 return 0;
10925}
10926# endif
10927
10928static int
10929numparam_nested_p(struct parser_params *p)
10930{
10931 struct local_vars *local = p->lvtbl;
10932 NODE *outer = local->numparam.outer;
10933 NODE *inner = local->numparam.inner;
10934 if (outer || inner) {
10935 NODE *used = outer ? outer : inner;
10936 compile_error(p, "numbered parameter is already used in\n"
10937 "%s:%d: %s block here",
10938 p->ruby_sourcefile, nd_line(used),
10939 outer ? "outer" : "inner");
10940 parser_show_error_line(p, &used->nd_loc);
10941 return 1;
10942 }
10943 return 0;
10944}
10945
10946static NODE*
10947gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10948{
10949 ID *vidp = NULL;
10950 NODE *node;
10951 switch (id) {
10952 case keyword_self:
10953 return NEW_SELF(loc);
10954 case keyword_nil:
10955 return NEW_NIL(loc);
10956 case keyword_true:
10957 return NEW_TRUE(loc);
10958 case keyword_false:
10959 return NEW_FALSE(loc);
10960 case keyword__FILE__:
10961 {
10962 VALUE file = p->ruby_sourcefile_string;
10963 if (NIL_P(file))
10964 file = rb_str_new(0, 0);
10965 else
10966 file = rb_str_dup(file);
10967 node = NEW_STR(file, loc);
10968 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10969 }
10970 return node;
10971 case keyword__LINE__:
10972 return NEW_LIT(INT2FIX(p->tokline), loc);
10973 case keyword__ENCODING__:
10974 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10975 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10976 return node;
10977
10978 }
10979 switch (id_type(id)) {
10980 case ID_LOCAL:
10981 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10982 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10983 if (id == p->cur_arg) {
10984 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10985 return 0;
10986 }
10987 if (vidp) *vidp |= LVAR_USED;
10988 node = NEW_DVAR(id, loc);
10989 return node;
10990 }
10991 if (local_id_ref(p, id, &vidp)) {
10992 if (id == p->cur_arg) {
10993 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10994 return 0;
10995 }
10996 if (vidp) *vidp |= LVAR_USED;
10997 node = NEW_LVAR(id, loc);
10998 return node;
10999 }
11000 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
11001 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
11002 if (numparam_nested_p(p)) return 0;
11003 node = NEW_DVAR(id, loc);
11004 struct local_vars *local = p->lvtbl;
11005 if (!local->numparam.current) local->numparam.current = node;
11006 return node;
11007 }
11008# if WARN_PAST_SCOPE
11009 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
11010 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
11011 }
11012# endif
11013 /* method call without arguments */
11014 return NEW_VCALL(id, loc);
11015 case ID_GLOBAL:
11016 return NEW_GVAR(id, loc);
11017 case ID_INSTANCE:
11018 return NEW_IVAR(id, loc);
11019 case ID_CONST:
11020 return NEW_CONST(id, loc);
11021 case ID_CLASS:
11022 return NEW_CVAR(id, loc);
11023 }
11024 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11025 return 0;
11026}
11027
11028static NODE *
11029opt_arg_append(NODE *opt_list, NODE *opt)
11030{
11031 NODE *opts = opt_list;
11032 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11033
11034 while (opts->nd_next) {
11035 opts = opts->nd_next;
11036 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11037 }
11038 opts->nd_next = opt;
11039
11040 return opt_list;
11041}
11042
11043static NODE *
11044kwd_append(NODE *kwlist, NODE *kw)
11045{
11046 if (kwlist) {
11047 opt_arg_append(kwlist, kw);
11048 }
11049 return kwlist;
11050}
11051
11052static NODE *
11053new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
11054{
11055 return NEW_DEFINED(remove_begin_all(expr), loc);
11056}
11057
11058static NODE*
11059symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
11060{
11061 enum node_type type = nd_type(symbol);
11062 switch (type) {
11063 case NODE_DSTR:
11064 nd_set_type(symbol, NODE_DSYM);
11065 break;
11066 case NODE_STR:
11067 nd_set_type(symbol, NODE_LIT);
11068 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
11069 break;
11070 default:
11071 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
11072 }
11073 return list_append(p, symbols, symbol);
11074}
11075
11076static NODE *
11077new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
11078{
11079 NODE *list, *prev;
11080 VALUE lit;
11081
11082 if (!node) {
11083 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
11084 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11085 return node;
11086 }
11087 switch (nd_type(node)) {
11088 case NODE_STR:
11089 {
11090 VALUE src = node->nd_lit;
11091 nd_set_type(node, NODE_LIT);
11092 nd_set_loc(node, loc);
11093 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11094 }
11095 break;
11096 default:
11097 lit = STR_NEW0();
11098 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
11099 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11100 /* fall through */
11101 case NODE_DSTR:
11102 nd_set_type(node, NODE_DREGX);
11103 nd_set_loc(node, loc);
11104 node->nd_cflag = options & RE_OPTION_MASK;
11105 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
11106 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
11107 NODE *frag = list->nd_head;
11108 enum node_type type = nd_type(frag);
11109 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
11110 VALUE tail = frag->nd_lit;
11111 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
11112 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
11113 if (!literal_concat0(p, lit, tail)) {
11114 return NEW_NIL(loc); /* dummy node on error */
11115 }
11116 rb_str_resize(tail, 0);
11117 prev->nd_next = list->nd_next;
11118 rb_discard_node(p, list->nd_head);
11119 rb_discard_node(p, list);
11120 list = prev;
11121 }
11122 else {
11123 prev = list;
11124 }
11125 }
11126 else {
11127 prev = 0;
11128 }
11129 }
11130 if (!node->nd_next) {
11131 VALUE src = node->nd_lit;
11132 nd_set_type(node, NODE_LIT);
11133 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11134 }
11135 if (options & RE_OPTION_ONCE) {
11136 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
11137 }
11138 break;
11139 }
11140 return node;
11141}
11142
11143static NODE *
11144new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
11145{
11146 if (!k) return 0;
11147 return NEW_KW_ARG(0, (k), loc);
11148}
11149
11150static NODE *
11151new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11152{
11153 if (!node) {
11154 VALUE lit = STR_NEW0();
11155 NODE *xstr = NEW_XSTR(lit, loc);
11156 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11157 return xstr;
11158 }
11159 switch (nd_type(node)) {
11160 case NODE_STR:
11161 nd_set_type(node, NODE_XSTR);
11162 nd_set_loc(node, loc);
11163 break;
11164 case NODE_DSTR:
11165 nd_set_type(node, NODE_DXSTR);
11166 nd_set_loc(node, loc);
11167 break;
11168 default:
11169 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
11170 break;
11171 }
11172 return node;
11173}
11174
11175static void
11176check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
11177{
11178 VALUE lit;
11179
11180 if (!arg || !p->case_labels) return;
11181
11182 lit = rb_node_case_when_optimizable_literal(arg);
11183 if (UNDEF_P(lit)) return;
11184 if (nd_type_p(arg, NODE_STR)) {
11185 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
11186 }
11187
11188 if (NIL_P(p->case_labels)) {
11189 p->case_labels = rb_obj_hide(rb_hash_new());
11190 }
11191 else {
11192 VALUE line = rb_hash_lookup(p->case_labels, lit);
11193 if (!NIL_P(line)) {
11194 rb_warning1("duplicated `when' clause with line %d is ignored",
11195 WARN_IVAL(line));
11196 return;
11197 }
11198 }
11199 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
11200}
11201
11202#else /* !RIPPER */
11203static int
11204id_is_var(struct parser_params *p, ID id)
11205{
11206 if (is_notop_id(id)) {
11207 switch (id & ID_SCOPE_MASK) {
11208 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
11209 return 1;
11210 case ID_LOCAL:
11211 if (dyna_in_block(p)) {
11212 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
11213 }
11214 if (local_id(p, id)) return 1;
11215 /* method call without arguments */
11216 return 0;
11217 }
11218 }
11219 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11220 return 0;
11221}
11222
11223static VALUE
11224new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
11225{
11226 VALUE src = 0, err;
11227 int options = 0;
11228 if (ripper_is_node_yylval(re)) {
11229 src = RNODE(re)->nd_cval;
11230 re = RNODE(re)->nd_rval;
11231 }
11232 if (ripper_is_node_yylval(opt)) {
11233 options = (int)RNODE(opt)->nd_tag;
11234 opt = RNODE(opt)->nd_rval;
11235 }
11236 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
11237 compile_error(p, "%"PRIsVALUE, err);
11238 }
11239 return dispatch2(regexp_literal, re, opt);
11240}
11241#endif /* !RIPPER */
11242
11243static inline enum lex_state_e
11244parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
11245{
11246 if (p->debug) {
11247 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
11248 }
11249 return p->lex.state = ls;
11250}
11251
11252#ifndef RIPPER
11253static const char rb_parser_lex_state_names[][8] = {
11254 "BEG", "END", "ENDARG", "ENDFN", "ARG",
11255 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
11256 "LABEL", "LABELED","FITEM",
11257};
11258
11259static VALUE
11260append_lex_state_name(enum lex_state_e state, VALUE buf)
11261{
11262 int i, sep = 0;
11263 unsigned int mask = 1;
11264 static const char none[] = "NONE";
11265
11266 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
11267 if ((unsigned)state & mask) {
11268 if (sep) {
11269 rb_str_cat(buf, "|", 1);
11270 }
11271 sep = 1;
11272 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
11273 }
11274 }
11275 if (!sep) {
11276 rb_str_cat(buf, none, sizeof(none)-1);
11277 }
11278 return buf;
11279}
11280
11281static void
11282flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
11283{
11284 VALUE mesg = p->debug_buffer;
11285
11286 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
11287 p->debug_buffer = Qnil;
11288 rb_io_puts(1, &mesg, out);
11289 }
11290 if (!NIL_P(str) && RSTRING_LEN(str)) {
11291 rb_io_write(p->debug_output, str);
11292 }
11293}
11294
11295enum lex_state_e
11296rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
11297 enum lex_state_e to, int line)
11298{
11299 VALUE mesg;
11300 mesg = rb_str_new_cstr("lex_state: ");
11301 append_lex_state_name(from, mesg);
11302 rb_str_cat_cstr(mesg, " -> ");
11303 append_lex_state_name(to, mesg);
11304 rb_str_catf(mesg, " at line %d\n", line);
11305 flush_debug_buffer(p, p->debug_output, mesg);
11306 return to;
11307}
11308
11309VALUE
11310rb_parser_lex_state_name(enum lex_state_e state)
11311{
11312 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
11313}
11314
11315static void
11316append_bitstack_value(stack_type stack, VALUE mesg)
11317{
11318 if (stack == 0) {
11319 rb_str_cat_cstr(mesg, "0");
11320 }
11321 else {
11322 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
11323 for (; mask && !(stack & mask); mask >>= 1) continue;
11324 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
11325 }
11326}
11327
11328void
11329rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
11330 const char *name, int line)
11331{
11332 VALUE mesg = rb_sprintf("%s: ", name);
11333 append_bitstack_value(stack, mesg);
11334 rb_str_catf(mesg, " at line %d\n", line);
11335 flush_debug_buffer(p, p->debug_output, mesg);
11336}
11337
11338void
11339rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
11340{
11341 va_list ap;
11342 VALUE mesg = rb_str_new_cstr("internal parser error: ");
11343
11344 va_start(ap, fmt);
11345 rb_str_vcatf(mesg, fmt, ap);
11346 va_end(ap);
11347 yyerror0(RSTRING_PTR(mesg));
11348 RB_GC_GUARD(mesg);
11349
11350 mesg = rb_str_new(0, 0);
11351 append_lex_state_name(p->lex.state, mesg);
11352 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
11353 rb_str_resize(mesg, 0);
11354 append_bitstack_value(p->cond_stack, mesg);
11355 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
11356 rb_str_resize(mesg, 0);
11357 append_bitstack_value(p->cmdarg_stack, mesg);
11358 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
11359 if (p->debug_output == rb_ractor_stdout())
11360 p->debug_output = rb_ractor_stderr();
11361 p->debug = TRUE;
11362}
11363
11364static YYLTYPE *
11365rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
11366{
11367 yylloc->beg_pos.lineno = sourceline;
11368 yylloc->beg_pos.column = beg_pos;
11369 yylloc->end_pos.lineno = sourceline;
11370 yylloc->end_pos.column = end_pos;
11371 return yylloc;
11372}
11373
11374YYLTYPE *
11375rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
11376{
11377 int sourceline = here->sourceline;
11378 int beg_pos = (int)here->offset - here->quote
11379 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
11380 int end_pos = (int)here->offset + here->length + here->quote;
11381
11382 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11383}
11384
11385YYLTYPE *
11386rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
11387{
11388 yylloc->beg_pos.lineno = p->delayed.beg_line;
11389 yylloc->beg_pos.column = p->delayed.beg_col;
11390 yylloc->end_pos.lineno = p->delayed.end_line;
11391 yylloc->end_pos.column = p->delayed.end_col;
11392
11393 return yylloc;
11394}
11395
11396YYLTYPE *
11397rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
11398{
11399 int sourceline = p->ruby_sourceline;
11400 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11401 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
11402 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11403}
11404
11405YYLTYPE *
11406rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
11407{
11408 yylloc->end_pos = yylloc->beg_pos;
11409
11410 return yylloc;
11411}
11412
11413YYLTYPE *
11414rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
11415{
11416 int sourceline = p->ruby_sourceline;
11417 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11418 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
11419 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11420}
11421
11422YYLTYPE *
11423rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
11424{
11425 int sourceline = p->ruby_sourceline;
11426 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11427 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
11428 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11429}
11430#endif /* !RIPPER */
11431
11432static int
11433assignable0(struct parser_params *p, ID id, const char **err)
11434{
11435 if (!id) return -1;
11436 switch (id) {
11437 case keyword_self:
11438 *err = "Can't change the value of self";
11439 return -1;
11440 case keyword_nil:
11441 *err = "Can't assign to nil";
11442 return -1;
11443 case keyword_true:
11444 *err = "Can't assign to true";
11445 return -1;
11446 case keyword_false:
11447 *err = "Can't assign to false";
11448 return -1;
11449 case keyword__FILE__:
11450 *err = "Can't assign to __FILE__";
11451 return -1;
11452 case keyword__LINE__:
11453 *err = "Can't assign to __LINE__";
11454 return -1;
11455 case keyword__ENCODING__:
11456 *err = "Can't assign to __ENCODING__";
11457 return -1;
11458 }
11459 switch (id_type(id)) {
11460 case ID_LOCAL:
11461 if (dyna_in_block(p)) {
11462 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
11463 compile_error(p, "Can't assign to numbered parameter _%d",
11464 NUMPARAM_ID_TO_IDX(id));
11465 return -1;
11466 }
11467 if (dvar_curr(p, id)) return NODE_DASGN;
11468 if (dvar_defined(p, id)) return NODE_DASGN;
11469 if (local_id(p, id)) return NODE_LASGN;
11470 dyna_var(p, id);
11471 return NODE_DASGN;
11472 }
11473 else {
11474 if (!local_id(p, id)) local_var(p, id);
11475 return NODE_LASGN;
11476 }
11477 break;
11478 case ID_GLOBAL: return NODE_GASGN;
11479 case ID_INSTANCE: return NODE_IASGN;
11480 case ID_CONST:
11481 if (!p->ctxt.in_def) return NODE_CDECL;
11482 *err = "dynamic constant assignment";
11483 return -1;
11484 case ID_CLASS: return NODE_CVASGN;
11485 default:
11486 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
11487 }
11488 return -1;
11489}
11490
11491#ifndef RIPPER
11492static NODE*
11493assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
11494{
11495 const char *err = 0;
11496 int node_type = assignable0(p, id, &err);
11497 switch (node_type) {
11498 case NODE_DASGN: return NEW_DASGN(id, val, loc);
11499 case NODE_LASGN: return NEW_LASGN(id, val, loc);
11500 case NODE_GASGN: return NEW_GASGN(id, val, loc);
11501 case NODE_IASGN: return NEW_IASGN(id, val, loc);
11502 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
11503 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
11504 }
11505 if (err) yyerror1(loc, err);
11506 return NEW_BEGIN(0, loc);
11507}
11508#else
11509static VALUE
11510assignable(struct parser_params *p, VALUE lhs)
11511{
11512 const char *err = 0;
11513 assignable0(p, get_id(lhs), &err);
11514 if (err) lhs = assign_error(p, err, lhs);
11515 return lhs;
11516}
11517#endif
11518
11519static int
11520is_private_local_id(ID name)
11521{
11522 VALUE s;
11523 if (name == idUScore) return 1;
11524 if (!is_local_id(name)) return 0;
11525 s = rb_id2str(name);
11526 if (!s) return 0;
11527 return RSTRING_PTR(s)[0] == '_';
11528}
11529
11530static int
11531shadowing_lvar_0(struct parser_params *p, ID name)
11532{
11533 if (dyna_in_block(p)) {
11534 if (dvar_curr(p, name)) {
11535 if (is_private_local_id(name)) return 1;
11536 yyerror0("duplicated argument name");
11537 }
11538 else if (dvar_defined(p, name) || local_id(p, name)) {
11539 vtable_add(p->lvtbl->vars, name);
11540 if (p->lvtbl->used) {
11541 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11542 }
11543 return 0;
11544 }
11545 }
11546 else {
11547 if (local_id(p, name)) {
11548 if (is_private_local_id(name)) return 1;
11549 yyerror0("duplicated argument name");
11550 }
11551 }
11552 return 1;
11553}
11554
11555static ID
11556shadowing_lvar(struct parser_params *p, ID name)
11557{
11558 shadowing_lvar_0(p, name);
11559 return name;
11560}
11561
11562static void
11563new_bv(struct parser_params *p, ID name)
11564{
11565 if (!name) return;
11566 if (!is_local_id(name)) {
11567 compile_error(p, "invalid local variable - %"PRIsVALUE,
11568 rb_id2str(name));
11569 return;
11570 }
11571 if (!shadowing_lvar_0(p, name)) return;
11572 dyna_var(p, name);
11573}
11574
11575#ifndef RIPPER
11576static NODE *
11577aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11578{
11579 return NEW_ATTRASGN(recv, tASET, idx, loc);
11580}
11581
11582static void
11583block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11584{
11585 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11586 compile_error(p, "both block arg and actual block given");
11587 }
11588}
11589
11590static NODE *
11591attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11592{
11593 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11594 return NEW_ATTRASGN(recv, id, 0, loc);
11595}
11596
11597static void
11598rb_backref_error(struct parser_params *p, NODE *node)
11599{
11600 switch (nd_type(node)) {
11601 case NODE_NTH_REF:
11602 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11603 break;
11604 case NODE_BACK_REF:
11605 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11606 break;
11607 }
11608}
11609#else
11610static VALUE
11611backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11612{
11613 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11614 rb_str_append(mesg, ref->nd_cval);
11615 return dispatch2(assign_error, mesg, expr);
11616}
11617#endif
11618
11619#ifndef RIPPER
11620static NODE *
11621arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11622{
11623 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11624 switch (nd_type(node1)) {
11625 case NODE_LIST:
11626 return list_append(p, node1, node2);
11627 case NODE_BLOCK_PASS:
11628 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11629 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11630 return node1;
11631 case NODE_ARGSPUSH:
11632 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11633 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11634 nd_set_type(node1, NODE_ARGSCAT);
11635 return node1;
11636 case NODE_ARGSCAT:
11637 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11638 node1->nd_body = list_append(p, node1->nd_body, node2);
11639 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11640 return node1;
11641 }
11642 return NEW_ARGSPUSH(node1, node2, loc);
11643}
11644
11645static NODE *
11646arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11647{
11648 if (!node2) return node1;
11649 switch (nd_type(node1)) {
11650 case NODE_BLOCK_PASS:
11651 if (node1->nd_head)
11652 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11653 else
11654 node1->nd_head = NEW_LIST(node2, loc);
11655 return node1;
11656 case NODE_ARGSPUSH:
11657 if (!nd_type_p(node2, NODE_LIST)) break;
11658 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11659 nd_set_type(node1, NODE_ARGSCAT);
11660 return node1;
11661 case NODE_ARGSCAT:
11662 if (!nd_type_p(node2, NODE_LIST) ||
11663 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11664 node1->nd_body = list_concat(node1->nd_body, node2);
11665 return node1;
11666 }
11667 return NEW_ARGSCAT(node1, node2, loc);
11668}
11669
11670static NODE *
11671last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11672{
11673 NODE *n1;
11674 if ((n1 = splat_array(args)) != 0) {
11675 return list_append(p, n1, last_arg);
11676 }
11677 return arg_append(p, args, last_arg, loc);
11678}
11679
11680static NODE *
11681rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11682{
11683 NODE *n1;
11684 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11685 return list_concat(n1, rest_arg);
11686 }
11687 return arg_concat(p, args, rest_arg, loc);
11688}
11689
11690static NODE *
11691splat_array(NODE* node)
11692{
11693 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11694 if (nd_type_p(node, NODE_LIST)) return node;
11695 return 0;
11696}
11697
11698static void
11699mark_lvar_used(struct parser_params *p, NODE *rhs)
11700{
11701 ID *vidp = NULL;
11702 if (!rhs) return;
11703 switch (nd_type(rhs)) {
11704 case NODE_LASGN:
11705 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11706 if (vidp) *vidp |= LVAR_USED;
11707 }
11708 break;
11709 case NODE_DASGN:
11710 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11711 if (vidp) *vidp |= LVAR_USED;
11712 }
11713 break;
11714#if 0
11715 case NODE_MASGN:
11716 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11717 mark_lvar_used(p, rhs->nd_head);
11718 }
11719 break;
11720#endif
11721 }
11722}
11723
11724static NODE *
11725const_decl_path(struct parser_params *p, NODE **dest)
11726{
11727 NODE *n = *dest;
11728 if (!nd_type_p(n, NODE_CALL)) {
11729 const YYLTYPE *loc = &n->nd_loc;
11730 VALUE path;
11731 if (n->nd_vid) {
11732 path = rb_id2str(n->nd_vid);
11733 }
11734 else {
11735 n = n->nd_else;
11736 path = rb_ary_new();
11737 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11738 rb_ary_push(path, rb_id2str(n->nd_mid));
11739 }
11740 if (n && nd_type_p(n, NODE_CONST)) {
11741 // Const::Name
11742 rb_ary_push(path, rb_id2str(n->nd_vid));
11743 }
11744 else if (n && nd_type_p(n, NODE_COLON3)) {
11745 // ::Const::Name
11746 rb_ary_push(path, rb_str_new(0, 0));
11747 }
11748 else {
11749 // expression::Name
11750 rb_ary_push(path, rb_str_new_cstr("..."));
11751 }
11752 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11753 path = rb_fstring(path);
11754 }
11755 *dest = n = NEW_LIT(path, loc);
11756 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11757 }
11758 return n;
11759}
11760
11761extern VALUE rb_mRubyVMFrozenCore;
11762
11763static NODE *
11764make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11765{
11766 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11767
11768 if (copy) {
11769 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11770 NEW_LIST(value, loc), loc);
11771 }
11772 else {
11773 return NEW_CALL(fcore, rb_intern("make_shareable"),
11774 NEW_LIST(value, loc), loc);
11775 }
11776}
11777
11778static NODE *
11779ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11780{
11781 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11782 NODE *args = NEW_LIST(value, loc);
11783 args = list_append(p, args, const_decl_path(p, dest));
11784 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11785}
11786
11787static int is_static_content(NODE *node);
11788
11789static VALUE
11790shareable_literal_value(NODE *node)
11791{
11792 if (!node) return Qnil;
11793 enum node_type type = nd_type(node);
11794 switch (type) {
11795 case NODE_TRUE:
11796 return Qtrue;
11797 case NODE_FALSE:
11798 return Qfalse;
11799 case NODE_NIL:
11800 return Qnil;
11801 case NODE_LIT:
11802 return node->nd_lit;
11803 default:
11804 return Qundef;
11805 }
11806}
11807
11808#ifndef SHAREABLE_BARE_EXPRESSION
11809#define SHAREABLE_BARE_EXPRESSION 1
11810#endif
11811
11812static NODE *
11813shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11814 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11815{
11816# define shareable_literal_constant_next(n) \
11817 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11818 VALUE lit = Qnil;
11819
11820 if (!value) return 0;
11821 enum node_type type = nd_type(value);
11822 switch (type) {
11823 case NODE_TRUE:
11824 case NODE_FALSE:
11825 case NODE_NIL:
11826 case NODE_LIT:
11827 return value;
11828
11829 case NODE_DSTR:
11830 if (shareable == shareable_literal) {
11831 value = NEW_CALL(value, idUMinus, 0, loc);
11832 }
11833 return value;
11834
11835 case NODE_STR:
11836 lit = rb_fstring(value->nd_lit);
11837 nd_set_type(value, NODE_LIT);
11838 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11839 return value;
11840
11841 case NODE_ZLIST:
11842 lit = rb_ary_new();
11843 OBJ_FREEZE_RAW(lit);
11844 NODE *n = NEW_LIT(lit, loc);
11845 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11846 return n;
11847
11848 case NODE_LIST:
11849 lit = rb_ary_new();
11850 for (NODE *n = value; n; n = n->nd_next) {
11851 NODE *elt = n->nd_head;
11852 if (elt) {
11853 elt = shareable_literal_constant_next(elt);
11854 if (elt) {
11855 n->nd_head = elt;
11856 }
11857 else if (RTEST(lit)) {
11858 rb_ary_clear(lit);
11859 lit = Qfalse;
11860 }
11861 }
11862 if (RTEST(lit)) {
11863 VALUE e = shareable_literal_value(elt);
11864 if (!UNDEF_P(e)) {
11865 rb_ary_push(lit, e);
11866 }
11867 else {
11868 rb_ary_clear(lit);
11869 lit = Qnil; /* make shareable at runtime */
11870 }
11871 }
11872 }
11873 break;
11874
11875 case NODE_HASH:
11876 if (!value->nd_brace) return 0;
11877 lit = rb_hash_new();
11878 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11879 NODE *key = n->nd_head;
11880 NODE *val = n->nd_next->nd_head;
11881 if (key) {
11882 key = shareable_literal_constant_next(key);
11883 if (key) {
11884 n->nd_head = key;
11885 }
11886 else if (RTEST(lit)) {
11887 rb_hash_clear(lit);
11888 lit = Qfalse;
11889 }
11890 }
11891 if (val) {
11892 val = shareable_literal_constant_next(val);
11893 if (val) {
11894 n->nd_next->nd_head = val;
11895 }
11896 else if (RTEST(lit)) {
11897 rb_hash_clear(lit);
11898 lit = Qfalse;
11899 }
11900 }
11901 if (RTEST(lit)) {
11902 VALUE k = shareable_literal_value(key);
11903 VALUE v = shareable_literal_value(val);
11904 if (!UNDEF_P(k) && !UNDEF_P(v)) {
11905 rb_hash_aset(lit, k, v);
11906 }
11907 else {
11908 rb_hash_clear(lit);
11909 lit = Qnil; /* make shareable at runtime */
11910 }
11911 }
11912 }
11913 break;
11914
11915 default:
11916 if (shareable == shareable_literal &&
11917 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11918 return ensure_shareable_node(p, dest, value, loc);
11919 }
11920 return 0;
11921 }
11922
11923 /* Array or Hash */
11924 if (!lit) return 0;
11925 if (NIL_P(lit)) {
11926 // if shareable_literal, all elements should have been ensured
11927 // as shareable
11928 value = make_shareable_node(p, value, false, loc);
11929 }
11930 else {
11931 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11932 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11933 }
11934
11935 return value;
11936# undef shareable_literal_constant_next
11937}
11938
11939static NODE *
11940shareable_constant_value(struct parser_params *p, enum shareability shareable,
11941 NODE *lhs, NODE *value, const YYLTYPE *loc)
11942{
11943 if (!value) return 0;
11944 switch (shareable) {
11945 case shareable_none:
11946 return value;
11947
11948 case shareable_literal:
11949 {
11950 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11951 if (lit) return lit;
11952 return value;
11953 }
11954 break;
11955
11956 case shareable_copy:
11957 case shareable_everything:
11958 {
11959 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11960 if (lit) return lit;
11961 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11962 }
11963 break;
11964
11965 default:
11966 UNREACHABLE_RETURN(0);
11967 }
11968}
11969
11970static NODE *
11971node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11972{
11973 if (!lhs) return 0;
11974
11975 switch (nd_type(lhs)) {
11976 case NODE_CDECL:
11977 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11978 /* fallthru */
11979
11980 case NODE_GASGN:
11981 case NODE_IASGN:
11982 case NODE_LASGN:
11983 case NODE_DASGN:
11984 case NODE_MASGN:
11985 case NODE_CVASGN:
11986 lhs->nd_value = rhs;
11987 nd_set_loc(lhs, loc);
11988 break;
11989
11990 case NODE_ATTRASGN:
11991 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11992 nd_set_loc(lhs, loc);
11993 break;
11994
11995 default:
11996 /* should not happen */
11997 break;
11998 }
11999
12000 return lhs;
12001}
12002
12003static NODE *
12004value_expr_check(struct parser_params *p, NODE *node)
12005{
12006 NODE *void_node = 0, *vn;
12007
12008 if (!node) {
12009 rb_warning0("empty expression");
12010 }
12011 while (node) {
12012 switch (nd_type(node)) {
12013 case NODE_RETURN:
12014 case NODE_BREAK:
12015 case NODE_NEXT:
12016 case NODE_REDO:
12017 case NODE_RETRY:
12018 return void_node ? void_node : node;
12019
12020 case NODE_CASE3:
12021 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
12022 compile_error(p, "unexpected node");
12023 return NULL;
12024 }
12025 if (node->nd_body->nd_body) {
12026 return NULL;
12027 }
12028 /* single line pattern matching */
12029 return void_node ? void_node : node;
12030
12031 case NODE_BLOCK:
12032 while (node->nd_next) {
12033 node = node->nd_next;
12034 }
12035 node = node->nd_head;
12036 break;
12037
12038 case NODE_BEGIN:
12039 node = node->nd_body;
12040 break;
12041
12042 case NODE_IF:
12043 case NODE_UNLESS:
12044 if (!node->nd_body) {
12045 return NULL;
12046 }
12047 else if (!node->nd_else) {
12048 return NULL;
12049 }
12050 vn = value_expr_check(p, node->nd_body);
12051 if (!vn) return NULL;
12052 if (!void_node) void_node = vn;
12053 node = node->nd_else;
12054 break;
12055
12056 case NODE_AND:
12057 case NODE_OR:
12058 node = node->nd_1st;
12059 break;
12060
12061 case NODE_LASGN:
12062 case NODE_DASGN:
12063 case NODE_MASGN:
12064 mark_lvar_used(p, node);
12065 return NULL;
12066
12067 default:
12068 return NULL;
12069 }
12070 }
12071
12072 return NULL;
12073}
12074
12075static int
12076value_expr_gen(struct parser_params *p, NODE *node)
12077{
12078 NODE *void_node = value_expr_check(p, node);
12079 if (void_node) {
12080 yyerror1(&void_node->nd_loc, "void value expression");
12081 /* or "control never reach"? */
12082 return FALSE;
12083 }
12084 return TRUE;
12085}
12086static void
12087void_expr(struct parser_params *p, NODE *node)
12088{
12089 const char *useless = 0;
12090
12091 if (!RTEST(ruby_verbose)) return;
12092
12093 if (!node || !(node = nd_once_body(node))) return;
12094 switch (nd_type(node)) {
12095 case NODE_OPCALL:
12096 switch (node->nd_mid) {
12097 case '+':
12098 case '-':
12099 case '*':
12100 case '/':
12101 case '%':
12102 case tPOW:
12103 case tUPLUS:
12104 case tUMINUS:
12105 case '|':
12106 case '^':
12107 case '&':
12108 case tCMP:
12109 case '>':
12110 case tGEQ:
12111 case '<':
12112 case tLEQ:
12113 case tEQ:
12114 case tNEQ:
12115 useless = rb_id2name(node->nd_mid);
12116 break;
12117 }
12118 break;
12119
12120 case NODE_LVAR:
12121 case NODE_DVAR:
12122 case NODE_GVAR:
12123 case NODE_IVAR:
12124 case NODE_CVAR:
12125 case NODE_NTH_REF:
12126 case NODE_BACK_REF:
12127 useless = "a variable";
12128 break;
12129 case NODE_CONST:
12130 useless = "a constant";
12131 break;
12132 case NODE_LIT:
12133 case NODE_STR:
12134 case NODE_DSTR:
12135 case NODE_DREGX:
12136 useless = "a literal";
12137 break;
12138 case NODE_COLON2:
12139 case NODE_COLON3:
12140 useless = "::";
12141 break;
12142 case NODE_DOT2:
12143 useless = "..";
12144 break;
12145 case NODE_DOT3:
12146 useless = "...";
12147 break;
12148 case NODE_SELF:
12149 useless = "self";
12150 break;
12151 case NODE_NIL:
12152 useless = "nil";
12153 break;
12154 case NODE_TRUE:
12155 useless = "true";
12156 break;
12157 case NODE_FALSE:
12158 useless = "false";
12159 break;
12160 case NODE_DEFINED:
12161 useless = "defined?";
12162 break;
12163 }
12164
12165 if (useless) {
12166 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
12167 }
12168}
12169
12170static NODE *
12171void_stmts(struct parser_params *p, NODE *node)
12172{
12173 NODE *const n = node;
12174 if (!RTEST(ruby_verbose)) return n;
12175 if (!node) return n;
12176 if (!nd_type_p(node, NODE_BLOCK)) return n;
12177
12178 while (node->nd_next) {
12179 void_expr(p, node->nd_head);
12180 node = node->nd_next;
12181 }
12182 return n;
12183}
12184
12185static NODE *
12186remove_begin(NODE *node)
12187{
12188 NODE **n = &node, *n1 = node;
12189 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
12190 *n = n1 = n1->nd_body;
12191 }
12192 return node;
12193}
12194
12195static NODE *
12196remove_begin_all(NODE *node)
12197{
12198 NODE **n = &node, *n1 = node;
12199 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
12200 *n = n1 = n1->nd_body;
12201 }
12202 return node;
12203}
12204
12205static void
12206reduce_nodes(struct parser_params *p, NODE **body)
12207{
12208 NODE *node = *body;
12209
12210 if (!node) {
12211 *body = NEW_NIL(&NULL_LOC);
12212 return;
12213 }
12214#define subnodes(n1, n2) \
12215 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
12216 (!node->n2) ? (body = &node->n1, 1) : \
12217 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
12218
12219 while (node) {
12220 int newline = (int)(node->flags & NODE_FL_NEWLINE);
12221 switch (nd_type(node)) {
12222 end:
12223 case NODE_NIL:
12224 *body = 0;
12225 return;
12226 case NODE_RETURN:
12227 *body = node = node->nd_stts;
12228 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12229 continue;
12230 case NODE_BEGIN:
12231 *body = node = node->nd_body;
12232 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12233 continue;
12234 case NODE_BLOCK:
12235 body = &node->nd_end->nd_head;
12236 break;
12237 case NODE_IF:
12238 case NODE_UNLESS:
12239 if (subnodes(nd_body, nd_else)) break;
12240 return;
12241 case NODE_CASE:
12242 body = &node->nd_body;
12243 break;
12244 case NODE_WHEN:
12245 if (!subnodes(nd_body, nd_next)) goto end;
12246 break;
12247 case NODE_ENSURE:
12248 if (!subnodes(nd_head, nd_resq)) goto end;
12249 break;
12250 case NODE_RESCUE:
12251 if (node->nd_else) {
12252 body = &node->nd_resq;
12253 break;
12254 }
12255 if (!subnodes(nd_head, nd_resq)) goto end;
12256 break;
12257 default:
12258 return;
12259 }
12260 node = *body;
12261 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12262 }
12263
12264#undef subnodes
12265}
12266
12267static int
12268is_static_content(NODE *node)
12269{
12270 if (!node) return 1;
12271 switch (nd_type(node)) {
12272 case NODE_HASH:
12273 if (!(node = node->nd_head)) break;
12274 case NODE_LIST:
12275 do {
12276 if (!is_static_content(node->nd_head)) return 0;
12277 } while ((node = node->nd_next) != 0);
12278 case NODE_LIT:
12279 case NODE_STR:
12280 case NODE_NIL:
12281 case NODE_TRUE:
12282 case NODE_FALSE:
12283 case NODE_ZLIST:
12284 break;
12285 default:
12286 return 0;
12287 }
12288 return 1;
12289}
12290
12291static int
12292assign_in_cond(struct parser_params *p, NODE *node)
12293{
12294 switch (nd_type(node)) {
12295 case NODE_MASGN:
12296 case NODE_LASGN:
12297 case NODE_DASGN:
12298 case NODE_GASGN:
12299 case NODE_IASGN:
12300 break;
12301
12302 default:
12303 return 0;
12304 }
12305
12306 if (!node->nd_value) return 1;
12307 if (is_static_content(node->nd_value)) {
12308 /* reports always */
12309 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
12310 }
12311 return 1;
12312}
12313
12314enum cond_type {
12315 COND_IN_OP,
12316 COND_IN_COND,
12317 COND_IN_FF
12318};
12319
12320#define SWITCH_BY_COND_TYPE(t, w, arg) \
12321 switch (t) { \
12322 case COND_IN_OP: break; \
12323 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
12324 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
12325 }
12326
12327static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
12328
12329static NODE*
12330range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12331{
12332 enum node_type type;
12333
12334 if (node == 0) return 0;
12335
12336 type = nd_type(node);
12337 value_expr(node);
12338 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
12339 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
12340 ID lineno = rb_intern("$.");
12341 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
12342 }
12343 return cond0(p, node, COND_IN_FF, loc);
12344}
12345
12346static NODE*
12347cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
12348{
12349 if (node == 0) return 0;
12350 if (!(node = nd_once_body(node))) return 0;
12351 assign_in_cond(p, node);
12352
12353 switch (nd_type(node)) {
12354 case NODE_DSTR:
12355 case NODE_EVSTR:
12356 case NODE_STR:
12357 SWITCH_BY_COND_TYPE(type, warn, "string ")
12358 break;
12359
12360 case NODE_DREGX:
12361 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
12362
12363 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
12364
12365 case NODE_AND:
12366 case NODE_OR:
12367 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
12368 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
12369 break;
12370
12371 case NODE_DOT2:
12372 case NODE_DOT3:
12373 node->nd_beg = range_op(p, node->nd_beg, loc);
12374 node->nd_end = range_op(p, node->nd_end, loc);
12375 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
12376 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
12377 break;
12378
12379 case NODE_DSYM:
12380 warn_symbol:
12381 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
12382 break;
12383
12384 case NODE_LIT:
12385 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
12386 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
12387 nd_set_type(node, NODE_MATCH);
12388 }
12389 else if (node->nd_lit == Qtrue ||
12390 node->nd_lit == Qfalse) {
12391 /* booleans are OK, e.g., while true */
12392 }
12393 else if (SYMBOL_P(node->nd_lit)) {
12394 goto warn_symbol;
12395 }
12396 else {
12397 SWITCH_BY_COND_TYPE(type, warning, "")
12398 }
12399 default:
12400 break;
12401 }
12402 return node;
12403}
12404
12405static NODE*
12406cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12407{
12408 if (node == 0) return 0;
12409 return cond0(p, node, COND_IN_COND, loc);
12410}
12411
12412static NODE*
12413method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12414{
12415 if (node == 0) return 0;
12416 return cond0(p, node, COND_IN_OP, loc);
12417}
12418
12419static NODE*
12420new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
12421{
12422 YYLTYPE loc = {*pos, *pos};
12423 return NEW_NIL(&loc);
12424}
12425
12426static NODE*
12427new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12428{
12429 if (!cc) return right;
12430 cc = cond0(p, cc, COND_IN_COND, loc);
12431 return newline_node(NEW_IF(cc, left, right, loc));
12432}
12433
12434static NODE*
12435new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12436{
12437 if (!cc) return right;
12438 cc = cond0(p, cc, COND_IN_COND, loc);
12439 return newline_node(NEW_UNLESS(cc, left, right, loc));
12440}
12441
12442static NODE*
12443logop(struct parser_params *p, ID id, NODE *left, NODE *right,
12444 const YYLTYPE *op_loc, const YYLTYPE *loc)
12445{
12446 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
12447 NODE *op;
12448 value_expr(left);
12449 if (left && nd_type_p(left, type)) {
12450 NODE *node = left, *second;
12451 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
12452 node = second;
12453 }
12454 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
12455 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
12456 left->nd_loc.end_pos = loc->end_pos;
12457 return left;
12458 }
12459 op = NEW_NODE(type, left, right, 0, loc);
12460 nd_set_line(op, op_loc->beg_pos.lineno);
12461 return op;
12462}
12463
12464static void
12465no_blockarg(struct parser_params *p, NODE *node)
12466{
12467 if (nd_type_p(node, NODE_BLOCK_PASS)) {
12468 compile_error(p, "block argument should not be given");
12469 }
12470}
12471
12472static NODE *
12473ret_args(struct parser_params *p, NODE *node)
12474{
12475 if (node) {
12476 no_blockarg(p, node);
12477 if (nd_type_p(node, NODE_LIST)) {
12478 if (node->nd_next == 0) {
12479 node = node->nd_head;
12480 }
12481 else {
12482 nd_set_type(node, NODE_VALUES);
12483 }
12484 }
12485 }
12486 return node;
12487}
12488
12489static NODE *
12490new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12491{
12492 if (node) no_blockarg(p, node);
12493
12494 return NEW_YIELD(node, loc);
12495}
12496
12497static VALUE
12498negate_lit(struct parser_params *p, VALUE lit)
12499{
12500 if (FIXNUM_P(lit)) {
12501 return LONG2FIX(-FIX2LONG(lit));
12502 }
12503 if (SPECIAL_CONST_P(lit)) {
12504#if USE_FLONUM
12505 if (FLONUM_P(lit)) {
12506 return DBL2NUM(-RFLOAT_VALUE(lit));
12507 }
12508#endif
12509 goto unknown;
12510 }
12511 switch (BUILTIN_TYPE(lit)) {
12512 case T_BIGNUM:
12513 BIGNUM_NEGATE(lit);
12514 lit = rb_big_norm(lit);
12515 break;
12516 case T_RATIONAL:
12517 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
12518 break;
12519 case T_COMPLEX:
12520 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
12521 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
12522 break;
12523 case T_FLOAT:
12524 lit = DBL2NUM(-RFLOAT_VALUE(lit));
12525 break;
12526 unknown:
12527 default:
12528 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
12529 rb_builtin_class_name(lit));
12530 break;
12531 }
12532 return lit;
12533}
12534
12535static NODE *
12536arg_blk_pass(NODE *node1, NODE *node2)
12537{
12538 if (node2) {
12539 if (!node1) return node2;
12540 node2->nd_head = node1;
12541 nd_set_first_lineno(node2, nd_first_lineno(node1));
12542 nd_set_first_column(node2, nd_first_column(node1));
12543 return node2;
12544 }
12545 return node1;
12546}
12547
12548static bool
12549args_info_empty_p(struct rb_args_info *args)
12550{
12551 if (args->pre_args_num) return false;
12552 if (args->post_args_num) return false;
12553 if (args->rest_arg) return false;
12554 if (args->opt_args) return false;
12555 if (args->block_arg) return false;
12556 if (args->kw_args) return false;
12557 if (args->kw_rest_arg) return false;
12558 return true;
12559}
12560
12561static NODE*
12562new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12563{
12564 int saved_line = p->ruby_sourceline;
12565 struct rb_args_info *args = tail->nd_ainfo;
12566
12567 if (args->forwarding) {
12568 if (rest_arg) {
12569 yyerror1(&tail->nd_loc, "... after rest argument");
12570 return tail;
12571 }
12572 rest_arg = idFWD_REST;
12573 }
12574
12575 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12576 args->pre_init = pre_args ? pre_args->nd_next : 0;
12577
12578 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12579 args->post_init = post_args ? post_args->nd_next : 0;
12580 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12581
12582 args->rest_arg = rest_arg;
12583
12584 args->opt_args = opt_args;
12585
12586#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
12587 args->ruby2_keywords = args->forwarding;
12588#else
12589 args->ruby2_keywords = 0;
12590#endif
12591
12592 p->ruby_sourceline = saved_line;
12593 nd_set_loc(tail, loc);
12594
12595 return tail;
12596}
12597
12598static NODE*
12599new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12600{
12601 int saved_line = p->ruby_sourceline;
12602 NODE *node;
12603 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12604 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12605 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12606 args->imemo = tmpbuf;
12607 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12608 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12609 if (p->error_p) return node;
12610
12611 args->block_arg = block;
12612 args->kw_args = kw_args;
12613
12614 if (kw_args) {
12615 /*
12616 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12617 * variable order: k1, kr1, k2, &b, internal_id, krest
12618 * #=> <reorder>
12619 * variable order: kr1, k1, k2, internal_id, krest, &b
12620 */
12621 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12622 struct vtable *vtargs = p->lvtbl->args;
12623 NODE *kwn = kw_args;
12624
12625 if (block) block = vtargs->tbl[vtargs->pos-1];
12626 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12627 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12628 while (kwn) {
12629 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12630 --kw_vars;
12631 --required_kw_vars;
12632 kwn = kwn->nd_next;
12633 }
12634
12635 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12636 ID vid = kwn->nd_body->nd_vid;
12637 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12638 *required_kw_vars++ = vid;
12639 }
12640 else {
12641 *kw_vars++ = vid;
12642 }
12643 }
12644
12645 arg_var(p, kw_bits);
12646 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12647 if (block) arg_var(p, block);
12648
12649 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12650 args->kw_rest_arg->nd_cflag = kw_bits;
12651 }
12652 else if (kw_rest_arg == idNil) {
12653 args->no_kwarg = 1;
12654 }
12655 else if (kw_rest_arg) {
12656 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12657 }
12658
12659 p->ruby_sourceline = saved_line;
12660 return node;
12661}
12662
12663static NODE *
12664args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12665{
12666 if (max_numparam > NO_PARAM) {
12667 if (!args) {
12668 YYLTYPE loc = RUBY_INIT_YYLLOC();
12669 args = new_args_tail(p, 0, 0, 0, 0);
12670 nd_set_loc(args, &loc);
12671 }
12672 args->nd_ainfo->pre_args_num = max_numparam;
12673 }
12674 return args;
12675}
12676
12677static NODE*
12678new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12679{
12680 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12681
12682 aryptn->nd_pconst = constant;
12683
12684 if (pre_arg) {
12685 NODE *pre_args = NEW_LIST(pre_arg, loc);
12686 if (apinfo->pre_args) {
12687 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12688 }
12689 else {
12690 apinfo->pre_args = pre_args;
12691 }
12692 }
12693 return aryptn;
12694}
12695
12696static NODE*
12697new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12698{
12699 int saved_line = p->ruby_sourceline;
12700 NODE *node;
12701 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12702 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12703 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12704 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12705 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12706
12707 apinfo->pre_args = pre_args;
12708
12709 if (has_rest) {
12710 if (rest_arg) {
12711 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12712 }
12713 else {
12714 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12715 }
12716 }
12717 else {
12718 apinfo->rest_arg = NULL;
12719 }
12720
12721 apinfo->post_args = post_args;
12722
12723 p->ruby_sourceline = saved_line;
12724 return node;
12725}
12726
12727static NODE*
12728new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12729{
12730 fndptn->nd_pconst = constant;
12731
12732 return fndptn;
12733}
12734
12735static NODE*
12736new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12737{
12738 int saved_line = p->ruby_sourceline;
12739 NODE *node;
12740 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12741 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12742 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12743 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12744 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12745
12746 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12747 fpinfo->args = args;
12748 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12749
12750 p->ruby_sourceline = saved_line;
12751 return node;
12752}
12753
12754static NODE*
12755new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12756{
12757 hshptn->nd_pconst = constant;
12758 return hshptn;
12759}
12760
12761static NODE*
12762new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12763{
12764 int saved_line = p->ruby_sourceline;
12765 NODE *node, *kw_rest_arg_node;
12766
12767 if (kw_rest_arg == idNil) {
12768 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12769 }
12770 else if (kw_rest_arg) {
12771 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12772 }
12773 else {
12774 kw_rest_arg_node = NULL;
12775 }
12776
12777 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12778
12779 p->ruby_sourceline = saved_line;
12780 return node;
12781}
12782
12783static NODE*
12784dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12785{
12786 VALUE lit;
12787
12788 if (!node) {
12789 return NEW_LIT(ID2SYM(idNULL), loc);
12790 }
12791
12792 switch (nd_type(node)) {
12793 case NODE_DSTR:
12794 nd_set_type(node, NODE_DSYM);
12795 nd_set_loc(node, loc);
12796 break;
12797 case NODE_STR:
12798 lit = node->nd_lit;
12799 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12800 nd_set_type(node, NODE_LIT);
12801 nd_set_loc(node, loc);
12802 break;
12803 default:
12804 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12805 break;
12806 }
12807 return node;
12808}
12809
12810static int
12811append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12812{
12813 NODE *node = (NODE *)v;
12814 NODE **result = (NODE **)h;
12815 node->nd_alen = 2;
12816 node->nd_next->nd_end = node->nd_next;
12817 node->nd_next->nd_next = 0;
12818 if (*result)
12819 list_concat(*result, node);
12820 else
12821 *result = node;
12822 return ST_CONTINUE;
12823}
12824
12825static bool
12826hash_literal_key_p(VALUE k)
12827{
12828 switch (OBJ_BUILTIN_TYPE(k)) {
12829 case T_NODE:
12830 return false;
12831 default:
12832 return true;
12833 }
12834}
12835
12836static int
12837literal_cmp(VALUE val, VALUE lit)
12838{
12839 if (val == lit) return 0;
12840 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12841 return rb_iseq_cdhash_cmp(val, lit);
12842}
12843
12844static st_index_t
12845literal_hash(VALUE a)
12846{
12847 if (!hash_literal_key_p(a)) return (st_index_t)a;
12848 return rb_iseq_cdhash_hash(a);
12849}
12850
12851static const struct st_hash_type literal_type = {
12852 literal_cmp,
12853 literal_hash,
12854};
12855
12856static NODE *
12857remove_duplicate_keys(struct parser_params *p, NODE *hash)
12858{
12859 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12860 NODE *result = 0;
12861 NODE *last_expr = 0;
12862 rb_code_location_t loc = hash->nd_loc;
12863 while (hash && hash->nd_head && hash->nd_next) {
12864 NODE *head = hash->nd_head;
12865 NODE *value = hash->nd_next;
12866 NODE *next = value->nd_next;
12867 st_data_t key = (st_data_t)head;
12868 st_data_t data;
12869 value->nd_next = 0;
12870 if (nd_type_p(head, NODE_LIT) &&
12871 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12872 NODE *dup_value = ((NODE *)data)->nd_next;
12873 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12874 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12875 head->nd_lit, nd_line(head));
12876 if (dup_value == last_expr) {
12877 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12878 }
12879 else {
12880 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12881 }
12882 }
12883 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12884 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12885 hash = next;
12886 }
12887 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12888 st_free_table(literal_keys);
12889 if (hash) {
12890 if (!result) result = hash;
12891 else list_concat(result, hash);
12892 }
12893 result->nd_loc = loc;
12894 return result;
12895}
12896
12897static NODE *
12898new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12899{
12900 if (hash) hash = remove_duplicate_keys(p, hash);
12901 return NEW_HASH(hash, loc);
12902}
12903#endif
12904
12905static void
12906error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12907{
12908 if (is_private_local_id(id)) {
12909 return;
12910 }
12911 if (st_is_member(p->pvtbl, id)) {
12912 yyerror1(loc, "duplicated variable name");
12913 }
12914 else {
12915 st_insert(p->pvtbl, (st_data_t)id, 0);
12916 }
12917}
12918
12919static void
12920error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12921{
12922 if (!p->pktbl) {
12923 p->pktbl = st_init_numtable();
12924 }
12925 else if (st_is_member(p->pktbl, key)) {
12926 yyerror1(loc, "duplicated key name");
12927 return;
12928 }
12929 st_insert(p->pktbl, (st_data_t)key, 0);
12930}
12931
12932#ifndef RIPPER
12933static NODE *
12934new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12935{
12936 return NEW_HASH(hash, loc);
12937}
12938#endif /* !RIPPER */
12939
12940#ifndef RIPPER
12941static NODE *
12942new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12943{
12944 NODE *asgn;
12945
12946 if (lhs) {
12947 ID vid = lhs->nd_vid;
12948 YYLTYPE lhs_loc = lhs->nd_loc;
12949 int shareable = ctxt.shareable_constant_value;
12950 if (shareable) {
12951 switch (nd_type(lhs)) {
12952 case NODE_CDECL:
12953 case NODE_COLON2:
12954 case NODE_COLON3:
12955 break;
12956 default:
12957 shareable = 0;
12958 break;
12959 }
12960 }
12961 if (op == tOROP) {
12962 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12963 lhs->nd_value = rhs;
12964 nd_set_loc(lhs, loc);
12965 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12966 if (is_notop_id(vid)) {
12967 switch (id_type(vid)) {
12968 case ID_GLOBAL:
12969 case ID_INSTANCE:
12970 case ID_CLASS:
12971 asgn->nd_aid = vid;
12972 }
12973 }
12974 }
12975 else if (op == tANDOP) {
12976 if (shareable) {
12977 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12978 }
12979 lhs->nd_value = rhs;
12980 nd_set_loc(lhs, loc);
12981 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12982 }
12983 else {
12984 asgn = lhs;
12985 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12986 if (shareable) {
12987 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12988 }
12989 asgn->nd_value = rhs;
12990 nd_set_loc(asgn, loc);
12991 }
12992 }
12993 else {
12994 asgn = NEW_BEGIN(0, loc);
12995 }
12996 return asgn;
12997}
12998
12999static NODE *
13000new_ary_op_assign(struct parser_params *p, NODE *ary,
13001 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
13002{
13003 NODE *asgn;
13004
13005 args = make_list(args, args_loc);
13006 if (nd_type_p(args, NODE_BLOCK_PASS)) {
13007 args = NEW_ARGSCAT(args, rhs, loc);
13008 }
13009 else {
13010 args = arg_concat(p, args, rhs, loc);
13011 }
13012 asgn = NEW_OP_ASGN1(ary, op, args, loc);
13013 fixpos(asgn, ary);
13014 return asgn;
13015}
13016
13017static NODE *
13018new_attr_op_assign(struct parser_params *p, NODE *lhs,
13019 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
13020{
13021 NODE *asgn;
13022
13023 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
13024 fixpos(asgn, lhs);
13025 return asgn;
13026}
13027
13028static NODE *
13029new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13030{
13031 NODE *asgn;
13032
13033 if (lhs) {
13034 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13035 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
13036 }
13037 else {
13038 asgn = NEW_BEGIN(0, loc);
13039 }
13040 fixpos(asgn, lhs);
13041 return asgn;
13042}
13043
13044static NODE *
13045const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
13046{
13047 if (p->ctxt.in_def) {
13048 yyerror1(loc, "dynamic constant assignment");
13049 }
13050 return NEW_CDECL(0, 0, (path), loc);
13051}
13052#else
13053static VALUE
13054const_decl(struct parser_params *p, VALUE path)
13055{
13056 if (p->ctxt.in_def) {
13057 path = assign_error(p, "dynamic constant assignment", path);
13058 }
13059 return path;
13060}
13061
13062static VALUE
13063assign_error(struct parser_params *p, const char *mesg, VALUE a)
13064{
13065 a = dispatch2(assign_error, ERR_MESG(), a);
13066 ripper_error(p);
13067 return a;
13068}
13069
13070static VALUE
13071var_field(struct parser_params *p, VALUE a)
13072{
13073 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
13074}
13075#endif
13076
13077#ifndef RIPPER
13078static NODE *
13079new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
13080{
13081 NODE *result = head;
13082 if (rescue) {
13083 NODE *tmp = rescue_else ? rescue_else : rescue;
13084 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
13085
13086 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
13087 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
13088 }
13089 else if (rescue_else) {
13090 result = block_append(p, result, rescue_else);
13091 }
13092 if (ensure) {
13093 result = NEW_ENSURE(result, ensure, loc);
13094 }
13095 fixpos(result, head);
13096 return result;
13097}
13098#endif
13099
13100static void
13101warn_unused_var(struct parser_params *p, struct local_vars *local)
13102{
13103 int cnt;
13104
13105 if (!local->used) return;
13106 cnt = local->used->pos;
13107 if (cnt != local->vars->pos) {
13108 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
13109 }
13110#ifndef RIPPER
13111 ID *v = local->vars->tbl;
13112 ID *u = local->used->tbl;
13113 for (int i = 0; i < cnt; ++i) {
13114 if (!v[i] || (u[i] & LVAR_USED)) continue;
13115 if (is_private_local_id(v[i])) continue;
13116 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
13117 }
13118#endif
13119}
13120
13121static void
13122local_push(struct parser_params *p, int toplevel_scope)
13123{
13124 struct local_vars *local;
13125 int inherits_dvars = toplevel_scope && compile_for_eval;
13126 int warn_unused_vars = RTEST(ruby_verbose);
13127
13128 local = ALLOC(struct local_vars);
13129 local->prev = p->lvtbl;
13130 local->args = vtable_alloc(0);
13131 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
13132#ifndef RIPPER
13133 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
13134 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
13135 local->numparam.outer = 0;
13136 local->numparam.inner = 0;
13137 local->numparam.current = 0;
13138#endif
13139 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
13140
13141# if WARN_PAST_SCOPE
13142 local->past = 0;
13143# endif
13144 CMDARG_PUSH(0);
13145 COND_PUSH(0);
13146 p->lvtbl = local;
13147}
13148
13149static void
13150local_pop(struct parser_params *p)
13151{
13152 struct local_vars *local = p->lvtbl->prev;
13153 if (p->lvtbl->used) {
13154 warn_unused_var(p, p->lvtbl);
13155 vtable_free(p->lvtbl->used);
13156 }
13157# if WARN_PAST_SCOPE
13158 while (p->lvtbl->past) {
13159 struct vtable *past = p->lvtbl->past;
13160 p->lvtbl->past = past->prev;
13161 vtable_free(past);
13162 }
13163# endif
13164 vtable_free(p->lvtbl->args);
13165 vtable_free(p->lvtbl->vars);
13166 CMDARG_POP();
13167 COND_POP();
13168 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13169 p->lvtbl = local;
13170}
13171
13172#ifndef RIPPER
13173static rb_ast_id_table_t *
13174local_tbl(struct parser_params *p)
13175{
13176 int cnt_args = vtable_size(p->lvtbl->args);
13177 int cnt_vars = vtable_size(p->lvtbl->vars);
13178 int cnt = cnt_args + cnt_vars;
13179 int i, j;
13180 rb_ast_id_table_t *tbl;
13181
13182 if (cnt <= 0) return 0;
13183 tbl = rb_ast_new_local_table(p->ast, cnt);
13184 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
13185 /* remove IDs duplicated to warn shadowing */
13186 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
13187 ID id = p->lvtbl->vars->tbl[i];
13188 if (!vtable_included(p->lvtbl->args, id)) {
13189 tbl->ids[j++] = id;
13190 }
13191 }
13192 if (j < cnt) {
13193 tbl = rb_ast_resize_latest_local_table(p->ast, j);
13194 }
13195
13196 return tbl;
13197}
13198
13199static NODE*
13200node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
13201{
13202 rb_ast_id_table_t *a0;
13203 NODE *n;
13204
13205 a0 = local_tbl(p);
13206 n = NEW_NODE(type, a0, a1, a2, loc);
13207 return n;
13208}
13209
13210#endif
13211
13212static void
13213numparam_name(struct parser_params *p, ID id)
13214{
13215 if (!NUMPARAM_ID_P(id)) return;
13216 compile_error(p, "_%d is reserved for numbered parameter",
13217 NUMPARAM_ID_TO_IDX(id));
13218}
13219
13220static void
13221arg_var(struct parser_params *p, ID id)
13222{
13223 numparam_name(p, id);
13224 vtable_add(p->lvtbl->args, id);
13225}
13226
13227static void
13228local_var(struct parser_params *p, ID id)
13229{
13230 numparam_name(p, id);
13231 vtable_add(p->lvtbl->vars, id);
13232 if (p->lvtbl->used) {
13233 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
13234 }
13235}
13236
13237static int
13238local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
13239{
13240 struct vtable *vars, *args, *used;
13241
13242 vars = p->lvtbl->vars;
13243 args = p->lvtbl->args;
13244 used = p->lvtbl->used;
13245
13246 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
13247 vars = vars->prev;
13248 args = args->prev;
13249 if (used) used = used->prev;
13250 }
13251
13252 if (vars && vars->prev == DVARS_INHERIT) {
13253 return rb_local_defined(id, p->parent_iseq);
13254 }
13255 else if (vtable_included(args, id)) {
13256 return 1;
13257 }
13258 else {
13259 int i = vtable_included(vars, id);
13260 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
13261 return i != 0;
13262 }
13263}
13264
13265static int
13266local_id(struct parser_params *p, ID id)
13267{
13268 return local_id_ref(p, id, NULL);
13269}
13270
13271static int
13272check_forwarding_args(struct parser_params *p)
13273{
13274 if (local_id(p, idFWD_ALL)) return TRUE;
13275 compile_error(p, "unexpected ...");
13276 return FALSE;
13277}
13278
13279static void
13280add_forwarding_args(struct parser_params *p)
13281{
13282 arg_var(p, idFWD_REST);
13283#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13284 arg_var(p, idFWD_KWREST);
13285#endif
13286 arg_var(p, idFWD_BLOCK);
13287 arg_var(p, idFWD_ALL);
13288}
13289
13290#ifndef RIPPER
13291static NODE *
13292new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
13293{
13294 NODE *rest = NEW_LVAR(idFWD_REST, loc);
13295#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13296 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
13297#endif
13298 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
13299 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
13300#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13301 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
13302#endif
13303 return arg_blk_pass(args, block);
13304}
13305#endif
13306
13307static NODE *
13308numparam_push(struct parser_params *p)
13309{
13310#ifndef RIPPER
13311 struct local_vars *local = p->lvtbl;
13312 NODE *inner = local->numparam.inner;
13313 if (!local->numparam.outer) {
13314 local->numparam.outer = local->numparam.current;
13315 }
13316 local->numparam.inner = 0;
13317 local->numparam.current = 0;
13318 return inner;
13319#else
13320 return 0;
13321#endif
13322}
13323
13324static void
13325numparam_pop(struct parser_params *p, NODE *prev_inner)
13326{
13327#ifndef RIPPER
13328 struct local_vars *local = p->lvtbl;
13329 if (prev_inner) {
13330 /* prefer first one */
13331 local->numparam.inner = prev_inner;
13332 }
13333 else if (local->numparam.current) {
13334 /* current and inner are exclusive */
13335 local->numparam.inner = local->numparam.current;
13336 }
13337 if (p->max_numparam > NO_PARAM) {
13338 /* current and outer are exclusive */
13339 local->numparam.current = local->numparam.outer;
13340 local->numparam.outer = 0;
13341 }
13342 else {
13343 /* no numbered parameter */
13344 local->numparam.current = 0;
13345 }
13346#endif
13347}
13348
13349static const struct vtable *
13350dyna_push(struct parser_params *p)
13351{
13352 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
13353 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
13354 if (p->lvtbl->used) {
13355 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
13356 }
13357 return p->lvtbl->args;
13358}
13359
13360static void
13361dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
13362{
13363 struct vtable *tmp = *vtblp;
13364 *vtblp = tmp->prev;
13365# if WARN_PAST_SCOPE
13366 if (p->past_scope_enabled) {
13367 tmp->prev = p->lvtbl->past;
13368 p->lvtbl->past = tmp;
13369 return;
13370 }
13371# endif
13372 vtable_free(tmp);
13373}
13374
13375static void
13376dyna_pop_1(struct parser_params *p)
13377{
13378 struct vtable *tmp;
13379
13380 if ((tmp = p->lvtbl->used) != 0) {
13381 warn_unused_var(p, p->lvtbl);
13382 p->lvtbl->used = p->lvtbl->used->prev;
13383 vtable_free(tmp);
13384 }
13385 dyna_pop_vtable(p, &p->lvtbl->args);
13386 dyna_pop_vtable(p, &p->lvtbl->vars);
13387}
13388
13389static void
13390dyna_pop(struct parser_params *p, const struct vtable *lvargs)
13391{
13392 while (p->lvtbl->args != lvargs) {
13393 dyna_pop_1(p);
13394 if (!p->lvtbl->args) {
13395 struct local_vars *local = p->lvtbl->prev;
13396 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13397 p->lvtbl = local;
13398 }
13399 }
13400 dyna_pop_1(p);
13401}
13402
13403static int
13404dyna_in_block(struct parser_params *p)
13405{
13406 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
13407}
13408
13409static int
13410dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
13411{
13412 struct vtable *vars, *args, *used;
13413 int i;
13414
13415 args = p->lvtbl->args;
13416 vars = p->lvtbl->vars;
13417 used = p->lvtbl->used;
13418
13419 while (!DVARS_TERMINAL_P(vars)) {
13420 if (vtable_included(args, id)) {
13421 return 1;
13422 }
13423 if ((i = vtable_included(vars, id)) != 0) {
13424 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
13425 return 1;
13426 }
13427 args = args->prev;
13428 vars = vars->prev;
13429 if (!vidrefp) used = 0;
13430 if (used) used = used->prev;
13431 }
13432
13433 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
13434 return rb_dvar_defined(id, p->parent_iseq);
13435 }
13436
13437 return 0;
13438}
13439
13440static int
13441dvar_defined(struct parser_params *p, ID id)
13442{
13443 return dvar_defined_ref(p, id, NULL);
13444}
13445
13446static int
13447dvar_curr(struct parser_params *p, ID id)
13448{
13449 return (vtable_included(p->lvtbl->args, id) ||
13450 vtable_included(p->lvtbl->vars, id));
13451}
13452
13453static void
13454reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
13455{
13456 compile_error(p,
13457 "regexp encoding option '%c' differs from source encoding '%s'",
13458 c, rb_enc_name(rb_enc_get(str)));
13459}
13460
13461#ifndef RIPPER
13462int
13463rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13464{
13465 int c = RE_OPTION_ENCODING_IDX(options);
13466
13467 if (c) {
13468 int opt, idx;
13469 rb_char_to_option_kcode(c, &opt, &idx);
13470 if (idx != ENCODING_GET(str) &&
13471 !is_ascii_string(str)) {
13472 goto error;
13473 }
13474 ENCODING_SET(str, idx);
13475 }
13476 else if (RE_OPTION_ENCODING_NONE(options)) {
13477 if (!ENCODING_IS_ASCII8BIT(str) &&
13478 !is_ascii_string(str)) {
13479 c = 'n';
13480 goto error;
13481 }
13482 rb_enc_associate(str, rb_ascii8bit_encoding());
13483 }
13484 else if (rb_is_usascii_enc(p->enc)) {
13485 if (!is_ascii_string(str)) {
13486 /* raise in re.c */
13487 rb_enc_associate(str, rb_usascii_encoding());
13488 }
13489 else {
13490 rb_enc_associate(str, rb_ascii8bit_encoding());
13491 }
13492 }
13493 return 0;
13494
13495 error:
13496 return c;
13497}
13498
13499static void
13500reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13501{
13502 int c = rb_reg_fragment_setenc(p, str, options);
13503 if (c) reg_fragment_enc_error(p, str, c);
13504}
13505
13506static int
13507reg_fragment_check(struct parser_params* p, VALUE str, int options)
13508{
13509 VALUE err;
13510 reg_fragment_setenc(p, str, options);
13511 err = rb_reg_check_preprocess(str);
13512 if (err != Qnil) {
13513 err = rb_obj_as_string(err);
13514 compile_error(p, "%"PRIsVALUE, err);
13515 return 0;
13516 }
13517 return 1;
13518}
13519
13520typedef struct {
13521 struct parser_params* parser;
13522 rb_encoding *enc;
13523 NODE *succ_block;
13524 const YYLTYPE *loc;
13525} reg_named_capture_assign_t;
13526
13527static int
13528reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
13529 int back_num, int *back_refs, OnigRegex regex, void *arg0)
13530{
13531 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
13532 struct parser_params* p = arg->parser;
13533 rb_encoding *enc = arg->enc;
13534 long len = name_end - name;
13535 const char *s = (const char *)name;
13536 ID var;
13537 NODE *node, *succ;
13538
13539 if (!len) return ST_CONTINUE;
13540 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13541 return ST_CONTINUE;
13542
13543 var = intern_cstr(s, len, enc);
13544 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13545 if (!lvar_defined(p, var)) return ST_CONTINUE;
13546 }
13547 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13548 succ = arg->succ_block;
13549 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13550 succ = block_append(p, succ, node);
13551 arg->succ_block = succ;
13552 return ST_CONTINUE;
13553}
13554
13555static NODE *
13556reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13557{
13558 reg_named_capture_assign_t arg;
13559
13560 arg.parser = p;
13561 arg.enc = rb_enc_get(regexp);
13562 arg.succ_block = 0;
13563 arg.loc = loc;
13564 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13565
13566 if (!arg.succ_block) return 0;
13567 return arg.succ_block->nd_next;
13568}
13569
13570static VALUE
13571parser_reg_compile(struct parser_params* p, VALUE str, int options)
13572{
13573 reg_fragment_setenc(p, str, options);
13574 return rb_parser_reg_compile(p, str, options);
13575}
13576
13577VALUE
13578rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13579{
13580 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13581}
13582
13583static VALUE
13584reg_compile(struct parser_params* p, VALUE str, int options)
13585{
13586 VALUE re;
13587 VALUE err;
13588
13589 err = rb_errinfo();
13590 re = parser_reg_compile(p, str, options);
13591 if (NIL_P(re)) {
13592 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13593 rb_set_errinfo(err);
13594 compile_error(p, "%"PRIsVALUE, m);
13595 return Qnil;
13596 }
13597 return re;
13598}
13599#else
13600static VALUE
13601parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13602{
13603 VALUE err = rb_errinfo();
13604 VALUE re;
13605 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13606 int c = rb_reg_fragment_setenc(p, str, options);
13607 if (c) reg_fragment_enc_error(p, str, c);
13608 re = rb_parser_reg_compile(p, str, options);
13609 if (NIL_P(re)) {
13610 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13611 rb_set_errinfo(err);
13612 }
13613 return re;
13614}
13615#endif
13616
13617#ifndef RIPPER
13618void
13619rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13620{
13621 struct parser_params *p;
13622 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13623 p->do_print = print;
13624 p->do_loop = loop;
13625 p->do_chomp = chomp;
13626 p->do_split = split;
13627}
13628
13629static NODE *
13630parser_append_options(struct parser_params *p, NODE *node)
13631{
13632 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13633 const YYLTYPE *const LOC = &default_location;
13634
13635 if (p->do_print) {
13636 NODE *print = NEW_FCALL(rb_intern("print"),
13637 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13638 LOC);
13639 node = block_append(p, node, print);
13640 }
13641
13642 if (p->do_loop) {
13643 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
13644
13645 if (p->do_split) {
13646 ID ifs = rb_intern("$;");
13647 ID fields = rb_intern("$F");
13648 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13649 NODE *split = NEW_GASGN(fields,
13650 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13651 rb_intern("split"), args, LOC),
13652 LOC);
13653 node = block_append(p, split, node);
13654 }
13655 if (p->do_chomp) {
13656 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
13657 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
13658 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
13659 }
13660
13661 node = NEW_WHILE(NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
13662 }
13663
13664 return node;
13665}
13666
13667void
13668rb_init_parse(void)
13669{
13670 /* just to suppress unused-function warnings */
13671 (void)nodetype;
13672 (void)nodeline;
13673}
13674
13675static ID
13676internal_id(struct parser_params *p)
13677{
13678 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13679}
13680#endif /* !RIPPER */
13681
13682static void
13683parser_initialize(struct parser_params *p)
13684{
13685 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13686 p->command_start = TRUE;
13687 p->ruby_sourcefile_string = Qnil;
13688 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13689 p->node_id = 0;
13690 p->delayed.token = Qnil;
13691#ifdef RIPPER
13692 p->result = Qnil;
13693 p->parsing_thread = Qnil;
13694#else
13695 p->error_buffer = Qfalse;
13696 p->end_expect_token_locations = Qnil;
13697 p->token_id = 0;
13698 p->tokens = Qnil;
13699#endif
13700 p->debug_buffer = Qnil;
13701 p->debug_output = rb_ractor_stdout();
13702 p->enc = rb_utf8_encoding();
13703}
13704
13705#ifdef RIPPER
13706#define parser_mark ripper_parser_mark
13707#define parser_free ripper_parser_free
13708#endif
13709
13710static void
13711parser_mark(void *ptr)
13712{
13713 struct parser_params *p = (struct parser_params*)ptr;
13714
13715 rb_gc_mark(p->lex.input);
13716 rb_gc_mark(p->lex.lastline);
13717 rb_gc_mark(p->lex.nextline);
13718 rb_gc_mark(p->ruby_sourcefile_string);
13719 rb_gc_mark((VALUE)p->lex.strterm);
13720 rb_gc_mark((VALUE)p->ast);
13721 rb_gc_mark(p->case_labels);
13722 rb_gc_mark(p->delayed.token);
13723#ifndef RIPPER
13724 rb_gc_mark(p->debug_lines);
13725 rb_gc_mark(p->compile_option);
13726 rb_gc_mark(p->error_buffer);
13727 rb_gc_mark(p->end_expect_token_locations);
13728 rb_gc_mark(p->tokens);
13729#else
13730 rb_gc_mark(p->value);
13731 rb_gc_mark(p->result);
13732 rb_gc_mark(p->parsing_thread);
13733#endif
13734 rb_gc_mark(p->debug_buffer);
13735 rb_gc_mark(p->debug_output);
13736#ifdef YYMALLOC
13737 rb_gc_mark((VALUE)p->heap);
13738#endif
13739}
13740
13741static void
13742parser_free(void *ptr)
13743{
13744 struct parser_params *p = (struct parser_params*)ptr;
13745 struct local_vars *local, *prev;
13746
13747 if (p->tokenbuf) {
13748 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13749 }
13750 for (local = p->lvtbl; local; local = prev) {
13751 if (local->vars) xfree(local->vars);
13752 prev = local->prev;
13753 xfree(local);
13754 }
13755 {
13756 token_info *ptinfo;
13757 while ((ptinfo = p->token_info) != 0) {
13758 p->token_info = ptinfo->next;
13759 xfree(ptinfo);
13760 }
13761 }
13762 xfree(ptr);
13763}
13764
13765static size_t
13766parser_memsize(const void *ptr)
13767{
13768 struct parser_params *p = (struct parser_params*)ptr;
13769 struct local_vars *local;
13770 size_t size = sizeof(*p);
13771
13772 size += p->toksiz;
13773 for (local = p->lvtbl; local; local = local->prev) {
13774 size += sizeof(*local);
13775 if (local->vars) size += local->vars->capa * sizeof(ID);
13776 }
13777 return size;
13778}
13779
13780static const rb_data_type_t parser_data_type = {
13781#ifndef RIPPER
13782 "parser",
13783#else
13784 "ripper",
13785#endif
13786 {
13787 parser_mark,
13788 parser_free,
13789 parser_memsize,
13790 },
13791 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13792};
13793
13794#ifndef RIPPER
13795#undef rb_reserved_word
13796
13797const struct kwtable *
13798rb_reserved_word(const char *str, unsigned int len)
13799{
13800 return reserved_word(str, len);
13801}
13802
13803VALUE
13804rb_parser_new(void)
13805{
13806 struct parser_params *p;
13807 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13808 &parser_data_type, p);
13809 parser_initialize(p);
13810 return parser;
13811}
13812
13813VALUE
13814rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13815{
13816 struct parser_params *p;
13817
13818 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13819 p->error_buffer = main ? Qfalse : Qnil;
13820 p->parent_iseq = base;
13821 return vparser;
13822}
13823
13824void
13825rb_parser_keep_script_lines(VALUE vparser)
13826{
13827 struct parser_params *p;
13828
13829 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13830 p->keep_script_lines = 1;
13831}
13832
13833void
13834rb_parser_error_tolerant(VALUE vparser)
13835{
13836 struct parser_params *p;
13837
13838 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13839 p->error_tolerant = 1;
13840 p->end_expect_token_locations = rb_ary_new();
13841}
13842
13843void
13844rb_parser_keep_tokens(VALUE vparser)
13845{
13846 struct parser_params *p;
13847
13848 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13849 p->keep_tokens = 1;
13850 p->tokens = rb_ary_new();
13851}
13852
13853#endif
13854
13855#ifdef RIPPER
13856#define rb_parser_end_seen_p ripper_parser_end_seen_p
13857#define rb_parser_encoding ripper_parser_encoding
13858#define rb_parser_get_yydebug ripper_parser_get_yydebug
13859#define rb_parser_set_yydebug ripper_parser_set_yydebug
13860#define rb_parser_get_debug_output ripper_parser_get_debug_output
13861#define rb_parser_set_debug_output ripper_parser_set_debug_output
13862static VALUE ripper_parser_end_seen_p(VALUE vparser);
13863static VALUE ripper_parser_encoding(VALUE vparser);
13864static VALUE ripper_parser_get_yydebug(VALUE self);
13865static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13866static VALUE ripper_parser_get_debug_output(VALUE self);
13867static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13868
13869/*
13870 * call-seq:
13871 * ripper.error? -> Boolean
13872 *
13873 * Return true if parsed source has errors.
13874 */
13875static VALUE
13876ripper_error_p(VALUE vparser)
13877{
13878 struct parser_params *p;
13879
13880 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13881 return RBOOL(p->error_p);
13882}
13883#endif
13884
13885/*
13886 * call-seq:
13887 * ripper.end_seen? -> Boolean
13888 *
13889 * Return true if parsed source ended by +\_\_END\_\_+.
13890 */
13891VALUE
13892rb_parser_end_seen_p(VALUE vparser)
13893{
13894 struct parser_params *p;
13895
13896 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13897 return RBOOL(p->ruby__end__seen);
13898}
13899
13900/*
13901 * call-seq:
13902 * ripper.encoding -> encoding
13903 *
13904 * Return encoding of the source.
13905 */
13906VALUE
13907rb_parser_encoding(VALUE vparser)
13908{
13909 struct parser_params *p;
13910
13911 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13912 return rb_enc_from_encoding(p->enc);
13913}
13914
13915#ifdef RIPPER
13916/*
13917 * call-seq:
13918 * ripper.yydebug -> true or false
13919 *
13920 * Get yydebug.
13921 */
13922VALUE
13923rb_parser_get_yydebug(VALUE self)
13924{
13925 struct parser_params *p;
13926
13927 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13928 return RBOOL(p->debug);
13929}
13930#endif
13931
13932/*
13933 * call-seq:
13934 * ripper.yydebug = flag
13935 *
13936 * Set yydebug.
13937 */
13938VALUE
13939rb_parser_set_yydebug(VALUE self, VALUE flag)
13940{
13941 struct parser_params *p;
13942
13943 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13944 p->debug = RTEST(flag);
13945 return flag;
13946}
13947
13948/*
13949 * call-seq:
13950 * ripper.debug_output -> obj
13951 *
13952 * Get debug output.
13953 */
13954VALUE
13955rb_parser_get_debug_output(VALUE self)
13956{
13957 struct parser_params *p;
13958
13959 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13960 return p->debug_output;
13961}
13962
13963/*
13964 * call-seq:
13965 * ripper.debug_output = obj
13966 *
13967 * Set debug output.
13968 */
13969VALUE
13970rb_parser_set_debug_output(VALUE self, VALUE output)
13971{
13972 struct parser_params *p;
13973
13974 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13975 return p->debug_output = output;
13976}
13977
13978#ifndef RIPPER
13979#ifdef YYMALLOC
13980#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13981/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13982 * potential memory leak */
13983#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13984#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13985 (new)->cnt = (cnt), (ptr))
13986
13987void *
13988rb_parser_malloc(struct parser_params *p, size_t size)
13989{
13990 size_t cnt = HEAPCNT(1, size);
13991 rb_imemo_tmpbuf_t *n = NEWHEAP();
13992 void *ptr = xmalloc(size);
13993
13994 return ADD2HEAP(n, cnt, ptr);
13995}
13996
13997void *
13998rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
13999{
14000 size_t cnt = HEAPCNT(nelem, size);
14001 rb_imemo_tmpbuf_t *n = NEWHEAP();
14002 void *ptr = xcalloc(nelem, size);
14003
14004 return ADD2HEAP(n, cnt, ptr);
14005}
14006
14007void *
14008rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
14009{
14010 rb_imemo_tmpbuf_t *n;
14011 size_t cnt = HEAPCNT(1, size);
14012
14013 if (ptr && (n = p->heap) != NULL) {
14014 do {
14015 if (n->ptr == ptr) {
14016 n->ptr = ptr = xrealloc(ptr, size);
14017 if (n->cnt) n->cnt = cnt;
14018 return ptr;
14019 }
14020 } while ((n = n->next) != NULL);
14021 }
14022 n = NEWHEAP();
14023 ptr = xrealloc(ptr, size);
14024 return ADD2HEAP(n, cnt, ptr);
14025}
14026
14027void
14028rb_parser_free(struct parser_params *p, void *ptr)
14029{
14030 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
14031
14032 while ((n = *prev) != NULL) {
14033 if (n->ptr == ptr) {
14034 *prev = n->next;
14035 break;
14036 }
14037 prev = &n->next;
14038 }
14039}
14040#endif
14041
14042void
14043rb_parser_printf(struct parser_params *p, const char *fmt, ...)
14044{
14045 va_list ap;
14046 VALUE mesg = p->debug_buffer;
14047
14048 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
14049 va_start(ap, fmt);
14050 rb_str_vcatf(mesg, fmt, ap);
14051 va_end(ap);
14052 if (RSTRING_END(mesg)[-1] == '\n') {
14053 rb_io_write(p->debug_output, mesg);
14054 p->debug_buffer = Qnil;
14055 }
14056}
14057
14058static void
14059parser_compile_error(struct parser_params *p, const char *fmt, ...)
14060{
14061 va_list ap;
14062
14063 rb_io_flush(p->debug_output);
14064 p->error_p = 1;
14065 va_start(ap, fmt);
14066 p->error_buffer =
14067 rb_syntax_error_append(p->error_buffer,
14068 p->ruby_sourcefile_string,
14069 p->ruby_sourceline,
14070 rb_long2int(p->lex.pcur - p->lex.pbeg),
14071 p->enc, fmt, ap);
14072 va_end(ap);
14073}
14074
14075static size_t
14076count_char(const char *str, int c)
14077{
14078 int n = 0;
14079 while (str[n] == c) ++n;
14080 return n;
14081}
14082
14083/*
14084 * strip enclosing double-quotes, same as the default yytnamerr except
14085 * for that single-quotes matching back-quotes do not stop stripping.
14086 *
14087 * "\"`class' keyword\"" => "`class' keyword"
14088 */
14089RUBY_FUNC_EXPORTED size_t
14090rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
14091{
14092 if (*yystr == '"') {
14093 size_t yyn = 0, bquote = 0;
14094 const char *yyp = yystr;
14095
14096 while (*++yyp) {
14097 switch (*yyp) {
14098 case '`':
14099 if (!bquote) {
14100 bquote = count_char(yyp+1, '`') + 1;
14101 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
14102 yyn += bquote;
14103 yyp += bquote - 1;
14104 break;
14105 }
14106 goto default_char;
14107
14108 case '\'':
14109 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
14110 if (yyres) memcpy(yyres + yyn, yyp, bquote);
14111 yyn += bquote;
14112 yyp += bquote - 1;
14113 bquote = 0;
14114 break;
14115 }
14116 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
14117 if (yyres) memcpy(yyres + yyn, yyp, 3);
14118 yyn += 3;
14119 yyp += 2;
14120 break;
14121 }
14122 goto do_not_strip_quotes;
14123
14124 case ',':
14125 goto do_not_strip_quotes;
14126
14127 case '\\':
14128 if (*++yyp != '\\')
14129 goto do_not_strip_quotes;
14130 /* Fall through. */
14131 default_char:
14132 default:
14133 if (yyres)
14134 yyres[yyn] = *yyp;
14135 yyn++;
14136 break;
14137
14138 case '"':
14139 case '\0':
14140 if (yyres)
14141 yyres[yyn] = '\0';
14142 return yyn;
14143 }
14144 }
14145 do_not_strip_quotes: ;
14146 }
14147
14148 if (!yyres) return strlen(yystr);
14149
14150 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
14151}
14152#endif
14153
14154#ifdef RIPPER
14155#ifdef RIPPER_DEBUG
14156/* :nodoc: */
14157static VALUE
14158ripper_validate_object(VALUE self, VALUE x)
14159{
14160 if (x == Qfalse) return x;
14161 if (x == Qtrue) return x;
14162 if (NIL_P(x)) return x;
14163 if (UNDEF_P(x))
14164 rb_raise(rb_eArgError, "Qundef given");
14165 if (FIXNUM_P(x)) return x;
14166 if (SYMBOL_P(x)) return x;
14167 switch (BUILTIN_TYPE(x)) {
14168 case T_STRING:
14169 case T_OBJECT:
14170 case T_ARRAY:
14171 case T_BIGNUM:
14172 case T_FLOAT:
14173 case T_COMPLEX:
14174 case T_RATIONAL:
14175 break;
14176 case T_NODE:
14177 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
14178 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
14179 }
14180 x = ((NODE *)x)->nd_rval;
14181 break;
14182 default:
14183 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
14184 (void *)x, rb_obj_classname(x));
14185 }
14186 if (!RBASIC_CLASS(x)) {
14187 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
14188 (void *)x, rb_builtin_type_name(TYPE(x)));
14189 }
14190 return x;
14191}
14192#endif
14193
14194#define validate(x) ((x) = get_value(x))
14195
14196static VALUE
14197ripper_dispatch0(struct parser_params *p, ID mid)
14198{
14199 return rb_funcall(p->value, mid, 0);
14200}
14201
14202static VALUE
14203ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
14204{
14205 validate(a);
14206 return rb_funcall(p->value, mid, 1, a);
14207}
14208
14209static VALUE
14210ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
14211{
14212 validate(a);
14213 validate(b);
14214 return rb_funcall(p->value, mid, 2, a, b);
14215}
14216
14217static VALUE
14218ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
14219{
14220 validate(a);
14221 validate(b);
14222 validate(c);
14223 return rb_funcall(p->value, mid, 3, a, b, c);
14224}
14225
14226static VALUE
14227ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
14228{
14229 validate(a);
14230 validate(b);
14231 validate(c);
14232 validate(d);
14233 return rb_funcall(p->value, mid, 4, a, b, c, d);
14234}
14235
14236static VALUE
14237ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
14238{
14239 validate(a);
14240 validate(b);
14241 validate(c);
14242 validate(d);
14243 validate(e);
14244 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
14245}
14246
14247static VALUE
14248ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
14249{
14250 validate(a);
14251 validate(b);
14252 validate(c);
14253 validate(d);
14254 validate(e);
14255 validate(f);
14256 validate(g);
14257 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
14258}
14259
14260static ID
14261ripper_get_id(VALUE v)
14262{
14263 NODE *nd;
14264 if (!RB_TYPE_P(v, T_NODE)) return 0;
14265 nd = (NODE *)v;
14266 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
14267 return nd->nd_vid;
14268}
14269
14270static VALUE
14271ripper_get_value(VALUE v)
14272{
14273 NODE *nd;
14274 if (UNDEF_P(v)) return Qnil;
14275 if (!RB_TYPE_P(v, T_NODE)) return v;
14276 nd = (NODE *)v;
14277 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
14278 return nd->nd_rval;
14279}
14280
14281static void
14282ripper_error(struct parser_params *p)
14283{
14284 p->error_p = TRUE;
14285}
14286
14287static void
14288ripper_compile_error(struct parser_params *p, const char *fmt, ...)
14289{
14290 VALUE str;
14291 va_list args;
14292
14293 va_start(args, fmt);
14294 str = rb_vsprintf(fmt, args);
14295 va_end(args);
14296 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
14297 ripper_error(p);
14298}
14299
14300static VALUE
14301ripper_lex_get_generic(struct parser_params *p, VALUE src)
14302{
14303 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
14304 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
14305 rb_raise(rb_eTypeError,
14306 "gets returned %"PRIsVALUE" (expected String or nil)",
14307 rb_obj_class(line));
14308 }
14309 return line;
14310}
14311
14312static VALUE
14313ripper_lex_io_get(struct parser_params *p, VALUE src)
14314{
14315 return rb_io_gets(src);
14316}
14317
14318static VALUE
14319ripper_s_allocate(VALUE klass)
14320{
14321 struct parser_params *p;
14322 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
14323 &parser_data_type, p);
14324 p->value = self;
14325 return self;
14326}
14327
14328#define ripper_initialized_p(r) ((r)->lex.input != 0)
14329
14330/*
14331 * call-seq:
14332 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
14333 *
14334 * Create a new Ripper object.
14335 * _src_ must be a String, an IO, or an Object which has #gets method.
14336 *
14337 * This method does not starts parsing.
14338 * See also Ripper#parse and Ripper.parse.
14339 */
14340static VALUE
14341ripper_initialize(int argc, VALUE *argv, VALUE self)
14342{
14343 struct parser_params *p;
14344 VALUE src, fname, lineno;
14345
14346 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14347 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
14348 if (RB_TYPE_P(src, T_FILE)) {
14349 p->lex.gets = ripper_lex_io_get;
14350 }
14351 else if (rb_respond_to(src, id_gets)) {
14352 p->lex.gets = ripper_lex_get_generic;
14353 }
14354 else {
14355 StringValue(src);
14356 p->lex.gets = lex_get_str;
14357 }
14358 p->lex.input = src;
14359 p->eofp = 0;
14360 if (NIL_P(fname)) {
14361 fname = STR_NEW2("(ripper)");
14362 OBJ_FREEZE(fname);
14363 }
14364 else {
14365 StringValueCStr(fname);
14366 fname = rb_str_new_frozen(fname);
14367 }
14368 parser_initialize(p);
14369
14370 p->ruby_sourcefile_string = fname;
14371 p->ruby_sourcefile = RSTRING_PTR(fname);
14372 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
14373
14374 return Qnil;
14375}
14376
14377static VALUE
14378ripper_parse0(VALUE parser_v)
14379{
14380 struct parser_params *p;
14381
14382 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14383 parser_prepare(p);
14384 p->ast = rb_ast_new();
14385 ripper_yyparse((void*)p);
14386 rb_ast_dispose(p->ast);
14387 p->ast = 0;
14388 return p->result;
14389}
14390
14391static VALUE
14392ripper_ensure(VALUE parser_v)
14393{
14394 struct parser_params *p;
14395
14396 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14397 p->parsing_thread = Qnil;
14398 return Qnil;
14399}
14400
14401/*
14402 * call-seq:
14403 * ripper.parse
14404 *
14405 * Start parsing and returns the value of the root action.
14406 */
14407static VALUE
14408ripper_parse(VALUE self)
14409{
14410 struct parser_params *p;
14411
14412 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14413 if (!ripper_initialized_p(p)) {
14414 rb_raise(rb_eArgError, "method called for uninitialized object");
14415 }
14416 if (!NIL_P(p->parsing_thread)) {
14417 if (p->parsing_thread == rb_thread_current())
14418 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
14419 else
14420 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
14421 }
14422 p->parsing_thread = rb_thread_current();
14423 rb_ensure(ripper_parse0, self, ripper_ensure, self);
14424
14425 return p->result;
14426}
14427
14428/*
14429 * call-seq:
14430 * ripper.column -> Integer
14431 *
14432 * Return column number of current parsing line.
14433 * This number starts from 0.
14434 */
14435static VALUE
14436ripper_column(VALUE self)
14437{
14438 struct parser_params *p;
14439 long col;
14440
14441 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14442 if (!ripper_initialized_p(p)) {
14443 rb_raise(rb_eArgError, "method called for uninitialized object");
14444 }
14445 if (NIL_P(p->parsing_thread)) return Qnil;
14446 col = p->lex.ptok - p->lex.pbeg;
14447 return LONG2NUM(col);
14448}
14449
14450/*
14451 * call-seq:
14452 * ripper.filename -> String
14453 *
14454 * Return current parsing filename.
14455 */
14456static VALUE
14457ripper_filename(VALUE self)
14458{
14459 struct parser_params *p;
14460
14461 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14462 if (!ripper_initialized_p(p)) {
14463 rb_raise(rb_eArgError, "method called for uninitialized object");
14464 }
14465 return p->ruby_sourcefile_string;
14466}
14467
14468/*
14469 * call-seq:
14470 * ripper.lineno -> Integer
14471 *
14472 * Return line number of current parsing line.
14473 * This number starts from 1.
14474 */
14475static VALUE
14476ripper_lineno(VALUE self)
14477{
14478 struct parser_params *p;
14479
14480 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14481 if (!ripper_initialized_p(p)) {
14482 rb_raise(rb_eArgError, "method called for uninitialized object");
14483 }
14484 if (NIL_P(p->parsing_thread)) return Qnil;
14485 return INT2NUM(p->ruby_sourceline);
14486}
14487
14488/*
14489 * call-seq:
14490 * ripper.state -> Integer
14491 *
14492 * Return scanner state of current token.
14493 */
14494static VALUE
14495ripper_state(VALUE self)
14496{
14497 struct parser_params *p;
14498
14499 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14500 if (!ripper_initialized_p(p)) {
14501 rb_raise(rb_eArgError, "method called for uninitialized object");
14502 }
14503 if (NIL_P(p->parsing_thread)) return Qnil;
14504 return INT2NUM(p->lex.state);
14505}
14506
14507/*
14508 * call-seq:
14509 * ripper.token -> String
14510 *
14511 * Return the current token string.
14512 */
14513static VALUE
14514ripper_token(VALUE self)
14515{
14516 struct parser_params *p;
14517 long pos, len;
14518
14519 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14520 if (!ripper_initialized_p(p)) {
14521 rb_raise(rb_eArgError, "method called for uninitialized object");
14522 }
14523 if (NIL_P(p->parsing_thread)) return Qnil;
14524 pos = p->lex.ptok - p->lex.pbeg;
14525 len = p->lex.pcur - p->lex.ptok;
14526 return rb_str_subseq(p->lex.lastline, pos, len);
14527}
14528
14529#ifdef RIPPER_DEBUG
14530/* :nodoc: */
14531static VALUE
14532ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
14533{
14534 StringValue(msg);
14535 if (UNDEF_P(obj)) {
14536 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
14537 }
14538 return Qnil;
14539}
14540
14541/* :nodoc: */
14542static VALUE
14543ripper_value(VALUE self, VALUE obj)
14544{
14545 return ULONG2NUM(obj);
14546}
14547#endif
14548
14549/*
14550 * call-seq:
14551 * Ripper.lex_state_name(integer) -> string
14552 *
14553 * Returns a string representation of lex_state.
14554 */
14555static VALUE
14556ripper_lex_state_name(VALUE self, VALUE state)
14557{
14558 return rb_parser_lex_state_name(NUM2INT(state));
14559}
14560
14561void
14562Init_ripper(void)
14563{
14564 ripper_init_eventids1();
14565 ripper_init_eventids2();
14566 id_warn = rb_intern_const("warn");
14567 id_warning = rb_intern_const("warning");
14568 id_gets = rb_intern_const("gets");
14569 id_assoc = rb_intern_const("=>");
14570
14571 (void)yystpcpy; /* may not used in newer bison */
14572
14573 InitVM(ripper);
14574}
14575
14576void
14577InitVM_ripper(void)
14578{
14579 VALUE Ripper;
14580
14581 Ripper = rb_define_class("Ripper", rb_cObject);
14582 /* version of Ripper */
14583 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14584 rb_define_alloc_func(Ripper, ripper_s_allocate);
14585 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14586 rb_define_method(Ripper, "parse", ripper_parse, 0);
14587 rb_define_method(Ripper, "column", ripper_column, 0);
14588 rb_define_method(Ripper, "filename", ripper_filename, 0);
14589 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14590 rb_define_method(Ripper, "state", ripper_state, 0);
14591 rb_define_method(Ripper, "token", ripper_token, 0);
14592 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14593 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14594 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14595 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14596 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14597 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14598 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14599#ifdef RIPPER_DEBUG
14600 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14601 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14602 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14603#endif
14604
14605 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14606 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14607
14608 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14609
14610<% @exprs.each do |expr, desc| -%>
14611 /* <%=desc%> */
14612 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14613<% end %>
14614 ripper_init_eventids1_table(Ripper);
14615 ripper_init_eventids2_table(Ripper);
14616
14617# if 0
14618 /* Hack to let RDoc document SCRIPT_LINES__ */
14619
14620 /*
14621 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14622 * after the assignment will be added as an Array of lines with the file
14623 * name as the key.
14624 */
14625 rb_define_global_const("SCRIPT_LINES__", Qnil);
14626#endif
14627
14628}
14629#endif /* RIPPER */
14630
14631/*
14632 * Local variables:
14633 * mode: c
14634 * c-file-style: "ruby"
14635 * End:
14636 */