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, ID2VAL(idCOLON2), $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, ID2VAL(idCOLON2), $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, ID2VAL(idCOLON2), $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, ID2VAL(idCOLON2), $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, ID2VAL(idCOLON2), $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, ID2VAL(idCOLON2), $3), $4) %*/
4246 | primary_value tCOLON2 operation3
4249 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4251 /*% ripper: call!($1, ID2VAL(idCOLON2), $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, ID2VAL(idCOLON2), 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);
7263/* return value is for ?\u3042 */
7265tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7266 int term, int symbol_literal, int regexp_literal)
7269 * If `term` is not -1, then we allow multiple codepoints in \u{}
7270 * upto `term` byte, otherwise we're parsing a character literal.
7271 * And then add the codepoints to the current token.
7273 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7275 const int open_brace = '{', close_brace = '}';
7277 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7279 if (peek(p, open_brace)) { /* handle \u{...} form */
7280 const char *second = NULL;
7281 int c, last = nextc(p);
7282 if (p->lex.pcur >= p->lex.pend) goto unterminated;
7283 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
7284 while (c != close_brace) {
7285 if (c == term) goto unterminated;
7286 if (second == multiple_codepoints)
7287 second = p->lex.pcur;
7288 if (regexp_literal) tokadd(p, last);
7289 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7292 while (ISSPACE(c = *p->lex.pcur)) {
7293 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
7296 if (term == -1 && !second)
7297 second = multiple_codepoints;
7300 if (c != close_brace) {
7303 yyerror0("unterminated Unicode escape");
7306 if (second && second != multiple_codepoints) {
7307 const char *pcur = p->lex.pcur;
7308 p->lex.pcur = second;
7309 dispatch_scan_event(p, tSTRING_CONTENT);
7312 yyerror0(multiple_codepoints);
7316 if (regexp_literal) tokadd(p, close_brace);
7319 else { /* handle \uxxxx form */
7320 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7327#define ESCAPE_CONTROL 1
7328#define ESCAPE_META 2
7331read_escape(struct parser_params *p, int flags, rb_encoding **encp)
7336 switch (c = nextc(p)) {
7337 case '\\': /* Backslash */
7340 case 'n': /* newline */
7343 case 't': /* horizontal tab */
7346 case 'r': /* carriage-return */
7349 case 'f': /* form-feed */
7352 case 'v': /* vertical tab */
7355 case 'a': /* alarm(bell) */
7358 case 'e': /* escape */
7361 case '0': case '1': case '2': case '3': /* octal constant */
7362 case '4': case '5': case '6': case '7':
7364 c = scan_oct(p->lex.pcur, 3, &numlen);
7365 p->lex.pcur += numlen;
7368 case 'x': /* hex constant */
7369 c = tok_hex(p, &numlen);
7370 if (numlen == 0) return 0;
7373 case 'b': /* backspace */
7376 case 's': /* space */
7380 if (flags & ESCAPE_META) goto eof;
7381 if ((c = nextc(p)) != '-') {
7384 if ((c = nextc(p)) == '\\') {
7390 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
7392 else if (c == -1 || !ISASCII(c)) goto eof;
7394 int c2 = escaped_control_code(c);
7396 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
7397 WARN_SPACE_CHAR(c2, "\\M-");
7400 WARN_SPACE_CHAR(c2, "\\C-\\M-");
7403 else if (ISCNTRL(c)) goto eof;
7404 return ((c & 0xff) | 0x80);
7408 if ((c = nextc(p)) != '-') {
7412 if (flags & ESCAPE_CONTROL) goto eof;
7413 if ((c = nextc(p))== '\\') {
7419 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
7423 else if (c == -1 || !ISASCII(c)) goto eof;
7425 int c2 = escaped_control_code(c);
7428 if (flags & ESCAPE_META) {
7429 WARN_SPACE_CHAR(c2, "\\M-");
7432 WARN_SPACE_CHAR(c2, "");
7436 if (flags & ESCAPE_META) {
7437 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7440 WARN_SPACE_CHAR(c2, "\\C-");
7444 else if (ISCNTRL(c)) goto eof;
7450 yyerror0("Invalid escape character syntax");
7460tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7462 int len = rb_enc_codelen(c, enc);
7463 rb_enc_mbcput(c, tokspace(p, len), enc);
7467tokadd_escape(struct parser_params *p, rb_encoding **encp)
7472 switch (c = nextc(p)) {
7474 return 0; /* just ignore */
7476 case '0': case '1': case '2': case '3': /* octal constant */
7477 case '4': case '5': case '6': case '7':
7479 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7480 if (numlen == 0) goto eof;
7481 p->lex.pcur += numlen;
7482 tokcopy(p, (int)numlen + 1);
7486 case 'x': /* hex constant */
7488 tok_hex(p, &numlen);
7489 if (numlen == 0) return -1;
7490 tokcopy(p, (int)numlen + 2);
7496 yyerror0("Invalid escape character syntax");
7508regx_options(struct parser_params *p)
7516 while (c = nextc(p), ISALPHA(c)) {
7518 options |= RE_OPTION_ONCE;
7520 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7522 if (kc != rb_ascii8bit_encindex()) kcode = c;
7536 YYLTYPE loc = RUBY_INIT_YYLLOC();
7538 compile_error(p, "unknown regexp option%s - %*s",
7539 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7540 parser_show_error_line(p, &loc);
7542 return options | RE_OPTION_ENCODING(kcode);
7546tokadd_mbchar(struct parser_params *p, int c)
7548 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7549 if (len < 0) return -1;
7551 p->lex.pcur += --len;
7552 if (len > 0) tokcopy(p, len);
7557simple_re_meta(int c)
7560 case '$': case '*': case '+': case '.':
7561 case '?': case '^': case '|':
7562 case ')': case ']': case '}': case '>':
7570parser_update_heredoc_indent(struct parser_params *p, int c)
7572 if (p->heredoc_line_indent == -1) {
7573 if (c == '\n') p->heredoc_line_indent = 0;
7577 p->heredoc_line_indent++;
7580 else if (c == '\t') {
7581 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7582 p->heredoc_line_indent = w * TAB_WIDTH;
7585 else if (c != '\n') {
7586 if (p->heredoc_indent > p->heredoc_line_indent) {
7587 p->heredoc_indent = p->heredoc_line_indent;
7589 p->heredoc_line_indent = -1;
7596parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7598 YYLTYPE loc = RUBY_INIT_YYLLOC();
7599 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7600 compile_error(p, "%s mixed within %s source", n1, n2);
7601 parser_show_error_line(p, &loc);
7605parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7607 const char *pos = p->lex.pcur;
7609 parser_mixed_error(p, enc1, enc2);
7614tokadd_string(struct parser_params *p,
7615 int func, int term, int paren, long *nest,
7616 rb_encoding **encp, rb_encoding **enc)
7621 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
7622 int top_of_line = FALSE;
7625#define mixed_error(enc1, enc2) \
7626 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7627#define mixed_escape(beg, enc1, enc2) \
7628 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7630 while ((c = nextc(p)) != -1) {
7631 if (p->heredoc_indent > 0) {
7632 parser_update_heredoc_indent(p, c);
7635 if (top_of_line && heredoc_end == p->ruby_sourceline) {
7641 if (paren && c == paren) {
7644 else if (c == term) {
7645 if (!nest || !*nest) {
7651 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7652 int c2 = *p->lex.pcur;
7653 if (c2 == '$' || c2 == '@' || c2 == '{') {
7658 else if (c == '\\') {
7662 if (func & STR_FUNC_QWORDS) break;
7663 if (func & STR_FUNC_EXPAND) {
7664 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7675 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7679 if ((func & STR_FUNC_EXPAND) == 0) {
7683 tokadd_utf8(p, enc, term,
7684 func & STR_FUNC_SYMBOL,
7685 func & STR_FUNC_REGEXP);
7689 if (c == -1) return -1;
7691 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7694 if (func & STR_FUNC_REGEXP) {
7700 c = read_escape(p, 0, enc);
7704 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7705 for (i = 0; i < 4; i++) {
7706 tokadd(p, escbuf[i]);
7712 if (c == term && !simple_re_meta(c)) {
7717 if ((c = tokadd_escape(p, enc)) < 0)
7719 if (*enc && *enc != *encp) {
7720 mixed_escape(p->lex.ptok+2, *enc, *encp);
7724 else if (func & STR_FUNC_EXPAND) {
7726 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7727 c = read_escape(p, 0, enc);
7729 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7730 /* ignore backslashed spaces in %w */
7732 else if (c != term && !(paren && c == paren)) {
7739 else if (!parser_isascii(p)) {
7744 else if (*enc != *encp) {
7745 mixed_error(*enc, *encp);
7748 if (tokadd_mbchar(p, c) == -1) return -1;
7751 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7759 else if (*enc != *encp) {
7760 mixed_error(*enc, *encp);
7766 top_of_line = (c == '\n');
7770 if (*enc) *encp = *enc;
7774static inline rb_strterm_t *
7775new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7777 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7780/* imemo_parser_strterm for literal */
7781#define NEW_STRTERM(func, term, paren) \
7782 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7786flush_string_content(struct parser_params *p, rb_encoding *enc)
7788 VALUE content = yylval.val;
7789 if (!ripper_is_node_yylval(content))
7790 content = ripper_new_yylval(p, 0, 0, content);
7791 if (has_delayed_token(p)) {
7792 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7794 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7796 dispatch_delayed_token(p, tSTRING_CONTENT);
7797 p->lex.ptok = p->lex.pcur;
7798 RNODE(content)->nd_rval = yylval.val;
7800 dispatch_scan_event(p, tSTRING_CONTENT);
7801 if (yylval.val != content)
7802 RNODE(content)->nd_rval = yylval.val;
7803 yylval.val = content;
7807flush_string_content(struct parser_params *p, rb_encoding *enc)
7809 if (has_delayed_token(p)) {
7810 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7812 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7813 p->delayed.end_line = p->ruby_sourceline;
7814 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
7816 dispatch_delayed_token(p, tSTRING_CONTENT);
7817 p->lex.ptok = p->lex.pcur;
7819 dispatch_scan_event(p, tSTRING_CONTENT);
7823RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7824/* this can be shared with ripper, since it's independent from struct
7827#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7828#define SPECIAL_PUNCT(idx) ( \
7829 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7830 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7831 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7832 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7833 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7835const unsigned int ruby_global_name_punct_bits[] = {
7844static enum yytokentype
7845parser_peek_variable_name(struct parser_params *p)
7848 const char *ptr = p->lex.pcur;
7850 if (ptr + 1 >= p->lex.pend) return 0;
7854 if ((c = *ptr) == '-') {
7855 if (++ptr >= p->lex.pend) return 0;
7858 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7859 return tSTRING_DVAR;
7863 if ((c = *ptr) == '@') {
7864 if (++ptr >= p->lex.pend) return 0;
7870 p->command_start = TRUE;
7871 return tSTRING_DBEG;
7875 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7876 return tSTRING_DVAR;
7880#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7881#define IS_END() IS_lex_state(EXPR_END_ANY)
7882#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7883#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7884#define IS_LABEL_POSSIBLE() (\
7885 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7887#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7888#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7890static inline enum yytokentype
7891parser_string_term(struct parser_params *p, int func)
7894 if (func & STR_FUNC_REGEXP) {
7895 set_yylval_num(regx_options(p));
7896 dispatch_scan_event(p, tREGEXP_END);
7897 SET_LEX_STATE(EXPR_END);
7900 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7902 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7905 SET_LEX_STATE(EXPR_END);
7909static enum yytokentype
7910parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7912 int func = (int)quote->u1.func;
7913 int term = (int)quote->u3.term;
7914 int paren = (int)quote->u2.paren;
7916 rb_encoding *enc = p->enc;
7917 rb_encoding *base_enc = 0;
7920 if (func & STR_FUNC_TERM) {
7921 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7922 SET_LEX_STATE(EXPR_END);
7924 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7927 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7928 do {c = nextc(p);} while (ISSPACE(c));
7931 if (func & STR_FUNC_LIST) {
7932 quote->u1.func &= ~STR_FUNC_LIST;
7935 if (c == term && !quote->u0.nest) {
7936 if (func & STR_FUNC_QWORDS) {
7937 quote->u1.func |= STR_FUNC_TERM;
7938 pushback(p, c); /* dispatch the term at tSTRING_END */
7939 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7942 return parser_string_term(p, func);
7946 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
7950 if ((func & STR_FUNC_EXPAND) && c == '#') {
7951 int t = parser_peek_variable_name(p);
7957 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7958 &enc, &base_enc) == -1) {
7961# define unterminated_literal(mesg) yyerror0(mesg)
7963# define unterminated_literal(mesg) compile_error(p, mesg)
7965 literal_flush(p, p->lex.pcur);
7966 if (func & STR_FUNC_QWORDS) {
7967 /* no content to add, bailing out here */
7968 unterminated_literal("unterminated list meets end of file");
7972 if (func & STR_FUNC_REGEXP) {
7973 unterminated_literal("unterminated regexp meets end of file");
7976 unterminated_literal("unterminated string meets end of file");
7978 quote->u1.func |= STR_FUNC_TERM;
7983 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7984 set_yylval_str(lit);
7985 flush_string_content(p, enc);
7987 return tSTRING_CONTENT;
7990static enum yytokentype
7991heredoc_identifier(struct parser_params *p)
7994 * term_len is length of `<<"END"` except `END`,
7995 * in this case term_len is 4 (<, <, " and ").
7997 long len, offset = p->lex.pcur - p->lex.pbeg;
7998 int c = nextc(p), term, func = 0, quote = 0;
7999 enum yytokentype token = tSTRING_BEG;
8004 func = STR_FUNC_INDENT;
8007 else if (c == '~') {
8009 func = STR_FUNC_INDENT;
8015 func |= str_squote; goto quoted;
8017 func |= str_dquote; goto quoted;
8019 token = tXSTRING_BEG;
8020 func |= str_xquote; goto quoted;
8027 while ((c = nextc(p)) != term) {
8028 if (c == -1 || c == '\r' || c == '\n') {
8029 yyerror0("unterminated here document identifier");
8036 if (!parser_is_identchar(p)) {
8038 if (func & STR_FUNC_INDENT) {
8039 pushback(p, indent > 0 ? '~' : '-');
8045 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8046 if (n < 0) return 0;
8048 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8053 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8054 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8055 yyerror0("too long here document identifier");
8056 dispatch_scan_event(p, tHEREDOC_BEG);
8059 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
8060 p->lex.strterm->flags |= STRTERM_HEREDOC;
8061 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8062 here->offset = offset;
8063 here->sourceline = p->ruby_sourceline;
8064 here->length = (int)len;
8065 here->quote = quote;
8069 p->heredoc_indent = indent;
8070 p->heredoc_line_indent = 0;
8075heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8080 line = here->lastline;
8081 p->lex.lastline = line;
8082 p->lex.pbeg = RSTRING_PTR(line);
8083 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8084 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8085 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8086 p->heredoc_end = p->ruby_sourceline;
8087 p->ruby_sourceline = (int)here->sourceline;
8088 if (p->eofp) p->lex.nextline = Qnil;
8093dedent_string(VALUE string, int width)
8099 RSTRING_GETMEM(string, str, len);
8100 for (i = 0; i < len && col < width; i++) {
8101 if (str[i] == ' ') {
8104 else if (str[i] == '\t') {
8105 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8106 if (n > width) break;
8114 rb_str_modify(string);
8115 str = RSTRING_PTR(string);
8116 if (RSTRING_LEN(string) != len)
8117 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8118 MEMMOVE(str, str + i, char, len - i);
8119 rb_str_set_len(string, len - i);
8125heredoc_dedent(struct parser_params *p, NODE *root)
8127 NODE *node, *str_node, *prev_node;
8128 int indent = p->heredoc_indent;
8131 if (indent <= 0) return root;
8132 p->heredoc_indent = 0;
8133 if (!root) return root;
8135 prev_node = node = str_node = root;
8136 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
8139 VALUE lit = str_node->nd_lit;
8140 if (str_node->flags & NODE_FL_NEWLINE) {
8141 dedent_string(lit, indent);
8146 else if (!literal_concat0(p, prev_lit, lit)) {
8150 NODE *end = node->nd_end;
8151 node = prev_node->nd_next = node->nd_next;
8153 if (nd_type_p(prev_node, NODE_DSTR))
8154 nd_set_type(prev_node, NODE_STR);
8162 while ((node = (prev_node = node)->nd_next) != 0) {
8164 if (!nd_type_p(node, NODE_LIST)) break;
8165 if ((str_node = node->nd_head) != 0) {
8166 enum node_type type = nd_type(str_node);
8167 if (type == NODE_STR || type == NODE_DSTR) break;
8177heredoc_dedent(struct parser_params *p, VALUE array)
8179 int indent = p->heredoc_indent;
8181 if (indent <= 0) return array;
8182 p->heredoc_indent = 0;
8183 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8189 * Ripper.dedent_string(input, width) -> Integer
8191 * USE OF RIPPER LIBRARY ONLY.
8193 * Strips up to +width+ leading whitespaces from +input+,
8194 * and returns the stripped column width.
8197parser_dedent_string(VALUE self, VALUE input, VALUE width)
8202 wid = NUM2UINT(width);
8203 col = dedent_string(input, wid);
8204 return INT2NUM(col);
8209whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8211 const char *ptr = p->lex.pbeg;
8215 while (*ptr && ISSPACE(*ptr)) ptr++;
8217 n = p->lex.pend - (ptr + len);
8218 if (n < 0) return FALSE;
8219 if (n > 0 && ptr[len] != '\n') {
8220 if (ptr[len] != '\r') return FALSE;
8221 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
8223 return strncmp(eos, ptr, len) == 0;
8227word_match_p(struct parser_params *p, const char *word, long len)
8229 if (strncmp(p->lex.pcur, word, len)) return 0;
8230 if (p->lex.pcur + len == p->lex.pend) return 1;
8231 int c = (unsigned char)p->lex.pcur[len];
8232 if (ISSPACE(c)) return 1;
8234 case '\0': case '\004': case '\032': return 1;
8239#define NUM_SUFFIX_R (1<<0)
8240#define NUM_SUFFIX_I (1<<1)
8241#define NUM_SUFFIX_ALL 3
8244number_literal_suffix(struct parser_params *p, int mask)
8247 const char *lastp = p->lex.pcur;
8249 while ((c = nextc(p)) != -1) {
8250 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8251 result |= (mask & NUM_SUFFIX_I);
8252 mask &= ~NUM_SUFFIX_I;
8253 /* r after i, rational of complex is disallowed */
8254 mask &= ~NUM_SUFFIX_R;
8257 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8258 result |= (mask & NUM_SUFFIX_R);
8259 mask &= ~NUM_SUFFIX_R;
8262 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8263 p->lex.pcur = lastp;
8264 literal_flush(p, p->lex.pcur);
8273static enum yytokentype
8274set_number_literal(struct parser_params *p, VALUE v,
8275 enum yytokentype type, int suffix)
8277 if (suffix & NUM_SUFFIX_I) {
8278 v = rb_complex_raw(INT2FIX(0), v);
8281 set_yylval_literal(v);
8282 SET_LEX_STATE(EXPR_END);
8286static enum yytokentype
8287set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8289 enum yytokentype type = tINTEGER;
8290 if (suffix & NUM_SUFFIX_R) {
8291 v = rb_rational_raw1(v);
8294 return set_number_literal(p, v, type, suffix);
8299dispatch_heredoc_end(struct parser_params *p)
8302 if (has_delayed_token(p))
8303 dispatch_delayed_token(p, tSTRING_CONTENT);
8304 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8305 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8306 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8312#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8314parser_dispatch_heredoc_end(struct parser_params *p, int line)
8316 if (has_delayed_token(p))
8317 dispatch_delayed_token(p, tSTRING_CONTENT);
8319 if (p->keep_tokens) {
8320 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8321 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8322 parser_append_tokens(p, str, tHEREDOC_END, line);
8325 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8331static enum yytokentype
8332here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8334 int c, func, indent = 0;
8335 const char *eos, *ptr, *ptr_end;
8338 rb_encoding *enc = p->enc;
8339 rb_encoding *base_enc = 0;
8342 eos = RSTRING_PTR(here->lastline) + here->offset;
8344 indent = (func = here->func) & STR_FUNC_INDENT;
8346 if ((c = nextc(p)) == -1) {
8349 if (!has_delayed_token(p)) {
8350 dispatch_scan_event(p, tSTRING_CONTENT);
8353 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
8354 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
8355 int cr = ENC_CODERANGE_UNKNOWN;
8356 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
8357 if (cr != ENC_CODERANGE_7BIT &&
8358 rb_is_usascii_enc(p->enc) &&
8359 enc != rb_utf8_encoding()) {
8360 enc = rb_ascii8bit_encoding();
8363 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8365 dispatch_delayed_token(p, tSTRING_CONTENT);
8369 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8370 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
8374 SET_LEX_STATE(EXPR_END);
8379 /* not beginning of line, cannot be the terminator */
8381 else if (p->heredoc_line_indent == -1) {
8382 /* `heredoc_line_indent == -1` means
8383 * - "after an interpolation in the same line", or
8384 * - "in a continuing line"
8386 p->heredoc_line_indent = 0;
8388 else if (whole_match_p(p, eos, len, indent)) {
8389 dispatch_heredoc_end(p);
8391 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8394 SET_LEX_STATE(EXPR_END);
8398 if (!(func & STR_FUNC_EXPAND)) {
8400 ptr = RSTRING_PTR(p->lex.lastline);
8401 ptr_end = p->lex.pend;
8402 if (ptr_end > ptr) {
8403 switch (ptr_end[-1]) {
8405 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
8414 if (p->heredoc_indent > 0) {
8416 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
8418 p->heredoc_line_indent = 0;
8422 rb_str_cat(str, ptr, ptr_end - ptr);
8424 str = STR_NEW(ptr, ptr_end - ptr);
8425 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
8427 if (p->heredoc_indent > 0) {
8430 if (nextc(p) == -1) {
8436 } while (!whole_match_p(p, eos, len, indent));
8439 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
8442 int t = parser_peek_variable_name(p);
8443 if (p->heredoc_line_indent != -1) {
8444 if (p->heredoc_indent > p->heredoc_line_indent) {
8445 p->heredoc_indent = p->heredoc_line_indent;
8447 p->heredoc_line_indent = -1;
8456 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
8457 if (p->eofp) goto error;
8461 if (c == '\\') p->heredoc_line_indent = -1;
8463 str = STR_NEW3(tok(p), toklen(p), enc, func);
8465 set_yylval_str(str);
8467 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8469 flush_string_content(p, enc);
8470 return tSTRING_CONTENT;
8472 tokadd(p, nextc(p));
8473 if (p->heredoc_indent > 0) {
8477 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8478 if ((c = nextc(p)) == -1) goto error;
8479 } while (!whole_match_p(p, eos, len, indent));
8480 str = STR_NEW3(tok(p), toklen(p), enc, func);
8482 dispatch_heredoc_end(p);
8484 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8487 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8489 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8490 set_yylval_str(str);
8492 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8494 return tSTRING_CONTENT;
8500arg_ambiguous(struct parser_params *p, char c)
8504 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8507 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8510 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8517formal_argument(struct parser_params *p, ID lhs)
8519formal_argument(struct parser_params *p, VALUE lhs)
8522 ID id = get_id(lhs);
8524 switch (id_type(id)) {
8528# define ERR(mesg) yyerror0(mesg)
8530# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8533 ERR("formal argument cannot be a constant");
8536 ERR("formal argument cannot be an instance variable");
8539 ERR("formal argument cannot be a global variable");
8542 ERR("formal argument cannot be a class variable");
8545 ERR("formal argument must be local variable");
8549 shadowing_lvar(p, id);
8554lvar_defined(struct parser_params *p, ID id)
8556 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8559/* emacsen -*- hack */
8561parser_encode_length(struct parser_params *p, const char *name, long len)
8565 if (len > 5 && name[nlen = len - 5] == '-') {
8566 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8569 if (len > 4 && name[nlen = len - 4] == '-') {
8570 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8572 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8573 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8574 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8581parser_set_encode(struct parser_params *p, const char *name)
8583 int idx = rb_enc_find_index(name);
8588 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8590 excargs[0] = rb_eArgError;
8591 excargs[2] = rb_make_backtrace();
8592 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8593 rb_exc_raise(rb_make_exception(3, excargs));
8595 enc = rb_enc_from_index(idx);
8596 if (!rb_enc_asciicompat(enc)) {
8597 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8602 if (p->debug_lines) {
8603 VALUE lines = p->debug_lines;
8604 long i, n = RARRAY_LEN(lines);
8605 for (i = 0; i < n; ++i) {
8606 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8613comment_at_top(struct parser_params *p)
8615 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8616 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8617 while (ptr < ptr_end) {
8618 if (!ISSPACE(*ptr)) return 0;
8624typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8625typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8627static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8630magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8632 if (!comment_at_top(p)) {
8635 parser_set_encode(p, val);
8639parser_get_bool(struct parser_params *p, const char *name, const char *val)
8643 if (STRCASECMP(val, "true") == 0) {
8648 if (STRCASECMP(val, "false") == 0) {
8653 return parser_invalid_pragma_value(p, name, val);
8657parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8659 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8664parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8666 int b = parser_get_bool(p, name, val);
8667 if (b >= 0) p->token_info_enabled = b;
8671parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8675 if (p->token_seen) {
8676 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8680 b = parser_get_bool(p, name, val);
8683 if (!p->compile_option)
8684 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8685 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8690parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8692 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8693 if (*s == ' ' || *s == '\t') continue;
8694 if (*s == '#') break;
8695 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8701 if (STRCASECMP(val, "none") == 0) {
8702 p->ctxt.shareable_constant_value = shareable_none;
8707 if (STRCASECMP(val, "literal") == 0) {
8708 p->ctxt.shareable_constant_value = shareable_literal;
8713 if (STRCASECMP(val, "experimental_copy") == 0) {
8714 p->ctxt.shareable_constant_value = shareable_copy;
8717 if (STRCASECMP(val, "experimental_everything") == 0) {
8718 p->ctxt.shareable_constant_value = shareable_everything;
8723 parser_invalid_pragma_value(p, name, val);
8728parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8730 int b = parser_get_bool(p, name, val);
8731 if (b >= 0) p->past_scope_enabled = b;
8735struct magic_comment {
8737 rb_magic_comment_setter_t func;
8738 rb_magic_comment_length_t length;
8741static const struct magic_comment magic_comments[] = {
8742 {"coding", magic_comment_encoding, parser_encode_length},
8743 {"encoding", magic_comment_encoding, parser_encode_length},
8744 {"frozen_string_literal", parser_set_compile_option_flag},
8745 {"shareable_constant_value", parser_set_shareable_constant_value},
8746 {"warn_indent", parser_set_token_info},
8748 {"warn_past_scope", parser_set_past_scope},
8753magic_comment_marker(const char *str, long len)
8760 if (str[i-1] == '*' && str[i-2] == '-') {
8766 if (i + 1 >= len) return 0;
8767 if (str[i+1] != '-') {
8770 else if (str[i-1] != '-') {
8786parser_magic_comment(struct parser_params *p, const char *str, long len)
8789 VALUE name = 0, val = 0;
8790 const char *beg, *end, *vbeg, *vend;
8791#define str_copy(_s, _p, _n) ((_s) \
8792 ? (void)(rb_str_resize((_s), (_n)), \
8793 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8794 : (void)((_s) = STR_NEW((_p), (_n))))
8796 if (len <= 7) return FALSE;
8797 if (!!(beg = magic_comment_marker(str, len))) {
8798 if (!(end = magic_comment_marker(beg, str + len - beg)))
8802 len = end - beg - 3;
8805 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8807 const struct magic_comment *mc = magic_comments;
8812 for (; len > 0 && *str; str++, --len) {
8814 case '\'': case '"': case ':': case ';':
8817 if (!ISSPACE(*str)) break;
8819 for (beg = str; len > 0; str++, --len) {
8821 case '\'': case '"': case ':': case ';':
8824 if (ISSPACE(*str)) break;
8829 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8832 if (!indicator) return FALSE;
8836 do str++; while (--len > 0 && ISSPACE(*str));
8839 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8852 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8856 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8859 while (len > 0 && (ISSPACE(*str))) --len, str++;
8860 if (len) return FALSE;
8864 str_copy(name, beg, n);
8865 s = RSTRING_PTR(name);
8866 for (i = 0; i < n; ++i) {
8867 if (s[i] == '-') s[i] = '_';
8870 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8873 n = (*mc->length)(p, vbeg, n);
8875 str_copy(val, vbeg, n);
8876 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8879 } while (++mc < magic_comments + numberof(magic_comments));
8881 str_copy(val, vbeg, vend - vbeg);
8882 dispatch2(magic_comment, name, val);
8890set_file_encoding(struct parser_params *p, const char *str, const char *send)
8893 const char *beg = str;
8897 if (send - str <= 6) return;
8899 case 'C': case 'c': str += 6; continue;
8900 case 'O': case 'o': str += 5; continue;
8901 case 'D': case 'd': str += 4; continue;
8902 case 'I': case 'i': str += 3; continue;
8903 case 'N': case 'n': str += 2; continue;
8904 case 'G': case 'g': str += 1; continue;
8911 if (ISSPACE(*str)) break;
8914 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8919 if (++str >= send) return;
8920 } while (ISSPACE(*str));
8922 if (*str != '=' && *str != ':') return;
8927 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8928 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8929 parser_set_encode(p, RSTRING_PTR(s));
8930 rb_str_resize(s, 0);
8934parser_prepare(struct parser_params *p)
8936 int c = nextc0(p, FALSE);
8937 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8940 if (peek(p, '!')) p->has_shebang = 1;
8942 case 0xef: /* UTF-8 BOM marker */
8943 if (p->lex.pend - p->lex.pcur >= 2 &&
8944 (unsigned char)p->lex.pcur[0] == 0xbb &&
8945 (unsigned char)p->lex.pcur[1] == 0xbf) {
8946 p->enc = rb_utf8_encoding();
8949 if (p->debug_lines) {
8950 rb_enc_associate(p->lex.lastline, p->enc);
8953 p->lex.pbeg = p->lex.pcur;
8961 p->enc = rb_enc_get(p->lex.lastline);
8965#define ambiguous_operator(tok, op, syn) ( \
8966 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8967 rb_warning0("even though it seems like "syn""))
8969#define ambiguous_operator(tok, op, syn) \
8970 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8972#define warn_balanced(tok, op, syn) ((void) \
8973 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8974 space_seen && !ISSPACE(c) && \
8975 (ambiguous_operator(tok, op, syn), 0)), \
8976 (enum yytokentype)(tok))
8979parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8982 char *point = &str[seen_point];
8983 size_t fraclen = len-seen_point-1;
8984 memmove(point, point+1, fraclen+1);
8985 v = rb_cstr_to_inum(str, 10, FALSE);
8986 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8989static enum yytokentype
8990no_digits(struct parser_params *p)
8992 yyerror0("numeric literal without digits");
8993 if (peek(p, '_')) nextc(p);
8994 /* dummy 0, for tUMINUS_NUM at numeric */
8995 return set_integer_literal(p, INT2FIX(0), 0);
8998static enum yytokentype
8999parse_numeric(struct parser_params *p, int c)
9001 int is_float, seen_point, seen_e, nondigit;
9004 is_float = seen_point = seen_e = nondigit = 0;
9005 SET_LEX_STATE(EXPR_END);
9007 if (c == '-' || c == '+') {
9012 int start = toklen(p);
9014 if (c == 'x' || c == 'X') {
9017 if (c != -1 && ISXDIGIT(c)) {
9020 if (nondigit) break;
9024 if (!ISXDIGIT(c)) break;
9027 } while ((c = nextc(p)) != -1);
9031 if (toklen(p) == start) {
9032 return no_digits(p);
9034 else if (nondigit) goto trailing_uc;
9035 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9036 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9038 if (c == 'b' || c == 'B') {
9041 if (c == '0' || c == '1') {
9044 if (nondigit) break;
9048 if (c != '0' && c != '1') break;
9051 } while ((c = nextc(p)) != -1);
9055 if (toklen(p) == start) {
9056 return no_digits(p);
9058 else if (nondigit) goto trailing_uc;
9059 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9060 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9062 if (c == 'd' || c == 'D') {
9065 if (c != -1 && ISDIGIT(c)) {
9068 if (nondigit) break;
9072 if (!ISDIGIT(c)) break;
9075 } while ((c = nextc(p)) != -1);
9079 if (toklen(p) == start) {
9080 return no_digits(p);
9082 else if (nondigit) goto trailing_uc;
9083 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9084 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9090 if (c == 'o' || c == 'O') {
9091 /* prefixed octal */
9093 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9094 return no_digits(p);
9097 if (c >= '0' && c <= '7') {
9102 if (nondigit) break;
9106 if (c < '0' || c > '9') break;
9107 if (c > '7') goto invalid_octal;
9110 } while ((c = nextc(p)) != -1);
9111 if (toklen(p) > start) {
9114 if (nondigit) goto trailing_uc;
9115 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9116 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9123 if (c > '7' && c <= '9') {
9125 yyerror0("Invalid octal digit");
9127 else if (c == '.' || c == 'e' || c == 'E') {
9132 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9133 return set_integer_literal(p, INT2FIX(0), suffix);
9139 case '0': case '1': case '2': case '3': case '4':
9140 case '5': case '6': case '7': case '8': case '9':
9146 if (nondigit) goto trailing_uc;
9147 if (seen_point || seen_e) {
9152 if (c0 == -1 || !ISDIGIT(c0)) {
9158 seen_point = toklen(p);
9177 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9182 tokadd(p, nondigit);
9186 nondigit = (c == '-' || c == '+') ? c : 0;
9189 case '_': /* `_' in number just ignored */
9190 if (nondigit) goto decode_num;
9204 literal_flush(p, p->lex.pcur - 1);
9205 YYLTYPE loc = RUBY_INIT_YYLLOC();
9206 compile_error(p, "trailing `%c' in number", nondigit);
9207 parser_show_error_line(p, &loc);
9211 enum yytokentype type = tFLOAT;
9214 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9215 if (suffix & NUM_SUFFIX_R) {
9217 v = parse_rational(p, tok(p), toklen(p), seen_point);
9220 double d = strtod(tok(p), 0);
9221 if (errno == ERANGE) {
9222 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9227 return set_number_literal(p, v, type, suffix);
9229 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9230 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9233static enum yytokentype
9234parse_qmark(struct parser_params *p, int space_seen)
9241 SET_LEX_STATE(EXPR_VALUE);
9246 compile_error(p, "incomplete character syntax");
9249 if (rb_enc_isspace(c, p->enc)) {
9251 int c2 = escaped_control_code(c);
9253 WARN_SPACE_CHAR(c2, "?");
9258 SET_LEX_STATE(EXPR_VALUE);
9263 if (!parser_isascii(p)) {
9264 if (tokadd_mbchar(p, c) == -1) return 0;
9266 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9267 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
9269 const char *start = p->lex.pcur - 1, *ptr = start;
9271 int n = parser_precise_mbclen(p, ptr);
9272 if (n < 0) return -1;
9274 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
9275 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9276 " a conditional operator, put a space after `?'",
9277 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9281 else if (c == '\\') {
9284 enc = rb_utf8_encoding();
9285 tokadd_utf8(p, &enc, -1, 0, 0);
9287 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
9289 if (tokadd_mbchar(p, c) == -1) return 0;
9292 c = read_escape(p, 0, &enc);
9300 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9301 set_yylval_str(lit);
9302 SET_LEX_STATE(EXPR_END);
9306static enum yytokentype
9307parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9310 const char *ptok = p->lex.pcur;
9318 if (c == -1) goto unterminated;
9321 if (!ISASCII(c)) goto unknown;
9326 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9329 c = parser_precise_mbclen(p, p->lex.pcur);
9330 if (c < 0) return 0;
9332 yyerror0("unknown type of %string");
9338 compile_error(p, "unterminated quoted string meets end of file");
9342 if (term == '(') term = ')';
9343 else if (term == '[') term = ']';
9344 else if (term == '{') term = '}';
9345 else if (term == '<') term = '>';
9348 p->lex.ptok = ptok-1;
9351 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9355 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
9359 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9363 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9367 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9368 return tSYMBOLS_BEG;
9371 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9372 return tQSYMBOLS_BEG;
9375 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
9376 return tXSTRING_BEG;
9379 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
9383 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
9384 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
9388 yyerror0("unknown type of %string");
9392 if ((c = nextc(p)) == '=') {
9394 SET_LEX_STATE(EXPR_BEG);
9397 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
9400 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9402 return warn_balanced('%', "%%", "string literal");
9406tokadd_ident(struct parser_params *p, int c)
9409 if (tokadd_mbchar(p, c) == -1) return -1;
9411 } while (parser_is_identchar(p));
9417tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
9419 ID ident = TOK_INTERN();
9421 set_yylval_name(ident);
9427parse_numvar(struct parser_params *p)
9431 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
9432 const unsigned long nth_ref_max =
9433 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
9434 /* NTH_REF is left-shifted to be ORed with back-ref flag and
9435 * turned into a Fixnum, in compile.c */
9437 if (overflow || n > nth_ref_max) {
9438 /* compile_error()? */
9439 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
9440 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
9447static enum yytokentype
9448parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
9450 const char *ptr = p->lex.pcur;
9453 SET_LEX_STATE(EXPR_END);
9454 p->lex.ptok = ptr - 1; /* from '$' */
9458 case '_': /* $_: last read line string */
9460 if (parser_is_identchar(p)) {
9468 case '~': /* $~: match-data */
9469 case '*': /* $*: argv */
9470 case '$': /* $$: pid */
9471 case '?': /* $?: last status */
9472 case '!': /* $!: error string */
9473 case '@': /* $@: error position */
9474 case '/': /* $/: input record separator */
9475 case '\\': /* $\: output record separator */
9476 case ';': /* $;: field separator */
9477 case ',': /* $,: output field separator */
9478 case '.': /* $.: last read line number */
9479 case '=': /* $=: ignorecase */
9480 case ':': /* $:: load path */
9481 case '<': /* $<: reading filename */
9482 case '>': /* $>: default output handle */
9483 case '\"': /* $": already loaded files */
9492 if (parser_is_identchar(p)) {
9493 if (tokadd_mbchar(p, c) == -1) return 0;
9501 set_yylval_name(TOK_INTERN());
9504 case '&': /* $&: last match */
9505 case '`': /* $`: string before last match */
9506 case '\'': /* $': string after last match */
9507 case '+': /* $+: string matches last paren. */
9508 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9513 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9516 case '1': case '2': case '3':
9517 case '4': case '5': case '6':
9518 case '7': case '8': case '9':
9523 } while (c != -1 && ISDIGIT(c));
9525 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9527 c = parse_numvar(p);
9528 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9532 if (!parser_is_identchar(p)) {
9533 YYLTYPE loc = RUBY_INIT_YYLLOC();
9534 if (c == -1 || ISSPACE(c)) {
9535 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9539 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9541 parser_show_error_line(p, &loc);
9542 set_yylval_noname();
9550 if (tokadd_ident(p, c)) return 0;
9551 SET_LEX_STATE(EXPR_END);
9552 tokenize_ident(p, last_state);
9558parser_numbered_param(struct parser_params *p, int n)
9560 if (n < 0) return false;
9562 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9565 if (p->max_numparam == ORDINAL_PARAM) {
9566 compile_error(p, "ordinary parameter is defined");
9569 struct vtable *args = p->lvtbl->args;
9570 if (p->max_numparam < n) {
9571 p->max_numparam = n;
9573 while (n > args->pos) {
9574 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9580static enum yytokentype
9581parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9583 const char *ptr = p->lex.pcur;
9584 enum yytokentype result = tIVAR;
9585 register int c = nextc(p);
9588 p->lex.ptok = ptr - 1; /* from '@' */
9596 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9597 if (c == -1 || !parser_is_identchar(p)) {
9599 RUBY_SET_YYLLOC(loc);
9600 if (result == tIVAR) {
9601 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9604 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9606 parser_show_error_line(p, &loc);
9607 set_yylval_noname();
9608 SET_LEX_STATE(EXPR_END);
9611 else if (ISDIGIT(c)) {
9613 RUBY_SET_YYLLOC(loc);
9614 if (result == tIVAR) {
9615 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9618 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9620 parser_show_error_line(p, &loc);
9621 set_yylval_noname();
9622 SET_LEX_STATE(EXPR_END);
9626 if (tokadd_ident(p, c)) return 0;
9627 tokenize_ident(p, last_state);
9631static enum yytokentype
9632parse_ident(struct parser_params *p, int c, int cmd_state)
9634 enum yytokentype result;
9635 int mb = ENC_CODERANGE_7BIT;
9636 const enum lex_state_e last_state = p->lex.state;
9638 int enforce_keyword_end = 0;
9641 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9642 if (tokadd_mbchar(p, c) == -1) return 0;
9644 } while (parser_is_identchar(p));
9645 if ((c == '!' || c == '?') && !peek(p, '=')) {
9649 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9650 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9651 result = tIDENTIFIER;
9655 result = tCONSTANT; /* assume provisionally */
9660 if (IS_LABEL_POSSIBLE()) {
9661 if (IS_LABEL_SUFFIX(0)) {
9662 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9664 set_yylval_name(TOK_INTERN());
9670 if (!NIL_P(peek_end_expect_token_locations(p))) {
9673 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
9675 end_loc = peek_end_expect_token_locations(p);
9676 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
9677 column = NUM2INT(rb_ary_entry(end_loc, 1));
9680 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
9681 p->ruby_sourceline, beg_pos, lineno, column);
9684 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
9685 const struct kwtable *kw;
9687 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
9688 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
9689 enforce_keyword_end = 1;
9695 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
9696 const struct kwtable *kw;
9698 /* See if it is a reserved word. */
9699 kw = rb_reserved_word(tok(p), toklen(p));
9701 enum lex_state_e state = p->lex.state;
9702 if (IS_lex_state_for(state, EXPR_FNAME)) {
9703 SET_LEX_STATE(EXPR_ENDFN);
9704 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9707 SET_LEX_STATE(kw->state);
9708 if (IS_lex_state(EXPR_BEG)) {
9709 p->command_start = TRUE;
9711 if (kw->id[0] == keyword_do) {
9712 if (lambda_beginning_p()) {
9713 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9714 return keyword_do_LAMBDA;
9716 if (COND_P()) return keyword_do_cond;
9717 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9718 return keyword_do_block;
9721 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
9724 if (kw->id[0] != kw->id[1])
9725 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9731 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9733 SET_LEX_STATE(EXPR_CMDARG);
9736 SET_LEX_STATE(EXPR_ARG);
9739 else if (p->lex.state == EXPR_FNAME) {
9740 SET_LEX_STATE(EXPR_ENDFN);
9743 SET_LEX_STATE(EXPR_END);
9746 ident = tokenize_ident(p, last_state);
9747 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9748 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9749 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9750 lvar_defined(p, ident)) {
9751 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9757warn_cr(struct parser_params *p)
9761 /* carried over with p->lex.nextline for nextc() */
9762 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9766static enum yytokentype
9767parser_yylex(struct parser_params *p)
9773 enum lex_state_e last_state;
9774 int fallthru = FALSE;
9775 int token_seen = p->token_seen;
9777 if (p->lex.strterm) {
9778 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9780 return here_document(p, &p->lex.strterm->u.heredoc);
9784 return parse_string(p, &p->lex.strterm->u.literal);
9787 cmd_state = p->command_start;
9788 p->command_start = FALSE;
9789 p->token_seen = TRUE;
9794 last_state = p->lex.state;
9795 switch (c = nextc(p)) {
9796 case '\0': /* NUL */
9797 case '\004': /* ^D */
9798 case '\032': /* ^Z */
9799 case -1: /* end of script. */
9802 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
9803 pop_end_expect_token_locations(p);
9804 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
9808 /* Set location for end-of-input because dispatch_scan_event is not called. */
9809 RUBY_SET_YYLLOC(*p->yylloc);
9816 case ' ': case '\t': case '\f':
9817 case '\13': /* '\v' */
9819 while ((c = nextc(p))) {
9824 case ' ': case '\t': case '\f':
9825 case '\13': /* '\v' */
9833 dispatch_scan_event(p, tSP);
9839 case '#': /* it's a comment */
9840 p->token_seen = token_seen;
9841 /* no magic_comment in shebang line */
9842 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9843 if (comment_at_top(p)) {
9844 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9848 dispatch_scan_event(p, tCOMMENT);
9852 p->token_seen = token_seen;
9853 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9854 !IS_lex_state(EXPR_LABELED));
9855 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9857 dispatch_scan_event(p, tIGNORED_NL);
9860 if (!c && p->ctxt.in_kwarg) {
9861 goto normal_newline;
9866 switch (c = nextc(p)) {
9867 case ' ': case '\t': case '\f': case '\r':
9868 case '\13': /* '\v' */
9874 dispatch_scan_event(p, tSP);
9880 dispatch_delayed_token(p, tIGNORED_NL);
9881 if (peek(p, '.') == (c == '&')) {
9883 dispatch_scan_event(p, tSP);
9888 p->ruby_sourceline--;
9889 p->lex.nextline = p->lex.lastline;
9890 case -1: /* EOF no decrement*/
9893 p->lex.ptok = p->lex.pcur;
9895 goto normal_newline;
9899 p->command_start = TRUE;
9900 SET_LEX_STATE(EXPR_BEG);
9904 if ((c = nextc(p)) == '*') {
9905 if ((c = nextc(p)) == '=') {
9906 set_yylval_id(idPow);
9907 SET_LEX_STATE(EXPR_BEG);
9912 rb_warning0("`**' interpreted as argument prefix");
9915 else if (IS_BEG()) {
9919 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9925 SET_LEX_STATE(EXPR_BEG);
9930 rb_warning0("`*' interpreted as argument prefix");
9933 else if (IS_BEG()) {
9937 c = warn_balanced('*', "*", "argument prefix");
9940 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9945 if (IS_AFTER_OPERATOR()) {
9946 SET_LEX_STATE(EXPR_ARG);
9952 SET_LEX_STATE(EXPR_BEG);
9965 /* skip embedded rd document */
9966 if (word_match_p(p, "begin", 5)) {
9970 dispatch_scan_event(p, tEMBDOC_BEG);
9974 dispatch_scan_event(p, tEMBDOC);
9979 compile_error(p, "embedded document meets end of file");
9982 if (c == '=' && word_match_p(p, "end", 3)) {
9988 dispatch_scan_event(p, tEMBDOC_END);
9993 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9994 if ((c = nextc(p)) == '=') {
9995 if ((c = nextc(p)) == '=') {
10004 else if (c == '>') {
10013 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10015 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10016 int token = heredoc_identifier(p);
10017 if (token) return token < 0 ? 0 : token;
10019 if (IS_AFTER_OPERATOR()) {
10020 SET_LEX_STATE(EXPR_ARG);
10023 if (IS_lex_state(EXPR_CLASS))
10024 p->command_start = TRUE;
10025 SET_LEX_STATE(EXPR_BEG);
10028 if ((c = nextc(p)) == '>') {
10035 if ((c = nextc(p)) == '=') {
10036 set_yylval_id(idLTLT);
10037 SET_LEX_STATE(EXPR_BEG);
10041 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10047 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10048 if ((c = nextc(p)) == '=') {
10052 if ((c = nextc(p)) == '=') {
10053 set_yylval_id(idGTGT);
10054 SET_LEX_STATE(EXPR_BEG);
10064 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10065 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10066 p->lex.ptok = p->lex.pcur-1;
10067 return tSTRING_BEG;
10070 if (IS_lex_state(EXPR_FNAME)) {
10071 SET_LEX_STATE(EXPR_ENDFN);
10074 if (IS_lex_state(EXPR_DOT)) {
10076 SET_LEX_STATE(EXPR_CMDARG);
10078 SET_LEX_STATE(EXPR_ARG);
10081 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10082 return tXSTRING_BEG;
10085 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10086 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10087 p->lex.ptok = p->lex.pcur-1;
10088 return tSTRING_BEG;
10091 return parse_qmark(p, space_seen);
10094 if ((c = nextc(p)) == '&') {
10095 SET_LEX_STATE(EXPR_BEG);
10096 if ((c = nextc(p)) == '=') {
10097 set_yylval_id(idANDOP);
10098 SET_LEX_STATE(EXPR_BEG);
10104 else if (c == '=') {
10105 set_yylval_id('&');
10106 SET_LEX_STATE(EXPR_BEG);
10109 else if (c == '.') {
10110 set_yylval_id(idANDDOT);
10111 SET_LEX_STATE(EXPR_DOT);
10115 if (IS_SPCARG(c)) {
10117 (c = peekc_n(p, 1)) == -1 ||
10118 !(c == '\'' || c == '"' ||
10119 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
10120 rb_warning0("`&' interpreted as argument prefix");
10124 else if (IS_BEG()) {
10128 c = warn_balanced('&', "&", "argument prefix");
10130 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10134 if ((c = nextc(p)) == '|') {
10135 SET_LEX_STATE(EXPR_BEG);
10136 if ((c = nextc(p)) == '=') {
10137 set_yylval_id(idOROP);
10138 SET_LEX_STATE(EXPR_BEG);
10142 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10150 set_yylval_id('|');
10151 SET_LEX_STATE(EXPR_BEG);
10154 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10160 if (IS_AFTER_OPERATOR()) {
10161 SET_LEX_STATE(EXPR_ARG);
10169 set_yylval_id('+');
10170 SET_LEX_STATE(EXPR_BEG);
10173 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10174 SET_LEX_STATE(EXPR_BEG);
10176 if (c != -1 && ISDIGIT(c)) {
10177 return parse_numeric(p, '+');
10181 SET_LEX_STATE(EXPR_BEG);
10183 return warn_balanced('+', "+", "unary operator");
10187 if (IS_AFTER_OPERATOR()) {
10188 SET_LEX_STATE(EXPR_ARG);
10196 set_yylval_id('-');
10197 SET_LEX_STATE(EXPR_BEG);
10201 SET_LEX_STATE(EXPR_ENDFN);
10204 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10205 SET_LEX_STATE(EXPR_BEG);
10207 if (c != -1 && ISDIGIT(c)) {
10208 return tUMINUS_NUM;
10212 SET_LEX_STATE(EXPR_BEG);
10214 return warn_balanced('-', "-", "unary operator");
10217 int is_beg = IS_BEG();
10218 SET_LEX_STATE(EXPR_BEG);
10219 if ((c = nextc(p)) == '.') {
10220 if ((c = nextc(p)) == '.') {
10221 if (p->ctxt.in_argdef) {
10222 SET_LEX_STATE(EXPR_ENDARG);
10225 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10226 rb_warn0("... at EOL, should be parenthesized?");
10228 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10229 if (IS_lex_state_for(last_state, EXPR_LABEL))
10232 return is_beg ? tBDOT3 : tDOT3;
10235 return is_beg ? tBDOT2 : tDOT2;
10238 if (c != -1 && ISDIGIT(c)) {
10239 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10240 parse_numeric(p, '.');
10241 if (ISDIGIT(prev)) {
10242 yyerror0("unexpected fraction part after numeric literal");
10245 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10247 SET_LEX_STATE(EXPR_END);
10248 p->lex.ptok = p->lex.pcur;
10251 set_yylval_id('.');
10252 SET_LEX_STATE(EXPR_DOT);
10256 case '0': case '1': case '2': case '3': case '4':
10257 case '5': case '6': case '7': case '8': case '9':
10258 return parse_numeric(p, c);
10263 SET_LEX_STATE(EXPR_ENDFN);
10264 p->lex.paren_nest--;
10270 SET_LEX_STATE(EXPR_END);
10271 p->lex.paren_nest--;
10275 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10276 if (!p->lex.brace_nest--) return tSTRING_DEND;
10279 SET_LEX_STATE(EXPR_END);
10280 p->lex.paren_nest--;
10286 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10287 SET_LEX_STATE(EXPR_BEG);
10290 set_yylval_id(idCOLON2);
10291 SET_LEX_STATE(EXPR_DOT);
10294 if (IS_END() || ISSPACE(c) || c == '#') {
10296 c = warn_balanced(':', ":", "symbol literal");
10297 SET_LEX_STATE(EXPR_BEG);
10302 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10305 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10311 SET_LEX_STATE(EXPR_FNAME);
10316 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10317 return tREGEXP_BEG;
10319 if ((c = nextc(p)) == '=') {
10320 set_yylval_id('/');
10321 SET_LEX_STATE(EXPR_BEG);
10325 if (IS_SPCARG(c)) {
10326 arg_ambiguous(p, '/');
10327 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10328 return tREGEXP_BEG;
10330 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10331 return warn_balanced('/', "/", "regexp literal");
10334 if ((c = nextc(p)) == '=') {
10335 set_yylval_id('^');
10336 SET_LEX_STATE(EXPR_BEG);
10339 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10344 SET_LEX_STATE(EXPR_BEG);
10345 p->command_start = TRUE;
10349 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10353 if (IS_AFTER_OPERATOR()) {
10354 if ((c = nextc(p)) != '@') {
10357 SET_LEX_STATE(EXPR_ARG);
10360 SET_LEX_STATE(EXPR_BEG);
10368 else if (!space_seen) {
10369 /* foo( ... ) => method call, no ambiguity */
10371 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
10374 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
10375 rb_warning0("parentheses after method name is interpreted as "
10376 "an argument list, not a decomposed argument");
10378 p->lex.paren_nest++;
10381 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10385 p->lex.paren_nest++;
10386 if (IS_AFTER_OPERATOR()) {
10387 if ((c = nextc(p)) == ']') {
10388 p->lex.paren_nest--;
10389 SET_LEX_STATE(EXPR_ARG);
10390 if ((c = nextc(p)) == '=') {
10397 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
10400 else if (IS_BEG()) {
10403 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
10406 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10412 ++p->lex.brace_nest;
10413 if (lambda_beginning_p())
10415 else if (IS_lex_state(EXPR_LABELED))
10416 c = tLBRACE; /* hash */
10417 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
10418 c = '{'; /* block (primary) */
10419 else if (IS_lex_state(EXPR_ENDARG))
10420 c = tLBRACE_ARG; /* block (expr) */
10422 c = tLBRACE; /* hash */
10423 if (c != tLBRACE) {
10424 p->command_start = TRUE;
10425 SET_LEX_STATE(EXPR_BEG);
10428 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10430 ++p->lex.paren_nest; /* after lambda_beginning_p() */
10439 dispatch_scan_event(p, tSP);
10440 goto retry; /* skip \\n */
10442 if (c == ' ') return tSP;
10443 if (ISSPACE(c)) return c;
10448 return parse_percent(p, space_seen, last_state);
10451 return parse_gvar(p, last_state);
10454 return parse_atmark(p, last_state);
10457 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
10458 p->ruby__end__seen = 1;
10464 dispatch_scan_event(p, k__END__);
10472 if (!parser_is_identchar(p)) {
10473 compile_error(p, "Invalid char `\\x%02X' in expression", c);
10482 return parse_ident(p, c, cmd_state);
10485static enum yytokentype
10486yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
10488 enum yytokentype t;
10491 lval->val = Qundef;
10492 p->yylloc = yylloc;
10494 t = parser_yylex(p);
10496 if (has_delayed_token(p))
10497 dispatch_delayed_token(p, t);
10499 dispatch_scan_event(p, t);
10504#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
10507node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
10509 NODE *n = rb_ast_newnode(p->ast, type);
10511 rb_node_init(n, type, a0, a1, a2);
10513 nd_set_loc(n, loc);
10514 nd_set_node_id(n, parser_get_node_id(p));
10519nd_set_loc(NODE *nd, const YYLTYPE *loc)
10522 nd_set_line(nd, loc->beg_pos.lineno);
10527static enum node_type
10528nodetype(NODE *node) /* for debug */
10530 return (enum node_type)nd_type(node);
10534nodeline(NODE *node)
10536 return nd_line(node);
10540newline_node(NODE *node)
10543 node = remove_begin(node);
10544 node->flags |= NODE_FL_NEWLINE;
10550fixpos(NODE *node, NODE *orig)
10554 nd_set_line(node, nd_line(orig));
10558parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10560 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10564parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10566 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10570block_append(struct parser_params *p, NODE *head, NODE *tail)
10572 NODE *end, *h = head, *nd;
10574 if (tail == 0) return head;
10576 if (h == 0) return tail;
10577 switch (nd_type(h)) {
10584 parser_warning(p, h, "unused literal ignored");
10587 h = end = NEW_BLOCK(head, &head->nd_loc);
10597 switch (nd_type(nd)) {
10603 if (RTEST(ruby_verbose)) {
10604 parser_warning(p, tail, "statement not reached");
10612 if (!nd_type_p(tail, NODE_BLOCK)) {
10613 tail = NEW_BLOCK(tail, &tail->nd_loc);
10614 tail->nd_end = tail;
10616 end->nd_next = tail;
10617 h->nd_end = tail->nd_end;
10618 nd_set_last_loc(head, nd_last_loc(tail));
10622/* append item to the list */
10624list_append(struct parser_params *p, NODE *list, NODE *item)
10628 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10629 if (list->nd_next) {
10630 last = list->nd_next->nd_end;
10636 list->nd_alen += 1;
10637 last->nd_next = NEW_LIST(item, &item->nd_loc);
10638 list->nd_next->nd_end = last->nd_next;
10640 nd_set_last_loc(list, nd_last_loc(item));
10645/* concat two lists */
10647list_concat(NODE *head, NODE *tail)
10651 if (head->nd_next) {
10652 last = head->nd_next->nd_end;
10658 head->nd_alen += tail->nd_alen;
10659 last->nd_next = tail;
10660 if (tail->nd_next) {
10661 head->nd_next->nd_end = tail->nd_next->nd_end;
10664 head->nd_next->nd_end = tail;
10667 nd_set_last_loc(head, nd_last_loc(tail));
10673literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10675 if (NIL_P(tail)) return 1;
10676 if (!rb_enc_compatible(head, tail)) {
10677 compile_error(p, "string literal encodings differ (%s / %s)",
10678 rb_enc_name(rb_enc_get(head)),
10679 rb_enc_name(rb_enc_get(tail)));
10680 rb_str_resize(head, 0);
10681 rb_str_resize(tail, 0);
10684 rb_str_buf_append(head, tail);
10689string_literal_head(enum node_type htype, NODE *head)
10691 if (htype != NODE_DSTR) return Qfalse;
10692 if (head->nd_next) {
10693 head = head->nd_next->nd_end->nd_head;
10694 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10696 const VALUE lit = head->nd_lit;
10697 ASSUME(lit != Qfalse);
10701/* concat two string literals */
10703literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10705 enum node_type htype;
10708 if (!head) return tail;
10709 if (!tail) return head;
10711 htype = nd_type(head);
10712 if (htype == NODE_EVSTR) {
10713 head = new_dstr(p, head, loc);
10716 if (p->heredoc_indent > 0) {
10719 nd_set_type(head, NODE_DSTR);
10721 return list_append(p, head, tail);
10726 switch (nd_type(tail)) {
10728 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10732 lit = head->nd_lit;
10734 if (htype == NODE_STR) {
10735 if (!literal_concat0(p, lit, tail->nd_lit)) {
10737 rb_discard_node(p, head);
10738 rb_discard_node(p, tail);
10741 rb_discard_node(p, tail);
10744 list_append(p, head, tail);
10749 if (htype == NODE_STR) {
10750 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10752 tail->nd_lit = head->nd_lit;
10753 rb_discard_node(p, head);
10756 else if (NIL_P(tail->nd_lit)) {
10758 head->nd_alen += tail->nd_alen - 1;
10759 if (!head->nd_next) {
10760 head->nd_next = tail->nd_next;
10762 else if (tail->nd_next) {
10763 head->nd_next->nd_end->nd_next = tail->nd_next;
10764 head->nd_next->nd_end = tail->nd_next->nd_end;
10766 rb_discard_node(p, tail);
10768 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10769 if (!literal_concat0(p, lit, tail->nd_lit))
10771 tail->nd_lit = Qnil;
10775 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10780 if (htype == NODE_STR) {
10781 nd_set_type(head, NODE_DSTR);
10784 list_append(p, head, tail);
10791evstr2dstr(struct parser_params *p, NODE *node)
10793 if (nd_type_p(node, NODE_EVSTR)) {
10794 node = new_dstr(p, node, &node->nd_loc);
10800new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10805 switch (nd_type(node)) {
10807 nd_set_type(node, NODE_DSTR);
10815 return NEW_EVSTR(head, loc);
10819new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10821 VALUE lit = STR_NEW0();
10822 NODE *dstr = NEW_DSTR(lit, loc);
10823 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10824 return list_append(p, dstr, node);
10828call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10829 const YYLTYPE *op_loc, const YYLTYPE *loc)
10834 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10835 nd_set_line(expr, op_loc->beg_pos.lineno);
10840call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10844 opcall = NEW_OPCALL(recv, id, 0, loc);
10845 nd_set_line(opcall, op_loc->beg_pos.lineno);
10850new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10852 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10853 nd_set_line(qcall, op_loc->beg_pos.lineno);
10858new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10861 if (block) block_dup_check(p, args, block);
10862 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10863 if (block) ret = method_add_block(p, ret, block, loc);
10868#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10870match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10873 int line = op_loc->beg_pos.lineno;
10877 if (node1 && (n = nd_once_body(node1)) != 0) {
10878 switch (nd_type(n)) {
10881 NODE *match = NEW_MATCH2(node1, node2, loc);
10882 nd_set_line(match, line);
10887 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10888 const VALUE lit = n->nd_lit;
10889 NODE *match = NEW_MATCH2(node1, node2, loc);
10890 match->nd_args = reg_named_capture_assign(p, lit, loc);
10891 nd_set_line(match, line);
10897 if (node2 && (n = nd_once_body(node2)) != 0) {
10900 switch (nd_type(n)) {
10902 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10905 match3 = NEW_MATCH3(node2, node1, loc);
10910 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10911 nd_set_line(n, line);
10915# if WARN_PAST_SCOPE
10917past_dvar_p(struct parser_params *p, ID id)
10919 struct vtable *past = p->lvtbl->past;
10921 if (vtable_included(past, id)) return 1;
10929numparam_nested_p(struct parser_params *p)
10931 struct local_vars *local = p->lvtbl;
10932 NODE *outer = local->numparam.outer;
10933 NODE *inner = local->numparam.inner;
10934 if (outer || inner) {
10935 NODE *used = outer ? outer : inner;
10936 compile_error(p, "numbered parameter is already used in\n"
10937 "%s:%d: %s block here",
10938 p->ruby_sourcefile, nd_line(used),
10939 outer ? "outer" : "inner");
10940 parser_show_error_line(p, &used->nd_loc);
10947gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10953 return NEW_SELF(loc);
10955 return NEW_NIL(loc);
10957 return NEW_TRUE(loc);
10958 case keyword_false:
10959 return NEW_FALSE(loc);
10960 case keyword__FILE__:
10962 VALUE file = p->ruby_sourcefile_string;
10964 file = rb_str_new(0, 0);
10966 file = rb_str_dup(file);
10967 node = NEW_STR(file, loc);
10968 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10971 case keyword__LINE__:
10972 return NEW_LIT(INT2FIX(p->tokline), loc);
10973 case keyword__ENCODING__:
10974 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10975 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10979 switch (id_type(id)) {
10981 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10982 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10983 if (id == p->cur_arg) {
10984 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10987 if (vidp) *vidp |= LVAR_USED;
10988 node = NEW_DVAR(id, loc);
10991 if (local_id_ref(p, id, &vidp)) {
10992 if (id == p->cur_arg) {
10993 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10996 if (vidp) *vidp |= LVAR_USED;
10997 node = NEW_LVAR(id, loc);
11000 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
11001 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
11002 if (numparam_nested_p(p)) return 0;
11003 node = NEW_DVAR(id, loc);
11004 struct local_vars *local = p->lvtbl;
11005 if (!local->numparam.current) local->numparam.current = node;
11008# if WARN_PAST_SCOPE
11009 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
11010 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
11013 /* method call without arguments */
11014 return NEW_VCALL(id, loc);
11016 return NEW_GVAR(id, loc);
11018 return NEW_IVAR(id, loc);
11020 return NEW_CONST(id, loc);
11022 return NEW_CVAR(id, loc);
11024 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11029opt_arg_append(NODE *opt_list, NODE *opt)
11031 NODE *opts = opt_list;
11032 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11034 while (opts->nd_next) {
11035 opts = opts->nd_next;
11036 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11038 opts->nd_next = opt;
11044kwd_append(NODE *kwlist, NODE *kw)
11047 opt_arg_append(kwlist, kw);
11053new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
11055 return NEW_DEFINED(remove_begin_all(expr), loc);
11059symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
11061 enum node_type type = nd_type(symbol);
11064 nd_set_type(symbol, NODE_DSYM);
11067 nd_set_type(symbol, NODE_LIT);
11068 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
11071 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
11073 return list_append(p, symbols, symbol);
11077new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
11083 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
11084 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11087 switch (nd_type(node)) {
11090 VALUE src = node->nd_lit;
11091 nd_set_type(node, NODE_LIT);
11092 nd_set_loc(node, loc);
11093 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11098 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
11099 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11102 nd_set_type(node, NODE_DREGX);
11103 nd_set_loc(node, loc);
11104 node->nd_cflag = options & RE_OPTION_MASK;
11105 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
11106 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
11107 NODE *frag = list->nd_head;
11108 enum node_type type = nd_type(frag);
11109 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
11110 VALUE tail = frag->nd_lit;
11111 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
11112 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
11113 if (!literal_concat0(p, lit, tail)) {
11114 return NEW_NIL(loc); /* dummy node on error */
11116 rb_str_resize(tail, 0);
11117 prev->nd_next = list->nd_next;
11118 rb_discard_node(p, list->nd_head);
11119 rb_discard_node(p, list);
11130 if (!node->nd_next) {
11131 VALUE src = node->nd_lit;
11132 nd_set_type(node, NODE_LIT);
11133 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11135 if (options & RE_OPTION_ONCE) {
11136 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
11144new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
11147 return NEW_KW_ARG(0, (k), loc);
11151new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11154 VALUE lit = STR_NEW0();
11155 NODE *xstr = NEW_XSTR(lit, loc);
11156 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11159 switch (nd_type(node)) {
11161 nd_set_type(node, NODE_XSTR);
11162 nd_set_loc(node, loc);
11165 nd_set_type(node, NODE_DXSTR);
11166 nd_set_loc(node, loc);
11169 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
11176check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
11180 if (!arg || !p->case_labels) return;
11182 lit = rb_node_case_when_optimizable_literal(arg);
11183 if (UNDEF_P(lit)) return;
11184 if (nd_type_p(arg, NODE_STR)) {
11185 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
11188 if (NIL_P(p->case_labels)) {
11189 p->case_labels = rb_obj_hide(rb_hash_new());
11192 VALUE line = rb_hash_lookup(p->case_labels, lit);
11193 if (!NIL_P(line)) {
11194 rb_warning1("duplicated `when' clause with line %d is ignored",
11199 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
11204id_is_var(struct parser_params *p, ID id)
11206 if (is_notop_id(id)) {
11207 switch (id & ID_SCOPE_MASK) {
11208 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
11211 if (dyna_in_block(p)) {
11212 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
11214 if (local_id(p, id)) return 1;
11215 /* method call without arguments */
11219 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11224new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
11226 VALUE src = 0, err;
11228 if (ripper_is_node_yylval(re)) {
11229 src = RNODE(re)->nd_cval;
11230 re = RNODE(re)->nd_rval;
11232 if (ripper_is_node_yylval(opt)) {
11233 options = (int)RNODE(opt)->nd_tag;
11234 opt = RNODE(opt)->nd_rval;
11236 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
11237 compile_error(p, "%"PRIsVALUE, err);
11239 return dispatch2(regexp_literal, re, opt);
11241#endif /* !RIPPER */
11243static inline enum lex_state_e
11244parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
11247 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
11249 return p->lex.state = ls;
11253static const char rb_parser_lex_state_names[][8] = {
11254 "BEG", "END", "ENDARG", "ENDFN", "ARG",
11255 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
11256 "LABEL", "LABELED","FITEM",
11260append_lex_state_name(enum lex_state_e state, VALUE buf)
11263 unsigned int mask = 1;
11264 static const char none[] = "NONE";
11266 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
11267 if ((unsigned)state & mask) {
11269 rb_str_cat(buf, "|", 1);
11272 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
11276 rb_str_cat(buf, none, sizeof(none)-1);
11282flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
11284 VALUE mesg = p->debug_buffer;
11286 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
11287 p->debug_buffer = Qnil;
11288 rb_io_puts(1, &mesg, out);
11290 if (!NIL_P(str) && RSTRING_LEN(str)) {
11291 rb_io_write(p->debug_output, str);
11296rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
11297 enum lex_state_e to, int line)
11300 mesg = rb_str_new_cstr("lex_state: ");
11301 append_lex_state_name(from, mesg);
11302 rb_str_cat_cstr(mesg, " -> ");
11303 append_lex_state_name(to, mesg);
11304 rb_str_catf(mesg, " at line %d\n", line);
11305 flush_debug_buffer(p, p->debug_output, mesg);
11310rb_parser_lex_state_name(enum lex_state_e state)
11312 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
11316append_bitstack_value(stack_type stack, VALUE mesg)
11319 rb_str_cat_cstr(mesg, "0");
11322 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
11323 for (; mask && !(stack & mask); mask >>= 1) continue;
11324 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
11329rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
11330 const char *name, int line)
11332 VALUE mesg = rb_sprintf("%s: ", name);
11333 append_bitstack_value(stack, mesg);
11334 rb_str_catf(mesg, " at line %d\n", line);
11335 flush_debug_buffer(p, p->debug_output, mesg);
11339rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
11342 VALUE mesg = rb_str_new_cstr("internal parser error: ");
11345 rb_str_vcatf(mesg, fmt, ap);
11347 yyerror0(RSTRING_PTR(mesg));
11350 mesg = rb_str_new(0, 0);
11351 append_lex_state_name(p->lex.state, mesg);
11352 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
11353 rb_str_resize(mesg, 0);
11354 append_bitstack_value(p->cond_stack, mesg);
11355 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
11356 rb_str_resize(mesg, 0);
11357 append_bitstack_value(p->cmdarg_stack, mesg);
11358 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
11359 if (p->debug_output == rb_ractor_stdout())
11360 p->debug_output = rb_ractor_stderr();
11365rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
11367 yylloc->beg_pos.lineno = sourceline;
11368 yylloc->beg_pos.column = beg_pos;
11369 yylloc->end_pos.lineno = sourceline;
11370 yylloc->end_pos.column = end_pos;
11375rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
11377 int sourceline = here->sourceline;
11378 int beg_pos = (int)here->offset - here->quote
11379 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
11380 int end_pos = (int)here->offset + here->length + here->quote;
11382 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11386rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
11388 yylloc->beg_pos.lineno = p->delayed.beg_line;
11389 yylloc->beg_pos.column = p->delayed.beg_col;
11390 yylloc->end_pos.lineno = p->delayed.end_line;
11391 yylloc->end_pos.column = p->delayed.end_col;
11397rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
11399 int sourceline = p->ruby_sourceline;
11400 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11401 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
11402 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11406rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
11408 yylloc->end_pos = yylloc->beg_pos;
11414rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
11416 int sourceline = p->ruby_sourceline;
11417 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11418 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
11419 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11423rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
11425 int sourceline = p->ruby_sourceline;
11426 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11427 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
11428 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11430#endif /* !RIPPER */
11433assignable0(struct parser_params *p, ID id, const char **err)
11435 if (!id) return -1;
11438 *err = "Can't change the value of self";
11441 *err = "Can't assign to nil";
11444 *err = "Can't assign to true";
11446 case keyword_false:
11447 *err = "Can't assign to false";
11449 case keyword__FILE__:
11450 *err = "Can't assign to __FILE__";
11452 case keyword__LINE__:
11453 *err = "Can't assign to __LINE__";
11455 case keyword__ENCODING__:
11456 *err = "Can't assign to __ENCODING__";
11459 switch (id_type(id)) {
11461 if (dyna_in_block(p)) {
11462 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
11463 compile_error(p, "Can't assign to numbered parameter _%d",
11464 NUMPARAM_ID_TO_IDX(id));
11467 if (dvar_curr(p, id)) return NODE_DASGN;
11468 if (dvar_defined(p, id)) return NODE_DASGN;
11469 if (local_id(p, id)) return NODE_LASGN;
11474 if (!local_id(p, id)) local_var(p, id);
11478 case ID_GLOBAL: return NODE_GASGN;
11479 case ID_INSTANCE: return NODE_IASGN;
11481 if (!p->ctxt.in_def) return NODE_CDECL;
11482 *err = "dynamic constant assignment";
11484 case ID_CLASS: return NODE_CVASGN;
11486 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
11493assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
11495 const char *err = 0;
11496 int node_type = assignable0(p, id, &err);
11497 switch (node_type) {
11498 case NODE_DASGN: return NEW_DASGN(id, val, loc);
11499 case NODE_LASGN: return NEW_LASGN(id, val, loc);
11500 case NODE_GASGN: return NEW_GASGN(id, val, loc);
11501 case NODE_IASGN: return NEW_IASGN(id, val, loc);
11502 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
11503 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
11505 if (err) yyerror1(loc, err);
11506 return NEW_BEGIN(0, loc);
11510assignable(struct parser_params *p, VALUE lhs)
11512 const char *err = 0;
11513 assignable0(p, get_id(lhs), &err);
11514 if (err) lhs = assign_error(p, err, lhs);
11520is_private_local_id(ID name)
11523 if (name == idUScore) return 1;
11524 if (!is_local_id(name)) return 0;
11525 s = rb_id2str(name);
11527 return RSTRING_PTR(s)[0] == '_';
11531shadowing_lvar_0(struct parser_params *p, ID name)
11533 if (dyna_in_block(p)) {
11534 if (dvar_curr(p, name)) {
11535 if (is_private_local_id(name)) return 1;
11536 yyerror0("duplicated argument name");
11538 else if (dvar_defined(p, name) || local_id(p, name)) {
11539 vtable_add(p->lvtbl->vars, name);
11540 if (p->lvtbl->used) {
11541 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11547 if (local_id(p, name)) {
11548 if (is_private_local_id(name)) return 1;
11549 yyerror0("duplicated argument name");
11556shadowing_lvar(struct parser_params *p, ID name)
11558 shadowing_lvar_0(p, name);
11563new_bv(struct parser_params *p, ID name)
11566 if (!is_local_id(name)) {
11567 compile_error(p, "invalid local variable - %"PRIsVALUE,
11571 if (!shadowing_lvar_0(p, name)) return;
11577aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11579 return NEW_ATTRASGN(recv, tASET, idx, loc);
11583block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11585 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11586 compile_error(p, "both block arg and actual block given");
11591attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11593 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11594 return NEW_ATTRASGN(recv, id, 0, loc);
11598rb_backref_error(struct parser_params *p, NODE *node)
11600 switch (nd_type(node)) {
11602 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11604 case NODE_BACK_REF:
11605 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11611backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11613 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11614 rb_str_append(mesg, ref->nd_cval);
11615 return dispatch2(assign_error, mesg, expr);
11621arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11623 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11624 switch (nd_type(node1)) {
11626 return list_append(p, node1, node2);
11627 case NODE_BLOCK_PASS:
11628 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11629 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11631 case NODE_ARGSPUSH:
11632 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11633 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11634 nd_set_type(node1, NODE_ARGSCAT);
11637 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11638 node1->nd_body = list_append(p, node1->nd_body, node2);
11639 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11642 return NEW_ARGSPUSH(node1, node2, loc);
11646arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11648 if (!node2) return node1;
11649 switch (nd_type(node1)) {
11650 case NODE_BLOCK_PASS:
11651 if (node1->nd_head)
11652 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11654 node1->nd_head = NEW_LIST(node2, loc);
11656 case NODE_ARGSPUSH:
11657 if (!nd_type_p(node2, NODE_LIST)) break;
11658 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11659 nd_set_type(node1, NODE_ARGSCAT);
11662 if (!nd_type_p(node2, NODE_LIST) ||
11663 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11664 node1->nd_body = list_concat(node1->nd_body, node2);
11667 return NEW_ARGSCAT(node1, node2, loc);
11671last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11674 if ((n1 = splat_array(args)) != 0) {
11675 return list_append(p, n1, last_arg);
11677 return arg_append(p, args, last_arg, loc);
11681rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11684 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11685 return list_concat(n1, rest_arg);
11687 return arg_concat(p, args, rest_arg, loc);
11691splat_array(NODE* node)
11693 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11694 if (nd_type_p(node, NODE_LIST)) return node;
11699mark_lvar_used(struct parser_params *p, NODE *rhs)
11703 switch (nd_type(rhs)) {
11705 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11706 if (vidp) *vidp |= LVAR_USED;
11710 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11711 if (vidp) *vidp |= LVAR_USED;
11716 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11717 mark_lvar_used(p, rhs->nd_head);
11725const_decl_path(struct parser_params *p, NODE **dest)
11728 if (!nd_type_p(n, NODE_CALL)) {
11729 const YYLTYPE *loc = &n->nd_loc;
11732 path = rb_id2str(n->nd_vid);
11736 path = rb_ary_new();
11737 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11738 rb_ary_push(path, rb_id2str(n->nd_mid));
11740 if (n && nd_type_p(n, NODE_CONST)) {
11742 rb_ary_push(path, rb_id2str(n->nd_vid));
11744 else if (n && nd_type_p(n, NODE_COLON3)) {
11746 rb_ary_push(path, rb_str_new(0, 0));
11749 // expression::Name
11750 rb_ary_push(path, rb_str_new_cstr("..."));
11752 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11753 path = rb_fstring(path);
11755 *dest = n = NEW_LIT(path, loc);
11756 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11761extern VALUE rb_mRubyVMFrozenCore;
11764make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11766 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11769 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11770 NEW_LIST(value, loc), loc);
11773 return NEW_CALL(fcore, rb_intern("make_shareable"),
11774 NEW_LIST(value, loc), loc);
11779ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11781 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11782 NODE *args = NEW_LIST(value, loc);
11783 args = list_append(p, args, const_decl_path(p, dest));
11784 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11787static int is_static_content(NODE *node);
11790shareable_literal_value(NODE *node)
11792 if (!node) return Qnil;
11793 enum node_type type = nd_type(node);
11802 return node->nd_lit;
11808#ifndef SHAREABLE_BARE_EXPRESSION
11809#define SHAREABLE_BARE_EXPRESSION 1
11813shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11814 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11816# define shareable_literal_constant_next(n) \
11817 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11820 if (!value) return 0;
11821 enum node_type type = nd_type(value);
11830 if (shareable == shareable_literal) {
11831 value = NEW_CALL(value, idUMinus, 0, loc);
11836 lit = rb_fstring(value->nd_lit);
11837 nd_set_type(value, NODE_LIT);
11838 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11842 lit = rb_ary_new();
11843 OBJ_FREEZE_RAW(lit);
11844 NODE *n = NEW_LIT(lit, loc);
11845 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11849 lit = rb_ary_new();
11850 for (NODE *n = value; n; n = n->nd_next) {
11851 NODE *elt = n->nd_head;
11853 elt = shareable_literal_constant_next(elt);
11857 else if (RTEST(lit)) {
11863 VALUE e = shareable_literal_value(elt);
11865 rb_ary_push(lit, e);
11869 lit = Qnil; /* make shareable at runtime */
11876 if (!value->nd_brace) return 0;
11877 lit = rb_hash_new();
11878 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11879 NODE *key = n->nd_head;
11880 NODE *val = n->nd_next->nd_head;
11882 key = shareable_literal_constant_next(key);
11886 else if (RTEST(lit)) {
11887 rb_hash_clear(lit);
11892 val = shareable_literal_constant_next(val);
11894 n->nd_next->nd_head = val;
11896 else if (RTEST(lit)) {
11897 rb_hash_clear(lit);
11902 VALUE k = shareable_literal_value(key);
11903 VALUE v = shareable_literal_value(val);
11904 if (!UNDEF_P(k) && !UNDEF_P(v)) {
11905 rb_hash_aset(lit, k, v);
11908 rb_hash_clear(lit);
11909 lit = Qnil; /* make shareable at runtime */
11916 if (shareable == shareable_literal &&
11917 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11918 return ensure_shareable_node(p, dest, value, loc);
11923 /* Array or Hash */
11924 if (!lit) return 0;
11926 // if shareable_literal, all elements should have been ensured
11928 value = make_shareable_node(p, value, false, loc);
11931 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11932 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
11936# undef shareable_literal_constant_next
11940shareable_constant_value(struct parser_params *p, enum shareability shareable,
11941 NODE *lhs, NODE *value, const YYLTYPE *loc)
11943 if (!value) return 0;
11944 switch (shareable) {
11945 case shareable_none:
11948 case shareable_literal:
11950 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11951 if (lit) return lit;
11956 case shareable_copy:
11957 case shareable_everything:
11959 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
11960 if (lit) return lit;
11961 return make_shareable_node(p, value, shareable == shareable_copy, loc);
11966 UNREACHABLE_RETURN(0);
11971node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
11973 if (!lhs) return 0;
11975 switch (nd_type(lhs)) {
11977 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
11986 lhs->nd_value = rhs;
11987 nd_set_loc(lhs, loc);
11990 case NODE_ATTRASGN:
11991 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
11992 nd_set_loc(lhs, loc);
11996 /* should not happen */
12004value_expr_check(struct parser_params *p, NODE *node)
12006 NODE *void_node = 0, *vn;
12009 rb_warning0("empty expression");
12012 switch (nd_type(node)) {
12018 return void_node ? void_node : node;
12021 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
12022 compile_error(p, "unexpected node");
12025 if (node->nd_body->nd_body) {
12028 /* single line pattern matching */
12029 return void_node ? void_node : node;
12032 while (node->nd_next) {
12033 node = node->nd_next;
12035 node = node->nd_head;
12039 node = node->nd_body;
12044 if (!node->nd_body) {
12047 else if (!node->nd_else) {
12050 vn = value_expr_check(p, node->nd_body);
12051 if (!vn) return NULL;
12052 if (!void_node) void_node = vn;
12053 node = node->nd_else;
12058 node = node->nd_1st;
12064 mark_lvar_used(p, node);
12076value_expr_gen(struct parser_params *p, NODE *node)
12078 NODE *void_node = value_expr_check(p, node);
12080 yyerror1(&void_node->nd_loc, "void value expression");
12081 /* or "control never reach"? */
12087void_expr(struct parser_params *p, NODE *node)
12089 const char *useless = 0;
12091 if (!RTEST(ruby_verbose)) return;
12093 if (!node || !(node = nd_once_body(node))) return;
12094 switch (nd_type(node)) {
12096 switch (node->nd_mid) {
12115 useless = rb_id2name(node->nd_mid);
12126 case NODE_BACK_REF:
12127 useless = "a variable";
12130 useless = "a constant";
12136 useless = "a literal";
12161 useless = "defined?";
12166 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
12171void_stmts(struct parser_params *p, NODE *node)
12173 NODE *const n = node;
12174 if (!RTEST(ruby_verbose)) return n;
12175 if (!node) return n;
12176 if (!nd_type_p(node, NODE_BLOCK)) return n;
12178 while (node->nd_next) {
12179 void_expr(p, node->nd_head);
12180 node = node->nd_next;
12186remove_begin(NODE *node)
12188 NODE **n = &node, *n1 = node;
12189 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
12190 *n = n1 = n1->nd_body;
12196remove_begin_all(NODE *node)
12198 NODE **n = &node, *n1 = node;
12199 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
12200 *n = n1 = n1->nd_body;
12206reduce_nodes(struct parser_params *p, NODE **body)
12208 NODE *node = *body;
12211 *body = NEW_NIL(&NULL_LOC);
12214#define subnodes(n1, n2) \
12215 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
12216 (!node->n2) ? (body = &node->n1, 1) : \
12217 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
12220 int newline = (int)(node->flags & NODE_FL_NEWLINE);
12221 switch (nd_type(node)) {
12227 *body = node = node->nd_stts;
12228 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12231 *body = node = node->nd_body;
12232 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12235 body = &node->nd_end->nd_head;
12239 if (subnodes(nd_body, nd_else)) break;
12242 body = &node->nd_body;
12245 if (!subnodes(nd_body, nd_next)) goto end;
12248 if (!subnodes(nd_head, nd_resq)) goto end;
12251 if (node->nd_else) {
12252 body = &node->nd_resq;
12255 if (!subnodes(nd_head, nd_resq)) goto end;
12261 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12268is_static_content(NODE *node)
12270 if (!node) return 1;
12271 switch (nd_type(node)) {
12273 if (!(node = node->nd_head)) break;
12276 if (!is_static_content(node->nd_head)) return 0;
12277 } while ((node = node->nd_next) != 0);
12292assign_in_cond(struct parser_params *p, NODE *node)
12294 switch (nd_type(node)) {
12306 if (!node->nd_value) return 1;
12307 if (is_static_content(node->nd_value)) {
12308 /* reports always */
12309 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
12320#define SWITCH_BY_COND_TYPE(t, w, arg) \
12322 case COND_IN_OP: break; \
12323 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
12324 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
12327static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
12330range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12332 enum node_type type;
12334 if (node == 0) return 0;
12336 type = nd_type(node);
12338 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
12339 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
12340 ID lineno = rb_intern("$.");
12341 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
12343 return cond0(p, node, COND_IN_FF, loc);
12347cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
12349 if (node == 0) return 0;
12350 if (!(node = nd_once_body(node))) return 0;
12351 assign_in_cond(p, node);
12353 switch (nd_type(node)) {
12357 SWITCH_BY_COND_TYPE(type, warn, "string ")
12361 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
12363 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
12367 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
12368 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
12373 node->nd_beg = range_op(p, node->nd_beg, loc);
12374 node->nd_end = range_op(p, node->nd_end, loc);
12375 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
12376 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
12381 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
12385 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
12386 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
12387 nd_set_type(node, NODE_MATCH);
12389 else if (node->nd_lit == Qtrue ||
12390 node->nd_lit == Qfalse) {
12391 /* booleans are OK, e.g., while true */
12393 else if (SYMBOL_P(node->nd_lit)) {
12397 SWITCH_BY_COND_TYPE(type, warning, "")
12406cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12408 if (node == 0) return 0;
12409 return cond0(p, node, COND_IN_COND, loc);
12413method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12415 if (node == 0) return 0;
12416 return cond0(p, node, COND_IN_OP, loc);
12420new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
12422 YYLTYPE loc = {*pos, *pos};
12423 return NEW_NIL(&loc);
12427new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12429 if (!cc) return right;
12430 cc = cond0(p, cc, COND_IN_COND, loc);
12431 return newline_node(NEW_IF(cc, left, right, loc));
12435new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12437 if (!cc) return right;
12438 cc = cond0(p, cc, COND_IN_COND, loc);
12439 return newline_node(NEW_UNLESS(cc, left, right, loc));
12443logop(struct parser_params *p, ID id, NODE *left, NODE *right,
12444 const YYLTYPE *op_loc, const YYLTYPE *loc)
12446 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
12449 if (left && nd_type_p(left, type)) {
12450 NODE *node = left, *second;
12451 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
12454 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
12455 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
12456 left->nd_loc.end_pos = loc->end_pos;
12459 op = NEW_NODE(type, left, right, 0, loc);
12460 nd_set_line(op, op_loc->beg_pos.lineno);
12465no_blockarg(struct parser_params *p, NODE *node)
12467 if (nd_type_p(node, NODE_BLOCK_PASS)) {
12468 compile_error(p, "block argument should not be given");
12473ret_args(struct parser_params *p, NODE *node)
12476 no_blockarg(p, node);
12477 if (nd_type_p(node, NODE_LIST)) {
12478 if (node->nd_next == 0) {
12479 node = node->nd_head;
12482 nd_set_type(node, NODE_VALUES);
12490new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12492 if (node) no_blockarg(p, node);
12494 return NEW_YIELD(node, loc);
12498negate_lit(struct parser_params *p, VALUE lit)
12500 if (FIXNUM_P(lit)) {
12501 return LONG2FIX(-FIX2LONG(lit));
12503 if (SPECIAL_CONST_P(lit)) {
12505 if (FLONUM_P(lit)) {
12506 return DBL2NUM(-RFLOAT_VALUE(lit));
12511 switch (BUILTIN_TYPE(lit)) {
12513 BIGNUM_NEGATE(lit);
12514 lit = rb_big_norm(lit);
12517 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
12520 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
12521 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
12524 lit = DBL2NUM(-RFLOAT_VALUE(lit));
12528 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
12529 rb_builtin_class_name(lit));
12536arg_blk_pass(NODE *node1, NODE *node2)
12539 if (!node1) return node2;
12540 node2->nd_head = node1;
12541 nd_set_first_lineno(node2, nd_first_lineno(node1));
12542 nd_set_first_column(node2, nd_first_column(node1));
12549args_info_empty_p(struct rb_args_info *args)
12551 if (args->pre_args_num) return false;
12552 if (args->post_args_num) return false;
12553 if (args->rest_arg) return false;
12554 if (args->opt_args) return false;
12555 if (args->block_arg) return false;
12556 if (args->kw_args) return false;
12557 if (args->kw_rest_arg) return false;
12562new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12564 int saved_line = p->ruby_sourceline;
12565 struct rb_args_info *args = tail->nd_ainfo;
12567 if (args->forwarding) {
12569 yyerror1(&tail->nd_loc, "... after rest argument");
12572 rest_arg = idFWD_REST;
12575 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12576 args->pre_init = pre_args ? pre_args->nd_next : 0;
12578 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12579 args->post_init = post_args ? post_args->nd_next : 0;
12580 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12582 args->rest_arg = rest_arg;
12584 args->opt_args = opt_args;
12586#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
12587 args->ruby2_keywords = args->forwarding;
12589 args->ruby2_keywords = 0;
12592 p->ruby_sourceline = saved_line;
12593 nd_set_loc(tail, loc);
12599new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12601 int saved_line = p->ruby_sourceline;
12603 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12604 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12605 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12606 args->imemo = tmpbuf;
12607 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12608 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12609 if (p->error_p) return node;
12611 args->block_arg = block;
12612 args->kw_args = kw_args;
12616 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12617 * variable order: k1, kr1, k2, &b, internal_id, krest
12619 * variable order: kr1, k1, k2, internal_id, krest, &b
12621 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12622 struct vtable *vtargs = p->lvtbl->args;
12623 NODE *kwn = kw_args;
12625 if (block) block = vtargs->tbl[vtargs->pos-1];
12626 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12627 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12629 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12631 --required_kw_vars;
12632 kwn = kwn->nd_next;
12635 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12636 ID vid = kwn->nd_body->nd_vid;
12637 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12638 *required_kw_vars++ = vid;
12645 arg_var(p, kw_bits);
12646 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12647 if (block) arg_var(p, block);
12649 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12650 args->kw_rest_arg->nd_cflag = kw_bits;
12652 else if (kw_rest_arg == idNil) {
12653 args->no_kwarg = 1;
12655 else if (kw_rest_arg) {
12656 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12659 p->ruby_sourceline = saved_line;
12664args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12666 if (max_numparam > NO_PARAM) {
12668 YYLTYPE loc = RUBY_INIT_YYLLOC();
12669 args = new_args_tail(p, 0, 0, 0, 0);
12670 nd_set_loc(args, &loc);
12672 args->nd_ainfo->pre_args_num = max_numparam;
12678new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12680 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12682 aryptn->nd_pconst = constant;
12685 NODE *pre_args = NEW_LIST(pre_arg, loc);
12686 if (apinfo->pre_args) {
12687 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12690 apinfo->pre_args = pre_args;
12697new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12699 int saved_line = p->ruby_sourceline;
12701 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12702 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12703 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12704 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12705 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12707 apinfo->pre_args = pre_args;
12711 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12714 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12718 apinfo->rest_arg = NULL;
12721 apinfo->post_args = post_args;
12723 p->ruby_sourceline = saved_line;
12728new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12730 fndptn->nd_pconst = constant;
12736new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12738 int saved_line = p->ruby_sourceline;
12740 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12741 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12742 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12743 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12744 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12746 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12747 fpinfo->args = args;
12748 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12750 p->ruby_sourceline = saved_line;
12755new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12757 hshptn->nd_pconst = constant;
12762new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12764 int saved_line = p->ruby_sourceline;
12765 NODE *node, *kw_rest_arg_node;
12767 if (kw_rest_arg == idNil) {
12768 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12770 else if (kw_rest_arg) {
12771 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12774 kw_rest_arg_node = NULL;
12777 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12779 p->ruby_sourceline = saved_line;
12784dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12789 return NEW_LIT(ID2SYM(idNULL), loc);
12792 switch (nd_type(node)) {
12794 nd_set_type(node, NODE_DSYM);
12795 nd_set_loc(node, loc);
12798 lit = node->nd_lit;
12799 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12800 nd_set_type(node, NODE_LIT);
12801 nd_set_loc(node, loc);
12804 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12811append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12813 NODE *node = (NODE *)v;
12814 NODE **result = (NODE **)h;
12816 node->nd_next->nd_end = node->nd_next;
12817 node->nd_next->nd_next = 0;
12819 list_concat(*result, node);
12822 return ST_CONTINUE;
12826hash_literal_key_p(VALUE k)
12828 switch (OBJ_BUILTIN_TYPE(k)) {
12837literal_cmp(VALUE val, VALUE lit)
12839 if (val == lit) return 0;
12840 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12841 return rb_iseq_cdhash_cmp(val, lit);
12845literal_hash(VALUE a)
12847 if (!hash_literal_key_p(a)) return (st_index_t)a;
12848 return rb_iseq_cdhash_hash(a);
12851static const struct st_hash_type literal_type = {
12857remove_duplicate_keys(struct parser_params *p, NODE *hash)
12859 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12861 NODE *last_expr = 0;
12862 rb_code_location_t loc = hash->nd_loc;
12863 while (hash && hash->nd_head && hash->nd_next) {
12864 NODE *head = hash->nd_head;
12865 NODE *value = hash->nd_next;
12866 NODE *next = value->nd_next;
12867 st_data_t key = (st_data_t)head;
12869 value->nd_next = 0;
12870 if (nd_type_p(head, NODE_LIT) &&
12871 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12872 NODE *dup_value = ((NODE *)data)->nd_next;
12873 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12874 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12875 head->nd_lit, nd_line(head));
12876 if (dup_value == last_expr) {
12877 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12880 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12883 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12884 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12887 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12888 st_free_table(literal_keys);
12890 if (!result) result = hash;
12891 else list_concat(result, hash);
12893 result->nd_loc = loc;
12898new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12900 if (hash) hash = remove_duplicate_keys(p, hash);
12901 return NEW_HASH(hash, loc);
12906error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12908 if (is_private_local_id(id)) {
12911 if (st_is_member(p->pvtbl, id)) {
12912 yyerror1(loc, "duplicated variable name");
12915 st_insert(p->pvtbl, (st_data_t)id, 0);
12920error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12923 p->pktbl = st_init_numtable();
12925 else if (st_is_member(p->pktbl, key)) {
12926 yyerror1(loc, "duplicated key name");
12929 st_insert(p->pktbl, (st_data_t)key, 0);
12934new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12936 return NEW_HASH(hash, loc);
12938#endif /* !RIPPER */
12942new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12947 ID vid = lhs->nd_vid;
12948 YYLTYPE lhs_loc = lhs->nd_loc;
12949 int shareable = ctxt.shareable_constant_value;
12951 switch (nd_type(lhs)) {
12962 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12963 lhs->nd_value = rhs;
12964 nd_set_loc(lhs, loc);
12965 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
12966 if (is_notop_id(vid)) {
12967 switch (id_type(vid)) {
12971 asgn->nd_aid = vid;
12975 else if (op == tANDOP) {
12977 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12979 lhs->nd_value = rhs;
12980 nd_set_loc(lhs, loc);
12981 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
12985 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
12987 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
12989 asgn->nd_value = rhs;
12990 nd_set_loc(asgn, loc);
12994 asgn = NEW_BEGIN(0, loc);
13000new_ary_op_assign(struct parser_params *p, NODE *ary,
13001 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
13005 args = make_list(args, args_loc);
13006 if (nd_type_p(args, NODE_BLOCK_PASS)) {
13007 args = NEW_ARGSCAT(args, rhs, loc);
13010 args = arg_concat(p, args, rhs, loc);
13012 asgn = NEW_OP_ASGN1(ary, op, args, loc);
13018new_attr_op_assign(struct parser_params *p, NODE *lhs,
13019 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
13023 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
13029new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13034 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13035 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
13038 asgn = NEW_BEGIN(0, loc);
13045const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
13047 if (p->ctxt.in_def) {
13048 yyerror1(loc, "dynamic constant assignment");
13050 return NEW_CDECL(0, 0, (path), loc);
13054const_decl(struct parser_params *p, VALUE path)
13056 if (p->ctxt.in_def) {
13057 path = assign_error(p, "dynamic constant assignment", path);
13063assign_error(struct parser_params *p, const char *mesg, VALUE a)
13065 a = dispatch2(assign_error, ERR_MESG(), a);
13071var_field(struct parser_params *p, VALUE a)
13073 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
13079new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
13081 NODE *result = head;
13083 NODE *tmp = rescue_else ? rescue_else : rescue;
13084 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
13086 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
13087 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
13089 else if (rescue_else) {
13090 result = block_append(p, result, rescue_else);
13093 result = NEW_ENSURE(result, ensure, loc);
13095 fixpos(result, head);
13101warn_unused_var(struct parser_params *p, struct local_vars *local)
13105 if (!local->used) return;
13106 cnt = local->used->pos;
13107 if (cnt != local->vars->pos) {
13108 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
13111 ID *v = local->vars->tbl;
13112 ID *u = local->used->tbl;
13113 for (int i = 0; i < cnt; ++i) {
13114 if (!v[i] || (u[i] & LVAR_USED)) continue;
13115 if (is_private_local_id(v[i])) continue;
13116 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
13122local_push(struct parser_params *p, int toplevel_scope)
13124 struct local_vars *local;
13125 int inherits_dvars = toplevel_scope && compile_for_eval;
13126 int warn_unused_vars = RTEST(ruby_verbose);
13128 local = ALLOC(struct local_vars);
13129 local->prev = p->lvtbl;
13130 local->args = vtable_alloc(0);
13131 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
13133 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
13134 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
13135 local->numparam.outer = 0;
13136 local->numparam.inner = 0;
13137 local->numparam.current = 0;
13139 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
13141# if WARN_PAST_SCOPE
13150local_pop(struct parser_params *p)
13152 struct local_vars *local = p->lvtbl->prev;
13153 if (p->lvtbl->used) {
13154 warn_unused_var(p, p->lvtbl);
13155 vtable_free(p->lvtbl->used);
13157# if WARN_PAST_SCOPE
13158 while (p->lvtbl->past) {
13159 struct vtable *past = p->lvtbl->past;
13160 p->lvtbl->past = past->prev;
13164 vtable_free(p->lvtbl->args);
13165 vtable_free(p->lvtbl->vars);
13168 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13173static rb_ast_id_table_t *
13174local_tbl(struct parser_params *p)
13176 int cnt_args = vtable_size(p->lvtbl->args);
13177 int cnt_vars = vtable_size(p->lvtbl->vars);
13178 int cnt = cnt_args + cnt_vars;
13180 rb_ast_id_table_t *tbl;
13182 if (cnt <= 0) return 0;
13183 tbl = rb_ast_new_local_table(p->ast, cnt);
13184 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
13185 /* remove IDs duplicated to warn shadowing */
13186 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
13187 ID id = p->lvtbl->vars->tbl[i];
13188 if (!vtable_included(p->lvtbl->args, id)) {
13189 tbl->ids[j++] = id;
13193 tbl = rb_ast_resize_latest_local_table(p->ast, j);
13200node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
13202 rb_ast_id_table_t *a0;
13206 n = NEW_NODE(type, a0, a1, a2, loc);
13213numparam_name(struct parser_params *p, ID id)
13215 if (!NUMPARAM_ID_P(id)) return;
13216 compile_error(p, "_%d is reserved for numbered parameter",
13217 NUMPARAM_ID_TO_IDX(id));
13221arg_var(struct parser_params *p, ID id)
13223 numparam_name(p, id);
13224 vtable_add(p->lvtbl->args, id);
13228local_var(struct parser_params *p, ID id)
13230 numparam_name(p, id);
13231 vtable_add(p->lvtbl->vars, id);
13232 if (p->lvtbl->used) {
13233 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
13238local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
13240 struct vtable *vars, *args, *used;
13242 vars = p->lvtbl->vars;
13243 args = p->lvtbl->args;
13244 used = p->lvtbl->used;
13246 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
13249 if (used) used = used->prev;
13252 if (vars && vars->prev == DVARS_INHERIT) {
13253 return rb_local_defined(id, p->parent_iseq);
13255 else if (vtable_included(args, id)) {
13259 int i = vtable_included(vars, id);
13260 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
13266local_id(struct parser_params *p, ID id)
13268 return local_id_ref(p, id, NULL);
13272check_forwarding_args(struct parser_params *p)
13274 if (local_id(p, idFWD_ALL)) return TRUE;
13275 compile_error(p, "unexpected ...");
13280add_forwarding_args(struct parser_params *p)
13282 arg_var(p, idFWD_REST);
13283#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13284 arg_var(p, idFWD_KWREST);
13286 arg_var(p, idFWD_BLOCK);
13287 arg_var(p, idFWD_ALL);
13292new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
13294 NODE *rest = NEW_LVAR(idFWD_REST, loc);
13295#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13296 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
13298 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
13299 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
13300#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13301 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
13303 return arg_blk_pass(args, block);
13308numparam_push(struct parser_params *p)
13311 struct local_vars *local = p->lvtbl;
13312 NODE *inner = local->numparam.inner;
13313 if (!local->numparam.outer) {
13314 local->numparam.outer = local->numparam.current;
13316 local->numparam.inner = 0;
13317 local->numparam.current = 0;
13325numparam_pop(struct parser_params *p, NODE *prev_inner)
13328 struct local_vars *local = p->lvtbl;
13330 /* prefer first one */
13331 local->numparam.inner = prev_inner;
13333 else if (local->numparam.current) {
13334 /* current and inner are exclusive */
13335 local->numparam.inner = local->numparam.current;
13337 if (p->max_numparam > NO_PARAM) {
13338 /* current and outer are exclusive */
13339 local->numparam.current = local->numparam.outer;
13340 local->numparam.outer = 0;
13343 /* no numbered parameter */
13344 local->numparam.current = 0;
13349static const struct vtable *
13350dyna_push(struct parser_params *p)
13352 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
13353 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
13354 if (p->lvtbl->used) {
13355 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
13357 return p->lvtbl->args;
13361dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
13363 struct vtable *tmp = *vtblp;
13364 *vtblp = tmp->prev;
13365# if WARN_PAST_SCOPE
13366 if (p->past_scope_enabled) {
13367 tmp->prev = p->lvtbl->past;
13368 p->lvtbl->past = tmp;
13376dyna_pop_1(struct parser_params *p)
13378 struct vtable *tmp;
13380 if ((tmp = p->lvtbl->used) != 0) {
13381 warn_unused_var(p, p->lvtbl);
13382 p->lvtbl->used = p->lvtbl->used->prev;
13385 dyna_pop_vtable(p, &p->lvtbl->args);
13386 dyna_pop_vtable(p, &p->lvtbl->vars);
13390dyna_pop(struct parser_params *p, const struct vtable *lvargs)
13392 while (p->lvtbl->args != lvargs) {
13394 if (!p->lvtbl->args) {
13395 struct local_vars *local = p->lvtbl->prev;
13396 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13404dyna_in_block(struct parser_params *p)
13406 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
13410dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
13412 struct vtable *vars, *args, *used;
13415 args = p->lvtbl->args;
13416 vars = p->lvtbl->vars;
13417 used = p->lvtbl->used;
13419 while (!DVARS_TERMINAL_P(vars)) {
13420 if (vtable_included(args, id)) {
13423 if ((i = vtable_included(vars, id)) != 0) {
13424 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
13429 if (!vidrefp) used = 0;
13430 if (used) used = used->prev;
13433 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
13434 return rb_dvar_defined(id, p->parent_iseq);
13441dvar_defined(struct parser_params *p, ID id)
13443 return dvar_defined_ref(p, id, NULL);
13447dvar_curr(struct parser_params *p, ID id)
13449 return (vtable_included(p->lvtbl->args, id) ||
13450 vtable_included(p->lvtbl->vars, id));
13454reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
13457 "regexp encoding option '%c' differs from source encoding '%s'",
13458 c, rb_enc_name(rb_enc_get(str)));
13463rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13465 int c = RE_OPTION_ENCODING_IDX(options);
13469 rb_char_to_option_kcode(c, &opt, &idx);
13470 if (idx != ENCODING_GET(str) &&
13471 !is_ascii_string(str)) {
13474 ENCODING_SET(str, idx);
13476 else if (RE_OPTION_ENCODING_NONE(options)) {
13477 if (!ENCODING_IS_ASCII8BIT(str) &&
13478 !is_ascii_string(str)) {
13482 rb_enc_associate(str, rb_ascii8bit_encoding());
13484 else if (rb_is_usascii_enc(p->enc)) {
13485 if (!is_ascii_string(str)) {
13486 /* raise in re.c */
13487 rb_enc_associate(str, rb_usascii_encoding());
13490 rb_enc_associate(str, rb_ascii8bit_encoding());
13500reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13502 int c = rb_reg_fragment_setenc(p, str, options);
13503 if (c) reg_fragment_enc_error(p, str, c);
13507reg_fragment_check(struct parser_params* p, VALUE str, int options)
13510 reg_fragment_setenc(p, str, options);
13511 err = rb_reg_check_preprocess(str);
13513 err = rb_obj_as_string(err);
13514 compile_error(p, "%"PRIsVALUE, err);
13521 struct parser_params* parser;
13524 const YYLTYPE *loc;
13525} reg_named_capture_assign_t;
13528reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
13529 int back_num, int *back_refs, OnigRegex regex, void *arg0)
13531 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
13532 struct parser_params* p = arg->parser;
13533 rb_encoding *enc = arg->enc;
13534 long len = name_end - name;
13535 const char *s = (const char *)name;
13539 if (!len) return ST_CONTINUE;
13540 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13541 return ST_CONTINUE;
13543 var = intern_cstr(s, len, enc);
13544 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13545 if (!lvar_defined(p, var)) return ST_CONTINUE;
13547 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13548 succ = arg->succ_block;
13549 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13550 succ = block_append(p, succ, node);
13551 arg->succ_block = succ;
13552 return ST_CONTINUE;
13556reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13558 reg_named_capture_assign_t arg;
13561 arg.enc = rb_enc_get(regexp);
13562 arg.succ_block = 0;
13564 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13566 if (!arg.succ_block) return 0;
13567 return arg.succ_block->nd_next;
13571parser_reg_compile(struct parser_params* p, VALUE str, int options)
13573 reg_fragment_setenc(p, str, options);
13574 return rb_parser_reg_compile(p, str, options);
13578rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13580 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13584reg_compile(struct parser_params* p, VALUE str, int options)
13589 err = rb_errinfo();
13590 re = parser_reg_compile(p, str, options);
13592 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13593 rb_set_errinfo(err);
13594 compile_error(p, "%"PRIsVALUE, m);
13601parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13603 VALUE err = rb_errinfo();
13605 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13606 int c = rb_reg_fragment_setenc(p, str, options);
13607 if (c) reg_fragment_enc_error(p, str, c);
13608 re = rb_parser_reg_compile(p, str, options);
13610 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13611 rb_set_errinfo(err);
13619rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13621 struct parser_params *p;
13622 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13623 p->do_print = print;
13625 p->do_chomp = chomp;
13626 p->do_split = split;
13630parser_append_options(struct parser_params *p, NODE *node)
13632 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13633 const YYLTYPE *const LOC = &default_location;
13636 NODE *print = NEW_FCALL(rb_intern("print"),
13637 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13639 node = block_append(p, node, print);
13643 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
13646 ID ifs = rb_intern("$;");
13647 ID fields = rb_intern("$F");
13648 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13649 NODE *split = NEW_GASGN(fields,
13650 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13651 rb_intern("split"), args, LOC),
13653 node = block_append(p, split, node);
13656 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
13657 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
13658 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
13661 node = NEW_WHILE(NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
13670 /* just to suppress unused-function warnings */
13676internal_id(struct parser_params *p)
13678 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13680#endif /* !RIPPER */
13683parser_initialize(struct parser_params *p)
13685 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13686 p->command_start = TRUE;
13687 p->ruby_sourcefile_string = Qnil;
13688 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13690 p->delayed.token = Qnil;
13693 p->parsing_thread = Qnil;
13695 p->error_buffer = Qfalse;
13696 p->end_expect_token_locations = Qnil;
13700 p->debug_buffer = Qnil;
13701 p->debug_output = rb_ractor_stdout();
13702 p->enc = rb_utf8_encoding();
13706#define parser_mark ripper_parser_mark
13707#define parser_free ripper_parser_free
13711parser_mark(void *ptr)
13713 struct parser_params *p = (struct parser_params*)ptr;
13715 rb_gc_mark(p->lex.input);
13716 rb_gc_mark(p->lex.lastline);
13717 rb_gc_mark(p->lex.nextline);
13718 rb_gc_mark(p->ruby_sourcefile_string);
13719 rb_gc_mark((VALUE)p->lex.strterm);
13720 rb_gc_mark((VALUE)p->ast);
13721 rb_gc_mark(p->case_labels);
13722 rb_gc_mark(p->delayed.token);
13724 rb_gc_mark(p->debug_lines);
13725 rb_gc_mark(p->compile_option);
13726 rb_gc_mark(p->error_buffer);
13727 rb_gc_mark(p->end_expect_token_locations);
13728 rb_gc_mark(p->tokens);
13730 rb_gc_mark(p->value);
13731 rb_gc_mark(p->result);
13732 rb_gc_mark(p->parsing_thread);
13734 rb_gc_mark(p->debug_buffer);
13735 rb_gc_mark(p->debug_output);
13737 rb_gc_mark((VALUE)p->heap);
13742parser_free(void *ptr)
13744 struct parser_params *p = (struct parser_params*)ptr;
13745 struct local_vars *local, *prev;
13748 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13750 for (local = p->lvtbl; local; local = prev) {
13751 if (local->vars) xfree(local->vars);
13752 prev = local->prev;
13756 token_info *ptinfo;
13757 while ((ptinfo = p->token_info) != 0) {
13758 p->token_info = ptinfo->next;
13766parser_memsize(const void *ptr)
13768 struct parser_params *p = (struct parser_params*)ptr;
13769 struct local_vars *local;
13770 size_t size = sizeof(*p);
13773 for (local = p->lvtbl; local; local = local->prev) {
13774 size += sizeof(*local);
13775 if (local->vars) size += local->vars->capa * sizeof(ID);
13780static const rb_data_type_t parser_data_type = {
13791 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13795#undef rb_reserved_word
13797const struct kwtable *
13798rb_reserved_word(const char *str, unsigned int len)
13800 return reserved_word(str, len);
13806 struct parser_params *p;
13807 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13808 &parser_data_type, p);
13809 parser_initialize(p);
13814rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13816 struct parser_params *p;
13818 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13819 p->error_buffer = main ? Qfalse : Qnil;
13820 p->parent_iseq = base;
13825rb_parser_keep_script_lines(VALUE vparser)
13827 struct parser_params *p;
13829 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13830 p->keep_script_lines = 1;
13834rb_parser_error_tolerant(VALUE vparser)
13836 struct parser_params *p;
13838 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13839 p->error_tolerant = 1;
13840 p->end_expect_token_locations = rb_ary_new();
13844rb_parser_keep_tokens(VALUE vparser)
13846 struct parser_params *p;
13848 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13849 p->keep_tokens = 1;
13850 p->tokens = rb_ary_new();
13856#define rb_parser_end_seen_p ripper_parser_end_seen_p
13857#define rb_parser_encoding ripper_parser_encoding
13858#define rb_parser_get_yydebug ripper_parser_get_yydebug
13859#define rb_parser_set_yydebug ripper_parser_set_yydebug
13860#define rb_parser_get_debug_output ripper_parser_get_debug_output
13861#define rb_parser_set_debug_output ripper_parser_set_debug_output
13862static VALUE ripper_parser_end_seen_p(VALUE vparser);
13863static VALUE ripper_parser_encoding(VALUE vparser);
13864static VALUE ripper_parser_get_yydebug(VALUE self);
13865static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13866static VALUE ripper_parser_get_debug_output(VALUE self);
13867static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13871 * ripper.error? -> Boolean
13873 * Return true if parsed source has errors.
13876ripper_error_p(VALUE vparser)
13878 struct parser_params *p;
13880 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13881 return RBOOL(p->error_p);
13887 * ripper.end_seen? -> Boolean
13889 * Return true if parsed source ended by +\_\_END\_\_+.
13892rb_parser_end_seen_p(VALUE vparser)
13894 struct parser_params *p;
13896 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13897 return RBOOL(p->ruby__end__seen);
13902 * ripper.encoding -> encoding
13904 * Return encoding of the source.
13907rb_parser_encoding(VALUE vparser)
13909 struct parser_params *p;
13911 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13912 return rb_enc_from_encoding(p->enc);
13918 * ripper.yydebug -> true or false
13923rb_parser_get_yydebug(VALUE self)
13925 struct parser_params *p;
13927 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13928 return RBOOL(p->debug);
13934 * ripper.yydebug = flag
13939rb_parser_set_yydebug(VALUE self, VALUE flag)
13941 struct parser_params *p;
13943 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13944 p->debug = RTEST(flag);
13950 * ripper.debug_output -> obj
13952 * Get debug output.
13955rb_parser_get_debug_output(VALUE self)
13957 struct parser_params *p;
13959 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13960 return p->debug_output;
13965 * ripper.debug_output = obj
13967 * Set debug output.
13970rb_parser_set_debug_output(VALUE self, VALUE output)
13972 struct parser_params *p;
13974 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13975 return p->debug_output = output;
13980#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
13981/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
13982 * potential memory leak */
13983#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
13984#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
13985 (new)->cnt = (cnt), (ptr))
13988rb_parser_malloc(struct parser_params *p, size_t size)
13990 size_t cnt = HEAPCNT(1, size);
13991 rb_imemo_tmpbuf_t *n = NEWHEAP();
13992 void *ptr = xmalloc(size);
13994 return ADD2HEAP(n, cnt, ptr);
13998rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
14000 size_t cnt = HEAPCNT(nelem, size);
14001 rb_imemo_tmpbuf_t *n = NEWHEAP();
14002 void *ptr = xcalloc(nelem, size);
14004 return ADD2HEAP(n, cnt, ptr);
14008rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
14010 rb_imemo_tmpbuf_t *n;
14011 size_t cnt = HEAPCNT(1, size);
14013 if (ptr && (n = p->heap) != NULL) {
14015 if (n->ptr == ptr) {
14016 n->ptr = ptr = xrealloc(ptr, size);
14017 if (n->cnt) n->cnt = cnt;
14020 } while ((n = n->next) != NULL);
14023 ptr = xrealloc(ptr, size);
14024 return ADD2HEAP(n, cnt, ptr);
14028rb_parser_free(struct parser_params *p, void *ptr)
14030 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
14032 while ((n = *prev) != NULL) {
14033 if (n->ptr == ptr) {
14043rb_parser_printf(struct parser_params *p, const char *fmt, ...)
14046 VALUE mesg = p->debug_buffer;
14048 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
14050 rb_str_vcatf(mesg, fmt, ap);
14052 if (RSTRING_END(mesg)[-1] == '\n') {
14053 rb_io_write(p->debug_output, mesg);
14054 p->debug_buffer = Qnil;
14059parser_compile_error(struct parser_params *p, const char *fmt, ...)
14063 rb_io_flush(p->debug_output);
14067 rb_syntax_error_append(p->error_buffer,
14068 p->ruby_sourcefile_string,
14069 p->ruby_sourceline,
14070 rb_long2int(p->lex.pcur - p->lex.pbeg),
14076count_char(const char *str, int c)
14079 while (str[n] == c) ++n;
14084 * strip enclosing double-quotes, same as the default yytnamerr except
14085 * for that single-quotes matching back-quotes do not stop stripping.
14087 * "\"`class' keyword\"" => "`class' keyword"
14089RUBY_FUNC_EXPORTED size_t
14090rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
14092 if (*yystr == '"') {
14093 size_t yyn = 0, bquote = 0;
14094 const char *yyp = yystr;
14100 bquote = count_char(yyp+1, '`') + 1;
14101 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
14109 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
14110 if (yyres) memcpy(yyres + yyn, yyp, bquote);
14116 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
14117 if (yyres) memcpy(yyres + yyn, yyp, 3);
14122 goto do_not_strip_quotes;
14125 goto do_not_strip_quotes;
14128 if (*++yyp != '\\')
14129 goto do_not_strip_quotes;
14130 /* Fall through. */
14145 do_not_strip_quotes: ;
14148 if (!yyres) return strlen(yystr);
14150 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
14158ripper_validate_object(VALUE self, VALUE x)
14160 if (x == Qfalse) return x;
14161 if (x == Qtrue) return x;
14162 if (NIL_P(x)) return x;
14164 rb_raise(rb_eArgError, "Qundef given");
14165 if (FIXNUM_P(x)) return x;
14166 if (SYMBOL_P(x)) return x;
14167 switch (BUILTIN_TYPE(x)) {
14177 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
14178 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
14180 x = ((NODE *)x)->nd_rval;
14183 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
14184 (void *)x, rb_obj_classname(x));
14186 if (!RBASIC_CLASS(x)) {
14187 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
14188 (void *)x, rb_builtin_type_name(TYPE(x)));
14194#define validate(x) ((x) = get_value(x))
14197ripper_dispatch0(struct parser_params *p, ID mid)
14199 return rb_funcall(p->value, mid, 0);
14203ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
14206 return rb_funcall(p->value, mid, 1, a);
14210ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
14214 return rb_funcall(p->value, mid, 2, a, b);
14218ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
14223 return rb_funcall(p->value, mid, 3, a, b, c);
14227ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
14233 return rb_funcall(p->value, mid, 4, a, b, c, d);
14237ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
14244 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
14248ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
14257 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
14261ripper_get_id(VALUE v)
14264 if (!RB_TYPE_P(v, T_NODE)) return 0;
14266 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
14271ripper_get_value(VALUE v)
14274 if (UNDEF_P(v)) return Qnil;
14275 if (!RB_TYPE_P(v, T_NODE)) return v;
14277 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
14278 return nd->nd_rval;
14282ripper_error(struct parser_params *p)
14288ripper_compile_error(struct parser_params *p, const char *fmt, ...)
14293 va_start(args, fmt);
14294 str = rb_vsprintf(fmt, args);
14296 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
14301ripper_lex_get_generic(struct parser_params *p, VALUE src)
14303 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
14304 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
14305 rb_raise(rb_eTypeError,
14306 "gets returned %"PRIsVALUE" (expected String or nil)",
14307 rb_obj_class(line));
14313ripper_lex_io_get(struct parser_params *p, VALUE src)
14315 return rb_io_gets(src);
14319ripper_s_allocate(VALUE klass)
14321 struct parser_params *p;
14322 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
14323 &parser_data_type, p);
14328#define ripper_initialized_p(r) ((r)->lex.input != 0)
14332 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
14334 * Create a new Ripper object.
14335 * _src_ must be a String, an IO, or an Object which has #gets method.
14337 * This method does not starts parsing.
14338 * See also Ripper#parse and Ripper.parse.
14341ripper_initialize(int argc, VALUE *argv, VALUE self)
14343 struct parser_params *p;
14344 VALUE src, fname, lineno;
14346 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14347 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
14348 if (RB_TYPE_P(src, T_FILE)) {
14349 p->lex.gets = ripper_lex_io_get;
14351 else if (rb_respond_to(src, id_gets)) {
14352 p->lex.gets = ripper_lex_get_generic;
14356 p->lex.gets = lex_get_str;
14358 p->lex.input = src;
14360 if (NIL_P(fname)) {
14361 fname = STR_NEW2("(ripper)");
14365 StringValueCStr(fname);
14366 fname = rb_str_new_frozen(fname);
14368 parser_initialize(p);
14370 p->ruby_sourcefile_string = fname;
14371 p->ruby_sourcefile = RSTRING_PTR(fname);
14372 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
14378ripper_parse0(VALUE parser_v)
14380 struct parser_params *p;
14382 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14384 p->ast = rb_ast_new();
14385 ripper_yyparse((void*)p);
14386 rb_ast_dispose(p->ast);
14392ripper_ensure(VALUE parser_v)
14394 struct parser_params *p;
14396 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14397 p->parsing_thread = Qnil;
14405 * Start parsing and returns the value of the root action.
14408ripper_parse(VALUE self)
14410 struct parser_params *p;
14412 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14413 if (!ripper_initialized_p(p)) {
14414 rb_raise(rb_eArgError, "method called for uninitialized object");
14416 if (!NIL_P(p->parsing_thread)) {
14417 if (p->parsing_thread == rb_thread_current())
14418 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
14420 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
14422 p->parsing_thread = rb_thread_current();
14423 rb_ensure(ripper_parse0, self, ripper_ensure, self);
14430 * ripper.column -> Integer
14432 * Return column number of current parsing line.
14433 * This number starts from 0.
14436ripper_column(VALUE self)
14438 struct parser_params *p;
14441 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14442 if (!ripper_initialized_p(p)) {
14443 rb_raise(rb_eArgError, "method called for uninitialized object");
14445 if (NIL_P(p->parsing_thread)) return Qnil;
14446 col = p->lex.ptok - p->lex.pbeg;
14447 return LONG2NUM(col);
14452 * ripper.filename -> String
14454 * Return current parsing filename.
14457ripper_filename(VALUE self)
14459 struct parser_params *p;
14461 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14462 if (!ripper_initialized_p(p)) {
14463 rb_raise(rb_eArgError, "method called for uninitialized object");
14465 return p->ruby_sourcefile_string;
14470 * ripper.lineno -> Integer
14472 * Return line number of current parsing line.
14473 * This number starts from 1.
14476ripper_lineno(VALUE self)
14478 struct parser_params *p;
14480 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14481 if (!ripper_initialized_p(p)) {
14482 rb_raise(rb_eArgError, "method called for uninitialized object");
14484 if (NIL_P(p->parsing_thread)) return Qnil;
14485 return INT2NUM(p->ruby_sourceline);
14490 * ripper.state -> Integer
14492 * Return scanner state of current token.
14495ripper_state(VALUE self)
14497 struct parser_params *p;
14499 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14500 if (!ripper_initialized_p(p)) {
14501 rb_raise(rb_eArgError, "method called for uninitialized object");
14503 if (NIL_P(p->parsing_thread)) return Qnil;
14504 return INT2NUM(p->lex.state);
14509 * ripper.token -> String
14511 * Return the current token string.
14514ripper_token(VALUE self)
14516 struct parser_params *p;
14519 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14520 if (!ripper_initialized_p(p)) {
14521 rb_raise(rb_eArgError, "method called for uninitialized object");
14523 if (NIL_P(p->parsing_thread)) return Qnil;
14524 pos = p->lex.ptok - p->lex.pbeg;
14525 len = p->lex.pcur - p->lex.ptok;
14526 return rb_str_subseq(p->lex.lastline, pos, len);
14532ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
14535 if (UNDEF_P(obj)) {
14536 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
14543ripper_value(VALUE self, VALUE obj)
14545 return ULONG2NUM(obj);
14551 * Ripper.lex_state_name(integer) -> string
14553 * Returns a string representation of lex_state.
14556ripper_lex_state_name(VALUE self, VALUE state)
14558 return rb_parser_lex_state_name(NUM2INT(state));
14564 ripper_init_eventids1();
14565 ripper_init_eventids2();
14566 id_warn = rb_intern_const("warn");
14567 id_warning = rb_intern_const("warning");
14568 id_gets = rb_intern_const("gets");
14569 id_assoc = rb_intern_const("=>");
14571 (void)yystpcpy; /* may not used in newer bison */
14581 Ripper = rb_define_class("Ripper", rb_cObject);
14582 /* version of Ripper */
14583 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14584 rb_define_alloc_func(Ripper, ripper_s_allocate);
14585 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14586 rb_define_method(Ripper, "parse", ripper_parse, 0);
14587 rb_define_method(Ripper, "column", ripper_column, 0);
14588 rb_define_method(Ripper, "filename", ripper_filename, 0);
14589 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14590 rb_define_method(Ripper, "state", ripper_state, 0);
14591 rb_define_method(Ripper, "token", ripper_token, 0);
14592 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14593 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14594 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14595 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14596 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14597 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14598 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14600 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14601 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14602 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14605 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14606 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14608 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14610<% @exprs.each do |expr, desc| -%>
14612 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14614 ripper_init_eventids1_table(Ripper);
14615 ripper_init_eventids2_table(Ripper);
14618 /* Hack to let RDoc document SCRIPT_LINES__ */
14621 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14622 * after the assignment will be added as an Array of lines with the file
14625 rb_define_global_const("SCRIPT_LINES__", Qnil);
14634 * c-file-style: "ruby"