* transcode.c (econv_primitive_convert): add output_byteoffset
[ruby-svn.git] / parse.y
blob9debf69e9b0f9714de881fe15bf97cceee410f42
1 /**********************************************************************
3 parse.y -
5 $Author$
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
14 #define YYDEBUG 1
15 #define YYERROR_VERBOSE 1
16 #define YYSTACK_USE_ALLOCA 0
18 #include "ruby/ruby.h"
19 #include "ruby/intern.h"
20 #include "ruby/node.h"
21 #include "ruby/st.h"
22 #include "ruby/encoding.h"
23 #include "id.h"
24 #include "regenc.h"
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
29 #define YYMALLOC(size) rb_parser_malloc(parser, size)
30 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, ptr, size)
31 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, nelem, size)
32 #define YYFREE(ptr) rb_parser_free(parser, ptr)
33 #define malloc YYMALLOC
34 #define realloc YYREALLOC
35 #define calloc YYCALLOC
36 #define free YYFREE
38 #define ID_SCOPE_SHIFT 3
39 #define ID_SCOPE_MASK 0x07
40 #define ID_LOCAL 0x00
41 #define ID_INSTANCE 0x01
42 #define ID_GLOBAL 0x03
43 #define ID_ATTRSET 0x04
44 #define ID_CONST 0x05
45 #define ID_CLASS 0x06
46 #define ID_JUNK 0x07
47 #define ID_INTERNAL ID_JUNK
49 #define is_notop_id(id) ((id)>tLAST_TOKEN)
50 #define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
51 #define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL)
52 #define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE)
53 #define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET)
54 #define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
55 #define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
56 #define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK)
58 #define is_asgn_or_id(id) ((is_notop_id(id)) && \
59 (((id)&ID_SCOPE_MASK) == ID_GLOBAL || \
60 ((id)&ID_SCOPE_MASK) == ID_INSTANCE || \
61 ((id)&ID_SCOPE_MASK) == ID_CLASS))
63 enum lex_state_e {
64 EXPR_BEG, /* ignore newline, +/- is a sign. */
65 EXPR_END, /* newline significant, +/- is a operator. */
66 EXPR_ENDARG, /* ditto, and unbound braces. */
67 EXPR_ARG, /* newline significant, +/- is a operator. */
68 EXPR_CMDARG, /* newline significant, +/- is a operator. */
69 EXPR_MID, /* newline significant, +/- is a operator. */
70 EXPR_FNAME, /* ignore newline, no reserved words. */
71 EXPR_DOT, /* right after `.' or `::', no reserved words. */
72 EXPR_CLASS, /* immediate after `class', no here document. */
73 EXPR_VALUE /* alike EXPR_BEG but label is disallowed. */
76 # ifdef HAVE_LONG_LONG
77 typedef unsigned LONG_LONG stack_type;
78 # else
79 typedef unsigned long stack_type;
80 # endif
82 # define BITSTACK_PUSH(stack, n) (stack = (stack<<1)|((n)&1))
83 # define BITSTACK_POP(stack) (stack = stack >> 1)
84 # define BITSTACK_LEXPOP(stack) (stack = (stack >> 1) | (stack & 1))
85 # define BITSTACK_SET_P(stack) (stack&1)
87 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, n)
88 #define COND_POP() BITSTACK_POP(cond_stack)
89 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
90 #define COND_P() BITSTACK_SET_P(cond_stack)
92 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, n)
93 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
94 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
95 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
97 /* must sync with real YYSTYPE */
98 union tmpyystype {
99 VALUE val;
100 NODE *node;
101 unsigned long id;
102 int num;
103 struct RVarmap *vars;
106 struct vtable {
107 ID *tbl;
108 int pos;
109 int capa;
110 struct vtable *prev;
113 struct local_vars {
114 struct vtable *args;
115 struct vtable *vars;
116 struct local_vars *prev;
119 #define DVARS_INHERIT ((void*)1)
120 #define DVARS_TOPSCOPE NULL
121 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
122 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
124 #ifndef RIPPER
125 static int
126 vtable_size(const struct vtable *tbl)
128 if (POINTER_P(tbl)) {
129 return tbl->pos;
131 else {
132 return 0;
136 #define VTBL_DEBUG 0
138 static struct vtable *
139 vtable_alloc(struct vtable *prev)
141 struct vtable *tbl = ALLOC(struct vtable);
142 tbl->pos = 0;
143 tbl->capa = 8;
144 tbl->tbl = ALLOC_N(ID, tbl->capa);
145 tbl->prev = prev;
146 if (VTBL_DEBUG) printf("vtable_alloc: %p\n", tbl);
147 return tbl;
150 static void
151 vtable_free(struct vtable *tbl)
153 if (VTBL_DEBUG)printf("vtable_free: %p\n", tbl);
154 if (POINTER_P(tbl)) {
155 if (tbl->tbl) {
156 xfree(tbl->tbl);
158 xfree(tbl);
162 static void
163 vtable_add(struct vtable *tbl, ID id)
165 if (!POINTER_P(tbl)) {
166 rb_bug("vtable_add: vtable is not allocated (%p)", tbl);
168 if (VTBL_DEBUG) printf("vtable_add: %p, %s\n", tbl, rb_id2name(id));
170 if (tbl->pos == tbl->capa) {
171 tbl->capa = tbl->capa * 2;
172 REALLOC_N(tbl->tbl, ID, tbl->capa);
174 tbl->tbl[tbl->pos++] = id;
177 static int
178 vtable_included(const struct vtable * tbl, ID id)
180 int i;
182 if (POINTER_P(tbl)) {
183 for (i = 0; i < tbl->pos; i++) {
184 if (tbl->tbl[i] == id) {
185 return 1;
189 return 0;
191 #endif
194 Structure of Lexer Buffer:
196 lex_pbeg tokp lex_p lex_pend
197 | | | |
198 |-----------+--------------+------------|
199 |<------------>|
200 token
202 struct parser_params {
203 int is_ripper;
204 NODE *heap;
206 union tmpyystype *parser_yylval; /* YYSTYPE not defined yet */
207 VALUE eofp;
209 NODE *parser_lex_strterm;
210 enum lex_state_e parser_lex_state;
211 stack_type parser_cond_stack;
212 stack_type parser_cmdarg_stack;
213 int parser_class_nest;
214 int parser_paren_nest;
215 int parser_lpar_beg;
216 int parser_in_single;
217 int parser_in_def;
218 int parser_compile_for_eval;
219 VALUE parser_cur_mid;
220 int parser_in_defined;
221 char *parser_tokenbuf;
222 int parser_tokidx;
223 int parser_toksiz;
224 VALUE parser_lex_input;
225 VALUE parser_lex_lastline;
226 VALUE parser_lex_nextline;
227 const char *parser_lex_pbeg;
228 const char *parser_lex_p;
229 const char *parser_lex_pend;
230 int parser_heredoc_end;
231 int parser_command_start;
232 NODE *parser_deferred_nodes;
233 int parser_lex_gets_ptr;
234 VALUE (*parser_lex_gets)(struct parser_params*,VALUE);
235 struct local_vars *parser_lvtbl;
236 int parser_ruby__end__seen;
237 int line_count;
238 int has_shebang;
239 char *parser_ruby_sourcefile; /* current source file */
240 int parser_ruby_sourceline; /* current line no. */
241 rb_encoding *enc;
242 rb_encoding *utf8;
244 int parser_yydebug;
246 #ifndef RIPPER
247 /* Ruby core only */
248 NODE *parser_eval_tree_begin;
249 NODE *parser_eval_tree;
250 VALUE debug_lines;
251 VALUE coverage;
252 int nerr;
253 #else
254 /* Ripper only */
255 VALUE parser_ruby_sourcefile_string;
256 const char *tokp;
257 VALUE delayed;
258 int delayed_line;
259 int delayed_col;
261 VALUE value;
262 VALUE result;
263 VALUE parsing_thread;
264 int toplevel_p;
265 #endif
268 #define UTF8_ENC() (parser->utf8 ? parser->utf8 : \
269 (parser->utf8 = rb_utf8_encoding()))
270 #define STR_NEW(p,n) rb_enc_str_new((p),(n),parser->enc)
271 #define STR_NEW0() rb_usascii_str_new(0,0)
272 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),parser->enc)
273 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),parser->enc)
274 #define STR_ENC(m) ((m)?parser->enc:rb_usascii_encoding())
275 #define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT)
276 #define TOK_INTERN(mb) rb_intern3(tok(), toklen(), STR_ENC(mb))
278 #ifdef YYMALLOC
279 void *rb_parser_malloc(struct parser_params *, size_t);
280 void *rb_parser_realloc(struct parser_params *, void *, size_t);
281 void *rb_parser_calloc(struct parser_params *, size_t, size_t);
282 void rb_parser_free(struct parser_params *, void *);
283 #endif
285 static int parser_yyerror(struct parser_params*, const char*);
286 #define yyerror(msg) parser_yyerror(parser, msg)
288 #define YYLEX_PARAM parser
290 #define lex_strterm (parser->parser_lex_strterm)
291 #define lex_state (parser->parser_lex_state)
292 #define cond_stack (parser->parser_cond_stack)
293 #define cmdarg_stack (parser->parser_cmdarg_stack)
294 #define class_nest (parser->parser_class_nest)
295 #define paren_nest (parser->parser_paren_nest)
296 #define lpar_beg (parser->parser_lpar_beg)
297 #define in_single (parser->parser_in_single)
298 #define in_def (parser->parser_in_def)
299 #define compile_for_eval (parser->parser_compile_for_eval)
300 #define cur_mid (parser->parser_cur_mid)
301 #define in_defined (parser->parser_in_defined)
302 #define tokenbuf (parser->parser_tokenbuf)
303 #define tokidx (parser->parser_tokidx)
304 #define toksiz (parser->parser_toksiz)
305 #define lex_input (parser->parser_lex_input)
306 #define lex_lastline (parser->parser_lex_lastline)
307 #define lex_nextline (parser->parser_lex_nextline)
308 #define lex_pbeg (parser->parser_lex_pbeg)
309 #define lex_p (parser->parser_lex_p)
310 #define lex_pend (parser->parser_lex_pend)
311 #define heredoc_end (parser->parser_heredoc_end)
312 #define command_start (parser->parser_command_start)
313 #define deferred_nodes (parser->parser_deferred_nodes)
314 #define lex_gets_ptr (parser->parser_lex_gets_ptr)
315 #define lex_gets (parser->parser_lex_gets)
316 #define lvtbl (parser->parser_lvtbl)
317 #define ruby__end__seen (parser->parser_ruby__end__seen)
318 #define ruby_sourceline (parser->parser_ruby_sourceline)
319 #define ruby_sourcefile (parser->parser_ruby_sourcefile)
320 #define yydebug (parser->parser_yydebug)
321 #ifdef RIPPER
322 #else
323 #define ruby_eval_tree (parser->parser_eval_tree)
324 #define ruby_eval_tree_begin (parser->parser_eval_tree_begin)
325 #define ruby_debug_lines (parser->debug_lines)
326 #define ruby_coverage (parser->coverage)
327 #endif
329 static int yylex(void*, void*);
331 #ifndef RIPPER
332 #define yyparse ruby_yyparse
334 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE);
335 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, type, a1, a2, a3)
337 static NODE *cond_gen(struct parser_params*,NODE*);
338 #define cond(node) cond_gen(parser, node)
339 static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*);
340 #define logop(type,node1,node2) logop_gen(parser, type, node1, node2)
342 static NODE *newline_node(NODE*);
343 static void fixpos(NODE*,NODE*);
345 static int value_expr_gen(struct parser_params*,NODE*);
346 static void void_expr_gen(struct parser_params*,NODE*);
347 static NODE *remove_begin(NODE*);
348 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
349 #define void_expr0(node) void_expr_gen(parser, (node))
350 #define void_expr(node) void_expr0((node) = remove_begin(node))
351 static void void_stmts_gen(struct parser_params*,NODE*);
352 #define void_stmts(node) void_stmts_gen(parser, node)
353 static void reduce_nodes_gen(struct parser_params*,NODE**);
354 #define reduce_nodes(n) reduce_nodes_gen(parser,n)
355 static void block_dup_check_gen(struct parser_params*,NODE*,NODE*);
356 #define block_dup_check(n1,n2) block_dup_check_gen(parser,n1,n2)
358 static NODE *block_append_gen(struct parser_params*,NODE*,NODE*);
359 #define block_append(h,t) block_append_gen(parser,h,t)
360 static NODE *list_append_gen(struct parser_params*,NODE*,NODE*);
361 #define list_append(l,i) list_append_gen(parser,l,i)
362 static NODE *list_concat_gen(struct parser_params*,NODE*,NODE*);
363 #define list_concat(h,t) list_concat_gen(parser,h,t)
364 static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*);
365 #define arg_append(h,t) arg_append_gen(parser,h,t)
366 static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*);
367 #define arg_concat(h,t) arg_concat_gen(parser,h,t)
368 static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*);
369 #define literal_concat(h,t) literal_concat_gen(parser,h,t)
370 static NODE *new_evstr_gen(struct parser_params*,NODE*);
371 #define new_evstr(n) new_evstr_gen(parser,n)
372 static NODE *evstr2dstr_gen(struct parser_params*,NODE*);
373 #define evstr2dstr(n) evstr2dstr_gen(parser,n)
374 static NODE *splat_array(NODE*);
376 static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*);
377 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, recv,id,arg1)
378 static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID);
379 #define call_uni_op(recv,id) call_uni_op_gen(parser, recv,id)
381 static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,ID);
382 #define new_args(f,o,r,p,b) new_args_gen(parser, f,o,r,p,b)
383 static void shadowing_lvar_gen(struct parser_params*,ID);
384 #define shadowing_lvar(name) shadowing_lvar_gen(parser, name)
386 static NODE *negate_lit(NODE*);
387 static NODE *ret_args_gen(struct parser_params*,NODE*);
388 #define ret_args(node) ret_args_gen(parser, node)
389 static NODE *arg_blk_pass(NODE*,NODE*);
390 static NODE *new_yield_gen(struct parser_params*,NODE*);
391 #define new_yield(node) new_yield_gen(parser, node)
393 static NODE *gettable_gen(struct parser_params*,ID);
394 #define gettable(id) gettable_gen(parser,id)
395 static NODE *assignable_gen(struct parser_params*,ID,NODE*);
396 #define assignable(id,node) assignable_gen(parser, id, node)
397 static void new_bv_gen(struct parser_params*,ID);
398 #define new_bv(id) new_bv_gen(parser, id)
399 static NODE *aryset_gen(struct parser_params*,NODE*,NODE*);
400 #define aryset(node1,node2) aryset_gen(parser, node1, node2)
401 static NODE *attrset_gen(struct parser_params*,NODE*,ID);
402 #define attrset(node,id) attrset_gen(parser, node, id)
404 static void rb_backref_error_gen(struct parser_params*,NODE*);
405 #define rb_backref_error(n) rb_backref_error_gen(parser,n)
406 static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*);
407 #define node_assign(node1, node2) node_assign_gen(parser, node1, node2)
409 static NODE *match_op_gen(struct parser_params*,NODE*,NODE*);
410 #define match_op(node1,node2) match_op_gen(parser, node1, node2)
412 static void local_push_gen(struct parser_params*,int);
413 #define local_push(top) local_push_gen(parser,top)
414 static void local_pop_gen(struct parser_params*);
415 #define local_pop() local_pop_gen(parser)
416 static int local_var_gen(struct parser_params*, ID);
417 #define local_var(id) local_var_gen(parser, id);
418 static int arg_var_gen(struct parser_params*, ID);
419 #define arg_var(id) arg_var_gen(parser, id)
420 static int local_id_gen(struct parser_params*, ID);
421 #define local_id(id) local_id_gen(parser, id)
422 static ID *local_tbl_gen(struct parser_params*);
423 #define local_tbl() local_tbl_gen(parser)
424 static ID internal_id_gen(struct parser_params*);
425 #define internal_id() internal_id_gen(parser)
427 static void dyna_push_gen(struct parser_params*);
428 #define dyna_push() dyna_push_gen(parser)
429 static void dyna_pop_gen(struct parser_params*);
430 #define dyna_pop() dyna_pop_gen(parser)
431 static int dyna_in_block_gen(struct parser_params*);
432 #define dyna_in_block() dyna_in_block_gen(parser)
433 #define dyna_var(id) local_var(id)
434 static int dvar_defined_gen(struct parser_params*,ID);
435 #define dvar_defined(id) dvar_defined_gen(parser, id)
436 static int dvar_curr_gen(struct parser_params*,ID);
437 #define dvar_curr(id) dvar_curr_gen(parser, id)
439 static void fixup_nodes(NODE **);
441 extern int rb_dvar_defined(ID);
442 extern int rb_local_defined(ID);
443 extern int rb_parse_in_eval(void);
445 static VALUE reg_compile_gen(struct parser_params*, VALUE, int);
446 #define reg_compile(str,options) reg_compile_gen(parser, str, options)
447 static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int);
448 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, str, options)
449 static void reg_fragment_check_gen(struct parser_params*, VALUE, int);
450 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, str, options)
451 static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match);
452 #define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,regexp,match)
453 int rb_enc_symname2_p(const char *, int, rb_encoding *);
454 #else
455 #define remove_begin(node) (node)
456 #endif /* !RIPPER */
457 static int lvar_defined_gen(struct parser_params*, ID);
458 #define lvar_defined(id) lvar_defined_gen(parser, id)
460 #define RE_OPTION_ONCE (1<<16)
461 #define RE_OPTION_ENCODING_SHIFT 8
462 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
463 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
464 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
465 #define RE_OPTION_MASK 0xff
466 #define RE_OPTION_ARG_ENCODING_NONE 32
468 #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
469 #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
470 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
471 #define nd_func u1.id
472 #if SIZEOF_SHORT == 2
473 #define nd_term(node) ((signed short)(node)->u2.id)
474 #else
475 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
476 #endif
477 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
478 #define nd_nest u3.cnt
480 /****** Ripper *******/
482 #ifdef RIPPER
483 #define RIPPER_VERSION "0.1.0"
485 #include "eventids1.c"
486 #include "eventids2.c"
487 static ID ripper_id_gets;
489 static VALUE ripper_dispatch0(struct parser_params*,ID);
490 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
491 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
492 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
493 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
494 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
496 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
497 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), a)
498 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), a, b)
499 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), a, b, c)
500 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), a, b, c, d)
501 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), a, b, c, d, e)
503 #define yyparse ripper_yyparse
505 static VALUE ripper_intern(const char*);
506 static VALUE ripper_id2sym(ID);
508 #define arg_new() dispatch0(args_new)
509 #define arg_add(l,a) dispatch2(args_add, l, a)
510 #define arg_prepend(l,a) dispatch2(args_prepend, l, a)
511 #define arg_add_star(l,a) dispatch2(args_add_star, l, a)
512 #define arg_add_block(l,b) dispatch2(args_add_block, l, b)
513 #define arg_add_optblock(l,b) ((b)==Qundef? l : dispatch2(args_add_block, l, b))
514 #define bare_assoc(v) dispatch1(bare_assoc_hash, v)
515 #define arg_add_assocs(l,b) arg_add(l, bare_assoc(b))
517 #define args2mrhs(a) dispatch1(mrhs_new_from_args, a)
518 #define mrhs_new() dispatch0(mrhs_new)
519 #define mrhs_add(l,a) dispatch2(mrhs_add, l, a)
520 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, l, a)
522 #define mlhs_new() dispatch0(mlhs_new)
523 #define mlhs_add(l,a) dispatch2(mlhs_add, l, a)
524 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, l, a)
526 #define params_new(pars, opts, rest, pars2, blk) \
527 dispatch5(params, pars, opts, rest, pars2, blk)
529 #define blockvar_new(p,v) dispatch2(block_var, p, v)
530 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, l, a)
531 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, l, a)
533 #define method_optarg(m,a) ((a)==Qundef ? m : dispatch2(method_add_arg,m,a))
534 #define method_arg(m,a) dispatch2(method_add_arg,m,a)
535 #define method_add_block(m,b) dispatch2(method_add_block, m, b)
537 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
539 #define FIXME 0
541 #endif /* RIPPER */
543 #ifndef RIPPER
544 # define ifndef_ripper(x) x
545 #else
546 # define ifndef_ripper(x)
547 #endif
549 #ifndef RIPPER
550 # define rb_warn0(fmt) rb_compile_warn(ruby_sourcefile, ruby_sourceline, fmt)
551 # define rb_warnI(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, fmt, a)
552 # define rb_warnS(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, fmt, a)
553 # define rb_warning0(fmt) rb_compile_warning(ruby_sourcefile, ruby_sourceline, fmt)
554 # define rb_warningS(fmt,a) rb_compile_warning(ruby_sourcefile, ruby_sourceline, fmt, a)
555 #else
556 # define rb_warn0(fmt) ripper_warn0(parser, fmt)
557 # define rb_warnI(fmt,a) ripper_warnI(parser, fmt, a)
558 # define rb_warnS(fmt,a) ripper_warnS(parser, fmt, a)
559 # define rb_warning0(fmt) ripper_warning0(parser, fmt)
560 # define rb_warningS(fmt,a) ripper_warningS(parser, fmt, a)
561 static void ripper_warn0(struct parser_params*, const char*);
562 static void ripper_warnI(struct parser_params*, const char*, int);
563 #if 0
564 static void ripper_warnS(struct parser_params*, const char*, const char*);
565 #endif
566 static void ripper_warning0(struct parser_params*, const char*);
567 static void ripper_warningS(struct parser_params*, const char*, const char*);
568 #endif
570 #ifdef RIPPER
571 static void ripper_compile_error(struct parser_params*, const char *fmt, ...);
572 # define rb_compile_error ripper_compile_error
573 # define compile_error ripper_compile_error
574 # define PARSER_ARG parser,
575 #else
576 # define compile_error parser->nerr++,rb_compile_error
577 # define PARSER_ARG ruby_sourcefile, ruby_sourceline,
578 #endif
580 /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
581 for instance). This is too low for Ruby to parse some files, such as
582 date/format.rb, therefore bump the value up to at least Bison's default. */
583 #ifdef OLD_YACC
584 #ifndef YYMAXDEPTH
585 #define YYMAXDEPTH 10000
586 #endif
587 #endif
591 %pure_parser
592 %parse-param {struct parser_params *parser}
594 %union {
595 VALUE val;
596 NODE *node;
597 ID id;
598 int num;
599 struct RVarmap *vars;
602 /*%%%*/
603 %token
605 %token <val>
607 keyword_class
608 keyword_module
609 keyword_def
610 keyword_undef
611 keyword_begin
612 keyword_rescue
613 keyword_ensure
614 keyword_end
615 keyword_if
616 keyword_unless
617 keyword_then
618 keyword_elsif
619 keyword_else
620 keyword_case
621 keyword_when
622 keyword_while
623 keyword_until
624 keyword_for
625 keyword_break
626 keyword_next
627 keyword_redo
628 keyword_retry
629 keyword_in
630 keyword_do
631 keyword_do_cond
632 keyword_do_block
633 keyword_do_LAMBDA
634 keyword_return
635 keyword_yield
636 keyword_super
637 keyword_self
638 keyword_nil
639 keyword_true
640 keyword_false
641 keyword_and
642 keyword_or
643 keyword_not
644 modifier_if
645 modifier_unless
646 modifier_while
647 modifier_until
648 modifier_rescue
649 keyword_alias
650 keyword_defined
651 keyword_BEGIN
652 keyword_END
653 keyword__LINE__
654 keyword__FILE__
655 keyword__ENCODING__
657 %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL
658 %token <node> tINTEGER tFLOAT tSTRING_CONTENT tCHAR
659 %token <node> tNTH_REF tBACK_REF
660 %token <num> tREGEXP_END
662 %type <node> singleton strings string string1 xstring regexp
663 %type <node> string_contents xstring_contents string_content
664 %type <node> words qwords word_list qword_list word
665 %type <node> literal numeric dsym cpath
666 %type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call
667 %type <node> expr_value arg_value primary_value
668 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
669 %type <node> args call_args opt_call_args
670 %type <node> paren_args opt_paren_args
671 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
672 %type <node> mrhs superclass block_call block_command
673 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
674 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
675 %type <node> block_param opt_block_param block_param_def f_opt
676 %type <node> bv_decls opt_bv_decl bvar
677 %type <node> lambda f_larglist lambda_body
678 %type <node> brace_block cmd_brace_block do_block lhs none fitem
679 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
680 %type <id> fsym variable sym symbol operation operation2 operation3
681 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
682 /*%%%*/
684 %type <val> program reswords then do dot_or_colon
686 %token tUPLUS /* unary+ */
687 %token tUMINUS /* unary- */
688 %token tPOW /* ** */
689 %token tCMP /* <=> */
690 %token tEQ /* == */
691 %token tEQQ /* === */
692 %token tNEQ /* != */
693 %token tGEQ /* >= */
694 %token tLEQ /* <= */
695 %token tANDOP tOROP /* && and || */
696 %token tMATCH tNMATCH /* =~ and !~ */
697 %token tDOT2 tDOT3 /* .. and ... */
698 %token tAREF tASET /* [] and []= */
699 %token tLSHFT tRSHFT /* << and >> */
700 %token tCOLON2 /* :: */
701 %token tCOLON3 /* :: at EXPR_BEG */
702 %token <id> tOP_ASGN /* +=, -= etc. */
703 %token tASSOC /* => */
704 %token tLPAREN /* ( */
705 %token tLPAREN_ARG /* ( */
706 %token tRPAREN /* ) */
707 %token tLBRACK /* [ */
708 %token tLBRACE /* { */
709 %token tLBRACE_ARG /* { */
710 %token tSTAR /* * */
711 %token tAMPER /* & */
712 %token tLAMBDA /* -> */
713 %token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
714 %token tSTRING_DBEG tSTRING_DVAR tSTRING_END tLAMBEG
717 * precedence table
720 %nonassoc tLOWEST
721 %nonassoc tLBRACE_ARG
723 %nonassoc modifier_if modifier_unless modifier_while modifier_until
724 %left keyword_or keyword_and
725 %right keyword_not
726 %nonassoc keyword_defined
727 %right '=' tOP_ASGN
728 %left modifier_rescue
729 %right '?' ':'
730 %nonassoc tDOT2 tDOT3
731 %left tOROP
732 %left tANDOP
733 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
734 %left '>' tGEQ '<' tLEQ
735 %left '|' '^'
736 %left '&'
737 %left tLSHFT tRSHFT
738 %left '+' '-'
739 %left '*' '/' '%'
740 %right tUMINUS_NUM tUMINUS
741 %right tPOW
742 %right '!' '~' tUPLUS
744 %token tLAST_TOKEN
747 program : {
748 lex_state = EXPR_BEG;
749 /*%%%*/
750 local_push(compile_for_eval);
754 compstmt
756 /*%%%*/
757 if ($2 && !compile_for_eval) {
758 /* last expression should not be void */
759 if (nd_type($2) != NODE_BLOCK) void_expr($2);
760 else {
761 NODE *node = $2;
762 while (node->nd_next) {
763 node = node->nd_next;
765 void_expr(node->nd_head);
768 ruby_eval_tree = NEW_SCOPE(0, block_append(ruby_eval_tree, $2));
769 local_pop();
771 $$ = $2;
772 parser->result = dispatch1(program, $$);
777 bodystmt : compstmt
778 opt_rescue
779 opt_else
780 opt_ensure
782 /*%%%*/
783 $$ = $1;
784 if ($2) {
785 $$ = NEW_RESCUE($1, $2, $3);
787 else if ($3) {
788 rb_warn0("else without rescue is useless");
789 $$ = block_append($$, $3);
791 if ($4) {
792 if ($$) {
793 $$ = NEW_ENSURE($$, $4);
795 else {
796 $$ = block_append($4, NEW_NIL());
799 fixpos($$, $1);
801 $$ = dispatch4(body_stmt,
802 escape_Qundef($1),
803 escape_Qundef($2),
804 escape_Qundef($3),
805 escape_Qundef($4));
810 compstmt : stmts opt_terms
812 /*%%%*/
813 void_stmts($1);
814 fixup_nodes(&deferred_nodes);
817 $$ = $1;
821 stmts : none
823 /*%%%*/
824 $$ = NEW_BEGIN(0);
826 $$ = dispatch2(stmts_add, dispatch0(stmts_new),
827 dispatch0(void_stmt));
830 | stmt
832 /*%%%*/
833 $$ = newline_node($1);
835 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
838 | stmts terms stmt
840 /*%%%*/
841 $$ = block_append($1, newline_node($3));
843 $$ = dispatch2(stmts_add, $1, $3);
846 | error stmt
848 $$ = remove_begin($2);
852 stmt : keyword_alias fitem {lex_state = EXPR_FNAME;} fitem
854 /*%%%*/
855 $$ = NEW_ALIAS($2, $4);
857 $$ = dispatch2(alias, $2, $4);
860 | keyword_alias tGVAR tGVAR
862 /*%%%*/
863 $$ = NEW_VALIAS($2, $3);
865 $$ = dispatch2(var_alias, $2, $3);
868 | keyword_alias tGVAR tBACK_REF
870 /*%%%*/
871 char buf[3];
873 sprintf(buf, "$%c", (char)$3->nd_nth);
874 $$ = NEW_VALIAS($2, rb_intern(buf));
876 $$ = dispatch2(var_alias, $2, $3);
879 | keyword_alias tGVAR tNTH_REF
881 /*%%%*/
882 yyerror("can't make alias for the number variables");
883 $$ = NEW_BEGIN(0);
885 $$ = dispatch2(var_alias, $2, $3);
886 $$ = dispatch1(alias_error, $$);
889 | keyword_undef undef_list
891 /*%%%*/
892 $$ = $2;
894 $$ = dispatch1(undef, $2);
897 | stmt modifier_if expr_value
899 /*%%%*/
900 $$ = NEW_IF(cond($3), remove_begin($1), 0);
901 fixpos($$, $3);
903 $$ = dispatch2(if_mod, $3, $1);
906 | stmt modifier_unless expr_value
908 /*%%%*/
909 $$ = NEW_UNLESS(cond($3), remove_begin($1), 0);
910 fixpos($$, $3);
912 $$ = dispatch2(unless_mod, $3, $1);
915 | stmt modifier_while expr_value
917 /*%%%*/
918 if ($1 && nd_type($1) == NODE_BEGIN) {
919 $$ = NEW_WHILE(cond($3), $1->nd_body, 0);
921 else {
922 $$ = NEW_WHILE(cond($3), $1, 1);
925 $$ = dispatch2(while_mod, $3, $1);
928 | stmt modifier_until expr_value
930 /*%%%*/
931 if ($1 && nd_type($1) == NODE_BEGIN) {
932 $$ = NEW_UNTIL(cond($3), $1->nd_body, 0);
934 else {
935 $$ = NEW_UNTIL(cond($3), $1, 1);
938 $$ = dispatch2(until_mod, $3, $1);
941 | stmt modifier_rescue stmt
943 /*%%%*/
944 NODE *resq = NEW_RESBODY(0, remove_begin($3), 0);
945 $$ = NEW_RESCUE(remove_begin($1), resq, 0);
947 $$ = dispatch2(rescue_mod, $3, $1);
950 | keyword_BEGIN
952 if (in_def || in_single) {
953 yyerror("BEGIN in method");
955 /*%%%*/
956 /* local_push(0); */
960 '{' compstmt '}'
962 /*%%%*/
963 ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
964 $4);
965 /* NEW_PREEXE($4)); */
966 /* local_pop(); */
967 $$ = 0;
969 $$ = dispatch1(BEGIN, $4);
972 | keyword_END '{' compstmt '}'
974 if (in_def || in_single) {
975 rb_warn0("END in method; use at_exit");
977 /*%%%*/
978 $$ = NEW_POSTEXE(NEW_NODE(
979 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */));
981 $$ = dispatch1(END, $3);
984 | lhs '=' command_call
986 /*%%%*/
987 value_expr($3);
988 $$ = node_assign($1, $3);
990 $$ = dispatch2(assign, $1, $3);
993 | mlhs '=' command_call
995 /*%%%*/
996 value_expr($3);
997 $1->nd_value = $3;
998 $$ = $1;
1000 $$ = dispatch2(massign, $1, $3);
1003 | var_lhs tOP_ASGN command_call
1005 /*%%%*/
1006 value_expr($3);
1007 if ($1) {
1008 ID vid = $1->nd_vid;
1009 if ($2 == tOROP) {
1010 $1->nd_value = $3;
1011 $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
1012 if (is_asgn_or_id(vid)) {
1013 $$->nd_aid = vid;
1016 else if ($2 == tANDOP) {
1017 $1->nd_value = $3;
1018 $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
1020 else {
1021 $$ = $1;
1022 $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
1025 else {
1026 $$ = NEW_BEGIN(0);
1029 $$ = dispatch3(opassign, $1, $2, $3);
1032 | primary_value '[' opt_call_args rbracket tOP_ASGN command_call
1034 /*%%%*/
1035 NODE *args;
1037 value_expr($6);
1038 if (!$3) $3 = NEW_ZARRAY();
1039 args = arg_concat($6, $3);
1040 if ($5 == tOROP) {
1041 $5 = 0;
1043 else if ($5 == tANDOP) {
1044 $5 = 1;
1046 $$ = NEW_OP_ASGN1($1, $5, args);
1047 fixpos($$, $1);
1049 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1050 $$ = dispatch3(opassign, $$, $5, $6);
1053 | primary_value '.' tIDENTIFIER tOP_ASGN command_call
1055 /*%%%*/
1056 value_expr($5);
1057 if ($4 == tOROP) {
1058 $4 = 0;
1060 else if ($4 == tANDOP) {
1061 $4 = 1;
1063 $$ = NEW_OP_ASGN2($1, $3, $4, $5);
1064 fixpos($$, $1);
1066 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
1067 $$ = dispatch3(opassign, $$, $4, $5);
1070 | primary_value '.' tCONSTANT tOP_ASGN command_call
1072 /*%%%*/
1073 value_expr($5);
1074 if ($4 == tOROP) {
1075 $4 = 0;
1077 else if ($4 == tANDOP) {
1078 $4 = 1;
1080 $$ = NEW_OP_ASGN2($1, $3, $4, $5);
1081 fixpos($$, $1);
1083 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
1084 $$ = dispatch3(opassign, $$, $4, $5);
1087 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
1089 /*%%%*/
1090 value_expr($5);
1091 if ($4 == tOROP) {
1092 $4 = 0;
1094 else if ($4 == tANDOP) {
1095 $4 = 1;
1097 $$ = NEW_OP_ASGN2($1, $3, $4, $5);
1098 fixpos($$, $1);
1100 $$ = dispatch3(field, $1, ripper_intern("::"), $3);
1101 $$ = dispatch3(opassign, $$, $4, $5);
1104 | backref tOP_ASGN command_call
1106 /*%%%*/
1107 rb_backref_error($1);
1108 $$ = NEW_BEGIN(0);
1110 $$ = dispatch2(assign, dispatch1(var_field, $1), $3);
1111 $$ = dispatch1(assign_error, $$);
1114 | lhs '=' mrhs
1116 /*%%%*/
1117 value_expr($3);
1118 $$ = node_assign($1, $3);
1120 $$ = dispatch2(assign, $1, $3);
1123 | mlhs '=' arg_value
1125 /*%%%*/
1126 $1->nd_value = $3;
1127 $$ = $1;
1129 dispatch2(massign, $1, $3);
1132 | mlhs '=' mrhs
1134 /*%%%*/
1135 $1->nd_value = $3;
1136 $$ = $1;
1138 $$ = dispatch2(massign, $1, $3);
1141 | expr
1144 expr : command_call
1145 | expr keyword_and expr
1147 /*%%%*/
1148 $$ = logop(NODE_AND, $1, $3);
1150 $$ = dispatch3(binary, $1, ripper_intern("and"), $3);
1153 | expr keyword_or expr
1155 /*%%%*/
1156 $$ = logop(NODE_OR, $1, $3);
1158 $$ = dispatch3(binary, $1, ripper_intern("or"), $3);
1161 | keyword_not expr
1163 /*%%%*/
1164 $$ = call_uni_op(cond($2), '!');
1166 $$ = dispatch2(unary, ripper_intern("not"), $2);
1169 | '!' command_call
1171 /*%%%*/
1172 $$ = call_uni_op(cond($2), '!');
1174 $$ = dispatch2(unary, ripper_id2sym('!'), $2);
1177 | arg
1180 expr_value : expr
1182 /*%%%*/
1183 value_expr($1);
1184 $$ = $1;
1185 if (!$$) $$ = NEW_NIL();
1187 $$ = $1;
1192 command_call : command
1193 | block_command
1194 | keyword_return call_args
1196 /*%%%*/
1197 $$ = NEW_RETURN(ret_args($2));
1199 $$ = dispatch1(return, $2);
1202 | keyword_break call_args
1204 /*%%%*/
1205 $$ = NEW_BREAK(ret_args($2));
1207 $$ = dispatch1(break, $2);
1210 | keyword_next call_args
1212 /*%%%*/
1213 $$ = NEW_NEXT(ret_args($2));
1215 $$ = dispatch1(next, $2);
1220 block_command : block_call
1221 | block_call '.' operation2 command_args
1223 /*%%%*/
1224 $$ = NEW_CALL($1, $3, $4);
1226 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
1227 $$ = method_arg($$, $4);
1230 | block_call tCOLON2 operation2 command_args
1232 /*%%%*/
1233 $$ = NEW_CALL($1, $3, $4);
1235 $$ = dispatch3(call, $1, ripper_intern("::"), $3);
1236 $$ = method_arg($$, $4);
1241 cmd_brace_block : tLBRACE_ARG
1243 /*%%%*/
1244 dyna_push();
1245 $<num>$ = ruby_sourceline;
1249 opt_block_param
1250 compstmt
1253 /*%%%*/
1254 $$ = NEW_ITER($3,$4);
1255 nd_set_line($$, $<num>2);
1256 dyna_pop();
1258 $$ = dispatch2(brace_block, escape_Qundef($3), $4);
1263 command : operation command_args %prec tLOWEST
1265 /*%%%*/
1266 $$ = NEW_FCALL($1, $2);
1267 fixpos($$, $2);
1269 $$ = dispatch2(command, $1, $2);
1272 | operation command_args cmd_brace_block
1274 /*%%%*/
1275 block_dup_check($2,$3);
1276 $3->nd_iter = NEW_FCALL($1, $2);
1277 $$ = $3;
1278 fixpos($$, $2);
1280 $$ = dispatch2(command, $1, $2);
1281 $$ = method_add_block($$, $3);
1284 | primary_value '.' operation2 command_args %prec tLOWEST
1286 /*%%%*/
1287 $$ = NEW_CALL($1, $3, $4);
1288 fixpos($$, $1);
1290 $$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4);
1293 | primary_value '.' operation2 command_args cmd_brace_block
1295 /*%%%*/
1296 block_dup_check($4,$5);
1297 $5->nd_iter = NEW_CALL($1, $3, $4);
1298 $$ = $5;
1299 fixpos($$, $1);
1301 $$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4);
1302 $$ = method_add_block($$, $5);
1305 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1307 /*%%%*/
1308 $$ = NEW_CALL($1, $3, $4);
1309 fixpos($$, $1);
1311 $$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4);
1314 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1316 /*%%%*/
1317 block_dup_check($4,$5);
1318 $5->nd_iter = NEW_CALL($1, $3, $4);
1319 $$ = $5;
1320 fixpos($$, $1);
1322 $$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4);
1323 $$ = method_add_block($$, $5);
1326 | keyword_super command_args
1328 /*%%%*/
1329 $$ = NEW_SUPER($2);
1330 fixpos($$, $2);
1332 $$ = dispatch1(super, $2);
1335 | keyword_yield command_args
1337 /*%%%*/
1338 $$ = new_yield($2);
1339 fixpos($$, $2);
1341 $$ = dispatch1(yield, $2);
1346 mlhs : mlhs_basic
1347 | tLPAREN mlhs_inner rparen
1349 /*%%%*/
1350 $$ = $2;
1352 $$ = dispatch1(mlhs_paren, $2);
1357 mlhs_inner : mlhs_basic
1358 | tLPAREN mlhs_inner rparen
1360 /*%%%*/
1361 $$ = NEW_MASGN(NEW_LIST($2), 0);
1363 $$ = dispatch1(mlhs_paren, $2);
1368 mlhs_basic : mlhs_head
1370 /*%%%*/
1371 $$ = NEW_MASGN($1, 0);
1373 $$ = $1;
1376 | mlhs_head mlhs_item
1378 /*%%%*/
1379 $$ = NEW_MASGN(list_append($1,$2), 0);
1381 $$ = mlhs_add($1, $2);
1384 | mlhs_head tSTAR mlhs_node
1386 /*%%%*/
1387 $$ = NEW_MASGN($1, $3);
1389 $$ = mlhs_add_star($1, $3);
1392 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1394 /*%%%*/
1395 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5));
1397 $$ = mlhs_add_star($1, $3);
1400 | mlhs_head tSTAR
1402 /*%%%*/
1403 $$ = NEW_MASGN($1, -1);
1405 $$ = mlhs_add_star($1, Qnil);
1408 | mlhs_head tSTAR ',' mlhs_post
1410 /*%%%*/
1411 $$ = NEW_MASGN($1, NEW_POSTARG(-1, $4));
1413 $$ = mlhs_add_star($1, Qnil);
1416 | tSTAR mlhs_node
1418 /*%%%*/
1419 $$ = NEW_MASGN(0, $2);
1421 $$ = mlhs_add_star(mlhs_new(), $2);
1424 | tSTAR mlhs_node ',' mlhs_post
1426 /*%%%*/
1427 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4));
1429 $$ = mlhs_add_star(mlhs_new(), $2);
1432 | tSTAR
1434 /*%%%*/
1435 $$ = NEW_MASGN(0, -1);
1437 $$ = mlhs_add_star(mlhs_new(), Qnil);
1440 | tSTAR ',' mlhs_post
1442 /*%%%*/
1443 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
1445 $$ = mlhs_add_star(mlhs_new(), Qnil);
1450 mlhs_item : mlhs_node
1451 | tLPAREN mlhs_inner rparen
1453 /*%%%*/
1454 $$ = $2;
1456 $$ = dispatch1(mlhs_paren, $2);
1461 mlhs_head : mlhs_item ','
1463 /*%%%*/
1464 $$ = NEW_LIST($1);
1466 $$ = mlhs_add(mlhs_new(), $1);
1469 | mlhs_head mlhs_item ','
1471 /*%%%*/
1472 $$ = list_append($1, $2);
1474 $$ = mlhs_add($1, $2);
1479 mlhs_post : mlhs_item
1481 /*%%%*/
1482 $$ = NEW_LIST($1);
1484 $$ = mlhs_add(mlhs_new(), $1);
1487 | mlhs_post ',' mlhs_item
1489 /*%%%*/
1490 $$ = list_append($1, $3);
1492 $$ = mlhs_add($1, $3);
1497 mlhs_node : variable
1499 /*%%%*/
1500 $$ = assignable($1, 0);
1502 $$ = $1;
1505 | primary_value '[' opt_call_args rbracket
1507 /*%%%*/
1508 $$ = aryset($1, $3);
1510 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1513 | primary_value '.' tIDENTIFIER
1515 /*%%%*/
1516 $$ = attrset($1, $3);
1518 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
1521 | primary_value tCOLON2 tIDENTIFIER
1523 /*%%%*/
1524 $$ = attrset($1, $3);
1526 $$ = dispatch2(const_path_field, $1, $3);
1529 | primary_value '.' tCONSTANT
1531 /*%%%*/
1532 $$ = attrset($1, $3);
1534 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
1537 | primary_value tCOLON2 tCONSTANT
1539 /*%%%*/
1540 if (in_def || in_single)
1541 yyerror("dynamic constant assignment");
1542 $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
1544 if (in_def || in_single)
1545 yyerror("dynamic constant assignment");
1546 $$ = dispatch2(const_path_field, $1, $3);
1549 | tCOLON3 tCONSTANT
1551 /*%%%*/
1552 if (in_def || in_single)
1553 yyerror("dynamic constant assignment");
1554 $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
1556 $$ = dispatch1(top_const_field, $2);
1559 | backref
1561 /*%%%*/
1562 rb_backref_error($1);
1563 $$ = NEW_BEGIN(0);
1565 $$ = dispatch1(var_field, $1);
1566 $$ = dispatch1(assign_error, $$);
1571 lhs : variable
1573 /*%%%*/
1574 if (!($$ = assignable($1, 0))) $$ = NEW_BEGIN(0);
1576 $$ = dispatch1(var_field, $1);
1579 | primary_value '[' opt_call_args rbracket
1581 /*%%%*/
1582 $$ = aryset($1, $3);
1584 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1587 | primary_value '.' tIDENTIFIER
1589 /*%%%*/
1590 $$ = attrset($1, $3);
1592 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
1595 | primary_value tCOLON2 tIDENTIFIER
1597 /*%%%*/
1598 $$ = attrset($1, $3);
1600 $$ = dispatch3(field, $1, ripper_intern("::"), $3);
1603 | primary_value '.' tCONSTANT
1605 /*%%%*/
1606 $$ = attrset($1, $3);
1608 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
1611 | primary_value tCOLON2 tCONSTANT
1613 /*%%%*/
1614 if (in_def || in_single)
1615 yyerror("dynamic constant assignment");
1616 $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
1618 $$ = dispatch2(const_path_field, $1, $3);
1619 if (in_def || in_single) {
1620 $$ = dispatch1(assign_error, $$);
1624 | tCOLON3 tCONSTANT
1626 /*%%%*/
1627 if (in_def || in_single)
1628 yyerror("dynamic constant assignment");
1629 $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
1631 $$ = dispatch1(top_const_field, $2);
1632 if (in_def || in_single) {
1633 $$ = dispatch1(assign_error, $$);
1637 | backref
1639 /*%%%*/
1640 rb_backref_error($1);
1641 $$ = NEW_BEGIN(0);
1643 $$ = dispatch1(assign_error, $1);
1648 cname : tIDENTIFIER
1650 /*%%%*/
1651 yyerror("class/module name must be CONSTANT");
1653 $$ = dispatch1(class_name_error, $1);
1656 | tCONSTANT
1659 cpath : tCOLON3 cname
1661 /*%%%*/
1662 $$ = NEW_COLON3($2);
1664 $$ = dispatch1(top_const_ref, $2);
1667 | cname
1669 /*%%%*/
1670 $$ = NEW_COLON2(0, $$);
1672 $$ = dispatch1(const_ref, $1);
1675 | primary_value tCOLON2 cname
1677 /*%%%*/
1678 $$ = NEW_COLON2($1, $3);
1680 $$ = dispatch2(const_path_ref, $1, $3);
1685 fname : tIDENTIFIER
1686 | tCONSTANT
1687 | tFID
1688 | op
1690 /*%%%*/
1691 lex_state = EXPR_END;
1692 $$ = $1;
1694 lex_state = EXPR_END;
1695 $$ = $1;
1698 | reswords
1700 /*%%%*/
1701 lex_state = EXPR_END;
1702 $$ = $<id>1;
1704 lex_state = EXPR_END;
1705 $$ = $1;
1710 fsym : fname
1711 | symbol
1714 fitem : fsym
1716 /*%%%*/
1717 $$ = NEW_LIT(ID2SYM($1));
1719 $$ = dispatch1(symbol_literal, $1);
1722 | dsym
1725 undef_list : fitem
1727 /*%%%*/
1728 $$ = NEW_UNDEF($1);
1730 $$ = rb_ary_new3(1, $1);
1733 | undef_list ',' {lex_state = EXPR_FNAME;} fitem
1735 /*%%%*/
1736 $$ = block_append($1, NEW_UNDEF($4));
1738 rb_ary_push($1, $4);
1743 op : '|' { ifndef_ripper($$ = '|'); }
1744 | '^' { ifndef_ripper($$ = '^'); }
1745 | '&' { ifndef_ripper($$ = '&'); }
1746 | tCMP { ifndef_ripper($$ = tCMP); }
1747 | tEQ { ifndef_ripper($$ = tEQ); }
1748 | tEQQ { ifndef_ripper($$ = tEQQ); }
1749 | tMATCH { ifndef_ripper($$ = tMATCH); }
1750 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
1751 | '>' { ifndef_ripper($$ = '>'); }
1752 | tGEQ { ifndef_ripper($$ = tGEQ); }
1753 | '<' { ifndef_ripper($$ = '<'); }
1754 | tLEQ { ifndef_ripper($$ = tLEQ); }
1755 | tNEQ { ifndef_ripper($$ = tNEQ); }
1756 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
1757 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
1758 | '+' { ifndef_ripper($$ = '+'); }
1759 | '-' { ifndef_ripper($$ = '-'); }
1760 | '*' { ifndef_ripper($$ = '*'); }
1761 | tSTAR { ifndef_ripper($$ = '*'); }
1762 | '/' { ifndef_ripper($$ = '/'); }
1763 | '%' { ifndef_ripper($$ = '%'); }
1764 | tPOW { ifndef_ripper($$ = tPOW); }
1765 | '!' { ifndef_ripper($$ = '!'); }
1766 | '~' { ifndef_ripper($$ = '~'); }
1767 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
1768 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
1769 | tAREF { ifndef_ripper($$ = tAREF); }
1770 | tASET { ifndef_ripper($$ = tASET); }
1771 | '`' { ifndef_ripper($$ = '`'); }
1774 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
1775 | keyword_BEGIN | keyword_END
1776 | keyword_alias | keyword_and | keyword_begin
1777 | keyword_break | keyword_case | keyword_class | keyword_def
1778 | keyword_defined | keyword_do | keyword_else | keyword_elsif
1779 | keyword_end | keyword_ensure | keyword_false
1780 | keyword_for | keyword_in | keyword_module | keyword_next
1781 | keyword_nil | keyword_not | keyword_or | keyword_redo
1782 | keyword_rescue | keyword_retry | keyword_return | keyword_self
1783 | keyword_super | keyword_then | keyword_true | keyword_undef
1784 | keyword_when | keyword_yield | keyword_if | keyword_unless
1785 | keyword_while | keyword_until
1788 arg : lhs '=' arg
1790 /*%%%*/
1791 value_expr($3);
1792 $$ = node_assign($1, $3);
1794 $$ = dispatch2(assign, $1, $3);
1797 | lhs '=' arg modifier_rescue arg
1799 /*%%%*/
1800 value_expr($3);
1801 $3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0);
1802 $$ = node_assign($1, $3);
1804 $$ = dispatch2(assign, $1, dispatch2(rescue_mod, $3, $5));
1807 | var_lhs tOP_ASGN arg
1809 /*%%%*/
1810 value_expr($3);
1811 if ($1) {
1812 ID vid = $1->nd_vid;
1813 if ($2 == tOROP) {
1814 $1->nd_value = $3;
1815 $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
1816 if (is_asgn_or_id(vid)) {
1817 $$->nd_aid = vid;
1820 else if ($2 == tANDOP) {
1821 $1->nd_value = $3;
1822 $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
1824 else {
1825 $$ = $1;
1826 $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
1829 else {
1830 $$ = NEW_BEGIN(0);
1833 $$ = dispatch3(opassign, $1, $2, $3);
1836 | var_lhs tOP_ASGN arg modifier_rescue arg
1838 /*%%%*/
1839 value_expr($3);
1840 $3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0);
1841 if ($1) {
1842 ID vid = $1->nd_vid;
1843 if ($2 == tOROP) {
1844 $1->nd_value = $3;
1845 $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
1846 if (is_asgn_or_id(vid)) {
1847 $$->nd_aid = vid;
1850 else if ($2 == tANDOP) {
1851 $1->nd_value = $3;
1852 $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
1854 else {
1855 $$ = $1;
1856 $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
1859 else {
1860 $$ = NEW_BEGIN(0);
1863 $3 = dispatch2(rescue_mod, $3, $5);
1864 $$ = dispatch3(opassign, $1, $2, $3);
1867 | primary_value '[' opt_call_args rbracket tOP_ASGN arg
1869 /*%%%*/
1870 NODE *args;
1872 value_expr($6);
1873 if (!$3) $3 = NEW_ZARRAY();
1874 args = arg_concat($6, $3);
1875 if ($5 == tOROP) {
1876 $5 = 0;
1878 else if ($5 == tANDOP) {
1879 $5 = 1;
1881 $$ = NEW_OP_ASGN1($1, $5, args);
1882 fixpos($$, $1);
1884 $1 = dispatch2(aref_field, $1, escape_Qundef($3));
1885 $$ = dispatch3(opassign, $1, $5, $6);
1888 | primary_value '.' tIDENTIFIER tOP_ASGN arg
1890 /*%%%*/
1891 value_expr($5);
1892 if ($4 == tOROP) {
1893 $4 = 0;
1895 else if ($4 == tANDOP) {
1896 $4 = 1;
1898 $$ = NEW_OP_ASGN2($1, $3, $4, $5);
1899 fixpos($$, $1);
1901 $1 = dispatch3(field, $1, ripper_id2sym('.'), $3);
1902 $$ = dispatch3(opassign, $1, $4, $5);
1905 | primary_value '.' tCONSTANT tOP_ASGN arg
1907 /*%%%*/
1908 value_expr($5);
1909 if ($4 == tOROP) {
1910 $4 = 0;
1912 else if ($4 == tANDOP) {
1913 $4 = 1;
1915 $$ = NEW_OP_ASGN2($1, $3, $4, $5);
1916 fixpos($$, $1);
1918 $1 = dispatch3(field, $1, ripper_id2sym('.'), $3);
1919 $$ = dispatch3(opassign, $1, $4, $5);
1922 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
1924 /*%%%*/
1925 value_expr($5);
1926 if ($4 == tOROP) {
1927 $4 = 0;
1929 else if ($4 == tANDOP) {
1930 $4 = 1;
1932 $$ = NEW_OP_ASGN2($1, $3, $4, $5);
1933 fixpos($$, $1);
1935 $1 = dispatch3(field, $1, ripper_intern("::"), $3);
1936 $$ = dispatch3(opassign, $1, $4, $5);
1939 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg
1941 /*%%%*/
1942 yyerror("constant re-assignment");
1943 $$ = NEW_BEGIN(0);
1945 $$ = dispatch2(const_path_field, $1, $3);
1946 $$ = dispatch3(opassign, $$, $4, $5);
1947 $$ = dispatch1(assign_error, $$);
1950 | tCOLON3 tCONSTANT tOP_ASGN arg
1952 /*%%%*/
1953 yyerror("constant re-assignment");
1954 $$ = NEW_BEGIN(0);
1956 $$ = dispatch1(top_const_field, $2);
1957 $$ = dispatch3(opassign, $$, $3, $4);
1958 $$ = dispatch1(assign_error, $$);
1961 | backref tOP_ASGN arg
1963 /*%%%*/
1964 rb_backref_error($1);
1965 $$ = NEW_BEGIN(0);
1967 $$ = dispatch1(var_field, $1);
1968 $$ = dispatch3(opassign, $$, $2, $3);
1969 $$ = dispatch1(assign_error, $$);
1972 | arg tDOT2 arg
1974 /*%%%*/
1975 value_expr($1);
1976 value_expr($3);
1977 $$ = NEW_DOT2($1, $3);
1978 if (nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) &&
1979 nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) {
1980 deferred_nodes = list_append(deferred_nodes, $$);
1983 $$ = dispatch2(dot2, $1, $3);
1986 | arg tDOT3 arg
1988 /*%%%*/
1989 value_expr($1);
1990 value_expr($3);
1991 $$ = NEW_DOT3($1, $3);
1992 if (nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) &&
1993 nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) {
1994 deferred_nodes = list_append(deferred_nodes, $$);
1997 $$ = dispatch2(dot3, $1, $3);
2000 | arg '+' arg
2002 /*%%%*/
2003 $$ = call_bin_op($1, '+', $3);
2005 $$ = dispatch3(binary, $1, ID2SYM('+'), $3);
2008 | arg '-' arg
2010 /*%%%*/
2011 $$ = call_bin_op($1, '-', $3);
2013 $$ = dispatch3(binary, $1, ID2SYM('-'), $3);
2016 | arg '*' arg
2018 /*%%%*/
2019 $$ = call_bin_op($1, '*', $3);
2021 $$ = dispatch3(binary, $1, ID2SYM('*'), $3);
2024 | arg '/' arg
2026 /*%%%*/
2027 $$ = call_bin_op($1, '/', $3);
2029 $$ = dispatch3(binary, $1, ID2SYM('/'), $3);
2032 | arg '%' arg
2034 /*%%%*/
2035 $$ = call_bin_op($1, '%', $3);
2037 $$ = dispatch3(binary, $1, ID2SYM('%'), $3);
2040 | arg tPOW arg
2042 /*%%%*/
2043 $$ = call_bin_op($1, tPOW, $3);
2045 $$ = dispatch3(binary, $1, ripper_intern("**"), $3);
2048 | tUMINUS_NUM tINTEGER tPOW arg
2050 /*%%%*/
2051 $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
2053 $$ = dispatch3(binary, $2, ripper_intern("**"), $4);
2054 $$ = dispatch2(unary, ripper_intern("-@"), $$);
2057 | tUMINUS_NUM tFLOAT tPOW arg
2059 /*%%%*/
2060 $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
2062 $$ = dispatch3(binary, $2, ripper_intern("**"), $4);
2063 $$ = dispatch2(unary, ripper_intern("-@"), $$);
2066 | tUPLUS arg
2068 /*%%%*/
2069 $$ = call_uni_op($2, tUPLUS);
2071 $$ = dispatch2(unary, ripper_intern("+@"), $2);
2074 | tUMINUS arg
2076 /*%%%*/
2077 $$ = call_uni_op($2, tUMINUS);
2079 $$ = dispatch2(unary, ripper_intern("-@"), $2);
2082 | arg '|' arg
2084 /*%%%*/
2085 $$ = call_bin_op($1, '|', $3);
2087 $$ = dispatch3(binary, $1, ID2SYM('|'), $3);
2090 | arg '^' arg
2092 /*%%%*/
2093 $$ = call_bin_op($1, '^', $3);
2095 $$ = dispatch3(binary, $1, ID2SYM('^'), $3);
2098 | arg '&' arg
2100 /*%%%*/
2101 $$ = call_bin_op($1, '&', $3);
2103 $$ = dispatch3(binary, $1, ID2SYM('&'), $3);
2106 | arg tCMP arg
2108 /*%%%*/
2109 $$ = call_bin_op($1, tCMP, $3);
2111 $$ = dispatch3(binary, $1, ripper_intern("<=>"), $3);
2114 | arg '>' arg
2116 /*%%%*/
2117 $$ = call_bin_op($1, '>', $3);
2119 $$ = dispatch3(binary, $1, ID2SYM('>'), $3);
2122 | arg tGEQ arg
2124 /*%%%*/
2125 $$ = call_bin_op($1, tGEQ, $3);
2127 $$ = dispatch3(binary, $1, ripper_intern(">="), $3);
2130 | arg '<' arg
2132 /*%%%*/
2133 $$ = call_bin_op($1, '<', $3);
2135 $$ = dispatch3(binary, $1, ID2SYM('<'), $3);
2138 | arg tLEQ arg
2140 /*%%%*/
2141 $$ = call_bin_op($1, tLEQ, $3);
2143 $$ = dispatch3(binary, $1, ripper_intern("<="), $3);
2146 | arg tEQ arg
2148 /*%%%*/
2149 $$ = call_bin_op($1, tEQ, $3);
2151 $$ = dispatch3(binary, $1, ripper_intern("=="), $3);
2154 | arg tEQQ arg
2156 /*%%%*/
2157 $$ = call_bin_op($1, tEQQ, $3);
2159 $$ = dispatch3(binary, $1, ripper_intern("==="), $3);
2162 | arg tNEQ arg
2164 /*%%%*/
2165 $$ = call_bin_op($1, tNEQ, $3);
2167 $$ = dispatch3(binary, $1, ripper_intern("!="), $3);
2170 | arg tMATCH arg
2172 /*%%%*/
2173 $$ = match_op($1, $3);
2174 if (nd_type($1) == NODE_LIT && TYPE($1->nd_lit) == T_REGEXP) {
2175 $$ = reg_named_capture_assign($1->nd_lit, $$);
2178 $$ = dispatch3(binary, $1, ripper_intern("=~"), $3);
2181 | arg tNMATCH arg
2183 /*%%%*/
2184 $$ = call_bin_op($1, tNMATCH, $3);
2186 $$ = dispatch3(binary, $1, ripper_intern("!~"), $3);
2189 | '!' arg
2191 /*%%%*/
2192 $$ = call_uni_op(cond($2), '!');
2194 $$ = dispatch2(unary, ID2SYM('!'), $2);
2197 | '~' arg
2199 /*%%%*/
2200 $$ = call_uni_op($2, '~');
2202 $$ = dispatch2(unary, ID2SYM('~'), $2);
2205 | arg tLSHFT arg
2207 /*%%%*/
2208 $$ = call_bin_op($1, tLSHFT, $3);
2210 $$ = dispatch3(binary, $1, ripper_intern("<<"), $3);
2213 | arg tRSHFT arg
2215 /*%%%*/
2216 $$ = call_bin_op($1, tRSHFT, $3);
2218 $$ = dispatch3(binary, $1, ripper_intern(">>"), $3);
2221 | arg tANDOP arg
2223 /*%%%*/
2224 $$ = logop(NODE_AND, $1, $3);
2226 $$ = dispatch3(binary, $1, ripper_intern("&&"), $3);
2229 | arg tOROP arg
2231 /*%%%*/
2232 $$ = logop(NODE_OR, $1, $3);
2234 $$ = dispatch3(binary, $1, ripper_intern("||"), $3);
2237 | keyword_defined opt_nl {in_defined = 1;} arg
2239 /*%%%*/
2240 in_defined = 0;
2241 $$ = NEW_DEFINED($4);
2243 in_defined = 0;
2244 $$ = dispatch1(defined, $4);
2247 | arg '?' arg opt_nl ':' arg
2249 /*%%%*/
2250 value_expr($1);
2251 $$ = NEW_IF(cond($1), $3, $6);
2252 fixpos($$, $1);
2254 $$ = dispatch3(ifop, $1, $3, $6);
2257 | primary
2259 $$ = $1;
2263 arg_value : arg
2265 /*%%%*/
2266 value_expr($1);
2267 $$ = $1;
2268 if (!$$) $$ = NEW_NIL();
2270 $$ = $1;
2275 aref_args : none
2276 | args trailer
2278 $$ = $1;
2280 | args ',' assocs trailer
2282 /*%%%*/
2283 $$ = arg_append($1, NEW_HASH($3));
2285 $$ = arg_add_assocs($1, $3);
2288 | assocs trailer
2290 /*%%%*/
2291 $$ = NEW_LIST(NEW_HASH($1));
2293 $$ = arg_add_assocs(arg_new(), $1);
2298 paren_args : '(' opt_call_args rparen
2300 /*%%%*/
2301 $$ = $2;
2303 $$ = dispatch1(arg_paren, escape_Qundef($2));
2308 opt_paren_args : none
2309 | paren_args
2312 opt_call_args : none
2313 | call_args
2316 call_args : command
2318 /*%%%*/
2319 $$ = NEW_LIST($1);
2321 $$ = arg_add(arg_new(), $1);
2324 | args opt_block_arg
2326 /*%%%*/
2327 $$ = arg_blk_pass($1, $2);
2329 $$ = arg_add_optblock($1, $2);
2332 | assocs opt_block_arg
2334 /*%%%*/
2335 $$ = NEW_LIST(NEW_HASH($1));
2336 $$ = arg_blk_pass($$, $2);
2338 $$ = arg_add_assocs(arg_new(), $1);
2339 $$ = arg_add_optblock($$, $2);
2342 | args ',' assocs opt_block_arg
2344 /*%%%*/
2345 $$ = arg_append($1, NEW_HASH($3));
2346 $$ = arg_blk_pass($$, $4);
2348 $$ = arg_add_optblock(arg_add_assocs($1, $3), $4);
2351 | block_arg
2352 /*%c%*/
2353 /*%c
2355 $$ = arg_add_block(arg_new(), $1);
2360 command_args : {
2361 $<num>$ = cmdarg_stack;
2362 CMDARG_PUSH(1);
2364 call_args
2366 /* CMDARG_POP() */
2367 cmdarg_stack = $<num>1;
2368 $$ = $2;
2372 block_arg : tAMPER arg_value
2374 /*%%%*/
2375 $$ = NEW_BLOCK_PASS($2);
2377 $$ = $2;
2382 opt_block_arg : ',' block_arg
2384 $$ = $2;
2386 | none
2388 $$ = 0;
2392 args : arg_value
2394 /*%%%*/
2395 $$ = NEW_LIST($1);
2397 $$ = arg_add(arg_new(), $1);
2400 | tSTAR arg_value
2402 /*%%%*/
2403 $$ = NEW_SPLAT($2);
2405 $$ = arg_add_star(arg_new(), $2);
2408 | args ',' arg_value
2410 /*%%%*/
2411 NODE *n1;
2412 if ((n1 = splat_array($1)) != 0) {
2413 $$ = list_append(n1, $3);
2415 else {
2416 $$ = arg_append($1, $3);
2419 $$ = arg_add($1, $3);
2422 | args ',' tSTAR arg_value
2424 /*%%%*/
2425 NODE *n1;
2426 if (nd_type($4) == NODE_ARRAY &&
2427 (n1 = splat_array($1)) != 0) {
2428 $$ = list_concat(n1, $4);
2430 else {
2431 $$ = arg_concat($1, $4);
2434 $$ = arg_add_star($1, $4);
2439 mrhs : args ',' arg_value
2441 /*%%%*/
2442 NODE *n1;
2443 if ((n1 = splat_array($1)) != 0) {
2444 $$ = list_append(n1, $3);
2446 else {
2447 $$ = arg_append($1, $3);
2450 $$ = mrhs_add(args2mrhs($1), $3);
2453 | args ',' tSTAR arg_value
2455 /*%%%*/
2456 NODE *n1;
2457 if (nd_type($4) == NODE_ARRAY &&
2458 (n1 = splat_array($1)) != 0) {
2459 $$ = list_concat(n1, $4);
2461 else {
2462 $$ = arg_concat($1, $4);
2465 $$ = mrhs_add_star(args2mrhs($1), $4);
2468 | tSTAR arg_value
2470 /*%%%*/
2471 $$ = NEW_SPLAT($2);
2473 $$ = mrhs_add_star(mrhs_new(), $2);
2478 primary : literal
2479 | strings
2480 | xstring
2481 | regexp
2482 | words
2483 | qwords
2484 | var_ref
2485 | backref
2486 | tFID
2488 /*%%%*/
2489 $$ = NEW_FCALL($1, 0);
2491 $$ = method_arg(dispatch1(fcall, $1), arg_new());
2494 | keyword_begin
2496 /*%%%*/
2497 $<num>$ = ruby_sourceline;
2501 bodystmt
2502 keyword_end
2504 /*%%%*/
2505 if ($3 == NULL) {
2506 $$ = NEW_NIL();
2508 else {
2509 if (nd_type($3) == NODE_RESCUE ||
2510 nd_type($3) == NODE_ENSURE)
2511 nd_set_line($3, $<num>2);
2512 $$ = NEW_BEGIN($3);
2514 nd_set_line($$, $<num>2);
2516 $$ = dispatch1(begin, $3);
2519 | tLPAREN_ARG expr {lex_state = EXPR_ENDARG;} rparen
2521 rb_warning0("(...) interpreted as grouped expression");
2522 /*%%%*/
2523 $$ = $2;
2525 $$ = dispatch1(paren, $2);
2528 | tLPAREN compstmt ')'
2530 /*%%%*/
2531 $$ = $2;
2533 $$ = dispatch1(paren, $2);
2536 | primary_value tCOLON2 tCONSTANT
2538 /*%%%*/
2539 $$ = NEW_COLON2($1, $3);
2541 $$ = dispatch2(const_path_ref, $1, $3);
2544 | tCOLON3 tCONSTANT
2546 /*%%%*/
2547 $$ = NEW_COLON3($2);
2549 $$ = dispatch1(top_const_ref, $2);
2552 | tLBRACK aref_args ']'
2554 /*%%%*/
2555 if ($2 == 0) {
2556 $$ = NEW_ZARRAY(); /* zero length array*/
2558 else {
2559 $$ = $2;
2562 $$ = dispatch1(array, escape_Qundef($2));
2565 | tLBRACE assoc_list '}'
2567 /*%%%*/
2568 $$ = NEW_HASH($2);
2570 $$ = dispatch1(hash, escape_Qundef($2));
2573 | keyword_return
2575 /*%%%*/
2576 $$ = NEW_RETURN(0);
2578 $$ = dispatch0(return0);
2581 | keyword_yield '(' call_args rparen
2583 /*%%%*/
2584 $$ = new_yield($3);
2586 $$ = dispatch1(yield, dispatch1(paren, $3));
2589 | keyword_yield '(' rparen
2591 /*%%%*/
2592 $$ = NEW_YIELD(0, Qfalse);
2594 $$ = dispatch1(yield, dispatch1(paren, arg_new()));
2597 | keyword_yield
2599 /*%%%*/
2600 $$ = NEW_YIELD(0, Qfalse);
2602 $$ = dispatch0(yield0);
2605 | keyword_defined opt_nl '(' {in_defined = 1;} expr rparen
2607 /*%%%*/
2608 in_defined = 0;
2609 $$ = NEW_DEFINED($5);
2611 in_defined = 0;
2612 $$ = dispatch1(defined, $5);
2615 | keyword_not '(' expr rparen
2617 /*%%%*/
2618 $$ = call_uni_op(cond($3), '!');
2620 $$ = dispatch2(unary, ripper_intern("not"), $3);
2623 | keyword_not '(' rparen
2625 /*%%%*/
2626 $$ = call_uni_op(cond(NEW_NIL()), '!');
2628 $$ = dispatch2(unary, ripper_intern("not"), Qnil);
2631 | operation brace_block
2633 /*%%%*/
2634 $2->nd_iter = NEW_FCALL($1, 0);
2635 $$ = $2;
2636 fixpos($2->nd_iter, $2);
2638 $$ = method_arg(dispatch1(fcall, $1), arg_new());
2639 $$ = method_add_block($$, $2);
2642 | method_call
2643 | method_call brace_block
2645 /*%%%*/
2646 block_dup_check($1->nd_args, $2);
2647 $2->nd_iter = $1;
2648 $$ = $2;
2649 fixpos($$, $1);
2651 $$ = method_add_block($1, $2);
2654 | tLAMBDA lambda
2656 $$ = $2;
2658 | keyword_if expr_value then
2659 compstmt
2660 if_tail
2661 keyword_end
2663 /*%%%*/
2664 $$ = NEW_IF(cond($2), $4, $5);
2665 fixpos($$, $2);
2667 $$ = dispatch3(if, $2, $4, escape_Qundef($5));
2670 | keyword_unless expr_value then
2671 compstmt
2672 opt_else
2673 keyword_end
2675 /*%%%*/
2676 $$ = NEW_UNLESS(cond($2), $4, $5);
2677 fixpos($$, $2);
2679 $$ = dispatch3(unless, $2, $4, escape_Qundef($5));
2682 | keyword_while {COND_PUSH(1);} expr_value do {COND_POP();}
2683 compstmt
2684 keyword_end
2686 /*%%%*/
2687 $$ = NEW_WHILE(cond($3), $6, 1);
2688 fixpos($$, $3);
2690 $$ = dispatch2(while, $3, $6);
2693 | keyword_until {COND_PUSH(1);} expr_value do {COND_POP();}
2694 compstmt
2695 keyword_end
2697 /*%%%*/
2698 $$ = NEW_UNTIL(cond($3), $6, 1);
2699 fixpos($$, $3);
2701 $$ = dispatch2(until, $3, $6);
2704 | keyword_case expr_value opt_terms
2705 case_body
2706 keyword_end
2708 /*%%%*/
2709 $$ = NEW_CASE($2, $4);
2710 fixpos($$, $2);
2712 $$ = dispatch2(case, $2, $4);
2715 | keyword_case opt_terms case_body keyword_end
2717 /*%%%*/
2718 $$ = NEW_CASE(0, $3);
2720 $$ = dispatch2(case, Qnil, $3);
2723 | keyword_for for_var keyword_in
2724 {COND_PUSH(1);}
2725 expr_value do
2726 {COND_POP();}
2727 compstmt
2728 keyword_end
2730 /*%%%*/
2732 * for a, b, c in e
2733 * #=>
2734 * e.each{|*x| a, b, c = x
2736 * for a in e
2737 * #=>
2738 * e.each{|x| a, = x}
2740 ID id = internal_id();
2741 ID *tbl = ALLOC_N(ID, 2);
2742 NODE *m = NEW_ARGS_AUX(0, 0);
2743 NODE *args, *scope;
2745 if (nd_type($2) == NODE_MASGN) {
2746 /* if args.length == 1 && args[0].kind_of?(Array)
2747 * args = args[0]
2748 * end
2750 NODE *one = NEW_LIST(NEW_LIT(INT2FIX(1)));
2751 NODE *zero = NEW_LIST(NEW_LIT(INT2FIX(0)));
2752 m->nd_next = block_append(
2753 NEW_IF(
2754 NEW_NODE(NODE_AND,
2755 NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("length"), 0),
2756 rb_intern("=="), one),
2757 NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero),
2758 rb_intern("kind_of?"), NEW_LIST(NEW_LIT(rb_cArray))),
2760 NEW_DASGN_CURR(id,
2761 NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero)),
2763 node_assign($2, NEW_DVAR(id)));
2765 else {
2766 m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id));
2769 args = new_args(m, 0, id, 0, 0);
2770 scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
2771 tbl[0] = 1; tbl[1] = id;
2772 $$ = NEW_FOR(0, $5, scope);
2773 fixpos($$, $2);
2775 $$ = dispatch3(for, $2, $5, $8);
2778 | keyword_class cpath superclass
2780 if (in_def || in_single)
2781 yyerror("class definition in method body");
2782 /*%%%*/
2783 local_push(0);
2784 $<num>$ = ruby_sourceline;
2788 bodystmt
2789 keyword_end
2791 /*%%%*/
2792 $$ = NEW_CLASS($2, $5, $3);
2793 nd_set_line($$, $<num>4);
2794 local_pop();
2796 $$ = dispatch3(class, $2, $3, $5);
2799 | keyword_class tLSHFT expr
2801 /*%%%*/
2802 $<num>$ = in_def;
2803 in_def = 0;
2805 in_def = 0;
2808 term
2810 /*%%%*/
2811 $<num>$ = in_single;
2812 in_single = 0;
2813 local_push(0);
2815 $$ = in_single;
2816 in_single = 0;
2819 bodystmt
2820 keyword_end
2822 /*%%%*/
2823 $$ = NEW_SCLASS($3, $7);
2824 fixpos($$, $3);
2825 local_pop();
2826 in_def = $<num>4;
2827 in_single = $<num>6;
2829 $$ = dispatch2(sclass, $3, $7);
2830 in_def = $<val>4;
2831 in_single = $<val>6;
2834 | keyword_module cpath
2836 if (in_def || in_single)
2837 yyerror("module definition in method body");
2838 /*%%%*/
2839 local_push(0);
2840 $<num>$ = ruby_sourceline;
2844 bodystmt
2845 keyword_end
2847 /*%%%*/
2848 $$ = NEW_MODULE($2, $4);
2849 nd_set_line($$, $<num>3);
2850 local_pop();
2852 $$ = dispatch2(module, $2, $4);
2855 | keyword_def fname
2857 $<id>$ = cur_mid;
2858 cur_mid = $2;
2859 in_def++;
2860 /*%%%*/
2861 local_push(0);
2865 f_arglist
2866 bodystmt
2867 keyword_end
2869 /*%%%*/
2870 NODE *body = remove_begin($5);
2871 reduce_nodes(&body);
2872 $$ = NEW_DEFN($2, $4, body, NOEX_PRIVATE);
2873 fixpos($$, $4);
2874 local_pop();
2875 in_def--;
2876 cur_mid = $<id>3;
2878 $$ = dispatch3(def, $2, $4, $5);
2879 in_def--;
2880 cur_mid = $<id>3;
2883 | keyword_def singleton dot_or_colon {lex_state = EXPR_FNAME;} fname
2885 in_single++;
2886 lex_state = EXPR_END; /* force for args */
2887 /*%%%*/
2888 local_push(0);
2892 f_arglist
2893 bodystmt
2894 keyword_end
2896 /*%%%*/
2897 NODE *body = remove_begin($8);
2898 reduce_nodes(&body);
2899 $$ = NEW_DEFS($2, $5, $7, body);
2900 fixpos($$, $2);
2901 local_pop();
2902 in_single--;
2904 $$ = dispatch5(defs, $2, $3, $5, $7, $8);
2905 in_single--;
2908 | keyword_break
2910 /*%%%*/
2911 $$ = NEW_BREAK(0);
2913 $$ = dispatch1(break, arg_new());
2916 | keyword_next
2918 /*%%%*/
2919 $$ = NEW_NEXT(0);
2921 $$ = dispatch1(next, arg_new());
2924 | keyword_redo
2926 /*%%%*/
2927 $$ = NEW_REDO();
2929 $$ = dispatch0(redo);
2932 | keyword_retry
2934 /*%%%*/
2935 $$ = NEW_RETRY();
2937 $$ = dispatch0(retry);
2942 primary_value : primary
2944 /*%%%*/
2945 value_expr($1);
2946 $$ = $1;
2947 if (!$$) $$ = NEW_NIL();
2949 $$ = $1;
2954 then : term
2955 /*%c%*/
2956 /*%c
2957 { $$ = Qnil; }
2959 | keyword_then
2960 | term keyword_then
2961 /*%c%*/
2962 /*%c
2963 { $$ = $2; }
2967 do : term
2968 /*%c%*/
2969 /*%c
2970 { $$ = Qnil; }
2972 | keyword_do_cond
2975 if_tail : opt_else
2976 | keyword_elsif expr_value then
2977 compstmt
2978 if_tail
2980 /*%%%*/
2981 $$ = NEW_IF(cond($2), $4, $5);
2982 fixpos($$, $2);
2984 $$ = dispatch3(elsif, $2, $4, escape_Qundef($5));
2989 opt_else : none
2990 | keyword_else compstmt
2992 /*%%%*/
2993 $$ = $2;
2995 $$ = dispatch1(else, $2);
3000 for_var : lhs
3001 | mlhs
3004 f_marg : f_norm_arg
3006 /*%%%*/
3007 $$ = assignable($1, 0);
3009 $$ = dispatch1(mlhs_paren, $1);
3012 | tLPAREN f_margs rparen
3014 /*%%%*/
3015 $$ = $2;
3017 $$ = dispatch1(mlhs_paren, $2);
3022 f_marg_list : f_marg
3024 /*%%%*/
3025 $$ = NEW_LIST($1);
3027 $$ = mlhs_add(mlhs_new(), $1);
3030 | f_marg_list ',' f_marg
3032 /*%%%*/
3033 $$ = list_append($1, $3);
3035 $$ = mlhs_add($1, $3);
3040 f_margs : f_marg_list
3042 /*%%%*/
3043 $$ = NEW_MASGN($1, 0);
3045 $$ = $1;
3048 | f_marg_list ',' tSTAR f_norm_arg
3050 /*%%%*/
3051 $$ = NEW_MASGN($1, assignable($4, 0));
3053 $$ = mlhs_add_star($1, $4);
3056 | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list
3058 /*%%%*/
3059 $$ = NEW_MASGN($1, NEW_POSTARG(assignable($4, 0), $6));
3061 $$ = mlhs_add_star($1, $4);
3064 | f_marg_list ',' tSTAR
3066 /*%%%*/
3067 $$ = NEW_MASGN($1, -1);
3069 $$ = mlhs_add_star($1, Qnil);
3072 | f_marg_list ',' tSTAR ',' f_marg_list
3074 /*%%%*/
3075 $$ = NEW_MASGN($1, NEW_POSTARG(-1, $5));
3077 $$ = mlhs_add_star($1, $5);
3080 | tSTAR f_norm_arg
3082 /*%%%*/
3083 $$ = NEW_MASGN(0, assignable($2, 0));
3085 $$ = mlhs_add_star(mlhs_new(), $2);
3088 | tSTAR f_norm_arg ',' f_marg_list
3090 /*%%%*/
3091 $$ = NEW_MASGN(0, NEW_POSTARG(assignable($2, 0), $4));
3093 #if 0
3094 TODO: Check me
3095 #endif
3096 $$ = mlhs_add_star($2, $4);
3099 | tSTAR
3101 /*%%%*/
3102 $$ = NEW_MASGN(0, -1);
3104 $$ = mlhs_add_star(mlhs_new(), Qnil);
3107 | tSTAR ',' f_marg_list
3109 /*%%%*/
3110 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
3112 $$ = mlhs_add_star(mlhs_new(), Qnil);
3117 block_param : f_arg ',' f_rest_arg opt_f_block_arg
3119 /*%%%*/
3120 $$ = new_args($1, 0, $3, 0, $4);
3122 $$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4));
3125 | f_arg ','
3127 /*%%%*/
3128 $$ = new_args($1, 0, 1, 0, 0);
3130 $$ = params_new($1, Qnil, Qnil, Qnil, Qnil);
3131 dispatch1(excessed_comma, $$);
3134 | f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg
3136 /*%%%*/
3137 $$ = new_args($1, 0, $3, $5, $6);
3139 $$ = params_new($1, Qnil, $3, $5, escape_Qundef($6));
3142 | f_arg opt_f_block_arg
3144 /*%%%*/
3145 $$ = new_args($1, 0, 0, 0, $2);
3147 $$ = params_new($1, Qnil,Qnil, Qnil, escape_Qundef($2));
3150 | f_rest_arg opt_f_block_arg
3152 /*%%%*/
3153 $$ = new_args(0, 0, $1, 0, $2);
3155 $$ = params_new(Qnil, Qnil, $1, Qnil, escape_Qundef($2));
3158 | f_rest_arg ',' f_arg opt_f_block_arg
3160 /*%%%*/
3161 $$ = new_args(0, 0, $1, $3, $4);
3163 $$ = params_new(Qnil, Qnil, $1, $3, escape_Qundef($4));
3166 | f_block_arg
3168 /*%%%*/
3169 $$ = new_args(0, 0, 0, 0, $1);
3171 $$ = params_new(Qnil, Qnil, Qnil, Qnil, $1);
3176 opt_block_param : none
3177 | block_param_def
3179 command_start = Qtrue;
3183 block_param_def : '|' opt_bv_decl '|'
3185 /*%%%*/
3186 $$ = 0;
3188 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil),
3189 escape_Qundef($2));
3192 | tOROP
3194 /*%%%*/
3195 $$ = 0;
3197 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil),
3198 Qnil);
3201 | '|' block_param opt_bv_decl '|'
3203 /*%%%*/
3204 $$ = $2;
3206 $$ = blockvar_new(escape_Qundef($2), escape_Qundef($3));
3212 opt_bv_decl : none
3213 | ';' bv_decls
3215 /*%%%*/
3216 $$ = 0;
3218 $$ = $2;
3223 bv_decls : bvar
3224 /*%c%*/
3225 /*%c
3227 $$ = rb_ary_new2($1);
3230 | bv_decls ',' bvar
3231 /*%c%*/
3232 /*%c
3234 rb_ary_push($$, $3);
3239 bvar : tIDENTIFIER
3241 /*%%%*/
3242 new_bv($1);
3244 $$ = $1;
3247 | f_bad_arg
3249 $$ = 0;
3253 lambda : {
3254 /*%%%*/
3255 dyna_push();
3256 $<num>$ = lpar_beg;
3257 lpar_beg = ++paren_nest;
3261 f_larglist
3262 lambda_body
3264 /*%%%*/
3265 $$ = $2;
3266 $$->nd_body = NEW_SCOPE($2->nd_head, $3);
3267 dyna_pop();
3268 lpar_beg = $<num>1;
3270 $$ = dispatch2(lambda, $2, $3);
3275 f_larglist : '(' f_args opt_bv_decl rparen
3277 /*%%%*/
3278 $$ = NEW_LAMBDA($2);
3280 $$ = dispatch1(paren, $2);
3283 | f_args opt_bv_decl
3285 /*%%%*/
3286 $$ = NEW_LAMBDA($1);
3288 $$ = $1;
3293 lambda_body : tLAMBEG compstmt '}'
3295 $$ = $2;
3297 | keyword_do_LAMBDA compstmt keyword_end
3299 $$ = $2;
3303 do_block : keyword_do_block
3305 /*%%%*/
3306 dyna_push();
3307 $<num>$ = ruby_sourceline;
3308 /*% %*/
3310 opt_block_param
3311 compstmt
3312 keyword_end
3314 /*%%%*/
3315 $$ = NEW_ITER($3,$4);
3316 nd_set_line($$, $<num>2);
3317 dyna_pop();
3319 $$ = dispatch2(do_block, escape_Qundef($3), $4);
3324 block_call : command do_block
3326 /*%%%*/
3327 block_dup_check($1->nd_args, $2);
3328 $2->nd_iter = $1;
3329 $$ = $2;
3330 fixpos($$, $1);
3332 $$ = method_add_block($1, $2);
3335 | block_call '.' operation2 opt_paren_args
3337 /*%%%*/
3338 $$ = NEW_CALL($1, $3, $4);
3340 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
3341 $$ = method_optarg($$, $4);
3344 | block_call tCOLON2 operation2 opt_paren_args
3346 /*%%%*/
3347 $$ = NEW_CALL($1, $3, $4);
3349 $$ = dispatch3(call, $1, ripper_intern("::"), $3);
3350 $$ = method_optarg($$, $4);
3355 method_call : operation paren_args
3357 /*%%%*/
3358 $$ = NEW_FCALL($1, $2);
3359 fixpos($$, $2);
3361 $$ = method_arg(dispatch1(fcall, $1), $2);
3364 | primary_value '.' operation2 opt_paren_args
3366 /*%%%*/
3367 $$ = NEW_CALL($1, $3, $4);
3368 fixpos($$, $1);
3370 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
3371 $$ = method_optarg($$, $4);
3374 | primary_value tCOLON2 operation2 paren_args
3376 /*%%%*/
3377 $$ = NEW_CALL($1, $3, $4);
3378 fixpos($$, $1);
3380 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
3381 $$ = method_optarg($$, $4);
3384 | primary_value tCOLON2 operation3
3386 /*%%%*/
3387 $$ = NEW_CALL($1, $3, 0);
3389 $$ = dispatch3(call, $1, ripper_intern("::"), $3);
3392 | primary_value '.' paren_args
3394 /*%%%*/
3395 $$ = NEW_CALL($1, rb_intern("call"), $3);
3396 fixpos($$, $1);
3398 $$ = dispatch3(call, dispatch1(paren, $1),
3399 ripper_id2sym('.'), rb_intern("call"));
3400 $$ = method_optarg($$, $3);
3403 | primary_value tCOLON2 paren_args
3405 /*%%%*/
3406 $$ = NEW_CALL($1, rb_intern("call"), $3);
3407 fixpos($$, $1);
3409 $$ = dispatch3(call, dispatch1(paren, $1),
3410 ripper_id2sym('.'), rb_intern("call"));
3411 $$ = method_optarg($$, $3);
3414 | keyword_super paren_args
3416 /*%%%*/
3417 $$ = NEW_SUPER($2);
3419 $$ = dispatch1(super, $2);
3422 | keyword_super
3424 /*%%%*/
3425 $$ = NEW_ZSUPER();
3427 $$ = dispatch0(zsuper);
3430 | primary_value '[' opt_call_args rbracket
3432 /*%%%*/
3433 if ($1 && nd_type($1) == NODE_SELF)
3434 $$ = NEW_FCALL(tAREF, $3);
3435 else
3436 $$ = NEW_CALL($1, tAREF, $3);
3437 fixpos($$, $1);
3439 $$ = dispatch2(aref, $1, escape_Qundef($3));
3444 brace_block : '{'
3446 /*%%%*/
3447 dyna_push();
3448 $<num>$ = ruby_sourceline;
3452 opt_block_param
3453 compstmt '}'
3455 /*%%%*/
3456 $$ = NEW_ITER($3,$4);
3457 nd_set_line($$, $<num>2);
3458 dyna_pop();
3460 $$ = dispatch2(brace_block, escape_Qundef($3), $4);
3463 | keyword_do
3465 /*%%%*/
3466 dyna_push();
3467 $<num>$ = ruby_sourceline;
3471 opt_block_param
3472 compstmt keyword_end
3474 /*%%%*/
3475 $$ = NEW_ITER($3,$4);
3476 nd_set_line($$, $<num>2);
3477 dyna_pop();
3479 $$ = dispatch2(do_block, escape_Qundef($3), $4);
3484 case_body : keyword_when args then
3485 compstmt
3486 cases
3488 /*%%%*/
3489 $$ = NEW_WHEN($2, $4, $5);
3491 $$ = dispatch3(when, $2, $4, escape_Qundef($5));
3496 cases : opt_else
3497 | case_body
3500 opt_rescue : keyword_rescue exc_list exc_var then
3501 compstmt
3502 opt_rescue
3504 /*%%%*/
3505 if ($3) {
3506 $3 = node_assign($3, NEW_ERRINFO());
3507 $5 = block_append($3, $5);
3509 $$ = NEW_RESBODY($2, $5, $6);
3510 fixpos($$, $2?$2:$5);
3512 $$ = dispatch4(rescue,
3513 escape_Qundef($2),
3514 escape_Qundef($3),
3515 escape_Qundef($5),
3516 escape_Qundef($6));
3519 | none
3522 exc_list : arg_value
3524 /*%%%*/
3525 $$ = NEW_LIST($1);
3527 $$ = rb_ary_new3(1, $1);
3530 | mrhs
3532 /*%%%*/
3533 if (!($$ = splat_array($1))) $$ = $1;
3535 $$ = $1;
3538 | none
3541 exc_var : tASSOC lhs
3543 $$ = $2;
3545 | none
3548 opt_ensure : keyword_ensure compstmt
3550 /*%%%*/
3551 $$ = $2;
3553 $$ = dispatch1(ensure, $2);
3556 | none
3559 literal : numeric
3560 | symbol
3562 /*%%%*/
3563 $$ = NEW_LIT(ID2SYM($1));
3565 $$ = dispatch1(symbol_literal, $1);
3568 | dsym
3571 strings : string
3573 /*%%%*/
3574 NODE *node = $1;
3575 if (!node) {
3576 node = NEW_STR(STR_NEW0());
3578 else {
3579 node = evstr2dstr(node);
3581 $$ = node;
3583 $$ = $1;
3588 string : tCHAR
3589 | string1
3590 | string string1
3592 /*%%%*/
3593 $$ = literal_concat($1, $2);
3595 $$ = dispatch2(string_concat, $1, $2);
3600 string1 : tSTRING_BEG string_contents tSTRING_END
3602 /*%%%*/
3603 $$ = $2;
3605 $$ = dispatch1(string_literal, $2);
3610 xstring : tXSTRING_BEG xstring_contents tSTRING_END
3612 /*%%%*/
3613 NODE *node = $2;
3614 if (!node) {
3615 node = NEW_XSTR(STR_NEW0());
3617 else {
3618 switch (nd_type(node)) {
3619 case NODE_STR:
3620 nd_set_type(node, NODE_XSTR);
3621 break;
3622 case NODE_DSTR:
3623 nd_set_type(node, NODE_DXSTR);
3624 break;
3625 default:
3626 node = NEW_NODE(NODE_DXSTR, STR_NEW0(), 1, NEW_LIST(node));
3627 break;
3630 $$ = node;
3632 $$ = dispatch1(xstring_literal, $2);
3637 regexp : tREGEXP_BEG xstring_contents tREGEXP_END
3639 /*%%%*/
3640 int options = $3;
3641 NODE *node = $2;
3642 NODE *list;
3643 if (!node) {
3644 node = NEW_LIT(reg_compile(STR_NEW0(), options));
3646 else switch (nd_type(node)) {
3647 case NODE_STR:
3649 VALUE src = node->nd_lit;
3650 nd_set_type(node, NODE_LIT);
3651 node->nd_lit = reg_compile(src, options);
3653 break;
3654 default:
3655 node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node));
3656 case NODE_DSTR:
3657 if (options & RE_OPTION_ONCE) {
3658 nd_set_type(node, NODE_DREGX_ONCE);
3660 else {
3661 nd_set_type(node, NODE_DREGX);
3663 node->nd_cflag = options & RE_OPTION_MASK;
3664 reg_fragment_check(node->nd_lit, options);
3665 for (list = node->nd_next; list; list = list->nd_next) {
3666 if (nd_type(list->nd_head) == NODE_STR) {
3667 reg_fragment_check(list->nd_head->nd_lit, options);
3670 break;
3672 $$ = node;
3674 $$ = dispatch2(regexp_literal, $2, $3);
3679 words : tWORDS_BEG ' ' tSTRING_END
3681 /*%%%*/
3682 $$ = NEW_ZARRAY();
3684 $$ = dispatch0(words_new);
3687 | tWORDS_BEG word_list tSTRING_END
3689 $$ = $2;
3693 word_list : /* none */
3695 /*%%%*/
3696 $$ = 0;
3698 $$ = dispatch0(words_new);
3701 | word_list word ' '
3703 /*%%%*/
3704 $$ = list_append($1, evstr2dstr($2));
3706 $$ = dispatch2(words_add, $1, $2);
3711 word : string_content
3712 /*%c%*/
3713 /*%c
3715 $$ = dispatch0(word_new);
3716 $$ = dispatch2(word_add, $$, $1);
3719 | word string_content
3721 /*%%%*/
3722 $$ = literal_concat($1, $2);
3724 $$ = dispatch2(word_add, $1, $2);
3729 qwords : tQWORDS_BEG ' ' tSTRING_END
3731 /*%%%*/
3732 $$ = NEW_ZARRAY();
3734 $$ = dispatch0(qwords_new);
3737 | tQWORDS_BEG qword_list tSTRING_END
3739 $$ = $2;
3743 qword_list : /* none */
3745 /*%%%*/
3746 $$ = 0;
3748 $$ = dispatch0(qwords_new);
3751 | qword_list tSTRING_CONTENT ' '
3753 /*%%%*/
3754 $$ = list_append($1, $2);
3756 $$ = dispatch2(qwords_add, $1, $2);
3761 string_contents : /* none */
3763 /*%%%*/
3764 $$ = 0;
3766 $$ = dispatch0(string_content);
3769 | string_contents string_content
3771 /*%%%*/
3772 $$ = literal_concat($1, $2);
3774 $$ = dispatch2(string_add, $1, $2);
3779 xstring_contents: /* none */
3781 /*%%%*/
3782 $$ = 0;
3784 $$ = dispatch0(xstring_new);
3787 | xstring_contents string_content
3789 /*%%%*/
3790 $$ = literal_concat($1, $2);
3792 $$ = dispatch2(xstring_add, $1, $2);
3797 string_content : tSTRING_CONTENT
3798 | tSTRING_DVAR
3800 $<node>$ = lex_strterm;
3801 lex_strterm = 0;
3802 lex_state = EXPR_BEG;
3804 string_dvar
3806 /*%%%*/
3807 lex_strterm = $<node>2;
3808 $$ = NEW_EVSTR($3);
3810 lex_strterm = $<node>2;
3811 $$ = dispatch1(string_dvar, $3);
3814 | tSTRING_DBEG
3816 $<node>$ = lex_strterm;
3817 lex_strterm = 0;
3818 lex_state = EXPR_BEG;
3819 COND_PUSH(0);
3820 CMDARG_PUSH(0);
3822 compstmt '}'
3824 lex_strterm = $<node>2;
3825 COND_LEXPOP();
3826 CMDARG_LEXPOP();
3827 /*%%%*/
3828 if ($3) $3->flags &= ~NODE_FL_NEWLINE;
3829 $$ = new_evstr($3);
3831 $$ = dispatch1(string_embexpr, $3);
3836 string_dvar : tGVAR
3838 /*%%%*/
3839 $$ = NEW_GVAR($1);
3841 $$ = dispatch1(var_ref, $1);
3844 | tIVAR
3846 /*%%%*/
3847 $$ = NEW_IVAR($1);
3849 $$ = dispatch1(var_ref, $1);
3852 | tCVAR
3854 /*%%%*/
3855 $$ = NEW_CVAR($1);
3857 $$ = dispatch1(var_ref, $1);
3860 | backref
3863 symbol : tSYMBEG sym
3865 /*%%%*/
3866 lex_state = EXPR_ENDARG;
3867 $$ = $2;
3869 lex_state = EXPR_ENDARG;
3870 $$ = dispatch1(symbol, $2);
3875 sym : fname
3876 | tIVAR
3877 | tGVAR
3878 | tCVAR
3881 dsym : tSYMBEG xstring_contents tSTRING_END
3883 /*%%%*/
3884 lex_state = EXPR_ENDARG;
3885 if (!($$ = $2)) {
3886 $$ = NEW_LIT(ID2SYM(rb_intern("")));
3888 else {
3889 VALUE lit;
3891 switch (nd_type($$)) {
3892 case NODE_DSTR:
3893 nd_set_type($$, NODE_DSYM);
3894 break;
3895 case NODE_STR:
3896 lit = $$->nd_lit;
3897 $$->nd_lit = ID2SYM(rb_intern_str(lit));
3898 nd_set_type($$, NODE_LIT);
3899 break;
3900 default:
3901 $$ = NEW_NODE(NODE_DSYM, STR_NEW0(), 1, NEW_LIST($$));
3902 break;
3906 lex_state = EXPR_ENDARG;
3907 $$ = dispatch1(dyna_symbol, $2);
3912 numeric : tINTEGER
3913 | tFLOAT
3914 | tUMINUS_NUM tINTEGER %prec tLOWEST
3916 /*%%%*/
3917 $$ = negate_lit($2);
3919 $$ = dispatch2(unary, ripper_intern("-@"), $2);
3922 | tUMINUS_NUM tFLOAT %prec tLOWEST
3924 /*%%%*/
3925 $$ = negate_lit($2);
3927 $$ = dispatch2(unary, ripper_intern("-@"), $2);
3932 variable : tIDENTIFIER
3933 | tIVAR
3934 | tGVAR
3935 | tCONSTANT
3936 | tCVAR
3937 | keyword_nil {ifndef_ripper($$ = keyword_nil);}
3938 | keyword_self {ifndef_ripper($$ = keyword_self);}
3939 | keyword_true {ifndef_ripper($$ = keyword_true);}
3940 | keyword_false {ifndef_ripper($$ = keyword_false);}
3941 | keyword__FILE__ {ifndef_ripper($$ = keyword__FILE__);}
3942 | keyword__LINE__ {ifndef_ripper($$ = keyword__LINE__);}
3943 | keyword__ENCODING__ {ifndef_ripper($$ = keyword__ENCODING__);}
3946 var_ref : variable
3948 /*%%%*/
3949 if (!($$ = gettable($1))) $$ = NEW_BEGIN(0);
3951 $$ = dispatch1(var_ref, $1);
3956 var_lhs : variable
3958 /*%%%*/
3959 $$ = assignable($1, 0);
3961 $$ = dispatch1(var_field, $1);
3966 backref : tNTH_REF
3967 | tBACK_REF
3970 superclass : term
3972 /*%%%*/
3973 $$ = 0;
3975 $$ = Qnil;
3978 | '<'
3980 lex_state = EXPR_BEG;
3982 expr_value term
3984 $$ = $3;
3986 | error term
3988 /*%%%*/
3989 yyerrok;
3990 $$ = 0;
3992 yyerrok;
3993 $$ = Qnil;
3998 f_arglist : '(' f_args rparen
4000 /*%%%*/
4001 $$ = $2;
4002 lex_state = EXPR_BEG;
4003 command_start = Qtrue;
4005 $$ = dispatch1(paren, $2);
4006 lex_state = EXPR_BEG;
4007 command_start = Qtrue;
4010 | f_args term
4012 $$ = $1;
4016 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg
4018 /*%%%*/
4019 $$ = new_args($1, $3, $5, 0, $6);
4021 $$ = params_new($1, $3, $5, Qnil, escape_Qundef($6));
4024 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
4026 /*%%%*/
4027 $$ = new_args($1, $3, $5, $7, $8);
4029 $$ = params_new($1, $3, $5, $7, escape_Qundef($8));
4032 | f_arg ',' f_optarg opt_f_block_arg
4034 /*%%%*/
4035 $$ = new_args($1, $3, 0, 0, $4);
4037 $$ = params_new($1, $3, Qnil, Qnil, escape_Qundef($4));
4040 | f_arg ',' f_optarg ',' f_arg opt_f_block_arg
4042 /*%%%*/
4043 $$ = new_args($1, $3, 0, $5, $6);
4045 $$ = params_new($1, $3, Qnil, $5, escape_Qundef($6));
4048 | f_arg ',' f_rest_arg opt_f_block_arg
4050 /*%%%*/
4051 $$ = new_args($1, 0, $3, 0, $4);
4053 $$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4));
4056 | f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg
4058 /*%%%*/
4059 $$ = new_args($1, 0, $3, $5, $6);
4061 $$ = params_new($1, Qnil, $3, $5, escape_Qundef($6));
4064 | f_arg opt_f_block_arg
4066 /*%%%*/
4067 $$ = new_args($1, 0, 0, 0, $2);
4069 $$ = params_new($1, Qnil, Qnil, Qnil,escape_Qundef($2));
4072 | f_optarg ',' f_rest_arg opt_f_block_arg
4074 /*%%%*/
4075 $$ = new_args(0, $1, $3, 0, $4);
4077 $$ = params_new(Qnil, $1, $3, Qnil, escape_Qundef($4));
4080 | f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
4082 /*%%%*/
4083 $$ = new_args(0, $1, $3, $5, $6);
4085 $$ = params_new(Qnil, $1, $3, $5, escape_Qundef($6));
4088 | f_optarg opt_f_block_arg
4090 /*%%%*/
4091 $$ = new_args(0, $1, 0, 0, $2);
4093 $$ = params_new(Qnil, $1, Qnil, Qnil,escape_Qundef($2));
4096 | f_optarg ',' f_arg opt_f_block_arg
4098 /*%%%*/
4099 $$ = new_args(0, $1, 0, $3, $4);
4101 $$ = params_new(Qnil, $1, Qnil, $3, escape_Qundef($4));
4104 | f_rest_arg opt_f_block_arg
4106 /*%%%*/
4107 $$ = new_args(0, 0, $1, 0, $2);
4109 $$ = params_new(Qnil, Qnil, $1, Qnil,escape_Qundef($2));
4112 | f_rest_arg ',' f_arg opt_f_block_arg
4114 /*%%%*/
4115 $$ = new_args(0, 0, $1, $3, $4);
4117 $$ = params_new(Qnil, Qnil, $1, $3, escape_Qundef($4));
4120 | f_block_arg
4122 /*%%%*/
4123 $$ = new_args(0, 0, 0, 0, $1);
4125 $$ = params_new(Qnil, Qnil, Qnil, Qnil, $1);
4128 | /* none */
4130 /*%%%*/
4131 $$ = new_args(0, 0, 0, 0, 0);
4133 $$ = params_new(Qnil, Qnil, Qnil, Qnil, Qnil);
4138 f_bad_arg : tCONSTANT
4140 /*%%%*/
4141 yyerror("formal argument cannot be a constant");
4142 $$ = 0;
4144 $$ = dispatch1(param_error, $1);
4147 | tIVAR
4149 /*%%%*/
4150 yyerror("formal argument cannot be an instance variable");
4151 $$ = 0;
4153 $$ = dispatch1(param_error, $1);
4156 | tGVAR
4158 /*%%%*/
4159 yyerror("formal argument cannot be a global variable");
4160 $$ = 0;
4162 $$ = dispatch1(param_error, $1);
4165 | tCVAR
4167 /*%%%*/
4168 yyerror("formal argument cannot be a class variable");
4169 $$ = 0;
4171 $$ = dispatch1(param_error, $1);
4176 f_norm_arg : f_bad_arg
4177 | tIDENTIFIER
4179 /*%%%*/
4180 if (!is_local_id($1))
4181 yyerror("formal argument must be local variable");
4182 shadowing_lvar($1);
4185 $$ = $1;
4189 f_arg_item : f_norm_arg
4191 /*%%%*/
4192 arg_var($1);
4193 $$ = NEW_ARGS_AUX($1, 1);
4197 | tLPAREN f_margs rparen
4199 /*%%%*/
4200 ID tid = internal_id();
4201 arg_var(tid);
4202 if (dyna_in_block()) {
4203 $2->nd_value = NEW_DVAR(tid);
4205 else {
4206 $2->nd_value = NEW_LVAR(tid);
4208 $$ = NEW_ARGS_AUX(tid, 1);
4209 $$->nd_next = $2;
4211 $$ = dispatch1(mlhs_paren, $2);
4216 f_arg : f_arg_item
4217 /*%c%*/
4218 /*%c
4220 $$ = rb_ary_new3(1, $1);
4222 c%*/
4223 | f_arg ',' f_arg_item
4225 /*%%%*/
4226 $$ = $1;
4227 $$->nd_plen++;
4228 $$->nd_next = block_append($$->nd_next, $3->nd_next);
4229 rb_gc_force_recycle((VALUE)$3);
4231 $$ = rb_ary_push($1, $3);
4236 f_opt : tIDENTIFIER '=' arg_value
4238 /*%%%*/
4239 if (!is_local_id($1))
4240 yyerror("formal argument must be local variable");
4241 shadowing_lvar($1);
4242 arg_var($1);
4243 $$ = NEW_OPT_ARG(0, assignable($1, $3));
4245 $$ = rb_assoc_new($1, $3);
4250 f_optarg : f_opt
4252 /*%%%*/
4253 $$ = $1;
4255 $$ = rb_ary_new3(1, $1);
4258 | f_optarg ',' f_opt
4260 /*%%%*/
4261 NODE *opts = $1;
4263 while (opts->nd_next) {
4264 opts = opts->nd_next;
4266 opts->nd_next = $3;
4267 $$ = $1;
4269 $$ = rb_ary_push($1, $3);
4274 restarg_mark : '*'
4275 | tSTAR
4278 f_rest_arg : restarg_mark tIDENTIFIER
4280 /*%%%*/
4281 if (!is_local_id($2))
4282 yyerror("rest argument must be local variable");
4283 shadowing_lvar($2);
4284 arg_var($2);
4285 $$ = $2;
4287 $$ = dispatch1(rest_param, $2);
4290 | restarg_mark
4292 /*%%%*/
4293 $$ = internal_id();
4294 arg_var($$);
4296 $$ = dispatch1(rest_param, Qnil);
4301 blkarg_mark : '&'
4302 | tAMPER
4305 f_block_arg : blkarg_mark tIDENTIFIER
4307 /*%%%*/
4308 if (!is_local_id($2))
4309 yyerror("block argument must be local variable");
4310 else if (!dyna_in_block() && local_id($2))
4311 yyerror("duplicated block argument name");
4312 shadowing_lvar($2);
4313 arg_var($2);
4314 $$ = $2;
4316 $$ = dispatch1(blockarg, $2);
4321 opt_f_block_arg : ',' f_block_arg
4323 $$ = $2;
4325 | none
4327 /*%%%*/
4328 $$ = 0;
4330 $$ = Qundef;
4335 singleton : var_ref
4337 /*%%%*/
4338 value_expr($1);
4339 $$ = $1;
4340 if (!$$) $$ = NEW_NIL();
4342 $$ = $1;
4345 | '(' {lex_state = EXPR_BEG;} expr rparen
4347 /*%%%*/
4348 if ($3 == 0) {
4349 yyerror("can't define singleton method for ().");
4351 else {
4352 switch (nd_type($3)) {
4353 case NODE_STR:
4354 case NODE_DSTR:
4355 case NODE_XSTR:
4356 case NODE_DXSTR:
4357 case NODE_DREGX:
4358 case NODE_LIT:
4359 case NODE_ARRAY:
4360 case NODE_ZARRAY:
4361 yyerror("can't define singleton method for literals");
4362 default:
4363 value_expr($3);
4364 break;
4367 $$ = $3;
4369 $$ = dispatch1(paren, $3);
4374 assoc_list : none
4375 | assocs trailer
4377 /*%%%*/
4378 $$ = $1;
4380 $$ = dispatch1(assoclist_from_args, $1);
4385 assocs : assoc
4386 /*%c%*/
4387 /*%c
4389 $$ = rb_ary_new3(1, $1);
4392 | assocs ',' assoc
4394 /*%%%*/
4395 $$ = list_concat($1, $3);
4397 $$ = rb_ary_push($1, $3);
4402 assoc : arg_value tASSOC arg_value
4404 /*%%%*/
4405 $$ = list_append(NEW_LIST($1), $3);
4407 $$ = dispatch2(assoc_new, $1, $3);
4410 | tLABEL arg_value
4412 /*%%%*/
4413 $$ = list_append(NEW_LIST(NEW_LIT(ID2SYM($1))), $2);
4415 $$ = dispatch2(assoc_new, $1, $2);
4420 operation : tIDENTIFIER
4421 | tCONSTANT
4422 | tFID
4425 operation2 : tIDENTIFIER
4426 | tCONSTANT
4427 | tFID
4428 | op
4431 operation3 : tIDENTIFIER
4432 | tFID
4433 | op
4436 dot_or_colon : '.'
4437 /*%c%*/
4438 /*%c
4439 { $$ = $<val>1; }
4441 | tCOLON2
4442 /*%c%*/
4443 /*%c
4444 { $$ = $<val>1; }
4448 opt_terms : /* none */
4449 | terms
4452 opt_nl : /* none */
4453 | '\n'
4456 rparen : opt_nl ')'
4459 rbracket : opt_nl ']'
4462 trailer : /* none */
4463 | '\n'
4464 | ','
4467 term : ';' {yyerrok;}
4468 | '\n'
4471 terms : term
4472 | terms ';' {yyerrok;}
4475 none : /* none */
4477 /*%%%*/
4478 $$ = 0;
4480 $$ = Qundef;
4485 # undef parser
4486 # undef yylex
4487 # undef yylval
4488 # define yylval (*((YYSTYPE*)(parser->parser_yylval)))
4490 static int parser_regx_options(struct parser_params*);
4491 static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**);
4492 static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc);
4493 static int parser_parse_string(struct parser_params*,NODE*);
4494 static int parser_here_document(struct parser_params*,NODE*);
4497 # define nextc() parser_nextc(parser)
4498 # define pushback(c) parser_pushback(parser, c)
4499 # define newtok() parser_newtok(parser)
4500 # define tokspace(n) parser_tokspace(parser, n)
4501 # define tokadd(c) parser_tokadd(parser, c)
4502 # define tok_hex(numlen) parser_tok_hex(parser, numlen)
4503 # define read_escape(flags,e) parser_read_escape(parser, flags, e)
4504 # define tokadd_escape(e) parser_tokadd_escape(parser, e)
4505 # define regx_options() parser_regx_options(parser)
4506 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,f,t,p,n,e)
4507 # define parse_string(n) parser_parse_string(parser,n)
4508 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, c, enc)
4509 # define here_document(n) parser_here_document(parser,n)
4510 # define heredoc_identifier() parser_heredoc_identifier(parser)
4511 # define heredoc_restore(n) parser_heredoc_restore(parser,n)
4512 # define whole_match_p(e,l,i) parser_whole_match_p(parser,e,l,i)
4514 #ifdef RIPPER
4515 /* FIXME */
4516 # define local_id(x) 1
4517 # define dyna_in_block() 1
4518 #endif /* RIPPER */
4520 #ifndef RIPPER
4521 # define set_yylval_str(x) yylval.node = NEW_STR(x)
4522 # define set_yylval_num(x) yylval.num = x
4523 # define set_yylval_id(x) yylval.id = x
4524 # define set_yylval_literal(x) yylval.node = NEW_LIT(x)
4525 # define set_yylval_node(x) yylval.node = x
4526 # define yylval_id() yylval.id
4527 #else
4528 # define set_yylval_str(x) (void)(x)
4529 # define set_yylval_num(x) (void)(x)
4530 # define set_yylval_id(x) (void)(x)
4531 # define set_yylval_literal(x) (void)(x)
4532 # define set_yylval_node(x) (void)(x)
4533 # define yylval_id() SYM2ID(yylval.val)
4534 #endif
4536 #ifdef RIPPER
4537 #define ripper_flush(p) (p->tokp = p->parser_lex_p)
4539 static void
4540 ripper_dispatch_scan_event(struct parser_params *parser, int t)
4542 VALUE str;
4544 if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
4545 if (lex_p == parser->tokp) return;
4546 str = STR_NEW(parser->tokp, lex_p - parser->tokp);
4547 yylval.val = ripper_dispatch1(parser, ripper_token2eventid(t), str);
4548 ripper_flush(parser);
4551 static void
4552 ripper_dispatch_delayed_token(struct parser_params *parser, int t)
4554 int saved_line = ruby_sourceline;
4555 const char *saved_tokp = parser->tokp;
4557 ruby_sourceline = parser->delayed_line;
4558 parser->tokp = lex_pbeg + parser->delayed_col;
4559 yylval.val = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed);
4560 parser->delayed = Qnil;
4561 ruby_sourceline = saved_line;
4562 parser->tokp = saved_tokp;
4564 #endif /* RIPPER */
4566 #include "ruby/regex.h"
4567 #include "ruby/util.h"
4569 /* We remove any previous definition of `SIGN_EXTEND_CHAR',
4570 since ours (we hope) works properly with all combinations of
4571 machines, compilers, `char' and `unsigned char' argument types.
4572 (Per Bothner suggested the basic approach.) */
4573 #undef SIGN_EXTEND_CHAR
4574 #if __STDC__
4575 # define SIGN_EXTEND_CHAR(c) ((signed char)(c))
4576 #else /* not __STDC__ */
4577 /* As in Harbison and Steele. */
4578 # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
4579 #endif
4581 #define parser_mbclen() mbclen((lex_p-1),lex_pend,parser->enc)
4582 #define parser_precise_mbclen() rb_enc_precise_mbclen((lex_p-1),lex_pend,parser->enc)
4583 #define is_identchar(p,e,enc) (rb_enc_isalnum(*p,enc) || (*p) == '_' || !ISASCII(*p))
4584 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,parser->enc))
4586 #define parser_isascii() ISASCII(*(lex_p-1))
4588 static int
4589 parser_yyerror(struct parser_params *parser, const char *msg)
4591 #ifndef RIPPER
4592 const int max_line_margin = 30;
4593 const char *p, *pe;
4594 char *buf;
4595 int len, i;
4597 compile_error(PARSER_ARG "%s", msg);
4598 p = lex_p;
4599 while (lex_pbeg <= p) {
4600 if (*p == '\n') break;
4601 p--;
4603 p++;
4605 pe = lex_p;
4606 while (pe < lex_pend) {
4607 if (*pe == '\n') break;
4608 pe++;
4611 len = pe - p;
4612 if (len > 4) {
4613 char *p2;
4614 const char *pre = "", *post = "";
4616 if (len > max_line_margin * 2 + 10) {
4617 if (lex_p - p > max_line_margin) {
4618 p = rb_enc_prev_char(p, lex_p - max_line_margin, rb_enc_get(lex_lastline));
4619 pre = "...";
4621 if (pe - lex_p > max_line_margin) {
4622 pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, rb_enc_get(lex_lastline));
4623 post = "...";
4625 len = pe - p;
4627 buf = ALLOCA_N(char, len+2);
4628 MEMCPY(buf, p, char, len);
4629 buf[len] = '\0';
4630 rb_compile_error_append("%s%s%s", pre, buf, post);
4632 i = lex_p - p;
4633 p2 = buf; pe = buf + len;
4635 while (p2 < pe) {
4636 if (*p2 != '\t') *p2 = ' ';
4637 p2++;
4639 buf[i] = '^';
4640 buf[i+1] = '\0';
4641 rb_compile_error_append("%s%s", pre, buf);
4643 #else
4644 dispatch1(parse_error, STR_NEW2(msg));
4645 #endif /* !RIPPER */
4646 return 0;
4649 static void parser_prepare(struct parser_params *parser);
4651 #ifndef RIPPER
4652 VALUE ruby_suppress_tracing(VALUE (*func)(VALUE, int), VALUE arg, int always);
4654 static VALUE
4655 debug_lines(const char *f)
4657 if (rb_const_defined_at(rb_cObject, rb_intern("SCRIPT_LINES__"))) {
4658 VALUE hash = rb_const_get_at(rb_cObject, rb_intern("SCRIPT_LINES__"));
4659 if (TYPE(hash) == T_HASH) {
4660 VALUE fname = rb_str_new2(f);
4661 VALUE lines = rb_ary_new();
4662 rb_hash_aset(hash, fname, lines);
4663 return lines;
4666 return 0;
4669 static VALUE
4670 coverage(const char *f, int n)
4672 extern VALUE rb_get_coverages(void);
4673 VALUE coverages = rb_get_coverages();
4674 if (RTEST(coverages) && RBASIC(coverages)->klass == 0) {
4675 VALUE fname = rb_str_new2(f);
4676 VALUE lines = rb_ary_new2(n);
4677 int i;
4678 RBASIC(lines)->klass = 0;
4679 for (i = 0; i < n; i++) RARRAY_PTR(lines)[i] = Qnil;
4680 RARRAY(lines)->len = n;
4681 rb_hash_aset(coverages, fname, lines);
4682 return lines;
4684 return 0;
4687 static int
4688 e_option_supplied(struct parser_params *parser)
4690 if (strcmp(ruby_sourcefile, "-e") == 0)
4691 return Qtrue;
4692 return Qfalse;
4695 static VALUE
4696 yycompile0(VALUE arg, int tracing)
4698 int n;
4699 NODE *tree;
4700 struct parser_params *parser = (struct parser_params *)arg;
4702 if (!compile_for_eval && rb_safe_level() == 0) {
4703 ruby_debug_lines = debug_lines(ruby_sourcefile);
4704 if (ruby_debug_lines && ruby_sourceline > 0) {
4705 VALUE str = STR_NEW0();
4706 n = ruby_sourceline;
4707 do {
4708 rb_ary_push(ruby_debug_lines, str);
4709 } while (--n);
4712 if (!e_option_supplied(parser)) {
4713 ruby_coverage = coverage(ruby_sourcefile, ruby_sourceline);
4717 parser_prepare(parser);
4718 deferred_nodes = 0;
4719 n = yyparse((void*)parser);
4720 ruby_debug_lines = 0;
4721 ruby_coverage = 0;
4722 compile_for_eval = 0;
4724 lex_strterm = 0;
4725 lex_p = lex_pbeg = lex_pend = 0;
4726 lex_lastline = lex_nextline = 0;
4727 if (parser->nerr) {
4728 return 0;
4730 tree = ruby_eval_tree;
4731 if (!tree) {
4732 tree = NEW_NIL();
4734 if (ruby_eval_tree_begin) {
4735 NODE *scope = ruby_eval_tree;
4737 if (scope) {
4738 scope->nd_body = NEW_PRELUDE(ruby_eval_tree_begin, scope->nd_body);
4740 tree = scope;
4742 else {
4743 tree = ruby_eval_tree;
4745 return (VALUE)tree;
4748 static NODE*
4749 yycompile(struct parser_params *parser, const char *f, int line)
4751 ruby_sourcefile = ruby_strdup(f);
4752 ruby_sourceline = line - 1;
4753 return (NODE *)ruby_suppress_tracing(yycompile0, (VALUE)parser, Qtrue);
4755 #endif /* !RIPPER */
4757 static VALUE
4758 lex_get_str(struct parser_params *parser, VALUE s)
4760 char *beg, *end, *pend;
4762 beg = RSTRING_PTR(s);
4763 if (lex_gets_ptr) {
4764 if (RSTRING_LEN(s) == lex_gets_ptr) return Qnil;
4765 beg += lex_gets_ptr;
4767 pend = RSTRING_PTR(s) + RSTRING_LEN(s);
4768 end = beg;
4769 while (end < pend) {
4770 if (*end++ == '\n') break;
4772 lex_gets_ptr = end - RSTRING_PTR(s);
4773 return rb_enc_str_new(beg, end - beg, rb_enc_get(s));
4776 static VALUE
4777 lex_getline(struct parser_params *parser)
4779 VALUE line = (*parser->parser_lex_gets)(parser, parser->parser_lex_input);
4780 #ifndef RIPPER
4781 if (ruby_debug_lines && !NIL_P(line)) {
4782 rb_ary_push(ruby_debug_lines, line);
4784 if (ruby_coverage && !NIL_P(line)) {
4785 rb_ary_push(ruby_coverage, Qnil);
4787 #endif
4788 return line;
4791 #ifndef RIPPER
4792 NODE*
4793 rb_compile_string(const char *f, VALUE s, int line)
4795 VALUE volatile vparser = rb_parser_new();
4797 return rb_parser_compile_string(vparser, f, s, line);
4800 NODE*
4801 rb_parser_compile_string(volatile VALUE vparser, const char *f, VALUE s, int line)
4803 struct parser_params *parser;
4804 NODE *node;
4805 volatile VALUE tmp;
4807 Data_Get_Struct(vparser, struct parser_params, parser);
4808 lex_gets = lex_get_str;
4809 lex_gets_ptr = 0;
4810 lex_input = s;
4811 lex_pbeg = lex_p = lex_pend = 0;
4812 compile_for_eval = rb_parse_in_eval();
4814 node = yycompile(parser, f, line);
4815 tmp = vparser; /* prohibit tail call optimization */
4817 return node;
4820 NODE*
4821 rb_compile_cstr(const char *f, const char *s, int len, int line)
4823 return rb_compile_string(f, rb_str_new(s, len), line);
4826 NODE*
4827 rb_parser_compile_cstr(volatile VALUE vparser, const char *f, const char *s, int len, int line)
4829 return rb_parser_compile_string(vparser, f, rb_str_new(s, len), line);
4832 static VALUE
4833 lex_io_gets(struct parser_params *parser, VALUE io)
4835 return rb_io_gets(io);
4838 NODE*
4839 rb_compile_file(const char *f, VALUE file, int start)
4841 VALUE volatile vparser = rb_parser_new();
4843 return rb_parser_compile_file(vparser, f, file, start);
4846 NODE*
4847 rb_parser_compile_file(volatile VALUE vparser, const char *f, VALUE file, int start)
4849 struct parser_params *parser;
4850 volatile VALUE tmp;
4851 NODE *node;
4853 Data_Get_Struct(vparser, struct parser_params, parser);
4854 lex_gets = lex_io_gets;
4855 lex_input = file;
4856 lex_pbeg = lex_p = lex_pend = 0;
4858 node = yycompile(parser, f, start);
4859 tmp = vparser; /* prohibit tail call optimization */
4861 return node;
4863 #endif /* !RIPPER */
4865 #define STR_FUNC_ESCAPE 0x01
4866 #define STR_FUNC_EXPAND 0x02
4867 #define STR_FUNC_REGEXP 0x04
4868 #define STR_FUNC_QWORDS 0x08
4869 #define STR_FUNC_SYMBOL 0x10
4870 #define STR_FUNC_INDENT 0x20
4872 enum string_type {
4873 str_squote = (0),
4874 str_dquote = (STR_FUNC_EXPAND),
4875 str_xquote = (STR_FUNC_EXPAND),
4876 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
4877 str_sword = (STR_FUNC_QWORDS),
4878 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
4879 str_ssym = (STR_FUNC_SYMBOL),
4880 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
4883 static VALUE
4884 parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *enc0)
4886 VALUE str;
4888 str = rb_enc_str_new(p, n, enc);
4889 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
4890 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
4891 rb_enc_associate(str, rb_usascii_encoding());
4893 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
4894 rb_enc_associate(str, rb_ascii8bit_encoding());
4898 return str;
4901 #define lex_goto_eol(parser) (parser->parser_lex_p = parser->parser_lex_pend)
4903 static inline int
4904 parser_nextc(struct parser_params *parser)
4906 int c;
4908 if (lex_p == lex_pend) {
4909 VALUE v = lex_nextline;
4910 lex_nextline = 0;
4911 if (!v) {
4912 if (parser->eofp)
4913 return -1;
4915 if (!lex_input || NIL_P(v = lex_getline(parser))) {
4916 parser->eofp = Qtrue;
4917 lex_goto_eol(parser);
4918 return -1;
4922 #ifdef RIPPER
4923 if (parser->tokp < lex_pend) {
4924 if (NIL_P(parser->delayed)) {
4925 parser->delayed = rb_str_buf_new(1024);
4926 rb_str_buf_cat(parser->delayed,
4927 parser->tokp, lex_pend - parser->tokp);
4928 parser->delayed_line = ruby_sourceline;
4929 parser->delayed_col = parser->tokp - lex_pbeg;
4931 else {
4932 rb_str_buf_cat(parser->delayed,
4933 parser->tokp, lex_pend - parser->tokp);
4936 #endif
4937 if (heredoc_end > 0) {
4938 ruby_sourceline = heredoc_end;
4939 heredoc_end = 0;
4941 ruby_sourceline++;
4942 parser->line_count++;
4943 lex_pbeg = lex_p = RSTRING_PTR(v);
4944 lex_pend = lex_p + RSTRING_LEN(v);
4945 #ifdef RIPPER
4946 ripper_flush(parser);
4947 #endif
4948 lex_lastline = v;
4951 c = (unsigned char)*lex_p++;
4952 if (c == '\r' && lex_p < lex_pend && *lex_p == '\n') {
4953 lex_p++;
4954 c = '\n';
4957 return c;
4960 static void
4961 parser_pushback(struct parser_params *parser, int c)
4963 if (c == -1) return;
4964 lex_p--;
4965 if (lex_p > lex_pbeg && lex_p[0] == '\n' && lex_p[-1] == '\r') {
4966 lex_p--;
4970 #define was_bol() (lex_p == lex_pbeg + 1)
4971 #define peek(c) (lex_p != lex_pend && (c) == *lex_p)
4973 #define tokfix() (tokenbuf[tokidx]='\0')
4974 #define tok() tokenbuf
4975 #define toklen() tokidx
4976 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
4978 static char*
4979 parser_newtok(struct parser_params *parser)
4981 tokidx = 0;
4982 if (!tokenbuf) {
4983 toksiz = 60;
4984 tokenbuf = ALLOC_N(char, 60);
4986 if (toksiz > 4096) {
4987 toksiz = 60;
4988 REALLOC_N(tokenbuf, char, 60);
4990 return tokenbuf;
4993 static char *
4994 parser_tokspace(struct parser_params *parser, int n)
4996 tokidx += n;
4998 if (tokidx >= toksiz) {
4999 do {toksiz *= 2;} while (toksiz < tokidx);
5000 REALLOC_N(tokenbuf, char, toksiz);
5002 return &tokenbuf[tokidx-n];
5005 static void
5006 parser_tokadd(struct parser_params *parser, int c)
5008 tokenbuf[tokidx++] = (char)c;
5009 if (tokidx >= toksiz) {
5010 toksiz *= 2;
5011 REALLOC_N(tokenbuf, char, toksiz);
5015 static int
5016 parser_tok_hex(struct parser_params *parser, int *numlen)
5018 int c;
5020 c = scan_hex(lex_p, 2, numlen);
5021 if (!*numlen) {
5022 yyerror("invalid hex escape");
5023 return 0;
5025 lex_p += *numlen;
5026 return c;
5029 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5031 static int
5032 parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp,
5033 int string_literal, int symbol_literal, int regexp_literal)
5036 * If string_literal is true, then we allow multiple codepoints
5037 * in \u{}, and add the codepoints to the current token.
5038 * Otherwise we're parsing a character literal and return a single
5039 * codepoint without adding it
5042 int codepoint;
5043 int numlen;
5045 if (regexp_literal) { tokadd('\\'); tokadd('u'); }
5047 if (peek('{')) { /* handle \u{...} form */
5048 do {
5049 if (regexp_literal) { tokadd(*lex_p); }
5050 nextc();
5051 codepoint = scan_hex(lex_p, 6, &numlen);
5052 if (numlen == 0) {
5053 yyerror("invalid Unicode escape");
5054 return 0;
5056 if (codepoint > 0x10ffff) {
5057 yyerror("invalid Unicode codepoint (too large)");
5058 return 0;
5060 lex_p += numlen;
5061 if (regexp_literal) {
5062 tokcopy(numlen);
5064 else if (codepoint >= 0x80) {
5065 *encp = UTF8_ENC();
5066 if (string_literal) tokaddmbc(codepoint, *encp);
5068 else if (string_literal) {
5069 if (codepoint == 0 && symbol_literal) {
5070 yyerror("symbol cannot contain '\\u{0}'");
5071 return 0;
5074 tokadd(codepoint);
5076 } while (string_literal && (peek(' ') || peek('\t')));
5078 if (!peek('}')) {
5079 yyerror("unterminated Unicode escape");
5080 return 0;
5083 if (regexp_literal) { tokadd('}'); }
5084 nextc();
5086 else { /* handle \uxxxx form */
5087 codepoint = scan_hex(lex_p, 4, &numlen);
5088 if (numlen < 4) {
5089 yyerror("invalid Unicode escape");
5090 return 0;
5092 lex_p += 4;
5093 if (regexp_literal) {
5094 tokcopy(4);
5096 else if (codepoint >= 0x80) {
5097 *encp = UTF8_ENC();
5098 if (string_literal) tokaddmbc(codepoint, *encp);
5100 else if (string_literal) {
5101 if (codepoint == 0 && symbol_literal) {
5102 yyerror("symbol cannot contain '\\u0000'");
5103 return 0;
5106 tokadd(codepoint);
5110 return codepoint;
5113 #define ESCAPE_CONTROL 1
5114 #define ESCAPE_META 2
5116 static int
5117 parser_read_escape(struct parser_params *parser, int flags,
5118 rb_encoding **encp)
5120 int c;
5121 int numlen;
5123 switch (c = nextc()) {
5124 case '\\': /* Backslash */
5125 return c;
5127 case 'n': /* newline */
5128 return '\n';
5130 case 't': /* horizontal tab */
5131 return '\t';
5133 case 'r': /* carriage-return */
5134 return '\r';
5136 case 'f': /* form-feed */
5137 return '\f';
5139 case 'v': /* vertical tab */
5140 return '\13';
5142 case 'a': /* alarm(bell) */
5143 return '\007';
5145 case 'e': /* escape */
5146 return 033;
5148 case '0': case '1': case '2': case '3': /* octal constant */
5149 case '4': case '5': case '6': case '7':
5150 if (flags & (ESCAPE_CONTROL|ESCAPE_META)) goto eof;
5152 int numlen;
5154 pushback(c);
5155 c = scan_oct(lex_p, 3, &numlen);
5156 lex_p += numlen;
5158 return c;
5160 case 'x': /* hex constant */
5161 if (flags & (ESCAPE_CONTROL|ESCAPE_META)) goto eof;
5162 c = tok_hex(&numlen);
5163 if (numlen == 0) return 0;
5164 return c;
5166 case 'b': /* backspace */
5167 return '\010';
5169 case 's': /* space */
5170 return ' ';
5172 case 'M':
5173 if (flags & ESCAPE_META) goto eof;
5174 if ((c = nextc()) != '-') {
5175 pushback(c);
5176 goto eof;
5178 if ((c = nextc()) == '\\') {
5179 return read_escape(flags|ESCAPE_META, encp) | 0x80;
5181 else if (c == -1 || !ISASCII(c)) goto eof;
5182 else {
5183 return ((c & 0xff) | 0x80);
5186 case 'C':
5187 if ((c = nextc()) != '-') {
5188 pushback(c);
5189 goto eof;
5191 case 'c':
5192 if (flags & ESCAPE_CONTROL) goto eof;
5193 if ((c = nextc())== '\\') {
5194 c = read_escape(flags|ESCAPE_CONTROL, encp);
5196 else if (c == '?')
5197 return 0177;
5198 else if (c == -1 || !ISASCII(c)) goto eof;
5199 return c & 0x9f;
5201 eof:
5202 case -1:
5203 yyerror("Invalid escape character syntax");
5204 return '\0';
5206 default:
5207 return c;
5211 static void
5212 parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc)
5214 int len = rb_enc_codelen(c, enc);
5215 rb_enc_mbcput(c, tokspace(len), enc);
5218 static int
5219 parser_tokadd_escape(struct parser_params *parser, rb_encoding **encp)
5221 int c;
5222 int flags = 0;
5224 first:
5225 switch (c = nextc()) {
5226 case '\n':
5227 return 0; /* just ignore */
5229 case '0': case '1': case '2': case '3': /* octal constant */
5230 case '4': case '5': case '6': case '7':
5231 if (flags & (ESCAPE_CONTROL|ESCAPE_META)) goto eof;
5233 int numlen;
5234 int oct;
5236 oct = scan_oct(--lex_p, 3, &numlen);
5237 if (numlen == 0) goto eof;
5238 lex_p += numlen;
5239 tokcopy(numlen + 1);
5241 return 0;
5243 case 'x': /* hex constant */
5244 if (flags & (ESCAPE_CONTROL|ESCAPE_META)) goto eof;
5246 int numlen;
5247 int hex;
5249 hex = tok_hex(&numlen);
5250 if (numlen == 0) goto eof;
5251 tokcopy(numlen + 2);
5253 return 0;
5255 case 'M':
5256 if (flags & ESCAPE_META) goto eof;
5257 if ((c = nextc()) != '-') {
5258 pushback(c);
5259 goto eof;
5261 tokcopy(3);
5262 flags |= ESCAPE_META;
5263 goto escaped;
5265 case 'C':
5266 if (flags & ESCAPE_CONTROL) goto eof;
5267 if ((c = nextc()) != '-') {
5268 pushback(c);
5269 goto eof;
5271 tokcopy(3);
5272 goto escaped;
5274 case 'c':
5275 if (flags & ESCAPE_CONTROL) goto eof;
5276 tokcopy(2);
5277 flags |= ESCAPE_CONTROL;
5278 escaped:
5279 if ((c = nextc()) == '\\') {
5280 goto first;
5282 else if (c == -1) goto eof;
5283 tokadd(c);
5284 return 0;
5286 eof:
5287 case -1:
5288 yyerror("Invalid escape character syntax");
5289 return -1;
5291 default:
5292 tokadd('\\');
5293 tokadd(c);
5295 return 0;
5298 extern int rb_char_to_option_kcode(int c, int *option, int *kcode);
5300 static int
5301 parser_regx_options(struct parser_params *parser)
5303 int kcode = 0;
5304 int options = 0;
5305 int c, opt, kc;
5307 newtok();
5308 while (c = nextc(), ISALPHA(c)) {
5309 if (c == 'o') {
5310 options |= RE_OPTION_ONCE;
5312 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
5313 options |= opt;
5314 if (kc >= 0) kcode = c;
5316 else {
5317 tokadd(c);
5320 pushback(c);
5321 if (toklen()) {
5322 tokfix();
5323 compile_error(PARSER_ARG "unknown regexp option%s - %s",
5324 toklen() > 1 ? "s" : "", tok());
5326 return options | RE_OPTION_ENCODING(kcode);
5329 static void
5330 dispose_string(VALUE str)
5332 /* TODO: should use another API? */
5333 if (RBASIC(str)->flags & RSTRING_NOEMBED)
5334 xfree(RSTRING_PTR(str));
5335 rb_gc_force_recycle(str);
5338 static int
5339 parser_tokadd_mbchar(struct parser_params *parser, int c)
5341 int len = parser_precise_mbclen();
5342 if (!MBCLEN_CHARFOUND_P(len)) {
5343 compile_error(PARSER_ARG "invalid multibyte char");
5344 return -1;
5346 tokadd(c);
5347 lex_p += --len;
5348 if (len > 0) tokcopy(len);
5349 return c;
5352 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, c)
5354 static int
5355 parser_tokadd_string(struct parser_params *parser,
5356 int func, int term, int paren, long *nest,
5357 rb_encoding **encp)
5359 int c;
5360 int has_nonascii = 0;
5361 rb_encoding *enc = *encp;
5362 char *errbuf = 0;
5363 static const char mixed_msg[] = "%s mixed within %s source";
5365 #define mixed_error(enc1, enc2) if (!errbuf) { \
5366 int len = sizeof(mixed_msg) - 4; \
5367 len += strlen(rb_enc_name(enc1)); \
5368 len += strlen(rb_enc_name(enc2)); \
5369 errbuf = ALLOCA_N(char, len); \
5370 snprintf(errbuf, len, mixed_msg, \
5371 rb_enc_name(enc1), \
5372 rb_enc_name(enc2)); \
5373 yyerror(errbuf); \
5375 #define mixed_escape(beg, enc1, enc2) do { \
5376 const char *pos = lex_p; \
5377 lex_p = beg; \
5378 mixed_error(enc1, enc2); \
5379 lex_p = pos; \
5380 } while (0)
5382 while ((c = nextc()) != -1) {
5383 if (paren && c == paren) {
5384 ++*nest;
5386 else if (c == term) {
5387 if (!nest || !*nest) {
5388 pushback(c);
5389 break;
5391 --*nest;
5393 else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) {
5394 int c2 = *lex_p;
5395 if (c2 == '$' || c2 == '@' || c2 == '{') {
5396 pushback(c);
5397 break;
5400 else if (c == '\\') {
5401 const char *beg = lex_p - 1;
5402 c = nextc();
5403 switch (c) {
5404 case '\n':
5405 if (func & STR_FUNC_QWORDS) break;
5406 if (func & STR_FUNC_EXPAND) continue;
5407 tokadd('\\');
5408 break;
5410 case '\\':
5411 if (func & STR_FUNC_ESCAPE) tokadd(c);
5412 break;
5414 case 'u':
5415 if ((func & STR_FUNC_EXPAND) == 0) {
5416 tokadd('\\');
5417 break;
5419 parser_tokadd_utf8(parser, &enc, 1,
5420 func & STR_FUNC_SYMBOL,
5421 func & STR_FUNC_REGEXP);
5422 if (has_nonascii && enc != *encp) {
5423 mixed_escape(beg, enc, *encp);
5425 continue;
5427 default:
5428 if (func & STR_FUNC_REGEXP) {
5429 pushback(c);
5430 if ((c = tokadd_escape(&enc)) < 0)
5431 return -1;
5432 if (has_nonascii && enc != *encp) {
5433 mixed_escape(beg, enc, *encp);
5435 continue;
5437 else if (func & STR_FUNC_EXPAND) {
5438 pushback(c);
5439 if (func & STR_FUNC_ESCAPE) tokadd('\\');
5440 c = read_escape(0, &enc);
5442 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
5443 /* ignore backslashed spaces in %w */
5445 else if (c != term && !(paren && c == paren)) {
5446 tokadd('\\');
5450 else if (!parser_isascii()) {
5451 has_nonascii = 1;
5452 if (enc != *encp) {
5453 mixed_error(enc, *encp);
5454 continue;
5456 if (tokadd_mbchar(c) == -1) return -1;
5457 continue;
5459 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
5460 pushback(c);
5461 break;
5463 if (!c && (func & STR_FUNC_SYMBOL)) {
5464 func &= ~STR_FUNC_SYMBOL;
5465 compile_error(PARSER_ARG "symbol cannot contain '\\0'");
5466 continue;
5468 if (c & 0x80) {
5469 has_nonascii = 1;
5470 if (enc != *encp) {
5471 mixed_error(enc, *encp);
5472 continue;
5475 tokadd(c);
5477 *encp = enc;
5478 return c;
5481 #define NEW_STRTERM(func, term, paren) \
5482 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
5484 static int
5485 parser_parse_string(struct parser_params *parser, NODE *quote)
5487 int func = quote->nd_func;
5488 int term = nd_term(quote);
5489 int paren = nd_paren(quote);
5490 int c, space = 0;
5491 rb_encoding *enc = parser->enc;
5493 if (func == -1) return tSTRING_END;
5494 c = nextc();
5495 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
5496 do {c = nextc();} while (ISSPACE(c));
5497 space = 1;
5499 if (c == term && !quote->nd_nest) {
5500 if (func & STR_FUNC_QWORDS) {
5501 quote->nd_func = -1;
5502 return ' ';
5504 if (!(func & STR_FUNC_REGEXP)) return tSTRING_END;
5505 set_yylval_num(regx_options());
5506 return tREGEXP_END;
5508 if (space) {
5509 pushback(c);
5510 return ' ';
5512 newtok();
5513 if ((func & STR_FUNC_EXPAND) && c == '#') {
5514 switch (c = nextc()) {
5515 case '$':
5516 case '@':
5517 pushback(c);
5518 return tSTRING_DVAR;
5519 case '{':
5520 return tSTRING_DBEG;
5522 tokadd('#');
5524 pushback(c);
5525 if (tokadd_string(func, term, paren, &quote->nd_nest,
5526 &enc) == -1) {
5527 ruby_sourceline = nd_line(quote);
5528 if (func & STR_FUNC_REGEXP) {
5529 if (parser->eofp)
5530 compile_error(PARSER_ARG "unterminated regexp meets end of file");
5531 return tREGEXP_END;
5533 else {
5534 if (parser->eofp)
5535 compile_error(PARSER_ARG "unterminated string meets end of file");
5536 return tSTRING_END;
5540 tokfix();
5541 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
5542 return tSTRING_CONTENT;
5545 static int
5546 parser_heredoc_identifier(struct parser_params *parser)
5548 int c = nextc(), term, func = 0, len;
5550 if (c == '-') {
5551 c = nextc();
5552 func = STR_FUNC_INDENT;
5554 switch (c) {
5555 case '\'':
5556 func |= str_squote; goto quoted;
5557 case '"':
5558 func |= str_dquote; goto quoted;
5559 case '`':
5560 func |= str_xquote;
5561 quoted:
5562 newtok();
5563 tokadd(func);
5564 term = c;
5565 while ((c = nextc()) != -1 && c != term) {
5566 if (tokadd_mbchar(c) == -1) return 0;
5568 if (c == -1) {
5569 compile_error(PARSER_ARG "unterminated here document identifier");
5570 return 0;
5572 break;
5574 default:
5575 if (!parser_is_identchar()) {
5576 pushback(c);
5577 if (func & STR_FUNC_INDENT) {
5578 pushback('-');
5580 return 0;
5582 newtok();
5583 term = '"';
5584 tokadd(func |= str_dquote);
5585 do {
5586 if (tokadd_mbchar(c) == -1) return 0;
5587 } while ((c = nextc()) != -1 && parser_is_identchar());
5588 pushback(c);
5589 break;
5592 tokfix();
5593 #ifdef RIPPER
5594 ripper_dispatch_scan_event(parser, tHEREDOC_BEG);
5595 #endif
5596 len = lex_p - lex_pbeg;
5597 lex_goto_eol(parser);
5598 lex_strterm = rb_node_newnode(NODE_HEREDOC,
5599 STR_NEW(tok(), toklen()), /* nd_lit */
5600 len, /* nd_nth */
5601 lex_lastline); /* nd_orig */
5602 nd_set_line(lex_strterm, ruby_sourceline);
5603 #ifdef RIPPER
5604 ripper_flush(parser);
5605 #endif
5606 return term == '`' ? tXSTRING_BEG : tSTRING_BEG;
5609 static void
5610 parser_heredoc_restore(struct parser_params *parser, NODE *here)
5612 VALUE line;
5614 #ifdef RIPPER
5615 if (!NIL_P(parser->delayed))
5616 ripper_dispatch_delayed_token(parser, tSTRING_CONTENT);
5617 lex_goto_eol(parser);
5618 ripper_dispatch_scan_event(parser, tHEREDOC_END);
5619 #endif
5620 line = here->nd_orig;
5621 lex_lastline = line;
5622 lex_pbeg = RSTRING_PTR(line);
5623 lex_pend = lex_pbeg + RSTRING_LEN(line);
5624 lex_p = lex_pbeg + here->nd_nth;
5625 heredoc_end = ruby_sourceline;
5626 ruby_sourceline = nd_line(here);
5627 dispose_string(here->nd_lit);
5628 rb_gc_force_recycle((VALUE)here);
5629 #ifdef RIPPER
5630 ripper_flush(parser);
5631 #endif
5634 static int
5635 parser_whole_match_p(struct parser_params *parser,
5636 const char *eos, int len, int indent)
5638 const char *p = lex_pbeg;
5639 int n;
5641 if (indent) {
5642 while (*p && ISSPACE(*p)) p++;
5644 n= lex_pend - (p + len);
5645 if (n < 0 || (n > 0 && p[len] != '\n' && p[len] != '\r')) return Qfalse;
5646 if (strncmp(eos, p, len) == 0) return Qtrue;
5647 return Qfalse;
5650 static int
5651 parser_here_document(struct parser_params *parser, NODE *here)
5653 int c, func, indent = 0;
5654 const char *eos, *p, *pend;
5655 long len;
5656 VALUE str = 0;
5658 eos = RSTRING_PTR(here->nd_lit);
5659 len = RSTRING_LEN(here->nd_lit) - 1;
5660 indent = (func = *eos++) & STR_FUNC_INDENT;
5662 if ((c = nextc()) == -1) {
5663 error:
5664 compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos);
5665 restore:
5666 heredoc_restore(lex_strterm);
5667 lex_strterm = 0;
5668 return 0;
5670 if (was_bol() && whole_match_p(eos, len, indent)) {
5671 heredoc_restore(lex_strterm);
5672 return tSTRING_END;
5675 if (!(func & STR_FUNC_EXPAND)) {
5676 do {
5677 p = RSTRING_PTR(lex_lastline);
5678 pend = lex_pend;
5679 if (pend > p) {
5680 switch (pend[-1]) {
5681 case '\n':
5682 if (--pend == p || pend[-1] != '\r') {
5683 pend++;
5684 break;
5686 case '\r':
5687 --pend;
5690 if (str)
5691 rb_str_cat(str, p, pend - p);
5692 else
5693 str = STR_NEW(p, pend - p);
5694 if (pend < lex_pend) rb_str_cat(str, "\n", 1);
5695 lex_goto_eol(parser);
5696 if (nextc() == -1) {
5697 if (str) dispose_string(str);
5698 goto error;
5700 } while (!whole_match_p(eos, len, indent));
5702 else {
5703 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
5704 rb_encoding *enc = parser->enc;
5705 newtok();
5706 if (c == '#') {
5707 switch (c = nextc()) {
5708 case '$':
5709 case '@':
5710 pushback(c);
5711 return tSTRING_DVAR;
5712 case '{':
5713 return tSTRING_DBEG;
5715 tokadd('#');
5717 do {
5718 pushback(c);
5719 if ((c = tokadd_string(func, '\n', 0, NULL, &enc)) == -1) {
5720 if (parser->eofp) goto error;
5721 goto restore;
5723 if (c != '\n') {
5724 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
5725 return tSTRING_CONTENT;
5727 tokadd(nextc());
5728 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
5729 if ((c = nextc()) == -1) goto error;
5730 } while (!whole_match_p(eos, len, indent));
5731 str = STR_NEW3(tok(), toklen(), enc, func);
5733 heredoc_restore(lex_strterm);
5734 lex_strterm = NEW_STRTERM(-1, 0, 0);
5735 set_yylval_str(str);
5736 return tSTRING_CONTENT;
5739 #include "lex.c"
5741 #ifndef RIPPER
5742 static void
5743 arg_ambiguous_gen(struct parser_params *parser)
5745 rb_warning0("ambiguous first argument; put parentheses or even spaces");
5747 #else
5748 static void
5749 arg_ambiguous_gen(struct parser_params *parser)
5751 dispatch0(arg_ambiguous);
5753 #endif
5754 #define arg_ambiguous() arg_ambiguous_gen(parser)
5756 static int
5757 lvar_defined_gen(struct parser_params *parser, ID id)
5759 #ifndef RIPPER
5760 return (dyna_in_block() && dvar_defined(id)) || local_id(id);
5761 #else
5762 return 0;
5763 #endif
5766 /* emacsen -*- hack */
5767 static int
5768 parser_encode_length(struct parser_params *parser, const char *name, int len)
5770 int nlen;
5772 if (len > 5 && name[nlen = len - 5] == '-') {
5773 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
5774 return nlen;
5776 if (len > 4 && name[nlen = len - 5] == '-') {
5777 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
5778 return nlen;
5779 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0)
5780 return nlen;
5782 return len;
5785 static void
5786 parser_set_encode(struct parser_params *parser, const char *name)
5788 int idx = rb_enc_find_index(name);
5789 rb_encoding *enc;
5791 if (idx < 0) {
5792 rb_raise(rb_eArgError, "unknown encoding name: %s", name);
5794 enc = rb_enc_from_index(idx);
5795 if (!rb_enc_asciicompat(enc)) {
5796 rb_raise(rb_eArgError, "%s is not ASCII compatible", rb_enc_name(enc));
5798 parser->enc = enc;
5801 #ifndef RIPPER
5802 typedef int (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, int len);
5803 typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val);
5805 static void
5806 magic_comment_encoding(struct parser_params *parser, const char *name, const char *val)
5808 if (parser->line_count != (parser->has_shebang ? 2 : 1))
5809 return;
5810 parser_set_encode(parser, val);
5813 struct magic_comment {
5814 const char *name;
5815 rb_magic_comment_setter_t func;
5816 rb_magic_comment_length_t length;
5819 static const struct magic_comment magic_comments[] = {
5820 {"coding", magic_comment_encoding, parser_encode_length},
5821 {"encoding", magic_comment_encoding, parser_encode_length},
5823 #endif
5825 static const char *
5826 magic_comment_marker(const char *str, int len)
5828 int i = 2;
5830 while (i < len) {
5831 switch (str[i]) {
5832 case '-':
5833 if (str[i-1] == '*' && str[i-2] == '-') {
5834 return str + i + 1;
5836 i += 2;
5837 break;
5838 case '*':
5839 if (i + 1 >= len) return 0;
5840 if (str[i+1] != '-') {
5841 i += 4;
5843 else if (str[i-1] != '-') {
5844 i += 2;
5846 else {
5847 return str + i + 2;
5849 break;
5850 default:
5851 i += 3;
5852 break;
5855 return 0;
5858 static int
5859 parser_magic_comment(struct parser_params *parser, const char *str, int len)
5861 VALUE name = 0, val = 0;
5862 const char *beg, *end, *vbeg, *vend;
5863 #define str_copy(_s, _p, _n) ((_s) \
5864 ? (rb_str_resize((_s), (_n)), \
5865 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
5866 : ((_s) = STR_NEW((_p), (_n))))
5868 if (len <= 7) return Qfalse;
5869 if (!(beg = magic_comment_marker(str, len))) return Qfalse;
5870 if (!(end = magic_comment_marker(beg, str + len - beg))) return Qfalse;
5871 str = beg;
5872 len = end - beg - 3;
5874 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
5875 while (len > 0) {
5876 #ifndef RIPPER
5877 const struct magic_comment *p = magic_comments;
5878 #endif
5879 int n = 0;
5881 for (; len > 0 && *str; str++, --len) {
5882 switch (*str) {
5883 case '\'': case '"': case ':': case ';':
5884 continue;
5886 if (!ISSPACE(*str)) break;
5888 for (beg = str; len > 0; str++, --len) {
5889 switch (*str) {
5890 case '\'': case '"': case ':': case ';':
5891 break;
5892 default:
5893 if (ISSPACE(*str)) break;
5894 continue;
5896 break;
5898 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
5899 if (!len) break;
5900 if (*str != ':') continue;
5902 do str++; while (--len > 0 && ISSPACE(*str));
5903 if (!len) break;
5904 if (*str == '"') {
5905 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
5906 if (*str == '\\') {
5907 --len;
5908 ++str;
5911 vend = str;
5912 if (len) {
5913 --len;
5914 ++str;
5917 else {
5918 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
5919 vend = str;
5921 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
5923 n = end - beg;
5924 str_copy(name, beg, n);
5925 #ifndef RIPPER
5926 do {
5927 if (STRNCASECMP(p->name, RSTRING_PTR(name), n) == 0) {
5928 n = vend - vbeg;
5929 if (p->length) {
5930 n = (*p->length)(parser, vbeg, n);
5932 str_copy(val, vbeg, n);
5933 (*p->func)(parser, RSTRING_PTR(name), RSTRING_PTR(val));
5934 break;
5936 } while (++p < magic_comments + sizeof(magic_comments) / sizeof(*p));
5937 #else
5938 dispatch2(magic_comment, name, val);
5939 #endif
5942 return Qtrue;
5945 static void
5946 set_file_encoding(struct parser_params *parser, const char *str, const char *send)
5948 int sep = 0;
5949 const char *beg = str;
5950 VALUE s;
5952 for (;;) {
5953 if (send - str <= 6) return;
5954 switch (str[6]) {
5955 case 'C': case 'c': str += 6; continue;
5956 case 'O': case 'o': str += 5; continue;
5957 case 'D': case 'd': str += 4; continue;
5958 case 'I': case 'i': str += 3; continue;
5959 case 'N': case 'n': str += 2; continue;
5960 case 'G': case 'g': str += 1; continue;
5961 case '=': case ':':
5962 sep = 1;
5963 str += 6;
5964 break;
5965 default:
5966 str += 6;
5967 if (ISSPACE(*str)) break;
5968 continue;
5970 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
5972 for (;;) {
5973 do {
5974 if (++str >= send) return;
5975 } while (ISSPACE(*str));
5976 if (sep) break;
5977 if (*str != '=' && *str != ':') return;
5978 sep = 1;
5979 str++;
5981 beg = str;
5982 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
5983 s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg));
5984 parser_set_encode(parser, RSTRING_PTR(s));
5985 rb_str_resize(s, 0);
5988 static void
5989 parser_prepare(struct parser_params *parser)
5991 int c = nextc();
5992 switch (c) {
5993 case '#':
5994 if (peek('!')) parser->has_shebang = 1;
5995 break;
5996 case 0xef: /* UTF-8 BOM marker */
5997 if (lex_pend - lex_p >= 2 &&
5998 (unsigned char)lex_p[0] == 0xbb &&
5999 (unsigned char)lex_p[1] == 0xbf) {
6000 parser_set_encode(parser, "UTF-8");
6001 lex_p += 2;
6002 lex_pbeg = lex_p;
6003 return;
6005 break;
6006 case EOF:
6007 return;
6009 pushback(c);
6010 parser->enc = rb_enc_get(lex_lastline);
6013 #define IS_ARG() (lex_state == EXPR_ARG || lex_state == EXPR_CMDARG)
6014 #define IS_BEG() (lex_state == EXPR_BEG || lex_state == EXPR_MID || lex_state == EXPR_VALUE || lex_state == EXPR_CLASS)
6016 static int
6017 parser_yylex(struct parser_params *parser)
6019 register int c;
6020 int space_seen = 0;
6021 int cmd_state;
6022 enum lex_state_e last_state;
6023 rb_encoding *enc;
6024 int mb;
6025 #ifdef RIPPER
6026 int fallthru = Qfalse;
6027 #endif
6029 if (lex_strterm) {
6030 int token;
6031 if (nd_type(lex_strterm) == NODE_HEREDOC) {
6032 token = here_document(lex_strterm);
6033 if (token == tSTRING_END) {
6034 lex_strterm = 0;
6035 lex_state = EXPR_ENDARG;
6038 else {
6039 token = parse_string(lex_strterm);
6040 if (token == tSTRING_END || token == tREGEXP_END) {
6041 rb_gc_force_recycle((VALUE)lex_strterm);
6042 lex_strterm = 0;
6043 lex_state = EXPR_ENDARG;
6046 return token;
6048 cmd_state = command_start;
6049 command_start = Qfalse;
6050 retry:
6051 switch (c = nextc()) {
6052 case '\0': /* NUL */
6053 case '\004': /* ^D */
6054 case '\032': /* ^Z */
6055 case -1: /* end of script. */
6056 return 0;
6058 /* white spaces */
6059 case ' ': case '\t': case '\f': case '\r':
6060 case '\13': /* '\v' */
6061 space_seen++;
6062 #ifdef RIPPER
6063 while ((c = nextc())) {
6064 switch (c) {
6065 case ' ': case '\t': case '\f': case '\r':
6066 case '\13': /* '\v' */
6067 break;
6068 default:
6069 goto outofloop;
6072 outofloop:
6073 pushback(c);
6074 ripper_dispatch_scan_event(parser, tSP);
6075 #endif
6076 goto retry;
6078 case '#': /* it's a comment */
6079 /* no magic_comment in shebang line */
6080 if (parser->line_count == (parser->has_shebang ? 2 : 1)
6081 && (lex_p - lex_pbeg) == 1) {
6082 if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
6083 set_file_encoding(parser, lex_p, lex_pend);
6086 lex_p = lex_pend;
6087 #ifdef RIPPER
6088 ripper_dispatch_scan_event(parser, tCOMMENT);
6089 fallthru = Qtrue;
6090 #endif
6091 /* fall through */
6092 case '\n':
6093 switch (lex_state) {
6094 case EXPR_BEG:
6095 case EXPR_FNAME:
6096 case EXPR_DOT:
6097 case EXPR_CLASS:
6098 case EXPR_VALUE:
6099 #ifdef RIPPER
6100 if (!fallthru) {
6101 ripper_dispatch_scan_event(parser, tIGNORED_NL);
6103 fallthru = Qfalse;
6104 #endif
6105 goto retry;
6106 default:
6107 break;
6109 while ((c = nextc())) {
6110 switch (c) {
6111 case ' ': case '\t': case '\f': case '\r':
6112 case '\13': /* '\v' */
6113 space_seen++;
6114 break;
6115 case '.': {
6116 if ((c = nextc()) != '.') {
6117 pushback(c);
6118 pushback('.');
6119 goto retry;
6122 default:
6123 --ruby_sourceline;
6124 lex_nextline = lex_lastline;
6125 case -1: /* EOF no decrement*/
6126 lex_goto_eol(parser);
6127 #ifdef RIPPER
6128 if (c != -1) {
6129 parser->tokp = lex_p;
6131 #endif
6132 goto normal_newline;
6135 normal_newline:
6136 command_start = Qtrue;
6137 lex_state = EXPR_BEG;
6138 return '\n';
6140 case '*':
6141 if ((c = nextc()) == '*') {
6142 if ((c = nextc()) == '=') {
6143 set_yylval_id(tPOW);
6144 lex_state = EXPR_BEG;
6145 return tOP_ASGN;
6147 pushback(c);
6148 c = tPOW;
6150 else {
6151 if (c == '=') {
6152 set_yylval_id('*');
6153 lex_state = EXPR_BEG;
6154 return tOP_ASGN;
6156 pushback(c);
6157 if (IS_ARG() && space_seen && !ISSPACE(c)) {
6158 rb_warning0("`*' interpreted as argument prefix");
6159 c = tSTAR;
6161 else if (IS_BEG()) {
6162 c = tSTAR;
6164 else {
6165 c = '*';
6168 switch (lex_state) {
6169 case EXPR_FNAME: case EXPR_DOT:
6170 lex_state = EXPR_ARG; break;
6171 default:
6172 lex_state = EXPR_BEG; break;
6174 return c;
6176 case '!':
6177 c = nextc();
6178 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
6179 lex_state = EXPR_ARG;
6180 if (c == '@') {
6181 return '!';
6184 else {
6185 lex_state = EXPR_BEG;
6187 if (c == '=') {
6188 return tNEQ;
6190 if (c == '~') {
6191 return tNMATCH;
6193 pushback(c);
6194 return '!';
6196 case '=':
6197 if (was_bol()) {
6198 /* skip embedded rd document */
6199 if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) {
6200 #ifdef RIPPER
6201 int first_p = Qtrue;
6203 lex_goto_eol(parser);
6204 ripper_dispatch_scan_event(parser, tEMBDOC_BEG);
6205 #endif
6206 for (;;) {
6207 lex_goto_eol(parser);
6208 #ifdef RIPPER
6209 if (!first_p) {
6210 ripper_dispatch_scan_event(parser, tEMBDOC);
6212 first_p = Qfalse;
6213 #endif
6214 c = nextc();
6215 if (c == -1) {
6216 compile_error(PARSER_ARG "embedded document meets end of file");
6217 return 0;
6219 if (c != '=') continue;
6220 if (strncmp(lex_p, "end", 3) == 0 &&
6221 (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
6222 break;
6225 lex_goto_eol(parser);
6226 #ifdef RIPPER
6227 ripper_dispatch_scan_event(parser, tEMBDOC_END);
6228 #endif
6229 goto retry;
6233 switch (lex_state) {
6234 case EXPR_FNAME: case EXPR_DOT:
6235 lex_state = EXPR_ARG; break;
6236 default:
6237 lex_state = EXPR_BEG; break;
6239 if ((c = nextc()) == '=') {
6240 if ((c = nextc()) == '=') {
6241 return tEQQ;
6243 pushback(c);
6244 return tEQ;
6246 if (c == '~') {
6247 return tMATCH;
6249 else if (c == '>') {
6250 return tASSOC;
6252 pushback(c);
6253 return '=';
6255 case '<':
6256 c = nextc();
6257 if (c == '<' &&
6258 lex_state != EXPR_END &&
6259 lex_state != EXPR_DOT &&
6260 lex_state != EXPR_ENDARG &&
6261 lex_state != EXPR_CLASS &&
6262 (!IS_ARG() || space_seen)) {
6263 int token = heredoc_identifier();
6264 if (token) return token;
6266 switch (lex_state) {
6267 case EXPR_FNAME: case EXPR_DOT:
6268 lex_state = EXPR_ARG; break;
6269 default:
6270 lex_state = EXPR_BEG; break;
6272 if (c == '=') {
6273 if ((c = nextc()) == '>') {
6274 return tCMP;
6276 pushback(c);
6277 return tLEQ;
6279 if (c == '<') {
6280 if ((c = nextc()) == '=') {
6281 set_yylval_id(tLSHFT);
6282 lex_state = EXPR_BEG;
6283 return tOP_ASGN;
6285 pushback(c);
6286 return tLSHFT;
6288 pushback(c);
6289 return '<';
6291 case '>':
6292 switch (lex_state) {
6293 case EXPR_FNAME: case EXPR_DOT:
6294 lex_state = EXPR_ARG; break;
6295 default:
6296 lex_state = EXPR_BEG; break;
6298 if ((c = nextc()) == '=') {
6299 return tGEQ;
6301 if (c == '>') {
6302 if ((c = nextc()) == '=') {
6303 set_yylval_id(tRSHFT);
6304 lex_state = EXPR_BEG;
6305 return tOP_ASGN;
6307 pushback(c);
6308 return tRSHFT;
6310 pushback(c);
6311 return '>';
6313 case '"':
6314 lex_strterm = NEW_STRTERM(str_dquote, '"', 0);
6315 return tSTRING_BEG;
6317 case '`':
6318 if (lex_state == EXPR_FNAME) {
6319 lex_state = EXPR_END;
6320 return c;
6322 if (lex_state == EXPR_DOT) {
6323 if (cmd_state)
6324 lex_state = EXPR_CMDARG;
6325 else
6326 lex_state = EXPR_ARG;
6327 return c;
6329 lex_strterm = NEW_STRTERM(str_xquote, '`', 0);
6330 return tXSTRING_BEG;
6332 case '\'':
6333 lex_strterm = NEW_STRTERM(str_squote, '\'', 0);
6334 return tSTRING_BEG;
6336 case '?':
6337 if (lex_state == EXPR_END ||
6338 lex_state == EXPR_ENDARG) {
6339 lex_state = EXPR_VALUE;
6340 return '?';
6342 c = nextc();
6343 if (c == -1) {
6344 compile_error(PARSER_ARG "incomplete character syntax");
6345 return 0;
6347 if (rb_enc_isspace(c, parser->enc)) {
6348 if (!IS_ARG()) {
6349 int c2 = 0;
6350 switch (c) {
6351 case ' ':
6352 c2 = 's';
6353 break;
6354 case '\n':
6355 c2 = 'n';
6356 break;
6357 case '\t':
6358 c2 = 't';
6359 break;
6360 case '\v':
6361 c2 = 'v';
6362 break;
6363 case '\r':
6364 c2 = 'r';
6365 break;
6366 case '\f':
6367 c2 = 'f';
6368 break;
6370 if (c2) {
6371 rb_warnI("invalid character syntax; use ?\\%c", c2);
6374 ternary:
6375 pushback(c);
6376 lex_state = EXPR_VALUE;
6377 return '?';
6379 newtok();
6380 enc = parser->enc;
6381 if (!parser_isascii()) {
6382 if (tokadd_mbchar(c) == -1) return 0;
6384 else if ((rb_enc_isalnum(c, parser->enc) || c == '_') &&
6385 lex_p < lex_pend && is_identchar(lex_p, lex_pend, parser->enc)) {
6386 goto ternary;
6388 else if (c == '\\') {
6389 if (peek('u')) {
6390 nextc();
6391 c = parser_tokadd_utf8(parser, &enc, 0, 0, 0);
6392 if (0x80 <= c) {
6393 tokaddmbc(c, enc);
6395 else {
6396 tokadd(c);
6399 else {
6400 c = read_escape(0, &enc);
6401 tokadd(c);
6404 else {
6405 tokadd(c);
6407 tokfix();
6408 set_yylval_str(STR_NEW3(tok(), toklen(), enc, 0));
6409 lex_state = EXPR_ENDARG;
6410 return tCHAR;
6412 case '&':
6413 if ((c = nextc()) == '&') {
6414 lex_state = EXPR_BEG;
6415 if ((c = nextc()) == '=') {
6416 set_yylval_id(tANDOP);
6417 lex_state = EXPR_BEG;
6418 return tOP_ASGN;
6420 pushback(c);
6421 return tANDOP;
6423 else if (c == '=') {
6424 set_yylval_id('&');
6425 lex_state = EXPR_BEG;
6426 return tOP_ASGN;
6428 pushback(c);
6429 if (IS_ARG() && space_seen && !ISSPACE(c)) {
6430 rb_warning0("`&' interpreted as argument prefix");
6431 c = tAMPER;
6433 else if (IS_BEG()) {
6434 c = tAMPER;
6436 else {
6437 c = '&';
6439 switch (lex_state) {
6440 case EXPR_FNAME: case EXPR_DOT:
6441 lex_state = EXPR_ARG; break;
6442 default:
6443 lex_state = EXPR_BEG;
6445 return c;
6447 case '|':
6448 if ((c = nextc()) == '|') {
6449 lex_state = EXPR_BEG;
6450 if ((c = nextc()) == '=') {
6451 set_yylval_id(tOROP);
6452 lex_state = EXPR_BEG;
6453 return tOP_ASGN;
6455 pushback(c);
6456 return tOROP;
6458 if (c == '=') {
6459 set_yylval_id('|');
6460 lex_state = EXPR_BEG;
6461 return tOP_ASGN;
6463 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
6464 lex_state = EXPR_ARG;
6466 else {
6467 lex_state = EXPR_BEG;
6469 pushback(c);
6470 return '|';
6472 case '+':
6473 c = nextc();
6474 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
6475 lex_state = EXPR_ARG;
6476 if (c == '@') {
6477 return tUPLUS;
6479 pushback(c);
6480 return '+';
6482 if (c == '=') {
6483 set_yylval_id('+');
6484 lex_state = EXPR_BEG;
6485 return tOP_ASGN;
6487 if (IS_BEG() ||
6488 (IS_ARG() && space_seen && !ISSPACE(c))) {
6489 if (IS_ARG()) arg_ambiguous();
6490 lex_state = EXPR_BEG;
6491 pushback(c);
6492 if (ISDIGIT(c)) {
6493 c = '+';
6494 goto start_num;
6496 return tUPLUS;
6498 lex_state = EXPR_BEG;
6499 pushback(c);
6500 return '+';
6502 case '-':
6503 c = nextc();
6504 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
6505 lex_state = EXPR_ARG;
6506 if (c == '@') {
6507 return tUMINUS;
6509 pushback(c);
6510 return '-';
6512 if (c == '=') {
6513 set_yylval_id('-');
6514 lex_state = EXPR_BEG;
6515 return tOP_ASGN;
6517 if (c == '>') {
6518 lex_state = EXPR_ARG;
6519 return tLAMBDA;
6521 if (IS_BEG() ||
6522 (IS_ARG() && space_seen && !ISSPACE(c))) {
6523 if (IS_ARG()) arg_ambiguous();
6524 lex_state = EXPR_BEG;
6525 pushback(c);
6526 if (ISDIGIT(c)) {
6527 return tUMINUS_NUM;
6529 return tUMINUS;
6531 lex_state = EXPR_BEG;
6532 pushback(c);
6533 return '-';
6535 case '.':
6536 lex_state = EXPR_BEG;
6537 if ((c = nextc()) == '.') {
6538 if ((c = nextc()) == '.') {
6539 return tDOT3;
6541 pushback(c);
6542 return tDOT2;
6544 pushback(c);
6545 if (ISDIGIT(c)) {
6546 yyerror("no .<digit> floating literal anymore; put 0 before dot");
6548 lex_state = EXPR_DOT;
6549 return '.';
6551 start_num:
6552 case '0': case '1': case '2': case '3': case '4':
6553 case '5': case '6': case '7': case '8': case '9':
6555 int is_float, seen_point, seen_e, nondigit;
6557 is_float = seen_point = seen_e = nondigit = 0;
6558 lex_state = EXPR_ENDARG;
6559 newtok();
6560 if (c == '-' || c == '+') {
6561 tokadd(c);
6562 c = nextc();
6564 if (c == '0') {
6565 int start = toklen();
6566 c = nextc();
6567 if (c == 'x' || c == 'X') {
6568 /* hexadecimal */
6569 c = nextc();
6570 if (ISXDIGIT(c)) {
6571 do {
6572 if (c == '_') {
6573 if (nondigit) break;
6574 nondigit = c;
6575 continue;
6577 if (!ISXDIGIT(c)) break;
6578 nondigit = 0;
6579 tokadd(c);
6580 } while ((c = nextc()) != -1);
6582 pushback(c);
6583 tokfix();
6584 if (toklen() == start) {
6585 yyerror("numeric literal without digits");
6587 else if (nondigit) goto trailing_uc;
6588 set_yylval_literal(rb_cstr_to_inum(tok(), 16, Qfalse));
6589 return tINTEGER;
6591 if (c == 'b' || c == 'B') {
6592 /* binary */
6593 c = nextc();
6594 if (c == '0' || c == '1') {
6595 do {
6596 if (c == '_') {
6597 if (nondigit) break;
6598 nondigit = c;
6599 continue;
6601 if (c != '0' && c != '1') break;
6602 nondigit = 0;
6603 tokadd(c);
6604 } while ((c = nextc()) != -1);
6606 pushback(c);
6607 tokfix();
6608 if (toklen() == start) {
6609 yyerror("numeric literal without digits");
6611 else if (nondigit) goto trailing_uc;
6612 set_yylval_literal(rb_cstr_to_inum(tok(), 2, Qfalse));
6613 return tINTEGER;
6615 if (c == 'd' || c == 'D') {
6616 /* decimal */
6617 c = nextc();
6618 if (ISDIGIT(c)) {
6619 do {
6620 if (c == '_') {
6621 if (nondigit) break;
6622 nondigit = c;
6623 continue;
6625 if (!ISDIGIT(c)) break;
6626 nondigit = 0;
6627 tokadd(c);
6628 } while ((c = nextc()) != -1);
6630 pushback(c);
6631 tokfix();
6632 if (toklen() == start) {
6633 yyerror("numeric literal without digits");
6635 else if (nondigit) goto trailing_uc;
6636 set_yylval_literal(rb_cstr_to_inum(tok(), 10, Qfalse));
6637 return tINTEGER;
6639 if (c == '_') {
6640 /* 0_0 */
6641 goto octal_number;
6643 if (c == 'o' || c == 'O') {
6644 /* prefixed octal */
6645 c = nextc();
6646 if (c == '_' || !ISDIGIT(c)) {
6647 yyerror("numeric literal without digits");
6650 if (c >= '0' && c <= '7') {
6651 /* octal */
6652 octal_number:
6653 do {
6654 if (c == '_') {
6655 if (nondigit) break;
6656 nondigit = c;
6657 continue;
6659 if (c < '0' || c > '9') break;
6660 if (c > '7') goto invalid_octal;
6661 nondigit = 0;
6662 tokadd(c);
6663 } while ((c = nextc()) != -1);
6664 if (toklen() > start) {
6665 pushback(c);
6666 tokfix();
6667 if (nondigit) goto trailing_uc;
6668 set_yylval_literal(rb_cstr_to_inum(tok(), 8, Qfalse));
6669 return tINTEGER;
6671 if (nondigit) {
6672 pushback(c);
6673 goto trailing_uc;
6676 if (c > '7' && c <= '9') {
6677 invalid_octal:
6678 yyerror("Invalid octal digit");
6680 else if (c == '.' || c == 'e' || c == 'E') {
6681 tokadd('0');
6683 else {
6684 pushback(c);
6685 set_yylval_literal(INT2FIX(0));
6686 return tINTEGER;
6690 for (;;) {
6691 switch (c) {
6692 case '0': case '1': case '2': case '3': case '4':
6693 case '5': case '6': case '7': case '8': case '9':
6694 nondigit = 0;
6695 tokadd(c);
6696 break;
6698 case '.':
6699 if (nondigit) goto trailing_uc;
6700 if (seen_point || seen_e) {
6701 goto decode_num;
6703 else {
6704 int c0 = nextc();
6705 if (!ISDIGIT(c0)) {
6706 pushback(c0);
6707 goto decode_num;
6709 c = c0;
6711 tokadd('.');
6712 tokadd(c);
6713 is_float++;
6714 seen_point++;
6715 nondigit = 0;
6716 break;
6718 case 'e':
6719 case 'E':
6720 if (nondigit) {
6721 pushback(c);
6722 c = nondigit;
6723 goto decode_num;
6725 if (seen_e) {
6726 goto decode_num;
6728 tokadd(c);
6729 seen_e++;
6730 is_float++;
6731 nondigit = c;
6732 c = nextc();
6733 if (c != '-' && c != '+') continue;
6734 tokadd(c);
6735 nondigit = c;
6736 break;
6738 case '_': /* `_' in number just ignored */
6739 if (nondigit) goto decode_num;
6740 nondigit = c;
6741 break;
6743 default:
6744 goto decode_num;
6746 c = nextc();
6749 decode_num:
6750 pushback(c);
6751 tokfix();
6752 if (nondigit) {
6753 char tmp[30];
6754 trailing_uc:
6755 sprintf(tmp, "trailing `%c' in number", nondigit);
6756 yyerror(tmp);
6758 if (is_float) {
6759 double d = strtod(tok(), 0);
6760 if (errno == ERANGE) {
6761 rb_warningS("Float %s out of range", tok());
6762 errno = 0;
6764 set_yylval_literal(DOUBLE2NUM(d));
6765 return tFLOAT;
6767 set_yylval_literal(rb_cstr_to_inum(tok(), 10, Qfalse));
6768 return tINTEGER;
6771 case ')':
6772 case ']':
6773 paren_nest--;
6774 case '}':
6775 COND_LEXPOP();
6776 CMDARG_LEXPOP();
6777 if (c == ')')
6778 lex_state = EXPR_END;
6779 else
6780 lex_state = EXPR_ENDARG;
6781 return c;
6783 case ':':
6784 c = nextc();
6785 if (c == ':') {
6786 if (IS_BEG() ||
6787 lex_state == EXPR_CLASS || (IS_ARG() && space_seen)) {
6788 lex_state = EXPR_BEG;
6789 return tCOLON3;
6791 lex_state = EXPR_DOT;
6792 return tCOLON2;
6794 if (lex_state == EXPR_END ||
6795 lex_state == EXPR_ENDARG || ISSPACE(c)) {
6796 pushback(c);
6797 lex_state = EXPR_BEG;
6798 return ':';
6800 switch (c) {
6801 case '\'':
6802 lex_strterm = NEW_STRTERM(str_ssym, c, 0);
6803 break;
6804 case '"':
6805 lex_strterm = NEW_STRTERM(str_dsym, c, 0);
6806 break;
6807 default:
6808 pushback(c);
6809 break;
6811 lex_state = EXPR_FNAME;
6812 return tSYMBEG;
6814 case '/':
6815 if (IS_BEG()) {
6816 lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
6817 return tREGEXP_BEG;
6819 if ((c = nextc()) == '=') {
6820 set_yylval_id('/');
6821 lex_state = EXPR_BEG;
6822 return tOP_ASGN;
6824 pushback(c);
6825 if (IS_ARG() && space_seen) {
6826 if (!ISSPACE(c)) {
6827 arg_ambiguous();
6828 lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
6829 return tREGEXP_BEG;
6832 switch (lex_state) {
6833 case EXPR_FNAME: case EXPR_DOT:
6834 lex_state = EXPR_ARG; break;
6835 default:
6836 lex_state = EXPR_BEG; break;
6838 return '/';
6840 case '^':
6841 if ((c = nextc()) == '=') {
6842 set_yylval_id('^');
6843 lex_state = EXPR_BEG;
6844 return tOP_ASGN;
6846 switch (lex_state) {
6847 case EXPR_FNAME: case EXPR_DOT:
6848 lex_state = EXPR_ARG; break;
6849 default:
6850 lex_state = EXPR_BEG; break;
6852 pushback(c);
6853 return '^';
6855 case ';':
6856 lex_state = EXPR_BEG;
6857 command_start = Qtrue;
6858 return ';';
6860 case ',':
6861 lex_state = EXPR_BEG;
6862 return ',';
6864 case '~':
6865 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
6866 if ((c = nextc()) != '@') {
6867 pushback(c);
6869 lex_state = EXPR_ARG;
6871 else {
6872 lex_state = EXPR_BEG;
6874 return '~';
6876 case '(':
6877 if (IS_BEG()) {
6878 c = tLPAREN;
6880 else if (space_seen) {
6881 if (IS_ARG()) {
6882 c = tLPAREN_ARG;
6885 paren_nest++;
6886 COND_PUSH(0);
6887 CMDARG_PUSH(0);
6888 lex_state = EXPR_BEG;
6889 return c;
6891 case '[':
6892 paren_nest++;
6893 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
6894 lex_state = EXPR_ARG;
6895 if ((c = nextc()) == ']') {
6896 if ((c = nextc()) == '=') {
6897 return tASET;
6899 pushback(c);
6900 return tAREF;
6902 pushback(c);
6903 return '[';
6905 else if (IS_BEG()) {
6906 c = tLBRACK;
6908 else if (IS_ARG() && space_seen) {
6909 c = tLBRACK;
6911 lex_state = EXPR_BEG;
6912 COND_PUSH(0);
6913 CMDARG_PUSH(0);
6914 return c;
6916 case '{':
6917 if (lpar_beg && lpar_beg == paren_nest) {
6918 lex_state = EXPR_BEG;
6919 lpar_beg = 0;
6920 --paren_nest;
6921 return tLAMBEG;
6923 if (IS_ARG() || lex_state == EXPR_END)
6924 c = '{'; /* block (primary) */
6925 else if (lex_state == EXPR_ENDARG)
6926 c = tLBRACE_ARG; /* block (expr) */
6927 else
6928 c = tLBRACE; /* hash */
6929 COND_PUSH(0);
6930 CMDARG_PUSH(0);
6931 lex_state = EXPR_BEG;
6932 if (c != tLBRACE) command_start = Qtrue;
6933 return c;
6935 case '\\':
6936 c = nextc();
6937 if (c == '\n') {
6938 space_seen = 1;
6939 #ifdef RIPPER
6940 ripper_dispatch_scan_event(parser, tSP);
6941 #endif
6942 goto retry; /* skip \\n */
6944 pushback(c);
6945 return '\\';
6947 case '%':
6948 if (IS_BEG()) {
6949 int term;
6950 int paren;
6952 c = nextc();
6953 quotation:
6954 if (!ISALNUM(c)) {
6955 term = c;
6956 c = 'Q';
6958 else {
6959 term = nextc();
6960 if (rb_enc_isalnum(term, parser->enc) || !parser_isascii()) {
6961 yyerror("unknown type of %string");
6962 return 0;
6965 if (c == -1 || term == -1) {
6966 compile_error(PARSER_ARG "unterminated quoted string meets end of file");
6967 return 0;
6969 paren = term;
6970 if (term == '(') term = ')';
6971 else if (term == '[') term = ']';
6972 else if (term == '{') term = '}';
6973 else if (term == '<') term = '>';
6974 else paren = 0;
6976 switch (c) {
6977 case 'Q':
6978 lex_strterm = NEW_STRTERM(str_dquote, term, paren);
6979 return tSTRING_BEG;
6981 case 'q':
6982 lex_strterm = NEW_STRTERM(str_squote, term, paren);
6983 return tSTRING_BEG;
6985 case 'W':
6986 lex_strterm = NEW_STRTERM(str_dword, term, paren);
6987 do {c = nextc();} while (ISSPACE(c));
6988 pushback(c);
6989 return tWORDS_BEG;
6991 case 'w':
6992 lex_strterm = NEW_STRTERM(str_sword, term, paren);
6993 do {c = nextc();} while (ISSPACE(c));
6994 pushback(c);
6995 return tQWORDS_BEG;
6997 case 'x':
6998 lex_strterm = NEW_STRTERM(str_xquote, term, paren);
6999 return tXSTRING_BEG;
7001 case 'r':
7002 lex_strterm = NEW_STRTERM(str_regexp, term, paren);
7003 return tREGEXP_BEG;
7005 case 's':
7006 lex_strterm = NEW_STRTERM(str_ssym, term, paren);
7007 lex_state = EXPR_FNAME;
7008 return tSYMBEG;
7010 default:
7011 yyerror("unknown type of %string");
7012 return 0;
7015 if ((c = nextc()) == '=') {
7016 set_yylval_id('%');
7017 lex_state = EXPR_BEG;
7018 return tOP_ASGN;
7020 if (IS_ARG() && space_seen && !ISSPACE(c)) {
7021 goto quotation;
7023 switch (lex_state) {
7024 case EXPR_FNAME: case EXPR_DOT:
7025 lex_state = EXPR_ARG; break;
7026 default:
7027 lex_state = EXPR_BEG; break;
7029 pushback(c);
7030 return '%';
7032 case '$':
7033 last_state = lex_state;
7034 lex_state = EXPR_ENDARG;
7035 newtok();
7036 c = nextc();
7037 switch (c) {
7038 case '_': /* $_: last read line string */
7039 c = nextc();
7040 if (parser_is_identchar()) {
7041 tokadd('$');
7042 tokadd('_');
7043 break;
7045 pushback(c);
7046 c = '_';
7047 /* fall through */
7048 case '~': /* $~: match-data */
7049 case '*': /* $*: argv */
7050 case '$': /* $$: pid */
7051 case '?': /* $?: last status */
7052 case '!': /* $!: error string */
7053 case '@': /* $@: error position */
7054 case '/': /* $/: input record separator */
7055 case '\\': /* $\: output record separator */
7056 case ';': /* $;: field separator */
7057 case ',': /* $,: output field separator */
7058 case '.': /* $.: last read line number */
7059 case '=': /* $=: ignorecase */
7060 case ':': /* $:: load path */
7061 case '<': /* $<: reading filename */
7062 case '>': /* $>: default output handle */
7063 case '\"': /* $": already loaded files */
7064 tokadd('$');
7065 tokadd(c);
7066 tokfix();
7067 set_yylval_id(rb_intern(tok()));
7068 return tGVAR;
7070 case '-':
7071 tokadd('$');
7072 tokadd(c);
7073 c = nextc();
7074 if (parser_is_identchar()) {
7075 if (tokadd_mbchar(c) == -1) return 0;
7077 else {
7078 pushback(c);
7080 gvar:
7081 tokfix();
7082 set_yylval_id(rb_intern(tok()));
7083 return tGVAR;
7085 case '&': /* $&: last match */
7086 case '`': /* $`: string before last match */
7087 case '\'': /* $': string after last match */
7088 case '+': /* $+: string matches last paren. */
7089 if (last_state == EXPR_FNAME) {
7090 tokadd('$');
7091 tokadd(c);
7092 goto gvar;
7094 set_yylval_node(NEW_BACK_REF(c));
7095 return tBACK_REF;
7097 case '1': case '2': case '3':
7098 case '4': case '5': case '6':
7099 case '7': case '8': case '9':
7100 tokadd('$');
7101 do {
7102 tokadd(c);
7103 c = nextc();
7104 } while (ISDIGIT(c));
7105 pushback(c);
7106 if (last_state == EXPR_FNAME) goto gvar;
7107 tokfix();
7108 set_yylval_node(NEW_NTH_REF(atoi(tok()+1)));
7109 return tNTH_REF;
7111 default:
7112 if (!parser_is_identchar()) {
7113 pushback(c);
7114 return '$';
7116 case '0':
7117 tokadd('$');
7119 break;
7121 case '@':
7122 c = nextc();
7123 newtok();
7124 tokadd('@');
7125 if (c == '@') {
7126 tokadd('@');
7127 c = nextc();
7129 if (ISDIGIT(c)) {
7130 if (tokidx == 1) {
7131 compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
7133 else {
7134 compile_error(PARSER_ARG "`@@%c' is not allowed as a class variable name", c);
7136 return 0;
7138 if (!parser_is_identchar()) {
7139 pushback(c);
7140 return '@';
7142 break;
7144 case '_':
7145 if (was_bol() && whole_match_p("__END__", 7, 0)) {
7146 ruby__end__seen = 1;
7147 parser->eofp = Qtrue;
7148 #ifndef RIPPER
7149 return -1;
7150 #else
7151 lex_goto_eol(parser);
7152 ripper_dispatch_scan_event(parser, k__END__);
7153 return 0;
7154 #endif
7156 newtok();
7157 break;
7159 default:
7160 if (!parser_is_identchar()) {
7161 rb_compile_error(PARSER_ARG "Invalid char `\\x%02X' in expression", c);
7162 goto retry;
7165 newtok();
7166 break;
7169 mb = ENC_CODERANGE_7BIT;
7170 do {
7171 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
7172 if (tokadd_mbchar(c) == -1) return 0;
7173 c = nextc();
7174 } while (parser_is_identchar());
7175 switch (tok()[0]) {
7176 case '@': case '$':
7177 pushback(c);
7178 break;
7179 default:
7180 if ((c == '!' || c == '?') && !peek('=')) {
7181 tokadd(c);
7183 else {
7184 pushback(c);
7187 tokfix();
7190 int result = 0;
7192 last_state = lex_state;
7193 switch (tok()[0]) {
7194 case '$':
7195 lex_state = EXPR_ENDARG;
7196 result = tGVAR;
7197 break;
7198 case '@':
7199 lex_state = EXPR_ENDARG;
7200 if (tok()[1] == '@')
7201 result = tCVAR;
7202 else
7203 result = tIVAR;
7204 break;
7206 default:
7207 if (toklast() == '!' || toklast() == '?') {
7208 result = tFID;
7210 else {
7211 if (lex_state == EXPR_FNAME) {
7212 if ((c = nextc()) == '=' && !peek('~') && !peek('>') &&
7213 (!peek('=') || (lex_p + 1 < lex_pend && lex_p[1] == '>'))) {
7214 result = tIDENTIFIER;
7215 tokadd(c);
7216 tokfix();
7218 else {
7219 pushback(c);
7222 if (result == 0 && ISUPPER(tok()[0])) {
7223 result = tCONSTANT;
7225 else {
7226 result = tIDENTIFIER;
7230 if (mb == ENC_CODERANGE_7BIT && lex_state != EXPR_DOT) {
7231 const struct kwtable *kw;
7233 /* See if it is a reserved word. */
7234 kw = rb_reserved_word(tok(), toklen());
7235 if (kw) {
7236 enum lex_state_e state = lex_state;
7237 lex_state = kw->state;
7238 if (state == EXPR_FNAME) {
7239 set_yylval_id(rb_intern(kw->name));
7240 return kw->id[0];
7242 if (kw->id[0] == keyword_do) {
7243 command_start = Qtrue;
7244 if (lpar_beg && lpar_beg == paren_nest) {
7245 lpar_beg = 0;
7246 --paren_nest;
7247 return keyword_do_LAMBDA;
7249 if (COND_P()) return keyword_do_cond;
7250 if (CMDARG_P() && state != EXPR_CMDARG)
7251 return keyword_do_block;
7252 if (state == EXPR_ENDARG || state == EXPR_BEG)
7253 return keyword_do_block;
7254 return keyword_do;
7256 if (state == EXPR_BEG || state == EXPR_VALUE)
7257 return kw->id[0];
7258 else {
7259 if (kw->id[0] != kw->id[1])
7260 lex_state = EXPR_BEG;
7261 return kw->id[1];
7266 if ((lex_state == EXPR_BEG && !cmd_state) ||
7267 lex_state == EXPR_ARG ||
7268 lex_state == EXPR_CMDARG) {
7269 if (peek(':') && !(lex_p + 1 < lex_pend && lex_p[1] == ':')) {
7270 lex_state = EXPR_BEG;
7271 nextc();
7272 set_yylval_id(TOK_INTERN(!ENC_SINGLE(mb)));
7273 return tLABEL;
7276 if (IS_BEG() ||
7277 lex_state == EXPR_DOT ||
7278 IS_ARG()) {
7279 if (cmd_state) {
7280 lex_state = EXPR_CMDARG;
7282 else {
7283 lex_state = EXPR_ARG;
7286 else {
7287 lex_state = EXPR_END;
7291 ID ident = TOK_INTERN(!ENC_SINGLE(mb));
7293 set_yylval_id(ident);
7294 if (last_state != EXPR_DOT && is_local_id(ident) && lvar_defined(ident)) {
7295 lex_state = EXPR_END;
7298 return result;
7302 #if YYPURE
7303 static int
7304 yylex(void *lval, void *p)
7305 #else
7306 yylex(void *p)
7307 #endif
7309 struct parser_params *parser = (struct parser_params*)p;
7310 int t;
7312 #if YYPURE
7313 parser->parser_yylval = (union tmpyystype*)lval;
7314 parser->parser_yylval->val = Qundef;
7315 #endif
7316 t = parser_yylex(parser);
7317 #ifdef RIPPER
7318 if (!NIL_P(parser->delayed)) {
7319 ripper_dispatch_delayed_token(parser, t);
7320 return t;
7322 if (t != 0)
7323 ripper_dispatch_scan_event(parser, t);
7324 #endif
7326 return t;
7329 #ifndef RIPPER
7330 static NODE*
7331 node_newnode(struct parser_params *parser, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
7333 NODE *n = (rb_node_newnode)(type, a0, a1, a2);
7334 nd_set_line(n, ruby_sourceline);
7335 return n;
7338 enum node_type
7339 nodetype(NODE *node) /* for debug */
7341 return (enum node_type)nd_type(node);
7345 nodeline(NODE *node)
7347 return nd_line(node);
7350 static NODE*
7351 newline_node(NODE *node)
7353 if (node) {
7354 node = remove_begin(node);
7355 node->flags |= NODE_FL_NEWLINE;
7357 return node;
7360 static void
7361 fixpos(NODE *node, NODE *orig)
7363 if (!node) return;
7364 if (!orig) return;
7365 if (orig == (NODE*)1) return;
7366 nd_set_line(node, nd_line(orig));
7369 static void
7370 parser_warning(struct parser_params *parser, NODE *node, const char *mesg)
7372 rb_compile_warning(ruby_sourcefile, nd_line(node), "%s", mesg);
7374 #define parser_warning(node, mesg) parser_warning(parser, node, mesg)
7376 static void
7377 parser_warn(struct parser_params *parser, NODE *node, const char *mesg)
7379 rb_compile_warn(ruby_sourcefile, nd_line(node), "%s", mesg);
7381 #define parser_warn(node, mesg) parser_warn(parser, node, mesg)
7383 static NODE*
7384 block_append_gen(struct parser_params *parser, NODE *head, NODE *tail)
7386 NODE *end, *h = head, *nd;
7388 if (tail == 0) return head;
7390 if (h == 0) return tail;
7391 switch (nd_type(h)) {
7392 case NODE_LIT:
7393 case NODE_STR:
7394 case NODE_SELF:
7395 case NODE_TRUE:
7396 case NODE_FALSE:
7397 case NODE_NIL:
7398 parser_warning(h, "unused literal ignored");
7399 return tail;
7400 default:
7401 h = end = NEW_BLOCK(head);
7402 end->nd_end = end;
7403 fixpos(end, head);
7404 head = end;
7405 break;
7406 case NODE_BLOCK:
7407 end = h->nd_end;
7408 break;
7411 nd = end->nd_head;
7412 switch (nd_type(nd)) {
7413 case NODE_RETURN:
7414 case NODE_BREAK:
7415 case NODE_NEXT:
7416 case NODE_REDO:
7417 case NODE_RETRY:
7418 if (RTEST(ruby_verbose)) {
7419 parser_warning(nd, "statement not reached");
7421 break;
7423 default:
7424 break;
7427 if (nd_type(tail) != NODE_BLOCK) {
7428 tail = NEW_BLOCK(tail);
7429 tail->nd_end = tail;
7431 end->nd_next = tail;
7432 h->nd_end = tail->nd_end;
7433 return head;
7436 /* append item to the list */
7437 static NODE*
7438 list_append_gen(struct parser_params *parser, NODE *list, NODE *item)
7440 NODE *last;
7442 if (list == 0) return NEW_LIST(item);
7443 if (list->nd_next) {
7444 last = list->nd_next->nd_end;
7446 else {
7447 last = list;
7450 list->nd_alen += 1;
7451 last->nd_next = NEW_LIST(item);
7452 list->nd_next->nd_end = last->nd_next;
7453 return list;
7456 /* concat two lists */
7457 static NODE*
7458 list_concat_gen(struct parser_params *parser, NODE *head, NODE *tail)
7460 NODE *last;
7462 if (head->nd_next) {
7463 last = head->nd_next->nd_end;
7465 else {
7466 last = head;
7469 head->nd_alen += tail->nd_alen;
7470 last->nd_next = tail;
7471 if (tail->nd_next) {
7472 head->nd_next->nd_end = tail->nd_next->nd_end;
7474 else {
7475 head->nd_next->nd_end = tail;
7478 return head;
7481 static int
7482 literal_concat0(struct parser_params *parser, VALUE head, VALUE tail)
7484 if (!rb_enc_compatible(head, tail)) {
7485 compile_error(PARSER_ARG "string literal encodings differ (%s / %s)",
7486 rb_enc_name(rb_enc_get(head)),
7487 rb_enc_name(rb_enc_get(tail)));
7488 rb_str_resize(head, 0);
7489 rb_str_resize(tail, 0);
7490 return 0;
7492 rb_str_buf_append(head, tail);
7493 return 1;
7496 /* concat two string literals */
7497 static NODE *
7498 literal_concat_gen(struct parser_params *parser, NODE *head, NODE *tail)
7500 enum node_type htype;
7502 if (!head) return tail;
7503 if (!tail) return head;
7505 htype = nd_type(head);
7506 if (htype == NODE_EVSTR) {
7507 NODE *node = NEW_DSTR(STR_NEW0());
7508 head = list_append(node, head);
7510 switch (nd_type(tail)) {
7511 case NODE_STR:
7512 if (htype == NODE_STR) {
7513 if (!literal_concat0(parser, head->nd_lit, tail->nd_lit)) {
7514 error:
7515 rb_gc_force_recycle((VALUE)head);
7516 rb_gc_force_recycle((VALUE)tail);
7517 return 0;
7519 rb_gc_force_recycle((VALUE)tail);
7521 else {
7522 list_append(head, tail);
7524 break;
7526 case NODE_DSTR:
7527 if (htype == NODE_STR) {
7528 if (!literal_concat0(parser, head->nd_lit, tail->nd_lit))
7529 goto error;
7530 tail->nd_lit = head->nd_lit;
7531 rb_gc_force_recycle((VALUE)head);
7532 head = tail;
7534 else {
7535 nd_set_type(tail, NODE_ARRAY);
7536 tail->nd_head = NEW_STR(tail->nd_lit);
7537 list_concat(head, tail);
7539 break;
7541 case NODE_EVSTR:
7542 if (htype == NODE_STR) {
7543 nd_set_type(head, NODE_DSTR);
7544 head->nd_alen = 1;
7546 list_append(head, tail);
7547 break;
7549 return head;
7552 static NODE *
7553 evstr2dstr_gen(struct parser_params *parser, NODE *node)
7555 if (nd_type(node) == NODE_EVSTR) {
7556 node = list_append(NEW_DSTR(STR_NEW0()), node);
7558 return node;
7561 static NODE *
7562 new_evstr_gen(struct parser_params *parser, NODE *node)
7564 NODE *head = node;
7566 if (node) {
7567 switch (nd_type(node)) {
7568 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
7569 return node;
7572 return NEW_EVSTR(head);
7575 static NODE *
7576 call_bin_op_gen(struct parser_params *parser, NODE *recv, ID id, NODE *arg1)
7578 value_expr(recv);
7579 value_expr(arg1);
7580 return NEW_CALL(recv, id, NEW_LIST(arg1));
7583 static NODE *
7584 call_uni_op_gen(struct parser_params *parser, NODE *recv, ID id)
7586 value_expr(recv);
7587 return NEW_CALL(recv, id, 0);
7590 static NODE*
7591 match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2)
7593 value_expr(node1);
7594 value_expr(node2);
7595 if (node1) {
7596 switch (nd_type(node1)) {
7597 case NODE_DREGX:
7598 case NODE_DREGX_ONCE:
7599 return NEW_MATCH2(node1, node2);
7601 case NODE_LIT:
7602 if (TYPE(node1->nd_lit) == T_REGEXP) {
7603 return NEW_MATCH2(node1, node2);
7608 if (node2) {
7609 switch (nd_type(node2)) {
7610 case NODE_DREGX:
7611 case NODE_DREGX_ONCE:
7612 return NEW_MATCH3(node2, node1);
7614 case NODE_LIT:
7615 if (TYPE(node2->nd_lit) == T_REGEXP) {
7616 return NEW_MATCH3(node2, node1);
7621 return NEW_CALL(node1, tMATCH, NEW_LIST(node2));
7624 static NODE*
7625 gettable_gen(struct parser_params *parser, ID id)
7627 if (id == keyword_self) {
7628 return NEW_SELF();
7630 else if (id == keyword_nil) {
7631 return NEW_NIL();
7633 else if (id == keyword_true) {
7634 return NEW_TRUE();
7636 else if (id == keyword_false) {
7637 return NEW_FALSE();
7639 else if (id == keyword__FILE__) {
7640 return NEW_STR(STR_NEW2(ruby_sourcefile));
7642 else if (id == keyword__LINE__) {
7643 return NEW_LIT(INT2FIX(ruby_sourceline));
7645 else if (id == keyword__ENCODING__) {
7646 return NEW_LIT(rb_enc_from_encoding(parser->enc));
7648 else if (is_local_id(id)) {
7649 if (dyna_in_block() && dvar_defined(id)) return NEW_DVAR(id);
7650 if (local_id(id)) return NEW_LVAR(id);
7651 /* method call without arguments */
7652 return NEW_VCALL(id);
7654 else if (is_global_id(id)) {
7655 return NEW_GVAR(id);
7657 else if (is_instance_id(id)) {
7658 return NEW_IVAR(id);
7660 else if (is_const_id(id)) {
7661 return NEW_CONST(id);
7663 else if (is_class_id(id)) {
7664 return NEW_CVAR(id);
7666 compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2name(id));
7667 return 0;
7670 static NODE*
7671 assignable_gen(struct parser_params *parser, ID id, NODE *val)
7673 if (!id) return 0;
7674 if (id == keyword_self) {
7675 yyerror("Can't change the value of self");
7677 else if (id == keyword_nil) {
7678 yyerror("Can't assign to nil");
7680 else if (id == keyword_true) {
7681 yyerror("Can't assign to true");
7683 else if (id == keyword_false) {
7684 yyerror("Can't assign to false");
7686 else if (id == keyword__FILE__) {
7687 yyerror("Can't assign to __FILE__");
7689 else if (id == keyword__LINE__) {
7690 yyerror("Can't assign to __LINE__");
7692 else if (id == keyword__ENCODING__) {
7693 yyerror("Can't assign to __ENCODING__");
7695 else if (is_local_id(id)) {
7696 if (dyna_in_block()) {
7697 if (dvar_curr(id)) {
7698 return NEW_DASGN_CURR(id, val);
7700 else if (dvar_defined(id)) {
7701 return NEW_DASGN(id, val);
7703 else if (local_id(id)) {
7704 return NEW_LASGN(id, val);
7706 else{
7707 dyna_var(id);
7708 return NEW_DASGN_CURR(id, val);
7711 else {
7712 if (!local_id(id)) {
7713 local_var(id);
7715 return NEW_LASGN(id, val);
7718 else if (is_global_id(id)) {
7719 return NEW_GASGN(id, val);
7721 else if (is_instance_id(id)) {
7722 return NEW_IASGN(id, val);
7724 else if (is_const_id(id)) {
7725 if (in_def || in_single)
7726 yyerror("dynamic constant assignment");
7727 return NEW_CDECL(id, val, 0);
7729 else if (is_class_id(id)) {
7730 return NEW_CVASGN(id, val);
7732 else {
7733 compile_error(PARSER_ARG "identifier %s is not valid to set", rb_id2name(id));
7735 return 0;
7738 static void
7739 shadowing_lvar_gen(struct parser_params *parser, ID name)
7741 ID uscore;
7743 CONST_ID(uscore, "_");
7744 if (uscore == name) return;
7745 if (dyna_in_block()) {
7746 if (dvar_curr(name)) {
7747 yyerror("duplicated argument name");
7749 else if (dvar_defined(name) || local_id(name)) {
7750 rb_warningS("shadowing outer local variable - %s", rb_id2name(name));
7751 vtable_add(lvtbl->vars, name);
7754 else {
7755 if (local_id(name)) {
7756 yyerror("duplicated argument name");
7761 static void
7762 new_bv_gen(struct parser_params *parser, ID name)
7764 if (!name) return;
7765 if (!is_local_id(name)) {
7766 compile_error(PARSER_ARG "invalid local variable - %s",
7767 rb_id2name(name));
7768 return;
7770 shadowing_lvar(name);
7771 dyna_var(name);
7774 static NODE *
7775 aryset_gen(struct parser_params *parser, NODE *recv, NODE *idx)
7777 if (recv && nd_type(recv) == NODE_SELF)
7778 recv = (NODE *)1;
7779 return NEW_ATTRASGN(recv, tASET, idx);
7782 static void
7783 block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2)
7785 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
7786 compile_error(PARSER_ARG "both block arg and actual block given");
7791 rb_id_attrset(ID id)
7793 id &= ~ID_SCOPE_MASK;
7794 id |= ID_ATTRSET;
7795 return id;
7798 static NODE *
7799 attrset_gen(struct parser_params *parser, NODE *recv, ID id)
7801 if (recv && nd_type(recv) == NODE_SELF)
7802 recv = (NODE *)1;
7803 return NEW_ATTRASGN(recv, rb_id_attrset(id), 0);
7806 static void
7807 rb_backref_error_gen(struct parser_params *parser, NODE *node)
7809 switch (nd_type(node)) {
7810 case NODE_NTH_REF:
7811 compile_error(PARSER_ARG "Can't set variable $%ld", node->nd_nth);
7812 break;
7813 case NODE_BACK_REF:
7814 compile_error(PARSER_ARG "Can't set variable $%c", (int)node->nd_nth);
7815 break;
7819 static NODE *
7820 arg_concat_gen(struct parser_params *parser, NODE *node1, NODE *node2)
7822 if (!node2) return node1;
7823 switch (nd_type(node1)) {
7824 case NODE_BLOCK_PASS:
7825 node1->nd_iter = arg_concat(node1->nd_iter, node2);
7826 return node1;
7827 case NODE_ARGSPUSH:
7828 if (nd_type(node2) != NODE_ARRAY) break;
7829 node1->nd_body = list_concat(NEW_LIST(node1->nd_body), node2);
7830 nd_set_type(node1, NODE_ARGSCAT);
7831 return node1;
7832 case NODE_ARGSCAT:
7833 if (nd_type(node2) != NODE_ARRAY) break;
7834 node1->nd_body = list_concat(node1->nd_body, node2);
7835 return node1;
7837 return NEW_ARGSCAT(node1, node2);
7840 static NODE *
7841 arg_append_gen(struct parser_params *parser, NODE *node1, NODE *node2)
7843 if (!node1) return NEW_LIST(node2);
7844 switch (nd_type(node1)) {
7845 case NODE_ARRAY:
7846 return list_append(node1, node2);
7847 case NODE_BLOCK_PASS:
7848 node1->nd_head = arg_append(node1->nd_head, node2);
7849 return node1;
7850 case NODE_ARGSPUSH:
7851 node1->nd_body = list_append(NEW_LIST(node1->nd_body), node2);
7852 nd_set_type(node1, NODE_ARGSCAT);
7853 return node1;
7855 return NEW_ARGSPUSH(node1, node2);
7858 static NODE *
7859 splat_array(NODE* node)
7861 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
7862 if (nd_type(node) == NODE_ARRAY) return node;
7863 return 0;
7866 static NODE *
7867 node_assign_gen(struct parser_params *parser, NODE *lhs, NODE *rhs)
7869 if (!lhs) return 0;
7871 switch (nd_type(lhs)) {
7872 case NODE_GASGN:
7873 case NODE_IASGN:
7874 case NODE_IASGN2:
7875 case NODE_LASGN:
7876 case NODE_DASGN:
7877 case NODE_DASGN_CURR:
7878 case NODE_MASGN:
7879 case NODE_CDECL:
7880 case NODE_CVASGN:
7881 lhs->nd_value = rhs;
7882 break;
7884 case NODE_ATTRASGN:
7885 case NODE_CALL:
7886 lhs->nd_args = arg_append(lhs->nd_args, rhs);
7887 break;
7889 default:
7890 /* should not happen */
7891 break;
7894 return lhs;
7897 static int
7898 value_expr_gen(struct parser_params *parser, NODE *node)
7900 int cond = 0;
7902 if (!node) {
7903 rb_warning0("empty expression");
7905 while (node) {
7906 switch (nd_type(node)) {
7907 case NODE_DEFN:
7908 case NODE_DEFS:
7909 parser_warning(node, "void value expression");
7910 return Qfalse;
7912 case NODE_RETURN:
7913 case NODE_BREAK:
7914 case NODE_NEXT:
7915 case NODE_REDO:
7916 case NODE_RETRY:
7917 if (!cond) yyerror("void value expression");
7918 /* or "control never reach"? */
7919 return Qfalse;
7921 case NODE_BLOCK:
7922 while (node->nd_next) {
7923 node = node->nd_next;
7925 node = node->nd_head;
7926 break;
7928 case NODE_BEGIN:
7929 node = node->nd_body;
7930 break;
7932 case NODE_IF:
7933 if (!node->nd_body) {
7934 node = node->nd_else;
7935 break;
7937 else if (!node->nd_else) {
7938 node = node->nd_body;
7939 break;
7941 if (!value_expr(node->nd_body)) return Qfalse;
7942 node = node->nd_else;
7943 break;
7945 case NODE_AND:
7946 case NODE_OR:
7947 cond = 1;
7948 node = node->nd_2nd;
7949 break;
7951 default:
7952 return Qtrue;
7956 return Qtrue;
7959 static void
7960 void_expr_gen(struct parser_params *parser, NODE *node)
7962 const char *useless = 0;
7964 if (!RTEST(ruby_verbose)) return;
7966 if (!node) return;
7967 switch (nd_type(node)) {
7968 case NODE_CALL:
7969 switch (node->nd_mid) {
7970 case '+':
7971 case '-':
7972 case '*':
7973 case '/':
7974 case '%':
7975 case tPOW:
7976 case tUPLUS:
7977 case tUMINUS:
7978 case '|':
7979 case '^':
7980 case '&':
7981 case tCMP:
7982 case '>':
7983 case tGEQ:
7984 case '<':
7985 case tLEQ:
7986 case tEQ:
7987 case tNEQ:
7988 useless = rb_id2name(node->nd_mid);
7989 break;
7991 break;
7993 case NODE_LVAR:
7994 case NODE_DVAR:
7995 case NODE_GVAR:
7996 case NODE_IVAR:
7997 case NODE_CVAR:
7998 case NODE_NTH_REF:
7999 case NODE_BACK_REF:
8000 useless = "a variable";
8001 break;
8002 case NODE_CONST:
8003 useless = "a constant";
8004 break;
8005 case NODE_LIT:
8006 case NODE_STR:
8007 case NODE_DSTR:
8008 case NODE_DREGX:
8009 case NODE_DREGX_ONCE:
8010 useless = "a literal";
8011 break;
8012 case NODE_COLON2:
8013 case NODE_COLON3:
8014 useless = "::";
8015 break;
8016 case NODE_DOT2:
8017 useless = "..";
8018 break;
8019 case NODE_DOT3:
8020 useless = "...";
8021 break;
8022 case NODE_SELF:
8023 useless = "self";
8024 break;
8025 case NODE_NIL:
8026 useless = "nil";
8027 break;
8028 case NODE_TRUE:
8029 useless = "true";
8030 break;
8031 case NODE_FALSE:
8032 useless = "false";
8033 break;
8034 case NODE_DEFINED:
8035 useless = "defined?";
8036 break;
8039 if (useless) {
8040 int line = ruby_sourceline;
8042 ruby_sourceline = nd_line(node);
8043 rb_warnS("useless use of %s in void context", useless);
8044 ruby_sourceline = line;
8048 static void
8049 void_stmts_gen(struct parser_params *parser, NODE *node)
8051 if (!RTEST(ruby_verbose)) return;
8052 if (!node) return;
8053 if (nd_type(node) != NODE_BLOCK) return;
8055 for (;;) {
8056 if (!node->nd_next) return;
8057 void_expr0(node->nd_head);
8058 node = node->nd_next;
8062 static NODE *
8063 remove_begin(NODE *node)
8065 NODE **n = &node, *n1 = node;
8066 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
8067 *n = n1 = n1->nd_body;
8069 return node;
8072 static void
8073 reduce_nodes_gen(struct parser_params *parser, NODE **body)
8075 NODE *node = *body;
8077 if (!node) {
8078 *body = NEW_NIL();
8079 return;
8081 #define subnodes(n1, n2) \
8082 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
8083 (!node->n2) ? (body = &node->n1, 1) : \
8084 (reduce_nodes(&node->n1), body = &node->n2, 1))
8086 while (node) {
8087 switch (nd_type(node)) {
8088 end:
8089 case NODE_NIL:
8090 *body = 0;
8091 return;
8092 case NODE_RETURN:
8093 *body = node = node->nd_stts;
8094 continue;
8095 case NODE_BEGIN:
8096 *body = node = node->nd_body;
8097 continue;
8098 case NODE_BLOCK:
8099 body = &node->nd_end->nd_head;
8100 break;
8101 case NODE_IF:
8102 if (subnodes(nd_body, nd_else)) break;
8103 return;
8104 case NODE_CASE:
8105 body = &node->nd_body;
8106 break;
8107 case NODE_WHEN:
8108 if (!subnodes(nd_body, nd_next)) goto end;
8109 break;
8110 case NODE_ENSURE:
8111 if (!subnodes(nd_head, nd_resq)) goto end;
8112 break;
8113 case NODE_RESCUE:
8114 if (!subnodes(nd_head, nd_resq)) goto end;
8115 break;
8116 default:
8117 return;
8119 node = *body;
8122 #undef subnodes
8125 static int
8126 assign_in_cond(struct parser_params *parser, NODE *node)
8128 switch (nd_type(node)) {
8129 case NODE_MASGN:
8130 yyerror("multiple assignment in conditional");
8131 return 1;
8133 case NODE_LASGN:
8134 case NODE_DASGN:
8135 case NODE_GASGN:
8136 case NODE_IASGN:
8137 break;
8139 default:
8140 return 0;
8143 switch (nd_type(node->nd_value)) {
8144 case NODE_LIT:
8145 case NODE_STR:
8146 case NODE_NIL:
8147 case NODE_TRUE:
8148 case NODE_FALSE:
8149 /* reports always */
8150 parser_warn(node->nd_value, "found = in conditional, should be ==");
8151 return 1;
8153 case NODE_DSTR:
8154 case NODE_XSTR:
8155 case NODE_DXSTR:
8156 case NODE_EVSTR:
8157 case NODE_DREGX:
8158 default:
8159 break;
8161 return 1;
8164 static void
8165 warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
8167 if (!e_option_supplied(parser)) parser_warn(node, str);
8170 static void
8171 warning_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
8173 if (!e_option_supplied(parser)) parser_warning(node, str);
8176 static void
8177 fixup_nodes(NODE **rootnode)
8179 NODE *node, *next, *head;
8181 for (node = *rootnode; node; node = next) {
8182 enum node_type type;
8183 VALUE val;
8185 next = node->nd_next;
8186 head = node->nd_head;
8187 rb_gc_force_recycle((VALUE)node);
8188 *rootnode = next;
8189 switch (type = nd_type(head)) {
8190 case NODE_DOT2:
8191 case NODE_DOT3:
8192 val = rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit,
8193 type == NODE_DOT3 ? Qtrue : Qfalse);
8194 rb_gc_force_recycle((VALUE)head->nd_beg);
8195 rb_gc_force_recycle((VALUE)head->nd_end);
8196 nd_set_type(head, NODE_LIT);
8197 head->nd_lit = val;
8198 break;
8199 default:
8200 break;
8205 static NODE *cond0(struct parser_params*,NODE*);
8207 static NODE*
8208 range_op(struct parser_params *parser, NODE *node)
8210 enum node_type type;
8212 if (node == 0) return 0;
8214 type = nd_type(node);
8215 value_expr(node);
8216 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
8217 warn_unless_e_option(parser, node, "integer literal in conditional range");
8218 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."))));
8220 return cond0(parser, node);
8223 static int
8224 literal_node(NODE *node)
8226 if (!node) return 1; /* same as NODE_NIL */
8227 switch (nd_type(node)) {
8228 case NODE_LIT:
8229 case NODE_STR:
8230 case NODE_DSTR:
8231 case NODE_EVSTR:
8232 case NODE_DREGX:
8233 case NODE_DREGX_ONCE:
8234 case NODE_DSYM:
8235 return 2;
8236 case NODE_TRUE:
8237 case NODE_FALSE:
8238 case NODE_NIL:
8239 return 1;
8241 return 0;
8244 static NODE*
8245 cond0(struct parser_params *parser, NODE *node)
8247 if (node == 0) return 0;
8248 assign_in_cond(parser, node);
8250 switch (nd_type(node)) {
8251 case NODE_DSTR:
8252 case NODE_EVSTR:
8253 case NODE_STR:
8254 rb_warn0("string literal in condition");
8255 break;
8257 case NODE_DREGX:
8258 case NODE_DREGX_ONCE:
8259 warning_unless_e_option(parser, node, "regex literal in condition");
8260 return NEW_MATCH2(node, NEW_GVAR(rb_intern("$_")));
8262 case NODE_AND:
8263 case NODE_OR:
8264 node->nd_1st = cond0(parser, node->nd_1st);
8265 node->nd_2nd = cond0(parser, node->nd_2nd);
8266 break;
8268 case NODE_DOT2:
8269 case NODE_DOT3:
8270 node->nd_beg = range_op(parser, node->nd_beg);
8271 node->nd_end = range_op(parser, node->nd_end);
8272 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
8273 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
8274 if (!e_option_supplied(parser)) {
8275 int b = literal_node(node->nd_beg);
8276 int e = literal_node(node->nd_end);
8277 if ((b == 1 && e == 1) || (b + e >= 2 && RTEST(ruby_verbose))) {
8278 parser_warn(node, "range literal in condition");
8281 break;
8283 case NODE_DSYM:
8284 parser_warning(node, "literal in condition");
8285 break;
8287 case NODE_LIT:
8288 if (TYPE(node->nd_lit) == T_REGEXP) {
8289 warn_unless_e_option(parser, node, "regex literal in condition");
8290 nd_set_type(node, NODE_MATCH);
8292 else {
8293 parser_warning(node, "literal in condition");
8295 default:
8296 break;
8298 return node;
8301 static NODE*
8302 cond_gen(struct parser_params *parser, NODE *node)
8304 if (node == 0) return 0;
8305 return cond0(parser, node);
8308 static NODE*
8309 logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right)
8311 value_expr(left);
8312 if (left && nd_type(left) == type) {
8313 NODE *node = left, *second;
8314 while ((second = node->nd_2nd) != 0 && nd_type(second) == type) {
8315 node = second;
8317 node->nd_2nd = NEW_NODE(type, second, right, 0);
8318 return left;
8320 return NEW_NODE(type, left, right, 0);
8323 static void
8324 no_blockarg(struct parser_params *parser, NODE *node)
8326 if (node && nd_type(node) == NODE_BLOCK_PASS) {
8327 compile_error(PARSER_ARG "block argument should not be given");
8331 static NODE *
8332 ret_args_gen(struct parser_params *parser, NODE *node)
8334 if (node) {
8335 no_blockarg(parser, node);
8336 if (nd_type(node) == NODE_ARRAY) {
8337 if (node->nd_next == 0) {
8338 node = node->nd_head;
8340 else {
8341 nd_set_type(node, NODE_VALUES);
8345 return node;
8348 static NODE *
8349 new_yield_gen(struct parser_params *parser, NODE *node)
8351 long state = Qtrue;
8353 if (node) {
8354 no_blockarg(parser, node);
8355 if (node && nd_type(node) == NODE_SPLAT) {
8356 state = Qtrue;
8359 else {
8360 state = Qfalse;
8362 return NEW_YIELD(node, state);
8365 static NODE*
8366 negate_lit(NODE *node)
8368 switch (TYPE(node->nd_lit)) {
8369 case T_FIXNUM:
8370 node->nd_lit = LONG2FIX(-FIX2LONG(node->nd_lit));
8371 break;
8372 case T_BIGNUM:
8373 node->nd_lit = rb_funcall(node->nd_lit,tUMINUS,0,0);
8374 break;
8375 case T_FLOAT:
8376 RFLOAT(node->nd_lit)->float_value = -RFLOAT_VALUE(node->nd_lit);
8377 break;
8378 default:
8379 break;
8381 return node;
8384 static NODE *
8385 arg_blk_pass(NODE *node1, NODE *node2)
8387 if (node2) {
8388 node2->nd_head = node1;
8389 return node2;
8391 return node1;
8394 static NODE*
8395 new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, ID b)
8397 int saved_line = ruby_sourceline;
8398 NODE *node;
8399 NODE *i1, *i2 = 0;
8401 node = NEW_ARGS(m ? m->nd_plen : 0, o);
8402 i1 = m ? m->nd_next : 0;
8403 node->nd_next = NEW_ARGS_AUX(r, b);
8405 if (p) {
8406 i2 = p->nd_next;
8407 node->nd_next->nd_next = NEW_ARGS_AUX(p->nd_pid, p->nd_plen);
8409 else if (i1) {
8410 node->nd_next->nd_next = NEW_ARGS_AUX(0, 0);
8412 if (i1 || i2) {
8413 node->nd_next->nd_next->nd_next = NEW_NODE(NODE_AND, i1, i2, 0);
8415 ruby_sourceline = saved_line;
8416 return node;
8419 static void
8420 local_push_gen(struct parser_params *parser, int inherit_dvars)
8422 struct local_vars *local;
8424 local = ALLOC(struct local_vars);
8425 local->prev = lvtbl;
8426 local->args = vtable_alloc(0);
8427 local->vars = vtable_alloc(inherit_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
8428 lvtbl = local;
8431 static void
8432 local_pop_gen(struct parser_params *parser)
8434 struct local_vars *local = lvtbl->prev;
8435 vtable_free(lvtbl->args);
8436 vtable_free(lvtbl->vars);
8437 xfree(lvtbl);
8438 lvtbl = local;
8441 static ID*
8442 vtable_tblcpy(ID *buf, const struct vtable *src)
8444 int i, cnt = vtable_size(src);
8446 if (cnt > 0) {
8447 buf[0] = cnt;
8448 for (i = 0; i < cnt; i++) {
8449 buf[i] = src->tbl[i];
8451 return buf;
8453 return 0;
8456 static ID*
8457 local_tbl_gen(struct parser_params *parser)
8459 int cnt = vtable_size(lvtbl->args) + vtable_size(lvtbl->vars);
8460 ID *buf;
8462 if (cnt <= 0) return 0;
8463 buf = ALLOC_N(ID, cnt + 1);
8464 vtable_tblcpy(buf+1, lvtbl->args);
8465 vtable_tblcpy(buf+vtable_size(lvtbl->args)+1, lvtbl->vars);
8466 buf[0] = cnt;
8467 return buf;
8470 #include "debug.h"
8471 static int
8472 arg_var_gen(struct parser_params *parser, ID id)
8474 vtable_add(lvtbl->args, id);
8475 return vtable_size(lvtbl->args) - 1;
8478 static int
8479 local_var_gen(struct parser_params *parser, ID id)
8481 vtable_add(lvtbl->vars, id);
8482 return vtable_size(lvtbl->vars) - 1;
8485 static int
8486 local_id_gen(struct parser_params *parser, ID id)
8488 struct vtable *vars, *args;
8490 vars = lvtbl->vars;
8491 args = lvtbl->args;
8493 while (vars && POINTER_P(vars->prev)) {
8494 vars = vars->prev;
8495 args = args->prev;
8498 if (vars && vars->prev == DVARS_INHERIT) {
8499 return rb_local_defined(id);
8501 else {
8502 return (vtable_included(args, id) ||
8503 vtable_included(vars, id));
8507 static void
8508 dyna_push_gen(struct parser_params *parser)
8510 lvtbl->args = vtable_alloc(lvtbl->args);
8511 lvtbl->vars = vtable_alloc(lvtbl->vars);
8514 static void
8515 dyna_pop_gen(struct parser_params *parser)
8517 struct vtable *tmp;
8519 tmp = lvtbl->args;
8520 lvtbl->args = lvtbl->args->prev;
8521 vtable_free(tmp);
8522 tmp = lvtbl->vars;
8523 lvtbl->vars = lvtbl->vars->prev;
8524 vtable_free(tmp);
8527 static int
8528 dyna_in_block_gen(struct parser_params *parser)
8530 return POINTER_P(lvtbl->vars) && lvtbl->vars->prev != DVARS_TOPSCOPE;
8533 static int
8534 dvar_defined_gen(struct parser_params *parser, ID id)
8536 struct vtable *vars, *args;
8538 args = lvtbl->args;
8539 vars = lvtbl->vars;
8541 while (POINTER_P(vars)) {
8542 if (vtable_included(args, id)) {
8543 return 1;
8545 if (vtable_included(vars, id)) {
8546 return 1;
8548 args = args->prev;
8549 vars = vars->prev;
8552 if (vars == DVARS_INHERIT) {
8553 return rb_dvar_defined(id);
8556 return 0;
8559 static int
8560 dvar_curr_gen(struct parser_params *parser, ID id)
8562 return (vtable_included(lvtbl->args, id) ||
8563 vtable_included(lvtbl->vars, id));
8566 VALUE rb_reg_compile(VALUE str, int options);
8567 VALUE rb_reg_check_preprocess(VALUE);
8569 static void
8570 reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options)
8572 int c = RE_OPTION_ENCODING_IDX(options);
8574 if (c) {
8575 int opt, idx;
8576 rb_char_to_option_kcode(c, &opt, &idx);
8577 if (idx != ENCODING_GET(str) &&
8578 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
8579 goto error;
8581 ENCODING_SET(str, idx);
8583 else if (RE_OPTION_ENCODING_NONE(options)) {
8584 if (!ENCODING_IS_ASCII8BIT(str) &&
8585 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
8586 c = 'n';
8587 goto error;
8589 rb_enc_associate(str, rb_ascii8bit_encoding());
8591 else if (parser->enc == rb_usascii_encoding()) {
8592 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
8593 /* raise in re.c */
8594 rb_enc_associate(str, rb_usascii_encoding());
8596 else {
8597 rb_enc_associate(str, rb_ascii8bit_encoding());
8600 return;
8602 error:
8603 compile_error(PARSER_ARG
8604 "regexp encoding option '%c' differs from source encoding '%s'",
8605 c, rb_enc_name(rb_enc_get(str)));
8608 static void
8609 reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options)
8611 VALUE err;
8612 reg_fragment_setenc_gen(parser, str, options);
8613 err = rb_reg_check_preprocess(str);
8614 if (err != Qnil) {
8615 err = rb_obj_as_string(err);
8616 compile_error(PARSER_ARG "%s", RSTRING_PTR(err));
8617 RB_GC_GUARD(err);
8621 typedef struct {
8622 struct parser_params* parser;
8623 rb_encoding *enc;
8624 NODE *succ_block;
8625 NODE *fail_block;
8626 int num;
8627 } reg_named_capture_assign_t;
8629 static int
8630 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
8631 int back_num, int *back_refs, OnigRegex regex, void *arg0)
8633 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
8634 struct parser_params* parser = arg->parser;
8635 rb_encoding *enc = arg->enc;
8636 int len = name_end - name;
8637 const char *s = (const char *)name;
8638 ID var;
8640 arg->num++;
8642 if (arg->succ_block == 0) {
8643 arg->succ_block = NEW_BEGIN(0);
8644 arg->fail_block = NEW_BEGIN(0);
8647 if (!len || (*name != '_' && ISASCII(*name) && !rb_enc_islower(*name, enc)) ||
8648 rb_reserved_word(s, len) || !rb_enc_symname2_p(s, len, enc)) {
8649 return ST_CONTINUE;
8651 var = rb_intern3(s, len, enc);
8652 if (dvar_defined(var) || local_id(var)) {
8653 rb_warningS("named capture conflicts a local variable - %s",
8654 rb_id2name(var));
8656 arg->succ_block = block_append(arg->succ_block,
8657 newline_node(node_assign(assignable(var,0),
8658 NEW_CALL(
8659 gettable(rb_intern("$~")),
8660 idAREF,
8661 NEW_LIST(NEW_LIT(ID2SYM(var))))
8662 )));
8663 arg->fail_block = block_append(arg->fail_block,
8664 newline_node(node_assign(assignable(var,0), NEW_LIT(Qnil))));
8665 return ST_CONTINUE;
8668 static NODE *
8669 reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match)
8671 reg_named_capture_assign_t arg;
8673 arg.parser = parser;
8674 arg.enc = rb_enc_get(regexp);
8675 arg.succ_block = 0;
8676 arg.fail_block = 0;
8677 arg.num = 0;
8678 onig_foreach_name(RREGEXP(regexp)->ptr, reg_named_capture_assign_iter, (void*)&arg);
8680 if (arg.num == 0)
8681 return match;
8683 return
8684 block_append(
8685 newline_node(match),
8686 NEW_IF(gettable(rb_intern("$~")),
8687 block_append(
8688 newline_node(arg.succ_block),
8689 newline_node(
8690 NEW_CALL(
8691 gettable(rb_intern("$~")),
8692 rb_intern("begin"),
8693 NEW_LIST(NEW_LIT(INT2FIX(0)))))),
8694 block_append(
8695 newline_node(arg.fail_block),
8696 newline_node(
8697 NEW_LIT(Qnil)))));
8700 static VALUE
8701 reg_compile_gen(struct parser_params* parser, VALUE str, int options)
8703 VALUE re;
8704 VALUE err;
8706 reg_fragment_setenc(str, options);
8707 err = rb_errinfo();
8708 re = rb_reg_compile(str, options & RE_OPTION_MASK);
8709 if (NIL_P(re)) {
8710 ID mesg = rb_intern("mesg");
8711 VALUE m = rb_attr_get(rb_errinfo(), mesg);
8712 rb_set_errinfo(err);
8713 if (!NIL_P(err)) {
8714 rb_str_append(rb_str_cat(rb_attr_get(err, mesg), "\n", 1), m);
8716 else {
8717 compile_error(PARSER_ARG "%s", RSTRING_PTR(m));
8719 return Qnil;
8721 return re;
8724 void
8725 rb_gc_mark_parser(void)
8729 NODE*
8730 rb_parser_append_print(VALUE vparser, NODE *node)
8732 NODE *prelude = 0;
8733 NODE *scope = node;
8734 struct parser_params *parser;
8736 if (!node) return node;
8738 Data_Get_Struct(vparser, struct parser_params, parser);
8740 node = node->nd_body;
8742 if (nd_type(node) == NODE_PRELUDE) {
8743 prelude = node;
8744 node = node->nd_body;
8747 node = block_append(node,
8748 NEW_FCALL(rb_intern("print"),
8749 NEW_ARRAY(NEW_GVAR(rb_intern("$_")))));
8750 if (prelude) {
8751 prelude->nd_body = node;
8752 scope->nd_body = prelude;
8754 else {
8755 scope->nd_body = node;
8758 return scope;
8761 NODE *
8762 rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split)
8764 NODE *prelude = 0;
8765 NODE *scope = node;
8766 struct parser_params *parser;
8768 if (!node) return node;
8770 Data_Get_Struct(vparser, struct parser_params, parser);
8772 node = node->nd_body;
8774 if (nd_type(node) == NODE_PRELUDE) {
8775 prelude = node;
8776 node = node->nd_body;
8778 if (split) {
8779 node = block_append(NEW_GASGN(rb_intern("$F"),
8780 NEW_CALL(NEW_GVAR(rb_intern("$_")),
8781 rb_intern("split"), 0)),
8782 node);
8784 if (chop) {
8785 node = block_append(NEW_CALL(NEW_GVAR(rb_intern("$_")),
8786 rb_intern("chop!"), 0), node);
8789 node = NEW_OPT_N(node);
8791 if (prelude) {
8792 prelude->nd_body = node;
8793 scope->nd_body = prelude;
8795 else {
8796 scope->nd_body = node;
8799 return scope;
8802 static const struct {
8803 ID token;
8804 const char *name;
8805 } op_tbl[] = {
8806 {tDOT2, ".."},
8807 {tDOT3, "..."},
8808 {'+', "+"},
8809 {'-', "-"},
8810 {'+', "+(binary)"},
8811 {'-', "-(binary)"},
8812 {'*', "*"},
8813 {'/', "/"},
8814 {'%', "%"},
8815 {tPOW, "**"},
8816 {tUPLUS, "+@"},
8817 {tUMINUS, "-@"},
8818 {'|', "|"},
8819 {'^', "^"},
8820 {'&', "&"},
8821 {'!', "!"},
8822 {tCMP, "<=>"},
8823 {'>', ">"},
8824 {tGEQ, ">="},
8825 {'<', "<"},
8826 {tLEQ, "<="},
8827 {tEQ, "=="},
8828 {tEQQ, "==="},
8829 {tNEQ, "!="},
8830 {tMATCH, "=~"},
8831 {tNMATCH, "!~"},
8832 {'~', "~"},
8833 {'!', "!"},
8834 {tAREF, "[]"},
8835 {tASET, "[]="},
8836 {tLSHFT, "<<"},
8837 {tRSHFT, ">>"},
8838 {tCOLON2, "::"},
8839 {'`', "`"},
8840 {0, 0}
8843 static struct symbols {
8844 ID last_id;
8845 st_table *sym_id;
8846 st_table *id_str;
8847 st_table *ivar2_id;
8848 st_table *id_ivar2;
8849 VALUE op_sym[tLAST_TOKEN];
8850 } global_symbols = {tLAST_TOKEN >> ID_SCOPE_SHIFT};
8852 static const struct st_hash_type symhash = {
8853 rb_str_hash_cmp,
8854 rb_str_hash,
8857 struct ivar2_key {
8858 ID id;
8859 VALUE klass;
8862 static int
8863 ivar2_cmp(struct ivar2_key *key1, struct ivar2_key *key2)
8865 if (key1->id == key2->id && key1->klass == key2->klass) {
8866 return 0;
8868 return 1;
8871 static int
8872 ivar2_hash(struct ivar2_key *key)
8874 return (key->id << 8) ^ (key->klass >> 2);
8877 static const struct st_hash_type ivar2_hash_type = {
8878 ivar2_cmp,
8879 ivar2_hash,
8882 void
8883 Init_sym(void)
8885 global_symbols.sym_id = st_init_table_with_size(&symhash, 1000);
8886 global_symbols.id_str = st_init_numtable_with_size(1000);
8887 global_symbols.ivar2_id = st_init_table_with_size(&ivar2_hash_type, 1000);
8888 global_symbols.id_ivar2 = st_init_numtable_with_size(1000);
8889 rb_intern2("", 0);
8892 void
8893 rb_gc_mark_symbols(void)
8895 rb_mark_tbl(global_symbols.id_str);
8896 rb_gc_mark_locations(global_symbols.op_sym,
8897 global_symbols.op_sym + tLAST_TOKEN);
8900 static ID
8901 internal_id_gen(struct parser_params *parser)
8903 ID id = (ID)vtable_size(lvtbl->args) + (ID)vtable_size(lvtbl->vars);
8904 id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1;
8905 return ID_INTERNAL | (id << ID_SCOPE_SHIFT);
8908 static int
8909 is_special_global_name(const char *m, const char *e, rb_encoding *enc)
8911 int mb = 0;
8913 if (m >= e) return 0;
8914 switch (*m) {
8915 case '~': case '*': case '$': case '?': case '!': case '@':
8916 case '/': case '\\': case ';': case ',': case '.': case '=':
8917 case ':': case '<': case '>': case '\"':
8918 case '&': case '`': case '\'': case '+':
8919 case '0':
8920 ++m;
8921 break;
8922 case '-':
8923 ++m;
8924 if (m < e && is_identchar(m, e, enc)) {
8925 if (!ISASCII(*m)) mb = 1;
8926 m += rb_enc_mbclen(m, e, enc);
8928 break;
8929 default:
8930 if (!rb_enc_isdigit(*m, enc)) return 0;
8931 do {
8932 if (!ISASCII(*m)) mb = 1;
8933 ++m;
8934 } while (rb_enc_isdigit(*m, enc));
8936 return m == e ? mb + 1 : 0;
8940 rb_symname_p(const char *name)
8942 return rb_enc_symname_p(name, rb_ascii8bit_encoding());
8946 rb_enc_symname_p(const char *name, rb_encoding *enc)
8948 return rb_enc_symname2_p(name, strlen(name), enc);
8952 rb_enc_symname2_p(const char *name, int len, rb_encoding *enc)
8954 const char *m = name;
8955 const char *e = m + len;
8956 int localid = Qfalse;
8958 if (!m) return Qfalse;
8959 switch (*m) {
8960 case '\0':
8961 return Qfalse;
8963 case '$':
8964 if (is_special_global_name(++m, e, enc)) return Qtrue;
8965 goto id;
8967 case '@':
8968 if (*++m == '@') ++m;
8969 goto id;
8971 case '<':
8972 switch (*++m) {
8973 case '<': ++m; break;
8974 case '=': if (*++m == '>') ++m; break;
8975 default: break;
8977 break;
8979 case '>':
8980 switch (*++m) {
8981 case '>': case '=': ++m; break;
8983 break;
8985 case '=':
8986 switch (*++m) {
8987 case '~': ++m; break;
8988 case '=': if (*++m == '=') ++m; break;
8989 default: return Qfalse;
8991 break;
8993 case '*':
8994 if (*++m == '*') ++m;
8995 break;
8997 case '+': case '-':
8998 if (*++m == '@') ++m;
8999 break;
9001 case '|': case '^': case '&': case '/': case '%': case '~': case '`':
9002 ++m;
9003 break;
9005 case '[':
9006 if (*++m != ']') return Qfalse;
9007 if (*++m == '=') ++m;
9008 break;
9010 case '!':
9011 switch (*++m) {
9012 case '\0': return Qtrue;
9013 case '=': case '~': ++m; break;
9014 default: return Qfalse;
9016 break;
9018 default:
9019 localid = !rb_enc_isupper(*m, enc);
9021 if (m >= e || (*m != '_' && !rb_enc_isalpha(*m, enc) && ISASCII(*m)))
9022 return Qfalse;
9023 while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc);
9024 if (localid) {
9025 switch (*m) {
9026 case '!': case '?': case '=': ++m;
9029 break;
9031 return *m ? Qfalse : Qtrue;
9035 rb_intern3(const char *name, long len, rb_encoding *enc)
9037 const char *m = name;
9038 const char *e = m + len;
9039 VALUE str;
9040 ID id;
9041 int last;
9042 int mb;
9043 struct RString fake_str;
9044 fake_str.basic.flags = T_STRING|RSTRING_NOEMBED|FL_FREEZE;
9045 fake_str.basic.klass = rb_cString;
9046 fake_str.as.heap.len = len;
9047 fake_str.as.heap.ptr = (char *)name;
9048 fake_str.as.heap.aux.capa = len;
9049 str = (VALUE)&fake_str;
9050 rb_enc_associate(str, enc);
9052 if (st_lookup(global_symbols.sym_id, str, (st_data_t *)&id))
9053 return id;
9055 if (rb_cString && !rb_enc_asciicompat(enc)) {
9056 id = ID_JUNK;
9057 goto new_id;
9059 last = len-1;
9060 id = 0;
9061 switch (*m) {
9062 case '$':
9063 id |= ID_GLOBAL;
9064 if ((mb = is_special_global_name(++m, e, enc)) != 0) {
9065 if (!--mb) enc = rb_ascii8bit_encoding();
9066 goto new_id;
9068 break;
9069 case '@':
9070 if (m[1] == '@') {
9071 m++;
9072 id |= ID_CLASS;
9074 else {
9075 id |= ID_INSTANCE;
9077 m++;
9078 break;
9079 default:
9080 if (m[0] != '_' && rb_enc_isascii((unsigned char)m[0], enc)
9081 && !rb_enc_isalnum(m[0], enc)) {
9082 /* operators */
9083 int i;
9085 for (i=0; op_tbl[i].token; i++) {
9086 if (*op_tbl[i].name == *m &&
9087 strcmp(op_tbl[i].name, m) == 0) {
9088 id = op_tbl[i].token;
9089 goto id_register;
9094 if (m[last] == '=') {
9095 /* attribute assignment */
9096 id = rb_intern3(name, last, enc);
9097 if (id > tLAST_TOKEN && !is_attrset_id(id)) {
9098 enc = rb_enc_get(rb_id2str(id));
9099 id = rb_id_attrset(id);
9100 goto id_register;
9102 id = ID_ATTRSET;
9104 else if (rb_enc_isupper(m[0], enc)) {
9105 id = ID_CONST;
9107 else {
9108 id = ID_LOCAL;
9110 break;
9112 mb = 0;
9113 if (!rb_enc_isdigit(*m, enc)) {
9114 while (m <= name + last && is_identchar(m, e, enc)) {
9115 if (ISASCII(*m)) {
9116 m++;
9118 else {
9119 mb = 1;
9120 m += rb_enc_mbclen(m, e, enc);
9124 if (m - name < len) id = ID_JUNK;
9125 if (enc != rb_usascii_encoding()) {
9127 * this clause makes sense only when called from other than
9128 * rb_intern_str() taking care of code-range.
9130 if (!mb) {
9131 for (; m <= name + len; ++m) {
9132 if (!ISASCII(*m)) goto mbstr;
9134 enc = rb_usascii_encoding();
9136 mbstr:;
9138 new_id:
9139 id |= ++global_symbols.last_id << ID_SCOPE_SHIFT;
9140 id_register:
9141 str = rb_enc_str_new(name, len, enc);
9142 OBJ_FREEZE(str);
9143 st_add_direct(global_symbols.sym_id, (st_data_t)str, id);
9144 st_add_direct(global_symbols.id_str, id, (st_data_t)str);
9145 return id;
9149 rb_intern2(const char *name, long len)
9151 return rb_intern3(name, len, rb_usascii_encoding());
9154 #undef rb_intern
9156 rb_intern(const char *name)
9158 return rb_intern2(name, strlen(name));
9162 rb_intern_str(VALUE str)
9164 rb_encoding *enc;
9165 ID id;
9167 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
9168 enc = rb_usascii_encoding();
9170 else {
9171 enc = rb_enc_get(str);
9173 id = rb_intern3(RSTRING_PTR(str), RSTRING_LEN(str), enc);
9174 RB_GC_GUARD(str);
9175 return id;
9178 VALUE
9179 rb_id2str(ID id)
9181 st_data_t data;
9183 if (id < tLAST_TOKEN) {
9184 int i = 0;
9186 for (i=0; op_tbl[i].token; i++) {
9187 if (op_tbl[i].token == id) {
9188 VALUE str = global_symbols.op_sym[i];
9189 if (!str) {
9190 str = rb_usascii_str_new2(op_tbl[i].name);
9191 OBJ_FREEZE(str);
9192 global_symbols.op_sym[i] = str;
9194 return str;
9199 if (st_lookup(global_symbols.id_str, id, &data)) {
9200 VALUE str = (VALUE)data;
9201 if (RBASIC(str)->klass == 0)
9202 RBASIC(str)->klass = rb_cString;
9203 return str;
9206 if (is_attrset_id(id)) {
9207 ID id2 = (id & ~ID_SCOPE_MASK) | ID_LOCAL;
9208 VALUE str;
9210 while (!(str = rb_id2str(id2))) {
9211 if (!is_local_id(id2)) return 0;
9212 id2 = (id & ~ID_SCOPE_MASK) | ID_CONST;
9214 str = rb_str_dup(str);
9215 rb_str_cat(str, "=", 1);
9216 rb_intern_str(str);
9217 if (st_lookup(global_symbols.id_str, id, &data)) {
9218 VALUE str = (VALUE)data;
9219 if (RBASIC(str)->klass == 0)
9220 RBASIC(str)->klass = rb_cString;
9221 return str;
9224 return 0;
9227 const char *
9228 rb_id2name(ID id)
9230 VALUE str = rb_id2str(id);
9232 if (!str) return 0;
9233 return RSTRING_PTR(str);
9236 static int
9237 symbols_i(VALUE sym, ID value, VALUE ary)
9239 rb_ary_push(ary, ID2SYM(value));
9240 return ST_CONTINUE;
9244 * call-seq:
9245 * Symbol.all_symbols => array
9247 * Returns an array of all the symbols currently in Ruby's symbol
9248 * table.
9250 * Symbol.all_symbols.size #=> 903
9251 * Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink,
9252 * :chown, :EOFError, :$;, :String,
9253 * :LOCK_SH, :"setuid?", :$<,
9254 * :default_proc, :compact, :extend,
9255 * :Tms, :getwd, :$=, :ThreadGroup,
9256 * :wait2, :$>]
9259 VALUE
9260 rb_sym_all_symbols(void)
9262 VALUE ary = rb_ary_new2(global_symbols.sym_id->num_entries);
9264 st_foreach(global_symbols.sym_id, symbols_i, ary);
9265 return ary;
9269 rb_is_const_id(ID id)
9271 if (is_const_id(id)) return Qtrue;
9272 return Qfalse;
9276 rb_is_class_id(ID id)
9278 if (is_class_id(id)) return Qtrue;
9279 return Qfalse;
9283 rb_is_instance_id(ID id)
9285 if (is_instance_id(id)) return Qtrue;
9286 return Qfalse;
9290 rb_is_local_id(ID id)
9292 if (is_local_id(id)) return Qtrue;
9293 return Qfalse;
9297 rb_is_junk_id(ID id)
9299 if (is_junk_id(id)) return Qtrue;
9300 return Qfalse;
9303 #endif /* !RIPPER */
9305 static void
9306 parser_initialize(struct parser_params *parser)
9308 parser->eofp = Qfalse;
9310 parser->parser_lex_strterm = 0;
9311 parser->parser_cond_stack = 0;
9312 parser->parser_cmdarg_stack = 0;
9313 parser->parser_class_nest = 0;
9314 parser->parser_paren_nest = 0;
9315 parser->parser_lpar_beg = 0;
9316 parser->parser_in_single = 0;
9317 parser->parser_in_def = 0;
9318 parser->parser_in_defined = 0;
9319 parser->parser_compile_for_eval = 0;
9320 parser->parser_cur_mid = 0;
9321 parser->parser_tokenbuf = NULL;
9322 parser->parser_tokidx = 0;
9323 parser->parser_toksiz = 0;
9324 parser->parser_heredoc_end = 0;
9325 parser->parser_command_start = Qtrue;
9326 parser->parser_deferred_nodes = 0;
9327 parser->parser_lex_pbeg = 0;
9328 parser->parser_lex_p = 0;
9329 parser->parser_lex_pend = 0;
9330 parser->parser_lvtbl = 0;
9331 parser->parser_ruby__end__seen = 0;
9332 parser->parser_ruby_sourcefile = 0;
9333 #ifndef RIPPER
9334 parser->is_ripper = 0;
9335 parser->parser_eval_tree_begin = 0;
9336 parser->parser_eval_tree = 0;
9337 #else
9338 parser->is_ripper = 1;
9339 parser->parser_ruby_sourcefile_string = Qnil;
9340 parser->delayed = Qnil;
9342 parser->result = Qnil;
9343 parser->parsing_thread = Qnil;
9344 parser->toplevel_p = Qtrue;
9345 #endif
9346 #ifdef YYMALLOC
9347 parser->heap = NULL;
9348 #endif
9349 parser->enc = rb_usascii_encoding();
9352 #ifdef RIPPER
9353 #define parser_mark ripper_parser_mark
9354 #define parser_free ripper_parser_free
9355 #endif
9357 static void
9358 parser_mark(void *ptr)
9360 struct parser_params *p = (struct parser_params*)ptr;
9362 rb_gc_mark((VALUE)p->parser_lex_strterm);
9363 rb_gc_mark((VALUE)p->parser_deferred_nodes);
9364 rb_gc_mark(p->parser_lex_input);
9365 rb_gc_mark(p->parser_lex_lastline);
9366 rb_gc_mark(p->parser_lex_nextline);
9367 #ifndef RIPPER
9368 rb_gc_mark((VALUE)p->parser_eval_tree_begin) ;
9369 rb_gc_mark((VALUE)p->parser_eval_tree) ;
9370 rb_gc_mark(p->debug_lines);
9371 #else
9372 rb_gc_mark(p->parser_ruby_sourcefile_string);
9373 rb_gc_mark(p->delayed);
9374 rb_gc_mark(p->value);
9375 rb_gc_mark(p->result);
9376 rb_gc_mark(p->parsing_thread);
9377 #endif
9378 #ifdef YYMALLOC
9379 rb_gc_mark((VALUE)p->heap);
9380 #endif
9383 static void
9384 parser_free(void *ptr)
9386 struct parser_params *p = (struct parser_params*)ptr;
9387 struct local_vars *local, *prev;
9389 if (p->parser_tokenbuf) {
9390 xfree(p->parser_tokenbuf);
9392 for (local = p->parser_lvtbl; local; local = prev) {
9393 if (local->vars) xfree(local->vars);
9394 prev = local->prev;
9395 xfree(local);
9397 #ifndef RIPPER
9398 xfree(p->parser_ruby_sourcefile);
9399 #endif
9400 xfree(p);
9403 VALUE rb_parser_get_yydebug(VALUE);
9404 VALUE rb_parser_set_yydebug(VALUE, VALUE);
9406 #ifndef RIPPER
9407 static struct parser_params *
9408 parser_new(void)
9410 struct parser_params *p;
9412 p = ALLOC_N(struct parser_params, 1);
9413 MEMZERO(p, struct parser_params, 1);
9414 parser_initialize(p);
9415 return p;
9418 VALUE
9419 rb_parser_new(void)
9421 struct parser_params *p = parser_new();
9423 return Data_Wrap_Struct(0, parser_mark, parser_free, p);
9427 * call-seq:
9428 * ripper#end_seen? -> Boolean
9430 * Return if parsed source ended by +\_\_END\_\_+.
9431 * This number starts from 1.
9433 VALUE
9434 rb_parser_end_seen_p(VALUE vparser)
9436 struct parser_params *parser;
9438 Data_Get_Struct(vparser, struct parser_params, parser);
9439 return ruby__end__seen ? Qtrue : Qfalse;
9443 * call-seq:
9444 * ripper#encoding -> encoding
9446 * Return encoding of the source.
9448 VALUE
9449 rb_parser_encoding(VALUE vparser)
9451 struct parser_params *parser;
9453 Data_Get_Struct(vparser, struct parser_params, parser);
9454 return rb_enc_from_encoding(parser->enc);
9458 * call-seq:
9459 * ripper.yydebug -> true or false
9461 * Get yydebug.
9463 VALUE
9464 rb_parser_get_yydebug(VALUE self)
9466 struct parser_params *parser;
9468 Data_Get_Struct(self, struct parser_params, parser);
9469 return yydebug ? Qtrue : Qfalse;
9473 * call-seq:
9474 * ripper.yydebug = flag
9476 * Set yydebug.
9478 VALUE
9479 rb_parser_set_yydebug(VALUE self, VALUE flag)
9481 struct parser_params *parser;
9483 Data_Get_Struct(self, struct parser_params, parser);
9484 yydebug = RTEST(flag);
9485 return flag;
9488 #ifdef YYMALLOC
9489 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
9490 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0)
9491 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \
9492 (n)->u3.cnt = (c), (p))
9494 void *
9495 rb_parser_malloc(struct parser_params *parser, size_t size)
9497 size_t cnt = HEAPCNT(1, size);
9498 NODE *n = NEWHEAP();
9499 void *ptr = xmalloc(size);
9501 return ADD2HEAP(n, cnt, ptr);
9504 void *
9505 rb_parser_calloc(struct parser_params *parser, size_t nelem, size_t size)
9507 size_t cnt = HEAPCNT(nelem, size);
9508 NODE *n = NEWHEAP();
9509 void *ptr = xcalloc(nelem, size);
9511 return ADD2HEAP(n, cnt, ptr);
9514 void *
9515 rb_parser_realloc(struct parser_params *parser, void *ptr, size_t size)
9517 NODE *n;
9518 size_t cnt = HEAPCNT(1, size);
9520 if (ptr && (n = parser->heap) != NULL) {
9521 do {
9522 if (n->u1.node == ptr) {
9523 n->u1.node = ptr = xrealloc(ptr, size);
9524 if (n->u3.cnt) n->u3.cnt = cnt;
9525 return ptr;
9527 } while ((n = n->u2.node) != NULL);
9529 n = NEWHEAP();
9530 ptr = xrealloc(ptr, size);
9531 return ADD2HEAP(n, cnt, ptr);
9534 void
9535 rb_parser_free(struct parser_params *parser, void *ptr)
9537 NODE **prev = &parser->heap, *n;
9539 while ((n = *prev) != NULL) {
9540 if (n->u1.node == ptr) {
9541 *prev = n->u2.node;
9542 rb_gc_force_recycle((VALUE)n);
9543 break;
9545 prev = &n->u2.node;
9547 xfree(ptr);
9549 #endif
9550 #endif
9552 #ifdef RIPPER
9553 #ifdef RIPPER_DEBUG
9554 extern int rb_is_pointer_to_heap(VALUE);
9556 /* :nodoc: */
9557 static VALUE
9558 ripper_validate_object(VALUE self, VALUE x)
9560 if (x == Qfalse) return x;
9561 if (x == Qtrue) return x;
9562 if (x == Qnil) return x;
9563 if (x == Qundef)
9564 rb_raise(rb_eArgError, "Qundef given");
9565 if (FIXNUM_P(x)) return x;
9566 if (SYMBOL_P(x)) return x;
9567 if (!rb_is_pointer_to_heap(x))
9568 rb_raise(rb_eArgError, "invalid pointer: %p", x);
9569 switch (TYPE(x)) {
9570 case T_STRING:
9571 case T_OBJECT:
9572 case T_ARRAY:
9573 case T_BIGNUM:
9574 case T_FLOAT:
9575 return x;
9576 case T_NODE:
9577 rb_raise(rb_eArgError, "NODE given: %p", x);
9578 default:
9579 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
9580 x, rb_obj_classname(x));
9582 return x;
9584 #endif
9586 #define validate(x)
9588 static VALUE
9589 ripper_dispatch0(struct parser_params *parser, ID mid)
9591 return rb_funcall(parser->value, mid, 0);
9594 static VALUE
9595 ripper_dispatch1(struct parser_params *parser, ID mid, VALUE a)
9597 validate(a);
9598 return rb_funcall(parser->value, mid, 1, a);
9601 static VALUE
9602 ripper_dispatch2(struct parser_params *parser, ID mid, VALUE a, VALUE b)
9604 validate(a);
9605 validate(b);
9606 return rb_funcall(parser->value, mid, 2, a, b);
9609 static VALUE
9610 ripper_dispatch3(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c)
9612 validate(a);
9613 validate(b);
9614 validate(c);
9615 return rb_funcall(parser->value, mid, 3, a, b, c);
9618 static VALUE
9619 ripper_dispatch4(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
9621 validate(a);
9622 validate(b);
9623 validate(c);
9624 validate(d);
9625 return rb_funcall(parser->value, mid, 4, a, b, c, d);
9628 static VALUE
9629 ripper_dispatch5(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
9631 validate(a);
9632 validate(b);
9633 validate(c);
9634 validate(d);
9635 validate(e);
9636 return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
9639 static const struct kw_assoc {
9640 ID id;
9641 const char *name;
9642 } keyword_to_name[] = {
9643 {keyword_class, "class"},
9644 {keyword_module, "module"},
9645 {keyword_def, "def"},
9646 {keyword_undef, "undef"},
9647 {keyword_begin, "begin"},
9648 {keyword_rescue, "rescue"},
9649 {keyword_ensure, "ensure"},
9650 {keyword_end, "end"},
9651 {keyword_if, "if"},
9652 {keyword_unless, "unless"},
9653 {keyword_then, "then"},
9654 {keyword_elsif, "elsif"},
9655 {keyword_else, "else"},
9656 {keyword_case, "case"},
9657 {keyword_when, "when"},
9658 {keyword_while, "while"},
9659 {keyword_until, "until"},
9660 {keyword_for, "for"},
9661 {keyword_break, "break"},
9662 {keyword_next, "next"},
9663 {keyword_redo, "redo"},
9664 {keyword_retry, "retry"},
9665 {keyword_in, "in"},
9666 {keyword_do, "do"},
9667 {keyword_do_cond, "do"},
9668 {keyword_do_block, "do"},
9669 {keyword_return, "return"},
9670 {keyword_yield, "yield"},
9671 {keyword_super, "super"},
9672 {keyword_self, "self"},
9673 {keyword_nil, "nil"},
9674 {keyword_true, "true"},
9675 {keyword_false, "false"},
9676 {keyword_and, "and"},
9677 {keyword_or, "or"},
9678 {keyword_not, "not"},
9679 {modifier_if, "if"},
9680 {modifier_unless, "unless"},
9681 {modifier_while, "while"},
9682 {modifier_until, "until"},
9683 {modifier_rescue, "rescue"},
9684 {keyword_alias, "alias"},
9685 {keyword_defined, "defined?"},
9686 {keyword_BEGIN, "BEGIN"},
9687 {keyword_END, "END"},
9688 {keyword__LINE__, "__LINE__"},
9689 {keyword__FILE__, "__FILE__"},
9690 {keyword__ENCODING__, "__ENCODING__"},
9691 {0, NULL}
9694 static const char*
9695 keyword_id_to_str(ID id)
9697 const struct kw_assoc *a;
9699 for (a = keyword_to_name; a->id; a++) {
9700 if (a->id == id)
9701 return a->name;
9703 return NULL;
9706 static VALUE
9707 ripper_id2sym(ID id)
9709 const char *name;
9710 char buf[8];
9712 if (id <= 256) {
9713 buf[0] = id;
9714 buf[1] = '\0';
9715 return ID2SYM(rb_intern(buf));
9717 if ((name = keyword_id_to_str(id))) {
9718 return ID2SYM(rb_intern(name));
9720 switch (id) {
9721 case tOROP:
9722 name = "||";
9723 break;
9724 case tANDOP:
9725 name = "&&";
9726 break;
9727 default:
9728 name = rb_id2name(id);
9729 if (!name) {
9730 rb_bug("cannot convert ID to string: %ld", (unsigned long)id);
9732 break;
9734 return ID2SYM(rb_intern(name));
9737 static VALUE
9738 ripper_intern(const char *s)
9740 return ID2SYM(rb_intern(s));
9743 static void
9744 ripper_compile_error(struct parser_params *parser, const char *fmt, ...)
9746 VALUE str;
9747 va_list args;
9749 va_start(args, fmt);
9750 str = rb_vsprintf(fmt, args);
9751 va_end(args);
9752 rb_funcall(parser->value, rb_intern("compile_error"), 1, str);
9755 static void
9756 ripper_warn0(struct parser_params *parser, const char *fmt)
9758 rb_funcall(parser->value, rb_intern("warn"), 1, STR_NEW2(fmt));
9761 static void
9762 ripper_warnI(struct parser_params *parser, const char *fmt, int a)
9764 rb_funcall(parser->value, rb_intern("warn"), 2,
9765 STR_NEW2(fmt), INT2NUM(a));
9768 #if 0
9769 static void
9770 ripper_warnS(struct parser_params *parser, const char *fmt, const char *str)
9772 rb_funcall(parser->value, rb_intern("warn"), 2,
9773 STR_NEW2(fmt), STR_NEW2(str));
9775 #endif
9777 static void
9778 ripper_warning0(struct parser_params *parser, const char *fmt)
9780 rb_funcall(parser->value, rb_intern("warning"), 1, STR_NEW2(fmt));
9783 static void
9784 ripper_warningS(struct parser_params *parser, const char *fmt, const char *str)
9786 rb_funcall(parser->value, rb_intern("warning"), 2,
9787 STR_NEW2(fmt), STR_NEW2(str));
9790 static VALUE
9791 ripper_lex_get_generic(struct parser_params *parser, VALUE src)
9793 return rb_funcall(src, ripper_id_gets, 0);
9796 static VALUE
9797 ripper_s_allocate(VALUE klass)
9799 struct parser_params *p;
9800 VALUE self;
9802 p = ALLOC_N(struct parser_params, 1);
9803 MEMZERO(p, struct parser_params, 1);
9804 self = Data_Wrap_Struct(klass, parser_mark, parser_free, p);
9805 p->value = self;
9806 return self;
9809 #define ripper_initialized_p(r) ((r)->parser_lex_input != 0)
9812 * call-seq:
9813 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
9815 * Create a new Ripper object.
9816 * _src_ must be a String, a IO, or an Object which has #gets method.
9818 * This method does not starts parsing.
9819 * See also Ripper#parse and Ripper.parse.
9821 static VALUE
9822 ripper_initialize(int argc, VALUE *argv, VALUE self)
9824 struct parser_params *parser;
9825 VALUE src, fname, lineno;
9827 Data_Get_Struct(self, struct parser_params, parser);
9828 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
9829 if (rb_obj_respond_to(src, ripper_id_gets, 0)) {
9830 parser->parser_lex_gets = ripper_lex_get_generic;
9832 else {
9833 StringValue(src);
9834 parser->parser_lex_gets = lex_get_str;
9836 parser->parser_lex_input = src;
9837 parser->eofp = Qfalse;
9838 if (NIL_P(fname)) {
9839 fname = STR_NEW2("(ripper)");
9841 else {
9842 StringValue(fname);
9844 parser_initialize(parser);
9846 parser->parser_ruby_sourcefile_string = fname;
9847 parser->parser_ruby_sourcefile = RSTRING_PTR(fname);
9848 parser->parser_ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
9850 return Qnil;
9853 extern VALUE rb_thread_pass(void);
9855 struct ripper_args {
9856 struct parser_params *parser;
9857 int argc;
9858 VALUE *argv;
9861 static VALUE
9862 ripper_parse0(VALUE parser_v)
9864 struct parser_params *parser;
9866 Data_Get_Struct(parser_v, struct parser_params, parser);
9867 parser_prepare(parser);
9868 ripper_yyparse((void*)parser);
9869 return parser->result;
9872 static VALUE
9873 ripper_ensure(VALUE parser_v)
9875 struct parser_params *parser;
9877 Data_Get_Struct(parser_v, struct parser_params, parser);
9878 parser->parsing_thread = Qnil;
9879 return Qnil;
9883 * call-seq:
9884 * ripper#parse
9886 * Start parsing and returns the value of the root action.
9888 static VALUE
9889 ripper_parse(VALUE self)
9891 struct parser_params *parser;
9893 Data_Get_Struct(self, struct parser_params, parser);
9894 if (!ripper_initialized_p(parser)) {
9895 rb_raise(rb_eArgError, "method called for uninitialized object");
9897 if (!NIL_P(parser->parsing_thread)) {
9898 if (parser->parsing_thread == rb_thread_current())
9899 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
9900 else
9901 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
9903 parser->parsing_thread = rb_thread_current();
9904 rb_ensure(ripper_parse0, self, ripper_ensure, self);
9906 return parser->result;
9910 * call-seq:
9911 * ripper#column -> Integer
9913 * Return column number of current parsing line.
9914 * This number starts from 0.
9916 static VALUE
9917 ripper_column(VALUE self)
9919 struct parser_params *parser;
9920 long col;
9922 Data_Get_Struct(self, struct parser_params, parser);
9923 if (!ripper_initialized_p(parser)) {
9924 rb_raise(rb_eArgError, "method called for uninitialized object");
9926 if (NIL_P(parser->parsing_thread)) return Qnil;
9927 col = parser->tokp - parser->parser_lex_pbeg;
9928 return LONG2NUM(col);
9932 * call-seq:
9933 * ripper#lineno -> Integer
9935 * Return line number of current parsing line.
9936 * This number starts from 1.
9938 static VALUE
9939 ripper_lineno(VALUE self)
9941 struct parser_params *parser;
9943 Data_Get_Struct(self, struct parser_params, parser);
9944 if (!ripper_initialized_p(parser)) {
9945 rb_raise(rb_eArgError, "method called for uninitialized object");
9947 if (NIL_P(parser->parsing_thread)) return Qnil;
9948 return INT2NUM(parser->parser_ruby_sourceline);
9951 #ifdef RIPPER_DEBUG
9952 /* :nodoc: */
9953 static VALUE
9954 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
9956 StringValue(msg);
9957 if (obj == Qundef) {
9958 rb_raise(rb_eArgError, "%s", RSTRING_PTR(msg));
9960 return Qnil;
9963 /* :nodoc: */
9964 static VALUE
9965 ripper_value(VALUE self, VALUE obj)
9967 return ULONG2NUM(obj);
9969 #endif
9971 void
9972 Init_ripper(void)
9974 VALUE Ripper;
9976 Ripper = rb_define_class("Ripper", rb_cObject);
9977 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
9978 rb_define_alloc_func(Ripper, ripper_s_allocate);
9979 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
9980 rb_define_method(Ripper, "parse", ripper_parse, 0);
9981 rb_define_method(Ripper, "column", ripper_column, 0);
9982 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
9983 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
9984 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
9985 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
9986 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
9987 #ifdef RIPPER_DEBUG
9988 rb_define_method(rb_mKernel, "assert_Qundef", ripper_assert_Qundef, 2);
9989 rb_define_method(rb_mKernel, "rawVALUE", ripper_value, 1);
9990 rb_define_method(rb_mKernel, "validate_object", ripper_validate_object, 1);
9991 #endif
9993 ripper_id_gets = rb_intern("gets");
9994 ripper_init_eventids1(Ripper);
9995 ripper_init_eventids2(Ripper);
9996 /* ensure existing in symbol table */
9997 rb_intern("||");
9998 rb_intern("&&");
10000 #endif /* RIPPER */