1/**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10**********************************************************************/
17# error needs pure parser
20#define YYERROR_VERBOSE 1
21#define YYSTACK_USE_ALLOCA 0
22#define YYLTYPE rb_code_location_t
23#define YYLTYPE_IS_DECLARED 1
25#include "ruby/internal/config.h"
34#include "internal/compile.h"
35#include "internal/compilers.h"
36#include "internal/complex.h"
37#include "internal/encoding.h"
38#include "internal/error.h"
39#include "internal/hash.h"
40#include "internal/imemo.h"
41#include "internal/io.h"
42#include "internal/numeric.h"
43#include "internal/parse.h"
44#include "internal/rational.h"
45#include "internal/re.h"
46#include "internal/symbol.h"
47#include "internal/thread.h"
48#include "internal/variable.h"
52#include "ruby/encoding.h"
53#include "ruby/regex.h"
57#include "ruby/ractor.h"
68 unsigned int in_defined: 1;
69 unsigned int in_kwarg: 1;
70 unsigned int in_argdef: 1;
71 unsigned int in_def: 1;
72 unsigned int in_class: 1;
73 BITFIELD(enum shareability, shareable_constant_value, 2);
76#if defined(__GNUC__) && !defined(__clang__)
77// Suppress "parameter passing for argument of type 'struct
78// lex_context' changed" notes. `struct lex_context` is file scope,
79// and has no ABI compatibility issue.
81RBIMPL_WARNING_IGNORED(-Wpsabi)
83// Not sure why effective even after popped.
88#define NO_LEX_CTXT (struct lex_context){0}
90#define AREF(ary, i) RARRAY_AREF(ary, i)
92#ifndef WARN_PAST_SCOPE
93# define WARN_PAST_SCOPE 0
98#define yydebug (p->debug) /* disable the global variable definition */
100#define YYMALLOC(size) rb_parser_malloc(p, (size))
101#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
102#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
103#define YYFREE(ptr) rb_parser_free(p, (ptr))
104#define YYFPRINTF rb_parser_printf
105#define YY_LOCATION_PRINT(File, loc) \
106 rb_parser_printf(p, "%d.%d-%d.%d", \
107 (loc).beg_pos.lineno, (loc).beg_pos.column,\
108 (loc).end_pos.lineno, (loc).end_pos.column)
109#define YYLLOC_DEFAULT(Current, Rhs, N) \
113 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
114 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
118 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
119 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
123 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
124 "nesting too deep" : (Msgid))
126#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
127 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
128#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
129 rb_parser_set_location_of_delayed_token(p, &(Current))
130#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
131 rb_parser_set_location_of_heredoc_end(p, &(Current))
132#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
133 rb_parser_set_location_of_dummy_end(p, &(Current))
134#define RUBY_SET_YYLLOC_OF_NONE(Current) \
135 rb_parser_set_location_of_none(p, &(Current))
136#define RUBY_SET_YYLLOC(Current) \
137 rb_parser_set_location(p, &(Current))
138#define RUBY_INIT_YYLLOC() \
140 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
141 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
145 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
146 EXPR_END_bit, /* newline significant, +/- is an operator. */
147 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
148 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
149 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
150 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
151 EXPR_MID_bit, /* newline significant, +/- is an operator. */
152 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
153 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
154 EXPR_CLASS_bit, /* immediate after `class', no here document. */
155 EXPR_LABEL_bit, /* flag bit, label is allowed. */
156 EXPR_LABELED_bit, /* flag bit, just after a label. */
157 EXPR_FITEM_bit, /* symbol literal as FNAME. */
160/* examine combinations */
162#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
176 EXPR_VALUE = EXPR_BEG,
177 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
178 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
179 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
182#define IS_lex_state_for(x, ls) ((x) & (ls))
183#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
184#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
185#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
187# define SET_LEX_STATE(ls) \
188 parser_set_lex_state(p, ls, __LINE__)
189static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
191typedef VALUE stack_type;
193static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
195# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
196# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
197# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
198# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
199# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
201/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
202 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
203#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
204#define COND_POP() BITSTACK_POP(cond_stack)
205#define COND_P() BITSTACK_SET_P(cond_stack)
206#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
208/* A flag to identify keyword_do_block; "do" keyword after command_call.
209 Example: `foo 1, 2 do`. */
210#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
211#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
212#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
213#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
229 struct local_vars *prev;
232 NODE *outer, *inner, *current;
243#define NUMPARAM_ID_P(id) numparam_id_p(id)
244#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
245#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
249 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
250 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
251 return idx > 0 && idx <= NUMPARAM_MAX;
253static void numparam_name(struct parser_params *p, ID id);
255#define DVARS_INHERIT ((void*)1)
256#define DVARS_TOPSCOPE NULL
257#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
259typedef struct token_info {
261 rb_code_position_t beg;
264 struct token_info *next;
267typedef struct rb_strterm_struct rb_strterm_t;
270 Structure of Lexer Buffer:
272 lex.pbeg lex.ptok lex.pcur lex.pend
274 |------------+------------+------------|
278struct parser_params {
279 rb_imemo_tmpbuf_t *heap;
285 rb_strterm_t *strterm;
286 VALUE (*gets)(struct parser_params*,VALUE);
296 VALUE (*call)(VALUE, int);
298 enum lex_state_e state;
299 /* track the nest level of any parens "()[]{}" */
301 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
303 /* track the nest level of only braces "{}" */
306 stack_type cond_stack;
307 stack_type cmdarg_stack;
313 int heredoc_line_indent;
315 struct local_vars *lvtbl;
319 int ruby_sourceline; /* current line no. */
320 const char *ruby_sourcefile; /* current source file */
321 VALUE ruby_sourcefile_string;
323 token_info *token_info;
325 VALUE compile_option;
345 struct lex_context ctxt;
347 unsigned int command_start:1;
348 unsigned int eofp: 1;
349 unsigned int ruby__end__seen: 1;
350 unsigned int debug: 1;
351 unsigned int has_shebang: 1;
352 unsigned int token_seen: 1;
353 unsigned int token_info_enabled: 1;
355 unsigned int past_scope_enabled: 1;
357 unsigned int error_p: 1;
358 unsigned int cr_seen: 1;
363 unsigned int do_print: 1;
364 unsigned int do_loop: 1;
365 unsigned int do_chomp: 1;
366 unsigned int do_split: 1;
367 unsigned int keep_script_lines: 1;
368 unsigned int error_tolerant: 1;
369 unsigned int keep_tokens: 1;
371 NODE *eval_tree_begin;
375 const struct rb_iseq_struct *parent_iseq;
376 /* store specific keyword locations to generate dummy end token */
377 VALUE end_expect_token_locations;
380 /* Array for term tokens */
387 VALUE parsing_thread;
391#define intern_cstr(n,l,en) rb_intern3(n,l,en)
393#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
394#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
395#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
396#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
397#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
400push_pvtbl(struct parser_params *p)
402 st_table *tbl = p->pvtbl;
403 p->pvtbl = st_init_numtable();
408pop_pvtbl(struct parser_params *p, st_table *tbl)
410 st_free_table(p->pvtbl);
415push_pktbl(struct parser_params *p)
417 st_table *tbl = p->pktbl;
423pop_pktbl(struct parser_params *p, st_table *tbl)
425 if (p->pktbl) st_free_table(p->pktbl);
430static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
433debug_end_expect_token_locations(struct parser_params *p, const char *name)
436 VALUE mesg = rb_sprintf("%s: ", name);
437 rb_str_catf(mesg, " %"PRIsVALUE"\n", p->end_expect_token_locations);
438 flush_debug_buffer(p, p->debug_output, mesg);
443push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
445 if(NIL_P(p->end_expect_token_locations)) return;
446 rb_ary_push(p->end_expect_token_locations, rb_ary_new_from_args(2, INT2NUM(pos->lineno), INT2NUM(pos->column)));
447 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
451pop_end_expect_token_locations(struct parser_params *p)
453 if(NIL_P(p->end_expect_token_locations)) return;
454 rb_ary_pop(p->end_expect_token_locations);
455 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
459peek_end_expect_token_locations(struct parser_params *p)
461 if(NIL_P(p->end_expect_token_locations)) return Qnil;
462 return rb_ary_last(0, 0, p->end_expect_token_locations);
466parser_token2id(enum yytokentype tok)
469#define TOKEN2ID(tok) case tok: return rb_intern(#tok);
470#define TOKEN2ID2(tok, name) case tok: return rb_intern(name);
471 TOKEN2ID2(' ', "words_sep")
491 TOKEN2ID2('\n', "nl");
498 TOKEN2ID(keyword_class);
499 TOKEN2ID(keyword_module);
500 TOKEN2ID(keyword_def);
501 TOKEN2ID(keyword_undef);
502 TOKEN2ID(keyword_begin);
503 TOKEN2ID(keyword_rescue);
504 TOKEN2ID(keyword_ensure);
505 TOKEN2ID(keyword_end);
506 TOKEN2ID(keyword_if);
507 TOKEN2ID(keyword_unless);
508 TOKEN2ID(keyword_then);
509 TOKEN2ID(keyword_elsif);
510 TOKEN2ID(keyword_else);
511 TOKEN2ID(keyword_case);
512 TOKEN2ID(keyword_when);
513 TOKEN2ID(keyword_while);
514 TOKEN2ID(keyword_until);
515 TOKEN2ID(keyword_for);
516 TOKEN2ID(keyword_break);
517 TOKEN2ID(keyword_next);
518 TOKEN2ID(keyword_redo);
519 TOKEN2ID(keyword_retry);
520 TOKEN2ID(keyword_in);
521 TOKEN2ID(keyword_do);
522 TOKEN2ID(keyword_do_cond);
523 TOKEN2ID(keyword_do_block);
524 TOKEN2ID(keyword_do_LAMBDA);
525 TOKEN2ID(keyword_return);
526 TOKEN2ID(keyword_yield);
527 TOKEN2ID(keyword_super);
528 TOKEN2ID(keyword_self);
529 TOKEN2ID(keyword_nil);
530 TOKEN2ID(keyword_true);
531 TOKEN2ID(keyword_false);
532 TOKEN2ID(keyword_and);
533 TOKEN2ID(keyword_or);
534 TOKEN2ID(keyword_not);
535 TOKEN2ID(modifier_if);
536 TOKEN2ID(modifier_unless);
537 TOKEN2ID(modifier_while);
538 TOKEN2ID(modifier_until);
539 TOKEN2ID(modifier_rescue);
540 TOKEN2ID(keyword_alias);
541 TOKEN2ID(keyword_defined);
542 TOKEN2ID(keyword_BEGIN);
543 TOKEN2ID(keyword_END);
544 TOKEN2ID(keyword__LINE__);
545 TOKEN2ID(keyword__FILE__);
546 TOKEN2ID(keyword__ENCODING__);
547 TOKEN2ID(tIDENTIFIER);
557 TOKEN2ID(tIMAGINARY);
561 TOKEN2ID(tSTRING_CONTENT);
562 TOKEN2ID(tREGEXP_END);
563 TOKEN2ID(tDUMNY_END);
592 TOKEN2ID(tLPAREN_ARG);
596 TOKEN2ID(tLBRACE_ARG);
602 TOKEN2ID(tSTRING_BEG);
603 TOKEN2ID(tXSTRING_BEG);
604 TOKEN2ID(tREGEXP_BEG);
605 TOKEN2ID(tWORDS_BEG);
606 TOKEN2ID(tQWORDS_BEG);
607 TOKEN2ID(tSYMBOLS_BEG);
608 TOKEN2ID(tQSYMBOLS_BEG);
609 TOKEN2ID(tSTRING_END);
610 TOKEN2ID(tSTRING_DEND);
611 TOKEN2ID(tSTRING_DBEG);
612 TOKEN2ID(tSTRING_DVAR);
614 TOKEN2ID(tLABEL_END);
615 TOKEN2ID(tIGNORED_NL);
617 TOKEN2ID(tEMBDOC_BEG);
619 TOKEN2ID(tEMBDOC_END);
620 TOKEN2ID(tHEREDOC_BEG);
621 TOKEN2ID(tHEREDOC_END);
624 TOKEN2ID(tUMINUS_NUM);
625 TOKEN2ID(tLAST_TOKEN);
630 rb_bug("parser_token2id: unknown token %d", tok);
632 UNREACHABLE_RETURN(0);
637RBIMPL_ATTR_NONNULL((1, 2, 3))
638static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
639RBIMPL_ATTR_NONNULL((1, 2))
640static int parser_yyerror0(struct parser_params*, const char*);
641#define yyerror0(msg) parser_yyerror0(p, (msg))
642#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
643#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
644#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
645#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
646#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
647#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
649static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
650static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
651static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
652static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
653static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
656#define compile_for_eval (0)
658#define compile_for_eval (p->parent_iseq != 0)
661#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
663#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
664#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
665#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
667#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
669static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
673rb_discard_node(struct parser_params *p, NODE *n)
675 rb_ast_delete_node(p->ast, n);
681add_mark_object(struct parser_params *p, VALUE obj)
683 if (!SPECIAL_CONST_P(obj)
684 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
686 rb_ast_add_mark_object(p->ast, obj);
691static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
694static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
695#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
697static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
700parser_get_node_id(struct parser_params *p)
702 int node_id = p->node_id;
709set_line_body(NODE *body, int line)
712 switch (nd_type(body)) {
715 nd_set_line(body, line);
719#define yyparse ruby_yyparse
721static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
722static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
723#define new_nil(loc) NEW_NIL(loc)
724static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
725static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
726static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
727static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
729static NODE *newline_node(NODE*);
730static void fixpos(NODE*,NODE*);
732static int value_expr_gen(struct parser_params*,NODE*);
733static void void_expr(struct parser_params*,NODE*);
734static NODE *remove_begin(NODE*);
735static NODE *remove_begin_all(NODE*);
736#define value_expr(node) value_expr_gen(p, (node))
737static NODE *void_stmts(struct parser_params*,NODE*);
738static void reduce_nodes(struct parser_params*,NODE**);
739static void block_dup_check(struct parser_params*,NODE*,NODE*);
741static NODE *block_append(struct parser_params*,NODE*,NODE*);
742static NODE *list_append(struct parser_params*,NODE*,NODE*);
743static NODE *list_concat(NODE*,NODE*);
744static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
745static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
746static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
747static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
748static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
749static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
750static NODE *evstr2dstr(struct parser_params*,NODE*);
751static NODE *splat_array(NODE*);
752static void mark_lvar_used(struct parser_params *p, NODE *rhs);
754static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
755static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
756static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
757static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
758static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {b->nd_iter = m; b->nd_loc = *loc; return b;}
760static bool args_info_empty_p(struct rb_args_info *args);
761static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
762static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
763static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
764static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
765static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
766static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
767static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
768static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
770static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
771static NODE *args_with_numbered(struct parser_params*,NODE*,int);
773static VALUE negate_lit(struct parser_params*, VALUE);
774static NODE *ret_args(struct parser_params*,NODE*);
775static NODE *arg_blk_pass(NODE*,NODE*);
776static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
777static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
779static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
780static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
782static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
783static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
785static void rb_backref_error(struct parser_params*,NODE*);
786static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
788static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
789static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
790static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
791static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
792static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
794static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
796static NODE *opt_arg_append(NODE*, NODE*);
797static NODE *kwd_append(NODE*, NODE*);
799static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
800static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
802static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
804static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
806#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
808static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
810static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
812static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
814static rb_ast_id_table_t *local_tbl(struct parser_params*);
816static VALUE reg_compile(struct parser_params*, VALUE, int);
817static void reg_fragment_setenc(struct parser_params*, VALUE, int);
818static int reg_fragment_check(struct parser_params*, VALUE, int);
819static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
821static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
822static NODE *heredoc_dedent(struct parser_params*,NODE*);
824static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
826#define get_id(id) (id)
827#define get_value(val) (val)
828#define get_num(num) (num)
830#define NODE_RIPPER NODE_CDECL
831#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
833static inline int ripper_is_node_yylval(VALUE n);
836ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
838 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
839 add_mark_object(p, b);
840 add_mark_object(p, c);
841 return NEW_RIPPER(a, b, c, &NULL_LOC);
845ripper_is_node_yylval(VALUE n)
847 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
850#define value_expr(node) ((void)(node))
851#define remove_begin(node) (node)
852#define void_stmts(p,x) (x)
853#define rb_dvar_defined(id, base) 0
854#define rb_local_defined(id, base) 0
855static ID ripper_get_id(VALUE);
856#define get_id(id) ripper_get_id(id)
857static VALUE ripper_get_value(VALUE);
858#define get_value(val) ripper_get_value(val)
859#define get_num(num) (int)get_id(num)
860static VALUE assignable(struct parser_params*,VALUE);
861static int id_is_var(struct parser_params *p, ID id);
863#define method_cond(p,node,loc) (node)
864#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
865#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
866#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
867#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
869#define new_nil(loc) Qnil
871static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
873static VALUE const_decl(struct parser_params *p, VALUE path);
875static VALUE var_field(struct parser_params *p, VALUE a);
876static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
878static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
880static VALUE backref_error(struct parser_params*, NODE *, VALUE);
883/* forward declaration */
884typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
886RUBY_SYMBOL_EXPORT_BEGIN
887VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
888int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
889enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
890VALUE rb_parser_lex_state_name(enum lex_state_e state);
891void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
892PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
893YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
894YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
895YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
896YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
897YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
898YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
899RUBY_SYMBOL_EXPORT_END
901static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
902static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
904static ID formal_argument(struct parser_params*, ID);
906static ID formal_argument(struct parser_params*, VALUE);
908static ID shadowing_lvar(struct parser_params*,ID);
909static void new_bv(struct parser_params*,ID);
911static void local_push(struct parser_params*,int);
912static void local_pop(struct parser_params*);
913static void local_var(struct parser_params*, ID);
914static void arg_var(struct parser_params*, ID);
915static int local_id(struct parser_params *p, ID id);
916static int local_id_ref(struct parser_params*, ID, ID **);
918static ID internal_id(struct parser_params*);
919static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
921static int check_forwarding_args(struct parser_params*);
922static void add_forwarding_args(struct parser_params *p);
924static const struct vtable *dyna_push(struct parser_params *);
925static void dyna_pop(struct parser_params*, const struct vtable *);
926static int dyna_in_block(struct parser_params*);
927#define dyna_var(p, id) local_var(p, id)
928static int dvar_defined(struct parser_params*, ID);
929static int dvar_defined_ref(struct parser_params*, ID, ID**);
930static int dvar_curr(struct parser_params*,ID);
932static int lvar_defined(struct parser_params*, ID);
934static NODE *numparam_push(struct parser_params *p);
935static void numparam_pop(struct parser_params *p, NODE *prev_inner);
938# define METHOD_NOT idNOT
940# define METHOD_NOT '!'
943#define idFWD_REST '*'
944#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
945#define idFWD_BLOCK '&'
946#define idFWD_ALL idDot3
947#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
949#define RE_OPTION_ONCE (1<<16)
950#define RE_OPTION_ENCODING_SHIFT 8
951#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
952#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
953#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
954#define RE_OPTION_MASK 0xff
955#define RE_OPTION_ARG_ENCODING_NONE 32
957/* structs for managing terminator of string literal and heredocment */
958typedef struct rb_strterm_literal_struct {
965 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
969 long paren; /* '(' of `%q(...)` */
973 long term; /* ')' of `%q(...)` */
975} rb_strterm_literal_t;
977#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
979struct rb_strterm_heredoc_struct {
980 VALUE lastline; /* the string of line that contains `<<"END"` */
981 long offset; /* the column of END in `<<"END"` */
982 int sourceline; /* lineno of the line that contains `<<"END"` */
983 unsigned length /* the length of END in `<<"END"` */
984#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
985 : HERETERM_LENGTH_BITS
986# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
988# define HERETERM_LENGTH_MAX UINT_MAX
991#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
999STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
1001#define STRTERM_HEREDOC IMEMO_FL_USER0
1003struct rb_strterm_struct {
1006 rb_strterm_literal_t literal;
1007 rb_strterm_heredoc_t heredoc;
1013rb_strterm_mark(VALUE obj)
1015 rb_strterm_t *strterm = (rb_strterm_t*)obj;
1016 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
1017 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
1018 rb_gc_mark(heredoc->lastline);
1023#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1024size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1026#define TOKEN2ID(tok) ( \
1027 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1028 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1029 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1030 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1031 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1032 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1033 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1035/****** Ripper *******/
1038#define RIPPER_VERSION "0.1.0"
1040static inline VALUE intern_sym(const char *name);
1042#include "eventids1.c"
1043#include "eventids2.c"
1045static VALUE ripper_dispatch0(struct parser_params*,ID);
1046static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1047static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1048static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1049static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1050static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1051static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1052static void ripper_error(struct parser_params *p);
1054#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1055#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1056#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1057#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1058#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1059#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1060#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e), (f), (g))
1062#define yyparse ripper_yyparse
1064#define ID2VAL(id) STATIC_ID2SYM(id)
1065#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1066#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1068#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1069 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1071#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
1074new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1076 NODE *t = (NODE *)tail;
1077 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
1078 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
1082new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1084 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
1085 add_mark_object(p, kw_args);
1086 add_mark_object(p, kw_rest_arg);
1087 add_mark_object(p, block);
1092args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1098new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1100 NODE *t = (NODE *)aryptn;
1101 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
1103 if (!NIL_P(pre_arg)) {
1104 if (!NIL_P(pre_args)) {
1105 rb_ary_unshift(pre_args, pre_arg);
1108 pre_args = rb_ary_new_from_args(1, pre_arg);
1111 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1115new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1120 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
1126 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
1127 add_mark_object(p, pre_args);
1128 add_mark_object(p, rest_arg);
1129 add_mark_object(p, post_args);
1134new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1136 NODE *t = (NODE *)fndptn;
1137 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
1139 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1143new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1147 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
1148 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
1150 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
1151 add_mark_object(p, pre_rest_arg);
1152 add_mark_object(p, args);
1153 add_mark_object(p, post_rest_arg);
1157#define new_hash(p,h,l) rb_ary_new_from_args(0)
1160new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1166new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1168 NODE *t = (NODE *)hshptn;
1169 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
1170 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1174new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1178 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1183 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
1185 add_mark_object(p, kw_args);
1186 add_mark_object(p, kw_rest_arg);
1190#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1192static VALUE heredoc_dedent(struct parser_params*,VALUE);
1195#define ID2VAL(id) (id)
1196#define TOKEN2VAL(t) ID2VAL(t)
1197#define KWD2EID(t, v) keyword_##t
1200set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
1202 body = remove_begin(body);
1203 reduce_nodes(p, &body);
1204 n->nd_defn = NEW_SCOPE(args, body, loc);
1206 nd_set_line(n->nd_defn, loc->end_pos.lineno);
1207 set_line_body(body, loc->beg_pos.lineno);
1212rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1213 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1215 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1216 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1217 loc.beg_pos = arg_loc->beg_pos;
1218 return NEW_RESCUE(arg, rescue, 0, &loc);
1224restore_defun(struct parser_params *p, NODE *name)
1226 YYSTYPE c = {.val = name->nd_cval};
1227 p->cur_arg = name->nd_vid;
1228 p->ctxt.in_def = c.ctxt.in_def;
1229 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
1233endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
1236 defn = defn->nd_defn;
1238 ID mid = defn->nd_mid;
1239 if (is_attrset_id(mid)) {
1240 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1242 token_info_drop(p, "def", loc->beg_pos);
1245#define debug_token_line(p, name, line) if (p->debug) rb_parser_printf(p, name ":%d (%d: %ld|%ld|%ld)\n", line, p->ruby_sourceline, p->lex.ptok - p->lex.pbeg, p->lex.pcur - p->lex.ptok, p->lex.pend - p->lex.pcur)
1250# define ifndef_ripper(x) (x)
1253# define Qnull Qundef
1254# define ifndef_ripper(x)
1257# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1258# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1259# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1260# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1261# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1262# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1263# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1264# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1265# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1266# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1267# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1268# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1269# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1270# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1271# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1272# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1273# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1274# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1275# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1276# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1278static ID id_warn, id_warning, id_gets, id_assoc;
1279# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1280# define WARN_S_L(s,l) STR_NEW(s,l)
1281# define WARN_S(s) STR_NEW2(s)
1282# define WARN_I(i) INT2NUM(i)
1283# define WARN_ID(i) rb_id2str(i)
1284# define WARN_IVAL(i) i
1285# define PRIsWARN "s"
1286# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1287# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1288# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1289# ifdef HAVE_VA_ARGS_MACRO
1290# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1292# define WARN_CALL rb_funcall
1294# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1295# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1296# ifdef HAVE_VA_ARGS_MACRO
1297# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1299# define WARNING_CALL rb_funcall
1301PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1302# define compile_error ripper_compile_error
1304# define WARN_S_L(s,l) s
1307# define WARN_ID(i) rb_id2name(i)
1308# define WARN_IVAL(i) NUM2INT(i)
1309# define PRIsWARN PRIsVALUE
1310# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1311# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1312# define WARN_CALL rb_compile_warn
1313# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1314# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1315# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1316# define WARNING_CALL rb_compile_warning
1317PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1318# define compile_error parser_compile_error
1321#define WARN_EOL(tok) \
1322 (looking_at_eol_p(p) ? \
1323 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1325static int looking_at_eol_p(struct parser_params *p);
1330%define parse.error verbose
1334 rb_parser_printf(p, "%s", ruby_node_name(nd_type($$)));
1341 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1343 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1345} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1348 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1350 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1352} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1355 rb_parser_printf(p, "$%ld", $$->nd_nth);
1357 rb_parser_printf(p, "%"PRIsVALUE, $$);
1362 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1364 rb_parser_printf(p, "%"PRIsVALUE, $$);
1368%lex-param {struct parser_params *p}
1369%parse-param {struct parser_params *p}
1372 RUBY_SET_YYLLOC_OF_NONE(@$);
1381 const struct vtable *vars;
1382 struct rb_strterm_struct *strterm;
1383 struct lex_context ctxt;
1387 keyword_class "`class'"
1388 keyword_module "`module'"
1390 keyword_undef "`undef'"
1391 keyword_begin "`begin'"
1392 keyword_rescue "`rescue'"
1393 keyword_ensure "`ensure'"
1396 keyword_unless "`unless'"
1397 keyword_then "`then'"
1398 keyword_elsif "`elsif'"
1399 keyword_else "`else'"
1400 keyword_case "`case'"
1401 keyword_when "`when'"
1402 keyword_while "`while'"
1403 keyword_until "`until'"
1405 keyword_break "`break'"
1406 keyword_next "`next'"
1407 keyword_redo "`redo'"
1408 keyword_retry "`retry'"
1411 keyword_do_cond "`do' for condition"
1412 keyword_do_block "`do' for block"
1413 keyword_do_LAMBDA "`do' for lambda"
1414 keyword_return "`return'"
1415 keyword_yield "`yield'"
1416 keyword_super "`super'"
1417 keyword_self "`self'"
1419 keyword_true "`true'"
1420 keyword_false "`false'"
1424 modifier_if "`if' modifier"
1425 modifier_unless "`unless' modifier"
1426 modifier_while "`while' modifier"
1427 modifier_until "`until' modifier"
1428 modifier_rescue "`rescue' modifier"
1429 keyword_alias "`alias'"
1430 keyword_defined "`defined?'"
1431 keyword_BEGIN "`BEGIN'"
1433 keyword__LINE__ "`__LINE__'"
1434 keyword__FILE__ "`__FILE__'"
1435 keyword__ENCODING__ "`__ENCODING__'"
1437%token <id> tIDENTIFIER "local variable or method"
1438%token <id> tFID "method"
1439%token <id> tGVAR "global variable"
1440%token <id> tIVAR "instance variable"
1441%token <id> tCONSTANT "constant"
1442%token <id> tCVAR "class variable"
1443%token <id> tLABEL "label"
1444%token <node> tINTEGER "integer literal"
1445%token <node> tFLOAT "float literal"
1446%token <node> tRATIONAL "rational literal"
1447%token <node> tIMAGINARY "imaginary literal"
1448%token <node> tCHAR "char literal"
1449%token <node> tNTH_REF "numbered reference"
1450%token <node> tBACK_REF "back reference"
1451%token <node> tSTRING_CONTENT "literal content"
1452%token <num> tREGEXP_END
1453%token <num> tDUMNY_END "dummy end"
1455%type <node> singleton strings string string1 xstring regexp
1456%type <node> string_contents xstring_contents regexp_contents string_content
1457%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1458%type <node> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1459%type <node> top_compstmt top_stmts top_stmt begin_block
1460%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1461%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1462%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1463%type <node> args call_args opt_call_args
1464%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1465%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1466%type <node> command_rhs arg_rhs
1467%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1468%type <node> f_block_optarg f_block_opt
1469%type <node> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1470%type <node> f_optarg f_marg f_marg_list f_margs f_rest_marg
1471%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1472%type <node> block_param opt_block_param block_param_def f_opt
1473%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1474%type <node> bv_decls opt_bv_decl bvar
1475%type <node> lambda f_larglist lambda_body brace_body do_body
1476%type <node> brace_block cmd_brace_block do_block lhs none fitem
1477%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1478%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1479%type <node> p_expr p_as p_alt p_expr_basic p_find
1480%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1481%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
1482%type <node> p_kwargs p_kwarg p_kw
1483%type <id> keyword_variable user_variable sym operation operation2 operation3
1484%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1485%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1486%type <id> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1487%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var
1488 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1489%token END_OF_INPUT 0 "end-of-input"
1491/* escaped chars, should be ignored otherwise */
1492%token <id> '\\' "backslash"
1493%token tSP "escaped space"
1494%token <id> '\t' "escaped horizontal tab"
1495%token <id> '\f' "escaped form feed"
1496%token <id> '\r' "escaped carriage return"
1497%token <id> '\13' "escaped vertical tab"
1498%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1499%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1500%token tPOW RUBY_TOKEN(POW) "**"
1501%token tCMP RUBY_TOKEN(CMP) "<=>"
1502%token tEQ RUBY_TOKEN(EQ) "=="
1503%token tEQQ RUBY_TOKEN(EQQ) "==="
1504%token tNEQ RUBY_TOKEN(NEQ) "!="
1505%token tGEQ RUBY_TOKEN(GEQ) ">="
1506%token tLEQ RUBY_TOKEN(LEQ) "<="
1507%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1508%token tOROP RUBY_TOKEN(OROP) "||"
1509%token tMATCH RUBY_TOKEN(MATCH) "=~"
1510%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1511%token tDOT2 RUBY_TOKEN(DOT2) ".."
1512%token tDOT3 RUBY_TOKEN(DOT3) "..."
1513%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1514%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1515%token tAREF RUBY_TOKEN(AREF) "[]"
1516%token tASET RUBY_TOKEN(ASET) "[]="
1517%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1518%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1519%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1520%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1521%token tCOLON3 ":: at EXPR_BEG"
1522%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1525%token tLPAREN_ARG "( arg"
1529%token tLBRACE_ARG "{ arg"
1531%token tDSTAR "**arg"
1534%token tSYMBEG "symbol literal"
1535%token tSTRING_BEG "string literal"
1536%token tXSTRING_BEG "backtick literal"
1537%token tREGEXP_BEG "regexp literal"
1538%token tWORDS_BEG "word list"
1539%token tQWORDS_BEG "verbatim word list"
1540%token tSYMBOLS_BEG "symbol list"
1541%token tQSYMBOLS_BEG "verbatim symbol list"
1542%token tSTRING_END "terminator"
1543%token tSTRING_DEND "'}'"
1544%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1546%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
1547%token tHEREDOC_BEG tHEREDOC_END k__END__
1554%nonassoc tLBRACE_ARG
1556%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1557%left keyword_or keyword_and
1559%nonassoc keyword_defined
1561%left modifier_rescue
1563%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1566%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1567%left '>' tGEQ '<' tLEQ
1573%right tUMINUS_NUM tUMINUS
1575%right '!' '~' tUPLUS
1581 SET_LEX_STATE(EXPR_BEG);
1582 local_push(p, ifndef_ripper(1)+0);
1587 if ($2 && !compile_for_eval) {
1589 /* last expression should not be void */
1590 if (nd_type_p(node, NODE_BLOCK)) {
1591 while (node->nd_next) {
1592 node = node->nd_next;
1594 node = node->nd_head;
1596 node = remove_begin(node);
1599 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1601 /*% ripper[final]: program!($2) %*/
1606top_compstmt : top_stmts opt_terms
1608 $$ = void_stmts(p, $1);
1615 $$ = NEW_BEGIN(0, &@$);
1617 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1622 $$ = newline_node($1);
1624 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1626 | top_stmts terms top_stmt
1629 $$ = block_append(p, $1, newline_node($3));
1631 /*% ripper: stmts_add!($1, $3) %*/
1636 | keyword_BEGIN begin_block
1642begin_block : '{' top_compstmt '}'
1645 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1646 NEW_BEGIN($2, &@$));
1647 $$ = NEW_BEGIN(0, &@$);
1649 /*% ripper: BEGIN!($2) %*/
1655 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1660 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1662 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1669 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1671 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1675compstmt : stmts opt_terms
1677 $$ = void_stmts(p, $1);
1684 $$ = NEW_BEGIN(0, &@$);
1686 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1691 $$ = newline_node($1);
1693 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1695 | stmts terms stmt_or_begin
1698 $$ = block_append(p, $1, newline_node($3));
1700 /*% ripper: stmts_add!($1, $3) %*/
1710 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1718stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1721 $$ = NEW_ALIAS($2, $4, &@$);
1723 /*% ripper: alias!($2, $4) %*/
1725 | keyword_alias tGVAR tGVAR
1728 $$ = NEW_VALIAS($2, $3, &@$);
1730 /*% ripper: var_alias!($2, $3) %*/
1732 | keyword_alias tGVAR tBACK_REF
1737 buf[1] = (char)$3->nd_nth;
1738 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1740 /*% ripper: var_alias!($2, $3) %*/
1742 | keyword_alias tGVAR tNTH_REF
1744 static const char mesg[] = "can't make alias for the number variables";
1746 yyerror1(&@3, mesg);
1747 $$ = NEW_BEGIN(0, &@$);
1749 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1751 | keyword_undef undef_list
1756 /*% ripper: undef!($2) %*/
1758 | stmt modifier_if expr_value
1761 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1764 /*% ripper: if_mod!($3, $1) %*/
1766 | stmt modifier_unless expr_value
1769 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1772 /*% ripper: unless_mod!($3, $1) %*/
1774 | stmt modifier_while expr_value
1777 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1778 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1781 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1784 /*% ripper: while_mod!($3, $1) %*/
1786 | stmt modifier_until expr_value
1789 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1790 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1793 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1796 /*% ripper: until_mod!($3, $1) %*/
1798 | stmt modifier_rescue stmt
1802 YYLTYPE loc = code_loc_gen(&@2, &@3);
1803 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1804 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1806 /*% ripper: rescue_mod!($1, $3) %*/
1808 | keyword_END '{' compstmt '}'
1810 if (p->ctxt.in_def) {
1811 rb_warn0("END in method; use at_exit");
1815 NODE *scope = NEW_NODE(
1816 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1817 $$ = NEW_POSTEXE(scope, &@$);
1820 /*% ripper: END!($3) %*/
1823 | mlhs '=' lex_ctxt command_call
1827 $$ = node_assign(p, $1, $4, $3, &@$);
1829 /*% ripper: massign!($1, $4) %*/
1831 | lhs '=' lex_ctxt mrhs
1834 $$ = node_assign(p, $1, $4, $3, &@$);
1836 /*% ripper: assign!($1, $4) %*/
1838 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1841 YYLTYPE loc = code_loc_gen(&@5, &@6);
1842 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1844 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1846 | mlhs '=' lex_ctxt mrhs_arg
1849 $$ = node_assign(p, $1, $4, $3, &@$);
1851 /*% ripper: massign!($1, $4) %*/
1857 $$ = NEW_ERROR(&@$);
1862command_asgn : lhs '=' lex_ctxt command_rhs
1865 $$ = node_assign(p, $1, $4, $3, &@$);
1867 /*% ripper: assign!($1, $4) %*/
1869 | var_lhs tOP_ASGN lex_ctxt command_rhs
1872 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1874 /*% ripper: opassign!($1, $2, $4) %*/
1876 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1879 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1881 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1884 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1887 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1889 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1891 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1894 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1896 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1898 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1901 YYLTYPE loc = code_loc_gen(&@1, &@3);
1902 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1904 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1906 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1909 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1911 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1913 | defn_head f_opt_paren_args '=' command
1915 endless_method_name(p, $<node>1, &@1);
1916 restore_defun(p, $<node>1->nd_defn);
1918 $$ = set_defun_body(p, $1, $2, $4, &@$);
1920 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1921 /*% ripper: def!(get_value($1), $2, $4) %*/
1924 | defn_head f_opt_paren_args '=' command modifier_rescue arg
1926 endless_method_name(p, $<node>1, &@1);
1927 restore_defun(p, $<node>1->nd_defn);
1929 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1930 $$ = set_defun_body(p, $1, $2, $4, &@$);
1932 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1933 /*% ripper: def!(get_value($1), $2, $4) %*/
1936 | defs_head f_opt_paren_args '=' command
1938 endless_method_name(p, $<node>1, &@1);
1939 restore_defun(p, $<node>1->nd_defn);
1941 $$ = set_defun_body(p, $1, $2, $4, &@$);
1945 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1946 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1949 | defs_head f_opt_paren_args '=' command modifier_rescue arg
1951 endless_method_name(p, $<node>1, &@1);
1952 restore_defun(p, $<node>1->nd_defn);
1954 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1955 $$ = set_defun_body(p, $1, $2, $4, &@$);
1959 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1960 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1963 | backref tOP_ASGN lex_ctxt command_rhs
1966 rb_backref_error(p, $1);
1967 $$ = NEW_BEGIN(0, &@$);
1969 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1973command_rhs : command_call %prec tOP_ASGN
1978 | command_call modifier_rescue stmt
1981 YYLTYPE loc = code_loc_gen(&@2, &@3);
1983 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1985 /*% ripper: rescue_mod!($1, $3) %*/
1991 | expr keyword_and expr
1993 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1995 | expr keyword_or expr
1997 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1999 | keyword_not opt_nl expr
2001 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2005 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2010 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2011 p->command_start = FALSE;
2013 p->ctxt.in_kwarg = 1;
2014 $<tbl>$ = push_pvtbl(p);
2017 $<tbl>$ = push_pktbl(p);
2021 pop_pktbl(p, $<tbl>4);
2022 pop_pvtbl(p, $<tbl>3);
2023 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2025 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
2027 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2032 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2033 p->command_start = FALSE;
2035 p->ctxt.in_kwarg = 1;
2036 $<tbl>$ = push_pvtbl(p);
2039 $<tbl>$ = push_pktbl(p);
2043 pop_pktbl(p, $<tbl>4);
2044 pop_pvtbl(p, $<tbl>3);
2045 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2047 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
2049 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2051 | arg %prec tLBRACE_ARG
2056 ID fname = get_id($1);
2057 ID cur_arg = p->cur_arg;
2058 YYSTYPE c = {.ctxt = p->ctxt};
2059 numparam_name(p, fname);
2063 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*cval*/c.val, &@$);
2066 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
2071defn_head : k_def def_name
2075 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
2080defs_head : k_def singleton dot_or_colon
2082 SET_LEX_STATE(EXPR_FNAME);
2083 p->ctxt.in_argdef = 1;
2087 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2090 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
2092 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
2093 add_mark_object(p, ary);
2094 $<node>$->nd_rval = ary;
2107 $$ = NEW_ERROR(&@$);
2112expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2118command_call : command
2122block_command : block_call
2123 | block_call call_op2 operation2 command_args
2126 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2128 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2132cmd_brace_block : tLBRACE_ARG brace_body '}'
2136 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
2137 nd_set_line($$, @1.end_pos.lineno);
2145 $$ = NEW_FCALL($1, 0, &@$);
2146 nd_set_line($$, p->tokline);
2152command : fcall command_args %prec tLOWEST
2156 nd_set_last_loc($1, @2.end_pos);
2159 /*% ripper: command!($1, $2) %*/
2161 | fcall command_args cmd_brace_block
2164 block_dup_check(p, $2, $3);
2166 $$ = method_add_block(p, $1, $3, &@$);
2168 nd_set_last_loc($1, @2.end_pos);
2170 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2172 | primary_value call_op operation2 command_args %prec tLOWEST
2175 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2177 /*% ripper: command_call!($1, $2, $3, $4) %*/
2179 | primary_value call_op operation2 command_args cmd_brace_block
2182 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2184 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2186 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2189 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2191 /*% ripper: command_call!($1, $2, $3, $4) %*/
2193 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2196 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2198 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2200 | keyword_super command_args
2203 $$ = NEW_SUPER($2, &@$);
2206 /*% ripper: super!($2) %*/
2208 | keyword_yield command_args
2211 $$ = new_yield(p, $2, &@$);
2214 /*% ripper: yield!($2) %*/
2216 | k_return call_args
2219 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2221 /*% ripper: return!($2) %*/
2223 | keyword_break call_args
2226 $$ = NEW_BREAK(ret_args(p, $2), &@$);
2228 /*% ripper: break!($2) %*/
2230 | keyword_next call_args
2233 $$ = NEW_NEXT(ret_args(p, $2), &@$);
2235 /*% ripper: next!($2) %*/
2240 | tLPAREN mlhs_inner rparen
2245 /*% ripper: mlhs_paren!($2) %*/
2249mlhs_inner : mlhs_basic
2250 | tLPAREN mlhs_inner rparen
2253 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
2255 /*% ripper: mlhs_paren!($2) %*/
2259mlhs_basic : mlhs_head
2262 $$ = NEW_MASGN($1, 0, &@$);
2266 | mlhs_head mlhs_item
2269 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2271 /*% ripper: mlhs_add!($1, $2) %*/
2273 | mlhs_head tSTAR mlhs_node
2276 $$ = NEW_MASGN($1, $3, &@$);
2278 /*% ripper: mlhs_add_star!($1, $3) %*/
2280 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2283 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2285 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2290 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2292 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2294 | mlhs_head tSTAR ',' mlhs_post
2297 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2299 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2304 $$ = NEW_MASGN(0, $2, &@$);
2306 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2308 | tSTAR mlhs_node ',' mlhs_post
2311 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2313 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2318 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2320 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2322 | tSTAR ',' mlhs_post
2325 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2327 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2331mlhs_item : mlhs_node
2332 | tLPAREN mlhs_inner rparen
2337 /*% ripper: mlhs_paren!($2) %*/
2341mlhs_head : mlhs_item ','
2344 $$ = NEW_LIST($1, &@1);
2346 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2348 | mlhs_head mlhs_item ','
2351 $$ = list_append(p, $1, $2);
2353 /*% ripper: mlhs_add!($1, $2) %*/
2357mlhs_post : mlhs_item
2360 $$ = NEW_LIST($1, &@$);
2362 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2364 | mlhs_post ',' mlhs_item
2367 $$ = list_append(p, $1, $3);
2369 /*% ripper: mlhs_add!($1, $3) %*/
2373mlhs_node : user_variable
2376 $$ = assignable(p, $1, 0, &@$);
2378 /*% ripper: assignable(p, var_field(p, $1)) %*/
2383 $$ = assignable(p, $1, 0, &@$);
2385 /*% ripper: assignable(p, var_field(p, $1)) %*/
2387 | primary_value '[' opt_call_args rbracket
2390 $$ = aryset(p, $1, $3, &@$);
2392 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2394 | primary_value call_op tIDENTIFIER
2396 if ($2 == tANDDOT) {
2397 yyerror1(&@2, "&. inside multiple assignment destination");
2400 $$ = attrset(p, $1, $2, $3, &@$);
2402 /*% ripper: field!($1, $2, $3) %*/
2404 | primary_value tCOLON2 tIDENTIFIER
2407 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2409 /*% ripper: const_path_field!($1, $3) %*/
2411 | primary_value call_op tCONSTANT
2413 if ($2 == tANDDOT) {
2414 yyerror1(&@2, "&. inside multiple assignment destination");
2417 $$ = attrset(p, $1, $2, $3, &@$);
2419 /*% ripper: field!($1, $2, $3) %*/
2421 | primary_value tCOLON2 tCONSTANT
2424 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2426 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2431 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2433 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2438 rb_backref_error(p, $1);
2439 $$ = NEW_BEGIN(0, &@$);
2441 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2448 $$ = assignable(p, $1, 0, &@$);
2450 /*% ripper: assignable(p, var_field(p, $1)) %*/
2455 $$ = assignable(p, $1, 0, &@$);
2457 /*% ripper: assignable(p, var_field(p, $1)) %*/
2459 | primary_value '[' opt_call_args rbracket
2462 $$ = aryset(p, $1, $3, &@$);
2464 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2466 | primary_value call_op tIDENTIFIER
2469 $$ = attrset(p, $1, $2, $3, &@$);
2471 /*% ripper: field!($1, $2, $3) %*/
2473 | primary_value tCOLON2 tIDENTIFIER
2476 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2478 /*% ripper: field!($1, $2, $3) %*/
2480 | primary_value call_op tCONSTANT
2483 $$ = attrset(p, $1, $2, $3, &@$);
2485 /*% ripper: field!($1, $2, $3) %*/
2487 | primary_value tCOLON2 tCONSTANT
2490 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2492 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2497 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2499 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2504 rb_backref_error(p, $1);
2505 $$ = NEW_BEGIN(0, &@$);
2507 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2513 static const char mesg[] = "class/module name must be CONSTANT";
2515 yyerror1(&@1, mesg);
2517 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2522cpath : tCOLON3 cname
2525 $$ = NEW_COLON3($2, &@$);
2527 /*% ripper: top_const_ref!($2) %*/
2532 $$ = NEW_COLON2(0, $$, &@$);
2534 /*% ripper: const_ref!($1) %*/
2536 | primary_value tCOLON2 cname
2539 $$ = NEW_COLON2($1, $3, &@$);
2541 /*% ripper: const_path_ref!($1, $3) %*/
2550 SET_LEX_STATE(EXPR_ENDFN);
2559 $$ = NEW_LIT(ID2SYM($1), &@$);
2561 /*% ripper: symbol_literal!($1) %*/
2569 $$ = NEW_UNDEF($1, &@$);
2571 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2573 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2576 NODE *undef = NEW_UNDEF($4, &@4);
2577 $$ = block_append(p, $1, undef);
2579 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2583op : '|' { ifndef_ripper($$ = '|'); }
2584 | '^' { ifndef_ripper($$ = '^'); }
2585 | '&' { ifndef_ripper($$ = '&'); }
2586 | tCMP { ifndef_ripper($$ = tCMP); }
2587 | tEQ { ifndef_ripper($$ = tEQ); }
2588 | tEQQ { ifndef_ripper($$ = tEQQ); }
2589 | tMATCH { ifndef_ripper($$ = tMATCH); }
2590 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2591 | '>' { ifndef_ripper($$ = '>'); }
2592 | tGEQ { ifndef_ripper($$ = tGEQ); }
2593 | '<' { ifndef_ripper($$ = '<'); }
2594 | tLEQ { ifndef_ripper($$ = tLEQ); }
2595 | tNEQ { ifndef_ripper($$ = tNEQ); }
2596 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2597 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2598 | '+' { ifndef_ripper($$ = '+'); }
2599 | '-' { ifndef_ripper($$ = '-'); }
2600 | '*' { ifndef_ripper($$ = '*'); }
2601 | tSTAR { ifndef_ripper($$ = '*'); }
2602 | '/' { ifndef_ripper($$ = '/'); }
2603 | '%' { ifndef_ripper($$ = '%'); }
2604 | tPOW { ifndef_ripper($$ = tPOW); }
2605 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2606 | '!' { ifndef_ripper($$ = '!'); }
2607 | '~' { ifndef_ripper($$ = '~'); }
2608 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2609 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2610 | tAREF { ifndef_ripper($$ = tAREF); }
2611 | tASET { ifndef_ripper($$ = tASET); }
2612 | '`' { ifndef_ripper($$ = '`'); }
2615reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2616 | keyword_BEGIN | keyword_END
2617 | keyword_alias | keyword_and | keyword_begin
2618 | keyword_break | keyword_case | keyword_class | keyword_def
2619 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2620 | keyword_end | keyword_ensure | keyword_false
2621 | keyword_for | keyword_in | keyword_module | keyword_next
2622 | keyword_nil | keyword_not | keyword_or | keyword_redo
2623 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2624 | keyword_super | keyword_then | keyword_true | keyword_undef
2625 | keyword_when | keyword_yield | keyword_if | keyword_unless
2626 | keyword_while | keyword_until
2629arg : lhs '=' lex_ctxt arg_rhs
2632 $$ = node_assign(p, $1, $4, $3, &@$);
2634 /*% ripper: assign!($1, $4) %*/
2636 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2639 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2641 /*% ripper: opassign!($1, $2, $4) %*/
2643 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2646 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2648 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2650 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2653 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2655 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2657 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2660 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2662 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2664 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2667 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2669 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2671 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2674 YYLTYPE loc = code_loc_gen(&@1, &@3);
2675 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2677 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2679 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2682 YYLTYPE loc = code_loc_gen(&@1, &@2);
2683 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2685 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2687 | backref tOP_ASGN lex_ctxt arg_rhs
2690 rb_backref_error(p, $1);
2691 $$ = NEW_BEGIN(0, &@$);
2693 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2700 $$ = NEW_DOT2($1, $3, &@$);
2702 /*% ripper: dot2!($1, $3) %*/
2709 $$ = NEW_DOT3($1, $3, &@$);
2711 /*% ripper: dot3!($1, $3) %*/
2717 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2719 /*% ripper: dot2!($1, Qnil) %*/
2725 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2727 /*% ripper: dot3!($1, Qnil) %*/
2733 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2735 /*% ripper: dot2!(Qnil, $2) %*/
2741 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2743 /*% ripper: dot3!(Qnil, $2) %*/
2747 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2751 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2755 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2759 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2763 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2767 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2769 | tUMINUS_NUM simple_numeric tPOW arg
2771 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2775 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2779 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2783 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2787 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2791 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2795 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2797 | rel_expr %prec tCMP
2800 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2804 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2808 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2812 $$ = match_op(p, $1, $3, &@2, &@$);
2816 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2820 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2824 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2828 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2832 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2836 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2840 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2842 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2844 p->ctxt.in_defined = 0;
2845 $$ = new_defined(p, $4, &@$);
2847 | arg '?' arg opt_nl ':' arg
2851 $$ = new_if(p, $1, $3, $6, &@$);
2854 /*% ripper: ifop!($1, $3, $6) %*/
2856 | defn_head f_opt_paren_args '=' arg
2858 endless_method_name(p, $<node>1, &@1);
2859 restore_defun(p, $<node>1->nd_defn);
2861 $$ = set_defun_body(p, $1, $2, $4, &@$);
2863 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2864 /*% ripper: def!(get_value($1), $2, $4) %*/
2867 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2869 endless_method_name(p, $<node>1, &@1);
2870 restore_defun(p, $<node>1->nd_defn);
2872 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2873 $$ = set_defun_body(p, $1, $2, $4, &@$);
2875 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2876 /*% ripper: def!(get_value($1), $2, $4) %*/
2879 | defs_head f_opt_paren_args '=' arg
2881 endless_method_name(p, $<node>1, &@1);
2882 restore_defun(p, $<node>1->nd_defn);
2884 $$ = set_defun_body(p, $1, $2, $4, &@$);
2888 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2889 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2892 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2894 endless_method_name(p, $<node>1, &@1);
2895 restore_defun(p, $<node>1->nd_defn);
2897 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2898 $$ = set_defun_body(p, $1, $2, $4, &@$);
2902 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2903 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2912relop : '>' {$$ = '>';}
2918rel_expr : arg relop arg %prec '>'
2920 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2922 | rel_expr relop arg %prec '>'
2924 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2925 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2947 | args ',' assocs trailer
2950 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2952 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2957 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2959 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2963arg_rhs : arg %prec tOP_ASGN
2968 | arg modifier_rescue arg
2972 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2974 /*% ripper: rescue_mod!($1, $3) %*/
2978paren_args : '(' opt_call_args rparen
2983 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2985 | '(' args ',' args_forward rparen
2987 if (!check_forwarding_args(p)) {
2992 $$ = new_args_forward_call(p, $2, &@4, &@$);
2994 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
2997 | '(' args_forward rparen
2999 if (!check_forwarding_args(p)) {
3004 $$ = new_args_forward_call(p, 0, &@2, &@$);
3006 /*% ripper: arg_paren!($2) %*/
3011opt_paren_args : none
3021 | args ',' assocs ','
3024 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3026 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3031 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3033 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3041 $$ = NEW_LIST($1, &@$);
3043 /*% ripper: args_add!(args_new!, $1) %*/
3045 | args opt_block_arg
3048 $$ = arg_blk_pass($1, $2);
3050 /*% ripper: args_add_block!($1, $2) %*/
3052 | assocs opt_block_arg
3055 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3056 $$ = arg_blk_pass($$, $2);
3058 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3060 | args ',' assocs opt_block_arg
3063 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3064 $$ = arg_blk_pass($$, $4);
3066 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3069 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3073 /* If call_args starts with a open paren '(' or '[',
3074 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3075 * but the push must be done after CMDARG_PUSH(1).
3076 * So this code makes them consistent by first cancelling
3077 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3078 * and finally redoing CMDARG_PUSH(0).
3082 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3085 if (lookahead) CMDARG_POP();
3087 if (lookahead) CMDARG_PUSH(0);
3091 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3092 * but the push must be done after CMDARG_POP() in the parser.
3093 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3094 * CMDARG_POP() to pop 1 pushed by command_args,
3095 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3102 if (lookahead) CMDARG_POP();
3104 if (lookahead) CMDARG_PUSH(0);
3109block_arg : tAMPER arg_value
3112 $$ = NEW_BLOCK_PASS($2, &@$);
3118 if (!local_id(p, idFWD_BLOCK)) {
3119 compile_error(p, "no anonymous block parameter");
3122 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3124 /*% ripper: Qnil %*/
3128opt_block_arg : ',' block_arg
3142 $$ = NEW_LIST($1, &@$);
3144 /*% ripper: args_add!(args_new!, $1) %*/
3149 $$ = NEW_SPLAT($2, &@$);
3151 /*% ripper: args_add_star!(args_new!, $2) %*/
3155 if (!local_id(p, idFWD_REST) ||
3156 local_id(p, idFWD_ALL)) {
3157 compile_error(p, "no anonymous rest parameter");
3160 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@1), &@$);
3162 /*% ripper: args_add_star!(args_new!, Qnil) %*/
3164 | args ',' arg_value
3167 $$ = last_arg_append(p, $1, $3, &@$);
3169 /*% ripper: args_add!($1, $3) %*/
3171 | args ',' tSTAR arg_value
3174 $$ = rest_arg_append(p, $1, $4, &@$);
3176 /*% ripper: args_add_star!($1, $4) %*/
3180 if (!local_id(p, idFWD_REST) ||
3181 local_id(p, idFWD_ALL)) {
3182 compile_error(p, "no anonymous rest parameter");
3185 $$ = rest_arg_append(p, $1, NEW_LVAR(idFWD_REST, &@3), &@$);
3187 /*% ripper: args_add_star!($1, Qnil) %*/
3197mrhs : args ',' arg_value
3200 $$ = last_arg_append(p, $1, $3, &@$);
3202 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3204 | args ',' tSTAR arg_value
3207 $$ = rest_arg_append(p, $1, $4, &@$);
3209 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3214 $$ = NEW_SPLAT($2, &@$);
3216 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3233 $$ = NEW_FCALL($1, 0, &@$);
3235 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3246 set_line_body($3, @1.end_pos.lineno);
3247 $$ = NEW_BEGIN($3, &@$);
3248 nd_set_line($$, @1.end_pos.lineno);
3250 /*% ripper: begin!($3) %*/
3252 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
3255 $$ = NEW_BEGIN(0, &@$);
3257 /*% ripper: paren!(0) %*/
3259 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
3262 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3265 /*% ripper: paren!($2) %*/
3267 | tLPAREN compstmt ')'
3270 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3273 /*% ripper: paren!($2) %*/
3275 | primary_value tCOLON2 tCONSTANT
3278 $$ = NEW_COLON2($1, $3, &@$);
3280 /*% ripper: const_path_ref!($1, $3) %*/
3285 $$ = NEW_COLON3($2, &@$);
3287 /*% ripper: top_const_ref!($2) %*/
3289 | tLBRACK aref_args ']'
3292 $$ = make_list($2, &@$);
3294 /*% ripper: array!(escape_Qundef($2)) %*/
3296 | tLBRACE assoc_list '}'
3299 $$ = new_hash(p, $2, &@$);
3300 $$->nd_brace = TRUE;
3302 /*% ripper: hash!(escape_Qundef($2)) %*/
3307 $$ = NEW_RETURN(0, &@$);
3309 /*% ripper: return0! %*/
3311 | keyword_yield '(' call_args rparen
3314 $$ = new_yield(p, $3, &@$);
3316 /*% ripper: yield!(paren!($3)) %*/
3318 | keyword_yield '(' rparen
3321 $$ = NEW_YIELD(0, &@$);
3323 /*% ripper: yield!(paren!(args_new!)) %*/
3328 $$ = NEW_YIELD(0, &@$);
3330 /*% ripper: yield0! %*/
3332 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3334 p->ctxt.in_defined = 0;
3335 $$ = new_defined(p, $5, &@$);
3337 | keyword_not '(' expr rparen
3339 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3341 | keyword_not '(' rparen
3343 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3348 $$ = method_add_block(p, $1, $2, &@$);
3350 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3353 | method_call brace_block
3356 block_dup_check(p, $1->nd_args, $2);
3357 $$ = method_add_block(p, $1, $2, &@$);
3359 /*% ripper: method_add_block!($1, $2) %*/
3362 | k_if expr_value then
3368 $$ = new_if(p, $2, $4, $5, &@$);
3371 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3373 | k_unless expr_value then
3379 $$ = new_unless(p, $2, $4, $5, &@$);
3382 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3384 | k_while expr_value_do
3389 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3392 /*% ripper: while!($2, $3) %*/
3394 | k_until expr_value_do
3399 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3402 /*% ripper: until!($2, $3) %*/
3404 | k_case expr_value opt_terms
3406 $<val>$ = p->case_labels;
3407 p->case_labels = Qnil;
3412 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3413 p->case_labels = $<val>4;
3415 $$ = NEW_CASE($2, $5, &@$);
3418 /*% ripper: case!($2, $5) %*/
3422 $<val>$ = p->case_labels;
3428 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3429 p->case_labels = $<val>3;
3431 $$ = NEW_CASE2($4, &@$);
3433 /*% ripper: case!(Qnil, $4) %*/
3435 | k_case expr_value opt_terms
3440 $$ = NEW_CASE3($2, $4, &@$);
3442 /*% ripper: case!($2, $4) %*/
3444 | k_for for_var keyword_in expr_value_do
3452 * e.each{|*x| a, b, c = x}
3456 * e.each{|x| a, = x}
3458 ID id = internal_id(p);
3459 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3460 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3461 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3462 tbl->ids[0] = id; /* internal id */
3464 switch (nd_type($2)) {
3466 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3467 $2->nd_value = internal_var;
3472 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3473 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3475 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3476 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3478 /* {|*internal_id| <m> = internal_id; ... } */
3479 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3480 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3481 $$ = NEW_FOR($4, scope, &@$);
3484 /*% ripper: for!($2, $4, $5) %*/
3486 | k_class cpath superclass
3488 if (p->ctxt.in_def) {
3489 YYLTYPE loc = code_loc_gen(&@1, &@2);
3490 yyerror1(&loc, "class definition in method body");
3492 p->ctxt.in_class = 1;
3499 $$ = NEW_CLASS($2, $5, $3, &@$);
3500 nd_set_line($$->nd_body, @6.end_pos.lineno);
3501 set_line_body($5, @3.end_pos.lineno);
3502 nd_set_line($$, @3.end_pos.lineno);
3504 /*% ripper: class!($2, $3, $5) %*/
3506 p->ctxt.in_class = $<ctxt>1.in_class;
3507 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3509 | k_class tLSHFT expr
3512 p->ctxt.in_class = 0;
3520 $$ = NEW_SCLASS($3, $6, &@$);
3521 nd_set_line($$->nd_body, @7.end_pos.lineno);
3522 set_line_body($6, nd_line($3));
3525 /*% ripper: sclass!($3, $6) %*/
3527 p->ctxt.in_def = $<ctxt>1.in_def;
3528 p->ctxt.in_class = $<ctxt>1.in_class;
3529 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3533 if (p->ctxt.in_def) {
3534 YYLTYPE loc = code_loc_gen(&@1, &@2);
3535 yyerror1(&loc, "module definition in method body");
3537 p->ctxt.in_class = 1;
3544 $$ = NEW_MODULE($2, $4, &@$);
3545 nd_set_line($$->nd_body, @5.end_pos.lineno);
3546 set_line_body($4, @2.end_pos.lineno);
3547 nd_set_line($$, @2.end_pos.lineno);
3549 /*% ripper: module!($2, $4) %*/
3551 p->ctxt.in_class = $<ctxt>1.in_class;
3552 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3558 push_end_expect_token_locations(p, &@1.beg_pos);
3564 restore_defun(p, $<node>1->nd_defn);
3566 $$ = set_defun_body(p, $1, $2, $4, &@$);
3568 /*% ripper: def!(get_value($1), $2, $4) %*/
3575 push_end_expect_token_locations(p, &@1.beg_pos);
3581 restore_defun(p, $<node>1->nd_defn);
3583 $$ = set_defun_body(p, $1, $2, $4, &@$);
3587 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
3593 $$ = NEW_BREAK(0, &@$);
3595 /*% ripper: break!(args_new!) %*/
3600 $$ = NEW_NEXT(0, &@$);
3602 /*% ripper: next!(args_new!) %*/
3609 /*% ripper: redo! %*/
3614 $$ = NEW_RETRY(&@$);
3616 /*% ripper: retry! %*/
3620primary_value : primary
3627k_begin : keyword_begin
3629 token_info_push(p, "begin", &@$);
3631 push_end_expect_token_locations(p, &@1.beg_pos);
3639 token_info_push(p, "if", &@$);
3640 if (p->token_info && p->token_info->nonspc &&
3641 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3642 const char *tok = p->lex.ptok - rb_strlen_lit("if");
3643 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3644 beg += rb_strlen_lit("else");
3645 while (beg < tok && ISSPACE(*beg)) beg++;
3647 p->token_info->nonspc = 0;
3651 push_end_expect_token_locations(p, &@1.beg_pos);
3656k_unless : keyword_unless
3658 token_info_push(p, "unless", &@$);
3660 push_end_expect_token_locations(p, &@1.beg_pos);
3665k_while : keyword_while
3667 token_info_push(p, "while", &@$);
3669 push_end_expect_token_locations(p, &@1.beg_pos);
3674k_until : keyword_until
3676 token_info_push(p, "until", &@$);
3678 push_end_expect_token_locations(p, &@1.beg_pos);
3683k_case : keyword_case
3685 token_info_push(p, "case", &@$);
3687 push_end_expect_token_locations(p, &@1.beg_pos);
3694 token_info_push(p, "for", &@$);
3696 push_end_expect_token_locations(p, &@1.beg_pos);
3701k_class : keyword_class
3703 token_info_push(p, "class", &@$);
3706 push_end_expect_token_locations(p, &@1.beg_pos);
3711k_module : keyword_module
3713 token_info_push(p, "module", &@$);
3716 push_end_expect_token_locations(p, &@1.beg_pos);
3723 token_info_push(p, "def", &@$);
3724 p->ctxt.in_argdef = 1;
3730 token_info_push(p, "do", &@$);
3732 push_end_expect_token_locations(p, &@1.beg_pos);
3738k_do_block : keyword_do_block
3740 token_info_push(p, "do", &@$);
3742 push_end_expect_token_locations(p, &@1.beg_pos);
3747k_rescue : keyword_rescue
3749 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3753k_ensure : keyword_ensure
3755 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3759k_when : keyword_when
3761 token_info_warn(p, "when", p->token_info, 0, &@$);
3765k_else : keyword_else
3767 token_info *ptinfo_beg = p->token_info;
3768 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3769 token_info_warn(p, "else", p->token_info, same, &@$);
3772 e.next = ptinfo_beg->next;
3774 token_info_setup(&e, p->lex.pbeg, &@$);
3775 if (!e.nonspc) *ptinfo_beg = e;
3780k_elsif : keyword_elsif
3783 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3789 token_info_pop(p, "end", &@$);
3791 pop_end_expect_token_locations(p);
3796 compile_error(p, "syntax error, unexpected end-of-input");
3800k_return : keyword_return
3802 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3803 yyerror1(&@1, "Invalid return in class/module body");
3817 | k_elsif expr_value then
3822 $$ = new_if(p, $2, $4, $5, &@$);
3825 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3835 /*% ripper: else!($2) %*/
3846 $$ = assignable(p, $1, 0, &@$);
3847 mark_lvar_used(p, $$);
3849 /*% ripper: assignable(p, $1) %*/
3851 | tLPAREN f_margs rparen
3856 /*% ripper: mlhs_paren!($2) %*/
3863 $$ = NEW_LIST($1, &@$);
3865 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3867 | f_marg_list ',' f_marg
3870 $$ = list_append(p, $1, $3);
3872 /*% ripper: mlhs_add!($1, $3) %*/
3876f_margs : f_marg_list
3879 $$ = NEW_MASGN($1, 0, &@$);
3883 | f_marg_list ',' f_rest_marg
3886 $$ = NEW_MASGN($1, $3, &@$);
3888 /*% ripper: mlhs_add_star!($1, $3) %*/
3890 | f_marg_list ',' f_rest_marg ',' f_marg_list
3893 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3895 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3900 $$ = NEW_MASGN(0, $1, &@$);
3902 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3904 | f_rest_marg ',' f_marg_list
3907 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3909 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3913f_rest_marg : tSTAR f_norm_arg
3916 $$ = assignable(p, $2, 0, &@$);
3917 mark_lvar_used(p, $$);
3919 /*% ripper: assignable(p, $2) %*/
3924 $$ = NODE_SPECIAL_NO_NAME_REST;
3926 /*% ripper: Qnil %*/
3930f_any_kwrest : f_kwrest
3931 | f_no_kwarg {$$ = ID2VAL(idNil);}
3934f_eq : {p->ctxt.in_argdef = 0;} '=';
3936block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3938 $$ = new_args_tail(p, $1, $3, $4, &@3);
3940 | f_block_kwarg opt_f_block_arg
3942 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3944 | f_any_kwrest opt_f_block_arg
3946 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3950 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3954opt_block_args_tail : ',' block_args_tail
3960 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3966 /* magic number for rest_id in iseq_set_arguments() */
3968 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3970 /*% ripper: excessed_comma! %*/
3974block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3976 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3978 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3980 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3982 | f_arg ',' f_block_optarg opt_block_args_tail
3984 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3986 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3988 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3990 | f_arg ',' f_rest_arg opt_block_args_tail
3992 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3994 | f_arg excessed_comma
3996 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
3997 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
3999 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4001 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4003 | f_arg opt_block_args_tail
4005 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4007 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4009 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4011 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4013 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4015 | f_block_optarg opt_block_args_tail
4017 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4019 | f_block_optarg ',' f_arg opt_block_args_tail
4021 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4023 | f_rest_arg opt_block_args_tail
4025 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4027 | f_rest_arg ',' f_arg opt_block_args_tail
4029 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4033 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4037opt_block_param : none
4040 p->command_start = TRUE;
4044block_param_def : '|' opt_bv_decl '|'
4047 p->max_numparam = ORDINAL_PARAM;
4048 p->ctxt.in_argdef = 0;
4052 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
4054 | '|' block_param opt_bv_decl '|'
4057 p->max_numparam = ORDINAL_PARAM;
4058 p->ctxt.in_argdef = 0;
4062 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
4071 | opt_nl ';' bv_decls opt_nl
4081 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4083 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4088 new_bv(p, get_id($1));
4089 /*% ripper: get_value($1) %*/
4099 token_info_push(p, "->", &@1);
4100 $<vars>1 = dyna_push(p);
4101 $<num>$ = p->lex.lpar_beg;
4102 p->lex.lpar_beg = p->lex.paren_nest;
4105 $<num>$ = p->max_numparam;
4106 p->max_numparam = 0;
4109 $<node>$ = numparam_push(p);
4117 int max_numparam = p->max_numparam;
4118 p->lex.lpar_beg = $<num>2;
4119 p->max_numparam = $<num>3;
4121 $5 = args_with_numbered(p, $5, max_numparam);
4124 YYLTYPE loc = code_loc_gen(&@5, &@7);
4125 $$ = NEW_LAMBDA($5, $7, &loc);
4126 nd_set_line($$->nd_body, @7.end_pos.lineno);
4127 nd_set_line($$, @5.end_pos.lineno);
4128 nd_set_first_loc($$, @1.beg_pos);
4131 /*% ripper: lambda!($5, $7) %*/
4132 numparam_pop(p, $<node>4);
4133 dyna_pop(p, $<vars>1);
4137f_larglist : '(' f_args opt_bv_decl ')'
4139 p->ctxt.in_argdef = 0;
4142 p->max_numparam = ORDINAL_PARAM;
4144 /*% ripper: paren!($2) %*/
4148 p->ctxt.in_argdef = 0;
4150 if (!args_info_empty_p($1->nd_ainfo))
4151 p->max_numparam = ORDINAL_PARAM;
4157lambda_body : tLAMBEG compstmt '}'
4159 token_info_pop(p, "}", &@3);
4165 push_end_expect_token_locations(p, &@1.beg_pos);
4174do_block : k_do_block do_body k_end
4178 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4179 nd_set_line($$, @1.end_pos.lineno);
4184block_call : command do_block
4187 if (nd_type_p($1, NODE_YIELD)) {
4188 compile_error(p, "block given to yield");
4191 block_dup_check(p, $1->nd_args, $2);
4193 $$ = method_add_block(p, $1, $2, &@$);
4196 /*% ripper: method_add_block!($1, $2) %*/
4198 | block_call call_op2 operation2 opt_paren_args
4201 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4203 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4205 | block_call call_op2 operation2 opt_paren_args brace_block
4208 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4210 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4212 | block_call call_op2 operation2 command_args do_block
4215 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4217 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4221method_call : fcall paren_args
4226 nd_set_last_loc($1, @2.end_pos);
4228 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4230 | primary_value call_op operation2 opt_paren_args
4233 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4234 nd_set_line($$, @3.end_pos.lineno);
4236 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4238 | primary_value tCOLON2 operation2 paren_args
4241 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4242 nd_set_line($$, @3.end_pos.lineno);
4244 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4246 | primary_value tCOLON2 operation3
4249 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4251 /*% ripper: call!($1, $2, $3) %*/
4253 | primary_value call_op paren_args
4256 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4257 nd_set_line($$, @2.end_pos.lineno);
4259 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4261 | primary_value tCOLON2 paren_args
4264 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4265 nd_set_line($$, @2.end_pos.lineno);
4267 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4269 | keyword_super paren_args
4272 $$ = NEW_SUPER($2, &@$);
4274 /*% ripper: super!($2) %*/
4279 $$ = NEW_ZSUPER(&@$);
4281 /*% ripper: zsuper! %*/
4283 | primary_value '[' opt_call_args rbracket
4286 if ($1 && nd_type_p($1, NODE_SELF))
4287 $$ = NEW_FCALL(tAREF, $3, &@$);
4289 $$ = NEW_CALL($1, tAREF, $3, &@$);
4292 /*% ripper: aref!($1, escape_Qundef($3)) %*/
4296brace_block : '{' brace_body '}'
4300 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4301 nd_set_line($$, @1.end_pos.lineno);
4304 | k_do do_body k_end
4308 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4309 nd_set_line($$, @1.end_pos.lineno);
4314brace_body : {$<vars>$ = dyna_push(p);}
4316 $<num>$ = p->max_numparam;
4317 p->max_numparam = 0;
4320 $<node>$ = numparam_push(p);
4322 opt_block_param compstmt
4324 int max_numparam = p->max_numparam;
4325 p->max_numparam = $<num>2;
4326 $4 = args_with_numbered(p, $4, max_numparam);
4328 $$ = NEW_ITER($4, $5, &@$);
4330 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
4331 numparam_pop(p, $<node>3);
4332 dyna_pop(p, $<vars>1);
4336do_body : {$<vars>$ = dyna_push(p);}
4338 $<num>$ = p->max_numparam;
4339 p->max_numparam = 0;
4342 $<node>$ = numparam_push(p);
4345 opt_block_param bodystmt
4347 int max_numparam = p->max_numparam;
4348 p->max_numparam = $<num>2;
4349 $4 = args_with_numbered(p, $4, max_numparam);
4351 $$ = NEW_ITER($4, $5, &@$);
4353 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4355 numparam_pop(p, $<node>3);
4356 dyna_pop(p, $<vars>1);
4360case_args : arg_value
4363 check_literal_when(p, $1, &@1);
4364 $$ = NEW_LIST($1, &@$);
4366 /*% ripper: args_add!(args_new!, $1) %*/
4371 $$ = NEW_SPLAT($2, &@$);
4373 /*% ripper: args_add_star!(args_new!, $2) %*/
4375 | case_args ',' arg_value
4378 check_literal_when(p, $3, &@3);
4379 $$ = last_arg_append(p, $1, $3, &@$);
4381 /*% ripper: args_add!($1, $3) %*/
4383 | case_args ',' tSTAR arg_value
4386 $$ = rest_arg_append(p, $1, $4, &@$);
4388 /*% ripper: args_add_star!($1, $4) %*/
4392case_body : k_when case_args then
4397 $$ = NEW_WHEN($2, $4, $5, &@$);
4400 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4408p_case_body : keyword_in
4410 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4411 p->command_start = FALSE;
4413 p->ctxt.in_kwarg = 1;
4414 $<tbl>$ = push_pvtbl(p);
4417 $<tbl>$ = push_pktbl(p);
4421 pop_pktbl(p, $<tbl>3);
4422 pop_pvtbl(p, $<tbl>2);
4423 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4429 $$ = NEW_IN($4, $7, $8, &@$);
4431 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4439p_top_expr : p_top_expr_body
4440 | p_top_expr_body modifier_if expr_value
4443 $$ = new_if(p, $3, $1, 0, &@$);
4446 /*% ripper: if_mod!($3, $1) %*/
4448 | p_top_expr_body modifier_unless expr_value
4451 $$ = new_unless(p, $3, $1, 0, &@$);
4454 /*% ripper: unless_mod!($3, $1) %*/
4458p_top_expr_body : p_expr
4461 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4462 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4466 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4468 nd_set_first_loc($$, @1.beg_pos);
4474 $$ = new_find_pattern(p, Qnone, $1, &@$);
4478 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4482 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4489p_as : p_expr tASSOC p_variable
4492 NODE *n = NEW_LIST($1, &@$);
4493 n = list_append(p, n, $3);
4494 $$ = new_hash(p, n, &@$);
4496 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4501p_alt : p_alt '|' p_expr_basic
4504 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4506 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4511p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4512p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4514p_expr_basic : p_value
4516 | p_const p_lparen p_args rparen
4518 pop_pktbl(p, $<tbl>2);
4519 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4521 nd_set_first_loc($$, @1.beg_pos);
4525 | p_const p_lparen p_find rparen
4527 pop_pktbl(p, $<tbl>2);
4528 $$ = new_find_pattern(p, $1, $3, &@$);
4530 nd_set_first_loc($$, @1.beg_pos);
4534 | p_const p_lparen p_kwargs rparen
4536 pop_pktbl(p, $<tbl>2);
4537 $$ = new_hash_pattern(p, $1, $3, &@$);
4539 nd_set_first_loc($$, @1.beg_pos);
4543 | p_const '(' rparen
4545 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4546 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4548 | p_const p_lbracket p_args rbracket
4550 pop_pktbl(p, $<tbl>2);
4551 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4553 nd_set_first_loc($$, @1.beg_pos);
4557 | p_const p_lbracket p_find rbracket
4559 pop_pktbl(p, $<tbl>2);
4560 $$ = new_find_pattern(p, $1, $3, &@$);
4562 nd_set_first_loc($$, @1.beg_pos);
4566 | p_const p_lbracket p_kwargs rbracket
4568 pop_pktbl(p, $<tbl>2);
4569 $$ = new_hash_pattern(p, $1, $3, &@$);
4571 nd_set_first_loc($$, @1.beg_pos);
4575 | p_const '[' rbracket
4577 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4578 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4580 | tLBRACK p_args rbracket
4582 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4584 | tLBRACK p_find rbracket
4586 $$ = new_find_pattern(p, Qnone, $2, &@$);
4590 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4591 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4595 $<tbl>$ = push_pktbl(p);
4597 p->ctxt.in_kwarg = 0;
4601 pop_pktbl(p, $<tbl>2);
4602 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4603 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4607 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4608 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4610 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4612 pop_pktbl(p, $<tbl>2);
4620 NODE *pre_args = NEW_LIST($1, &@$);
4621 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4623 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4628 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4633 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4635 VALUE pre_args = rb_ary_concat($1, get_value($2));
4636 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4639 | p_args_head p_rest
4641 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
4643 | p_args_head p_rest ',' p_args_post
4645 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
4650p_args_head : p_arg ','
4654 | p_args_head p_arg ','
4657 $$ = list_concat($1, $2);
4659 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4665 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4667 | p_rest ',' p_args_post
4669 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4673p_find : p_rest ',' p_args_post ',' p_rest
4675 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4680p_rest : tSTAR tIDENTIFIER
4691 | p_args_post ',' p_arg
4694 $$ = list_concat($1, $3);
4696 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4703 $$ = NEW_LIST($1, &@$);
4705 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4709p_kwargs : p_kwarg ',' p_any_kwrest
4711 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4715 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4719 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4723 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4728 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4732 $$ = list_concat($1, $3);
4734 /*% ripper: rb_ary_push($1, $3) %*/
4738p_kw : p_kw_label p_expr
4740 error_duplicate_pattern_key(p, get_id($1), &@1);
4742 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
4744 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4748 error_duplicate_pattern_key(p, get_id($1), &@1);
4749 if ($1 && !is_local_id(get_id($1))) {
4750 yyerror1(&@1, "key must be valid as local variables");
4752 error_duplicate_pattern_variable(p, get_id($1), &@1);
4754 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4756 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4761 | tSTRING_BEG string_contents tLABEL_END
4763 YYLTYPE loc = code_loc_gen(&@1, &@3);
4765 if (!$2 || nd_type_p($2, NODE_STR)) {
4766 NODE *node = dsym_node(p, $2, &loc);
4767 $$ = SYM2ID(node->nd_lit);
4770 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4771 VALUE label = RNODE($2)->nd_cval;
4772 VALUE rval = RNODE($2)->nd_rval;
4773 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4774 RNODE($$)->nd_loc = loc;
4778 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4784p_kwrest : kwrest_mark tIDENTIFIER
4794p_kwnorest : kwrest_mark keyword_nil
4800p_any_kwrest : p_kwrest
4801 | p_kwnorest {$$ = ID2VAL(idNil);}
4804p_value : p_primitive
4805 | p_primitive tDOT2 p_primitive
4810 $$ = NEW_DOT2($1, $3, &@$);
4812 /*% ripper: dot2!($1, $3) %*/
4814 | p_primitive tDOT3 p_primitive
4819 $$ = NEW_DOT3($1, $3, &@$);
4821 /*% ripper: dot3!($1, $3) %*/
4827 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4829 /*% ripper: dot2!($1, Qnil) %*/
4835 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4837 /*% ripper: dot3!($1, Qnil) %*/
4842 | tBDOT2 p_primitive
4846 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4848 /*% ripper: dot2!(Qnil, $2) %*/
4850 | tBDOT3 p_primitive
4854 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4856 /*% ripper: dot3!(Qnil, $2) %*/
4860p_primitive : literal
4871 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4873 /*% ripper: var_ref!($1) %*/
4878p_variable : tIDENTIFIER
4881 error_duplicate_pattern_variable(p, $1, &@1);
4882 $$ = assignable(p, $1, 0, &@$);
4884 /*% ripper: assignable(p, var_field(p, $1)) %*/
4888p_var_ref : '^' tIDENTIFIER
4891 NODE *n = gettable(p, $2, &@$);
4892 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4893 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4897 /*% ripper: var_ref!($2) %*/
4902 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4904 /*% ripper: var_ref!($2) %*/
4908p_expr_ref : '^' tLPAREN expr_value rparen
4911 $$ = NEW_BEGIN($3, &@$);
4913 /*% ripper: begin!($3) %*/
4917p_const : tCOLON3 cname
4920 $$ = NEW_COLON3($2, &@$);
4922 /*% ripper: top_const_ref!($2) %*/
4924 | p_const tCOLON2 cname
4927 $$ = NEW_COLON2($1, $3, &@$);
4929 /*% ripper: const_path_ref!($1, $3) %*/
4934 $$ = gettable(p, $1, &@$);
4936 /*% ripper: var_ref!($1) %*/
4940opt_rescue : k_rescue exc_list exc_var then
4945 $$ = NEW_RESBODY($2,
4946 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4959 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4967 $$ = NEW_LIST($1, &@$);
4969 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4974 if (!($$ = splat_array($1))) $$ = $1;
4988opt_ensure : k_ensure compstmt
4993 /*% ripper: ensure!($2) %*/
5007 node = NEW_STR(STR_NEW0(), &@$);
5008 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
5011 node = evstr2dstr(p, node);
5024 $$ = literal_concat(p, $1, $2, &@$);
5026 /*% ripper: string_concat!($1, $2) %*/
5030string1 : tSTRING_BEG string_contents tSTRING_END
5033 $$ = heredoc_dedent(p, $2);
5034 if ($$) nd_set_loc($$, &@$);
5036 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5040xstring : tXSTRING_BEG xstring_contents tSTRING_END
5043 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5045 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5049regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5051 $$ = new_regexp(p, $2, $3, &@$);
5055words : tWORDS_BEG ' ' word_list tSTRING_END
5058 $$ = make_list($3, &@$);
5060 /*% ripper: array!($3) %*/
5064word_list : /* none */
5069 /*% ripper: words_new! %*/
5071 | word_list word ' '
5074 $$ = list_append(p, $1, evstr2dstr(p, $2));
5076 /*% ripper: words_add!($1, $2) %*/
5080word : string_content
5081 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5082 | word string_content
5085 $$ = literal_concat(p, $1, $2, &@$);
5087 /*% ripper: word_add!($1, $2) %*/
5091symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
5094 $$ = make_list($3, &@$);
5096 /*% ripper: array!($3) %*/
5100symbol_list : /* none */
5105 /*% ripper: symbols_new! %*/
5107 | symbol_list word ' '
5110 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5112 /*% ripper: symbols_add!($1, $2) %*/
5116qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
5119 $$ = make_list($3, &@$);
5121 /*% ripper: array!($3) %*/
5125qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
5128 $$ = make_list($3, &@$);
5130 /*% ripper: array!($3) %*/
5134qword_list : /* none */
5139 /*% ripper: qwords_new! %*/
5141 | qword_list tSTRING_CONTENT ' '
5144 $$ = list_append(p, $1, $2);
5146 /*% ripper: qwords_add!($1, $2) %*/
5150qsym_list : /* none */
5155 /*% ripper: qsymbols_new! %*/
5157 | qsym_list tSTRING_CONTENT ' '
5160 $$ = symbol_append(p, $1, $2);
5162 /*% ripper: qsymbols_add!($1, $2) %*/
5166string_contents : /* none */
5171 /*% ripper: string_content! %*/
5174 $$ = ripper_new_yylval(p, 0, $$, 0);
5177 | string_contents string_content
5180 $$ = literal_concat(p, $1, $2, &@$);
5182 /*% ripper: string_add!($1, $2) %*/
5185 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
5186 !RNODE($1)->nd_cval) {
5187 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
5188 RNODE($1)->nd_rval = add_mark_object(p, $$);
5195xstring_contents: /* none */
5200 /*% ripper: xstring_new! %*/
5202 | xstring_contents string_content
5205 $$ = literal_concat(p, $1, $2, &@$);
5207 /*% ripper: xstring_add!($1, $2) %*/
5211regexp_contents: /* none */
5216 /*% ripper: regexp_new! %*/
5219 $$ = ripper_new_yylval(p, 0, $$, 0);
5222 | regexp_contents string_content
5225 NODE *head = $1, *tail = $2;
5233 switch (nd_type(head)) {
5235 nd_set_type(head, NODE_DSTR);
5240 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5243 $$ = list_append(p, head, tail);
5246 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5247 if (ripper_is_node_yylval(n1)) {
5248 s1 = RNODE(n1)->nd_cval;
5249 n1 = RNODE(n1)->nd_rval;
5251 if (ripper_is_node_yylval(n2)) {
5252 s2 = RNODE(n2)->nd_cval;
5253 n2 = RNODE(n2)->nd_rval;
5255 $$ = dispatch2(regexp_add, n1, n2);
5257 $$ = ripper_new_yylval(p, 0, $$, s2);
5263string_content : tSTRING_CONTENT
5264 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5267 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5268 $<strterm>$ = p->lex.strterm;
5270 SET_LEX_STATE(EXPR_BEG);
5274 p->lex.strterm = $<strterm>2;
5276 $$ = NEW_EVSTR($3, &@$);
5277 nd_set_line($$, @3.end_pos.lineno);
5279 /*% ripper: string_dvar!($3) %*/
5287 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5288 $<strterm>$ = p->lex.strterm;
5292 $<num>$ = p->lex.state;
5293 SET_LEX_STATE(EXPR_BEG);
5296 $<num>$ = p->lex.brace_nest;
5297 p->lex.brace_nest = 0;
5300 $<num>$ = p->heredoc_indent;
5301 p->heredoc_indent = 0;
5303 compstmt tSTRING_DEND
5307 p->lex.strterm = $<strterm>3;
5308 SET_LEX_STATE($<num>4);
5309 p->lex.brace_nest = $<num>5;
5310 p->heredoc_indent = $<num>6;
5311 p->heredoc_line_indent = -1;
5313 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
5314 $$ = new_evstr(p, $7, &@$);
5316 /*% ripper: string_embexpr!($7) %*/
5323 $$ = NEW_GVAR($1, &@$);
5325 /*% ripper: var_ref!($1) %*/
5330 $$ = NEW_IVAR($1, &@$);
5332 /*% ripper: var_ref!($1) %*/
5337 $$ = NEW_CVAR($1, &@$);
5339 /*% ripper: var_ref!($1) %*/
5350 SET_LEX_STATE(EXPR_END);
5352 $$ = NEW_LIT(ID2SYM($2), &@$);
5354 /*% ripper: symbol_literal!(symbol!($2)) %*/
5362dsym : tSYMBEG string_contents tSTRING_END
5364 SET_LEX_STATE(EXPR_END);
5366 $$ = dsym_node(p, $2, &@$);
5368 /*% ripper: dyna_symbol!($2) %*/
5372numeric : simple_numeric
5373 | tUMINUS_NUM simple_numeric %prec tLOWEST
5377 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5379 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5383simple_numeric : tINTEGER
5394user_variable : tIDENTIFIER
5399keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5400 | keyword_self {$$ = KWD2EID(self, $1);}
5401 | keyword_true {$$ = KWD2EID(true, $1);}
5402 | keyword_false {$$ = KWD2EID(false, $1);}
5403 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5404 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5405 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5408var_ref : user_variable
5411 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5413 if (id_is_var(p, get_id($1))) {
5414 $$ = dispatch1(var_ref, $1);
5417 $$ = dispatch1(vcall, $1);
5424 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5426 /*% ripper: var_ref!($1) %*/
5430var_lhs : user_variable
5433 $$ = assignable(p, $1, 0, &@$);
5435 /*% ripper: assignable(p, var_field(p, $1)) %*/
5440 $$ = assignable(p, $1, 0, &@$);
5442 /*% ripper: assignable(p, var_field(p, $1)) %*/
5452 SET_LEX_STATE(EXPR_BEG);
5453 p->command_start = TRUE;
5464 /*% ripper: Qnil %*/
5468f_opt_paren_args: f_paren_args
5471 p->ctxt.in_argdef = 0;
5472 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5473 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5477f_paren_args : '(' f_args rparen
5482 /*% ripper: paren!($2) %*/
5483 SET_LEX_STATE(EXPR_BEG);
5484 p->command_start = TRUE;
5485 p->ctxt.in_argdef = 0;
5489f_arglist : f_paren_args
5492 p->ctxt.in_kwarg = 1;
5493 p->ctxt.in_argdef = 1;
5494 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5498 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5499 p->ctxt.in_argdef = 0;
5501 SET_LEX_STATE(EXPR_BEG);
5502 p->command_start = TRUE;
5506args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5508 $$ = new_args_tail(p, $1, $3, $4, &@3);
5510 | f_kwarg opt_f_block_arg
5512 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5514 | f_any_kwrest opt_f_block_arg
5516 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5520 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5524 add_forwarding_args(p);
5525 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5527 ($$->nd_ainfo)->forwarding = 1;
5532opt_args_tail : ',' args_tail
5538 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5542f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5544 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5546 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5548 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5550 | f_arg ',' f_optarg opt_args_tail
5552 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5554 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5556 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5558 | f_arg ',' f_rest_arg opt_args_tail
5560 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5562 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5564 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5566 | f_arg opt_args_tail
5568 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5570 | f_optarg ',' f_rest_arg opt_args_tail
5572 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5574 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5576 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5578 | f_optarg opt_args_tail
5580 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5582 | f_optarg ',' f_arg opt_args_tail
5584 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5586 | f_rest_arg opt_args_tail
5588 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5590 | f_rest_arg ',' f_arg opt_args_tail
5592 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5596 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5600 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5601 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5605args_forward : tBDOT3
5608#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
5614 /*% ripper: args_forward! %*/
5618f_bad_arg : tCONSTANT
5620 static const char mesg[] = "formal argument cannot be a constant";
5622 yyerror1(&@1, mesg);
5625 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5629 static const char mesg[] = "formal argument cannot be an instance variable";
5631 yyerror1(&@1, mesg);
5634 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5638 static const char mesg[] = "formal argument cannot be a global variable";
5640 yyerror1(&@1, mesg);
5643 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5647 static const char mesg[] = "formal argument cannot be a class variable";
5649 yyerror1(&@1, mesg);
5652 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5656f_norm_arg : f_bad_arg
5659 formal_argument(p, $1);
5660 p->max_numparam = ORDINAL_PARAM;
5665f_arg_asgn : f_norm_arg
5674f_arg_item : f_arg_asgn
5678 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5680 /*% ripper: get_value($1) %*/
5682 | tLPAREN f_margs rparen
5685 ID tid = internal_id(p);
5687 loc.beg_pos = @2.beg_pos;
5688 loc.end_pos = @2.beg_pos;
5690 if (dyna_in_block(p)) {
5691 $2->nd_value = NEW_DVAR(tid, &loc);
5694 $2->nd_value = NEW_LVAR(tid, &loc);
5696 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5699 /*% ripper: mlhs_paren!($2) %*/
5704 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5705 | f_arg ',' f_arg_item
5710 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5711 rb_discard_node(p, $3);
5713 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5720 arg_var(p, formal_argument(p, $1));
5721 p->cur_arg = get_id($1);
5722 p->max_numparam = ORDINAL_PARAM;
5723 p->ctxt.in_argdef = 0;
5728f_kw : f_label arg_value
5731 p->ctxt.in_argdef = 1;
5733 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5735 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5740 p->ctxt.in_argdef = 1;
5742 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5744 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5748f_block_kw : f_label primary_value
5750 p->ctxt.in_argdef = 1;
5752 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5754 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5758 p->ctxt.in_argdef = 1;
5760 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5762 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5766f_block_kwarg : f_block_kw
5771 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5773 | f_block_kwarg ',' f_block_kw
5776 $$ = kwd_append($1, $3);
5778 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5788 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5793 $$ = kwd_append($1, $3);
5795 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5803f_no_kwarg : p_kwnorest
5807 /*% ripper: nokw_param!(Qnil) %*/
5811f_kwrest : kwrest_mark tIDENTIFIER
5813 arg_var(p, shadowing_lvar(p, get_id($2)));
5817 /*% ripper: kwrest_param!($2) %*/
5821 arg_var(p, idFWD_KWREST);
5825 /*% ripper: kwrest_param!(Qnil) %*/
5829f_opt : f_arg_asgn f_eq arg_value
5832 p->ctxt.in_argdef = 1;
5834 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5836 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5840f_block_opt : f_arg_asgn f_eq primary_value
5843 p->ctxt.in_argdef = 1;
5845 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5847 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5851f_block_optarg : f_block_opt
5856 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5858 | f_block_optarg ',' f_block_opt
5861 $$ = opt_arg_append($1, $3);
5863 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5872 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5874 | f_optarg ',' f_opt
5877 $$ = opt_arg_append($1, $3);
5879 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5887f_rest_arg : restarg_mark tIDENTIFIER
5889 arg_var(p, shadowing_lvar(p, get_id($2)));
5893 /*% ripper: rest_param!($2) %*/
5897 arg_var(p, idFWD_REST);
5901 /*% ripper: rest_param!(Qnil) %*/
5909f_block_arg : blkarg_mark tIDENTIFIER
5911 arg_var(p, shadowing_lvar(p, get_id($2)));
5915 /*% ripper: blockarg!($2) %*/
5919 arg_var(p, idFWD_BLOCK);
5923 /*% ripper: blockarg!(Qnil) %*/
5927opt_f_block_arg : ',' f_block_arg
5942 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5945 switch (nd_type($3)) {
5954 yyerror1(&@3, "can't define singleton method for literals");
5962 /*% ripper: paren!($3) %*/
5972 /*% ripper: assoclist_from_args!($1) %*/
5977 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5987 if (assocs->nd_head &&
5988 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
5989 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
5991 tail = tail->nd_next->nd_head->nd_head;
5993 assocs = list_concat(assocs, tail);
5997 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6001assoc : arg_value tASSOC arg_value
6004 if (nd_type_p($1, NODE_STR)) {
6005 nd_set_type($1, NODE_LIT);
6006 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
6008 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6010 /*% ripper: assoc_new!($1, $3) %*/
6015 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6017 /*% ripper: assoc_new!($1, $2) %*/
6022 NODE *val = gettable(p, $1, &@$);
6023 if (!val) val = NEW_BEGIN(0, &@$);
6024 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6026 /*% ripper: assoc_new!($1, Qnil) %*/
6028 | tSTRING_BEG string_contents tLABEL_END arg_value
6031 YYLTYPE loc = code_loc_gen(&@1, &@3);
6032 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6034 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6039 if (nd_type_p($2, NODE_HASH) &&
6040 !($2->nd_head && $2->nd_head->nd_alen)) {
6041 static VALUE empty_hash;
6043 empty_hash = rb_obj_freeze(rb_hash_new());
6044 rb_gc_register_mark_object(empty_hash);
6046 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6049 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6051 /*% ripper: assoc_splat!($2) %*/
6055 if (!local_id(p, idFWD_KWREST) ||
6056 local_id(p, idFWD_ALL)) {
6057 compile_error(p, "no anonymous keyword rest parameter");
6060 $$ = list_append(p, NEW_LIST(0, &@$),
6061 NEW_LVAR(idFWD_KWREST, &@$));
6063 /*% ripper: assoc_splat!(Qnil) %*/
6067operation : tIDENTIFIER
6072operation2 : operation
6076operation3 : tIDENTIFIER
6093opt_terms : /* none */
6104rbracket : opt_nl ']'
6114term : ';' {yyerrok;token_flush(p);}
6117 @$.end_pos = @$.beg_pos;
6123 | terms ';' {yyerrok;}
6135# define yylval (*p->lval)
6137static int regx_options(struct parser_params*);
6138static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6139static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6140static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6141static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6144# define set_yylval_node(x) { \
6146 rb_parser_set_location(p, &_cur_loc); \
6147 yylval.node = (x); \
6149# define set_yylval_str(x) \
6151 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6152 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6154# define set_yylval_literal(x) \
6156 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6157 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6159# define set_yylval_num(x) (yylval.num = (x))
6160# define set_yylval_id(x) (yylval.id = (x))
6161# define set_yylval_name(x) (yylval.id = (x))
6162# define yylval_id() (yylval.id)
6165ripper_yylval_id(struct parser_params *p, ID x)
6167 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6169# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6170# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6171# define set_yylval_id(x) (void)(x)
6172# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6173# define set_yylval_literal(x) add_mark_object(p, (x))
6174# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6175# define yylval_id() yylval.id
6176# define _cur_loc NULL_LOC /* dummy */
6179#define set_yylval_noname() set_yylval_id(keyword_nil)
6180#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6183#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6184#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6187parser_has_token(struct parser_params *p)
6189 if (p->keep_tokens && (p->lex.pcur < p->lex.ptok)) rb_bug("lex.pcur < lex.ptok. (line: %d) %ld|%ld|%ld", p->ruby_sourceline, p->lex.ptok - p->lex.pbeg, p->lex.pcur - p->lex.ptok, p->lex.pend - p->lex.pcur);
6190 return p->lex.pcur > p->lex.ptok;
6194code_loc_to_ary(const rb_code_location_t *loc)
6196 VALUE ary = rb_ary_new_from_args(4,
6197 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6198 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6205parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6210 ary = rb_ary_new2(4);
6211 token_id = p->token_id;
6212 rb_ary_push(ary, INT2FIX(token_id));
6213 rb_ary_push(ary, ID2SYM(parser_token2id(t)));
6214 rb_ary_push(ary, str);
6215 rb_ary_push(ary, code_loc_to_ary(p->yylloc));
6217 rb_ary_push(p->tokens, ary);
6221 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6226parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6228 debug_token_line(p, "parser_dispatch_scan_event", line);
6230 if (!parser_has_token(p)) return;
6232 RUBY_SET_YYLLOC(*p->yylloc);
6234 if (p->keep_tokens) {
6235 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6236 parser_append_tokens(p, str, t, line);
6242#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6244parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6246 int saved_line = p->ruby_sourceline;
6247 const char *saved_tokp = p->lex.ptok;
6249 debug_token_line(p, "parser_dispatch_delayed_token", line);
6251 if (!has_delayed_token(p)) return;
6253 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6255 if (p->keep_tokens) {
6256 p->ruby_sourceline = p->delayed.beg_line;
6257 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6258 parser_append_tokens(p, p->delayed.token, t, line);
6259 p->ruby_sourceline = saved_line;
6260 p->lex.ptok = saved_tokp;
6263 p->delayed.token = Qnil;
6266#define literal_flush(p, ptr) ((void)(ptr))
6268#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
6271intern_sym(const char *name)
6273 ID id = rb_intern_const(name);
6278ripper_has_scan_event(struct parser_params *p)
6280 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6281 return p->lex.pcur > p->lex.ptok;
6285ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6287 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6288 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6289 RUBY_SET_YYLLOC(*p->yylloc);
6295ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6297 if (!ripper_has_scan_event(p)) return;
6298 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6300#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6303ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6305 int saved_line = p->ruby_sourceline;
6306 const char *saved_tokp = p->lex.ptok;
6308 if (!has_delayed_token(p)) return;
6309 p->ruby_sourceline = p->delayed.beg_line;
6310 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6311 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6312 p->delayed.token = Qnil;
6313 p->ruby_sourceline = saved_line;
6314 p->lex.ptok = saved_tokp;
6316#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6320is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6322 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6326parser_is_identchar(struct parser_params *p)
6328 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
6332parser_isascii(struct parser_params *p)
6334 return ISASCII(*(p->lex.pcur-1));
6338token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6340 int column = 1, nonspc = 0, i;
6341 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6343 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6346 if (*ptr != ' ' && *ptr != '\t') {
6351 ptinfo->beg = loc->beg_pos;
6352 ptinfo->indent = column;
6353 ptinfo->nonspc = nonspc;
6357token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6361 if (!p->token_info_enabled) return;
6362 ptinfo = ALLOC(token_info);
6363 ptinfo->token = token;
6364 ptinfo->next = p->token_info;
6365 token_info_setup(ptinfo, p->lex.pbeg, loc);
6367 p->token_info = ptinfo;
6371token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6373 token_info *ptinfo_beg = p->token_info;
6375 if (!ptinfo_beg) return;
6376 p->token_info = ptinfo_beg->next;
6378 /* indentation check of matched keywords (begin..end, if..end, etc.) */
6379 token_info_warn(p, token, ptinfo_beg, 1, loc);
6380 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6384token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
6386 token_info *ptinfo_beg = p->token_info;
6388 if (!ptinfo_beg) return;
6389 p->token_info = ptinfo_beg->next;
6391 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
6392 ptinfo_beg->beg.column != beg_pos.column ||
6393 strcmp(ptinfo_beg->token, token)) {
6394 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
6395 beg_pos.lineno, beg_pos.column, token,
6396 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
6400 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6404token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
6406 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
6407 if (!p->token_info_enabled) return;
6408 if (!ptinfo_beg) return;
6409 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
6410 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
6411 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
6412 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
6413 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
6414 rb_warn3L(ptinfo_end->beg.lineno,
6415 "mismatched indentations at '%s' with '%s' at %d",
6416 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
6420parser_precise_mbclen(struct parser_params *p, const char *ptr)
6422 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
6423 if (!MBCLEN_CHARFOUND_P(len)) {
6424 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
6431static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6434parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6437 int lineno = p->ruby_sourceline;
6441 else if (yylloc->beg_pos.lineno == lineno) {
6442 str = p->lex.lastline;
6447 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6451parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6457 yylloc = RUBY_SET_YYLLOC(current);
6459 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6460 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6464 compile_error(p, "%s", msg);
6465 parser_show_error_line(p, yylloc);
6470parser_yyerror0(struct parser_params *p, const char *msg)
6473 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6477ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6480 const int max_line_margin = 30;
6481 const char *ptr, *ptr_end, *pt, *pb;
6482 const char *pre = "", *post = "", *pend;
6483 const char *code = "", *caret = "";
6485 const char *const pbeg = RSTRING_PTR(str);
6490 if (!yylloc) return;
6491 pend = RSTRING_END(str);
6492 if (pend > pbeg && pend[-1] == '\n') {
6493 if (--pend > pbeg && pend[-1] == '\r') --pend;
6497 if (lineno == yylloc->end_pos.lineno &&
6498 (pend - pbeg) > yylloc->end_pos.column) {
6499 pt = pbeg + yylloc->end_pos.column;
6503 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6504 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6506 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6507 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6509 len = ptr_end - ptr;
6512 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6513 if (ptr > pbeg) pre = "...";
6515 if (ptr_end < pend) {
6516 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6517 if (ptr_end < pend) post = "...";
6521 if (lineno == yylloc->beg_pos.lineno) {
6522 pb += yylloc->beg_pos.column;
6523 if (pb > pt) pb = pt;
6525 if (pb < ptr) pb = ptr;
6526 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6529 if (RTEST(errbuf)) {
6530 mesg = rb_attr_get(errbuf, idMesg);
6531 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6532 rb_str_cat_cstr(mesg, "\n");
6535 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6537 if (!errbuf && rb_stderr_tty_p()) {
6538#define CSI_BEGIN "\033["
6541 CSI_BEGIN""CSI_SGR"%s" /* pre */
6542 CSI_BEGIN"1"CSI_SGR"%.*s"
6543 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6544 CSI_BEGIN";1"CSI_SGR"%.*s"
6545 CSI_BEGIN""CSI_SGR"%s" /* post */
6548 (int)(pb - ptr), ptr,
6550 (int)(ptr_end - pt), pt,
6556 len = ptr_end - ptr;
6557 lim = pt < pend ? pt : pend;
6558 i = (int)(lim - ptr);
6559 buf = ALLOCA_N(char, i+2);
6564 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6570 memset(p2, '~', (lim - ptr));
6574 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6575 pre, (int)len, code, post,
6578 if (!errbuf) rb_write_error_str(mesg);
6582parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6584 const char *pcur = 0, *ptok = 0;
6585 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6586 p->ruby_sourceline == yylloc->end_pos.lineno) {
6589 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6590 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6592 parser_yyerror0(p, msg);
6601parser_yyerror0(struct parser_params *p, const char *msg)
6603 dispatch1(parse_error, STR_NEW2(msg));
6609parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6616vtable_size(const struct vtable *tbl)
6618 if (!DVARS_TERMINAL_P(tbl)) {
6627static struct vtable *
6628vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6630 struct vtable *tbl = ALLOC(struct vtable);
6633 tbl->tbl = ALLOC_N(ID, tbl->capa);
6637 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6642#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6645vtable_free_gen(struct parser_params *p, int line, const char *name,
6650 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6653 if (!DVARS_TERMINAL_P(tbl)) {
6655 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6657 ruby_sized_xfree(tbl, sizeof(*tbl));
6660#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6663vtable_add_gen(struct parser_params *p, int line, const char *name,
6664 struct vtable *tbl, ID id)
6668 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6669 line, name, (void *)tbl, rb_id2name(id));
6672 if (DVARS_TERMINAL_P(tbl)) {
6673 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6676 if (tbl->pos == tbl->capa) {
6677 tbl->capa = tbl->capa * 2;
6678 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6680 tbl->tbl[tbl->pos++] = id;
6682#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6686vtable_pop_gen(struct parser_params *p, int line, const char *name,
6687 struct vtable *tbl, int n)
6690 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6691 line, name, (void *)tbl, n);
6694 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6699#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6703vtable_included(const struct vtable * tbl, ID id)
6707 if (!DVARS_TERMINAL_P(tbl)) {
6708 for (i = 0; i < tbl->pos; i++) {
6709 if (tbl->tbl[i] == id) {
6717static void parser_prepare(struct parser_params *p);
6720static NODE *parser_append_options(struct parser_params *p, NODE *node);
6723debug_lines(VALUE fname)
6726 CONST_ID(script_lines, "SCRIPT_LINES__");
6727 if (rb_const_defined_at(rb_cObject, script_lines)) {
6728 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6729 if (RB_TYPE_P(hash, T_HASH)) {
6730 VALUE lines = rb_ary_new();
6731 rb_hash_aset(hash, fname, lines);
6739e_option_supplied(struct parser_params *p)
6741 return strcmp(p->ruby_sourcefile, "-e") == 0;
6745yycompile0(VALUE arg)
6749 struct parser_params *p = (struct parser_params *)arg;
6752 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6753 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6754 if (p->debug_lines && p->ruby_sourceline > 0) {
6755 VALUE str = rb_default_rs;
6756 n = p->ruby_sourceline;
6758 rb_ary_push(p->debug_lines, str);
6762 if (!e_option_supplied(p)) {
6767 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6768 if (!p->debug_lines) {
6769 p->debug_lines = rb_ary_new();
6772 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6776#define RUBY_DTRACE_PARSE_HOOK(name) \
6777 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6778 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6780 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6782 RUBY_DTRACE_PARSE_HOOK(END);
6786 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6787 if (n || p->error_p) {
6788 VALUE mesg = p->error_buffer;
6790 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6792 if (!p->error_tolerant) {
6793 rb_set_errinfo(mesg);
6797 tree = p->eval_tree;
6799 tree = NEW_NIL(&NULL_LOC);
6802 VALUE opt = p->compile_option;
6803 VALUE tokens = p->tokens;
6805 NODE *body = parser_append_options(p, tree->nd_body);
6806 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6807 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6808 prelude = block_append(p, p->eval_tree_begin, body);
6809 tree->nd_body = prelude;
6810 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6811 if (p->keep_tokens) {
6812 rb_obj_freeze(tokens);
6813 rb_ast_set_tokens(p->ast, tokens);
6816 p->ast->body.root = tree;
6817 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6822yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6826 p->ruby_sourcefile_string = Qnil;
6827 p->ruby_sourcefile = "(none)";
6830 p->ruby_sourcefile_string = rb_fstring(fname);
6831 p->ruby_sourcefile = StringValueCStr(fname);
6833 p->ruby_sourceline = line - 1;
6837 p->ast = ast = rb_ast_new();
6838 rb_suppress_tracing(yycompile0, (VALUE)p);
6840 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6851must_be_ascii_compatible(VALUE s)
6853 rb_encoding *enc = rb_enc_get(s);
6854 if (!rb_enc_asciicompat(enc)) {
6855 rb_raise(rb_eArgError, "invalid source encoding");
6861lex_get_str(struct parser_params *p, VALUE s)
6863 char *beg, *end, *start;
6866 beg = RSTRING_PTR(s);
6867 len = RSTRING_LEN(s);
6869 if (p->lex.gets_.ptr) {
6870 if (len == p->lex.gets_.ptr) return Qnil;
6871 beg += p->lex.gets_.ptr;
6872 len -= p->lex.gets_.ptr;
6874 end = memchr(beg, '\n', len);
6875 if (end) len = ++end - beg;
6876 p->lex.gets_.ptr += len;
6877 return rb_str_subseq(s, beg - start, len);
6881lex_getline(struct parser_params *p)
6883 VALUE line = (*p->lex.gets)(p, p->lex.input);
6884 if (NIL_P(line)) return line;
6885 must_be_ascii_compatible(line);
6886 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6891static const rb_data_type_t parser_data_type;
6895parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6897 struct parser_params *p;
6899 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6901 p->lex.gets = lex_get_str;
6902 p->lex.gets_.ptr = 0;
6903 p->lex.input = rb_str_new_frozen(s);
6904 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6906 return yycompile(vparser, p, fname, line);
6910rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6912 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6916rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6918 must_be_ascii_compatible(s);
6919 return parser_compile_string(vparser, f, s, line);
6922VALUE rb_io_gets_internal(VALUE io);
6925lex_io_gets(struct parser_params *p, VALUE io)
6927 return rb_io_gets_internal(io);
6931rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6933 struct parser_params *p;
6935 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6937 p->lex.gets = lex_io_gets;
6938 p->lex.input = file;
6939 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6941 return yycompile(vparser, p, fname, start);
6945lex_generic_gets(struct parser_params *p, VALUE input)
6947 return (*p->lex.gets_.call)(input, p->line_count);
6951rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6953 struct parser_params *p;
6955 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6957 p->lex.gets = lex_generic_gets;
6958 p->lex.gets_.call = lex_gets;
6959 p->lex.input = input;
6960 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6962 return yycompile(vparser, p, fname, start);
6966#define STR_FUNC_ESCAPE 0x01
6967#define STR_FUNC_EXPAND 0x02
6968#define STR_FUNC_REGEXP 0x04
6969#define STR_FUNC_QWORDS 0x08
6970#define STR_FUNC_SYMBOL 0x10
6971#define STR_FUNC_INDENT 0x20
6972#define STR_FUNC_LABEL 0x40
6973#define STR_FUNC_LIST 0x4000
6974#define STR_FUNC_TERM 0x8000
6977 str_label = STR_FUNC_LABEL,
6979 str_dquote = (STR_FUNC_EXPAND),
6980 str_xquote = (STR_FUNC_EXPAND),
6981 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6982 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6983 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6984 str_ssym = (STR_FUNC_SYMBOL),
6985 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6989parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6993 str = rb_enc_str_new(ptr, len, enc);
6994 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6995 if (is_ascii_string(str)) {
6997 else if (rb_is_usascii_enc(enc0) && enc != rb_utf8_encoding()) {
6998 rb_enc_associate(str, rb_ascii8bit_encoding());
7005#define peek(p,c) peek_n(p, (c), 0)
7006#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7007#define peekc(p) peekc_n(p, 0)
7008#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7011add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7014 debug_token_line(p, "add_delayed_token", line);
7018 if (!has_delayed_token(p)) {
7019 p->delayed.token = rb_str_buf_new(end - tok);
7020 rb_enc_associate(p->delayed.token, p->enc);
7021 p->delayed.beg_line = p->ruby_sourceline;
7022 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7024 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7025 p->delayed.end_line = p->ruby_sourceline;
7026 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7032nextline(struct parser_params *p, int set_encoding)
7034 VALUE v = p->lex.nextline;
7035 p->lex.nextline = 0;
7040 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
7044 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7051 if (p->debug_lines) {
7052 if (set_encoding) rb_enc_associate(v, p->enc);
7053 rb_ary_push(p->debug_lines, v);
7058 else if (NIL_P(v)) {
7059 /* after here-document without terminator */
7062 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7063 if (p->heredoc_end > 0) {
7064 p->ruby_sourceline = p->heredoc_end;
7067 p->ruby_sourceline++;
7068 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7069 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7071 p->lex.lastline = v;
7076parser_cr(struct parser_params *p, int c)
7078 if (peek(p, '\n')) {
7086nextc0(struct parser_params *p, int set_encoding)
7090 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
7091 if (nextline(p, set_encoding)) return -1;
7093 c = (unsigned char)*p->lex.pcur++;
7094 if (UNLIKELY(c == '\r')) {
7095 c = parser_cr(p, c);
7100#define nextc(p) nextc0(p, TRUE)
7103pushback(struct parser_params *p, int c)
7105 if (c == -1) return;
7107 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7112#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7114#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7115#define tok(p) (p)->tokenbuf
7116#define toklen(p) (p)->tokidx
7119looking_at_eol_p(struct parser_params *p)
7121 const char *ptr = p->lex.pcur;
7122 while (ptr < p->lex.pend) {
7123 int c = (unsigned char)*ptr++;
7124 int eol = (c == '\n' || c == '#');
7125 if (eol || !ISSPACE(c)) {
7133newtok(struct parser_params *p)
7136 p->tokline = p->ruby_sourceline;
7139 p->tokenbuf = ALLOC_N(char, 60);
7141 if (p->toksiz > 4096) {
7143 REALLOC_N(p->tokenbuf, char, 60);
7149tokspace(struct parser_params *p, int n)
7153 if (p->tokidx >= p->toksiz) {
7154 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7155 REALLOC_N(p->tokenbuf, char, p->toksiz);
7157 return &p->tokenbuf[p->tokidx-n];
7161tokadd(struct parser_params *p, int c)
7163 p->tokenbuf[p->tokidx++] = (char)c;
7164 if (p->tokidx >= p->toksiz) {
7166 REALLOC_N(p->tokenbuf, char, p->toksiz);
7171tok_hex(struct parser_params *p, size_t *numlen)
7175 c = scan_hex(p->lex.pcur, 2, numlen);
7177 yyerror0("invalid hex escape");
7181 p->lex.pcur += *numlen;
7185#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7188escaped_control_code(int c)
7214#define WARN_SPACE_CHAR(c, prefix) \
7215 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7218tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7219 int regexp_literal, int wide)
7222 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7223 p->lex.pcur += numlen;
7224 if (p->lex.strterm == NULL ||
7225 (p->lex.strterm->flags & STRTERM_HEREDOC) ||
7226 (p->lex.strterm->u.literal.u1.func != str_regexp)) {
7227 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7228 literal_flush(p, p->lex.pcur);
7229 yyerror0("invalid Unicode escape");
7230 return wide && numlen > 0;
7232 if (codepoint > 0x10ffff) {
7233 literal_flush(p, p->lex.pcur);
7234 yyerror0("invalid Unicode codepoint (too large)");
7237 if ((codepoint & 0xfffff800) == 0xd800) {
7238 literal_flush(p, p->lex.pcur);
7239 yyerror0("invalid Unicode codepoint");
7243 if (regexp_literal) {
7244 tokcopy(p, (int)numlen);
7246 else if (codepoint >= 0x80) {
7247 rb_encoding *utf8 = rb_utf8_encoding();
7248 if (*encp && utf8 != *encp) {
7249 YYLTYPE loc = RUBY_INIT_YYLLOC();
7250 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7251 parser_show_error_line(p, &loc);
7255 tokaddmbc(p, codepoint, *encp);
7258 tokadd(p, codepoint);
7263static int tokadd_mbchar(struct parser_params *p, int c);
7265/* return value is for ?\u3042 */
7267tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7268 int term, int symbol_literal, int regexp_literal)
7271 * If `term` is not -1, then we allow multiple codepoints in \u{}
7272 * upto `term` byte, otherwise we're parsing a character literal.
7273 * And then add the codepoints to the current token.
7275 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7277 const int open_brace = '{', close_brace = '}';
7279 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7281 if (peek(p, open_brace)) { /* handle \u{...} form */
7282 if (regexp_literal && p->lex.strterm->u.literal.u1.func == str_regexp) {
7284 * Skip parsing validation code and copy bytes as-is until term or
7285 * closing brace, in order to correctly handle extended regexps where
7286 * invalid unicode escapes are allowed in comments. The regexp parser
7287 * does its own validation and will catch any issues.
7289 tokadd(p, open_brace);
7290 while (++p->lex.pcur < p->lex.pend) {
7292 if (c == close_brace) {
7297 else if (c == term) {
7300 if (c == '\\' && p->lex.pcur + 1 < p->lex.pend) {
7304 tokadd_mbchar(p, c);
7308 const char *second = NULL;
7309 int c, last = nextc(p);
7310 if (p->lex.pcur >= p->lex.pend) goto unterminated;
7311 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
7312 while (c != close_brace) {
7313 if (c == term) goto unterminated;
7314 if (second == multiple_codepoints)
7315 second = p->lex.pcur;
7316 if (regexp_literal) tokadd(p, last);
7317 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7320 while (ISSPACE(c = *p->lex.pcur)) {
7321 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
7324 if (term == -1 && !second)
7325 second = multiple_codepoints;
7328 if (c != close_brace) {
7331 yyerror0("unterminated Unicode escape");
7334 if (second && second != multiple_codepoints) {
7335 const char *pcur = p->lex.pcur;
7336 p->lex.pcur = second;
7337 dispatch_scan_event(p, tSTRING_CONTENT);
7340 yyerror0(multiple_codepoints);
7344 if (regexp_literal) tokadd(p, close_brace);
7348 else { /* handle \uxxxx form */
7349 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7356#define ESCAPE_CONTROL 1
7357#define ESCAPE_META 2
7360read_escape(struct parser_params *p, int flags, rb_encoding **encp)
7365 switch (c = nextc(p)) {
7366 case '\\': /* Backslash */
7369 case 'n': /* newline */
7372 case 't': /* horizontal tab */
7375 case 'r': /* carriage-return */
7378 case 'f': /* form-feed */
7381 case 'v': /* vertical tab */
7384 case 'a': /* alarm(bell) */
7387 case 'e': /* escape */
7390 case '0': case '1': case '2': case '3': /* octal constant */
7391 case '4': case '5': case '6': case '7':
7393 c = scan_oct(p->lex.pcur, 3, &numlen);
7394 p->lex.pcur += numlen;
7397 case 'x': /* hex constant */
7398 c = tok_hex(p, &numlen);
7399 if (numlen == 0) return 0;
7402 case 'b': /* backspace */
7405 case 's': /* space */
7409 if (flags & ESCAPE_META) goto eof;
7410 if ((c = nextc(p)) != '-') {
7413 if ((c = nextc(p)) == '\\') {
7419 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
7421 else if (c == -1 || !ISASCII(c)) goto eof;
7423 int c2 = escaped_control_code(c);
7425 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
7426 WARN_SPACE_CHAR(c2, "\\M-");
7429 WARN_SPACE_CHAR(c2, "\\C-\\M-");
7432 else if (ISCNTRL(c)) goto eof;
7433 return ((c & 0xff) | 0x80);
7437 if ((c = nextc(p)) != '-') {
7441 if (flags & ESCAPE_CONTROL) goto eof;
7442 if ((c = nextc(p))== '\\') {
7448 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
7452 else if (c == -1 || !ISASCII(c)) goto eof;
7454 int c2 = escaped_control_code(c);
7457 if (flags & ESCAPE_META) {
7458 WARN_SPACE_CHAR(c2, "\\M-");
7461 WARN_SPACE_CHAR(c2, "");
7465 if (flags & ESCAPE_META) {
7466 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7469 WARN_SPACE_CHAR(c2, "\\C-");
7473 else if (ISCNTRL(c)) goto eof;
7479 yyerror0("Invalid escape character syntax");
7489tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7491 int len = rb_enc_codelen(c, enc);
7492 rb_enc_mbcput(c, tokspace(p, len), enc);
7496tokadd_escape(struct parser_params *p, rb_encoding **encp)
7501 switch (c = nextc(p)) {
7503 return 0; /* just ignore */
7505 case '0': case '1': case '2': case '3': /* octal constant */
7506 case '4': case '5': case '6': case '7':
7508 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7509 if (numlen == 0) goto eof;
7510 p->lex.pcur += numlen;
7511 tokcopy(p, (int)numlen + 1);
7515 case 'x': /* hex constant */
7517 tok_hex(p, &numlen);
7518 if (numlen == 0) return -1;
7519 tokcopy(p, (int)numlen + 2);
7525 yyerror0("Invalid escape character syntax");
7537regx_options(struct parser_params *p)
7545 while (c = nextc(p), ISALPHA(c)) {
7547 options |= RE_OPTION_ONCE;
7549 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7551 if (kc != rb_ascii8bit_encindex()) kcode = c;
7565 YYLTYPE loc = RUBY_INIT_YYLLOC();
7567 compile_error(p, "unknown regexp option%s - %*s",
7568 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7569 parser_show_error_line(p, &loc);
7571 return options | RE_OPTION_ENCODING(kcode);
7575tokadd_mbchar(struct parser_params *p, int c)
7577 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7578 if (len < 0) return -1;
7580 p->lex.pcur += --len;
7581 if (len > 0) tokcopy(p, len);
7586simple_re_meta(int c)
7589 case '$': case '*': case '+': case '.':
7590 case '?': case '^': case '|':
7591 case ')': case ']': case '}': case '>':
7599parser_update_heredoc_indent(struct parser_params *p, int c)
7601 if (p->heredoc_line_indent == -1) {
7602 if (c == '\n') p->heredoc_line_indent = 0;
7606 p->heredoc_line_indent++;
7609 else if (c == '\t') {
7610 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7611 p->heredoc_line_indent = w * TAB_WIDTH;
7614 else if (c != '\n') {
7615 if (p->heredoc_indent > p->heredoc_line_indent) {
7616 p->heredoc_indent = p->heredoc_line_indent;
7618 p->heredoc_line_indent = -1;
7625parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7627 YYLTYPE loc = RUBY_INIT_YYLLOC();
7628 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7629 compile_error(p, "%s mixed within %s source", n1, n2);
7630 parser_show_error_line(p, &loc);
7634parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7636 const char *pos = p->lex.pcur;
7638 parser_mixed_error(p, enc1, enc2);
7643tokadd_string(struct parser_params *p,
7644 int func, int term, int paren, long *nest,
7645 rb_encoding **encp, rb_encoding **enc)
7650 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
7651 int top_of_line = FALSE;
7654#define mixed_error(enc1, enc2) \
7655 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7656#define mixed_escape(beg, enc1, enc2) \
7657 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7659 while ((c = nextc(p)) != -1) {
7660 if (p->heredoc_indent > 0) {
7661 parser_update_heredoc_indent(p, c);
7664 if (top_of_line && heredoc_end == p->ruby_sourceline) {
7670 if (paren && c == paren) {
7673 else if (c == term) {
7674 if (!nest || !*nest) {
7680 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7681 unsigned char c2 = *p->lex.pcur;
7682 if (c2 == '$' || c2 == '@' || c2 == '{') {
7687 else if (c == '\\') {
7691 if (func & STR_FUNC_QWORDS) break;
7692 if (func & STR_FUNC_EXPAND) {
7693 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7704 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7708 if ((func & STR_FUNC_EXPAND) == 0) {
7712 tokadd_utf8(p, enc, term,
7713 func & STR_FUNC_SYMBOL,
7714 func & STR_FUNC_REGEXP);
7718 if (c == -1) return -1;
7720 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7723 if (func & STR_FUNC_REGEXP) {
7729 c = read_escape(p, 0, enc);
7733 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7734 for (i = 0; i < 4; i++) {
7735 tokadd(p, escbuf[i]);
7741 if (c == term && !simple_re_meta(c)) {
7746 if ((c = tokadd_escape(p, enc)) < 0)
7748 if (*enc && *enc != *encp) {
7749 mixed_escape(p->lex.ptok+2, *enc, *encp);
7753 else if (func & STR_FUNC_EXPAND) {
7755 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7756 c = read_escape(p, 0, enc);
7758 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7759 /* ignore backslashed spaces in %w */
7761 else if (c != term && !(paren && c == paren)) {
7768 else if (!parser_isascii(p)) {
7773 else if (*enc != *encp) {
7774 mixed_error(*enc, *encp);
7777 if (tokadd_mbchar(p, c) == -1) return -1;
7780 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7788 else if (*enc != *encp) {
7789 mixed_error(*enc, *encp);
7795 top_of_line = (c == '\n');
7799 if (*enc) *encp = *enc;
7803static inline rb_strterm_t *
7804new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7806 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7809/* imemo_parser_strterm for literal */
7810#define NEW_STRTERM(func, term, paren) \
7811 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7815flush_string_content(struct parser_params *p, rb_encoding *enc)
7817 VALUE content = yylval.val;
7818 if (!ripper_is_node_yylval(content))
7819 content = ripper_new_yylval(p, 0, 0, content);
7820 if (has_delayed_token(p)) {
7821 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7823 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7825 dispatch_delayed_token(p, tSTRING_CONTENT);
7826 p->lex.ptok = p->lex.pcur;
7827 RNODE(content)->nd_rval = yylval.val;
7829 dispatch_scan_event(p, tSTRING_CONTENT);
7830 if (yylval.val != content)
7831 RNODE(content)->nd_rval = yylval.val;
7832 yylval.val = content;
7836flush_string_content(struct parser_params *p, rb_encoding *enc)
7838 if (has_delayed_token(p)) {
7839 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7841 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7842 p->delayed.end_line = p->ruby_sourceline;
7843 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
7845 dispatch_delayed_token(p, tSTRING_CONTENT);
7846 p->lex.ptok = p->lex.pcur;
7848 dispatch_scan_event(p, tSTRING_CONTENT);
7852RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7853/* this can be shared with ripper, since it's independent from struct
7856#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7857#define SPECIAL_PUNCT(idx) ( \
7858 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7859 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7860 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7861 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7862 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7864const unsigned int ruby_global_name_punct_bits[] = {
7873static enum yytokentype
7874parser_peek_variable_name(struct parser_params *p)
7877 const char *ptr = p->lex.pcur;
7879 if (ptr + 1 >= p->lex.pend) return 0;
7883 if ((c = *ptr) == '-') {
7884 if (++ptr >= p->lex.pend) return 0;
7887 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7888 return tSTRING_DVAR;
7892 if ((c = *ptr) == '@') {
7893 if (++ptr >= p->lex.pend) return 0;
7899 p->command_start = TRUE;
7900 return tSTRING_DBEG;
7904 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7905 return tSTRING_DVAR;
7909#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7910#define IS_END() IS_lex_state(EXPR_END_ANY)
7911#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7912#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7913#define IS_LABEL_POSSIBLE() (\
7914 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7916#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7917#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7919static inline enum yytokentype
7920parser_string_term(struct parser_params *p, int func)
7923 if (func & STR_FUNC_REGEXP) {
7924 set_yylval_num(regx_options(p));
7925 dispatch_scan_event(p, tREGEXP_END);
7926 SET_LEX_STATE(EXPR_END);
7929 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7931 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7934 SET_LEX_STATE(EXPR_END);
7938static enum yytokentype
7939parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7941 int func = (int)quote->u1.func;
7942 int term = (int)quote->u3.term;
7943 int paren = (int)quote->u2.paren;
7945 rb_encoding *enc = p->enc;
7946 rb_encoding *base_enc = 0;
7949 if (func & STR_FUNC_TERM) {
7950 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7951 SET_LEX_STATE(EXPR_END);
7953 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7956 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7957 do {c = nextc(p);} while (ISSPACE(c));
7960 if (func & STR_FUNC_LIST) {
7961 quote->u1.func &= ~STR_FUNC_LIST;
7964 if (c == term && !quote->u0.nest) {
7965 if (func & STR_FUNC_QWORDS) {
7966 quote->u1.func |= STR_FUNC_TERM;
7967 pushback(p, c); /* dispatch the term at tSTRING_END */
7968 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7971 return parser_string_term(p, func);
7975 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7979 if ((func & STR_FUNC_EXPAND) && c == '#') {
7980 int t = parser_peek_variable_name(p);
7986 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7987 &enc, &base_enc) == -1) {
7990# define unterminated_literal(mesg) yyerror0(mesg)
7992# define unterminated_literal(mesg) compile_error(p, mesg)
7994 literal_flush(p, p->lex.pcur);
7995 if (func & STR_FUNC_QWORDS) {
7996 /* no content to add, bailing out here */
7997 unterminated_literal("unterminated list meets end of file");
8001 if (func & STR_FUNC_REGEXP) {
8002 unterminated_literal("unterminated regexp meets end of file");
8005 unterminated_literal("unterminated string meets end of file");
8007 quote->u1.func |= STR_FUNC_TERM;
8012 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8013 set_yylval_str(lit);
8014 flush_string_content(p, enc);
8016 return tSTRING_CONTENT;
8019static enum yytokentype
8020heredoc_identifier(struct parser_params *p)
8023 * term_len is length of `<<"END"` except `END`,
8024 * in this case term_len is 4 (<, <, " and ").
8026 long len, offset = p->lex.pcur - p->lex.pbeg;
8027 int c = nextc(p), term, func = 0, quote = 0;
8028 enum yytokentype token = tSTRING_BEG;
8033 func = STR_FUNC_INDENT;
8036 else if (c == '~') {
8038 func = STR_FUNC_INDENT;
8044 func |= str_squote; goto quoted;
8046 func |= str_dquote; goto quoted;
8048 token = tXSTRING_BEG;
8049 func |= str_xquote; goto quoted;
8056 while ((c = nextc(p)) != term) {
8057 if (c == -1 || c == '\r' || c == '\n') {
8058 yyerror0("unterminated here document identifier");
8065 if (!parser_is_identchar(p)) {
8067 if (func & STR_FUNC_INDENT) {
8068 pushback(p, indent > 0 ? '~' : '-');
8074 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8075 if (n < 0) return 0;
8077 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8082 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8083 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8084 yyerror0("too long here document identifier");
8085 dispatch_scan_event(p, tHEREDOC_BEG);
8088 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
8089 p->lex.strterm->flags |= STRTERM_HEREDOC;
8090 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8091 here->offset = offset;
8092 here->sourceline = p->ruby_sourceline;
8093 here->length = (int)len;
8094 here->quote = quote;
8098 p->heredoc_indent = indent;
8099 p->heredoc_line_indent = 0;
8104heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8109 line = here->lastline;
8110 p->lex.lastline = line;
8111 p->lex.pbeg = RSTRING_PTR(line);
8112 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8113 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8114 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8115 p->heredoc_end = p->ruby_sourceline;
8116 p->ruby_sourceline = (int)here->sourceline;
8117 if (p->eofp) p->lex.nextline = Qnil;
8122dedent_string(VALUE string, int width)
8128 RSTRING_GETMEM(string, str, len);
8129 for (i = 0; i < len && col < width; i++) {
8130 if (str[i] == ' ') {
8133 else if (str[i] == '\t') {
8134 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8135 if (n > width) break;
8143 rb_str_modify(string);
8144 str = RSTRING_PTR(string);
8145 if (RSTRING_LEN(string) != len)
8146 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8147 MEMMOVE(str, str + i, char, len - i);
8148 rb_str_set_len(string, len - i);
8154heredoc_dedent(struct parser_params *p, NODE *root)
8156 NODE *node, *str_node, *prev_node;
8157 int indent = p->heredoc_indent;
8160 if (indent <= 0) return root;
8161 p->heredoc_indent = 0;
8162 if (!root) return root;
8164 prev_node = node = str_node = root;
8165 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
8168 VALUE lit = str_node->nd_lit;
8169 if (str_node->flags & NODE_FL_NEWLINE) {
8170 dedent_string(lit, indent);
8175 else if (!literal_concat0(p, prev_lit, lit)) {
8179 NODE *end = node->nd_end;
8180 node = prev_node->nd_next = node->nd_next;
8182 if (nd_type_p(prev_node, NODE_DSTR))
8183 nd_set_type(prev_node, NODE_STR);
8191 while ((node = (prev_node = node)->nd_next) != 0) {
8193 if (!nd_type_p(node, NODE_LIST)) break;
8194 if ((str_node = node->nd_head) != 0) {
8195 enum node_type type = nd_type(str_node);
8196 if (type == NODE_STR || type == NODE_DSTR) break;
8206heredoc_dedent(struct parser_params *p, VALUE array)
8208 int indent = p->heredoc_indent;
8210 if (indent <= 0) return array;
8211 p->heredoc_indent = 0;
8212 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8218 * Ripper.dedent_string(input, width) -> Integer
8220 * USE OF RIPPER LIBRARY ONLY.
8222 * Strips up to +width+ leading whitespaces from +input+,
8223 * and returns the stripped column width.
8226parser_dedent_string(VALUE self, VALUE input, VALUE width)
8231 wid = NUM2UINT(width);
8232 col = dedent_string(input, wid);
8233 return INT2NUM(col);
8238whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8240 const char *ptr = p->lex.pbeg;
8244 while (*ptr && ISSPACE(*ptr)) ptr++;
8246 n = p->lex.pend - (ptr + len);
8247 if (n < 0) return FALSE;
8248 if (n > 0 && ptr[len] != '\n') {
8249 if (ptr[len] != '\r') return FALSE;
8250 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
8252 return strncmp(eos, ptr, len) == 0;
8256word_match_p(struct parser_params *p, const char *word, long len)
8258 if (strncmp(p->lex.pcur, word, len)) return 0;
8259 if (p->lex.pcur + len == p->lex.pend) return 1;
8260 int c = (unsigned char)p->lex.pcur[len];
8261 if (ISSPACE(c)) return 1;
8263 case '\0': case '\004': case '\032': return 1;
8268#define NUM_SUFFIX_R (1<<0)
8269#define NUM_SUFFIX_I (1<<1)
8270#define NUM_SUFFIX_ALL 3
8273number_literal_suffix(struct parser_params *p, int mask)
8276 const char *lastp = p->lex.pcur;
8278 while ((c = nextc(p)) != -1) {
8279 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8280 result |= (mask & NUM_SUFFIX_I);
8281 mask &= ~NUM_SUFFIX_I;
8282 /* r after i, rational of complex is disallowed */
8283 mask &= ~NUM_SUFFIX_R;
8286 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8287 result |= (mask & NUM_SUFFIX_R);
8288 mask &= ~NUM_SUFFIX_R;
8291 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8292 p->lex.pcur = lastp;
8293 literal_flush(p, p->lex.pcur);
8302static enum yytokentype
8303set_number_literal(struct parser_params *p, VALUE v,
8304 enum yytokentype type, int suffix)
8306 if (suffix & NUM_SUFFIX_I) {
8307 v = rb_complex_raw(INT2FIX(0), v);
8310 set_yylval_literal(v);
8311 SET_LEX_STATE(EXPR_END);
8315static enum yytokentype
8316set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8318 enum yytokentype type = tINTEGER;
8319 if (suffix & NUM_SUFFIX_R) {
8320 v = rb_rational_raw1(v);
8323 return set_number_literal(p, v, type, suffix);
8328dispatch_heredoc_end(struct parser_params *p)
8331 if (has_delayed_token(p))
8332 dispatch_delayed_token(p, tSTRING_CONTENT);
8333 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8334 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8335 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8341#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8343parser_dispatch_heredoc_end(struct parser_params *p, int line)
8345 if (has_delayed_token(p))
8346 dispatch_delayed_token(p, tSTRING_CONTENT);
8348 if (p->keep_tokens) {
8349 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8350 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8351 parser_append_tokens(p, str, tHEREDOC_END, line);
8354 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8360static enum yytokentype
8361here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8363 int c, func, indent = 0;
8364 const char *eos, *ptr, *ptr_end;
8367 rb_encoding *enc = p->enc;
8368 rb_encoding *base_enc = 0;
8371 eos = RSTRING_PTR(here->lastline) + here->offset;
8373 indent = (func = here->func) & STR_FUNC_INDENT;
8375 if ((c = nextc(p)) == -1) {
8378 if (!has_delayed_token(p)) {
8379 dispatch_scan_event(p, tSTRING_CONTENT);
8382 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
8383 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
8384 int cr = ENC_CODERANGE_UNKNOWN;
8385 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
8386 if (cr != ENC_CODERANGE_7BIT &&
8387 rb_is_usascii_enc(p->enc) &&
8388 enc != rb_utf8_encoding()) {
8389 enc = rb_ascii8bit_encoding();
8392 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8394 dispatch_delayed_token(p, tSTRING_CONTENT);
8398 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8399 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
8403 SET_LEX_STATE(EXPR_END);
8408 /* not beginning of line, cannot be the terminator */
8410 else if (p->heredoc_line_indent == -1) {
8411 /* `heredoc_line_indent == -1` means
8412 * - "after an interpolation in the same line", or
8413 * - "in a continuing line"
8415 p->heredoc_line_indent = 0;
8417 else if (whole_match_p(p, eos, len, indent)) {
8418 dispatch_heredoc_end(p);
8420 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8423 SET_LEX_STATE(EXPR_END);
8427 if (!(func & STR_FUNC_EXPAND)) {
8429 ptr = RSTRING_PTR(p->lex.lastline);
8430 ptr_end = p->lex.pend;
8431 if (ptr_end > ptr) {
8432 switch (ptr_end[-1]) {
8434 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
8443 if (p->heredoc_indent > 0) {
8445 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
8447 p->heredoc_line_indent = 0;
8451 rb_str_cat(str, ptr, ptr_end - ptr);
8453 str = STR_NEW(ptr, ptr_end - ptr);
8454 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
8456 if (p->heredoc_indent > 0) {
8459 if (nextc(p) == -1) {
8465 } while (!whole_match_p(p, eos, len, indent));
8468 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
8471 int t = parser_peek_variable_name(p);
8472 if (p->heredoc_line_indent != -1) {
8473 if (p->heredoc_indent > p->heredoc_line_indent) {
8474 p->heredoc_indent = p->heredoc_line_indent;
8476 p->heredoc_line_indent = -1;
8485 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
8486 if (p->eofp) goto error;
8490 if (c == '\\') p->heredoc_line_indent = -1;
8492 str = STR_NEW3(tok(p), toklen(p), enc, func);
8494 set_yylval_str(str);
8496 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8498 flush_string_content(p, enc);
8499 return tSTRING_CONTENT;
8501 tokadd(p, nextc(p));
8502 if (p->heredoc_indent > 0) {
8506 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8507 if ((c = nextc(p)) == -1) goto error;
8508 } while (!whole_match_p(p, eos, len, indent));
8509 str = STR_NEW3(tok(p), toklen(p), enc, func);
8511 dispatch_heredoc_end(p);
8513 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8516 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8518 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8519 set_yylval_str(str);
8521 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8523 return tSTRING_CONTENT;
8529arg_ambiguous(struct parser_params *p, char c)
8533 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8536 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8539 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8546formal_argument(struct parser_params *p, ID lhs)
8548formal_argument(struct parser_params *p, VALUE lhs)
8551 ID id = get_id(lhs);
8553 switch (id_type(id)) {
8557# define ERR(mesg) yyerror0(mesg)
8559# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8562 ERR("formal argument cannot be a constant");
8565 ERR("formal argument cannot be an instance variable");
8568 ERR("formal argument cannot be a global variable");
8571 ERR("formal argument cannot be a class variable");
8574 ERR("formal argument must be local variable");
8578 shadowing_lvar(p, id);
8583lvar_defined(struct parser_params *p, ID id)
8585 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8588/* emacsen -*- hack */
8590parser_encode_length(struct parser_params *p, const char *name, long len)
8594 if (len > 5 && name[nlen = len - 5] == '-') {
8595 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8598 if (len > 4 && name[nlen = len - 4] == '-') {
8599 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8601 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8602 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8603 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8610parser_set_encode(struct parser_params *p, const char *name)
8612 int idx = rb_enc_find_index(name);
8617 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8619 excargs[0] = rb_eArgError;
8620 excargs[2] = rb_make_backtrace();
8621 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8622 rb_exc_raise(rb_make_exception(3, excargs));
8624 enc = rb_enc_from_index(idx);
8625 if (!rb_enc_asciicompat(enc)) {
8626 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8631 if (p->debug_lines) {
8632 VALUE lines = p->debug_lines;
8633 long i, n = RARRAY_LEN(lines);
8634 for (i = 0; i < n; ++i) {
8635 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8642comment_at_top(struct parser_params *p)
8644 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8645 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8646 while (ptr < ptr_end) {
8647 if (!ISSPACE(*ptr)) return 0;
8653typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8654typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8656static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8659magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8661 if (!comment_at_top(p)) {
8664 parser_set_encode(p, val);
8668parser_get_bool(struct parser_params *p, const char *name, const char *val)
8672 if (STRCASECMP(val, "true") == 0) {
8677 if (STRCASECMP(val, "false") == 0) {
8682 return parser_invalid_pragma_value(p, name, val);
8686parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8688 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8693parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8695 int b = parser_get_bool(p, name, val);
8696 if (b >= 0) p->token_info_enabled = b;
8700parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8704 if (p->token_seen) {
8705 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8709 b = parser_get_bool(p, name, val);
8712 if (!p->compile_option)
8713 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8714 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8719parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8721 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8722 if (*s == ' ' || *s == '\t') continue;
8723 if (*s == '#') break;
8724 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8730 if (STRCASECMP(val, "none") == 0) {
8731 p->ctxt.shareable_constant_value = shareable_none;
8736 if (STRCASECMP(val, "literal") == 0) {
8737 p->ctxt.shareable_constant_value = shareable_literal;
8742 if (STRCASECMP(val, "experimental_copy") == 0) {
8743 p->ctxt.shareable_constant_value = shareable_copy;
8746 if (STRCASECMP(val, "experimental_everything") == 0) {
8747 p->ctxt.shareable_constant_value = shareable_everything;
8752 parser_invalid_pragma_value(p, name, val);
8757parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8759 int b = parser_get_bool(p, name, val);
8760 if (b >= 0) p->past_scope_enabled = b;
8764struct magic_comment {
8766 rb_magic_comment_setter_t func;
8767 rb_magic_comment_length_t length;
8770static const struct magic_comment magic_comments[] = {
8771 {"coding", magic_comment_encoding, parser_encode_length},
8772 {"encoding", magic_comment_encoding, parser_encode_length},
8773 {"frozen_string_literal", parser_set_compile_option_flag},
8774 {"shareable_constant_value", parser_set_shareable_constant_value},
8775 {"warn_indent", parser_set_token_info},
8777 {"warn_past_scope", parser_set_past_scope},
8782magic_comment_marker(const char *str, long len)
8789 if (str[i-1] == '*' && str[i-2] == '-') {
8795 if (i + 1 >= len) return 0;
8796 if (str[i+1] != '-') {
8799 else if (str[i-1] != '-') {
8815parser_magic_comment(struct parser_params *p, const char *str, long len)
8818 VALUE name = 0, val = 0;
8819 const char *beg, *end, *vbeg, *vend;
8820#define str_copy(_s, _p, _n) ((_s) \
8821 ? (void)(rb_str_resize((_s), (_n)), \
8822 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8823 : (void)((_s) = STR_NEW((_p), (_n))))
8825 if (len <= 7) return FALSE;
8826 if (!!(beg = magic_comment_marker(str, len))) {
8827 if (!(end = magic_comment_marker(beg, str + len - beg)))
8831 len = end - beg - 3;
8834 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8836 const struct magic_comment *mc = magic_comments;
8841 for (; len > 0 && *str; str++, --len) {
8843 case '\'': case '"': case ':': case ';':
8846 if (!ISSPACE(*str)) break;
8848 for (beg = str; len > 0; str++, --len) {
8850 case '\'': case '"': case ':': case ';':
8853 if (ISSPACE(*str)) break;
8858 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8861 if (!indicator) return FALSE;
8865 do str++; while (--len > 0 && ISSPACE(*str));
8868 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8881 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8885 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8888 while (len > 0 && (ISSPACE(*str))) --len, str++;
8889 if (len) return FALSE;
8893 str_copy(name, beg, n);
8894 s = RSTRING_PTR(name);
8895 for (i = 0; i < n; ++i) {
8896 if (s[i] == '-') s[i] = '_';
8899 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8902 n = (*mc->length)(p, vbeg, n);
8904 str_copy(val, vbeg, n);
8905 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8908 } while (++mc < magic_comments + numberof(magic_comments));
8910 str_copy(val, vbeg, vend - vbeg);
8911 dispatch2(magic_comment, name, val);
8919set_file_encoding(struct parser_params *p, const char *str, const char *send)
8922 const char *beg = str;
8926 if (send - str <= 6) return;
8928 case 'C': case 'c': str += 6; continue;
8929 case 'O': case 'o': str += 5; continue;
8930 case 'D': case 'd': str += 4; continue;
8931 case 'I': case 'i': str += 3; continue;
8932 case 'N': case 'n': str += 2; continue;
8933 case 'G': case 'g': str += 1; continue;
8940 if (ISSPACE(*str)) break;
8943 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8948 if (++str >= send) return;
8949 } while (ISSPACE(*str));
8951 if (*str != '=' && *str != ':') return;
8956 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8957 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8958 parser_set_encode(p, RSTRING_PTR(s));
8959 rb_str_resize(s, 0);
8963parser_prepare(struct parser_params *p)
8965 int c = nextc0(p, FALSE);
8966 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8969 if (peek(p, '!')) p->has_shebang = 1;
8971 case 0xef: /* UTF-8 BOM marker */
8972 if (p->lex.pend - p->lex.pcur >= 2 &&
8973 (unsigned char)p->lex.pcur[0] == 0xbb &&
8974 (unsigned char)p->lex.pcur[1] == 0xbf) {
8975 p->enc = rb_utf8_encoding();
8978 if (p->debug_lines) {
8979 rb_enc_associate(p->lex.lastline, p->enc);
8982 p->lex.pbeg = p->lex.pcur;
8990 p->enc = rb_enc_get(p->lex.lastline);
8994#define ambiguous_operator(tok, op, syn) ( \
8995 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8996 rb_warning0("even though it seems like "syn""))
8998#define ambiguous_operator(tok, op, syn) \
8999 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9001#define warn_balanced(tok, op, syn) ((void) \
9002 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9003 space_seen && !ISSPACE(c) && \
9004 (ambiguous_operator(tok, op, syn), 0)), \
9005 (enum yytokentype)(tok))
9008parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9011 char *point = &str[seen_point];
9012 size_t fraclen = len-seen_point-1;
9013 memmove(point, point+1, fraclen+1);
9014 v = rb_cstr_to_inum(str, 10, FALSE);
9015 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9018static enum yytokentype
9019no_digits(struct parser_params *p)
9021 yyerror0("numeric literal without digits");
9022 if (peek(p, '_')) nextc(p);
9023 /* dummy 0, for tUMINUS_NUM at numeric */
9024 return set_integer_literal(p, INT2FIX(0), 0);
9027static enum yytokentype
9028parse_numeric(struct parser_params *p, int c)
9030 int is_float, seen_point, seen_e, nondigit;
9033 is_float = seen_point = seen_e = nondigit = 0;
9034 SET_LEX_STATE(EXPR_END);
9036 if (c == '-' || c == '+') {
9041 int start = toklen(p);
9043 if (c == 'x' || c == 'X') {
9046 if (c != -1 && ISXDIGIT(c)) {
9049 if (nondigit) break;
9053 if (!ISXDIGIT(c)) break;
9056 } while ((c = nextc(p)) != -1);
9060 if (toklen(p) == start) {
9061 return no_digits(p);
9063 else if (nondigit) goto trailing_uc;
9064 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9065 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9067 if (c == 'b' || c == 'B') {
9070 if (c == '0' || c == '1') {
9073 if (nondigit) break;
9077 if (c != '0' && c != '1') break;
9080 } while ((c = nextc(p)) != -1);
9084 if (toklen(p) == start) {
9085 return no_digits(p);
9087 else if (nondigit) goto trailing_uc;
9088 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9089 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9091 if (c == 'd' || c == 'D') {
9094 if (c != -1 && ISDIGIT(c)) {
9097 if (nondigit) break;
9101 if (!ISDIGIT(c)) break;
9104 } while ((c = nextc(p)) != -1);
9108 if (toklen(p) == start) {
9109 return no_digits(p);
9111 else if (nondigit) goto trailing_uc;
9112 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9113 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9119 if (c == 'o' || c == 'O') {
9120 /* prefixed octal */
9122 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9123 return no_digits(p);
9126 if (c >= '0' && c <= '7') {
9131 if (nondigit) break;
9135 if (c < '0' || c > '9') break;
9136 if (c > '7') goto invalid_octal;
9139 } while ((c = nextc(p)) != -1);
9140 if (toklen(p) > start) {
9143 if (nondigit) goto trailing_uc;
9144 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9145 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9152 if (c > '7' && c <= '9') {
9154 yyerror0("Invalid octal digit");
9156 else if (c == '.' || c == 'e' || c == 'E') {
9161 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9162 return set_integer_literal(p, INT2FIX(0), suffix);
9168 case '0': case '1': case '2': case '3': case '4':
9169 case '5': case '6': case '7': case '8': case '9':
9175 if (nondigit) goto trailing_uc;
9176 if (seen_point || seen_e) {
9181 if (c0 == -1 || !ISDIGIT(c0)) {
9187 seen_point = toklen(p);
9206 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9211 tokadd(p, nondigit);
9215 nondigit = (c == '-' || c == '+') ? c : 0;
9218 case '_': /* `_' in number just ignored */
9219 if (nondigit) goto decode_num;
9233 literal_flush(p, p->lex.pcur - 1);
9234 YYLTYPE loc = RUBY_INIT_YYLLOC();
9235 compile_error(p, "trailing `%c' in number", nondigit);
9236 parser_show_error_line(p, &loc);
9240 enum yytokentype type = tFLOAT;
9243 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9244 if (suffix & NUM_SUFFIX_R) {
9246 v = parse_rational(p, tok(p), toklen(p), seen_point);
9249 double d = strtod(tok(p), 0);
9250 if (errno == ERANGE) {
9251 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9256 return set_number_literal(p, v, type, suffix);
9258 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9259 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9262static enum yytokentype
9263parse_qmark(struct parser_params *p, int space_seen)
9270 SET_LEX_STATE(EXPR_VALUE);
9275 compile_error(p, "incomplete character syntax");
9278 if (rb_enc_isspace(c, p->enc)) {
9280 int c2 = escaped_control_code(c);
9282 WARN_SPACE_CHAR(c2, "?");
9287 SET_LEX_STATE(EXPR_VALUE);
9292 if (!parser_isascii(p)) {
9293 if (tokadd_mbchar(p, c) == -1) return 0;
9295 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9296 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
9298 const char *start = p->lex.pcur - 1, *ptr = start;
9300 int n = parser_precise_mbclen(p, ptr);
9301 if (n < 0) return -1;
9303 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
9304 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9305 " a conditional operator, put a space after `?'",
9306 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9310 else if (c == '\\') {
9313 enc = rb_utf8_encoding();
9314 tokadd_utf8(p, &enc, -1, 0, 0);
9316 else if (!ISASCII(c = peekc(p))) {
9318 if (tokadd_mbchar(p, c) == -1) return 0;
9321 c = read_escape(p, 0, &enc);
9329 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9330 set_yylval_str(lit);
9331 SET_LEX_STATE(EXPR_END);
9335static enum yytokentype
9336parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9339 const char *ptok = p->lex.pcur;
9347 if (c == -1) goto unterminated;
9350 if (!ISASCII(c)) goto unknown;
9355 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9358 c = parser_precise_mbclen(p, p->lex.pcur);
9359 if (c < 0) return 0;
9361 yyerror0("unknown type of %string");
9367 compile_error(p, "unterminated quoted string meets end of file");
9371 if (term == '(') term = ')';
9372 else if (term == '[') term = ']';
9373 else if (term == '{') term = '}';
9374 else if (term == '<') term = '>';
9377 p->lex.ptok = ptok-1;
9380 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9384 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
9388 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9392 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9396 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9397 return tSYMBOLS_BEG;
9400 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9401 return tQSYMBOLS_BEG;
9404 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
9405 return tXSTRING_BEG;
9408 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
9412 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
9413 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
9417 yyerror0("unknown type of %string");
9421 if ((c = nextc(p)) == '=') {
9423 SET_LEX_STATE(EXPR_BEG);
9426 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
9429 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9431 return warn_balanced('%', "%%", "string literal");
9435tokadd_ident(struct parser_params *p, int c)
9438 if (tokadd_mbchar(p, c) == -1) return -1;
9440 } while (parser_is_identchar(p));
9446tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
9448 ID ident = TOK_INTERN();
9450 set_yylval_name(ident);
9456parse_numvar(struct parser_params *p)
9460 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
9461 const unsigned long nth_ref_max =
9462 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
9463 /* NTH_REF is left-shifted to be ORed with back-ref flag and
9464 * turned into a Fixnum, in compile.c */
9466 if (overflow || n > nth_ref_max) {
9467 /* compile_error()? */
9468 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
9469 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
9476static enum yytokentype
9477parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
9479 const char *ptr = p->lex.pcur;
9482 SET_LEX_STATE(EXPR_END);
9483 p->lex.ptok = ptr - 1; /* from '$' */
9487 case '_': /* $_: last read line string */
9489 if (parser_is_identchar(p)) {
9497 case '~': /* $~: match-data */
9498 case '*': /* $*: argv */
9499 case '$': /* $$: pid */
9500 case '?': /* $?: last status */
9501 case '!': /* $!: error string */
9502 case '@': /* $@: error position */
9503 case '/': /* $/: input record separator */
9504 case '\\': /* $\: output record separator */
9505 case ';': /* $;: field separator */
9506 case ',': /* $,: output field separator */
9507 case '.': /* $.: last read line number */
9508 case '=': /* $=: ignorecase */
9509 case ':': /* $:: load path */
9510 case '<': /* $<: reading filename */
9511 case '>': /* $>: default output handle */
9512 case '\"': /* $": already loaded files */
9521 if (parser_is_identchar(p)) {
9522 if (tokadd_mbchar(p, c) == -1) return 0;
9530 set_yylval_name(TOK_INTERN());
9533 case '&': /* $&: last match */
9534 case '`': /* $`: string before last match */
9535 case '\'': /* $': string after last match */
9536 case '+': /* $+: string matches last paren. */
9537 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9542 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9545 case '1': case '2': case '3':
9546 case '4': case '5': case '6':
9547 case '7': case '8': case '9':
9552 } while (c != -1 && ISDIGIT(c));
9554 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9556 c = parse_numvar(p);
9557 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9561 if (!parser_is_identchar(p)) {
9562 YYLTYPE loc = RUBY_INIT_YYLLOC();
9563 if (c == -1 || ISSPACE(c)) {
9564 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9568 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9570 parser_show_error_line(p, &loc);
9571 set_yylval_noname();
9579 if (tokadd_ident(p, c)) return 0;
9580 SET_LEX_STATE(EXPR_END);
9581 tokenize_ident(p, last_state);
9587parser_numbered_param(struct parser_params *p, int n)
9589 if (n < 0) return false;
9591 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9594 if (p->max_numparam == ORDINAL_PARAM) {
9595 compile_error(p, "ordinary parameter is defined");
9598 struct vtable *args = p->lvtbl->args;
9599 if (p->max_numparam < n) {
9600 p->max_numparam = n;
9602 while (n > args->pos) {
9603 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9609static enum yytokentype
9610parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9612 const char *ptr = p->lex.pcur;
9613 enum yytokentype result = tIVAR;
9614 register int c = nextc(p);
9617 p->lex.ptok = ptr - 1; /* from '@' */
9625 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9626 if (c == -1 || !parser_is_identchar(p)) {
9628 RUBY_SET_YYLLOC(loc);
9629 if (result == tIVAR) {
9630 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9633 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9635 parser_show_error_line(p, &loc);
9636 set_yylval_noname();
9637 SET_LEX_STATE(EXPR_END);
9640 else if (ISDIGIT(c)) {
9642 RUBY_SET_YYLLOC(loc);
9643 if (result == tIVAR) {
9644 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9647 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9649 parser_show_error_line(p, &loc);
9650 set_yylval_noname();
9651 SET_LEX_STATE(EXPR_END);
9655 if (tokadd_ident(p, c)) return 0;
9656 tokenize_ident(p, last_state);
9660static enum yytokentype
9661parse_ident(struct parser_params *p, int c, int cmd_state)
9663 enum yytokentype result;
9664 int mb = ENC_CODERANGE_7BIT;
9665 const enum lex_state_e last_state = p->lex.state;
9667 int enforce_keyword_end = 0;
9670 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9671 if (tokadd_mbchar(p, c) == -1) return 0;
9673 } while (parser_is_identchar(p));
9674 if ((c == '!' || c == '?') && !peek(p, '=')) {
9678 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9679 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9680 result = tIDENTIFIER;
9684 result = tCONSTANT; /* assume provisionally */
9689 if (IS_LABEL_POSSIBLE()) {
9690 if (IS_LABEL_SUFFIX(0)) {
9691 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9693 set_yylval_name(TOK_INTERN());
9699 if (!NIL_P(peek_end_expect_token_locations(p))) {
9702 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
9704 end_loc = peek_end_expect_token_locations(p);
9705 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
9706 column = NUM2INT(rb_ary_entry(end_loc, 1));
9709 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
9710 p->ruby_sourceline, beg_pos, lineno, column);
9713 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
9714 const struct kwtable *kw;
9716 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
9717 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
9718 enforce_keyword_end = 1;
9724 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
9725 const struct kwtable *kw;
9727 /* See if it is a reserved word. */
9728 kw = rb_reserved_word(tok(p), toklen(p));
9730 enum lex_state_e state = p->lex.state;
9731 if (IS_lex_state_for(state, EXPR_FNAME)) {
9732 SET_LEX_STATE(EXPR_ENDFN);
9733 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9736 SET_LEX_STATE(kw->state);
9737 if (IS_lex_state(EXPR_BEG)) {
9738 p->command_start = TRUE;
9740 if (kw->id[0] == keyword_do) {
9741 if (lambda_beginning_p()) {
9742 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9743 return keyword_do_LAMBDA;
9745 if (COND_P()) return keyword_do_cond;
9746 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9747 return keyword_do_block;
9750 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
9753 if (kw->id[0] != kw->id[1])
9754 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9760 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9762 SET_LEX_STATE(EXPR_CMDARG);
9765 SET_LEX_STATE(EXPR_ARG);
9768 else if (p->lex.state == EXPR_FNAME) {
9769 SET_LEX_STATE(EXPR_ENDFN);
9772 SET_LEX_STATE(EXPR_END);
9775 ident = tokenize_ident(p, last_state);
9776 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9777 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9778 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9779 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
9780 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9786warn_cr(struct parser_params *p)
9790 /* carried over with p->lex.nextline for nextc() */
9791 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9795static enum yytokentype
9796parser_yylex(struct parser_params *p)
9802 enum lex_state_e last_state;
9803 int fallthru = FALSE;
9804 int token_seen = p->token_seen;
9806 if (p->lex.strterm) {
9807 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9809 return here_document(p, &p->lex.strterm->u.heredoc);
9813 return parse_string(p, &p->lex.strterm->u.literal);
9816 cmd_state = p->command_start;
9817 p->command_start = FALSE;
9818 p->token_seen = TRUE;
9823 last_state = p->lex.state;
9824 switch (c = nextc(p)) {
9825 case '\0': /* NUL */
9826 case '\004': /* ^D */
9827 case '\032': /* ^Z */
9828 case -1: /* end of script. */
9831 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
9832 pop_end_expect_token_locations(p);
9833 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
9837 /* Set location for end-of-input because dispatch_scan_event is not called. */
9838 RUBY_SET_YYLLOC(*p->yylloc);
9845 case ' ': case '\t': case '\f':
9846 case '\13': /* '\v' */
9848 while ((c = nextc(p))) {
9853 case ' ': case '\t': case '\f':
9854 case '\13': /* '\v' */
9862 dispatch_scan_event(p, tSP);
9868 case '#': /* it's a comment */
9869 p->token_seen = token_seen;
9870 /* no magic_comment in shebang line */
9871 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9872 if (comment_at_top(p)) {
9873 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9877 dispatch_scan_event(p, tCOMMENT);
9881 p->token_seen = token_seen;
9882 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9883 !IS_lex_state(EXPR_LABELED));
9884 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9886 dispatch_scan_event(p, tIGNORED_NL);
9889 if (!c && p->ctxt.in_kwarg) {
9890 goto normal_newline;
9895 switch (c = nextc(p)) {
9896 case ' ': case '\t': case '\f': case '\r':
9897 case '\13': /* '\v' */
9903 dispatch_scan_event(p, tSP);
9909 dispatch_delayed_token(p, tIGNORED_NL);
9910 if (peek(p, '.') == (c == '&')) {
9912 dispatch_scan_event(p, tSP);
9917 p->ruby_sourceline--;
9918 p->lex.nextline = p->lex.lastline;
9919 case -1: /* EOF no decrement*/
9922 p->lex.ptok = p->lex.pcur;
9924 goto normal_newline;
9928 p->command_start = TRUE;
9929 SET_LEX_STATE(EXPR_BEG);
9933 if ((c = nextc(p)) == '*') {
9934 if ((c = nextc(p)) == '=') {
9935 set_yylval_id(idPow);
9936 SET_LEX_STATE(EXPR_BEG);
9941 rb_warning0("`**' interpreted as argument prefix");
9944 else if (IS_BEG()) {
9948 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9954 SET_LEX_STATE(EXPR_BEG);
9959 rb_warning0("`*' interpreted as argument prefix");
9962 else if (IS_BEG()) {
9966 c = warn_balanced('*', "*", "argument prefix");
9969 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9974 if (IS_AFTER_OPERATOR()) {
9975 SET_LEX_STATE(EXPR_ARG);
9981 SET_LEX_STATE(EXPR_BEG);
9994 /* skip embedded rd document */
9995 if (word_match_p(p, "begin", 5)) {
9999 dispatch_scan_event(p, tEMBDOC_BEG);
10003 dispatch_scan_event(p, tEMBDOC);
10008 compile_error(p, "embedded document meets end of file");
10011 if (c == '=' && word_match_p(p, "end", 3)) {
10017 dispatch_scan_event(p, tEMBDOC_END);
10022 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10023 if ((c = nextc(p)) == '=') {
10024 if ((c = nextc(p)) == '=') {
10033 else if (c == '>') {
10042 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10044 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10045 int token = heredoc_identifier(p);
10046 if (token) return token < 0 ? 0 : token;
10048 if (IS_AFTER_OPERATOR()) {
10049 SET_LEX_STATE(EXPR_ARG);
10052 if (IS_lex_state(EXPR_CLASS))
10053 p->command_start = TRUE;
10054 SET_LEX_STATE(EXPR_BEG);
10057 if ((c = nextc(p)) == '>') {
10064 if ((c = nextc(p)) == '=') {
10065 set_yylval_id(idLTLT);
10066 SET_LEX_STATE(EXPR_BEG);
10070 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10076 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10077 if ((c = nextc(p)) == '=') {
10081 if ((c = nextc(p)) == '=') {
10082 set_yylval_id(idGTGT);
10083 SET_LEX_STATE(EXPR_BEG);
10093 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10094 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10095 p->lex.ptok = p->lex.pcur-1;
10096 return tSTRING_BEG;
10099 if (IS_lex_state(EXPR_FNAME)) {
10100 SET_LEX_STATE(EXPR_ENDFN);
10103 if (IS_lex_state(EXPR_DOT)) {
10105 SET_LEX_STATE(EXPR_CMDARG);
10107 SET_LEX_STATE(EXPR_ARG);
10110 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10111 return tXSTRING_BEG;
10114 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10115 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10116 p->lex.ptok = p->lex.pcur-1;
10117 return tSTRING_BEG;
10120 return parse_qmark(p, space_seen);
10123 if ((c = nextc(p)) == '&') {
10124 SET_LEX_STATE(EXPR_BEG);
10125 if ((c = nextc(p)) == '=') {
10126 set_yylval_id(idANDOP);
10127 SET_LEX_STATE(EXPR_BEG);
10133 else if (c == '=') {
10134 set_yylval_id('&');
10135 SET_LEX_STATE(EXPR_BEG);
10138 else if (c == '.') {
10139 set_yylval_id(idANDDOT);
10140 SET_LEX_STATE(EXPR_DOT);
10144 if (IS_SPCARG(c)) {
10146 (c = peekc_n(p, 1)) == -1 ||
10147 !(c == '\'' || c == '"' ||
10148 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
10149 rb_warning0("`&' interpreted as argument prefix");
10153 else if (IS_BEG()) {
10157 c = warn_balanced('&', "&", "argument prefix");
10159 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10163 if ((c = nextc(p)) == '|') {
10164 SET_LEX_STATE(EXPR_BEG);
10165 if ((c = nextc(p)) == '=') {
10166 set_yylval_id(idOROP);
10167 SET_LEX_STATE(EXPR_BEG);
10171 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10179 set_yylval_id('|');
10180 SET_LEX_STATE(EXPR_BEG);
10183 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10189 if (IS_AFTER_OPERATOR()) {
10190 SET_LEX_STATE(EXPR_ARG);
10198 set_yylval_id('+');
10199 SET_LEX_STATE(EXPR_BEG);
10202 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10203 SET_LEX_STATE(EXPR_BEG);
10205 if (c != -1 && ISDIGIT(c)) {
10206 return parse_numeric(p, '+');
10210 SET_LEX_STATE(EXPR_BEG);
10212 return warn_balanced('+', "+", "unary operator");
10216 if (IS_AFTER_OPERATOR()) {
10217 SET_LEX_STATE(EXPR_ARG);
10225 set_yylval_id('-');
10226 SET_LEX_STATE(EXPR_BEG);
10230 SET_LEX_STATE(EXPR_ENDFN);
10233 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10234 SET_LEX_STATE(EXPR_BEG);
10236 if (c != -1 && ISDIGIT(c)) {
10237 return tUMINUS_NUM;
10241 SET_LEX_STATE(EXPR_BEG);
10243 return warn_balanced('-', "-", "unary operator");
10246 int is_beg = IS_BEG();
10247 SET_LEX_STATE(EXPR_BEG);
10248 if ((c = nextc(p)) == '.') {
10249 if ((c = nextc(p)) == '.') {
10250 if (p->ctxt.in_argdef) {
10251 SET_LEX_STATE(EXPR_ENDARG);
10254 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10255 rb_warn0("... at EOL, should be parenthesized?");
10257 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10258 if (IS_lex_state_for(last_state, EXPR_LABEL))
10261 return is_beg ? tBDOT3 : tDOT3;
10264 return is_beg ? tBDOT2 : tDOT2;
10267 if (c != -1 && ISDIGIT(c)) {
10268 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10269 parse_numeric(p, '.');
10270 if (ISDIGIT(prev)) {
10271 yyerror0("unexpected fraction part after numeric literal");
10274 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10276 SET_LEX_STATE(EXPR_END);
10277 p->lex.ptok = p->lex.pcur;
10280 set_yylval_id('.');
10281 SET_LEX_STATE(EXPR_DOT);
10285 case '0': case '1': case '2': case '3': case '4':
10286 case '5': case '6': case '7': case '8': case '9':
10287 return parse_numeric(p, c);
10292 SET_LEX_STATE(EXPR_ENDFN);
10293 p->lex.paren_nest--;
10299 SET_LEX_STATE(EXPR_END);
10300 p->lex.paren_nest--;
10304 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10305 if (!p->lex.brace_nest--) return tSTRING_DEND;
10308 SET_LEX_STATE(EXPR_END);
10309 p->lex.paren_nest--;
10315 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10316 SET_LEX_STATE(EXPR_BEG);
10319 set_yylval_id(idCOLON2);
10320 SET_LEX_STATE(EXPR_DOT);
10323 if (IS_END() || ISSPACE(c) || c == '#') {
10325 c = warn_balanced(':', ":", "symbol literal");
10326 SET_LEX_STATE(EXPR_BEG);
10331 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10334 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10340 SET_LEX_STATE(EXPR_FNAME);
10345 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10346 return tREGEXP_BEG;
10348 if ((c = nextc(p)) == '=') {
10349 set_yylval_id('/');
10350 SET_LEX_STATE(EXPR_BEG);
10354 if (IS_SPCARG(c)) {
10355 arg_ambiguous(p, '/');
10356 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10357 return tREGEXP_BEG;
10359 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10360 return warn_balanced('/', "/", "regexp literal");
10363 if ((c = nextc(p)) == '=') {
10364 set_yylval_id('^');
10365 SET_LEX_STATE(EXPR_BEG);
10368 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10373 SET_LEX_STATE(EXPR_BEG);
10374 p->command_start = TRUE;
10378 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10382 if (IS_AFTER_OPERATOR()) {
10383 if ((c = nextc(p)) != '@') {
10386 SET_LEX_STATE(EXPR_ARG);
10389 SET_LEX_STATE(EXPR_BEG);
10397 else if (!space_seen) {
10398 /* foo( ... ) => method call, no ambiguity */
10400 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
10403 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
10404 rb_warning0("parentheses after method name is interpreted as "
10405 "an argument list, not a decomposed argument");
10407 p->lex.paren_nest++;
10410 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10414 p->lex.paren_nest++;
10415 if (IS_AFTER_OPERATOR()) {
10416 if ((c = nextc(p)) == ']') {
10417 p->lex.paren_nest--;
10418 SET_LEX_STATE(EXPR_ARG);
10419 if ((c = nextc(p)) == '=') {
10426 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
10429 else if (IS_BEG()) {
10432 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
10435 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10441 ++p->lex.brace_nest;
10442 if (lambda_beginning_p())
10444 else if (IS_lex_state(EXPR_LABELED))
10445 c = tLBRACE; /* hash */
10446 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
10447 c = '{'; /* block (primary) */
10448 else if (IS_lex_state(EXPR_ENDARG))
10449 c = tLBRACE_ARG; /* block (expr) */
10451 c = tLBRACE; /* hash */
10452 if (c != tLBRACE) {
10453 p->command_start = TRUE;
10454 SET_LEX_STATE(EXPR_BEG);
10457 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10459 ++p->lex.paren_nest; /* after lambda_beginning_p() */
10468 dispatch_scan_event(p, tSP);
10469 goto retry; /* skip \\n */
10471 if (c == ' ') return tSP;
10472 if (ISSPACE(c)) return c;
10477 return parse_percent(p, space_seen, last_state);
10480 return parse_gvar(p, last_state);
10483 return parse_atmark(p, last_state);
10486 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
10487 p->ruby__end__seen = 1;
10493 dispatch_scan_event(p, k__END__);
10501 if (!parser_is_identchar(p)) {
10502 compile_error(p, "Invalid char `\\x%02X' in expression", c);
10511 return parse_ident(p, c, cmd_state);
10514static enum yytokentype
10515yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
10517 enum yytokentype t;
10520 lval->val = Qundef;
10521 p->yylloc = yylloc;
10523 t = parser_yylex(p);
10525 if (has_delayed_token(p))
10526 dispatch_delayed_token(p, t);
10528 dispatch_scan_event(p, t);
10533#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
10536node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
10538 NODE *n = rb_ast_newnode(p->ast, type);
10540 rb_node_init(n, type, a0, a1, a2);
10542 nd_set_loc(n, loc);
10543 nd_set_node_id(n, parser_get_node_id(p));
10548nd_set_loc(NODE *nd, const YYLTYPE *loc)
10551 nd_set_line(nd, loc->beg_pos.lineno);
10556static enum node_type
10557nodetype(NODE *node) /* for debug */
10559 return (enum node_type)nd_type(node);
10563nodeline(NODE *node)
10565 return nd_line(node);
10569newline_node(NODE *node)
10572 node = remove_begin(node);
10573 node->flags |= NODE_FL_NEWLINE;
10579fixpos(NODE *node, NODE *orig)
10583 nd_set_line(node, nd_line(orig));
10587parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10589 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10593parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10595 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10599block_append(struct parser_params *p, NODE *head, NODE *tail)
10601 NODE *end, *h = head, *nd;
10603 if (tail == 0) return head;
10605 if (h == 0) return tail;
10606 switch (nd_type(h)) {
10613 parser_warning(p, h, "unused literal ignored");
10616 h = end = NEW_BLOCK(head, &head->nd_loc);
10626 switch (nd_type(nd)) {
10632 if (RTEST(ruby_verbose)) {
10633 parser_warning(p, tail, "statement not reached");
10641 if (!nd_type_p(tail, NODE_BLOCK)) {
10642 tail = NEW_BLOCK(tail, &tail->nd_loc);
10643 tail->nd_end = tail;
10645 end->nd_next = tail;
10646 h->nd_end = tail->nd_end;
10647 nd_set_last_loc(head, nd_last_loc(tail));
10651/* append item to the list */
10653list_append(struct parser_params *p, NODE *list, NODE *item)
10657 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10658 if (list->nd_next) {
10659 last = list->nd_next->nd_end;
10665 list->nd_alen += 1;
10666 last->nd_next = NEW_LIST(item, &item->nd_loc);
10667 list->nd_next->nd_end = last->nd_next;
10669 nd_set_last_loc(list, nd_last_loc(item));
10674/* concat two lists */
10676list_concat(NODE *head, NODE *tail)
10680 if (head->nd_next) {
10681 last = head->nd_next->nd_end;
10687 head->nd_alen += tail->nd_alen;
10688 last->nd_next = tail;
10689 if (tail->nd_next) {
10690 head->nd_next->nd_end = tail->nd_next->nd_end;
10693 head->nd_next->nd_end = tail;
10696 nd_set_last_loc(head, nd_last_loc(tail));
10702literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10704 if (NIL_P(tail)) return 1;
10705 if (!rb_enc_compatible(head, tail)) {
10706 compile_error(p, "string literal encodings differ (%s / %s)",
10707 rb_enc_name(rb_enc_get(head)),
10708 rb_enc_name(rb_enc_get(tail)));
10709 rb_str_resize(head, 0);
10710 rb_str_resize(tail, 0);
10713 rb_str_buf_append(head, tail);
10718string_literal_head(enum node_type htype, NODE *head)
10720 if (htype != NODE_DSTR) return Qfalse;
10721 if (head->nd_next) {
10722 head = head->nd_next->nd_end->nd_head;
10723 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10725 const VALUE lit = head->nd_lit;
10726 ASSUME(lit != Qfalse);
10730/* concat two string literals */
10732literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10734 enum node_type htype;
10737 if (!head) return tail;
10738 if (!tail) return head;
10740 htype = nd_type(head);
10741 if (htype == NODE_EVSTR) {
10742 head = new_dstr(p, head, loc);
10745 if (p->heredoc_indent > 0) {
10748 nd_set_type(head, NODE_DSTR);
10750 return list_append(p, head, tail);
10755 switch (nd_type(tail)) {
10757 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10761 lit = head->nd_lit;
10763 if (htype == NODE_STR) {
10764 if (!literal_concat0(p, lit, tail->nd_lit)) {
10766 rb_discard_node(p, head);
10767 rb_discard_node(p, tail);
10770 rb_discard_node(p, tail);
10773 list_append(p, head, tail);
10778 if (htype == NODE_STR) {
10779 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10781 tail->nd_lit = head->nd_lit;
10782 rb_discard_node(p, head);
10785 else if (NIL_P(tail->nd_lit)) {
10787 head->nd_alen += tail->nd_alen - 1;
10788 if (!head->nd_next) {
10789 head->nd_next = tail->nd_next;
10791 else if (tail->nd_next) {
10792 head->nd_next->nd_end->nd_next = tail->nd_next;
10793 head->nd_next->nd_end = tail->nd_next->nd_end;
10795 rb_discard_node(p, tail);
10797 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10798 if (!literal_concat0(p, lit, tail->nd_lit))
10800 tail->nd_lit = Qnil;
10804 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10809 if (htype == NODE_STR) {
10810 nd_set_type(head, NODE_DSTR);
10813 list_append(p, head, tail);
10820evstr2dstr(struct parser_params *p, NODE *node)
10822 if (nd_type_p(node, NODE_EVSTR)) {
10823 node = new_dstr(p, node, &node->nd_loc);
10829new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10834 switch (nd_type(node)) {
10836 nd_set_type(node, NODE_DSTR);
10844 return NEW_EVSTR(head, loc);
10848new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10850 VALUE lit = STR_NEW0();
10851 NODE *dstr = NEW_DSTR(lit, loc);
10852 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10853 return list_append(p, dstr, node);
10857call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10858 const YYLTYPE *op_loc, const YYLTYPE *loc)
10863 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10864 nd_set_line(expr, op_loc->beg_pos.lineno);
10869call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10873 opcall = NEW_OPCALL(recv, id, 0, loc);
10874 nd_set_line(opcall, op_loc->beg_pos.lineno);
10879new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10881 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10882 nd_set_line(qcall, op_loc->beg_pos.lineno);
10887new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10890 if (block) block_dup_check(p, args, block);
10891 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10892 if (block) ret = method_add_block(p, ret, block, loc);
10897#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10899match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10902 int line = op_loc->beg_pos.lineno;
10906 if (node1 && (n = nd_once_body(node1)) != 0) {
10907 switch (nd_type(n)) {
10910 NODE *match = NEW_MATCH2(node1, node2, loc);
10911 nd_set_line(match, line);
10916 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10917 const VALUE lit = n->nd_lit;
10918 NODE *match = NEW_MATCH2(node1, node2, loc);
10919 match->nd_args = reg_named_capture_assign(p, lit, loc);
10920 nd_set_line(match, line);
10926 if (node2 && (n = nd_once_body(node2)) != 0) {
10929 switch (nd_type(n)) {
10931 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10934 match3 = NEW_MATCH3(node2, node1, loc);
10939 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10940 nd_set_line(n, line);
10944# if WARN_PAST_SCOPE
10946past_dvar_p(struct parser_params *p, ID id)
10948 struct vtable *past = p->lvtbl->past;
10950 if (vtable_included(past, id)) return 1;
10958numparam_nested_p(struct parser_params *p)
10960 struct local_vars *local = p->lvtbl;
10961 NODE *outer = local->numparam.outer;
10962 NODE *inner = local->numparam.inner;
10963 if (outer || inner) {
10964 NODE *used = outer ? outer : inner;
10965 compile_error(p, "numbered parameter is already used in\n"
10966 "%s:%d: %s block here",
10967 p->ruby_sourcefile, nd_line(used),
10968 outer ? "outer" : "inner");
10969 parser_show_error_line(p, &used->nd_loc);
10976gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10982 return NEW_SELF(loc);
10984 return NEW_NIL(loc);
10986 return NEW_TRUE(loc);
10987 case keyword_false:
10988 return NEW_FALSE(loc);
10989 case keyword__FILE__:
10991 VALUE file = p->ruby_sourcefile_string;
10993 file = rb_str_new(0, 0);
10995 file = rb_str_dup(file);
10996 node = NEW_STR(file, loc);
10997 RB_OBJ_WRITTEN(p->ast, Qnil, file);
11000 case keyword__LINE__:
11001 return NEW_LIT(INT2FIX(p->tokline), loc);
11002 case keyword__ENCODING__:
11003 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
11004 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11008 switch (id_type(id)) {
11010 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
11011 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
11012 if (id == p->cur_arg) {
11013 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11016 if (vidp) *vidp |= LVAR_USED;
11017 node = NEW_DVAR(id, loc);
11020 if (local_id_ref(p, id, &vidp)) {
11021 if (id == p->cur_arg) {
11022 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11025 if (vidp) *vidp |= LVAR_USED;
11026 node = NEW_LVAR(id, loc);
11029 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
11030 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
11031 if (numparam_nested_p(p)) return 0;
11032 node = NEW_DVAR(id, loc);
11033 struct local_vars *local = p->lvtbl;
11034 if (!local->numparam.current) local->numparam.current = node;
11037# if WARN_PAST_SCOPE
11038 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
11039 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
11042 /* method call without arguments */
11043 return NEW_VCALL(id, loc);
11045 return NEW_GVAR(id, loc);
11047 return NEW_IVAR(id, loc);
11049 return NEW_CONST(id, loc);
11051 return NEW_CVAR(id, loc);
11053 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11058opt_arg_append(NODE *opt_list, NODE *opt)
11060 NODE *opts = opt_list;
11061 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11063 while (opts->nd_next) {
11064 opts = opts->nd_next;
11065 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11067 opts->nd_next = opt;
11073kwd_append(NODE *kwlist, NODE *kw)
11076 opt_arg_append(kwlist, kw);
11082new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
11084 return NEW_DEFINED(remove_begin_all(expr), loc);
11088symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
11090 enum node_type type = nd_type(symbol);
11093 nd_set_type(symbol, NODE_DSYM);
11096 nd_set_type(symbol, NODE_LIT);
11097 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
11100 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
11102 return list_append(p, symbols, symbol);
11106new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
11112 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
11113 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11116 switch (nd_type(node)) {
11119 VALUE src = node->nd_lit;
11120 nd_set_type(node, NODE_LIT);
11121 nd_set_loc(node, loc);
11122 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11127 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
11128 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11131 nd_set_type(node, NODE_DREGX);
11132 nd_set_loc(node, loc);
11133 node->nd_cflag = options & RE_OPTION_MASK;
11134 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
11135 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
11136 NODE *frag = list->nd_head;
11137 enum node_type type = nd_type(frag);
11138 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
11139 VALUE tail = frag->nd_lit;
11140 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
11141 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
11142 if (!literal_concat0(p, lit, tail)) {
11143 return NEW_NIL(loc); /* dummy node on error */
11145 rb_str_resize(tail, 0);
11146 prev->nd_next = list->nd_next;
11147 rb_discard_node(p, list->nd_head);
11148 rb_discard_node(p, list);
11159 if (!node->nd_next) {
11160 VALUE src = node->nd_lit;
11161 nd_set_type(node, NODE_LIT);
11162 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11164 if (options & RE_OPTION_ONCE) {
11165 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
11173new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
11176 return NEW_KW_ARG(0, (k), loc);
11180new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11183 VALUE lit = STR_NEW0();
11184 NODE *xstr = NEW_XSTR(lit, loc);
11185 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11188 switch (nd_type(node)) {
11190 nd_set_type(node, NODE_XSTR);
11191 nd_set_loc(node, loc);
11194 nd_set_type(node, NODE_DXSTR);
11195 nd_set_loc(node, loc);
11198 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
11205check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
11209 if (!arg || !p->case_labels) return;
11211 lit = rb_node_case_when_optimizable_literal(arg);
11212 if (UNDEF_P(lit)) return;
11213 if (nd_type_p(arg, NODE_STR)) {
11214 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
11217 if (NIL_P(p->case_labels)) {
11218 p->case_labels = rb_obj_hide(rb_hash_new());
11221 VALUE line = rb_hash_lookup(p->case_labels, lit);
11222 if (!NIL_P(line)) {
11223 rb_warning1("duplicated `when' clause with line %d is ignored",
11228 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
11233id_is_var(struct parser_params *p, ID id)
11235 if (is_notop_id(id)) {
11236 switch (id & ID_SCOPE_MASK) {
11237 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
11240 if (dyna_in_block(p)) {
11241 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
11243 if (local_id(p, id)) return 1;
11244 /* method call without arguments */
11248 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11253new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
11255 VALUE src = 0, err;
11257 if (ripper_is_node_yylval(re)) {
11258 src = RNODE(re)->nd_cval;
11259 re = RNODE(re)->nd_rval;
11261 if (ripper_is_node_yylval(opt)) {
11262 options = (int)RNODE(opt)->nd_tag;
11263 opt = RNODE(opt)->nd_rval;
11265 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
11266 compile_error(p, "%"PRIsVALUE, err);
11268 return dispatch2(regexp_literal, re, opt);
11270#endif /* !RIPPER */
11272static inline enum lex_state_e
11273parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
11276 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
11278 return p->lex.state = ls;
11282static const char rb_parser_lex_state_names[][8] = {
11283 "BEG", "END", "ENDARG", "ENDFN", "ARG",
11284 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
11285 "LABEL", "LABELED","FITEM",
11289append_lex_state_name(enum lex_state_e state, VALUE buf)
11292 unsigned int mask = 1;
11293 static const char none[] = "NONE";
11295 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
11296 if ((unsigned)state & mask) {
11298 rb_str_cat(buf, "|", 1);
11301 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
11305 rb_str_cat(buf, none, sizeof(none)-1);
11311flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
11313 VALUE mesg = p->debug_buffer;
11315 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
11316 p->debug_buffer = Qnil;
11317 rb_io_puts(1, &mesg, out);
11319 if (!NIL_P(str) && RSTRING_LEN(str)) {
11320 rb_io_write(p->debug_output, str);
11325rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
11326 enum lex_state_e to, int line)
11329 mesg = rb_str_new_cstr("lex_state: ");
11330 append_lex_state_name(from, mesg);
11331 rb_str_cat_cstr(mesg, " -> ");
11332 append_lex_state_name(to, mesg);
11333 rb_str_catf(mesg, " at line %d\n", line);
11334 flush_debug_buffer(p, p->debug_output, mesg);
11339rb_parser_lex_state_name(enum lex_state_e state)
11341 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
11345append_bitstack_value(stack_type stack, VALUE mesg)
11348 rb_str_cat_cstr(mesg, "0");
11351 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
11352 for (; mask && !(stack & mask); mask >>= 1) continue;
11353 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
11358rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
11359 const char *name, int line)
11361 VALUE mesg = rb_sprintf("%s: ", name);
11362 append_bitstack_value(stack, mesg);
11363 rb_str_catf(mesg, " at line %d\n", line);
11364 flush_debug_buffer(p, p->debug_output, mesg);
11368rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
11371 VALUE mesg = rb_str_new_cstr("internal parser error: ");
11374 rb_str_vcatf(mesg, fmt, ap);
11376 yyerror0(RSTRING_PTR(mesg));
11379 mesg = rb_str_new(0, 0);
11380 append_lex_state_name(p->lex.state, mesg);
11381 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
11382 rb_str_resize(mesg, 0);
11383 append_bitstack_value(p->cond_stack, mesg);
11384 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
11385 rb_str_resize(mesg, 0);
11386 append_bitstack_value(p->cmdarg_stack, mesg);
11387 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
11388 if (p->debug_output == rb_ractor_stdout())
11389 p->debug_output = rb_ractor_stderr();
11394rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
11396 yylloc->beg_pos.lineno = sourceline;
11397 yylloc->beg_pos.column = beg_pos;
11398 yylloc->end_pos.lineno = sourceline;
11399 yylloc->end_pos.column = end_pos;
11404rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
11406 int sourceline = here->sourceline;
11407 int beg_pos = (int)here->offset - here->quote
11408 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
11409 int end_pos = (int)here->offset + here->length + here->quote;
11411 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11415rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
11417 yylloc->beg_pos.lineno = p->delayed.beg_line;
11418 yylloc->beg_pos.column = p->delayed.beg_col;
11419 yylloc->end_pos.lineno = p->delayed.end_line;
11420 yylloc->end_pos.column = p->delayed.end_col;
11426rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
11428 int sourceline = p->ruby_sourceline;
11429 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11430 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
11431 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11435rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
11437 yylloc->end_pos = yylloc->beg_pos;
11443rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
11445 int sourceline = p->ruby_sourceline;
11446 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11447 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
11448 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11452rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
11454 int sourceline = p->ruby_sourceline;
11455 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11456 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
11457 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11459#endif /* !RIPPER */
11462assignable0(struct parser_params *p, ID id, const char **err)
11464 if (!id) return -1;
11467 *err = "Can't change the value of self";
11470 *err = "Can't assign to nil";
11473 *err = "Can't assign to true";
11475 case keyword_false:
11476 *err = "Can't assign to false";
11478 case keyword__FILE__:
11479 *err = "Can't assign to __FILE__";
11481 case keyword__LINE__:
11482 *err = "Can't assign to __LINE__";
11484 case keyword__ENCODING__:
11485 *err = "Can't assign to __ENCODING__";
11488 switch (id_type(id)) {
11490 if (dyna_in_block(p)) {
11491 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
11492 compile_error(p, "Can't assign to numbered parameter _%d",
11493 NUMPARAM_ID_TO_IDX(id));
11496 if (dvar_curr(p, id)) return NODE_DASGN;
11497 if (dvar_defined(p, id)) return NODE_DASGN;
11498 if (local_id(p, id)) return NODE_LASGN;
11503 if (!local_id(p, id)) local_var(p, id);
11507 case ID_GLOBAL: return NODE_GASGN;
11508 case ID_INSTANCE: return NODE_IASGN;
11510 if (!p->ctxt.in_def) return NODE_CDECL;
11511 *err = "dynamic constant assignment";
11513 case ID_CLASS: return NODE_CVASGN;
11515 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
11522assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
11524 const char *err = 0;
11525 int node_type = assignable0(p, id, &err);
11526 switch (node_type) {
11527 case NODE_DASGN: return NEW_DASGN(id, val, loc);
11528 case NODE_LASGN: return NEW_LASGN(id, val, loc);
11529 case NODE_GASGN: return NEW_GASGN(id, val, loc);
11530 case NODE_IASGN: return NEW_IASGN(id, val, loc);
11531 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
11532 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
11534 if (err) yyerror1(loc, err);
11535 return NEW_BEGIN(0, loc);
11539assignable(struct parser_params *p, VALUE lhs)
11541 const char *err = 0;
11542 assignable0(p, get_id(lhs), &err);
11543 if (err) lhs = assign_error(p, err, lhs);
11549is_private_local_id(ID name)
11552 if (name == idUScore) return 1;
11553 if (!is_local_id(name)) return 0;
11554 s = rb_id2str(name);
11556 return RSTRING_PTR(s)[0] == '_';
11560shadowing_lvar_0(struct parser_params *p, ID name)
11562 if (dyna_in_block(p)) {
11563 if (dvar_curr(p, name)) {
11564 if (is_private_local_id(name)) return 1;
11565 yyerror0("duplicated argument name");
11567 else if (dvar_defined(p, name) || local_id(p, name)) {
11568 vtable_add(p->lvtbl->vars, name);
11569 if (p->lvtbl->used) {
11570 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11576 if (local_id(p, name)) {
11577 if (is_private_local_id(name)) return 1;
11578 yyerror0("duplicated argument name");
11585shadowing_lvar(struct parser_params *p, ID name)
11587 shadowing_lvar_0(p, name);
11592new_bv(struct parser_params *p, ID name)
11595 if (!is_local_id(name)) {
11596 compile_error(p, "invalid local variable - %"PRIsVALUE,
11600 if (!shadowing_lvar_0(p, name)) return;
11606aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11608 return NEW_ATTRASGN(recv, tASET, idx, loc);
11612block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11614 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11615 compile_error(p, "both block arg and actual block given");
11620attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11622 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11623 return NEW_ATTRASGN(recv, id, 0, loc);
11627rb_backref_error(struct parser_params *p, NODE *node)
11629 switch (nd_type(node)) {
11631 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11633 case NODE_BACK_REF:
11634 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11640backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11642 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11643 rb_str_append(mesg, ref->nd_cval);
11644 return dispatch2(assign_error, mesg, expr);
11650arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11652 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11653 switch (nd_type(node1)) {
11655 return list_append(p, node1, node2);
11656 case NODE_BLOCK_PASS:
11657 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11658 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11660 case NODE_ARGSPUSH:
11661 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11662 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11663 nd_set_type(node1, NODE_ARGSCAT);
11666 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11667 node1->nd_body = list_append(p, node1->nd_body, node2);
11668 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11671 return NEW_ARGSPUSH(node1, node2, loc);
11675arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11677 if (!node2) return node1;
11678 switch (nd_type(node1)) {
11679 case NODE_BLOCK_PASS:
11680 if (node1->nd_head)
11681 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11683 node1->nd_head = NEW_LIST(node2, loc);
11685 case NODE_ARGSPUSH:
11686 if (!nd_type_p(node2, NODE_LIST)) break;
11687 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11688 nd_set_type(node1, NODE_ARGSCAT);
11691 if (!nd_type_p(node2, NODE_LIST) ||
11692 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11693 node1->nd_body = list_concat(node1->nd_body, node2);
11696 return NEW_ARGSCAT(node1, node2, loc);
11700last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11703 if ((n1 = splat_array(args)) != 0) {
11704 return list_append(p, n1, last_arg);
11706 return arg_append(p, args, last_arg, loc);
11710rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11713 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11714 return list_concat(n1, rest_arg);
11716 return arg_concat(p, args, rest_arg, loc);
11720splat_array(NODE* node)
11722 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11723 if (nd_type_p(node, NODE_LIST)) return node;
11728mark_lvar_used(struct parser_params *p, NODE *rhs)
11732 switch (nd_type(rhs)) {
11734 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11735 if (vidp) *vidp |= LVAR_USED;
11739 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11740 if (vidp) *vidp |= LVAR_USED;
11745 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11746 mark_lvar_used(p, rhs->nd_head);
11754const_decl_path(struct parser_params *p, NODE **dest)
11757 if (!nd_type_p(n, NODE_CALL)) {
11758 const YYLTYPE *loc = &n->nd_loc;
11761 path = rb_id2str(n->nd_vid);
11765 path = rb_ary_new();
11766 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11767 rb_ary_push(path, rb_id2str(n->nd_mid));
11769 if (n && nd_type_p(n, NODE_CONST)) {
11771 rb_ary_push(path, rb_id2str(n->nd_vid));
11773 else if (n && nd_type_p(n, NODE_COLON3)) {
11775 rb_ary_push(path, rb_str_new(0, 0));
11778 // expression::Name
11779 rb_ary_push(path, rb_str_new_cstr("..."));
11781 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11782 path = rb_fstring(path);
11784 *dest = n = NEW_LIT(path, loc);
11785 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11790extern VALUE rb_mRubyVMFrozenCore;
11793make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11795 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11798 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11799 NEW_LIST(value, loc), loc);
11802 return NEW_CALL(fcore, rb_intern("make_shareable"),
11803 NEW_LIST(value, loc), loc);
11808ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11810 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11811 NODE *args = NEW_LIST(value, loc);
11812 args = list_append(p, args, const_decl_path(p, dest));
11813 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11816static int is_static_content(NODE *node);
11819shareable_literal_value(NODE *node)
11821 if (!node) return Qnil;
11822 enum node_type type = nd_type(node);
11831 return node->nd_lit;
11837#ifndef SHAREABLE_BARE_EXPRESSION
11838#define SHAREABLE_BARE_EXPRESSION 1
11842shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11843 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11845# define shareable_literal_constant_next(n) \
11846 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11849 if (!value) return 0;
11850 enum node_type type = nd_type(value);
11859 if (shareable == shareable_literal) {
11860 value = NEW_CALL(value, idUMinus, 0, loc);
11865 lit = rb_fstring(value->nd_lit);
11866 nd_set_type(value, NODE_LIT);
11867 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11871 lit = rb_ary_new();
11872 OBJ_FREEZE_RAW(lit);
11873 NODE *n = NEW_LIT(lit, loc);
11874 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11878 lit = rb_ary_new();
11879 for (NODE *n = value; n; n = n->nd_next) {
11880 NODE *elt = n->nd_head;
11882 elt = shareable_literal_constant_next(elt);
11886 else if (RTEST(lit)) {
11892 VALUE e = shareable_literal_value(elt);
11894 rb_ary_push(lit, e);
11898 lit = Qnil; /* make shareable at runtime */
11905 if (!value->nd_brace) return 0;
11906 lit = rb_hash_new();
11907 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11908 NODE *key = n->nd_head;
11909 NODE *val = n->nd_next->nd_head;
11911 key = shareable_literal_constant_next(key);
11915 else if (RTEST(lit)) {
11916 rb_hash_clear(lit);
11921 val = shareable_literal_constant_next(val);
11923 n->nd_next->nd_head = val;
11925 else if (RTEST(lit)) {
11926 rb_hash_clear(lit);
11931 VALUE k = shareable_literal_value(key);
11932 VALUE v = shareable_literal_value(val);
11933 if (!UNDEF_P(k) && !UNDEF_P(v)) {
11934 rb_hash_aset(lit, k, v);
11937 rb_hash_clear(lit);
11938 lit = Qnil; /* make shareable at runtime */
11945 if (shareable == shareable_literal &&
11946 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11947 return ensure_shareable_node(p, dest, value, loc);
11952 /* Array or Hash */
11953 if (!lit) return 0;
11955 // if shareable_literal, all elements should have been ensured
11957 value = make_shareable_node(p, value, false, loc);
11960 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11961 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11965# undef shareable_literal_constant_next
11969shareable_constant_value(struct parser_params *p, enum shareability shareable,
11970 NODE *lhs, NODE *value, const YYLTYPE *loc)
11972 if (!value) return 0;
11973 switch (shareable) {
11974 case shareable_none:
11977 case shareable_literal:
11979 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11980 if (lit) return lit;
11985 case shareable_copy:
11986 case shareable_everything:
11988 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11989 if (lit) return lit;
11990 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11995 UNREACHABLE_RETURN(0);
12000node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12002 if (!lhs) return 0;
12004 switch (nd_type(lhs)) {
12006 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12015 lhs->nd_value = rhs;
12016 nd_set_loc(lhs, loc);
12019 case NODE_ATTRASGN:
12020 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
12021 nd_set_loc(lhs, loc);
12025 /* should not happen */
12033value_expr_check(struct parser_params *p, NODE *node)
12035 NODE *void_node = 0, *vn;
12038 rb_warning0("empty expression");
12041 switch (nd_type(node)) {
12047 return void_node ? void_node : node;
12050 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
12051 compile_error(p, "unexpected node");
12054 if (node->nd_body->nd_body) {
12057 /* single line pattern matching */
12058 return void_node ? void_node : node;
12061 while (node->nd_next) {
12062 node = node->nd_next;
12064 node = node->nd_head;
12068 node = node->nd_body;
12073 if (!node->nd_body) {
12076 else if (!node->nd_else) {
12079 vn = value_expr_check(p, node->nd_body);
12080 if (!vn) return NULL;
12081 if (!void_node) void_node = vn;
12082 node = node->nd_else;
12087 node = node->nd_1st;
12093 mark_lvar_used(p, node);
12105value_expr_gen(struct parser_params *p, NODE *node)
12107 NODE *void_node = value_expr_check(p, node);
12109 yyerror1(&void_node->nd_loc, "void value expression");
12110 /* or "control never reach"? */
12116void_expr(struct parser_params *p, NODE *node)
12118 const char *useless = 0;
12120 if (!RTEST(ruby_verbose)) return;
12122 if (!node || !(node = nd_once_body(node))) return;
12123 switch (nd_type(node)) {
12125 switch (node->nd_mid) {
12144 useless = rb_id2name(node->nd_mid);
12155 case NODE_BACK_REF:
12156 useless = "a variable";
12159 useless = "a constant";
12165 useless = "a literal";
12190 useless = "defined?";
12195 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
12200void_stmts(struct parser_params *p, NODE *node)
12202 NODE *const n = node;
12203 if (!RTEST(ruby_verbose)) return n;
12204 if (!node) return n;
12205 if (!nd_type_p(node, NODE_BLOCK)) return n;
12207 while (node->nd_next) {
12208 void_expr(p, node->nd_head);
12209 node = node->nd_next;
12215remove_begin(NODE *node)
12217 NODE **n = &node, *n1 = node;
12218 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
12219 *n = n1 = n1->nd_body;
12225remove_begin_all(NODE *node)
12227 NODE **n = &node, *n1 = node;
12228 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
12229 *n = n1 = n1->nd_body;
12235reduce_nodes(struct parser_params *p, NODE **body)
12237 NODE *node = *body;
12240 *body = NEW_NIL(&NULL_LOC);
12243#define subnodes(n1, n2) \
12244 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
12245 (!node->n2) ? (body = &node->n1, 1) : \
12246 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
12249 int newline = (int)(node->flags & NODE_FL_NEWLINE);
12250 switch (nd_type(node)) {
12256 *body = node = node->nd_stts;
12257 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12260 *body = node = node->nd_body;
12261 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12264 body = &node->nd_end->nd_head;
12268 if (subnodes(nd_body, nd_else)) break;
12271 body = &node->nd_body;
12274 if (!subnodes(nd_body, nd_next)) goto end;
12277 if (!subnodes(nd_head, nd_resq)) goto end;
12280 if (node->nd_else) {
12281 body = &node->nd_resq;
12284 if (!subnodes(nd_head, nd_resq)) goto end;
12290 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12297is_static_content(NODE *node)
12299 if (!node) return 1;
12300 switch (nd_type(node)) {
12302 if (!(node = node->nd_head)) break;
12305 if (!is_static_content(node->nd_head)) return 0;
12306 } while ((node = node->nd_next) != 0);
12321assign_in_cond(struct parser_params *p, NODE *node)
12323 switch (nd_type(node)) {
12335 if (!node->nd_value) return 1;
12336 if (is_static_content(node->nd_value)) {
12337 /* reports always */
12338 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
12349#define SWITCH_BY_COND_TYPE(t, w, arg) \
12351 case COND_IN_OP: break; \
12352 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
12353 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
12356static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
12359range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12361 enum node_type type;
12363 if (node == 0) return 0;
12365 type = nd_type(node);
12367 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
12368 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
12369 ID lineno = rb_intern("$.");
12370 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
12372 return cond0(p, node, COND_IN_FF, loc);
12376cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
12378 if (node == 0) return 0;
12379 if (!(node = nd_once_body(node))) return 0;
12380 assign_in_cond(p, node);
12382 switch (nd_type(node)) {
12386 SWITCH_BY_COND_TYPE(type, warn, "string ")
12390 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
12392 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
12396 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
12397 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
12402 node->nd_beg = range_op(p, node->nd_beg, loc);
12403 node->nd_end = range_op(p, node->nd_end, loc);
12404 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
12405 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
12410 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
12414 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
12415 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
12416 nd_set_type(node, NODE_MATCH);
12418 else if (node->nd_lit == Qtrue ||
12419 node->nd_lit == Qfalse) {
12420 /* booleans are OK, e.g., while true */
12422 else if (SYMBOL_P(node->nd_lit)) {
12426 SWITCH_BY_COND_TYPE(type, warning, "")
12435cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12437 if (node == 0) return 0;
12438 return cond0(p, node, COND_IN_COND, loc);
12442method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12444 if (node == 0) return 0;
12445 return cond0(p, node, COND_IN_OP, loc);
12449new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
12451 YYLTYPE loc = {*pos, *pos};
12452 return NEW_NIL(&loc);
12456new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12458 if (!cc) return right;
12459 cc = cond0(p, cc, COND_IN_COND, loc);
12460 return newline_node(NEW_IF(cc, left, right, loc));
12464new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12466 if (!cc) return right;
12467 cc = cond0(p, cc, COND_IN_COND, loc);
12468 return newline_node(NEW_UNLESS(cc, left, right, loc));
12472logop(struct parser_params *p, ID id, NODE *left, NODE *right,
12473 const YYLTYPE *op_loc, const YYLTYPE *loc)
12475 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
12478 if (left && nd_type_p(left, type)) {
12479 NODE *node = left, *second;
12480 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
12483 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
12484 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
12485 left->nd_loc.end_pos = loc->end_pos;
12488 op = NEW_NODE(type, left, right, 0, loc);
12489 nd_set_line(op, op_loc->beg_pos.lineno);
12494no_blockarg(struct parser_params *p, NODE *node)
12496 if (nd_type_p(node, NODE_BLOCK_PASS)) {
12497 compile_error(p, "block argument should not be given");
12502ret_args(struct parser_params *p, NODE *node)
12505 no_blockarg(p, node);
12506 if (nd_type_p(node, NODE_LIST)) {
12507 if (node->nd_next == 0) {
12508 node = node->nd_head;
12511 nd_set_type(node, NODE_VALUES);
12519new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12521 if (node) no_blockarg(p, node);
12523 return NEW_YIELD(node, loc);
12527negate_lit(struct parser_params *p, VALUE lit)
12529 if (FIXNUM_P(lit)) {
12530 return LONG2FIX(-FIX2LONG(lit));
12532 if (SPECIAL_CONST_P(lit)) {
12534 if (FLONUM_P(lit)) {
12535 return DBL2NUM(-RFLOAT_VALUE(lit));
12540 switch (BUILTIN_TYPE(lit)) {
12542 BIGNUM_NEGATE(lit);
12543 lit = rb_big_norm(lit);
12546 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
12549 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
12550 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
12553 lit = DBL2NUM(-RFLOAT_VALUE(lit));
12557 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
12558 rb_builtin_class_name(lit));
12565arg_blk_pass(NODE *node1, NODE *node2)
12568 if (!node1) return node2;
12569 node2->nd_head = node1;
12570 nd_set_first_lineno(node2, nd_first_lineno(node1));
12571 nd_set_first_column(node2, nd_first_column(node1));
12578args_info_empty_p(struct rb_args_info *args)
12580 if (args->pre_args_num) return false;
12581 if (args->post_args_num) return false;
12582 if (args->rest_arg) return false;
12583 if (args->opt_args) return false;
12584 if (args->block_arg) return false;
12585 if (args->kw_args) return false;
12586 if (args->kw_rest_arg) return false;
12591new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12593 int saved_line = p->ruby_sourceline;
12594 struct rb_args_info *args = tail->nd_ainfo;
12596 if (args->forwarding) {
12598 yyerror1(&tail->nd_loc, "... after rest argument");
12601 rest_arg = idFWD_REST;
12604 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12605 args->pre_init = pre_args ? pre_args->nd_next : 0;
12607 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12608 args->post_init = post_args ? post_args->nd_next : 0;
12609 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12611 args->rest_arg = rest_arg;
12613 args->opt_args = opt_args;
12615#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
12616 args->ruby2_keywords = args->forwarding;
12618 args->ruby2_keywords = 0;
12621 p->ruby_sourceline = saved_line;
12622 nd_set_loc(tail, loc);
12628new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12630 int saved_line = p->ruby_sourceline;
12632 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12633 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12634 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12635 args->imemo = tmpbuf;
12636 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12637 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12638 if (p->error_p) return node;
12640 args->block_arg = block;
12641 args->kw_args = kw_args;
12645 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12646 * variable order: k1, kr1, k2, &b, internal_id, krest
12648 * variable order: kr1, k1, k2, internal_id, krest, &b
12650 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12651 struct vtable *vtargs = p->lvtbl->args;
12652 NODE *kwn = kw_args;
12654 if (block) block = vtargs->tbl[vtargs->pos-1];
12655 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12656 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12658 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12660 --required_kw_vars;
12661 kwn = kwn->nd_next;
12664 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12665 ID vid = kwn->nd_body->nd_vid;
12666 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12667 *required_kw_vars++ = vid;
12674 arg_var(p, kw_bits);
12675 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12676 if (block) arg_var(p, block);
12678 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12679 args->kw_rest_arg->nd_cflag = kw_bits;
12681 else if (kw_rest_arg == idNil) {
12682 args->no_kwarg = 1;
12684 else if (kw_rest_arg) {
12685 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12688 p->ruby_sourceline = saved_line;
12693args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12695 if (max_numparam > NO_PARAM) {
12697 YYLTYPE loc = RUBY_INIT_YYLLOC();
12698 args = new_args_tail(p, 0, 0, 0, 0);
12699 nd_set_loc(args, &loc);
12701 args->nd_ainfo->pre_args_num = max_numparam;
12707new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12709 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12711 aryptn->nd_pconst = constant;
12714 NODE *pre_args = NEW_LIST(pre_arg, loc);
12715 if (apinfo->pre_args) {
12716 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12719 apinfo->pre_args = pre_args;
12726new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12728 int saved_line = p->ruby_sourceline;
12730 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12731 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12732 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12733 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12734 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12736 apinfo->pre_args = pre_args;
12740 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12743 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12747 apinfo->rest_arg = NULL;
12750 apinfo->post_args = post_args;
12752 p->ruby_sourceline = saved_line;
12757new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12759 fndptn->nd_pconst = constant;
12765new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12767 int saved_line = p->ruby_sourceline;
12769 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12770 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12771 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12772 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12773 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12775 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12776 fpinfo->args = args;
12777 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12779 p->ruby_sourceline = saved_line;
12784new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12786 hshptn->nd_pconst = constant;
12791new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12793 int saved_line = p->ruby_sourceline;
12794 NODE *node, *kw_rest_arg_node;
12796 if (kw_rest_arg == idNil) {
12797 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12799 else if (kw_rest_arg) {
12800 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12803 kw_rest_arg_node = NULL;
12806 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12808 p->ruby_sourceline = saved_line;
12813dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12818 return NEW_LIT(ID2SYM(idNULL), loc);
12821 switch (nd_type(node)) {
12823 nd_set_type(node, NODE_DSYM);
12824 nd_set_loc(node, loc);
12827 lit = node->nd_lit;
12828 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12829 nd_set_type(node, NODE_LIT);
12830 nd_set_loc(node, loc);
12833 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12840append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12842 NODE *node = (NODE *)v;
12843 NODE **result = (NODE **)h;
12845 node->nd_next->nd_end = node->nd_next;
12846 node->nd_next->nd_next = 0;
12848 list_concat(*result, node);
12851 return ST_CONTINUE;
12855hash_literal_key_p(VALUE k)
12857 switch (OBJ_BUILTIN_TYPE(k)) {
12866literal_cmp(VALUE val, VALUE lit)
12868 if (val == lit) return 0;
12869 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12870 return rb_iseq_cdhash_cmp(val, lit);
12874literal_hash(VALUE a)
12876 if (!hash_literal_key_p(a)) return (st_index_t)a;
12877 return rb_iseq_cdhash_hash(a);
12880static const struct st_hash_type literal_type = {
12886remove_duplicate_keys(struct parser_params *p, NODE *hash)
12888 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12890 NODE *last_expr = 0;
12891 rb_code_location_t loc = hash->nd_loc;
12892 while (hash && hash->nd_head && hash->nd_next) {
12893 NODE *head = hash->nd_head;
12894 NODE *value = hash->nd_next;
12895 NODE *next = value->nd_next;
12896 st_data_t key = (st_data_t)head;
12898 value->nd_next = 0;
12899 if (nd_type_p(head, NODE_LIT) &&
12900 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12901 NODE *dup_value = ((NODE *)data)->nd_next;
12902 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12903 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12904 head->nd_lit, nd_line(head));
12905 if (dup_value == last_expr) {
12906 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12909 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12912 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12913 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12916 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12917 st_free_table(literal_keys);
12919 if (!result) result = hash;
12920 else list_concat(result, hash);
12922 result->nd_loc = loc;
12927new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12929 if (hash) hash = remove_duplicate_keys(p, hash);
12930 return NEW_HASH(hash, loc);
12935error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12937 if (is_private_local_id(id)) {
12940 if (st_is_member(p->pvtbl, id)) {
12941 yyerror1(loc, "duplicated variable name");
12944 st_insert(p->pvtbl, (st_data_t)id, 0);
12949error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12952 p->pktbl = st_init_numtable();
12954 else if (st_is_member(p->pktbl, key)) {
12955 yyerror1(loc, "duplicated key name");
12958 st_insert(p->pktbl, (st_data_t)key, 0);
12963new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12965 return NEW_HASH(hash, loc);
12967#endif /* !RIPPER */
12971new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12976 ID vid = lhs->nd_vid;
12977 YYLTYPE lhs_loc = lhs->nd_loc;
12978 int shareable = ctxt.shareable_constant_value;
12980 switch (nd_type(lhs)) {
12991 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12992 lhs->nd_value = rhs;
12993 nd_set_loc(lhs, loc);
12994 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12995 if (is_notop_id(vid)) {
12996 switch (id_type(vid)) {
13000 asgn->nd_aid = vid;
13004 else if (op == tANDOP) {
13006 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13008 lhs->nd_value = rhs;
13009 nd_set_loc(lhs, loc);
13010 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
13014 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
13016 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13018 asgn->nd_value = rhs;
13019 nd_set_loc(asgn, loc);
13023 asgn = NEW_BEGIN(0, loc);
13029new_ary_op_assign(struct parser_params *p, NODE *ary,
13030 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
13034 args = make_list(args, args_loc);
13035 if (nd_type_p(args, NODE_BLOCK_PASS)) {
13036 args = NEW_ARGSCAT(args, rhs, loc);
13039 args = arg_concat(p, args, rhs, loc);
13041 asgn = NEW_OP_ASGN1(ary, op, args, loc);
13047new_attr_op_assign(struct parser_params *p, NODE *lhs,
13048 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
13052 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
13058new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13063 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13064 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
13067 asgn = NEW_BEGIN(0, loc);
13074const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
13076 if (p->ctxt.in_def) {
13077 yyerror1(loc, "dynamic constant assignment");
13079 return NEW_CDECL(0, 0, (path), loc);
13083const_decl(struct parser_params *p, VALUE path)
13085 if (p->ctxt.in_def) {
13086 path = assign_error(p, "dynamic constant assignment", path);
13092assign_error(struct parser_params *p, const char *mesg, VALUE a)
13094 a = dispatch2(assign_error, ERR_MESG(), a);
13100var_field(struct parser_params *p, VALUE a)
13102 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
13108new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
13110 NODE *result = head;
13112 NODE *tmp = rescue_else ? rescue_else : rescue;
13113 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
13115 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
13116 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
13118 else if (rescue_else) {
13119 result = block_append(p, result, rescue_else);
13122 result = NEW_ENSURE(result, ensure, loc);
13124 fixpos(result, head);
13130warn_unused_var(struct parser_params *p, struct local_vars *local)
13134 if (!local->used) return;
13135 cnt = local->used->pos;
13136 if (cnt != local->vars->pos) {
13137 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
13140 ID *v = local->vars->tbl;
13141 ID *u = local->used->tbl;
13142 for (int i = 0; i < cnt; ++i) {
13143 if (!v[i] || (u[i] & LVAR_USED)) continue;
13144 if (is_private_local_id(v[i])) continue;
13145 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
13151local_push(struct parser_params *p, int toplevel_scope)
13153 struct local_vars *local;
13154 int inherits_dvars = toplevel_scope && compile_for_eval;
13155 int warn_unused_vars = RTEST(ruby_verbose);
13157 local = ALLOC(struct local_vars);
13158 local->prev = p->lvtbl;
13159 local->args = vtable_alloc(0);
13160 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
13162 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
13163 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
13164 local->numparam.outer = 0;
13165 local->numparam.inner = 0;
13166 local->numparam.current = 0;
13168 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
13170# if WARN_PAST_SCOPE
13179vtable_chain_free(struct parser_params *p, struct vtable *table)
13181 while (!DVARS_TERMINAL_P(table)) {
13182 struct vtable *cur_table = table;
13183 table = cur_table->prev;
13184 vtable_free(cur_table);
13189local_free(struct parser_params *p, struct local_vars *local)
13191 vtable_chain_free(p, local->used);
13193# if WARN_PAST_SCOPE
13194 vtable_chain_free(p, local->past);
13197 vtable_chain_free(p, local->args);
13198 vtable_chain_free(p, local->vars);
13200 ruby_sized_xfree(local, sizeof(struct local_vars));
13204local_pop(struct parser_params *p)
13206 struct local_vars *local = p->lvtbl->prev;
13207 if (p->lvtbl->used) {
13208 warn_unused_var(p, p->lvtbl);
13211 local_free(p, p->lvtbl);
13219static rb_ast_id_table_t *
13220local_tbl(struct parser_params *p)
13222 int cnt_args = vtable_size(p->lvtbl->args);
13223 int cnt_vars = vtable_size(p->lvtbl->vars);
13224 int cnt = cnt_args + cnt_vars;
13226 rb_ast_id_table_t *tbl;
13228 if (cnt <= 0) return 0;
13229 tbl = rb_ast_new_local_table(p->ast, cnt);
13230 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
13231 /* remove IDs duplicated to warn shadowing */
13232 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
13233 ID id = p->lvtbl->vars->tbl[i];
13234 if (!vtable_included(p->lvtbl->args, id)) {
13235 tbl->ids[j++] = id;
13239 tbl = rb_ast_resize_latest_local_table(p->ast, j);
13246node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
13248 rb_ast_id_table_t *a0;
13252 n = NEW_NODE(type, a0, a1, a2, loc);
13259numparam_name(struct parser_params *p, ID id)
13261 if (!NUMPARAM_ID_P(id)) return;
13262 compile_error(p, "_%d is reserved for numbered parameter",
13263 NUMPARAM_ID_TO_IDX(id));
13267arg_var(struct parser_params *p, ID id)
13269 numparam_name(p, id);
13270 vtable_add(p->lvtbl->args, id);
13274local_var(struct parser_params *p, ID id)
13276 numparam_name(p, id);
13277 vtable_add(p->lvtbl->vars, id);
13278 if (p->lvtbl->used) {
13279 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
13284local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
13286 struct vtable *vars, *args, *used;
13288 vars = p->lvtbl->vars;
13289 args = p->lvtbl->args;
13290 used = p->lvtbl->used;
13292 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
13295 if (used) used = used->prev;
13298 if (vars && vars->prev == DVARS_INHERIT) {
13299 return rb_local_defined(id, p->parent_iseq);
13301 else if (vtable_included(args, id)) {
13305 int i = vtable_included(vars, id);
13306 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
13312local_id(struct parser_params *p, ID id)
13314 return local_id_ref(p, id, NULL);
13318check_forwarding_args(struct parser_params *p)
13320 if (local_id(p, idFWD_ALL)) return TRUE;
13321 compile_error(p, "unexpected ...");
13326add_forwarding_args(struct parser_params *p)
13328 arg_var(p, idFWD_REST);
13329#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13330 arg_var(p, idFWD_KWREST);
13332 arg_var(p, idFWD_BLOCK);
13333 arg_var(p, idFWD_ALL);
13338new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
13340 NODE *rest = NEW_LVAR(idFWD_REST, loc);
13341#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13342 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
13344 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
13345 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
13346#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13347 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
13349 return arg_blk_pass(args, block);
13354numparam_push(struct parser_params *p)
13357 struct local_vars *local = p->lvtbl;
13358 NODE *inner = local->numparam.inner;
13359 if (!local->numparam.outer) {
13360 local->numparam.outer = local->numparam.current;
13362 local->numparam.inner = 0;
13363 local->numparam.current = 0;
13371numparam_pop(struct parser_params *p, NODE *prev_inner)
13374 struct local_vars *local = p->lvtbl;
13376 /* prefer first one */
13377 local->numparam.inner = prev_inner;
13379 else if (local->numparam.current) {
13380 /* current and inner are exclusive */
13381 local->numparam.inner = local->numparam.current;
13383 if (p->max_numparam > NO_PARAM) {
13384 /* current and outer are exclusive */
13385 local->numparam.current = local->numparam.outer;
13386 local->numparam.outer = 0;
13389 /* no numbered parameter */
13390 local->numparam.current = 0;
13395static const struct vtable *
13396dyna_push(struct parser_params *p)
13398 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
13399 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
13400 if (p->lvtbl->used) {
13401 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
13403 return p->lvtbl->args;
13407dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
13409 struct vtable *tmp = *vtblp;
13410 *vtblp = tmp->prev;
13411# if WARN_PAST_SCOPE
13412 if (p->past_scope_enabled) {
13413 tmp->prev = p->lvtbl->past;
13414 p->lvtbl->past = tmp;
13422dyna_pop_1(struct parser_params *p)
13424 struct vtable *tmp;
13426 if ((tmp = p->lvtbl->used) != 0) {
13427 warn_unused_var(p, p->lvtbl);
13428 p->lvtbl->used = p->lvtbl->used->prev;
13431 dyna_pop_vtable(p, &p->lvtbl->args);
13432 dyna_pop_vtable(p, &p->lvtbl->vars);
13436dyna_pop(struct parser_params *p, const struct vtable *lvargs)
13438 while (p->lvtbl->args != lvargs) {
13440 if (!p->lvtbl->args) {
13441 struct local_vars *local = p->lvtbl->prev;
13442 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13450dyna_in_block(struct parser_params *p)
13452 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
13456dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
13458 struct vtable *vars, *args, *used;
13461 args = p->lvtbl->args;
13462 vars = p->lvtbl->vars;
13463 used = p->lvtbl->used;
13465 while (!DVARS_TERMINAL_P(vars)) {
13466 if (vtable_included(args, id)) {
13469 if ((i = vtable_included(vars, id)) != 0) {
13470 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
13475 if (!vidrefp) used = 0;
13476 if (used) used = used->prev;
13479 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
13480 return rb_dvar_defined(id, p->parent_iseq);
13487dvar_defined(struct parser_params *p, ID id)
13489 return dvar_defined_ref(p, id, NULL);
13493dvar_curr(struct parser_params *p, ID id)
13495 return (vtable_included(p->lvtbl->args, id) ||
13496 vtable_included(p->lvtbl->vars, id));
13500reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
13503 "regexp encoding option '%c' differs from source encoding '%s'",
13504 c, rb_enc_name(rb_enc_get(str)));
13509rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13511 int c = RE_OPTION_ENCODING_IDX(options);
13515 rb_char_to_option_kcode(c, &opt, &idx);
13516 if (idx != ENCODING_GET(str) &&
13517 !is_ascii_string(str)) {
13520 ENCODING_SET(str, idx);
13522 else if (RE_OPTION_ENCODING_NONE(options)) {
13523 if (!ENCODING_IS_ASCII8BIT(str) &&
13524 !is_ascii_string(str)) {
13528 rb_enc_associate(str, rb_ascii8bit_encoding());
13530 else if (rb_is_usascii_enc(p->enc)) {
13531 if (!is_ascii_string(str)) {
13532 /* raise in re.c */
13533 rb_enc_associate(str, rb_usascii_encoding());
13536 rb_enc_associate(str, rb_ascii8bit_encoding());
13546reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13548 int c = rb_reg_fragment_setenc(p, str, options);
13549 if (c) reg_fragment_enc_error(p, str, c);
13553reg_fragment_check(struct parser_params* p, VALUE str, int options)
13556 reg_fragment_setenc(p, str, options);
13557 err = rb_reg_check_preprocess(str);
13559 err = rb_obj_as_string(err);
13560 compile_error(p, "%"PRIsVALUE, err);
13567 struct parser_params* parser;
13570 const YYLTYPE *loc;
13571} reg_named_capture_assign_t;
13574reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
13575 int back_num, int *back_refs, OnigRegex regex, void *arg0)
13577 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
13578 struct parser_params* p = arg->parser;
13579 rb_encoding *enc = arg->enc;
13580 long len = name_end - name;
13581 const char *s = (const char *)name;
13585 if (!len) return ST_CONTINUE;
13586 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13587 return ST_CONTINUE;
13589 var = intern_cstr(s, len, enc);
13590 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13591 if (!lvar_defined(p, var)) return ST_CONTINUE;
13593 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13594 succ = arg->succ_block;
13595 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13596 succ = block_append(p, succ, node);
13597 arg->succ_block = succ;
13598 return ST_CONTINUE;
13602reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13604 reg_named_capture_assign_t arg;
13607 arg.enc = rb_enc_get(regexp);
13608 arg.succ_block = 0;
13610 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13612 if (!arg.succ_block) return 0;
13613 return arg.succ_block->nd_next;
13617parser_reg_compile(struct parser_params* p, VALUE str, int options)
13619 reg_fragment_setenc(p, str, options);
13620 return rb_parser_reg_compile(p, str, options);
13624rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13626 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13630reg_compile(struct parser_params* p, VALUE str, int options)
13635 err = rb_errinfo();
13636 re = parser_reg_compile(p, str, options);
13638 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13639 rb_set_errinfo(err);
13640 compile_error(p, "%"PRIsVALUE, m);
13647parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13649 VALUE err = rb_errinfo();
13651 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13652 int c = rb_reg_fragment_setenc(p, str, options);
13653 if (c) reg_fragment_enc_error(p, str, c);
13654 re = rb_parser_reg_compile(p, str, options);
13656 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13657 rb_set_errinfo(err);
13665rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13667 struct parser_params *p;
13668 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13669 p->do_print = print;
13671 p->do_chomp = chomp;
13672 p->do_split = split;
13676parser_append_options(struct parser_params *p, NODE *node)
13678 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13679 const YYLTYPE *const LOC = &default_location;
13682 NODE *print = NEW_FCALL(rb_intern("print"),
13683 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13685 node = block_append(p, node, print);
13689 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
13692 ID ifs = rb_intern("$;");
13693 ID fields = rb_intern("$F");
13694 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13695 NODE *split = NEW_GASGN(fields,
13696 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13697 rb_intern("split"), args, LOC),
13699 node = block_append(p, split, node);
13702 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
13703 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
13704 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
13707 node = NEW_WHILE(NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
13716 /* just to suppress unused-function warnings */
13722internal_id(struct parser_params *p)
13724 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13726#endif /* !RIPPER */
13729parser_initialize(struct parser_params *p)
13731 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13732 p->command_start = TRUE;
13733 p->ruby_sourcefile_string = Qnil;
13734 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13736 p->delayed.token = Qnil;
13739 p->parsing_thread = Qnil;
13741 p->error_buffer = Qfalse;
13742 p->end_expect_token_locations = Qnil;
13746 p->debug_buffer = Qnil;
13747 p->debug_output = rb_ractor_stdout();
13748 p->enc = rb_utf8_encoding();
13752#define parser_mark ripper_parser_mark
13753#define parser_free ripper_parser_free
13757parser_mark(void *ptr)
13759 struct parser_params *p = (struct parser_params*)ptr;
13761 rb_gc_mark(p->lex.input);
13762 rb_gc_mark(p->lex.lastline);
13763 rb_gc_mark(p->lex.nextline);
13764 rb_gc_mark(p->ruby_sourcefile_string);
13765 rb_gc_mark((VALUE)p->lex.strterm);
13766 rb_gc_mark((VALUE)p->ast);
13767 rb_gc_mark(p->case_labels);
13768 rb_gc_mark(p->delayed.token);
13770 rb_gc_mark(p->debug_lines);
13771 rb_gc_mark(p->compile_option);
13772 rb_gc_mark(p->error_buffer);
13773 rb_gc_mark(p->end_expect_token_locations);
13774 rb_gc_mark(p->tokens);
13776 rb_gc_mark(p->value);
13777 rb_gc_mark(p->result);
13778 rb_gc_mark(p->parsing_thread);
13780 rb_gc_mark(p->debug_buffer);
13781 rb_gc_mark(p->debug_output);
13783 rb_gc_mark((VALUE)p->heap);
13788parser_free(void *ptr)
13790 struct parser_params *p = (struct parser_params*)ptr;
13791 struct local_vars *local, *prev;
13794 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13797 for (local = p->lvtbl; local; local = prev) {
13798 prev = local->prev;
13799 local_free(p, local);
13803 token_info *ptinfo;
13804 while ((ptinfo = p->token_info) != 0) {
13805 p->token_info = ptinfo->next;
13813parser_memsize(const void *ptr)
13815 struct parser_params *p = (struct parser_params*)ptr;
13816 struct local_vars *local;
13817 size_t size = sizeof(*p);
13820 for (local = p->lvtbl; local; local = local->prev) {
13821 size += sizeof(*local);
13822 if (local->vars) size += local->vars->capa * sizeof(ID);
13827static const rb_data_type_t parser_data_type = {
13838 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13842#undef rb_reserved_word
13844const struct kwtable *
13845rb_reserved_word(const char *str, unsigned int len)
13847 return reserved_word(str, len);
13853 struct parser_params *p;
13854 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13855 &parser_data_type, p);
13856 parser_initialize(p);
13861rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13863 struct parser_params *p;
13865 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13866 p->error_buffer = main ? Qfalse : Qnil;
13867 p->parent_iseq = base;
13872rb_parser_keep_script_lines(VALUE vparser)
13874 struct parser_params *p;
13876 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13877 p->keep_script_lines = 1;
13881rb_parser_error_tolerant(VALUE vparser)
13883 struct parser_params *p;
13885 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13886 p->error_tolerant = 1;
13887 p->end_expect_token_locations = rb_ary_new();
13891rb_parser_keep_tokens(VALUE vparser)
13893 struct parser_params *p;
13895 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13896 p->keep_tokens = 1;
13897 p->tokens = rb_ary_new();
13903#define rb_parser_end_seen_p ripper_parser_end_seen_p
13904#define rb_parser_encoding ripper_parser_encoding
13905#define rb_parser_get_yydebug ripper_parser_get_yydebug
13906#define rb_parser_set_yydebug ripper_parser_set_yydebug
13907#define rb_parser_get_debug_output ripper_parser_get_debug_output
13908#define rb_parser_set_debug_output ripper_parser_set_debug_output
13909static VALUE ripper_parser_end_seen_p(VALUE vparser);
13910static VALUE ripper_parser_encoding(VALUE vparser);
13911static VALUE ripper_parser_get_yydebug(VALUE self);
13912static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13913static VALUE ripper_parser_get_debug_output(VALUE self);
13914static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13918 * ripper.error? -> Boolean
13920 * Return true if parsed source has errors.
13923ripper_error_p(VALUE vparser)
13925 struct parser_params *p;
13927 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13928 return RBOOL(p->error_p);
13934 * ripper.end_seen? -> Boolean
13936 * Return true if parsed source ended by +\_\_END\_\_+.
13939rb_parser_end_seen_p(VALUE vparser)
13941 struct parser_params *p;
13943 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13944 return RBOOL(p->ruby__end__seen);
13949 * ripper.encoding -> encoding
13951 * Return encoding of the source.
13954rb_parser_encoding(VALUE vparser)
13956 struct parser_params *p;
13958 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13959 return rb_enc_from_encoding(p->enc);
13965 * ripper.yydebug -> true or false
13970rb_parser_get_yydebug(VALUE self)
13972 struct parser_params *p;
13974 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13975 return RBOOL(p->debug);
13981 * ripper.yydebug = flag
13986rb_parser_set_yydebug(VALUE self, VALUE flag)
13988 struct parser_params *p;
13990 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13991 p->debug = RTEST(flag);
13997 * ripper.debug_output -> obj
13999 * Get debug output.
14002rb_parser_get_debug_output(VALUE self)
14004 struct parser_params *p;
14006 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14007 return p->debug_output;
14012 * ripper.debug_output = obj
14014 * Set debug output.
14017rb_parser_set_debug_output(VALUE self, VALUE output)
14019 struct parser_params *p;
14021 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14022 return p->debug_output = output;
14027#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
14028/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
14029 * potential memory leak */
14030#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
14031#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
14032 (new)->cnt = (cnt), (ptr))
14035rb_parser_malloc(struct parser_params *p, size_t size)
14037 size_t cnt = HEAPCNT(1, size);
14038 rb_imemo_tmpbuf_t *n = NEWHEAP();
14039 void *ptr = xmalloc(size);
14041 return ADD2HEAP(n, cnt, ptr);
14045rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
14047 size_t cnt = HEAPCNT(nelem, size);
14048 rb_imemo_tmpbuf_t *n = NEWHEAP();
14049 void *ptr = xcalloc(nelem, size);
14051 return ADD2HEAP(n, cnt, ptr);
14055rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
14057 rb_imemo_tmpbuf_t *n;
14058 size_t cnt = HEAPCNT(1, size);
14060 if (ptr && (n = p->heap) != NULL) {
14062 if (n->ptr == ptr) {
14063 n->ptr = ptr = xrealloc(ptr, size);
14064 if (n->cnt) n->cnt = cnt;
14067 } while ((n = n->next) != NULL);
14070 ptr = xrealloc(ptr, size);
14071 return ADD2HEAP(n, cnt, ptr);
14075rb_parser_free(struct parser_params *p, void *ptr)
14077 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
14079 while ((n = *prev) != NULL) {
14080 if (n->ptr == ptr) {
14090rb_parser_printf(struct parser_params *p, const char *fmt, ...)
14093 VALUE mesg = p->debug_buffer;
14095 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
14097 rb_str_vcatf(mesg, fmt, ap);
14099 if (RSTRING_END(mesg)[-1] == '\n') {
14100 rb_io_write(p->debug_output, mesg);
14101 p->debug_buffer = Qnil;
14106parser_compile_error(struct parser_params *p, const char *fmt, ...)
14110 rb_io_flush(p->debug_output);
14114 rb_syntax_error_append(p->error_buffer,
14115 p->ruby_sourcefile_string,
14116 p->ruby_sourceline,
14117 rb_long2int(p->lex.pcur - p->lex.pbeg),
14123count_char(const char *str, int c)
14126 while (str[n] == c) ++n;
14131 * strip enclosing double-quotes, same as the default yytnamerr except
14132 * for that single-quotes matching back-quotes do not stop stripping.
14134 * "\"`class' keyword\"" => "`class' keyword"
14136RUBY_FUNC_EXPORTED size_t
14137rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
14139 if (*yystr == '"') {
14140 size_t yyn = 0, bquote = 0;
14141 const char *yyp = yystr;
14147 bquote = count_char(yyp+1, '`') + 1;
14148 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
14156 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
14157 if (yyres) memcpy(yyres + yyn, yyp, bquote);
14163 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
14164 if (yyres) memcpy(yyres + yyn, yyp, 3);
14169 goto do_not_strip_quotes;
14172 goto do_not_strip_quotes;
14175 if (*++yyp != '\\')
14176 goto do_not_strip_quotes;
14177 /* Fall through. */
14192 do_not_strip_quotes: ;
14195 if (!yyres) return strlen(yystr);
14197 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
14205ripper_validate_object(VALUE self, VALUE x)
14207 if (x == Qfalse) return x;
14208 if (x == Qtrue) return x;
14209 if (NIL_P(x)) return x;
14211 rb_raise(rb_eArgError, "Qundef given");
14212 if (FIXNUM_P(x)) return x;
14213 if (SYMBOL_P(x)) return x;
14214 switch (BUILTIN_TYPE(x)) {
14224 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
14225 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
14227 x = ((NODE *)x)->nd_rval;
14230 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
14231 (void *)x, rb_obj_classname(x));
14233 if (!RBASIC_CLASS(x)) {
14234 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
14235 (void *)x, rb_builtin_type_name(TYPE(x)));
14241#define validate(x) ((x) = get_value(x))
14244ripper_dispatch0(struct parser_params *p, ID mid)
14246 return rb_funcall(p->value, mid, 0);
14250ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
14253 return rb_funcall(p->value, mid, 1, a);
14257ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
14261 return rb_funcall(p->value, mid, 2, a, b);
14265ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
14270 return rb_funcall(p->value, mid, 3, a, b, c);
14274ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
14280 return rb_funcall(p->value, mid, 4, a, b, c, d);
14284ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
14291 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
14295ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
14304 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
14308ripper_get_id(VALUE v)
14311 if (!RB_TYPE_P(v, T_NODE)) return 0;
14313 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
14318ripper_get_value(VALUE v)
14321 if (UNDEF_P(v)) return Qnil;
14322 if (!RB_TYPE_P(v, T_NODE)) return v;
14324 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
14325 return nd->nd_rval;
14329ripper_error(struct parser_params *p)
14335ripper_compile_error(struct parser_params *p, const char *fmt, ...)
14340 va_start(args, fmt);
14341 str = rb_vsprintf(fmt, args);
14343 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
14348ripper_lex_get_generic(struct parser_params *p, VALUE src)
14350 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
14351 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
14352 rb_raise(rb_eTypeError,
14353 "gets returned %"PRIsVALUE" (expected String or nil)",
14354 rb_obj_class(line));
14360ripper_lex_io_get(struct parser_params *p, VALUE src)
14362 return rb_io_gets(src);
14366ripper_s_allocate(VALUE klass)
14368 struct parser_params *p;
14369 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
14370 &parser_data_type, p);
14375#define ripper_initialized_p(r) ((r)->lex.input != 0)
14379 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
14381 * Create a new Ripper object.
14382 * _src_ must be a String, an IO, or an Object which has #gets method.
14384 * This method does not starts parsing.
14385 * See also Ripper#parse and Ripper.parse.
14388ripper_initialize(int argc, VALUE *argv, VALUE self)
14390 struct parser_params *p;
14391 VALUE src, fname, lineno;
14393 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14394 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
14395 if (RB_TYPE_P(src, T_FILE)) {
14396 p->lex.gets = ripper_lex_io_get;
14398 else if (rb_respond_to(src, id_gets)) {
14399 p->lex.gets = ripper_lex_get_generic;
14403 p->lex.gets = lex_get_str;
14405 p->lex.input = src;
14407 if (NIL_P(fname)) {
14408 fname = STR_NEW2("(ripper)");
14412 StringValueCStr(fname);
14413 fname = rb_str_new_frozen(fname);
14415 parser_initialize(p);
14417 p->ruby_sourcefile_string = fname;
14418 p->ruby_sourcefile = RSTRING_PTR(fname);
14419 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
14425ripper_parse0(VALUE parser_v)
14427 struct parser_params *p;
14429 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14431 p->ast = rb_ast_new();
14432 ripper_yyparse((void*)p);
14433 rb_ast_dispose(p->ast);
14439ripper_ensure(VALUE parser_v)
14441 struct parser_params *p;
14443 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14444 p->parsing_thread = Qnil;
14452 * Start parsing and returns the value of the root action.
14455ripper_parse(VALUE self)
14457 struct parser_params *p;
14459 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14460 if (!ripper_initialized_p(p)) {
14461 rb_raise(rb_eArgError, "method called for uninitialized object");
14463 if (!NIL_P(p->parsing_thread)) {
14464 if (p->parsing_thread == rb_thread_current())
14465 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
14467 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
14469 p->parsing_thread = rb_thread_current();
14470 rb_ensure(ripper_parse0, self, ripper_ensure, self);
14477 * ripper.column -> Integer
14479 * Return column number of current parsing line.
14480 * This number starts from 0.
14483ripper_column(VALUE self)
14485 struct parser_params *p;
14488 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14489 if (!ripper_initialized_p(p)) {
14490 rb_raise(rb_eArgError, "method called for uninitialized object");
14492 if (NIL_P(p->parsing_thread)) return Qnil;
14493 col = p->lex.ptok - p->lex.pbeg;
14494 return LONG2NUM(col);
14499 * ripper.filename -> String
14501 * Return current parsing filename.
14504ripper_filename(VALUE self)
14506 struct parser_params *p;
14508 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14509 if (!ripper_initialized_p(p)) {
14510 rb_raise(rb_eArgError, "method called for uninitialized object");
14512 return p->ruby_sourcefile_string;
14517 * ripper.lineno -> Integer
14519 * Return line number of current parsing line.
14520 * This number starts from 1.
14523ripper_lineno(VALUE self)
14525 struct parser_params *p;
14527 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14528 if (!ripper_initialized_p(p)) {
14529 rb_raise(rb_eArgError, "method called for uninitialized object");
14531 if (NIL_P(p->parsing_thread)) return Qnil;
14532 return INT2NUM(p->ruby_sourceline);
14537 * ripper.state -> Integer
14539 * Return scanner state of current token.
14542ripper_state(VALUE self)
14544 struct parser_params *p;
14546 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14547 if (!ripper_initialized_p(p)) {
14548 rb_raise(rb_eArgError, "method called for uninitialized object");
14550 if (NIL_P(p->parsing_thread)) return Qnil;
14551 return INT2NUM(p->lex.state);
14556 * ripper.token -> String
14558 * Return the current token string.
14561ripper_token(VALUE self)
14563 struct parser_params *p;
14566 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14567 if (!ripper_initialized_p(p)) {
14568 rb_raise(rb_eArgError, "method called for uninitialized object");
14570 if (NIL_P(p->parsing_thread)) return Qnil;
14571 pos = p->lex.ptok - p->lex.pbeg;
14572 len = p->lex.pcur - p->lex.ptok;
14573 return rb_str_subseq(p->lex.lastline, pos, len);
14579ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
14582 if (UNDEF_P(obj)) {
14583 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
14590ripper_value(VALUE self, VALUE obj)
14592 return ULONG2NUM(obj);
14598 * Ripper.lex_state_name(integer) -> string
14600 * Returns a string representation of lex_state.
14603ripper_lex_state_name(VALUE self, VALUE state)
14605 return rb_parser_lex_state_name(NUM2INT(state));
14611 ripper_init_eventids1();
14612 ripper_init_eventids2();
14613 id_warn = rb_intern_const("warn");
14614 id_warning = rb_intern_const("warning");
14615 id_gets = rb_intern_const("gets");
14616 id_assoc = rb_intern_const("=>");
14618 (void)yystpcpy; /* may not used in newer bison */
14628 Ripper = rb_define_class("Ripper", rb_cObject);
14629 /* version of Ripper */
14630 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14631 rb_define_alloc_func(Ripper, ripper_s_allocate);
14632 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14633 rb_define_method(Ripper, "parse", ripper_parse, 0);
14634 rb_define_method(Ripper, "column", ripper_column, 0);
14635 rb_define_method(Ripper, "filename", ripper_filename, 0);
14636 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14637 rb_define_method(Ripper, "state", ripper_state, 0);
14638 rb_define_method(Ripper, "token", ripper_token, 0);
14639 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14640 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14641 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14642 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14643 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14644 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14645 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14647 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14648 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14649 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14652 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14653 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14655 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14657<% @exprs.each do |expr, desc| -%>
14659 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14661 ripper_init_eventids1_table(Ripper);
14662 ripper_init_eventids2_table(Ripper);
14665 /* Hack to let RDoc document SCRIPT_LINES__ */
14668 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14669 * after the assignment will be added as an Array of lines with the file
14672 rb_define_global_const("SCRIPT_LINES__", Qnil);
14681 * c-file-style: "ruby"