Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

parse.y
Go to the documentation of this file.
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 %{
13
14 #if !YYPURE
15 # error needs pure parser
16 #endif
17 #define YYDEBUG 1
18 #define YYERROR_VERBOSE 1
19 #define YYSTACK_USE_ALLOCA 0
20 #define YYLTYPE rb_code_location_t
21 #define YYLTYPE_IS_DECLARED 1
22
23 #include "ruby/ruby.h"
24 #include "ruby/st.h"
25 #include "ruby/encoding.h"
26 #include "internal.h"
27 #include "node.h"
28 #include "parse.h"
29 #include "symbol.h"
30 #include "regenc.h"
31 #include <stdio.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include "probes.h"
35
36 #ifndef WARN_PAST_SCOPE
37 # define WARN_PAST_SCOPE 0
38 #endif
39
40 #define TAB_WIDTH 8
41
42 #define yydebug (p->debug) /* disable the global variable definition */
43
44 #define YYMALLOC(size) rb_parser_malloc(p, (size))
45 #define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
46 #define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
47 #define YYFREE(ptr) rb_parser_free(p, (ptr))
48 #define YYFPRINTF rb_parser_printf
49 #define YYPRINT(out, tok, val) parser_token_value_print(p, (tok), &(val))
50 #define YY_LOCATION_PRINT(File, loc) \
51 rb_parser_printf(p, "%d.%d-%d.%d", \
52 (loc).beg_pos.lineno, (loc).beg_pos.column,\
53 (loc).end_pos.lineno, (loc).end_pos.column)
54 #define YYLLOC_DEFAULT(Current, Rhs, N) \
55 do \
56 if (N) \
57 { \
58 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
59 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
60 } \
61 else \
62 { \
63 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
64 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
65 } \
66 while (0)
67
68 #define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
69 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
70 #define RUBY_SET_YYLLOC_OF_NONE(Current) \
71 rb_parser_set_location_of_none(p, &(Current))
72 #define RUBY_SET_YYLLOC(Current) \
73 rb_parser_set_location(p, &(Current))
74 #define RUBY_INIT_YYLLOC() \
75 { \
76 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
77 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
78 }
79
80 enum lex_state_bits {
81 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
82 EXPR_END_bit, /* newline significant, +/- is an operator. */
83 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
84 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
85 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
86 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
87 EXPR_MID_bit, /* newline significant, +/- is an operator. */
88 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
89 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
90 EXPR_CLASS_bit, /* immediate after `class', no here document. */
91 EXPR_LABEL_bit, /* flag bit, label is allowed. */
92 EXPR_LABELED_bit, /* flag bit, just after a label. */
93 EXPR_FITEM_bit, /* symbol literal as FNAME. */
94 EXPR_MAX_STATE
95 };
96 /* examine combinations */
97 enum lex_state_e {
98 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
99 DEF_EXPR(BEG),
100 DEF_EXPR(END),
101 DEF_EXPR(ENDARG),
102 DEF_EXPR(ENDFN),
103 DEF_EXPR(ARG),
104 DEF_EXPR(CMDARG),
105 DEF_EXPR(MID),
106 DEF_EXPR(FNAME),
107 DEF_EXPR(DOT),
108 DEF_EXPR(CLASS),
109 DEF_EXPR(LABEL),
110 DEF_EXPR(LABELED),
111 DEF_EXPR(FITEM),
112 EXPR_VALUE = EXPR_BEG,
113 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
114 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
115 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
116 EXPR_NONE = 0
117 };
118 #define IS_lex_state_for(x, ls) ((x) & (ls))
119 #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
120 #define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
121 #define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
122
123 # define SET_LEX_STATE(ls) \
124 (p->lex.state = \
125 (p->debug ? \
126 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
127 (enum lex_state_e)(ls)))
128
129 typedef VALUE stack_type;
130
131 static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
132
133 # define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
134 # define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
135 # define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
136 # define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
137 # define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
138
139 /* A flag to identify keyword_do_cond, "do" keyword after condition expression.
140 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
141 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
142 #define COND_POP() BITSTACK_POP(cond_stack)
143 #define COND_P() BITSTACK_SET_P(cond_stack)
144 #define COND_SET(n) BITSTACK_SET(cond_stack, (n))
145
146 /* A flag to identify keyword_do_block; "do" keyword after command_call.
147 Example: `foo 1, 2 do`. */
148 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
149 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
150 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
151 #define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
152
153 struct vtable {
154 ID *tbl;
155 int pos;
156 int capa;
157 struct vtable *prev;
158 };
159
160 struct local_vars {
161 struct vtable *args;
162 struct vtable *vars;
163 struct vtable *used;
164 # if WARN_PAST_SCOPE
165 struct vtable *past;
166 # endif
167 struct local_vars *prev;
168 # ifndef RIPPER
169 struct {
170 NODE *outer, *inner, *current;
171 } numparam;
172 # endif
173 };
174
175 enum {
176 ORDINAL_PARAM = -1,
177 NO_PARAM = 0,
178 NUMPARAM_MAX = 9,
179 };
180
181 #define NUMPARAM_ID_P(id) numparam_id_p(id)
182 #define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
183 #define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
184 static int
185 numparam_id_p(ID id)
186 {
187 if (!is_local_id(id)) return 0;
188 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
189 return idx > 0 && idx <= NUMPARAM_MAX;
190 }
191 static void numparam_name(struct parser_params *p, ID id);
192
193 #define DVARS_INHERIT ((void*)1)
194 #define DVARS_TOPSCOPE NULL
195 #define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
196
197 typedef struct token_info {
198 const char *token;
199 rb_code_position_t beg;
200 int indent;
201 int nonspc;
202 struct token_info *next;
203 } token_info;
204
205 typedef struct rb_strterm_struct rb_strterm_t;
206
207 /*
208 Structure of Lexer Buffer:
209
210 lex.pbeg lex.ptok lex.pcur lex.pend
211 | | | |
212 |------------+------------+------------|
213 |<---------->|
214 token
215 */
216 struct parser_params {
217 rb_imemo_tmpbuf_t *heap;
218
219 YYSTYPE *lval;
220
221 struct {
222 rb_strterm_t *strterm;
223 VALUE (*gets)(struct parser_params*,VALUE);
224 VALUE input;
225 VALUE prevline;
226 VALUE lastline;
227 VALUE nextline;
228 const char *pbeg;
229 const char *pcur;
230 const char *pend;
231 const char *ptok;
232 union {
233 long ptr;
234 VALUE (*call)(VALUE, int);
235 } gets_;
236 enum lex_state_e state;
237 /* track the nest level of any parens "()[]{}" */
238 int paren_nest;
239 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
240 int lpar_beg;
241 /* track the nest level of only braces "{}" */
242 int brace_nest;
243 } lex;
244 stack_type cond_stack;
245 stack_type cmdarg_stack;
246 int tokidx;
247 int toksiz;
248 int tokline;
249 int heredoc_end;
250 int heredoc_indent;
251 int heredoc_line_indent;
252 char *tokenbuf;
253 struct local_vars *lvtbl;
254 st_table *pvtbl;
255 st_table *pktbl;
256 int line_count;
257 int ruby_sourceline; /* current line no. */
258 const char *ruby_sourcefile; /* current source file */
259 VALUE ruby_sourcefile_string;
260 rb_encoding *enc;
261 token_info *token_info;
262 VALUE case_labels;
263 VALUE compile_option;
264
265 VALUE debug_buffer;
266 VALUE debug_output;
267
268 ID cur_arg;
269
270 rb_ast_t *ast;
271 int node_id;
272
273 int max_numparam;
274
275 unsigned int command_start:1;
276 unsigned int eofp: 1;
277 unsigned int ruby__end__seen: 1;
278 unsigned int debug: 1;
279 unsigned int has_shebang: 1;
280 unsigned int in_defined: 1;
281 unsigned int in_kwarg: 1;
282 unsigned int in_def: 1;
283 unsigned int in_class: 1;
284 unsigned int token_seen: 1;
285 unsigned int token_info_enabled: 1;
286 # if WARN_PAST_SCOPE
287 unsigned int past_scope_enabled: 1;
288 # endif
289 unsigned int error_p: 1;
290 unsigned int cr_seen: 1;
291
292 #ifndef RIPPER
293 /* Ruby core only */
294
295 unsigned int do_print: 1;
296 unsigned int do_loop: 1;
297 unsigned int do_chomp: 1;
298 unsigned int do_split: 1;
299 unsigned int warn_location: 1;
300
301 NODE *eval_tree_begin;
302 NODE *eval_tree;
303 VALUE error_buffer;
304 VALUE debug_lines;
305 const struct rb_iseq_struct *parent_iseq;
306 #else
307 /* Ripper only */
308
309 struct {
310 VALUE token;
311 int line;
312 int col;
313 } delayed;
314
315 VALUE value;
316 VALUE result;
317 VALUE parsing_thread;
318 #endif
319 };
320
321 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
322
323 #define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
324 #define STR_NEW0() rb_enc_str_new(0,0,p->enc)
325 #define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
326 #define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
327 #define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
328
329 static st_table *
330 push_pvtbl(struct parser_params *p)
331 {
332 st_table *tbl = p->pvtbl;
333 p->pvtbl = st_init_numtable();
334 return tbl;
335 }
336
337 static void
338 pop_pvtbl(struct parser_params *p, st_table *tbl)
339 {
340 st_free_table(p->pvtbl);
341 p->pvtbl = tbl;
342 }
343
344 static st_table *
345 push_pktbl(struct parser_params *p)
346 {
347 st_table *tbl = p->pktbl;
348 p->pktbl = 0;
349 return tbl;
350 }
351
352 static void
353 pop_pktbl(struct parser_params *p, st_table *tbl)
354 {
355 if (p->pktbl) st_free_table(p->pktbl);
356 p->pktbl = tbl;
357 }
358
359 static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
360 #define yyerror0(msg) parser_yyerror(p, NULL, (msg))
361 #define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
362 #define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
363 #define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
364
365 #ifdef RIPPER
366 #define compile_for_eval (0)
367 #else
368 #define compile_for_eval (p->parent_iseq != 0)
369 #endif
370
371 #define token_column ((int)(p->lex.ptok - p->lex.pbeg))
372
373 #define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
374 #define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
375 #define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
376
377 #define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
378
379 static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
380
381 #ifndef RIPPER
382 static inline void
383 rb_discard_node(struct parser_params *p, NODE *n)
384 {
385 rb_ast_delete_node(p->ast, n);
386 }
387 #endif
388
389 #ifdef RIPPER
390 static inline VALUE
391 add_mark_object(struct parser_params *p, VALUE obj)
392 {
393 if (!SPECIAL_CONST_P(obj)
394 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
395 ) {
396 rb_ast_add_mark_object(p->ast, obj);
397 }
398 return obj;
399 }
400 #else
401 static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
402 #endif
403
404 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
405 #define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
406
407 static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
408
409 static int
410 parser_get_node_id(struct parser_params *p)
411 {
412 int node_id = p->node_id;
413 p->node_id++;
414 return node_id;
415 }
416
417 #ifndef RIPPER
418 static inline void
419 set_line_body(NODE *body, int line)
420 {
421 if (!body) return;
422 switch (nd_type(body)) {
423 case NODE_RESCUE:
424 case NODE_ENSURE:
425 nd_set_line(body, line);
426 }
427 }
428
429 #define yyparse ruby_yyparse
430
431 static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432 static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
433 #define new_nil(loc) NEW_NIL(loc)
434 static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435 static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
436 static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
437
438 static NODE *newline_node(NODE*);
439 static void fixpos(NODE*,NODE*);
440
441 static int value_expr_gen(struct parser_params*,NODE*);
442 static void void_expr(struct parser_params*,NODE*);
443 static NODE *remove_begin(NODE*);
444 static NODE *remove_begin_all(NODE*);
445 #define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
446 static NODE *void_stmts(struct parser_params*,NODE*);
447 static void reduce_nodes(struct parser_params*,NODE**);
448 static void block_dup_check(struct parser_params*,NODE*,NODE*);
449
450 static NODE *block_append(struct parser_params*,NODE*,NODE*);
451 static NODE *list_append(struct parser_params*,NODE*,NODE*);
452 static NODE *list_concat(NODE*,NODE*);
453 static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
454 static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
455 static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
456 static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
457 static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
458 static NODE *evstr2dstr(struct parser_params*,NODE*);
459 static NODE *splat_array(NODE*);
460 static void mark_lvar_used(struct parser_params *p, NODE *rhs);
461
462 static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
463 static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
464 static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
465 static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
466 static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {b->nd_iter = m; b->nd_loc = *loc; return b;}
467
468 static bool args_info_empty_p(struct rb_args_info *args);
469 static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
470 static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
471 static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
472 static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
473 static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
474 static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
475 static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
476
477 static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
478 static NODE *args_with_numbered(struct parser_params*,NODE*,int);
479
480 static VALUE negate_lit(struct parser_params*, VALUE);
481 static NODE *ret_args(struct parser_params*,NODE*);
482 static NODE *arg_blk_pass(NODE*,NODE*);
483 static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
484 static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
485
486 static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
487 static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
488
489 static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
490 static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
491
492 static void rb_backref_error(struct parser_params*,NODE*);
493 static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
494
495 static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
496 static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
497 static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
498 static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
499 static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
500
501 static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
502
503 static NODE *opt_arg_append(NODE*, NODE*);
504 static NODE *kwd_append(NODE*, NODE*);
505
506 static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
507 static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
508
509 static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
510
511 static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
512
513 #define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
514
515 static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
516
517 static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
518
519 static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
520
521 static ID *local_tbl(struct parser_params*);
522
523 static VALUE reg_compile(struct parser_params*, VALUE, int);
524 static void reg_fragment_setenc(struct parser_params*, VALUE, int);
525 static int reg_fragment_check(struct parser_params*, VALUE, int);
526 static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
527
528 static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
529 static NODE *heredoc_dedent(struct parser_params*,NODE*);
530
531 static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
532
533 #define get_id(id) (id)
534 #define get_value(val) (val)
535 #define get_num(num) (num)
536 #else /* RIPPER */
537 #define NODE_RIPPER NODE_CDECL
538
539 static inline int ripper_is_node_yylval(VALUE n);
540
541 static inline VALUE
542 ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
543 {
544 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
545 add_mark_object(p, b);
546 add_mark_object(p, c);
547 return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
548 }
549
550 static inline int
551 ripper_is_node_yylval(VALUE n)
552 {
553 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
554 }
555
556 #define value_expr(node) ((void)(node))
557 #define remove_begin(node) (node)
558 #define void_stmts(p,x) (x)
559 #define rb_dvar_defined(id, base) 0
560 #define rb_local_defined(id, base) 0
561 static ID ripper_get_id(VALUE);
562 #define get_id(id) ripper_get_id(id)
563 static VALUE ripper_get_value(VALUE);
564 #define get_value(val) ripper_get_value(val)
565 #define get_num(num) (int)get_id(num)
566 static VALUE assignable(struct parser_params*,VALUE);
567 static int id_is_var(struct parser_params *p, ID id);
568
569 #define method_cond(p,node,loc) (node)
570 #define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
571 #define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
572 #define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
573 #define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
574
575 #define new_nil(loc) Qnil
576
577 static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
578
579 static VALUE const_decl(struct parser_params *p, VALUE path);
580
581 static VALUE var_field(struct parser_params *p, VALUE a);
582 static VALUE assign_error(struct parser_params *p, VALUE a);
583
584 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
585
586 #endif /* !RIPPER */
587
588 /* forward declaration */
589 typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
590
591 RUBY_SYMBOL_EXPORT_BEGIN
592 VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
593 int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
594 enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
595 VALUE rb_parser_lex_state_name(enum lex_state_e state);
596 void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
597 PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
598 YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
599 YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
600 YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
601 RUBY_SYMBOL_EXPORT_END
602
603 static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
604 static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
605 static void parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp);
606 static ID formal_argument(struct parser_params*, ID);
607 static ID shadowing_lvar(struct parser_params*,ID);
608 static void new_bv(struct parser_params*,ID);
609
610 static void local_push(struct parser_params*,int);
611 static void local_pop(struct parser_params*);
612 static void local_var(struct parser_params*, ID);
613 static void arg_var(struct parser_params*, ID);
614 static int local_id(struct parser_params *p, ID id);
615 static int local_id_ref(struct parser_params*, ID, ID **);
616 #ifndef RIPPER
617 static ID internal_id(struct parser_params*);
618 #endif
619
620 static const struct vtable *dyna_push(struct parser_params *);
621 static void dyna_pop(struct parser_params*, const struct vtable *);
622 static int dyna_in_block(struct parser_params*);
623 #define dyna_var(p, id) local_var(p, id)
624 static int dvar_defined(struct parser_params*, ID);
625 static int dvar_defined_ref(struct parser_params*, ID, ID**);
626 static int dvar_curr(struct parser_params*,ID);
627
628 static int lvar_defined(struct parser_params*, ID);
629
630 static NODE *numparam_push(struct parser_params *p);
631 static void numparam_pop(struct parser_params *p, NODE *prev_inner);
632
633 #ifdef RIPPER
634 # define METHOD_NOT idNOT
635 #else
636 # define METHOD_NOT '!'
637 #endif
638
639 #define idFWD_REST '*'
640 #ifdef RUBY3_KEYWORDS
641 #define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
642 #else
643 #define idFWD_KWREST 0
644 #endif
645 #define idFWD_BLOCK '&'
646
647 #define RE_OPTION_ONCE (1<<16)
648 #define RE_OPTION_ENCODING_SHIFT 8
649 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
650 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
651 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
652 #define RE_OPTION_MASK 0xff
653 #define RE_OPTION_ARG_ENCODING_NONE 32
654
655 /* structs for managing terminator of string literal and heredocment */
656 typedef struct rb_strterm_literal_struct {
657 union {
658 VALUE dummy;
659 long nest;
660 } u0;
661 union {
662 VALUE dummy;
663 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
664 } u1;
665 union {
666 VALUE dummy;
667 long paren; /* '(' of `%q(...)` */
668 } u2;
669 union {
670 VALUE dummy;
671 long term; /* ')' of `%q(...)` */
672 } u3;
673 } rb_strterm_literal_t;
674
675 #define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
676
677 struct rb_strterm_heredoc_struct {
678 VALUE lastline; /* the string of line that contains `<<"END"` */
679 long offset; /* the column of END in `<<"END"` */
680 int sourceline; /* lineno of the line that contains `<<"END"` */
681 unsigned length /* the length of END in `<<"END"` */
682 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
683 : HERETERM_LENGTH_BITS
684 # define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
685 #else
686 # define HERETERM_LENGTH_MAX UINT_MAX
687 #endif
688 ;
689 #if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
690 unsigned quote: 1;
691 unsigned func: 8;
692 #else
693 uint8_t quote;
694 uint8_t func;
695 #endif
696 };
697 STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
698
699 #define STRTERM_HEREDOC IMEMO_FL_USER0
700
701 struct rb_strterm_struct {
702 VALUE flags;
703 union {
704 rb_strterm_literal_t literal;
705 rb_strterm_heredoc_t heredoc;
706 } u;
707 };
708
709 #ifndef RIPPER
710 void
711 rb_strterm_mark(VALUE obj)
712 {
713 rb_strterm_t *strterm = (rb_strterm_t*)obj;
714 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
715 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
716 rb_gc_mark(heredoc->lastline);
717 }
718 }
719 #endif
720
721 #define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
722 size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
723
724 #define TOKEN2ID(tok) ( \
725 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
726 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
727 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
728 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
729 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
730 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
731 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
732
733 /****** Ripper *******/
734
735 #ifdef RIPPER
736 #define RIPPER_VERSION "0.1.0"
737
738 static inline VALUE intern_sym(const char *name);
739
740 #include "eventids1.c"
741 #include "eventids2.c"
742
743 static VALUE ripper_dispatch0(struct parser_params*,ID);
744 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
745 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
746 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
747 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
748 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
749 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
750 static void ripper_error(struct parser_params *p);
751
752 #define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
753 #define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
754 #define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
755 #define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
756 #define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
757 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
758 #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))
759
760 #define yyparse ripper_yyparse
761
762 #define ID2VAL(id) STATIC_ID2SYM(id)
763 #define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
764 #define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
765
766 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
767 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
768
769 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
770
771 static inline VALUE
772 new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
773 {
774 NODE *t = (NODE *)tail;
775 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
776 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
777 }
778
779 static inline VALUE
780 new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
781 {
782 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
783 add_mark_object(p, kw_args);
784 add_mark_object(p, kw_rest_arg);
785 add_mark_object(p, block);
786 return (VALUE)t;
787 }
788
789 static inline VALUE
790 args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
791 {
792 return args;
793 }
794
795 static VALUE
796 new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
797 {
798 NODE *t = (NODE *)aryptn;
799 struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
800 VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
801
802 if (apinfo) {
803 pre_args = rb_ary_entry(apinfo->imemo, 0);
804 rest_arg = rb_ary_entry(apinfo->imemo, 1);
805 post_args = rb_ary_entry(apinfo->imemo, 2);
806 }
807
808 if (!NIL_P(pre_arg)) {
809 if (!NIL_P(pre_args)) {
810 rb_ary_unshift(pre_args, pre_arg);
811 }
812 else {
813 pre_args = rb_ary_new_from_args(1, pre_arg);
814 }
815 }
816 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
817 }
818
819 static VALUE
820 new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
821 {
822 NODE *t;
823 struct rb_ary_pattern_info *apinfo;
824
825 if (has_rest) {
826 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
827 }
828 else {
829 rest_arg = Qnil;
830 }
831
832 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
833 apinfo = ZALLOC(struct rb_ary_pattern_info);
834 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
835 apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
836
837 t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
838 RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
839
840 return (VALUE)t;
841 }
842
843 #define new_hash(p,h,l) rb_ary_new_from_args(0)
844
845 static VALUE
846 new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
847 {
848 return ary;
849 }
850
851 static VALUE
852 new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
853 {
854 NODE *t = (NODE *)hshptn;
855 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
856 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
857 }
858
859 static VALUE
860 new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
861 {
862 NODE *t;
863 if (kw_rest_arg) {
864 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
865 }
866 else {
867 kw_rest_arg = Qnil;
868 }
869 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
870
871 add_mark_object(p, kw_args);
872 add_mark_object(p, kw_rest_arg);
873 return (VALUE)t;
874 }
875
876 #define new_defined(p,expr,loc) dispatch1(defined, (expr))
877
878 static VALUE heredoc_dedent(struct parser_params*,VALUE);
879
880 #else
881 #define ID2VAL(id) (id)
882 #define TOKEN2VAL(t) ID2VAL(t)
883 #define KWD2EID(t, v) keyword_##t
884 #endif /* RIPPER */
885
886 #ifndef RIPPER
887 # define Qnone 0
888 # define Qnull 0
889 # define ifndef_ripper(x) (x)
890 #else
891 # define Qnone Qnil
892 # define Qnull Qundef
893 # define ifndef_ripper(x)
894 #endif
895
896 # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
897 # define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
898 # define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
899 # define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
900 # define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
901 # define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
902 # define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
903 # define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
904 # define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
905 # define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
906 # define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
907 # define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
908 # define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
909 # define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
910 # define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
911 # define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
912 # define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
913 # define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
914 # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
915 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
916 #ifdef RIPPER
917 static ID id_warn, id_warning, id_gets, id_assoc;
918 # define WARN_S_L(s,l) STR_NEW(s,l)
919 # define WARN_S(s) STR_NEW2(s)
920 # define WARN_I(i) INT2NUM(i)
921 # define WARN_ID(i) rb_id2str(i)
922 # define WARN_IVAL(i) i
923 # define PRIsWARN "s"
924 # define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
925 # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
926 # ifdef HAVE_VA_ARGS_MACRO
927 # define WARN_CALL(...) rb_funcall(__VA_ARGS__)
928 # else
929 # define WARN_CALL rb_funcall
930 # endif
931 # define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
932 # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
933 # ifdef HAVE_VA_ARGS_MACRO
934 # define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
935 # else
936 # define WARNING_CALL rb_funcall
937 # endif
938 PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
939 # define compile_error ripper_compile_error
940 #else
941 # define WARN_S_L(s,l) s
942 # define WARN_S(s) s
943 # define WARN_I(i) i
944 # define WARN_ID(i) rb_id2name(i)
945 # define WARN_IVAL(i) NUM2INT(i)
946 # define PRIsWARN PRIsVALUE
947 # define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
948 # define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
949 # define WARN_CALL rb_compile_warn
950 # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
951 # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
952 # define WARNING_CALL rb_compile_warning
953 PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
954 # define compile_error parser_compile_error
955 #endif
956
957 static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
958 static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
959 static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
960 static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
961
962 #define WARN_EOL(tok) \
963 (looking_at_eol_p(p) ? \
964 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
965 (void)0)
966 static int looking_at_eol_p(struct parser_params *p);
967 %}
968
969 %expect 0
970 %define api.pure
971 %lex-param {struct parser_params *p}
972 %parse-param {struct parser_params *p}
973 %initial-action
974 {
975 RUBY_SET_YYLLOC_OF_NONE(@$);
976 };
977
978 %union {
979 VALUE val;
980 NODE *node;
981 ID id;
982 int num;
983 st_table *tbl;
984 const struct vtable *vars;
985 struct rb_strterm_struct *strterm;
986 }
987
988 %token <id>
989 keyword_class "`class'"
990 keyword_module "`module'"
991 keyword_def "`def'"
992 keyword_undef "`undef'"
993 keyword_begin "`begin'"
994 keyword_rescue "`rescue'"
995 keyword_ensure "`ensure'"
996 keyword_end "`end'"
997 keyword_if "`if'"
998 keyword_unless "`unless'"
999 keyword_then "`then'"
1000 keyword_elsif "`elsif'"
1001 keyword_else "`else'"
1002 keyword_case "`case'"
1003 keyword_when "`when'"
1004 keyword_while "`while'"
1005 keyword_until "`until'"
1006 keyword_for "`for'"
1007 keyword_break "`break'"
1008 keyword_next "`next'"
1009 keyword_redo "`redo'"
1010 keyword_retry "`retry'"
1011 keyword_in "`in'"
1012 keyword_do "`do'"
1013 keyword_do_cond "`do' for condition"
1014 keyword_do_block "`do' for block"
1015 keyword_do_LAMBDA "`do' for lambda"
1016 keyword_return "`return'"
1017 keyword_yield "`yield'"
1018 keyword_super "`super'"
1019 keyword_self "`self'"
1020 keyword_nil "`nil'"
1021 keyword_true "`true'"
1022 keyword_false "`false'"
1023 keyword_and "`and'"
1024 keyword_or "`or'"
1025 keyword_not "`not'"
1026 modifier_if "`if' modifier"
1027 modifier_unless "`unless' modifier"
1028 modifier_while "`while' modifier"
1029 modifier_until "`until' modifier"
1030 modifier_rescue "`rescue' modifier"
1031 keyword_alias "`alias'"
1032 keyword_defined "`defined?'"
1033 keyword_BEGIN "`BEGIN'"
1034 keyword_END "`END'"
1035 keyword__LINE__ "`__LINE__'"
1036 keyword__FILE__ "`__FILE__'"
1037 keyword__ENCODING__ "`__ENCODING__'"
1038
1039 %token <id> tIDENTIFIER "local variable or method"
1040 %token <id> tFID "method"
1041 %token <id> tGVAR "global variable"
1042 %token <id> tIVAR "instance variable"
1043 %token <id> tCONSTANT "constant"
1044 %token <id> tCVAR "class variable"
1045 %token <id> tLABEL
1046 %token <node> tINTEGER "integer literal"
1047 %token <node> tFLOAT "float literal"
1048 %token <node> tRATIONAL "rational literal"
1049 %token <node> tIMAGINARY "imaginary literal"
1050 %token <node> tCHAR "char literal"
1051 %token <node> tNTH_REF "numbered reference"
1052 %token <node> tBACK_REF "back reference"
1053 %token <node> tSTRING_CONTENT "literal content"
1054 %token <num> tREGEXP_END
1055
1056 %type <node> singleton strings string string1 xstring regexp
1057 %type <node> string_contents xstring_contents regexp_contents string_content
1058 %type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1059 %type <node> literal numeric simple_numeric ssym dsym symbol cpath
1060 %type <node> top_compstmt top_stmts top_stmt begin_block
1061 %type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1062 %type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1063 %type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1064 %type <node> args call_args opt_call_args
1065 %type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1066 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1067 %type <node> command_rhs arg_rhs
1068 %type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1069 %type <node> f_block_optarg f_block_opt
1070 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1071 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1072 %type <node> block_param opt_block_param block_param_def f_opt
1073 %type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1074 %type <node> bv_decls opt_bv_decl bvar
1075 %type <node> lambda f_larglist lambda_body brace_body do_body
1076 %type <node> brace_block cmd_brace_block do_block lhs none fitem
1077 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1078 %type <node> p_case_body p_cases p_top_expr p_top_expr_body
1079 %type <node> p_expr p_as p_alt p_expr_basic
1080 %type <node> p_args p_args_head p_args_tail p_args_post p_arg
1081 %type <node> p_value p_primitive p_variable p_var_ref p_const
1082 %type <node> p_kwargs p_kwarg p_kw
1083 %type <id> keyword_variable user_variable sym operation operation2 operation3
1084 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1085 %type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1086 %type <id> p_kwrest p_kwnorest p_kw_label
1087 %type <id> f_no_kwarg args_forward
1088 %token END_OF_INPUT 0 "end-of-input"
1089 %token <id> '.'
1090 /* escaped chars, should be ignored otherwise */
1091 %token <id> '\\' "backslash"
1092 %token tSP "escaped space"
1093 %token <id> '\t' "escaped horizontal tab"
1094 %token <id> '\f' "escaped form feed"
1095 %token <id> '\r' "escaped carriage return"
1096 %token <id> '\13' "escaped vertical tab"
1097 %token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1098 %token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1099 %token tPOW RUBY_TOKEN(POW) "**"
1100 %token tCMP RUBY_TOKEN(CMP) "<=>"
1101 %token tEQ RUBY_TOKEN(EQ) "=="
1102 %token tEQQ RUBY_TOKEN(EQQ) "==="
1103 %token tNEQ RUBY_TOKEN(NEQ) "!="
1104 %token tGEQ RUBY_TOKEN(GEQ) ">="
1105 %token tLEQ RUBY_TOKEN(LEQ) "<="
1106 %token tANDOP RUBY_TOKEN(ANDOP) "&&"
1107 %token tOROP RUBY_TOKEN(OROP) "||"
1108 %token tMATCH RUBY_TOKEN(MATCH) "=~"
1109 %token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1110 %token tDOT2 RUBY_TOKEN(DOT2) ".."
1111 %token tDOT3 RUBY_TOKEN(DOT3) "..."
1112 %token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1113 %token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1114 %token tAREF RUBY_TOKEN(AREF) "[]"
1115 %token tASET RUBY_TOKEN(ASET) "[]="
1116 %token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1117 %token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1118 %token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1119 %token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1120 %token tCOLON3 ":: at EXPR_BEG"
1121 %token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1122 %token tASSOC "=>"
1123 %token tLPAREN "("
1124 %token tLPAREN_ARG "( arg"
1125 %token tRPAREN ")"
1126 %token tLBRACK "["
1127 %token tLBRACE "{"
1128 %token tLBRACE_ARG "{ arg"
1129 %token tSTAR "*"
1130 %token tDSTAR "**arg"
1131 %token tAMPER "&"
1132 %token tLAMBDA "->"
1133 %token tSYMBEG "symbol literal"
1134 %token tSTRING_BEG "string literal"
1135 %token tXSTRING_BEG "backtick literal"
1136 %token tREGEXP_BEG "regexp literal"
1137 %token tWORDS_BEG "word list"
1138 %token tQWORDS_BEG "verbatim word list"
1139 %token tSYMBOLS_BEG "symbol list"
1140 %token tQSYMBOLS_BEG "verbatim symbol list"
1141 %token tSTRING_END "terminator"
1142 %token tSTRING_DEND "'}'"
1143 %token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1144
1145 /*
1146 * precedence table
1147 */
1148
1149 %nonassoc tLOWEST
1150 %nonassoc tLBRACE_ARG
1151
1152 %nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1153 %left keyword_or keyword_and
1154 %right keyword_not
1155 %nonassoc keyword_defined
1156 %right '=' tOP_ASGN
1157 %left modifier_rescue
1158 %right '?' ':'
1159 %nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1160 %left tOROP
1161 %left tANDOP
1162 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1163 %left '>' tGEQ '<' tLEQ
1164 %left '|' '^'
1165 %left '&'
1166 %left tLSHFT tRSHFT
1167 %left '+' '-'
1168 %left '*' '/' '%'
1169 %right tUMINUS_NUM tUMINUS
1170 %right tPOW
1171 %right '!' '~' tUPLUS
1172
1173 %token tLAST_TOKEN
1174
1175 %%
1176 program : {
1177 SET_LEX_STATE(EXPR_BEG);
1178 local_push(p, ifndef_ripper(1)+0);
1179 }
1180 top_compstmt
1181 {
1182 /*%%%*/
1183 if ($2 && !compile_for_eval) {
1184 NODE *node = $2;
1185 /* last expression should not be void */
1186 if (nd_type(node) == NODE_BLOCK) {
1187 while (node->nd_next) {
1188 node = node->nd_next;
1189 }
1190 node = node->nd_head;
1191 }
1192 node = remove_begin(node);
1193 void_expr(p, node);
1194 }
1195 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1196 /*% %*/
1197 /*% ripper[final]: program!($2) %*/
1198 local_pop(p);
1199 }
1200 ;
1201
1202 top_compstmt : top_stmts opt_terms
1203 {
1204 $$ = void_stmts(p, $1);
1205 }
1206 ;
1207
1208 top_stmts : none
1209 {
1210 /*%%%*/
1211 $$ = NEW_BEGIN(0, &@$);
1212 /*% %*/
1213 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1214 }
1215 | top_stmt
1216 {
1217 /*%%%*/
1218 $$ = newline_node($1);
1219 /*% %*/
1220 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1221 }
1222 | top_stmts terms top_stmt
1223 {
1224 /*%%%*/
1225 $$ = block_append(p, $1, newline_node($3));
1226 /*% %*/
1227 /*% ripper: stmts_add!($1, $3) %*/
1228 }
1229 | error top_stmt
1230 {
1231 $$ = remove_begin($2);
1232 }
1233 ;
1234
1235 top_stmt : stmt
1236 | keyword_BEGIN begin_block
1237 {
1238 $$ = $2;
1239 }
1240 ;
1241
1242 begin_block : '{' top_compstmt '}'
1243 {
1244 /*%%%*/
1245 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1246 NEW_BEGIN($2, &@$));
1247 $$ = NEW_BEGIN(0, &@$);
1248 /*% %*/
1249 /*% ripper: BEGIN!($2) %*/
1250 }
1251 ;
1252
1253 bodystmt : compstmt
1254 opt_rescue
1255 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1256 compstmt
1257 opt_ensure
1258 {
1259 /*%%%*/
1260 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1261 /*% %*/
1262 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1263 }
1264 | compstmt
1265 opt_rescue
1266 opt_ensure
1267 {
1268 /*%%%*/
1269 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1270 /*% %*/
1271 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1272 }
1273 ;
1274
1275 compstmt : stmts opt_terms
1276 {
1277 $$ = void_stmts(p, $1);
1278 }
1279 ;
1280
1281 stmts : none
1282 {
1283 /*%%%*/
1284 $$ = NEW_BEGIN(0, &@$);
1285 /*% %*/
1286 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1287 }
1288 | stmt_or_begin
1289 {
1290 /*%%%*/
1291 $$ = newline_node($1);
1292 /*% %*/
1293 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1294 }
1295 | stmts terms stmt_or_begin
1296 {
1297 /*%%%*/
1298 $$ = block_append(p, $1, newline_node($3));
1299 /*% %*/
1300 /*% ripper: stmts_add!($1, $3) %*/
1301 }
1302 | error stmt
1303 {
1304 $$ = remove_begin($2);
1305 }
1306 ;
1307
1308 stmt_or_begin : stmt
1309 {
1310 $$ = $1;
1311 }
1312 | keyword_BEGIN
1313 {
1314 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1315 }
1316 begin_block
1317 {
1318 $$ = $3;
1319 }
1320 ;
1321
1322 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1323 {
1324 /*%%%*/
1325 $$ = NEW_ALIAS($2, $4, &@$);
1326 /*% %*/
1327 /*% ripper: alias!($2, $4) %*/
1328 }
1329 | keyword_alias tGVAR tGVAR
1330 {
1331 /*%%%*/
1332 $$ = NEW_VALIAS($2, $3, &@$);
1333 /*% %*/
1334 /*% ripper: var_alias!($2, $3) %*/
1335 }
1336 | keyword_alias tGVAR tBACK_REF
1337 {
1338 /*%%%*/
1339 char buf[2];
1340 buf[0] = '$';
1341 buf[1] = (char)$3->nd_nth;
1342 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1343 /*% %*/
1344 /*% ripper: var_alias!($2, $3) %*/
1345 }
1346 | keyword_alias tGVAR tNTH_REF
1347 {
1348 /*%%%*/
1349 yyerror1(&@3, "can't make alias for the number variables");
1350 $$ = NEW_BEGIN(0, &@$);
1351 /*% %*/
1352 /*% ripper[error]: alias_error!(var_alias!($2, $3)) %*/
1353 }
1354 | keyword_undef undef_list
1355 {
1356 /*%%%*/
1357 $$ = $2;
1358 /*% %*/
1359 /*% ripper: undef!($2) %*/
1360 }
1361 | stmt modifier_if expr_value
1362 {
1363 /*%%%*/
1364 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1365 fixpos($$, $3);
1366 /*% %*/
1367 /*% ripper: if_mod!($3, $1) %*/
1368 }
1369 | stmt modifier_unless expr_value
1370 {
1371 /*%%%*/
1372 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1373 fixpos($$, $3);
1374 /*% %*/
1375 /*% ripper: unless_mod!($3, $1) %*/
1376 }
1377 | stmt modifier_while expr_value
1378 {
1379 /*%%%*/
1380 if ($1 && nd_type($1) == NODE_BEGIN) {
1381 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1382 }
1383 else {
1384 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1385 }
1386 /*% %*/
1387 /*% ripper: while_mod!($3, $1) %*/
1388 }
1389 | stmt modifier_until expr_value
1390 {
1391 /*%%%*/
1392 if ($1 && nd_type($1) == NODE_BEGIN) {
1393 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1394 }
1395 else {
1396 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1397 }
1398 /*% %*/
1399 /*% ripper: until_mod!($3, $1) %*/
1400 }
1401 | stmt modifier_rescue stmt
1402 {
1403 /*%%%*/
1404 NODE *resq;
1405 YYLTYPE loc = code_loc_gen(&@2, &@3);
1406 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1407 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1408 /*% %*/
1409 /*% ripper: rescue_mod!($1, $3) %*/
1410 }
1411 | keyword_END '{' compstmt '}'
1412 {
1413 if (p->in_def) {
1414 rb_warn0("END in method; use at_exit");
1415 }
1416 /*%%%*/
1417 {
1418 NODE *scope = NEW_NODE(
1419 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1420 $$ = NEW_POSTEXE(scope, &@$);
1421 }
1422 /*% %*/
1423 /*% ripper: END!($3) %*/
1424 }
1425 | command_asgn
1426 | mlhs '=' command_call
1427 {
1428 /*%%%*/
1429 value_expr($3);
1430 $$ = node_assign(p, $1, $3, &@$);
1431 /*% %*/
1432 /*% ripper: massign!($1, $3) %*/
1433 }
1434 | lhs '=' mrhs
1435 {
1436 /*%%%*/
1437 value_expr($3);
1438 $$ = node_assign(p, $1, $3, &@$);
1439 /*% %*/
1440 /*% ripper: assign!($1, $3) %*/
1441 }
1442 | mlhs '=' mrhs_arg modifier_rescue stmt
1443 {
1444 /*%%%*/
1445 YYLTYPE loc = code_loc_gen(&@4, &@5);
1446 value_expr($3);
1447 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1448 /*% %*/
1449 /*% ripper: massign!($1, rescue_mod!($3, $5)) %*/
1450 }
1451 | mlhs '=' mrhs_arg
1452 {
1453 /*%%%*/
1454 $$ = node_assign(p, $1, $3, &@$);
1455 /*% %*/
1456 /*% ripper: massign!($1, $3) %*/
1457 }
1458 | expr
1459 ;
1460
1461 command_asgn : lhs '=' command_rhs
1462 {
1463 /*%%%*/
1464 value_expr($3);
1465 $$ = node_assign(p, $1, $3, &@$);
1466 /*% %*/
1467 /*% ripper: assign!($1, $3) %*/
1468 }
1469 | var_lhs tOP_ASGN command_rhs
1470 {
1471 /*%%%*/
1472 value_expr($3);
1473 $$ = new_op_assign(p, $1, $2, $3, &@$);
1474 /*% %*/
1475 /*% ripper: opassign!($1, $2, $3) %*/
1476 }
1477 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1478 {
1479 /*%%%*/
1480 value_expr($6);
1481 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1482 /*% %*/
1483 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $6) %*/
1484
1485 }
1486 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1487 {
1488 /*%%%*/
1489 value_expr($5);
1490 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1491 /*% %*/
1492 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
1493 }
1494 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1495 {
1496 /*%%%*/
1497 value_expr($5);
1498 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1499 /*% %*/
1500 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
1501 }
1502 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1503 {
1504 /*%%%*/
1505 YYLTYPE loc = code_loc_gen(&@1, &@3);
1506 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1507 /*% %*/
1508 /*% ripper: opassign!(const_path_field!($1, $3), $4, $5) %*/
1509 }
1510 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1511 {
1512 /*%%%*/
1513 value_expr($5);
1514 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1515 /*% %*/
1516 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $5) %*/
1517 }
1518 | backref tOP_ASGN command_rhs
1519 {
1520 /*%%%*/
1521 rb_backref_error(p, $1);
1522 $$ = NEW_BEGIN(0, &@$);
1523 /*% %*/
1524 /*% ripper[error]: assign_error!(assign!(var_field(p, $1), $3)) %*/
1525 }
1526 ;
1527
1528 command_rhs : command_call %prec tOP_ASGN
1529 {
1530 value_expr($1);
1531 $$ = $1;
1532 }
1533 | command_call modifier_rescue stmt
1534 {
1535 /*%%%*/
1536 YYLTYPE loc = code_loc_gen(&@2, &@3);
1537 value_expr($1);
1538 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1539 /*% %*/
1540 /*% ripper: rescue_mod!($1, $3) %*/
1541 }
1542 | command_asgn
1543 ;
1544
1545 expr : command_call
1546 | expr keyword_and expr
1547 {
1548 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1549 }
1550 | expr keyword_or expr
1551 {
1552 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1553 }
1554 | keyword_not opt_nl expr
1555 {
1556 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1557 }
1558 | '!' command_call
1559 {
1560 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1561 }
1562 | arg keyword_in
1563 {
1564 value_expr($1);
1565 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1566 p->command_start = FALSE;
1567 $<num>$ = p->in_kwarg;
1568 p->in_kwarg = 1;
1569 }
1570 {$<tbl>$ = push_pvtbl(p);}
1571 p_expr
1572 {pop_pvtbl(p, $<tbl>4);}
1573 {
1574 p->in_kwarg = !!$<num>3;
1575 /*%%%*/
1576 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1577 /*% %*/
1578 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1579 }
1580 | arg %prec tLBRACE_ARG
1581 ;
1582
1583 expr_value : expr
1584 {
1585 value_expr($1);
1586 $$ = $1;
1587 }
1588 ;
1589
1590 expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1591 {
1592 $$ = $2;
1593 }
1594
1595
1596 command_call : command
1597 | block_command
1598 ;
1599
1600 block_command : block_call
1601 | block_call call_op2 operation2 command_args
1602 {
1603 /*%%%*/
1604 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1605 /*% %*/
1606 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
1607 }
1608 ;
1609
1610 cmd_brace_block : tLBRACE_ARG brace_body '}'
1611 {
1612 $$ = $2;
1613 /*%%%*/
1614 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1615 nd_set_line($$, @1.end_pos.lineno);
1616 /*% %*/
1617 }
1618 ;
1619
1620 fcall : operation
1621 {
1622 /*%%%*/
1623 $$ = NEW_FCALL($1, 0, &@$);
1624 nd_set_line($$, p->tokline);
1625 /*% %*/
1626 /*% ripper: $1 %*/
1627 }
1628 ;
1629
1630 command : fcall command_args %prec tLOWEST
1631 {
1632 /*%%%*/
1633 $1->nd_args = $2;
1634 nd_set_last_loc($1, @2.end_pos);
1635 $$ = $1;
1636 /*% %*/
1637 /*% ripper: command!($1, $2) %*/
1638 }
1639 | fcall command_args cmd_brace_block
1640 {
1641 /*%%%*/
1642 block_dup_check(p, $2, $3);
1643 $1->nd_args = $2;
1644 $$ = method_add_block(p, $1, $3, &@$);
1645 fixpos($$, $1);
1646 nd_set_last_loc($1, @2.end_pos);
1647 /*% %*/
1648 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
1649 }
1650 | primary_value call_op operation2 command_args %prec tLOWEST
1651 {
1652 /*%%%*/
1653 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1654 /*% %*/
1655 /*% ripper: command_call!($1, $2, $3, $4) %*/
1656 }
1657 | primary_value call_op operation2 command_args cmd_brace_block
1658 {
1659 /*%%%*/
1660 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1661 /*% %*/
1662 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
1663 }
1664 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1665 {
1666 /*%%%*/
1667 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1668 /*% %*/
1669 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
1670 }
1671 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1672 {
1673 /*%%%*/
1674 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1675 /*% %*/
1676 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
1677 }
1678 | keyword_super command_args
1679 {
1680 /*%%%*/
1681 $$ = NEW_SUPER($2, &@$);
1682 fixpos($$, $2);
1683 /*% %*/
1684 /*% ripper: super!($2) %*/
1685 }
1686 | keyword_yield command_args
1687 {
1688 /*%%%*/
1689 $$ = new_yield(p, $2, &@$);
1690 fixpos($$, $2);
1691 /*% %*/
1692 /*% ripper: yield!($2) %*/
1693 }
1694 | k_return call_args
1695 {
1696 /*%%%*/
1697 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1698 /*% %*/
1699 /*% ripper: return!($2) %*/
1700 }
1701 | keyword_break call_args
1702 {
1703 /*%%%*/
1704 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1705 /*% %*/
1706 /*% ripper: break!($2) %*/
1707 }
1708 | keyword_next call_args
1709 {
1710 /*%%%*/
1711 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1712 /*% %*/
1713 /*% ripper: next!($2) %*/
1714 }
1715 ;
1716
1717 mlhs : mlhs_basic
1718 | tLPAREN mlhs_inner rparen
1719 {
1720 /*%%%*/
1721 $$ = $2;
1722 /*% %*/
1723 /*% ripper: mlhs_paren!($2) %*/
1724 }
1725 ;
1726
1727 mlhs_inner : mlhs_basic
1728 | tLPAREN mlhs_inner rparen
1729 {
1730 /*%%%*/
1731 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1732 /*% %*/
1733 /*% ripper: mlhs_paren!($2) %*/
1734 }
1735 ;
1736
1737 mlhs_basic : mlhs_head
1738 {
1739 /*%%%*/
1740 $$ = NEW_MASGN($1, 0, &@$);
1741 /*% %*/
1742 /*% ripper: $1 %*/
1743 }
1744 | mlhs_head mlhs_item
1745 {
1746 /*%%%*/
1747 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1748 /*% %*/
1749 /*% ripper: mlhs_add!($1, $2) %*/
1750 }
1751 | mlhs_head tSTAR mlhs_node
1752 {
1753 /*%%%*/
1754 $$ = NEW_MASGN($1, $3, &@$);
1755 /*% %*/
1756 /*% ripper: mlhs_add_star!($1, $3) %*/
1757 }
1758 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1759 {
1760 /*%%%*/
1761 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1762 /*% %*/
1763 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
1764 }
1765 | mlhs_head tSTAR
1766 {
1767 /*%%%*/
1768 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1769 /*% %*/
1770 /*% ripper: mlhs_add_star!($1, Qnil) %*/
1771 }
1772 | mlhs_head tSTAR ',' mlhs_post
1773 {
1774 /*%%%*/
1775 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1776 /*% %*/
1777 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
1778 }
1779 | tSTAR mlhs_node
1780 {
1781 /*%%%*/
1782 $$ = NEW_MASGN(0, $2, &@$);
1783 /*% %*/
1784 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
1785 }
1786 | tSTAR mlhs_node ',' mlhs_post
1787 {
1788 /*%%%*/
1789 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1790 /*% %*/
1791 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
1792 }
1793 | tSTAR
1794 {
1795 /*%%%*/
1796 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1797 /*% %*/
1798 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
1799 }
1800 | tSTAR ',' mlhs_post
1801 {
1802 /*%%%*/
1803 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1804 /*% %*/
1805 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
1806 }
1807 ;
1808
1809 mlhs_item : mlhs_node
1810 | tLPAREN mlhs_inner rparen
1811 {
1812 /*%%%*/
1813 $$ = $2;
1814 /*% %*/
1815 /*% ripper: mlhs_paren!($2) %*/
1816 }
1817 ;
1818
1819 mlhs_head : mlhs_item ','
1820 {
1821 /*%%%*/
1822 $$ = NEW_LIST($1, &@1);
1823 /*% %*/
1824 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
1825 }
1826 | mlhs_head mlhs_item ','
1827 {
1828 /*%%%*/
1829 $$ = list_append(p, $1, $2);
1830 /*% %*/
1831 /*% ripper: mlhs_add!($1, $2) %*/
1832 }
1833 ;
1834
1835 mlhs_post : mlhs_item
1836 {
1837 /*%%%*/
1838 $$ = NEW_LIST($1, &@$);
1839 /*% %*/
1840 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
1841 }
1842 | mlhs_post ',' mlhs_item
1843 {
1844 /*%%%*/
1845 $$ = list_append(p, $1, $3);
1846 /*% %*/
1847 /*% ripper: mlhs_add!($1, $3) %*/
1848 }
1849 ;
1850
1851 mlhs_node : user_variable
1852 {
1853 /*%%%*/
1854 $$ = assignable(p, $1, 0, &@$);
1855 /*% %*/
1856 /*% ripper: assignable(p, var_field(p, $1)) %*/
1857 }
1858 | keyword_variable
1859 {
1860 /*%%%*/
1861 $$ = assignable(p, $1, 0, &@$);
1862 /*% %*/
1863 /*% ripper: assignable(p, var_field(p, $1)) %*/
1864 }
1865 | primary_value '[' opt_call_args rbracket
1866 {
1867 /*%%%*/
1868 $$ = aryset(p, $1, $3, &@$);
1869 /*% %*/
1870 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
1871 }
1872 | primary_value call_op tIDENTIFIER
1873 {
1874 if ($2 == tANDDOT) {
1875 yyerror1(&@2, "&. inside multiple assignment destination");
1876 }
1877 /*%%%*/
1878 $$ = attrset(p, $1, $2, $3, &@$);
1879 /*% %*/
1880 /*% ripper: field!($1, $2, $3) %*/
1881 }
1882 | primary_value tCOLON2 tIDENTIFIER
1883 {
1884 /*%%%*/
1885 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1886 /*% %*/
1887 /*% ripper: const_path_field!($1, $3) %*/
1888 }
1889 | primary_value call_op tCONSTANT
1890 {
1891 if ($2 == tANDDOT) {
1892 yyerror1(&@2, "&. inside multiple assignment destination");
1893 }
1894 /*%%%*/
1895 $$ = attrset(p, $1, $2, $3, &@$);
1896 /*% %*/
1897 /*% ripper: field!($1, $2, $3) %*/
1898 }
1899 | primary_value tCOLON2 tCONSTANT
1900 {
1901 /*%%%*/
1902 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1903 /*% %*/
1904 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
1905 }
1906 | tCOLON3 tCONSTANT
1907 {
1908 /*%%%*/
1909 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1910 /*% %*/
1911 /*% ripper: const_decl(p, top_const_field!($2)) %*/
1912 }
1913 | backref
1914 {
1915 /*%%%*/
1916 rb_backref_error(p, $1);
1917 $$ = NEW_BEGIN(0, &@$);
1918 /*% %*/
1919 /*% ripper[error]: assign_error!(var_field(p, $1)) %*/
1920 }
1921 ;
1922
1923 lhs : user_variable
1924 {
1925 /*%%%*/
1926 $$ = assignable(p, $1, 0, &@$);
1927 /*% %*/
1928 /*% ripper: assignable(p, var_field(p, $1)) %*/
1929 }
1930 | keyword_variable
1931 {
1932 /*%%%*/
1933 $$ = assignable(p, $1, 0, &@$);
1934 /*% %*/
1935 /*% ripper: assignable(p, var_field(p, $1)) %*/
1936 }
1937 | primary_value '[' opt_call_args rbracket
1938 {
1939 /*%%%*/
1940 $$ = aryset(p, $1, $3, &@$);
1941 /*% %*/
1942 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
1943 }
1944 | primary_value call_op tIDENTIFIER
1945 {
1946 /*%%%*/
1947 $$ = attrset(p, $1, $2, $3, &@$);
1948 /*% %*/
1949 /*% ripper: field!($1, $2, $3) %*/
1950 }
1951 | primary_value tCOLON2 tIDENTIFIER
1952 {
1953 /*%%%*/
1954 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1955 /*% %*/
1956 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
1957 }
1958 | primary_value call_op tCONSTANT
1959 {
1960 /*%%%*/
1961 $$ = attrset(p, $1, $2, $3, &@$);
1962 /*% %*/
1963 /*% ripper: field!($1, $2, $3) %*/
1964 }
1965 | primary_value tCOLON2 tCONSTANT
1966 {
1967 /*%%%*/
1968 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1969 /*% %*/
1970 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
1971 }
1972 | tCOLON3 tCONSTANT
1973 {
1974 /*%%%*/
1975 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1976 /*% %*/
1977 /*% ripper: const_decl(p, top_const_field!($2)) %*/
1978 }
1979 | backref
1980 {
1981 /*%%%*/
1982 rb_backref_error(p, $1);
1983 $$ = NEW_BEGIN(0, &@$);
1984 /*% %*/
1985 /*% ripper[error]: assign_error!(var_field(p, $1)) %*/
1986 }
1987 ;
1988
1989 cname : tIDENTIFIER
1990 {
1991 /*%%%*/
1992 yyerror1(&@1, "class/module name must be CONSTANT");
1993 /*% %*/
1994 /*% ripper[error]: class_name_error!($1) %*/
1995 }
1996 | tCONSTANT
1997 ;
1998
1999 cpath : tCOLON3 cname
2000 {
2001 /*%%%*/
2002 $$ = NEW_COLON3($2, &@$);
2003 /*% %*/
2004 /*% ripper: top_const_ref!($2) %*/
2005 }
2006 | cname
2007 {
2008 /*%%%*/
2009 $$ = NEW_COLON2(0, $$, &@$);
2010 /*% %*/
2011 /*% ripper: const_ref!($1) %*/
2012 }
2013 | primary_value tCOLON2 cname
2014 {
2015 /*%%%*/
2016 $$ = NEW_COLON2($1, $3, &@$);
2017 /*% %*/
2018 /*% ripper: const_path_ref!($1, $3) %*/
2019 }
2020 ;
2021
2022 fname : tIDENTIFIER
2023 | tCONSTANT
2024 | tFID
2025 | op
2026 {
2027 SET_LEX_STATE(EXPR_ENDFN);
2028 $$ = $1;
2029 }
2030 | reswords
2031 {
2032 SET_LEX_STATE(EXPR_ENDFN);
2033 $$ = $1;
2034 }
2035 ;
2036
2037 fitem : fname
2038 {
2039 /*%%%*/
2040 $$ = NEW_LIT(ID2SYM($1), &@$);
2041 /*% %*/
2042 /*% ripper: symbol_literal!($1) %*/
2043 }
2044 | symbol
2045 ;
2046
2047 undef_list : fitem
2048 {
2049 /*%%%*/
2050 $$ = NEW_UNDEF($1, &@$);
2051 /*% %*/
2052 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2053 }
2054 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2055 {
2056 /*%%%*/
2057 NODE *undef = NEW_UNDEF($4, &@4);
2058 $$ = block_append(p, $1, undef);
2059 /*% %*/
2060 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2061 }
2062 ;
2063
2064 op : '|' { ifndef_ripper($$ = '|'); }
2065 | '^' { ifndef_ripper($$ = '^'); }
2066 | '&' { ifndef_ripper($$ = '&'); }
2067 | tCMP { ifndef_ripper($$ = tCMP); }
2068 | tEQ { ifndef_ripper($$ = tEQ); }
2069 | tEQQ { ifndef_ripper($$ = tEQQ); }
2070 | tMATCH { ifndef_ripper($$ = tMATCH); }
2071 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2072 | '>' { ifndef_ripper($$ = '>'); }
2073 | tGEQ { ifndef_ripper($$ = tGEQ); }
2074 | '<' { ifndef_ripper($$ = '<'); }
2075 | tLEQ { ifndef_ripper($$ = tLEQ); }
2076 | tNEQ { ifndef_ripper($$ = tNEQ); }
2077 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2078 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2079 | '+' { ifndef_ripper($$ = '+'); }
2080 | '-' { ifndef_ripper($$ = '-'); }
2081 | '*' { ifndef_ripper($$ = '*'); }
2082 | tSTAR { ifndef_ripper($$ = '*'); }
2083 | '/' { ifndef_ripper($$ = '/'); }
2084 | '%' { ifndef_ripper($$ = '%'); }
2085 | tPOW { ifndef_ripper($$ = tPOW); }
2086 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2087 | '!' { ifndef_ripper($$ = '!'); }
2088 | '~' { ifndef_ripper($$ = '~'); }
2089 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2090 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2091 | tAREF { ifndef_ripper($$ = tAREF); }
2092 | tASET { ifndef_ripper($$ = tASET); }
2093 | '`' { ifndef_ripper($$ = '`'); }
2094 ;
2095
2096 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2097 | keyword_BEGIN | keyword_END
2098 | keyword_alias | keyword_and | keyword_begin
2099 | keyword_break | keyword_case | keyword_class | keyword_def
2100 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2101 | keyword_end | keyword_ensure | keyword_false
2102 | keyword_for | keyword_in | keyword_module | keyword_next
2103 | keyword_nil | keyword_not | keyword_or | keyword_redo
2104 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2105 | keyword_super | keyword_then | keyword_true | keyword_undef
2106 | keyword_when | keyword_yield | keyword_if | keyword_unless
2107 | keyword_while | keyword_until
2108 ;
2109
2110 arg : lhs '=' arg_rhs
2111 {
2112 /*%%%*/
2113 $$ = node_assign(p, $1, $3, &@$);
2114 /*% %*/
2115 /*% ripper: assign!($1, $3) %*/
2116 }
2117 | var_lhs tOP_ASGN arg_rhs
2118 {
2119 /*%%%*/
2120 $$ = new_op_assign(p, $1, $2, $3, &@$);
2121 /*% %*/
2122 /*% ripper: opassign!($1, $2, $3) %*/
2123 }
2124 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2125 {
2126 /*%%%*/
2127 value_expr($6);
2128 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2129 /*% %*/
2130 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $6) %*/
2131 }
2132 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2133 {
2134 /*%%%*/
2135 value_expr($5);
2136 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2137 /*% %*/
2138 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
2139 }
2140 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2141 {
2142 /*%%%*/
2143 value_expr($5);
2144 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2145 /*% %*/
2146 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
2147 }
2148 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2149 {
2150 /*%%%*/
2151 value_expr($5);
2152 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2153 /*% %*/
2154 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $5) %*/
2155 }
2156 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2157 {
2158 /*%%%*/
2159 YYLTYPE loc = code_loc_gen(&@1, &@3);
2160 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2161 /*% %*/
2162 /*% ripper: opassign!(const_path_field!($1, $3), $4, $5) %*/
2163 }
2164 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2165 {
2166 /*%%%*/
2167 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2168 /*% %*/
2169 /*% ripper: opassign!(top_const_field!($2), $3, $4) %*/
2170 }
2171 | backref tOP_ASGN arg_rhs
2172 {
2173 /*%%%*/
2174 rb_backref_error(p, $1);
2175 $$ = NEW_BEGIN(0, &@$);
2176 /*% %*/
2177 /*% ripper[error]: assign_error!(opassign!(var_field(p, $1), $2, $3)) %*/
2178 }
2179 | arg tDOT2 arg
2180 {
2181 /*%%%*/
2182 value_expr($1);
2183 value_expr($3);
2184 $$ = NEW_DOT2($1, $3, &@$);
2185 /*% %*/
2186 /*% ripper: dot2!($1, $3) %*/
2187 }
2188 | arg tDOT3 arg
2189 {
2190 /*%%%*/
2191 value_expr($1);
2192 value_expr($3);
2193 $$ = NEW_DOT3($1, $3, &@$);
2194 /*% %*/
2195 /*% ripper: dot3!($1, $3) %*/
2196 }
2197 | arg tDOT2
2198 {
2199 /*%%%*/
2200 YYLTYPE loc;
2201 loc.beg_pos = @2.end_pos;
2202 loc.end_pos = @2.end_pos;
2203
2204 value_expr($1);
2205 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2206 /*% %*/
2207 /*% ripper: dot2!($1, Qnil) %*/
2208 }
2209 | arg tDOT3
2210 {
2211 /*%%%*/
2212 YYLTYPE loc;
2213 loc.beg_pos = @2.end_pos;
2214 loc.end_pos = @2.end_pos;
2215
2216 value_expr($1);
2217 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2218 /*% %*/
2219 /*% ripper: dot3!($1, Qnil) %*/
2220 }
2221 | tBDOT2 arg
2222 {
2223 /*%%%*/
2224 YYLTYPE loc;
2225 loc.beg_pos = @1.beg_pos;
2226 loc.end_pos = @1.beg_pos;
2227
2228 value_expr($2);
2229 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2230 /*% %*/
2231 /*% ripper: dot2!(Qnil, $2) %*/
2232 }
2233 | tBDOT3 arg
2234 {
2235 /*%%%*/
2236 YYLTYPE loc;
2237 loc.beg_pos = @1.beg_pos;
2238 loc.end_pos = @1.beg_pos;
2239
2240 value_expr($2);
2241 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2242 /*% %*/
2243 /*% ripper: dot3!(Qnil, $2) %*/
2244 }
2245 | arg '+' arg
2246 {
2247 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2248 }
2249 | arg '-' arg
2250 {
2251 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2252 }
2253 | arg '*' arg
2254 {
2255 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2256 }
2257 | arg '/' arg
2258 {
2259 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2260 }
2261 | arg '%' arg
2262 {
2263 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2264 }
2265 | arg tPOW arg
2266 {
2267 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2268 }
2269 | tUMINUS_NUM simple_numeric tPOW arg
2270 {
2271 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2272 }
2273 | tUPLUS arg
2274 {
2275 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2276 }
2277 | tUMINUS arg
2278 {
2279 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2280 }
2281 | arg '|' arg
2282 {
2283 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2284 }
2285 | arg '^' arg
2286 {
2287 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2288 }
2289 | arg '&' arg
2290 {
2291 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2292 }
2293 | arg tCMP arg
2294 {
2295 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2296 }
2297 | rel_expr %prec tCMP
2298 | arg tEQ arg
2299 {
2300 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2301 }
2302 | arg tEQQ arg
2303 {
2304 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2305 }
2306 | arg tNEQ arg
2307 {
2308 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2309 }
2310 | arg tMATCH arg
2311 {
2312 $$ = match_op(p, $1, $3, &@2, &@$);
2313 }
2314 | arg tNMATCH arg
2315 {
2316 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2317 }
2318 | '!' arg
2319 {
2320 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2321 }
2322 | '~' arg
2323 {
2324 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2325 }
2326 | arg tLSHFT arg
2327 {
2328 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2329 }
2330 | arg tRSHFT arg
2331 {
2332 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2333 }
2334 | arg tANDOP arg
2335 {
2336 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2337 }
2338 | arg tOROP arg
2339 {
2340 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2341 }
2342 | keyword_defined opt_nl {p->in_defined = 1;} arg
2343 {
2344 p->in_defined = 0;
2345 $$ = new_defined(p, $4, &@$);
2346 }
2347 | arg '?' arg opt_nl ':' arg
2348 {
2349 /*%%%*/
2350 value_expr($1);
2351 $$ = new_if(p, $1, $3, $6, &@$);
2352 fixpos($$, $1);
2353 /*% %*/
2354 /*% ripper: ifop!($1, $3, $6) %*/
2355 }
2356 | primary
2357 {
2358 $$ = $1;
2359 }
2360 ;
2361
2362 relop : '>' {$$ = '>';}
2363 | '<' {$$ = '<';}
2364 | tGEQ {$$ = idGE;}
2365 | tLEQ {$$ = idLE;}
2366 ;
2367
2368 rel_expr : arg relop arg %prec '>'
2369 {
2370 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2371 }
2372 | rel_expr relop arg %prec '>'
2373 {
2374 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2375 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2376 }
2377 ;
2378
2379 arg_value : arg
2380 {
2381 value_expr($1);
2382 $$ = $1;
2383 }
2384 ;
2385
2386 aref_args : none
2387 | args trailer
2388 {
2389 $$ = $1;
2390 }
2391 | args ',' assocs trailer
2392 {
2393 /*%%%*/
2394 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2395 /*% %*/
2396 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2397 }
2398 | assocs trailer
2399 {
2400 /*%%%*/
2401 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2402 /*% %*/
2403 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2404 }
2405 ;
2406
2407 arg_rhs : arg %prec tOP_ASGN
2408 {
2409 value_expr($1);
2410 $$ = $1;
2411 }
2412 | arg modifier_rescue arg
2413 {
2414 /*%%%*/
2415 YYLTYPE loc = code_loc_gen(&@2, &@3);
2416 value_expr($1);
2417 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2418 /*% %*/
2419 /*% ripper: rescue_mod!($1, $3) %*/
2420 }
2421 ;
2422
2423 paren_args : '(' opt_call_args rparen
2424 {
2425 /*%%%*/
2426 $$ = $2;
2427 /*% %*/
2428 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2429 }
2430 | '(' args_forward rparen
2431 {
2432 if (!local_id(p, idFWD_REST) ||
2433 #if idFWD_KWREST
2434 !local_id(p, idFWD_KWREST) ||
2435 #endif
2436 !local_id(p, idFWD_BLOCK)) {
2437 compile_error(p, "unexpected ...");
2438 $$ = Qnone;
2439 }
2440 else {
2441 /*%%%*/
2442 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2443 #if idFWD_KWREST
2444 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2445 #endif
2446 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2447 #if idFWD_KWREST
2448 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2449 #else
2450 $$ = splat;
2451 #endif
2452 $$ = arg_blk_pass($$, block);
2453 /*% %*/
2454 /*% ripper: arg_paren!($2) %*/
2455 }
2456 }
2457 ;
2458
2459 opt_paren_args : none
2460 | paren_args
2461 ;
2462
2463 opt_call_args : none
2464 | call_args
2465 | args ','
2466 {
2467 $$ = $1;
2468 }
2469 | args ',' assocs ','
2470 {
2471 /*%%%*/
2472 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2473 /*% %*/
2474 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2475 }
2476 | assocs ','
2477 {
2478 /*%%%*/
2479 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2480 /*% %*/
2481 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2482 }
2483 ;
2484
2485 call_args : command
2486 {
2487 /*%%%*/
2488 value_expr($1);
2489 $$ = NEW_LIST($1, &@$);
2490 /*% %*/
2491 /*% ripper: args_add!(args_new!, $1) %*/
2492 }
2493 | args opt_block_arg
2494 {
2495 /*%%%*/
2496 $$ = arg_blk_pass($1, $2);
2497 /*% %*/
2498 /*% ripper: args_add_block!($1, $2) %*/
2499 }
2500 | assocs opt_block_arg
2501 {
2502 /*%%%*/
2503 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2504 $$ = arg_blk_pass($$, $2);
2505 /*% %*/
2506 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
2507 }
2508 | args ',' assocs opt_block_arg
2509 {
2510 /*%%%*/
2511 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2512 $$ = arg_blk_pass($$, $4);
2513 /*% %*/
2514 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
2515 }
2516 | block_arg
2517 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
2518 ;
2519
2520 command_args : {
2521 /* If call_args starts with a open paren '(' or '[',
2522 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2523 * but the push must be done after CMDARG_PUSH(1).
2524 * So this code makes them consistent by first cancelling
2525 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2526 * and finally redoing CMDARG_PUSH(0).
2527 */
2528 int lookahead = 0;
2529 switch (yychar) {
2530 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2531 lookahead = 1;
2532 }
2533 if (lookahead) CMDARG_POP();
2534 CMDARG_PUSH(1);
2535 if (lookahead) CMDARG_PUSH(0);
2536 }
2537 call_args
2538 {
2539 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2540 * but the push must be done after CMDARG_POP() in the parser.
2541 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2542 * CMDARG_POP() to pop 1 pushed by command_args,
2543 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2544 */
2545 int lookahead = 0;
2546 switch (yychar) {
2547 case tLBRACE_ARG:
2548 lookahead = 1;
2549 }
2550 if (lookahead) CMDARG_POP();
2551 CMDARG_POP();
2552 if (lookahead) CMDARG_PUSH(0);
2553 $$ = $2;
2554 }
2555 ;
2556
2557 block_arg : tAMPER arg_value
2558 {
2559 /*%%%*/
2560 $$ = NEW_BLOCK_PASS($2, &@$);
2561 /*% %*/
2562 /*% ripper: $2 %*/
2563 }
2564 ;
2565
2566 opt_block_arg : ',' block_arg
2567 {
2568 $$ = $2;
2569 }
2570 | none
2571 {
2572 $$ = 0;
2573 }
2574 ;
2575
2576 args : arg_value
2577 {
2578 /*%%%*/
2579 $$ = NEW_LIST($1, &@$);
2580 /*% %*/
2581 /*% ripper: args_add!(args_new!, $1) %*/
2582 }
2583 | tSTAR arg_value
2584 {
2585 /*%%%*/
2586 $$ = NEW_SPLAT($2, &@$);
2587 /*% %*/
2588 /*% ripper: args_add_star!(args_new!, $2) %*/
2589 }
2590 | args ',' arg_value
2591 {
2592 /*%%%*/
2593 $$ = last_arg_append(p, $1, $3, &@$);
2594 /*% %*/
2595 /*% ripper: args_add!($1, $3) %*/
2596 }
2597 | args ',' tSTAR arg_value
2598 {
2599 /*%%%*/
2600 $$ = rest_arg_append(p, $1, $4, &@$);
2601 /*% %*/
2602 /*% ripper: args_add_star!($1, $4) %*/
2603 }
2604 ;
2605
2606 mrhs_arg : mrhs
2607 | arg_value
2608 ;
2609
2610 mrhs : args ',' arg_value
2611 {
2612 /*%%%*/
2613 $$ = last_arg_append(p, $1, $3, &@$);
2614 /*% %*/
2615 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
2616 }
2617 | args ',' tSTAR arg_value
2618 {
2619 /*%%%*/
2620 $$ = rest_arg_append(p, $1, $4, &@$);
2621 /*% %*/
2622 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
2623 }
2624 | tSTAR arg_value
2625 {
2626 /*%%%*/
2627 $$ = NEW_SPLAT($2, &@$);
2628 /*% %*/
2629 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
2630 }
2631 ;
2632
2633 primary : literal
2634 | strings
2635 | xstring
2636 | regexp
2637 | words
2638 | qwords
2639 | symbols
2640 | qsymbols
2641 | var_ref
2642 | backref
2643 | tFID
2644 {
2645 /*%%%*/
2646 $$ = NEW_FCALL($1, 0, &@$);
2647 /*% %*/
2648 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
2649 }
2650 | k_begin
2651 {
2652 CMDARG_PUSH(0);
2653 }
2654 bodystmt
2655 k_end
2656 {
2657 CMDARG_POP();
2658 /*%%%*/
2659 set_line_body($3, @1.end_pos.lineno);
2660 $$ = NEW_BEGIN($3, &@$);
2661 nd_set_line($$, @1.end_pos.lineno);
2662 /*% %*/
2663 /*% ripper: begin!($3) %*/
2664 }
2665 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2666 {
2667 /*%%%*/
2668 $$ = NEW_BEGIN(0, &@$);
2669 /*% %*/
2670 /*% ripper: paren!(0) %*/
2671 }
2672 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2673 {
2674 /*%%%*/
2675 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2676 $$ = $2;
2677 /*% %*/
2678 /*% ripper: paren!($2) %*/
2679 }
2680 | tLPAREN compstmt ')'
2681 {
2682 /*%%%*/
2683 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2684 $$ = $2;
2685 /*% %*/
2686 /*% ripper: paren!($2) %*/
2687 }
2688 | primary_value tCOLON2 tCONSTANT
2689 {
2690 /*%%%*/
2691 $$ = NEW_COLON2($1, $3, &@$);
2692 /*% %*/
2693 /*% ripper: const_path_ref!($1, $3) %*/
2694 }
2695 | tCOLON3 tCONSTANT
2696 {
2697 /*%%%*/
2698 $$ = NEW_COLON3($2, &@$);
2699 /*% %*/
2700 /*% ripper: top_const_ref!($2) %*/
2701 }
2702 | tLBRACK aref_args ']'
2703 {
2704 /*%%%*/
2705 $$ = make_list($2, &@$);
2706 /*% %*/
2707 /*% ripper: array!(escape_Qundef($2)) %*/
2708 }
2709 | tLBRACE assoc_list '}'
2710 {
2711 /*%%%*/
2712 $$ = new_hash(p, $2, &@$);
2713 $$->nd_brace = TRUE;
2714 /*% %*/
2715 /*% ripper: hash!(escape_Qundef($2)) %*/
2716 }
2717 | k_return
2718 {
2719 /*%%%*/
2720 $$ = NEW_RETURN(0, &@$);
2721 /*% %*/
2722 /*% ripper: return0! %*/
2723 }
2724 | keyword_yield '(' call_args rparen
2725 {
2726 /*%%%*/
2727 $$ = new_yield(p, $3, &@$);
2728 /*% %*/
2729 /*% ripper: yield!(paren!($3)) %*/
2730 }
2731 | keyword_yield '(' rparen
2732 {
2733 /*%%%*/
2734 $$ = NEW_YIELD(0, &@$);
2735 /*% %*/
2736 /*% ripper: yield!(paren!(args_new!)) %*/
2737 }
2738 | keyword_yield
2739 {
2740 /*%%%*/
2741 $$ = NEW_YIELD(0, &@$);
2742 /*% %*/
2743 /*% ripper: yield0! %*/
2744 }
2745 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2746 {
2747 p->in_defined = 0;
2748 $$ = new_defined(p, $5, &@$);
2749 }
2750 | keyword_not '(' expr rparen
2751 {
2752 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2753 }
2754 | keyword_not '(' rparen
2755 {
2756 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2757 }
2758 | fcall brace_block
2759 {
2760 /*%%%*/
2761 $$ = method_add_block(p, $1, $2, &@$);
2762 /*% %*/
2763 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
2764 }
2765 | method_call
2766 | method_call brace_block
2767 {
2768 /*%%%*/
2769 block_dup_check(p, $1->nd_args, $2);
2770 $$ = method_add_block(p, $1, $2, &@$);
2771 /*% %*/
2772 /*% ripper: method_add_block!($1, $2) %*/
2773 }
2774 | tLAMBDA
2775 {
2776 token_info_push(p, "->", &@1);
2777 }
2778 lambda
2779 {
2780 $$ = $3;
2781 /*%%%*/
2782 nd_set_first_loc($$, @1.beg_pos);
2783 /*% %*/
2784 }
2785 | k_if expr_value then
2786 compstmt
2787 if_tail
2788 k_end
2789 {
2790 /*%%%*/
2791 $$ = new_if(p, $2, $4, $5, &@$);
2792 fixpos($$, $2);
2793 /*% %*/
2794 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
2795 }
2796 | k_unless expr_value then
2797 compstmt
2798 opt_else
2799 k_end
2800 {
2801 /*%%%*/
2802 $$ = new_unless(p, $2, $4, $5, &@$);
2803 fixpos($$, $2);
2804 /*% %*/
2805 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
2806 }
2807 | k_while expr_value_do
2808 compstmt
2809 k_end
2810 {
2811 /*%%%*/
2812 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2813 fixpos($$, $2);
2814 /*% %*/
2815 /*% ripper: while!($2, $3) %*/
2816 }
2817 | k_until expr_value_do
2818 compstmt
2819 k_end
2820 {
2821 /*%%%*/
2822 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2823 fixpos($$, $2);
2824 /*% %*/
2825 /*% ripper: until!($2, $3) %*/
2826 }
2827 | k_case expr_value opt_terms
2828 {
2829 $<val>$ = p->case_labels;
2830 p->case_labels = Qnil;
2831 }
2832 case_body
2833 k_end
2834 {
2835 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2836 p->case_labels = $<val>4;
2837 /*%%%*/
2838 $$ = NEW_CASE($2, $5, &@$);
2839 fixpos($$, $2);
2840 /*% %*/
2841 /*% ripper: case!($2, $5) %*/
2842 }
2843 | k_case opt_terms
2844 {
2845 $<val>$ = p->case_labels;
2846 p->case_labels = 0;
2847 }
2848 case_body
2849 k_end
2850 {
2851 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2852 p->case_labels = $<val>3;
2853 /*%%%*/
2854 $$ = NEW_CASE2($4, &@$);
2855 /*% %*/
2856 /*% ripper: case!(Qnil, $4) %*/
2857 }
2858 | k_case expr_value opt_terms
2859 p_case_body
2860 k_end
2861 {
2862 /*%%%*/
2863 $$ = new_case3(p, $2, $4, &@$);
2864 /*% %*/
2865 /*% ripper: case!($2, $4) %*/
2866 }
2867 | k_for for_var keyword_in expr_value_do
2868 compstmt
2869 k_end
2870 {
2871 /*%%%*/
2872 /*
2873 * for a, b, c in e
2874 * #=>
2875 * e.each{|*x| a, b, c = x}
2876 *
2877 * for a in e
2878 * #=>
2879 * e.each{|x| a, = x}
2880 */
2881 ID id = internal_id(p);
2882 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2883 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2884 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
2885 ID *tbl = ALLOC_N(ID, 3);
2886 rb_imemo_tmpbuf_set_ptr(tmpbuf, tbl);
2887 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2888 tbl[2] = tmpbuf;
2889
2890 switch (nd_type($2)) {
2891 case NODE_LASGN:
2892 case NODE_DASGN:
2893 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2894 $2->nd_value = internal_var;
2895 id = 0;
2896 m->nd_plen = 1;
2897 m->nd_next = $2;
2898 break;
2899 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2900 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2901 break;
2902 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2903 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2904 }
2905 /* {|*internal_id| <m> = internal_id; ... } */
2906 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2907 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2908 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
2909 $$ = NEW_FOR($4, scope, &@$);
2910 fixpos($$, $2);
2911 /*% %*/
2912 /*% ripper: for!($2, $4, $5) %*/
2913 }
2914 | k_class cpath superclass
2915 {
2916 if (p->in_def) {
2917 YYLTYPE loc = code_loc_gen(&@1, &@2);
2918 yyerror1(&loc, "class definition in method body");
2919 }
2920 $<num>1 = p->in_class;
2921 p->in_class = 1;
2922 local_push(p, 0);
2923 }
2924 bodystmt
2925 k_end
2926 {
2927 /*%%%*/
2928 $$ = NEW_CLASS($2, $5, $3, &@$);
2929 nd_set_line($$->nd_body, @6.end_pos.lineno);
2930 set_line_body($5, @3.end_pos.lineno);
2931 nd_set_line($$, @3.end_pos.lineno);
2932 /*% %*/
2933 /*% ripper: class!($2, $3, $5) %*/
2934 local_pop(p);
2935 p->in_class = $<num>1 & 1;
2936 }
2937 | k_class tLSHFT expr
2938 {
2939 $<num>$ = (p->in_class << 1) | p->in_def;
2940 p->in_def = 0;
2941 p->in_class = 0;
2942 local_push(p, 0);
2943 }
2944 term
2945 bodystmt
2946 k_end
2947 {
2948 /*%%%*/
2949 $$ = NEW_SCLASS($3, $6, &@$);
2950 nd_set_line($$->nd_body, @7.end_pos.lineno);
2951 set_line_body($6, nd_line($3));
2952 fixpos($$, $3);
2953 /*% %*/
2954 /*% ripper: sclass!($3, $6) %*/
2955 local_pop(p);
2956 p->in_def = $<num>4 & 1;
2957 p->in_class = ($<num>4 >> 1) & 1;
2958 }
2959 | k_module cpath
2960 {
2961 if (p->in_def) {
2962 YYLTYPE loc = code_loc_gen(&@1, &@2);
2963 yyerror1(&loc, "module definition in method body");
2964 }
2965 $<num>1 = p->in_class;
2966 p->in_class = 1;
2967 local_push(p, 0);
2968 }
2969 bodystmt
2970 k_end
2971 {
2972 /*%%%*/
2973 $$ = NEW_MODULE($2, $4, &@$);
2974 nd_set_line($$->nd_body, @5.end_pos.lineno);
2975 set_line_body($4, @2.end_pos.lineno);
2976 nd_set_line($$, @2.end_pos.lineno);
2977 /*% %*/
2978 /*% ripper: module!($2, $4) %*/
2979 local_pop(p);
2980 p->in_class = $<num>1 & 1;
2981 }
2982 | k_def fname
2983 {
2984 numparam_name(p, get_id($2));
2985 local_push(p, 0);
2986 $<id>$ = p->cur_arg;
2987 p->cur_arg = 0;
2988 }
2989 {
2990 $<num>$ = p->in_def;
2991 p->in_def = 1;
2992 }
2993 f_arglist
2994 bodystmt
2995 k_end
2996 {
2997 /*%%%*/
2998 NODE *body = remove_begin($6);
2999 reduce_nodes(p, &body);
3000 $$ = NEW_DEFN($2, $5, body, &@$);
3001 nd_set_line($$->nd_defn, @7.end_pos.lineno);
3002 set_line_body(body, @1.beg_pos.lineno);
3003 /*% %*/
3004 /*% ripper: def!($2, $5, $6) %*/
3005 local_pop(p);
3006 p->in_def = $<num>4 & 1;
3007 p->cur_arg = $<id>3;
3008 }
3009 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3010 {
3011 numparam_name(p, get_id($5));
3012 $<num>4 = p->in_def;
3013 p->in_def = 1;
3014 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3015 local_push(p, 0);
3016 $<id>$ = p->cur_arg;
3017 p->cur_arg = 0;
3018 }
3019 f_arglist
3020 bodystmt
3021 k_end
3022 {
3023 /*%%%*/
3024 NODE *body = remove_begin($8);
3025 reduce_nodes(p, &body);
3026 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3027 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3028 set_line_body(body, @1.beg_pos.lineno);
3029 /*% %*/
3030 /*% ripper: defs!($2, $3, $5, $7, $8) %*/
3031 local_pop(p);
3032 p->in_def = $<num>4 & 1;
3033 p->cur_arg = $<id>6;
3034 }
3035 | keyword_break
3036 {
3037 /*%%%*/
3038 $$ = NEW_BREAK(0, &@$);
3039 /*% %*/
3040 /*% ripper: break!(args_new!) %*/
3041 }
3042 | keyword_next
3043 {
3044 /*%%%*/
3045 $$ = NEW_NEXT(0, &@$);
3046 /*% %*/
3047 /*% ripper: next!(args_new!) %*/
3048 }
3049 | keyword_redo
3050 {
3051 /*%%%*/
3052 $$ = NEW_REDO(&@$);
3053 /*% %*/
3054 /*% ripper: redo! %*/
3055 }
3056 | keyword_retry
3057 {
3058 /*%%%*/
3059 $$ = NEW_RETRY(&@$);
3060 /*% %*/
3061 /*% ripper: retry! %*/
3062 }
3063 ;
3064
3065 primary_value : primary
3066 {
3067 value_expr($1);
3068 $$ = $1;
3069 }
3070 ;
3071
3072 k_begin : keyword_begin
3073 {
3074 token_info_push(p, "begin", &@$);
3075 }
3076 ;
3077
3078 k_if : keyword_if
3079 {
3080 WARN_EOL("if");
3081 token_info_push(p, "if", &@$);
3082 if (p->token_info && p->token_info->nonspc &&
3083 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3084 const char *tok = p->lex.ptok;
3085 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3086 beg += rb_strlen_lit("else");
3087 while (beg < tok && ISSPACE(*beg)) beg++;
3088 if (beg == tok) {
3089 p->token_info->nonspc = 0;
3090 }
3091 }
3092 }
3093 ;
3094
3095 k_unless : keyword_unless
3096 {
3097 token_info_push(p, "unless", &@$);
3098 }
3099 ;
3100
3101 k_while : keyword_while
3102 {
3103 token_info_push(p, "while", &@$);
3104 }
3105 ;
3106
3107 k_until : keyword_until
3108 {
3109 token_info_push(p, "until", &@$);
3110 }
3111 ;
3112
3113 k_case : keyword_case
3114 {
3115 token_info_push(p, "case", &@$);
3116 }
3117 ;
3118
3119 k_for : keyword_for
3120 {
3121 token_info_push(p, "for", &@$);
3122 }
3123 ;
3124
3125 k_class : keyword_class
3126 {
3127 token_info_push(p, "class", &@$);
3128 }
3129 ;
3130
3131 k_module : keyword_module
3132 {
3133 token_info_push(p, "module", &@$);
3134 }
3135 ;
3136
3137 k_def : keyword_def
3138 {
3139 token_info_push(p, "def", &@$);
3140 }
3141 ;
3142
3143 k_do : keyword_do
3144 {
3145 token_info_push(p, "do", &@$);
3146 }
3147 ;
3148
3149 k_do_block : keyword_do_block
3150 {
3151 token_info_push(p, "do", &@$);
3152 }
3153 ;
3154
3155 k_rescue : keyword_rescue
3156 {
3157 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3158 }
3159 ;
3160
3161 k_ensure : keyword_ensure
3162 {
3163 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3164 }
3165 ;
3166
3167 k_when : keyword_when
3168 {
3169 token_info_warn(p, "when", p->token_info, 0, &@$);
3170 }
3171 ;
3172
3173 k_else : keyword_else
3174 {
3175 token_info *ptinfo_beg = p->token_info;
3176 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3177 token_info_warn(p, "else", p->token_info, same, &@$);
3178 if (same) {
3179 token_info e;
3180 e.next = ptinfo_beg->next;
3181 e.token = "else";
3182 token_info_setup(&e, p->lex.pbeg, &@$);
3183 if (!e.nonspc) *ptinfo_beg = e;
3184 }
3185 }
3186 ;
3187
3188 k_elsif : keyword_elsif
3189 {
3190 WARN_EOL("elsif");
3191 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3192 }
3193 ;
3194
3195 k_end : keyword_end
3196 {
3197 token_info_pop(p, "end", &@$);
3198 }
3199 ;
3200
3201 k_return : keyword_return
3202 {
3203 if (p->in_class && !p->in_def && !dyna_in_block(p))
3204 yyerror1(&@1, "Invalid return in class/module body");
3205 }
3206 ;
3207
3208 then : term
3209 | keyword_then
3210 | term keyword_then
3211 ;
3212
3213 do : term
3214 | keyword_do_cond
3215 ;
3216
3217 if_tail : opt_else
3218 | k_elsif expr_value then
3219 compstmt
3220 if_tail
3221 {
3222 /*%%%*/
3223 $$ = new_if(p, $2, $4, $5, &@$);
3224 fixpos($$, $2);
3225 /*% %*/
3226 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3227 }
3228 ;
3229
3230 opt_else : none
3231 | k_else compstmt
3232 {
3233 /*%%%*/
3234 $$ = $2;
3235 /*% %*/
3236 /*% ripper: else!($2) %*/
3237 }
3238 ;
3239
3240 for_var : lhs
3241 | mlhs
3242 ;
3243
3244 f_marg : f_norm_arg
3245 {
3246 /*%%%*/
3247 $$ = assignable(p, $1, 0, &@$);
3248 mark_lvar_used(p, $$);
3249 /*% %*/
3250 /*% ripper: assignable(p, $1) %*/
3251 }
3252 | tLPAREN f_margs rparen
3253 {
3254 /*%%%*/
3255 $$ = $2;
3256 /*% %*/
3257 /*% ripper: mlhs_paren!($2) %*/
3258 }
3259 ;
3260
3261 f_marg_list : f_marg
3262 {
3263 /*%%%*/
3264 $$ = NEW_LIST($1, &@$);
3265 /*% %*/
3266 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3267 }
3268 | f_marg_list ',' f_marg
3269 {
3270 /*%%%*/
3271 $$ = list_append(p, $1, $3);
3272 /*% %*/
3273 /*% ripper: mlhs_add!($1, $3) %*/
3274 }
3275 ;
3276
3277 f_margs : f_marg_list
3278 {
3279 /*%%%*/
3280 $$ = NEW_MASGN($1, 0, &@$);
3281 /*% %*/
3282 /*% ripper: $1 %*/
3283 }
3284 | f_marg_list ',' f_rest_marg
3285 {
3286 /*%%%*/
3287 $$ = NEW_MASGN($1, $3, &@$);
3288 /*% %*/
3289 /*% ripper: mlhs_add_star!($1, $3) %*/
3290 }
3291 | f_marg_list ',' f_rest_marg ',' f_marg_list
3292 {
3293 /*%%%*/
3294 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3295 /*% %*/
3296 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3297 }
3298 | f_rest_marg
3299 {
3300 /*%%%*/
3301 $$ = NEW_MASGN(0, $1, &@$);
3302 /*% %*/
3303 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3304 }
3305 | f_rest_marg ',' f_marg_list
3306 {
3307 /*%%%*/
3308 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3309 /*% %*/
3310 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3311 }
3312 ;
3313
3314 f_rest_marg : tSTAR f_norm_arg
3315 {
3316 /*%%%*/
3317 $$ = assignable(p, $2, 0, &@$);
3318 mark_lvar_used(p, $$);
3319 /*% %*/
3320 /*% ripper: assignable(p, $2) %*/
3321 }
3322 | tSTAR
3323 {
3324 /*%%%*/
3325 $$ = NODE_SPECIAL_NO_NAME_REST;
3326 /*% %*/
3327 /*% ripper: Qnil %*/
3328 }
3329 ;
3330
3331 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3332 {
3333 $$ = new_args_tail(p, $1, $3, $4, &@3);
3334 }
3335 | f_block_kwarg opt_f_block_arg
3336 {
3337 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3338 }
3339 | f_kwrest opt_f_block_arg
3340 {
3341 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3342 }
3343 | f_no_kwarg opt_f_block_arg
3344 {
3345 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3346 }
3347 | f_block_arg
3348 {
3349 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3350 }
3351 ;
3352
3353 opt_block_args_tail : ',' block_args_tail
3354 {
3355 $$ = $2;
3356 }
3357 | /* none */
3358 {
3359 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3360 }
3361 ;
3362
3363 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3364 {
3365 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3366 }
3367 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3368 {
3369 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3370 }
3371 | f_arg ',' f_block_optarg opt_block_args_tail
3372 {
3373 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3374 }
3375 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3376 {
3377 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3378 }
3379 | f_arg ',' f_rest_arg opt_block_args_tail
3380 {
3381 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3382 }
3383 | f_arg ','
3384 {
3385 /*%%%*/
3386 /* magic number for rest_id in iseq_set_arguments() */
3387 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3388 /*% %*/
3389 /*% ripper: new_args(p, $1, Qnone, excessed_comma!, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL) %*/
3390 }
3391 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3392 {
3393 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3394 }
3395 | f_arg opt_block_args_tail
3396 {
3397 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3398 }
3399 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3400 {
3401 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3402 }
3403 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3404 {
3405 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3406 }
3407 | f_block_optarg opt_block_args_tail
3408 {
3409 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3410 }
3411 | f_block_optarg ',' f_arg opt_block_args_tail
3412 {
3413 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3414 }
3415 | f_rest_arg opt_block_args_tail
3416 {
3417 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3418 }
3419 | f_rest_arg ',' f_arg opt_block_args_tail
3420 {
3421 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3422 }
3423 | block_args_tail
3424 {
3425 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3426 }
3427 ;
3428
3429 opt_block_param : none
3430 | block_param_def
3431 {
3432 p->command_start = TRUE;
3433 }
3434 ;
3435
3436 block_param_def : '|' opt_bv_decl '|'
3437 {
3438 p->cur_arg = 0;
3439 p->max_numparam = ORDINAL_PARAM;
3440 /*%%%*/
3441 $$ = 0;
3442 /*% %*/
3443 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
3444 }
3445 | '|' block_param opt_bv_decl '|'
3446 {
3447 p->cur_arg = 0;
3448 p->max_numparam = ORDINAL_PARAM;
3449 /*%%%*/
3450 $$ = $2;
3451 /*% %*/
3452 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
3453 }
3454 ;
3455
3456
3457 opt_bv_decl : opt_nl
3458 {
3459 $$ = 0;
3460 }
3461 | opt_nl ';' bv_decls opt_nl
3462 {
3463 /*%%%*/
3464 $$ = 0;
3465 /*% %*/
3466 /*% ripper: $3 %*/
3467 }
3468 ;
3469
3470 bv_decls : bvar
3471 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
3472 | bv_decls ',' bvar
3473 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
3474 ;
3475
3476 bvar : tIDENTIFIER
3477 {
3478 new_bv(p, get_id($1));
3479 /*% ripper: get_value($1) %*/
3480 }
3481 | f_bad_arg
3482 {
3483 $$ = 0;
3484 }
3485 ;
3486
3487 lambda : {
3488 $<vars>$ = dyna_push(p);
3489 }
3490 {
3491 $<num>$ = p->lex.lpar_beg;
3492 p->lex.lpar_beg = p->lex.paren_nest;
3493 }
3494 {
3495 $<num>$ = p->max_numparam;
3496 p->max_numparam = 0;
3497 }
3498 {
3499 $<node>$ = numparam_push(p);
3500 }
3501 f_larglist
3502 {
3503 CMDARG_PUSH(0);
3504 }
3505 lambda_body
3506 {
3507 int max_numparam = p->max_numparam;
3508 p->lex.lpar_beg = $<num>2;
3509 p->max_numparam = $<num>3;
3510 CMDARG_POP();
3511 $5 = args_with_numbered(p, $5, max_numparam);
3512 /*%%%*/
3513 {
3514 YYLTYPE loc = code_loc_gen(&@5, &@7);
3515 $$ = NEW_LAMBDA($5, $7, &loc);
3516 nd_set_line($$->nd_body, @7.end_pos.lineno);
3517 nd_set_line($$, @5.end_pos.lineno);
3518 }
3519 /*% %*/
3520 /*% ripper: lambda!($5, $7) %*/
3521 numparam_pop(p, $<node>4);
3522 dyna_pop(p, $<vars>1);
3523 }
3524 ;
3525
3526 f_larglist : '(' f_args opt_bv_decl ')'
3527 {
3528 /*%%%*/
3529 $$ = $2;
3530 p->max_numparam = ORDINAL_PARAM;
3531 /*% %*/
3532 /*% ripper: paren!($2) %*/
3533 }
3534 | f_args
3535 {
3536 /*%%%*/
3537 if (!args_info_empty_p($1->nd_ainfo))
3538 p->max_numparam = ORDINAL_PARAM;
3539 /*% %*/
3540 $$ = $1;
3541 }
3542 ;
3543
3544 lambda_body : tLAMBEG compstmt '}'
3545 {
3546 token_info_pop(p, "}", &@3);
3547 $$ = $2;
3548 }
3549 | keyword_do_LAMBDA bodystmt k_end
3550 {
3551 $$ = $2;
3552 }
3553 ;
3554
3555 do_block : k_do_block do_body k_end
3556 {
3557 $$ = $2;
3558 /*%%%*/
3559 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3560 nd_set_line($$, @1.end_pos.lineno);
3561 /*% %*/
3562 }
3563 ;
3564
3565 block_call : command do_block
3566 {
3567 /*%%%*/
3568 if (nd_type($1) == NODE_YIELD) {
3569 compile_error(p, "block given to yield");
3570 }
3571 else {
3572 block_dup_check(p, $1->nd_args, $2);
3573 }
3574 $$ = method_add_block(p, $1, $2, &@$);
3575 fixpos($$, $1);
3576 /*% %*/
3577 /*% ripper: method_add_block!($1, $2) %*/
3578 }
3579 | block_call call_op2 operation2 opt_paren_args
3580 {
3581 /*%%%*/
3582 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3583 /*% %*/
3584 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3585 }
3586 | block_call call_op2 operation2 opt_paren_args brace_block
3587 {
3588 /*%%%*/
3589 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3590 /*% %*/
3591 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
3592 }
3593 | block_call call_op2 operation2 command_args do_block
3594 {
3595 /*%%%*/
3596 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3597 /*% %*/
3598 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
3599 }
3600 ;
3601
3602 method_call : fcall paren_args
3603 {
3604 /*%%%*/
3605 $$ = $1;
3606 $$->nd_args = $2;
3607 nd_set_last_loc($1, @2.end_pos);
3608 /*% %*/
3609 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
3610 }
3611 | primary_value call_op operation2 opt_paren_args
3612 {
3613 /*%%%*/
3614 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3615 nd_set_line($$, @3.end_pos.lineno);
3616 /*% %*/
3617 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3618 }
3619 | primary_value tCOLON2 operation2 paren_args
3620 {
3621 /*%%%*/
3622 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3623 nd_set_line($$, @3.end_pos.lineno);
3624 /*% %*/
3625 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
3626 }
3627 | primary_value tCOLON2 operation3
3628 {
3629 /*%%%*/
3630 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3631 /*% %*/
3632 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
3633 }
3634 | primary_value call_op paren_args
3635 {
3636 /*%%%*/
3637 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3638 nd_set_line($$, @2.end_pos.lineno);
3639 /*% %*/
3640 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
3641 }
3642 | primary_value tCOLON2 paren_args
3643 {
3644 /*%%%*/
3645 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3646 nd_set_line($$, @2.end_pos.lineno);
3647 /*% %*/
3648 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
3649 }
3650 | keyword_super paren_args
3651 {
3652 /*%%%*/
3653 $$ = NEW_SUPER($2, &@$);
3654 /*% %*/
3655 /*% ripper: super!($2) %*/
3656 }
3657 | keyword_super
3658 {
3659 /*%%%*/
3660 $$ = NEW_ZSUPER(&@$);
3661 /*% %*/
3662 /*% ripper: zsuper! %*/
3663 }
3664 | primary_value '[' opt_call_args rbracket
3665 {
3666 /*%%%*/
3667 if ($1 && nd_type($1) == NODE_SELF)
3668 $$ = NEW_FCALL(tAREF, $3, &@$);
3669 else
3670 $$ = NEW_CALL($1, tAREF, $3, &@$);
3671 fixpos($$, $1);
3672 /*% %*/
3673 /*% ripper: aref!($1, escape_Qundef($3)) %*/
3674 }
3675 ;
3676
3677 brace_block : '{' brace_body '}'
3678 {
3679 $$ = $2;
3680 /*%%%*/
3681 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3682 nd_set_line($$, @1.end_pos.lineno);
3683 /*% %*/
3684 }
3685 | k_do do_body k_end
3686 {
3687 $$ = $2;
3688 /*%%%*/
3689 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3690 nd_set_line($$, @1.end_pos.lineno);
3691 /*% %*/
3692 }
3693 ;
3694
3695 brace_body : {$<vars>$ = dyna_push(p);}
3696 {
3697 $<num>$ = p->max_numparam;
3698 p->max_numparam = 0;
3699 }
3700 {
3701 $<node>$ = numparam_push(p);
3702 }
3703 opt_block_param compstmt
3704 {
3705 int max_numparam = p->max_numparam;
3706 p->max_numparam = $<num>2;
3707 $4 = args_with_numbered(p, $4, max_numparam);
3708 /*%%%*/
3709 $$ = NEW_ITER($4, $5, &@$);
3710 /*% %*/
3711 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
3712 numparam_pop(p, $<node>3);
3713 dyna_pop(p, $<vars>1);
3714 }
3715 ;
3716
3717 do_body : {$<vars>$ = dyna_push(p);}
3718 {
3719 $<num>$ = p->max_numparam;
3720 p->max_numparam = 0;
3721 }
3722 {
3723 $<node>$ = numparam_push(p);
3724 CMDARG_PUSH(0);
3725 }
3726 opt_block_param bodystmt
3727 {
3728 int max_numparam = p->max_numparam;
3729 p->max_numparam = $<num>2;
3730 $4 = args_with_numbered(p, $4, max_numparam);
3731 /*%%%*/
3732 $$ = NEW_ITER($4, $5, &@$);
3733 /*% %*/
3734 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
3735 CMDARG_POP();
3736 numparam_pop(p, $<node>3);
3737 dyna_pop(p, $<vars>1);
3738 }
3739 ;
3740
3741 case_args : arg_value
3742 {
3743 /*%%%*/
3744 check_literal_when(p, $1, &@1);
3745 $$ = NEW_LIST($1, &@$);
3746 /*% %*/
3747 /*% ripper: args_add!(args_new!, $1) %*/
3748 }
3749 | tSTAR arg_value
3750 {
3751 /*%%%*/
3752 $$ = NEW_SPLAT($2, &@$);
3753 /*% %*/
3754 /*% ripper: args_add_star!(args_new!, $2) %*/
3755 }
3756 | case_args ',' arg_value
3757 {
3758 /*%%%*/
3759 check_literal_when(p, $3, &@3);
3760 $$ = last_arg_append(p, $1, $3, &@$);
3761 /*% %*/
3762 /*% ripper: args_add!($1, $3) %*/
3763 }
3764 | case_args ',' tSTAR arg_value
3765 {
3766 /*%%%*/
3767 $$ = rest_arg_append(p, $1, $4, &@$);
3768 /*% %*/
3769 /*% ripper: args_add_star!($1, $4) %*/
3770 }
3771 ;
3772
3773 case_body : k_when case_args then
3774 compstmt
3775 cases
3776 {
3777 /*%%%*/
3778 $$ = NEW_WHEN($2, $4, $5, &@$);
3779 fixpos($$, $2);
3780 /*% %*/
3781 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
3782 }
3783 ;
3784
3785 cases : opt_else
3786 | case_body
3787 ;
3788
3789 p_case_body : keyword_in
3790 {
3791 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3792 p->command_start = FALSE;
3793 $<num>$ = p->in_kwarg;
3794 p->in_kwarg = 1;
3795 }
3796 {$<tbl>$ = push_pvtbl(p);}
3797 {$<tbl>$ = push_pktbl(p);}
3798 p_top_expr then
3799 {pop_pktbl(p, $<tbl>4);}
3800 {pop_pvtbl(p, $<tbl>3);}
3801 {
3802 p->in_kwarg = !!$<num>2;
3803 }
3804 compstmt
3805 p_cases
3806 {
3807 /*%%%*/
3808 $$ = NEW_IN($5, $10, $11, &@$);
3809 /*% %*/
3810 /*% ripper: in!($5, $10, escape_Qundef($11)) %*/
3811 }
3812 ;
3813
3814 p_cases : opt_else
3815 | p_case_body
3816 ;
3817
3818 p_top_expr : p_top_expr_body
3819 | p_top_expr_body modifier_if expr_value
3820 {
3821 /*%%%*/
3822 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3823 fixpos($$, $3);
3824 /*% %*/
3825 /*% ripper: if_mod!($3, $1) %*/
3826 }
3827 | p_top_expr_body modifier_unless expr_value
3828 {
3829 /*%%%*/
3830 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3831 fixpos($$, $3);
3832 /*% %*/
3833 /*% ripper: unless_mod!($3, $1) %*/
3834 }
3835 ;
3836
3837 p_top_expr_body : p_expr
3838 | p_expr ','
3839 {
3840 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3841 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3842 }
3843 | p_expr ',' p_args
3844 {
3845 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3846 /*%%%*/
3847 nd_set_first_loc($$, @1.beg_pos);
3848 /*%
3849 %*/
3850 }
3851 | p_args_tail
3852 {
3853 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3854 }
3855 | p_kwargs
3856 {
3857 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3858 }
3859 ;
3860
3861 p_expr : p_as
3862 ;
3863
3864 p_as : p_expr tASSOC p_variable
3865 {
3866 /*%%%*/
3867 NODE *n = NEW_LIST($1, &@$);
3868 n = list_append(p, n, $3);
3869 $$ = new_hash(p, n, &@$);
3870 /*% %*/
3871 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
3872 }
3873 | p_alt
3874 ;
3875
3876 p_alt : p_alt '|' p_expr_basic
3877 {
3878 /*%%%*/
3879 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3880 /*% %*/
3881 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
3882 }
3883 | p_expr_basic
3884 ;
3885
3886 p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3887 p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3888
3889 p_expr_basic : p_value
3890 | p_const p_lparen p_args rparen
3891 {
3892 pop_pktbl(p, $<tbl>2);
3893 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3894 /*%%%*/
3895 nd_set_first_loc($$, @1.beg_pos);
3896 /*%
3897 %*/
3898 }
3899 | p_const p_lparen p_kwargs rparen
3900 {
3901 pop_pktbl(p, $<tbl>2);
3902 $$ = new_hash_pattern(p, $1, $3, &@$);
3903 /*%%%*/
3904 nd_set_first_loc($$, @1.beg_pos);
3905 /*%
3906 %*/
3907 }
3908 | p_const '(' rparen
3909 {
3910 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3911 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3912 }
3913 | p_const p_lbracket p_args rbracket
3914 {
3915 pop_pktbl(p, $<tbl>2);
3916 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3917 /*%%%*/
3918 nd_set_first_loc($$, @1.beg_pos);
3919 /*%
3920 %*/
3921 }
3922 | p_const p_lbracket p_kwargs rbracket
3923 {
3924 pop_pktbl(p, $<tbl>2);
3925 $$ = new_hash_pattern(p, $1, $3, &@$);
3926 /*%%%*/
3927 nd_set_first_loc($$, @1.beg_pos);
3928 /*%
3929 %*/
3930 }
3931 | p_const '[' rbracket
3932 {
3933 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3934 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3935 }
3936 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3937 {
3938 pop_pktbl(p, $<tbl>2);
3939 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3940 }
3941 | tLBRACK rbracket
3942 {
3943 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3944 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3945 }
3946 | tLBRACE {$<tbl>$ = push_pktbl(p);} p_kwargs '}'
3947 {
3948 pop_pktbl(p, $<tbl>2);
3949 $$ = new_hash_pattern(p, Qnone, $3, &@$);
3950 }
3951 | tLBRACE '}'
3952 {
3953 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
3954 $$ = new_hash_pattern(p, Qnone, $$, &@$);
3955 }
3956 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
3957 {
3958 pop_pktbl(p, $<tbl>2);
3959 $$ = $3;
3960 }
3961 ;
3962
3963 p_args : p_expr
3964 {
3965 /*%%%*/
3966 NODE *pre_args = NEW_LIST($1, &@$);
3967 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3968 /*%
3969 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
3970 %*/
3971 }
3972 | p_args_head
3973 {
3974 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3975 }
3976 | p_args_head p_arg
3977 {
3978 /*%%%*/
3979 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
3980 /*%
3981 VALUE pre_args = rb_ary_concat($1, get_value($2));
3982 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
3983 %*/
3984 }
3985 | p_args_head tSTAR tIDENTIFIER
3986 {
3987 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
3988 }
3989 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
3990 {
3991 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
3992 }
3993 | p_args_head tSTAR
3994 {
3995 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
3996 }
3997 | p_args_head tSTAR ',' p_args_post
3998 {
3999 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4000 }
4001 | p_args_tail
4002 ;
4003
4004 p_args_head : p_arg ','
4005 {
4006 $$ = $1;
4007 }
4008 | p_args_head p_arg ','
4009 {
4010 /*%%%*/
4011 $$ = list_concat($1, $2);
4012 /*% %*/
4013 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4014 }
4015 ;
4016
4017 p_args_tail : tSTAR tIDENTIFIER
4018 {
4019 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4020 }
4021 | tSTAR tIDENTIFIER ',' p_args_post
4022 {
4023 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4024 }
4025 | tSTAR
4026 {
4027 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4028 }
4029 | tSTAR ',' p_args_post
4030 {
4031 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4032 }
4033 ;
4034
4035 p_args_post : p_arg
4036 | p_args_post ',' p_arg
4037 {
4038 /*%%%*/
4039 $$ = list_concat($1, $3);
4040 /*% %*/
4041 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4042 }
4043 ;
4044
4045 p_arg : p_expr
4046 {
4047 /*%%%*/
4048 $$ = NEW_LIST($1, &@$);
4049 /*% %*/
4050 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4051 }
4052 ;
4053
4054 p_kwargs : p_kwarg ',' p_kwrest
4055 {
4056 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4057 }
4058 | p_kwarg
4059 {
4060 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4061 }
4062 | p_kwrest
4063 {
4064 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4065 }
4066 | p_kwarg ',' p_kwnorest
4067 {
4068 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4069 }
4070 | p_kwnorest
4071 {
4072 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4073 }
4074 ;
4075
4076 p_kwarg : p_kw
4077 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4078 | p_kwarg ',' p_kw
4079 {
4080 /*%%%*/
4081 $$ = list_concat($1, $3);
4082 /*% %*/
4083 /*% ripper: rb_ary_push($1, $3) %*/
4084 }
4085 ;
4086
4087 p_kw : p_kw_label p_expr
4088 {
4089 error_duplicate_pattern_key(p, get_id($1), &@1);
4090 /*%%%*/
4091 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4092 /*% %*/
4093 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4094 }
4095 | p_kw_label
4096 {
4097 error_duplicate_pattern_key(p, get_id($1), &@1);
4098 if ($1 && !is_local_id(get_id($1))) {
4099 yyerror1(&@1, "key must be valid as local variables");
4100 }
4101 error_duplicate_pattern_variable(p, get_id($1), &@1);
4102 /*%%%*/
4103 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4104 /*% %*/
4105 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4106 }
4107 ;
4108
4109 p_kw_label : tLABEL
4110 | tSTRING_BEG string_contents tLABEL_END
4111 {
4112 YYLTYPE loc = code_loc_gen(&@1, &@3);
4113 /*%%%*/
4114 if (!$2 || nd_type($2) == NODE_STR) {
4115 NODE *node = dsym_node(p, $2, &loc);
4116 $$ = SYM2ID(node->nd_lit);
4117 }
4118 /*%
4119 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4120 VALUE label = RNODE($2)->nd_cval;
4121 VALUE rval = RNODE($2)->nd_rval;
4122 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4123 RNODE($$)->nd_loc = loc;
4124 }
4125 %*/
4126 else {
4127 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4128 $$ = 0;
4129 }
4130 }
4131 ;
4132
4133 p_kwrest : kwrest_mark tIDENTIFIER
4134 {
4135 $$ = $2;
4136 }
4137 | kwrest_mark
4138 {
4139 $$ = 0;
4140 }
4141 ;
4142
4143 p_kwnorest : kwrest_mark keyword_nil
4144 {
4145 $$ = 0;
4146 }
4147 ;
4148
4149 p_value : p_primitive
4150 | p_primitive tDOT2 p_primitive
4151 {
4152 /*%%%*/
4153 value_expr($1);
4154 value_expr($3);
4155 $$ = NEW_DOT2($1, $3, &@$);
4156 /*% %*/
4157 /*% ripper: dot2!($1, $3) %*/
4158 }
4159 | p_primitive tDOT3 p_primitive
4160 {
4161 /*%%%*/
4162 value_expr($1);
4163 value_expr($3);
4164 $$ = NEW_DOT3($1, $3, &@$);
4165 /*% %*/
4166 /*% ripper: dot3!($1, $3) %*/
4167 }
4168 | p_primitive tDOT2
4169 {
4170 /*%%%*/
4171 YYLTYPE loc;
4172 loc.beg_pos = @2.end_pos;
4173 loc.end_pos = @2.end_pos;
4174
4175 value_expr($1);
4176 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4177 /*% %*/
4178 /*% ripper: dot2!($1, Qnil) %*/
4179 }
4180 | p_primitive tDOT3
4181 {
4182 /*%%%*/
4183 YYLTYPE loc;
4184 loc.beg_pos = @2.end_pos;
4185 loc.end_pos = @2.end_pos;
4186
4187 value_expr($1);
4188 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4189 /*% %*/
4190 /*% ripper: dot3!($1, Qnil) %*/
4191 }
4192 | p_variable
4193 | p_var_ref
4194 | p_const
4195 | tBDOT2 p_primitive
4196 {
4197 /*%%%*/
4198 YYLTYPE loc;
4199 loc.beg_pos = @1.beg_pos;
4200 loc.end_pos = @1.beg_pos;
4201
4202 value_expr($2);
4203 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4204 /*% %*/
4205 /*% ripper: dot2!(Qnil, $2) %*/
4206 }
4207 | tBDOT3 p_primitive
4208 {
4209 /*%%%*/
4210 YYLTYPE loc;
4211 loc.beg_pos = @1.beg_pos;
4212 loc.end_pos = @1.beg_pos;
4213
4214 value_expr($2);
4215 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4216 /*% %*/
4217 /*% ripper: dot3!(Qnil, $2) %*/
4218 }
4219 ;
4220
4221 p_primitive : literal
4222 | strings
4223 | xstring
4224 | regexp
4225 | words
4226 | qwords
4227 | symbols
4228 | qsymbols
4229 | keyword_variable
4230 {
4231 /*%%%*/
4232 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4233 /*% %*/
4234 /*% ripper: var_ref!($1) %*/
4235 }
4236 | tLAMBDA
4237 {
4238 token_info_push(p, "->", &@1);
4239 }
4240 lambda
4241 {
4242 $$ = $3;
4243 /*%%%*/
4244 nd_set_first_loc($$, @1.beg_pos);
4245 /*% %*/
4246 }
4247 ;
4248
4249 p_variable : tIDENTIFIER
4250 {
4251 /*%%%*/
4252 error_duplicate_pattern_variable(p, $1, &@1);
4253 $$ = assignable(p, $1, 0, &@$);
4254 /*% %*/
4255 /*% ripper: assignable(p, var_field(p, $1)) %*/
4256 }
4257 ;
4258
4259 p_var_ref : '^' tIDENTIFIER
4260 {
4261 /*%%%*/
4262 NODE *n = gettable(p, $2, &@$);
4263 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4264 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4265 }
4266 $$ = n;
4267 /*% %*/
4268 /*% ripper: var_ref!($2) %*/
4269 }
4270 ;
4271
4272 p_const : tCOLON3 cname
4273 {
4274 /*%%%*/
4275 $$ = NEW_COLON3($2, &@$);
4276 /*% %*/
4277 /*% ripper: top_const_ref!($2) %*/
4278 }
4279 | p_const tCOLON2 cname
4280 {
4281 /*%%%*/
4282 $$ = NEW_COLON2($1, $3, &@$);
4283 /*% %*/
4284 /*% ripper: const_path_ref!($1, $3) %*/
4285 }
4286 | tCONSTANT
4287 {
4288 /*%%%*/
4289 $$ = gettable(p, $1, &@$);
4290 /*% %*/
4291 /*% ripper: var_ref!($1) %*/
4292 }
4293 ;
4294
4295 opt_rescue : k_rescue exc_list exc_var then
4296 compstmt
4297 opt_rescue
4298 {
4299 /*%%%*/
4300 $$ = NEW_RESBODY($2,
4301 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4302 $6, &@$);
4303 fixpos($$, $2?$2:$5);
4304 /*% %*/
4305 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4306 }
4307 | none
4308 ;
4309
4310 exc_list : arg_value
4311 {
4312 /*%%%*/
4313 $$ = NEW_LIST($1, &@$);
4314 /*% %*/
4315 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4316 }
4317 | mrhs
4318 {
4319 /*%%%*/
4320 if (!($$ = splat_array($1))) $$ = $1;
4321 /*% %*/
4322 /*% ripper: $1 %*/
4323 }
4324 | none
4325 ;
4326
4327 exc_var : tASSOC lhs
4328 {
4329 $$ = $2;
4330 }
4331 | none
4332 ;
4333
4334 opt_ensure : k_ensure compstmt
4335 {
4336 /*%%%*/
4337 $$ = $2;
4338 /*% %*/
4339 /*% ripper: ensure!($2) %*/
4340 }
4341 | none
4342 ;
4343
4344 literal : numeric
4345 | symbol
4346 ;
4347
4348 strings : string
4349 {
4350 /*%%%*/
4351 NODE *node = $1;
4352 if (!node) {
4353 node = NEW_STR(STR_NEW0(), &@$);
4354 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4355 }
4356 else {
4357 node = evstr2dstr(p, node);
4358 }
4359 $$ = node;
4360 /*% %*/
4361 /*% ripper: $1 %*/
4362 }
4363 ;
4364
4365 string : tCHAR
4366 | string1
4367 | string string1
4368 {
4369 /*%%%*/
4370 $$ = literal_concat(p, $1, $2, &@$);
4371 /*% %*/
4372 /*% ripper: string_concat!($1, $2) %*/
4373 }
4374 ;
4375
4376 string1 : tSTRING_BEG string_contents tSTRING_END
4377 {
4378 /*%%%*/
4379 $$ = heredoc_dedent(p, $2);
4380 if ($$) nd_set_loc($$, &@$);
4381 /*% %*/
4382 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4383 }
4384 ;
4385
4386 xstring : tXSTRING_BEG xstring_contents tSTRING_END
4387 {
4388 /*%%%*/
4389 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4390 /*% %*/
4391 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4392 }
4393 ;
4394
4395 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4396 {
4397 $$ = new_regexp(p, $2, $3, &@$);
4398 }
4399 ;
4400
4401 words : tWORDS_BEG ' ' word_list tSTRING_END
4402 {
4403 /*%%%*/
4404 $$ = make_list($3, &@$);
4405 /*% %*/
4406 /*% ripper: array!($3) %*/
4407 }
4408 ;
4409
4410 word_list : /* none */
4411 {
4412 /*%%%*/
4413 $$ = 0;
4414 /*% %*/
4415 /*% ripper: words_new! %*/
4416 }
4417 | word_list word ' '
4418 {
4419 /*%%%*/
4420 $$ = list_append(p, $1, evstr2dstr(p, $2));
4421 /*% %*/
4422 /*% ripper: words_add!($1, $2) %*/
4423 }
4424 ;
4425
4426 word : string_content
4427 /*% ripper[brace]: word_add!(word_new!, $1) %*/
4428 | word string_content
4429 {
4430 /*%%%*/
4431 $$ = literal_concat(p, $1, $2, &@$);
4432 /*% %*/
4433 /*% ripper: word_add!($1, $2) %*/
4434 }
4435 ;
4436
4437 symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4438 {
4439 /*%%%*/
4440 $$ = make_list($3, &@$);
4441 /*% %*/
4442 /*% ripper: array!($3) %*/
4443 }
4444 ;
4445
4446 symbol_list : /* none */
4447 {
4448 /*%%%*/
4449 $$ = 0;
4450 /*% %*/
4451 /*% ripper: symbols_new! %*/
4452 }
4453 | symbol_list word ' '
4454 {
4455 /*%%%*/
4456 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4457 /*% %*/
4458 /*% ripper: symbols_add!($1, $2) %*/
4459 }
4460 ;
4461
4462 qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4463 {
4464 /*%%%*/
4465 $$ = make_list($3, &@$);
4466 /*% %*/
4467 /*% ripper: array!($3) %*/
4468 }
4469 ;
4470
4471 qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4472 {
4473 /*%%%*/
4474 $$ = make_list($3, &@$);
4475 /*% %*/
4476 /*% ripper: array!($3) %*/
4477 }
4478 ;
4479
4480 qword_list : /* none */
4481 {
4482 /*%%%*/
4483 $$ = 0;
4484 /*% %*/
4485 /*% ripper: qwords_new! %*/
4486 }
4487 | qword_list tSTRING_CONTENT ' '
4488 {
4489 /*%%%*/
4490 $$ = list_append(p, $1, $2);
4491 /*% %*/
4492 /*% ripper: qwords_add!($1, $2) %*/
4493 }
4494 ;
4495
4496 qsym_list : /* none */
4497 {
4498 /*%%%*/
4499 $$ = 0;
4500 /*% %*/
4501 /*% ripper: qsymbols_new! %*/
4502 }
4503 | qsym_list tSTRING_CONTENT ' '
4504 {
4505 /*%%%*/
4506 $$ = symbol_append(p, $1, $2);
4507 /*% %*/
4508 /*% ripper: qsymbols_add!($1, $2) %*/
4509 }
4510 ;
4511
4512 string_contents : /* none */
4513 {
4514 /*%%%*/
4515 $$ = 0;
4516 /*% %*/
4517 /*% ripper: string_content! %*/
4518 /*%%%*/
4519 /*%
4520 $$ = ripper_new_yylval(p, 0, $$, 0);
4521 %*/
4522 }
4523 | string_contents string_content
4524 {
4525 /*%%%*/
4526 $$ = literal_concat(p, $1, $2, &@$);
4527 /*% %*/
4528 /*% ripper: string_add!($1, $2) %*/
4529 /*%%%*/
4530 /*%
4531 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4532 !RNODE($1)->nd_cval) {
4533 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4534 RNODE($1)->nd_rval = add_mark_object(p, $$);
4535 $$ = $1;
4536 }
4537 %*/
4538 }
4539 ;
4540
4541 xstring_contents: /* none */
4542 {
4543 /*%%%*/
4544 $$ = 0;
4545 /*% %*/
4546 /*% ripper: xstring_new! %*/
4547 }
4548 | xstring_contents string_content
4549 {
4550 /*%%%*/
4551 $$ = literal_concat(p, $1, $2, &@$);
4552 /*% %*/
4553 /*% ripper: xstring_add!($1, $2) %*/
4554 }
4555 ;
4556
4557 regexp_contents: /* none */
4558 {
4559 /*%%%*/
4560 $$ = 0;
4561 /*% %*/
4562 /*% ripper: regexp_new! %*/
4563 /*%%%*/
4564 /*%
4565 $$ = ripper_new_yylval(p, 0, $$, 0);
4566 %*/
4567 }
4568 | regexp_contents string_content
4569 {
4570 /*%%%*/
4571 NODE *head = $1, *tail = $2;
4572 if (!head) {
4573 $$ = tail;
4574 }
4575 else if (!tail) {
4576 $$ = head;
4577 }
4578 else {
4579 switch (nd_type(head)) {
4580 case NODE_STR:
4581 nd_set_type(head, NODE_DSTR);
4582 break;
4583 case NODE_DSTR:
4584 break;
4585 default:
4586 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4587 break;
4588 }
4589 $$ = list_append(p, head, tail);
4590 }
4591 /*%
4592 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4593 if (ripper_is_node_yylval(n1)) {
4594 s1 = RNODE(n1)->nd_cval;
4595 n1 = RNODE(n1)->nd_rval;
4596 }
4597 if (ripper_is_node_yylval(n2)) {
4598 s2 = RNODE(n2)->nd_cval;
4599 n2 = RNODE(n2)->nd_rval;
4600 }
4601 $$ = dispatch2(regexp_add, n1, n2);
4602 if (!s1 && s2) {
4603 $$ = ripper_new_yylval(p, 0, $$, s2);
4604 }
4605 %*/
4606 }
4607 ;
4608
4609 string_content : tSTRING_CONTENT
4610 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
4611 | tSTRING_DVAR
4612 {
4613 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4614 $<strterm>$ = p->lex.strterm;
4615 p->lex.strterm = 0;
4616 SET_LEX_STATE(EXPR_BEG);
4617 }
4618 string_dvar
4619 {
4620 p->lex.strterm = $<strterm>2;
4621 /*%%%*/
4622 $$ = NEW_EVSTR($3, &@$);
4623 nd_set_line($$, @3.end_pos.lineno);
4624 /*% %*/
4625 /*% ripper: string_dvar!($3) %*/
4626 }
4627 | tSTRING_DBEG
4628 {
4629 CMDARG_PUSH(0);
4630 COND_PUSH(0);
4631 }
4632 {
4633 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4634 $<strterm>$ = p->lex.strterm;
4635 p->lex.strterm = 0;
4636 }
4637 {
4638 $<num>$ = p->lex.state;
4639 SET_LEX_STATE(EXPR_BEG);
4640 }
4641 {
4642 $<num>$ = p->lex.brace_nest;
4643 p->lex.brace_nest = 0;
4644 }
4645 {
4646 $<num>$ = p->heredoc_indent;
4647 p->heredoc_indent = 0;
4648 }
4649 compstmt tSTRING_DEND
4650 {
4651 COND_POP();
4652 CMDARG_POP();
4653 p->lex.strterm = $<strterm>3;
4654 SET_LEX_STATE($<num>4);
4655 p->lex.brace_nest = $<num>5;
4656 p->heredoc_indent = $<num>6;
4657 p->heredoc_line_indent = -1;
4658 /*%%%*/
4659 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4660 $$ = new_evstr(p, $7, &@$);
4661 /*% %*/
4662 /*% ripper: string_embexpr!($7) %*/
4663 }
4664 ;
4665
4666 string_dvar : tGVAR
4667 {
4668 /*%%%*/
4669 $$ = NEW_GVAR($1, &@$);
4670 /*% %*/
4671 /*% ripper: var_ref!($1) %*/
4672 }
4673 | tIVAR
4674 {
4675 /*%%%*/
4676 $$ = NEW_IVAR($1, &@$);
4677 /*% %*/
4678 /*% ripper: var_ref!($1) %*/
4679 }
4680 | tCVAR
4681 {
4682 /*%%%*/
4683 $$ = NEW_CVAR($1, &@$);
4684 /*% %*/
4685 /*% ripper: var_ref!($1) %*/
4686 }
4687 | backref
4688 ;
4689
4690 symbol : ssym
4691 | dsym
4692 ;
4693
4694 ssym : tSYMBEG sym
4695 {
4696 SET_LEX_STATE(EXPR_END);
4697 /*%%%*/
4698 $$ = NEW_LIT(ID2SYM($2), &@$);
4699 /*% %*/
4700 /*% ripper: symbol_literal!(symbol!($2)) %*/
4701 }
4702 ;
4703
4704 sym : fname
4705 | tIVAR
4706 | tGVAR
4707 | tCVAR
4708 ;
4709
4710 dsym : tSYMBEG string_contents tSTRING_END
4711 {
4712 SET_LEX_STATE(EXPR_END);
4713 /*%%%*/
4714 $$ = dsym_node(p, $2, &@$);
4715 /*% %*/
4716 /*% ripper: dyna_symbol!($2) %*/
4717 }
4718 ;
4719
4720 numeric : simple_numeric
4721 | tUMINUS_NUM simple_numeric %prec tLOWEST
4722 {
4723 /*%%%*/
4724 $$ = $2;
4725 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4726 /*% %*/
4727 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
4728 }
4729 ;
4730
4731 simple_numeric : tINTEGER
4732 | tFLOAT
4733 | tRATIONAL
4734 | tIMAGINARY
4735 ;
4736
4737 user_variable : tIDENTIFIER
4738 | tIVAR
4739 | tGVAR
4740 | tCONSTANT
4741 | tCVAR
4742 ;
4743
4744 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4745 | keyword_self {$$ = KWD2EID(self, $1);}
4746 | keyword_true {$$ = KWD2EID(true, $1);}
4747 | keyword_false {$$ = KWD2EID(false, $1);}
4748 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4749 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4750 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4751 ;
4752
4753 var_ref : user_variable
4754 {
4755 /*%%%*/
4756 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4757 /*%
4758 if (id_is_var(p, get_id($1))) {
4759 $$ = dispatch1(var_ref, $1);
4760 }
4761 else {
4762 $$ = dispatch1(vcall, $1);
4763 }
4764 %*/
4765 }
4766 | keyword_variable
4767 {
4768 /*%%%*/
4769 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4770 /*% %*/
4771 /*% ripper: var_ref!($1) %*/
4772 }
4773 ;
4774
4775 var_lhs : user_variable
4776 {
4777 /*%%%*/
4778 $$ = assignable(p, $1, 0, &@$);
4779 /*% %*/
4780 /*% ripper: assignable(p, var_field(p, $1)) %*/
4781 }
4782 | keyword_variable
4783 {
4784 /*%%%*/
4785 $$ = assignable(p, $1, 0, &@$);
4786 /*% %*/
4787 /*% ripper: assignable(p, var_field(p, $1)) %*/
4788 }
4789 ;
4790
4791 backref : tNTH_REF
4792 | tBACK_REF
4793 ;
4794
4795 superclass : '<'
4796 {
4797 SET_LEX_STATE(EXPR_BEG);
4798 p->command_start = TRUE;
4799 }
4800 expr_value term
4801 {
4802 $$ = $3;
4803 }
4804 | /* none */
4805 {
4806 /*%%%*/
4807 $$ = 0;
4808 /*% %*/
4809 /*% ripper: Qnil %*/
4810 }
4811 ;
4812
4813 f_arglist : '(' f_args rparen
4814 {
4815 /*%%%*/
4816 $$ = $2;
4817 /*% %*/
4818 /*% ripper: paren!($2) %*/
4819 SET_LEX_STATE(EXPR_BEG);
4820 p->command_start = TRUE;
4821 }
4822 | '(' args_forward rparen
4823 {
4824 arg_var(p, idFWD_REST);
4825 #if idFWD_KWREST
4826 arg_var(p, idFWD_KWREST);
4827 #endif
4828 arg_var(p, idFWD_BLOCK);
4829 /*%%%*/
4830 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4831 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4832 /*% %*/
4833 /*% ripper: paren!(params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone)) %*/
4834 SET_LEX_STATE(EXPR_BEG);
4835 p->command_start = TRUE;
4836 }
4837 | {
4838 $<num>$ = p->in_kwarg;
4839 p->in_kwarg = 1;
4840 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4841 }
4842 f_args term
4843 {
4844 p->in_kwarg = !!$<num>1;
4845 $$ = $2;
4846 SET_LEX_STATE(EXPR_BEG);
4847 p->command_start = TRUE;
4848 }
4849 ;
4850
4851 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4852 {
4853 $$ = new_args_tail(p, $1, $3, $4, &@3);
4854 }
4855 | f_kwarg opt_f_block_arg
4856 {
4857 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4858 }
4859 | f_kwrest opt_f_block_arg
4860 {
4861 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4862 }
4863 | f_no_kwarg opt_f_block_arg
4864 {
4865 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4866 }
4867 | f_block_arg
4868 {
4869 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4870 }
4871 ;
4872
4873 opt_args_tail : ',' args_tail
4874 {
4875 $$ = $2;
4876 }
4877 | /* none */
4878 {
4879 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4880 }
4881 ;
4882
4883 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4884 {
4885 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4886 }
4887 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4888 {
4889 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4890 }
4891 | f_arg ',' f_optarg opt_args_tail
4892 {
4893 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4894 }
4895 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4896 {
4897 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4898 }
4899 | f_arg ',' f_rest_arg opt_args_tail
4900 {
4901 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4902 }
4903 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4904 {
4905 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4906 }
4907 | f_arg opt_args_tail
4908 {
4909 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4910 }
4911 | f_optarg ',' f_rest_arg opt_args_tail
4912 {
4913 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4914 }
4915 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4916 {
4917 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4918 }
4919 | f_optarg opt_args_tail
4920 {
4921 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4922 }
4923 | f_optarg ',' f_arg opt_args_tail
4924 {
4925 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4926 }
4927 | f_rest_arg opt_args_tail
4928 {
4929 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4930 }
4931 | f_rest_arg ',' f_arg opt_args_tail
4932 {
4933 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4934 }
4935 | args_tail
4936 {
4937 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4938 }
4939 | /* none */
4940 {
4941 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4942 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
4943 }
4944 ;
4945
4946 args_forward : tBDOT3
4947 {
4948 /*%%%*/
4949 $$ = idDot3;
4950 /*% %*/
4951 /*% ripper: args_forward! %*/
4952 }
4953 ;
4954
4955 f_bad_arg : tCONSTANT
4956 {
4957 /*%%%*/
4958 yyerror1(&@1, "formal argument cannot be a constant");
4959 $$ = 0;
4960 /*% %*/
4961 /*% ripper[error]: param_error!($1) %*/
4962 }
4963 | tIVAR
4964 {
4965 /*%%%*/
4966 yyerror1(&@1, "formal argument cannot be an instance variable");
4967 $$ = 0;
4968 /*% %*/
4969 /*% ripper[error]: param_error!($1) %*/
4970 }
4971 | tGVAR
4972 {
4973 /*%%%*/
4974 yyerror1(&@1, "formal argument cannot be a global variable");
4975 $$ = 0;
4976 /*% %*/
4977 /*% ripper[error]: param_error!($1) %*/
4978 }
4979 | tCVAR
4980 {
4981 /*%%%*/
4982 yyerror1(&@1, "formal argument cannot be a class variable");
4983 $$ = 0;
4984 /*% %*/
4985 /*% ripper[error]: param_error!($1) %*/
4986 }
4987 ;
4988
4989 f_norm_arg : f_bad_arg
4990 | tIDENTIFIER
4991 {
4992 formal_argument(p, get_id($1));
4993 p->max_numparam = ORDINAL_PARAM;
4994 $$ = $1;
4995 }
4996 ;
4997
4998 f_arg_asgn : f_norm_arg
4999 {
5000 ID id = get_id($1);
5001 arg_var(p, id);
5002 p->cur_arg = id;
5003 $$ = $1;
5004 }
5005 ;
5006
5007 f_arg_item : f_arg_asgn
5008 {
5009 p->cur_arg = 0;
5010 /*%%%*/
5011 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5012 /*% %*/
5013 /*% ripper: get_value($1) %*/
5014 }
5015 | tLPAREN f_margs rparen
5016 {
5017 /*%%%*/
5018 ID tid = internal_id(p);
5019 YYLTYPE loc;
5020 loc.beg_pos = @2.beg_pos;
5021 loc.end_pos = @2.beg_pos;
5022 arg_var(p, tid);
5023 if (dyna_in_block(p)) {
5024 $2->nd_value = NEW_DVAR(tid, &loc);
5025 }
5026 else {
5027 $2->nd_value = NEW_LVAR(tid, &loc);
5028 }
5029 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5030 $$->nd_next = $2;
5031 /*% %*/
5032 /*% ripper: mlhs_paren!($2) %*/
5033 }
5034 ;
5035
5036 f_arg : f_arg_item
5037 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5038 | f_arg ',' f_arg_item
5039 {
5040 /*%%%*/
5041 $$ = $1;
5042 $$->nd_plen++;
5043 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5044 rb_discard_node(p, $3);
5045 /*% %*/
5046 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5047 }
5048 ;
5049
5050
5051 f_label : tLABEL
5052 {
5053 ID id = get_id($1);
5054 arg_var(p, formal_argument(p, id));
5055 p->cur_arg = id;
5056 p->max_numparam = ORDINAL_PARAM;
5057 $$ = $1;
5058 }
5059 ;
5060
5061 f_kw : f_label arg_value
5062 {
5063 p->cur_arg = 0;
5064 /*%%%*/
5065 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5066 /*% %*/
5067 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5068 }
5069 | f_label
5070 {
5071 p->cur_arg = 0;
5072 /*%%%*/
5073 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5074 /*% %*/
5075 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5076 }
5077 ;
5078
5079 f_block_kw : f_label primary_value
5080 {
5081 /*%%%*/
5082 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5083 /*% %*/
5084 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5085 }
5086 | f_label
5087 {
5088 /*%%%*/
5089 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5090 /*% %*/
5091 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5092 }
5093 ;
5094
5095 f_block_kwarg : f_block_kw
5096 {
5097 /*%%%*/
5098 $$ = $1;
5099 /*% %*/
5100 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5101 }
5102 | f_block_kwarg ',' f_block_kw
5103 {
5104 /*%%%*/
5105 $$ = kwd_append($1, $3);
5106 /*% %*/
5107 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5108 }
5109 ;
5110
5111
5112 f_kwarg : f_kw
5113 {
5114 /*%%%*/
5115 $$ = $1;
5116 /*% %*/
5117 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5118 }
5119 | f_kwarg ',' f_kw
5120 {
5121 /*%%%*/
5122 $$ = kwd_append($1, $3);
5123 /*% %*/
5124 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5125 }
5126 ;
5127
5128 kwrest_mark : tPOW
5129 | tDSTAR
5130 ;
5131
5132 f_no_kwarg : kwrest_mark keyword_nil
5133 {
5134 /*%%%*/
5135 /*% %*/
5136 /*% ripper: nokw_param!(Qnil) %*/
5137 }
5138 ;
5139
5140 f_kwrest : kwrest_mark tIDENTIFIER
5141 {
5142 arg_var(p, shadowing_lvar(p, get_id($2)));
5143 /*%%%*/
5144 $$ = $2;
5145 /*% %*/
5146 /*% ripper: kwrest_param!($2) %*/
5147 }
5148 | kwrest_mark
5149 {
5150 /*%%%*/
5151 $$ = internal_id(p);
5152 arg_var(p, $$);
5153 /*% %*/
5154 /*% ripper: kwrest_param!(Qnil) %*/
5155 }
5156 ;
5157
5158 f_opt : f_arg_asgn '=' arg_value
5159 {
5160 p->cur_arg = 0;
5161 /*%%%*/
5162 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5163 /*% %*/
5164 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5165 }
5166 ;
5167
5168 f_block_opt : f_arg_asgn '=' primary_value
5169 {
5170 p->cur_arg = 0;
5171 /*%%%*/
5172 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5173 /*% %*/
5174 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5175 }
5176 ;
5177
5178 f_block_optarg : f_block_opt
5179 {
5180 /*%%%*/
5181 $$ = $1;
5182 /*% %*/
5183 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5184 }
5185 | f_block_optarg ',' f_block_opt
5186 {
5187 /*%%%*/
5188 $$ = opt_arg_append($1, $3);
5189 /*% %*/
5190 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5191 }
5192 ;
5193
5194 f_optarg : f_opt
5195 {
5196 /*%%%*/
5197 $$ = $1;
5198 /*% %*/
5199 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5200 }
5201 | f_optarg ',' f_opt
5202 {
5203 /*%%%*/
5204 $$ = opt_arg_append($1, $3);
5205 /*% %*/
5206 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5207 }
5208 ;
5209
5210 restarg_mark : '*'
5211 | tSTAR
5212 ;
5213
5214 f_rest_arg : restarg_mark tIDENTIFIER
5215 {
5216 arg_var(p, shadowing_lvar(p, get_id($2)));
5217 /*%%%*/
5218 $$ = $2;
5219 /*% %*/
5220 /*% ripper: rest_param!($2) %*/
5221 }
5222 | restarg_mark
5223 {
5224 /*%%%*/
5225 $$ = internal_id(p);
5226 arg_var(p, $$);
5227 /*% %*/
5228 /*% ripper: rest_param!(Qnil) %*/
5229 }
5230 ;
5231
5232 blkarg_mark : '&'
5233 | tAMPER
5234 ;
5235
5236 f_block_arg : blkarg_mark tIDENTIFIER
5237 {
5238 arg_var(p, shadowing_lvar(p, get_id($2)));
5239 /*%%%*/
5240 $$ = $2;
5241 /*% %*/
5242 /*% ripper: blockarg!($2) %*/
5243 }
5244 ;
5245
5246 opt_f_block_arg : ',' f_block_arg
5247 {
5248 $$ = $2;
5249 }
5250 | none
5251 {
5252 $$ = Qnull;
5253 }
5254 ;
5255
5256 singleton : var_ref
5257 {
5258 value_expr($1);
5259 $$ = $1;
5260 }
5261 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5262 {
5263 /*%%%*/
5264 switch (nd_type($3)) {
5265 case NODE_STR:
5266 case NODE_DSTR:
5267 case NODE_XSTR:
5268 case NODE_DXSTR:
5269 case NODE_DREGX:
5270 case NODE_LIT:
5271 case NODE_LIST:
5272 case NODE_ZLIST:
5273 yyerror1(&@3, "can't define singleton method for literals");
5274 break;
5275 default:
5276 value_expr($3);
5277 break;
5278 }
5279 $$ = $3;
5280 /*% %*/
5281 /*% ripper: paren!($3) %*/
5282 }
5283 ;
5284
5285 assoc_list : none
5286 | assocs trailer
5287 {
5288 /*%%%*/
5289 $$ = $1;
5290 /*% %*/
5291 /*% ripper: assoclist_from_args!($1) %*/
5292 }
5293 ;
5294
5295 assocs : assoc
5296 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5297 | assocs ',' assoc
5298 {
5299 /*%%%*/
5300 NODE *assocs = $1;
5301 NODE *tail = $3;
5302 if (!assocs) {
5303 assocs = tail;
5304 }
5305 else if (tail) {
5306 if (assocs->nd_head &&
5307 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5308 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5309 /* DSTAR */
5310 tail = tail->nd_next->nd_head->nd_head;
5311 }
5312 assocs = list_concat(assocs, tail);
5313 }
5314 $$ = assocs;
5315 /*% %*/
5316 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5317 }
5318 ;
5319
5320 assoc : arg_value tASSOC arg_value
5321 {
5322 /*%%%*/
5323 if (nd_type($1) == NODE_STR) {
5324 nd_set_type($1, NODE_LIT);
5325 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5326 }
5327 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5328 /*% %*/
5329 /*% ripper: assoc_new!($1, $3) %*/
5330 }
5331 | tLABEL arg_value
5332 {
5333 /*%%%*/
5334 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5335 /*% %*/
5336 /*% ripper: assoc_new!($1, $2) %*/
5337 }
5338 | tSTRING_BEG string_contents tLABEL_END arg_value
5339 {
5340 /*%%%*/
5341 YYLTYPE loc = code_loc_gen(&@1, &@3);
5342 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5343 /*% %*/
5344 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
5345 }
5346 | tDSTAR arg_value
5347 {
5348 /*%%%*/
5349 if (nd_type($2) == NODE_HASH &&
5350 !($2->nd_head && $2->nd_head->nd_alen)) {
5351 static VALUE empty_hash;
5352 if (!empty_hash) {
5353 empty_hash = rb_obj_freeze(rb_hash_new());
5354 rb_gc_register_mark_object(empty_hash);
5355 }
5356 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5357 }
5358 else
5359 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5360 /*% %*/
5361 /*% ripper: assoc_splat!($2) %*/
5362 }
5363 ;
5364
5365 operation : tIDENTIFIER
5366 | tCONSTANT
5367 | tFID
5368 ;
5369
5370 operation2 : tIDENTIFIER
5371 | tCONSTANT
5372 | tFID
5373 | op
5374 ;
5375
5376 operation3 : tIDENTIFIER
5377 | tFID
5378 | op
5379 ;
5380
5381 dot_or_colon : '.'
5382 | tCOLON2
5383 ;
5384
5385 call_op : '.'
5386 | tANDDOT
5387 ;
5388
5389 call_op2 : call_op
5390 | tCOLON2
5391 ;
5392
5393 opt_terms : /* none */
5394 | terms
5395 ;
5396
5397 opt_nl : /* none */
5398 | '\n'
5399 ;
5400
5401 rparen : opt_nl ')'
5402 ;
5403
5404 rbracket : opt_nl ']'
5405 ;
5406
5407 trailer : /* none */
5408 | '\n'
5409 | ','
5410 ;
5411
5412 term : ';' {yyerrok;token_flush(p);}
5413 | '\n' {token_flush(p);}
5414 ;
5415
5416 terms : term
5417 | terms ';' {yyerrok;}
5418 ;
5419
5420 none : /* none */
5421 {
5422 $$ = Qnull;
5423 }
5424 ;
5425 %%
5426 # undef p
5427 # undef yylex
5428 # undef yylval
5429 # define yylval (*p->lval)
5430
5431 static int regx_options(struct parser_params*);
5432 static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5433 static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5434 static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5435 static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5436
5437 #ifndef RIPPER
5438 # define set_yylval_node(x) { \
5439 YYLTYPE _cur_loc; \
5440 rb_parser_set_location(p, &_cur_loc); \
5441 yylval.node = (x); \
5442 }
5443 # define set_yylval_str(x) \
5444 do { \
5445 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5446 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5447 } while(0)
5448 # define set_yylval_literal(x) \
5449 do { \
5450 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5451 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5452 } while(0)
5453 # define set_yylval_num(x) (yylval.num = (x))
5454 # define set_yylval_id(x) (yylval.id = (x))
5455 # define set_yylval_name(x) (yylval.id = (x))
5456 # define yylval_id() (yylval.id)
5457 #else
5458 static inline VALUE
5459 ripper_yylval_id(struct parser_params *p, ID x)
5460 {
5461 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5462 }
5463 # define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5464 # define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5465 # define set_yylval_id(x) (void)(x)
5466 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5467 # define set_yylval_literal(x) add_mark_object(p, (x))
5468 # define set_yylval_node(x) (void)(x)
5469 # define yylval_id() yylval.id
5470 # define _cur_loc NULL_LOC /* dummy */
5471 #endif
5472
5473 #define set_yylval_noname() set_yylval_id(keyword_nil)
5474
5475 #ifndef RIPPER
5476 #define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5477 #define dispatch_scan_event(p, t) ((void)0)
5478 #define dispatch_delayed_token(p, t) ((void)0)
5479 #define has_delayed_token(p) (0)
5480 #else
5481 #define literal_flush(p, ptr) ((void)(ptr))
5482
5483 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5484
5485 static inline VALUE
5486 intern_sym(const char *name)
5487 {
5488 ID id = rb_intern_const(name);
5489 return ID2SYM(id);
5490 }
5491
5492 static int
5493 ripper_has_scan_event(struct parser_params *p)
5494 {
5495 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5496 return p->lex.pcur > p->lex.ptok;
5497 }
5498
5499 static VALUE
5500 ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5501 {
5502 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5503 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5504 token_flush(p);
5505 return rval;
5506 }
5507
5508 static void
5509 ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5510 {
5511 if (!ripper_has_scan_event(p)) return;
5512 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5513 }
5514 #define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5515
5516 static void
5517 ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5518 {
5519 int saved_line = p->ruby_sourceline;
5520 const char *saved_tokp = p->lex.ptok;
5521
5522 if (NIL_P(p->delayed.token)) return;
5523 p->ruby_sourceline = p->delayed.line;
5524 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5525 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5526 p->delayed.token = Qnil;
5527 p->ruby_sourceline = saved_line;
5528 p->lex.ptok = saved_tokp;
5529 }
5530 #define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5531 #define has_delayed_token(p) (!NIL_P(p->delayed.token))
5532 #endif /* RIPPER */
5533
5534 #include "ruby/regex.h"
5535 #include "ruby/util.h"
5536
5537 static inline int
5538 is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5539 {
5540 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5541 }
5542
5543 static inline int
5544 parser_is_identchar(struct parser_params *p)
5545 {
5546 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5547 }
5548
5549 static inline int
5550 parser_isascii(struct parser_params *p)
5551 {
5552 return ISASCII(*(p->lex.pcur-1));
5553 }
5554
5555 static void
5556 token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5557 {
5558 int column = 1, nonspc = 0, i;
5559 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5560 if (*ptr == '\t') {
5561 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5562 }
5563 column++;
5564 if (*ptr != ' ' && *ptr != '\t') {
5565 nonspc = 1;
5566 }
5567 }
5568
5569 ptinfo->beg = loc->beg_pos;
5570 ptinfo->indent = column;
5571 ptinfo->nonspc = nonspc;
5572 }
5573
5574 static void
5575 token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5576 {
5577 token_info *ptinfo;
5578
5579 if (!p->token_info_enabled) return;
5580 ptinfo = ALLOC(token_info);
5581 ptinfo->token = token;
5582 ptinfo->next = p->token_info;
5583 token_info_setup(ptinfo, p->lex.pbeg, loc);
5584
5585 p->token_info = ptinfo;
5586 }
5587
5588 static void
5589 token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5590 {
5591 token_info *ptinfo_beg = p->token_info;
5592
5593 if (!ptinfo_beg) return;
5594 p->token_info = ptinfo_beg->next;
5595
5596 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5597 token_info_warn(p, token, ptinfo_beg, 1, loc);
5598 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5599 }
5600
5601 static void
5602 token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5603 {
5604 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5605 if (!p->token_info_enabled) return;
5606 if (!ptinfo_beg) return;
5607 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5608 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5609 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5610 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5611 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5612 rb_warn3L(ptinfo_end->beg.lineno,
5613 "mismatched indentations at '%s' with '%s' at %d",
5614 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5615 }
5616
5617 static int
5618 parser_precise_mbclen(struct parser_params *p, const char *ptr)
5619 {
5620 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5621 if (!MBCLEN_CHARFOUND_P(len)) {
5622 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5623 return -1;
5624 }
5625 return len;
5626 }
5627
5628 #ifndef RIPPER
5629 static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5630
5631 static inline void
5632 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5633 {
5634 VALUE str;
5635 int lineno = p->ruby_sourceline;
5636 if (!yylloc) {
5637 return;
5638 }
5639 else if (yylloc->beg_pos.lineno == lineno) {
5640 str = p->lex.lastline;
5641 }
5642 else {
5643 return;
5644 }
5645 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5646 }
5647
5648 static int
5649 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5650 {
5651 YYLTYPE current;
5652
5653 if (!yylloc) {
5654 yylloc = RUBY_SET_YYLLOC(current);
5655 }
5656 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5657 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5658 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5659 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5660 yylloc = 0;
5661 }
5662 compile_error(p, "%s", msg);
5663 parser_show_error_line(p, yylloc);
5664 return 0;
5665 }
5666
5667 static void
5668 ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5669 {
5670 VALUE mesg;
5671 const int max_line_margin = 30;
5672 const char *ptr, *ptr_end, *pt, *pb;
5673 const char *pre = "", *post = "", *pend;
5674 const char *code = "", *caret = "";
5675 const char *lim;
5676 const char *const pbeg = RSTRING_PTR(str);
5677 char *buf;
5678 long len;
5679 int i;
5680
5681 if (!yylloc) return;
5682 pend = RSTRING_END(str);
5683 if (pend > pbeg && pend[-1] == '\n') {
5684 if (--pend > pbeg && pend[-1] == '\r') --pend;
5685 }
5686
5687 pt = pend;
5688 if (lineno == yylloc->end_pos.lineno &&
5689 (pend - pbeg) > yylloc->end_pos.column) {
5690 pt = pbeg + yylloc->end_pos.column;
5691 }
5692
5693 ptr = ptr_end = pt;
5694 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5695 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5696
5697 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5698 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5699
5700 len = ptr_end - ptr;
5701 if (len > 4) {
5702 if (ptr > pbeg) {
5703 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5704 if (ptr > pbeg) pre = "...";
5705 }
5706 if (ptr_end < pend) {
5707 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5708 if (ptr_end < pend) post = "...";
5709 }
5710 }
5711 pb = pbeg;
5712 if (lineno == yylloc->beg_pos.lineno) {
5713 pb += yylloc->beg_pos.column;
5714 if (pb > pt) pb = pt;
5715 }
5716 if (pb < ptr) pb = ptr;
5717 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5718 return;
5719 }
5720 if (RTEST(errbuf)) {
5721 mesg = rb_attr_get(errbuf, idMesg);
5722 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5723 rb_str_cat_cstr(mesg, "\n");
5724 }
5725 else {
5726 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5727 }
5728 if (!errbuf && rb_stderr_tty_p()) {
5729 #define CSI_BEGIN "\033["
5730 #define CSI_SGR "m"
5731 rb_str_catf(mesg,
5732 CSI_BEGIN""CSI_SGR"%s" /* pre */
5733 CSI_BEGIN"1"CSI_SGR"%.*s"
5734 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5735 CSI_BEGIN";1"CSI_SGR"%.*s"
5736 CSI_BEGIN""CSI_SGR"%s" /* post */
5737 "\n",
5738 pre,
5739 (int)(pb - ptr), ptr,
5740 (int)(pt - pb), pb,
5741 (int)(ptr_end - pt), pt,
5742 post);
5743 }
5744 else {
5745 char *p2;
5746
5747 len = ptr_end - ptr;
5748 lim = pt < pend ? pt : pend;
5749 i = (int)(lim - ptr);
5750 buf = ALLOCA_N(char, i+2);
5751 code = ptr;
5752 caret = p2 = buf;
5753 if (ptr <= pb) {
5754 while (ptr < pb) {
5755 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5756 }
5757 *p2++ = '^';
5758 ptr++;
5759 }
5760 if (lim > ptr) {
5761 memset(p2, '~', (lim - ptr));
5762 p2 += (lim - ptr);
5763 }
5764 *p2 = '\0';
5765 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5766 pre, (int)len, code, post,
5767 pre, caret);
5768 }
5769 if (!errbuf) rb_write_error_str(mesg);
5770 }
5771 #else
5772 static int
5773 parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5774 {
5775 const char *pcur = 0, *ptok = 0;
5776 if (yylloc &&
5777 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5778 p->ruby_sourceline == yylloc->end_pos.lineno) {
5779 pcur = p->lex.pcur;
5780 ptok = p->lex.ptok;
5781 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5782 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5783 }
5784 dispatch1(parse_error, STR_NEW2(msg));
5785 ripper_error(p);
5786 if (pcur) {
5787 p->lex.ptok = ptok;
5788 p->lex.pcur = pcur;
5789 }
5790 return 0;
5791 }
5792
5793 static inline void
5794 parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5795 {
5796 }
5797 #endif /* !RIPPER */
5798
5799 #ifndef RIPPER
5800 static int
5801 vtable_size(const struct vtable *tbl)
5802 {
5803 if (!DVARS_TERMINAL_P(tbl)) {
5804 return tbl->pos;
5805 }
5806 else {
5807 return 0;
5808 }
5809 }
5810 #endif
5811
5812 static struct vtable *
5813 vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5814 {
5815 struct vtable *tbl = ALLOC(struct vtable);
5816 tbl->pos = 0;
5817 tbl->capa = 8;
5818 tbl->tbl = ALLOC_N(ID, tbl->capa);
5819 tbl->prev = prev;
5820 #ifndef RIPPER
5821 if (p->debug) {
5822 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5823 }
5824 #endif
5825 return tbl;
5826 }
5827 #define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5828
5829 static void
5830 vtable_free_gen(struct parser_params *p, int line, const char *name,
5831 struct vtable *tbl)
5832 {
5833 #ifndef RIPPER
5834 if (p->debug) {
5835 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5836 }
5837 #endif
5838 if (!DVARS_TERMINAL_P(tbl)) {
5839 if (tbl->tbl) {
5840 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5841 }
5842 ruby_sized_xfree(tbl, sizeof(tbl));
5843 }
5844 }
5845 #define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5846
5847 static void
5848 vtable_add_gen(struct parser_params *p, int line, const char *name,
5849 struct vtable *tbl, ID id)
5850 {
5851 #ifndef RIPPER
5852 if (p->debug) {
5853 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5854 line, name, (void *)tbl, rb_id2name(id));
5855 }
5856 #endif
5857 if (DVARS_TERMINAL_P(tbl)) {
5858 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5859 return;
5860 }
5861 if (tbl->pos == tbl->capa) {
5862 tbl->capa = tbl->capa * 2;
5863 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5864 }
5865 tbl->tbl[tbl->pos++] = id;
5866 }
5867 #define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5868
5869 #ifndef RIPPER
5870 static void
5871 vtable_pop_gen(struct parser_params *p, int line, const char *name,
5872 struct vtable *tbl, int n)
5873 {
5874 if (p->debug) {
5875 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5876 line, name, (void *)tbl, n);
5877 }
5878 if (tbl->pos < n) {
5879 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5880 return;
5881 }
5882 tbl->pos -= n;
5883 }
5884 #define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5885 #endif
5886
5887 static int
5888 vtable_included(const struct vtable * tbl, ID id)
5889 {
5890 int i;
5891
5892 if (!DVARS_TERMINAL_P(tbl)) {
5893 for (i = 0; i < tbl->pos; i++) {
5894 if (tbl->tbl[i] == id) {
5895 return i+1;
5896 }
5897 }
5898 }
5899 return 0;
5900 }
5901
5902 static void parser_prepare(struct parser_params *p);
5903
5904 #ifndef RIPPER
5905 static NODE *parser_append_options(struct parser_params *p, NODE *node);
5906
5907 static VALUE
5908 debug_lines(VALUE fname)
5909 {
5910 ID script_lines;
5911 CONST_ID(script_lines, "SCRIPT_LINES__");
5912 if (rb_const_defined_at(rb_cObject, script_lines)) {
5913 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5914 if (RB_TYPE_P(hash, T_HASH)) {
5915 VALUE lines = rb_ary_new();
5916 rb_hash_aset(hash, fname, lines);
5917 return lines;
5918 }
5919 }
5920 return 0;
5921 }
5922
5923 static int
5924 e_option_supplied(struct parser_params *p)
5925 {
5926 return strcmp(p->ruby_sourcefile, "-e") == 0;
5927 }
5928
5929 static VALUE
5930 yycompile0(VALUE arg)
5931 {
5932 int n;
5933 NODE *tree;
5934 struct parser_params *p = (struct parser_params *)arg;
5935 VALUE cov = Qfalse;
5936
5937 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
5938 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
5939 if (p->debug_lines && p->ruby_sourceline > 0) {
5940 VALUE str = STR_NEW0();
5941 n = p->ruby_sourceline;
5942 do {
5943 rb_ary_push(p->debug_lines, str);
5944 } while (--n);
5945 }
5946
5947 if (!e_option_supplied(p)) {
5948 cov = Qtrue;
5949 }
5950 }
5951
5952 parser_prepare(p);
5953 #define RUBY_DTRACE_PARSE_HOOK(name) \
5954 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5955 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
5956 }
5957 RUBY_DTRACE_PARSE_HOOK(BEGIN);
5958 n = yyparse(p);
5959 RUBY_DTRACE_PARSE_HOOK(END);
5960 p->debug_lines = 0;
5961
5962 p->lex.strterm = 0;
5963 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
5964 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
5965 if (n || p->error_p) {
5966 VALUE mesg = p->error_buffer;
5967 if (!mesg) {
5968 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5969 }
5970 rb_set_errinfo(mesg);
5971 return FALSE;
5972 }
5973 tree = p->eval_tree;
5974 if (!tree) {
5975 tree = NEW_NIL(&NULL_LOC);
5976 }
5977 else {
5978 VALUE opt = p->compile_option;
5979 NODE *prelude;
5980 NODE *body = parser_append_options(p, tree->nd_body);
5981 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5982 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5983 prelude = block_append(p, p->eval_tree_begin, body);
5984 tree->nd_body = prelude;
5985 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
5986 }
5987 p->ast->body.root = tree;
5988 p->ast->body.line_count = p->line_count;
5989 return TRUE;
5990 }
5991
5992 static rb_ast_t *
5993 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
5994 {
5995 rb_ast_t *ast;
5996 if (NIL_P(fname)) {
5997 p->ruby_sourcefile_string = Qnil;
5998 p->ruby_sourcefile = "(none)";
5999 }
6000 else {
6001 p->ruby_sourcefile_string = rb_fstring(fname);
6002 p->ruby_sourcefile = StringValueCStr(fname);
6003 }
6004 p->ruby_sourceline = line - 1;
6005
6006 p->ast = ast = rb_ast_new();
6007 rb_suppress_tracing(yycompile0, (VALUE)p);
6008 p->ast = 0;
6009 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6010
6011 return ast;
6012 }
6013 #endif /* !RIPPER */
6014
6015 static rb_encoding *
6016 must_be_ascii_compatible(VALUE s)
6017 {
6018 rb_encoding *enc = rb_enc_get(s);
6019 if (!rb_enc_asciicompat(enc)) {
6020 rb_raise(rb_eArgError, "invalid source encoding");
6021 }
6022 return enc;
6023 }
6024
6025 static VALUE
6026 lex_get_str(struct parser_params *p, VALUE s)
6027 {
6028 char *beg, *end, *start;
6029 long len;
6030
6031 beg = RSTRING_PTR(s);
6032 len = RSTRING_LEN(s);
6033 start = beg;
6034 if (p->lex.gets_.ptr) {
6035 if (len == p->lex.gets_.ptr) return Qnil;
6036 beg += p->lex.gets_.ptr;
6037 len -= p->lex.gets_.ptr;
6038 }
6039 end = memchr(beg, '\n', len);
6040 if (end) len = ++end - beg;
6041 p->lex.gets_.ptr += len;
6042 return rb_str_subseq(s, beg - start, len);
6043 }
6044
6045 static VALUE
6046 lex_getline(struct parser_params *p)
6047 {
6048 VALUE line = (*p->lex.gets)(p, p->lex.input);
6049 if (NIL_P(line)) return line;
6050 must_be_ascii_compatible(line);
6051 #ifndef RIPPER
6052 if (p->debug_lines) {
6053 rb_enc_associate(line, p->enc);
6054 rb_ary_push(p->debug_lines, line);
6055 }
6056 #endif
6057 p->line_count++;
6058 return line;
6059 }
6060
6061 static const rb_data_type_t parser_data_type;
6062
6063 #ifndef RIPPER
6064 static rb_ast_t*
6065 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6066 {
6067 struct parser_params *p;
6068
6069 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6070
6071 p->lex.gets = lex_get_str;
6072 p->lex.gets_.ptr = 0;
6073 p->lex.input = rb_str_new_frozen(s);
6074 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6075
6076 return yycompile(vparser, p, fname, line);
6077 }
6078
6079 rb_ast_t*
6080 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6081 {
6082 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6083 }
6084
6085 rb_ast_t*
6086 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6087 {
6088 must_be_ascii_compatible(s);
6089 return parser_compile_string(vparser, f, s, line);
6090 }
6091
6092 VALUE rb_io_gets_internal(VALUE io);
6093
6094 static VALUE
6095 lex_io_gets(struct parser_params *p, VALUE io)
6096 {
6097 return rb_io_gets_internal(io);
6098 }
6099
6100 rb_ast_t*
6101 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6102 {
6103 struct parser_params *p;
6104
6105 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6106
6107 p->lex.gets = lex_io_gets;
6108 p->lex.input = file;
6109 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6110
6111 return yycompile(vparser, p, fname, start);
6112 }
6113
6114 static VALUE
6115 lex_generic_gets(struct parser_params *p, VALUE input)
6116 {
6117 return (*p->lex.gets_.call)(input, p->line_count);
6118 }
6119
6120 rb_ast_t*
6121 rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6122 {
6123 struct parser_params *p;
6124
6125 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6126
6127 p->lex.gets = lex_generic_gets;
6128 p->lex.gets_.call = lex_gets;
6129 p->lex.input = input;
6130 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6131
6132 return yycompile(vparser, p, fname, start);
6133 }
6134 #endif /* !RIPPER */
6135
6136 #define STR_FUNC_ESCAPE 0x01
6137 #define STR_FUNC_EXPAND 0x02
6138 #define STR_FUNC_REGEXP 0x04
6139 #define STR_FUNC_QWORDS 0x08
6140 #define STR_FUNC_SYMBOL 0x10
6141 #define STR_FUNC_INDENT 0x20
6142 #define STR_FUNC_LABEL 0x40
6143 #define STR_FUNC_LIST 0x4000
6144 #define STR_FUNC_TERM 0x8000
6145
6146 enum string_type {
6147 str_label = STR_FUNC_LABEL,
6148 str_squote = (0),
6149 str_dquote = (STR_FUNC_EXPAND),
6150 str_xquote = (STR_FUNC_EXPAND),
6151 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6152 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6153 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6154 str_ssym = (STR_FUNC_SYMBOL),
6155 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6156 };
6157
6158 static VALUE
6159 parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6160 {
6161 VALUE str;
6162
6163 str = rb_enc_str_new(ptr, len, enc);
6164 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6165 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6166 }
6167 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6168 rb_enc_associate(str, rb_ascii8bit_encoding());
6169 }
6170 }
6171
6172 return str;
6173 }
6174
6175 #define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6176 #define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6177 #define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6178 #define peek(p,c) peek_n(p, (c), 0)
6179 #define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6180 #define peekc(p) peekc_n(p, 0)
6181 #define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6182
6183 #ifdef RIPPER
6184 static void
6185 add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6186 {
6187 if (tok < end) {
6188 if (!has_delayed_token(p)) {
6189 p->delayed.token = rb_str_buf_new(end - tok);
6190 rb_enc_associate(p->delayed.token, p->enc);
6191 p->delayed.line = p->ruby_sourceline;
6192 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6193 }
6194 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6195 p->lex.ptok = end;
6196 }
6197 }
6198 #else
6199 #define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6200 #endif
6201
6202 static int
6203 nextline(struct parser_params *p)
6204 {
6205 VALUE v = p->lex.nextline;
6206 p->lex.nextline = 0;
6207 if (!v) {
6208 if (p->eofp)
6209 return -1;
6210
6211 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6212 goto end_of_input;
6213 }
6214
6215 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6216 end_of_input:
6217 p->eofp = 1;
6218 lex_goto_eol(p);
6219 return -1;
6220 }
6221 p->cr_seen = FALSE;
6222 }
6223 else if (NIL_P(v)) {
6224 /* after here-document without terminator */
6225 goto end_of_input;
6226 }
6227 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6228 if (p->heredoc_end > 0) {
6229 p->ruby_sourceline = p->heredoc_end;
6230 p->heredoc_end = 0;
6231 }
6232 p->ruby_sourceline++;
6233 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6234 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6235 token_flush(p);
6236 p->lex.prevline = p->lex.lastline;
6237 p->lex.lastline = v;
6238 return 0;
6239 }
6240
6241 static int
6242 parser_cr(struct parser_params *p, int c)
6243 {
6244 if (peek(p, '\n')) {
6245 p->lex.pcur++;
6246 c = '\n';
6247 }
6248 else if (!p->cr_seen) {
6249 p->cr_seen = TRUE;
6250 /* carried over with p->lex.nextline for nextc() */
6251 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6252 }
6253 return c;
6254 }
6255
6256 static inline int
6257 nextc(struct parser_params *p)
6258 {
6259 int c;
6260
6261 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6262 if (nextline(p)) return -1;
6263 }
6264 c = (unsigned char)*p->lex.pcur++;
6265 if (UNLIKELY(c == '\r')) {
6266 c = parser_cr(p, c);
6267 }
6268
6269 return c;
6270 }
6271
6272 static void
6273 pushback(struct parser_params *p, int c)
6274 {
6275 if (c == -1) return;
6276 p->lex.pcur--;
6277 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6278 p->lex.pcur--;
6279 }
6280 }
6281
6282 #define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6283
6284 #define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6285 #define tok(p) (p)->tokenbuf
6286 #define toklen(p) (p)->tokidx
6287
6288 static int
6289 looking_at_eol_p(struct parser_params *p)
6290 {
6291 const char *ptr = p->lex.pcur;
6292 while (ptr < p->lex.pend) {
6293 int c = (unsigned char)*ptr++;
6294 int eol = (c == '\n' || c == '#');
6295 if (eol || !ISSPACE(c)) {
6296 return eol;
6297 }
6298 }
6299 return TRUE;
6300 }
6301
6302 static char*
6303 newtok(struct parser_params *p)
6304 {
6305 p->tokidx = 0;
6306 p->tokline = p->ruby_sourceline;
6307 if (!p->tokenbuf) {
6308 p->toksiz = 60;
6309 p->tokenbuf = ALLOC_N(char, 60);
6310 }
6311 if (p->toksiz > 4096) {
6312 p->toksiz = 60;
6313 REALLOC_N(p->tokenbuf, char, 60);
6314 }
6315 return p->tokenbuf;
6316 }
6317
6318 static char *
6319 tokspace(struct parser_params *p, int n)
6320 {
6321 p->tokidx += n;
6322
6323 if (p->tokidx >= p->toksiz) {
6324 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6325 REALLOC_N(p->tokenbuf, char, p->toksiz);
6326 }
6327 return &p->tokenbuf[p->tokidx-n];
6328 }
6329
6330 static void
6331 tokadd(struct parser_params *p, int c)
6332 {
6333 p->tokenbuf[p->tokidx++] = (char)c;
6334 if (p->tokidx >= p->toksiz) {
6335 p->toksiz *= 2;
6336 REALLOC_N(p->tokenbuf, char, p->toksiz);
6337 }
6338 }
6339
6340 static int
6341 tok_hex(struct parser_params *p, size_t *numlen)
6342 {
6343 int c;
6344
6345 c = scan_hex(p->lex.pcur, 2, numlen);
6346 if (!*numlen) {
6347 yyerror0("invalid hex escape");
6348 token_flush(p);
6349 return 0;
6350 }
6351 p->lex.pcur += *numlen;
6352 return c;
6353 }
6354
6355 #define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6356
6357 static int
6358 escaped_control_code(int c)
6359 {
6360 int c2 = 0;
6361 switch (c) {
6362 case ' ':
6363 c2 = 's';
6364 break;
6365 case '\n':
6366 c2 = 'n';
6367 break;
6368 case '\t':
6369 c2 = 't';
6370 break;
6371 case '\v':
6372 c2 = 'v';
6373 break;
6374 case '\r':
6375 c2 = 'r';
6376 break;
6377 case '\f':
6378 c2 = 'f';
6379 break;
6380 }
6381 return c2;
6382 }
6383
6384 #define WARN_SPACE_CHAR(c, prefix) \
6385 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6386
6387 static int
6388 tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6389 int regexp_literal, int wide)
6390 {
6391 size_t numlen;
6392 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6393 literal_flush(p, p->lex.pcur);
6394 p->lex.pcur += numlen;
6395 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6396 yyerror0("invalid Unicode escape");
6397 return wide && numlen > 0;
6398 }
6399 if (codepoint > 0x10ffff) {
6400 yyerror0("invalid Unicode codepoint (too large)");
6401 return wide;
6402 }
6403 if ((codepoint & 0xfffff800) == 0xd800) {
6404 yyerror0("invalid Unicode codepoint");
6405 return wide;
6406 }
6407 if (regexp_literal) {
6408 tokcopy(p, (int)numlen);
6409 }
6410 else if (codepoint >= 0x80) {
6411 rb_encoding *utf8 = rb_utf8_encoding();
6412 if (*encp && utf8 != *encp) {
6413 YYLTYPE loc = RUBY_INIT_YYLLOC();
6414 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6415 parser_show_error_line(p, &loc);
6416 return wide;
6417 }
6418 *encp = utf8;
6419 tokaddmbc(p, codepoint, *encp);
6420 }
6421 else {
6422 tokadd(p, codepoint);
6423 }
6424 return TRUE;
6425 }
6426
6427 /* return value is for ?\u3042 */
6428 static void
6429 tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6430 int term, int symbol_literal, int regexp_literal)
6431 {
6432 /*
6433 * If `term` is not -1, then we allow multiple codepoints in \u{}
6434 * upto `term` byte, otherwise we're parsing a character literal.
6435 * And then add the codepoints to the current token.
6436 */
6437 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6438
6439 const int open_brace = '{', close_brace = '}';
6440
6441 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6442
6443 if (peek(p, open_brace)) { /* handle \u{...} form */
6444 const char *second = NULL;
6445 int c, last = nextc(p);
6446 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6447 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6448 while (c != close_brace) {
6449 if (c == term) goto unterminated;
6450 if (second == multiple_codepoints)
6451 second = p->lex.pcur;
6452 if (regexp_literal) tokadd(p, last);
6453 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6454 break;
6455 }
6456 while (ISSPACE(c = *p->lex.pcur)) {
6457 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6458 last = c;
6459 }
6460 if (term == -1 && !second)
6461 second = multiple_codepoints;
6462 }
6463
6464 if (c != close_brace) {
6465 unterminated:
6466 token_flush(p);
6467 yyerror0("unterminated Unicode escape");
6468 return;
6469 }
6470 if (second && second != multiple_codepoints) {
6471 const char *pcur = p->lex.pcur;
6472 p->lex.pcur = second;
6473 dispatch_scan_event(p, tSTRING_CONTENT);
6474 token_flush(p);
6475 p->lex.pcur = pcur;
6476 yyerror0(multiple_codepoints);
6477 token_flush(p);
6478 }
6479
6480 if (regexp_literal) tokadd(p, close_brace);
6481 nextc(p);
6482 }
6483 else { /* handle \uxxxx form */
6484 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6485 token_flush(p);
6486 return;
6487 }
6488 }
6489 }
6490
6491 #define ESCAPE_CONTROL 1
6492 #define ESCAPE_META 2
6493
6494 static int
6495 read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6496 {
6497 int c;
6498 size_t numlen;
6499
6500 switch (c = nextc(p)) {
6501 case '\\': /* Backslash */
6502 return c;
6503
6504 case 'n': /* newline */
6505 return '\n';
6506
6507 case 't': /* horizontal tab */
6508 return '\t';
6509
6510 case 'r': /* carriage-return */
6511 return '\r';
6512
6513 case 'f': /* form-feed */
6514 return '\f';
6515
6516 case 'v': /* vertical tab */
6517 return '\13';
6518
6519 case 'a': /* alarm(bell) */
6520 return '\007';
6521
6522 case 'e': /* escape */
6523 return 033;
6524
6525 case '0': case '1': case '2': case '3': /* octal constant */
6526 case '4': case '5': case '6': case '7':
6527 pushback(p, c);
6528 c = scan_oct(p->lex.pcur, 3, &numlen);
6529 p->lex.pcur += numlen;
6530 return c;
6531
6532 case 'x': /* hex constant */
6533 c = tok_hex(p, &numlen);
6534 if (numlen == 0) return 0;
6535 return c;
6536
6537 case 'b': /* backspace */
6538 return '\010';
6539
6540 case 's': /* space */
6541 return ' ';
6542
6543 case 'M':
6544 if (flags & ESCAPE_META) goto eof;
6545 if ((c = nextc(p)) != '-') {
6546 goto eof;
6547 }
6548 if ((c = nextc(p)) == '\\') {
6549 if (peek(p, 'u')) goto eof;
6550 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6551 }
6552 else if (c == -1 || !ISASCII(c)) goto eof;
6553 else {
6554 int c2 = escaped_control_code(c);
6555 if (c2) {
6556 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6557 WARN_SPACE_CHAR(c2, "\\M-");
6558 }
6559 else {
6560 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6561 }
6562 }
6563 else if (ISCNTRL(c)) goto eof;
6564 return ((c & 0xff) | 0x80);
6565 }
6566
6567 case 'C':
6568 if ((c = nextc(p)) != '-') {
6569 goto eof;
6570 }
6571 case 'c':
6572 if (flags & ESCAPE_CONTROL) goto eof;
6573 if ((c = nextc(p))== '\\') {
6574 if (peek(p, 'u')) goto eof;
6575 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6576 }
6577 else if (c == '?')
6578 return 0177;
6579 else if (c == -1 || !ISASCII(c)) goto eof;
6580 else {
6581 int c2 = escaped_control_code(c);
6582 if (c2) {
6583 if (ISCNTRL(c)) {
6584 if (flags & ESCAPE_META) {
6585 WARN_SPACE_CHAR(c2, "\\M-");
6586 }
6587 else {
6588 WARN_SPACE_CHAR(c2, "");
6589 }
6590 }
6591 else {
6592 if (flags & ESCAPE_META) {
6593 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6594 }
6595 else {
6596 WARN_SPACE_CHAR(c2, "\\C-");
6597 }
6598 }
6599 }
6600 else if (ISCNTRL(c)) goto eof;
6601 }
6602 return c & 0x9f;
6603
6604 eof:
6605 case -1:
6606 yyerror0("Invalid escape character syntax");
6607 token_flush(p);
6608 return '\0';
6609
6610 default:
6611 return c;
6612 }
6613 }
6614
6615 static void
6616 tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6617 {
6618 int len = rb_enc_codelen(c, enc);
6619 rb_enc_mbcput(c, tokspace(p, len), enc);
6620 }
6621
6622 static int
6623 tokadd_escape(struct parser_params *p, rb_encoding **encp)
6624 {
6625 int c;
6626 int flags = 0;
6627 size_t numlen;
6628
6629 first:
6630 switch (c = nextc(p)) {
6631 case '\n':
6632 return 0; /* just ignore */
6633
6634 case '0': case '1': case '2': case '3': /* octal constant */
6635 case '4': case '5': case '6': case '7':
6636 {
6637 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6638 if (numlen == 0) goto eof;
6639 p->lex.pcur += numlen;
6640 tokcopy(p, (int)numlen + 1);
6641 }
6642 return 0;
6643
6644 case 'x': /* hex constant */
6645 {
6646 tok_hex(p, &numlen);
6647 if (numlen == 0) return -1;
6648 tokcopy(p, (int)numlen + 2);
6649 }
6650 return 0;
6651
6652 case 'M':
6653 if (flags & ESCAPE_META) goto eof;
6654 if ((c = nextc(p)) != '-') {
6655 pushback(p, c);
6656 goto eof;
6657 }
6658 tokcopy(p, 3);
6659 flags |= ESCAPE_META;
6660 goto escaped;
6661
6662 case 'C':
6663 if (flags & ESCAPE_CONTROL) goto eof;
6664 if ((c = nextc(p)) != '-') {
6665 pushback(p, c);
6666 goto eof;
6667 }
6668 tokcopy(p, 3);
6669 goto escaped;
6670
6671 case 'c':
6672 if (flags & ESCAPE_CONTROL) goto eof;
6673 tokcopy(p, 2);
6674 flags |= ESCAPE_CONTROL;
6675 escaped:
6676 if ((c = nextc(p)) == '\\') {
6677 goto first;
6678 }
6679 else if (c == -1) goto eof;
6680 tokadd(p, c);
6681 return 0;
6682
6683 eof:
6684 case -1:
6685 yyerror0("Invalid escape character syntax");
6686 token_flush(p);
6687 return -1;
6688
6689 default:
6690 tokadd(p, '\\');
6691 tokadd(p, c);
6692 }
6693 return 0;
6694 }
6695
6696 static int
6697 regx_options(struct parser_params *p)
6698 {
6699 int kcode = 0;
6700 int kopt = 0;
6701 int options = 0;
6702 int c, opt, kc;
6703
6704 newtok(p);
6705 while (c = nextc(p), ISALPHA(c)) {
6706 if (c == 'o') {
6707 options |= RE_OPTION_ONCE;
6708 }
6709 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6710 if (kc >= 0) {
6711 if (kc != rb_ascii8bit_encindex()) kcode = c;
6712 kopt = opt;
6713 }
6714 else {
6715 options |= opt;
6716 }
6717 }
6718 else {
6719 tokadd(p, c);
6720 }
6721 }
6722 options |= kopt;
6723 pushback(p, c);
6724 if (toklen(p)) {
6725 YYLTYPE loc = RUBY_INIT_YYLLOC();
6726 tokfix(p);
6727 compile_error(p, "unknown regexp option%s - %*s",
6728 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6729 parser_show_error_line(p, &loc);
6730 }
6731 return options | RE_OPTION_ENCODING(kcode);
6732 }
6733
6734 static int
6735 tokadd_mbchar(struct parser_params *p, int c)
6736 {
6737 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6738 if (len < 0) return -1;
6739 tokadd(p, c);
6740 p->lex.pcur += --len;
6741 if (len > 0) tokcopy(p, len);
6742 return c;
6743 }
6744
6745 static inline int
6746 simple_re_meta(int c)
6747 {
6748 switch (c) {
6749 case '$': case '*': case '+': case '.':
6750 case '?': case '^': case '|':
6751 case ')': case ']': case '}': case '>':
6752 return TRUE;
6753 default:
6754 return FALSE;
6755 }
6756 }
6757
6758 static int
6759 parser_update_heredoc_indent(struct parser_params *p, int c)
6760 {
6761 if (p->heredoc_line_indent == -1) {
6762 if (c == '\n') p->heredoc_line_indent = 0;
6763 }
6764 else {
6765 if (c == ' ') {
6766 p->heredoc_line_indent++;
6767 return TRUE;
6768 }
6769 else if (c == '\t') {
6770 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6771 p->heredoc_line_indent = w * TAB_WIDTH;
6772 return TRUE;
6773 }
6774 else if (c != '\n') {
6775 if (p->heredoc_indent > p->heredoc_line_indent) {
6776 p->heredoc_indent = p->heredoc_line_indent;
6777 }
6778 p->heredoc_line_indent = -1;
6779 }
6780 }
6781 return FALSE;
6782 }
6783
6784 static void
6785 parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6786 {
6787 YYLTYPE loc = RUBY_INIT_YYLLOC();
6788 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6789 compile_error(p, "%s mixed within %s source", n1, n2);
6790 parser_show_error_line(p, &loc);
6791 }
6792
6793 static void
6794 parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6795 {
6796 const char *pos = p->lex.pcur;
6797 p->lex.pcur = beg;
6798 parser_mixed_error(p, enc1, enc2);
6799 p->lex.pcur = pos;
6800 }
6801
6802 static int
6803 tokadd_string(struct parser_params *p,
6804 int func, int term, int paren, long *nest,
6805 rb_encoding **encp, rb_encoding **enc)
6806 {
6807 int c;
6808 bool erred = false;
6809
6810 #define mixed_error(enc1, enc2) \
6811 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6812 #define mixed_escape(beg, enc1, enc2) \
6813 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6814
6815 while ((c = nextc(p)) != -1) {
6816 if (p->heredoc_indent > 0) {
6817 parser_update_heredoc_indent(p, c);
6818 }
6819
6820 if (paren && c == paren) {
6821 ++*nest;
6822 }
6823 else if (c == term) {
6824 if (!nest || !*nest) {
6825 pushback(p, c);
6826 break;
6827 }
6828 --*nest;
6829 }
6830 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6831 int c2 = *p->lex.pcur;
6832 if (c2 == '$' || c2 == '@' || c2 == '{') {
6833 pushback(p, c);
6834 break;
6835 }
6836 }
6837 else if (c == '\\') {
6838 literal_flush(p, p->lex.pcur - 1);
6839 c = nextc(p);
6840 switch (c) {
6841 case '\n':
6842 if (func & STR_FUNC_QWORDS) break;
6843 if (func & STR_FUNC_EXPAND) {
6844 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6845 continue;
6846 if (c == term) {
6847 c = '\\';
6848 goto terminate;
6849 }
6850 }
6851 tokadd(p, '\\');
6852 break;
6853
6854 case '\\':
6855 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6856 break;
6857
6858 case 'u':
6859 if ((func & STR_FUNC_EXPAND) == 0) {
6860 tokadd(p, '\\');
6861 break;
6862 }
6863 tokadd_utf8(p, enc, term,
6864 func & STR_FUNC_SYMBOL,
6865 func & STR_FUNC_REGEXP);
6866 continue;
6867
6868 default:
6869 if (c == -1) return -1;
6870 if (!ISASCII(c)) {
6871 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6872 goto non_ascii;
6873 }
6874 if (func & STR_FUNC_REGEXP) {
6875 if (c == term && !simple_re_meta(c)) {
6876 tokadd(p, c);
6877 continue;
6878 }
6879 pushback(p, c);
6880 if ((c = tokadd_escape(p, enc)) < 0)
6881 return -1;
6882 if (*enc && *enc != *encp) {
6883 mixed_escape(p->lex.ptok+2, *enc, *encp);
6884 }
6885 continue;
6886 }
6887 else if (func & STR_FUNC_EXPAND) {
6888 pushback(p, c);
6889 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6890 c = read_escape(p, 0, enc);
6891 }
6892 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6893 /* ignore backslashed spaces in %w */
6894 }
6895 else if (c != term && !(paren && c == paren)) {
6896 tokadd(p, '\\');
6897 pushback(p, c);
6898 continue;
6899 }
6900 }
6901 }
6902 else if (!parser_isascii(p)) {
6903 non_ascii:
6904 if (!*enc) {
6905 *enc = *encp;
6906 }
6907 else if (*enc != *encp) {
6908 mixed_error(*enc, *encp);
6909 continue;
6910 }
6911 if (tokadd_mbchar(p, c) == -1) return -1;
6912 continue;
6913 }
6914 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6915 pushback(p, c);
6916 break;
6917 }
6918 if (c & 0x80) {
6919 if (!*enc) {
6920 *enc = *encp;
6921 }
6922 else if (*enc != *encp) {
6923 mixed_error(*enc, *encp);
6924 continue;
6925 }
6926 }
6927 tokadd(p, c);
6928 }
6929 terminate:
6930 if (*enc) *encp = *enc;
6931 return c;
6932 }
6933
6934 static inline rb_strterm_t *
6935 new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
6936 {
6937 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
6938 }
6939
6940 /* imemo_parser_strterm for literal */
6941 #define NEW_STRTERM(func, term, paren) \
6942 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
6943
6944 #ifdef RIPPER
6945 static void
6946 flush_string_content(struct parser_params *p, rb_encoding *enc)
6947 {
6948 VALUE content = yylval.val;
6949 if (!ripper_is_node_yylval(content))
6950 content = ripper_new_yylval(p, 0, 0, content);
6951 if (has_delayed_token(p)) {
6952 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
6953 if (len > 0) {
6954 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
6955 }
6956 dispatch_delayed_token(p, tSTRING_CONTENT);
6957 p->lex.ptok = p->lex.pcur;
6958 RNODE(content)->nd_rval = yylval.val;
6959 }
6960 dispatch_scan_event(p, tSTRING_CONTENT);
6961 if (yylval.val != content)
6962 RNODE(content)->nd_rval = yylval.val;
6963 yylval.val = content;
6964 }
6965 #else
6966 #define flush_string_content(p, enc) ((void)(enc))
6967 #endif
6968
6969 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6970 /* this can be shared with ripper, since it's independent from struct
6971 * parser_params. */
6972 #ifndef RIPPER
6973 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6974 #define SPECIAL_PUNCT(idx) ( \
6975 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6976 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6977 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6978 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6979 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6980 BIT('0', idx))
6981 const unsigned int ruby_global_name_punct_bits[] = {
6982 SPECIAL_PUNCT(0),
6983 SPECIAL_PUNCT(1),
6984 SPECIAL_PUNCT(2),
6985 };
6986 #undef BIT
6987 #undef SPECIAL_PUNCT
6988 #endif
6989
6990 static enum yytokentype
6991 parser_peek_variable_name(struct parser_params *p)
6992 {
6993 int c;
6994 const char *ptr = p->lex.pcur;
6995
6996 if (ptr + 1 >= p->lex.pend) return 0;
6997 c = *ptr++;
6998 switch (c) {
6999 case '$':
7000 if ((c = *ptr) == '-') {
7001 if (++ptr >= p->lex.pend) return 0;
7002 c = *ptr;
7003 }
7004 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7005 return tSTRING_DVAR;
7006 }
7007 break;
7008 case '@':
7009 if ((c = *ptr) == '@') {
7010 if (++ptr >= p->lex.pend) return 0;
7011 c = *ptr;
7012 }
7013 break;
7014 case '{':
7015 p->lex.pcur = ptr;
7016 p->command_start = TRUE;
7017 return tSTRING_DBEG;
7018 default:
7019 return 0;
7020 }
7021 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7022 return tSTRING_DVAR;
7023 return 0;
7024 }
7025
7026 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7027 #define IS_END() IS_lex_state(EXPR_END_ANY)
7028 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7029 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7030 #define IS_LABEL_POSSIBLE() (\
7031 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7032 IS_ARG())
7033 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7034 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7035
7036 static inline enum yytokentype
7037 parser_string_term(struct parser_params *p, int func)
7038 {
7039 p->lex.strterm = 0;
7040 if (func & STR_FUNC_REGEXP) {
7041 set_yylval_num(regx_options(p));
7042 dispatch_scan_event(p, tREGEXP_END);
7043 SET_LEX_STATE(EXPR_END);
7044 return tREGEXP_END;
7045 }
7046 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7047 nextc(p);
7048 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7049 return tLABEL_END;
7050 }
7051 SET_LEX_STATE(EXPR_END);
7052 return tSTRING_END;
7053 }
7054
7055 static enum yytokentype
7056 parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7057 {
7058 int func = (int)quote->u1.func;
7059 int term = (int)quote->u3.term;
7060 int paren = (int)quote->u2.paren;
7061 int c, space = 0;
7062 rb_encoding *enc = p->enc;
7063 rb_encoding *base_enc = 0;
7064 VALUE lit;
7065
7066 if (func & STR_FUNC_TERM) {
7067 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7068 SET_LEX_STATE(EXPR_END);
7069 p->lex.strterm = 0;
7070 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7071 }
7072 c = nextc(p);
7073 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7074 do {c = nextc(p);} while (ISSPACE(c));
7075 space = 1;
7076 }
7077 if (func & STR_FUNC_LIST) {
7078 quote->u1.func &= ~STR_FUNC_LIST;
7079 space = 1;
7080 }
7081 if (c == term && !quote->u0.nest) {
7082 if (func & STR_FUNC_QWORDS) {
7083 quote->u1.func |= STR_FUNC_TERM;
7084 pushback(p, c); /* dispatch the term at tSTRING_END */
7085 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7086 return ' ';
7087 }
7088 return parser_string_term(p, func);
7089 }
7090 if (space) {
7091 pushback(p, c);
7092 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7093 return ' ';
7094 }
7095 newtok(p);
7096 if ((func & STR_FUNC_EXPAND) && c == '#') {
7097 int t = parser_peek_variable_name(p);
7098 if (t) return t;
7099 tokadd(p, '#');
7100 c = nextc(p);
7101 }
7102 pushback(p, c);
7103 if (tokadd_string(p, func, term, paren, &quote->u0.nest,
7104 &enc, &base_enc) == -1) {
7105 if (p->eofp) {
7106 #ifndef RIPPER
7107 # define unterminated_literal(mesg) yyerror0(mesg)
7108 #else
7109 # define unterminated_literal(mesg) compile_error(p, mesg)
7110 #endif
7111 literal_flush(p, p->lex.pcur);
7112 if (func & STR_FUNC_QWORDS) {
7113 /* no content to add, bailing out here */
7114 unterminated_literal("unterminated list meets end of file");
7115 p->lex.strterm = 0;
7116 return tSTRING_END;
7117 }
7118 if (func & STR_FUNC_REGEXP) {
7119 unterminated_literal("unterminated regexp meets end of file");
7120 }
7121 else {
7122 unterminated_literal("unterminated string meets end of file");
7123 }
7124 quote->u1.func |= STR_FUNC_TERM;
7125 }
7126 }
7127
7128 tokfix(p);
7129 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7130 set_yylval_str(lit);
7131 flush_string_content(p, enc);
7132
7133 return tSTRING_CONTENT;
7134 }
7135
7136 static enum yytokentype
7137 heredoc_identifier(struct parser_params *p)
7138 {
7139 /*
7140 * term_len is length of `<<"END"` except `END`,
7141 * in this case term_len is 4 (<, <, " and ").
7142 */
7143 long len, offset = p->lex.pcur - p->lex.pbeg;
7144 int c = nextc(p), term, func = 0, quote = 0;
7145 enum yytokentype token = tSTRING_BEG;
7146 int indent = 0;
7147
7148 if (c == '-') {
7149 c = nextc(p);
7150 func = STR_FUNC_INDENT;
7151 offset++;
7152 }
7153 else if (c == '~') {
7154 c = nextc(p);
7155 func = STR_FUNC_INDENT;
7156 offset++;
7157 indent = INT_MAX;
7158 }
7159 switch (c) {
7160 case '\'':
7161 func |= str_squote; goto quoted;
7162 case '"':
7163 func |= str_dquote; goto quoted;
7164 case '`':
7165 token = tXSTRING_BEG;
7166 func |= str_xquote; goto quoted;
7167
7168 quoted:
7169 quote++;
7170 offset++;
7171 term = c;
7172 len = 0;
7173 while ((c = nextc(p)) != term) {
7174 if (c == -1 || c == '\r' || c == '\n') {
7175 yyerror(NULL, p, "unterminated here document identifier");
7176 return -1;
7177 }
7178 }
7179 break;
7180
7181 default:
7182 if (!parser_is_identchar(p)) {
7183 pushback(p, c);
7184 if (func & STR_FUNC_INDENT) {
7185 pushback(p, indent > 0 ? '~' : '-');
7186 }
7187 return 0;
7188 }
7189 func |= str_dquote;
7190 do {
7191 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7192 if (n < 0) return 0;
7193 p->lex.pcur += --n;
7194 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7195 pushback(p, c);
7196 break;
7197 }
7198
7199 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7200 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7201 yyerror(NULL, p, "too long here document identifier");
7202 dispatch_scan_event(p, tHEREDOC_BEG);
7203 lex_goto_eol(p);
7204
7205 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7206 p->lex.strterm->flags |= STRTERM_HEREDOC;
7207 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7208 here->offset = offset;
7209 here->sourceline = p->ruby_sourceline;
7210 here->length = (int)len;
7211 here->quote = quote;
7212 here->func = func;
7213
7214 token_flush(p);
7215 p->heredoc_indent = indent;
7216 p->heredoc_line_indent = 0;
7217 return token;
7218 }
7219
7220 static void
7221 heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7222 {
7223 VALUE line;
7224
7225 p->lex.strterm = 0;
7226 line = here->lastline;
7227 p->lex.lastline = line;
7228 p->lex.pbeg = RSTRING_PTR(line);
7229 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7230 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7231 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7232 p->heredoc_end = p->ruby_sourceline;
7233 p->ruby_sourceline = (int)here->sourceline;
7234 if (p->eofp) p->lex.nextline = Qnil;
7235 p->eofp = 0;
7236 }
7237
7238 static int
7239 dedent_string(VALUE string, int width)
7240 {
7241 char *str;
7242 long len;
7243 int i, col = 0;
7244
7245 RSTRING_GETMEM(string, str, len);
7246 for (i = 0; i < len && col < width; i++) {
7247 if (str[i] == ' ') {
7248 col++;
7249 }
7250 else if (str[i] == '\t') {
7251 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7252 if (n > width) break;
7253 col = n;
7254 }
7255 else {
7256 break;
7257 }
7258 }
7259 if (!i) return 0;
7260 rb_str_modify(string);
7261 str = RSTRING_PTR(string);
7262 if (RSTRING_LEN(string) != len)
7263 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7264 MEMMOVE(str, str + i, char, len - i);
7265 rb_str_set_len(string, len - i);
7266 return i;
7267 }
7268
7269 #ifndef RIPPER
7270 static NODE *
7271 heredoc_dedent(struct parser_params *p, NODE *root)
7272 {
7273 NODE *node, *str_node, *prev_node;
7274 int indent = p->heredoc_indent;
7275 VALUE prev_lit = 0;
7276
7277 if (indent <= 0) return root;
7278 p->heredoc_indent = 0;
7279 if (!root) return root;
7280
7281 prev_node = node = str_node = root;
7282 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7283
7284 while (str_node) {
7285 VALUE lit = str_node->nd_lit;
7286 if (str_node->flags & NODE_FL_NEWLINE) {
7287 dedent_string(lit, indent);
7288 }
7289 if (!prev_lit) {
7290 prev_lit = lit;
7291 }
7292 else if (!literal_concat0(p, prev_lit, lit)) {
7293 return 0;
7294 }
7295 else {
7296 NODE *end = node->nd_end;
7297 node = prev_node->nd_next = node->nd_next;
7298 if (!node) {
7299 if (nd_type(prev_node) == NODE_DSTR)
7300 nd_set_type(prev_node, NODE_STR);
7301 break;
7302 }
7303 node->nd_end = end;
7304 goto next_str;
7305 }
7306
7307 str_node = 0;
7308 while ((node = (prev_node = node)->nd_next) != 0) {
7309 next_str:
7310 if (nd_type(node) != NODE_LIST) break;
7311 if ((str_node = node->nd_head) != 0) {
7312 enum node_type type = nd_type(str_node);
7313 if (type == NODE_STR || type == NODE_DSTR) break;
7314 prev_lit = 0;
7315 str_node = 0;
7316 }
7317 }
7318 }
7319 return root;
7320 }
7321 #else /* RIPPER */
7322 static VALUE
7323 heredoc_dedent(struct parser_params *p, VALUE array)
7324 {
7325 int indent = p->heredoc_indent;
7326
7327 if (indent <= 0) return array;
7328 p->heredoc_indent = 0;
7329 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7330 return array;
7331 }
7332
7333 /*
7334 * call-seq:
7335 * Ripper.dedent_string(input, width) -> Integer
7336 *
7337 * USE OF RIPPER LIBRARY ONLY.
7338 *
7339 * Strips up to +width+ leading whitespaces from +input+,
7340 * and returns the stripped column width.
7341 */
7342 static VALUE
7343 parser_dedent_string(VALUE self, VALUE input, VALUE width)
7344 {
7345 int wid, col;
7346
7347 StringValue(input);
7348 wid = NUM2UINT(width);
7349 col = dedent_string(input, wid);
7350 return INT2NUM(col);
7351 }
7352 #endif
7353
7354 static int
7355 whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7356 {
7357 const char *ptr = p->lex.pbeg;
7358 long n;
7359
7360 if (indent) {
7361 while (*ptr && ISSPACE(*ptr)) ptr++;
7362 }
7363 n = p->lex.pend - (ptr + len);
7364 if (n < 0) return FALSE;
7365 if (n > 0 && ptr[len] != '\n') {
7366 if (ptr[len] != '\r') return FALSE;
7367 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7368 }
7369 return strncmp(eos, ptr, len) == 0;
7370 }
7371
7372 static int
7373 word_match_p(struct parser_params *p, const char *word, long len)
7374 {
7375 if (strncmp(p->lex.pcur, word, len)) return 0;
7376 if (p->lex.pcur + len == p->lex.pend) return 1;
7377 int c = (unsigned char)p->lex.pcur[len];
7378 if (ISSPACE(c)) return 1;
7379 switch (c) {
7380 case '\0': case '\004': case '\032': return 1;
7381 }
7382 return 0;
7383 }
7384
7385 #define NUM_SUFFIX_R (1<<0)
7386 #define NUM_SUFFIX_I (1<<1)
7387 #define NUM_SUFFIX_ALL 3
7388
7389 static int
7390 number_literal_suffix(struct parser_params *p, int mask)
7391 {
7392 int c, result = 0;
7393 const char *lastp = p->lex.pcur;
7394
7395 while ((c = nextc(p)) != -1) {
7396 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7397 result |= (mask & NUM_SUFFIX_I);
7398 mask &= ~NUM_SUFFIX_I;
7399 /* r after i, rational of complex is disallowed */
7400 mask &= ~NUM_SUFFIX_R;
7401 continue;
7402 }
7403 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7404 result |= (mask & NUM_SUFFIX_R);
7405 mask &= ~NUM_SUFFIX_R;
7406 continue;
7407 }
7408 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7409 p->lex.pcur = lastp;
7410 literal_flush(p, p->lex.pcur);
7411 return 0;
7412 }
7413 pushback(p, c);
7414 break;
7415 }
7416 return result;
7417 }
7418
7419 static enum yytokentype
7420 set_number_literal(struct parser_params *p, VALUE v,
7421 enum yytokentype type, int suffix)
7422 {
7423 if (suffix & NUM_SUFFIX_I) {
7424 v = rb_complex_raw(INT2FIX(0), v);
7425 type = tIMAGINARY;
7426 }
7427 set_yylval_literal(v);
7428 SET_LEX_STATE(EXPR_END);
7429 return type;
7430 }
7431
7432 static enum yytokentype
7433 set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7434 {
7435 enum yytokentype type = tINTEGER;
7436 if (suffix & NUM_SUFFIX_R) {
7437 v = rb_rational_raw1(v);
7438 type = tRATIONAL;
7439 }
7440 return set_number_literal(p, v, type, suffix);
7441 }
7442
7443 #ifdef RIPPER
7444 static void
7445 dispatch_heredoc_end(struct parser_params *p)
7446 {
7447 VALUE str;
7448 if (has_delayed_token(p))
7449 dispatch_delayed_token(p, tSTRING_CONTENT);
7450 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7451 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7452 lex_goto_eol(p);
7453 token_flush(p);
7454 }
7455
7456 #else
7457 #define dispatch_heredoc_end(p) ((void)0)
7458 #endif
7459
7460 static enum yytokentype
7461 here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7462 {
7463 int c, func, indent = 0;
7464 const char *eos, *ptr, *ptr_end;
7465 long len;
7466 VALUE str = 0;
7467 rb_encoding *enc = p->enc;
7468 rb_encoding *base_enc = 0;
7469 int bol;
7470
7471 eos = RSTRING_PTR(here->lastline) + here->offset;
7472 len = here->length;
7473 indent = (func = here->func) & STR_FUNC_INDENT;
7474
7475 if ((c = nextc(p)) == -1) {
7476 error:
7477 #ifdef RIPPER
7478 if (!has_delayed_token(p)) {
7479 dispatch_scan_event(p, tSTRING_CONTENT);
7480 }
7481 else {
7482 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7483 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7484 int cr = ENC_CODERANGE_UNKNOWN;
7485 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7486 if (cr != ENC_CODERANGE_7BIT &&
7487 p->enc == rb_usascii_encoding() &&
7488 enc != rb_utf8_encoding()) {
7489 enc = rb_ascii8bit_encoding();
7490 }
7491 }
7492 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7493 }
7494 dispatch_delayed_token(p, tSTRING_CONTENT);
7495 }
7496 lex_goto_eol(p);
7497 #endif
7498 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7499 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7500 (int)len, eos);
7501 token_flush(p);
7502 p->lex.strterm = 0;
7503 SET_LEX_STATE(EXPR_END);
7504 return tSTRING_END;
7505 }
7506 bol = was_bol(p);
7507 if (!bol) {
7508 /* not beginning of line, cannot be the terminator */
7509 }
7510 else if (p->heredoc_line_indent == -1) {
7511 /* `heredoc_line_indent == -1` means
7512 * - "after an interpolation in the same line", or
7513 * - "in a continuing line"
7514 */
7515 p->heredoc_line_indent = 0;
7516 }
7517 else if (whole_match_p(p, eos, len, indent)) {
7518 dispatch_heredoc_end(p);
7519 restore:
7520 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7521 token_flush(p);
7522 p->lex.strterm = 0;
7523 SET_LEX_STATE(EXPR_END);
7524 return tSTRING_END;
7525 }
7526
7527 if (!(func & STR_FUNC_EXPAND)) {
7528 do {
7529 ptr = RSTRING_PTR(p->lex.lastline);
7530 ptr_end = p->lex.pend;
7531 if (ptr_end > ptr) {
7532 switch (ptr_end[-1]) {
7533 case '\n':
7534 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7535 ptr_end++;
7536 break;
7537 }
7538 case '\r':
7539 --ptr_end;
7540 }
7541 }
7542
7543 if (p->heredoc_indent > 0) {
7544 long i = 0;
7545 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7546 i++;
7547 p->heredoc_line_indent = 0;
7548 }
7549
7550 if (str)
7551 rb_str_cat(str, ptr, ptr_end - ptr);
7552 else
7553 str = STR_NEW(ptr, ptr_end - ptr);
7554 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7555 lex_goto_eol(p);
7556 if (p->heredoc_indent > 0) {
7557 goto flush_str;
7558 }
7559 if (nextc(p) == -1) {
7560 if (str) {
7561 str = 0;
7562 }
7563 goto error;
7564 }
7565 } while (!whole_match_p(p, eos, len, indent));
7566 }
7567 else {
7568 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7569 newtok(p);
7570 if (c == '#') {
7571 int t = parser_peek_variable_name(p);
7572 if (p->heredoc_line_indent != -1) {
7573 if (p->heredoc_indent > p->heredoc_line_indent) {
7574 p->heredoc_indent = p->heredoc_line_indent;
7575 }
7576 p->heredoc_line_indent = -1;
7577 }
7578 if (t) return t;
7579 tokadd(p, '#');
7580 c = nextc(p);
7581 }
7582 do {
7583 pushback(p, c);
7584 enc = p->enc;
7585 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7586 if (p->eofp) goto error;
7587 goto restore;
7588 }
7589 if (c != '\n') {
7590 if (c == '\\') p->heredoc_line_indent = -1;
7591 flush:
7592 str = STR_NEW3(tok(p), toklen(p), enc, func);
7593 flush_str:
7594 set_yylval_str(str);
7595 #ifndef RIPPER
7596 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7597 #endif
7598 flush_string_content(p, enc);
7599 return tSTRING_CONTENT;
7600 }
7601 tokadd(p, nextc(p));
7602 if (p->heredoc_indent > 0) {
7603 lex_goto_eol(p);
7604 goto flush;
7605 }
7606 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7607 if ((c = nextc(p)) == -1) goto error;
7608 } while (!whole_match_p(p, eos, len, indent));
7609 str = STR_NEW3(tok(p), toklen(p), enc, func);
7610 }
7611 dispatch_heredoc_end(p);
7612 #ifdef RIPPER
7613 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7614 yylval.val, str);
7615 #endif
7616 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7617 token_flush(p);
7618 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7619 set_yylval_str(str);
7620 #ifndef RIPPER
7621 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7622 #endif
7623 return tSTRING_CONTENT;
7624 }
7625
7626 #include "lex.c"
7627
7628 static int
7629 arg_ambiguous(struct parser_params *p, char c)
7630 {
7631 #ifndef RIPPER
7632 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7633 #else
7634 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7635 #endif
7636 return TRUE;
7637 }
7638
7639 static ID
7640 formal_argument(struct parser_params *p, ID lhs)
7641 {
7642 switch (id_type(lhs)) {
7643 case ID_LOCAL:
7644 break;
7645 #ifndef RIPPER
7646 case ID_CONST:
7647 yyerror0("formal argument cannot be a constant");
7648 return 0;
7649 case ID_INSTANCE:
7650 yyerror0("formal argument cannot be an instance variable");
7651 return 0;
7652 case ID_GLOBAL:
7653 yyerror0("formal argument cannot be a global variable");
7654 return 0;
7655 case ID_CLASS:
7656 yyerror0("formal argument cannot be a class variable");
7657 return 0;
7658 default:
7659 yyerror0("formal argument must be local variable");
7660 return 0;
7661 #else
7662 default:
7663 lhs = dispatch1(param_error, lhs);
7664 ripper_error(p);
7665 return 0;
7666 #endif
7667 }
7668 shadowing_lvar(p, lhs);
7669 return lhs;
7670 }
7671
7672 static int
7673 lvar_defined(struct parser_params *p, ID id)
7674 {
7675 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7676 }
7677
7678 /* emacsen -*- hack */
7679 static long
7680 parser_encode_length(struct parser_params *p, const char *name, long len)
7681 {
7682 long nlen;
7683
7684 if (len > 5 && name[nlen = len - 5] == '-') {
7685 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7686 return nlen;
7687 }
7688 if (len > 4 && name[nlen = len - 4] == '-') {
7689 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7690 return nlen;
7691 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7692 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7693 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7694 return nlen;
7695 }
7696 return len;
7697 }
7698
7699 static void
7700 parser_set_encode(struct parser_params *p, const char *name)
7701 {
7702 int idx = rb_enc_find_index(name);
7703 rb_encoding *enc;
7704 VALUE excargs[3];
7705
7706 if (idx < 0) {
7707 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7708 error:
7709 excargs[0] = rb_eArgError;
7710 excargs[2] = rb_make_backtrace();
7711 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7712 rb_exc_raise(rb_make_exception(3, excargs));
7713 }
7714 enc = rb_enc_from_index(idx);
7715 if (!rb_enc_asciicompat(enc)) {
7716 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7717 goto error;
7718 }
7719 p->enc = enc;
7720 #ifndef RIPPER
7721 if (p->debug_lines) {
7722 VALUE lines = p->debug_lines;
7723 long i, n = RARRAY_LEN(lines);
7724 for (i = 0; i < n; ++i) {
7725 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7726 }
7727 }
7728 #endif
7729 }
7730
7731 static int
7732 comment_at_top(struct parser_params *p)
7733 {
7734 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7735 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7736 while (ptr < ptr_end) {
7737 if (!ISSPACE(*ptr)) return 0;
7738 ptr++;
7739 }
7740 return 1;
7741 }
7742
7743 typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7744 typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7745
7746 static void
7747 magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7748 {
7749 if (!comment_at_top(p)) {
7750 return;
7751 }
7752 parser_set_encode(p, val);
7753 }
7754
7755 static int
7756 parser_get_bool(struct parser_params *p, const char *name, const char *val)
7757 {
7758 switch (*val) {
7759 case 't': case 'T':
7760 if (strcasecmp(val, "true") == 0) {
7761 return TRUE;
7762 }
7763 break;
7764 case 'f': case 'F':
7765 if (strcasecmp(val, "false") == 0) {
7766 return FALSE;
7767 }
7768 break;
7769 }
7770 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7771 return -1;
7772 }
7773
7774 static void
7775 parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7776 {
7777 int b = parser_get_bool(p, name, val);
7778 if (b >= 0) p->token_info_enabled = b;
7779 }
7780
7781 static void
7782 parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7783 {
7784 int b;
7785
7786 if (p->token_seen) {
7787 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7788 return;
7789 }
7790
7791 b = parser_get_bool(p, name, val);
7792 if (b < 0) return;
7793
7794 if (!p->compile_option)
7795 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7796 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7797 (b ? Qtrue : Qfalse));
7798 }
7799
7800 # if WARN_PAST_SCOPE
7801 static void
7802 parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7803 {
7804 int b = parser_get_bool(p, name, val);
7805 if (b >= 0) p->past_scope_enabled = b;
7806 }
7807 # endif
7808
7809 struct magic_comment {
7810 const char *name;
7811 rb_magic_comment_setter_t func;
7812 rb_magic_comment_length_t length;
7813 };
7814
7815 static const struct magic_comment magic_comments[] = {
7816 {"coding", magic_comment_encoding, parser_encode_length},
7817 {"encoding", magic_comment_encoding, parser_encode_length},
7818 {"frozen_string_literal", parser_set_compile_option_flag},
7819 {"warn_indent", parser_set_token_info},
7820 # if WARN_PAST_SCOPE
7821 {"warn_past_scope", parser_set_past_scope},
7822 # endif
7823 };
7824
7825 static const char *
7826 magic_comment_marker(const char *str, long len)
7827 {
7828 long i = 2;
7829
7830 while (i < len) {
7831 switch (str[i]) {
7832 case '-':
7833 if (str[i-1] == '*' && str[i-2] == '-') {
7834 return str + i + 1;
7835 }
7836 i += 2;
7837 break;
7838 case '*':
7839 if (i + 1 >= len) return 0;
7840 if (str[i+1] != '-') {
7841 i += 4;
7842 }
7843 else if (str[i-1] != '-') {
7844 i += 2;
7845 }
7846 else {
7847 return str + i + 2;
7848 }
7849 break;
7850 default:
7851 i += 3;
7852 break;
7853 }
7854 }
7855 return 0;
7856 }
7857
7858 static int
7859 parser_magic_comment(struct parser_params *p, const char *str, long len)
7860 {
7861 int indicator = 0;
7862 VALUE name = 0, val = 0;
7863 const char *beg, *end, *vbeg, *vend;
7864 #define str_copy(_s, _p, _n) ((_s) \
7865 ? (void)(rb_str_resize((_s), (_n)), \
7866 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7867 : (void)((_s) = STR_NEW((_p), (_n))))
7868
7869 if (len <= 7) return FALSE;
7870 if (!!(beg = magic_comment_marker(str, len))) {
7871 if (!(end = magic_comment_marker(beg, str + len - beg)))
7872 return FALSE;
7873 indicator = TRUE;
7874 str = beg;
7875 len = end - beg - 3;
7876 }
7877
7878 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7879 while (len > 0) {
7880 const struct magic_comment *mc = magic_comments;
7881 char *s;
7882 int i;
7883 long n = 0;
7884
7885 for (; len > 0 && *str; str++, --len) {
7886 switch (*str) {
7887 case '\'': case '"': case ':': case ';':
7888 continue;
7889 }
7890 if (!ISSPACE(*str)) break;
7891 }
7892 for (beg = str; len > 0; str++, --len) {
7893 switch (*str) {
7894 case '\'': case '"': case ':': case ';':
7895 break;
7896 default:
7897 if (ISSPACE(*str)) break;
7898 continue;
7899 }
7900 break;
7901 }
7902 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7903 if (!len) break;
7904 if (*str != ':') {
7905 if (!indicator) return FALSE;
7906 continue;
7907 }
7908
7909 do str++; while (--len > 0 && ISSPACE(*str));
7910 if (!len) break;
7911 if (*str == '"') {
7912 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7913 if (*str == '\\') {
7914 --len;
7915 ++str;
7916 }
7917 }
7918 vend = str;
7919 if (len) {
7920 --len;
7921 ++str;
7922 }
7923 }
7924 else {
7925 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7926 vend = str;
7927 }
7928 if (indicator) {
7929 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7930 }
7931 else {
7932 while (len > 0 && (ISSPACE(*str))) --len, str++;
7933 if (len) return FALSE;
7934 }
7935
7936 n = end - beg;
7937 str_copy(name, beg, n);
7938 s = RSTRING_PTR(name);
7939 for (i = 0; i < n; ++i) {
7940 if (s[i] == '-') s[i] = '_';
7941 }
7942 do {
7943 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
7944 n = vend - vbeg;
7945 if (mc->length) {
7946 n = (*mc->length)(p, vbeg, n);
7947 }
7948 str_copy(val, vbeg, n);
7949 (*mc->func)(p, mc->name, RSTRING_PTR(val));
7950 break;
7951 }
7952 } while (++mc < magic_comments + numberof(magic_comments));
7953 #ifdef RIPPER
7954 str_copy(val, vbeg, vend - vbeg);
7955 dispatch2(magic_comment, name, val);
7956 #endif
7957 }
7958
7959 return TRUE;
7960 }
7961
7962 static void
7963 set_file_encoding(struct parser_params *p, const char *str, const char *send)
7964 {
7965 int sep = 0;
7966 const char *beg = str;
7967 VALUE s;
7968
7969 for (;;) {
7970 if (send - str <= 6) return;
7971 switch (str[6]) {
7972 case 'C': case 'c': str += 6; continue;
7973 case 'O': case 'o': str += 5; continue;
7974 case 'D': case 'd': str += 4; continue;
7975 case 'I': case 'i': str += 3; continue;
7976 case 'N': case 'n': str += 2; continue;
7977 case 'G': case 'g': str += 1; continue;
7978 case '=': case ':':
7979 sep = 1;
7980 str += 6;
7981 break;
7982 default:
7983 str += 6;
7984 if (ISSPACE(*str)) break;
7985 continue;
7986 }
7987 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7988 }
7989 for (;;) {
7990 do {
7991 if (++str >= send) return;
7992 } while (ISSPACE(*str));
7993 if (sep) break;
7994 if (*str != '=' && *str != ':') return;
7995 sep = 1;
7996 str++;
7997 }
7998 beg = str;
7999 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8000 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8001 parser_set_encode(p, RSTRING_PTR(s));
8002 rb_str_resize(s, 0);
8003 }
8004
8005 static void
8006 parser_prepare(struct parser_params *p)
8007 {
8008 int c = nextc(p);
8009 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8010 switch (c) {
8011 case '#':
8012 if (peek(p, '!')) p->has_shebang = 1;
8013 break;
8014 case 0xef: /* UTF-8 BOM marker */
8015 if (p->lex.pend - p->lex.pcur >= 2 &&
8016 (unsigned char)p->lex.pcur[0] == 0xbb &&
8017 (unsigned char)p->lex.pcur[1] == 0xbf) {
8018 p->enc = rb_utf8_encoding();
8019 p->lex.pcur += 2;
8020 p->lex.pbeg = p->lex.pcur;
8021 return;
8022 }
8023 break;
8024 case EOF:
8025 return;
8026 }
8027 pushback(p, c);
8028 p->enc = rb_enc_get(p->lex.lastline);
8029 }
8030
8031 #ifndef RIPPER
8032 #define ambiguous_operator(tok, op, syn) ( \
8033 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8034 rb_warning0("even though it seems like "syn""))
8035 #else
8036 #define ambiguous_operator(tok, op, syn) \
8037 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8038 #endif
8039 #define warn_balanced(tok, op, syn) ((void) \
8040 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8041 space_seen && !ISSPACE(c) && \
8042 (ambiguous_operator(tok, op, syn), 0)), \
8043 (enum yytokentype)(tok))
8044
8045 static VALUE
8046 parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8047 {
8048 VALUE v;
8049 char *point = &str[seen_point];
8050 size_t fraclen = len-seen_point-1;
8051 memmove(point, point+1, fraclen+1);
8052 v = rb_cstr_to_inum(str, 10, FALSE);
8053 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8054 }
8055
8056 static enum yytokentype
8057 no_digits(struct parser_params *p)
8058 {
8059 yyerror0("numeric literal without digits");
8060 if (peek(p, '_')) nextc(p);
8061 /* dummy 0, for tUMINUS_NUM at numeric */
8062 return set_integer_literal(p, INT2FIX(0), 0);
8063 }
8064
8065 static enum yytokentype
8066 parse_numeric(struct parser_params *p, int c)
8067 {
8068 int is_float, seen_point, seen_e, nondigit;
8069 int suffix;
8070
8071 is_float = seen_point = seen_e = nondigit = 0;
8072 SET_LEX_STATE(EXPR_END);
8073 newtok(p);
8074 if (c == '-' || c == '+') {
8075 tokadd(p, c);
8076 c = nextc(p);
8077 }
8078 if (c == '0') {
8079 int start = toklen(p);
8080 c = nextc(p);
8081 if (c == 'x' || c == 'X') {
8082 /* hexadecimal */
8083 c = nextc(p);
8084 if (c != -1 && ISXDIGIT(c)) {
8085 do {
8086 if (c == '_') {
8087 if (nondigit) break;
8088 nondigit = c;
8089 continue;
8090 }
8091 if (!ISXDIGIT(c)) break;
8092 nondigit = 0;
8093 tokadd(p, c);
8094 } while ((c = nextc(p)) != -1);
8095 }
8096 pushback(p, c);
8097 tokfix(p);
8098 if (toklen(p) == start) {
8099 return no_digits(p);
8100 }
8101 else if (nondigit) goto trailing_uc;
8102 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8103 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8104 }
8105 if (c == 'b' || c == 'B') {
8106 /* binary */
8107 c = nextc(p);
8108 if (c == '0' || c == '1') {
8109 do {
8110 if (c == '_') {
8111 if (nondigit) break;
8112 nondigit = c;
8113 continue;
8114 }
8115 if (c != '0' && c != '1') break;
8116 nondigit = 0;
8117 tokadd(p, c);
8118 } while ((c = nextc(p)) != -1);
8119 }
8120 pushback(p, c);
8121 tokfix(p);
8122 if (toklen(p) == start) {
8123 return no_digits(p);
8124 }
8125 else if (nondigit) goto trailing_uc;
8126 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8127 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8128 }
8129 if (c == 'd' || c == 'D') {
8130 /* decimal */
8131 c = nextc(p);
8132 if (c != -1 && ISDIGIT(c)) {
8133 do {
8134 if (c == '_') {
8135 if (nondigit) break;
8136 nondigit = c;
8137 continue;
8138 }
8139 if (!ISDIGIT(c)) break;
8140 nondigit = 0;
8141 tokadd(p, c);
8142 } while ((c = nextc(p)) != -1);
8143 }
8144 pushback(p, c);
8145 tokfix(p);
8146 if (toklen(p) == start) {
8147 return no_digits(p);
8148 }
8149 else if (nondigit) goto trailing_uc;
8150 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8151 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8152 }
8153 if (c == '_') {
8154 /* 0_0 */
8155 goto octal_number;
8156 }
8157 if (c == 'o' || c == 'O') {
8158 /* prefixed octal */
8159 c = nextc(p);
8160 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8161 return no_digits(p);
8162 }
8163 }
8164 if (c >= '0' && c <= '7') {
8165 /* octal */
8166 octal_number:
8167 do {
8168 if (c == '_') {
8169 if (nondigit) break;
8170 nondigit = c;
8171 continue;
8172 }
8173 if (c < '0' || c > '9') break;
8174 if (c > '7') goto invalid_octal;
8175 nondigit = 0;
8176 tokadd(p, c);
8177 } while ((c = nextc(p)) != -1);
8178 if (toklen(p) > start) {
8179 pushback(p, c);
8180 tokfix(p);
8181 if (nondigit) goto trailing_uc;
8182 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8183 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8184 }
8185 if (nondigit) {
8186 pushback(p, c);
8187 goto trailing_uc;
8188 }
8189 }
8190 if (c > '7' && c <= '9') {
8191 invalid_octal:
8192 yyerror0("Invalid octal digit");
8193 }
8194 else if (c == '.' || c == 'e' || c == 'E') {
8195 tokadd(p, '0');
8196 }
8197 else {
8198 pushback(p, c);
8199 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8200 return set_integer_literal(p, INT2FIX(0), suffix);
8201 }
8202 }
8203
8204 for (;;) {
8205 switch (c) {
8206 case '0': case '1': case '2': case '3': case '4':
8207 case '5': case '6': case '7': case '8': case '9':
8208 nondigit = 0;
8209 tokadd(p, c);
8210 break;
8211
8212 case '.':
8213 if (nondigit) goto trailing_uc;
8214 if (seen_point || seen_e) {
8215 goto decode_num;
8216 }
8217 else {
8218 int c0 = nextc(p);
8219 if (c0 == -1 || !ISDIGIT(c0)) {
8220 pushback(p, c0);
8221 goto decode_num;
8222 }
8223 c = c0;
8224 }
8225 seen_point = toklen(p);
8226 tokadd(p, '.');
8227 tokadd(p, c);
8228 is_float++;
8229 nondigit = 0;
8230 break;
8231
8232 case 'e':
8233 case 'E':
8234 if (nondigit) {
8235 pushback(p, c);
8236 c = nondigit;
8237 goto decode_num;
8238 }
8239 if (seen_e) {
8240 goto decode_num;
8241 }
8242 nondigit = c;
8243 c = nextc(p);
8244 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8245 pushback(p, c);
8246 nondigit = 0;
8247 goto decode_num;
8248 }
8249 tokadd(p, nondigit);
8250 seen_e++;
8251 is_float++;
8252 tokadd(p, c);
8253 nondigit = (c == '-' || c == '+') ? c : 0;
8254 break;
8255
8256 case '_': /* `_' in number just ignored */
8257 if (nondigit) goto decode_num;
8258 nondigit = c;
8259 break;
8260
8261 default:
8262 goto decode_num;
8263 }
8264 c = nextc(p);
8265 }
8266
8267 decode_num:
8268 pushback(p, c);
8269 if (nondigit) {
8270 trailing_uc:
8271 literal_flush(p, p->lex.pcur - 1);
8272 YYLTYPE loc = RUBY_INIT_YYLLOC();
8273 compile_error(p, "trailing `%c' in number", nondigit);
8274 parser_show_error_line(p, &loc);
8275 }
8276 tokfix(p);
8277 if (is_float) {
8278 enum yytokentype type = tFLOAT;
8279 VALUE v;
8280
8281 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8282 if (suffix & NUM_SUFFIX_R) {
8283 type = tRATIONAL;
8284 v = parse_rational(p, tok(p), toklen(p), seen_point);
8285 }
8286 else {
8287 double d = strtod(tok(p), 0);
8288 if (errno == ERANGE) {
8289 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8290 errno = 0;
8291 }
8292 v = DBL2NUM(d);
8293 }
8294 return set_number_literal(p, v, type, suffix);
8295 }
8296 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8297 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8298 }
8299
8300 static enum yytokentype
8301 parse_qmark(struct parser_params *p, int space_seen)
8302 {
8303 rb_encoding *enc;
8304 register int c;
8305 VALUE lit;
8306
8307 if (IS_END()) {
8308 SET_LEX_STATE(EXPR_VALUE);
8309 return '?';
8310 }
8311 c = nextc(p);
8312 if (c == -1) {
8313 compile_error(p, "incomplete character syntax");
8314 return 0;
8315 }
8316 if (rb_enc_isspace(c, p->enc)) {
8317 if (!IS_ARG()) {
8318 int c2 = escaped_control_code(c);
8319 if (c2) {
8320 WARN_SPACE_CHAR(c2, "?");
8321 }
8322 }
8323 ternary:
8324 pushback(p, c);
8325 SET_LEX_STATE(EXPR_VALUE);
8326 return '?';
8327 }
8328 newtok(p);
8329 enc = p->enc;
8330 if (!parser_isascii(p)) {
8331 if (tokadd_mbchar(p, c) == -1) return 0;
8332 }
8333 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8334 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8335 if (space_seen) {
8336 const char *start = p->lex.pcur - 1, *ptr = start;
8337 do {
8338 int n = parser_precise_mbclen(p, ptr);
8339 if (n < 0) return -1;
8340 ptr += n;
8341 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8342 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8343 " a conditional operator, put a space after `?'",
8344 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8345 }
8346 goto ternary;
8347 }
8348 else if (c == '\\') {
8349 if (peek(p, 'u')) {
8350 nextc(p);
8351 enc = rb_utf8_encoding();
8352 tokadd_utf8(p, &enc, -1, 0, 0);
8353 }
8354 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8355 nextc(p);
8356 if (tokadd_mbchar(p, c) == -1) return 0;
8357 }
8358 else {
8359 c = read_escape(p, 0, &enc);
8360 tokadd(p, c);
8361 }
8362 }
8363 else {
8364 tokadd(p, c);
8365 }
8366 tokfix(p);
8367 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8368 set_yylval_str(lit);
8369 SET_LEX_STATE(EXPR_END);
8370 return tCHAR;
8371 }
8372
8373 static enum yytokentype
8374 parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8375 {
8376 register int c;
8377 const char *ptok = p->lex.pcur;
8378
8379 if (IS_BEG()) {
8380 int term;
8381 int paren;
8382
8383 c = nextc(p);
8384 quotation:
8385 if (c == -1 || !ISALNUM(c)) {
8386 term = c;
8387 c = 'Q';
8388 }
8389 else {
8390 term = nextc(p);
8391 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8392 yyerror0("unknown type of %string");
8393 return 0;
8394 }
8395 }
8396 if (c == -1 || term == -1) {
8397 compile_error(p, "unterminated quoted string meets end of file");
8398 return 0;
8399 }
8400 paren = term;
8401 if (term == '(') term = ')';
8402 else if (term == '[') term = ']';
8403 else if (term == '{') term = '}';
8404 else if (term == '<') term = '>';
8405 else paren = 0;
8406
8407 p->lex.ptok = ptok-1;
8408 switch (c) {
8409 case 'Q':
8410 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8411 return tSTRING_BEG;
8412
8413 case 'q':
8414 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8415 return tSTRING_BEG;
8416
8417 case 'W':
8418 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8419 return tWORDS_BEG;
8420
8421 case 'w':
8422 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8423 return tQWORDS_BEG;
8424
8425 case 'I':
8426 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8427 return tSYMBOLS_BEG;
8428
8429 case 'i':
8430 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8431 return tQSYMBOLS_BEG;
8432
8433 case 'x':
8434 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8435 return tXSTRING_BEG;
8436
8437 case 'r':
8438 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8439 return tREGEXP_BEG;
8440
8441 case 's':
8442 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8443 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8444 return tSYMBEG;
8445
8446 default:
8447 yyerror0("unknown type of %string");
8448 return 0;
8449 }
8450 }
8451 if ((c = nextc(p)) == '=') {
8452 set_yylval_id('%');
8453 SET_LEX_STATE(EXPR_BEG);
8454 return tOP_ASGN;
8455 }
8456 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8457 goto quotation;
8458 }
8459 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8460 pushback(p, c);
8461 return warn_balanced('%', "%%", "string literal");
8462 }
8463
8464 static int
8465 tokadd_ident(struct parser_params *p, int c)
8466 {
8467 do {
8468 if (tokadd_mbchar(p, c) == -1) return -1;
8469 c = nextc(p);
8470 } while (parser_is_identchar(p));
8471 pushback(p, c);
8472 return 0;
8473 }
8474
8475 static ID
8476 tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8477 {
8478 ID ident = TOK_INTERN();
8479
8480 set_yylval_name(ident);
8481
8482 return ident;
8483 }
8484
8485 static int
8486 parse_numvar(struct parser_params *p)
8487 {
8488 size_t len;
8489 int overflow;
8490 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8491 const unsigned long nth_ref_max =
8492 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8493 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8494 * turned into a Fixnum, in compile.c */
8495
8496 if (overflow || n > nth_ref_max) {
8497 /* compile_error()? */
8498 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8499 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8500 }
8501 else {
8502 return (int)n;
8503 }
8504 }
8505
8506 static enum yytokentype
8507 parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8508 {
8509 const char *ptr = p->lex.pcur;
8510 register int c;
8511
8512 SET_LEX_STATE(EXPR_END);
8513 p->lex.ptok = ptr - 1; /* from '$' */
8514 newtok(p);
8515 c = nextc(p);
8516 switch (c) {
8517 case '_': /* $_: last read line string */
8518 c = nextc(p);
8519 if (parser_is_identchar(p)) {
8520 tokadd(p, '$');
8521 tokadd(p, '_');
8522 break;
8523 }
8524 pushback(p, c);
8525 c = '_';
8526 /* fall through */
8527 case '~': /* $~: match-data */
8528 case '*': /* $*: argv */
8529 case '$': /* $$: pid */
8530 case '?': /* $?: last status */
8531 case '!': /* $!: error string */
8532 case '@': /* $@: error position */
8533 case '/': /* $/: input record separator */
8534 case '\\': /* $\: output record separator */
8535 case ';': /* $;: field separator */
8536 case ',': /* $,: output field separator */
8537 case '.': /* $.: last read line number */
8538 case '=': /* $=: ignorecase */
8539 case ':': /* $:: load path */
8540 case '<': /* $<: reading filename */
8541 case '>': /* $>: default output handle */
8542 case '\"': /* $": already loaded files */
8543 tokadd(p, '$');
8544 tokadd(p, c);
8545 goto gvar;
8546
8547 case '-':
8548 tokadd(p, '$');
8549 tokadd(p, c);
8550 c = nextc(p);
8551 if (parser_is_identchar(p)) {
8552 if (tokadd_mbchar(p, c) == -1) return 0;
8553 }
8554 else {
8555 pushback(p, c);
8556 pushback(p, '-');
8557 return '$';
8558 }
8559 gvar:
8560 set_yylval_name(TOK_INTERN());
8561 return tGVAR;
8562
8563 case '&': /* $&: last match */
8564 case '`': /* $`: string before last match */
8565 case '\'': /* $': string after last match */
8566 case '+': /* $+: string matches last paren. */
8567 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8568 tokadd(p, '$');
8569 tokadd(p, c);
8570 goto gvar;
8571 }
8572 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8573 return tBACK_REF;
8574
8575 case '1': case '2': case '3':
8576 case '4': case '5': case '6':
8577 case '7': case '8': case '9':
8578 tokadd(p, '$');
8579 do {
8580 tokadd(p, c);
8581 c = nextc(p);
8582 } while (c != -1 && ISDIGIT(c));
8583 pushback(p, c);
8584 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8585 tokfix(p);
8586 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8587 return tNTH_REF;
8588
8589 default:
8590 if (!parser_is_identchar(p)) {
8591 YYLTYPE loc = RUBY_INIT_YYLLOC();
8592 if (c == -1 || ISSPACE(c)) {
8593 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8594 }
8595 else {
8596 pushback(p, c);
8597 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8598 }
8599 parser_show_error_line(p, &loc);
8600 set_yylval_noname();
8601 return tGVAR;
8602 }
8603 /* fall through */
8604 case '0':
8605 tokadd(p, '$');
8606 }
8607
8608 if (tokadd_ident(p, c)) return 0;
8609 SET_LEX_STATE(EXPR_END);
8610 tokenize_ident(p, last_state);
8611 return tGVAR;
8612 }
8613
8614 #ifndef RIPPER
8615 static bool
8616 parser_numbered_param(struct parser_params *p, int n)
8617 {
8618 if (n < 0) return false;
8619
8620 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8621 return false;
8622 }
8623 if (p->max_numparam == ORDINAL_PARAM) {
8624 compile_error(p, "ordinary parameter is defined");
8625 return false;
8626 }
8627 struct vtable *args = p->lvtbl->args;
8628 if (p->max_numparam < n) {
8629 p->max_numparam = n;
8630 }
8631 while (n > args->pos) {
8632 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8633 }
8634 return true;
8635 }
8636 #endif
8637
8638 static enum yytokentype
8639 parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8640 {
8641 const char *ptr = p->lex.pcur;
8642 enum yytokentype result = tIVAR;
8643 register int c = nextc(p);
8644 YYLTYPE loc;
8645
8646 p->lex.ptok = ptr - 1; /* from '@' */
8647 newtok(p);
8648 tokadd(p, '@');
8649 if (c == '@') {
8650 result = tCVAR;
8651 tokadd(p, '@');
8652 c = nextc(p);
8653 }
8654 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8655 if (c == -1 || !parser_is_identchar(p)) {
8656 pushback(p, c);
8657 RUBY_SET_YYLLOC(loc);
8658 if (result == tIVAR) {
8659 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8660 }
8661 else {
8662 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8663 }
8664 parser_show_error_line(p, &loc);
8665 set_yylval_noname();
8666 SET_LEX_STATE(EXPR_END);
8667 return result;
8668 }
8669 else if (ISDIGIT(c)) {
8670 pushback(p, c);
8671 RUBY_SET_YYLLOC(loc);
8672 if (result == tIVAR) {
8673 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8674 }
8675 else {
8676 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8677 }
8678 parser_show_error_line(p, &loc);
8679 set_yylval_noname();
8680 SET_LEX_STATE(EXPR_END);
8681 return result;
8682 }
8683
8684 if (tokadd_ident(p, c)) return 0;
8685 tokenize_ident(p, last_state);
8686 return result;
8687 }
8688
8689 static enum yytokentype
8690 parse_ident(struct parser_params *p, int c, int cmd_state)
8691 {
8692 enum yytokentype result;
8693 int mb = ENC_CODERANGE_7BIT;
8694 const enum lex_state_e last_state = p->lex.state;
8695 ID ident;
8696
8697 do {
8698 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8699 if (tokadd_mbchar(p, c) == -1) return 0;
8700 c = nextc(p);
8701 } while (parser_is_identchar(p));
8702 if ((c == '!' || c == '?') && !peek(p, '=')) {
8703 result = tFID;
8704 tokadd(p, c);
8705 }
8706 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8707 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8708 result = tIDENTIFIER;
8709 tokadd(p, c);
8710 }
8711 else {
8712 result = tCONSTANT; /* assume provisionally */
8713 pushback(p, c);
8714 }
8715 tokfix(p);
8716
8717 if (IS_LABEL_POSSIBLE()) {
8718 if (IS_LABEL_SUFFIX(0)) {
8719 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8720 nextc(p);
8721 set_yylval_name(TOK_INTERN());
8722 return tLABEL;
8723 }
8724 }
8725 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8726 const struct kwtable *kw;
8727
8728 /* See if it is a reserved word. */
8729 kw = rb_reserved_word(tok(p), toklen(p));
8730 if (kw) {
8731 enum lex_state_e state = p->lex.state;
8732 SET_LEX_STATE(kw->state);
8733 if (IS_lex_state_for(state, EXPR_FNAME)) {
8734 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8735 return kw->id[0];
8736 }
8737 if (IS_lex_state(EXPR_BEG)) {
8738 p->command_start = TRUE;
8739 }
8740 if (kw->id[0] == keyword_do) {
8741 if (lambda_beginning_p()) {
8742 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8743 return keyword_do_LAMBDA;
8744 }
8745 if (COND_P()) return keyword_do_cond;
8746 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8747 return keyword_do_block;
8748 return keyword_do;
8749 }
8750 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8751 return kw->id[0];
8752 else {
8753 if (kw->id[0] != kw->id[1])
8754 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8755 return kw->id[1];
8756 }
8757 }
8758 }
8759
8760 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8761 if (cmd_state) {
8762 SET_LEX_STATE(EXPR_CMDARG);
8763 }
8764 else {
8765 SET_LEX_STATE(EXPR_ARG);
8766 }
8767 }
8768 else if (p->lex.state == EXPR_FNAME) {
8769 SET_LEX_STATE(EXPR_ENDFN);
8770 }
8771 else {
8772 SET_LEX_STATE(EXPR_END);
8773 }
8774
8775 ident = tokenize_ident(p, last_state);
8776 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8777 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8778 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8779 lvar_defined(p, ident)) {
8780 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8781 }
8782 return result;
8783 }
8784
8785 static enum yytokentype
8786 parser_yylex(struct parser_params *p)
8787 {
8788 register int c;
8789 int space_seen = 0;
8790 int cmd_state;
8791 int label;
8792 enum lex_state_e last_state;
8793 int fallthru = FALSE;
8794 int token_seen = p->token_seen;
8795
8796 if (p->lex.strterm) {
8797 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8798 return here_document(p, &p->lex.strterm->u.heredoc);
8799 }
8800 else {
8801 token_flush(p);
8802 return parse_string(p, &p->lex.strterm->u.literal);
8803 }
8804 }
8805 cmd_state = p->command_start;
8806 p->command_start = FALSE;
8807 p->token_seen = TRUE;
8808 retry:
8809 last_state = p->lex.state;
8810 #ifndef RIPPER
8811 token_flush(p);
8812 #endif
8813 switch (c = nextc(p)) {
8814 case '\0': /* NUL */
8815 case '\004': /* ^D */
8816 case '\032': /* ^Z */
8817 case -1: /* end of script. */
8818 return 0;
8819
8820 /* white spaces */
8821 case ' ': case '\t': case '\f': case '\r':
8822 case '\13': /* '\v' */
8823 space_seen = 1;
8824 #ifdef RIPPER
8825 while ((c = nextc(p))) {
8826 switch (c) {
8827 case ' ': case '\t': case '\f': case '\r':
8828 case '\13': /* '\v' */
8829 break;
8830 default:
8831 goto outofloop;
8832 }
8833 }
8834 outofloop:
8835 pushback(p, c);
8836 dispatch_scan_event(p, tSP);
8837 #endif
8838 goto retry;
8839
8840 case '#': /* it's a comment */
8841 p->token_seen = token_seen;
8842 /* no magic_comment in shebang line */
8843 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8844 if (comment_at_top(p)) {
8845 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8846 }
8847 }
8848 lex_goto_eol(p);
8849 dispatch_scan_event(p, tCOMMENT);
8850 fallthru = TRUE;
8851 /* fall through */
8852 case '\n':
8853 p->token_seen = token_seen;
8854 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8855 !IS_lex_state(EXPR_LABELED));
8856 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8857 if (!fallthru) {
8858 dispatch_scan_event(p, tIGNORED_NL);
8859 }
8860 fallthru = FALSE;
8861 if (!c && p->in_kwarg) {
8862 goto normal_newline;
8863 }
8864 goto retry;
8865 }
8866 while (1) {
8867 switch (c = nextc(p)) {
8868 case ' ': case '\t': case '\f': case '\r':
8869 case '\13': /* '\v' */
8870 space_seen = 1;
8871 break;
8872 case '#':
8873 pushback(p, c);
8874 if (space_seen) dispatch_scan_event(p, tSP);
8875 goto retry;
8876 case '&':
8877 case '.': {
8878 dispatch_delayed_token(p, tIGNORED_NL);
8879 if (peek(p, '.') == (c == '&')) {
8880 pushback(p, c);
8881 dispatch_scan_event(p, tSP);
8882 goto retry;
8883 }
8884 }
8885 default:
8886 p->ruby_sourceline--;
8887 p->lex.nextline = p->lex.lastline;
8888 case -1: /* EOF no decrement*/
8889 #ifndef RIPPER
8890 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8891 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8892 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8893 pushback(p, 1); /* always pushback */
8894 p->lex.ptok = p->lex.pcur;
8895 #else
8896 lex_goto_eol(p);
8897 if (c != -1) {
8898 p->lex.ptok = p->lex.pcur;
8899 }
8900 #endif
8901 goto normal_newline;
8902 }
8903 }
8904 normal_newline:
8905 p->command_start = TRUE;
8906 SET_LEX_STATE(EXPR_BEG);
8907 return '\n';
8908
8909 case '*':
8910 if ((c = nextc(p)) == '*') {
8911 if ((c = nextc(p)) == '=') {
8912 set_yylval_id(idPow);
8913 SET_LEX_STATE(EXPR_BEG);
8914 return tOP_ASGN;
8915 }
8916 pushback(p, c);
8917 if (IS_SPCARG(c)) {
8918 rb_warning0("`**' interpreted as argument prefix");
8919 c = tDSTAR;
8920 }
8921 else if (IS_BEG()) {
8922 c = tDSTAR;
8923 }
8924 else {
8925 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8926 }
8927 }
8928 else {
8929 if (c == '=') {
8930 set_yylval_id('*');
8931 SET_LEX_STATE(EXPR_BEG);
8932 return tOP_ASGN;
8933 }
8934 pushback(p, c);
8935 if (IS_SPCARG(c)) {
8936 rb_warning0("`*' interpreted as argument prefix");
8937 c = tSTAR;
8938 }
8939 else if (IS_BEG()) {
8940 c = tSTAR;
8941 }
8942 else {
8943 c = warn_balanced('*', "*", "argument prefix");
8944 }
8945 }
8946 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8947 return c;
8948
8949 case '!':
8950 c = nextc(p);
8951 if (IS_AFTER_OPERATOR()) {
8952 SET_LEX_STATE(EXPR_ARG);
8953 if (c == '@') {
8954 return '!';
8955 }
8956 }
8957 else {
8958 SET_LEX_STATE(EXPR_BEG);
8959 }
8960 if (c == '=') {
8961 return tNEQ;
8962 }
8963 if (c == '~') {
8964 return tNMATCH;
8965 }
8966 pushback(p, c);
8967 return '!';
8968
8969 case '=':
8970 if (was_bol(p)) {
8971 /* skip embedded rd document */
8972 if (word_match_p(p, "begin", 5)) {
8973 int first_p = TRUE;
8974
8975 lex_goto_eol(p);
8976 dispatch_scan_event(p, tEMBDOC_BEG);
8977 for (;;) {
8978 lex_goto_eol(p);
8979 if (!first_p) {
8980 dispatch_scan_event(p, tEMBDOC);
8981 }
8982 first_p = FALSE;
8983 c = nextc(p);
8984 if (c == -1) {
8985 compile_error(p, "embedded document meets end of file");
8986 return 0;
8987 }
8988 if (c == '=' && word_match_p(p, "end", 3)) {
8989 break;
8990 }
8991 pushback(p, c);
8992 }
8993 lex_goto_eol(p);
8994 dispatch_scan_event(p, tEMBDOC_END);
8995 goto retry;
8996 }
8997 }
8998
8999 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9000 if ((c = nextc(p)) == '=') {
9001 if ((c = nextc(p)) == '=') {
9002 return tEQQ;
9003 }
9004 pushback(p, c);
9005 return tEQ;
9006 }
9007 if (c == '~') {
9008 return tMATCH;
9009 }
9010 else if (c == '>') {
9011 return tASSOC;
9012 }
9013 pushback(p, c);
9014 return '=';
9015
9016 case '<':
9017 c = nextc(p);
9018 if (c == '<' &&
9019 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9020 !IS_END() &&
9021 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9022 int token = heredoc_identifier(p);
9023 if (token) return token < 0 ? 0 : token;
9024 }
9025 if (IS_AFTER_OPERATOR()) {
9026 SET_LEX_STATE(EXPR_ARG);
9027 }
9028 else {
9029 if (IS_lex_state(EXPR_CLASS))
9030 p->command_start = TRUE;
9031 SET_LEX_STATE(EXPR_BEG);
9032 }
9033 if (c == '=') {
9034 if ((c = nextc(p)) == '>') {
9035 return tCMP;
9036 }
9037 pushback(p, c);
9038 return tLEQ;
9039 }
9040 if (c == '<') {
9041 if ((c = nextc(p)) == '=') {
9042 set_yylval_id(idLTLT);
9043 SET_LEX_STATE(EXPR_BEG);
9044 return tOP_ASGN;
9045 }
9046 pushback(p, c);
9047 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9048 }
9049 pushback(p, c);
9050 return '<';
9051
9052 case '>':
9053 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9054 if ((c = nextc(p)) == '=') {
9055 return tGEQ;
9056 }
9057 if (c == '>') {
9058 if ((c = nextc(p)) == '=') {
9059 set_yylval_id(idGTGT);
9060 SET_LEX_STATE(EXPR_BEG);
9061 return tOP_ASGN;
9062 }
9063 pushback(p, c);
9064 return tRSHFT;
9065 }
9066 pushback(p, c);
9067 return '>';
9068
9069 case '"':
9070 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9071 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9072 p->lex.ptok = p->lex.pcur-1;
9073 return tSTRING_BEG;
9074
9075 case '`':
9076 if (IS_lex_state(EXPR_FNAME)) {
9077 SET_LEX_STATE(EXPR_ENDFN);
9078 return c;
9079 }
9080 if (IS_lex_state(EXPR_DOT)) {
9081 if (cmd_state)
9082 SET_LEX_STATE(EXPR_CMDARG);
9083 else
9084 SET_LEX_STATE(EXPR_ARG);
9085 return c;
9086 }
9087 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9088 return tXSTRING_BEG;
9089
9090 case '\'':
9091 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9092 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9093 p->lex.ptok = p->lex.pcur-1;
9094 return tSTRING_BEG;
9095
9096 case '?':
9097 return parse_qmark(p, space_seen);
9098
9099 case '&':
9100 if ((c = nextc(p)) == '&') {
9101 SET_LEX_STATE(EXPR_BEG);
9102 if ((c = nextc(p)) == '=') {
9103 set_yylval_id(idANDOP);
9104 SET_LEX_STATE(EXPR_BEG);
9105 return tOP_ASGN;
9106 }
9107 pushback(p, c);
9108 return tANDOP;
9109 }
9110 else if (c == '=') {
9111 set_yylval_id('&');
9112 SET_LEX_STATE(EXPR_BEG);
9113 return tOP_ASGN;
9114 }
9115 else if (c == '.') {
9116 set_yylval_id(idANDDOT);
9117 SET_LEX_STATE(EXPR_DOT);
9118 return tANDDOT;
9119 }
9120 pushback(p, c);
9121 if (IS_SPCARG(c)) {
9122 if ((c != ':') ||
9123 (c = peekc_n(p, 1)) == -1 ||
9124 !(c == '\'' || c == '"' ||
9125 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9126 rb_warning0("`&' interpreted as argument prefix");
9127 }
9128 c = tAMPER;
9129 }
9130 else if (IS_BEG()) {
9131 c = tAMPER;
9132 }
9133 else {
9134 c = warn_balanced('&', "&", "argument prefix");
9135 }
9136 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9137 return c;
9138
9139 case '|':
9140 if ((c = nextc(p)) == '|') {
9141 SET_LEX_STATE(EXPR_BEG);
9142 if ((c = nextc(p)) == '=') {
9143 set_yylval_id(idOROP);
9144 SET_LEX_STATE(EXPR_BEG);
9145 return tOP_ASGN;
9146 }
9147 pushback(p, c);
9148 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9149 c = '|';
9150 pushback(p, '|');
9151 return c;
9152 }
9153 return tOROP;
9154 }
9155 if (c == '=') {
9156 set_yylval_id('|');
9157 SET_LEX_STATE(EXPR_BEG);
9158 return tOP_ASGN;
9159 }
9160 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9161 pushback(p, c);
9162 return '|';
9163
9164 case '+':
9165 c = nextc(p);
9166 if (IS_AFTER_OPERATOR()) {
9167 SET_LEX_STATE(EXPR_ARG);
9168 if (c == '@') {
9169 return tUPLUS;
9170 }
9171 pushback(p, c);
9172 return '+';
9173 }
9174 if (c == '=') {
9175 set_yylval_id('+');
9176 SET_LEX_STATE(EXPR_BEG);
9177 return tOP_ASGN;
9178 }
9179 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9180 SET_LEX_STATE(EXPR_BEG);
9181 pushback(p, c);
9182 if (c != -1 && ISDIGIT(c)) {
9183 return parse_numeric(p, '+');
9184 }
9185 return tUPLUS;
9186 }
9187 SET_LEX_STATE(EXPR_BEG);
9188 pushback(p, c);
9189 return warn_balanced('+', "+", "unary operator");
9190
9191 case '-':
9192 c = nextc(p);
9193 if (IS_AFTER_OPERATOR()) {
9194 SET_LEX_STATE(EXPR_ARG);
9195 if (c == '@') {
9196 return tUMINUS;
9197 }
9198 pushback(p, c);
9199 return '-';
9200 }
9201 if (c == '=') {
9202 set_yylval_id('-');
9203 SET_LEX_STATE(EXPR_BEG);
9204 return tOP_ASGN;
9205 }
9206 if (c == '>') {
9207 SET_LEX_STATE(EXPR_ENDFN);
9208 return tLAMBDA;
9209 }
9210 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9211 SET_LEX_STATE(EXPR_BEG);
9212 pushback(p, c);
9213 if (c != -1 && ISDIGIT(c)) {
9214 return tUMINUS_NUM;
9215 }
9216 return tUMINUS;
9217 }
9218 SET_LEX_STATE(EXPR_BEG);
9219 pushback(p, c);
9220 return warn_balanced('-', "-", "unary operator");
9221
9222 case '.': {
9223 int is_beg = IS_BEG();
9224 SET_LEX_STATE(EXPR_BEG);
9225 if ((c = nextc(p)) == '.') {
9226 if ((c = nextc(p)) == '.') {
9227 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9228 rb_warn0("... at EOL, should be parenthesized?");
9229 }
9230 return is_beg ? tBDOT3 : tDOT3;
9231 }
9232 pushback(p, c);
9233 return is_beg ? tBDOT2 : tDOT2;
9234 }
9235 pushback(p, c);
9236 if (c != -1 && ISDIGIT(c)) {
9237 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9238 parse_numeric(p, '.');
9239 if (ISDIGIT(prev)) {
9240 yyerror0("unexpected fraction part after numeric literal");
9241 }
9242 else {
9243 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9244 }
9245 SET_LEX_STATE(EXPR_END);
9246 p->lex.ptok = p->lex.pcur;
9247 goto retry;
9248 }
9249 set_yylval_id('.');
9250 SET_LEX_STATE(EXPR_DOT);
9251 return '.';
9252 }
9253
9254 case '0': case '1': case '2': case '3': case '4':
9255 case '5': case '6': case '7': case '8': case '9':
9256 return parse_numeric(p, c);
9257
9258 case ')':
9259 COND_POP();
9260 CMDARG_POP();
9261 SET_LEX_STATE(EXPR_ENDFN);
9262 p->lex.paren_nest--;
9263 return c;
9264
9265 case ']':
9266 COND_POP();
9267 CMDARG_POP();
9268 SET_LEX_STATE(EXPR_END);
9269 p->lex.paren_nest--;
9270 return c;
9271
9272 case '}':
9273 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9274 if (!p->lex.brace_nest--) return tSTRING_DEND;
9275 COND_POP();
9276 CMDARG_POP();
9277 SET_LEX_STATE(EXPR_END);
9278 p->lex.paren_nest--;
9279 return c;
9280
9281 case ':':
9282 c = nextc(p);
9283 if (c == ':') {
9284 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9285 SET_LEX_STATE(EXPR_BEG);
9286 return tCOLON3;
9287 }
9288 set_yylval_id(idCOLON2);
9289 SET_LEX_STATE(EXPR_DOT);
9290 return tCOLON2;
9291 }
9292 if (IS_END() || ISSPACE(c) || c == '#') {
9293 pushback(p, c);
9294 c = warn_balanced(':', ":", "symbol literal");
9295 SET_LEX_STATE(EXPR_BEG);
9296 return c;
9297 }
9298 switch (c) {
9299 case '\'':
9300 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9301 break;
9302 case '"':
9303 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9304 break;
9305 default:
9306 pushback(p, c);
9307 break;
9308 }
9309 SET_LEX_STATE(EXPR_FNAME);
9310 return tSYMBEG;
9311
9312 case '/':
9313 if (IS_BEG()) {
9314 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9315 return tREGEXP_BEG;
9316 }
9317 if ((c = nextc(p)) == '=') {
9318 set_yylval_id('/');
9319 SET_LEX_STATE(EXPR_BEG);
9320 return tOP_ASGN;
9321 }
9322 pushback(p, c);
9323 if (IS_SPCARG(c)) {
9324 arg_ambiguous(p, '/');
9325 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9326 return tREGEXP_BEG;
9327 }
9328 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9329 return warn_balanced('/', "/", "regexp literal");
9330
9331 case '^':
9332 if ((c = nextc(p)) == '=') {
9333 set_yylval_id('^');
9334 SET_LEX_STATE(EXPR_BEG);
9335 return tOP_ASGN;
9336 }
9337 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9338 pushback(p, c);
9339 return '^';
9340
9341 case ';':
9342 SET_LEX_STATE(EXPR_BEG);
9343 p->command_start = TRUE;
9344 return ';';
9345
9346 case ',':
9347 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9348 return ',';
9349
9350 case '~':
9351 if (IS_AFTER_OPERATOR()) {
9352 if ((c = nextc(p)) != '@') {
9353 pushback(p, c);
9354 }
9355 SET_LEX_STATE(EXPR_ARG);
9356 }
9357 else {
9358 SET_LEX_STATE(EXPR_BEG);
9359 }
9360 return '~';
9361
9362 case '(':
9363 if (IS_BEG()) {
9364 c = tLPAREN;
9365 }
9366 else if (!space_seen) {
9367 /* foo( ... ) => method call, no ambiguity */
9368 }
9369 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9370 c = tLPAREN_ARG;
9371 }
9372 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9373 rb_warning0("parentheses after method name is interpreted as "
9374 "an argument list, not a decomposed argument");
9375 }
9376 p->lex.paren_nest++;
9377 COND_PUSH(0);
9378 CMDARG_PUSH(0);
9379 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9380 return c;
9381
9382 case '[':
9383 p->lex.paren_nest++;
9384 if (IS_AFTER_OPERATOR()) {
9385 if ((c = nextc(p)) == ']') {
9386 SET_LEX_STATE(EXPR_ARG);
9387 if ((c = nextc(p)) == '=') {
9388 return tASET;
9389 }
9390 pushback(p, c);
9391 return tAREF;
9392 }
9393 pushback(p, c);
9394 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9395 return '[';
9396 }
9397 else if (IS_BEG()) {
9398 c = tLBRACK;
9399 }
9400 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9401 c = tLBRACK;
9402 }
9403 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9404 COND_PUSH(0);
9405 CMDARG_PUSH(0);
9406 return c;
9407
9408 case '{':
9409 ++p->lex.brace_nest;
9410 if (lambda_beginning_p())
9411 c = tLAMBEG;
9412 else if (IS_lex_state(EXPR_LABELED))
9413 c = tLBRACE; /* hash */
9414 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9415 c = '{'; /* block (primary) */
9416 else if (IS_lex_state(EXPR_ENDARG))
9417 c = tLBRACE_ARG; /* block (expr) */
9418 else
9419 c = tLBRACE; /* hash */
9420 if (c != tLBRACE) {
9421 p->command_start = TRUE;
9422 SET_LEX_STATE(EXPR_BEG);
9423 }
9424 else {
9425 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9426 }
9427 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9428 COND_PUSH(0);
9429 CMDARG_PUSH(0);
9430 return c;
9431
9432 case '\\':
9433 c = nextc(p);
9434 if (c == '\n') {
9435 space_seen = 1;
9436 dispatch_scan_event(p, tSP);
9437 goto retry; /* skip \\n */
9438 }
9439 if (c == ' ') return tSP;
9440 if (ISSPACE(c)) return c;
9441 pushback(p, c);
9442 return '\\';
9443
9444 case '%':
9445 return parse_percent(p, space_seen, last_state);
9446
9447 case '$':
9448 return parse_gvar(p, last_state);
9449
9450 case '@':
9451 return parse_atmark(p, last_state);
9452
9453 case '_':
9454 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9455 p->ruby__end__seen = 1;
9456 p->eofp = 1;
9457 #ifndef RIPPER
9458 return -1;
9459 #else
9460 lex_goto_eol(p);
9461 dispatch_scan_event(p, k__END__);
9462 return 0;
9463 #endif
9464 }
9465 newtok(p);
9466 break;
9467
9468 default:
9469 if (!parser_is_identchar(p)) {
9470 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9471 token_flush(p);
9472 goto retry;
9473 }
9474
9475 newtok(p);
9476 break;
9477 }
9478
9479 return parse_ident(p, c, cmd_state);
9480 }
9481
9482 static enum yytokentype
9483 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9484 {
9485 enum yytokentype t;
9486
9487 p->lval = lval;
9488 lval->val = Qundef;
9489 t = parser_yylex(p);
9490 if (has_delayed_token(p))
9491 dispatch_delayed_token(p, t);
9492 else if (t != 0)
9493 dispatch_scan_event(p, t);
9494
9495 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9496 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9497 else
9498 RUBY_SET_YYLLOC(*yylloc);
9499
9500 return t;
9501 }
9502
9503 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9504
9505 static NODE*
9506 node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9507 {
9508 NODE *n = rb_ast_newnode(p->ast, type);
9509
9510 rb_node_init(n, type, a0, a1, a2);
9511
9512 nd_set_loc(n, loc);
9513 nd_set_node_id(n, parser_get_node_id(p));
9514 return n;
9515 }
9516
9517 static NODE *
9518 nd_set_loc(NODE *nd, const YYLTYPE *loc)
9519 {
9520 nd->nd_loc = *loc;
9521 nd_set_line(nd, loc->beg_pos.lineno);
9522 return nd;
9523 }
9524
9525 #ifndef RIPPER
9526 static enum node_type
9527 nodetype(NODE *node) /* for debug */
9528 {
9529 return (enum node_type)nd_type(node);
9530 }
9531
9532 static int
9533 nodeline(NODE *node)
9534 {
9535 return nd_line(node);
9536 }
9537
9538 static NODE*
9539 newline_node(NODE *node)
9540 {
9541 if (node) {
9542 node = remove_begin(node);
9543 node->flags |= NODE_FL_NEWLINE;
9544 }
9545 return node;
9546 }
9547
9548 static void
9549 fixpos(NODE *node, NODE *orig)
9550 {
9551 if (!node) return;
9552 if (!orig) return;
9553 nd_set_line(node, nd_line(orig));
9554 }
9555
9556 static void
9557 parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9558 {
9559 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9560 }
9561
9562 static void
9563 parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9564 {
9565 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9566 }
9567
9568 static NODE*
9569 block_append(struct parser_params *p, NODE *head, NODE *tail)
9570 {
9571 NODE *end, *h = head, *nd;
9572
9573 if (tail == 0) return head;
9574
9575 if (h == 0) return tail;
9576 switch (nd_type(h)) {
9577 case NODE_LIT:
9578 case NODE_STR:
9579 case NODE_SELF:
9580 case NODE_TRUE:
9581 case NODE_FALSE:
9582 case NODE_NIL:
9583 parser_warning(p, h, "unused literal ignored");
9584 return tail;
9585 default:
9586 h = end = NEW_BLOCK(head, &head->nd_loc);
9587 end->nd_end = end;
9588 head = end;
9589 break;
9590 case NODE_BLOCK:
9591 end = h->nd_end;
9592 break;
9593 }
9594
9595 nd = end->nd_head;
9596 switch (nd_type(nd)) {
9597 case NODE_RETURN:
9598 case NODE_BREAK:
9599 case NODE_NEXT:
9600 case NODE_REDO:
9601 case NODE_RETRY:
9602 if (RTEST(ruby_verbose)) {
9603 parser_warning(p, tail, "statement not reached");
9604 }
9605 break;
9606
9607 default:
9608 break;
9609 }
9610
9611 if (nd_type(tail) != NODE_BLOCK) {
9612 tail = NEW_BLOCK(tail, &tail->nd_loc);
9613 tail->nd_end = tail;
9614 }
9615 end->nd_next = tail;
9616 h->nd_end = tail->nd_end;
9617 nd_set_last_loc(head, nd_last_loc(tail));
9618 return head;
9619 }
9620
9621 /* append item to the list */
9622 static NODE*
9623 list_append(struct parser_params *p, NODE *list, NODE *item)
9624 {
9625 NODE *last;
9626
9627 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9628 if (list->nd_next) {
9629 last = list->nd_next->nd_end;
9630 }
9631 else {
9632 last = list;
9633 }
9634
9635 list->nd_alen += 1;
9636 last->nd_next = NEW_LIST(item, &item->nd_loc);
9637 list->nd_next->nd_end = last->nd_next;
9638
9639 nd_set_last_loc(list, nd_last_loc(item));
9640
9641 return list;
9642 }
9643
9644 /* concat two lists */
9645 static NODE*
9646 list_concat(NODE *head, NODE *tail)
9647 {
9648 NODE *last;
9649
9650 if (head->nd_next) {
9651 last = head->nd_next->nd_end;
9652 }
9653 else {
9654 last = head;
9655 }
9656
9657 head->nd_alen += tail->nd_alen;
9658 last->nd_next = tail;
9659 if (tail->nd_next) {
9660 head->nd_next->nd_end = tail->nd_next->nd_end;
9661 }
9662 else {
9663 head->nd_next->nd_end = tail;
9664 }
9665
9666 nd_set_last_loc(head, nd_last_loc(tail));
9667
9668 return head;
9669 }
9670
9671 static int
9672 literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9673 {
9674 if (NIL_P(tail)) return 1;
9675 if (!rb_enc_compatible(head, tail)) {
9676 compile_error(p, "string literal encodings differ (%s / %s)",
9677 rb_enc_name(rb_enc_get(head)),
9678 rb_enc_name(rb_enc_get(tail)));
9679 rb_str_resize(head, 0);
9680 rb_str_resize(tail, 0);
9681 return 0;
9682 }
9683 rb_str_buf_append(head, tail);
9684 return 1;
9685 }
9686
9687 /* concat two string literals */
9688 static NODE *
9689 literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9690 {
9691 enum node_type htype;
9692 NODE *headlast;
9693 VALUE lit;
9694
9695 if (!head) return tail;
9696 if (!tail) return head;
9697
9698 htype = nd_type(head);
9699 if (htype == NODE_EVSTR) {
9700 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9701 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9702 head = list_append(p, node, head);
9703 htype = NODE_DSTR;
9704 }
9705 if (p->heredoc_indent > 0) {
9706 switch (htype) {
9707 case NODE_STR:
9708 nd_set_type(head, NODE_DSTR);
9709 case NODE_DSTR:
9710 return list_append(p, head, tail);
9711 default:
9712 break;
9713 }
9714 }
9715 switch (nd_type(tail)) {
9716 case NODE_STR:
9717 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9718 nd_type(headlast) == NODE_STR) {
9719 htype = NODE_STR;
9720 lit = headlast->nd_lit;
9721 }
9722 else {
9723 lit = head->nd_lit;
9724 }
9725 if (htype == NODE_STR) {
9726 if (!literal_concat0(p, lit, tail->nd_lit)) {
9727 error:
9728 rb_discard_node(p, head);
9729 rb_discard_node(p, tail);
9730 return 0;
9731 }
9732 rb_discard_node(p, tail);
9733 }
9734 else {
9735 list_append(p, head, tail);
9736 }
9737 break;
9738
9739 case NODE_DSTR:
9740 if (htype == NODE_STR) {
9741 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9742 goto error;
9743 tail->nd_lit = head->nd_lit;
9744 rb_discard_node(p, head);
9745 head = tail;
9746 }
9747 else if (NIL_P(tail->nd_lit)) {
9748 append:
9749 head->nd_alen += tail->nd_alen - 1;
9750 head->nd_next->nd_end->nd_next = tail->nd_next;
9751 head->nd_next->nd_end = tail->nd_next->nd_end;
9752 rb_discard_node(p, tail);
9753 }
9754 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9755 nd_type(headlast) == NODE_STR) {
9756 lit = headlast->nd_lit;
9757 if (!literal_concat0(p, lit, tail->nd_lit))
9758 goto error;
9759 tail->nd_lit = Qnil;
9760 goto append;
9761 }
9762 else {
9763 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9764 }
9765 break;
9766
9767 case NODE_EVSTR:
9768 if (htype == NODE_STR) {
9769 nd_set_type(head, NODE_DSTR);
9770 head->nd_alen = 1;
9771 }
9772 list_append(p, head, tail);
9773 break;
9774 }
9775 return head;
9776 }
9777
9778 static NODE *
9779 evstr2dstr(struct parser_params *p, NODE *node)
9780 {
9781 if (nd_type(node) == NODE_EVSTR) {
9782 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9783 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9784 node = list_append(p, dstr, node);
9785 }
9786 return node;
9787 }
9788
9789 static NODE *
9790 new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9791 {
9792 NODE *head = node;
9793
9794 if (node) {
9795 switch (nd_type(node)) {
9796 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9797 return node;
9798 }
9799 }
9800 return NEW_EVSTR(head, loc);
9801 }
9802
9803 static NODE *
9804 call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9805 const YYLTYPE *op_loc, const YYLTYPE *loc)
9806 {
9807 NODE *expr;
9808 value_expr(recv);
9809 value_expr(arg1);
9810 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9811 nd_set_line(expr, op_loc->beg_pos.lineno);
9812 return expr;
9813 }
9814
9815 static NODE *
9816 call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9817 {
9818 NODE *opcall;
9819 value_expr(recv);
9820 opcall = NEW_OPCALL(recv, id, 0, loc);
9821 nd_set_line(opcall, op_loc->beg_pos.lineno);
9822 return opcall;
9823 }
9824
9825 static NODE *
9826 new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9827 {
9828 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9829 nd_set_line(qcall, op_loc->beg_pos.lineno);
9830 return qcall;
9831 }
9832
9833 static NODE*
9834 new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9835 {
9836 NODE *ret;
9837 if (block) block_dup_check(p, args, block);
9838 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9839 if (block) ret = method_add_block(p, ret, block, loc);
9840 fixpos(ret, recv);
9841 return ret;
9842 }
9843
9844 #define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9845 static NODE*
9846 match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9847 {
9848 NODE *n;
9849 int line = op_loc->beg_pos.lineno;
9850
9851 value_expr(node1);
9852 value_expr(node2);
9853 if (node1 && (n = nd_once_body(node1)) != 0) {
9854 switch (nd_type(n)) {
9855 case NODE_DREGX:
9856 {
9857 NODE *match = NEW_MATCH2(node1, node2, loc);
9858 nd_set_line(match, line);
9859 return match;
9860 }
9861
9862 case NODE_LIT:
9863 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9864 const VALUE lit = n->nd_lit;
9865 NODE *match = NEW_MATCH2(node1, node2, loc);
9866 match->nd_args = reg_named_capture_assign(p, lit, loc);
9867 nd_set_line(match, line);
9868 return match;
9869 }
9870 }
9871 }
9872
9873 if (node2 && (n = nd_once_body(node2)) != 0) {
9874 NODE *match3;
9875
9876 switch (nd_type(n)) {
9877 case NODE_LIT:
9878 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9879 /* fallthru */
9880 case NODE_DREGX:
9881 match3 = NEW_MATCH3(node2, node1, loc);
9882 return match3;
9883 }
9884 }
9885
9886 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9887 nd_set_line(n, line);
9888 return n;
9889 }
9890
9891 # if WARN_PAST_SCOPE
9892 static int
9893 past_dvar_p(struct parser_params *p, ID id)
9894 {
9895 struct vtable *past = p->lvtbl->past;
9896 while (past) {
9897 if (vtable_included(past, id)) return 1;
9898 past = past->prev;
9899 }
9900 return 0;
9901 }
9902 # endif
9903
9904 /* As Ripper#warn does not have arguments for the location, so the
9905 * following messages cannot be separated */
9906 #define WARN_LOCATION(type) do { \
9907 if (p->warn_location) { \
9908 int line; \
9909 VALUE file = rb_source_location(&line); \
9910 rb_warn3(type" in eval may not return location in binding;" \
9911 " use Binding#source_location instead\n" \
9912 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9913 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9914 } \
9915 } while (0)
9916
9917 static int
9918 numparam_nested_p(struct parser_params *p)
9919 {
9920 struct local_vars *local = p->lvtbl;
9921 NODE *outer = local->numparam.outer;
9922 NODE *inner = local->numparam.inner;
9923 if (outer || inner) {
9924 NODE *used = outer ? outer : inner;
9925 compile_error(p, "numbered parameter is already used in\n"
9926 "%s:%d: %s block here",
9927 p->ruby_sourcefile, nd_line(used),
9928 outer ? "outer" : "inner");
9929 parser_show_error_line(p, &used->nd_loc);
9930 return 1;
9931 }
9932 return 0;
9933 }
9934
9935 static NODE*
9936 gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
9937 {
9938 ID *vidp = NULL;
9939 NODE *node;
9940 switch (id) {
9941 case keyword_self:
9942 return NEW_SELF(loc);
9943 case keyword_nil:
9944 return NEW_NIL(loc);
9945 case keyword_true:
9946 return NEW_TRUE(loc);
9947 case keyword_false:
9948 return NEW_FALSE(loc);
9949 case keyword__FILE__:
9950 WARN_LOCATION("__FILE__");
9951 {
9952 VALUE file = p->ruby_sourcefile_string;
9953 if (NIL_P(file))
9954 file = rb_str_new(0, 0);
9955 else
9956 file = rb_str_dup(file);
9957 node = NEW_STR(file, loc);
9958 RB_OBJ_WRITTEN(p->ast, Qnil, file);
9959 }
9960 return node;
9961 case keyword__LINE__:
9962 WARN_LOCATION("__LINE__");
9963 return NEW_LIT(INT2FIX(p->tokline), loc);
9964 case keyword__ENCODING__:
9965 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
9966 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9967 return node;
9968
9969 }
9970 switch (id_type(id)) {
9971 case ID_LOCAL:
9972 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
9973 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
9974 if (id == p->cur_arg) {
9975 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9976 return 0;
9977 }
9978 if (vidp) *vidp |= LVAR_USED;
9979 node = NEW_DVAR(id, loc);
9980 return node;
9981 }
9982 if (local_id_ref(p, id, &vidp)) {
9983 if (id == p->cur_arg) {
9984 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
9985 return 0;
9986 }
9987 if (vidp) *vidp |= LVAR_USED;
9988 node = NEW_LVAR(id, loc);
9989 return node;
9990 }
9991 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
9992 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
9993 if (numparam_nested_p(p)) return 0;
9994 node = NEW_DVAR(id, loc);
9995 struct local_vars *local = p->lvtbl;
9996 if (!local->numparam.current) local->numparam.current = node;
9997 return node;
9998 }
9999 # if WARN_PAST_SCOPE
10000 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10001 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10002 }
10003 # endif
10004 /* method call without arguments */
10005 return NEW_VCALL(id, loc);
10006 case ID_GLOBAL:
10007 return NEW_GVAR(id, loc);
10008 case ID_INSTANCE:
10009 return NEW_IVAR(id, loc);
10010 case ID_CONST:
10011 return NEW_CONST(id, loc);
10012 case ID_CLASS:
10013 return NEW_CVAR(id, loc);
10014 }
10015 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10016 return 0;
10017 }
10018
10019 static NODE *
10020 opt_arg_append(NODE *opt_list, NODE *opt)
10021 {
10022 NODE *opts = opt_list;
10023 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10024
10025 while (opts->nd_next) {
10026 opts = opts->nd_next;
10027 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10028 }
10029 opts->nd_next = opt;
10030
10031 return opt_list;
10032 }
10033
10034 static NODE *
10035 kwd_append(NODE *kwlist, NODE *kw)
10036 {
10037 if (kwlist) {
10038 NODE *kws = kwlist;
10039 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10040 while (kws->nd_next) {
10041 kws = kws->nd_next;
10042 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10043 }
10044 kws->nd_next = kw;
10045 }
10046 return kwlist;
10047 }
10048
10049 static NODE *
10050 new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10051 {
10052 return NEW_DEFINED(remove_begin_all(expr), loc);
10053 }
10054
10055 static NODE*
10056 symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10057 {
10058 if (nd_type(symbol) == NODE_DSTR) {
10059 nd_set_type(symbol, NODE_DSYM);
10060 }
10061 else {
10062 nd_set_type(symbol, NODE_LIT);
10063 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10064 }
10065 return list_append(p, symbols, symbol);
10066 }
10067
10068 static NODE *
10069 new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10070 {
10071 NODE *list, *prev;
10072 VALUE lit;
10073
10074 if (!node) {
10075 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10076 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10077 return node;
10078 }
10079 switch (nd_type(node)) {
10080 case NODE_STR:
10081 {
10082 VALUE src = node->nd_lit;
10083 nd_set_type(node, NODE_LIT);
10084 nd_set_loc(node, loc);
10085 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10086 }
10087 break;
10088 default:
10089 lit = STR_NEW0();
10090 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10091 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10092 /* fall through */
10093 case NODE_DSTR:
10094 nd_set_type(node, NODE_DREGX);
10095 nd_set_loc(node, loc);
10096 node->nd_cflag = options & RE_OPTION_MASK;
10097 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10098 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10099 if (nd_type(list->nd_head) == NODE_STR) {
10100 VALUE tail = list->nd_head->nd_lit;
10101 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10102 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10103 if (!literal_concat0(p, lit, tail)) {
10104 return NEW_NIL(loc); /* dummy node on error */
10105 }
10106 rb_str_resize(tail, 0);
10107 prev->nd_next = list->nd_next;
10108 rb_discard_node(p, list->nd_head);
10109 rb_discard_node(p, list);
10110 list = prev;
10111 }
10112 else {
10113 prev = list;
10114 }
10115 }
10116 else {
10117 prev = 0;
10118 }
10119 }
10120 if (!node->nd_next) {
10121 VALUE src = node->nd_lit;
10122 nd_set_type(node, NODE_LIT);
10123 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10124 }
10125 if (options & RE_OPTION_ONCE) {
10126 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10127 }
10128 break;
10129 }
10130 return node;
10131 }
10132
10133 static NODE *
10134 new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10135 {
10136 if (!k) return 0;
10137 return NEW_KW_ARG(0, (k), loc);
10138 }
10139
10140 static NODE *
10141 new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10142 {
10143 if (!node) {
10144 VALUE lit = STR_NEW0();
10145 NODE *xstr = NEW_XSTR(lit, loc);
10146 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10147 return xstr;
10148 }
10149 switch (nd_type(node)) {
10150 case NODE_STR:
10151 nd_set_type(node, NODE_XSTR);
10152 nd_set_loc(node, loc);
10153 break;
10154 case NODE_DSTR:
10155 nd_set_type(node, NODE_DXSTR);
10156 nd_set_loc(node, loc);
10157 break;
10158 default:
10159 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10160 break;
10161 }
10162 return node;
10163 }
10164
10165 static void
10166 check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10167 {
10168 VALUE lit;
10169
10170 if (!arg || !p->case_labels) return;
10171
10172 lit = rb_node_case_when_optimizable_literal(arg);
10173 if (lit == Qundef) return;
10174 if (nd_type(arg) == NODE_STR) {
10175 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10176 }
10177
10178 if (NIL_P(p->case_labels)) {
10179 p->case_labels = rb_obj_hide(rb_hash_new());
10180 }
10181 else {
10182 VALUE line = rb_hash_lookup(p->case_labels, lit);
10183 if (!NIL_P(line)) {
10184 rb_warning1("duplicated `when' clause with line %d is ignored",
10185 WARN_IVAL(line));
10186 return;
10187 }
10188 }
10189 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10190 }
10191
10192 #else /* !RIPPER */
10193 static int
10194 id_is_var(struct parser_params *p, ID id)
10195 {
10196 if (is_notop_id(id)) {
10197 switch (id & ID_SCOPE_MASK) {
10198 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10199 return 1;
10200 case ID_LOCAL:
10201 if (dyna_in_block(p)) {
10202 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10203 }
10204 if (local_id(p, id)) return 1;
10205 /* method call without arguments */
10206 return 0;
10207 }
10208 }
10209 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10210 return 0;
10211 }
10212
10213 static VALUE
10214 new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10215 {
10216 VALUE src = 0, err;
10217 int options = 0;
10218 if (ripper_is_node_yylval(re)) {
10219 src = RNODE(re)->nd_cval;
10220 re = RNODE(re)->nd_rval;
10221 }
10222 if (ripper_is_node_yylval(opt)) {
10223 options = (int)RNODE(opt)->nd_tag;
10224 opt = RNODE(opt)->nd_rval;
10225 }
10226 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10227 compile_error(p, "%"PRIsVALUE, err);
10228 }
10229 return dispatch2(regexp_literal, re, opt);
10230 }
10231 #endif /* !RIPPER */
10232
10233
10234 #ifndef RIPPER
10235 static const char rb_parser_lex_state_names[][8] = {
10236 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10237 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10238 "LABEL", "LABELED","FITEM",
10239 };
10240
10241 static VALUE
10242 append_lex_state_name(enum lex_state_e state, VALUE buf)
10243 {
10244 int i, sep = 0;
10245 unsigned int mask = 1;
10246 static const char none[] = "NONE";
10247
10248 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10249 if ((unsigned)state & mask) {
10250 if (sep) {
10251 rb_str_cat(buf, "|", 1);
10252 }
10253 sep = 1;
10254 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10255 }
10256 }
10257 if (!sep) {
10258 rb_str_cat(buf, none, sizeof(none)-1);
10259 }
10260 return buf;
10261 }
10262
10263 static void
10264 flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10265 {
10266 VALUE mesg = p->debug_buffer;
10267
10268 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10269 p->debug_buffer = Qnil;
10270 rb_io_puts(1, &mesg, out);
10271 }
10272 if (!NIL_P(str) && RSTRING_LEN(str)) {
10273 rb_io_write(p->debug_output, str);
10274 }
10275 }
10276
10277 enum lex_state_e
10278 rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10279 enum lex_state_e to, int line)
10280 {
10281 VALUE mesg;
10282 mesg = rb_str_new_cstr("lex_state: ");
10283 append_lex_state_name(from, mesg);
10284 rb_str_cat_cstr(mesg, " -> ");
10285 append_lex_state_name(to, mesg);
10286 rb_str_catf(mesg, " at line %d\n", line);
10287 flush_debug_buffer(p, p->debug_output, mesg);
10288 return to;
10289 }
10290
10291 VALUE
10292 rb_parser_lex_state_name(enum lex_state_e state)
10293 {
10294 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10295 }
10296
10297 static void
10298 append_bitstack_value(stack_type stack, VALUE mesg)
10299 {
10300 if (stack == 0) {
10301 rb_str_cat_cstr(mesg, "0");
10302 }
10303 else {
10304 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10305 for (; mask && !(stack & mask); mask >>= 1) continue;
10306 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10307 }
10308 }
10309
10310 void
10311 rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10312 const char *name, int line)
10313 {
10314 VALUE mesg = rb_sprintf("%s: ", name);
10315 append_bitstack_value(stack, mesg);
10316 rb_str_catf(mesg, " at line %d\n", line);
10317 flush_debug_buffer(p, p->debug_output, mesg);
10318 }
10319
10320 void
10321 rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10322 {
10323 va_list ap;
10324 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10325
10326 va_start(ap, fmt);
10327 rb_str_vcatf(mesg, fmt, ap);
10328 va_end(ap);
10329 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10330 RB_GC_GUARD(mesg);
10331
10332 mesg = rb_str_new(0, 0);
10333 append_lex_state_name(p->lex.state, mesg);
10334 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10335 rb_str_resize(mesg, 0);
10336 append_bitstack_value(p->cond_stack, mesg);
10337 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10338 rb_str_resize(mesg, 0);
10339 append_bitstack_value(p->cmdarg_stack, mesg);
10340 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10341 if (p->debug_output == rb_stdout)
10342 p->debug_output = rb_stderr;
10343 p->debug = TRUE;
10344 }
10345
10346 YYLTYPE *
10347 rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10348 {
10349 int sourceline = here->sourceline;
10350 int beg_pos = (int)here->offset - here->quote
10351 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10352 int end_pos = (int)here->offset + here->length + here->quote;
10353
10354 yylloc->beg_pos.lineno = sourceline;
10355 yylloc->beg_pos.column = beg_pos;
10356 yylloc->end_pos.lineno = sourceline;
10357 yylloc->end_pos.column = end_pos;
10358 return yylloc;
10359 }
10360
10361 YYLTYPE *
10362 rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10363 {
10364 yylloc->beg_pos.lineno = p->ruby_sourceline;
10365 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10366 yylloc->end_pos.lineno = p->ruby_sourceline;
10367 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10368 return yylloc;
10369 }
10370
10371 YYLTYPE *
10372 rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10373 {
10374 yylloc->beg_pos.lineno = p->ruby_sourceline;
10375 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10376 yylloc->end_pos.lineno = p->ruby_sourceline;
10377 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10378 return yylloc;
10379 }
10380 #endif /* !RIPPER */
10381
10382 static void
10383 parser_token_value_print(struct parser_params *p, enum yytokentype type, const YYSTYPE *valp)
10384 {
10385 VALUE v;
10386
10387 switch (type) {
10388 case tIDENTIFIER: case tFID: case tGVAR: case tIVAR:
10389 case tCONSTANT: case tCVAR: case tLABEL: case tOP_ASGN:
10390 #ifndef RIPPER
10391 v = rb_id2str(valp->id);
10392 #else
10393 v = valp->node->nd_rval;
10394 #endif
10395 rb_parser_printf(p, "%"PRIsVALUE, v);
10396 break;
10397 case tINTEGER: case tFLOAT: case tRATIONAL: case tIMAGINARY:
10398 case tSTRING_CONTENT: case tCHAR:
10399 #ifndef RIPPER
10400 v = valp->node->nd_lit;
10401 #else
10402 v = valp->val;
10403 #endif
10404 rb_parser_printf(p, "%+"PRIsVALUE, v);
10405 break;
10406 case tNTH_REF:
10407 #ifndef RIPPER
10408 rb_parser_printf(p, "$%ld", valp->node->nd_nth);
10409 #else
10410 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10411 #endif
10412 break;
10413 case tBACK_REF:
10414 #ifndef RIPPER
10415 rb_parser_printf(p, "$%c", (int)valp->node->nd_nth);
10416 #else
10417 rb_parser_printf(p, "%"PRIsVALUE, valp->val);
10418 #endif
10419 break;
10420 default:
10421 break;
10422 }
10423 }
10424
10425 static int
10426 assignable0(struct parser_params *p, ID id, const char **err)
10427 {
10428 if (!id) return -1;
10429 switch (id) {
10430 case keyword_self:
10431 *err = "Can't change the value of self";
10432 return -1;
10433 case keyword_nil:
10434 *err = "Can't assign to nil";
10435 return -1;
10436 case keyword_true:
10437 *err = "Can't assign to true";
10438 return -1;
10439 case keyword_false:
10440 *err = "Can't assign to false";
10441 return -1;
10442 case keyword__FILE__:
10443 *err = "Can't assign to __FILE__";
10444 return -1;
10445 case keyword__LINE__:
10446 *err = "Can't assign to __LINE__";
10447 return -1;
10448 case keyword__ENCODING__:
10449 *err = "Can't assign to __ENCODING__";
10450 return -1;
10451 }
10452 switch (id_type(id)) {
10453 case ID_LOCAL:
10454 if (dyna_in_block(p)) {
10455 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10456 compile_error(p, "Can't assign to numbered parameter _%d",
10457 NUMPARAM_ID_TO_IDX(id));
10458 return -1;
10459 }
10460 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10461 if (dvar_defined(p, id)) return NODE_DASGN;
10462 if (local_id(p, id)) return NODE_LASGN;
10463 dyna_var(p, id);
10464 return NODE_DASGN_CURR;
10465 }
10466 else {
10467 if (!local_id(p, id)) local_var(p, id);
10468 return NODE_LASGN;
10469 }
10470 break;
10471 case ID_GLOBAL: return NODE_GASGN;
10472 case ID_INSTANCE: return NODE_IASGN;
10473 case ID_CONST:
10474 if (!p->in_def) return NODE_CDECL;
10475 *err = "dynamic constant assignment";
10476 return -1;
10477 case ID_CLASS: return NODE_CVASGN;
10478 default:
10479 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10480 }
10481 return -1;
10482 }
10483
10484 #ifndef RIPPER
10485 static NODE*
10486 assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10487 {
10488 const char *err = 0;
10489 int node_type = assignable0(p, id, &err);
10490 switch (node_type) {
10491 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10492 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10493 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10494 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10495 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10496 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10497 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10498 }
10499 if (err) yyerror1(loc, err);
10500 return NEW_BEGIN(0, loc);
10501 }
10502 #else
10503 static VALUE
10504 assignable(struct parser_params *p, VALUE lhs)
10505 {
10506 const char *err = 0;
10507 assignable0(p, get_id(lhs), &err);
10508 if (err) lhs = assign_error(p, lhs);
10509 return lhs;
10510 }
10511 #endif
10512
10513 static int
10514 is_private_local_id(ID name)
10515 {
10516 VALUE s;
10517 if (name == idUScore) return 1;
10518 if (!is_local_id(name)) return 0;
10519 s = rb_id2str(name);
10520 if (!s) return 0;
10521 return RSTRING_PTR(s)[0] == '_';
10522 }
10523
10524 static int
10525 shadowing_lvar_0(struct parser_params *p, ID name)
10526 {
10527 if (is_private_local_id(name)) return 1;
10528 if (dyna_in_block(p)) {
10529 if (dvar_curr(p, name)) {
10530 yyerror0("duplicated argument name");
10531 }
10532 else if (dvar_defined(p, name) || local_id(p, name)) {
10533 vtable_add(p->lvtbl->vars, name);
10534 if (p->lvtbl->used) {
10535 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10536 }
10537 return 0;
10538 }
10539 }
10540 else {
10541 if (local_id(p, name)) {
10542 yyerror0("duplicated argument name");
10543 }
10544 }
10545 return 1;
10546 }
10547
10548 static ID
10549 shadowing_lvar(struct parser_params *p, ID name)
10550 {
10551 shadowing_lvar_0(p, name);
10552 return name;
10553 }
10554
10555 static void
10556 new_bv(struct parser_params *p, ID name)
10557 {
10558 if (!name) return;
10559 if (!is_local_id(name)) {
10560 compile_error(p, "invalid local variable - %"PRIsVALUE,
10561 rb_id2str(name));
10562 return;
10563 }
10564 if (!shadowing_lvar_0(p, name)) return;
10565 dyna_var(p, name);
10566 }
10567
10568 #ifndef RIPPER
10569 static NODE *
10570 aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10571 {
10572 return NEW_ATTRASGN(recv, tASET, idx, loc);
10573 }
10574
10575 static void
10576 block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10577 {
10578 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10579 compile_error(p, "both block arg and actual block given");
10580 }
10581 }
10582
10583 static NODE *
10584 attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10585 {
10586 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10587 return NEW_ATTRASGN(recv, id, 0, loc);
10588 }
10589
10590 static void
10591 rb_backref_error(struct parser_params *p, NODE *node)
10592 {
10593 switch (nd_type(node)) {
10594 case NODE_NTH_REF:
10595 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10596 break;
10597 case NODE_BACK_REF:
10598 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10599 break;
10600 }
10601 }
10602
10603 static NODE *
10604 arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10605 {
10606 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10607 switch (nd_type(node1)) {
10608 case NODE_LIST:
10609 return list_append(p, node1, node2);
10610 case NODE_BLOCK_PASS:
10611 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10612 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10613 return node1;
10614 case NODE_ARGSPUSH:
10615 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10616 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10617 nd_set_type(node1, NODE_ARGSCAT);
10618 return node1;
10619 case NODE_ARGSCAT:
10620 if (nd_type(node1->nd_body) != NODE_LIST) break;
10621 node1->nd_body = list_append(p, node1->nd_body, node2);
10622 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10623 return node1;
10624 }
10625 return NEW_ARGSPUSH(node1, node2, loc);
10626 }
10627
10628 static NODE *
10629 arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10630 {
10631 if (!node2) return node1;
10632 switch (nd_type(node1)) {
10633 case NODE_BLOCK_PASS:
10634 if (node1->nd_head)
10635 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10636 else
10637 node1->nd_head = NEW_LIST(node2, loc);
10638 return node1;
10639 case NODE_ARGSPUSH:
10640 if (nd_type(node2) != NODE_LIST) break;
10641 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10642 nd_set_type(node1, NODE_ARGSCAT);
10643 return node1;
10644 case NODE_ARGSCAT:
10645 if (nd_type(node2) != NODE_LIST ||
10646 nd_type(node1->nd_body) != NODE_LIST) break;
10647 node1->nd_body = list_concat(node1->nd_body, node2);
10648 return node1;
10649 }
10650 return NEW_ARGSCAT(node1, node2, loc);
10651 }
10652
10653 static NODE *
10654 last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10655 {
10656 NODE *n1;
10657 if ((n1 = splat_array(args)) != 0) {
10658 return list_append(p, n1, last_arg);
10659 }
10660 return arg_append(p, args, last_arg, loc);
10661 }
10662
10663 static NODE *
10664 rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10665 {
10666 NODE *n1;
10667 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10668 return list_concat(n1, rest_arg);
10669 }
10670 return arg_concat(p, args, rest_arg, loc);
10671 }
10672
10673 static NODE *
10674 splat_array(NODE* node)
10675 {
10676 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10677 if (nd_type(node) == NODE_LIST) return node;
10678 return 0;
10679 }
10680
10681 static void
10682 mark_lvar_used(struct parser_params *p, NODE *rhs)
10683 {
10684 ID *vidp = NULL;
10685 if (!rhs) return;
10686 switch (nd_type(rhs)) {
10687 case NODE_LASGN:
10688 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10689 if (vidp) *vidp |= LVAR_USED;
10690 }
10691 break;
10692 case NODE_DASGN:
10693 case NODE_DASGN_CURR:
10694 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10695 if (vidp) *vidp |= LVAR_USED;
10696 }
10697 break;
10698 #if 0
10699 case NODE_MASGN:
10700 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10701 mark_lvar_used(p, rhs->nd_head);
10702 }
10703 break;
10704 #endif
10705 }
10706 }
10707
10708 static NODE *
10709 node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10710 {
10711 if (!lhs) return 0;
10712
10713 switch (nd_type(lhs)) {
10714 case NODE_GASGN:
10715 case NODE_IASGN:
10716 case NODE_LASGN:
10717 case NODE_DASGN:
10718 case NODE_DASGN_CURR:
10719 case NODE_MASGN:
10720 case NODE_CDECL:
10721 case NODE_CVASGN:
10722 lhs->nd_value = rhs;
10723 nd_set_loc(lhs, loc);
10724 break;
10725
10726 case NODE_ATTRASGN:
10727 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10728 nd_set_loc(lhs, loc);
10729 break;
10730
10731 default:
10732 /* should not happen */
10733 break;
10734 }
10735
10736 return lhs;
10737 }
10738
10739 static NODE *
10740 value_expr_check(struct parser_params *p, NODE *node)
10741 {
10742 NODE *void_node = 0, *vn;
10743
10744 if (!node) {
10745 rb_warning0("empty expression");
10746 }
10747 while (node) {
10748 switch (nd_type(node)) {
10749 case NODE_RETURN:
10750 case NODE_BREAK:
10751 case NODE_NEXT:
10752 case NODE_REDO:
10753 case NODE_RETRY:
10754 return void_node ? void_node : node;
10755
10756 case NODE_CASE3:
10757 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10758 compile_error(p, "unexpected node");
10759 return NULL;
10760 }
10761 if (node->nd_body->nd_body) {
10762 return NULL;
10763 }
10764 /* single line pattern matching */
10765 return void_node ? void_node : node;
10766
10767 case NODE_BLOCK:
10768 while (node->nd_next) {
10769 node = node->nd_next;
10770 }
10771 node = node->nd_head;
10772 break;
10773
10774 case NODE_BEGIN:
10775 node = node->nd_body;
10776 break;
10777
10778 case NODE_IF:
10779 case NODE_UNLESS:
10780 if (!node->nd_body) {
10781 return NULL;
10782 }
10783 else if (!node->nd_else) {
10784 return NULL;
10785 }
10786 vn = value_expr_check(p, node->nd_body);
10787 if (!vn) return NULL;
10788 if (!void_node) void_node = vn;
10789 node = node->nd_else;
10790 break;
10791
10792 case NODE_AND:
10793 case NODE_OR:
10794 node = node->nd_1st;
10795 break;
10796
10797 case NODE_LASGN:
10798 case NODE_DASGN:
10799 case NODE_DASGN_CURR:
10800 case NODE_MASGN:
10801 mark_lvar_used(p, node);
10802 return NULL;
10803
10804 default:
10805 return NULL;
10806 }
10807 }
10808
10809 return NULL;
10810 }
10811
10812 static int
10813 value_expr_gen(struct parser_params *p, NODE *node)
10814 {
10815 NODE *void_node = value_expr_check(p, node);
10816 if (void_node) {
10817 yyerror1(&void_node->nd_loc, "void value expression");
10818 /* or "control never reach"? */
10819 return FALSE;
10820 }
10821 return TRUE;
10822 }
10823 static void
10824 void_expr(struct parser_params *p, NODE *node)
10825 {
10826 const char *useless = 0;
10827
10828 if (!RTEST(ruby_verbose)) return;
10829
10830 if (!node || !(node = nd_once_body(node))) return;
10831 switch (nd_type(node)) {
10832 case NODE_OPCALL:
10833 switch (node->nd_mid) {
10834 case '+':
10835 case '-':
10836 case '*':
10837 case '/':
10838 case '%':
10839 case tPOW:
10840 case tUPLUS:
10841 case tUMINUS:
10842 case '|':
10843 case '^':
10844 case '&':
10845 case tCMP:
10846 case '>':
10847 case tGEQ:
10848 case '<':
10849 case tLEQ:
10850 case tEQ:
10851 case tNEQ:
10852 useless = rb_id2name(node->nd_mid);
10853 break;
10854 }
10855 break;
10856
10857 case NODE_LVAR:
10858 case NODE_DVAR:
10859 case NODE_GVAR:
10860 case NODE_IVAR:
10861 case NODE_CVAR:
10862 case NODE_NTH_REF:
10863 case NODE_BACK_REF:
10864 useless = "a variable";
10865 break;
10866 case NODE_CONST:
10867 useless = "a constant";
10868 break;
10869 case NODE_LIT:
10870 case NODE_STR:
10871 case NODE_DSTR:
10872 case NODE_DREGX:
10873 useless = "a literal";
10874 break;
10875 case NODE_COLON2:
10876 case NODE_COLON3:
10877 useless = "::";
10878 break;
10879 case NODE_DOT2:
10880 useless = "..";
10881 break;
10882 case NODE_DOT3:
10883 useless = "...";
10884 break;
10885 case NODE_SELF:
10886 useless = "self";
10887 break;
10888 case NODE_NIL:
10889 useless = "nil";
10890 break;
10891 case NODE_TRUE:
10892 useless = "true";
10893 break;
10894 case NODE_FALSE:
10895 useless = "false";
10896 break;
10897 case NODE_DEFINED:
10898 useless = "defined?";
10899 break;
10900 }
10901
10902 if (useless) {
10903 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10904 }
10905 }
10906
10907 static NODE *
10908 void_stmts(struct parser_params *p, NODE *node)
10909 {
10910 NODE *const n = node;
10911 if (!RTEST(ruby_verbose)) return n;
10912 if (!node) return n;
10913 if (nd_type(node) != NODE_BLOCK) return n;
10914
10915 while (node->nd_next) {
10916 void_expr(p, node->nd_head);
10917 node = node->nd_next;
10918 }
10919 return n;
10920 }
10921
10922 static NODE *
10923 remove_begin(NODE *node)
10924 {
10925 NODE **n = &node, *n1 = node;
10926 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10927 *n = n1 = n1->nd_body;
10928 }
10929 return node;
10930 }
10931
10932 static NODE *
10933 remove_begin_all(NODE *node)
10934 {
10935 NODE **n = &node, *n1 = node;
10936 while (n1 && nd_type(n1) == NODE_BEGIN) {
10937 *n = n1 = n1->nd_body;
10938 }
10939 return node;
10940 }
10941
10942 static void
10943 reduce_nodes(struct parser_params *p, NODE **body)
10944 {
10945 NODE *node = *body;
10946
10947 if (!node) {
10948 *body = NEW_NIL(&NULL_LOC);
10949 return;
10950 }
10951 #define subnodes(n1, n2) \
10952 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10953 (!node->n2) ? (body = &node->n1, 1) : \
10954 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10955
10956 while (node) {
10957 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10958 switch (nd_type(node)) {
10959 end:
10960 case NODE_NIL:
10961 *body = 0;
10962 return;
10963 case NODE_RETURN:
10964 *body = node = node->nd_stts;
10965 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10966 continue;
10967 case NODE_BEGIN:
10968 *body = node = node->nd_body;
10969 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10970 continue;
10971 case NODE_BLOCK:
10972 body = &node->nd_end->nd_head;
10973 break;
10974 case NODE_IF:
10975 case NODE_UNLESS:
10976 if (subnodes(nd_body, nd_else)) break;
10977 return;
10978 case NODE_CASE:
10979 body = &node->nd_body;
10980 break;
10981 case NODE_WHEN:
10982 if (!subnodes(nd_body, nd_next)) goto end;
10983 break;
10984 case NODE_ENSURE:
10985 if (!subnodes(nd_head, nd_resq)) goto end;
10986 break;
10987 case NODE_RESCUE:
10988 if (node->nd_else) {
10989 body = &node->nd_resq;
10990 break;
10991 }
10992 if (!subnodes(nd_head, nd_resq)) goto end;
10993 break;
10994 default:
10995 return;
10996 }
10997 node = *body;
10998 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10999 }
11000
11001 #undef subnodes
11002 }
11003
11004 static int
11005 is_static_content(NODE *node)
11006 {
11007 if (!node) return 1;
11008 switch (nd_type(node)) {
11009 case NODE_HASH:
11010 if (!(node = node->nd_head)) break;
11011 case NODE_LIST:
11012 do {
11013 if (!is_static_content(node->nd_head)) return 0;
11014 } while ((node = node->nd_next) != 0);
11015 case NODE_LIT:
11016 case NODE_STR:
11017 case NODE_NIL:
11018 case NODE_TRUE:
11019 case NODE_FALSE:
11020 case NODE_ZLIST:
11021 break;
11022 default:
11023 return 0;
11024 }
11025 return 1;
11026 }
11027
11028 static int
11029 assign_in_cond(struct parser_params *p, NODE *node)
11030 {
11031 switch (nd_type(node)) {
11032 case NODE_MASGN:
11033 case NODE_LASGN:
11034 case NODE_DASGN:
11035 case NODE_DASGN_CURR:
11036 case NODE_GASGN:
11037 case NODE_IASGN:
11038 break;
11039
11040 default:
11041 return 0;
11042 }
11043
11044 if (!node->nd_value) return 1;
11045 if (is_static_content(node->nd_value)) {
11046 /* reports always */
11047 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11048 }
11049 return 1;
11050 }
11051
11052 enum cond_type {
11053 COND_IN_OP,
11054 COND_IN_COND,
11055 COND_IN_FF
11056 };
11057
11058 #define SWITCH_BY_COND_TYPE(t, w, arg) \
11059 switch (t) { \
11060 case COND_IN_OP: break; \
11061 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11062 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11063 }
11064
11065 static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11066
11067 static NODE*
11068 range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11069 {
11070 enum node_type type;
11071
11072 if (node == 0) return 0;
11073
11074 type = nd_type(node);
11075 value_expr(node);
11076 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11077 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11078 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11079 }
11080 return cond0(p, node, COND_IN_FF, loc);
11081 }
11082
11083 static NODE*
11084 cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11085 {
11086 if (node == 0) return 0;
11087 if (!(node = nd_once_body(node))) return 0;
11088 assign_in_cond(p, node);
11089
11090 switch (nd_type(node)) {
11091 case NODE_DSTR:
11092 case NODE_EVSTR:
11093 case NODE_STR:
11094 SWITCH_BY_COND_TYPE(type, warn, "string ")
11095 break;
11096
11097 case NODE_DREGX:
11098 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11099
11100 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11101
11102 case NODE_AND:
11103 case NODE_OR:
11104 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11105 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11106 break;
11107
11108 case NODE_DOT2:
11109 case NODE_DOT3:
11110 node->nd_beg = range_op(p, node->nd_beg, loc);
11111 node->nd_end = range_op(p, node->nd_end, loc);
11112 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11113 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11114 break;
11115
11116 case NODE_DSYM:
11117 SWITCH_BY_COND_TYPE(type, warning, "string ")
11118 break;
11119
11120 case NODE_LIT:
11121 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11122 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11123 nd_set_type(node, NODE_MATCH);
11124 }
11125 else if (node->nd_lit == Qtrue ||
11126 node->nd_lit == Qfalse) {
11127 /* booleans are OK, e.g., while true */
11128 }
11129 else {
11130 SWITCH_BY_COND_TYPE(type, warning, "")
11131 }
11132 default:
11133 break;
11134 }
11135 return node;
11136 }
11137
11138 static NODE*
11139 cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11140 {
11141 if (node == 0) return 0;
11142 return cond0(p, node, COND_IN_COND, loc);
11143 }
11144
11145 static NODE*
11146 method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11147 {
11148 if (node == 0) return 0;
11149 return cond0(p, node, COND_IN_OP, loc);
11150 }
11151
11152 static NODE*
11153 new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11154 {
11155 if (!cc) return right;
11156 cc = cond0(p, cc, COND_IN_COND, loc);
11157 return newline_node(NEW_IF(cc, left, right, loc));
11158 }
11159
11160 static NODE*
11161 new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11162 {
11163 if (!cc) return right;
11164 cc = cond0(p, cc, COND_IN_COND, loc);
11165 return newline_node(NEW_UNLESS(cc, left, right, loc));
11166 }
11167
11168 static NODE*
11169 logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11170 const YYLTYPE *op_loc, const YYLTYPE *loc)
11171 {
11172 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11173 NODE *op;
11174 value_expr(left);
11175 if (left && (enum node_type)nd_type(left) == type) {
11176 NODE *node = left, *second;
11177 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11178 node = second;
11179 }
11180 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11181 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11182 left->nd_loc.end_pos = loc->end_pos;
11183 return left;
11184 }
11185 op = NEW_NODE(type, left, right, 0, loc);
11186 nd_set_line(op, op_loc->beg_pos.lineno);
11187 return op;
11188 }
11189
11190 static void
11191 no_blockarg(struct parser_params *p, NODE *node)
11192 {
11193 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11194 compile_error(p, "block argument should not be given");
11195 }
11196 }
11197
11198 static NODE *
11199 ret_args(struct parser_params *p, NODE *node)
11200 {
11201 if (node) {
11202 no_blockarg(p, node);
11203 if (nd_type(node) == NODE_LIST) {
11204 if (node->nd_next == 0) {
11205 node = node->nd_head;
11206 }
11207 else {
11208 nd_set_type(node, NODE_VALUES);
11209 }
11210 }
11211 }
11212 return node;
11213 }
11214
11215 static NODE *
11216 new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11217 {
11218 if (node) no_blockarg(p, node);
11219
11220 return NEW_YIELD(node, loc);
11221 }
11222
11223 static VALUE
11224 negate_lit(struct parser_params *p, VALUE lit)
11225 {
11226 if (FIXNUM_P(lit)) {
11227 return LONG2FIX(-FIX2LONG(lit));
11228 }
11229 if (SPECIAL_CONST_P(lit)) {
11230 #if USE_FLONUM
11231 if (FLONUM_P(lit)) {
11232 return DBL2NUM(-RFLOAT_VALUE(lit));
11233 }
11234 #endif
11235 goto unknown;
11236 }
11237 switch (BUILTIN_TYPE(lit)) {
11238 case T_BIGNUM:
11239 BIGNUM_NEGATE(lit);
11240 lit = rb_big_norm(lit);
11241 break;
11242 case T_RATIONAL:
11243 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11244 break;
11245 case T_COMPLEX:
11246 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11247 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11248 break;
11249 case T_FLOAT:
11250 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11251 break;
11252 unknown:
11253 default:
11254 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11255 rb_builtin_class_name(lit));
11256 break;
11257 }
11258 return lit;
11259 }
11260
11261 static NODE *
11262 arg_blk_pass(NODE *node1, NODE *node2)
11263 {
11264 if (node2) {
11265 if (!node1) return node2;
11266 node2->nd_head = node1;
11267 nd_set_first_lineno(node2, nd_first_lineno(node1));
11268 nd_set_first_column(node2, nd_first_column(node1));
11269 return node2;
11270 }
11271 return node1;
11272 }
11273
11274 static bool
11275 args_info_empty_p(struct rb_args_info *args)
11276 {
11277 if (args->pre_args_num) return false;
11278 if (args->post_args_num) return false;
11279 if (args->rest_arg) return false;
11280 if (args->opt_args) return false;
11281 if (args->block_arg) return false;
11282 if (args->kw_args) return false;
11283 if (args->kw_rest_arg) return false;
11284 return true;
11285 }
11286
11287 static NODE*
11288 new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11289 {
11290 int saved_line = p->ruby_sourceline;
11291 struct rb_args_info *args = tail->nd_ainfo;
11292
11293 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11294 args->pre_init = pre_args ? pre_args->nd_next : 0;
11295
11296 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11297 args->post_init = post_args ? post_args->nd_next : 0;
11298 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11299
11300 args->rest_arg = rest_arg;
11301
11302 args->opt_args = opt_args;
11303
11304 args->ruby2_keywords = rest_arg == idFWD_REST;
11305
11306 p->ruby_sourceline = saved_line;
11307 nd_set_loc(tail, loc);
11308
11309 return tail;
11310 }
11311
11312 static NODE*
11313 new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11314 {
11315 int saved_line = p->ruby_sourceline;
11316 NODE *node;
11317 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11318 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11319 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11320 args->imemo = tmpbuf;
11321 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11322 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11323 if (p->error_p) return node;
11324
11325 args->block_arg = block;
11326 args->kw_args = kw_args;
11327
11328 if (kw_args) {
11329 /*
11330 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11331 * variable order: k1, kr1, k2, &b, internal_id, krest
11332 * #=> <reorder>
11333 * variable order: kr1, k1, k2, internal_id, krest, &b
11334 */
11335 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11336 struct vtable *vtargs = p->lvtbl->args;
11337 NODE *kwn = kw_args;
11338
11339 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11340 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11341 while (kwn) {
11342 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11343 --kw_vars;
11344 --required_kw_vars;
11345 kwn = kwn->nd_next;
11346 }
11347
11348 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11349 ID vid = kwn->nd_body->nd_vid;
11350 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11351 *required_kw_vars++ = vid;
11352 }
11353 else {
11354 *kw_vars++ = vid;
11355 }
11356 }
11357
11358 arg_var(p, kw_bits);
11359 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11360 if (block) arg_var(p, block);
11361
11362 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11363 args->kw_rest_arg->nd_cflag = kw_bits;
11364 }
11365 else if (kw_rest_arg == idNil) {
11366 args->no_kwarg = 1;
11367 }
11368 else if (kw_rest_arg) {
11369 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11370 }
11371
11372 p->ruby_sourceline = saved_line;
11373 return node;
11374 }
11375
11376 static NODE *
11377 args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11378 {
11379 if (max_numparam > NO_PARAM) {
11380 if (!args) args = new_args_tail(p, 0, 0, 0, 0);
11381 args->nd_ainfo->pre_args_num = max_numparam;
11382 }
11383 return args;
11384 }
11385
11386 static NODE*
11387 new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11388 {
11389 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11390
11391 aryptn->nd_pconst = constant;
11392
11393 if (pre_arg) {
11394 NODE *pre_args = NEW_LIST(pre_arg, loc);
11395 if (apinfo->pre_args) {
11396 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11397 }
11398 else {
11399 apinfo->pre_args = pre_args;
11400 }
11401 }
11402 return aryptn;
11403 }
11404
11405 static NODE*
11406 new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11407 {
11408 int saved_line = p->ruby_sourceline;
11409 NODE *node;
11410 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11411 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11412 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11413 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11414 apinfo->imemo = tmpbuf;
11415 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11416
11417 apinfo->pre_args = pre_args;
11418
11419 if (has_rest) {
11420 if (rest_arg) {
11421 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11422 }
11423 else {
11424 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11425 }
11426 }
11427 else {
11428 apinfo->rest_arg = NULL;
11429 }
11430
11431 apinfo->post_args = post_args;
11432
11433 p->ruby_sourceline = saved_line;
11434 return node;
11435 }
11436
11437 static NODE*
11438 new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11439 {
11440 hshptn->nd_pconst = constant;
11441 return hshptn;
11442 }
11443
11444 static NODE*
11445 new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11446 {
11447 int saved_line = p->ruby_sourceline;
11448 NODE *node, *kw_rest_arg_node;
11449
11450 if (kw_rest_arg == idNil) {
11451 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11452 }
11453 else if (kw_rest_arg) {
11454 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11455 }
11456 else {
11457 kw_rest_arg_node = NULL;
11458 }
11459
11460 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11461
11462 p->ruby_sourceline = saved_line;
11463 return node;
11464 }
11465
11466 static NODE *
11467 new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11468 {
11469 NODE *node = NEW_CASE3(val, pat, loc);
11470
11471 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11472 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11473 return node;
11474 }
11475
11476 static NODE*
11477 dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11478 {
11479 VALUE lit;
11480
11481 if (!node) {
11482 return NEW_LIT(ID2SYM(idNULL), loc);
11483 }
11484
11485 switch (nd_type(node)) {
11486 case NODE_DSTR:
11487 nd_set_type(node, NODE_DSYM);
11488 nd_set_loc(node, loc);
11489 break;
11490 case NODE_STR:
11491 lit = node->nd_lit;
11492 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11493 nd_set_type(node, NODE_LIT);
11494 nd_set_loc(node, loc);
11495 break;
11496 default:
11497 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11498 break;
11499 }
11500 return node;
11501 }
11502
11503 static int
11504 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11505 {
11506 NODE *node = (NODE *)v;
11507 NODE **result = (NODE **)h;
11508 node->nd_alen = 2;
11509 node->nd_next->nd_end = node->nd_next;
11510 node->nd_next->nd_next = 0;
11511 if (*result)
11512 list_concat(*result, node);
11513 else
11514 *result = node;
11515 return ST_CONTINUE;
11516 }
11517
11518 static NODE *
11519 remove_duplicate_keys(struct parser_params *p, NODE *hash)
11520 {
11521 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11522 NODE *result = 0;
11523 rb_code_location_t loc = hash->nd_loc;
11524 while (hash && hash->nd_head && hash->nd_next) {
11525 NODE *head = hash->nd_head;
11526 NODE *value = hash->nd_next;
11527 NODE *next = value->nd_next;
11528 VALUE key = (VALUE)head;
11529 st_data_t data;
11530 if (nd_type(head) == NODE_LIT &&
11531 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11532 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11533 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11534 head->nd_lit, nd_line(head));
11535 head = ((NODE *)data)->nd_next;
11536 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11537 }
11538 else {
11539 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11540 }
11541 hash = next;
11542 }
11543 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11544 st_free_table(literal_keys);
11545 if (hash) {
11546 if (!result) result = hash;
11547 else list_concat(result, hash);
11548 }
11549 result->nd_loc = loc;
11550 return result;
11551 }
11552
11553 static NODE *
11554 new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11555 {
11556 if (hash) hash = remove_duplicate_keys(p, hash);
11557 return NEW_HASH(hash, loc);
11558 }
11559 #endif
11560
11561 static void
11562 error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11563 {
11564 if (is_private_local_id(id)) {
11565 return;
11566 }
11567 if (st_is_member(p->pvtbl, id)) {
11568 yyerror1(loc, "duplicated variable name");
11569 }
11570 else {
11571 st_insert(p->pvtbl, (st_data_t)id, 0);
11572 }
11573 }
11574
11575 static void
11576 error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11577 {
11578 if (!p->pktbl) {
11579 p->pktbl = st_init_numtable();
11580 }
11581 else if (st_is_member(p->pktbl, key)) {
11582 yyerror1(loc, "duplicated key name");
11583 return;
11584 }
11585 st_insert(p->pktbl, (st_data_t)key, 0);
11586 }
11587
11588 #ifndef RIPPER
11589 static NODE *
11590 new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11591 {
11592 return NEW_HASH(hash, loc);
11593 }
11594 #endif /* !RIPPER */
11595
11596 #ifndef RIPPER
11597 static NODE *
11598 new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11599 {
11600 NODE *asgn;
11601
11602 if (lhs) {
11603 ID vid = lhs->nd_vid;
11604 YYLTYPE lhs_loc = lhs->nd_loc;
11605 if (op == tOROP) {
11606 lhs->nd_value = rhs;
11607 nd_set_loc(lhs, loc);
11608 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11609 if (is_notop_id(vid)) {
11610 switch (id_type(vid)) {
11611 case ID_GLOBAL:
11612 case ID_INSTANCE:
11613 case ID_CLASS:
11614 asgn->nd_aid = vid;
11615 }
11616 }
11617 }
11618 else if (op == tANDOP) {
11619 lhs->nd_value = rhs;
11620 nd_set_loc(lhs, loc);
11621 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11622 }
11623 else {
11624 asgn = lhs;
11625 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11626 nd_set_loc(asgn, loc);
11627 }
11628 }
11629 else {
11630 asgn = NEW_BEGIN(0, loc);
11631 }
11632 return asgn;
11633 }
11634
11635 static NODE *
11636 new_ary_op_assign(struct parser_params *p, NODE *ary,
11637 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11638 {
11639 NODE *asgn;
11640
11641 args = make_list(args, args_loc);
11642 if (nd_type(args) == NODE_BLOCK_PASS) {
11643 args = NEW_ARGSCAT(args, rhs, loc);
11644 }
11645 else {
11646 args = arg_concat(p, args, rhs, loc);
11647 }
11648 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11649 fixpos(asgn, ary);
11650 return asgn;
11651 }
11652
11653 static NODE *
11654 new_attr_op_assign(struct parser_params *p, NODE *lhs,
11655 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11656 {
11657 NODE *asgn;
11658
11659 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11660 fixpos(asgn, lhs);
11661 return asgn;
11662 }
11663
11664 static NODE *
11665 new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11666 {
11667 NODE *asgn;
11668
11669 if (lhs) {
11670 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11671 }
11672 else {
11673 asgn = NEW_BEGIN(0, loc);
11674 }
11675 fixpos(asgn, lhs);
11676 return asgn;
11677 }
11678
11679 static NODE *
11680 const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11681 {
11682 if (p->in_def) {
11683 yyerror1(loc, "dynamic constant assignment");
11684 }
11685 return NEW_CDECL(0, 0, (path), loc);
11686 }
11687 #else
11688 static VALUE
11689 const_decl(struct parser_params *p, VALUE path)
11690 {
11691 if (p->in_def) {
11692 path = dispatch1(assign_error, path);
11693 ripper_error(p);
11694 }
11695 return path;
11696 }
11697
11698 static VALUE
11699 assign_error(struct parser_params *p, VALUE a)
11700 {
11701 a = dispatch1(assign_error, a);
11702 ripper_error(p);
11703 return a;
11704 }
11705
11706 static VALUE
11707 var_field(struct parser_params *p, VALUE a)
11708 {
11709 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11710 }
11711 #endif
11712
11713 #ifndef RIPPER
11714 static NODE *
11715 new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11716 {
11717 NODE *result = head;
11718 if (rescue) {
11719 NODE *tmp = rescue_else ? rescue_else : rescue;
11720 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11721
11722 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11723 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11724 }
11725 else if (rescue_else) {
11726 result = block_append(p, result, rescue_else);
11727 }
11728 if (ensure) {
11729 result = NEW_ENSURE(result, ensure, loc);
11730 }
11731 fixpos(result, head);
11732 return result;
11733 }
11734 #endif
11735
11736 static void
11737 warn_unused_var(struct parser_params *p, struct local_vars *local)
11738 {
11739 int cnt;
11740
11741 if (!local->used) return;
11742 cnt = local->used->pos;
11743 if (cnt != local->vars->pos) {
11744 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11745 }
11746 #ifndef RIPPER
11747 ID *v = local->vars->tbl;
11748 ID *u = local->used->tbl;
11749 for (int i = 0; i < cnt; ++i) {
11750 if (!v[i] || (u[i] & LVAR_USED)) continue;
11751 if (is_private_local_id(v[i])) continue;
11752 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11753 }
11754 #endif
11755 }
11756
11757 static void
11758 local_push(struct parser_params *p, int toplevel_scope)
11759 {
11760 struct local_vars *local;
11761 int inherits_dvars = toplevel_scope && compile_for_eval;
11762 int warn_unused_vars = RTEST(ruby_verbose);
11763
11764 local = ALLOC(struct local_vars);
11765 local->prev = p->lvtbl;
11766 local->args = vtable_alloc(0);
11767 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11768 #ifndef RIPPER
11769 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11770 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11771 local->numparam.outer = 0;
11772 local->numparam.inner = 0;
11773 local->numparam.current = 0;
11774 #endif
11775 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11776
11777 # if WARN_PAST_SCOPE
11778 local->past = 0;
11779 # endif
11780 CMDARG_PUSH(0);
11781 COND_PUSH(0);
11782 p->lvtbl = local;
11783 }
11784
11785 static void
11786 local_pop(struct parser_params *p)
11787 {
11788 struct local_vars *local = p->lvtbl->prev;
11789 if (p->lvtbl->used) {
11790 warn_unused_var(p, p->lvtbl);
11791 vtable_free(p->lvtbl->used);
11792 }
11793 # if WARN_PAST_SCOPE
11794 while (p->lvtbl->past) {
11795 struct vtable *past = p->lvtbl->past;
11796 p->lvtbl->past = past->prev;
11797 vtable_free(past);
11798 }
11799 # endif
11800 vtable_free(p->lvtbl->args);
11801 vtable_free(p->lvtbl->vars);
11802 CMDARG_POP();
11803 COND_POP();
11804 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11805 p->lvtbl = local;
11806 }
11807
11808 #ifndef RIPPER
11809 static ID*
11810 local_tbl(struct parser_params *p)
11811 {
11812 int cnt_args = vtable_size(p->lvtbl->args);
11813 int cnt_vars = vtable_size(p->lvtbl->vars);
11814 int cnt = cnt_args + cnt_vars;
11815 int i, j;
11816 ID *buf;
11817 VALUE tbl = 0;
11818
11819 if (cnt <= 0) return 0;
11820 tbl = rb_imemo_tmpbuf_auto_free_pointer();
11821 buf = ALLOC_N(ID, cnt + 2);
11822 rb_imemo_tmpbuf_set_ptr(tbl, buf);
11823 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11824 /* remove IDs duplicated to warn shadowing */
11825 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11826 ID id = p->lvtbl->vars->tbl[i];
11827 if (!vtable_included(p->lvtbl->args, id)) {
11828 buf[j++] = id;
11829 }
11830 }
11831 if (--j < cnt) {
11832 REALLOC_N(buf, ID, (cnt = j) + 2);
11833 rb_imemo_tmpbuf_set_ptr(tbl, buf);
11834 }
11835 buf[0] = cnt;
11836 buf[cnt + 1] = (ID)tbl;
11837 RB_OBJ_WRITTEN(p->ast, Qnil, tbl);
11838
11839 return buf;
11840 }
11841
11842 static NODE*
11843 node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11844 {
11845 ID *a0;
11846 NODE *n;
11847
11848 a0 = local_tbl(p);
11849 n = NEW_NODE(type, a0, a1, a2, loc);
11850 return n;
11851 }
11852
11853 #endif
11854
11855 static void
11856 numparam_name(struct parser_params *p, ID id)
11857 {
11858 if (!NUMPARAM_ID_P(id)) return;
11859 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11860 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11861 }
11862
11863 static void
11864 arg_var(struct parser_params *p, ID id)
11865 {
11866 numparam_name(p, id);
11867 vtable_add(p->lvtbl->args, id);
11868 }
11869
11870 static void
11871 local_var(struct parser_params *p, ID id)
11872 {
11873 numparam_name(p, id);
11874 vtable_add(p->lvtbl->vars, id);
11875 if (p->lvtbl->used) {
11876 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11877 }
11878 }
11879
11880 static int
11881 local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11882 {
11883 struct vtable *vars, *args, *used;
11884
11885 vars = p->lvtbl->vars;
11886 args = p->lvtbl->args;
11887 used = p->lvtbl->used;
11888
11889 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11890 vars = vars->prev;
11891 args = args->prev;
11892 if (used) used = used->prev;
11893 }
11894
11895 if (vars && vars->prev == DVARS_INHERIT) {
11896 return rb_local_defined(id, p->parent_iseq);
11897 }
11898 else if (vtable_included(args, id)) {
11899 return 1;
11900 }
11901 else {
11902 int i = vtable_included(vars, id);
11903 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11904 return i != 0;
11905 }
11906 }
11907
11908 static int
11909 local_id(struct parser_params *p, ID id)
11910 {
11911 return local_id_ref(p, id, NULL);
11912 }
11913
11914 static NODE *
11915 numparam_push(struct parser_params *p)
11916 {
11917 #ifndef RIPPER
11918 struct local_vars *local = p->lvtbl;
11919 NODE *inner = local->numparam.inner;
11920 if (!local->numparam.outer) {
11921 local->numparam.outer = local->numparam.current;
11922 }
11923 local->numparam.inner = 0;
11924 local->numparam.current = 0;
11925 return inner;
11926 #else
11927 return 0;
11928 #endif
11929 }
11930
11931 static void
11932 numparam_pop(struct parser_params *p, NODE *prev_inner)
11933 {
11934 #ifndef RIPPER
11935 struct local_vars *local = p->lvtbl;
11936 if (prev_inner) {
11937 /* prefer first one */
11938 local->numparam.inner = prev_inner;
11939 }
11940 else if (local->numparam.current) {
11941 /* current and inner are exclusive */
11942 local->numparam.inner = local->numparam.current;
11943 }
11944 if (p->max_numparam > NO_PARAM) {
11945 /* current and outer are exclusive */
11946 local->numparam.current = local->numparam.outer;
11947 local->numparam.outer = 0;
11948 }
11949 else {
11950 /* no numbered parameter */
11951 local->numparam.current = 0;
11952 }
11953 #endif
11954 }
11955
11956 static const struct vtable *
11957 dyna_push(struct parser_params *p)
11958 {
11959 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11960 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11961 if (p->lvtbl->used) {
11962 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11963 }
11964 return p->lvtbl->args;
11965 }
11966
11967 static void
11968 dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11969 {
11970 struct vtable *tmp = *vtblp;
11971 *vtblp = tmp->prev;
11972 # if WARN_PAST_SCOPE
11973 if (p->past_scope_enabled) {
11974 tmp->prev = p->lvtbl->past;
11975 p->lvtbl->past = tmp;
11976 return;
11977 }
11978 # endif
11979 vtable_free(tmp);
11980 }
11981
11982 static void
11983 dyna_pop_1(struct parser_params *p)
11984 {
11985 struct vtable *tmp;
11986
11987 if ((tmp = p->lvtbl->used) != 0) {
11988 warn_unused_var(p, p->lvtbl);
11989 p->lvtbl->used = p->lvtbl->used->prev;
11990 vtable_free(tmp);
11991 }
11992 dyna_pop_vtable(p, &p->lvtbl->args);
11993 dyna_pop_vtable(p, &p->lvtbl->vars);
11994 }
11995
11996 static void
11997 dyna_pop(struct parser_params *p, const struct vtable *lvargs)
11998 {
11999 while (p->lvtbl->args != lvargs) {
12000 dyna_pop_1(p);
12001 if (!p->lvtbl->args) {
12002 struct local_vars *local = p->lvtbl->prev;
12003 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12004 p->lvtbl = local;
12005 }
12006 }
12007 dyna_pop_1(p);
12008 }
12009
12010 static int
12011 dyna_in_block(struct parser_params *p)
12012 {
12013 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12014 }
12015
12016 static int
12017 dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12018 {
12019 struct vtable *vars, *args, *used;
12020 int i;
12021
12022 args = p->lvtbl->args;
12023 vars = p->lvtbl->vars;
12024 used = p->lvtbl->used;
12025
12026 while (!DVARS_TERMINAL_P(vars)) {
12027 if (vtable_included(args, id)) {
12028 return 1;
12029 }
12030 if ((i = vtable_included(vars, id)) != 0) {
12031 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12032 return 1;
12033 }
12034 args = args->prev;
12035 vars = vars->prev;
12036 if (!vidrefp) used = 0;
12037 if (used) used = used->prev;
12038 }
12039
12040 if (vars == DVARS_INHERIT) {
12041 return rb_dvar_defined(id, p->parent_iseq);
12042 }
12043
12044 return 0;
12045 }
12046
12047 static int
12048 dvar_defined(struct parser_params *p, ID id)
12049 {
12050 return dvar_defined_ref(p, id, NULL);
12051 }
12052
12053 static int
12054 dvar_curr(struct parser_params *p, ID id)
12055 {
12056 return (vtable_included(p->lvtbl->args, id) ||
12057 vtable_included(p->lvtbl->vars, id));
12058 }
12059
12060 static void
12061 reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12062 {
12063 compile_error(p,
12064 "regexp encoding option '%c' differs from source encoding '%s'",
12065 c, rb_enc_name(rb_enc_get(str)));
12066 }
12067
12068 #ifndef RIPPER
12069 int
12070 rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12071 {
12072 int c = RE_OPTION_ENCODING_IDX(options);
12073
12074 if (c) {
12075 int opt, idx;
12076 rb_char_to_option_kcode(c, &opt, &idx);
12077 if (idx != ENCODING_GET(str) &&
12078 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12079 goto error;
12080 }
12081 ENCODING_SET(str, idx);
12082 }
12083 else if (RE_OPTION_ENCODING_NONE(options)) {
12084 if (!ENCODING_IS_ASCII8BIT(str) &&
12085 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12086 c = 'n';
12087 goto error;
12088 }
12089 rb_enc_associate(str, rb_ascii8bit_encoding());
12090 }
12091 else if (p->enc == rb_usascii_encoding()) {
12092 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12093 /* raise in re.c */
12094 rb_enc_associate(str, rb_usascii_encoding());
12095 }
12096 else {
12097 rb_enc_associate(str, rb_ascii8bit_encoding());
12098 }
12099 }
12100 return 0;
12101
12102 error:
12103 return c;
12104 }
12105
12106 static void
12107 reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12108 {
12109 int c = rb_reg_fragment_setenc(p, str, options);
12110 if (c) reg_fragment_enc_error(p, str, c);
12111 }
12112
12113 static int
12114 reg_fragment_check(struct parser_params* p, VALUE str, int options)
12115 {
12116 VALUE err;
12117 reg_fragment_setenc(p, str, options);
12118 err = rb_reg_check_preprocess(str);
12119 if (err != Qnil) {
12120 err = rb_obj_as_string(err);
12121 compile_error(p, "%"PRIsVALUE, err);
12122 return 0;
12123 }
12124 return 1;
12125 }
12126
12127 typedef struct {
12128 struct parser_params* parser;
12129 rb_encoding *enc;
12130 NODE *succ_block;
12131 const YYLTYPE *loc;
12132 } reg_named_capture_assign_t;
12133
12134 static int
12135 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12136 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12137 {
12138 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12139 struct parser_params* p = arg->parser;
12140 rb_encoding *enc = arg->enc;
12141 long len = name_end - name;
12142 const char *s = (const char *)name;
12143 ID var;
12144 NODE *node, *succ;
12145
12146 if (!len) return ST_CONTINUE;
12147 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len))
12148 return ST_CONTINUE;
12149 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12150 return ST_CONTINUE;
12151
12152 var = intern_cstr(s, len, enc);
12153 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12154 succ = arg->succ_block;
12155 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12156 succ = block_append(p, succ, node);
12157 arg->succ_block = succ;
12158 return ST_CONTINUE;
12159 }
12160
12161 static NODE *
12162 reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12163 {
12164 reg_named_capture_assign_t arg;
12165
12166 arg.parser = p;
12167 arg.enc = rb_enc_get(regexp);
12168 arg.succ_block = 0;
12169 arg.loc = loc;
12170 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12171
12172 if (!arg.succ_block) return 0;
12173 return arg.succ_block->nd_next;
12174 }
12175
12176 static VALUE
12177 parser_reg_compile(struct parser_params* p, VALUE str, int options)
12178 {
12179 reg_fragment_setenc(p, str, options);
12180 return rb_parser_reg_compile(p, str, options);
12181 }
12182
12183 VALUE
12184 rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12185 {
12186 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12187 }
12188
12189 static VALUE
12190 reg_compile(struct parser_params* p, VALUE str, int options)
12191 {
12192 VALUE re;
12193 VALUE err;
12194
12195 err = rb_errinfo();
12196 re = parser_reg_compile(p, str, options);
12197 if (NIL_P(re)) {
12198 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12199 rb_set_errinfo(err);
12200 compile_error(p, "%"PRIsVALUE, m);
12201 return Qnil;
12202 }
12203 return re;
12204 }
12205 #else
12206 static VALUE
12207 parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12208 {
12209 VALUE err = rb_errinfo();
12210 VALUE re;
12211 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12212 int c = rb_reg_fragment_setenc(p, str, options);
12213 if (c) reg_fragment_enc_error(p, str, c);
12214 re = rb_parser_reg_compile(p, str, options);
12215 if (NIL_P(re)) {
12216 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12217 rb_set_errinfo(err);
12218 }
12219 return re;
12220 }
12221 #endif
12222
12223 #ifndef RIPPER
12224 void
12225 rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12226 {
12227 struct parser_params *p;
12228 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12229 p->do_print = print;
12230 p->do_loop = loop;
12231 p->do_chomp = chomp;
12232 p->do_split = split;
12233 }
12234
12235 void
12236 rb_parser_warn_location(VALUE vparser, int warn)
12237 {
12238 struct parser_params *p;
12239 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12240 p->warn_location = warn;
12241 }
12242
12243 static NODE *
12244 parser_append_options(struct parser_params *p, NODE *node)
12245 {
12246 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12247 const YYLTYPE *const LOC = &default_location;
12248
12249 if (p->do_print) {
12250 NODE *print = NEW_FCALL(rb_intern("print"),
12251 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12252 LOC);
12253 node = block_append(p, node, print);
12254 }
12255
12256 if (p->do_loop) {
12257 if (p->do_split) {
12258 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12259 NODE *split = NEW_GASGN(rb_intern("$F"),
12260 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12261 rb_intern("split"), args, LOC),
12262 LOC);
12263 node = block_append(p, split, node);
12264 }
12265 if (p->do_chomp) {
12266 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12267 rb_intern("chomp!"), 0, LOC);
12268 node = block_append(p, chomp, node);
12269 }
12270
12271 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12272 }
12273
12274 return node;
12275 }
12276
12277 void
12278 rb_init_parse(void)
12279 {
12280 /* just to suppress unused-function warnings */
12281 (void)nodetype;
12282 (void)nodeline;
12283 }
12284
12285 static ID
12286 internal_id(struct parser_params *p)
12287 {
12288 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12289 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12290 id = max_id - id;
12291 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12292 }
12293 #endif /* !RIPPER */
12294
12295 static void
12296 parser_initialize(struct parser_params *p)
12297 {
12298 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12299 p->command_start = TRUE;
12300 p->ruby_sourcefile_string = Qnil;
12301 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12302 p->node_id = 0;
12303 #ifdef RIPPER
12304 p->delayed.token = Qnil;
12305 p->result = Qnil;
12306 p->parsing_thread = Qnil;
12307 #else
12308 p->error_buffer = Qfalse;
12309 #endif
12310 p->debug_buffer = Qnil;
12311 p->debug_output = rb_stdout;
12312 p->enc = rb_utf8_encoding();
12313 }
12314
12315 #ifdef RIPPER
12316 #define parser_mark ripper_parser_mark
12317 #define parser_free ripper_parser_free
12318 #endif
12319
12320 static void
12321 parser_mark(void *ptr)
12322 {
12323 struct parser_params *p = (struct parser_params*)ptr;
12324
12325 rb_gc_mark(p->lex.input);
12326 rb_gc_mark(p->lex.prevline);
12327 rb_gc_mark(p->lex.lastline);
12328 rb_gc_mark(p->lex.nextline);
12329 rb_gc_mark(p->ruby_sourcefile_string);
12330 rb_gc_mark((VALUE)p->lex.strterm);
12331 rb_gc_mark((VALUE)p->ast);
12332 rb_gc_mark(p->case_labels);
12333 #ifndef RIPPER
12334 rb_gc_mark(p->debug_lines);
12335 rb_gc_mark(p->compile_option);
12336 rb_gc_mark(p->error_buffer);
12337 #else
12338 rb_gc_mark(p->delayed.token);
12339 rb_gc_mark(p->value);
12340 rb_gc_mark(p->result);
12341 rb_gc_mark(p->parsing_thread);
12342 #endif
12343 rb_gc_mark(p->debug_buffer);
12344 rb_gc_mark(p->debug_output);
12345 #ifdef YYMALLOC
12346 rb_gc_mark((VALUE)p->heap);
12347 #endif
12348 }
12349
12350 static void
12351 parser_free(void *ptr)
12352 {
12353 struct parser_params *p = (struct parser_params*)ptr;
12354 struct local_vars *local, *prev;
12355
12356 if (p->tokenbuf) {
12357 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12358 }
12359 for (local = p->lvtbl; local; local = prev) {
12360 if (local->vars) xfree(local->vars);
12361 prev = local->prev;
12362 xfree(local);
12363 }
12364 {
12365 token_info *ptinfo;
12366 while ((ptinfo = p->token_info) != 0) {
12367 p->token_info = ptinfo->next;
12368 xfree(ptinfo);
12369 }
12370 }
12371 xfree(ptr);
12372 }
12373
12374 static size_t
12375 parser_memsize(const void *ptr)
12376 {
12377 struct parser_params *p = (struct parser_params*)ptr;
12378 struct local_vars *local;
12379 size_t size = sizeof(*p);
12380
12381 size += p->toksiz;
12382 for (local = p->lvtbl; local; local = local->prev) {
12383 size += sizeof(*local);
12384 if (local->vars) size += local->vars->capa * sizeof(ID);
12385 }
12386 return size;
12387 }
12388
12389 static const rb_data_type_t parser_data_type = {
12390 #ifndef RIPPER
12391 "parser",
12392 #else
12393 "ripper",
12394 #endif
12395 {
12396 parser_mark,
12397 parser_free,
12398 parser_memsize,
12399 },
12400 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12401 };
12402
12403 #ifndef RIPPER
12404 #undef rb_reserved_word
12405
12406 const struct kwtable *
12407 rb_reserved_word(const char *str, unsigned int len)
12408 {
12409 return reserved_word(str, len);
12410 }
12411
12412 VALUE
12413 rb_parser_new(void)
12414 {
12415 struct parser_params *p;
12416 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12417 &parser_data_type, p);
12418 parser_initialize(p);
12419 return parser;
12420 }
12421
12422 VALUE
12423 rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12424 {
12425 struct parser_params *p;
12426
12427 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12428 p->error_buffer = main ? Qfalse : Qnil;
12429 p->parent_iseq = base;
12430 return vparser;
12431 }
12432 #endif
12433
12434 #ifdef RIPPER
12435 #define rb_parser_end_seen_p ripper_parser_end_seen_p
12436 #define rb_parser_encoding ripper_parser_encoding
12437 #define rb_parser_get_yydebug ripper_parser_get_yydebug
12438 #define rb_parser_set_yydebug ripper_parser_set_yydebug
12439 #define rb_parser_get_debug_output ripper_parser_get_debug_output
12440 #define rb_parser_set_debug_output ripper_parser_set_debug_output
12441 static VALUE ripper_parser_end_seen_p(VALUE vparser);
12442 static VALUE ripper_parser_encoding(VALUE vparser);
12443 static VALUE ripper_parser_get_yydebug(VALUE self);
12444 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12445 static VALUE ripper_parser_get_debug_output(VALUE self);
12446 static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12447
12448 /*
12449 * call-seq:
12450 * ripper.error? -> Boolean
12451 *
12452 * Return true if parsed source has errors.
12453 */
12454 static VALUE
12455 ripper_error_p(VALUE vparser)
12456 {
12457 struct parser_params *p;
12458
12459 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12460 return p->error_p ? Qtrue : Qfalse;
12461 }
12462 #endif
12463
12464 /*
12465 * call-seq:
12466 * ripper.end_seen? -> Boolean
12467 *
12468 * Return true if parsed source ended by +\_\_END\_\_+.
12469 */
12470 VALUE
12471 rb_parser_end_seen_p(VALUE vparser)
12472 {
12473 struct parser_params *p;
12474
12475 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12476 return p->ruby__end__seen ? Qtrue : Qfalse;
12477 }
12478
12479 /*
12480 * call-seq:
12481 * ripper.encoding -> encoding
12482 *
12483 * Return encoding of the source.
12484 */
12485 VALUE
12486 rb_parser_encoding(VALUE vparser)
12487 {
12488 struct parser_params *p;
12489
12490 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12491 return rb_enc_from_encoding(p->enc);
12492 }
12493
12494 #ifdef RIPPER
12495 /*
12496 * call-seq:
12497 * ripper.yydebug -> true or false
12498 *
12499 * Get yydebug.
12500 */
12501 VALUE
12502 rb_parser_get_yydebug(VALUE self)
12503 {
12504 struct parser_params *p;
12505
12506 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12507 return p->debug ? Qtrue : Qfalse;
12508 }
12509 #endif
12510
12511 /*
12512 * call-seq:
12513 * ripper.yydebug = flag
12514 *
12515 * Set yydebug.
12516 */
12517 VALUE
12518 rb_parser_set_yydebug(VALUE self, VALUE flag)
12519 {
12520 struct parser_params *p;
12521
12522 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12523 p->debug = RTEST(flag);
12524 return flag;
12525 }
12526
12527 /*
12528 * call-seq:
12529 * ripper.debug_output -> obj
12530 *
12531 * Get debug output.
12532 */
12533 VALUE
12534 rb_parser_get_debug_output(VALUE self)
12535 {
12536 struct parser_params *p;
12537
12538 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12539 return p->debug_output;
12540 }
12541
12542 /*
12543 * call-seq:
12544 * ripper.debug_output = obj
12545 *
12546 * Set debug output.
12547 */
12548 VALUE
12549 rb_parser_set_debug_output(VALUE self, VALUE output)
12550 {
12551 struct parser_params *p;
12552
12553 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12554 return p->debug_output = output;
12555 }
12556
12557 #ifndef RIPPER
12558 #ifdef YYMALLOC
12559 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12560 /* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12561 * potential memory leak */
12562 #define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12563 #define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12564 (new)->cnt = (cnt), (ptr))
12565
12566 void *
12567 rb_parser_malloc(struct parser_params *p, size_t size)
12568 {
12569 size_t cnt = HEAPCNT(1, size);
12570 rb_imemo_tmpbuf_t *n = NEWHEAP();
12571 void *ptr = xmalloc(size);
12572
12573 return ADD2HEAP(n, cnt, ptr);
12574 }
12575
12576 void *
12577 rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12578 {
12579 size_t cnt = HEAPCNT(nelem, size);
12580 rb_imemo_tmpbuf_t *n = NEWHEAP();
12581 void *ptr = xcalloc(nelem, size);
12582
12583 return ADD2HEAP(n, cnt, ptr);
12584 }
12585
12586 void *
12587 rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12588 {
12589 rb_imemo_tmpbuf_t *n;
12590 size_t cnt = HEAPCNT(1, size);
12591
12592 if (ptr && (n = p->heap) != NULL) {
12593 do {
12594 if (n->ptr == ptr) {
12595 n->ptr = ptr = xrealloc(ptr, size);
12596 if (n->cnt) n->cnt = cnt;
12597 return ptr;
12598 }
12599 } while ((n = n->next) != NULL);
12600 }
12601 n = NEWHEAP();
12602 ptr = xrealloc(ptr, size);
12603 return ADD2HEAP(n, cnt, ptr);
12604 }
12605
12606 void
12607 rb_parser_free(struct parser_params *p, void *ptr)
12608 {
12609 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12610
12611 while ((n = *prev) != NULL) {
12612 if (n->ptr == ptr) {
12613 *prev = n->next;
12614 rb_gc_force_recycle((VALUE)n);
12615 break;
12616 }
12617 prev = &n->next;
12618 }
12619 xfree(ptr);
12620 }
12621 #endif
12622
12623 void
12624 rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12625 {
12626 va_list ap;
12627 VALUE mesg = p->debug_buffer;
12628
12629 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12630 va_start(ap, fmt);
12631 rb_str_vcatf(mesg, fmt, ap);
12632 va_end(ap);
12633 if (RSTRING_END(mesg)[-1] == '\n') {
12634 rb_io_write(p->debug_output, mesg);
12635 p->debug_buffer = Qnil;
12636 }
12637 }
12638
12639 static void
12640 parser_compile_error(struct parser_params *p, const char *fmt, ...)
12641 {
12642 va_list ap;
12643
12644 rb_io_flush(p->debug_output);
12645 p->error_p = 1;
12646 va_start(ap, fmt);
12647 p->error_buffer =
12648 rb_syntax_error_append(p->error_buffer,
12649 p->ruby_sourcefile_string,
12650 p->ruby_sourceline,
12651 rb_long2int(p->lex.pcur - p->lex.pbeg),
12652 p->enc, fmt, ap);
12653 va_end(ap);
12654 }
12655
12656 static size_t
12657 count_char(const char *str, int c)
12658 {
12659 int n = 0;
12660 while (str[n] == c) ++n;
12661 return n;
12662 }
12663
12664 /*
12665 * strip enclosing double-quotes, same as the default yytnamerr except
12666 * for that single-quotes matching back-quotes do not stop stripping.
12667 *
12668 * "\"`class' keyword\"" => "`class' keyword"
12669 */
12670 RUBY_FUNC_EXPORTED size_t
12671 rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12672 {
12673 YYUSE(p);
12674 if (*yystr == '"') {
12675 size_t yyn = 0, bquote = 0;
12676 const char *yyp = yystr;
12677
12678 while (*++yyp) {
12679 switch (*yyp) {
12680 case '`':
12681 if (!bquote) {
12682 bquote = count_char(yyp+1, '`') + 1;
12683 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12684 yyn += bquote;
12685 yyp += bquote - 1;
12686 break;
12687 }
12688 goto default_char;
12689
12690 case '\'':
12691 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12692 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12693 yyn += bquote;
12694 yyp += bquote - 1;
12695 bquote = 0;
12696 break;
12697 }
12698 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12699 if (yyres) memcpy(yyres + yyn, yyp, 3);
12700 yyn += 3;
12701 yyp += 2;
12702 break;
12703 }
12704 goto do_not_strip_quotes;
12705
12706 case ',':
12707 goto do_not_strip_quotes;
12708
12709 case '\\':
12710 if (*++yyp != '\\')
12711 goto do_not_strip_quotes;
12712 /* Fall through. */
12713 default_char:
12714 default:
12715 if (yyres)
12716 yyres[yyn] = *yyp;
12717 yyn++;
12718 break;
12719
12720 case '"':
12721 case '\0':
12722 if (yyres)
12723 yyres[yyn] = '\0';
12724 return yyn;
12725 }
12726 }
12727 do_not_strip_quotes: ;
12728 }
12729
12730 if (!yyres) return strlen(yystr);
12731
12732 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12733 }
12734 #endif
12735
12736 #ifdef RIPPER
12737 #ifdef RIPPER_DEBUG
12738 /* :nodoc: */
12739 static VALUE
12740 ripper_validate_object(VALUE self, VALUE x)
12741 {
12742 if (x == Qfalse) return x;
12743 if (x == Qtrue) return x;
12744 if (x == Qnil) return x;
12745 if (x == Qundef)
12746 rb_raise(rb_eArgError, "Qundef given");
12747 if (FIXNUM_P(x)) return x;
12748 if (SYMBOL_P(x)) return x;
12749 switch (BUILTIN_TYPE(x)) {
12750 case T_STRING:
12751 case T_OBJECT:
12752 case T_ARRAY:
12753 case T_BIGNUM:
12754 case T_FLOAT:
12755 case T_COMPLEX:
12756 case T_RATIONAL:
12757 break;
12758 case T_NODE:
12759 if (nd_type((NODE *)x) != NODE_RIPPER) {
12760 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12761 }
12762 x = ((NODE *)x)->nd_rval;
12763 break;
12764 default:
12765 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12766 (void *)x, rb_obj_classname(x));
12767 }
12768 if (!RBASIC_CLASS(x)) {
12769 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12770 (void *)x, rb_builtin_type_name(TYPE(x)));
12771 }
12772 return x;
12773 }
12774 #endif
12775
12776 #define validate(x) ((x) = get_value(x))
12777
12778 static VALUE
12779 ripper_dispatch0(struct parser_params *p, ID mid)
12780 {
12781 return rb_funcall(p->value, mid, 0);
12782 }
12783
12784 static VALUE
12785 ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12786 {
12787 validate(a);
12788 return rb_funcall(p->value, mid, 1, a);
12789 }
12790
12791 static VALUE
12792 ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12793 {
12794 validate(a);
12795 validate(b);
12796 return rb_funcall(p->value, mid, 2, a, b);
12797 }
12798
12799 static VALUE
12800 ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12801 {
12802 validate(a);
12803 validate(b);
12804 validate(c);
12805 return rb_funcall(p->value, mid, 3, a, b, c);
12806 }
12807
12808 static VALUE
12809 ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12810 {
12811 validate(a);
12812 validate(b);
12813 validate(c);
12814 validate(d);
12815 return rb_funcall(p->value, mid, 4, a, b, c, d);
12816 }
12817
12818 static VALUE
12819 ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12820 {
12821 validate(a);
12822 validate(b);
12823 validate(c);
12824 validate(d);
12825 validate(e);
12826 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12827 }
12828
12829 static VALUE
12830 ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12831 {
12832 validate(a);
12833 validate(b);
12834 validate(c);
12835 validate(d);
12836 validate(e);
12837 validate(f);
12838 validate(g);
12839 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12840 }
12841
12842 static ID
12843 ripper_get_id(VALUE v)
12844 {
12845 NODE *nd;
12846 if (!RB_TYPE_P(v, T_NODE)) return 0;
12847 nd = (NODE *)v;
12848 if (nd_type(nd) != NODE_RIPPER) return 0;
12849 return nd->nd_vid;
12850 }
12851
12852 static VALUE
12853 ripper_get_value(VALUE v)
12854 {
12855 NODE *nd;
12856 if (v == Qundef) return Qnil;
12857 if (!RB_TYPE_P(v, T_NODE)) return v;
12858 nd = (NODE *)v;
12859 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12860 return nd->nd_rval;
12861 }
12862
12863 static void
12864 ripper_error(struct parser_params *p)
12865 {
12866 p->error_p = TRUE;
12867 }
12868
12869 static void
12870 ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12871 {
12872 VALUE str;
12873 va_list args;
12874
12875 va_start(args, fmt);
12876 str = rb_vsprintf(fmt, args);
12877 va_end(args);
12878 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12879 ripper_error(p);
12880 }
12881
12882 static VALUE
12883 ripper_lex_get_generic(struct parser_params *p, VALUE src)
12884 {
12885 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12886 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12887 rb_raise(rb_eTypeError,
12888 "gets returned %"PRIsVALUE" (expected String or nil)",
12889 rb_obj_class(line));
12890 }
12891 return line;
12892 }
12893
12894 static VALUE
12895 ripper_lex_io_get(struct parser_params *p, VALUE src)
12896 {
12897 return rb_io_gets(src);
12898 }
12899
12900 static VALUE
12901 ripper_s_allocate(VALUE klass)
12902 {
12903 struct parser_params *p;
12904 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12905 &parser_data_type, p);
12906 p->value = self;
12907 return self;
12908 }
12909
12910 #define ripper_initialized_p(r) ((r)->lex.input != 0)
12911
12912 /*
12913 * call-seq:
12914 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12915 *
12916 * Create a new Ripper object.
12917 * _src_ must be a String, an IO, or an Object which has #gets method.
12918 *
12919 * This method does not starts parsing.
12920 * See also Ripper#parse and Ripper.parse.
12921 */
12922 static VALUE
12923 ripper_initialize(int argc, VALUE *argv, VALUE self)
12924 {
12925 struct parser_params *p;
12926 VALUE src, fname, lineno;
12927
12928 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12929 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12930 if (RB_TYPE_P(src, T_FILE)) {
12931 p->lex.gets = ripper_lex_io_get;
12932 }
12933 else if (rb_respond_to(src, id_gets)) {
12934 p->lex.gets = ripper_lex_get_generic;
12935 }
12936 else {
12937 StringValue(src);
12938 p->lex.gets = lex_get_str;
12939 }
12940 p->lex.input = src;
12941 p->eofp = 0;
12942 if (NIL_P(fname)) {
12943 fname = STR_NEW2("(ripper)");
12944 OBJ_FREEZE(fname);
12945 }
12946 else {
12947 StringValueCStr(fname);
12948 fname = rb_str_new_frozen(fname);
12949 }
12950 parser_initialize(p);
12951
12952 p->ruby_sourcefile_string = fname;
12953 p->ruby_sourcefile = RSTRING_PTR(fname);
12954 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12955
12956 return Qnil;
12957 }
12958
12959 static VALUE
12960 ripper_parse0(VALUE parser_v)
12961 {
12962 struct parser_params *p;
12963
12964 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12965 parser_prepare(p);
12966 p->ast = rb_ast_new();
12967 ripper_yyparse((void*)p);
12968 rb_ast_dispose(p->ast);
12969 p->ast = 0;
12970 return p->result;
12971 }
12972
12973 static VALUE
12974 ripper_ensure(VALUE parser_v)
12975 {
12976 struct parser_params *p;
12977
12978 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12979 p->parsing_thread = Qnil;
12980 return Qnil;
12981 }
12982
12983 /*
12984 * call-seq:
12985 * ripper.parse
12986 *
12987 * Start parsing and returns the value of the root action.
12988 */
12989 static VALUE
12990 ripper_parse(VALUE self)
12991 {
12992 struct parser_params *p;
12993
12994 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12995 if (!ripper_initialized_p(p)) {
12996 rb_raise(rb_eArgError, "method called for uninitialized object");
12997 }
12998 if (!NIL_P(p->parsing_thread)) {
12999 if (p->parsing_thread == rb_thread_current())
13000 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13001 else
13002 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13003 }
13004 p->parsing_thread = rb_thread_current();
13005 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13006
13007 return p->result;
13008 }
13009
13010 /*
13011 * call-seq:
13012 * ripper.column -> Integer
13013 *
13014 * Return column number of current parsing line.
13015 * This number starts from 0.
13016 */
13017 static VALUE
13018 ripper_column(VALUE self)
13019 {
13020 struct parser_params *p;
13021 long col;
13022
13023 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13024 if (!ripper_initialized_p(p)) {
13025 rb_raise(rb_eArgError, "method called for uninitialized object");
13026 }
13027 if (NIL_P(p->parsing_thread)) return Qnil;
13028 col = p->lex.ptok - p->lex.pbeg;
13029 return LONG2NUM(col);
13030 }
13031
13032 /*
13033 * call-seq:
13034 * ripper.filename -> String
13035 *
13036 * Return current parsing filename.
13037 */
13038 static VALUE
13039 ripper_filename(VALUE self)
13040 {
13041 struct parser_params *p;
13042
13043 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13044 if (!ripper_initialized_p(p)) {
13045 rb_raise(rb_eArgError, "method called for uninitialized object");
13046 }
13047 return p->ruby_sourcefile_string;
13048 }
13049
13050 /*
13051 * call-seq:
13052 * ripper.lineno -> Integer
13053 *
13054 * Return line number of current parsing line.
13055 * This number starts from 1.
13056 */
13057 static VALUE
13058 ripper_lineno(VALUE self)
13059 {
13060 struct parser_params *p;
13061
13062 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13063 if (!ripper_initialized_p(p)) {
13064 rb_raise(rb_eArgError, "method called for uninitialized object");
13065 }
13066 if (NIL_P(p->parsing_thread)) return Qnil;
13067 return INT2NUM(p->ruby_sourceline);
13068 }
13069
13070 /*
13071 * call-seq:
13072 * ripper.state -> Integer
13073 *
13074 * Return scanner state of current token.
13075 */
13076 static VALUE
13077 ripper_state(VALUE self)
13078 {
13079 struct parser_params *p;
13080
13081 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13082 if (!ripper_initialized_p(p)) {
13083 rb_raise(rb_eArgError, "method called for uninitialized object");
13084 }
13085 if (NIL_P(p->parsing_thread)) return Qnil;
13086 return INT2NUM(p->lex.state);
13087 }
13088
13089 /*
13090 * call-seq:
13091 * ripper.token -> String
13092 *
13093 * Return the current token string.
13094 */
13095 static VALUE
13096 ripper_token(VALUE self)
13097 {
13098 struct parser_params *p;
13099 long pos, len;
13100
13101 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13102 if (!ripper_initialized_p(p)) {
13103 rb_raise(rb_eArgError, "method called for uninitialized object");
13104 }
13105 if (NIL_P(p->parsing_thread)) return Qnil;
13106 pos = p->lex.ptok - p->lex.pbeg;
13107 len = p->lex.pcur - p->lex.ptok;
13108 return rb_str_subseq(p->lex.lastline, pos, len);
13109 }
13110
13111 #ifdef RIPPER_DEBUG
13112 /* :nodoc: */
13113 static VALUE
13114 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13115 {
13116 StringValue(msg);
13117 if (obj == Qundef) {
13118 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13119 }
13120 return Qnil;
13121 }
13122
13123 /* :nodoc: */
13124 static VALUE
13125 ripper_value(VALUE self, VALUE obj)
13126 {
13127 return ULONG2NUM(obj);
13128 }
13129 #endif
13130
13131 /*
13132 * call-seq:
13133 * Ripper.lex_state_name(integer) -> string
13134 *
13135 * Returns a string representation of lex_state.
13136 */
13137 static VALUE
13138 ripper_lex_state_name(VALUE self, VALUE state)
13139 {
13140 return rb_parser_lex_state_name(NUM2INT(state));
13141 }
13142
13143 void
13144 Init_ripper(void)
13145 {
13146 ripper_init_eventids1();
13147 ripper_init_eventids2();
13148 id_warn = rb_intern_const("warn");
13149 id_warning = rb_intern_const("warning");
13150 id_gets = rb_intern_const("gets");
13151 id_assoc = rb_intern_const("=>");
13152
13153 (void)yystpcpy; /* may not used in newer bison */
13154
13155 InitVM(ripper);
13156 }
13157
13158 void
13159 InitVM_ripper(void)
13160 {
13161 VALUE Ripper;
13162
13163 Ripper = rb_define_class("Ripper", rb_cObject);
13164 /* version of Ripper */
13165 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13166 rb_define_alloc_func(Ripper, ripper_s_allocate);
13167 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13168 rb_define_method(Ripper, "parse", ripper_parse, 0);
13169 rb_define_method(Ripper, "column", ripper_column, 0);
13170 rb_define_method(Ripper, "filename", ripper_filename, 0);
13171 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13172 rb_define_method(Ripper, "state", ripper_state, 0);
13173 rb_define_method(Ripper, "token", ripper_token, 0);
13174 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13175 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13176 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13177 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13178 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13179 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13180 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13181 #ifdef RIPPER_DEBUG
13182 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13183 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13184 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13185 #endif
13186
13187 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13188 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13189
13190 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13191
13192 <% @exprs.each do |expr, desc| -%>
13193 /* <%=desc%> */
13194 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
13195 <% end %>
13196 ripper_init_eventids1_table(Ripper);
13197 ripper_init_eventids2_table(Ripper);
13198
13199 # if 0
13200 /* Hack to let RDoc document SCRIPT_LINES__ */
13201
13202 /*
13203 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13204 * after the assignment will be added as an Array of lines with the file
13205 * name as the key.
13206 */
13207 rb_define_global_const("SCRIPT_LINES__", Qnil);
13208 #endif
13209
13210 }
13211 #endif /* RIPPER */
13212
13213 /*
13214 * Local variables:
13215 * mode: c
13216 * c-file-style: "ruby"
13217 * End:
13218 */