Ruby 3.2.3p157 (2024-01-18 revision 52bb2ac0a6971d0391efa2275f7a66bff319087c)
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, $2, $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, $2, $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, $2, $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, $2, $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, $2, $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, $2, $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, $2, $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, $2, 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
7263static int tokadd_mbchar(struct parser_params *p, int c);
7264
7265/* return value is for ?\u3042 */
7266static void
7267tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7268 int term, int symbol_literal, int regexp_literal)
7269{
7270 /*
7271 * If `term` is not -1, then we allow multiple codepoints in \u{}
7272 * upto `term` byte, otherwise we're parsing a character literal.
7273 * And then add the codepoints to the current token.
7274 */
7275 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7276
7277 const int open_brace = '{', close_brace = '}';
7278
7279 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7280
7281 if (peek(p, open_brace)) { /* handle \u{...} form */
7282 if (regexp_literal && p->lex.strterm->u.literal.u1.func == str_regexp) {
7283 /*
7284 * Skip parsing validation code and copy bytes as-is until term or
7285 * closing brace, in order to correctly handle extended regexps where
7286 * invalid unicode escapes are allowed in comments. The regexp parser
7287 * does its own validation and will catch any issues.
7288 */
7289 tokadd(p, open_brace);
7290 while (++p->lex.pcur < p->lex.pend) {
7291 int c = peekc(p);
7292 if (c == close_brace) {
7293 tokadd(p, c);
7294 ++p->lex.pcur;
7295 break;
7296 }
7297 else if (c == term) {
7298 break;
7299 }
7300 if (c == '\\' && p->lex.pcur + 1 < p->lex.pend) {
7301 tokadd(p, c);
7302 c = *++p->lex.pcur;
7303 }
7304 tokadd_mbchar(p, c);
7305 }
7306 }
7307 else {
7308 const char *second = NULL;
7309 int c, last = nextc(p);
7310 if (p->lex.pcur >= p->lex.pend) goto unterminated;
7311 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
7312 while (c != close_brace) {
7313 if (c == term) goto unterminated;
7314 if (second == multiple_codepoints)
7315 second = p->lex.pcur;
7316 if (regexp_literal) tokadd(p, last);
7317 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7318 break;
7319 }
7320 while (ISSPACE(c = *p->lex.pcur)) {
7321 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
7322 last = c;
7323 }
7324 if (term == -1 && !second)
7325 second = multiple_codepoints;
7326 }
7327
7328 if (c != close_brace) {
7329 unterminated:
7330 token_flush(p);
7331 yyerror0("unterminated Unicode escape");
7332 return;
7333 }
7334 if (second && second != multiple_codepoints) {
7335 const char *pcur = p->lex.pcur;
7336 p->lex.pcur = second;
7337 dispatch_scan_event(p, tSTRING_CONTENT);
7338 token_flush(p);
7339 p->lex.pcur = pcur;
7340 yyerror0(multiple_codepoints);
7341 token_flush(p);
7342 }
7343
7344 if (regexp_literal) tokadd(p, close_brace);
7345 nextc(p);
7346 }
7347 }
7348 else { /* handle \uxxxx form */
7349 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7350 token_flush(p);
7351 return;
7352 }
7353 }
7354}
7355
7356#define ESCAPE_CONTROL 1
7357#define ESCAPE_META 2
7358
7359static int
7360read_escape(struct parser_params *p, int flags, rb_encoding **encp)
7361{
7362 int c;
7363 size_t numlen;
7364
7365 switch (c = nextc(p)) {
7366 case '\\': /* Backslash */
7367 return c;
7368
7369 case 'n': /* newline */
7370 return '\n';
7371
7372 case 't': /* horizontal tab */
7373 return '\t';
7374
7375 case 'r': /* carriage-return */
7376 return '\r';
7377
7378 case 'f': /* form-feed */
7379 return '\f';
7380
7381 case 'v': /* vertical tab */
7382 return '\13';
7383
7384 case 'a': /* alarm(bell) */
7385 return '\007';
7386
7387 case 'e': /* escape */
7388 return 033;
7389
7390 case '0': case '1': case '2': case '3': /* octal constant */
7391 case '4': case '5': case '6': case '7':
7392 pushback(p, c);
7393 c = scan_oct(p->lex.pcur, 3, &numlen);
7394 p->lex.pcur += numlen;
7395 return c;
7396
7397 case 'x': /* hex constant */
7398 c = tok_hex(p, &numlen);
7399 if (numlen == 0) return 0;
7400 return c;
7401
7402 case 'b': /* backspace */
7403 return '\010';
7404
7405 case 's': /* space */
7406 return ' ';
7407
7408 case 'M':
7409 if (flags & ESCAPE_META) goto eof;
7410 if ((c = nextc(p)) != '-') {
7411 goto eof;
7412 }
7413 if ((c = nextc(p)) == '\\') {
7414 switch (peekc(p)) {
7415 case 'u': case 'U':
7416 nextc(p);
7417 goto eof;
7418 }
7419 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
7420 }
7421 else if (c == -1 || !ISASCII(c)) goto eof;
7422 else {
7423 int c2 = escaped_control_code(c);
7424 if (c2) {
7425 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
7426 WARN_SPACE_CHAR(c2, "\\M-");
7427 }
7428 else {
7429 WARN_SPACE_CHAR(c2, "\\C-\\M-");
7430 }
7431 }
7432 else if (ISCNTRL(c)) goto eof;
7433 return ((c & 0xff) | 0x80);
7434 }
7435
7436 case 'C':
7437 if ((c = nextc(p)) != '-') {
7438 goto eof;
7439 }
7440 case 'c':
7441 if (flags & ESCAPE_CONTROL) goto eof;
7442 if ((c = nextc(p))== '\\') {
7443 switch (peekc(p)) {
7444 case 'u': case 'U':
7445 nextc(p);
7446 goto eof;
7447 }
7448 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
7449 }
7450 else if (c == '?')
7451 return 0177;
7452 else if (c == -1 || !ISASCII(c)) goto eof;
7453 else {
7454 int c2 = escaped_control_code(c);
7455 if (c2) {
7456 if (ISCNTRL(c)) {
7457 if (flags & ESCAPE_META) {
7458 WARN_SPACE_CHAR(c2, "\\M-");
7459 }
7460 else {
7461 WARN_SPACE_CHAR(c2, "");
7462 }
7463 }
7464 else {
7465 if (flags & ESCAPE_META) {
7466 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7467 }
7468 else {
7469 WARN_SPACE_CHAR(c2, "\\C-");
7470 }
7471 }
7472 }
7473 else if (ISCNTRL(c)) goto eof;
7474 }
7475 return c & 0x9f;
7476
7477 eof:
7478 case -1:
7479 yyerror0("Invalid escape character syntax");
7480 token_flush(p);
7481 return '\0';
7482
7483 default:
7484 return c;
7485 }
7486}
7487
7488static void
7489tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7490{
7491 int len = rb_enc_codelen(c, enc);
7492 rb_enc_mbcput(c, tokspace(p, len), enc);
7493}
7494
7495static int
7496tokadd_escape(struct parser_params *p, rb_encoding **encp)
7497{
7498 int c;
7499 size_t numlen;
7500
7501 switch (c = nextc(p)) {
7502 case '\n':
7503 return 0; /* just ignore */
7504
7505 case '0': case '1': case '2': case '3': /* octal constant */
7506 case '4': case '5': case '6': case '7':
7507 {
7508 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7509 if (numlen == 0) goto eof;
7510 p->lex.pcur += numlen;
7511 tokcopy(p, (int)numlen + 1);
7512 }
7513 return 0;
7514
7515 case 'x': /* hex constant */
7516 {
7517 tok_hex(p, &numlen);
7518 if (numlen == 0) return -1;
7519 tokcopy(p, (int)numlen + 2);
7520 }
7521 return 0;
7522
7523 eof:
7524 case -1:
7525 yyerror0("Invalid escape character syntax");
7526 token_flush(p);
7527 return -1;
7528
7529 default:
7530 tokadd(p, '\\');
7531 tokadd(p, c);
7532 }
7533 return 0;
7534}
7535
7536static int
7537regx_options(struct parser_params *p)
7538{
7539 int kcode = 0;
7540 int kopt = 0;
7541 int options = 0;
7542 int c, opt, kc;
7543
7544 newtok(p);
7545 while (c = nextc(p), ISALPHA(c)) {
7546 if (c == 'o') {
7547 options |= RE_OPTION_ONCE;
7548 }
7549 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7550 if (kc >= 0) {
7551 if (kc != rb_ascii8bit_encindex()) kcode = c;
7552 kopt = opt;
7553 }
7554 else {
7555 options |= opt;
7556 }
7557 }
7558 else {
7559 tokadd(p, c);
7560 }
7561 }
7562 options |= kopt;
7563 pushback(p, c);
7564 if (toklen(p)) {
7565 YYLTYPE loc = RUBY_INIT_YYLLOC();
7566 tokfix(p);
7567 compile_error(p, "unknown regexp option%s - %*s",
7568 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7569 parser_show_error_line(p, &loc);
7570 }
7571 return options | RE_OPTION_ENCODING(kcode);
7572}
7573
7574static int
7575tokadd_mbchar(struct parser_params *p, int c)
7576{
7577 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7578 if (len < 0) return -1;
7579 tokadd(p, c);
7580 p->lex.pcur += --len;
7581 if (len > 0) tokcopy(p, len);
7582 return c;
7583}
7584
7585static inline int
7586simple_re_meta(int c)
7587{
7588 switch (c) {
7589 case '$': case '*': case '+': case '.':
7590 case '?': case '^': case '|':
7591 case ')': case ']': case '}': case '>':
7592 return TRUE;
7593 default:
7594 return FALSE;
7595 }
7596}
7597
7598static int
7599parser_update_heredoc_indent(struct parser_params *p, int c)
7600{
7601 if (p->heredoc_line_indent == -1) {
7602 if (c == '\n') p->heredoc_line_indent = 0;
7603 }
7604 else {
7605 if (c == ' ') {
7606 p->heredoc_line_indent++;
7607 return TRUE;
7608 }
7609 else if (c == '\t') {
7610 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7611 p->heredoc_line_indent = w * TAB_WIDTH;
7612 return TRUE;
7613 }
7614 else if (c != '\n') {
7615 if (p->heredoc_indent > p->heredoc_line_indent) {
7616 p->heredoc_indent = p->heredoc_line_indent;
7617 }
7618 p->heredoc_line_indent = -1;
7619 }
7620 }
7621 return FALSE;
7622}
7623
7624static void
7625parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7626{
7627 YYLTYPE loc = RUBY_INIT_YYLLOC();
7628 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7629 compile_error(p, "%s mixed within %s source", n1, n2);
7630 parser_show_error_line(p, &loc);
7631}
7632
7633static void
7634parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7635{
7636 const char *pos = p->lex.pcur;
7637 p->lex.pcur = beg;
7638 parser_mixed_error(p, enc1, enc2);
7639 p->lex.pcur = pos;
7640}
7641
7642static int
7643tokadd_string(struct parser_params *p,
7644 int func, int term, int paren, long *nest,
7645 rb_encoding **encp, rb_encoding **enc)
7646{
7647 int c;
7648 bool erred = false;
7649#ifdef RIPPER
7650 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
7651 int top_of_line = FALSE;
7652#endif
7653
7654#define mixed_error(enc1, enc2) \
7655 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7656#define mixed_escape(beg, enc1, enc2) \
7657 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7658
7659 while ((c = nextc(p)) != -1) {
7660 if (p->heredoc_indent > 0) {
7661 parser_update_heredoc_indent(p, c);
7662 }
7663#ifdef RIPPER
7664 if (top_of_line && heredoc_end == p->ruby_sourceline) {
7665 pushback(p, c);
7666 break;
7667 }
7668#endif
7669
7670 if (paren && c == paren) {
7671 ++*nest;
7672 }
7673 else if (c == term) {
7674 if (!nest || !*nest) {
7675 pushback(p, c);
7676 break;
7677 }
7678 --*nest;
7679 }
7680 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7681 unsigned char c2 = *p->lex.pcur;
7682 if (c2 == '$' || c2 == '@' || c2 == '{') {
7683 pushback(p, c);
7684 break;
7685 }
7686 }
7687 else if (c == '\\') {
7688 c = nextc(p);
7689 switch (c) {
7690 case '\n':
7691 if (func & STR_FUNC_QWORDS) break;
7692 if (func & STR_FUNC_EXPAND) {
7693 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7694 continue;
7695 if (c == term) {
7696 c = '\\';
7697 goto terminate;
7698 }
7699 }
7700 tokadd(p, '\\');
7701 break;
7702
7703 case '\\':
7704 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7705 break;
7706
7707 case 'u':
7708 if ((func & STR_FUNC_EXPAND) == 0) {
7709 tokadd(p, '\\');
7710 break;
7711 }
7712 tokadd_utf8(p, enc, term,
7713 func & STR_FUNC_SYMBOL,
7714 func & STR_FUNC_REGEXP);
7715 continue;
7716
7717 default:
7718 if (c == -1) return -1;
7719 if (!ISASCII(c)) {
7720 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7721 goto non_ascii;
7722 }
7723 if (func & STR_FUNC_REGEXP) {
7724 switch (c) {
7725 case 'c':
7726 case 'C':
7727 case 'M': {
7728 pushback(p, c);
7729 c = read_escape(p, 0, enc);
7730
7731 int i;
7732 char escbuf[5];
7733 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7734 for (i = 0; i < 4; i++) {
7735 tokadd(p, escbuf[i]);
7736 }
7737 continue;
7738 }
7739 }
7740
7741 if (c == term && !simple_re_meta(c)) {
7742 tokadd(p, c);
7743 continue;
7744 }
7745 pushback(p, c);
7746 if ((c = tokadd_escape(p, enc)) < 0)
7747 return -1;
7748 if (*enc && *enc != *encp) {
7749 mixed_escape(p->lex.ptok+2, *enc, *encp);
7750 }
7751 continue;
7752 }
7753 else if (func & STR_FUNC_EXPAND) {
7754 pushback(p, c);
7755 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7756 c = read_escape(p, 0, enc);
7757 }
7758 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7759 /* ignore backslashed spaces in %w */
7760 }
7761 else if (c != term && !(paren && c == paren)) {
7762 tokadd(p, '\\');
7763 pushback(p, c);
7764 continue;
7765 }
7766 }
7767 }
7768 else if (!parser_isascii(p)) {
7769 non_ascii:
7770 if (!*enc) {
7771 *enc = *encp;
7772 }
7773 else if (*enc != *encp) {
7774 mixed_error(*enc, *encp);
7775 continue;
7776 }
7777 if (tokadd_mbchar(p, c) == -1) return -1;
7778 continue;
7779 }
7780 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7781 pushback(p, c);
7782 break;
7783 }
7784 if (c & 0x80) {
7785 if (!*enc) {
7786 *enc = *encp;
7787 }
7788 else if (*enc != *encp) {
7789 mixed_error(*enc, *encp);
7790 continue;
7791 }
7792 }
7793 tokadd(p, c);
7794#ifdef RIPPER
7795 top_of_line = (c == '\n');
7796#endif
7797 }
7798 terminate:
7799 if (*enc) *encp = *enc;
7800 return c;
7801}
7802
7803static inline rb_strterm_t *
7804new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7805{
7806 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7807}
7808
7809/* imemo_parser_strterm for literal */
7810#define NEW_STRTERM(func, term, paren) \
7811 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7812
7813#ifdef RIPPER
7814static void
7815flush_string_content(struct parser_params *p, rb_encoding *enc)
7816{
7817 VALUE content = yylval.val;
7818 if (!ripper_is_node_yylval(content))
7819 content = ripper_new_yylval(p, 0, 0, content);
7820 if (has_delayed_token(p)) {
7821 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7822 if (len > 0) {
7823 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7824 }
7825 dispatch_delayed_token(p, tSTRING_CONTENT);
7826 p->lex.ptok = p->lex.pcur;
7827 RNODE(content)->nd_rval = yylval.val;
7828 }
7829 dispatch_scan_event(p, tSTRING_CONTENT);
7830 if (yylval.val != content)
7831 RNODE(content)->nd_rval = yylval.val;
7832 yylval.val = content;
7833}
7834#else
7835static void
7836flush_string_content(struct parser_params *p, rb_encoding *enc)
7837{
7838 if (has_delayed_token(p)) {
7839 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7840 if (len > 0) {
7841 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7842 p->delayed.end_line = p->ruby_sourceline;
7843 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
7844 }
7845 dispatch_delayed_token(p, tSTRING_CONTENT);
7846 p->lex.ptok = p->lex.pcur;
7847 }
7848 dispatch_scan_event(p, tSTRING_CONTENT);
7849}
7850#endif
7851
7852RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7853/* this can be shared with ripper, since it's independent from struct
7854 * parser_params. */
7855#ifndef RIPPER
7856#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7857#define SPECIAL_PUNCT(idx) ( \
7858 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7859 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7860 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7861 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7862 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7863 BIT('0', idx))
7864const unsigned int ruby_global_name_punct_bits[] = {
7865 SPECIAL_PUNCT(0),
7866 SPECIAL_PUNCT(1),
7867 SPECIAL_PUNCT(2),
7868};
7869#undef BIT
7870#undef SPECIAL_PUNCT
7871#endif
7872
7873static enum yytokentype
7874parser_peek_variable_name(struct parser_params *p)
7875{
7876 int c;
7877 const char *ptr = p->lex.pcur;
7878
7879 if (ptr + 1 >= p->lex.pend) return 0;
7880 c = *ptr++;
7881 switch (c) {
7882 case '$':
7883 if ((c = *ptr) == '-') {
7884 if (++ptr >= p->lex.pend) return 0;
7885 c = *ptr;
7886 }
7887 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7888 return tSTRING_DVAR;
7889 }
7890 break;
7891 case '@':
7892 if ((c = *ptr) == '@') {
7893 if (++ptr >= p->lex.pend) return 0;
7894 c = *ptr;
7895 }
7896 break;
7897 case '{':
7898 p->lex.pcur = ptr;
7899 p->command_start = TRUE;
7900 return tSTRING_DBEG;
7901 default:
7902 return 0;
7903 }
7904 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7905 return tSTRING_DVAR;
7906 return 0;
7907}
7908
7909#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7910#define IS_END() IS_lex_state(EXPR_END_ANY)
7911#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7912#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7913#define IS_LABEL_POSSIBLE() (\
7914 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7915 IS_ARG())
7916#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7917#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7918
7919static inline enum yytokentype
7920parser_string_term(struct parser_params *p, int func)
7921{
7922 p->lex.strterm = 0;
7923 if (func & STR_FUNC_REGEXP) {
7924 set_yylval_num(regx_options(p));
7925 dispatch_scan_event(p, tREGEXP_END);
7926 SET_LEX_STATE(EXPR_END);
7927 return tREGEXP_END;
7928 }
7929 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7930 nextc(p);
7931 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7932 return tLABEL_END;
7933 }
7934 SET_LEX_STATE(EXPR_END);
7935 return tSTRING_END;
7936}
7937
7938static enum yytokentype
7939parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7940{
7941 int func = (int)quote->u1.func;
7942 int term = (int)quote->u3.term;
7943 int paren = (int)quote->u2.paren;
7944 int c, space = 0;
7945 rb_encoding *enc = p->enc;
7946 rb_encoding *base_enc = 0;
7947 VALUE lit;
7948
7949 if (func & STR_FUNC_TERM) {
7950 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7951 SET_LEX_STATE(EXPR_END);
7952 p->lex.strterm = 0;
7953 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7954 }
7955 c = nextc(p);
7956 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7957 do {c = nextc(p);} while (ISSPACE(c));
7958 space = 1;
7959 }
7960 if (func & STR_FUNC_LIST) {
7961 quote->u1.func &= ~STR_FUNC_LIST;
7962 space = 1;
7963 }
7964 if (c == term && !quote->u0.nest) {
7965 if (func & STR_FUNC_QWORDS) {
7966 quote->u1.func |= STR_FUNC_TERM;
7967 pushback(p, c); /* dispatch the term at tSTRING_END */
7968 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7969 return ' ';
7970 }
7971 return parser_string_term(p, func);
7972 }
7973 if (space) {
7974 pushback(p, c);
7975 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7976 return ' ';
7977 }
7978 newtok(p);
7979 if ((func & STR_FUNC_EXPAND) && c == '#') {
7980 int t = parser_peek_variable_name(p);
7981 if (t) return t;
7982 tokadd(p, '#');
7983 c = nextc(p);
7984 }
7985 pushback(p, c);
7986 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7987 &enc, &base_enc) == -1) {
7988 if (p->eofp) {
7989#ifndef RIPPER
7990# define unterminated_literal(mesg) yyerror0(mesg)
7991#else
7992# define unterminated_literal(mesg) compile_error(p, mesg)
7993#endif
7994 literal_flush(p, p->lex.pcur);
7995 if (func & STR_FUNC_QWORDS) {
7996 /* no content to add, bailing out here */
7997 unterminated_literal("unterminated list meets end of file");
7998 p->lex.strterm = 0;
7999 return tSTRING_END;
8000 }
8001 if (func & STR_FUNC_REGEXP) {
8002 unterminated_literal("unterminated regexp meets end of file");
8003 }
8004 else {
8005 unterminated_literal("unterminated string meets end of file");
8006 }
8007 quote->u1.func |= STR_FUNC_TERM;
8008 }
8009 }
8010
8011 tokfix(p);
8012 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8013 set_yylval_str(lit);
8014 flush_string_content(p, enc);
8015
8016 return tSTRING_CONTENT;
8017}
8018
8019static enum yytokentype
8020heredoc_identifier(struct parser_params *p)
8021{
8022 /*
8023 * term_len is length of `<<"END"` except `END`,
8024 * in this case term_len is 4 (<, <, " and ").
8025 */
8026 long len, offset = p->lex.pcur - p->lex.pbeg;
8027 int c = nextc(p), term, func = 0, quote = 0;
8028 enum yytokentype token = tSTRING_BEG;
8029 int indent = 0;
8030
8031 if (c == '-') {
8032 c = nextc(p);
8033 func = STR_FUNC_INDENT;
8034 offset++;
8035 }
8036 else if (c == '~') {
8037 c = nextc(p);
8038 func = STR_FUNC_INDENT;
8039 offset++;
8040 indent = INT_MAX;
8041 }
8042 switch (c) {
8043 case '\'':
8044 func |= str_squote; goto quoted;
8045 case '"':
8046 func |= str_dquote; goto quoted;
8047 case '`':
8048 token = tXSTRING_BEG;
8049 func |= str_xquote; goto quoted;
8050
8051 quoted:
8052 quote++;
8053 offset++;
8054 term = c;
8055 len = 0;
8056 while ((c = nextc(p)) != term) {
8057 if (c == -1 || c == '\r' || c == '\n') {
8058 yyerror0("unterminated here document identifier");
8059 return -1;
8060 }
8061 }
8062 break;
8063
8064 default:
8065 if (!parser_is_identchar(p)) {
8066 pushback(p, c);
8067 if (func & STR_FUNC_INDENT) {
8068 pushback(p, indent > 0 ? '~' : '-');
8069 }
8070 return 0;
8071 }
8072 func |= str_dquote;
8073 do {
8074 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8075 if (n < 0) return 0;
8076 p->lex.pcur += --n;
8077 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8078 pushback(p, c);
8079 break;
8080 }
8081
8082 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8083 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8084 yyerror0("too long here document identifier");
8085 dispatch_scan_event(p, tHEREDOC_BEG);
8086 lex_goto_eol(p);
8087
8088 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
8089 p->lex.strterm->flags |= STRTERM_HEREDOC;
8090 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8091 here->offset = offset;
8092 here->sourceline = p->ruby_sourceline;
8093 here->length = (int)len;
8094 here->quote = quote;
8095 here->func = func;
8096
8097 token_flush(p);
8098 p->heredoc_indent = indent;
8099 p->heredoc_line_indent = 0;
8100 return token;
8101}
8102
8103static void
8104heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8105{
8106 VALUE line;
8107
8108 p->lex.strterm = 0;
8109 line = here->lastline;
8110 p->lex.lastline = line;
8111 p->lex.pbeg = RSTRING_PTR(line);
8112 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8113 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8114 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8115 p->heredoc_end = p->ruby_sourceline;
8116 p->ruby_sourceline = (int)here->sourceline;
8117 if (p->eofp) p->lex.nextline = Qnil;
8118 p->eofp = 0;
8119}
8120
8121static int
8122dedent_string(VALUE string, int width)
8123{
8124 char *str;
8125 long len;
8126 int i, col = 0;
8127
8128 RSTRING_GETMEM(string, str, len);
8129 for (i = 0; i < len && col < width; i++) {
8130 if (str[i] == ' ') {
8131 col++;
8132 }
8133 else if (str[i] == '\t') {
8134 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8135 if (n > width) break;
8136 col = n;
8137 }
8138 else {
8139 break;
8140 }
8141 }
8142 if (!i) return 0;
8143 rb_str_modify(string);
8144 str = RSTRING_PTR(string);
8145 if (RSTRING_LEN(string) != len)
8146 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8147 MEMMOVE(str, str + i, char, len - i);
8148 rb_str_set_len(string, len - i);
8149 return i;
8150}
8151
8152#ifndef RIPPER
8153static NODE *
8154heredoc_dedent(struct parser_params *p, NODE *root)
8155{
8156 NODE *node, *str_node, *prev_node;
8157 int indent = p->heredoc_indent;
8158 VALUE prev_lit = 0;
8159
8160 if (indent <= 0) return root;
8161 p->heredoc_indent = 0;
8162 if (!root) return root;
8163
8164 prev_node = node = str_node = root;
8165 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
8166
8167 while (str_node) {
8168 VALUE lit = str_node->nd_lit;
8169 if (str_node->flags & NODE_FL_NEWLINE) {
8170 dedent_string(lit, indent);
8171 }
8172 if (!prev_lit) {
8173 prev_lit = lit;
8174 }
8175 else if (!literal_concat0(p, prev_lit, lit)) {
8176 return 0;
8177 }
8178 else {
8179 NODE *end = node->nd_end;
8180 node = prev_node->nd_next = node->nd_next;
8181 if (!node) {
8182 if (nd_type_p(prev_node, NODE_DSTR))
8183 nd_set_type(prev_node, NODE_STR);
8184 break;
8185 }
8186 node->nd_end = end;
8187 goto next_str;
8188 }
8189
8190 str_node = 0;
8191 while ((node = (prev_node = node)->nd_next) != 0) {
8192 next_str:
8193 if (!nd_type_p(node, NODE_LIST)) break;
8194 if ((str_node = node->nd_head) != 0) {
8195 enum node_type type = nd_type(str_node);
8196 if (type == NODE_STR || type == NODE_DSTR) break;
8197 prev_lit = 0;
8198 str_node = 0;
8199 }
8200 }
8201 }
8202 return root;
8203}
8204#else /* RIPPER */
8205static VALUE
8206heredoc_dedent(struct parser_params *p, VALUE array)
8207{
8208 int indent = p->heredoc_indent;
8209
8210 if (indent <= 0) return array;
8211 p->heredoc_indent = 0;
8212 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8213 return array;
8214}
8215
8216/*
8217 * call-seq:
8218 * Ripper.dedent_string(input, width) -> Integer
8219 *
8220 * USE OF RIPPER LIBRARY ONLY.
8221 *
8222 * Strips up to +width+ leading whitespaces from +input+,
8223 * and returns the stripped column width.
8224 */
8225static VALUE
8226parser_dedent_string(VALUE self, VALUE input, VALUE width)
8227{
8228 int wid, col;
8229
8230 StringValue(input);
8231 wid = NUM2UINT(width);
8232 col = dedent_string(input, wid);
8233 return INT2NUM(col);
8234}
8235#endif
8236
8237static int
8238whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8239{
8240 const char *ptr = p->lex.pbeg;
8241 long n;
8242
8243 if (indent) {
8244 while (*ptr && ISSPACE(*ptr)) ptr++;
8245 }
8246 n = p->lex.pend - (ptr + len);
8247 if (n < 0) return FALSE;
8248 if (n > 0 && ptr[len] != '\n') {
8249 if (ptr[len] != '\r') return FALSE;
8250 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
8251 }
8252 return strncmp(eos, ptr, len) == 0;
8253}
8254
8255static int
8256word_match_p(struct parser_params *p, const char *word, long len)
8257{
8258 if (strncmp(p->lex.pcur, word, len)) return 0;
8259 if (p->lex.pcur + len == p->lex.pend) return 1;
8260 int c = (unsigned char)p->lex.pcur[len];
8261 if (ISSPACE(c)) return 1;
8262 switch (c) {
8263 case '\0': case '\004': case '\032': return 1;
8264 }
8265 return 0;
8266}
8267
8268#define NUM_SUFFIX_R (1<<0)
8269#define NUM_SUFFIX_I (1<<1)
8270#define NUM_SUFFIX_ALL 3
8271
8272static int
8273number_literal_suffix(struct parser_params *p, int mask)
8274{
8275 int c, result = 0;
8276 const char *lastp = p->lex.pcur;
8277
8278 while ((c = nextc(p)) != -1) {
8279 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8280 result |= (mask & NUM_SUFFIX_I);
8281 mask &= ~NUM_SUFFIX_I;
8282 /* r after i, rational of complex is disallowed */
8283 mask &= ~NUM_SUFFIX_R;
8284 continue;
8285 }
8286 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8287 result |= (mask & NUM_SUFFIX_R);
8288 mask &= ~NUM_SUFFIX_R;
8289 continue;
8290 }
8291 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8292 p->lex.pcur = lastp;
8293 literal_flush(p, p->lex.pcur);
8294 return 0;
8295 }
8296 pushback(p, c);
8297 break;
8298 }
8299 return result;
8300}
8301
8302static enum yytokentype
8303set_number_literal(struct parser_params *p, VALUE v,
8304 enum yytokentype type, int suffix)
8305{
8306 if (suffix & NUM_SUFFIX_I) {
8307 v = rb_complex_raw(INT2FIX(0), v);
8308 type = tIMAGINARY;
8309 }
8310 set_yylval_literal(v);
8311 SET_LEX_STATE(EXPR_END);
8312 return type;
8313}
8314
8315static enum yytokentype
8316set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8317{
8318 enum yytokentype type = tINTEGER;
8319 if (suffix & NUM_SUFFIX_R) {
8320 v = rb_rational_raw1(v);
8321 type = tRATIONAL;
8322 }
8323 return set_number_literal(p, v, type, suffix);
8324}
8325
8326#ifdef RIPPER
8327static void
8328dispatch_heredoc_end(struct parser_params *p)
8329{
8330 VALUE str;
8331 if (has_delayed_token(p))
8332 dispatch_delayed_token(p, tSTRING_CONTENT);
8333 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8334 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8335 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8336 lex_goto_eol(p);
8337 token_flush(p);
8338}
8339
8340#else
8341#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8342static void
8343parser_dispatch_heredoc_end(struct parser_params *p, int line)
8344{
8345 if (has_delayed_token(p))
8346 dispatch_delayed_token(p, tSTRING_CONTENT);
8347
8348 if (p->keep_tokens) {
8349 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8350 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8351 parser_append_tokens(p, str, tHEREDOC_END, line);
8352 }
8353
8354 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8355 lex_goto_eol(p);
8356 token_flush(p);
8357}
8358#endif
8359
8360static enum yytokentype
8361here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8362{
8363 int c, func, indent = 0;
8364 const char *eos, *ptr, *ptr_end;
8365 long len;
8366 VALUE str = 0;
8367 rb_encoding *enc = p->enc;
8368 rb_encoding *base_enc = 0;
8369 int bol;
8370
8371 eos = RSTRING_PTR(here->lastline) + here->offset;
8372 len = here->length;
8373 indent = (func = here->func) & STR_FUNC_INDENT;
8374
8375 if ((c = nextc(p)) == -1) {
8376 error:
8377#ifdef RIPPER
8378 if (!has_delayed_token(p)) {
8379 dispatch_scan_event(p, tSTRING_CONTENT);
8380 }
8381 else {
8382 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
8383 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
8384 int cr = ENC_CODERANGE_UNKNOWN;
8385 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
8386 if (cr != ENC_CODERANGE_7BIT &&
8387 rb_is_usascii_enc(p->enc) &&
8388 enc != rb_utf8_encoding()) {
8389 enc = rb_ascii8bit_encoding();
8390 }
8391 }
8392 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8393 }
8394 dispatch_delayed_token(p, tSTRING_CONTENT);
8395 }
8396 lex_goto_eol(p);
8397#endif
8398 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8399 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
8400 (int)len, eos);
8401 token_flush(p);
8402 p->lex.strterm = 0;
8403 SET_LEX_STATE(EXPR_END);
8404 return tSTRING_END;
8405 }
8406 bol = was_bol(p);
8407 if (!bol) {
8408 /* not beginning of line, cannot be the terminator */
8409 }
8410 else if (p->heredoc_line_indent == -1) {
8411 /* `heredoc_line_indent == -1` means
8412 * - "after an interpolation in the same line", or
8413 * - "in a continuing line"
8414 */
8415 p->heredoc_line_indent = 0;
8416 }
8417 else if (whole_match_p(p, eos, len, indent)) {
8418 dispatch_heredoc_end(p);
8419 restore:
8420 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8421 token_flush(p);
8422 p->lex.strterm = 0;
8423 SET_LEX_STATE(EXPR_END);
8424 return tSTRING_END;
8425 }
8426
8427 if (!(func & STR_FUNC_EXPAND)) {
8428 do {
8429 ptr = RSTRING_PTR(p->lex.lastline);
8430 ptr_end = p->lex.pend;
8431 if (ptr_end > ptr) {
8432 switch (ptr_end[-1]) {
8433 case '\n':
8434 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
8435 ptr_end++;
8436 break;
8437 }
8438 case '\r':
8439 --ptr_end;
8440 }
8441 }
8442
8443 if (p->heredoc_indent > 0) {
8444 long i = 0;
8445 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
8446 i++;
8447 p->heredoc_line_indent = 0;
8448 }
8449
8450 if (str)
8451 rb_str_cat(str, ptr, ptr_end - ptr);
8452 else
8453 str = STR_NEW(ptr, ptr_end - ptr);
8454 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
8455 lex_goto_eol(p);
8456 if (p->heredoc_indent > 0) {
8457 goto flush_str;
8458 }
8459 if (nextc(p) == -1) {
8460 if (str) {
8461 str = 0;
8462 }
8463 goto error;
8464 }
8465 } while (!whole_match_p(p, eos, len, indent));
8466 }
8467 else {
8468 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
8469 newtok(p);
8470 if (c == '#') {
8471 int t = parser_peek_variable_name(p);
8472 if (p->heredoc_line_indent != -1) {
8473 if (p->heredoc_indent > p->heredoc_line_indent) {
8474 p->heredoc_indent = p->heredoc_line_indent;
8475 }
8476 p->heredoc_line_indent = -1;
8477 }
8478 if (t) return t;
8479 tokadd(p, '#');
8480 c = nextc(p);
8481 }
8482 do {
8483 pushback(p, c);
8484 enc = p->enc;
8485 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
8486 if (p->eofp) goto error;
8487 goto restore;
8488 }
8489 if (c != '\n') {
8490 if (c == '\\') p->heredoc_line_indent = -1;
8491 flush:
8492 str = STR_NEW3(tok(p), toklen(p), enc, func);
8493 flush_str:
8494 set_yylval_str(str);
8495#ifndef RIPPER
8496 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8497#endif
8498 flush_string_content(p, enc);
8499 return tSTRING_CONTENT;
8500 }
8501 tokadd(p, nextc(p));
8502 if (p->heredoc_indent > 0) {
8503 lex_goto_eol(p);
8504 goto flush;
8505 }
8506 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8507 if ((c = nextc(p)) == -1) goto error;
8508 } while (!whole_match_p(p, eos, len, indent));
8509 str = STR_NEW3(tok(p), toklen(p), enc, func);
8510 }
8511 dispatch_heredoc_end(p);
8512#ifdef RIPPER
8513 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8514 yylval.val, str);
8515#endif
8516 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8517 token_flush(p);
8518 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8519 set_yylval_str(str);
8520#ifndef RIPPER
8521 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8522#endif
8523 return tSTRING_CONTENT;
8524}
8525
8526#include "lex.c"
8527
8528static int
8529arg_ambiguous(struct parser_params *p, char c)
8530{
8531#ifndef RIPPER
8532 if (c == '/') {
8533 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8534 }
8535 else {
8536 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8537 }
8538#else
8539 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8540#endif
8541 return TRUE;
8542}
8543
8544static ID
8545#ifndef RIPPER
8546formal_argument(struct parser_params *p, ID lhs)
8547#else
8548formal_argument(struct parser_params *p, VALUE lhs)
8549#endif
8550{
8551 ID id = get_id(lhs);
8552
8553 switch (id_type(id)) {
8554 case ID_LOCAL:
8555 break;
8556#ifndef RIPPER
8557# define ERR(mesg) yyerror0(mesg)
8558#else
8559# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8560#endif
8561 case ID_CONST:
8562 ERR("formal argument cannot be a constant");
8563 return 0;
8564 case ID_INSTANCE:
8565 ERR("formal argument cannot be an instance variable");
8566 return 0;
8567 case ID_GLOBAL:
8568 ERR("formal argument cannot be a global variable");
8569 return 0;
8570 case ID_CLASS:
8571 ERR("formal argument cannot be a class variable");
8572 return 0;
8573 default:
8574 ERR("formal argument must be local variable");
8575 return 0;
8576#undef ERR
8577 }
8578 shadowing_lvar(p, id);
8579 return lhs;
8580}
8581
8582static int
8583lvar_defined(struct parser_params *p, ID id)
8584{
8585 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8586}
8587
8588/* emacsen -*- hack */
8589static long
8590parser_encode_length(struct parser_params *p, const char *name, long len)
8591{
8592 long nlen;
8593
8594 if (len > 5 && name[nlen = len - 5] == '-') {
8595 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8596 return nlen;
8597 }
8598 if (len > 4 && name[nlen = len - 4] == '-') {
8599 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8600 return nlen;
8601 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8602 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8603 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8604 return nlen;
8605 }
8606 return len;
8607}
8608
8609static void
8610parser_set_encode(struct parser_params *p, const char *name)
8611{
8612 int idx = rb_enc_find_index(name);
8613 rb_encoding *enc;
8614 VALUE excargs[3];
8615
8616 if (idx < 0) {
8617 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8618 error:
8619 excargs[0] = rb_eArgError;
8620 excargs[2] = rb_make_backtrace();
8621 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8622 rb_exc_raise(rb_make_exception(3, excargs));
8623 }
8624 enc = rb_enc_from_index(idx);
8625 if (!rb_enc_asciicompat(enc)) {
8626 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8627 goto error;
8628 }
8629 p->enc = enc;
8630#ifndef RIPPER
8631 if (p->debug_lines) {
8632 VALUE lines = p->debug_lines;
8633 long i, n = RARRAY_LEN(lines);
8634 for (i = 0; i < n; ++i) {
8635 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8636 }
8637 }
8638#endif
8639}
8640
8641static int
8642comment_at_top(struct parser_params *p)
8643{
8644 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8645 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8646 while (ptr < ptr_end) {
8647 if (!ISSPACE(*ptr)) return 0;
8648 ptr++;
8649 }
8650 return 1;
8651}
8652
8653typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8654typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8655
8656static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8657
8658static void
8659magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8660{
8661 if (!comment_at_top(p)) {
8662 return;
8663 }
8664 parser_set_encode(p, val);
8665}
8666
8667static int
8668parser_get_bool(struct parser_params *p, const char *name, const char *val)
8669{
8670 switch (*val) {
8671 case 't': case 'T':
8672 if (STRCASECMP(val, "true") == 0) {
8673 return TRUE;
8674 }
8675 break;
8676 case 'f': case 'F':
8677 if (STRCASECMP(val, "false") == 0) {
8678 return FALSE;
8679 }
8680 break;
8681 }
8682 return parser_invalid_pragma_value(p, name, val);
8683}
8684
8685static int
8686parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8687{
8688 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8689 return -1;
8690}
8691
8692static void
8693parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8694{
8695 int b = parser_get_bool(p, name, val);
8696 if (b >= 0) p->token_info_enabled = b;
8697}
8698
8699static void
8700parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8701{
8702 int b;
8703
8704 if (p->token_seen) {
8705 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8706 return;
8707 }
8708
8709 b = parser_get_bool(p, name, val);
8710 if (b < 0) return;
8711
8712 if (!p->compile_option)
8713 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8714 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8715 RBOOL(b));
8716}
8717
8718static void
8719parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8720{
8721 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8722 if (*s == ' ' || *s == '\t') continue;
8723 if (*s == '#') break;
8724 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8725 return;
8726 }
8727
8728 switch (*val) {
8729 case 'n': case 'N':
8730 if (STRCASECMP(val, "none") == 0) {
8731 p->ctxt.shareable_constant_value = shareable_none;
8732 return;
8733 }
8734 break;
8735 case 'l': case 'L':
8736 if (STRCASECMP(val, "literal") == 0) {
8737 p->ctxt.shareable_constant_value = shareable_literal;
8738 return;
8739 }
8740 break;
8741 case 'e': case 'E':
8742 if (STRCASECMP(val, "experimental_copy") == 0) {
8743 p->ctxt.shareable_constant_value = shareable_copy;
8744 return;
8745 }
8746 if (STRCASECMP(val, "experimental_everything") == 0) {
8747 p->ctxt.shareable_constant_value = shareable_everything;
8748 return;
8749 }
8750 break;
8751 }
8752 parser_invalid_pragma_value(p, name, val);
8753}
8754
8755# if WARN_PAST_SCOPE
8756static void
8757parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8758{
8759 int b = parser_get_bool(p, name, val);
8760 if (b >= 0) p->past_scope_enabled = b;
8761}
8762# endif
8763
8764struct magic_comment {
8765 const char *name;
8766 rb_magic_comment_setter_t func;
8767 rb_magic_comment_length_t length;
8768};
8769
8770static const struct magic_comment magic_comments[] = {
8771 {"coding", magic_comment_encoding, parser_encode_length},
8772 {"encoding", magic_comment_encoding, parser_encode_length},
8773 {"frozen_string_literal", parser_set_compile_option_flag},
8774 {"shareable_constant_value", parser_set_shareable_constant_value},
8775 {"warn_indent", parser_set_token_info},
8776# if WARN_PAST_SCOPE
8777 {"warn_past_scope", parser_set_past_scope},
8778# endif
8779};
8780
8781static const char *
8782magic_comment_marker(const char *str, long len)
8783{
8784 long i = 2;
8785
8786 while (i < len) {
8787 switch (str[i]) {
8788 case '-':
8789 if (str[i-1] == '*' && str[i-2] == '-') {
8790 return str + i + 1;
8791 }
8792 i += 2;
8793 break;
8794 case '*':
8795 if (i + 1 >= len) return 0;
8796 if (str[i+1] != '-') {
8797 i += 4;
8798 }
8799 else if (str[i-1] != '-') {
8800 i += 2;
8801 }
8802 else {
8803 return str + i + 2;
8804 }
8805 break;
8806 default:
8807 i += 3;
8808 break;
8809 }
8810 }
8811 return 0;
8812}
8813
8814static int
8815parser_magic_comment(struct parser_params *p, const char *str, long len)
8816{
8817 int indicator = 0;
8818 VALUE name = 0, val = 0;
8819 const char *beg, *end, *vbeg, *vend;
8820#define str_copy(_s, _p, _n) ((_s) \
8821 ? (void)(rb_str_resize((_s), (_n)), \
8822 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8823 : (void)((_s) = STR_NEW((_p), (_n))))
8824
8825 if (len <= 7) return FALSE;
8826 if (!!(beg = magic_comment_marker(str, len))) {
8827 if (!(end = magic_comment_marker(beg, str + len - beg)))
8828 return FALSE;
8829 indicator = TRUE;
8830 str = beg;
8831 len = end - beg - 3;
8832 }
8833
8834 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8835 while (len > 0) {
8836 const struct magic_comment *mc = magic_comments;
8837 char *s;
8838 int i;
8839 long n = 0;
8840
8841 for (; len > 0 && *str; str++, --len) {
8842 switch (*str) {
8843 case '\'': case '"': case ':': case ';':
8844 continue;
8845 }
8846 if (!ISSPACE(*str)) break;
8847 }
8848 for (beg = str; len > 0; str++, --len) {
8849 switch (*str) {
8850 case '\'': case '"': case ':': case ';':
8851 break;
8852 default:
8853 if (ISSPACE(*str)) break;
8854 continue;
8855 }
8856 break;
8857 }
8858 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8859 if (!len) break;
8860 if (*str != ':') {
8861 if (!indicator) return FALSE;
8862 continue;
8863 }
8864
8865 do str++; while (--len > 0 && ISSPACE(*str));
8866 if (!len) break;
8867 if (*str == '"') {
8868 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8869 if (*str == '\\') {
8870 --len;
8871 ++str;
8872 }
8873 }
8874 vend = str;
8875 if (len) {
8876 --len;
8877 ++str;
8878 }
8879 }
8880 else {
8881 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8882 vend = str;
8883 }
8884 if (indicator) {
8885 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8886 }
8887 else {
8888 while (len > 0 && (ISSPACE(*str))) --len, str++;
8889 if (len) return FALSE;
8890 }
8891
8892 n = end - beg;
8893 str_copy(name, beg, n);
8894 s = RSTRING_PTR(name);
8895 for (i = 0; i < n; ++i) {
8896 if (s[i] == '-') s[i] = '_';
8897 }
8898 do {
8899 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8900 n = vend - vbeg;
8901 if (mc->length) {
8902 n = (*mc->length)(p, vbeg, n);
8903 }
8904 str_copy(val, vbeg, n);
8905 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8906 break;
8907 }
8908 } while (++mc < magic_comments + numberof(magic_comments));
8909#ifdef RIPPER
8910 str_copy(val, vbeg, vend - vbeg);
8911 dispatch2(magic_comment, name, val);
8912#endif
8913 }
8914
8915 return TRUE;
8916}
8917
8918static void
8919set_file_encoding(struct parser_params *p, const char *str, const char *send)
8920{
8921 int sep = 0;
8922 const char *beg = str;
8923 VALUE s;
8924
8925 for (;;) {
8926 if (send - str <= 6) return;
8927 switch (str[6]) {
8928 case 'C': case 'c': str += 6; continue;
8929 case 'O': case 'o': str += 5; continue;
8930 case 'D': case 'd': str += 4; continue;
8931 case 'I': case 'i': str += 3; continue;
8932 case 'N': case 'n': str += 2; continue;
8933 case 'G': case 'g': str += 1; continue;
8934 case '=': case ':':
8935 sep = 1;
8936 str += 6;
8937 break;
8938 default:
8939 str += 6;
8940 if (ISSPACE(*str)) break;
8941 continue;
8942 }
8943 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8944 sep = 0;
8945 }
8946 for (;;) {
8947 do {
8948 if (++str >= send) return;
8949 } while (ISSPACE(*str));
8950 if (sep) break;
8951 if (*str != '=' && *str != ':') return;
8952 sep = 1;
8953 str++;
8954 }
8955 beg = str;
8956 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8957 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8958 parser_set_encode(p, RSTRING_PTR(s));
8959 rb_str_resize(s, 0);
8960}
8961
8962static void
8963parser_prepare(struct parser_params *p)
8964{
8965 int c = nextc0(p, FALSE);
8966 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8967 switch (c) {
8968 case '#':
8969 if (peek(p, '!')) p->has_shebang = 1;
8970 break;
8971 case 0xef: /* UTF-8 BOM marker */
8972 if (p->lex.pend - p->lex.pcur >= 2 &&
8973 (unsigned char)p->lex.pcur[0] == 0xbb &&
8974 (unsigned char)p->lex.pcur[1] == 0xbf) {
8975 p->enc = rb_utf8_encoding();
8976 p->lex.pcur += 2;
8977#ifndef RIPPER
8978 if (p->debug_lines) {
8979 rb_enc_associate(p->lex.lastline, p->enc);
8980 }
8981#endif
8982 p->lex.pbeg = p->lex.pcur;
8983 return;
8984 }
8985 break;
8986 case EOF:
8987 return;
8988 }
8989 pushback(p, c);
8990 p->enc = rb_enc_get(p->lex.lastline);
8991}
8992
8993#ifndef RIPPER
8994#define ambiguous_operator(tok, op, syn) ( \
8995 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8996 rb_warning0("even though it seems like "syn""))
8997#else
8998#define ambiguous_operator(tok, op, syn) \
8999 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9000#endif
9001#define warn_balanced(tok, op, syn) ((void) \
9002 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9003 space_seen && !ISSPACE(c) && \
9004 (ambiguous_operator(tok, op, syn), 0)), \
9005 (enum yytokentype)(tok))
9006
9007static VALUE
9008parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9009{
9010 VALUE v;
9011 char *point = &str[seen_point];
9012 size_t fraclen = len-seen_point-1;
9013 memmove(point, point+1, fraclen+1);
9014 v = rb_cstr_to_inum(str, 10, FALSE);
9015 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9016}
9017
9018static enum yytokentype
9019no_digits(struct parser_params *p)
9020{
9021 yyerror0("numeric literal without digits");
9022 if (peek(p, '_')) nextc(p);
9023 /* dummy 0, for tUMINUS_NUM at numeric */
9024 return set_integer_literal(p, INT2FIX(0), 0);
9025}
9026
9027static enum yytokentype
9028parse_numeric(struct parser_params *p, int c)
9029{
9030 int is_float, seen_point, seen_e, nondigit;
9031 int suffix;
9032
9033 is_float = seen_point = seen_e = nondigit = 0;
9034 SET_LEX_STATE(EXPR_END);
9035 newtok(p);
9036 if (c == '-' || c == '+') {
9037 tokadd(p, c);
9038 c = nextc(p);
9039 }
9040 if (c == '0') {
9041 int start = toklen(p);
9042 c = nextc(p);
9043 if (c == 'x' || c == 'X') {
9044 /* hexadecimal */
9045 c = nextc(p);
9046 if (c != -1 && ISXDIGIT(c)) {
9047 do {
9048 if (c == '_') {
9049 if (nondigit) break;
9050 nondigit = c;
9051 continue;
9052 }
9053 if (!ISXDIGIT(c)) break;
9054 nondigit = 0;
9055 tokadd(p, c);
9056 } while ((c = nextc(p)) != -1);
9057 }
9058 pushback(p, c);
9059 tokfix(p);
9060 if (toklen(p) == start) {
9061 return no_digits(p);
9062 }
9063 else if (nondigit) goto trailing_uc;
9064 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9065 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9066 }
9067 if (c == 'b' || c == 'B') {
9068 /* binary */
9069 c = nextc(p);
9070 if (c == '0' || c == '1') {
9071 do {
9072 if (c == '_') {
9073 if (nondigit) break;
9074 nondigit = c;
9075 continue;
9076 }
9077 if (c != '0' && c != '1') break;
9078 nondigit = 0;
9079 tokadd(p, c);
9080 } while ((c = nextc(p)) != -1);
9081 }
9082 pushback(p, c);
9083 tokfix(p);
9084 if (toklen(p) == start) {
9085 return no_digits(p);
9086 }
9087 else if (nondigit) goto trailing_uc;
9088 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9089 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9090 }
9091 if (c == 'd' || c == 'D') {
9092 /* decimal */
9093 c = nextc(p);
9094 if (c != -1 && ISDIGIT(c)) {
9095 do {
9096 if (c == '_') {
9097 if (nondigit) break;
9098 nondigit = c;
9099 continue;
9100 }
9101 if (!ISDIGIT(c)) break;
9102 nondigit = 0;
9103 tokadd(p, c);
9104 } while ((c = nextc(p)) != -1);
9105 }
9106 pushback(p, c);
9107 tokfix(p);
9108 if (toklen(p) == start) {
9109 return no_digits(p);
9110 }
9111 else if (nondigit) goto trailing_uc;
9112 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9113 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9114 }
9115 if (c == '_') {
9116 /* 0_0 */
9117 goto octal_number;
9118 }
9119 if (c == 'o' || c == 'O') {
9120 /* prefixed octal */
9121 c = nextc(p);
9122 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9123 return no_digits(p);
9124 }
9125 }
9126 if (c >= '0' && c <= '7') {
9127 /* octal */
9128 octal_number:
9129 do {
9130 if (c == '_') {
9131 if (nondigit) break;
9132 nondigit = c;
9133 continue;
9134 }
9135 if (c < '0' || c > '9') break;
9136 if (c > '7') goto invalid_octal;
9137 nondigit = 0;
9138 tokadd(p, c);
9139 } while ((c = nextc(p)) != -1);
9140 if (toklen(p) > start) {
9141 pushback(p, c);
9142 tokfix(p);
9143 if (nondigit) goto trailing_uc;
9144 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9145 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9146 }
9147 if (nondigit) {
9148 pushback(p, c);
9149 goto trailing_uc;
9150 }
9151 }
9152 if (c > '7' && c <= '9') {
9153 invalid_octal:
9154 yyerror0("Invalid octal digit");
9155 }
9156 else if (c == '.' || c == 'e' || c == 'E') {
9157 tokadd(p, '0');
9158 }
9159 else {
9160 pushback(p, c);
9161 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9162 return set_integer_literal(p, INT2FIX(0), suffix);
9163 }
9164 }
9165
9166 for (;;) {
9167 switch (c) {
9168 case '0': case '1': case '2': case '3': case '4':
9169 case '5': case '6': case '7': case '8': case '9':
9170 nondigit = 0;
9171 tokadd(p, c);
9172 break;
9173
9174 case '.':
9175 if (nondigit) goto trailing_uc;
9176 if (seen_point || seen_e) {
9177 goto decode_num;
9178 }
9179 else {
9180 int c0 = nextc(p);
9181 if (c0 == -1 || !ISDIGIT(c0)) {
9182 pushback(p, c0);
9183 goto decode_num;
9184 }
9185 c = c0;
9186 }
9187 seen_point = toklen(p);
9188 tokadd(p, '.');
9189 tokadd(p, c);
9190 is_float++;
9191 nondigit = 0;
9192 break;
9193
9194 case 'e':
9195 case 'E':
9196 if (nondigit) {
9197 pushback(p, c);
9198 c = nondigit;
9199 goto decode_num;
9200 }
9201 if (seen_e) {
9202 goto decode_num;
9203 }
9204 nondigit = c;
9205 c = nextc(p);
9206 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9207 pushback(p, c);
9208 nondigit = 0;
9209 goto decode_num;
9210 }
9211 tokadd(p, nondigit);
9212 seen_e++;
9213 is_float++;
9214 tokadd(p, c);
9215 nondigit = (c == '-' || c == '+') ? c : 0;
9216 break;
9217
9218 case '_': /* `_' in number just ignored */
9219 if (nondigit) goto decode_num;
9220 nondigit = c;
9221 break;
9222
9223 default:
9224 goto decode_num;
9225 }
9226 c = nextc(p);
9227 }
9228
9229 decode_num:
9230 pushback(p, c);
9231 if (nondigit) {
9232 trailing_uc:
9233 literal_flush(p, p->lex.pcur - 1);
9234 YYLTYPE loc = RUBY_INIT_YYLLOC();
9235 compile_error(p, "trailing `%c' in number", nondigit);
9236 parser_show_error_line(p, &loc);
9237 }
9238 tokfix(p);
9239 if (is_float) {
9240 enum yytokentype type = tFLOAT;
9241 VALUE v;
9242
9243 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9244 if (suffix & NUM_SUFFIX_R) {
9245 type = tRATIONAL;
9246 v = parse_rational(p, tok(p), toklen(p), seen_point);
9247 }
9248 else {
9249 double d = strtod(tok(p), 0);
9250 if (errno == ERANGE) {
9251 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9252 errno = 0;
9253 }
9254 v = DBL2NUM(d);
9255 }
9256 return set_number_literal(p, v, type, suffix);
9257 }
9258 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9259 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9260}
9261
9262static enum yytokentype
9263parse_qmark(struct parser_params *p, int space_seen)
9264{
9265 rb_encoding *enc;
9266 register int c;
9267 VALUE lit;
9268
9269 if (IS_END()) {
9270 SET_LEX_STATE(EXPR_VALUE);
9271 return '?';
9272 }
9273 c = nextc(p);
9274 if (c == -1) {
9275 compile_error(p, "incomplete character syntax");
9276 return 0;
9277 }
9278 if (rb_enc_isspace(c, p->enc)) {
9279 if (!IS_ARG()) {
9280 int c2 = escaped_control_code(c);
9281 if (c2) {
9282 WARN_SPACE_CHAR(c2, "?");
9283 }
9284 }
9285 ternary:
9286 pushback(p, c);
9287 SET_LEX_STATE(EXPR_VALUE);
9288 return '?';
9289 }
9290 newtok(p);
9291 enc = p->enc;
9292 if (!parser_isascii(p)) {
9293 if (tokadd_mbchar(p, c) == -1) return 0;
9294 }
9295 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9296 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
9297 if (space_seen) {
9298 const char *start = p->lex.pcur - 1, *ptr = start;
9299 do {
9300 int n = parser_precise_mbclen(p, ptr);
9301 if (n < 0) return -1;
9302 ptr += n;
9303 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
9304 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9305 " a conditional operator, put a space after `?'",
9306 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9307 }
9308 goto ternary;
9309 }
9310 else if (c == '\\') {
9311 if (peek(p, 'u')) {
9312 nextc(p);
9313 enc = rb_utf8_encoding();
9314 tokadd_utf8(p, &enc, -1, 0, 0);
9315 }
9316 else if (!ISASCII(c = peekc(p))) {
9317 nextc(p);
9318 if (tokadd_mbchar(p, c) == -1) return 0;
9319 }
9320 else {
9321 c = read_escape(p, 0, &enc);
9322 tokadd(p, c);
9323 }
9324 }
9325 else {
9326 tokadd(p, c);
9327 }
9328 tokfix(p);
9329 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9330 set_yylval_str(lit);
9331 SET_LEX_STATE(EXPR_END);
9332 return tCHAR;
9333}
9334
9335static enum yytokentype
9336parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9337{
9338 register int c;
9339 const char *ptok = p->lex.pcur;
9340
9341 if (IS_BEG()) {
9342 int term;
9343 int paren;
9344
9345 c = nextc(p);
9346 quotation:
9347 if (c == -1) goto unterminated;
9348 if (!ISALNUM(c)) {
9349 term = c;
9350 if (!ISASCII(c)) goto unknown;
9351 c = 'Q';
9352 }
9353 else {
9354 term = nextc(p);
9355 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9356 unknown:
9357 pushback(p, term);
9358 c = parser_precise_mbclen(p, p->lex.pcur);
9359 if (c < 0) return 0;
9360 p->lex.pcur += c;
9361 yyerror0("unknown type of %string");
9362 return 0;
9363 }
9364 }
9365 if (term == -1) {
9366 unterminated:
9367 compile_error(p, "unterminated quoted string meets end of file");
9368 return 0;
9369 }
9370 paren = term;
9371 if (term == '(') term = ')';
9372 else if (term == '[') term = ']';
9373 else if (term == '{') term = '}';
9374 else if (term == '<') term = '>';
9375 else paren = 0;
9376
9377 p->lex.ptok = ptok-1;
9378 switch (c) {
9379 case 'Q':
9380 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9381 return tSTRING_BEG;
9382
9383 case 'q':
9384 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
9385 return tSTRING_BEG;
9386
9387 case 'W':
9388 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9389 return tWORDS_BEG;
9390
9391 case 'w':
9392 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9393 return tQWORDS_BEG;
9394
9395 case 'I':
9396 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9397 return tSYMBOLS_BEG;
9398
9399 case 'i':
9400 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9401 return tQSYMBOLS_BEG;
9402
9403 case 'x':
9404 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
9405 return tXSTRING_BEG;
9406
9407 case 'r':
9408 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
9409 return tREGEXP_BEG;
9410
9411 case 's':
9412 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
9413 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
9414 return tSYMBEG;
9415
9416 default:
9417 yyerror0("unknown type of %string");
9418 return 0;
9419 }
9420 }
9421 if ((c = nextc(p)) == '=') {
9422 set_yylval_id('%');
9423 SET_LEX_STATE(EXPR_BEG);
9424 return tOP_ASGN;
9425 }
9426 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
9427 goto quotation;
9428 }
9429 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9430 pushback(p, c);
9431 return warn_balanced('%', "%%", "string literal");
9432}
9433
9434static int
9435tokadd_ident(struct parser_params *p, int c)
9436{
9437 do {
9438 if (tokadd_mbchar(p, c) == -1) return -1;
9439 c = nextc(p);
9440 } while (parser_is_identchar(p));
9441 pushback(p, c);
9442 return 0;
9443}
9444
9445static ID
9446tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
9447{
9448 ID ident = TOK_INTERN();
9449
9450 set_yylval_name(ident);
9451
9452 return ident;
9453}
9454
9455static int
9456parse_numvar(struct parser_params *p)
9457{
9458 size_t len;
9459 int overflow;
9460 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
9461 const unsigned long nth_ref_max =
9462 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
9463 /* NTH_REF is left-shifted to be ORed with back-ref flag and
9464 * turned into a Fixnum, in compile.c */
9465
9466 if (overflow || n > nth_ref_max) {
9467 /* compile_error()? */
9468 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
9469 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
9470 }
9471 else {
9472 return (int)n;
9473 }
9474}
9475
9476static enum yytokentype
9477parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
9478{
9479 const char *ptr = p->lex.pcur;
9480 register int c;
9481
9482 SET_LEX_STATE(EXPR_END);
9483 p->lex.ptok = ptr - 1; /* from '$' */
9484 newtok(p);
9485 c = nextc(p);
9486 switch (c) {
9487 case '_': /* $_: last read line string */
9488 c = nextc(p);
9489 if (parser_is_identchar(p)) {
9490 tokadd(p, '$');
9491 tokadd(p, '_');
9492 break;
9493 }
9494 pushback(p, c);
9495 c = '_';
9496 /* fall through */
9497 case '~': /* $~: match-data */
9498 case '*': /* $*: argv */
9499 case '$': /* $$: pid */
9500 case '?': /* $?: last status */
9501 case '!': /* $!: error string */
9502 case '@': /* $@: error position */
9503 case '/': /* $/: input record separator */
9504 case '\\': /* $\: output record separator */
9505 case ';': /* $;: field separator */
9506 case ',': /* $,: output field separator */
9507 case '.': /* $.: last read line number */
9508 case '=': /* $=: ignorecase */
9509 case ':': /* $:: load path */
9510 case '<': /* $<: reading filename */
9511 case '>': /* $>: default output handle */
9512 case '\"': /* $": already loaded files */
9513 tokadd(p, '$');
9514 tokadd(p, c);
9515 goto gvar;
9516
9517 case '-':
9518 tokadd(p, '$');
9519 tokadd(p, c);
9520 c = nextc(p);
9521 if (parser_is_identchar(p)) {
9522 if (tokadd_mbchar(p, c) == -1) return 0;
9523 }
9524 else {
9525 pushback(p, c);
9526 pushback(p, '-');
9527 return '$';
9528 }
9529 gvar:
9530 set_yylval_name(TOK_INTERN());
9531 return tGVAR;
9532
9533 case '&': /* $&: last match */
9534 case '`': /* $`: string before last match */
9535 case '\'': /* $': string after last match */
9536 case '+': /* $+: string matches last paren. */
9537 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9538 tokadd(p, '$');
9539 tokadd(p, c);
9540 goto gvar;
9541 }
9542 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9543 return tBACK_REF;
9544
9545 case '1': case '2': case '3':
9546 case '4': case '5': case '6':
9547 case '7': case '8': case '9':
9548 tokadd(p, '$');
9549 do {
9550 tokadd(p, c);
9551 c = nextc(p);
9552 } while (c != -1 && ISDIGIT(c));
9553 pushback(p, c);
9554 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9555 tokfix(p);
9556 c = parse_numvar(p);
9557 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9558 return tNTH_REF;
9559
9560 default:
9561 if (!parser_is_identchar(p)) {
9562 YYLTYPE loc = RUBY_INIT_YYLLOC();
9563 if (c == -1 || ISSPACE(c)) {
9564 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9565 }
9566 else {
9567 pushback(p, c);
9568 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9569 }
9570 parser_show_error_line(p, &loc);
9571 set_yylval_noname();
9572 return tGVAR;
9573 }
9574 /* fall through */
9575 case '0':
9576 tokadd(p, '$');
9577 }
9578
9579 if (tokadd_ident(p, c)) return 0;
9580 SET_LEX_STATE(EXPR_END);
9581 tokenize_ident(p, last_state);
9582 return tGVAR;
9583}
9584
9585#ifndef RIPPER
9586static bool
9587parser_numbered_param(struct parser_params *p, int n)
9588{
9589 if (n < 0) return false;
9590
9591 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9592 return false;
9593 }
9594 if (p->max_numparam == ORDINAL_PARAM) {
9595 compile_error(p, "ordinary parameter is defined");
9596 return false;
9597 }
9598 struct vtable *args = p->lvtbl->args;
9599 if (p->max_numparam < n) {
9600 p->max_numparam = n;
9601 }
9602 while (n > args->pos) {
9603 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9604 }
9605 return true;
9606}
9607#endif
9608
9609static enum yytokentype
9610parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9611{
9612 const char *ptr = p->lex.pcur;
9613 enum yytokentype result = tIVAR;
9614 register int c = nextc(p);
9615 YYLTYPE loc;
9616
9617 p->lex.ptok = ptr - 1; /* from '@' */
9618 newtok(p);
9619 tokadd(p, '@');
9620 if (c == '@') {
9621 result = tCVAR;
9622 tokadd(p, '@');
9623 c = nextc(p);
9624 }
9625 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9626 if (c == -1 || !parser_is_identchar(p)) {
9627 pushback(p, c);
9628 RUBY_SET_YYLLOC(loc);
9629 if (result == tIVAR) {
9630 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9631 }
9632 else {
9633 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9634 }
9635 parser_show_error_line(p, &loc);
9636 set_yylval_noname();
9637 SET_LEX_STATE(EXPR_END);
9638 return result;
9639 }
9640 else if (ISDIGIT(c)) {
9641 pushback(p, c);
9642 RUBY_SET_YYLLOC(loc);
9643 if (result == tIVAR) {
9644 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9645 }
9646 else {
9647 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9648 }
9649 parser_show_error_line(p, &loc);
9650 set_yylval_noname();
9651 SET_LEX_STATE(EXPR_END);
9652 return result;
9653 }
9654
9655 if (tokadd_ident(p, c)) return 0;
9656 tokenize_ident(p, last_state);
9657 return result;
9658}
9659
9660static enum yytokentype
9661parse_ident(struct parser_params *p, int c, int cmd_state)
9662{
9663 enum yytokentype result;
9664 int mb = ENC_CODERANGE_7BIT;
9665 const enum lex_state_e last_state = p->lex.state;
9666 ID ident;
9667 int enforce_keyword_end = 0;
9668
9669 do {
9670 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9671 if (tokadd_mbchar(p, c) == -1) return 0;
9672 c = nextc(p);
9673 } while (parser_is_identchar(p));
9674 if ((c == '!' || c == '?') && !peek(p, '=')) {
9675 result = tFID;
9676 tokadd(p, c);
9677 }
9678 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9679 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9680 result = tIDENTIFIER;
9681 tokadd(p, c);
9682 }
9683 else {
9684 result = tCONSTANT; /* assume provisionally */
9685 pushback(p, c);
9686 }
9687 tokfix(p);
9688
9689 if (IS_LABEL_POSSIBLE()) {
9690 if (IS_LABEL_SUFFIX(0)) {
9691 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9692 nextc(p);
9693 set_yylval_name(TOK_INTERN());
9694 return tLABEL;
9695 }
9696 }
9697
9698#ifndef RIPPER
9699 if (!NIL_P(peek_end_expect_token_locations(p))) {
9700 VALUE end_loc;
9701 int lineno, column;
9702 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
9703
9704 end_loc = peek_end_expect_token_locations(p);
9705 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
9706 column = NUM2INT(rb_ary_entry(end_loc, 1));
9707
9708 if (p->debug) {
9709 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
9710 p->ruby_sourceline, beg_pos, lineno, column);
9711 }
9712
9713 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
9714 const struct kwtable *kw;
9715
9716 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
9717 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
9718 enforce_keyword_end = 1;
9719 }
9720 }
9721 }
9722#endif
9723
9724 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
9725 const struct kwtable *kw;
9726
9727 /* See if it is a reserved word. */
9728 kw = rb_reserved_word(tok(p), toklen(p));
9729 if (kw) {
9730 enum lex_state_e state = p->lex.state;
9731 if (IS_lex_state_for(state, EXPR_FNAME)) {
9732 SET_LEX_STATE(EXPR_ENDFN);
9733 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9734 return kw->id[0];
9735 }
9736 SET_LEX_STATE(kw->state);
9737 if (IS_lex_state(EXPR_BEG)) {
9738 p->command_start = TRUE;
9739 }
9740 if (kw->id[0] == keyword_do) {
9741 if (lambda_beginning_p()) {
9742 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9743 return keyword_do_LAMBDA;
9744 }
9745 if (COND_P()) return keyword_do_cond;
9746 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9747 return keyword_do_block;
9748 return keyword_do;
9749 }
9750 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
9751 return kw->id[0];
9752 else {
9753 if (kw->id[0] != kw->id[1])
9754 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9755 return kw->id[1];
9756 }
9757 }
9758 }
9759
9760 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9761 if (cmd_state) {
9762 SET_LEX_STATE(EXPR_CMDARG);
9763 }
9764 else {
9765 SET_LEX_STATE(EXPR_ARG);
9766 }
9767 }
9768 else if (p->lex.state == EXPR_FNAME) {
9769 SET_LEX_STATE(EXPR_ENDFN);
9770 }
9771 else {
9772 SET_LEX_STATE(EXPR_END);
9773 }
9774
9775 ident = tokenize_ident(p, last_state);
9776 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9777 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9778 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9779 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
9780 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9781 }
9782 return result;
9783}
9784
9785static void
9786warn_cr(struct parser_params *p)
9787{
9788 if (!p->cr_seen) {
9789 p->cr_seen = TRUE;
9790 /* carried over with p->lex.nextline for nextc() */
9791 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9792 }
9793}
9794
9795static enum yytokentype
9796parser_yylex(struct parser_params *p)
9797{
9798 register int c;
9799 int space_seen = 0;
9800 int cmd_state;
9801 int label;
9802 enum lex_state_e last_state;
9803 int fallthru = FALSE;
9804 int token_seen = p->token_seen;
9805
9806 if (p->lex.strterm) {
9807 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9808 token_flush(p);
9809 return here_document(p, &p->lex.strterm->u.heredoc);
9810 }
9811 else {
9812 token_flush(p);
9813 return parse_string(p, &p->lex.strterm->u.literal);
9814 }
9815 }
9816 cmd_state = p->command_start;
9817 p->command_start = FALSE;
9818 p->token_seen = TRUE;
9819#ifndef RIPPER
9820 token_flush(p);
9821#endif
9822 retry:
9823 last_state = p->lex.state;
9824 switch (c = nextc(p)) {
9825 case '\0': /* NUL */
9826 case '\004': /* ^D */
9827 case '\032': /* ^Z */
9828 case -1: /* end of script. */
9829 p->eofp = 1;
9830#ifndef RIPPER
9831 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
9832 pop_end_expect_token_locations(p);
9833 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
9834 return tDUMNY_END;
9835 }
9836#endif
9837 /* Set location for end-of-input because dispatch_scan_event is not called. */
9838 RUBY_SET_YYLLOC(*p->yylloc);
9839 return 0;
9840
9841 /* white spaces */
9842 case '\r':
9843 warn_cr(p);
9844 /* fall through */
9845 case ' ': case '\t': case '\f':
9846 case '\13': /* '\v' */
9847 space_seen = 1;
9848 while ((c = nextc(p))) {
9849 switch (c) {
9850 case '\r':
9851 warn_cr(p);
9852 /* fall through */
9853 case ' ': case '\t': case '\f':
9854 case '\13': /* '\v' */
9855 break;
9856 default:
9857 goto outofloop;
9858 }
9859 }
9860 outofloop:
9861 pushback(p, c);
9862 dispatch_scan_event(p, tSP);
9863#ifndef RIPPER
9864 token_flush(p);
9865#endif
9866 goto retry;
9867
9868 case '#': /* it's a comment */
9869 p->token_seen = token_seen;
9870 /* no magic_comment in shebang line */
9871 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9872 if (comment_at_top(p)) {
9873 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9874 }
9875 }
9876 lex_goto_eol(p);
9877 dispatch_scan_event(p, tCOMMENT);
9878 fallthru = TRUE;
9879 /* fall through */
9880 case '\n':
9881 p->token_seen = token_seen;
9882 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9883 !IS_lex_state(EXPR_LABELED));
9884 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9885 if (!fallthru) {
9886 dispatch_scan_event(p, tIGNORED_NL);
9887 }
9888 fallthru = FALSE;
9889 if (!c && p->ctxt.in_kwarg) {
9890 goto normal_newline;
9891 }
9892 goto retry;
9893 }
9894 while (1) {
9895 switch (c = nextc(p)) {
9896 case ' ': case '\t': case '\f': case '\r':
9897 case '\13': /* '\v' */
9898 space_seen = 1;
9899 break;
9900 case '#':
9901 pushback(p, c);
9902 if (space_seen) {
9903 dispatch_scan_event(p, tSP);
9904 token_flush(p);
9905 }
9906 goto retry;
9907 case '&':
9908 case '.': {
9909 dispatch_delayed_token(p, tIGNORED_NL);
9910 if (peek(p, '.') == (c == '&')) {
9911 pushback(p, c);
9912 dispatch_scan_event(p, tSP);
9913 goto retry;
9914 }
9915 }
9916 default:
9917 p->ruby_sourceline--;
9918 p->lex.nextline = p->lex.lastline;
9919 case -1: /* EOF no decrement*/
9920 lex_goto_eol(p);
9921 if (c != -1) {
9922 p->lex.ptok = p->lex.pcur;
9923 }
9924 goto normal_newline;
9925 }
9926 }
9927 normal_newline:
9928 p->command_start = TRUE;
9929 SET_LEX_STATE(EXPR_BEG);
9930 return '\n';
9931
9932 case '*':
9933 if ((c = nextc(p)) == '*') {
9934 if ((c = nextc(p)) == '=') {
9935 set_yylval_id(idPow);
9936 SET_LEX_STATE(EXPR_BEG);
9937 return tOP_ASGN;
9938 }
9939 pushback(p, c);
9940 if (IS_SPCARG(c)) {
9941 rb_warning0("`**' interpreted as argument prefix");
9942 c = tDSTAR;
9943 }
9944 else if (IS_BEG()) {
9945 c = tDSTAR;
9946 }
9947 else {
9948 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9949 }
9950 }
9951 else {
9952 if (c == '=') {
9953 set_yylval_id('*');
9954 SET_LEX_STATE(EXPR_BEG);
9955 return tOP_ASGN;
9956 }
9957 pushback(p, c);
9958 if (IS_SPCARG(c)) {
9959 rb_warning0("`*' interpreted as argument prefix");
9960 c = tSTAR;
9961 }
9962 else if (IS_BEG()) {
9963 c = tSTAR;
9964 }
9965 else {
9966 c = warn_balanced('*', "*", "argument prefix");
9967 }
9968 }
9969 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9970 return c;
9971
9972 case '!':
9973 c = nextc(p);
9974 if (IS_AFTER_OPERATOR()) {
9975 SET_LEX_STATE(EXPR_ARG);
9976 if (c == '@') {
9977 return '!';
9978 }
9979 }
9980 else {
9981 SET_LEX_STATE(EXPR_BEG);
9982 }
9983 if (c == '=') {
9984 return tNEQ;
9985 }
9986 if (c == '~') {
9987 return tNMATCH;
9988 }
9989 pushback(p, c);
9990 return '!';
9991
9992 case '=':
9993 if (was_bol(p)) {
9994 /* skip embedded rd document */
9995 if (word_match_p(p, "begin", 5)) {
9996 int first_p = TRUE;
9997
9998 lex_goto_eol(p);
9999 dispatch_scan_event(p, tEMBDOC_BEG);
10000 for (;;) {
10001 lex_goto_eol(p);
10002 if (!first_p) {
10003 dispatch_scan_event(p, tEMBDOC);
10004 }
10005 first_p = FALSE;
10006 c = nextc(p);
10007 if (c == -1) {
10008 compile_error(p, "embedded document meets end of file");
10009 return 0;
10010 }
10011 if (c == '=' && word_match_p(p, "end", 3)) {
10012 break;
10013 }
10014 pushback(p, c);
10015 }
10016 lex_goto_eol(p);
10017 dispatch_scan_event(p, tEMBDOC_END);
10018 goto retry;
10019 }
10020 }
10021
10022 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10023 if ((c = nextc(p)) == '=') {
10024 if ((c = nextc(p)) == '=') {
10025 return tEQQ;
10026 }
10027 pushback(p, c);
10028 return tEQ;
10029 }
10030 if (c == '~') {
10031 return tMATCH;
10032 }
10033 else if (c == '>') {
10034 return tASSOC;
10035 }
10036 pushback(p, c);
10037 return '=';
10038
10039 case '<':
10040 c = nextc(p);
10041 if (c == '<' &&
10042 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10043 !IS_END() &&
10044 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10045 int token = heredoc_identifier(p);
10046 if (token) return token < 0 ? 0 : token;
10047 }
10048 if (IS_AFTER_OPERATOR()) {
10049 SET_LEX_STATE(EXPR_ARG);
10050 }
10051 else {
10052 if (IS_lex_state(EXPR_CLASS))
10053 p->command_start = TRUE;
10054 SET_LEX_STATE(EXPR_BEG);
10055 }
10056 if (c == '=') {
10057 if ((c = nextc(p)) == '>') {
10058 return tCMP;
10059 }
10060 pushback(p, c);
10061 return tLEQ;
10062 }
10063 if (c == '<') {
10064 if ((c = nextc(p)) == '=') {
10065 set_yylval_id(idLTLT);
10066 SET_LEX_STATE(EXPR_BEG);
10067 return tOP_ASGN;
10068 }
10069 pushback(p, c);
10070 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10071 }
10072 pushback(p, c);
10073 return '<';
10074
10075 case '>':
10076 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10077 if ((c = nextc(p)) == '=') {
10078 return tGEQ;
10079 }
10080 if (c == '>') {
10081 if ((c = nextc(p)) == '=') {
10082 set_yylval_id(idGTGT);
10083 SET_LEX_STATE(EXPR_BEG);
10084 return tOP_ASGN;
10085 }
10086 pushback(p, c);
10087 return tRSHFT;
10088 }
10089 pushback(p, c);
10090 return '>';
10091
10092 case '"':
10093 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10094 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10095 p->lex.ptok = p->lex.pcur-1;
10096 return tSTRING_BEG;
10097
10098 case '`':
10099 if (IS_lex_state(EXPR_FNAME)) {
10100 SET_LEX_STATE(EXPR_ENDFN);
10101 return c;
10102 }
10103 if (IS_lex_state(EXPR_DOT)) {
10104 if (cmd_state)
10105 SET_LEX_STATE(EXPR_CMDARG);
10106 else
10107 SET_LEX_STATE(EXPR_ARG);
10108 return c;
10109 }
10110 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10111 return tXSTRING_BEG;
10112
10113 case '\'':
10114 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10115 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10116 p->lex.ptok = p->lex.pcur-1;
10117 return tSTRING_BEG;
10118
10119 case '?':
10120 return parse_qmark(p, space_seen);
10121
10122 case '&':
10123 if ((c = nextc(p)) == '&') {
10124 SET_LEX_STATE(EXPR_BEG);
10125 if ((c = nextc(p)) == '=') {
10126 set_yylval_id(idANDOP);
10127 SET_LEX_STATE(EXPR_BEG);
10128 return tOP_ASGN;
10129 }
10130 pushback(p, c);
10131 return tANDOP;
10132 }
10133 else if (c == '=') {
10134 set_yylval_id('&');
10135 SET_LEX_STATE(EXPR_BEG);
10136 return tOP_ASGN;
10137 }
10138 else if (c == '.') {
10139 set_yylval_id(idANDDOT);
10140 SET_LEX_STATE(EXPR_DOT);
10141 return tANDDOT;
10142 }
10143 pushback(p, c);
10144 if (IS_SPCARG(c)) {
10145 if ((c != ':') ||
10146 (c = peekc_n(p, 1)) == -1 ||
10147 !(c == '\'' || c == '"' ||
10148 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
10149 rb_warning0("`&' interpreted as argument prefix");
10150 }
10151 c = tAMPER;
10152 }
10153 else if (IS_BEG()) {
10154 c = tAMPER;
10155 }
10156 else {
10157 c = warn_balanced('&', "&", "argument prefix");
10158 }
10159 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10160 return c;
10161
10162 case '|':
10163 if ((c = nextc(p)) == '|') {
10164 SET_LEX_STATE(EXPR_BEG);
10165 if ((c = nextc(p)) == '=') {
10166 set_yylval_id(idOROP);
10167 SET_LEX_STATE(EXPR_BEG);
10168 return tOP_ASGN;
10169 }
10170 pushback(p, c);
10171 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10172 c = '|';
10173 pushback(p, '|');
10174 return c;
10175 }
10176 return tOROP;
10177 }
10178 if (c == '=') {
10179 set_yylval_id('|');
10180 SET_LEX_STATE(EXPR_BEG);
10181 return tOP_ASGN;
10182 }
10183 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10184 pushback(p, c);
10185 return '|';
10186
10187 case '+':
10188 c = nextc(p);
10189 if (IS_AFTER_OPERATOR()) {
10190 SET_LEX_STATE(EXPR_ARG);
10191 if (c == '@') {
10192 return tUPLUS;
10193 }
10194 pushback(p, c);
10195 return '+';
10196 }
10197 if (c == '=') {
10198 set_yylval_id('+');
10199 SET_LEX_STATE(EXPR_BEG);
10200 return tOP_ASGN;
10201 }
10202 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10203 SET_LEX_STATE(EXPR_BEG);
10204 pushback(p, c);
10205 if (c != -1 && ISDIGIT(c)) {
10206 return parse_numeric(p, '+');
10207 }
10208 return tUPLUS;
10209 }
10210 SET_LEX_STATE(EXPR_BEG);
10211 pushback(p, c);
10212 return warn_balanced('+', "+", "unary operator");
10213
10214 case '-':
10215 c = nextc(p);
10216 if (IS_AFTER_OPERATOR()) {
10217 SET_LEX_STATE(EXPR_ARG);
10218 if (c == '@') {
10219 return tUMINUS;
10220 }
10221 pushback(p, c);
10222 return '-';
10223 }
10224 if (c == '=') {
10225 set_yylval_id('-');
10226 SET_LEX_STATE(EXPR_BEG);
10227 return tOP_ASGN;
10228 }
10229 if (c == '>') {
10230 SET_LEX_STATE(EXPR_ENDFN);
10231 return tLAMBDA;
10232 }
10233 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10234 SET_LEX_STATE(EXPR_BEG);
10235 pushback(p, c);
10236 if (c != -1 && ISDIGIT(c)) {
10237 return tUMINUS_NUM;
10238 }
10239 return tUMINUS;
10240 }
10241 SET_LEX_STATE(EXPR_BEG);
10242 pushback(p, c);
10243 return warn_balanced('-', "-", "unary operator");
10244
10245 case '.': {
10246 int is_beg = IS_BEG();
10247 SET_LEX_STATE(EXPR_BEG);
10248 if ((c = nextc(p)) == '.') {
10249 if ((c = nextc(p)) == '.') {
10250 if (p->ctxt.in_argdef) {
10251 SET_LEX_STATE(EXPR_ENDARG);
10252 return tBDOT3;
10253 }
10254 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10255 rb_warn0("... at EOL, should be parenthesized?");
10256 }
10257 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10258 if (IS_lex_state_for(last_state, EXPR_LABEL))
10259 return tDOT3;
10260 }
10261 return is_beg ? tBDOT3 : tDOT3;
10262 }
10263 pushback(p, c);
10264 return is_beg ? tBDOT2 : tDOT2;
10265 }
10266 pushback(p, c);
10267 if (c != -1 && ISDIGIT(c)) {
10268 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10269 parse_numeric(p, '.');
10270 if (ISDIGIT(prev)) {
10271 yyerror0("unexpected fraction part after numeric literal");
10272 }
10273 else {
10274 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10275 }
10276 SET_LEX_STATE(EXPR_END);
10277 p->lex.ptok = p->lex.pcur;
10278 goto retry;
10279 }
10280 set_yylval_id('.');
10281 SET_LEX_STATE(EXPR_DOT);
10282 return '.';
10283 }
10284
10285 case '0': case '1': case '2': case '3': case '4':
10286 case '5': case '6': case '7': case '8': case '9':
10287 return parse_numeric(p, c);
10288
10289 case ')':
10290 COND_POP();
10291 CMDARG_POP();
10292 SET_LEX_STATE(EXPR_ENDFN);
10293 p->lex.paren_nest--;
10294 return c;
10295
10296 case ']':
10297 COND_POP();
10298 CMDARG_POP();
10299 SET_LEX_STATE(EXPR_END);
10300 p->lex.paren_nest--;
10301 return c;
10302
10303 case '}':
10304 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10305 if (!p->lex.brace_nest--) return tSTRING_DEND;
10306 COND_POP();
10307 CMDARG_POP();
10308 SET_LEX_STATE(EXPR_END);
10309 p->lex.paren_nest--;
10310 return c;
10311
10312 case ':':
10313 c = nextc(p);
10314 if (c == ':') {
10315 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10316 SET_LEX_STATE(EXPR_BEG);
10317 return tCOLON3;
10318 }
10319 set_yylval_id(idCOLON2);
10320 SET_LEX_STATE(EXPR_DOT);
10321 return tCOLON2;
10322 }
10323 if (IS_END() || ISSPACE(c) || c == '#') {
10324 pushback(p, c);
10325 c = warn_balanced(':', ":", "symbol literal");
10326 SET_LEX_STATE(EXPR_BEG);
10327 return c;
10328 }
10329 switch (c) {
10330 case '\'':
10331 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10332 break;
10333 case '"':
10334 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10335 break;
10336 default:
10337 pushback(p, c);
10338 break;
10339 }
10340 SET_LEX_STATE(EXPR_FNAME);
10341 return tSYMBEG;
10342
10343 case '/':
10344 if (IS_BEG()) {
10345 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10346 return tREGEXP_BEG;
10347 }
10348 if ((c = nextc(p)) == '=') {
10349 set_yylval_id('/');
10350 SET_LEX_STATE(EXPR_BEG);
10351 return tOP_ASGN;
10352 }
10353 pushback(p, c);
10354 if (IS_SPCARG(c)) {
10355 arg_ambiguous(p, '/');
10356 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10357 return tREGEXP_BEG;
10358 }
10359 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10360 return warn_balanced('/', "/", "regexp literal");
10361
10362 case '^':
10363 if ((c = nextc(p)) == '=') {
10364 set_yylval_id('^');
10365 SET_LEX_STATE(EXPR_BEG);
10366 return tOP_ASGN;
10367 }
10368 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10369 pushback(p, c);
10370 return '^';
10371
10372 case ';':
10373 SET_LEX_STATE(EXPR_BEG);
10374 p->command_start = TRUE;
10375 return ';';
10376
10377 case ',':
10378 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10379 return ',';
10380
10381 case '~':
10382 if (IS_AFTER_OPERATOR()) {
10383 if ((c = nextc(p)) != '@') {
10384 pushback(p, c);
10385 }
10386 SET_LEX_STATE(EXPR_ARG);
10387 }
10388 else {
10389 SET_LEX_STATE(EXPR_BEG);
10390 }
10391 return '~';
10392
10393 case '(':
10394 if (IS_BEG()) {
10395 c = tLPAREN;
10396 }
10397 else if (!space_seen) {
10398 /* foo( ... ) => method call, no ambiguity */
10399 }
10400 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
10401 c = tLPAREN_ARG;
10402 }
10403 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
10404 rb_warning0("parentheses after method name is interpreted as "
10405 "an argument list, not a decomposed argument");
10406 }
10407 p->lex.paren_nest++;
10408 COND_PUSH(0);
10409 CMDARG_PUSH(0);
10410 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10411 return c;
10412
10413 case '[':
10414 p->lex.paren_nest++;
10415 if (IS_AFTER_OPERATOR()) {
10416 if ((c = nextc(p)) == ']') {
10417 p->lex.paren_nest--;
10418 SET_LEX_STATE(EXPR_ARG);
10419 if ((c = nextc(p)) == '=') {
10420 return tASET;
10421 }
10422 pushback(p, c);
10423 return tAREF;
10424 }
10425 pushback(p, c);
10426 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
10427 return '[';
10428 }
10429 else if (IS_BEG()) {
10430 c = tLBRACK;
10431 }
10432 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
10433 c = tLBRACK;
10434 }
10435 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10436 COND_PUSH(0);
10437 CMDARG_PUSH(0);
10438 return c;
10439
10440 case '{':
10441 ++p->lex.brace_nest;
10442 if (lambda_beginning_p())
10443 c = tLAMBEG;
10444 else if (IS_lex_state(EXPR_LABELED))
10445 c = tLBRACE; /* hash */
10446 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
10447 c = '{'; /* block (primary) */
10448 else if (IS_lex_state(EXPR_ENDARG))
10449 c = tLBRACE_ARG; /* block (expr) */
10450 else
10451 c = tLBRACE; /* hash */
10452 if (c != tLBRACE) {
10453 p->command_start = TRUE;
10454 SET_LEX_STATE(EXPR_BEG);
10455 }
10456 else {
10457 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10458 }
10459 ++p->lex.paren_nest; /* after lambda_beginning_p() */
10460 COND_PUSH(0);
10461 CMDARG_PUSH(0);
10462 return c;
10463
10464 case '\\':
10465 c = nextc(p);
10466 if (c == '\n') {
10467 space_seen = 1;
10468 dispatch_scan_event(p, tSP);
10469 goto retry; /* skip \\n */
10470 }
10471 if (c == ' ') return tSP;
10472 if (ISSPACE(c)) return c;
10473 pushback(p, c);
10474 return '\\';
10475
10476 case '%':
10477 return parse_percent(p, space_seen, last_state);
10478
10479 case '$':
10480 return parse_gvar(p, last_state);
10481
10482 case '@':
10483 return parse_atmark(p, last_state);
10484
10485 case '_':
10486 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
10487 p->ruby__end__seen = 1;
10488 p->eofp = 1;
10489#ifndef RIPPER
10490 return -1;
10491#else
10492 lex_goto_eol(p);
10493 dispatch_scan_event(p, k__END__);
10494 return 0;
10495#endif
10496 }
10497 newtok(p);
10498 break;
10499
10500 default:
10501 if (!parser_is_identchar(p)) {
10502 compile_error(p, "Invalid char `\\x%02X' in expression", c);
10503 token_flush(p);
10504 goto retry;
10505 }
10506
10507 newtok(p);
10508 break;
10509 }
10510
10511 return parse_ident(p, c, cmd_state);
10512}
10513
10514static enum yytokentype
10515yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
10516{
10517 enum yytokentype t;
10518
10519 p->lval = lval;
10520 lval->val = Qundef;
10521 p->yylloc = yylloc;
10522
10523 t = parser_yylex(p);
10524
10525 if (has_delayed_token(p))
10526 dispatch_delayed_token(p, t);
10527 else if (t != 0)
10528 dispatch_scan_event(p, t);
10529
10530 return t;
10531}
10532
10533#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
10534
10535static NODE*
10536node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
10537{
10538 NODE *n = rb_ast_newnode(p->ast, type);
10539
10540 rb_node_init(n, type, a0, a1, a2);
10541
10542 nd_set_loc(n, loc);
10543 nd_set_node_id(n, parser_get_node_id(p));
10544 return n;
10545}
10546
10547static NODE *
10548nd_set_loc(NODE *nd, const YYLTYPE *loc)
10549{
10550 nd->nd_loc = *loc;
10551 nd_set_line(nd, loc->beg_pos.lineno);
10552 return nd;
10553}
10554
10555#ifndef RIPPER
10556static enum node_type
10557nodetype(NODE *node) /* for debug */
10558{
10559 return (enum node_type)nd_type(node);
10560}
10561
10562static int
10563nodeline(NODE *node)
10564{
10565 return nd_line(node);
10566}
10567
10568static NODE*
10569newline_node(NODE *node)
10570{
10571 if (node) {
10572 node = remove_begin(node);
10573 node->flags |= NODE_FL_NEWLINE;
10574 }
10575 return node;
10576}
10577
10578static void
10579fixpos(NODE *node, NODE *orig)
10580{
10581 if (!node) return;
10582 if (!orig) return;
10583 nd_set_line(node, nd_line(orig));
10584}
10585
10586static void
10587parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10588{
10589 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10590}
10591
10592static void
10593parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10594{
10595 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10596}
10597
10598static NODE*
10599block_append(struct parser_params *p, NODE *head, NODE *tail)
10600{
10601 NODE *end, *h = head, *nd;
10602
10603 if (tail == 0) return head;
10604
10605 if (h == 0) return tail;
10606 switch (nd_type(h)) {
10607 case NODE_LIT:
10608 case NODE_STR:
10609 case NODE_SELF:
10610 case NODE_TRUE:
10611 case NODE_FALSE:
10612 case NODE_NIL:
10613 parser_warning(p, h, "unused literal ignored");
10614 return tail;
10615 default:
10616 h = end = NEW_BLOCK(head, &head->nd_loc);
10617 end->nd_end = end;
10618 head = end;
10619 break;
10620 case NODE_BLOCK:
10621 end = h->nd_end;
10622 break;
10623 }
10624
10625 nd = end->nd_head;
10626 switch (nd_type(nd)) {
10627 case NODE_RETURN:
10628 case NODE_BREAK:
10629 case NODE_NEXT:
10630 case NODE_REDO:
10631 case NODE_RETRY:
10632 if (RTEST(ruby_verbose)) {
10633 parser_warning(p, tail, "statement not reached");
10634 }
10635 break;
10636
10637 default:
10638 break;
10639 }
10640
10641 if (!nd_type_p(tail, NODE_BLOCK)) {
10642 tail = NEW_BLOCK(tail, &tail->nd_loc);
10643 tail->nd_end = tail;
10644 }
10645 end->nd_next = tail;
10646 h->nd_end = tail->nd_end;
10647 nd_set_last_loc(head, nd_last_loc(tail));
10648 return head;
10649}
10650
10651/* append item to the list */
10652static NODE*
10653list_append(struct parser_params *p, NODE *list, NODE *item)
10654{
10655 NODE *last;
10656
10657 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10658 if (list->nd_next) {
10659 last = list->nd_next->nd_end;
10660 }
10661 else {
10662 last = list;
10663 }
10664
10665 list->nd_alen += 1;
10666 last->nd_next = NEW_LIST(item, &item->nd_loc);
10667 list->nd_next->nd_end = last->nd_next;
10668
10669 nd_set_last_loc(list, nd_last_loc(item));
10670
10671 return list;
10672}
10673
10674/* concat two lists */
10675static NODE*
10676list_concat(NODE *head, NODE *tail)
10677{
10678 NODE *last;
10679
10680 if (head->nd_next) {
10681 last = head->nd_next->nd_end;
10682 }
10683 else {
10684 last = head;
10685 }
10686
10687 head->nd_alen += tail->nd_alen;
10688 last->nd_next = tail;
10689 if (tail->nd_next) {
10690 head->nd_next->nd_end = tail->nd_next->nd_end;
10691 }
10692 else {
10693 head->nd_next->nd_end = tail;
10694 }
10695
10696 nd_set_last_loc(head, nd_last_loc(tail));
10697
10698 return head;
10699}
10700
10701static int
10702literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10703{
10704 if (NIL_P(tail)) return 1;
10705 if (!rb_enc_compatible(head, tail)) {
10706 compile_error(p, "string literal encodings differ (%s / %s)",
10707 rb_enc_name(rb_enc_get(head)),
10708 rb_enc_name(rb_enc_get(tail)));
10709 rb_str_resize(head, 0);
10710 rb_str_resize(tail, 0);
10711 return 0;
10712 }
10713 rb_str_buf_append(head, tail);
10714 return 1;
10715}
10716
10717static VALUE
10718string_literal_head(enum node_type htype, NODE *head)
10719{
10720 if (htype != NODE_DSTR) return Qfalse;
10721 if (head->nd_next) {
10722 head = head->nd_next->nd_end->nd_head;
10723 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10724 }
10725 const VALUE lit = head->nd_lit;
10726 ASSUME(lit != Qfalse);
10727 return lit;
10728}
10729
10730/* concat two string literals */
10731static NODE *
10732literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10733{
10734 enum node_type htype;
10735 VALUE lit;
10736
10737 if (!head) return tail;
10738 if (!tail) return head;
10739
10740 htype = nd_type(head);
10741 if (htype == NODE_EVSTR) {
10742 head = new_dstr(p, head, loc);
10743 htype = NODE_DSTR;
10744 }
10745 if (p->heredoc_indent > 0) {
10746 switch (htype) {
10747 case NODE_STR:
10748 nd_set_type(head, NODE_DSTR);
10749 case NODE_DSTR:
10750 return list_append(p, head, tail);
10751 default:
10752 break;
10753 }
10754 }
10755 switch (nd_type(tail)) {
10756 case NODE_STR:
10757 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10758 htype = NODE_STR;
10759 }
10760 else {
10761 lit = head->nd_lit;
10762 }
10763 if (htype == NODE_STR) {
10764 if (!literal_concat0(p, lit, tail->nd_lit)) {
10765 error:
10766 rb_discard_node(p, head);
10767 rb_discard_node(p, tail);
10768 return 0;
10769 }
10770 rb_discard_node(p, tail);
10771 }
10772 else {
10773 list_append(p, head, tail);
10774 }
10775 break;
10776
10777 case NODE_DSTR:
10778 if (htype == NODE_STR) {
10779 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10780 goto error;
10781 tail->nd_lit = head->nd_lit;
10782 rb_discard_node(p, head);
10783 head = tail;
10784 }
10785 else if (NIL_P(tail->nd_lit)) {
10786 append:
10787 head->nd_alen += tail->nd_alen - 1;
10788 if (!head->nd_next) {
10789 head->nd_next = tail->nd_next;
10790 }
10791 else if (tail->nd_next) {
10792 head->nd_next->nd_end->nd_next = tail->nd_next;
10793 head->nd_next->nd_end = tail->nd_next->nd_end;
10794 }
10795 rb_discard_node(p, tail);
10796 }
10797 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10798 if (!literal_concat0(p, lit, tail->nd_lit))
10799 goto error;
10800 tail->nd_lit = Qnil;
10801 goto append;
10802 }
10803 else {
10804 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10805 }
10806 break;
10807
10808 case NODE_EVSTR:
10809 if (htype == NODE_STR) {
10810 nd_set_type(head, NODE_DSTR);
10811 head->nd_alen = 1;
10812 }
10813 list_append(p, head, tail);
10814 break;
10815 }
10816 return head;
10817}
10818
10819static NODE *
10820evstr2dstr(struct parser_params *p, NODE *node)
10821{
10822 if (nd_type_p(node, NODE_EVSTR)) {
10823 node = new_dstr(p, node, &node->nd_loc);
10824 }
10825 return node;
10826}
10827
10828static NODE *
10829new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10830{
10831 NODE *head = node;
10832
10833 if (node) {
10834 switch (nd_type(node)) {
10835 case NODE_STR:
10836 nd_set_type(node, NODE_DSTR);
10837 return node;
10838 case NODE_DSTR:
10839 break;
10840 case NODE_EVSTR:
10841 return node;
10842 }
10843 }
10844 return NEW_EVSTR(head, loc);
10845}
10846
10847static NODE *
10848new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10849{
10850 VALUE lit = STR_NEW0();
10851 NODE *dstr = NEW_DSTR(lit, loc);
10852 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10853 return list_append(p, dstr, node);
10854}
10855
10856static NODE *
10857call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10858 const YYLTYPE *op_loc, const YYLTYPE *loc)
10859{
10860 NODE *expr;
10861 value_expr(recv);
10862 value_expr(arg1);
10863 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10864 nd_set_line(expr, op_loc->beg_pos.lineno);
10865 return expr;
10866}
10867
10868static NODE *
10869call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10870{
10871 NODE *opcall;
10872 value_expr(recv);
10873 opcall = NEW_OPCALL(recv, id, 0, loc);
10874 nd_set_line(opcall, op_loc->beg_pos.lineno);
10875 return opcall;
10876}
10877
10878static NODE *
10879new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10880{
10881 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10882 nd_set_line(qcall, op_loc->beg_pos.lineno);
10883 return qcall;
10884}
10885
10886static NODE*
10887new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10888{
10889 NODE *ret;
10890 if (block) block_dup_check(p, args, block);
10891 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10892 if (block) ret = method_add_block(p, ret, block, loc);
10893 fixpos(ret, recv);
10894 return ret;
10895}
10896
10897#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10898static NODE*
10899match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10900{
10901 NODE *n;
10902 int line = op_loc->beg_pos.lineno;
10903
10904 value_expr(node1);
10905 value_expr(node2);
10906 if (node1 && (n = nd_once_body(node1)) != 0) {
10907 switch (nd_type(n)) {
10908 case NODE_DREGX:
10909 {
10910 NODE *match = NEW_MATCH2(node1, node2, loc);
10911 nd_set_line(match, line);
10912 return match;
10913 }
10914
10915 case NODE_LIT:
10916 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10917 const VALUE lit = n->nd_lit;
10918 NODE *match = NEW_MATCH2(node1, node2, loc);
10919 match->nd_args = reg_named_capture_assign(p, lit, loc);
10920 nd_set_line(match, line);
10921 return match;
10922 }
10923 }
10924 }
10925
10926 if (node2 && (n = nd_once_body(node2)) != 0) {
10927 NODE *match3;
10928
10929 switch (nd_type(n)) {
10930 case NODE_LIT:
10931 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10932 /* fallthru */
10933 case NODE_DREGX:
10934 match3 = NEW_MATCH3(node2, node1, loc);
10935 return match3;
10936 }
10937 }
10938
10939 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10940 nd_set_line(n, line);
10941 return n;
10942}
10943
10944# if WARN_PAST_SCOPE
10945static int
10946past_dvar_p(struct parser_params *p, ID id)
10947{
10948 struct vtable *past = p->lvtbl->past;
10949 while (past) {
10950 if (vtable_included(past, id)) return 1;
10951 past = past->prev;
10952 }
10953 return 0;
10954}
10955# endif
10956
10957static int
10958numparam_nested_p(struct parser_params *p)
10959{
10960 struct local_vars *local = p->lvtbl;
10961 NODE *outer = local->numparam.outer;
10962 NODE *inner = local->numparam.inner;
10963 if (outer || inner) {
10964 NODE *used = outer ? outer : inner;
10965 compile_error(p, "numbered parameter is already used in\n"
10966 "%s:%d: %s block here",
10967 p->ruby_sourcefile, nd_line(used),
10968 outer ? "outer" : "inner");
10969 parser_show_error_line(p, &used->nd_loc);
10970 return 1;
10971 }
10972 return 0;
10973}
10974
10975static NODE*
10976gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10977{
10978 ID *vidp = NULL;
10979 NODE *node;
10980 switch (id) {
10981 case keyword_self:
10982 return NEW_SELF(loc);
10983 case keyword_nil:
10984 return NEW_NIL(loc);
10985 case keyword_true:
10986 return NEW_TRUE(loc);
10987 case keyword_false:
10988 return NEW_FALSE(loc);
10989 case keyword__FILE__:
10990 {
10991 VALUE file = p->ruby_sourcefile_string;
10992 if (NIL_P(file))
10993 file = rb_str_new(0, 0);
10994 else
10995 file = rb_str_dup(file);
10996 node = NEW_STR(file, loc);
10997 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10998 }
10999 return node;
11000 case keyword__LINE__:
11001 return NEW_LIT(INT2FIX(p->tokline), loc);
11002 case keyword__ENCODING__:
11003 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
11004 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11005 return node;
11006
11007 }
11008 switch (id_type(id)) {
11009 case ID_LOCAL:
11010 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
11011 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
11012 if (id == p->cur_arg) {
11013 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11014 return 0;
11015 }
11016 if (vidp) *vidp |= LVAR_USED;
11017 node = NEW_DVAR(id, loc);
11018 return node;
11019 }
11020 if (local_id_ref(p, id, &vidp)) {
11021 if (id == p->cur_arg) {
11022 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11023 return 0;
11024 }
11025 if (vidp) *vidp |= LVAR_USED;
11026 node = NEW_LVAR(id, loc);
11027 return node;
11028 }
11029 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
11030 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
11031 if (numparam_nested_p(p)) return 0;
11032 node = NEW_DVAR(id, loc);
11033 struct local_vars *local = p->lvtbl;
11034 if (!local->numparam.current) local->numparam.current = node;
11035 return node;
11036 }
11037# if WARN_PAST_SCOPE
11038 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
11039 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
11040 }
11041# endif
11042 /* method call without arguments */
11043 return NEW_VCALL(id, loc);
11044 case ID_GLOBAL:
11045 return NEW_GVAR(id, loc);
11046 case ID_INSTANCE:
11047 return NEW_IVAR(id, loc);
11048 case ID_CONST:
11049 return NEW_CONST(id, loc);
11050 case ID_CLASS:
11051 return NEW_CVAR(id, loc);
11052 }
11053 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11054 return 0;
11055}
11056
11057static NODE *
11058opt_arg_append(NODE *opt_list, NODE *opt)
11059{
11060 NODE *opts = opt_list;
11061 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11062
11063 while (opts->nd_next) {
11064 opts = opts->nd_next;
11065 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11066 }
11067 opts->nd_next = opt;
11068
11069 return opt_list;
11070}
11071
11072static NODE *
11073kwd_append(NODE *kwlist, NODE *kw)
11074{
11075 if (kwlist) {
11076 opt_arg_append(kwlist, kw);
11077 }
11078 return kwlist;
11079}
11080
11081static NODE *
11082new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
11083{
11084 return NEW_DEFINED(remove_begin_all(expr), loc);
11085}
11086
11087static NODE*
11088symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
11089{
11090 enum node_type type = nd_type(symbol);
11091 switch (type) {
11092 case NODE_DSTR:
11093 nd_set_type(symbol, NODE_DSYM);
11094 break;
11095 case NODE_STR:
11096 nd_set_type(symbol, NODE_LIT);
11097 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
11098 break;
11099 default:
11100 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
11101 }
11102 return list_append(p, symbols, symbol);
11103}
11104
11105static NODE *
11106new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
11107{
11108 NODE *list, *prev;
11109 VALUE lit;
11110
11111 if (!node) {
11112 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
11113 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11114 return node;
11115 }
11116 switch (nd_type(node)) {
11117 case NODE_STR:
11118 {
11119 VALUE src = node->nd_lit;
11120 nd_set_type(node, NODE_LIT);
11121 nd_set_loc(node, loc);
11122 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11123 }
11124 break;
11125 default:
11126 lit = STR_NEW0();
11127 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
11128 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11129 /* fall through */
11130 case NODE_DSTR:
11131 nd_set_type(node, NODE_DREGX);
11132 nd_set_loc(node, loc);
11133 node->nd_cflag = options & RE_OPTION_MASK;
11134 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
11135 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
11136 NODE *frag = list->nd_head;
11137 enum node_type type = nd_type(frag);
11138 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
11139 VALUE tail = frag->nd_lit;
11140 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
11141 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
11142 if (!literal_concat0(p, lit, tail)) {
11143 return NEW_NIL(loc); /* dummy node on error */
11144 }
11145 rb_str_resize(tail, 0);
11146 prev->nd_next = list->nd_next;
11147 rb_discard_node(p, list->nd_head);
11148 rb_discard_node(p, list);
11149 list = prev;
11150 }
11151 else {
11152 prev = list;
11153 }
11154 }
11155 else {
11156 prev = 0;
11157 }
11158 }
11159 if (!node->nd_next) {
11160 VALUE src = node->nd_lit;
11161 nd_set_type(node, NODE_LIT);
11162 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11163 }
11164 if (options & RE_OPTION_ONCE) {
11165 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
11166 }
11167 break;
11168 }
11169 return node;
11170}
11171
11172static NODE *
11173new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
11174{
11175 if (!k) return 0;
11176 return NEW_KW_ARG(0, (k), loc);
11177}
11178
11179static NODE *
11180new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11181{
11182 if (!node) {
11183 VALUE lit = STR_NEW0();
11184 NODE *xstr = NEW_XSTR(lit, loc);
11185 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11186 return xstr;
11187 }
11188 switch (nd_type(node)) {
11189 case NODE_STR:
11190 nd_set_type(node, NODE_XSTR);
11191 nd_set_loc(node, loc);
11192 break;
11193 case NODE_DSTR:
11194 nd_set_type(node, NODE_DXSTR);
11195 nd_set_loc(node, loc);
11196 break;
11197 default:
11198 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
11199 break;
11200 }
11201 return node;
11202}
11203
11204static void
11205check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
11206{
11207 VALUE lit;
11208
11209 if (!arg || !p->case_labels) return;
11210
11211 lit = rb_node_case_when_optimizable_literal(arg);
11212 if (UNDEF_P(lit)) return;
11213 if (nd_type_p(arg, NODE_STR)) {
11214 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
11215 }
11216
11217 if (NIL_P(p->case_labels)) {
11218 p->case_labels = rb_obj_hide(rb_hash_new());
11219 }
11220 else {
11221 VALUE line = rb_hash_lookup(p->case_labels, lit);
11222 if (!NIL_P(line)) {
11223 rb_warning1("duplicated `when' clause with line %d is ignored",
11224 WARN_IVAL(line));
11225 return;
11226 }
11227 }
11228 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
11229}
11230
11231#else /* !RIPPER */
11232static int
11233id_is_var(struct parser_params *p, ID id)
11234{
11235 if (is_notop_id(id)) {
11236 switch (id & ID_SCOPE_MASK) {
11237 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
11238 return 1;
11239 case ID_LOCAL:
11240 if (dyna_in_block(p)) {
11241 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
11242 }
11243 if (local_id(p, id)) return 1;
11244 /* method call without arguments */
11245 return 0;
11246 }
11247 }
11248 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11249 return 0;
11250}
11251
11252static VALUE
11253new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
11254{
11255 VALUE src = 0, err;
11256 int options = 0;
11257 if (ripper_is_node_yylval(re)) {
11258 src = RNODE(re)->nd_cval;
11259 re = RNODE(re)->nd_rval;
11260 }
11261 if (ripper_is_node_yylval(opt)) {
11262 options = (int)RNODE(opt)->nd_tag;
11263 opt = RNODE(opt)->nd_rval;
11264 }
11265 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
11266 compile_error(p, "%"PRIsVALUE, err);
11267 }
11268 return dispatch2(regexp_literal, re, opt);
11269}
11270#endif /* !RIPPER */
11271
11272static inline enum lex_state_e
11273parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
11274{
11275 if (p->debug) {
11276 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
11277 }
11278 return p->lex.state = ls;
11279}
11280
11281#ifndef RIPPER
11282static const char rb_parser_lex_state_names[][8] = {
11283 "BEG", "END", "ENDARG", "ENDFN", "ARG",
11284 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
11285 "LABEL", "LABELED","FITEM",
11286};
11287
11288static VALUE
11289append_lex_state_name(enum lex_state_e state, VALUE buf)
11290{
11291 int i, sep = 0;
11292 unsigned int mask = 1;
11293 static const char none[] = "NONE";
11294
11295 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
11296 if ((unsigned)state & mask) {
11297 if (sep) {
11298 rb_str_cat(buf, "|", 1);
11299 }
11300 sep = 1;
11301 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
11302 }
11303 }
11304 if (!sep) {
11305 rb_str_cat(buf, none, sizeof(none)-1);
11306 }
11307 return buf;
11308}
11309
11310static void
11311flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
11312{
11313 VALUE mesg = p->debug_buffer;
11314
11315 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
11316 p->debug_buffer = Qnil;
11317 rb_io_puts(1, &mesg, out);
11318 }
11319 if (!NIL_P(str) && RSTRING_LEN(str)) {
11320 rb_io_write(p->debug_output, str);
11321 }
11322}
11323
11324enum lex_state_e
11325rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
11326 enum lex_state_e to, int line)
11327{
11328 VALUE mesg;
11329 mesg = rb_str_new_cstr("lex_state: ");
11330 append_lex_state_name(from, mesg);
11331 rb_str_cat_cstr(mesg, " -> ");
11332 append_lex_state_name(to, mesg);
11333 rb_str_catf(mesg, " at line %d\n", line);
11334 flush_debug_buffer(p, p->debug_output, mesg);
11335 return to;
11336}
11337
11338VALUE
11339rb_parser_lex_state_name(enum lex_state_e state)
11340{
11341 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
11342}
11343
11344static void
11345append_bitstack_value(stack_type stack, VALUE mesg)
11346{
11347 if (stack == 0) {
11348 rb_str_cat_cstr(mesg, "0");
11349 }
11350 else {
11351 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
11352 for (; mask && !(stack & mask); mask >>= 1) continue;
11353 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
11354 }
11355}
11356
11357void
11358rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
11359 const char *name, int line)
11360{
11361 VALUE mesg = rb_sprintf("%s: ", name);
11362 append_bitstack_value(stack, mesg);
11363 rb_str_catf(mesg, " at line %d\n", line);
11364 flush_debug_buffer(p, p->debug_output, mesg);
11365}
11366
11367void
11368rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
11369{
11370 va_list ap;
11371 VALUE mesg = rb_str_new_cstr("internal parser error: ");
11372
11373 va_start(ap, fmt);
11374 rb_str_vcatf(mesg, fmt, ap);
11375 va_end(ap);
11376 yyerror0(RSTRING_PTR(mesg));
11377 RB_GC_GUARD(mesg);
11378
11379 mesg = rb_str_new(0, 0);
11380 append_lex_state_name(p->lex.state, mesg);
11381 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
11382 rb_str_resize(mesg, 0);
11383 append_bitstack_value(p->cond_stack, mesg);
11384 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
11385 rb_str_resize(mesg, 0);
11386 append_bitstack_value(p->cmdarg_stack, mesg);
11387 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
11388 if (p->debug_output == rb_ractor_stdout())
11389 p->debug_output = rb_ractor_stderr();
11390 p->debug = TRUE;
11391}
11392
11393static YYLTYPE *
11394rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
11395{
11396 yylloc->beg_pos.lineno = sourceline;
11397 yylloc->beg_pos.column = beg_pos;
11398 yylloc->end_pos.lineno = sourceline;
11399 yylloc->end_pos.column = end_pos;
11400 return yylloc;
11401}
11402
11403YYLTYPE *
11404rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
11405{
11406 int sourceline = here->sourceline;
11407 int beg_pos = (int)here->offset - here->quote
11408 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
11409 int end_pos = (int)here->offset + here->length + here->quote;
11410
11411 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11412}
11413
11414YYLTYPE *
11415rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
11416{
11417 yylloc->beg_pos.lineno = p->delayed.beg_line;
11418 yylloc->beg_pos.column = p->delayed.beg_col;
11419 yylloc->end_pos.lineno = p->delayed.end_line;
11420 yylloc->end_pos.column = p->delayed.end_col;
11421
11422 return yylloc;
11423}
11424
11425YYLTYPE *
11426rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
11427{
11428 int sourceline = p->ruby_sourceline;
11429 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11430 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
11431 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11432}
11433
11434YYLTYPE *
11435rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
11436{
11437 yylloc->end_pos = yylloc->beg_pos;
11438
11439 return yylloc;
11440}
11441
11442YYLTYPE *
11443rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
11444{
11445 int sourceline = p->ruby_sourceline;
11446 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11447 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
11448 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11449}
11450
11451YYLTYPE *
11452rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
11453{
11454 int sourceline = p->ruby_sourceline;
11455 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11456 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
11457 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11458}
11459#endif /* !RIPPER */
11460
11461static int
11462assignable0(struct parser_params *p, ID id, const char **err)
11463{
11464 if (!id) return -1;
11465 switch (id) {
11466 case keyword_self:
11467 *err = "Can't change the value of self";
11468 return -1;
11469 case keyword_nil:
11470 *err = "Can't assign to nil";
11471 return -1;
11472 case keyword_true:
11473 *err = "Can't assign to true";
11474 return -1;
11475 case keyword_false:
11476 *err = "Can't assign to false";
11477 return -1;
11478 case keyword__FILE__:
11479 *err = "Can't assign to __FILE__";
11480 return -1;
11481 case keyword__LINE__:
11482 *err = "Can't assign to __LINE__";
11483 return -1;
11484 case keyword__ENCODING__:
11485 *err = "Can't assign to __ENCODING__";
11486 return -1;
11487 }
11488 switch (id_type(id)) {
11489 case ID_LOCAL:
11490 if (dyna_in_block(p)) {
11491 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
11492 compile_error(p, "Can't assign to numbered parameter _%d",
11493 NUMPARAM_ID_TO_IDX(id));
11494 return -1;
11495 }
11496 if (dvar_curr(p, id)) return NODE_DASGN;
11497 if (dvar_defined(p, id)) return NODE_DASGN;
11498 if (local_id(p, id)) return NODE_LASGN;
11499 dyna_var(p, id);
11500 return NODE_DASGN;
11501 }
11502 else {
11503 if (!local_id(p, id)) local_var(p, id);
11504 return NODE_LASGN;
11505 }
11506 break;
11507 case ID_GLOBAL: return NODE_GASGN;
11508 case ID_INSTANCE: return NODE_IASGN;
11509 case ID_CONST:
11510 if (!p->ctxt.in_def) return NODE_CDECL;
11511 *err = "dynamic constant assignment";
11512 return -1;
11513 case ID_CLASS: return NODE_CVASGN;
11514 default:
11515 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
11516 }
11517 return -1;
11518}
11519
11520#ifndef RIPPER
11521static NODE*
11522assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
11523{
11524 const char *err = 0;
11525 int node_type = assignable0(p, id, &err);
11526 switch (node_type) {
11527 case NODE_DASGN: return NEW_DASGN(id, val, loc);
11528 case NODE_LASGN: return NEW_LASGN(id, val, loc);
11529 case NODE_GASGN: return NEW_GASGN(id, val, loc);
11530 case NODE_IASGN: return NEW_IASGN(id, val, loc);
11531 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
11532 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
11533 }
11534 if (err) yyerror1(loc, err);
11535 return NEW_BEGIN(0, loc);
11536}
11537#else
11538static VALUE
11539assignable(struct parser_params *p, VALUE lhs)
11540{
11541 const char *err = 0;
11542 assignable0(p, get_id(lhs), &err);
11543 if (err) lhs = assign_error(p, err, lhs);
11544 return lhs;
11545}
11546#endif
11547
11548static int
11549is_private_local_id(ID name)
11550{
11551 VALUE s;
11552 if (name == idUScore) return 1;
11553 if (!is_local_id(name)) return 0;
11554 s = rb_id2str(name);
11555 if (!s) return 0;
11556 return RSTRING_PTR(s)[0] == '_';
11557}
11558
11559static int
11560shadowing_lvar_0(struct parser_params *p, ID name)
11561{
11562 if (dyna_in_block(p)) {
11563 if (dvar_curr(p, name)) {
11564 if (is_private_local_id(name)) return 1;
11565 yyerror0("duplicated argument name");
11566 }
11567 else if (dvar_defined(p, name) || local_id(p, name)) {
11568 vtable_add(p->lvtbl->vars, name);
11569 if (p->lvtbl->used) {
11570 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11571 }
11572 return 0;
11573 }
11574 }
11575 else {
11576 if (local_id(p, name)) {
11577 if (is_private_local_id(name)) return 1;
11578 yyerror0("duplicated argument name");
11579 }
11580 }
11581 return 1;
11582}
11583
11584static ID
11585shadowing_lvar(struct parser_params *p, ID name)
11586{
11587 shadowing_lvar_0(p, name);
11588 return name;
11589}
11590
11591static void
11592new_bv(struct parser_params *p, ID name)
11593{
11594 if (!name) return;
11595 if (!is_local_id(name)) {
11596 compile_error(p, "invalid local variable - %"PRIsVALUE,
11597 rb_id2str(name));
11598 return;
11599 }
11600 if (!shadowing_lvar_0(p, name)) return;
11601 dyna_var(p, name);
11602}
11603
11604#ifndef RIPPER
11605static NODE *
11606aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11607{
11608 return NEW_ATTRASGN(recv, tASET, idx, loc);
11609}
11610
11611static void
11612block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11613{
11614 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11615 compile_error(p, "both block arg and actual block given");
11616 }
11617}
11618
11619static NODE *
11620attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11621{
11622 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11623 return NEW_ATTRASGN(recv, id, 0, loc);
11624}
11625
11626static void
11627rb_backref_error(struct parser_params *p, NODE *node)
11628{
11629 switch (nd_type(node)) {
11630 case NODE_NTH_REF:
11631 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11632 break;
11633 case NODE_BACK_REF:
11634 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11635 break;
11636 }
11637}
11638#else
11639static VALUE
11640backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11641{
11642 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11643 rb_str_append(mesg, ref->nd_cval);
11644 return dispatch2(assign_error, mesg, expr);
11645}
11646#endif
11647
11648#ifndef RIPPER
11649static NODE *
11650arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11651{
11652 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11653 switch (nd_type(node1)) {
11654 case NODE_LIST:
11655 return list_append(p, node1, node2);
11656 case NODE_BLOCK_PASS:
11657 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11658 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11659 return node1;
11660 case NODE_ARGSPUSH:
11661 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11662 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11663 nd_set_type(node1, NODE_ARGSCAT);
11664 return node1;
11665 case NODE_ARGSCAT:
11666 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11667 node1->nd_body = list_append(p, node1->nd_body, node2);
11668 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11669 return node1;
11670 }
11671 return NEW_ARGSPUSH(node1, node2, loc);
11672}
11673
11674static NODE *
11675arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11676{
11677 if (!node2) return node1;
11678 switch (nd_type(node1)) {
11679 case NODE_BLOCK_PASS:
11680 if (node1->nd_head)
11681 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11682 else
11683 node1->nd_head = NEW_LIST(node2, loc);
11684 return node1;
11685 case NODE_ARGSPUSH:
11686 if (!nd_type_p(node2, NODE_LIST)) break;
11687 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11688 nd_set_type(node1, NODE_ARGSCAT);
11689 return node1;
11690 case NODE_ARGSCAT:
11691 if (!nd_type_p(node2, NODE_LIST) ||
11692 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11693 node1->nd_body = list_concat(node1->nd_body, node2);
11694 return node1;
11695 }
11696 return NEW_ARGSCAT(node1, node2, loc);
11697}
11698
11699static NODE *
11700last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11701{
11702 NODE *n1;
11703 if ((n1 = splat_array(args)) != 0) {
11704 return list_append(p, n1, last_arg);
11705 }
11706 return arg_append(p, args, last_arg, loc);
11707}
11708
11709static NODE *
11710rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11711{
11712 NODE *n1;
11713 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11714 return list_concat(n1, rest_arg);
11715 }
11716 return arg_concat(p, args, rest_arg, loc);
11717}
11718
11719static NODE *
11720splat_array(NODE* node)
11721{
11722 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11723 if (nd_type_p(node, NODE_LIST)) return node;
11724 return 0;
11725}
11726
11727static void
11728mark_lvar_used(struct parser_params *p, NODE *rhs)
11729{
11730 ID *vidp = NULL;
11731 if (!rhs) return;
11732 switch (nd_type(rhs)) {
11733 case NODE_LASGN:
11734 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11735 if (vidp) *vidp |= LVAR_USED;
11736 }
11737 break;
11738 case NODE_DASGN:
11739 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11740 if (vidp) *vidp |= LVAR_USED;
11741 }
11742 break;
11743#if 0
11744 case NODE_MASGN:
11745 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11746 mark_lvar_used(p, rhs->nd_head);
11747 }
11748 break;
11749#endif
11750 }
11751}
11752
11753static NODE *
11754const_decl_path(struct parser_params *p, NODE **dest)
11755{
11756 NODE *n = *dest;
11757 if (!nd_type_p(n, NODE_CALL)) {
11758 const YYLTYPE *loc = &n->nd_loc;
11759 VALUE path;
11760 if (n->nd_vid) {
11761 path = rb_id2str(n->nd_vid);
11762 }
11763 else {
11764 n = n->nd_else;
11765 path = rb_ary_new();
11766 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11767 rb_ary_push(path, rb_id2str(n->nd_mid));
11768 }
11769 if (n && nd_type_p(n, NODE_CONST)) {
11770 // Const::Name
11771 rb_ary_push(path, rb_id2str(n->nd_vid));
11772 }
11773 else if (n && nd_type_p(n, NODE_COLON3)) {
11774 // ::Const::Name
11775 rb_ary_push(path, rb_str_new(0, 0));
11776 }
11777 else {
11778 // expression::Name
11779 rb_ary_push(path, rb_str_new_cstr("..."));
11780 }
11781 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11782 path = rb_fstring(path);
11783 }
11784 *dest = n = NEW_LIT(path, loc);
11785 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11786 }
11787 return n;
11788}
11789
11790extern VALUE rb_mRubyVMFrozenCore;
11791
11792static NODE *
11793make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11794{
11795 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11796
11797 if (copy) {
11798 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11799 NEW_LIST(value, loc), loc);
11800 }
11801 else {
11802 return NEW_CALL(fcore, rb_intern("make_shareable"),
11803 NEW_LIST(value, loc), loc);
11804 }
11805}
11806
11807static NODE *
11808ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11809{
11810 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11811 NODE *args = NEW_LIST(value, loc);
11812 args = list_append(p, args, const_decl_path(p, dest));
11813 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11814}
11815
11816static int is_static_content(NODE *node);
11817
11818static VALUE
11819shareable_literal_value(NODE *node)
11820{
11821 if (!node) return Qnil;
11822 enum node_type type = nd_type(node);
11823 switch (type) {
11824 case NODE_TRUE:
11825 return Qtrue;
11826 case NODE_FALSE:
11827 return Qfalse;
11828 case NODE_NIL:
11829 return Qnil;
11830 case NODE_LIT:
11831 return node->nd_lit;
11832 default:
11833 return Qundef;
11834 }
11835}
11836
11837#ifndef SHAREABLE_BARE_EXPRESSION
11838#define SHAREABLE_BARE_EXPRESSION 1
11839#endif
11840
11841static NODE *
11842shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11843 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11844{
11845# define shareable_literal_constant_next(n) \
11846 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11847 VALUE lit = Qnil;
11848
11849 if (!value) return 0;
11850 enum node_type type = nd_type(value);
11851 switch (type) {
11852 case NODE_TRUE:
11853 case NODE_FALSE:
11854 case NODE_NIL:
11855 case NODE_LIT:
11856 return value;
11857
11858 case NODE_DSTR:
11859 if (shareable == shareable_literal) {
11860 value = NEW_CALL(value, idUMinus, 0, loc);
11861 }
11862 return value;
11863
11864 case NODE_STR:
11865 lit = rb_fstring(value->nd_lit);
11866 nd_set_type(value, NODE_LIT);
11867 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11868 return value;
11869
11870 case NODE_ZLIST:
11871 lit = rb_ary_new();
11872 OBJ_FREEZE_RAW(lit);
11873 NODE *n = NEW_LIT(lit, loc);
11874 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11875 return n;
11876
11877 case NODE_LIST:
11878 lit = rb_ary_new();
11879 for (NODE *n = value; n; n = n->nd_next) {
11880 NODE *elt = n->nd_head;
11881 if (elt) {
11882 elt = shareable_literal_constant_next(elt);
11883 if (elt) {
11884 n->nd_head = elt;
11885 }
11886 else if (RTEST(lit)) {
11887 rb_ary_clear(lit);
11888 lit = Qfalse;
11889 }
11890 }
11891 if (RTEST(lit)) {
11892 VALUE e = shareable_literal_value(elt);
11893 if (!UNDEF_P(e)) {
11894 rb_ary_push(lit, e);
11895 }
11896 else {
11897 rb_ary_clear(lit);
11898 lit = Qnil; /* make shareable at runtime */
11899 }
11900 }
11901 }
11902 break;
11903
11904 case NODE_HASH:
11905 if (!value->nd_brace) return 0;
11906 lit = rb_hash_new();
11907 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11908 NODE *key = n->nd_head;
11909 NODE *val = n->nd_next->nd_head;
11910 if (key) {
11911 key = shareable_literal_constant_next(key);
11912 if (key) {
11913 n->nd_head = key;
11914 }
11915 else if (RTEST(lit)) {
11916 rb_hash_clear(lit);
11917 lit = Qfalse;
11918 }
11919 }
11920 if (val) {
11921 val = shareable_literal_constant_next(val);
11922 if (val) {
11923 n->nd_next->nd_head = val;
11924 }
11925 else if (RTEST(lit)) {
11926 rb_hash_clear(lit);
11927 lit = Qfalse;
11928 }
11929 }
11930 if (RTEST(lit)) {
11931 VALUE k = shareable_literal_value(key);
11932 VALUE v = shareable_literal_value(val);
11933 if (!UNDEF_P(k) && !UNDEF_P(v)) {
11934 rb_hash_aset(lit, k, v);
11935 }
11936 else {
11937 rb_hash_clear(lit);
11938 lit = Qnil; /* make shareable at runtime */
11939 }
11940 }
11941 }
11942 break;
11943
11944 default:
11945 if (shareable == shareable_literal &&
11946 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11947 return ensure_shareable_node(p, dest, value, loc);
11948 }
11949 return 0;
11950 }
11951
11952 /* Array or Hash */
11953 if (!lit) return 0;
11954 if (NIL_P(lit)) {
11955 // if shareable_literal, all elements should have been ensured
11956 // as shareable
11957 value = make_shareable_node(p, value, false, loc);
11958 }
11959 else {
11960 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11961 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11962 }
11963
11964 return value;
11965# undef shareable_literal_constant_next
11966}
11967
11968static NODE *
11969shareable_constant_value(struct parser_params *p, enum shareability shareable,
11970 NODE *lhs, NODE *value, const YYLTYPE *loc)
11971{
11972 if (!value) return 0;
11973 switch (shareable) {
11974 case shareable_none:
11975 return value;
11976
11977 case shareable_literal:
11978 {
11979 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11980 if (lit) return lit;
11981 return value;
11982 }
11983 break;
11984
11985 case shareable_copy:
11986 case shareable_everything:
11987 {
11988 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11989 if (lit) return lit;
11990 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11991 }
11992 break;
11993
11994 default:
11995 UNREACHABLE_RETURN(0);
11996 }
11997}
11998
11999static NODE *
12000node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12001{
12002 if (!lhs) return 0;
12003
12004 switch (nd_type(lhs)) {
12005 case NODE_CDECL:
12006 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12007 /* fallthru */
12008
12009 case NODE_GASGN:
12010 case NODE_IASGN:
12011 case NODE_LASGN:
12012 case NODE_DASGN:
12013 case NODE_MASGN:
12014 case NODE_CVASGN:
12015 lhs->nd_value = rhs;
12016 nd_set_loc(lhs, loc);
12017 break;
12018
12019 case NODE_ATTRASGN:
12020 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
12021 nd_set_loc(lhs, loc);
12022 break;
12023
12024 default:
12025 /* should not happen */
12026 break;
12027 }
12028
12029 return lhs;
12030}
12031
12032static NODE *
12033value_expr_check(struct parser_params *p, NODE *node)
12034{
12035 NODE *void_node = 0, *vn;
12036
12037 if (!node) {
12038 rb_warning0("empty expression");
12039 }
12040 while (node) {
12041 switch (nd_type(node)) {
12042 case NODE_RETURN:
12043 case NODE_BREAK:
12044 case NODE_NEXT:
12045 case NODE_REDO:
12046 case NODE_RETRY:
12047 return void_node ? void_node : node;
12048
12049 case NODE_CASE3:
12050 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
12051 compile_error(p, "unexpected node");
12052 return NULL;
12053 }
12054 if (node->nd_body->nd_body) {
12055 return NULL;
12056 }
12057 /* single line pattern matching */
12058 return void_node ? void_node : node;
12059
12060 case NODE_BLOCK:
12061 while (node->nd_next) {
12062 node = node->nd_next;
12063 }
12064 node = node->nd_head;
12065 break;
12066
12067 case NODE_BEGIN:
12068 node = node->nd_body;
12069 break;
12070
12071 case NODE_IF:
12072 case NODE_UNLESS:
12073 if (!node->nd_body) {
12074 return NULL;
12075 }
12076 else if (!node->nd_else) {
12077 return NULL;
12078 }
12079 vn = value_expr_check(p, node->nd_body);
12080 if (!vn) return NULL;
12081 if (!void_node) void_node = vn;
12082 node = node->nd_else;
12083 break;
12084
12085 case NODE_AND:
12086 case NODE_OR:
12087 node = node->nd_1st;
12088 break;
12089
12090 case NODE_LASGN:
12091 case NODE_DASGN:
12092 case NODE_MASGN:
12093 mark_lvar_used(p, node);
12094 return NULL;
12095
12096 default:
12097 return NULL;
12098 }
12099 }
12100
12101 return NULL;
12102}
12103
12104static int
12105value_expr_gen(struct parser_params *p, NODE *node)
12106{
12107 NODE *void_node = value_expr_check(p, node);
12108 if (void_node) {
12109 yyerror1(&void_node->nd_loc, "void value expression");
12110 /* or "control never reach"? */
12111 return FALSE;
12112 }
12113 return TRUE;
12114}
12115static void
12116void_expr(struct parser_params *p, NODE *node)
12117{
12118 const char *useless = 0;
12119
12120 if (!RTEST(ruby_verbose)) return;
12121
12122 if (!node || !(node = nd_once_body(node))) return;
12123 switch (nd_type(node)) {
12124 case NODE_OPCALL:
12125 switch (node->nd_mid) {
12126 case '+':
12127 case '-':
12128 case '*':
12129 case '/':
12130 case '%':
12131 case tPOW:
12132 case tUPLUS:
12133 case tUMINUS:
12134 case '|':
12135 case '^':
12136 case '&':
12137 case tCMP:
12138 case '>':
12139 case tGEQ:
12140 case '<':
12141 case tLEQ:
12142 case tEQ:
12143 case tNEQ:
12144 useless = rb_id2name(node->nd_mid);
12145 break;
12146 }
12147 break;
12148
12149 case NODE_LVAR:
12150 case NODE_DVAR:
12151 case NODE_GVAR:
12152 case NODE_IVAR:
12153 case NODE_CVAR:
12154 case NODE_NTH_REF:
12155 case NODE_BACK_REF:
12156 useless = "a variable";
12157 break;
12158 case NODE_CONST:
12159 useless = "a constant";
12160 break;
12161 case NODE_LIT:
12162 case NODE_STR:
12163 case NODE_DSTR:
12164 case NODE_DREGX:
12165 useless = "a literal";
12166 break;
12167 case NODE_COLON2:
12168 case NODE_COLON3:
12169 useless = "::";
12170 break;
12171 case NODE_DOT2:
12172 useless = "..";
12173 break;
12174 case NODE_DOT3:
12175 useless = "...";
12176 break;
12177 case NODE_SELF:
12178 useless = "self";
12179 break;
12180 case NODE_NIL:
12181 useless = "nil";
12182 break;
12183 case NODE_TRUE:
12184 useless = "true";
12185 break;
12186 case NODE_FALSE:
12187 useless = "false";
12188 break;
12189 case NODE_DEFINED:
12190 useless = "defined?";
12191 break;
12192 }
12193
12194 if (useless) {
12195 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
12196 }
12197}
12198
12199static NODE *
12200void_stmts(struct parser_params *p, NODE *node)
12201{
12202 NODE *const n = node;
12203 if (!RTEST(ruby_verbose)) return n;
12204 if (!node) return n;
12205 if (!nd_type_p(node, NODE_BLOCK)) return n;
12206
12207 while (node->nd_next) {
12208 void_expr(p, node->nd_head);
12209 node = node->nd_next;
12210 }
12211 return n;
12212}
12213
12214static NODE *
12215remove_begin(NODE *node)
12216{
12217 NODE **n = &node, *n1 = node;
12218 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
12219 *n = n1 = n1->nd_body;
12220 }
12221 return node;
12222}
12223
12224static NODE *
12225remove_begin_all(NODE *node)
12226{
12227 NODE **n = &node, *n1 = node;
12228 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
12229 *n = n1 = n1->nd_body;
12230 }
12231 return node;
12232}
12233
12234static void
12235reduce_nodes(struct parser_params *p, NODE **body)
12236{
12237 NODE *node = *body;
12238
12239 if (!node) {
12240 *body = NEW_NIL(&NULL_LOC);
12241 return;
12242 }
12243#define subnodes(n1, n2) \
12244 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
12245 (!node->n2) ? (body = &node->n1, 1) : \
12246 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
12247
12248 while (node) {
12249 int newline = (int)(node->flags & NODE_FL_NEWLINE);
12250 switch (nd_type(node)) {
12251 end:
12252 case NODE_NIL:
12253 *body = 0;
12254 return;
12255 case NODE_RETURN:
12256 *body = node = node->nd_stts;
12257 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12258 continue;
12259 case NODE_BEGIN:
12260 *body = node = node->nd_body;
12261 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12262 continue;
12263 case NODE_BLOCK:
12264 body = &node->nd_end->nd_head;
12265 break;
12266 case NODE_IF:
12267 case NODE_UNLESS:
12268 if (subnodes(nd_body, nd_else)) break;
12269 return;
12270 case NODE_CASE:
12271 body = &node->nd_body;
12272 break;
12273 case NODE_WHEN:
12274 if (!subnodes(nd_body, nd_next)) goto end;
12275 break;
12276 case NODE_ENSURE:
12277 if (!subnodes(nd_head, nd_resq)) goto end;
12278 break;
12279 case NODE_RESCUE:
12280 if (node->nd_else) {
12281 body = &node->nd_resq;
12282 break;
12283 }
12284 if (!subnodes(nd_head, nd_resq)) goto end;
12285 break;
12286 default:
12287 return;
12288 }
12289 node = *body;
12290 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12291 }
12292
12293#undef subnodes
12294}
12295
12296static int
12297is_static_content(NODE *node)
12298{
12299 if (!node) return 1;
12300 switch (nd_type(node)) {
12301 case NODE_HASH:
12302 if (!(node = node->nd_head)) break;
12303 case NODE_LIST:
12304 do {
12305 if (!is_static_content(node->nd_head)) return 0;
12306 } while ((node = node->nd_next) != 0);
12307 case NODE_LIT:
12308 case NODE_STR:
12309 case NODE_NIL:
12310 case NODE_TRUE:
12311 case NODE_FALSE:
12312 case NODE_ZLIST:
12313 break;
12314 default:
12315 return 0;
12316 }
12317 return 1;
12318}
12319
12320static int
12321assign_in_cond(struct parser_params *p, NODE *node)
12322{
12323 switch (nd_type(node)) {
12324 case NODE_MASGN:
12325 case NODE_LASGN:
12326 case NODE_DASGN:
12327 case NODE_GASGN:
12328 case NODE_IASGN:
12329 break;
12330
12331 default:
12332 return 0;
12333 }
12334
12335 if (!node->nd_value) return 1;
12336 if (is_static_content(node->nd_value)) {
12337 /* reports always */
12338 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
12339 }
12340 return 1;
12341}
12342
12343enum cond_type {
12344 COND_IN_OP,
12345 COND_IN_COND,
12346 COND_IN_FF
12347};
12348
12349#define SWITCH_BY_COND_TYPE(t, w, arg) \
12350 switch (t) { \
12351 case COND_IN_OP: break; \
12352 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
12353 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
12354 }
12355
12356static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
12357
12358static NODE*
12359range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12360{
12361 enum node_type type;
12362
12363 if (node == 0) return 0;
12364
12365 type = nd_type(node);
12366 value_expr(node);
12367 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
12368 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
12369 ID lineno = rb_intern("$.");
12370 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
12371 }
12372 return cond0(p, node, COND_IN_FF, loc);
12373}
12374
12375static NODE*
12376cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
12377{
12378 if (node == 0) return 0;
12379 if (!(node = nd_once_body(node))) return 0;
12380 assign_in_cond(p, node);
12381
12382 switch (nd_type(node)) {
12383 case NODE_DSTR:
12384 case NODE_EVSTR:
12385 case NODE_STR:
12386 SWITCH_BY_COND_TYPE(type, warn, "string ")
12387 break;
12388
12389 case NODE_DREGX:
12390 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
12391
12392 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
12393
12394 case NODE_AND:
12395 case NODE_OR:
12396 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
12397 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
12398 break;
12399
12400 case NODE_DOT2:
12401 case NODE_DOT3:
12402 node->nd_beg = range_op(p, node->nd_beg, loc);
12403 node->nd_end = range_op(p, node->nd_end, loc);
12404 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
12405 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
12406 break;
12407
12408 case NODE_DSYM:
12409 warn_symbol:
12410 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
12411 break;
12412
12413 case NODE_LIT:
12414 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
12415 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
12416 nd_set_type(node, NODE_MATCH);
12417 }
12418 else if (node->nd_lit == Qtrue ||
12419 node->nd_lit == Qfalse) {
12420 /* booleans are OK, e.g., while true */
12421 }
12422 else if (SYMBOL_P(node->nd_lit)) {
12423 goto warn_symbol;
12424 }
12425 else {
12426 SWITCH_BY_COND_TYPE(type, warning, "")
12427 }
12428 default:
12429 break;
12430 }
12431 return node;
12432}
12433
12434static NODE*
12435cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12436{
12437 if (node == 0) return 0;
12438 return cond0(p, node, COND_IN_COND, loc);
12439}
12440
12441static NODE*
12442method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12443{
12444 if (node == 0) return 0;
12445 return cond0(p, node, COND_IN_OP, loc);
12446}
12447
12448static NODE*
12449new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
12450{
12451 YYLTYPE loc = {*pos, *pos};
12452 return NEW_NIL(&loc);
12453}
12454
12455static NODE*
12456new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12457{
12458 if (!cc) return right;
12459 cc = cond0(p, cc, COND_IN_COND, loc);
12460 return newline_node(NEW_IF(cc, left, right, loc));
12461}
12462
12463static NODE*
12464new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12465{
12466 if (!cc) return right;
12467 cc = cond0(p, cc, COND_IN_COND, loc);
12468 return newline_node(NEW_UNLESS(cc, left, right, loc));
12469}
12470
12471static NODE*
12472logop(struct parser_params *p, ID id, NODE *left, NODE *right,
12473 const YYLTYPE *op_loc, const YYLTYPE *loc)
12474{
12475 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
12476 NODE *op;
12477 value_expr(left);
12478 if (left && nd_type_p(left, type)) {
12479 NODE *node = left, *second;
12480 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
12481 node = second;
12482 }
12483 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
12484 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
12485 left->nd_loc.end_pos = loc->end_pos;
12486 return left;
12487 }
12488 op = NEW_NODE(type, left, right, 0, loc);
12489 nd_set_line(op, op_loc->beg_pos.lineno);
12490 return op;
12491}
12492
12493static void
12494no_blockarg(struct parser_params *p, NODE *node)
12495{
12496 if (nd_type_p(node, NODE_BLOCK_PASS)) {
12497 compile_error(p, "block argument should not be given");
12498 }
12499}
12500
12501static NODE *
12502ret_args(struct parser_params *p, NODE *node)
12503{
12504 if (node) {
12505 no_blockarg(p, node);
12506 if (nd_type_p(node, NODE_LIST)) {
12507 if (node->nd_next == 0) {
12508 node = node->nd_head;
12509 }
12510 else {
12511 nd_set_type(node, NODE_VALUES);
12512 }
12513 }
12514 }
12515 return node;
12516}
12517
12518static NODE *
12519new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12520{
12521 if (node) no_blockarg(p, node);
12522
12523 return NEW_YIELD(node, loc);
12524}
12525
12526static VALUE
12527negate_lit(struct parser_params *p, VALUE lit)
12528{
12529 if (FIXNUM_P(lit)) {
12530 return LONG2FIX(-FIX2LONG(lit));
12531 }
12532 if (SPECIAL_CONST_P(lit)) {
12533#if USE_FLONUM
12534 if (FLONUM_P(lit)) {
12535 return DBL2NUM(-RFLOAT_VALUE(lit));
12536 }
12537#endif
12538 goto unknown;
12539 }
12540 switch (BUILTIN_TYPE(lit)) {
12541 case T_BIGNUM:
12542 BIGNUM_NEGATE(lit);
12543 lit = rb_big_norm(lit);
12544 break;
12545 case T_RATIONAL:
12546 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
12547 break;
12548 case T_COMPLEX:
12549 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
12550 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
12551 break;
12552 case T_FLOAT:
12553 lit = DBL2NUM(-RFLOAT_VALUE(lit));
12554 break;
12555 unknown:
12556 default:
12557 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
12558 rb_builtin_class_name(lit));
12559 break;
12560 }
12561 return lit;
12562}
12563
12564static NODE *
12565arg_blk_pass(NODE *node1, NODE *node2)
12566{
12567 if (node2) {
12568 if (!node1) return node2;
12569 node2->nd_head = node1;
12570 nd_set_first_lineno(node2, nd_first_lineno(node1));
12571 nd_set_first_column(node2, nd_first_column(node1));
12572 return node2;
12573 }
12574 return node1;
12575}
12576
12577static bool
12578args_info_empty_p(struct rb_args_info *args)
12579{
12580 if (args->pre_args_num) return false;
12581 if (args->post_args_num) return false;
12582 if (args->rest_arg) return false;
12583 if (args->opt_args) return false;
12584 if (args->block_arg) return false;
12585 if (args->kw_args) return false;
12586 if (args->kw_rest_arg) return false;
12587 return true;
12588}
12589
12590static NODE*
12591new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12592{
12593 int saved_line = p->ruby_sourceline;
12594 struct rb_args_info *args = tail->nd_ainfo;
12595
12596 if (args->forwarding) {
12597 if (rest_arg) {
12598 yyerror1(&tail->nd_loc, "... after rest argument");
12599 return tail;
12600 }
12601 rest_arg = idFWD_REST;
12602 }
12603
12604 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12605 args->pre_init = pre_args ? pre_args->nd_next : 0;
12606
12607 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12608 args->post_init = post_args ? post_args->nd_next : 0;
12609 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12610
12611 args->rest_arg = rest_arg;
12612
12613 args->opt_args = opt_args;
12614
12615#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
12616 args->ruby2_keywords = args->forwarding;
12617#else
12618 args->ruby2_keywords = 0;
12619#endif
12620
12621 p->ruby_sourceline = saved_line;
12622 nd_set_loc(tail, loc);
12623
12624 return tail;
12625}
12626
12627static NODE*
12628new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12629{
12630 int saved_line = p->ruby_sourceline;
12631 NODE *node;
12632 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12633 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12634 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12635 args->imemo = tmpbuf;
12636 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12637 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12638 if (p->error_p) return node;
12639
12640 args->block_arg = block;
12641 args->kw_args = kw_args;
12642
12643 if (kw_args) {
12644 /*
12645 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12646 * variable order: k1, kr1, k2, &b, internal_id, krest
12647 * #=> <reorder>
12648 * variable order: kr1, k1, k2, internal_id, krest, &b
12649 */
12650 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12651 struct vtable *vtargs = p->lvtbl->args;
12652 NODE *kwn = kw_args;
12653
12654 if (block) block = vtargs->tbl[vtargs->pos-1];
12655 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12656 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12657 while (kwn) {
12658 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12659 --kw_vars;
12660 --required_kw_vars;
12661 kwn = kwn->nd_next;
12662 }
12663
12664 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12665 ID vid = kwn->nd_body->nd_vid;
12666 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12667 *required_kw_vars++ = vid;
12668 }
12669 else {
12670 *kw_vars++ = vid;
12671 }
12672 }
12673
12674 arg_var(p, kw_bits);
12675 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12676 if (block) arg_var(p, block);
12677
12678 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12679 args->kw_rest_arg->nd_cflag = kw_bits;
12680 }
12681 else if (kw_rest_arg == idNil) {
12682 args->no_kwarg = 1;
12683 }
12684 else if (kw_rest_arg) {
12685 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12686 }
12687
12688 p->ruby_sourceline = saved_line;
12689 return node;
12690}
12691
12692static NODE *
12693args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12694{
12695 if (max_numparam > NO_PARAM) {
12696 if (!args) {
12697 YYLTYPE loc = RUBY_INIT_YYLLOC();
12698 args = new_args_tail(p, 0, 0, 0, 0);
12699 nd_set_loc(args, &loc);
12700 }
12701 args->nd_ainfo->pre_args_num = max_numparam;
12702 }
12703 return args;
12704}
12705
12706static NODE*
12707new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12708{
12709 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12710
12711 aryptn->nd_pconst = constant;
12712
12713 if (pre_arg) {
12714 NODE *pre_args = NEW_LIST(pre_arg, loc);
12715 if (apinfo->pre_args) {
12716 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12717 }
12718 else {
12719 apinfo->pre_args = pre_args;
12720 }
12721 }
12722 return aryptn;
12723}
12724
12725static NODE*
12726new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12727{
12728 int saved_line = p->ruby_sourceline;
12729 NODE *node;
12730 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12731 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12732 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12733 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12734 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12735
12736 apinfo->pre_args = pre_args;
12737
12738 if (has_rest) {
12739 if (rest_arg) {
12740 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12741 }
12742 else {
12743 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12744 }
12745 }
12746 else {
12747 apinfo->rest_arg = NULL;
12748 }
12749
12750 apinfo->post_args = post_args;
12751
12752 p->ruby_sourceline = saved_line;
12753 return node;
12754}
12755
12756static NODE*
12757new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12758{
12759 fndptn->nd_pconst = constant;
12760
12761 return fndptn;
12762}
12763
12764static NODE*
12765new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12766{
12767 int saved_line = p->ruby_sourceline;
12768 NODE *node;
12769 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12770 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12771 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12772 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12773 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12774
12775 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12776 fpinfo->args = args;
12777 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12778
12779 p->ruby_sourceline = saved_line;
12780 return node;
12781}
12782
12783static NODE*
12784new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12785{
12786 hshptn->nd_pconst = constant;
12787 return hshptn;
12788}
12789
12790static NODE*
12791new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12792{
12793 int saved_line = p->ruby_sourceline;
12794 NODE *node, *kw_rest_arg_node;
12795
12796 if (kw_rest_arg == idNil) {
12797 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12798 }
12799 else if (kw_rest_arg) {
12800 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12801 }
12802 else {
12803 kw_rest_arg_node = NULL;
12804 }
12805
12806 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12807
12808 p->ruby_sourceline = saved_line;
12809 return node;
12810}
12811
12812static NODE*
12813dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12814{
12815 VALUE lit;
12816
12817 if (!node) {
12818 return NEW_LIT(ID2SYM(idNULL), loc);
12819 }
12820
12821 switch (nd_type(node)) {
12822 case NODE_DSTR:
12823 nd_set_type(node, NODE_DSYM);
12824 nd_set_loc(node, loc);
12825 break;
12826 case NODE_STR:
12827 lit = node->nd_lit;
12828 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12829 nd_set_type(node, NODE_LIT);
12830 nd_set_loc(node, loc);
12831 break;
12832 default:
12833 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12834 break;
12835 }
12836 return node;
12837}
12838
12839static int
12840append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12841{
12842 NODE *node = (NODE *)v;
12843 NODE **result = (NODE **)h;
12844 node->nd_alen = 2;
12845 node->nd_next->nd_end = node->nd_next;
12846 node->nd_next->nd_next = 0;
12847 if (*result)
12848 list_concat(*result, node);
12849 else
12850 *result = node;
12851 return ST_CONTINUE;
12852}
12853
12854static bool
12855hash_literal_key_p(VALUE k)
12856{
12857 switch (OBJ_BUILTIN_TYPE(k)) {
12858 case T_NODE:
12859 return false;
12860 default:
12861 return true;
12862 }
12863}
12864
12865static int
12866literal_cmp(VALUE val, VALUE lit)
12867{
12868 if (val == lit) return 0;
12869 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12870 return rb_iseq_cdhash_cmp(val, lit);
12871}
12872
12873static st_index_t
12874literal_hash(VALUE a)
12875{
12876 if (!hash_literal_key_p(a)) return (st_index_t)a;
12877 return rb_iseq_cdhash_hash(a);
12878}
12879
12880static const struct st_hash_type literal_type = {
12881 literal_cmp,
12882 literal_hash,
12883};
12884
12885static NODE *
12886remove_duplicate_keys(struct parser_params *p, NODE *hash)
12887{
12888 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12889 NODE *result = 0;
12890 NODE *last_expr = 0;
12891 rb_code_location_t loc = hash->nd_loc;
12892 while (hash && hash->nd_head && hash->nd_next) {
12893 NODE *head = hash->nd_head;
12894 NODE *value = hash->nd_next;
12895 NODE *next = value->nd_next;
12896 st_data_t key = (st_data_t)head;
12897 st_data_t data;
12898 value->nd_next = 0;
12899 if (nd_type_p(head, NODE_LIT) &&
12900 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12901 NODE *dup_value = ((NODE *)data)->nd_next;
12902 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12903 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12904 head->nd_lit, nd_line(head));
12905 if (dup_value == last_expr) {
12906 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12907 }
12908 else {
12909 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12910 }
12911 }
12912 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12913 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12914 hash = next;
12915 }
12916 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12917 st_free_table(literal_keys);
12918 if (hash) {
12919 if (!result) result = hash;
12920 else list_concat(result, hash);
12921 }
12922 result->nd_loc = loc;
12923 return result;
12924}
12925
12926static NODE *
12927new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12928{
12929 if (hash) hash = remove_duplicate_keys(p, hash);
12930 return NEW_HASH(hash, loc);
12931}
12932#endif
12933
12934static void
12935error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12936{
12937 if (is_private_local_id(id)) {
12938 return;
12939 }
12940 if (st_is_member(p->pvtbl, id)) {
12941 yyerror1(loc, "duplicated variable name");
12942 }
12943 else {
12944 st_insert(p->pvtbl, (st_data_t)id, 0);
12945 }
12946}
12947
12948static void
12949error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12950{
12951 if (!p->pktbl) {
12952 p->pktbl = st_init_numtable();
12953 }
12954 else if (st_is_member(p->pktbl, key)) {
12955 yyerror1(loc, "duplicated key name");
12956 return;
12957 }
12958 st_insert(p->pktbl, (st_data_t)key, 0);
12959}
12960
12961#ifndef RIPPER
12962static NODE *
12963new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12964{
12965 return NEW_HASH(hash, loc);
12966}
12967#endif /* !RIPPER */
12968
12969#ifndef RIPPER
12970static NODE *
12971new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12972{
12973 NODE *asgn;
12974
12975 if (lhs) {
12976 ID vid = lhs->nd_vid;
12977 YYLTYPE lhs_loc = lhs->nd_loc;
12978 int shareable = ctxt.shareable_constant_value;
12979 if (shareable) {
12980 switch (nd_type(lhs)) {
12981 case NODE_CDECL:
12982 case NODE_COLON2:
12983 case NODE_COLON3:
12984 break;
12985 default:
12986 shareable = 0;
12987 break;
12988 }
12989 }
12990 if (op == tOROP) {
12991 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12992 lhs->nd_value = rhs;
12993 nd_set_loc(lhs, loc);
12994 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12995 if (is_notop_id(vid)) {
12996 switch (id_type(vid)) {
12997 case ID_GLOBAL:
12998 case ID_INSTANCE:
12999 case ID_CLASS:
13000 asgn->nd_aid = vid;
13001 }
13002 }
13003 }
13004 else if (op == tANDOP) {
13005 if (shareable) {
13006 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13007 }
13008 lhs->nd_value = rhs;
13009 nd_set_loc(lhs, loc);
13010 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
13011 }
13012 else {
13013 asgn = lhs;
13014 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
13015 if (shareable) {
13016 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13017 }
13018 asgn->nd_value = rhs;
13019 nd_set_loc(asgn, loc);
13020 }
13021 }
13022 else {
13023 asgn = NEW_BEGIN(0, loc);
13024 }
13025 return asgn;
13026}
13027
13028static NODE *
13029new_ary_op_assign(struct parser_params *p, NODE *ary,
13030 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
13031{
13032 NODE *asgn;
13033
13034 args = make_list(args, args_loc);
13035 if (nd_type_p(args, NODE_BLOCK_PASS)) {
13036 args = NEW_ARGSCAT(args, rhs, loc);
13037 }
13038 else {
13039 args = arg_concat(p, args, rhs, loc);
13040 }
13041 asgn = NEW_OP_ASGN1(ary, op, args, loc);
13042 fixpos(asgn, ary);
13043 return asgn;
13044}
13045
13046static NODE *
13047new_attr_op_assign(struct parser_params *p, NODE *lhs,
13048 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
13049{
13050 NODE *asgn;
13051
13052 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
13053 fixpos(asgn, lhs);
13054 return asgn;
13055}
13056
13057static NODE *
13058new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13059{
13060 NODE *asgn;
13061
13062 if (lhs) {
13063 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13064 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
13065 }
13066 else {
13067 asgn = NEW_BEGIN(0, loc);
13068 }
13069 fixpos(asgn, lhs);
13070 return asgn;
13071}
13072
13073static NODE *
13074const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
13075{
13076 if (p->ctxt.in_def) {
13077 yyerror1(loc, "dynamic constant assignment");
13078 }
13079 return NEW_CDECL(0, 0, (path), loc);
13080}
13081#else
13082static VALUE
13083const_decl(struct parser_params *p, VALUE path)
13084{
13085 if (p->ctxt.in_def) {
13086 path = assign_error(p, "dynamic constant assignment", path);
13087 }
13088 return path;
13089}
13090
13091static VALUE
13092assign_error(struct parser_params *p, const char *mesg, VALUE a)
13093{
13094 a = dispatch2(assign_error, ERR_MESG(), a);
13095 ripper_error(p);
13096 return a;
13097}
13098
13099static VALUE
13100var_field(struct parser_params *p, VALUE a)
13101{
13102 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
13103}
13104#endif
13105
13106#ifndef RIPPER
13107static NODE *
13108new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
13109{
13110 NODE *result = head;
13111 if (rescue) {
13112 NODE *tmp = rescue_else ? rescue_else : rescue;
13113 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
13114
13115 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
13116 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
13117 }
13118 else if (rescue_else) {
13119 result = block_append(p, result, rescue_else);
13120 }
13121 if (ensure) {
13122 result = NEW_ENSURE(result, ensure, loc);
13123 }
13124 fixpos(result, head);
13125 return result;
13126}
13127#endif
13128
13129static void
13130warn_unused_var(struct parser_params *p, struct local_vars *local)
13131{
13132 int cnt;
13133
13134 if (!local->used) return;
13135 cnt = local->used->pos;
13136 if (cnt != local->vars->pos) {
13137 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
13138 }
13139#ifndef RIPPER
13140 ID *v = local->vars->tbl;
13141 ID *u = local->used->tbl;
13142 for (int i = 0; i < cnt; ++i) {
13143 if (!v[i] || (u[i] & LVAR_USED)) continue;
13144 if (is_private_local_id(v[i])) continue;
13145 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
13146 }
13147#endif
13148}
13149
13150static void
13151local_push(struct parser_params *p, int toplevel_scope)
13152{
13153 struct local_vars *local;
13154 int inherits_dvars = toplevel_scope && compile_for_eval;
13155 int warn_unused_vars = RTEST(ruby_verbose);
13156
13157 local = ALLOC(struct local_vars);
13158 local->prev = p->lvtbl;
13159 local->args = vtable_alloc(0);
13160 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
13161#ifndef RIPPER
13162 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
13163 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
13164 local->numparam.outer = 0;
13165 local->numparam.inner = 0;
13166 local->numparam.current = 0;
13167#endif
13168 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
13169
13170# if WARN_PAST_SCOPE
13171 local->past = 0;
13172# endif
13173 CMDARG_PUSH(0);
13174 COND_PUSH(0);
13175 p->lvtbl = local;
13176}
13177
13178static void
13179vtable_chain_free(struct parser_params *p, struct vtable *table)
13180{
13181 while (!DVARS_TERMINAL_P(table)) {
13182 struct vtable *cur_table = table;
13183 table = cur_table->prev;
13184 vtable_free(cur_table);
13185 }
13186}
13187
13188static void
13189local_free(struct parser_params *p, struct local_vars *local)
13190{
13191 vtable_chain_free(p, local->used);
13192
13193# if WARN_PAST_SCOPE
13194 vtable_chain_free(p, local->past);
13195# endif
13196
13197 vtable_chain_free(p, local->args);
13198 vtable_chain_free(p, local->vars);
13199
13200 ruby_sized_xfree(local, sizeof(struct local_vars));
13201}
13202
13203static void
13204local_pop(struct parser_params *p)
13205{
13206 struct local_vars *local = p->lvtbl->prev;
13207 if (p->lvtbl->used) {
13208 warn_unused_var(p, p->lvtbl);
13209 }
13210
13211 local_free(p, p->lvtbl);
13212 p->lvtbl = local;
13213
13214 CMDARG_POP();
13215 COND_POP();
13216}
13217
13218#ifndef RIPPER
13219static rb_ast_id_table_t *
13220local_tbl(struct parser_params *p)
13221{
13222 int cnt_args = vtable_size(p->lvtbl->args);
13223 int cnt_vars = vtable_size(p->lvtbl->vars);
13224 int cnt = cnt_args + cnt_vars;
13225 int i, j;
13226 rb_ast_id_table_t *tbl;
13227
13228 if (cnt <= 0) return 0;
13229 tbl = rb_ast_new_local_table(p->ast, cnt);
13230 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
13231 /* remove IDs duplicated to warn shadowing */
13232 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
13233 ID id = p->lvtbl->vars->tbl[i];
13234 if (!vtable_included(p->lvtbl->args, id)) {
13235 tbl->ids[j++] = id;
13236 }
13237 }
13238 if (j < cnt) {
13239 tbl = rb_ast_resize_latest_local_table(p->ast, j);
13240 }
13241
13242 return tbl;
13243}
13244
13245static NODE*
13246node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
13247{
13248 rb_ast_id_table_t *a0;
13249 NODE *n;
13250
13251 a0 = local_tbl(p);
13252 n = NEW_NODE(type, a0, a1, a2, loc);
13253 return n;
13254}
13255
13256#endif
13257
13258static void
13259numparam_name(struct parser_params *p, ID id)
13260{
13261 if (!NUMPARAM_ID_P(id)) return;
13262 compile_error(p, "_%d is reserved for numbered parameter",
13263 NUMPARAM_ID_TO_IDX(id));
13264}
13265
13266static void
13267arg_var(struct parser_params *p, ID id)
13268{
13269 numparam_name(p, id);
13270 vtable_add(p->lvtbl->args, id);
13271}
13272
13273static void
13274local_var(struct parser_params *p, ID id)
13275{
13276 numparam_name(p, id);
13277 vtable_add(p->lvtbl->vars, id);
13278 if (p->lvtbl->used) {
13279 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
13280 }
13281}
13282
13283static int
13284local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
13285{
13286 struct vtable *vars, *args, *used;
13287
13288 vars = p->lvtbl->vars;
13289 args = p->lvtbl->args;
13290 used = p->lvtbl->used;
13291
13292 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
13293 vars = vars->prev;
13294 args = args->prev;
13295 if (used) used = used->prev;
13296 }
13297
13298 if (vars && vars->prev == DVARS_INHERIT) {
13299 return rb_local_defined(id, p->parent_iseq);
13300 }
13301 else if (vtable_included(args, id)) {
13302 return 1;
13303 }
13304 else {
13305 int i = vtable_included(vars, id);
13306 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
13307 return i != 0;
13308 }
13309}
13310
13311static int
13312local_id(struct parser_params *p, ID id)
13313{
13314 return local_id_ref(p, id, NULL);
13315}
13316
13317static int
13318check_forwarding_args(struct parser_params *p)
13319{
13320 if (local_id(p, idFWD_ALL)) return TRUE;
13321 compile_error(p, "unexpected ...");
13322 return FALSE;
13323}
13324
13325static void
13326add_forwarding_args(struct parser_params *p)
13327{
13328 arg_var(p, idFWD_REST);
13329#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13330 arg_var(p, idFWD_KWREST);
13331#endif
13332 arg_var(p, idFWD_BLOCK);
13333 arg_var(p, idFWD_ALL);
13334}
13335
13336#ifndef RIPPER
13337static NODE *
13338new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
13339{
13340 NODE *rest = NEW_LVAR(idFWD_REST, loc);
13341#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13342 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
13343#endif
13344 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
13345 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
13346#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13347 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
13348#endif
13349 return arg_blk_pass(args, block);
13350}
13351#endif
13352
13353static NODE *
13354numparam_push(struct parser_params *p)
13355{
13356#ifndef RIPPER
13357 struct local_vars *local = p->lvtbl;
13358 NODE *inner = local->numparam.inner;
13359 if (!local->numparam.outer) {
13360 local->numparam.outer = local->numparam.current;
13361 }
13362 local->numparam.inner = 0;
13363 local->numparam.current = 0;
13364 return inner;
13365#else
13366 return 0;
13367#endif
13368}
13369
13370static void
13371numparam_pop(struct parser_params *p, NODE *prev_inner)
13372{
13373#ifndef RIPPER
13374 struct local_vars *local = p->lvtbl;
13375 if (prev_inner) {
13376 /* prefer first one */
13377 local->numparam.inner = prev_inner;
13378 }
13379 else if (local->numparam.current) {
13380 /* current and inner are exclusive */
13381 local->numparam.inner = local->numparam.current;
13382 }
13383 if (p->max_numparam > NO_PARAM) {
13384 /* current and outer are exclusive */
13385 local->numparam.current = local->numparam.outer;
13386 local->numparam.outer = 0;
13387 }
13388 else {
13389 /* no numbered parameter */
13390 local->numparam.current = 0;
13391 }
13392#endif
13393}
13394
13395static const struct vtable *
13396dyna_push(struct parser_params *p)
13397{
13398 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
13399 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
13400 if (p->lvtbl->used) {
13401 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
13402 }
13403 return p->lvtbl->args;
13404}
13405
13406static void
13407dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
13408{
13409 struct vtable *tmp = *vtblp;
13410 *vtblp = tmp->prev;
13411# if WARN_PAST_SCOPE
13412 if (p->past_scope_enabled) {
13413 tmp->prev = p->lvtbl->past;
13414 p->lvtbl->past = tmp;
13415 return;
13416 }
13417# endif
13418 vtable_free(tmp);
13419}
13420
13421static void
13422dyna_pop_1(struct parser_params *p)
13423{
13424 struct vtable *tmp;
13425
13426 if ((tmp = p->lvtbl->used) != 0) {
13427 warn_unused_var(p, p->lvtbl);
13428 p->lvtbl->used = p->lvtbl->used->prev;
13429 vtable_free(tmp);
13430 }
13431 dyna_pop_vtable(p, &p->lvtbl->args);
13432 dyna_pop_vtable(p, &p->lvtbl->vars);
13433}
13434
13435static void
13436dyna_pop(struct parser_params *p, const struct vtable *lvargs)
13437{
13438 while (p->lvtbl->args != lvargs) {
13439 dyna_pop_1(p);
13440 if (!p->lvtbl->args) {
13441 struct local_vars *local = p->lvtbl->prev;
13442 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13443 p->lvtbl = local;
13444 }
13445 }
13446 dyna_pop_1(p);
13447}
13448
13449static int
13450dyna_in_block(struct parser_params *p)
13451{
13452 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
13453}
13454
13455static int
13456dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
13457{
13458 struct vtable *vars, *args, *used;
13459 int i;
13460
13461 args = p->lvtbl->args;
13462 vars = p->lvtbl->vars;
13463 used = p->lvtbl->used;
13464
13465 while (!DVARS_TERMINAL_P(vars)) {
13466 if (vtable_included(args, id)) {
13467 return 1;
13468 }
13469 if ((i = vtable_included(vars, id)) != 0) {
13470 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
13471 return 1;
13472 }
13473 args = args->prev;
13474 vars = vars->prev;
13475 if (!vidrefp) used = 0;
13476 if (used) used = used->prev;
13477 }
13478
13479 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
13480 return rb_dvar_defined(id, p->parent_iseq);
13481 }
13482
13483 return 0;
13484}
13485
13486static int
13487dvar_defined(struct parser_params *p, ID id)
13488{
13489 return dvar_defined_ref(p, id, NULL);
13490}
13491
13492static int
13493dvar_curr(struct parser_params *p, ID id)
13494{
13495 return (vtable_included(p->lvtbl->args, id) ||
13496 vtable_included(p->lvtbl->vars, id));
13497}
13498
13499static void
13500reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
13501{
13502 compile_error(p,
13503 "regexp encoding option '%c' differs from source encoding '%s'",
13504 c, rb_enc_name(rb_enc_get(str)));
13505}
13506
13507#ifndef RIPPER
13508int
13509rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13510{
13511 int c = RE_OPTION_ENCODING_IDX(options);
13512
13513 if (c) {
13514 int opt, idx;
13515 rb_char_to_option_kcode(c, &opt, &idx);
13516 if (idx != ENCODING_GET(str) &&
13517 !is_ascii_string(str)) {
13518 goto error;
13519 }
13520 ENCODING_SET(str, idx);
13521 }
13522 else if (RE_OPTION_ENCODING_NONE(options)) {
13523 if (!ENCODING_IS_ASCII8BIT(str) &&
13524 !is_ascii_string(str)) {
13525 c = 'n';
13526 goto error;
13527 }
13528 rb_enc_associate(str, rb_ascii8bit_encoding());
13529 }
13530 else if (rb_is_usascii_enc(p->enc)) {
13531 if (!is_ascii_string(str)) {
13532 /* raise in re.c */
13533 rb_enc_associate(str, rb_usascii_encoding());
13534 }
13535 else {
13536 rb_enc_associate(str, rb_ascii8bit_encoding());
13537 }
13538 }
13539 return 0;
13540
13541 error:
13542 return c;
13543}
13544
13545static void
13546reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13547{
13548 int c = rb_reg_fragment_setenc(p, str, options);
13549 if (c) reg_fragment_enc_error(p, str, c);
13550}
13551
13552static int
13553reg_fragment_check(struct parser_params* p, VALUE str, int options)
13554{
13555 VALUE err;
13556 reg_fragment_setenc(p, str, options);
13557 err = rb_reg_check_preprocess(str);
13558 if (err != Qnil) {
13559 err = rb_obj_as_string(err);
13560 compile_error(p, "%"PRIsVALUE, err);
13561 return 0;
13562 }
13563 return 1;
13564}
13565
13566typedef struct {
13567 struct parser_params* parser;
13568 rb_encoding *enc;
13569 NODE *succ_block;
13570 const YYLTYPE *loc;
13571} reg_named_capture_assign_t;
13572
13573static int
13574reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
13575 int back_num, int *back_refs, OnigRegex regex, void *arg0)
13576{
13577 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
13578 struct parser_params* p = arg->parser;
13579 rb_encoding *enc = arg->enc;
13580 long len = name_end - name;
13581 const char *s = (const char *)name;
13582 ID var;
13583 NODE *node, *succ;
13584
13585 if (!len) return ST_CONTINUE;
13586 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13587 return ST_CONTINUE;
13588
13589 var = intern_cstr(s, len, enc);
13590 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13591 if (!lvar_defined(p, var)) return ST_CONTINUE;
13592 }
13593 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13594 succ = arg->succ_block;
13595 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13596 succ = block_append(p, succ, node);
13597 arg->succ_block = succ;
13598 return ST_CONTINUE;
13599}
13600
13601static NODE *
13602reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13603{
13604 reg_named_capture_assign_t arg;
13605
13606 arg.parser = p;
13607 arg.enc = rb_enc_get(regexp);
13608 arg.succ_block = 0;
13609 arg.loc = loc;
13610 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13611
13612 if (!arg.succ_block) return 0;
13613 return arg.succ_block->nd_next;
13614}
13615
13616static VALUE
13617parser_reg_compile(struct parser_params* p, VALUE str, int options)
13618{
13619 reg_fragment_setenc(p, str, options);
13620 return rb_parser_reg_compile(p, str, options);
13621}
13622
13623VALUE
13624rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13625{
13626 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13627}
13628
13629static VALUE
13630reg_compile(struct parser_params* p, VALUE str, int options)
13631{
13632 VALUE re;
13633 VALUE err;
13634
13635 err = rb_errinfo();
13636 re = parser_reg_compile(p, str, options);
13637 if (NIL_P(re)) {
13638 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13639 rb_set_errinfo(err);
13640 compile_error(p, "%"PRIsVALUE, m);
13641 return Qnil;
13642 }
13643 return re;
13644}
13645#else
13646static VALUE
13647parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13648{
13649 VALUE err = rb_errinfo();
13650 VALUE re;
13651 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13652 int c = rb_reg_fragment_setenc(p, str, options);
13653 if (c) reg_fragment_enc_error(p, str, c);
13654 re = rb_parser_reg_compile(p, str, options);
13655 if (NIL_P(re)) {
13656 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13657 rb_set_errinfo(err);
13658 }
13659 return re;
13660}
13661#endif
13662
13663#ifndef RIPPER
13664void
13665rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13666{
13667 struct parser_params *p;
13668 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13669 p->do_print = print;
13670 p->do_loop = loop;
13671 p->do_chomp = chomp;
13672 p->do_split = split;
13673}
13674
13675static NODE *
13676parser_append_options(struct parser_params *p, NODE *node)
13677{
13678 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13679 const YYLTYPE *const LOC = &default_location;
13680
13681 if (p->do_print) {
13682 NODE *print = NEW_FCALL(rb_intern("print"),
13683 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13684 LOC);
13685 node = block_append(p, node, print);
13686 }
13687
13688 if (p->do_loop) {
13689 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
13690
13691 if (p->do_split) {
13692 ID ifs = rb_intern("$;");
13693 ID fields = rb_intern("$F");
13694 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13695 NODE *split = NEW_GASGN(fields,
13696 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13697 rb_intern("split"), args, LOC),
13698 LOC);
13699 node = block_append(p, split, node);
13700 }
13701 if (p->do_chomp) {
13702 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
13703 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
13704 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
13705 }
13706
13707 node = NEW_WHILE(NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
13708 }
13709
13710 return node;
13711}
13712
13713void
13714rb_init_parse(void)
13715{
13716 /* just to suppress unused-function warnings */
13717 (void)nodetype;
13718 (void)nodeline;
13719}
13720
13721static ID
13722internal_id(struct parser_params *p)
13723{
13724 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13725}
13726#endif /* !RIPPER */
13727
13728static void
13729parser_initialize(struct parser_params *p)
13730{
13731 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13732 p->command_start = TRUE;
13733 p->ruby_sourcefile_string = Qnil;
13734 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13735 p->node_id = 0;
13736 p->delayed.token = Qnil;
13737#ifdef RIPPER
13738 p->result = Qnil;
13739 p->parsing_thread = Qnil;
13740#else
13741 p->error_buffer = Qfalse;
13742 p->end_expect_token_locations = Qnil;
13743 p->token_id = 0;
13744 p->tokens = Qnil;
13745#endif
13746 p->debug_buffer = Qnil;
13747 p->debug_output = rb_ractor_stdout();
13748 p->enc = rb_utf8_encoding();
13749}
13750
13751#ifdef RIPPER
13752#define parser_mark ripper_parser_mark
13753#define parser_free ripper_parser_free
13754#endif
13755
13756static void
13757parser_mark(void *ptr)
13758{
13759 struct parser_params *p = (struct parser_params*)ptr;
13760
13761 rb_gc_mark(p->lex.input);
13762 rb_gc_mark(p->lex.lastline);
13763 rb_gc_mark(p->lex.nextline);
13764 rb_gc_mark(p->ruby_sourcefile_string);
13765 rb_gc_mark((VALUE)p->lex.strterm);
13766 rb_gc_mark((VALUE)p->ast);
13767 rb_gc_mark(p->case_labels);
13768 rb_gc_mark(p->delayed.token);
13769#ifndef RIPPER
13770 rb_gc_mark(p->debug_lines);
13771 rb_gc_mark(p->compile_option);
13772 rb_gc_mark(p->error_buffer);
13773 rb_gc_mark(p->end_expect_token_locations);
13774 rb_gc_mark(p->tokens);
13775#else
13776 rb_gc_mark(p->value);
13777 rb_gc_mark(p->result);
13778 rb_gc_mark(p->parsing_thread);
13779#endif
13780 rb_gc_mark(p->debug_buffer);
13781 rb_gc_mark(p->debug_output);
13782#ifdef YYMALLOC
13783 rb_gc_mark((VALUE)p->heap);
13784#endif
13785}
13786
13787static void
13788parser_free(void *ptr)
13789{
13790 struct parser_params *p = (struct parser_params*)ptr;
13791 struct local_vars *local, *prev;
13792
13793 if (p->tokenbuf) {
13794 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13795 }
13796
13797 for (local = p->lvtbl; local; local = prev) {
13798 prev = local->prev;
13799 local_free(p, local);
13800 }
13801
13802 {
13803 token_info *ptinfo;
13804 while ((ptinfo = p->token_info) != 0) {
13805 p->token_info = ptinfo->next;
13806 xfree(ptinfo);
13807 }
13808 }
13809 xfree(ptr);
13810}
13811
13812static size_t
13813parser_memsize(const void *ptr)
13814{
13815 struct parser_params *p = (struct parser_params*)ptr;
13816 struct local_vars *local;
13817 size_t size = sizeof(*p);
13818
13819 size += p->toksiz;
13820 for (local = p->lvtbl; local; local = local->prev) {
13821 size += sizeof(*local);
13822 if (local->vars) size += local->vars->capa * sizeof(ID);
13823 }
13824 return size;
13825}
13826
13827static const rb_data_type_t parser_data_type = {
13828#ifndef RIPPER
13829 "parser",
13830#else
13831 "ripper",
13832#endif
13833 {
13834 parser_mark,
13835 parser_free,
13836 parser_memsize,
13837 },
13838 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13839};
13840
13841#ifndef RIPPER
13842#undef rb_reserved_word
13843
13844const struct kwtable *
13845rb_reserved_word(const char *str, unsigned int len)
13846{
13847 return reserved_word(str, len);
13848}
13849
13850VALUE
13851rb_parser_new(void)
13852{
13853 struct parser_params *p;
13854 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13855 &parser_data_type, p);
13856 parser_initialize(p);
13857 return parser;
13858}
13859
13860VALUE
13861rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13862{
13863 struct parser_params *p;
13864
13865 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13866 p->error_buffer = main ? Qfalse : Qnil;
13867 p->parent_iseq = base;
13868 return vparser;
13869}
13870
13871void
13872rb_parser_keep_script_lines(VALUE vparser)
13873{
13874 struct parser_params *p;
13875
13876 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13877 p->keep_script_lines = 1;
13878}
13879
13880void
13881rb_parser_error_tolerant(VALUE vparser)
13882{
13883 struct parser_params *p;
13884
13885 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13886 p->error_tolerant = 1;
13887 p->end_expect_token_locations = rb_ary_new();
13888}
13889
13890void
13891rb_parser_keep_tokens(VALUE vparser)
13892{
13893 struct parser_params *p;
13894
13895 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13896 p->keep_tokens = 1;
13897 p->tokens = rb_ary_new();
13898}
13899
13900#endif
13901
13902#ifdef RIPPER
13903#define rb_parser_end_seen_p ripper_parser_end_seen_p
13904#define rb_parser_encoding ripper_parser_encoding
13905#define rb_parser_get_yydebug ripper_parser_get_yydebug
13906#define rb_parser_set_yydebug ripper_parser_set_yydebug
13907#define rb_parser_get_debug_output ripper_parser_get_debug_output
13908#define rb_parser_set_debug_output ripper_parser_set_debug_output
13909static VALUE ripper_parser_end_seen_p(VALUE vparser);
13910static VALUE ripper_parser_encoding(VALUE vparser);
13911static VALUE ripper_parser_get_yydebug(VALUE self);
13912static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13913static VALUE ripper_parser_get_debug_output(VALUE self);
13914static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13915
13916/*
13917 * call-seq:
13918 * ripper.error? -> Boolean
13919 *
13920 * Return true if parsed source has errors.
13921 */
13922static VALUE
13923ripper_error_p(VALUE vparser)
13924{
13925 struct parser_params *p;
13926
13927 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13928 return RBOOL(p->error_p);
13929}
13930#endif
13931
13932/*
13933 * call-seq:
13934 * ripper.end_seen? -> Boolean
13935 *
13936 * Return true if parsed source ended by +\_\_END\_\_+.
13937 */
13938VALUE
13939rb_parser_end_seen_p(VALUE vparser)
13940{
13941 struct parser_params *p;
13942
13943 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13944 return RBOOL(p->ruby__end__seen);
13945}
13946
13947/*
13948 * call-seq:
13949 * ripper.encoding -> encoding
13950 *
13951 * Return encoding of the source.
13952 */
13953VALUE
13954rb_parser_encoding(VALUE vparser)
13955{
13956 struct parser_params *p;
13957
13958 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13959 return rb_enc_from_encoding(p->enc);
13960}
13961
13962#ifdef RIPPER
13963/*
13964 * call-seq:
13965 * ripper.yydebug -> true or false
13966 *
13967 * Get yydebug.
13968 */
13969VALUE
13970rb_parser_get_yydebug(VALUE self)
13971{
13972 struct parser_params *p;
13973
13974 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13975 return RBOOL(p->debug);
13976}
13977#endif
13978
13979/*
13980 * call-seq:
13981 * ripper.yydebug = flag
13982 *
13983 * Set yydebug.
13984 */
13985VALUE
13986rb_parser_set_yydebug(VALUE self, VALUE flag)
13987{
13988 struct parser_params *p;
13989
13990 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13991 p->debug = RTEST(flag);
13992 return flag;
13993}
13994
13995/*
13996 * call-seq:
13997 * ripper.debug_output -> obj
13998 *
13999 * Get debug output.
14000 */
14001VALUE
14002rb_parser_get_debug_output(VALUE self)
14003{
14004 struct parser_params *p;
14005
14006 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14007 return p->debug_output;
14008}
14009
14010/*
14011 * call-seq:
14012 * ripper.debug_output = obj
14013 *
14014 * Set debug output.
14015 */
14016VALUE
14017rb_parser_set_debug_output(VALUE self, VALUE output)
14018{
14019 struct parser_params *p;
14020
14021 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14022 return p->debug_output = output;
14023}
14024
14025#ifndef RIPPER
14026#ifdef YYMALLOC
14027#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
14028/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
14029 * potential memory leak */
14030#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
14031#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
14032 (new)->cnt = (cnt), (ptr))
14033
14034void *
14035rb_parser_malloc(struct parser_params *p, size_t size)
14036{
14037 size_t cnt = HEAPCNT(1, size);
14038 rb_imemo_tmpbuf_t *n = NEWHEAP();
14039 void *ptr = xmalloc(size);
14040
14041 return ADD2HEAP(n, cnt, ptr);
14042}
14043
14044void *
14045rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
14046{
14047 size_t cnt = HEAPCNT(nelem, size);
14048 rb_imemo_tmpbuf_t *n = NEWHEAP();
14049 void *ptr = xcalloc(nelem, size);
14050
14051 return ADD2HEAP(n, cnt, ptr);
14052}
14053
14054void *
14055rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
14056{
14057 rb_imemo_tmpbuf_t *n;
14058 size_t cnt = HEAPCNT(1, size);
14059
14060 if (ptr && (n = p->heap) != NULL) {
14061 do {
14062 if (n->ptr == ptr) {
14063 n->ptr = ptr = xrealloc(ptr, size);
14064 if (n->cnt) n->cnt = cnt;
14065 return ptr;
14066 }
14067 } while ((n = n->next) != NULL);
14068 }
14069 n = NEWHEAP();
14070 ptr = xrealloc(ptr, size);
14071 return ADD2HEAP(n, cnt, ptr);
14072}
14073
14074void
14075rb_parser_free(struct parser_params *p, void *ptr)
14076{
14077 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
14078
14079 while ((n = *prev) != NULL) {
14080 if (n->ptr == ptr) {
14081 *prev = n->next;
14082 break;
14083 }
14084 prev = &n->next;
14085 }
14086}
14087#endif
14088
14089void
14090rb_parser_printf(struct parser_params *p, const char *fmt, ...)
14091{
14092 va_list ap;
14093 VALUE mesg = p->debug_buffer;
14094
14095 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
14096 va_start(ap, fmt);
14097 rb_str_vcatf(mesg, fmt, ap);
14098 va_end(ap);
14099 if (RSTRING_END(mesg)[-1] == '\n') {
14100 rb_io_write(p->debug_output, mesg);
14101 p->debug_buffer = Qnil;
14102 }
14103}
14104
14105static void
14106parser_compile_error(struct parser_params *p, const char *fmt, ...)
14107{
14108 va_list ap;
14109
14110 rb_io_flush(p->debug_output);
14111 p->error_p = 1;
14112 va_start(ap, fmt);
14113 p->error_buffer =
14114 rb_syntax_error_append(p->error_buffer,
14115 p->ruby_sourcefile_string,
14116 p->ruby_sourceline,
14117 rb_long2int(p->lex.pcur - p->lex.pbeg),
14118 p->enc, fmt, ap);
14119 va_end(ap);
14120}
14121
14122static size_t
14123count_char(const char *str, int c)
14124{
14125 int n = 0;
14126 while (str[n] == c) ++n;
14127 return n;
14128}
14129
14130/*
14131 * strip enclosing double-quotes, same as the default yytnamerr except
14132 * for that single-quotes matching back-quotes do not stop stripping.
14133 *
14134 * "\"`class' keyword\"" => "`class' keyword"
14135 */
14136RUBY_FUNC_EXPORTED size_t
14137rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
14138{
14139 if (*yystr == '"') {
14140 size_t yyn = 0, bquote = 0;
14141 const char *yyp = yystr;
14142
14143 while (*++yyp) {
14144 switch (*yyp) {
14145 case '`':
14146 if (!bquote) {
14147 bquote = count_char(yyp+1, '`') + 1;
14148 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
14149 yyn += bquote;
14150 yyp += bquote - 1;
14151 break;
14152 }
14153 goto default_char;
14154
14155 case '\'':
14156 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
14157 if (yyres) memcpy(yyres + yyn, yyp, bquote);
14158 yyn += bquote;
14159 yyp += bquote - 1;
14160 bquote = 0;
14161 break;
14162 }
14163 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
14164 if (yyres) memcpy(yyres + yyn, yyp, 3);
14165 yyn += 3;
14166 yyp += 2;
14167 break;
14168 }
14169 goto do_not_strip_quotes;
14170
14171 case ',':
14172 goto do_not_strip_quotes;
14173
14174 case '\\':
14175 if (*++yyp != '\\')
14176 goto do_not_strip_quotes;
14177 /* Fall through. */
14178 default_char:
14179 default:
14180 if (yyres)
14181 yyres[yyn] = *yyp;
14182 yyn++;
14183 break;
14184
14185 case '"':
14186 case '\0':
14187 if (yyres)
14188 yyres[yyn] = '\0';
14189 return yyn;
14190 }
14191 }
14192 do_not_strip_quotes: ;
14193 }
14194
14195 if (!yyres) return strlen(yystr);
14196
14197 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
14198}
14199#endif
14200
14201#ifdef RIPPER
14202#ifdef RIPPER_DEBUG
14203/* :nodoc: */
14204static VALUE
14205ripper_validate_object(VALUE self, VALUE x)
14206{
14207 if (x == Qfalse) return x;
14208 if (x == Qtrue) return x;
14209 if (NIL_P(x)) return x;
14210 if (UNDEF_P(x))
14211 rb_raise(rb_eArgError, "Qundef given");
14212 if (FIXNUM_P(x)) return x;
14213 if (SYMBOL_P(x)) return x;
14214 switch (BUILTIN_TYPE(x)) {
14215 case T_STRING:
14216 case T_OBJECT:
14217 case T_ARRAY:
14218 case T_BIGNUM:
14219 case T_FLOAT:
14220 case T_COMPLEX:
14221 case T_RATIONAL:
14222 break;
14223 case T_NODE:
14224 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
14225 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
14226 }
14227 x = ((NODE *)x)->nd_rval;
14228 break;
14229 default:
14230 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
14231 (void *)x, rb_obj_classname(x));
14232 }
14233 if (!RBASIC_CLASS(x)) {
14234 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
14235 (void *)x, rb_builtin_type_name(TYPE(x)));
14236 }
14237 return x;
14238}
14239#endif
14240
14241#define validate(x) ((x) = get_value(x))
14242
14243static VALUE
14244ripper_dispatch0(struct parser_params *p, ID mid)
14245{
14246 return rb_funcall(p->value, mid, 0);
14247}
14248
14249static VALUE
14250ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
14251{
14252 validate(a);
14253 return rb_funcall(p->value, mid, 1, a);
14254}
14255
14256static VALUE
14257ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
14258{
14259 validate(a);
14260 validate(b);
14261 return rb_funcall(p->value, mid, 2, a, b);
14262}
14263
14264static VALUE
14265ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
14266{
14267 validate(a);
14268 validate(b);
14269 validate(c);
14270 return rb_funcall(p->value, mid, 3, a, b, c);
14271}
14272
14273static VALUE
14274ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
14275{
14276 validate(a);
14277 validate(b);
14278 validate(c);
14279 validate(d);
14280 return rb_funcall(p->value, mid, 4, a, b, c, d);
14281}
14282
14283static VALUE
14284ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
14285{
14286 validate(a);
14287 validate(b);
14288 validate(c);
14289 validate(d);
14290 validate(e);
14291 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
14292}
14293
14294static VALUE
14295ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
14296{
14297 validate(a);
14298 validate(b);
14299 validate(c);
14300 validate(d);
14301 validate(e);
14302 validate(f);
14303 validate(g);
14304 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
14305}
14306
14307static ID
14308ripper_get_id(VALUE v)
14309{
14310 NODE *nd;
14311 if (!RB_TYPE_P(v, T_NODE)) return 0;
14312 nd = (NODE *)v;
14313 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
14314 return nd->nd_vid;
14315}
14316
14317static VALUE
14318ripper_get_value(VALUE v)
14319{
14320 NODE *nd;
14321 if (UNDEF_P(v)) return Qnil;
14322 if (!RB_TYPE_P(v, T_NODE)) return v;
14323 nd = (NODE *)v;
14324 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
14325 return nd->nd_rval;
14326}
14327
14328static void
14329ripper_error(struct parser_params *p)
14330{
14331 p->error_p = TRUE;
14332}
14333
14334static void
14335ripper_compile_error(struct parser_params *p, const char *fmt, ...)
14336{
14337 VALUE str;
14338 va_list args;
14339
14340 va_start(args, fmt);
14341 str = rb_vsprintf(fmt, args);
14342 va_end(args);
14343 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
14344 ripper_error(p);
14345}
14346
14347static VALUE
14348ripper_lex_get_generic(struct parser_params *p, VALUE src)
14349{
14350 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
14351 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
14352 rb_raise(rb_eTypeError,
14353 "gets returned %"PRIsVALUE" (expected String or nil)",
14354 rb_obj_class(line));
14355 }
14356 return line;
14357}
14358
14359static VALUE
14360ripper_lex_io_get(struct parser_params *p, VALUE src)
14361{
14362 return rb_io_gets(src);
14363}
14364
14365static VALUE
14366ripper_s_allocate(VALUE klass)
14367{
14368 struct parser_params *p;
14369 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
14370 &parser_data_type, p);
14371 p->value = self;
14372 return self;
14373}
14374
14375#define ripper_initialized_p(r) ((r)->lex.input != 0)
14376
14377/*
14378 * call-seq:
14379 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
14380 *
14381 * Create a new Ripper object.
14382 * _src_ must be a String, an IO, or an Object which has #gets method.
14383 *
14384 * This method does not starts parsing.
14385 * See also Ripper#parse and Ripper.parse.
14386 */
14387static VALUE
14388ripper_initialize(int argc, VALUE *argv, VALUE self)
14389{
14390 struct parser_params *p;
14391 VALUE src, fname, lineno;
14392
14393 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14394 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
14395 if (RB_TYPE_P(src, T_FILE)) {
14396 p->lex.gets = ripper_lex_io_get;
14397 }
14398 else if (rb_respond_to(src, id_gets)) {
14399 p->lex.gets = ripper_lex_get_generic;
14400 }
14401 else {
14402 StringValue(src);
14403 p->lex.gets = lex_get_str;
14404 }
14405 p->lex.input = src;
14406 p->eofp = 0;
14407 if (NIL_P(fname)) {
14408 fname = STR_NEW2("(ripper)");
14409 OBJ_FREEZE(fname);
14410 }
14411 else {
14412 StringValueCStr(fname);
14413 fname = rb_str_new_frozen(fname);
14414 }
14415 parser_initialize(p);
14416
14417 p->ruby_sourcefile_string = fname;
14418 p->ruby_sourcefile = RSTRING_PTR(fname);
14419 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
14420
14421 return Qnil;
14422}
14423
14424static VALUE
14425ripper_parse0(VALUE parser_v)
14426{
14427 struct parser_params *p;
14428
14429 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14430 parser_prepare(p);
14431 p->ast = rb_ast_new();
14432 ripper_yyparse((void*)p);
14433 rb_ast_dispose(p->ast);
14434 p->ast = 0;
14435 return p->result;
14436}
14437
14438static VALUE
14439ripper_ensure(VALUE parser_v)
14440{
14441 struct parser_params *p;
14442
14443 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14444 p->parsing_thread = Qnil;
14445 return Qnil;
14446}
14447
14448/*
14449 * call-seq:
14450 * ripper.parse
14451 *
14452 * Start parsing and returns the value of the root action.
14453 */
14454static VALUE
14455ripper_parse(VALUE self)
14456{
14457 struct parser_params *p;
14458
14459 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14460 if (!ripper_initialized_p(p)) {
14461 rb_raise(rb_eArgError, "method called for uninitialized object");
14462 }
14463 if (!NIL_P(p->parsing_thread)) {
14464 if (p->parsing_thread == rb_thread_current())
14465 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
14466 else
14467 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
14468 }
14469 p->parsing_thread = rb_thread_current();
14470 rb_ensure(ripper_parse0, self, ripper_ensure, self);
14471
14472 return p->result;
14473}
14474
14475/*
14476 * call-seq:
14477 * ripper.column -> Integer
14478 *
14479 * Return column number of current parsing line.
14480 * This number starts from 0.
14481 */
14482static VALUE
14483ripper_column(VALUE self)
14484{
14485 struct parser_params *p;
14486 long col;
14487
14488 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14489 if (!ripper_initialized_p(p)) {
14490 rb_raise(rb_eArgError, "method called for uninitialized object");
14491 }
14492 if (NIL_P(p->parsing_thread)) return Qnil;
14493 col = p->lex.ptok - p->lex.pbeg;
14494 return LONG2NUM(col);
14495}
14496
14497/*
14498 * call-seq:
14499 * ripper.filename -> String
14500 *
14501 * Return current parsing filename.
14502 */
14503static VALUE
14504ripper_filename(VALUE self)
14505{
14506 struct parser_params *p;
14507
14508 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14509 if (!ripper_initialized_p(p)) {
14510 rb_raise(rb_eArgError, "method called for uninitialized object");
14511 }
14512 return p->ruby_sourcefile_string;
14513}
14514
14515/*
14516 * call-seq:
14517 * ripper.lineno -> Integer
14518 *
14519 * Return line number of current parsing line.
14520 * This number starts from 1.
14521 */
14522static VALUE
14523ripper_lineno(VALUE self)
14524{
14525 struct parser_params *p;
14526
14527 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14528 if (!ripper_initialized_p(p)) {
14529 rb_raise(rb_eArgError, "method called for uninitialized object");
14530 }
14531 if (NIL_P(p->parsing_thread)) return Qnil;
14532 return INT2NUM(p->ruby_sourceline);
14533}
14534
14535/*
14536 * call-seq:
14537 * ripper.state -> Integer
14538 *
14539 * Return scanner state of current token.
14540 */
14541static VALUE
14542ripper_state(VALUE self)
14543{
14544 struct parser_params *p;
14545
14546 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14547 if (!ripper_initialized_p(p)) {
14548 rb_raise(rb_eArgError, "method called for uninitialized object");
14549 }
14550 if (NIL_P(p->parsing_thread)) return Qnil;
14551 return INT2NUM(p->lex.state);
14552}
14553
14554/*
14555 * call-seq:
14556 * ripper.token -> String
14557 *
14558 * Return the current token string.
14559 */
14560static VALUE
14561ripper_token(VALUE self)
14562{
14563 struct parser_params *p;
14564 long pos, len;
14565
14566 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14567 if (!ripper_initialized_p(p)) {
14568 rb_raise(rb_eArgError, "method called for uninitialized object");
14569 }
14570 if (NIL_P(p->parsing_thread)) return Qnil;
14571 pos = p->lex.ptok - p->lex.pbeg;
14572 len = p->lex.pcur - p->lex.ptok;
14573 return rb_str_subseq(p->lex.lastline, pos, len);
14574}
14575
14576#ifdef RIPPER_DEBUG
14577/* :nodoc: */
14578static VALUE
14579ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
14580{
14581 StringValue(msg);
14582 if (UNDEF_P(obj)) {
14583 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
14584 }
14585 return Qnil;
14586}
14587
14588/* :nodoc: */
14589static VALUE
14590ripper_value(VALUE self, VALUE obj)
14591{
14592 return ULONG2NUM(obj);
14593}
14594#endif
14595
14596/*
14597 * call-seq:
14598 * Ripper.lex_state_name(integer) -> string
14599 *
14600 * Returns a string representation of lex_state.
14601 */
14602static VALUE
14603ripper_lex_state_name(VALUE self, VALUE state)
14604{
14605 return rb_parser_lex_state_name(NUM2INT(state));
14606}
14607
14608void
14609Init_ripper(void)
14610{
14611 ripper_init_eventids1();
14612 ripper_init_eventids2();
14613 id_warn = rb_intern_const("warn");
14614 id_warning = rb_intern_const("warning");
14615 id_gets = rb_intern_const("gets");
14616 id_assoc = rb_intern_const("=>");
14617
14618 (void)yystpcpy; /* may not used in newer bison */
14619
14620 InitVM(ripper);
14621}
14622
14623void
14624InitVM_ripper(void)
14625{
14626 VALUE Ripper;
14627
14628 Ripper = rb_define_class("Ripper", rb_cObject);
14629 /* version of Ripper */
14630 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14631 rb_define_alloc_func(Ripper, ripper_s_allocate);
14632 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14633 rb_define_method(Ripper, "parse", ripper_parse, 0);
14634 rb_define_method(Ripper, "column", ripper_column, 0);
14635 rb_define_method(Ripper, "filename", ripper_filename, 0);
14636 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14637 rb_define_method(Ripper, "state", ripper_state, 0);
14638 rb_define_method(Ripper, "token", ripper_token, 0);
14639 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14640 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14641 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14642 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14643 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14644 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14645 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14646#ifdef RIPPER_DEBUG
14647 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14648 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14649 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14650#endif
14651
14652 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14653 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14654
14655 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14656
14657<% @exprs.each do |expr, desc| -%>
14658 /* <%=desc%> */
14659 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14660<% end %>
14661 ripper_init_eventids1_table(Ripper);
14662 ripper_init_eventids2_table(Ripper);
14663
14664# if 0
14665 /* Hack to let RDoc document SCRIPT_LINES__ */
14666
14667 /*
14668 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14669 * after the assignment will be added as an Array of lines with the file
14670 * name as the key.
14671 */
14672 rb_define_global_const("SCRIPT_LINES__", Qnil);
14673#endif
14674
14675}
14676#endif /* RIPPER */
14677
14678/*
14679 * Local variables:
14680 * mode: c
14681 * c-file-style: "ruby"
14682 * End:
14683 */