1 /**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
15 # error needs pure parser
18 #define PARSER_DEBUG 0
21 #define YYERROR_VERBOSE 1
22 #define YYSTACK_USE_ALLOCA 0
24 #include "ruby/ruby.h"
26 #include "ruby/encoding.h"
37 #ifndef WARN_PAST_SCOPE
38 # define WARN_PAST_SCOPE 0
43 #define YYMALLOC(size) rb_parser_malloc(parser, (size))
44 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size))
45 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size))
46 #define YYFREE(ptr) rb_parser_free(parser, (ptr))
47 #define YYFPRINTF rb_parser_printf
48 #if defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL
49 # define YY_LOCATION_PRINT(File, Loc) \
50 rb_parser_printf(parser, "%d.%d-%d.%d", \
51 (Loc).first_line, (Loc).first_column, \
52 (Loc).last_line, (Loc).last_column)
58 #define malloc YYMALLOC
59 #define realloc YYREALLOC
60 #define calloc YYCALLOC
64 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
65 EXPR_END_bit, /* newline significant, +/- is an operator. */
66 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
67 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
68 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
69 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
70 EXPR_MID_bit, /* newline significant, +/- is an operator. */
71 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
72 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
73 EXPR_CLASS_bit, /* immediate after `class', no here document. */
74 EXPR_LABEL_bit, /* flag bit, label is allowed. */
75 EXPR_LABELED_bit, /* flag bit, just after a label. */
76 EXPR_FITEM_bit, /* symbol literal as FNAME. */
79 /* examine combinations */
81 #define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
95 EXPR_VALUE = EXPR_BEG,
96 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
97 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
98 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN)
100 #define IS_lex_state_for(x, ls) ((x) & (ls))
101 #define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
102 #define IS_lex_state(ls) IS_lex_state_for(lex_state, (ls))
103 #define IS_lex_state_all(ls) IS_lex_state_all_for(lex_state, (ls))
105 # define SET_LEX_STATE(ls) \
108 rb_parser_trace_lex_state(parser, lex_state, (ls), __LINE__) : \
109 (enum lex_state_e)(ls)))
111 typedef VALUE stack_type;
113 # define SHOW_BITSTACK(stack, name) (yydebug ? rb_parser_show_bitstack(parser, stack, name, __LINE__) : (void)0)
114 # define BITSTACK_PUSH(stack, n) (((stack) = ((stack)<<1)|((n)&1)), SHOW_BITSTACK(stack, #stack"(push)"))
115 # define BITSTACK_POP(stack) (((stack) = (stack) >> 1), SHOW_BITSTACK(stack, #stack"(pop)"))
116 # define BITSTACK_LEXPOP(stack) (((stack) = ((stack) >> 1) | ((stack) & 1)), SHOW_BITSTACK(stack, #stack"(lexpop)"))
117 # define BITSTACK_SET_P(stack) (SHOW_BITSTACK(stack, #stack), (stack)&1)
118 # define BITSTACK_SET(stack, n) ((stack)=(n), SHOW_BITSTACK(stack, #stack"(set)"))
120 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
121 #define COND_POP() BITSTACK_POP(cond_stack)
122 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
123 #define COND_P() BITSTACK_SET_P(cond_stack)
124 #define COND_SET(n) BITSTACK_SET(cond_stack, (n))
126 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
127 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
128 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
129 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
130 #define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
146 struct local_vars *prev;
150 #define DVARS_INHERIT ((void*)1)
151 #define DVARS_TOPSCOPE NULL
152 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
153 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
155 typedef struct token_info {
160 struct token_info *next;
164 Structure of Lexer Buffer:
166 lex_pbeg tokp lex_p lex_pend
168 |-----------+--------------+------------|
172 struct parser_params {
173 rb_imemo_alloc_t *heap;
179 VALUE (*gets)(struct parser_params*,VALUE);
188 enum lex_state_e state;
193 stack_type cond_stack;
194 stack_type cmdarg_stack;
200 int heredoc_line_indent;
202 struct local_vars *lvtbl;
204 int ruby_sourceline; /* current line no. */
205 char *ruby_sourcefile; /* current source file */
206 VALUE ruby_sourcefile_string;
208 token_info *token_info;
209 VALUE compile_option;
216 unsigned int command_start:1;
217 unsigned int eofp: 1;
218 unsigned int ruby__end__seen: 1;
219 unsigned int yydebug: 1;
220 unsigned int has_shebang: 1;
221 unsigned int in_defined: 1;
222 unsigned int in_main: 1;
223 unsigned int in_kwarg: 1;
224 unsigned int in_single: 1;
225 unsigned int in_def: 1;
226 unsigned int token_seen: 1;
227 unsigned int token_info_enabled: 1;
229 unsigned int past_scope_enabled: 1;
231 unsigned int error_p: 1;
232 unsigned int cr_seen: 1;
237 NODE *eval_tree_begin;
242 const struct rb_block *base_block;
252 VALUE parsing_thread;
257 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
259 #define intern_cstr(n,l,en) rb_intern3(n,l,en)
262 #define STR_NEW(p,n) rb_enc_str_new((p),(n),current_enc)
263 #define STR_NEW0() rb_enc_str_new(0,0,current_enc)
264 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),current_enc)
265 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),current_enc)
266 #define TOK_INTERN() intern_cstr(tok(), toklen(), current_enc)
268 static int parser_yyerror(struct parser_params*, const char*);
269 #define yyerror0(msg) parser_yyerror(parser, (msg))
270 #define yyerror(yylloc, parser, msg) yyerror0(msg)
271 #define token_flush(p) ((p)->lex.ptok = (p)->lex.pcur)
273 #define lex_strterm (parser->lex.strterm)
274 #define lex_state (parser->lex.state)
275 #define cond_stack (parser->cond_stack)
276 #define cmdarg_stack (parser->cmdarg_stack)
277 #define paren_nest (parser->lex.paren_nest)
278 #define lpar_beg (parser->lex.lpar_beg)
279 #define brace_nest (parser->lex.brace_nest)
280 #define in_single (parser->in_single)
281 #define in_def (parser->in_def)
282 #define in_main (parser->in_main)
283 #define in_defined (parser->in_defined)
284 #define tokenbuf (parser->tokenbuf)
285 #define tokidx (parser->tokidx)
286 #define toksiz (parser->toksiz)
287 #define tokline (parser->tokline)
288 #define lex_input (parser->lex.input)
289 #define lex_lastline (parser->lex.lastline)
290 #define lex_nextline (parser->lex.nextline)
291 #define lex_pbeg (parser->lex.pbeg)
292 #define lex_p (parser->lex.pcur)
293 #define lex_pend (parser->lex.pend)
294 #define heredoc_end (parser->heredoc_end)
295 #define heredoc_indent (parser->heredoc_indent)
296 #define heredoc_line_indent (parser->heredoc_line_indent)
297 #define command_start (parser->command_start)
298 #define lex_gets_ptr (parser->lex.gets_ptr)
299 #define lex_gets (parser->lex.gets)
300 #define lvtbl (parser->lvtbl)
301 #define ruby__end__seen (parser->ruby__end__seen)
302 #define ruby_sourceline (parser->ruby_sourceline)
303 #define ruby_sourcefile (parser->ruby_sourcefile)
304 #define ruby_sourcefile_string (parser->ruby_sourcefile_string)
305 #define current_enc (parser->enc)
306 #define current_arg (parser->cur_arg)
307 #define yydebug (parser->yydebug)
309 #define compile_for_eval (0)
311 #define compile_for_eval (parser->base_block != 0 && !in_main)
312 #define ruby_eval_tree (parser->eval_tree)
313 #define ruby_eval_tree_begin (parser->eval_tree_begin)
314 #define ruby_debug_lines (parser->debug_lines)
315 #define ruby_coverage (parser->coverage)
317 #define tokp lex.ptok
319 #define token_column ((int)(parser->tokp - lex_pbeg))
321 #define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
322 #define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
323 #define NEW_QCALL(q,r,m,a) NEW_NODE(NODE_CALL_Q(q),r,m,a)
325 #define lambda_beginning_p() (lpar_beg && lpar_beg == paren_nest)
327 static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
330 parser_set_line(NODE *n, int l)
337 set_line_body(NODE *body, int line)
340 switch (nd_type(body)) {
343 nd_set_line(body, line);
347 #define yyparse ruby_yyparse
349 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE);
350 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
352 static NODE *cond_gen(struct parser_params*,NODE*,int,int);
353 #define cond(node,column) cond_gen(parser, (node), FALSE, column)
354 #define method_cond(node,column) cond_gen(parser, (node), TRUE, column)
355 #define new_nil() NEW_NIL()
356 static NODE *new_if_gen(struct parser_params*,NODE*,NODE*,NODE*,int);
357 #define new_if(cc,left,right,column) new_if_gen(parser, (cc), (left), (right), (column))
358 static NODE *new_unless_gen(struct parser_params*,NODE*,NODE*,NODE*,int);
359 #define new_unless(cc,left,right,column) new_unless_gen(parser, (cc), (left), (right), (column))
360 static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*,int);
361 #define logop(id,node1,node2,column) \
362 logop_gen(parser, ((id)==idAND||(id)==idANDOP)?NODE_AND:NODE_OR, \
363 (node1), (node2), (column))
365 static NODE *newline_node(NODE*);
366 static void fixpos(NODE*,NODE*);
368 static int value_expr_gen(struct parser_params*,NODE*);
369 static void void_expr_gen(struct parser_params*,NODE*);
370 static NODE *remove_begin(NODE*);
371 static NODE *remove_begin_all(NODE*);
372 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
373 #define void_expr0(node) void_expr_gen(parser, (node))
374 #define void_expr(node) void_expr0((node) = remove_begin(node))
375 static void void_stmts_gen(struct parser_params*,NODE*);
376 #define void_stmts(node) void_stmts_gen(parser, (node))
377 static void reduce_nodes_gen(struct parser_params*,NODE**);
378 #define reduce_nodes(n) reduce_nodes_gen(parser,(n))
379 static void block_dup_check_gen(struct parser_params*,NODE*,NODE*);
380 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
382 static NODE *block_append_gen(struct parser_params*,NODE*,NODE*,int);
383 #define block_append(h,t,column) block_append_gen(parser,(h),(t),(column))
384 static NODE *list_append_gen(struct parser_params*,NODE*,NODE*,int);
385 #define list_append(l,i,column) list_append_gen(parser,(l),(i),(column))
386 static NODE *list_concat(NODE*,NODE*);
387 static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*,int);
388 #define arg_append(h,t,column) arg_append_gen(parser,(h),(t),(column))
389 static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*,int);
390 #define arg_concat(h,t,column) arg_concat_gen(parser,(h),(t),(column))
391 static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*,int);
392 #define literal_concat(h,t,column) literal_concat_gen(parser,(h),(t),(column))
393 static int literal_concat0(struct parser_params *, VALUE, VALUE);
394 static NODE *new_evstr_gen(struct parser_params*,NODE*,int);
395 #define new_evstr(n, column) new_evstr_gen(parser,(n),(column))
396 static NODE *evstr2dstr_gen(struct parser_params*,NODE*,int);
397 #define evstr2dstr(n,column) evstr2dstr_gen(parser,(n),(column))
398 static NODE *splat_array(NODE*);
400 static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*,int);
401 #define call_bin_op(recv,id,arg1,column) call_bin_op_gen(parser, (recv),(id),(arg1),(column))
402 static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID,int);
403 #define call_uni_op(recv,id,column) call_uni_op_gen(parser, (recv),(id),(column))
404 static NODE *new_qcall_gen(struct parser_params* parser, ID atype, NODE *recv, ID mid, NODE *args, int column);
405 #define new_qcall(q,r,m,a,column) new_qcall_gen(parser,q,r,m,a,column)
406 #define new_command_qcall(q,r,m,a,column) new_qcall_gen(parser,q,r,m,a,column)
407 static NODE *new_command_gen(struct parser_params*parser, NODE *m, NODE *a) {m->nd_args = a; return m;}
408 #define new_command(m,a) new_command_gen(parser, m, a)
409 static NODE *method_add_block_gen(struct parser_params*parser, NODE *m, NODE *b) {b->nd_iter = m; return b;}
410 #define method_add_block(m,b) method_add_block_gen(parser, m, b)
412 static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*);
413 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
414 static NODE *new_args_tail_gen(struct parser_params*,NODE*,ID,ID,int);
415 #define new_args_tail(k,kr,b,column) new_args_tail_gen(parser, (k),(kr),(b),(column))
416 static NODE *new_kw_arg_gen(struct parser_params *parser, NODE *k, int column);
417 #define new_kw_arg(k,column) new_kw_arg_gen(parser, k, column)
419 static VALUE negate_lit_gen(struct parser_params*, VALUE);
420 #define negate_lit(lit) negate_lit_gen(parser, lit)
421 static NODE *ret_args_gen(struct parser_params*,NODE*);
422 #define ret_args(node) ret_args_gen(parser, (node))
423 static NODE *arg_blk_pass(NODE*,NODE*);
424 static NODE *new_yield_gen(struct parser_params*,NODE*,int);
425 #define new_yield(node,column) new_yield_gen(parser, (node), (column))
426 static NODE *dsym_node_gen(struct parser_params*,NODE*,int);
427 #define dsym_node(node,column) dsym_node_gen(parser, (node), (column))
429 static NODE *gettable_gen(struct parser_params*,ID,int);
430 #define gettable(id,column) gettable_gen(parser,(id),(column))
431 static NODE *assignable_gen(struct parser_params*,ID,NODE*,int);
432 #define assignable(id,node,column) assignable_gen(parser, (id), (node), (column))
434 static NODE *aryset_gen(struct parser_params*,NODE*,NODE*,int);
435 #define aryset(node1,node2,column) aryset_gen(parser, (node1), (node2), (column))
436 static NODE *attrset_gen(struct parser_params*,NODE*,ID,ID,int);
437 #define attrset(node,q,id,column) attrset_gen(parser, (node), (q), (id), (column))
439 static void rb_backref_error_gen(struct parser_params*,NODE*);
440 #define rb_backref_error(n) rb_backref_error_gen(parser,(n))
441 static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*,int);
442 #define node_assign(node1, node2, column) node_assign_gen(parser, (node1), (node2), (column))
444 static NODE *new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs, int column);
445 #define new_op_assign(lhs, op, rhs, column) new_op_assign_gen(parser, (lhs), (op), (rhs), (column))
446 static NODE *new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, int column);
447 #define new_attr_op_assign(lhs, type, attr, op, rhs, column) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs), (column))
448 static NODE *new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs, int column);
449 #define new_const_op_assign(lhs, op, rhs, column) new_const_op_assign_gen(parser, (lhs), (op), (rhs), (column))
451 static NODE *const_path_field_gen(struct parser_params *parser, NODE *head, ID mid, int column);
452 #define const_path_field(w, n, column) const_path_field_gen(parser, w, n, column)
453 #define top_const_field(n) NEW_COLON3(n)
454 static NODE *const_decl_gen(struct parser_params *parser, NODE* path, int column);
455 #define const_decl(path, column) const_decl_gen(parser, path, column)
457 #define var_field(n) (n)
458 #define backref_assign_error(n, a, column) (rb_backref_error(n), new_begin(0, column))
460 static NODE *kwd_append(NODE*, NODE*);
462 static NODE *new_hash_gen(struct parser_params *parser, NODE *hash, int column);
463 #define new_hash(hash, column) new_hash_gen(parser, (hash), column)
465 static NODE *new_defined_gen(struct parser_params *parser, NODE *expr, int column);
466 #define new_defined(expr, column) new_defined_gen(parser, expr, column)
468 static NODE *new_regexp_gen(struct parser_params *, NODE *, int, int);
469 #define new_regexp(node, opt, column) new_regexp_gen(parser, node, opt, column)
471 static NODE *new_lit_gen(struct parser_params *parser, VALUE sym, int column);
472 #define new_lit(sym, column) new_lit_gen(parser, sym, column)
474 static NODE *new_list_gen(struct parser_params *parser, NODE *item, int column);
475 #define new_list(item, column) new_list_gen(parser, item, column)
477 static NODE *new_str_gen(struct parser_params *parser, VALUE str, int column);
478 #define new_str(s,column) new_str_gen(parser, s, column)
480 static NODE *new_dvar_gen(struct parser_params *parser, ID id, int column);
481 #define new_dvar(id, column) new_dvar_gen(parser, id, column)
483 static NODE *new_resbody_gen(struct parser_params *parser, NODE *exc_list, NODE *stmt, NODE *rescue, int column);
484 #define new_resbody(e,s,r,column) new_resbody_gen(parser, (e),(s),(r),(column))
486 static NODE *new_errinfo_gen(struct parser_params *parser, int column);
487 #define new_errinfo(column) new_errinfo_gen(parser, column)
489 static NODE *new_call_gen(struct parser_params *parser, NODE *recv, ID mid, NODE *args, int column);
490 #define new_call(recv,mid,args,column) new_call_gen(parser, recv,mid,args,column)
492 static NODE *new_fcall_gen(struct parser_params *parser, ID mid, NODE *args, int column);
493 #define new_fcall(mid,args,column) new_fcall_gen(parser, mid, args, column)
495 static NODE *new_for_gen(struct parser_params *parser, NODE *var, NODE *iter, NODE *body, int column);
496 #define new_for(var,iter,body,column) new_for_gen(parser, var, iter, body, column)
498 static NODE *new_gvar_gen(struct parser_params *parser, ID id, int column);
499 #define new_gvar(id, column) new_gvar_gen(parser, id, column)
501 static NODE *new_lvar_gen(struct parser_params *parser, ID id, int column);
502 #define new_lvar(id, column) new_lvar_gen(parser, id, column)
504 static NODE *new_dstr_gen(struct parser_params *parser, VALUE str, int column);
505 #define new_dstr(s, column) new_dstr_gen(parser, s, column)
507 static NODE *new_rescue_gen(struct parser_params *parser, NODE *b, NODE *res, NODE *e, int column);
508 #define new_rescue(b,res,e,column) new_rescue_gen(parser,b,res,e,column)
510 static NODE *new_undef_gen(struct parser_params *parser, NODE *i, int column);
511 #define new_undef(i, column) new_undef_gen(parser, i, column)
513 static NODE *new_zarray_gen(struct parser_params *parser, int column);
514 #define new_zarray(column) new_zarray_gen(parser, column)
516 static NODE *new_ivar_gen(struct parser_params *parser, ID id, int column);
517 #define new_ivar(id, column) new_ivar_gen(parser,id,column)
519 static NODE *new_postarg_gen(struct parser_params *parser, NODE *i, NODE *v, int column);
520 #define new_postarg(i,v,column) new_postarg_gen(parser,i,v,column)
522 static NODE *new_cdecl_gen(struct parser_params *parser, ID v, NODE *val, NODE *path, int column);
523 #define new_cdecl(v,val,path,column) new_cdecl_gen(parser,v,val,path,column)
525 static NODE *new_scope_gen(struct parser_params *parser, NODE *a, NODE *b, int column);
526 #define new_scope(a,b,column) new_scope_gen(parser,a,b,column)
528 static NODE *new_begin_gen(struct parser_params *parser, NODE *b, int column);
529 #define new_begin(b,column) new_begin_gen(parser,b,column)
531 static NODE *new_masgn_gen(struct parser_params *parser, NODE *l, NODE *r, int column);
532 #define new_masgn(l,r,column) new_masgn_gen(parser,l,r,column)
534 static NODE *new_xstring_gen(struct parser_params *, NODE *, int column);
535 #define new_xstring(node, column) new_xstring_gen(parser, node, column)
536 #define new_string1(str) (str)
538 static NODE *new_body_gen(struct parser_params *parser, NODE *param, NODE *stmt, int column);
539 #define new_brace_body(param, stmt, column) new_body_gen(parser, param, stmt, column)
540 #define new_do_body(param, stmt, column) new_body_gen(parser, param, stmt, column)
542 static NODE *match_op_gen(struct parser_params*,NODE*,NODE*,int);
543 #define match_op(node1,node2,column) match_op_gen(parser, (node1), (node2), (column))
545 static ID *local_tbl_gen(struct parser_params*);
546 #define local_tbl() local_tbl_gen(parser)
548 static VALUE reg_compile_gen(struct parser_params*, VALUE, int);
549 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
550 static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int);
551 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
552 static int reg_fragment_check_gen(struct parser_params*, VALUE, int);
553 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
554 static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, int column);
555 #define reg_named_capture_assign(regexp,column) reg_named_capture_assign_gen(parser,(regexp),column)
557 static NODE *parser_heredoc_dedent(struct parser_params*,NODE*);
558 # define heredoc_dedent(str) parser_heredoc_dedent(parser, (str))
560 #define get_id(id) (id)
561 #define get_value(val) (val)
563 #define NODE_RIPPER NODE_CDECL
566 ripper_new_yylval(ID a, VALUE b, VALUE c)
568 return (VALUE)NEW_CDECL(a, b, c);
572 ripper_is_node_yylval(VALUE n)
574 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
577 #define value_expr(node) ((void)(node))
578 #define remove_begin(node) (node)
579 #define rb_dvar_defined(id, base) 0
580 #define rb_local_defined(id, base) 0
581 static ID ripper_get_id(VALUE);
582 #define get_id(id) ripper_get_id(id)
583 static VALUE ripper_get_value(VALUE);
584 #define get_value(val) ripper_get_value(val)
585 static VALUE assignable_gen(struct parser_params*,VALUE);
586 #define assignable(lhs,node,column) assignable_gen(parser, (lhs))
587 static int id_is_var_gen(struct parser_params *parser, ID id);
588 #define id_is_var(id) id_is_var_gen(parser, (id))
590 #define method_cond(node,column) (node)
591 #define call_bin_op(recv,id,arg1,column) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
592 #define match_op(node1,node2,column) call_bin_op((node1), idEqTilde, (node2), -1)
593 #define call_uni_op(recv,id,column) dispatch2(unary, STATIC_ID2SYM(id), (recv))
594 #define logop(id,node1,node2,column) call_bin_op((node1), (id), (node2), -1)
595 #define node_assign(node1, node2, column) dispatch2(assign, (node1), (node2))
596 static VALUE new_qcall_gen(struct parser_params *parser, VALUE q, VALUE r, VALUE m, VALUE a);
597 #define new_qcall(q,r,m,a,column) new_qcall_gen(parser, (r), (q), (m), (a))
598 #define new_command_qcall(q,r,m,a,column) dispatch4(command_call, (r), (q), (m), (a))
599 #define new_command_call(q,r,m,a) dispatch4(command_call, (r), (q), (m), (a))
600 #define new_command(m,a) dispatch2(command, (m), (a));
602 #define new_nil() Qnil
603 static VALUE new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs, int column);
604 #define new_op_assign(lhs, op, rhs, column) new_op_assign_gen(parser, (lhs), (op), (rhs), (column))
605 static VALUE new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs);
606 #define new_attr_op_assign(lhs, type, attr, op, rhs, column) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs))
607 #define new_const_op_assign(lhs, op, rhs, column) new_op_assign(lhs, op, rhs, column)
609 static VALUE new_regexp_gen(struct parser_params *, VALUE, VALUE);
610 #define new_regexp(node, opt, column) new_regexp_gen(parser, node, opt)
612 static VALUE new_xstring_gen(struct parser_params *, VALUE);
613 #define new_xstring(str, column) new_xstring_gen(parser, str)
614 #define new_string1(str) dispatch1(string_literal, str)
616 #define new_brace_body(param, stmt, column) dispatch2(brace_block, escape_Qundef(param), stmt)
617 #define new_do_body(param, stmt, column) dispatch2(do_block, escape_Qundef(param), stmt)
619 #define const_path_field(w, n, column) dispatch2(const_path_field, (w), (n))
620 #define top_const_field(n) dispatch1(top_const_field, (n))
621 static VALUE const_decl_gen(struct parser_params *parser, VALUE path);
622 #define const_decl(path, column) const_decl_gen(parser, path)
624 static VALUE var_field_gen(struct parser_params *parser, VALUE a);
625 #define var_field(a) var_field_gen(parser, (a))
626 static VALUE assign_error_gen(struct parser_params *parser, VALUE a);
627 #define assign_error(a) assign_error_gen(parser, (a))
628 #define backref_assign_error(n, a, column) assign_error(a)
630 #define block_dup_check(n1,n2) ((void)(n1), (void)(n2))
631 #define fixpos(n1,n2) ((void)(n1), (void)(n2))
633 #define nd_set_line(n,l) ((void)(n))
635 static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
639 RUBY_SYMBOL_EXPORT_BEGIN
640 VALUE rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options);
641 int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
642 enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
643 VALUE rb_parser_lex_state_name(enum lex_state_e state);
644 void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
645 PRINTF_ARGS(void rb_parser_fatal(struct parser_params *parser, const char *fmt, ...), 2, 3);
646 RUBY_SYMBOL_EXPORT_END
648 static ID formal_argument_gen(struct parser_params*, ID);
649 #define formal_argument(id) formal_argument_gen(parser, (id))
650 static ID shadowing_lvar_gen(struct parser_params*,ID);
651 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
652 static void new_bv_gen(struct parser_params*,ID);
653 #define new_bv(id) new_bv_gen(parser, (id))
655 static void local_push_gen(struct parser_params*,int);
656 #define local_push(top) local_push_gen(parser,(top))
657 static void local_pop_gen(struct parser_params*);
658 #define local_pop() local_pop_gen(parser)
659 static void local_var_gen(struct parser_params*, ID);
660 #define local_var(id) local_var_gen(parser, (id))
661 static void arg_var_gen(struct parser_params*, ID);
662 #define arg_var(id) arg_var_gen(parser, (id))
663 static int local_id_gen(struct parser_params*, ID, ID **);
664 #define local_id_ref(id, vidp) local_id_gen(parser, (id), &(vidp))
665 #define local_id(id) local_id_gen(parser, (id), NULL)
666 static ID internal_id_gen(struct parser_params*);
667 #define internal_id() internal_id_gen(parser)
669 static const struct vtable *dyna_push_gen(struct parser_params *);
670 #define dyna_push() dyna_push_gen(parser)
671 static void dyna_pop_gen(struct parser_params*, const struct vtable *);
672 #define dyna_pop(node) dyna_pop_gen(parser, (node))
673 static int dyna_in_block_gen(struct parser_params*);
674 #define dyna_in_block() dyna_in_block_gen(parser)
675 #define dyna_var(id) local_var(id)
676 static int dvar_defined_gen(struct parser_params*, ID, ID**);
677 #define dvar_defined_ref(id, vidp) dvar_defined_gen(parser, (id), &(vidp))
678 #define dvar_defined(id) dvar_defined_gen(parser, (id), NULL)
679 static int dvar_curr_gen(struct parser_params*,ID);
680 #define dvar_curr(id) dvar_curr_gen(parser, (id))
682 static int lvar_defined_gen(struct parser_params*, ID);
683 #define lvar_defined(id) lvar_defined_gen(parser, (id))
686 # define METHOD_NOT idNOT
688 # define METHOD_NOT '!'
691 #define RE_OPTION_ONCE (1<<16)
692 #define RE_OPTION_ENCODING_SHIFT 8
693 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
694 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
695 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
696 #define RE_OPTION_MASK 0xff
697 #define RE_OPTION_ARG_ENCODING_NONE 32
699 #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
700 #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
701 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
702 #define nd_func u1.id
703 #if SIZEOF_SHORT == 2
704 #define nd_term(node) ((signed short)(node)->u2.id)
706 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
708 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
709 #define nd_nest u3.cnt
711 #define TOKEN2ID(tok) ( \
712 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
713 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
714 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
715 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
716 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
717 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
718 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
720 /****** Ripper *******/
723 #define RIPPER_VERSION "0.1.0"
725 static inline VALUE intern_sym(const char *name);
727 #include "eventids1.c"
728 #include "eventids2.c"
730 static VALUE ripper_dispatch0(struct parser_params*,ID);
731 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
732 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
733 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
734 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
735 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
736 static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
737 static void ripper_error_gen(struct parser_params *parser);
738 #define ripper_error() ripper_error_gen(parser)
740 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
741 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
742 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
743 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
744 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
745 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
746 #define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
748 #define yyparse ripper_yyparse
750 #define ID2VAL(id) STATIC_ID2SYM(id)
751 #define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
752 #define KWD2EID(t, v) ripper_new_yylval(keyword_##t, get_value(v), 0)
754 #define arg_new() dispatch0(args_new)
755 #define arg_add(l,a) dispatch2(args_add, (l), (a))
756 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
757 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
758 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
759 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
760 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
762 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
763 #define mrhs_new() dispatch0(mrhs_new)
764 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
765 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
767 #define mlhs_new() dispatch0(mlhs_new)
768 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
769 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
770 #define mlhs_add_post(l,a) dispatch2(mlhs_add_post, (l), (a))
772 #define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
773 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
775 #define blockvar_new(p,v) dispatch2(block_var, (p), (v))
777 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
778 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
779 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
781 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
784 new_args_gen(struct parser_params *parser, VALUE f, VALUE o, VALUE r, VALUE p, VALUE tail)
786 NODE *t = (NODE *)tail;
787 VALUE k = t->u1.value, kr = t->u2.value, b = t->u3.value;
788 return params_new(f, o, r, p, k, kr, escape_Qundef(b));
790 #define new_args(f,o,r,p,t) new_args_gen(parser, (f),(o),(r),(p),(t))
793 new_args_tail_gen(struct parser_params *parser, VALUE k, VALUE kr, VALUE b)
795 return (VALUE)MEMO_NEW(k, kr, b);
797 #define new_args_tail(k,kr,b,column) new_args_tail_gen(parser, (k),(kr),(b))
799 #define new_defined(expr,column) dispatch1(defined, (expr))
801 static VALUE parser_heredoc_dedent(struct parser_params*,VALUE);
802 # define heredoc_dedent(str) parser_heredoc_dedent(parser, (str))
807 #define ID2VAL(id) ((VALUE)(id))
808 #define TOKEN2VAL(t) ID2VAL(t)
809 #define KWD2EID(t, v) keyword_##t
815 # define ifndef_ripper(x) (x)
818 # define Qnull Qundef
819 # define ifndef_ripper(x)
822 # define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
823 # define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
824 # define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
825 # define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
826 # define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
827 # define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
828 # define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
829 # define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
830 # define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
831 # define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
832 # define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
833 # define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
834 # define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
835 # define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
836 # define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
837 # define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
838 # define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
839 # define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
840 # define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
841 # define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
843 static ID id_warn, id_warning, id_gets;
844 # define WARN_S_L(s,l) STR_NEW(s,l)
845 # define WARN_S(s) STR_NEW2(s)
846 # define WARN_I(i) INT2NUM(i)
847 # define WARN_ID(i) rb_id2str(i)
848 # define PRIsWARN "s"
849 # define WARN_ARGS(fmt,n) parser->value, id_warn, n, rb_usascii_str_new_lit(fmt)
850 # define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
851 # ifdef HAVE_VA_ARGS_MACRO
852 # define WARN_CALL(...) rb_funcall(__VA_ARGS__)
854 # define WARN_CALL rb_funcall
856 # define WARNING_ARGS(fmt,n) parser->value, id_warning, n, rb_usascii_str_new_lit(fmt)
857 # define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
858 # ifdef HAVE_VA_ARGS_MACRO
859 # define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
861 # define WARNING_CALL rb_funcall
863 PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
864 # define compile_error ripper_compile_error
865 # define PARSER_ARG parser,
867 # define WARN_S_L(s,l) s
870 # define WARN_ID(i) rb_id2name(i)
871 # define PRIsWARN PRIsVALUE
872 # define WARN_ARGS(fmt,n) WARN_ARGS_L(ruby_sourceline,fmt,n)
873 # define WARN_ARGS_L(l,fmt,n) ruby_sourcefile, (l), (fmt)
874 # define WARN_CALL rb_compile_warn
875 # define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
876 # define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
877 # define WARNING_CALL rb_compile_warning
878 PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
879 # define compile_error parser_compile_error
880 # define PARSER_ARG parser,
883 /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
884 for instance). This is too low for Ruby to parse some files, such as
885 date/format.rb, therefore bump the value up to at least Bison's default. */
888 #define YYMAXDEPTH 10000
892 static void token_info_push_gen(struct parser_params*, const char *token, size_t len);
893 static void token_info_pop_gen(struct parser_params*, const char *token, size_t len);
894 #define token_info_push(token) token_info_push_gen(parser, (token), rb_strlen_lit(token))
895 #define token_info_pop(token) token_info_pop_gen(parser, (token), rb_strlen_lit(token))
899 %lex-param {struct parser_params *parser}
900 %parse-param {struct parser_params *parser}
907 const struct vtable *vars;
961 %token <val> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL
962 %token <val> tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
963 %token <val> tNTH_REF tBACK_REF
964 %token <val> tREGEXP_END
966 %type <val> singleton strings string string1 xstring regexp
967 %type <val> string_contents xstring_contents regexp_contents string_content
968 %type <val> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
969 %type <val> literal numeric simple_numeric dsym cpath
970 %type <val> top_compstmt top_stmts top_stmt
971 %type <val> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
972 %type <val> expr_value arg_value primary_value fcall rel_expr
973 %type <val> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
974 %type <val> args call_args opt_call_args
975 %type <val> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
976 %type <val> command_args aref_args opt_block_arg block_arg var_ref var_lhs
977 %type <val> command_rhs arg_rhs
978 %type <val> command_asgn mrhs mrhs_arg superclass block_call block_command
979 %type <val> f_block_optarg f_block_opt
980 %type <val> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
981 %type <val> assoc_list assocs assoc undef_list backref string_dvar for_var
982 %type <val> block_param opt_block_param block_param_def f_opt
983 %type <val> f_kwarg f_kw f_block_kwarg f_block_kw
984 %type <val> bv_decls opt_bv_decl bvar
985 %type <val> lambda f_larglist lambda_body brace_body do_body
986 %type <val> brace_block cmd_brace_block do_block lhs none fitem
987 %type <val> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
988 %type <val> fsym keyword_variable user_variable sym symbol operation operation2 operation3
989 %type <val> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
990 %type <val> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop
993 %type <val> program then do
995 %token END_OF_INPUT 0 "end-of-input"
996 %token tUPLUS 130 "unary+"
997 %token tUMINUS 131 "unary-"
999 %token tCMP 133 "<=>"
1001 %token tEQQ 139 "==="
1002 %token tNEQ 140 "!="
1003 %token tGEQ 137 ">="
1004 %token tLEQ 136 "<="
1005 %token tANDOP 146 "&&"
1006 %token tOROP 147 "||"
1007 %token tMATCH 141 "=~"
1008 %token tNMATCH 142 "!~"
1009 %token tDOT2 128 ".."
1010 %token tDOT3 129 "..."
1011 %token tAREF 143 "[]"
1012 %token tASET 144 "[]="
1013 %token tLSHFT 134 "<<"
1014 %token tRSHFT 135 ">>"
1015 %token tANDDOT 148 "&."
1016 %token tCOLON2 145 "::"
1017 %token tCOLON3 ":: at EXPR_BEG"
1018 %token <val> tOP_ASGN /* +=, -= etc. */
1021 %token tLPAREN_ARG "( arg"
1025 %token tLBRACE_ARG "{ arg"
1027 %token tDSTAR "**arg"
1030 %token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG
1031 %token tSTRING_DBEG tSTRING_DEND tSTRING_DVAR tSTRING_END tLAMBEG tLABEL_END
1038 %nonassoc tLBRACE_ARG
1040 %nonassoc modifier_if modifier_unless modifier_while modifier_until
1041 %left keyword_or keyword_and
1043 %nonassoc keyword_defined
1045 %left modifier_rescue
1047 %nonassoc tDOT2 tDOT3
1050 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1051 %left '>' tGEQ '<' tLEQ
1057 %right tUMINUS_NUM tUMINUS
1059 %right '!' '~' tUPLUS
1065 SET_LEX_STATE(EXPR_BEG);
1067 local_push(compile_for_eval || in_main);
1075 if ($2 && !compile_for_eval) {
1076 /* last expression should not be void */
1077 if (nd_type($2) != NODE_BLOCK) void_expr($2);
1080 while (node->nd_next) {
1081 node = node->nd_next;
1083 void_expr(node->nd_head);
1086 ruby_eval_tree = new_scope(0, block_append(ruby_eval_tree, $2, @1.first_column), @1.first_column);
1089 parser->result = dispatch1(program, $$);
1095 top_compstmt : top_stmts opt_terms
1108 $$ = new_begin(0, @1.first_column);
1110 $$ = dispatch2(stmts_add, dispatch0(stmts_new),
1111 dispatch0(void_stmt));
1117 $$ = newline_node($1);
1119 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
1122 | top_stmts terms top_stmt
1125 $$ = block_append($1, newline_node($3), @1.first_column);
1127 $$ = dispatch2(stmts_add, $1, $3);
1132 $$ = remove_begin($2);
1140 /* local_push(0); */
1144 '{' top_compstmt '}'
1147 ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
1148 $4, @1.first_column);
1149 /* NEW_PREEXE($4)); */
1151 $$ = new_begin(0, @1.first_column);
1153 $$ = dispatch1(BEGIN, $4);
1166 $$ = new_rescue($1, $2, $3, @1.first_column);
1169 rb_warn0("else without rescue is useless");
1170 $$ = block_append($$, $3, @1.first_column);
1174 $$ = NEW_ENSURE($$, $4);
1175 nd_set_column($$, @1.first_column);
1178 NODE *nil = NEW_NIL();
1179 nd_set_column(nil, @1.first_column);
1180 $$ = block_append($4, nil, @1.first_column);
1185 $$ = dispatch4(bodystmt,
1194 compstmt : stmts opt_terms
1207 $$ = new_begin(0, @1.first_column);
1209 $$ = dispatch2(stmts_add, dispatch0(stmts_new),
1210 dispatch0(void_stmt));
1216 $$ = newline_node($1);
1218 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
1221 | stmts terms stmt_or_begin
1224 $$ = block_append($1, newline_node($3), @1.first_column);
1226 $$ = dispatch2(stmts_add, $1, $3);
1231 $$ = remove_begin($2);
1235 stmt_or_begin : stmt
1241 yyerror0("BEGIN is permitted only at toplevel");
1243 /* local_push(0); */
1247 '{' top_compstmt '}'
1250 ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
1251 $4, @1.first_column);
1252 /* NEW_PREEXE($4)); */
1254 $$ = new_begin(0, @1.first_column);
1256 $$ = dispatch1(BEGIN, $4);
1261 stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1264 $$ = NEW_ALIAS($2, $4);
1265 nd_set_column($$, @1.first_column);
1267 $$ = dispatch2(alias, $2, $4);
1270 | keyword_alias tGVAR tGVAR
1273 $$ = NEW_VALIAS($2, $3);
1274 nd_set_column($$, @1.first_column);
1276 $$ = dispatch2(var_alias, $2, $3);
1279 | keyword_alias tGVAR tBACK_REF
1284 buf[1] = (char)$3->nd_nth;
1285 $$ = NEW_VALIAS($2, rb_intern2(buf, 2));
1286 nd_set_column($$, @1.first_column);
1288 $$ = dispatch2(var_alias, $2, $3);
1291 | keyword_alias tGVAR tNTH_REF
1294 yyerror0("can't make alias for the number variables");
1295 $$ = new_begin(0, @1.first_column);
1297 $$ = dispatch2(var_alias, $2, $3);
1298 $$ = dispatch1(alias_error, $$);
1302 | keyword_undef undef_list
1307 $$ = dispatch1(undef, $2);
1310 | stmt modifier_if expr_value
1313 $$ = new_if($3, remove_begin($1), 0, @1.first_column);
1316 $$ = dispatch2(if_mod, $3, $1);
1319 | stmt modifier_unless expr_value
1322 $$ = new_unless($3, remove_begin($1), 0, @1.first_column);
1325 $$ = dispatch2(unless_mod, $3, $1);
1328 | stmt modifier_while expr_value
1331 if ($1 && nd_type($1) == NODE_BEGIN) {
1332 $$ = NEW_WHILE(cond($3, @1.first_column), $1->nd_body, 0);
1335 $$ = NEW_WHILE(cond($3, @1.first_column), $1, 1);
1337 nd_set_column($$, @1.first_column);
1339 $$ = dispatch2(while_mod, $3, $1);
1342 | stmt modifier_until expr_value
1345 if ($1 && nd_type($1) == NODE_BEGIN) {
1346 $$ = NEW_UNTIL(cond($3, @1.first_column), $1->nd_body, 0);
1349 $$ = NEW_UNTIL(cond($3, @1.first_column), $1, 1);
1351 nd_set_column($$, @1.first_column);
1353 $$ = dispatch2(until_mod, $3, $1);
1356 | stmt modifier_rescue stmt
1359 NODE *resq = new_resbody(0, remove_begin($3), 0, @1.first_column);
1360 $$ = new_rescue(remove_begin($1), resq, 0, @1.first_column);
1361 nd_set_column(resq, @1.first_column);
1363 $$ = dispatch2(rescue_mod, $1, $3);
1366 | keyword_END '{' compstmt '}'
1368 if (in_def || in_single) {
1369 rb_warn0("END in method; use at_exit");
1373 NODE *scope = NEW_NODE(
1374 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */);
1375 $$ = NEW_POSTEXE(scope);
1376 nd_set_column(scope, @1.first_column);
1377 nd_set_column($$, @1.first_column);
1380 $$ = dispatch1(END, $3);
1384 | mlhs '=' command_call
1391 $$ = dispatch2(massign, $1, $3);
1397 $$ = node_assign($1, $3, @1.first_column);
1405 $$ = dispatch2(massign, $1, $3);
1411 command_asgn : lhs '=' command_rhs
1414 $$ = node_assign($1, $3, @1.first_column);
1416 | var_lhs tOP_ASGN command_rhs
1419 $$ = new_op_assign($1, $2, $3, @1.first_column);
1421 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1427 if (!$3) $3 = new_zarray(@1.first_column);
1428 args = arg_concat($3, $6, @1.first_column);
1432 else if ($5 == tANDOP) {
1435 $$ = NEW_OP_ASGN1($1, $5, args);
1437 nd_set_column($$, @1.first_column);
1439 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1440 $$ = dispatch3(opassign, $$, $5, $6);
1443 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1446 $$ = new_attr_op_assign($1, $2, $3, $4, $5, @1.first_column);
1448 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1451 $$ = new_attr_op_assign($1, $2, $3, $4, $5, @1.first_column);
1453 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1455 $$ = const_path_field($1, $3, @1.first_column);
1456 $$ = new_const_op_assign($$, $4, $5, @1.first_column);
1458 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1461 $$ = new_attr_op_assign($1, ID2VAL(idCOLON2), $3, $4, $5, @1.first_column);
1463 | backref tOP_ASGN command_rhs
1466 $$ = backref_assign_error($1, node_assign($1, $3, @1.first_column), @1.first_column);
1470 command_rhs : command_call %prec tOP_ASGN
1478 | command_call modifier_rescue stmt
1482 $$ = new_rescue($1, new_resbody(0, remove_begin($3), 0, @1.first_column), 0, @1.first_column);
1484 $$ = dispatch2(rescue_mod, $1, $3);
1491 | expr keyword_and expr
1493 $$ = logop(idAND, $1, $3, @1.first_column);
1495 | expr keyword_or expr
1497 $$ = logop(idOR, $1, $3, @1.first_column);
1499 | keyword_not opt_nl expr
1501 $$ = call_uni_op(method_cond($3, @1.first_column), METHOD_NOT, @1.first_column);
1505 $$ = call_uni_op(method_cond($2, @1.first_column), '!', @1.first_column);
1515 if (!$$) $$ = NEW_NIL();
1522 command_call : command
1526 block_command : block_call
1527 | block_call call_op2 operation2 command_args
1529 $$ = new_qcall($2, $1, $3, $4, @1.first_column);
1533 cmd_brace_block : tLBRACE_ARG
1536 $<num>$ = ruby_sourceline;
1544 nd_set_line($$, $<num>2);
1552 $$ = new_fcall($1, 0, @1.first_column);
1553 nd_set_line($$, tokline);
1559 command : fcall command_args %prec tLOWEST
1565 $$ = dispatch2(command, $1, $2);
1568 | fcall command_args cmd_brace_block
1570 block_dup_check($2,$3);
1571 $$ = new_command($1, $2);
1572 $$ = method_add_block($$, $3);
1575 | primary_value call_op operation2 command_args %prec tLOWEST
1577 $$ = new_command_qcall($2, $1, $3, $4, @1.first_column);
1580 | primary_value call_op operation2 command_args cmd_brace_block
1582 block_dup_check($4,$5);
1583 $$ = new_command_qcall($2, $1, $3, $4, @1.first_column);
1584 $$ = method_add_block($$, $5);
1587 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1589 $$ = new_command_qcall(ID2VAL(idCOLON2), $1, $3, $4, @1.first_column);
1592 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1594 block_dup_check($4,$5);
1595 $$ = new_command_qcall(ID2VAL(idCOLON2), $1, $3, $4, @1.first_column);
1596 $$ = method_add_block($$, $5);
1599 | keyword_super command_args
1604 nd_set_column($$, @1.first_column);
1606 $$ = dispatch1(super, $2);
1609 | keyword_yield command_args
1612 $$ = new_yield($2, @1.first_column);
1615 $$ = dispatch1(yield, $2);
1618 | keyword_return call_args
1621 $$ = NEW_RETURN(ret_args($2));
1622 nd_set_column($$, @1.first_column);
1624 $$ = dispatch1(return, $2);
1627 | keyword_break call_args
1630 $$ = NEW_BREAK(ret_args($2));
1631 nd_set_column($$, @1.first_column);
1633 $$ = dispatch1(break, $2);
1636 | keyword_next call_args
1639 $$ = NEW_NEXT(ret_args($2));
1640 nd_set_column($$, @1.first_column);
1642 $$ = dispatch1(next, $2);
1648 | tLPAREN mlhs_inner rparen
1653 $$ = dispatch1(mlhs_paren, $2);
1658 mlhs_inner : mlhs_basic
1659 | tLPAREN mlhs_inner rparen
1662 $$ = new_masgn(new_list($2, @1.first_column), 0, @1.first_column);
1664 $$ = dispatch1(mlhs_paren, $2);
1669 mlhs_basic : mlhs_head
1672 $$ = new_masgn($1, 0, @1.first_column);
1677 | mlhs_head mlhs_item
1680 $$ = new_masgn(list_append($1,$2,@1.first_column), 0, @1.first_column);
1682 $$ = mlhs_add($1, $2);
1685 | mlhs_head tSTAR mlhs_node
1688 $$ = new_masgn($1, $3, @1.first_column);
1690 $$ = mlhs_add_star($1, $3);
1693 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1696 $$ = new_masgn($1, new_postarg($3,$5,@1.first_column), @1.first_column);
1698 $1 = mlhs_add_star($1, $3);
1699 $$ = mlhs_add_post($1, $5);
1705 $$ = new_masgn($1, (NODE *)-1, @1.first_column);
1707 $$ = mlhs_add_star($1, Qnil);
1710 | mlhs_head tSTAR ',' mlhs_post
1713 $$ = new_masgn($1, new_postarg((NODE *)-1, $4, @1.first_column), @1.first_column);
1715 $1 = mlhs_add_star($1, Qnil);
1716 $$ = mlhs_add_post($1, $4);
1722 $$ = new_masgn(0, $2, @1.first_column);
1724 $$ = mlhs_add_star(mlhs_new(), $2);
1727 | tSTAR mlhs_node ',' mlhs_post
1730 $$ = new_masgn(0, new_postarg($2,$4,@1.first_column), @1.first_column);
1732 $2 = mlhs_add_star(mlhs_new(), $2);
1733 $$ = mlhs_add_post($2, $4);
1739 $$ = new_masgn(0, (NODE *)-1, @1.first_column);
1741 $$ = mlhs_add_star(mlhs_new(), Qnil);
1744 | tSTAR ',' mlhs_post
1747 $$ = new_masgn(0, new_postarg((NODE *)-1, $3, @1.first_column), @1.first_column);
1749 $$ = mlhs_add_star(mlhs_new(), Qnil);
1750 $$ = mlhs_add_post($$, $3);
1755 mlhs_item : mlhs_node
1756 | tLPAREN mlhs_inner rparen
1761 $$ = dispatch1(mlhs_paren, $2);
1766 mlhs_head : mlhs_item ','
1769 $$ = new_list($1, @1.first_column);
1771 $$ = mlhs_add(mlhs_new(), $1);
1774 | mlhs_head mlhs_item ','
1777 $$ = list_append($1, $2, @1.first_column);
1779 $$ = mlhs_add($1, $2);
1784 mlhs_post : mlhs_item
1787 $$ = new_list($1, @1.first_column);
1789 $$ = mlhs_add(mlhs_new(), $1);
1792 | mlhs_post ',' mlhs_item
1795 $$ = list_append($1, $3, @1.first_column);
1797 $$ = mlhs_add($1, $3);
1802 mlhs_node : user_variable
1804 $$ = assignable(var_field($1), 0, @1.first_column);
1808 $$ = assignable(var_field($1), 0, @1.first_column);
1810 | primary_value '[' opt_call_args rbracket
1813 $$ = aryset($1, $3, @1.first_column);
1815 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1818 | primary_value call_op tIDENTIFIER
1821 $$ = attrset($1, $2, $3, @1.first_column);
1823 $$ = dispatch3(field, $1, $2, $3);
1826 | primary_value tCOLON2 tIDENTIFIER
1829 $$ = attrset($1, idCOLON2, $3, @1.first_column);
1831 $$ = dispatch2(const_path_field, $1, $3);
1834 | primary_value call_op tCONSTANT
1837 $$ = attrset($1, $2, $3, @1.first_column);
1839 $$ = dispatch3(field, $1, $2, $3);
1842 | primary_value tCOLON2 tCONSTANT
1844 $$ = const_decl(const_path_field($1, $3, @1.first_column), @1.first_column);
1848 $$ = const_decl(top_const_field($2), @1.first_column);
1853 $$ = backref_assign_error($1, $1, @1.first_column);
1859 $$ = assignable(var_field($1), 0, @1.first_column);
1861 if (!$$) $$ = new_begin(0, @1.first_column);
1867 $$ = assignable(var_field($1), 0, @1.first_column);
1869 if (!$$) $$ = new_begin(0, @1.first_column);
1873 | primary_value '[' opt_call_args rbracket
1876 $$ = aryset($1, $3, @1.first_column);
1878 $$ = dispatch2(aref_field, $1, escape_Qundef($3));
1881 | primary_value call_op tIDENTIFIER
1884 $$ = attrset($1, $2, $3, @1.first_column);
1886 $$ = dispatch3(field, $1, $2, $3);
1889 | primary_value tCOLON2 tIDENTIFIER
1892 $$ = attrset($1, idCOLON2, $3, @1.first_column);
1894 $$ = dispatch3(field, $1, ID2VAL(idCOLON2), $3);
1897 | primary_value call_op tCONSTANT
1900 $$ = attrset($1, $2, $3, @1.first_column);
1902 $$ = dispatch3(field, $1, $2, $3);
1905 | primary_value tCOLON2 tCONSTANT
1907 $$ = const_decl(const_path_field($1, $3, @1.first_column), @1.first_column);
1911 $$ = const_decl(top_const_field($2), @1.first_column);
1916 $$ = backref_assign_error($1, $1, @1.first_column);
1923 yyerror0("class/module name must be CONSTANT");
1925 $$ = dispatch1(class_name_error, $1);
1932 cpath : tCOLON3 cname
1935 $$ = NEW_COLON3($2);
1937 $$ = dispatch1(top_const_ref, $2);
1943 $$ = NEW_COLON2(0, $$);
1944 nd_set_column($$, @1.first_column);
1946 $$ = dispatch1(const_ref, $1);
1949 | primary_value tCOLON2 cname
1952 $$ = NEW_COLON2($1, $3);
1953 nd_set_column($$, @1.first_column);
1955 $$ = dispatch2(const_path_ref, $1, $3);
1965 SET_LEX_STATE(EXPR_ENDFN);
1970 SET_LEX_STATE(EXPR_ENDFN);
1982 $$ = new_lit(ID2SYM($1), @1.first_column);
1984 $$ = dispatch1(symbol_literal, $1);
1993 $$ = new_undef($1, @1.first_column);
1995 $$ = rb_ary_new3(1, get_value($1));
1998 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2001 NODE *undef = new_undef($4, @1.first_column);
2002 $$ = block_append($1, undef, @1.first_column);
2004 rb_ary_push($1, get_value($4));
2009 op : '|' { ifndef_ripper($$ = '|'); }
2010 | '^' { ifndef_ripper($$ = '^'); }
2011 | '&' { ifndef_ripper($$ = '&'); }
2012 | tCMP { ifndef_ripper($$ = tCMP); }
2013 | tEQ { ifndef_ripper($$ = tEQ); }
2014 | tEQQ { ifndef_ripper($$ = tEQQ); }
2015 | tMATCH { ifndef_ripper($$ = tMATCH); }
2016 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2017 | '>' { ifndef_ripper($$ = '>'); }
2018 | tGEQ { ifndef_ripper($$ = tGEQ); }
2019 | '<' { ifndef_ripper($$ = '<'); }
2020 | tLEQ { ifndef_ripper($$ = tLEQ); }
2021 | tNEQ { ifndef_ripper($$ = tNEQ); }
2022 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2023 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2024 | '+' { ifndef_ripper($$ = '+'); }
2025 | '-' { ifndef_ripper($$ = '-'); }
2026 | '*' { ifndef_ripper($$ = '*'); }
2027 | tSTAR { ifndef_ripper($$ = '*'); }
2028 | '/' { ifndef_ripper($$ = '/'); }
2029 | '%' { ifndef_ripper($$ = '%'); }
2030 | tPOW { ifndef_ripper($$ = tPOW); }
2031 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2032 | '!' { ifndef_ripper($$ = '!'); }
2033 | '~' { ifndef_ripper($$ = '~'); }
2034 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2035 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2036 | tAREF { ifndef_ripper($$ = tAREF); }
2037 | tASET { ifndef_ripper($$ = tASET); }
2038 | '`' { ifndef_ripper($$ = '`'); }
2041 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2042 | keyword_BEGIN | keyword_END
2043 | keyword_alias | keyword_and | keyword_begin
2044 | keyword_break | keyword_case | keyword_class | keyword_def
2045 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2046 | keyword_end | keyword_ensure | keyword_false
2047 | keyword_for | keyword_in | keyword_module | keyword_next
2048 | keyword_nil | keyword_not | keyword_or | keyword_redo
2049 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2050 | keyword_super | keyword_then | keyword_true | keyword_undef
2051 | keyword_when | keyword_yield | keyword_if | keyword_unless
2052 | keyword_while | keyword_until
2055 arg : lhs '=' arg_rhs
2057 $$ = node_assign($1, $3, @1.first_column);
2059 | var_lhs tOP_ASGN arg_rhs
2061 $$ = new_op_assign($1, $2, $3, @1.first_column);
2063 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2069 if (!$3) $3 = new_zarray(@1.first_column);
2070 if (nd_type($3) == NODE_BLOCK_PASS) {
2071 args = NEW_ARGSCAT($3, $6);
2072 nd_set_column(args, @1.first_column);
2075 args = arg_concat($3, $6, @1.first_column);
2080 else if ($5 == tANDOP) {
2083 $$ = NEW_OP_ASGN1($1, $5, args);
2085 nd_set_column($$, @1.first_column);
2087 $1 = dispatch2(aref_field, $1, escape_Qundef($3));
2088 $$ = dispatch3(opassign, $1, $5, $6);
2091 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2094 $$ = new_attr_op_assign($1, $2, $3, $4, $5, @1.first_column);
2096 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2099 $$ = new_attr_op_assign($1, $2, $3, $4, $5, @1.first_column);
2101 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2104 $$ = new_attr_op_assign($1, ID2VAL(idCOLON2), $3, $4, $5, @1.first_column);
2106 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2108 $$ = const_path_field($1, $3, @1.first_column);
2109 $$ = new_const_op_assign($$, $4, $5, @1.first_column);
2111 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2113 $$ = top_const_field($2);
2114 $$ = new_const_op_assign($$, $3, $4, @1.first_column);
2116 | backref tOP_ASGN arg_rhs
2119 $$ = backref_assign_error($1, new_op_assign($1, $2, $3, @1.first_column), @1.first_column);
2126 $$ = NEW_DOT2($1, $3);
2127 nd_set_column($$, @1.first_column);
2129 $$ = dispatch2(dot2, $1, $3);
2137 $$ = NEW_DOT3($1, $3);
2138 nd_set_column($$, @1.first_column);
2140 $$ = dispatch2(dot3, $1, $3);
2145 $$ = call_bin_op($1, '+', $3, @1.first_column);
2149 $$ = call_bin_op($1, '-', $3, @1.first_column);
2153 $$ = call_bin_op($1, '*', $3, @1.first_column);
2157 $$ = call_bin_op($1, '/', $3, @1.first_column);
2161 $$ = call_bin_op($1, '%', $3, @1.first_column);
2165 $$ = call_bin_op($1, idPow, $3, @1.first_column);
2167 | tUMINUS_NUM simple_numeric tPOW arg
2169 $$ = call_uni_op(call_bin_op($2, idPow, $4, @1.first_column), idUMinus, @1.first_column);
2173 $$ = call_uni_op($2, idUPlus, @1.first_column);
2177 $$ = call_uni_op($2, idUMinus, @1.first_column);
2181 $$ = call_bin_op($1, '|', $3, @1.first_column);
2185 $$ = call_bin_op($1, '^', $3, @1.first_column);
2189 $$ = call_bin_op($1, '&', $3, @1.first_column);
2193 $$ = call_bin_op($1, idCmp, $3, @1.first_column);
2195 | rel_expr %prec tCMP
2198 $$ = call_bin_op($1, idEq, $3, @1.first_column);
2202 $$ = call_bin_op($1, idEqq, $3, @1.first_column);
2206 $$ = call_bin_op($1, idNeq, $3, @1.first_column);
2210 $$ = match_op($1, $3, @1.first_column);
2214 $$ = call_bin_op($1, idNeqTilde, $3, @1.first_column);
2218 $$ = call_uni_op(method_cond($2, @1.first_column), '!', @1.first_column);
2222 $$ = call_uni_op($2, '~', @1.first_column);
2226 $$ = call_bin_op($1, idLTLT, $3, @1.first_column);
2230 $$ = call_bin_op($1, idGTGT, $3, @1.first_column);
2234 $$ = logop(idANDOP, $1, $3, @1.first_column);
2238 $$ = logop(idOROP, $1, $3, @1.first_column);
2240 | keyword_defined opt_nl {in_defined = 1;} arg
2243 $$ = new_defined($4, @1.first_column);
2245 | arg '?' arg opt_nl ':' arg
2249 $$ = new_if($1, $3, $6, @1.first_column);
2252 $$ = dispatch3(ifop, $1, $3, $6);
2261 relop : '>' {$$ = '>';}
2267 rel_expr : arg relop arg %prec '>'
2269 $$ = call_bin_op($1, $2, $3, @1.first_column);
2271 | rel_expr relop arg %prec '>'
2273 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2274 $$ = call_bin_op($1, $2, $3, @1.first_column);
2283 if (!$$) $$ = NEW_NIL();
2295 | args ',' assocs trailer
2298 $$ = $3 ? arg_append($1, new_hash($3, @1.first_column), @1.first_column) : $1;
2300 $$ = arg_add_assocs($1, $3);
2306 $$ = $1 ? new_list(new_hash($1, @1.first_column), @1.first_column) : 0;
2308 $$ = arg_add_assocs(arg_new(), $1);
2313 arg_rhs : arg %prec tOP_ASGN
2321 | arg modifier_rescue arg
2325 $$ = new_rescue($1, new_resbody(0, remove_begin($3), 0, @1.first_column), 0, @1.first_column);
2327 $$ = dispatch2(rescue_mod, $1, $3);
2332 paren_args : '(' opt_call_args rparen
2337 $$ = dispatch1(arg_paren, escape_Qundef($2));
2342 opt_paren_args : none
2346 opt_call_args : none
2352 | args ',' assocs ','
2355 $$ = $3 ? arg_append($1, new_hash($3, @1.first_column), @1.first_column) : $1;
2357 $$ = arg_add_assocs($1, $3);
2363 $$ = $1 ? new_list(new_hash($1, @1.first_column), @1.first_column) : 0;
2365 $$ = arg_add_assocs(arg_new(), $1);
2374 $$ = new_list($1, @1.first_column);
2376 $$ = arg_add(arg_new(), $1);
2379 | args opt_block_arg
2382 $$ = arg_blk_pass($1, $2);
2384 $$ = arg_add_optblock($1, $2);
2387 | assocs opt_block_arg
2390 $$ = $1 ? new_list(new_hash($1, @1.first_column), @1.first_column) : 0;
2391 $$ = arg_blk_pass($$, $2);
2393 $$ = arg_add_assocs(arg_new(), $1);
2394 $$ = arg_add_optblock($$, $2);
2397 | args ',' assocs opt_block_arg
2400 $$ = $3 ? arg_append($1, new_hash($3, @1.first_column), @1.first_column) : $1;
2401 $$ = arg_blk_pass($$, $4);
2403 $$ = arg_add_optblock(arg_add_assocs($1, $3), $4);
2410 $$ = arg_add_block(arg_new(), $1);
2416 $<val>$ = cmdarg_stack;
2422 CMDARG_SET($<val>1);
2427 block_arg : tAMPER arg_value
2430 $$ = NEW_BLOCK_PASS($2);
2431 nd_set_column($$, @1.first_column);
2438 opt_block_arg : ',' block_arg
2451 $$ = new_list($1, @1.first_column);
2453 $$ = arg_add(arg_new(), $1);
2460 nd_set_column($$, @1.first_column);
2462 $$ = arg_add_star(arg_new(), $2);
2465 | args ',' arg_value
2469 if ((n1 = splat_array($1)) != 0) {
2470 $$ = list_append(n1, $3, @1.first_column);
2473 $$ = arg_append($1, $3, @1.first_column);
2476 $$ = arg_add($1, $3);
2479 | args ',' tSTAR arg_value
2483 if ((nd_type($4) == NODE_ARRAY) && (n1 = splat_array($1)) != 0) {
2484 $$ = list_concat(n1, $4);
2487 $$ = arg_concat($1, $4, @1.first_column);
2490 $$ = arg_add_star($1, $4);
2499 mrhs : args ',' arg_value
2503 if ((n1 = splat_array($1)) != 0) {
2504 $$ = list_append(n1, $3, @1.first_column);
2507 $$ = arg_append($1, $3, @1.first_column);
2510 $$ = mrhs_add(args2mrhs($1), $3);
2513 | args ',' tSTAR arg_value
2517 if (nd_type($4) == NODE_ARRAY &&
2518 (n1 = splat_array($1)) != 0) {
2519 $$ = list_concat(n1, $4);
2522 $$ = arg_concat($1, $4, @1.first_column);
2525 $$ = mrhs_add_star(args2mrhs($1), $4);
2532 nd_set_column($$, @1.first_column);
2534 $$ = mrhs_add_star(mrhs_new(), $2);
2552 $$ = new_fcall($1, 0, @1.first_column);
2554 $$ = method_arg(dispatch1(fcall, $1), arg_new());
2559 $<val>1 = cmdarg_stack;
2562 $<num>$ = ruby_sourceline;
2569 CMDARG_SET($<val>1);
2573 nd_set_column($$, @1.first_column);
2576 set_line_body($3, $<num>2);
2577 $$ = new_begin($3, @1.first_column);
2579 nd_set_line($$, $<num>2);
2581 $$ = dispatch1(begin, $3);
2584 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2587 $$ = new_begin(0, @1.first_column);
2589 $$ = dispatch1(paren, 0);
2594 $<val>1 = cmdarg_stack;
2597 stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2599 CMDARG_SET($<val>1);
2603 $$ = dispatch1(paren, $3);
2606 | tLPAREN compstmt ')'
2611 $$ = dispatch1(paren, $2);
2614 | primary_value tCOLON2 tCONSTANT
2617 $$ = NEW_COLON2($1, $3);
2618 nd_set_column($$, @1.first_column);
2620 $$ = dispatch2(const_path_ref, $1, $3);
2626 $$ = NEW_COLON3($2);
2627 nd_set_column($$, @1.first_column);
2629 $$ = dispatch1(top_const_ref, $2);
2632 | tLBRACK aref_args ']'
2636 $$ = new_zarray(@1.first_column); /* zero length array*/
2642 $$ = dispatch1(array, escape_Qundef($2));
2645 | tLBRACE assoc_list '}'
2648 $$ = new_hash($2, @1.first_column);
2651 $$ = dispatch1(hash, escape_Qundef($2));
2658 nd_set_column($$, @1.first_column);
2660 $$ = dispatch0(return0);
2663 | keyword_yield '(' call_args rparen
2666 $$ = new_yield($3, @1.first_column);
2668 $$ = dispatch1(yield, dispatch1(paren, $3));
2671 | keyword_yield '(' rparen
2675 nd_set_column($$, @1.first_column);
2677 $$ = dispatch1(yield, dispatch1(paren, arg_new()));
2684 nd_set_column($$, @1.first_column);
2686 $$ = dispatch0(yield0);
2689 | keyword_defined opt_nl '(' {in_defined = 1;} expr rparen
2692 $$ = new_defined($5, @1.first_column);
2694 | keyword_not '(' expr rparen
2696 $$ = call_uni_op(method_cond($3, @1.first_column), METHOD_NOT, @1.first_column);
2698 | keyword_not '(' rparen
2700 $$ = call_uni_op(method_cond(new_nil(), @1.first_column), METHOD_NOT, @1.first_column);
2708 $$ = method_arg(dispatch1(fcall, $1), arg_new());
2709 $$ = method_add_block($$, $2);
2713 | method_call brace_block
2716 block_dup_check($1->nd_args, $2);
2720 $$ = method_add_block($1, $2);
2727 | k_if expr_value then
2733 $$ = new_if($2, $4, $5, @1.first_column);
2736 $$ = dispatch3(if, $2, $4, escape_Qundef($5));
2739 | k_unless expr_value then
2745 $$ = new_unless($2, $4, $5, @1.first_column);
2748 $$ = dispatch3(unless, $2, $4, escape_Qundef($5));
2751 | k_while {COND_PUSH(1);} expr_value do {COND_POP();}
2756 $$ = NEW_WHILE(cond($3, @1.first_column), $6, 1);
2758 nd_set_column($$, @1.first_column);
2760 $$ = dispatch2(while, $3, $6);
2763 | k_until {COND_PUSH(1);} expr_value do {COND_POP();}
2768 $$ = NEW_UNTIL(cond($3, @1.first_column), $6, 1);
2770 nd_set_column($$, @1.first_column);
2772 $$ = dispatch2(until, $3, $6);
2775 | k_case expr_value opt_terms
2780 $$ = NEW_CASE($2, $4);
2782 nd_set_column($$, @1.first_column);
2784 $$ = dispatch2(case, $2, $4);
2787 | k_case opt_terms case_body k_end
2790 $$ = NEW_CASE(0, $3);
2791 nd_set_line($3, $<num>1);
2792 nd_set_column($$, @1.first_column);
2794 $$ = dispatch2(case, Qnil, $3);
2797 | k_for for_var keyword_in
2808 * e.each{|*x| a, b, c = x}
2812 * e.each{|x| a, = x}
2814 ID id = internal_id();
2815 ID *tbl = ALLOC_N(ID, 2);
2816 NODE *m = NEW_ARGS_AUX(0, 0);
2819 switch (nd_type($2)) {
2821 m->nd_next = node_assign($2, new_for(new_dvar(id, @1.first_column), 0, 0, @1.first_column), @1.first_column);
2822 args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0, @1.first_column));
2826 case NODE_DASGN_CURR:
2827 $2->nd_value = new_dvar(id, @1.first_column);
2830 args = new_args(m, 0, 0, 0, new_args_tail(0, 0, 0, @1.first_column));
2834 NODE *masgn = new_masgn(new_list($2, @1.first_column), 0, @1.first_column);
2835 m->nd_next = node_assign(masgn, new_dvar(id, @1.first_column), @1.first_column);
2836 args = new_args(m, 0, id, 0, new_args_tail(0, 0, 0, @1.first_column));
2840 scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
2841 nd_set_column(scope, @1.first_column);
2842 tbl[0] = 1; tbl[1] = id;
2843 $$ = new_for(0, $5, scope, @1.first_column);
2846 $$ = dispatch3(for, $2, $5, $8);
2849 | k_class cpath superclass
2851 if (in_def || in_single)
2852 yyerror0("class definition in method body");
2855 $<num>$ = ruby_sourceline;
2863 $$ = NEW_CLASS($2, $5, $3);
2864 nd_set_column($$->nd_body, @1.first_column);
2865 set_line_body($5, $<num>4);
2866 nd_set_line($$, $<num>4);
2867 nd_set_column($$, @1.first_column);
2869 $$ = dispatch3(class, $2, $3, $5);
2873 | k_class tLSHFT expr
2875 $<num>$ = (in_def << 1) | in_single;
2885 $$ = NEW_SCLASS($3, $6);
2886 nd_set_column($$->nd_body, @1.first_column);
2887 set_line_body($6, nd_line($3));
2889 nd_set_column($$, @1.first_column);
2891 $$ = dispatch2(sclass, $3, $6);
2894 in_def = ($<num>4 >> 1) & 1;
2895 in_single = $<num>4 & 1;
2899 if (in_def || in_single)
2900 yyerror0("module definition in method body");
2903 $<num>$ = ruby_sourceline;
2911 $$ = NEW_MODULE($2, $4);
2912 nd_set_column($$->nd_body, @1.first_column);
2913 set_line_body($4, $<num>3);
2914 nd_set_line($$, $<num>3);
2915 nd_set_column($$, @1.first_column);
2917 $$ = dispatch2(module, $2, $4);
2924 $<id>$ = current_arg;
2936 NODE *body = remove_begin($6);
2937 reduce_nodes(&body);
2938 $$ = NEW_DEFN($2, $5, body, METHOD_VISI_PRIVATE);
2939 nd_set_column($$->nd_defn, @1.first_column);
2940 set_line_body(body, $<num>1);
2941 nd_set_line($$, $<num>1);
2942 nd_set_column($$, @1.first_column);
2944 $$ = dispatch3(def, $2, $5, $6);
2947 in_def = $<num>4 & 1;
2948 current_arg = $<id>3;
2950 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
2952 $<num>4 = in_single;
2954 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2956 $<id>$ = current_arg;
2964 NODE *body = remove_begin($8);
2965 reduce_nodes(&body);
2966 $$ = NEW_DEFS($2, $5, $7, body);
2967 nd_set_column($$->nd_defn, @1.first_column);
2968 set_line_body(body, $<num>1);
2969 nd_set_line($$, $<num>1);
2970 nd_set_column($$, @1.first_column);
2972 $$ = dispatch5(defs, $2, $<val>3, $5, $7, $8);
2975 in_single = $<num>4 & 1;
2976 current_arg = $<id>6;
2982 nd_set_column($$, @1.first_column);
2984 $$ = dispatch1(break, arg_new());
2991 nd_set_column($$, @1.first_column);
2993 $$ = dispatch1(next, arg_new());
3000 nd_set_column($$, @1.first_column);
3002 $$ = dispatch0(redo);
3009 nd_set_column($$, @1.first_column);
3011 $$ = dispatch0(retry);
3016 primary_value : primary
3021 if (!$$) $$ = NEW_NIL();
3028 k_begin : keyword_begin
3030 token_info_push("begin");
3036 token_info_push("if");
3040 k_unless : keyword_unless
3042 token_info_push("unless");
3046 k_while : keyword_while
3048 token_info_push("while");
3052 k_until : keyword_until
3054 token_info_push("until");
3058 k_case : keyword_case
3060 token_info_push("case");
3062 $<num>$ = ruby_sourceline;
3070 token_info_push("for");
3074 k_class : keyword_class
3076 token_info_push("class");
3080 k_module : keyword_module
3082 token_info_push("module");
3088 token_info_push("def");
3090 $<num>$ = ruby_sourceline;
3098 token_info_pop("end");
3124 | keyword_elsif expr_value then
3129 $$ = new_if($2, $4, $5, @1.first_column);
3132 $$ = dispatch3(elsif, $2, $4, escape_Qundef($5));
3138 | keyword_else compstmt
3143 $$ = dispatch1(else, $2);
3154 $$ = assignable($1, 0, @1.first_column);
3159 | tLPAREN f_margs rparen
3164 $$ = dispatch1(mlhs_paren, $2);
3169 f_marg_list : f_marg
3172 $$ = new_list($1, @1.first_column);
3174 $$ = mlhs_add(mlhs_new(), $1);
3177 | f_marg_list ',' f_marg
3180 $$ = list_append($1, $3, @1.first_column);
3182 $$ = mlhs_add($1, $3);
3187 f_margs : f_marg_list
3190 $$ = new_masgn($1, 0, @1.first_column);
3195 | f_marg_list ',' tSTAR f_norm_arg
3197 $$ = assignable($4, 0, @1.first_column);
3199 $$ = new_masgn($1, $$, @1.first_column);
3201 $$ = mlhs_add_star($1, $$);
3204 | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list
3206 $$ = assignable($4, 0, @1.first_column);
3208 $$ = new_masgn($1, new_postarg($$, $6, @1.first_column), @1.first_column);
3210 $$ = mlhs_add_star($1, $$);
3211 $$ = mlhs_add_post($$, $6);
3214 | f_marg_list ',' tSTAR
3217 $$ = new_masgn($1, (NODE *)-1, @1.first_column);
3219 $$ = mlhs_add_star($1, Qnil);
3222 | f_marg_list ',' tSTAR ',' f_marg_list
3225 $$ = new_masgn($1, new_postarg((NODE *)-1, $5, @1.first_column), @1.first_column);
3227 $$ = mlhs_add_star($1, Qnil);
3228 $$ = mlhs_add_post($$, $5);
3233 $$ = assignable($2, 0, @1.first_column);
3235 $$ = new_masgn(0, $$, @1.first_column);
3237 $$ = mlhs_add_star(mlhs_new(), $$);
3240 | tSTAR f_norm_arg ',' f_marg_list
3242 $$ = assignable($2, 0, @1.first_column);
3244 $$ = new_masgn(0, new_postarg($$, $4, @1.first_column), @1.first_column);
3246 $$ = mlhs_add_star(mlhs_new(), $$);
3247 $$ = mlhs_add_post($$, $4);
3253 $$ = new_masgn(0, (NODE *)-1, @1.first_column);
3255 $$ = mlhs_add_star(mlhs_new(), Qnil);
3258 | tSTAR ',' f_marg_list
3261 $$ = new_masgn(0, new_postarg((NODE *)-1, $3, @1.first_column), @1.first_column);
3263 $$ = mlhs_add_star(mlhs_new(), Qnil);
3264 $$ = mlhs_add_post($$, $3);
3270 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3272 $$ = new_args_tail($1, $3, $4, @1.first_column);
3274 | f_block_kwarg opt_f_block_arg
3276 $$ = new_args_tail($1, Qnone, $2, @1.first_column);
3278 | f_kwrest opt_f_block_arg
3280 $$ = new_args_tail(Qnone, $1, $2, @1.first_column);
3284 $$ = new_args_tail(Qnone, Qnone, $1, @1.first_column);
3288 opt_block_args_tail : ',' block_args_tail
3294 $$ = new_args_tail(Qnone, Qnone, Qnone, @0.first_column);
3298 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3300 $$ = new_args($1, $3, $5, Qnone, $6);
3302 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3304 $$ = new_args($1, $3, $5, $7, $8);
3306 | f_arg ',' f_block_optarg opt_block_args_tail
3308 $$ = new_args($1, $3, Qnone, Qnone, $4);
3310 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3312 $$ = new_args($1, $3, Qnone, $5, $6);
3314 | f_arg ',' f_rest_arg opt_block_args_tail
3316 $$ = new_args($1, Qnone, $3, Qnone, $4);
3320 $$ = new_args($1, Qnone, 1, Qnone, new_args_tail(Qnone, Qnone, Qnone, @1.first_column));
3323 dispatch1(excessed_comma, $$);
3326 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3328 $$ = new_args($1, Qnone, $3, $5, $6);
3330 | f_arg opt_block_args_tail
3332 $$ = new_args($1, Qnone, Qnone, Qnone, $2);
3334 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3336 $$ = new_args(Qnone, $1, $3, Qnone, $4);
3338 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3340 $$ = new_args(Qnone, $1, $3, $5, $6);
3342 | f_block_optarg opt_block_args_tail
3344 $$ = new_args(Qnone, $1, Qnone, Qnone, $2);
3346 | f_block_optarg ',' f_arg opt_block_args_tail
3348 $$ = new_args(Qnone, $1, Qnone, $3, $4);
3350 | f_rest_arg opt_block_args_tail
3352 $$ = new_args(Qnone, Qnone, $1, Qnone, $2);
3354 | f_rest_arg ',' f_arg opt_block_args_tail
3356 $$ = new_args(Qnone, Qnone, $1, $3, $4);
3360 $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1);
3364 opt_block_param : none
3367 command_start = TRUE;
3371 block_param_def : '|' opt_bv_decl '|'
3377 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil),
3386 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil),
3390 | '|' block_param opt_bv_decl '|'
3396 $$ = blockvar_new(escape_Qundef($2), escape_Qundef($3));
3402 opt_bv_decl : opt_nl
3406 | opt_nl ';' bv_decls opt_nl
3420 $$ = rb_ary_new3(1, get_value($1));
3427 rb_ary_push($1, get_value($3));
3447 $<vars>$ = dyna_push();
3451 lpar_beg = ++paren_nest;
3455 $<num>$ = ruby_sourceline;
3458 $<val>$ = cmdarg_stack;
3464 CMDARG_SET($<val>5);
3467 $$ = NEW_LAMBDA($3, $6);
3468 nd_set_line($$, $<num>4);
3469 nd_set_column($$, @1.first_column);
3470 nd_set_column($$->nd_body, @1.first_column);
3472 $$ = dispatch2(lambda, $3, $6);
3478 f_larglist : '(' f_args opt_bv_decl ')'
3483 $$ = dispatch1(paren, $2);
3492 lambda_body : tLAMBEG compstmt '}'
3494 token_info_pop("}");
3497 | keyword_do_LAMBDA compstmt k_end
3503 do_block : keyword_do_block
3506 $<num>$ = ruby_sourceline;
3513 nd_set_line($$, $<num>2);
3518 block_call : command do_block
3521 if (nd_type($1) == NODE_YIELD) {
3522 compile_error(PARSER_ARG "block given to yield");
3525 block_dup_check($1->nd_args, $2);
3531 $$ = method_add_block($1, $2);
3534 | block_call call_op2 operation2 opt_paren_args
3536 $$ = new_qcall($2, $1, $3, $4, @1.first_column);
3538 | block_call call_op2 operation2 opt_paren_args brace_block
3541 block_dup_check($4, $5);
3542 $5->nd_iter = new_command_qcall($2, $1, $3, $4, @1.first_column);
3546 $$ = dispatch4(command_call, $1, $2, $3, $4);
3547 $$ = method_add_block($$, $5);
3550 | block_call call_op2 operation2 command_args do_block
3553 block_dup_check($4, $5);
3554 $5->nd_iter = new_command_qcall($2, $1, $3, $4, @1.first_column);
3558 $$ = dispatch4(command_call, $1, $2, $3, $4);
3559 $$ = method_add_block($$, $5);
3564 method_call : fcall paren_args
3570 $$ = method_arg(dispatch1(fcall, $1), $2);
3573 | primary_value call_op operation2
3576 $<num>$ = ruby_sourceline;
3581 $$ = new_qcall($2, $1, $3, $5, @1.first_column);
3582 nd_set_line($$, $<num>4);
3584 | primary_value tCOLON2 operation2
3587 $<num>$ = ruby_sourceline;
3592 $$ = new_qcall(ID2VAL(idCOLON2), $1, $3, $5, @1.first_column);
3593 nd_set_line($$, $<num>4);
3595 | primary_value tCOLON2 operation3
3597 $$ = new_qcall(ID2VAL(idCOLON2), $1, $3, Qnull, @1.first_column);
3599 | primary_value call_op
3602 $<num>$ = ruby_sourceline;
3607 $$ = new_qcall($2, $1, ID2VAL(idCall), $4, @1.first_column);
3608 nd_set_line($$, $<num>3);
3610 | primary_value tCOLON2
3613 $<num>$ = ruby_sourceline;
3618 $$ = new_qcall(ID2VAL(idCOLON2), $1, ID2VAL(idCall), $4, @1.first_column);
3619 nd_set_line($$, $<num>3);
3621 | keyword_super paren_args
3625 nd_set_column($$, @1.first_column);
3627 $$ = dispatch1(super, $2);
3634 nd_set_column($$, @1.first_column);
3636 $$ = dispatch0(zsuper);
3639 | primary_value '[' opt_call_args rbracket
3642 if ($1 && nd_type($1) == NODE_SELF)
3643 $$ = new_fcall(tAREF, $3, @1.first_column);
3645 $$ = new_call($1, tAREF, $3, @1.first_column);
3648 $$ = dispatch2(aref, $1, escape_Qundef($3));
3656 $<num>$ = ruby_sourceline;
3663 nd_set_line($$, $<num>2);
3669 $<num>$ = ruby_sourceline;
3676 nd_set_line($$, $<num>2);
3681 brace_body : {$<vars>$ = dyna_push();}
3682 {$<val>$ = cmdarg_stack >> 1; CMDARG_SET(0);}
3683 opt_block_param compstmt
3685 $$ = new_brace_body($3, $4, @1.first_column);
3687 CMDARG_SET($<val>2);
3691 do_body : {$<vars>$ = dyna_push();}
3692 {$<val>$ = cmdarg_stack; CMDARG_SET(0);}
3693 opt_block_param bodystmt
3695 $$ = new_do_body($3, $4, @1.first_column);
3697 CMDARG_SET($<val>2);
3701 case_body : keyword_when args then
3706 $$ = NEW_WHEN($2, $4, $5);
3707 nd_set_column($$, @1.first_column);
3709 $$ = dispatch3(when, $2, $4, escape_Qundef($5));
3718 opt_rescue : keyword_rescue exc_list exc_var then
3724 $3 = node_assign($3, new_errinfo(@1.first_column), @1.first_column);
3725 $5 = block_append($3, $5, @1.first_column);
3727 $$ = new_resbody($2, $5, $6, @1.first_column);
3728 fixpos($$, $2?$2:$5);
3730 $$ = dispatch4(rescue,
3740 exc_list : arg_value
3743 $$ = new_list($1, @1.first_column);
3745 $$ = rb_ary_new3(1, get_value($1));
3751 if (!($$ = splat_array($1))) $$ = $1;
3759 exc_var : tASSOC lhs
3766 opt_ensure : keyword_ensure compstmt
3771 $$ = dispatch1(ensure, $2);
3781 $$ = new_lit(ID2SYM($1), @1.first_column);
3783 $$ = dispatch1(symbol_literal, $1);
3794 node = new_str(STR_NEW0(), @1.first_column);
3797 node = evstr2dstr(node, @1.first_column);
3809 nd_set_column($$, @1.first_column);
3817 $$ = literal_concat($1, $2, @1.first_column);
3819 $$ = dispatch2(string_concat, $1, $2);
3824 string1 : tSTRING_BEG string_contents tSTRING_END
3826 $$ = new_string1(heredoc_dedent($2));
3830 xstring : tXSTRING_BEG xstring_contents tSTRING_END
3832 $$ = new_xstring(heredoc_dedent($2), @1.first_column);
3836 regexp : tREGEXP_BEG regexp_contents tREGEXP_END
3838 $$ = new_regexp($2, $3, @1.first_column);
3842 words : tWORDS_BEG ' ' tSTRING_END
3845 $$ = new_zarray(@1.first_column);
3847 $$ = dispatch0(words_new);
3848 $$ = dispatch1(array, $$);
3851 | tWORDS_BEG word_list tSTRING_END
3856 $$ = dispatch1(array, $2);
3861 word_list : /* none */
3866 $$ = dispatch0(words_new);
3869 | word_list word ' '
3872 $$ = list_append($1, evstr2dstr($2, @1.first_column), @1.first_column);
3874 $$ = dispatch2(words_add, $1, $2);
3879 word : string_content
3883 $$ = dispatch0(word_new);
3884 $$ = dispatch2(word_add, $$, $1);
3887 | word string_content
3890 $$ = literal_concat($1, $2, @1.first_column);
3892 $$ = dispatch2(word_add, $1, $2);
3897 symbols : tSYMBOLS_BEG ' ' tSTRING_END
3900 $$ = new_zarray(@1.first_column);
3902 $$ = dispatch0(symbols_new);
3903 $$ = dispatch1(array, $$);
3906 | tSYMBOLS_BEG symbol_list tSTRING_END
3911 $$ = dispatch1(array, $2);
3916 symbol_list : /* none */
3921 $$ = dispatch0(symbols_new);
3924 | symbol_list word ' '
3927 $2 = evstr2dstr($2, @1.first_column);
3928 if (nd_type($2) == NODE_DSTR) {
3929 nd_set_type($2, NODE_DSYM);
3932 nd_set_type($2, NODE_LIT);
3933 $2->nd_lit = rb_str_intern($2->nd_lit);
3935 $$ = list_append($1, $2, @1.first_column);
3937 $$ = dispatch2(symbols_add, $1, $2);
3942 qwords : tQWORDS_BEG ' ' tSTRING_END
3945 $$ = new_zarray(@1.first_column);
3947 $$ = dispatch0(qwords_new);
3948 $$ = dispatch1(array, $$);
3951 | tQWORDS_BEG qword_list tSTRING_END
3956 $$ = dispatch1(array, $2);
3961 qsymbols : tQSYMBOLS_BEG ' ' tSTRING_END
3964 $$ = new_zarray(@1.first_column);
3966 $$ = dispatch0(qsymbols_new);
3967 $$ = dispatch1(array, $$);
3970 | tQSYMBOLS_BEG qsym_list tSTRING_END
3975 $$ = dispatch1(array, $2);
3980 qword_list : /* none */
3985 $$ = dispatch0(qwords_new);
3988 | qword_list tSTRING_CONTENT ' '
3991 $$ = list_append($1, $2, @1.first_column);
3992 nd_set_column($2, @1.first_column);
3994 $$ = dispatch2(qwords_add, $1, $2);
3999 qsym_list : /* none */
4004 $$ = dispatch0(qsymbols_new);
4007 | qsym_list tSTRING_CONTENT ' '
4012 $2->nd_lit = ID2SYM(rb_intern_str(lit));
4013 nd_set_type($2, NODE_LIT);
4014 $$ = list_append($1, $2, @1.first_column);
4015 nd_set_column($2, @1.first_column);
4017 $$ = dispatch2(qsymbols_add, $1, $2);
4022 string_contents : /* none */
4027 $$ = dispatch0(string_content);
4030 | string_contents string_content
4033 $$ = literal_concat($1, $2, @1.first_column);
4035 $$ = dispatch2(string_add, $1, $2);
4040 xstring_contents: /* none */
4045 $$ = dispatch0(xstring_new);
4048 | xstring_contents string_content
4051 $$ = literal_concat($1, $2, @1.first_column);
4053 $$ = dispatch2(xstring_add, $1, $2);
4058 regexp_contents: /* none */
4063 $$ = ripper_new_yylval(0, dispatch0(regexp_new), 0);
4066 | regexp_contents string_content
4069 NODE *head = $1, *tail = $2;
4077 switch (nd_type(head)) {
4079 nd_set_type(head, NODE_DSTR);
4084 head = list_append(new_dstr(Qnil, @1.first_column), head, @1.first_column);
4087 $$ = list_append(head, tail, @1.first_column);
4090 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4091 if (ripper_is_node_yylval(n1)) {
4092 s1 = RNODE(n1)->nd_cval;
4093 n1 = RNODE(n1)->nd_rval;
4095 if (ripper_is_node_yylval(n2)) {
4096 s2 = RNODE(n2)->nd_cval;
4097 n2 = RNODE(n2)->nd_rval;
4099 $$ = dispatch2(regexp_add, n1, n2);
4101 $$ = ripper_new_yylval(0, $$, s2);
4107 string_content : tSTRING_CONTENT
4110 nd_set_column($$, @1.first_column);
4116 $<node>$ = lex_strterm;
4118 SET_LEX_STATE(EXPR_BEG);
4122 lex_strterm = $<node>2;
4125 nd_set_column($$, @1.first_column);
4127 $$ = dispatch1(string_dvar, $3);
4132 $<val>1 = cond_stack;
4133 $<val>$ = cmdarg_stack;
4138 $<node>$ = lex_strterm;
4142 $<num>$ = lex_state;
4143 SET_LEX_STATE(EXPR_BEG);
4146 $<num>$ = brace_nest;
4150 $<num>$ = heredoc_indent;
4153 compstmt tSTRING_DEND
4156 CMDARG_SET($<val>2);
4157 lex_strterm = $<node>3;
4158 SET_LEX_STATE($<num>4);
4159 brace_nest = $<num>5;
4160 heredoc_indent = $<num>6;
4161 heredoc_line_indent = -1;
4163 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4164 $$ = new_evstr($7, @1.first_column);
4166 $$ = dispatch1(string_embexpr, $7);
4174 $$ = new_gvar($1, @1.first_column);
4176 $$ = dispatch1(var_ref, $1);
4182 $$ = new_ivar($1, @1.first_column);
4184 $$ = dispatch1(var_ref, $1);
4191 nd_set_column($$, @1.first_column);
4193 $$ = dispatch1(var_ref, $1);
4199 symbol : tSYMBEG sym
4201 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
4205 $$ = dispatch1(symbol, $2);
4216 dsym : tSYMBEG xstring_contents tSTRING_END
4218 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
4220 $$ = dsym_node($2, @1.first_column);
4222 $$ = dispatch1(dyna_symbol, $2);
4227 numeric : simple_numeric
4228 | tUMINUS_NUM simple_numeric %prec tLOWEST
4232 $$->nd_lit = negate_lit($$->nd_lit);
4234 $$ = dispatch2(unary, ID2VAL(idUMinus), $2);
4239 simple_numeric : tINTEGER
4242 nd_set_column($$, @1.first_column);
4249 nd_set_column($$, @1.first_column);
4256 nd_set_column($$, @1.first_column);
4263 nd_set_column($$, @1.first_column);
4269 user_variable : tIDENTIFIER
4276 keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4277 | keyword_self {$$ = KWD2EID(self, $1);}
4278 | keyword_true {$$ = KWD2EID(true, $1);}
4279 | keyword_false {$$ = KWD2EID(false, $1);}
4280 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4281 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4282 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4285 var_ref : user_variable
4288 if (!($$ = gettable($1, @1.first_column))) $$ = new_begin(0, @1.first_column);
4290 if (id_is_var(get_id($1))) {
4291 $$ = dispatch1(var_ref, $1);
4294 $$ = dispatch1(vcall, $1);
4301 if (!($$ = gettable($1, @1.first_column))) $$ = new_begin(0, @1.first_column);
4303 $$ = dispatch1(var_ref, $1);
4308 var_lhs : user_variable
4310 $$ = assignable(var_field($1), 0, @1.first_column);
4314 $$ = assignable(var_field($1), 0, @1.first_column);
4321 nd_set_column($$, @1.first_column);
4328 nd_set_column($$, @1.first_column);
4336 SET_LEX_STATE(EXPR_BEG);
4337 command_start = TRUE;
4353 f_arglist : '(' f_args rparen
4358 $$ = dispatch1(paren, $2);
4360 SET_LEX_STATE(EXPR_BEG);
4361 command_start = TRUE;
4364 $<num>$ = parser->in_kwarg;
4365 parser->in_kwarg = 1;
4366 SET_LEX_STATE(lex_state|EXPR_LABEL); /* force for args */
4370 parser->in_kwarg = !!$<num>1;
4372 SET_LEX_STATE(EXPR_BEG);
4373 command_start = TRUE;
4377 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4379 $$ = new_args_tail($1, $3, $4, @1.first_column);
4381 | f_kwarg opt_f_block_arg
4383 $$ = new_args_tail($1, Qnone, $2, @1.first_column);
4385 | f_kwrest opt_f_block_arg
4387 $$ = new_args_tail(Qnone, $1, $2, @1.first_column);
4391 $$ = new_args_tail(Qnone, Qnone, $1, @1.first_column);
4395 opt_args_tail : ',' args_tail
4401 $$ = new_args_tail(Qnone, Qnone, Qnone, @0.first_column);
4405 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4407 $$ = new_args($1, $3, $5, Qnone, $6);
4409 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4411 $$ = new_args($1, $3, $5, $7, $8);
4413 | f_arg ',' f_optarg opt_args_tail
4415 $$ = new_args($1, $3, Qnone, Qnone, $4);
4417 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4419 $$ = new_args($1, $3, Qnone, $5, $6);
4421 | f_arg ',' f_rest_arg opt_args_tail
4423 $$ = new_args($1, Qnone, $3, Qnone, $4);
4425 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4427 $$ = new_args($1, Qnone, $3, $5, $6);
4429 | f_arg opt_args_tail
4431 $$ = new_args($1, Qnone, Qnone, Qnone, $2);
4433 | f_optarg ',' f_rest_arg opt_args_tail
4435 $$ = new_args(Qnone, $1, $3, Qnone, $4);
4437 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4439 $$ = new_args(Qnone, $1, $3, $5, $6);
4441 | f_optarg opt_args_tail
4443 $$ = new_args(Qnone, $1, Qnone, Qnone, $2);
4445 | f_optarg ',' f_arg opt_args_tail
4447 $$ = new_args(Qnone, $1, Qnone, $3, $4);
4449 | f_rest_arg opt_args_tail
4451 $$ = new_args(Qnone, Qnone, $1, Qnone, $2);
4453 | f_rest_arg ',' f_arg opt_args_tail
4455 $$ = new_args(Qnone, Qnone, $1, $3, $4);
4459 $$ = new_args(Qnone, Qnone, Qnone, Qnone, $1);
4463 $$ = new_args_tail(Qnone, Qnone, Qnone, @0.first_column);
4464 $$ = new_args(Qnone, Qnone, Qnone, Qnone, $$);
4468 f_bad_arg : tCONSTANT
4471 yyerror0("formal argument cannot be a constant");
4474 $$ = dispatch1(param_error, $1);
4481 yyerror0("formal argument cannot be an instance variable");
4484 $$ = dispatch1(param_error, $1);
4491 yyerror0("formal argument cannot be a global variable");
4494 $$ = dispatch1(param_error, $1);
4501 yyerror0("formal argument cannot be a class variable");
4504 $$ = dispatch1(param_error, $1);
4510 f_norm_arg : f_bad_arg
4513 formal_argument(get_id($1));
4518 f_arg_asgn : f_norm_arg
4527 f_arg_item : f_arg_asgn
4531 $$ = NEW_ARGS_AUX($1, 1);
4536 | tLPAREN f_margs rparen
4538 ID tid = internal_id();
4541 if (dyna_in_block()) {
4542 $2->nd_value = new_dvar(tid, @1.first_column);
4545 $2->nd_value = new_lvar(tid, @1.first_column);
4547 $$ = NEW_ARGS_AUX(tid, 1);
4550 $$ = dispatch1(mlhs_paren, $2);
4559 $$ = rb_ary_new3(1, get_value($1));
4562 | f_arg ',' f_arg_item
4567 $$->nd_next = block_append($$->nd_next, $3->nd_next, @1.first_column);
4568 rb_gc_force_recycle((VALUE)$3);
4570 $$ = rb_ary_push($1, get_value($3));
4579 arg_var(formal_argument(id));
4585 f_kw : f_label arg_value
4588 $$ = assignable($1, $2, @1.first_column);
4590 $$ = new_kw_arg($$, @1.first_column);
4592 $$ = rb_assoc_new(get_value($$), get_value($2));
4598 $$ = assignable($1, (NODE *)-1, @1.first_column);
4600 $$ = new_kw_arg($$, @1.first_column);
4602 $$ = rb_assoc_new(get_value($$), 0);
4607 f_block_kw : f_label primary_value
4609 $$ = assignable($1, $2, @1.first_column);
4611 $$ = new_kw_arg($$, @1.first_column);
4613 $$ = rb_assoc_new(get_value($$), get_value($2));
4618 $$ = assignable($1, (NODE *)-1, @1.first_column);
4620 $$ = new_kw_arg($$, @1.first_column);
4622 $$ = rb_assoc_new(get_value($$), 0);
4627 f_block_kwarg : f_block_kw
4632 $$ = rb_ary_new3(1, get_value($1));
4635 | f_block_kwarg ',' f_block_kw
4638 $$ = kwd_append($1, $3);
4640 $$ = rb_ary_push($1, get_value($3));
4651 $$ = rb_ary_new3(1, get_value($1));
4657 $$ = kwd_append($1, $3);
4659 $$ = rb_ary_push($1, get_value($3));
4668 f_kwrest : kwrest_mark tIDENTIFIER
4670 shadowing_lvar(get_id($2));
4674 $$ = dispatch1(kwrest_param, $2);
4683 $$ = dispatch1(kwrest_param, Qnil);
4688 f_opt : f_arg_asgn '=' arg_value
4691 $$ = assignable($1, $3, @1.first_column);
4693 $$ = NEW_OPT_ARG(0, $$);
4694 nd_set_column($$, @1.first_column);
4696 $$ = rb_assoc_new(get_value($$), get_value($3));
4701 f_block_opt : f_arg_asgn '=' primary_value
4704 $$ = assignable($1, $3, @1.first_column);
4706 $$ = NEW_OPT_ARG(0, $$);
4707 nd_set_column($$, @1.first_column);
4709 $$ = rb_assoc_new(get_value($$), get_value($3));
4714 f_block_optarg : f_block_opt
4719 $$ = rb_ary_new3(1, get_value($1));
4722 | f_block_optarg ',' f_block_opt
4727 while (opts->nd_next) {
4728 opts = opts->nd_next;
4733 $$ = rb_ary_push($1, get_value($3));
4743 $$ = rb_ary_new3(1, get_value($1));
4746 | f_optarg ',' f_opt
4751 while (opts->nd_next) {
4752 opts = opts->nd_next;
4757 $$ = rb_ary_push($1, get_value($3));
4766 f_rest_arg : restarg_mark tIDENTIFIER
4769 if (!is_local_id($2))
4770 yyerror0("rest argument must be local variable");
4772 arg_var(shadowing_lvar(get_id($2)));
4776 $$ = dispatch1(rest_param, $2);
4785 $$ = dispatch1(rest_param, Qnil);
4794 f_block_arg : blkarg_mark tIDENTIFIER
4797 if (!is_local_id($2))
4798 yyerror0("block argument must be local variable");
4799 else if (!dyna_in_block() && local_id($2))
4800 yyerror0("duplicated block argument name");
4802 arg_var(shadowing_lvar(get_id($2)));
4806 $$ = dispatch1(blockarg, $2);
4811 opt_f_block_arg : ',' f_block_arg
4830 if (!$$) $$ = NEW_NIL();
4835 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
4839 yyerror0("can't define singleton method for ().");
4842 switch (nd_type($3)) {
4851 yyerror0("can't define singleton method for literals");
4860 $$ = dispatch1(paren, $3);
4871 $$ = dispatch1(assoclist_from_args, $1);
4880 $$ = rb_ary_new3(1, get_value($1));
4892 if (assocs->nd_head &&
4893 !tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY &&
4894 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
4896 tail = tail->nd_next->nd_head->nd_head;
4898 assocs = list_concat(assocs, tail);
4902 $$ = rb_ary_push($1, get_value($3));
4907 assoc : arg_value tASSOC arg_value
4910 if (nd_type($1) == NODE_STR) {
4911 nd_set_type($1, NODE_LIT);
4912 $1->nd_lit = rb_fstring($1->nd_lit);
4914 $$ = list_append(new_list($1, @1.first_column), $3, @1.first_column);
4916 $$ = dispatch2(assoc_new, $1, $3);
4922 $$ = list_append(new_list(new_lit(ID2SYM($1), @1.first_column), @1.first_column), $2, @1.first_column);
4924 $$ = dispatch2(assoc_new, $1, $2);
4927 | tSTRING_BEG string_contents tLABEL_END arg_value
4930 $$ = list_append(new_list(dsym_node($2, @1.first_column), @1.first_column), $4, @1.first_column);
4932 $$ = dispatch2(assoc_new, dispatch1(dyna_symbol, $2), $4);
4938 if (nd_type($2) == NODE_HASH &&
4939 !($2->nd_head && $2->nd_head->nd_alen))
4942 $$ = list_append(new_list(0, @1.first_column), $2, @1.first_column);
4944 $$ = dispatch1(assoc_splat, $2);
4949 operation : tIDENTIFIER
4954 operation2 : tIDENTIFIER
4960 operation3 : tIDENTIFIER
4971 $$ = TOKEN2VAL('.');
4975 $$ = ID2VAL(idANDDOT);
4982 $$ = ID2VAL(idCOLON2);
4986 opt_terms : /* none */
4997 rbracket : opt_nl ']'
5000 trailer : /* none */
5005 term : ';' {yyerrok;token_flush(parser);}
5006 | '\n' {token_flush(parser);}
5010 | terms ';' {yyerrok;}
5022 # define yylval (*parser->lval)
5024 static int parser_regx_options(struct parser_params*);
5025 static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**);
5026 static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc);
5027 static enum yytokentype parser_parse_string(struct parser_params*,NODE*);
5028 static enum yytokentype parser_here_document(struct parser_params*,NODE*);
5031 # define nextc() parser_nextc(parser)
5032 # define pushback(c) parser_pushback(parser, (c))
5033 # define newtok() parser_newtok(parser)
5034 # define tokspace(n) parser_tokspace(parser, (n))
5035 # define tokadd(c) parser_tokadd(parser, (c))
5036 # define tok_hex(numlen) parser_tok_hex(parser, (numlen))
5037 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e))
5038 # define tokadd_escape(e) parser_tokadd_escape(parser, (e))
5039 # define regx_options() parser_regx_options(parser)
5040 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e))
5041 # define parse_string(n) parser_parse_string(parser,(n))
5042 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc))
5043 # define here_document(n) parser_here_document(parser,(n))
5044 # define heredoc_identifier() parser_heredoc_identifier(parser)
5045 # define heredoc_restore(n) parser_heredoc_restore(parser,(n))
5046 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i))
5047 # define number_literal_suffix(f) parser_number_literal_suffix(parser, (f))
5048 # define set_number_literal(v, t, f) parser_set_number_literal(parser, (v), (t), (f))
5049 # define set_integer_literal(v, f) parser_set_integer_literal(parser, (v), (f))
5052 # define set_yylval_str(x) (yylval.node = NEW_STR(x))
5053 # define set_yylval_num(x) (yylval.num = (x))
5054 # define set_yylval_id(x) (yylval.id = (x))
5055 # define set_yylval_name(x) (yylval.id = (x))
5056 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x))
5057 # define set_yylval_node(x) (yylval.node = (x))
5058 # define yylval_id() (yylval.id)
5061 ripper_yylval_id(ID x)
5063 return ripper_new_yylval(x, ID2SYM(x), 0);
5065 # define set_yylval_str(x) (yylval.val = (x))
5066 # define set_yylval_num(x) (yylval.val = ripper_new_yylval((x), 0, 0))
5067 # define set_yylval_id(x) (void)(x)
5068 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
5069 # define set_yylval_literal(x) (void)(x)
5070 # define set_yylval_node(x) (void)(x)
5071 # define yylval_id() yylval.id
5075 #define literal_flush(p) (parser->tokp = (p))
5076 #define dispatch_scan_event(t) ((void)0)
5077 #define dispatch_delayed_token(t) ((void)0)
5078 #define has_delayed_token() (0)
5080 #define literal_flush(p) ((void)0)
5082 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5085 intern_sym(const char *name)
5087 ID id = rb_intern_const(name);
5092 ripper_has_scan_event(struct parser_params *parser)
5095 if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
5096 return lex_p > parser->tokp;
5100 ripper_scan_event_val(struct parser_params *parser, int t)
5102 VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp);
5103 VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
5104 token_flush(parser);
5109 ripper_dispatch_scan_event(struct parser_params *parser, int t)
5111 if (!ripper_has_scan_event(parser)) return;
5112 yylval_rval = ripper_scan_event_val(parser, t);
5114 #define dispatch_scan_event(t) ripper_dispatch_scan_event(parser, t)
5117 ripper_dispatch_delayed_token(struct parser_params *parser, int t)
5119 int saved_line = ruby_sourceline;
5120 const char *saved_tokp = parser->tokp;
5122 ruby_sourceline = parser->delayed_line;
5123 parser->tokp = lex_pbeg + parser->delayed_col;
5124 yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed);
5125 parser->delayed = Qnil;
5126 ruby_sourceline = saved_line;
5127 parser->tokp = saved_tokp;
5129 #define dispatch_delayed_token(t) ripper_dispatch_delayed_token(parser, t)
5130 #define has_delayed_token() (!NIL_P(parser->delayed))
5133 #include "ruby/regex.h"
5134 #include "ruby/util.h"
5136 #define parser_encoding_name() (current_enc->name)
5137 #define parser_mbclen() mbclen((lex_p-1),lex_pend,current_enc)
5138 #define is_identchar(p,e,enc) (rb_enc_isalnum((unsigned char)(*(p)),(enc)) || (*(p)) == '_' || !ISASCII(*(p)))
5139 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,current_enc))
5141 #define parser_isascii() ISASCII(*(lex_p-1))
5144 token_info_get_column(struct parser_params *parser, const char *pend)
5148 for (p = lex_pbeg; p < pend; p++) {
5150 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5158 token_info_has_nonspaces(struct parser_params *parser, const char *pend)
5161 for (p = lex_pbeg; p < pend; p++) {
5162 if (*p != ' ' && *p != '\t') {
5170 token_info_push_gen(struct parser_params *parser, const char *token, size_t len)
5173 const char *t = lex_p - len;
5175 if (!parser->token_info_enabled) return;
5176 ptinfo = ALLOC(token_info);
5177 ptinfo->token = token;
5178 ptinfo->linenum = ruby_sourceline;
5179 ptinfo->column = token_info_get_column(parser, t);
5180 ptinfo->nonspc = token_info_has_nonspaces(parser, t);
5181 ptinfo->next = parser->token_info;
5183 parser->token_info = ptinfo;
5187 token_info_pop_gen(struct parser_params *parser, const char *token, size_t len)
5190 token_info *ptinfo = parser->token_info;
5191 const char *t = lex_p - len;
5193 if (!ptinfo) return;
5194 parser->token_info = ptinfo->next;
5195 linenum = ruby_sourceline;
5196 if (parser->token_info_enabled &&
5197 linenum != ptinfo->linenum && !ptinfo->nonspc &&
5198 !token_info_has_nonspaces(parser, t) &&
5199 token_info_get_column(parser, t) != ptinfo->column) {
5201 "mismatched indentations at '%s' with '%s' at %d",
5202 WARN_S(token), WARN_S(ptinfo->token), WARN_I(ptinfo->linenum));
5209 parser_precise_mbclen(struct parser_params *parser, const char *p)
5211 int len = rb_enc_precise_mbclen(p, lex_pend, current_enc);
5212 if (!MBCLEN_CHARFOUND_P(len)) {
5213 compile_error(PARSER_ARG "invalid multibyte char (%s)", parser_encoding_name());
5220 parser_yyerror(struct parser_params *parser, const char *msg)
5223 const int max_line_margin = 30;
5225 const char *pre = "", *post = "", *pend;
5226 const char *code = "", *caret = "", *newline = "";
5233 if (pend > lex_pbeg && pend[-1] == '\n') {
5234 if (--pend > lex_pbeg && pend[-1] == '\r') --pend;
5237 p = pe = lex_p < pend ? lex_p : pend;
5238 lim = p - lex_pbeg > max_line_margin ? p - max_line_margin : lex_pbeg;
5239 while ((lim < p) && (*(p-1) != '\n')) p--;
5241 lim = pend - pe > max_line_margin ? pe + max_line_margin : pend;
5242 while ((pe < lim) && (*pe != '\n')) pe++;
5249 p = rb_enc_prev_char(lex_pbeg, p, lex_p, rb_enc_get(lex_lastline));
5250 if (p > lex_pbeg) pre = "...";
5253 pe = rb_enc_prev_char(lex_p, pe, pend, rb_enc_get(lex_lastline));
5254 if (pe < pend) post = "...";
5257 lim = lex_p < pend ? lex_p : pend;
5259 buf = ALLOCA_N(char, i+2);
5262 pe = (parser->tokp < lim ? parser->tokp : lim);
5265 *p2++ = *p++ == '\t' ? '\t' : ' ';
5271 memset(p2, '~', (lim - p));
5280 compile_error(PARSER_ARG "%s%s""%s%.*s%s%s""%s%s",
5282 pre, (int)len, code, post, newline,
5285 dispatch1(parse_error, STR_NEW2(msg));
5287 #endif /* !RIPPER */
5292 vtable_size(const struct vtable *tbl)
5294 if (POINTER_P(tbl)) {
5302 static struct vtable *
5303 vtable_alloc_gen(struct parser_params *parser, int line, struct vtable *prev)
5305 struct vtable *tbl = ALLOC(struct vtable);
5308 tbl->tbl = ALLOC_N(ID, tbl->capa);
5312 rb_parser_printf(parser, "vtable_alloc:%d: %p\n", line, tbl);
5317 #define vtable_alloc(prev) vtable_alloc_gen(parser, __LINE__, prev)
5320 vtable_free_gen(struct parser_params *parser, int line, const char *name,
5325 rb_parser_printf(parser, "vtable_free:%d: %s(%p)\n", line, name, tbl);
5328 if (POINTER_P(tbl)) {
5335 #define vtable_free(tbl) vtable_free_gen(parser, __LINE__, #tbl, tbl)
5338 vtable_add_gen(struct parser_params *parser, int line, const char *name,
5339 struct vtable *tbl, ID id)
5343 rb_parser_printf(parser, "vtable_add:%d: %s(%p), %s\n",
5344 line, name, tbl, rb_id2name(id));
5347 if (!POINTER_P(tbl)) {
5348 rb_parser_fatal(parser, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5351 if (tbl->pos == tbl->capa) {
5352 tbl->capa = tbl->capa * 2;
5353 REALLOC_N(tbl->tbl, ID, tbl->capa);
5355 tbl->tbl[tbl->pos++] = id;
5357 #define vtable_add(tbl, id) vtable_add_gen(parser, __LINE__, #tbl, tbl, id)
5361 vtable_pop_gen(struct parser_params *parser, int line, const char *name,
5362 struct vtable *tbl, int n)
5365 rb_parser_printf(parser, "vtable_pop:%d: %s(%p), %d\n",
5366 line, name, tbl, n);
5369 rb_parser_fatal(parser, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5374 #define vtable_pop(tbl, n) vtable_pop_gen(parser, __LINE__, #tbl, tbl, n)
5378 vtable_included(const struct vtable * tbl, ID id)
5382 if (POINTER_P(tbl)) {
5383 for (i = 0; i < tbl->pos; i++) {
5384 if (tbl->tbl[i] == id) {
5392 static void parser_prepare(struct parser_params *parser);
5396 debug_lines(VALUE fname)
5399 CONST_ID(script_lines, "SCRIPT_LINES__");
5400 if (rb_const_defined_at(rb_cObject, script_lines)) {
5401 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5402 if (RB_TYPE_P(hash, T_HASH)) {
5403 VALUE lines = rb_ary_new();
5404 rb_hash_aset(hash, fname, lines);
5412 coverage(VALUE fname, int n)
5414 VALUE coverages = rb_get_coverages();
5415 if (RTEST(coverages) && RBASIC(coverages)->klass == 0) {
5416 VALUE coverage = rb_default_coverage(n);
5417 VALUE lines = RARRAY_AREF(coverage, COVERAGE_INDEX_LINES);
5419 rb_hash_aset(coverages, fname, coverage);
5421 return lines == Qnil ? Qfalse : lines;
5427 e_option_supplied(struct parser_params *parser)
5429 return strcmp(ruby_sourcefile, "-e") == 0;
5433 yycompile0(VALUE arg)
5437 struct parser_params *parser = (struct parser_params *)arg;
5440 if (!compile_for_eval && rb_safe_level() == 0) {
5441 ruby_debug_lines = debug_lines(ruby_sourcefile_string);
5442 if (ruby_debug_lines && ruby_sourceline > 0) {
5443 VALUE str = STR_NEW0();
5444 n = ruby_sourceline;
5446 rb_ary_push(ruby_debug_lines, str);
5450 if (!e_option_supplied(parser)) {
5451 ruby_coverage = coverage(ruby_sourcefile_string, ruby_sourceline);
5456 parser_prepare(parser);
5458 #define RUBY_DTRACE_PARSE_HOOK(name) \
5459 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
5460 RUBY_DTRACE_PARSE_##name(ruby_sourcefile, ruby_sourceline); \
5462 RUBY_DTRACE_PARSE_HOOK(BEGIN);
5464 n = yyparse((void*)parser);
5466 RUBY_DTRACE_PARSE_HOOK(END);
5468 ruby_debug_lines = 0;
5472 lex_p = lex_pbeg = lex_pend = 0;
5473 lex_lastline = lex_nextline = 0;
5474 if (parser->error_p) {
5475 VALUE mesg = parser->error_buffer;
5477 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
5479 rb_set_errinfo(mesg);
5482 tree = ruby_eval_tree;
5487 VALUE opt = parser->compile_option;
5489 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
5490 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
5491 prelude = NEW_PRELUDE(ruby_eval_tree_begin, tree->nd_body, opt);
5492 nd_set_column(prelude, nd_column(tree->nd_body));
5493 tree->nd_body = prelude;
5499 yycompile(struct parser_params *parser, VALUE fname, int line)
5501 ruby_sourcefile_string = rb_str_new_frozen(fname);
5502 ruby_sourcefile = RSTRING_PTR(fname);
5503 ruby_sourceline = line - 1;
5504 return (NODE *)rb_suppress_tracing(yycompile0, (VALUE)parser);
5506 #endif /* !RIPPER */
5508 static rb_encoding *
5509 must_be_ascii_compatible(VALUE s)
5511 rb_encoding *enc = rb_enc_get(s);
5512 if (!rb_enc_asciicompat(enc)) {
5513 rb_raise(rb_eArgError, "invalid source encoding");
5519 lex_get_str(struct parser_params *parser, VALUE s)
5521 char *beg, *end, *start;
5524 beg = RSTRING_PTR(s);
5525 len = RSTRING_LEN(s);
5528 if (len == lex_gets_ptr) return Qnil;
5529 beg += lex_gets_ptr;
5530 len -= lex_gets_ptr;
5532 end = memchr(beg, '\n', len);
5533 if (end) len = ++end - beg;
5534 lex_gets_ptr += len;
5535 return rb_str_subseq(s, beg - start, len);
5539 lex_getline(struct parser_params *parser)
5541 VALUE line = (*lex_gets)(parser, lex_input);
5542 if (NIL_P(line)) return line;
5543 must_be_ascii_compatible(line);
5545 if (ruby_debug_lines) {
5546 rb_enc_associate(line, current_enc);
5547 rb_ary_push(ruby_debug_lines, line);
5549 if (ruby_coverage) {
5550 rb_ary_push(ruby_coverage, Qnil);
5556 static const rb_data_type_t parser_data_type;
5560 parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
5562 struct parser_params *parser;
5565 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
5566 lex_gets = lex_get_str;
5568 lex_input = rb_str_new_frozen(s);
5569 lex_pbeg = lex_p = lex_pend = 0;
5571 node = yycompile(parser, fname, line);
5572 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
5578 rb_compile_string(const char *f, VALUE s, int line)
5580 must_be_ascii_compatible(s);
5581 return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), s, line);
5585 rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
5587 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
5591 rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
5593 must_be_ascii_compatible(s);
5594 return parser_compile_string(vparser, f, s, line);
5598 rb_compile_cstr(const char *f, const char *s, int len, int line)
5600 VALUE str = rb_str_new(s, len);
5601 return parser_compile_string(rb_parser_new(), rb_filesystem_str_new_cstr(f), str, line);
5605 rb_parser_compile_cstr(VALUE vparser, const char *f, const char *s, int len, int line)
5607 VALUE str = rb_str_new(s, len);
5608 return parser_compile_string(vparser, rb_filesystem_str_new_cstr(f), str, line);
5611 VALUE rb_io_gets_internal(VALUE io);
5614 lex_io_gets(struct parser_params *parser, VALUE io)
5616 return rb_io_gets_internal(io);
5620 rb_compile_file(const char *f, VALUE file, int start)
5622 VALUE vparser = rb_parser_new();
5624 return rb_parser_compile_file(vparser, f, file, start);
5628 rb_parser_compile_file(VALUE vparser, const char *f, VALUE file, int start)
5630 return rb_parser_compile_file_path(vparser, rb_filesystem_str_new_cstr(f), file, start);
5634 rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
5636 struct parser_params *parser;
5639 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
5640 lex_gets = lex_io_gets;
5642 lex_pbeg = lex_p = lex_pend = 0;
5644 node = yycompile(parser, fname, start);
5645 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
5649 #endif /* !RIPPER */
5651 #define STR_FUNC_ESCAPE 0x01
5652 #define STR_FUNC_EXPAND 0x02
5653 #define STR_FUNC_REGEXP 0x04
5654 #define STR_FUNC_QWORDS 0x08
5655 #define STR_FUNC_SYMBOL 0x10
5656 #define STR_FUNC_INDENT 0x20
5657 #define STR_FUNC_LABEL 0x40
5658 #define STR_FUNC_TERM 0x8000
5661 str_label = STR_FUNC_LABEL,
5663 str_dquote = (STR_FUNC_EXPAND),
5664 str_xquote = (STR_FUNC_EXPAND),
5665 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
5666 str_sword = (STR_FUNC_QWORDS),
5667 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
5668 str_ssym = (STR_FUNC_SYMBOL),
5669 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
5673 parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *enc0)
5677 str = rb_enc_str_new(p, n, enc);
5678 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
5679 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
5681 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
5682 rb_enc_associate(str, rb_ascii8bit_encoding());
5689 #define lex_goto_eol(parser) ((parser)->lex.pcur = (parser)->lex.pend)
5690 #define lex_eol_p() (lex_p >= lex_pend)
5691 #define peek(c) peek_n((c), 0)
5692 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n])
5693 #define peekc() peekc_n(0)
5694 #define peekc_n(n) (lex_p+(n) < lex_pend ? (unsigned char)lex_p[n] : -1)
5697 parser_nextline(struct parser_params *parser)
5699 VALUE v = lex_nextline;
5705 if (!lex_input || NIL_P(v = lex_getline(parser))) {
5707 lex_goto_eol(parser);
5710 parser->cr_seen = FALSE;
5713 if (parser->tokp < lex_pend) {
5714 if (!has_delayed_token()) {
5715 parser->delayed = rb_str_buf_new(1024);
5716 rb_enc_associate(parser->delayed, current_enc);
5717 rb_str_buf_cat(parser->delayed,
5718 parser->tokp, lex_pend - parser->tokp);
5719 parser->delayed_line = ruby_sourceline;
5720 parser->delayed_col = (int)(parser->tokp - lex_pbeg);
5723 rb_str_buf_cat(parser->delayed,
5724 parser->tokp, lex_pend - parser->tokp);
5728 if (heredoc_end > 0) {
5729 ruby_sourceline = heredoc_end;
5733 parser->line_count++;
5734 lex_pbeg = lex_p = RSTRING_PTR(v);
5735 lex_pend = lex_p + RSTRING_LEN(v);
5736 token_flush(parser);
5742 parser_cr(struct parser_params *parser, int c)
5748 else if (!parser->cr_seen) {
5749 parser->cr_seen = TRUE;
5750 /* carried over with lex_nextline for nextc() */
5751 rb_warn0("encountered \\r in middle of line, treated as a mere space");
5757 parser_nextc(struct parser_params *parser)
5761 if (UNLIKELY(lex_p == lex_pend)) {
5762 if (parser_nextline(parser)) return -1;
5764 c = (unsigned char)*lex_p++;
5765 if (UNLIKELY(c == '\r')) {
5766 c = parser_cr(parser, c);
5773 parser_pushback(struct parser_params *parser, int c)
5775 if (c == -1) return;
5777 if (lex_p > lex_pbeg && lex_p[0] == '\n' && lex_p[-1] == '\r') {
5782 #define was_bol() (lex_p == lex_pbeg + 1)
5784 #define tokfix() (tokenbuf[tokidx]='\0')
5785 #define tok() tokenbuf
5786 #define toklen() tokidx
5787 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
5790 parser_newtok(struct parser_params *parser)
5793 tokline = ruby_sourceline;
5796 tokenbuf = ALLOC_N(char, 60);
5798 if (toksiz > 4096) {
5800 REALLOC_N(tokenbuf, char, 60);
5806 parser_tokspace(struct parser_params *parser, int n)
5810 if (tokidx >= toksiz) {
5811 do {toksiz *= 2;} while (toksiz < tokidx);
5812 REALLOC_N(tokenbuf, char, toksiz);
5814 return &tokenbuf[tokidx-n];
5818 parser_tokadd(struct parser_params *parser, int c)
5820 tokenbuf[tokidx++] = (char)c;
5821 if (tokidx >= toksiz) {
5823 REALLOC_N(tokenbuf, char, toksiz);
5828 parser_tok_hex(struct parser_params *parser, size_t *numlen)
5832 c = scan_hex(lex_p, 2, numlen);
5834 parser->tokp = lex_p;
5835 yyerror0("invalid hex escape");
5842 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n))
5845 parser_tokadd_codepoint(struct parser_params *parser, rb_encoding **encp,
5846 int regexp_literal, int wide)
5849 int codepoint = scan_hex(lex_p, wide ? lex_pend - lex_p : 4, &numlen);
5850 literal_flush(lex_p);
5852 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
5853 yyerror0("invalid Unicode escape");
5854 return wide && numlen > 0;
5856 if (codepoint > 0x10ffff) {
5857 yyerror0("invalid Unicode codepoint (too large)");
5860 if ((codepoint & 0xfffff800) == 0xd800) {
5861 yyerror0("invalid Unicode codepoint");
5864 if (regexp_literal) {
5865 tokcopy((int)numlen);
5867 else if (codepoint >= 0x80) {
5868 rb_encoding *utf8 = rb_utf8_encoding();
5869 if (*encp && utf8 != *encp) {
5870 static const char mixed_utf8[] = "UTF-8 mixed within %s source";
5871 size_t len = sizeof(mixed_utf8) - 2 + strlen(rb_enc_name(*encp));
5872 char *mesg = alloca(len);
5873 snprintf(mesg, len, mixed_utf8, rb_enc_name(*encp));
5878 tokaddmbc(codepoint, *encp);
5886 /* return value is for ?\u3042 */
5888 parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp,
5889 int string_literal, int symbol_literal, int regexp_literal)
5892 * If string_literal is true, then we allow multiple codepoints
5893 * in \u{}, and add the codepoints to the current token.
5894 * Otherwise we're parsing a character literal and return a single
5895 * codepoint without adding it
5898 const int open_brace = '{', close_brace = '}';
5900 if (regexp_literal) { tokadd('\\'); tokadd('u'); }
5902 if (peek(open_brace)) { /* handle \u{...} form */
5903 int c, last = nextc();
5904 if (lex_p >= lex_pend) goto unterminated;
5905 while (ISSPACE(c = *lex_p) && ++lex_p < lex_pend);
5907 if (regexp_literal) tokadd(last);
5908 if (!parser_tokadd_codepoint(parser, encp, regexp_literal, TRUE)) {
5911 while (ISSPACE(c = *lex_p)) {
5912 if (++lex_p >= lex_pend) goto unterminated;
5915 } while (c != close_brace);
5917 if (c != close_brace) {
5919 literal_flush(lex_p);
5920 yyerror0("unterminated Unicode escape");
5924 if (regexp_literal) tokadd(close_brace);
5927 else { /* handle \uxxxx form */
5928 if (!parser_tokadd_codepoint(parser, encp, regexp_literal, FALSE)) {
5936 #define ESCAPE_CONTROL 1
5937 #define ESCAPE_META 2
5940 parser_read_escape(struct parser_params *parser, int flags,
5946 switch (c = nextc()) {
5947 case '\\': /* Backslash */
5950 case 'n': /* newline */
5953 case 't': /* horizontal tab */
5956 case 'r': /* carriage-return */
5959 case 'f': /* form-feed */
5962 case 'v': /* vertical tab */
5965 case 'a': /* alarm(bell) */
5968 case 'e': /* escape */
5971 case '0': case '1': case '2': case '3': /* octal constant */
5972 case '4': case '5': case '6': case '7':
5974 c = scan_oct(lex_p, 3, &numlen);
5978 case 'x': /* hex constant */
5979 c = tok_hex(&numlen);
5980 if (numlen == 0) return 0;
5983 case 'b': /* backspace */
5986 case 's': /* space */
5990 if (flags & ESCAPE_META) goto eof;
5991 if ((c = nextc()) != '-') {
5994 if ((c = nextc()) == '\\') {
5995 if (peek('u')) goto eof;
5996 return read_escape(flags|ESCAPE_META, encp) | 0x80;
5998 else if (c == -1 || !ISASCII(c)) goto eof;
6000 return ((c & 0xff) | 0x80);
6004 if ((c = nextc()) != '-') {
6008 if (flags & ESCAPE_CONTROL) goto eof;
6009 if ((c = nextc())== '\\') {
6010 if (peek('u')) goto eof;
6011 c = read_escape(flags|ESCAPE_CONTROL, encp);
6015 else if (c == -1 || !ISASCII(c)) goto eof;
6020 yyerror0("Invalid escape character syntax");
6030 parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc)
6032 int len = rb_enc_codelen(c, enc);
6033 rb_enc_mbcput(c, tokspace(len), enc);
6037 parser_tokadd_escape(struct parser_params *parser, rb_encoding **encp)
6044 switch (c = nextc()) {
6046 return 0; /* just ignore */
6048 case '0': case '1': case '2': case '3': /* octal constant */
6049 case '4': case '5': case '6': case '7':
6051 ruby_scan_oct(--lex_p, 3, &numlen);
6052 if (numlen == 0) goto eof;
6054 tokcopy((int)numlen + 1);
6058 case 'x': /* hex constant */
6061 if (numlen == 0) return -1;
6062 tokcopy((int)numlen + 2);
6067 if (flags & ESCAPE_META) goto eof;
6068 if ((c = nextc()) != '-') {
6073 flags |= ESCAPE_META;
6077 if (flags & ESCAPE_CONTROL) goto eof;
6078 if ((c = nextc()) != '-') {
6086 if (flags & ESCAPE_CONTROL) goto eof;
6088 flags |= ESCAPE_CONTROL;
6090 if ((c = nextc()) == '\\') {
6093 else if (c == -1) goto eof;
6099 yyerror0("Invalid escape character syntax");
6110 parser_regx_options(struct parser_params *parser)
6118 while (c = nextc(), ISALPHA(c)) {
6120 options |= RE_OPTION_ONCE;
6122 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6124 if (kc != rb_ascii8bit_encindex()) kcode = c;
6139 compile_error(PARSER_ARG "unknown regexp option%s - %s",
6140 toklen() > 1 ? "s" : "", tok());
6142 return options | RE_OPTION_ENCODING(kcode);
6146 dispose_string(VALUE str)
6149 rb_gc_force_recycle(str);
6153 parser_tokadd_mbchar(struct parser_params *parser, int c)
6155 int len = parser_precise_mbclen(parser, lex_p-1);
6156 if (len < 0) return -1;
6159 if (len > 0) tokcopy(len);
6163 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
6166 simple_re_meta(int c)
6169 case '$': case '*': case '+': case '.':
6170 case '?': case '^': case '|':
6171 case ')': case ']': case '}': case '>':
6179 parser_update_heredoc_indent(struct parser_params *parser, int c)
6181 if (heredoc_line_indent == -1) {
6182 if (c == '\n') heredoc_line_indent = 0;
6186 heredoc_line_indent++;
6189 else if (c == '\t') {
6190 int w = (heredoc_line_indent / TAB_WIDTH) + 1;
6191 heredoc_line_indent = w * TAB_WIDTH;
6194 else if (c != '\n') {
6195 if (heredoc_indent > heredoc_line_indent) {
6196 heredoc_indent = heredoc_line_indent;
6198 heredoc_line_indent = -1;
6205 parser_tokadd_string(struct parser_params *parser,
6206 int func, int term, int paren, long *nest,
6210 rb_encoding *enc = 0;
6212 static const char mixed_msg[] = "%s mixed within %s source";
6214 #define mixed_error(enc1, enc2) if (!errbuf) { \
6215 size_t len = sizeof(mixed_msg) - 4; \
6216 len += strlen(rb_enc_name(enc1)); \
6217 len += strlen(rb_enc_name(enc2)); \
6218 errbuf = ALLOCA_N(char, len); \
6219 snprintf(errbuf, len, mixed_msg, \
6220 rb_enc_name(enc1), \
6221 rb_enc_name(enc2)); \
6224 #define mixed_escape(beg, enc1, enc2) do { \
6225 const char *pos = lex_p; \
6227 mixed_error((enc1), (enc2)); \
6231 while ((c = nextc()) != -1) {
6232 if (heredoc_indent > 0) {
6233 parser_update_heredoc_indent(parser, c);
6236 if (paren && c == paren) {
6239 else if (c == term) {
6240 if (!nest || !*nest) {
6246 else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) {
6248 if (c2 == '$' || c2 == '@' || c2 == '{') {
6253 else if (c == '\\') {
6254 literal_flush(lex_p - 1);
6258 if (func & STR_FUNC_QWORDS) break;
6259 if (func & STR_FUNC_EXPAND) continue;
6264 if (func & STR_FUNC_ESCAPE) tokadd(c);
6268 if ((func & STR_FUNC_EXPAND) == 0) {
6272 if (!parser_tokadd_utf8(parser, &enc, term,
6273 func & STR_FUNC_SYMBOL,
6274 func & STR_FUNC_REGEXP)) {
6280 if (c == -1) return -1;
6282 if ((func & STR_FUNC_EXPAND) == 0) tokadd('\\');
6285 if (func & STR_FUNC_REGEXP) {
6286 if (c == term && !simple_re_meta(c)) {
6291 if ((c = tokadd_escape(&enc)) < 0)
6293 if (enc && enc != *encp) {
6294 mixed_escape(parser->tokp+2, enc, *encp);
6298 else if (func & STR_FUNC_EXPAND) {
6300 if (func & STR_FUNC_ESCAPE) tokadd('\\');
6301 c = read_escape(0, &enc);
6303 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6304 /* ignore backslashed spaces in %w */
6306 else if (c != term && !(paren && c == paren)) {
6313 else if (!parser_isascii()) {
6318 else if (enc != *encp) {
6319 mixed_error(enc, *encp);
6322 if (tokadd_mbchar(c) == -1) return -1;
6325 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6333 else if (enc != *encp) {
6334 mixed_error(enc, *encp);
6340 if (enc) *encp = enc;
6344 #define NEW_STRTERM(func, term, paren) \
6345 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0)
6349 token_flush_string_content(struct parser_params *parser, rb_encoding *enc)
6351 VALUE content = yylval.val;
6352 if (!ripper_is_node_yylval(content))
6353 content = ripper_new_yylval(0, 0, content);
6354 if (has_delayed_token()) {
6355 ptrdiff_t len = lex_p - parser->tokp;
6357 rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc);
6359 dispatch_delayed_token(tSTRING_CONTENT);
6360 parser->tokp = lex_p;
6361 RNODE(content)->nd_rval = yylval.val;
6363 dispatch_scan_event(tSTRING_CONTENT);
6364 if (yylval.val != content)
6365 RNODE(content)->nd_rval = yylval.val;
6366 yylval.val = content;
6369 #define flush_string_content(enc) token_flush_string_content(parser, (enc))
6371 #define flush_string_content(enc) ((void)(enc))
6374 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
6375 /* this can be shared with ripper, since it's independent from struct
6378 #define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
6379 #define SPECIAL_PUNCT(idx) ( \
6380 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
6381 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
6382 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
6383 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
6384 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
6386 const unsigned int ruby_global_name_punct_bits[] = {
6392 #undef SPECIAL_PUNCT
6395 static enum yytokentype
6396 parser_peek_variable_name(struct parser_params *parser)
6399 const char *p = lex_p;
6401 if (p + 1 >= lex_pend) return 0;
6405 if ((c = *p) == '-') {
6406 if (++p >= lex_pend) return 0;
6409 else if (is_global_name_punct(c) || ISDIGIT(c)) {
6410 return tSTRING_DVAR;
6414 if ((c = *p) == '@') {
6415 if (++p >= lex_pend) return 0;
6421 command_start = TRUE;
6422 return tSTRING_DBEG;
6426 if (!ISASCII(c) || c == '_' || ISALPHA(c))
6427 return tSTRING_DVAR;
6431 #define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
6432 #define IS_END() IS_lex_state(EXPR_END_ANY)
6433 #define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
6434 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
6435 #define IS_LABEL_POSSIBLE() (\
6436 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
6438 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1))
6439 #define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
6441 static inline enum yytokentype
6442 parser_string_term(struct parser_params *parser, int func)
6444 rb_gc_force_recycle((VALUE)lex_strterm);
6446 if (func & STR_FUNC_REGEXP) {
6447 set_yylval_num(regx_options());
6448 dispatch_scan_event(tREGEXP_END);
6449 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
6452 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
6454 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
6457 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
6461 static enum yytokentype
6462 parser_parse_string(struct parser_params *parser, NODE *quote)
6464 int func = (int)quote->nd_func;
6465 int term = nd_term(quote);
6466 int paren = nd_paren(quote);
6468 rb_encoding *enc = current_enc;
6470 if (func & STR_FUNC_TERM) {
6471 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
6473 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
6476 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6477 do {c = nextc();} while (ISSPACE(c));
6480 if (c == term && !quote->nd_nest) {
6481 if (func & STR_FUNC_QWORDS) {
6482 quote->nd_func |= STR_FUNC_TERM;
6485 return parser_string_term(parser, func);
6492 if ((func & STR_FUNC_EXPAND) && c == '#') {
6493 int t = parser_peek_variable_name(parser);
6499 if (tokadd_string(func, term, paren, "e->nd_nest,
6503 # define unterminated_literal(mesg) yyerror0(mesg)
6505 # define unterminated_literal(mesg) compile_error(PARSER_ARG mesg)
6507 literal_flush(lex_p);
6508 if (func & STR_FUNC_REGEXP) {
6509 unterminated_literal("unterminated regexp meets end of file");
6512 unterminated_literal("unterminated string meets end of file");
6514 quote->nd_func |= STR_FUNC_TERM;
6519 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
6520 flush_string_content(enc);
6522 return tSTRING_CONTENT;
6525 static enum yytokentype
6526 parser_heredoc_identifier(struct parser_params *parser)
6528 int c = nextc(), term, func = 0;
6529 enum yytokentype token = tSTRING_BEG;
6536 func = STR_FUNC_INDENT;
6538 else if (c == '~') {
6540 func = STR_FUNC_INDENT;
6545 func |= str_squote; goto quoted;
6547 func |= str_dquote; goto quoted;
6549 token = tXSTRING_BEG;
6550 func |= str_xquote; goto quoted;
6556 while ((c = nextc()) != -1 && c != term) {
6557 if (tokadd_mbchar(c) == -1) return 0;
6558 if (!newline && c == '\n') newline = 1;
6559 else if (newline) newline = 2;
6562 compile_error(PARSER_ARG "unterminated here document identifier");
6567 rb_warn0("here document identifier ends with a newline");
6568 if (--tokidx > 0 && tokenbuf[tokidx] == '\r') --tokidx;
6571 compile_error(PARSER_ARG "here document identifier across newlines, never match");
6577 if (!parser_is_identchar()) {
6579 if (func & STR_FUNC_INDENT) {
6580 pushback(indent > 0 ? '~' : '-');
6585 tokadd(func |= str_dquote);
6587 if (tokadd_mbchar(c) == -1) return 0;
6588 } while ((c = nextc()) != -1 && parser_is_identchar());
6594 dispatch_scan_event(tHEREDOC_BEG);
6595 len = lex_p - lex_pbeg;
6596 lex_goto_eol(parser);
6597 lex_strterm = rb_node_newnode(NODE_HEREDOC,
6598 STR_NEW(tok(), toklen()), /* nd_lit */
6600 lex_lastline); /* nd_orig */
6601 parser_set_line(lex_strterm, ruby_sourceline);
6602 token_flush(parser);
6603 heredoc_indent = indent;
6604 heredoc_line_indent = 0;
6609 parser_heredoc_restore(struct parser_params *parser, NODE *here)
6614 line = here->nd_orig;
6615 lex_lastline = line;
6616 lex_pbeg = RSTRING_PTR(line);
6617 lex_pend = lex_pbeg + RSTRING_LEN(line);
6618 lex_p = lex_pbeg + here->nd_nth;
6619 heredoc_end = ruby_sourceline;
6620 ruby_sourceline = nd_line(here);
6621 dispose_string(here->nd_lit);
6622 rb_gc_force_recycle((VALUE)here);
6623 token_flush(parser);
6627 dedent_string(VALUE string, int width)
6633 RSTRING_GETMEM(string, str, len);
6634 for (i = 0; i < len && col < width; i++) {
6635 if (str[i] == ' ') {
6638 else if (str[i] == '\t') {
6639 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
6640 if (n > width) break;
6648 rb_str_modify(string);
6649 str = RSTRING_PTR(string);
6650 if (RSTRING_LEN(string) != len)
6651 rb_fatal("literal string changed: %+"PRIsVALUE, string);
6652 MEMMOVE(str, str + i, char, len - i);
6653 rb_str_set_len(string, len - i);
6659 parser_heredoc_dedent(struct parser_params *parser, NODE *root)
6661 NODE *node, *str_node;
6663 int indent = heredoc_indent;
6665 if (indent <= 0) return root;
6667 if (!root) return root;
6669 node = str_node = root;
6670 if (nd_type(root) == NODE_ARRAY) str_node = root->nd_head;
6673 VALUE lit = str_node->nd_lit;
6674 if (bol) dedent_string(lit, indent);
6678 while ((node = node->nd_next) != 0 && nd_type(node) == NODE_ARRAY) {
6679 if ((str_node = node->nd_head) != 0) {
6680 enum node_type type = nd_type(str_node);
6681 if (type == NODE_STR || type == NODE_DSTR) break;
6691 parser_heredoc_dedent(struct parser_params *parser, VALUE array)
6693 int indent = heredoc_indent;
6695 if (indent <= 0) return array;
6697 dispatch2(heredoc_dedent, array, INT2NUM(indent));
6702 parser_dedent_string(VALUE self, VALUE input, VALUE width)
6707 wid = NUM2UINT(width);
6708 col = dedent_string(input, wid);
6709 return INT2NUM(col);
6714 parser_whole_match_p(struct parser_params *parser,
6715 const char *eos, long len, int indent)
6717 const char *p = lex_pbeg;
6721 while (*p && ISSPACE(*p)) p++;
6723 n = lex_pend - (p + len);
6724 if (n < 0) return FALSE;
6725 if (n > 0 && p[len] != '\n') {
6726 if (p[len] != '\r') return FALSE;
6727 if (n <= 1 || p[len+1] != '\n') return FALSE;
6729 return strncmp(eos, p, len) == 0;
6732 #define NUM_SUFFIX_R (1<<0)
6733 #define NUM_SUFFIX_I (1<<1)
6734 #define NUM_SUFFIX_ALL 3
6737 parser_number_literal_suffix(struct parser_params *parser, int mask)
6740 const char *lastp = lex_p;
6742 while ((c = nextc()) != -1) {
6743 if ((mask & NUM_SUFFIX_I) && c == 'i') {
6744 result |= (mask & NUM_SUFFIX_I);
6745 mask &= ~NUM_SUFFIX_I;
6746 /* r after i, rational of complex is disallowed */
6747 mask &= ~NUM_SUFFIX_R;
6750 if ((mask & NUM_SUFFIX_R) && c == 'r') {
6751 result |= (mask & NUM_SUFFIX_R);
6752 mask &= ~NUM_SUFFIX_R;
6755 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
6757 literal_flush(lex_p);
6764 yyerror0("unexpected fraction part after numeric literal");
6766 while (parser_is_identchar()) nextc();
6774 static enum yytokentype
6775 parser_set_number_literal(struct parser_params *parser, VALUE v,
6776 enum yytokentype type, int suffix)
6778 if (suffix & NUM_SUFFIX_I) {
6779 v = rb_complex_raw(INT2FIX(0), v);
6782 set_yylval_literal(v);
6783 SET_LEX_STATE(EXPR_END|EXPR_ENDARG);
6788 parser_set_integer_literal(struct parser_params *parser, VALUE v, int suffix)
6790 enum yytokentype type = tINTEGER;
6791 if (suffix & NUM_SUFFIX_R) {
6792 v = rb_rational_raw1(v);
6795 return set_number_literal(v, type, suffix);
6800 ripper_dispatch_heredoc_end(struct parser_params *parser)
6803 if (has_delayed_token())
6804 dispatch_delayed_token(tSTRING_CONTENT);
6805 str = STR_NEW(parser->tokp, lex_pend - parser->tokp);
6806 ripper_dispatch1(parser, ripper_token2eventid(tHEREDOC_END), str);
6807 lex_goto_eol(parser);
6808 token_flush(parser);
6811 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser)
6813 #define dispatch_heredoc_end() ((void)0)
6816 static enum yytokentype
6817 parser_here_document(struct parser_params *parser, NODE *here)
6819 int c, func, indent = 0;
6820 const char *eos, *p, *pend;
6823 rb_encoding *enc = current_enc;
6825 eos = RSTRING_PTR(here->nd_lit);
6826 len = RSTRING_LEN(here->nd_lit) - 1;
6827 indent = (func = *eos++) & STR_FUNC_INDENT;
6829 if ((c = nextc()) == -1) {
6831 compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos);
6833 if (!has_delayed_token()) {
6834 dispatch_scan_event(tSTRING_CONTENT);
6838 rb_str_append(parser->delayed, str);
6840 else if ((len = lex_p - parser->tokp) > 0) {
6841 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6842 int cr = ENC_CODERANGE_UNKNOWN;
6843 rb_str_coderange_scan_restartable(parser->tokp, lex_p, enc, &cr);
6844 if (cr != ENC_CODERANGE_7BIT &&
6845 current_enc == rb_usascii_encoding() &&
6846 enc != rb_utf8_encoding()) {
6847 enc = rb_ascii8bit_encoding();
6850 rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc);
6852 dispatch_delayed_token(tSTRING_CONTENT);
6854 lex_goto_eol(parser);
6857 heredoc_restore(lex_strterm);
6861 if (was_bol() && whole_match_p(eos, len, indent)) {
6862 dispatch_heredoc_end();
6863 heredoc_restore(lex_strterm);
6865 SET_LEX_STATE(EXPR_END);
6869 if (!(func & STR_FUNC_EXPAND)) {
6871 p = RSTRING_PTR(lex_lastline);
6876 if (--pend == p || pend[-1] != '\r') {
6885 if (heredoc_indent > 0) {
6887 while (p + i < pend && parser_update_heredoc_indent(parser, p[i]))
6889 heredoc_line_indent = 0;
6893 rb_str_cat(str, p, pend - p);
6895 str = STR_NEW(p, pend - p);
6896 if (pend < lex_pend) rb_str_cat(str, "\n", 1);
6897 lex_goto_eol(parser);
6898 if (heredoc_indent > 0) {
6899 set_yylval_str(str);
6900 flush_string_content(enc);
6901 return tSTRING_CONTENT;
6903 if (nextc() == -1) {
6905 dispose_string(str);
6910 } while (!whole_match_p(eos, len, indent));
6913 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
6916 int t = parser_peek_variable_name(parser);
6917 if (heredoc_line_indent != -1) {
6918 if (heredoc_indent > heredoc_line_indent) {
6919 heredoc_indent = heredoc_line_indent;
6921 heredoc_line_indent = -1;
6929 if ((c = tokadd_string(func, '\n', 0, NULL, &enc)) == -1) {
6930 if (parser->eofp) goto error;
6935 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func));
6936 flush_string_content(enc);
6937 return tSTRING_CONTENT;
6940 if (heredoc_indent > 0) {
6941 lex_goto_eol(parser);
6944 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
6945 if ((c = nextc()) == -1) goto error;
6946 } while (!whole_match_p(eos, len, indent));
6947 str = STR_NEW3(tok(), toklen(), enc, func);
6949 dispatch_heredoc_end();
6951 str = ripper_new_yylval(ripper_token2eventid(tSTRING_CONTENT),
6954 heredoc_restore(lex_strterm);
6955 lex_strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
6956 set_yylval_str(str);
6957 return tSTRING_CONTENT;
6963 arg_ambiguous_gen(struct parser_params *parser, char c)
6966 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
6968 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
6971 #define arg_ambiguous(c) (arg_ambiguous_gen(parser, (c)), 1)
6974 formal_argument_gen(struct parser_params *parser, ID lhs)
6976 switch (id_type(lhs)) {
6981 yyerror0("formal argument cannot be a constant");
6984 yyerror0("formal argument cannot be an instance variable");
6987 yyerror0("formal argument cannot be a global variable");
6990 yyerror0("formal argument cannot be a class variable");
6993 yyerror0("formal argument must be local variable");
6997 lhs = dispatch1(param_error, lhs);
7002 shadowing_lvar(lhs);
7007 lvar_defined_gen(struct parser_params *parser, ID id)
7009 return (dyna_in_block() && dvar_defined(id)) || local_id(id);
7012 /* emacsen -*- hack */
7014 parser_encode_length(struct parser_params *parser, const char *name, long len)
7018 if (len > 5 && name[nlen = len - 5] == '-') {
7019 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7022 if (len > 4 && name[nlen = len - 4] == '-') {
7023 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7025 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7026 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7027 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7034 parser_set_encode(struct parser_params *parser, const char *name)
7036 int idx = rb_enc_find_index(name);
7041 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7043 excargs[0] = rb_eArgError;
7044 excargs[2] = rb_make_backtrace();
7045 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", ruby_sourcefile_string, ruby_sourceline));
7046 rb_exc_raise(rb_make_exception(3, excargs));
7048 enc = rb_enc_from_index(idx);
7049 if (!rb_enc_asciicompat(enc)) {
7050 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7055 if (ruby_debug_lines) {
7056 VALUE lines = ruby_debug_lines;
7057 long i, n = RARRAY_LEN(lines);
7058 for (i = 0; i < n; ++i) {
7059 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7066 comment_at_top(struct parser_params *parser)
7068 const char *p = lex_pbeg, *pend = lex_p - 1;
7069 if (parser->line_count != (parser->has_shebang ? 2 : 1)) return 0;
7071 if (!ISSPACE(*p)) return 0;
7077 typedef long (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, long len);
7078 typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val);
7081 magic_comment_encoding(struct parser_params *parser, const char *name, const char *val)
7083 if (!comment_at_top(parser)) {
7086 parser_set_encode(parser, val);
7090 parser_get_bool(struct parser_params *parser, const char *name, const char *val)
7094 if (strcasecmp(val, "true") == 0) {
7099 if (strcasecmp(val, "false") == 0) {
7104 rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val);
7109 parser_set_token_info(struct parser_params *parser, const char *name, const char *val)
7111 int b = parser_get_bool(parser, name, val);
7112 if (b >= 0) parser->token_info_enabled = b;
7116 parser_set_compile_option_flag(struct parser_params *parser, const char *name, const char *val)
7120 if (parser->token_seen) {
7121 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7125 b = parser_get_bool(parser, name, val);
7128 if (!parser->compile_option)
7129 parser->compile_option = rb_obj_hide(rb_ident_hash_new());
7130 rb_hash_aset(parser->compile_option, ID2SYM(rb_intern(name)),
7131 (b ? Qtrue : Qfalse));
7134 # if WARN_PAST_SCOPE
7136 parser_set_past_scope(struct parser_params *parser, const char *name, const char *val)
7138 int b = parser_get_bool(parser, name, val);
7139 if (b >= 0) parser->past_scope_enabled = b;
7143 struct magic_comment {
7145 rb_magic_comment_setter_t func;
7146 rb_magic_comment_length_t length;
7149 static const struct magic_comment magic_comments[] = {
7150 {"coding", magic_comment_encoding, parser_encode_length},
7151 {"encoding", magic_comment_encoding, parser_encode_length},
7152 {"frozen_string_literal", parser_set_compile_option_flag},
7153 {"warn_indent", parser_set_token_info},
7154 # if WARN_PAST_SCOPE
7155 {"warn_past_scope", parser_set_past_scope},
7160 magic_comment_marker(const char *str, long len)
7167 if (str[i-1] == '*' && str[i-2] == '-') {
7173 if (i + 1 >= len) return 0;
7174 if (str[i+1] != '-') {
7177 else if (str[i-1] != '-') {
7193 parser_magic_comment(struct parser_params *parser, const char *str, long len)
7196 VALUE name = 0, val = 0;
7197 const char *beg, *end, *vbeg, *vend;
7198 #define str_copy(_s, _p, _n) ((_s) \
7199 ? (void)(rb_str_resize((_s), (_n)), \
7200 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7201 : (void)((_s) = STR_NEW((_p), (_n))))
7203 if (len <= 7) return FALSE;
7204 if (!!(beg = magic_comment_marker(str, len))) {
7205 if (!(end = magic_comment_marker(beg, str + len - beg)))
7209 len = end - beg - 3;
7212 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7214 const struct magic_comment *p = magic_comments;
7219 for (; len > 0 && *str; str++, --len) {
7221 case '\'': case '"': case ':': case ';':
7224 if (!ISSPACE(*str)) break;
7226 for (beg = str; len > 0; str++, --len) {
7228 case '\'': case '"': case ':': case ';':
7231 if (ISSPACE(*str)) break;
7236 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7239 if (!indicator) return FALSE;
7243 do str++; while (--len > 0 && ISSPACE(*str));
7246 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7259 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7263 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
7266 while (len > 0 && (ISSPACE(*str))) --len, str++;
7267 if (len) return FALSE;
7271 str_copy(name, beg, n);
7272 s = RSTRING_PTR(name);
7273 for (i = 0; i < n; ++i) {
7274 if (s[i] == '-') s[i] = '_';
7277 if (STRNCASECMP(p->name, s, n) == 0 && !p->name[n]) {
7280 n = (*p->length)(parser, vbeg, n);
7282 str_copy(val, vbeg, n);
7283 (*p->func)(parser, p->name, RSTRING_PTR(val));
7286 } while (++p < magic_comments + numberof(magic_comments));
7288 str_copy(val, vbeg, vend - vbeg);
7289 dispatch2(magic_comment, name, val);
7297 set_file_encoding(struct parser_params *parser, const char *str, const char *send)
7300 const char *beg = str;
7304 if (send - str <= 6) return;
7306 case 'C': case 'c': str += 6; continue;
7307 case 'O': case 'o': str += 5; continue;
7308 case 'D': case 'd': str += 4; continue;
7309 case 'I': case 'i': str += 3; continue;
7310 case 'N': case 'n': str += 2; continue;
7311 case 'G': case 'g': str += 1; continue;
7318 if (ISSPACE(*str)) break;
7321 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
7325 if (++str >= send) return;
7326 } while (ISSPACE(*str));
7328 if (*str != '=' && *str != ':') return;
7333 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
7334 s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg));
7335 parser_set_encode(parser, RSTRING_PTR(s));
7336 rb_str_resize(s, 0);
7340 parser_prepare(struct parser_params *parser)
7343 parser->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
7346 if (peek('!')) parser->has_shebang = 1;
7348 case 0xef: /* UTF-8 BOM marker */
7349 if (lex_pend - lex_p >= 2 &&
7350 (unsigned char)lex_p[0] == 0xbb &&
7351 (unsigned char)lex_p[1] == 0xbf) {
7352 parser->enc = rb_utf8_encoding();
7362 parser->enc = rb_enc_get(lex_lastline);
7366 #define ambiguous_operator(tok, op, syn) ( \
7367 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
7368 rb_warning0("even though it seems like "syn""))
7370 #define ambiguous_operator(tok, op, syn) \
7371 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
7373 #define warn_balanced(tok, op, syn) ((void) \
7374 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
7375 space_seen && !ISSPACE(c) && \
7376 (ambiguous_operator(tok, op, syn), 0)), \
7377 (enum yytokentype)(tok))
7380 parse_rational(struct parser_params *parser, char *str, int len, int seen_point)
7383 char *point = &str[seen_point];
7384 size_t fraclen = len-seen_point-1;
7385 memmove(point, point+1, fraclen+1);
7386 v = rb_cstr_to_inum(str, 10, FALSE);
7387 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
7391 parse_numeric(struct parser_params *parser, int c)
7393 int is_float, seen_point, seen_e, nondigit;
7396 is_float = seen_point = seen_e = nondigit = 0;
7397 SET_LEX_STATE(EXPR_END);
7399 if (c == '-' || c == '+') {
7404 #define no_digits() do {yyerror0("numeric literal without digits"); return 0;} while (0)
7405 int start = toklen();
7407 if (c == 'x' || c == 'X') {
7410 if (c != -1 && ISXDIGIT(c)) {
7413 if (nondigit) break;
7417 if (!ISXDIGIT(c)) break;
7420 } while ((c = nextc()) != -1);
7424 if (toklen() == start) {
7427 else if (nondigit) goto trailing_uc;
7428 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7429 return set_integer_literal(rb_cstr_to_inum(tok(), 16, FALSE), suffix);
7431 if (c == 'b' || c == 'B') {
7434 if (c == '0' || c == '1') {
7437 if (nondigit) break;
7441 if (c != '0' && c != '1') break;
7444 } while ((c = nextc()) != -1);
7448 if (toklen() == start) {
7451 else if (nondigit) goto trailing_uc;
7452 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7453 return set_integer_literal(rb_cstr_to_inum(tok(), 2, FALSE), suffix);
7455 if (c == 'd' || c == 'D') {
7458 if (c != -1 && ISDIGIT(c)) {
7461 if (nondigit) break;
7465 if (!ISDIGIT(c)) break;
7468 } while ((c = nextc()) != -1);
7472 if (toklen() == start) {
7475 else if (nondigit) goto trailing_uc;
7476 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7477 return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix);
7483 if (c == 'o' || c == 'O') {
7484 /* prefixed octal */
7486 if (c == -1 || c == '_' || !ISDIGIT(c)) {
7490 if (c >= '0' && c <= '7') {
7495 if (nondigit) break;
7499 if (c < '0' || c > '9') break;
7500 if (c > '7') goto invalid_octal;
7503 } while ((c = nextc()) != -1);
7504 if (toklen() > start) {
7507 if (nondigit) goto trailing_uc;
7508 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7509 return set_integer_literal(rb_cstr_to_inum(tok(), 8, FALSE), suffix);
7516 if (c > '7' && c <= '9') {
7518 yyerror0("Invalid octal digit");
7520 else if (c == '.' || c == 'e' || c == 'E') {
7525 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7526 return set_integer_literal(INT2FIX(0), suffix);
7532 case '0': case '1': case '2': case '3': case '4':
7533 case '5': case '6': case '7': case '8': case '9':
7539 if (nondigit) goto trailing_uc;
7540 if (seen_point || seen_e) {
7545 if (c0 == -1 || !ISDIGIT(c0)) {
7551 seen_point = toklen();
7570 if (c != '-' && c != '+' && !ISDIGIT(c)) {
7579 nondigit = (c == '-' || c == '+') ? c : 0;
7582 case '_': /* `_' in number just ignored */
7583 if (nondigit) goto decode_num;
7598 literal_flush(lex_p - 1);
7599 snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit);
7607 suffix = number_literal_suffix(seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
7608 if (suffix & NUM_SUFFIX_R) {
7610 v = parse_rational(parser, tok(), toklen(), seen_point);
7613 double d = strtod(tok(), 0);
7614 if (errno == ERANGE) {
7615 rb_warning1("Float %s out of range", WARN_S(tok()));
7620 return set_number_literal(v, type, suffix);
7622 suffix = number_literal_suffix(NUM_SUFFIX_ALL);
7623 return set_integer_literal(rb_cstr_to_inum(tok(), 10, FALSE), suffix);
7626 static enum yytokentype
7627 parse_qmark(struct parser_params *parser, int space_seen)
7633 SET_LEX_STATE(EXPR_VALUE);
7638 compile_error(PARSER_ARG "incomplete character syntax");
7641 if (rb_enc_isspace(c, current_enc)) {
7665 rb_warn1("invalid character syntax; use ?\\%c", WARN_I(c2));
7670 SET_LEX_STATE(EXPR_VALUE);
7675 if (!parser_isascii()) {
7676 if (tokadd_mbchar(c) == -1) return 0;
7678 else if ((rb_enc_isalnum(c, current_enc) || c == '_') &&
7679 lex_p < lex_pend && is_identchar(lex_p, lex_pend, current_enc)) {
7681 const char *start = lex_p - 1, *p = start;
7683 int n = parser_precise_mbclen(parser, p);
7684 if (n < 0) return -1;
7686 } while (p < lex_pend && is_identchar(p, lex_pend, current_enc));
7687 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
7688 " a conditional operator, put a space after `?'",
7689 WARN_I((int)(p - start)), WARN_S_L(start, (p - start)));
7693 else if (c == '\\') {
7696 enc = rb_utf8_encoding();
7697 if (!parser_tokadd_utf8(parser, &enc, -1, 0, 0))
7700 else if (!lex_eol_p() && !(c = *lex_p, ISASCII(c))) {
7702 if (tokadd_mbchar(c) == -1) return 0;
7705 c = read_escape(0, &enc);
7713 set_yylval_str(STR_NEW3(tok(), toklen(), enc, 0));
7714 SET_LEX_STATE(EXPR_END);
7718 static enum yytokentype
7719 parse_percent(struct parser_params *parser, const int space_seen, const enum lex_state_e last_state)
7729 if (c == -1 || !ISALNUM(c)) {
7735 if (rb_enc_isalnum(term, current_enc) || !parser_isascii()) {
7736 yyerror0("unknown type of %string");
7740 if (c == -1 || term == -1) {
7741 compile_error(PARSER_ARG "unterminated quoted string meets end of file");
7745 if (term == '(') term = ')';
7746 else if (term == '[') term = ']';
7747 else if (term == '{') term = '}';
7748 else if (term == '<') term = '>';
7753 lex_strterm = NEW_STRTERM(str_dquote, term, paren);
7757 lex_strterm = NEW_STRTERM(str_squote, term, paren);
7761 lex_strterm = NEW_STRTERM(str_dword, term, paren);
7762 do {c = nextc();} while (ISSPACE(c));
7767 lex_strterm = NEW_STRTERM(str_sword, term, paren);
7768 do {c = nextc();} while (ISSPACE(c));
7773 lex_strterm = NEW_STRTERM(str_dword, term, paren);
7774 do {c = nextc();} while (ISSPACE(c));
7776 return tSYMBOLS_BEG;
7779 lex_strterm = NEW_STRTERM(str_sword, term, paren);
7780 do {c = nextc();} while (ISSPACE(c));
7782 return tQSYMBOLS_BEG;
7785 lex_strterm = NEW_STRTERM(str_xquote, term, paren);
7786 return tXSTRING_BEG;
7789 lex_strterm = NEW_STRTERM(str_regexp, term, paren);
7793 lex_strterm = NEW_STRTERM(str_ssym, term, paren);
7794 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
7798 yyerror0("unknown type of %string");
7802 if ((c = nextc()) == '=') {
7804 SET_LEX_STATE(EXPR_BEG);
7807 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
7810 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
7812 return warn_balanced('%', "%%", "string literal");
7816 tokadd_ident(struct parser_params *parser, int c)
7819 if (tokadd_mbchar(c) == -1) return -1;
7821 } while (parser_is_identchar());
7827 tokenize_ident(struct parser_params *parser, const enum lex_state_e last_state)
7829 ID ident = TOK_INTERN();
7831 set_yylval_name(ident);
7837 parse_numvar(struct parser_params *parser)
7841 unsigned long n = ruby_scan_digits(tok()+1, toklen()-1, 10, &len, &overflow);
7842 const unsigned long nth_ref_max =
7843 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
7844 /* NTH_REF is left-shifted to be ORed with back-ref flag and
7845 * turned into a Fixnum, in compile.c */
7847 if (overflow || n > nth_ref_max) {
7848 /* compile_error()? */
7849 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok()));
7850 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
7857 static enum yytokentype
7858 parse_gvar(struct parser_params *parser, const enum lex_state_e last_state)
7862 SET_LEX_STATE(EXPR_END);
7866 case '_': /* $_: last read line string */
7868 if (parser_is_identchar()) {
7876 case '~': /* $~: match-data */
7877 case '*': /* $*: argv */
7878 case '$': /* $$: pid */
7879 case '?': /* $?: last status */
7880 case '!': /* $!: error string */
7881 case '@': /* $@: error position */
7882 case '/': /* $/: input record separator */
7883 case '\\': /* $\: output record separator */
7884 case ';': /* $;: field separator */
7885 case ',': /* $,: output field separator */
7886 case '.': /* $.: last read line number */
7887 case '=': /* $=: ignorecase */
7888 case ':': /* $:: load path */
7889 case '<': /* $<: reading filename */
7890 case '>': /* $>: default output handle */
7891 case '\"': /* $": already loaded files */
7900 if (parser_is_identchar()) {
7901 if (tokadd_mbchar(c) == -1) return 0;
7909 set_yylval_name(TOK_INTERN());
7912 case '&': /* $&: last match */
7913 case '`': /* $`: string before last match */
7914 case '\'': /* $': string after last match */
7915 case '+': /* $+: string matches last paren. */
7916 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
7921 set_yylval_node(NEW_BACK_REF(c));
7924 case '1': case '2': case '3':
7925 case '4': case '5': case '6':
7926 case '7': case '8': case '9':
7931 } while (c != -1 && ISDIGIT(c));
7933 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
7935 set_yylval_node(NEW_NTH_REF(parse_numvar(parser)));
7939 if (!parser_is_identchar()) {
7940 if (c == -1 || ISSPACE(c)) {
7941 compile_error(PARSER_ARG "`$' without identifiers is not allowed as a global variable name");
7945 compile_error(PARSER_ARG "`$%c' is not allowed as a global variable name", c);
7953 if (tokadd_ident(parser, c)) return 0;
7954 SET_LEX_STATE(EXPR_END);
7955 tokenize_ident(parser, last_state);
7959 static enum yytokentype
7960 parse_atmark(struct parser_params *parser, const enum lex_state_e last_state)
7962 enum yytokentype result = tIVAR;
7963 register int c = nextc();
7972 if (c == -1 || ISSPACE(c)) {
7973 if (result == tIVAR) {
7974 compile_error(PARSER_ARG "`@' without identifiers is not allowed as an instance variable name");
7977 compile_error(PARSER_ARG "`@@' without identifiers is not allowed as a class variable name");
7981 else if (ISDIGIT(c) || !parser_is_identchar()) {
7983 if (result == tIVAR) {
7984 compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c);
7987 compile_error(PARSER_ARG "`@@%c' is not allowed as a class variable name", c);
7992 if (tokadd_ident(parser, c)) return 0;
7993 SET_LEX_STATE(EXPR_END);
7994 tokenize_ident(parser, last_state);
7998 static enum yytokentype
7999 parse_ident(struct parser_params *parser, int c, int cmd_state)
8001 enum yytokentype result;
8002 int mb = ENC_CODERANGE_7BIT;
8003 const enum lex_state_e last_state = lex_state;
8007 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8008 if (tokadd_mbchar(c) == -1) return 0;
8010 } while (parser_is_identchar());
8011 if ((c == '!' || c == '?') && !peek('=')) {
8015 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8016 (!peek('~') && !peek('>') && (!peek('=') || (peek_n('>', 1))))) {
8017 result = tIDENTIFIER;
8021 result = tCONSTANT; /* assume provisionally */
8026 if (IS_LABEL_POSSIBLE()) {
8027 if (IS_LABEL_SUFFIX(0)) {
8028 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8030 set_yylval_name(TOK_INTERN());
8034 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8035 const struct kwtable *kw;
8037 /* See if it is a reserved word. */
8038 kw = rb_reserved_word(tok(), toklen());
8040 enum lex_state_e state = lex_state;
8041 SET_LEX_STATE(kw->state);
8042 if (IS_lex_state_for(state, EXPR_FNAME)) {
8043 set_yylval_name(rb_intern2(tok(), toklen()));
8046 if (IS_lex_state(EXPR_BEG)) {
8047 command_start = TRUE;
8049 if (kw->id[0] == keyword_do) {
8050 if (lambda_beginning_p()) {
8053 return keyword_do_LAMBDA;
8055 if (COND_P()) return keyword_do_cond;
8056 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8057 return keyword_do_block;
8058 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_ENDARG)))
8059 return keyword_do_block;
8062 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8065 if (kw->id[0] != kw->id[1])
8066 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8072 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8074 SET_LEX_STATE(EXPR_CMDARG);
8077 SET_LEX_STATE(EXPR_ARG);
8080 else if (lex_state == EXPR_FNAME) {
8081 SET_LEX_STATE(EXPR_ENDFN);
8084 SET_LEX_STATE(EXPR_END);
8087 ident = tokenize_ident(parser, last_state);
8088 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8089 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8090 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8091 lvar_defined(ident)) {
8092 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8097 static enum yytokentype
8098 parser_yylex(struct parser_params *parser)
8104 enum lex_state_e last_state;
8105 int fallthru = FALSE;
8106 int token_seen = parser->token_seen;
8109 if (nd_type(lex_strterm) == NODE_HEREDOC) {
8110 return here_document(lex_strterm);
8113 return parse_string(lex_strterm);
8116 cmd_state = command_start;
8117 command_start = FALSE;
8118 parser->token_seen = TRUE;
8120 last_state = lex_state;
8122 token_flush(parser);
8124 switch (c = nextc()) {
8125 case '\0': /* NUL */
8126 case '\004': /* ^D */
8127 case '\032': /* ^Z */
8128 case -1: /* end of script. */
8132 case ' ': case '\t': case '\f': case '\r':
8133 case '\13': /* '\v' */
8136 while ((c = nextc())) {
8138 case ' ': case '\t': case '\f': case '\r':
8139 case '\13': /* '\v' */
8147 dispatch_scan_event(tSP);
8151 case '#': /* it's a comment */
8152 parser->token_seen = token_seen;
8153 /* no magic_comment in shebang line */
8154 if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) {
8155 if (comment_at_top(parser)) {
8156 set_file_encoding(parser, lex_p, lex_pend);
8160 dispatch_scan_event(tCOMMENT);
8164 parser->token_seen = token_seen;
8165 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8166 !IS_lex_state(EXPR_LABELED));
8167 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8169 dispatch_scan_event(tIGNORED_NL);
8172 if (!c && parser->in_kwarg) {
8173 goto normal_newline;
8177 while ((c = nextc())) {
8179 case ' ': case '\t': case '\f': case '\r':
8180 case '\13': /* '\v' */
8185 dispatch_delayed_token(tIGNORED_NL);
8186 if (peek('.') == (c == '&')) {
8188 dispatch_scan_event(tSP);
8194 lex_nextline = lex_lastline;
8195 case -1: /* EOF no decrement*/
8196 lex_goto_eol(parser);
8199 parser->tokp = lex_p;
8202 goto normal_newline;
8206 command_start = TRUE;
8207 SET_LEX_STATE(EXPR_BEG);
8211 if ((c = nextc()) == '*') {
8212 if ((c = nextc()) == '=') {
8213 set_yylval_id(tPOW);
8214 SET_LEX_STATE(EXPR_BEG);
8219 rb_warning0("`**' interpreted as argument prefix");
8222 else if (IS_BEG()) {
8226 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
8232 SET_LEX_STATE(EXPR_BEG);
8237 rb_warning0("`*' interpreted as argument prefix");
8240 else if (IS_BEG()) {
8244 c = warn_balanced('*', "*", "argument prefix");
8247 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8252 if (IS_AFTER_OPERATOR()) {
8253 SET_LEX_STATE(EXPR_ARG);
8259 SET_LEX_STATE(EXPR_BEG);
8272 /* skip embedded rd document */
8273 if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) {
8276 lex_goto_eol(parser);
8277 dispatch_scan_event(tEMBDOC_BEG);
8279 lex_goto_eol(parser);
8281 dispatch_scan_event(tEMBDOC);
8286 compile_error(PARSER_ARG "embedded document meets end of file");
8289 if (c != '=') continue;
8290 if (c == '=' && strncmp(lex_p, "end", 3) == 0 &&
8291 (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
8295 lex_goto_eol(parser);
8296 dispatch_scan_event(tEMBDOC_END);
8301 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8302 if ((c = nextc()) == '=') {
8303 if ((c = nextc()) == '=') {
8312 else if (c == '>') {
8319 last_state = lex_state;
8322 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
8324 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
8325 int token = heredoc_identifier();
8326 if (token) return token;
8328 if (IS_AFTER_OPERATOR()) {
8329 SET_LEX_STATE(EXPR_ARG);
8332 if (IS_lex_state(EXPR_CLASS))
8333 command_start = TRUE;
8334 SET_LEX_STATE(EXPR_BEG);
8337 if ((c = nextc()) == '>') {
8344 if ((c = nextc()) == '=') {
8345 set_yylval_id(tLSHFT);
8346 SET_LEX_STATE(EXPR_BEG);
8350 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
8356 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8357 if ((c = nextc()) == '=') {
8361 if ((c = nextc()) == '=') {
8362 set_yylval_id(tRSHFT);
8363 SET_LEX_STATE(EXPR_BEG);
8373 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
8374 lex_strterm = NEW_STRTERM(str_dquote | label, '"', 0);
8378 if (IS_lex_state(EXPR_FNAME)) {
8379 SET_LEX_STATE(EXPR_ENDFN);
8382 if (IS_lex_state(EXPR_DOT)) {
8384 SET_LEX_STATE(EXPR_CMDARG);
8386 SET_LEX_STATE(EXPR_ARG);
8389 lex_strterm = NEW_STRTERM(str_xquote, '`', 0);
8390 return tXSTRING_BEG;
8393 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
8394 lex_strterm = NEW_STRTERM(str_squote | label, '\'', 0);
8398 return parse_qmark(parser, space_seen);
8401 if ((c = nextc()) == '&') {
8402 SET_LEX_STATE(EXPR_BEG);
8403 if ((c = nextc()) == '=') {
8404 set_yylval_id(tANDOP);
8405 SET_LEX_STATE(EXPR_BEG);
8411 else if (c == '=') {
8413 SET_LEX_STATE(EXPR_BEG);
8416 else if (c == '.') {
8417 SET_LEX_STATE(EXPR_DOT);
8423 (c = peekc_n(1)) == -1 ||
8424 !(c == '\'' || c == '"' ||
8425 is_identchar((lex_p+1), lex_pend, current_enc))) {
8426 rb_warning0("`&' interpreted as argument prefix");
8430 else if (IS_BEG()) {
8434 c = warn_balanced('&', "&", "argument prefix");
8436 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8440 if ((c = nextc()) == '|') {
8441 SET_LEX_STATE(EXPR_BEG);
8442 if ((c = nextc()) == '=') {
8443 set_yylval_id(tOROP);
8444 SET_LEX_STATE(EXPR_BEG);
8452 SET_LEX_STATE(EXPR_BEG);
8455 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
8461 if (IS_AFTER_OPERATOR()) {
8462 SET_LEX_STATE(EXPR_ARG);
8471 SET_LEX_STATE(EXPR_BEG);
8474 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('+'))) {
8475 SET_LEX_STATE(EXPR_BEG);
8477 if (c != -1 && ISDIGIT(c)) {
8478 return parse_numeric(parser, '+');
8482 SET_LEX_STATE(EXPR_BEG);
8484 return warn_balanced('+', "+", "unary operator");
8488 if (IS_AFTER_OPERATOR()) {
8489 SET_LEX_STATE(EXPR_ARG);
8498 SET_LEX_STATE(EXPR_BEG);
8502 SET_LEX_STATE(EXPR_ENDFN);
8503 token_info_push("->");
8506 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous('-'))) {
8507 SET_LEX_STATE(EXPR_BEG);
8509 if (c != -1 && ISDIGIT(c)) {
8514 SET_LEX_STATE(EXPR_BEG);
8516 return warn_balanced('-', "-", "unary operator");
8519 SET_LEX_STATE(EXPR_BEG);
8520 if ((c = nextc()) == '.') {
8521 if ((c = nextc()) == '.') {
8528 if (c != -1 && ISDIGIT(c)) {
8529 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
8531 SET_LEX_STATE(EXPR_DOT);
8534 case '0': case '1': case '2': case '3': case '4':
8535 case '5': case '6': case '7': case '8': case '9':
8536 return parse_numeric(parser, c);
8545 SET_LEX_STATE(EXPR_ENDFN);
8547 SET_LEX_STATE(EXPR_END);
8549 if (!brace_nest--) c = tSTRING_DEND;
8556 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
8557 SET_LEX_STATE(EXPR_BEG);
8560 SET_LEX_STATE(EXPR_DOT);
8563 if (IS_END() || ISSPACE(c) || c == '#') {
8565 c = warn_balanced(':', ":", "symbol literal");
8566 SET_LEX_STATE(EXPR_BEG);
8571 lex_strterm = NEW_STRTERM(str_ssym, c, 0);
8574 lex_strterm = NEW_STRTERM(str_dsym, c, 0);
8580 SET_LEX_STATE(EXPR_FNAME);
8585 lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
8588 if ((c = nextc()) == '=') {
8590 SET_LEX_STATE(EXPR_BEG);
8595 (void)arg_ambiguous('/');
8596 lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
8599 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8600 return warn_balanced('/', "/", "regexp literal");
8603 if ((c = nextc()) == '=') {
8605 SET_LEX_STATE(EXPR_BEG);
8608 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8613 SET_LEX_STATE(EXPR_BEG);
8614 command_start = TRUE;
8618 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8622 if (IS_AFTER_OPERATOR()) {
8623 if ((c = nextc()) != '@') {
8626 SET_LEX_STATE(EXPR_ARG);
8629 SET_LEX_STATE(EXPR_BEG);
8637 else if (!space_seen) {
8638 /* foo( ... ) => method call, no ambiguity */
8640 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
8643 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
8644 rb_warning0("parentheses after method name is interpreted as "
8645 "an argument list, not a decomposed argument");
8650 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8655 if (IS_AFTER_OPERATOR()) {
8656 if ((c = nextc()) == ']') {
8657 SET_LEX_STATE(EXPR_ARG);
8658 if ((c = nextc()) == '=') {
8665 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
8668 else if (IS_BEG()) {
8671 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
8674 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
8681 if (lambda_beginning_p()) {
8682 SET_LEX_STATE(EXPR_BEG);
8689 if (IS_lex_state(EXPR_LABELED))
8690 c = tLBRACE; /* hash */
8691 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
8692 c = '{'; /* block (primary) */
8693 else if (IS_lex_state(EXPR_ENDARG))
8694 c = tLBRACE_ARG; /* block (expr) */
8696 c = tLBRACE; /* hash */
8699 SET_LEX_STATE(c == tLBRACE_ARG ? EXPR_BEG : EXPR_BEG|EXPR_LABEL);
8700 if (c != tLBRACE) command_start = TRUE;
8707 dispatch_scan_event(tSP);
8708 goto retry; /* skip \\n */
8714 return parse_percent(parser, space_seen, last_state);
8717 return parse_gvar(parser, last_state);
8720 return parse_atmark(parser, last_state);
8723 if (was_bol() && whole_match_p("__END__", 7, 0)) {
8724 ruby__end__seen = 1;
8729 lex_goto_eol(parser);
8730 dispatch_scan_event(k__END__);
8738 if (!parser_is_identchar()) {
8739 compile_error(PARSER_ARG "Invalid char `\\x%02X' in expression", c);
8747 return parse_ident(parser, c, cmd_state);
8750 static enum yytokentype
8751 yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *parser)
8755 parser->lval = lval;
8757 t = parser_yylex(parser);
8758 if (has_delayed_token())
8759 dispatch_delayed_token(t);
8761 dispatch_scan_event(t);
8763 yylloc->first_column = (int)(parser->tokp - lex_pbeg);
8764 yylloc->last_column = (int)(lex_p - lex_pbeg);
8769 #define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
8773 node_newnode(struct parser_params *parser, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
8775 NODE *n = (rb_node_newnode)(type, a0, a1, a2);
8776 nd_set_line(n, ruby_sourceline);
8777 /* mark not cared column to -1 */
8778 nd_set_column(n, -1);
8782 static enum node_type
8783 nodetype(NODE *node) /* for debug */
8785 return (enum node_type)nd_type(node);
8789 nodeline(NODE *node)
8791 return nd_line(node);
8795 newline_node(NODE *node)
8798 node = remove_begin(node);
8799 node->flags |= NODE_FL_NEWLINE;
8805 fixpos(NODE *node, NODE *orig)
8809 if (orig == (NODE*)1) return;
8810 nd_set_line(node, nd_line(orig));
8814 parser_warning(struct parser_params *parser, NODE *node, const char *mesg)
8816 rb_compile_warning(ruby_sourcefile, nd_line(node), "%s", mesg);
8818 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg))
8821 parser_warn(struct parser_params *parser, NODE *node, const char *mesg)
8823 rb_compile_warn(ruby_sourcefile, nd_line(node), "%s", mesg);
8825 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg))
8828 block_append_gen(struct parser_params *parser, NODE *head, NODE *tail, int column)
8830 NODE *end, *h = head, *nd;
8832 if (tail == 0) return head;
8834 if (h == 0) return tail;
8835 switch (nd_type(h)) {
8842 parser_warning(h, "unused literal ignored");
8845 h = end = NEW_BLOCK(head);
8846 nd_set_column(end, column);
8857 switch (nd_type(nd)) {
8863 if (RTEST(ruby_verbose)) {
8864 parser_warning(tail, "statement not reached");
8872 if (nd_type(tail) != NODE_BLOCK) {
8873 tail = NEW_BLOCK(tail);
8874 nd_set_column(tail, column);
8875 tail->nd_end = tail;
8877 end->nd_next = tail;
8878 h->nd_end = tail->nd_end;
8882 /* append item to the list */
8884 list_append_gen(struct parser_params *parser, NODE *list, NODE *item, int column)
8888 if (list == 0) return new_list(item, column);
8889 if (list->nd_next) {
8890 last = list->nd_next->nd_end;
8897 last->nd_next = new_list(item, column);
8898 list->nd_next->nd_end = last->nd_next;
8902 /* concat two lists */
8904 list_concat(NODE *head, NODE *tail)
8908 if (head->nd_next) {
8909 last = head->nd_next->nd_end;
8915 head->nd_alen += tail->nd_alen;
8916 last->nd_next = tail;
8917 if (tail->nd_next) {
8918 head->nd_next->nd_end = tail->nd_next->nd_end;
8921 head->nd_next->nd_end = tail;
8928 literal_concat0(struct parser_params *parser, VALUE head, VALUE tail)
8930 if (NIL_P(tail)) return 1;
8931 if (!rb_enc_compatible(head, tail)) {
8932 compile_error(PARSER_ARG "string literal encodings differ (%s / %s)",
8933 rb_enc_name(rb_enc_get(head)),
8934 rb_enc_name(rb_enc_get(tail)));
8935 rb_str_resize(head, 0);
8936 rb_str_resize(tail, 0);
8939 rb_str_buf_append(head, tail);
8943 /* concat two string literals */
8945 literal_concat_gen(struct parser_params *parser, NODE *head, NODE *tail, int column)
8947 enum node_type htype;
8951 if (!head) return tail;
8952 if (!tail) return head;
8954 htype = nd_type(head);
8955 if (htype == NODE_EVSTR) {
8956 NODE *node = new_dstr(STR_NEW0(), column);
8957 head = list_append(node, head, column);
8960 if (heredoc_indent > 0) {
8963 nd_set_type(head, NODE_DSTR);
8965 return list_append(head, tail, column);
8970 switch (nd_type(tail)) {
8972 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
8973 nd_type(headlast) == NODE_STR) {
8975 lit = headlast->nd_lit;
8980 if (htype == NODE_STR) {
8981 if (!literal_concat0(parser, lit, tail->nd_lit)) {
8983 rb_gc_force_recycle((VALUE)head);
8984 rb_gc_force_recycle((VALUE)tail);
8987 rb_gc_force_recycle((VALUE)tail);
8990 list_append(head, tail, column);
8995 if (htype == NODE_STR) {
8996 if (!literal_concat0(parser, head->nd_lit, tail->nd_lit))
8998 tail->nd_lit = head->nd_lit;
8999 rb_gc_force_recycle((VALUE)head);
9002 else if (NIL_P(tail->nd_lit)) {
9004 head->nd_alen += tail->nd_alen - 1;
9005 head->nd_next->nd_end->nd_next = tail->nd_next;
9006 head->nd_next->nd_end = tail->nd_next->nd_end;
9007 rb_gc_force_recycle((VALUE)tail);
9009 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9010 nd_type(headlast) == NODE_STR) {
9011 lit = headlast->nd_lit;
9012 if (!literal_concat0(parser, lit, tail->nd_lit))
9014 tail->nd_lit = Qnil;
9018 nd_set_type(tail, NODE_ARRAY);
9019 tail->nd_head = new_str(tail->nd_lit, column);
9020 list_concat(head, tail);
9025 if (htype == NODE_STR) {
9026 nd_set_type(head, NODE_DSTR);
9029 list_append(head, tail, column);
9036 evstr2dstr_gen(struct parser_params *parser, NODE *node, int column)
9038 if (nd_type(node) == NODE_EVSTR) {
9039 node = list_append(new_dstr(STR_NEW0(), column), node, column);
9045 new_evstr_gen(struct parser_params *parser, NODE *node, int column)
9051 switch (nd_type(node)) {
9052 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9056 evstr = NEW_EVSTR(head);
9057 nd_set_column(evstr, column);
9062 call_bin_op_gen(struct parser_params *parser, NODE *recv, ID id, NODE *arg1, int column)
9067 expr = NEW_OPCALL(recv, id, new_list(arg1, column));
9069 nd_set_column(expr, column);
9074 call_uni_op_gen(struct parser_params *parser, NODE *recv, ID id, int column)
9078 opcall = NEW_OPCALL(recv, id, 0);
9079 nd_set_column(opcall, column);
9084 new_qcall_gen(struct parser_params* parser, ID atype, NODE *recv, ID mid, NODE *args, int column)
9086 NODE *qcall = NEW_QCALL(atype, recv, mid, args);
9087 nd_set_column(qcall, column);
9092 match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2, int column)
9097 switch (nd_type(node1)) {
9099 case NODE_DREGX_ONCE:
9101 NODE *match = NEW_MATCH2(node1, node2);
9102 nd_set_column(match, column);
9107 if (RB_TYPE_P(node1->nd_lit, T_REGEXP)) {
9108 const VALUE lit = node1->nd_lit;
9109 NODE *match = NEW_MATCH2(node1, node2);
9110 match->nd_args = reg_named_capture_assign(lit, column);
9111 nd_set_column(match, column);
9120 switch (nd_type(node2)) {
9122 case NODE_DREGX_ONCE:
9123 match3 = NEW_MATCH3(node2, node1);
9124 nd_set_column(match3, column);
9128 if (RB_TYPE_P(node2->nd_lit, T_REGEXP)) {
9129 match3 = NEW_MATCH3(node2, node1);
9130 nd_set_column(match3, column);
9136 return new_call(node1, tMATCH, new_list(node2, column), column);
9139 # if WARN_PAST_SCOPE
9141 past_dvar_p(struct parser_params *parser, ID id)
9143 struct vtable *past = lvtbl->past;
9145 if (vtable_included(past, id)) return 1;
9153 gettable_gen(struct parser_params *parser, ID id, int column)
9160 nd_set_column(node, column);
9164 nd_set_column(node, column);
9168 nd_set_column(node, column);
9172 nd_set_column(node, column);
9174 case keyword__FILE__:
9175 node = new_str(rb_str_dup(ruby_sourcefile_string), column);
9177 case keyword__LINE__:
9178 return new_lit(INT2FIX(tokline), column);
9179 case keyword__ENCODING__:
9180 return new_lit(rb_enc_from_encoding(current_enc), column);
9182 switch (id_type(id)) {
9184 if (dyna_in_block() && dvar_defined_ref(id, vidp)) {
9185 if (id == current_arg) {
9186 rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id));
9188 if (vidp) *vidp |= LVAR_USED;
9189 node = new_dvar(id, column);
9192 if (local_id_ref(id, vidp)) {
9193 if (id == current_arg) {
9194 rb_warn1("circular argument reference - %"PRIsWARN, rb_id2str(id));
9196 if (vidp) *vidp |= LVAR_USED;
9197 node = new_lvar(id, column);
9200 # if WARN_PAST_SCOPE
9201 if (!in_defined && RTEST(ruby_verbose) && past_dvar_p(parser, id)) {
9202 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
9205 /* method call without arguments */
9206 node = NEW_VCALL(id);
9207 nd_set_column(node, column);
9210 node = new_gvar(id, column);
9213 node = new_ivar(id, column);
9216 node = NEW_CONST(id);
9217 nd_set_column(node, column);
9220 node = NEW_CVAR(id);
9221 nd_set_column(node, column);
9224 compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
9229 kwd_append(NODE *kwlist, NODE *kw)
9233 while (kws->nd_next) {
9242 new_defined_gen(struct parser_params *parser, NODE *expr, int column)
9244 NODE *defined = NEW_DEFINED(remove_begin_all(expr));
9245 nd_set_column(defined, column);
9250 new_regexp_gen(struct parser_params *parser, NODE *node, int options, int column)
9255 return new_lit(reg_compile(STR_NEW0(), options), column);
9257 switch (nd_type(node)) {
9260 VALUE src = node->nd_lit;
9261 nd_set_type(node, NODE_LIT);
9262 node->nd_lit = reg_compile(src, options);
9266 node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, new_list(node, column));
9267 nd_set_column(node, column);
9269 nd_set_type(node, NODE_DREGX);
9270 node->nd_cflag = options & RE_OPTION_MASK;
9271 if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options);
9272 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
9273 if (nd_type(list->nd_head) == NODE_STR) {
9274 VALUE tail = list->nd_head->nd_lit;
9275 if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) {
9276 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
9277 if (!literal_concat0(parser, lit, tail)) {
9281 rb_str_resize(tail, 0);
9282 prev->nd_next = list->nd_next;
9283 rb_gc_force_recycle((VALUE)list->nd_head);
9284 rb_gc_force_recycle((VALUE)list);
9295 if (!node->nd_next) {
9296 VALUE src = node->nd_lit;
9297 nd_set_type(node, NODE_LIT);
9298 node->nd_lit = reg_compile(src, options);
9300 if (options & RE_OPTION_ONCE) {
9301 node = NEW_NODE(NODE_SCOPE, 0, node, 0);
9309 new_lit_gen(struct parser_params *parser, VALUE sym, int column)
9311 NODE *lit = NEW_LIT(sym);
9312 nd_set_column(lit, column);
9317 new_list_gen(struct parser_params *parser, NODE *item, int column)
9319 NODE *list = NEW_LIST(item);
9320 nd_set_column(list, column);
9325 new_str_gen(struct parser_params *parser, VALUE str, int column)
9327 NODE *nd_str = NEW_STR(str);
9328 nd_set_column(nd_str, column);
9333 new_dvar_gen(struct parser_params *parser, ID id, int column)
9335 NODE *dvar = NEW_DVAR(id);
9336 nd_set_column(dvar, column);
9341 new_resbody_gen(struct parser_params *parser, NODE *exc_list, NODE *stmt, NODE *rescue, int column)
9343 NODE *resbody = NEW_RESBODY(exc_list, stmt, rescue);
9344 nd_set_column(resbody, column);
9349 new_errinfo_gen(struct parser_params *parser, int column)
9351 NODE *errinfo = NEW_ERRINFO();
9352 nd_set_column(errinfo, column);
9357 new_call_gen(struct parser_params *parser, NODE *recv, ID mid, NODE *args, int column)
9359 NODE *call = NEW_CALL(recv, mid, args);
9360 nd_set_column(call, column);
9365 new_fcall_gen(struct parser_params *parser, ID mid, NODE *args, int column)
9367 NODE *fcall = NEW_FCALL(mid, args);
9368 nd_set_column(fcall, column);
9373 new_for_gen(struct parser_params *parser, NODE *var, NODE *iter, NODE *body, int column)
9375 NODE *nd_for = NEW_FOR(var, iter, body);
9376 nd_set_column(nd_for, column);
9381 new_gvar_gen(struct parser_params *parser, ID id, int column)
9383 NODE *gvar = NEW_GVAR(id);
9384 nd_set_column(gvar, column);
9389 new_lvar_gen(struct parser_params *parser, ID id, int column)
9391 NODE *lvar = NEW_LVAR(id);
9392 nd_set_column(lvar, column);
9397 new_dstr_gen(struct parser_params *parser, VALUE str, int column)
9399 NODE *dstr = NEW_DSTR(str);
9400 nd_set_column(dstr, column);
9405 new_rescue_gen(struct parser_params *parser, NODE *b, NODE *res, NODE *e, int column)
9407 NODE *rescue = NEW_RESCUE(b, res, e);
9408 nd_set_column(rescue, column);
9413 new_undef_gen(struct parser_params *parser, NODE *i, int column)
9415 NODE *undef = NEW_UNDEF(i);
9416 nd_set_column(undef, column);
9421 new_zarray_gen(struct parser_params *parser, int column)
9423 NODE *zarray = NEW_ZARRAY();
9424 nd_set_column(zarray, column);
9429 new_ivar_gen(struct parser_params *parser, ID id, int column)
9431 NODE *ivar = NEW_IVAR(id);
9432 nd_set_column(ivar, column);
9437 new_postarg_gen(struct parser_params *parser, NODE *i, NODE *v, int column)
9439 NODE *postarg = NEW_POSTARG(i, v);
9440 nd_set_column(postarg, column);
9445 new_cdecl_gen(struct parser_params *parser, ID v, NODE *val, NODE *path, int column)
9447 NODE *nd_cdecl = NEW_CDECL(v, val, path);
9448 nd_set_column(nd_cdecl, column);
9453 new_scope_gen(struct parser_params *parser, NODE *a, NODE *b, int column)
9455 NODE *scope = NEW_SCOPE(a, b);
9456 nd_set_column(scope, column);
9461 new_begin_gen(struct parser_params *parser, NODE *b, int column)
9463 NODE *begin = NEW_BEGIN(b);
9464 nd_set_column(begin, column);
9469 new_masgn_gen(struct parser_params *parser, NODE *l, NODE *r, int column)
9471 NODE *masgn = NEW_MASGN(l, r);
9472 nd_set_column(masgn, column);
9478 new_kw_arg_gen(struct parser_params *parser, NODE *k, int column)
9482 kw_arg = NEW_KW_ARG(0, (k));
9483 nd_set_column(kw_arg, column);
9488 new_xstring_gen(struct parser_params *parser, NODE *node, int column)
9491 NODE *xstr = NEW_XSTR(STR_NEW0());
9492 nd_set_column(xstr, column);
9495 switch (nd_type(node)) {
9497 nd_set_type(node, NODE_XSTR);
9500 nd_set_type(node, NODE_DXSTR);
9503 node = NEW_NODE(NODE_DXSTR, Qnil, 1, new_list(node, column));
9504 nd_set_column(node, column);
9511 new_body_gen(struct parser_params *parser, NODE *param, NODE *stmt, int column)
9513 NODE *iter = NEW_ITER(param, stmt);
9514 nd_set_column(iter->nd_body, column);
9515 nd_set_column(iter, column);
9521 id_is_var_gen(struct parser_params *parser, ID id)
9523 if (is_notop_id(id)) {
9524 switch (id & ID_SCOPE_MASK) {
9525 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
9528 if (dyna_in_block() && dvar_defined(id)) return 1;
9529 if (local_id(id)) return 1;
9530 /* method call without arguments */
9534 compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
9539 new_regexp_gen(struct parser_params *parser, VALUE re, VALUE opt)
9543 if (ripper_is_node_yylval(re)) {
9544 src = RNODE(re)->nd_cval;
9545 re = RNODE(re)->nd_rval;
9547 if (ripper_is_node_yylval(opt)) {
9548 options = (int)RNODE(opt)->nd_tag;
9549 opt = RNODE(opt)->nd_rval;
9551 if (src && NIL_P(parser_reg_compile(parser, src, options, &err))) {
9552 compile_error(PARSER_ARG "%"PRIsVALUE, err);
9554 return dispatch2(regexp_literal, re, opt);
9558 new_xstring_gen(struct parser_params *parser, VALUE str)
9560 return dispatch1(xstring_literal, str);
9562 #endif /* !RIPPER */
9565 const char rb_parser_lex_state_names[][13] = {
9566 "EXPR_BEG", "EXPR_END", "EXPR_ENDARG", "EXPR_ENDFN", "EXPR_ARG",
9567 "EXPR_CMDARG", "EXPR_MID", "EXPR_FNAME", "EXPR_DOT", "EXPR_CLASS",
9568 "EXPR_LABEL", "EXPR_LABELED","EXPR_FITEM",
9572 append_lex_state_name(enum lex_state_e state, VALUE buf)
9575 unsigned int mask = 1;
9576 static const char none[] = "EXPR_NONE";
9578 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
9579 if ((unsigned)state & mask) {
9581 rb_str_cat(buf, "|", 1);
9584 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
9588 rb_str_cat(buf, none, sizeof(none)-1);
9594 flush_debug_buffer(struct parser_params *parser, VALUE out, VALUE str)
9596 VALUE mesg = parser->debug_buffer;
9598 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
9599 parser->debug_buffer = Qnil;
9600 rb_io_puts(1, &mesg, out);
9602 if (!NIL_P(str) && RSTRING_LEN(str)) {
9603 rb_io_write(parser->debug_output, str);
9608 rb_parser_trace_lex_state(struct parser_params *parser, enum lex_state_e from,
9609 enum lex_state_e to, int line)
9612 mesg = rb_str_new_cstr("lex_state: ");
9613 append_lex_state_name(from, mesg);
9614 rb_str_cat_cstr(mesg, " -> ");
9615 append_lex_state_name(to, mesg);
9616 rb_str_catf(mesg, " at line %d\n", line);
9617 flush_debug_buffer(parser, parser->debug_output, mesg);
9622 rb_parser_lex_state_name(enum lex_state_e state)
9624 return append_lex_state_name(state, rb_str_new(0, 0));
9628 append_bitstack_value(stack_type stack, VALUE mesg)
9631 rb_str_cat_cstr(mesg, "0");
9634 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
9635 for (; mask && !(stack & mask); mask >>= 1) continue;
9636 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
9641 rb_parser_show_bitstack(struct parser_params *parser, stack_type stack,
9642 const char *name, int line)
9644 VALUE mesg = rb_sprintf("%s: ", name);
9645 append_bitstack_value(stack, mesg);
9646 rb_str_catf(mesg, " at line %d\n", line);
9647 flush_debug_buffer(parser, parser->debug_output, mesg);
9651 rb_parser_fatal(struct parser_params *parser, const char *fmt, ...)
9654 VALUE mesg = rb_str_new_cstr("internal parser error: ");
9657 rb_str_vcatf(mesg, fmt, ap);
9660 parser_yyerror(parser, RSTRING_PTR(mesg));
9663 dispatch1(parse_error, mesg);
9665 #endif /* !RIPPER */
9667 mesg = rb_str_new(0, 0);
9668 append_lex_state_name(lex_state, mesg);
9669 compile_error(PARSER_ARG "lex_state: %"PRIsVALUE, mesg);
9670 rb_str_resize(mesg, 0);
9671 append_bitstack_value(cond_stack, mesg);
9672 compile_error(PARSER_ARG "cond_stack: %"PRIsVALUE, mesg);
9673 rb_str_resize(mesg, 0);
9674 append_bitstack_value(cmdarg_stack, mesg);
9675 compile_error(PARSER_ARG "cmdarg_stack: %"PRIsVALUE, mesg);
9676 if (parser->debug_output == rb_stdout)
9677 parser->debug_output = rb_stderr;
9680 #endif /* !RIPPER */
9684 assignable_result0(NODE *node, int column)
9686 if (node) nd_set_column(node, column);
9689 #endif /* !RIPPER */
9693 assignable_gen(struct parser_params *parser, VALUE lhs)
9696 assignable_gen(struct parser_params *parser, ID id, NODE *val, int column)
9700 ID id = get_id(lhs);
9701 # define assignable_result(x) (lhs)
9702 # define parser_yyerror(parser, x) (lhs = assign_error_gen(parser, lhs))
9704 # define assignable_result(x) assignable_result0(x, column)
9706 if (!id) return assignable_result(0);
9709 yyerror0("Can't change the value of self");
9712 yyerror0("Can't assign to nil");
9715 yyerror0("Can't assign to true");
9718 yyerror0("Can't assign to false");
9720 case keyword__FILE__:
9721 yyerror0("Can't assign to __FILE__");
9723 case keyword__LINE__:
9724 yyerror0("Can't assign to __LINE__");
9726 case keyword__ENCODING__:
9727 yyerror0("Can't assign to __ENCODING__");
9730 switch (id_type(id)) {
9732 if (dyna_in_block()) {
9733 if (dvar_curr(id)) {
9734 return assignable_result(NEW_DASGN_CURR(id, val));
9736 else if (dvar_defined(id)) {
9737 return assignable_result(NEW_DASGN(id, val));
9739 else if (local_id(id)) {
9740 return assignable_result(NEW_LASGN(id, val));
9744 return assignable_result(NEW_DASGN_CURR(id, val));
9748 if (!local_id(id)) {
9751 return assignable_result(NEW_LASGN(id, val));
9755 return assignable_result(NEW_GASGN(id, val));
9757 return assignable_result(NEW_IASGN(id, val));
9759 if (!in_def && !in_single)
9760 return assignable_result(new_cdecl(id, val, 0, column));
9761 yyerror0("dynamic constant assignment");
9764 return assignable_result(NEW_CVASGN(id, val));
9766 compile_error(PARSER_ARG "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
9769 return assignable_result(0);
9770 #undef assignable_result
9771 #undef parser_yyerror
9775 is_private_local_id(ID name)
9778 if (name == idUScore) return 1;
9779 if (!is_local_id(name)) return 0;
9780 s = rb_id2str(name);
9782 return RSTRING_PTR(s)[0] == '_';
9786 shadowing_lvar_0(struct parser_params *parser, ID name)
9788 if (is_private_local_id(name)) return 1;
9789 if (dyna_in_block()) {
9790 if (dvar_curr(name)) {
9791 yyerror0("duplicated argument name");
9793 else if (dvar_defined(name) || local_id(name)) {
9794 rb_warning1("shadowing outer local variable - %"PRIsWARN, rb_id2str(name));
9795 vtable_add(lvtbl->vars, name);
9797 vtable_add(lvtbl->used, (ID)ruby_sourceline | LVAR_USED);
9803 if (local_id(name)) {
9804 yyerror0("duplicated argument name");
9811 shadowing_lvar_gen(struct parser_params *parser, ID name)
9813 shadowing_lvar_0(parser, name);
9818 new_bv_gen(struct parser_params *parser, ID name)
9821 if (!is_local_id(name)) {
9822 compile_error(PARSER_ARG "invalid local variable - %"PRIsVALUE,
9826 if (!shadowing_lvar_0(parser, name)) return;
9832 aryset_gen(struct parser_params *parser, NODE *recv, NODE *idx, int column)
9834 NODE *attrasgn = NEW_ATTRASGN(recv, tASET, idx);
9835 nd_set_column(attrasgn, column);
9840 block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2)
9842 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
9843 compile_error(PARSER_ARG "both block arg and actual block given");
9848 attrset_gen(struct parser_params *parser, NODE *recv, ID atype, ID id, int column)
9851 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
9852 attrasgn = NEW_ATTRASGN(recv, id, 0);
9853 nd_set_column(attrasgn, column);
9858 rb_backref_error_gen(struct parser_params *parser, NODE *node)
9860 switch (nd_type(node)) {
9862 compile_error(PARSER_ARG "Can't set variable $%ld", node->nd_nth);
9865 compile_error(PARSER_ARG "Can't set variable $%c", (int)node->nd_nth);
9871 arg_concat_gen(struct parser_params *parser, NODE *node1, NODE *node2, int column)
9875 if (!node2) return node1;
9876 switch (nd_type(node1)) {
9877 case NODE_BLOCK_PASS:
9879 node1->nd_head = arg_concat(node1->nd_head, node2, column);
9881 node1->nd_head = new_list(node2, column);
9884 if (nd_type(node2) != NODE_ARRAY) break;
9885 node1->nd_body = list_concat(new_list(node1->nd_body, column), node2);
9886 nd_set_type(node1, NODE_ARGSCAT);
9889 if (nd_type(node2) != NODE_ARRAY ||
9890 nd_type(node1->nd_body) != NODE_ARRAY) break;
9891 node1->nd_body = list_concat(node1->nd_body, node2);
9894 argscat = NEW_ARGSCAT(node1, node2);
9895 nd_set_column(argscat, column);
9900 arg_append_gen(struct parser_params *parser, NODE *node1, NODE *node2, int column)
9904 if (!node1) return new_list(node2, column);
9905 switch (nd_type(node1)) {
9907 return list_append(node1, node2, column);
9908 case NODE_BLOCK_PASS:
9909 node1->nd_head = arg_append(node1->nd_head, node2, column);
9912 node1->nd_body = list_append(new_list(node1->nd_body, column), node2, column);
9913 nd_set_type(node1, NODE_ARGSCAT);
9916 argspush = NEW_ARGSPUSH(node1, node2);
9917 nd_set_column(argspush, column);
9922 splat_array(NODE* node)
9924 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
9925 if (nd_type(node) == NODE_ARRAY) return node;
9930 node_assign_gen(struct parser_params *parser, NODE *lhs, NODE *rhs, int column)
9934 switch (nd_type(lhs)) {
9940 case NODE_DASGN_CURR:
9944 lhs->nd_value = rhs;
9949 lhs->nd_args = arg_append(lhs->nd_args, rhs, column);
9953 /* should not happen */
9961 value_expr_gen(struct parser_params *parser, NODE *node)
9966 rb_warning0("empty expression");
9969 switch (nd_type(node)) {
9975 if (!cond) yyerror0("void value expression");
9976 /* or "control never reach"? */
9980 while (node->nd_next) {
9981 node = node->nd_next;
9983 node = node->nd_head;
9987 node = node->nd_body;
9992 if (!node->nd_body) {
9993 node = node->nd_else;
9996 else if (!node->nd_else) {
9997 node = node->nd_body;
10000 if (!value_expr(node->nd_body)) return FALSE;
10001 node = node->nd_else;
10007 node = node->nd_2nd;
10019 void_expr_gen(struct parser_params *parser, NODE *node)
10021 const char *useless = 0;
10023 if (!RTEST(ruby_verbose)) return;
10026 switch (nd_type(node)) {
10028 switch (node->nd_mid) {
10047 useless = rb_id2name(node->nd_mid);
10058 case NODE_BACK_REF:
10059 useless = "a variable";
10062 useless = "a constant";
10068 case NODE_DREGX_ONCE:
10069 useless = "a literal";
10094 useless = "defined?";
10099 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10104 void_stmts_gen(struct parser_params *parser, NODE *node)
10106 if (!RTEST(ruby_verbose)) return;
10108 if (nd_type(node) != NODE_BLOCK) return;
10111 if (!node->nd_next) return;
10112 void_expr0(node->nd_head);
10113 node = node->nd_next;
10118 remove_begin(NODE *node)
10120 NODE **n = &node, *n1 = node;
10121 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10122 *n = n1 = n1->nd_body;
10128 remove_begin_all(NODE *node)
10130 NODE **n = &node, *n1 = node;
10131 while (n1 && nd_type(n1) == NODE_BEGIN) {
10132 *n = n1 = n1->nd_body;
10138 reduce_nodes_gen(struct parser_params *parser, NODE **body)
10140 NODE *node = *body;
10146 #define subnodes(n1, n2) \
10147 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10148 (!node->n2) ? (body = &node->n1, 1) : \
10149 (reduce_nodes(&node->n1), body = &node->n2, 1))
10152 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10153 switch (nd_type(node)) {
10159 *body = node = node->nd_stts;
10160 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10163 *body = node = node->nd_body;
10164 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10167 body = &node->nd_end->nd_head;
10171 if (subnodes(nd_body, nd_else)) break;
10174 body = &node->nd_body;
10177 if (!subnodes(nd_body, nd_next)) goto end;
10180 if (!subnodes(nd_head, nd_resq)) goto end;
10183 if (node->nd_else) {
10184 body = &node->nd_resq;
10187 if (!subnodes(nd_head, nd_resq)) goto end;
10193 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10200 is_static_content(NODE *node)
10202 if (!node) return 1;
10203 switch (nd_type(node)) {
10205 if (!(node = node->nd_head)) break;
10208 if (!is_static_content(node->nd_head)) return 0;
10209 } while ((node = node->nd_next) != 0);
10224 assign_in_cond(struct parser_params *parser, NODE *node)
10226 switch (nd_type(node)) {
10230 case NODE_DASGN_CURR:
10239 if (!node->nd_value) return 1;
10240 if (is_static_content(node->nd_value)) {
10241 /* reports always */
10242 parser_warn(node->nd_value, "found = in conditional, should be ==");
10248 warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
10250 if (!e_option_supplied(parser)) parser_warn(node, str);
10254 warning_unless_e_option(struct parser_params *parser, NODE *node, const char *str)
10256 if (!e_option_supplied(parser)) parser_warning(node, str);
10259 static NODE *cond0(struct parser_params*,NODE*,int,int);
10262 range_op(struct parser_params *parser, NODE *node, int column)
10264 enum node_type type;
10266 if (node == 0) return 0;
10268 type = nd_type(node);
10270 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
10271 warn_unless_e_option(parser, node, "integer literal in conditional range");
10272 return new_call(node, tEQ, new_list(new_gvar(rb_intern("$."), column), column), column);
10274 return cond0(parser, node, FALSE, column);
10278 literal_node(NODE *node)
10280 if (!node) return 1; /* same as NODE_NIL */
10281 switch (nd_type(node)) {
10287 case NODE_DREGX_ONCE:
10299 cond0(struct parser_params *parser, NODE *node, int method_op, int column)
10301 if (node == 0) return 0;
10302 assign_in_cond(parser, node);
10304 switch (nd_type(node)) {
10308 if (!method_op) rb_warn0("string literal in condition");
10312 case NODE_DREGX_ONCE:
10316 warning_unless_e_option(parser, node, "regex literal in condition");
10318 match = NEW_MATCH2(node, new_gvar(idLASTLINE, column));
10319 nd_set_column(match, column);
10325 node->nd_1st = cond0(parser, node->nd_1st, FALSE, column);
10326 node->nd_2nd = cond0(parser, node->nd_2nd, FALSE, column);
10331 node->nd_beg = range_op(parser, node->nd_beg, column);
10332 node->nd_end = range_op(parser, node->nd_end, column);
10333 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
10334 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
10335 if (!method_op && !e_option_supplied(parser)) {
10336 int b = literal_node(node->nd_beg);
10337 int e = literal_node(node->nd_end);
10338 if ((b == 1 && e == 1) || (b + e >= 2 && RTEST(ruby_verbose))) {
10339 parser_warn(node, "range literal in condition");
10345 if (!method_op) parser_warning(node, "literal in condition");
10349 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
10351 warn_unless_e_option(parser, node, "regex literal in condition");
10352 nd_set_type(node, NODE_MATCH);
10356 parser_warning(node, "literal in condition");
10365 cond_gen(struct parser_params *parser, NODE *node, int method_op, int column)
10367 if (node == 0) return 0;
10368 return cond0(parser, node, method_op, column);
10372 new_if_gen(struct parser_params *parser, NODE *cc, NODE *left, NODE *right, int column)
10376 if (!cc) return right;
10377 cc = cond0(parser, cc, FALSE, column);
10378 node_if = NEW_IF(cc, left, right);
10379 nd_set_column(node_if, column);
10380 return newline_node(node_if);
10384 new_unless_gen(struct parser_params *parser, NODE *cc, NODE *left, NODE *right, int column)
10388 if (!cc) return right;
10389 cc = cond0(parser, cc, FALSE, column);
10390 node_unless = NEW_UNLESS(cc, left, right);
10391 nd_set_column(node_unless, column);
10392 return newline_node(node_unless);
10396 logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right, int column)
10400 if (left && (enum node_type)nd_type(left) == type) {
10401 NODE *node = left, *second;
10402 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
10405 node->nd_2nd = NEW_NODE(type, second, right, 0);
10406 nd_set_column(node->nd_2nd, column);
10409 op = NEW_NODE(type, left, right, 0);
10410 nd_set_column(op, column);
10415 no_blockarg(struct parser_params *parser, NODE *node)
10417 if (node && nd_type(node) == NODE_BLOCK_PASS) {
10418 compile_error(PARSER_ARG "block argument should not be given");
10423 ret_args_gen(struct parser_params *parser, NODE *node)
10426 no_blockarg(parser, node);
10427 if (nd_type(node) == NODE_ARRAY) {
10428 if (node->nd_next == 0) {
10429 node = node->nd_head;
10432 nd_set_type(node, NODE_VALUES);
10440 new_yield_gen(struct parser_params *parser, NODE *node, int column)
10443 if (node) no_blockarg(parser, node);
10445 yield = NEW_YIELD(node);
10446 nd_set_column(yield, column);
10451 negate_lit_gen(struct parser_params *parser, VALUE lit)
10453 int type = TYPE(lit);
10456 lit = LONG2FIX(-FIX2LONG(lit));
10459 BIGNUM_NEGATE(lit);
10460 lit = rb_big_norm(lit);
10463 RRATIONAL_SET_NUM(lit, negate_lit(RRATIONAL(lit)->num));
10466 RCOMPLEX_SET_REAL(lit, negate_lit(RCOMPLEX(lit)->real));
10467 RCOMPLEX_SET_IMAG(lit, negate_lit(RCOMPLEX(lit)->imag));
10471 if (FLONUM_P(lit)) {
10472 lit = DBL2NUM(-RFLOAT_VALUE(lit));
10476 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
10479 rb_parser_fatal(parser, "unknown literal type (%d) passed to negate_lit", type);
10486 arg_blk_pass(NODE *node1, NODE *node2)
10489 node2->nd_head = node1;
10497 new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, NODE *tail)
10499 int saved_line = ruby_sourceline;
10500 struct rb_args_info *args = tail->nd_ainfo;
10502 args->pre_args_num = m ? rb_long2int(m->nd_plen) : 0;
10503 args->pre_init = m ? m->nd_next : 0;
10505 args->post_args_num = p ? rb_long2int(p->nd_plen) : 0;
10506 args->post_init = p ? p->nd_next : 0;
10507 args->first_post_arg = p ? p->nd_pid : 0;
10509 args->rest_arg = r;
10511 args->opt_args = o;
10513 ruby_sourceline = saved_line;
10519 new_args_tail_gen(struct parser_params *parser, NODE *k, ID kr, ID b, int column)
10521 int saved_line = ruby_sourceline;
10522 struct rb_args_info *args;
10525 args = ZALLOC(struct rb_args_info);
10526 node = NEW_NODE(NODE_ARGS, 0, 0, args);
10527 nd_set_column(node, column);
10528 if (parser->error_p) return node;
10530 args->block_arg = b;
10535 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
10536 * variable order: k1, kr1, k2, &b, internal_id, krest
10538 * variable order: kr1, k1, k2, internal_id, krest, &b
10542 struct vtable *required_kw_vars = vtable_alloc(NULL);
10543 struct vtable *kw_vars = vtable_alloc(NULL);
10547 NODE *val_node = kwn->nd_body->nd_value;
10548 ID vid = kwn->nd_body->nd_vid;
10550 if (val_node == (NODE *)-1) {
10551 vtable_add(required_kw_vars, vid);
10554 vtable_add(kw_vars, vid);
10557 kwn = kwn->nd_next;
10560 kw_bits = internal_id();
10561 if (kr && is_junk_id(kr)) vtable_pop(lvtbl->args, 1);
10562 vtable_pop(lvtbl->args, vtable_size(required_kw_vars) + vtable_size(kw_vars) + (b != 0));
10564 for (i=0; i<vtable_size(required_kw_vars); i++) arg_var(required_kw_vars->tbl[i]);
10565 for (i=0; i<vtable_size(kw_vars); i++) arg_var(kw_vars->tbl[i]);
10566 vtable_free(required_kw_vars);
10567 vtable_free(kw_vars);
10570 if (kr) arg_var(kr);
10573 args->kw_rest_arg = new_dvar(kr, column);
10574 args->kw_rest_arg->nd_cflag = kw_bits;
10577 if (b) vtable_pop(lvtbl->args, 1); /* reorder */
10580 args->kw_rest_arg = new_dvar(kr, column);
10583 ruby_sourceline = saved_line;
10588 dsym_node_gen(struct parser_params *parser, NODE *node, int column)
10593 return new_lit(ID2SYM(idNULL), column);
10596 switch (nd_type(node)) {
10598 nd_set_type(node, NODE_DSYM);
10601 lit = node->nd_lit;
10602 node->nd_lit = ID2SYM(rb_intern_str(lit));
10603 nd_set_type(node, NODE_LIT);
10606 node = NEW_NODE(NODE_DSYM, Qnil, 1, new_list(node, column));
10607 nd_set_column(node, column);
10614 append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
10616 NODE *node = (NODE *)v;
10617 NODE **result = (NODE **)h;
10619 node->nd_next->nd_end = node->nd_next;
10620 node->nd_next->nd_next = 0;
10622 list_concat(*result, node);
10625 return ST_CONTINUE;
10629 remove_duplicate_keys(struct parser_params *parser, NODE *hash, int column)
10631 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
10633 while (hash && hash->nd_head && hash->nd_next) {
10634 NODE *head = hash->nd_head;
10635 NODE *value = hash->nd_next;
10636 NODE *next = value->nd_next;
10637 VALUE key = (VALUE)head;
10639 if (nd_type(head) == NODE_LIT &&
10640 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
10641 rb_compile_warn(ruby_sourcefile, nd_line((NODE *)data),
10642 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
10643 head->nd_lit, nd_line(head));
10644 head = ((NODE *)data)->nd_next;
10645 head->nd_head = block_append(head->nd_head, value->nd_head, column);
10648 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
10652 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
10653 st_free_table(literal_keys);
10655 if (!result) result = hash;
10656 else list_concat(result, hash);
10662 new_hash_gen(struct parser_params *parser, NODE *hash, int column)
10665 if (hash) hash = remove_duplicate_keys(parser, hash, column);
10666 nd_hash = NEW_HASH(hash);
10667 nd_set_column(nd_hash, column);
10670 #endif /* !RIPPER */
10674 new_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs, int column)
10679 ID vid = lhs->nd_vid;
10681 lhs->nd_value = rhs;
10682 asgn = NEW_OP_ASGN_OR(gettable(vid, column), lhs);
10683 nd_set_column(asgn, column);
10684 if (is_notop_id(vid)) {
10685 switch (id_type(vid)) {
10689 asgn->nd_aid = vid;
10693 else if (op == tANDOP) {
10694 lhs->nd_value = rhs;
10695 asgn = NEW_OP_ASGN_AND(gettable(vid, column), lhs);
10696 nd_set_column(asgn, column);
10700 asgn->nd_value = new_call(gettable(vid, column), op, new_list(rhs, column), column);
10704 asgn = new_begin(0, column);
10710 new_attr_op_assign_gen(struct parser_params *parser, NODE *lhs,
10711 ID atype, ID attr, ID op, NODE *rhs, int column)
10718 else if (op == tANDOP) {
10721 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs);
10722 nd_set_column(asgn, column);
10728 new_const_op_assign_gen(struct parser_params *parser, NODE *lhs, ID op, NODE *rhs, int column)
10735 else if (op == tANDOP) {
10739 asgn = NEW_OP_CDECL(lhs, op, rhs);
10742 asgn = new_begin(0, column);
10745 nd_set_column(asgn, column);
10750 const_path_field_gen(struct parser_params *parser, NODE *head, ID mid, int column)
10752 NODE *colon2 = NEW_COLON2(head, mid);
10753 nd_set_column(colon2, column);
10758 const_decl_gen(struct parser_params *parser, NODE *path, int column)
10760 if (in_def || in_single) {
10761 yyerror0("dynamic constant assignment");
10763 return new_cdecl(0, 0, (path), column);
10767 new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op, VALUE rhs, int column)
10769 return dispatch3(opassign, lhs, op, rhs);
10773 new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs)
10775 VALUE recv = dispatch3(field, lhs, type, attr);
10776 return dispatch3(opassign, recv, op, rhs);
10780 new_qcall_gen(struct parser_params *parser, VALUE r, VALUE q, VALUE m, VALUE a)
10782 VALUE ret = dispatch3(call, (r), (q), (m));
10783 return method_optarg(ret, (a));
10787 const_decl_gen(struct parser_params *parser, VALUE path)
10789 if (in_def || in_single) {
10790 path = dispatch1(assign_error, path);
10797 assign_error_gen(struct parser_params *parser, VALUE a)
10799 a = dispatch1(assign_error, a);
10805 var_field_gen(struct parser_params *parser, VALUE a)
10807 return ripper_new_yylval(get_id(a), dispatch1(var_field, a), 0);
10812 warn_unused_var(struct parser_params *parser, struct local_vars *local)
10817 if (!local->used) return;
10818 v = local->vars->tbl;
10819 u = local->used->tbl;
10820 cnt = local->used->pos;
10821 if (cnt != local->vars->pos) {
10822 rb_parser_fatal(parser, "local->used->pos != local->vars->pos");
10824 for (i = 0; i < cnt; ++i) {
10825 if (!v[i] || (u[i] & LVAR_USED)) continue;
10826 if (is_private_local_id(v[i])) continue;
10827 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
10832 local_push_gen(struct parser_params *parser, int inherit_dvars)
10834 struct local_vars *local;
10836 local = ALLOC(struct local_vars);
10837 local->prev = lvtbl;
10838 local->args = vtable_alloc(0);
10839 local->vars = vtable_alloc(inherit_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
10840 local->used = !(inherit_dvars &&
10841 (ifndef_ripper(compile_for_eval || e_option_supplied(parser))+0)) &&
10842 RTEST(ruby_verbose) ? vtable_alloc(0) : 0;
10843 # if WARN_PAST_SCOPE
10846 local->cmdargs = cmdarg_stack;
10852 local_pop_gen(struct parser_params *parser)
10854 struct local_vars *local = lvtbl->prev;
10856 warn_unused_var(parser, lvtbl);
10857 vtable_free(lvtbl->used);
10859 # if WARN_PAST_SCOPE
10860 while (lvtbl->past) {
10861 struct vtable *past = lvtbl->past;
10862 lvtbl->past = past->prev;
10866 vtable_free(lvtbl->args);
10867 vtable_free(lvtbl->vars);
10868 CMDARG_SET(lvtbl->cmdargs);
10875 local_tbl_gen(struct parser_params *parser)
10877 int cnt_args = vtable_size(lvtbl->args);
10878 int cnt_vars = vtable_size(lvtbl->vars);
10879 int cnt = cnt_args + cnt_vars;
10883 if (cnt <= 0) return 0;
10884 buf = ALLOC_N(ID, cnt + 1);
10885 MEMCPY(buf+1, lvtbl->args->tbl, ID, cnt_args);
10886 /* remove IDs duplicated to warn shadowing */
10887 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
10888 ID id = lvtbl->vars->tbl[i];
10889 if (!vtable_included(lvtbl->args, id)) {
10893 if (--j < cnt) REALLOC_N(buf, ID, (cnt = j) + 1);
10900 arg_var_gen(struct parser_params *parser, ID id)
10902 vtable_add(lvtbl->args, id);
10906 local_var_gen(struct parser_params *parser, ID id)
10908 vtable_add(lvtbl->vars, id);
10910 vtable_add(lvtbl->used, (ID)ruby_sourceline);
10915 local_id_gen(struct parser_params *parser, ID id, ID **vidrefp)
10917 struct vtable *vars, *args, *used;
10919 vars = lvtbl->vars;
10920 args = lvtbl->args;
10921 used = lvtbl->used;
10923 while (vars && POINTER_P(vars->prev)) {
10926 if (used) used = used->prev;
10929 if (vars && vars->prev == DVARS_INHERIT) {
10930 return rb_local_defined(id, parser->base_block);
10932 else if (vtable_included(args, id)) {
10936 int i = vtable_included(vars, id);
10937 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
10942 static const struct vtable *
10943 dyna_push_gen(struct parser_params *parser)
10945 lvtbl->args = vtable_alloc(lvtbl->args);
10946 lvtbl->vars = vtable_alloc(lvtbl->vars);
10948 lvtbl->used = vtable_alloc(lvtbl->used);
10950 return lvtbl->args;
10954 dyna_pop_vtable(struct parser_params *parser, struct vtable **vtblp)
10956 struct vtable *tmp = *vtblp;
10957 *vtblp = tmp->prev;
10958 # if WARN_PAST_SCOPE
10959 if (parser->past_scope_enabled) {
10960 tmp->prev = lvtbl->past;
10969 dyna_pop_1(struct parser_params *parser)
10971 struct vtable *tmp;
10973 if ((tmp = lvtbl->used) != 0) {
10974 warn_unused_var(parser, lvtbl);
10975 lvtbl->used = lvtbl->used->prev;
10978 dyna_pop_vtable(parser, &lvtbl->args);
10979 dyna_pop_vtable(parser, &lvtbl->vars);
10983 dyna_pop_gen(struct parser_params *parser, const struct vtable *lvargs)
10985 while (lvtbl->args != lvargs) {
10986 dyna_pop_1(parser);
10987 if (!lvtbl->args) {
10988 struct local_vars *local = lvtbl->prev;
10993 dyna_pop_1(parser);
10997 dyna_in_block_gen(struct parser_params *parser)
10999 return POINTER_P(lvtbl->vars) && lvtbl->vars->prev != DVARS_TOPSCOPE;
11003 dvar_defined_gen(struct parser_params *parser, ID id, ID **vidrefp)
11005 struct vtable *vars, *args, *used;
11008 args = lvtbl->args;
11009 vars = lvtbl->vars;
11010 used = lvtbl->used;
11012 while (POINTER_P(vars)) {
11013 if (vtable_included(args, id)) {
11016 if ((i = vtable_included(vars, id)) != 0) {
11017 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
11022 if (!vidrefp) used = 0;
11023 if (used) used = used->prev;
11026 if (vars == DVARS_INHERIT) {
11027 return rb_dvar_defined(id, parser->base_block);
11034 dvar_curr_gen(struct parser_params *parser, ID id)
11036 return (vtable_included(lvtbl->args, id) ||
11037 vtable_included(lvtbl->vars, id));
11041 reg_fragment_enc_error(struct parser_params* parser, VALUE str, int c)
11043 compile_error(PARSER_ARG
11044 "regexp encoding option '%c' differs from source encoding '%s'",
11045 c, rb_enc_name(rb_enc_get(str)));
11050 rb_reg_fragment_setenc(struct parser_params* parser, VALUE str, int options)
11052 int c = RE_OPTION_ENCODING_IDX(options);
11056 rb_char_to_option_kcode(c, &opt, &idx);
11057 if (idx != ENCODING_GET(str) &&
11058 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
11061 ENCODING_SET(str, idx);
11063 else if (RE_OPTION_ENCODING_NONE(options)) {
11064 if (!ENCODING_IS_ASCII8BIT(str) &&
11065 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
11069 rb_enc_associate(str, rb_ascii8bit_encoding());
11071 else if (current_enc == rb_usascii_encoding()) {
11072 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
11073 /* raise in re.c */
11074 rb_enc_associate(str, rb_usascii_encoding());
11077 rb_enc_associate(str, rb_ascii8bit_encoding());
11087 reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options)
11089 int c = rb_reg_fragment_setenc(parser, str, options);
11090 if (c) reg_fragment_enc_error(parser, str, c);
11094 reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options)
11097 reg_fragment_setenc(str, options);
11098 err = rb_reg_check_preprocess(str);
11100 err = rb_obj_as_string(err);
11101 compile_error(PARSER_ARG "%"PRIsVALUE, err);
11108 struct parser_params* parser;
11112 } reg_named_capture_assign_t;
11115 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
11116 int back_num, int *back_refs, OnigRegex regex, void *arg0)
11118 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
11119 struct parser_params* parser = arg->parser;
11120 rb_encoding *enc = arg->enc;
11121 long len = name_end - name;
11122 const char *s = (const char *)name;
11126 if (!len || (*name != '_' && ISASCII(*name) && !rb_enc_islower(*name, enc)) ||
11127 (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) ||
11128 !rb_enc_symname2_p(s, len, enc)) {
11129 return ST_CONTINUE;
11131 var = intern_cstr(s, len, enc);
11132 node = node_assign(assignable(var, 0, arg->column), new_lit(ID2SYM(var), arg->column), arg->column);
11133 succ = arg->succ_block;
11134 if (!succ) succ = new_begin(0, arg->column);
11135 succ = block_append(succ, node, arg->column);
11136 arg->succ_block = succ;
11137 return ST_CONTINUE;
11141 reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, int column)
11143 reg_named_capture_assign_t arg;
11145 arg.parser = parser;
11146 arg.enc = rb_enc_get(regexp);
11147 arg.succ_block = 0;
11148 arg.column = column;
11149 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
11151 if (!arg.succ_block) return 0;
11152 return arg.succ_block->nd_next;
11156 parser_reg_compile(struct parser_params* parser, VALUE str, int options)
11158 reg_fragment_setenc(str, options);
11159 return rb_parser_reg_compile(parser, str, options);
11163 rb_parser_reg_compile(struct parser_params* parser, VALUE str, int options)
11165 return rb_reg_compile(str, options & RE_OPTION_MASK, ruby_sourcefile, ruby_sourceline);
11169 reg_compile_gen(struct parser_params* parser, VALUE str, int options)
11174 err = rb_errinfo();
11175 re = parser_reg_compile(parser, str, options);
11177 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
11178 rb_set_errinfo(err);
11179 compile_error(PARSER_ARG "%"PRIsVALUE, m);
11186 parser_reg_compile(struct parser_params* parser, VALUE str, int options, VALUE *errmsg)
11188 VALUE err = rb_errinfo();
11190 int c = rb_reg_fragment_setenc(parser, str, options);
11191 if (c) reg_fragment_enc_error(parser, str, c);
11192 re = rb_parser_reg_compile(parser, str, options);
11194 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
11195 rb_set_errinfo(err);
11203 rb_parser_append_print(VALUE vparser, NODE *node)
11206 NODE *scope = node;
11207 struct parser_params *parser;
11209 if (!node) return node;
11211 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
11213 node = node->nd_body;
11215 if (nd_type(node) == NODE_PRELUDE) {
11217 node = node->nd_body;
11220 node = block_append(node,
11221 new_fcall(rb_intern("print"),
11222 NEW_ARRAY(new_gvar(idLASTLINE, 0)), 0),
11225 prelude->nd_body = node;
11226 scope->nd_body = prelude;
11229 scope->nd_body = node;
11236 rb_parser_while_loop(VALUE vparser, NODE *node, int chomp, int split)
11239 NODE *scope = node;
11240 struct parser_params *parser;
11242 if (!node) return node;
11244 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
11246 node = node->nd_body;
11248 if (nd_type(node) == NODE_PRELUDE) {
11250 node = node->nd_body;
11253 node = block_append(NEW_GASGN(rb_intern("$F"),
11254 new_call(new_gvar(idLASTLINE, 0),
11255 rb_intern("split"), 0, 0)),
11259 node = block_append(new_call(new_gvar(idLASTLINE, 0),
11260 rb_intern("chomp!"), 0, 0), node, 0);
11263 node = NEW_OPT_N(node);
11266 prelude->nd_body = node;
11267 scope->nd_body = prelude;
11270 scope->nd_body = node;
11277 rb_init_parse(void)
11279 /* just to suppress unused-function warnings */
11283 #endif /* !RIPPER */
11286 internal_id_gen(struct parser_params *parser)
11288 ID id = (ID)vtable_size(lvtbl->args) + (ID)vtable_size(lvtbl->vars);
11289 id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1;
11290 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
11294 parser_initialize(struct parser_params *parser)
11296 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
11297 command_start = TRUE;
11298 ruby_sourcefile_string = Qnil;
11300 parser->delayed = Qnil;
11301 parser->result = Qnil;
11302 parser->parsing_thread = Qnil;
11304 parser->error_buffer = Qfalse;
11306 parser->debug_buffer = Qnil;
11307 parser->debug_output = rb_stdout;
11308 parser->enc = rb_utf8_encoding();
11312 #define parser_mark ripper_parser_mark
11313 #define parser_free ripper_parser_free
11317 parser_mark(void *ptr)
11319 struct parser_params *parser = (struct parser_params*)ptr;
11321 rb_gc_mark((VALUE)lex_strterm);
11322 rb_gc_mark(lex_input);
11323 rb_gc_mark(lex_lastline);
11324 rb_gc_mark(lex_nextline);
11325 rb_gc_mark(ruby_sourcefile_string);
11327 rb_gc_mark((VALUE)ruby_eval_tree_begin);
11328 rb_gc_mark((VALUE)ruby_eval_tree);
11329 rb_gc_mark(ruby_debug_lines);
11330 rb_gc_mark(parser->compile_option);
11331 rb_gc_mark(parser->error_buffer);
11333 rb_gc_mark(parser->delayed);
11334 rb_gc_mark(parser->value);
11335 rb_gc_mark(parser->result);
11336 rb_gc_mark(parser->parsing_thread);
11338 rb_gc_mark(parser->debug_buffer);
11339 rb_gc_mark(parser->debug_output);
11341 rb_gc_mark((VALUE)parser->heap);
11346 parser_free(void *ptr)
11348 struct parser_params *parser = (struct parser_params*)ptr;
11349 struct local_vars *local, *prev;
11354 for (local = lvtbl; local; local = prev) {
11355 if (local->vars) xfree(local->vars);
11356 prev = local->prev;
11360 token_info *ptinfo;
11361 while ((ptinfo = parser->token_info) != 0) {
11362 parser->token_info = ptinfo->next;
11370 parser_memsize(const void *ptr)
11372 struct parser_params *parser = (struct parser_params*)ptr;
11373 struct local_vars *local;
11374 size_t size = sizeof(*parser);
11377 for (local = lvtbl; local; local = local->prev) {
11378 size += sizeof(*local);
11379 if (local->vars) size += local->vars->capa * sizeof(ID);
11384 static const rb_data_type_t parser_data_type = {
11395 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
11399 #undef rb_reserved_word
11401 const struct kwtable *
11402 rb_reserved_word(const char *str, unsigned int len)
11404 return reserved_word(str, len);
11408 rb_parser_new(void)
11410 struct parser_params *p;
11411 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
11412 &parser_data_type, p);
11413 parser_initialize(p);
11418 rb_parser_set_context(VALUE vparser, const struct rb_block *base, int main)
11420 struct parser_params *parser;
11422 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
11423 parser->error_buffer = main ? Qfalse : Qnil;
11424 parser->base_block = base;
11431 #define rb_parser_end_seen_p ripper_parser_end_seen_p
11432 #define rb_parser_encoding ripper_parser_encoding
11433 #define rb_parser_get_yydebug ripper_parser_get_yydebug
11434 #define rb_parser_set_yydebug ripper_parser_set_yydebug
11435 static VALUE ripper_parser_end_seen_p(VALUE vparser);
11436 static VALUE ripper_parser_encoding(VALUE vparser);
11437 static VALUE ripper_parser_get_yydebug(VALUE self);
11438 static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
11442 * ripper.error? -> Boolean
11444 * Return true if parsed source has errors.
11447 ripper_error_p(VALUE vparser)
11449 struct parser_params *parser;
11451 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
11452 return parser->error_p ? Qtrue : Qfalse;
11458 * ripper.end_seen? -> Boolean
11460 * Return true if parsed source ended by +\_\_END\_\_+.
11463 rb_parser_end_seen_p(VALUE vparser)
11465 struct parser_params *parser;
11467 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
11468 return ruby__end__seen ? Qtrue : Qfalse;
11473 * ripper.encoding -> encoding
11475 * Return encoding of the source.
11478 rb_parser_encoding(VALUE vparser)
11480 struct parser_params *parser;
11482 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser);
11483 return rb_enc_from_encoding(current_enc);
11488 * ripper.yydebug -> true or false
11493 rb_parser_get_yydebug(VALUE self)
11495 struct parser_params *parser;
11497 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11498 return yydebug ? Qtrue : Qfalse;
11503 * ripper.yydebug = flag
11508 rb_parser_set_yydebug(VALUE self, VALUE flag)
11510 struct parser_params *parser;
11512 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11513 yydebug = RTEST(flag);
11519 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
11520 #define NEWHEAP() (rb_imemo_alloc_t *)rb_imemo_new(imemo_alloc, 0, (VALUE)parser->heap, 0, 0)
11521 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->ptr = (p), \
11522 (n)->cnt = (c), (p))
11525 rb_parser_malloc(struct parser_params *parser, size_t size)
11527 size_t cnt = HEAPCNT(1, size);
11528 rb_imemo_alloc_t *n = NEWHEAP();
11529 void *ptr = xmalloc(size);
11531 return ADD2HEAP(n, cnt, ptr);
11535 rb_parser_calloc(struct parser_params *parser, size_t nelem, size_t size)
11537 size_t cnt = HEAPCNT(nelem, size);
11538 rb_imemo_alloc_t *n = NEWHEAP();
11539 void *ptr = xcalloc(nelem, size);
11541 return ADD2HEAP(n, cnt, ptr);
11545 rb_parser_realloc(struct parser_params *parser, void *ptr, size_t size)
11547 rb_imemo_alloc_t *n;
11548 size_t cnt = HEAPCNT(1, size);
11550 if (ptr && (n = parser->heap) != NULL) {
11552 if (n->ptr == ptr) {
11553 n->ptr = ptr = xrealloc(ptr, size);
11554 if (n->cnt) n->cnt = cnt;
11557 } while ((n = n->next) != NULL);
11560 ptr = xrealloc(ptr, size);
11561 return ADD2HEAP(n, cnt, ptr);
11565 rb_parser_free(struct parser_params *parser, void *ptr)
11567 rb_imemo_alloc_t **prev = &parser->heap, *n;
11569 while ((n = *prev) != NULL) {
11570 if (n->ptr == ptr) {
11572 rb_gc_force_recycle((VALUE)n);
11582 rb_parser_printf(struct parser_params *parser, const char *fmt, ...)
11585 VALUE mesg = parser->debug_buffer;
11587 if (NIL_P(mesg)) parser->debug_buffer = mesg = rb_str_new(0, 0);
11589 rb_str_vcatf(mesg, fmt, ap);
11591 if (RSTRING_END(mesg)[-1] == '\n') {
11592 rb_io_write(parser->debug_output, mesg);
11593 parser->debug_buffer = Qnil;
11598 parser_compile_error(struct parser_params *parser, const char *fmt, ...)
11602 rb_io_flush(parser->debug_output);
11603 parser->error_p = 1;
11605 parser->error_buffer =
11606 rb_syntax_error_append(parser->error_buffer,
11607 ruby_sourcefile_string,
11609 rb_long2int(lex_p - lex_pbeg),
11610 current_enc, fmt, ap);
11616 #ifdef RIPPER_DEBUG
11617 extern int rb_is_pointer_to_heap(VALUE);
11621 ripper_validate_object(VALUE self, VALUE x)
11623 if (x == Qfalse) return x;
11624 if (x == Qtrue) return x;
11625 if (x == Qnil) return x;
11627 rb_raise(rb_eArgError, "Qundef given");
11628 if (FIXNUM_P(x)) return x;
11629 if (SYMBOL_P(x)) return x;
11630 if (!rb_is_pointer_to_heap(x))
11631 rb_raise(rb_eArgError, "invalid pointer: %p", x);
11632 switch (BUILTIN_TYPE(x)) {
11642 if (nd_type(x) != NODE_RIPPER) {
11643 rb_raise(rb_eArgError, "NODE given: %p", x);
11645 return ((NODE *)x)->nd_rval;
11647 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
11648 x, rb_obj_classname(x));
11654 #define validate(x) ((x) = get_value(x))
11657 ripper_dispatch0(struct parser_params *parser, ID mid)
11659 return rb_funcall(parser->value, mid, 0);
11663 ripper_dispatch1(struct parser_params *parser, ID mid, VALUE a)
11666 return rb_funcall(parser->value, mid, 1, a);
11670 ripper_dispatch2(struct parser_params *parser, ID mid, VALUE a, VALUE b)
11674 return rb_funcall(parser->value, mid, 2, a, b);
11678 ripper_dispatch3(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c)
11683 return rb_funcall(parser->value, mid, 3, a, b, c);
11687 ripper_dispatch4(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
11693 return rb_funcall(parser->value, mid, 4, a, b, c, d);
11697 ripper_dispatch5(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
11704 return rb_funcall(parser->value, mid, 5, a, b, c, d, e);
11708 ripper_dispatch7(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
11717 return rb_funcall(parser->value, mid, 7, a, b, c, d, e, f, g);
11721 ripper_get_id(VALUE v)
11724 if (!RB_TYPE_P(v, T_NODE)) return 0;
11726 if (nd_type(nd) != NODE_RIPPER) return 0;
11731 ripper_get_value(VALUE v)
11734 if (v == Qundef) return Qnil;
11735 if (!RB_TYPE_P(v, T_NODE)) return v;
11737 if (nd_type(nd) != NODE_RIPPER) return Qnil;
11738 return nd->nd_rval;
11742 ripper_error_gen(struct parser_params *parser)
11744 parser->error_p = TRUE;
11748 ripper_compile_error(struct parser_params *parser, const char *fmt, ...)
11753 va_start(args, fmt);
11754 str = rb_vsprintf(fmt, args);
11756 rb_funcall(parser->value, rb_intern("compile_error"), 1, str);
11757 ripper_error_gen(parser);
11761 ripper_lex_get_generic(struct parser_params *parser, VALUE src)
11763 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
11764 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
11765 rb_raise(rb_eTypeError,
11766 "gets returned %"PRIsVALUE" (expected String or nil)",
11767 rb_obj_class(line));
11773 ripper_lex_io_get(struct parser_params *parser, VALUE src)
11775 return rb_io_gets(src);
11779 ripper_s_allocate(VALUE klass)
11781 struct parser_params *p;
11782 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
11783 &parser_data_type, p);
11788 #define ripper_initialized_p(r) ((r)->lex.input != 0)
11792 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
11794 * Create a new Ripper object.
11795 * _src_ must be a String, an IO, or an Object which has #gets method.
11797 * This method does not starts parsing.
11798 * See also Ripper#parse and Ripper.parse.
11801 ripper_initialize(int argc, VALUE *argv, VALUE self)
11803 struct parser_params *parser;
11804 VALUE src, fname, lineno;
11806 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11807 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
11808 if (RB_TYPE_P(src, T_FILE)) {
11809 lex_gets = ripper_lex_io_get;
11811 else if (rb_respond_to(src, id_gets)) {
11812 lex_gets = ripper_lex_get_generic;
11816 lex_gets = lex_get_str;
11820 if (NIL_P(fname)) {
11821 fname = STR_NEW2("(ripper)");
11825 StringValueCStr(fname);
11826 fname = rb_str_new_frozen(fname);
11828 parser_initialize(parser);
11830 ruby_sourcefile_string = fname;
11831 ruby_sourcefile = RSTRING_PTR(fname);
11832 ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
11837 struct ripper_args {
11838 struct parser_params *parser;
11844 ripper_parse0(VALUE parser_v)
11846 struct parser_params *parser;
11848 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser);
11849 parser_prepare(parser);
11850 ripper_yyparse((void*)parser);
11851 return parser->result;
11855 ripper_ensure(VALUE parser_v)
11857 struct parser_params *parser;
11859 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser);
11860 parser->parsing_thread = Qnil;
11868 * Start parsing and returns the value of the root action.
11871 ripper_parse(VALUE self)
11873 struct parser_params *parser;
11875 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11876 if (!ripper_initialized_p(parser)) {
11877 rb_raise(rb_eArgError, "method called for uninitialized object");
11879 if (!NIL_P(parser->parsing_thread)) {
11880 if (parser->parsing_thread == rb_thread_current())
11881 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
11883 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
11885 parser->parsing_thread = rb_thread_current();
11886 rb_ensure(ripper_parse0, self, ripper_ensure, self);
11888 return parser->result;
11893 * ripper.column -> Integer
11895 * Return column number of current parsing line.
11896 * This number starts from 0.
11899 ripper_column(VALUE self)
11901 struct parser_params *parser;
11904 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11905 if (!ripper_initialized_p(parser)) {
11906 rb_raise(rb_eArgError, "method called for uninitialized object");
11908 if (NIL_P(parser->parsing_thread)) return Qnil;
11909 col = parser->tokp - lex_pbeg;
11910 return LONG2NUM(col);
11915 * ripper.filename -> String
11917 * Return current parsing filename.
11920 ripper_filename(VALUE self)
11922 struct parser_params *parser;
11924 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11925 if (!ripper_initialized_p(parser)) {
11926 rb_raise(rb_eArgError, "method called for uninitialized object");
11928 return ruby_sourcefile_string;
11933 * ripper.lineno -> Integer
11935 * Return line number of current parsing line.
11936 * This number starts from 1.
11939 ripper_lineno(VALUE self)
11941 struct parser_params *parser;
11943 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11944 if (!ripper_initialized_p(parser)) {
11945 rb_raise(rb_eArgError, "method called for uninitialized object");
11947 if (NIL_P(parser->parsing_thread)) return Qnil;
11948 return INT2NUM(ruby_sourceline);
11953 * ripper.state -> Integer
11955 * Return scanner state of current token.
11958 ripper_state(VALUE self)
11960 struct parser_params *parser;
11962 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser);
11963 if (!ripper_initialized_p(parser)) {
11964 rb_raise(rb_eArgError, "method called for uninitialized object");
11966 if (NIL_P(parser->parsing_thread)) return Qnil;
11967 return INT2NUM(lex_state);
11970 #ifdef RIPPER_DEBUG
11973 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
11976 if (obj == Qundef) {
11977 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
11984 ripper_value(VALUE self, VALUE obj)
11986 return ULONG2NUM(obj);
11991 ripper_lex_state_name(VALUE self, VALUE state)
11993 return rb_parser_lex_state_name(NUM2INT(state));
11999 ripper_init_eventids1();
12000 ripper_init_eventids2();
12001 id_warn = rb_intern_const("warn");
12002 id_warning = rb_intern_const("warning");
12003 id_gets = rb_intern_const("gets");
12009 InitVM_ripper(void)
12013 Ripper = rb_define_class("Ripper", rb_cObject);
12014 /* version of Ripper */
12015 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
12016 rb_define_alloc_func(Ripper, ripper_s_allocate);
12017 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
12018 rb_define_method(Ripper, "parse", ripper_parse, 0);
12019 rb_define_method(Ripper, "column", ripper_column, 0);
12020 rb_define_method(Ripper, "filename", ripper_filename, 0);
12021 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
12022 rb_define_method(Ripper, "state", ripper_state, 0);
12023 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
12024 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
12025 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
12026 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
12027 rb_define_method(Ripper, "error?", ripper_error_p, 0);
12028 #ifdef RIPPER_DEBUG
12029 rb_define_method(rb_mKernel, "assert_Qundef", ripper_assert_Qundef, 2);
12030 rb_define_method(rb_mKernel, "rawVALUE", ripper_value, 1);
12031 rb_define_method(rb_mKernel, "validate_object", ripper_validate_object, 1);
12034 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
12035 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
12037 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
12039 /* ignore newline, +/- is a sign. */
12040 rb_define_const(Ripper, "EXPR_BEG", INT2NUM(EXPR_BEG));
12041 /* newline significant, +/- is an operator. */
12042 rb_define_const(Ripper, "EXPR_END", INT2NUM(EXPR_END));
12043 /* ditto, and unbound braces. */
12044 rb_define_const(Ripper, "EXPR_ENDARG", INT2NUM(EXPR_ENDARG));
12045 /* ditto, and unbound braces. */
12046 rb_define_const(Ripper, "EXPR_ENDFN", INT2NUM(EXPR_ENDFN));
12047 /* newline significant, +/- is an operator. */
12048 rb_define_const(Ripper, "EXPR_ARG", INT2NUM(EXPR_ARG));
12049 /* newline significant, +/- is an operator. */
12050 rb_define_const(Ripper, "EXPR_CMDARG", INT2NUM(EXPR_CMDARG));
12051 /* newline significant, +/- is an operator. */
12052 rb_define_const(Ripper, "EXPR_MID", INT2NUM(EXPR_MID));
12053 /* ignore newline, no reserved words. */
12054 rb_define_const(Ripper, "EXPR_FNAME", INT2NUM(EXPR_FNAME));
12055 /* right after `.' or `::', no reserved words. */
12056 rb_define_const(Ripper, "EXPR_DOT", INT2NUM(EXPR_DOT));
12057 /* immediate after `class', no here document. */
12058 rb_define_const(Ripper, "EXPR_CLASS", INT2NUM(EXPR_CLASS));
12059 /* flag bit, label is allowed. */
12060 rb_define_const(Ripper, "EXPR_LABEL", INT2NUM(EXPR_LABEL));
12061 /* flag bit, just after a label. */
12062 rb_define_const(Ripper, "EXPR_LABELED", INT2NUM(EXPR_LABELED));
12063 /* symbol literal as FNAME. */
12064 rb_define_const(Ripper, "EXPR_FITEM", INT2NUM(EXPR_FITEM));
12065 /* equals to +EXPR_BEG+ */
12066 rb_define_const(Ripper, "EXPR_VALUE", INT2NUM(EXPR_VALUE));
12067 /* equals to <tt>(EXPR_BEG | EXPR_MID | EXPR_CLASS)</tt> */
12068 rb_define_const(Ripper, "EXPR_BEG_ANY", INT2NUM(EXPR_BEG_ANY));
12069 /* equals to <tt>(EXPR_ARG | EXPR_CMDARG)</tt> */
12070 rb_define_const(Ripper, "EXPR_ARG_ANY", INT2NUM(EXPR_ARG_ANY));
12071 /* equals to <tt>(EXPR_END | EXPR_ENDARG | EXPR_ENDFN)</tt> */
12072 rb_define_const(Ripper, "EXPR_END_ANY", INT2NUM(EXPR_END_ANY));
12074 ripper_init_eventids1_table(Ripper);
12075 ripper_init_eventids2_table(Ripper);
12078 /* Hack to let RDoc document SCRIPT_LINES__ */
12081 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
12082 * after the assignment will be added as an Array of lines with the file
12085 rb_define_global_const("SCRIPT_LINES__", Qnil);
12089 #endif /* RIPPER */