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))
697/* Make a new temporal node, which should not be appeared in the
698 * result AST and does not have node_id and location. */
699static NODE* node_new_temporal(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2);
700#define NODE_NEW_TEMPORAL(t,a0,a1,a2) node_new_temporal(p, (t),(VALUE)(a0),(VALUE)(a1),(VALUE)(a2))
702static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
705parser_get_node_id(struct parser_params *p)
707 int node_id = p->node_id;
714set_line_body(NODE *body, int line)
717 switch (nd_type(body)) {
720 nd_set_line(body, line);
724#define yyparse ruby_yyparse
726static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
727static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
728#define new_nil(loc) NEW_NIL(loc)
729static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
730static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
731static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
732static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
734static NODE *newline_node(NODE*);
735static void fixpos(NODE*,NODE*);
737static int value_expr_gen(struct parser_params*,NODE*);
738static void void_expr(struct parser_params*,NODE*);
739static NODE *remove_begin(NODE*);
740static NODE *remove_begin_all(NODE*);
741#define value_expr(node) value_expr_gen(p, (node))
742static NODE *void_stmts(struct parser_params*,NODE*);
743static void reduce_nodes(struct parser_params*,NODE**);
744static void block_dup_check(struct parser_params*,NODE*,NODE*);
746static NODE *block_append(struct parser_params*,NODE*,NODE*);
747static NODE *list_append(struct parser_params*,NODE*,NODE*);
748static NODE *list_concat(NODE*,NODE*);
749static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
750static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
751static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
752static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
753static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
754static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
755static NODE *evstr2dstr(struct parser_params*,NODE*);
756static NODE *splat_array(NODE*);
757static void mark_lvar_used(struct parser_params *p, NODE *rhs);
759static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
760static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
761static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
762static 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);
763static 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;}
765static bool args_info_empty_p(struct rb_args_info *args);
766static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
767static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
768static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
769static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
770static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
771static NODE *new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc);
772static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
773static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
775static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
776static NODE *args_with_numbered(struct parser_params*,NODE*,int);
778static VALUE negate_lit(struct parser_params*, VALUE);
779static NODE *ret_args(struct parser_params*,NODE*);
780static NODE *arg_blk_pass(NODE*,NODE*);
781static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
782static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
784static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
785static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
787static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
788static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
790static void rb_backref_error(struct parser_params*,NODE*);
791static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
793static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
794static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
795static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
796static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
797static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
799static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
801static NODE *opt_arg_append(NODE*, NODE*);
802static NODE *kwd_append(NODE*, NODE*);
804static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
805static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
807static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
809static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
811#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
813static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
815static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
817static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
819static rb_ast_id_table_t *local_tbl(struct parser_params*);
821static VALUE reg_compile(struct parser_params*, VALUE, int);
822static void reg_fragment_setenc(struct parser_params*, VALUE, int);
823static int reg_fragment_check(struct parser_params*, VALUE, int);
824static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
826static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
827static NODE *heredoc_dedent(struct parser_params*,NODE*);
829static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
831#define get_id(id) (id)
832#define get_value(val) (val)
833#define get_num(num) (num)
835#define NODE_RIPPER NODE_CDECL
836#define NEW_RIPPER(a,b,c,loc) (VALUE)NEW_CDECL(a,b,c,loc)
838static inline int ripper_is_node_yylval(VALUE n);
841ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
843 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
844 add_mark_object(p, b);
845 add_mark_object(p, c);
846 return NEW_RIPPER(a, b, c, &NULL_LOC);
850ripper_is_node_yylval(VALUE n)
852 return RB_TYPE_P(n, T_NODE) && nd_type_p(RNODE(n), NODE_RIPPER);
855#define value_expr(node) ((void)(node))
856#define remove_begin(node) (node)
857#define void_stmts(p,x) (x)
858#define rb_dvar_defined(id, base) 0
859#define rb_local_defined(id, base) 0
860static ID ripper_get_id(VALUE);
861#define get_id(id) ripper_get_id(id)
862static VALUE ripper_get_value(VALUE);
863#define get_value(val) ripper_get_value(val)
864#define get_num(num) (int)get_id(num)
865static VALUE assignable(struct parser_params*,VALUE);
866static int id_is_var(struct parser_params *p, ID id);
868#define method_cond(p,node,loc) (node)
869#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
870#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
871#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
872#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
874#define new_nil(loc) Qnil
876static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
878static VALUE const_decl(struct parser_params *p, VALUE path);
880static VALUE var_field(struct parser_params *p, VALUE a);
881static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
883static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
885static VALUE backref_error(struct parser_params*, NODE *, VALUE);
888/* forward declaration */
889typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
891RUBY_SYMBOL_EXPORT_BEGIN
892VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
893int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
894enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
895VALUE rb_parser_lex_state_name(enum lex_state_e state);
896void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
897PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
898YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
899YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
900YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
901YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
902YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
903YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
904RUBY_SYMBOL_EXPORT_END
906static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
907static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
909static ID formal_argument(struct parser_params*, ID);
911static ID formal_argument(struct parser_params*, VALUE);
913static ID shadowing_lvar(struct parser_params*,ID);
914static void new_bv(struct parser_params*,ID);
916static void local_push(struct parser_params*,int);
917static void local_pop(struct parser_params*);
918static void local_var(struct parser_params*, ID);
919static void arg_var(struct parser_params*, ID);
920static int local_id(struct parser_params *p, ID id);
921static int local_id_ref(struct parser_params*, ID, ID **);
923static ID internal_id(struct parser_params*);
924static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
926static int check_forwarding_args(struct parser_params*);
927static void add_forwarding_args(struct parser_params *p);
929static const struct vtable *dyna_push(struct parser_params *);
930static void dyna_pop(struct parser_params*, const struct vtable *);
931static int dyna_in_block(struct parser_params*);
932#define dyna_var(p, id) local_var(p, id)
933static int dvar_defined(struct parser_params*, ID);
934static int dvar_defined_ref(struct parser_params*, ID, ID**);
935static int dvar_curr(struct parser_params*,ID);
937static int lvar_defined(struct parser_params*, ID);
939static NODE *numparam_push(struct parser_params *p);
940static void numparam_pop(struct parser_params *p, NODE *prev_inner);
943# define METHOD_NOT idNOT
945# define METHOD_NOT '!'
948#define idFWD_REST '*'
949#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
950#define idFWD_BLOCK '&'
951#define idFWD_ALL idDot3
952#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS
954#define RE_OPTION_ONCE (1<<16)
955#define RE_OPTION_ENCODING_SHIFT 8
956#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
957#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
958#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
959#define RE_OPTION_MASK 0xff
960#define RE_OPTION_ARG_ENCODING_NONE 32
962/* structs for managing terminator of string literal and heredocment */
963typedef struct rb_strterm_literal_struct {
970 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
974 long paren; /* '(' of `%q(...)` */
978 long term; /* ')' of `%q(...)` */
980} rb_strterm_literal_t;
982#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
984struct rb_strterm_heredoc_struct {
985 VALUE lastline; /* the string of line that contains `<<"END"` */
986 long offset; /* the column of END in `<<"END"` */
987 int sourceline; /* lineno of the line that contains `<<"END"` */
988 unsigned length /* the length of END in `<<"END"` */
989#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
990 : HERETERM_LENGTH_BITS
991# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
993# define HERETERM_LENGTH_MAX UINT_MAX
996#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
1004STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
1006#define STRTERM_HEREDOC IMEMO_FL_USER0
1008struct rb_strterm_struct {
1011 rb_strterm_literal_t literal;
1012 rb_strterm_heredoc_t heredoc;
1018rb_strterm_mark(VALUE obj)
1020 rb_strterm_t *strterm = (rb_strterm_t*)obj;
1021 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
1022 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
1023 rb_gc_mark(heredoc->lastline);
1028#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1029size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1031#define TOKEN2ID(tok) ( \
1032 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1033 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1034 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1035 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1036 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1037 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1038 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1040/****** Ripper *******/
1043#define RIPPER_VERSION "0.1.0"
1045static inline VALUE intern_sym(const char *name);
1047#include "eventids1.c"
1048#include "eventids2.c"
1050static VALUE ripper_dispatch0(struct parser_params*,ID);
1051static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1052static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1053static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1054static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1055static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1056static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1057static void ripper_error(struct parser_params *p);
1059#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
1060#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
1061#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
1062#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
1063#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
1064#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
1065#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))
1067#define yyparse ripper_yyparse
1069#define ID2VAL(id) STATIC_ID2SYM(id)
1070#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1071#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
1073#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
1074 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
1076#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
1079new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
1081 NODE *t = (NODE *)tail;
1082 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
1083 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
1087new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
1089 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
1090 add_mark_object(p, kw_args);
1091 add_mark_object(p, kw_rest_arg);
1092 add_mark_object(p, block);
1097args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
1103new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
1105 NODE *t = (NODE *)aryptn;
1106 VALUE pre_args = t->u1.value, rest_arg = t->u2.value, post_args = t->u3.value;
1108 if (!NIL_P(pre_arg)) {
1109 if (!NIL_P(pre_args)) {
1110 rb_ary_unshift(pre_args, pre_arg);
1113 pre_args = rb_ary_new_from_args(1, pre_arg);
1116 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
1120new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
1125 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
1131 t = rb_node_newnode(NODE_ARYPTN, pre_args, rest_arg, post_args, &NULL_LOC);
1132 add_mark_object(p, pre_args);
1133 add_mark_object(p, rest_arg);
1134 add_mark_object(p, post_args);
1139new_find_pattern(struct parser_params *p, VALUE constant, VALUE fndptn, const YYLTYPE *loc)
1141 NODE *t = (NODE *)fndptn;
1142 VALUE pre_rest_arg = t->u1.value, args = t->u2.value, post_rest_arg = t->u3.value;
1144 return dispatch4(fndptn, constant, pre_rest_arg, args, post_rest_arg);
1148new_find_pattern_tail(struct parser_params *p, VALUE pre_rest_arg, VALUE args, VALUE post_rest_arg, const YYLTYPE *loc)
1152 pre_rest_arg = dispatch1(var_field, pre_rest_arg ? pre_rest_arg : Qnil);
1153 post_rest_arg = dispatch1(var_field, post_rest_arg ? post_rest_arg : Qnil);
1155 t = rb_node_newnode(NODE_FNDPTN, pre_rest_arg, args, post_rest_arg, &NULL_LOC);
1156 add_mark_object(p, pre_rest_arg);
1157 add_mark_object(p, args);
1158 add_mark_object(p, post_rest_arg);
1162#define new_hash(p,h,l) rb_ary_new_from_args(0)
1165new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
1171new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
1173 NODE *t = (NODE *)hshptn;
1174 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
1175 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
1179new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
1183 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
1188 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
1190 add_mark_object(p, kw_args);
1191 add_mark_object(p, kw_rest_arg);
1195#define new_defined(p,expr,loc) dispatch1(defined, (expr))
1197static VALUE heredoc_dedent(struct parser_params*,VALUE);
1200#define ID2VAL(id) (id)
1201#define TOKEN2VAL(t) ID2VAL(t)
1202#define KWD2EID(t, v) keyword_##t
1205set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const YYLTYPE *loc)
1207 body = remove_begin(body);
1208 reduce_nodes(p, &body);
1209 n->nd_defn = NEW_SCOPE(args, body, loc);
1211 nd_set_line(n->nd_defn, loc->end_pos.lineno);
1212 set_line_body(body, loc->beg_pos.lineno);
1217rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1218 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1220 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1221 rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1222 loc.beg_pos = arg_loc->beg_pos;
1223 return NEW_RESCUE(arg, rescue, 0, &loc);
1229restore_defun(struct parser_params *p, NODE *name)
1231 NODE *save = name->nd_next;
1232 YYSTYPE c = {.val = save->nd_cval};
1233 p->cur_arg = name->nd_vid;
1234 p->ctxt.in_def = c.ctxt.in_def;
1235 p->ctxt.shareable_constant_value = c.ctxt.shareable_constant_value;
1236 p->max_numparam = (int)save->nd_nth;
1237 numparam_pop(p, save->nd_head);
1241endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
1244 defn = defn->nd_defn;
1246 ID mid = defn->nd_mid;
1247 if (is_attrset_id(mid)) {
1248 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1250 token_info_drop(p, "def", loc->beg_pos);
1253#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)
1258# define ifndef_ripper(x) (x)
1261# define Qnull Qundef
1262# define ifndef_ripper(x)
1265# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1266# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1267# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1268# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1269# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1270# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1271# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1272# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1273# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1274# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1275# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1276# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1277# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1278# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1279# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1280# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1281# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1282# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1283# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1284# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1286static ID id_warn, id_warning, id_gets, id_assoc;
1287# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1288# define WARN_S_L(s,l) STR_NEW(s,l)
1289# define WARN_S(s) STR_NEW2(s)
1290# define WARN_I(i) INT2NUM(i)
1291# define WARN_ID(i) rb_id2str(i)
1292# define WARN_IVAL(i) i
1293# define PRIsWARN "s"
1294# define rb_warn0L_experimental(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1295# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1296# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1297# ifdef HAVE_VA_ARGS_MACRO
1298# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1300# define WARN_CALL rb_funcall
1302# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1303# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1304# ifdef HAVE_VA_ARGS_MACRO
1305# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1307# define WARNING_CALL rb_funcall
1309PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1310# define compile_error ripper_compile_error
1312# define WARN_S_L(s,l) s
1315# define WARN_ID(i) rb_id2name(i)
1316# define WARN_IVAL(i) NUM2INT(i)
1317# define PRIsWARN PRIsVALUE
1318# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1319# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1320# define WARN_CALL rb_compile_warn
1321# define rb_warn0L_experimental(l,fmt) rb_category_compile_warn(RB_WARN_CATEGORY_EXPERIMENTAL, WARN_ARGS_L(l, fmt, 1))
1322# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1323# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1324# define WARNING_CALL rb_compile_warning
1325PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
1326# define compile_error parser_compile_error
1329#define WARN_EOL(tok) \
1330 (looking_at_eol_p(p) ? \
1331 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
1333static int looking_at_eol_p(struct parser_params *p);
1338%define parse.error verbose
1342 rb_parser_printf(p, "%s", ruby_node_name(nd_type($$)));
1349 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
1351 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
1353} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
1356 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
1358 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
1360} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
1363 rb_parser_printf(p, "$%ld", $$->nd_nth);
1365 rb_parser_printf(p, "%"PRIsVALUE, $$);
1370 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
1372 rb_parser_printf(p, "%"PRIsVALUE, $$);
1376%lex-param {struct parser_params *p}
1377%parse-param {struct parser_params *p}
1380 RUBY_SET_YYLLOC_OF_NONE(@$);
1389 const struct vtable *vars;
1390 struct rb_strterm_struct *strterm;
1391 struct lex_context ctxt;
1395 keyword_class "`class'"
1396 keyword_module "`module'"
1398 keyword_undef "`undef'"
1399 keyword_begin "`begin'"
1400 keyword_rescue "`rescue'"
1401 keyword_ensure "`ensure'"
1404 keyword_unless "`unless'"
1405 keyword_then "`then'"
1406 keyword_elsif "`elsif'"
1407 keyword_else "`else'"
1408 keyword_case "`case'"
1409 keyword_when "`when'"
1410 keyword_while "`while'"
1411 keyword_until "`until'"
1413 keyword_break "`break'"
1414 keyword_next "`next'"
1415 keyword_redo "`redo'"
1416 keyword_retry "`retry'"
1419 keyword_do_cond "`do' for condition"
1420 keyword_do_block "`do' for block"
1421 keyword_do_LAMBDA "`do' for lambda"
1422 keyword_return "`return'"
1423 keyword_yield "`yield'"
1424 keyword_super "`super'"
1425 keyword_self "`self'"
1427 keyword_true "`true'"
1428 keyword_false "`false'"
1432 modifier_if "`if' modifier"
1433 modifier_unless "`unless' modifier"
1434 modifier_while "`while' modifier"
1435 modifier_until "`until' modifier"
1436 modifier_rescue "`rescue' modifier"
1437 keyword_alias "`alias'"
1438 keyword_defined "`defined?'"
1439 keyword_BEGIN "`BEGIN'"
1441 keyword__LINE__ "`__LINE__'"
1442 keyword__FILE__ "`__FILE__'"
1443 keyword__ENCODING__ "`__ENCODING__'"
1445%token <id> tIDENTIFIER "local variable or method"
1446%token <id> tFID "method"
1447%token <id> tGVAR "global variable"
1448%token <id> tIVAR "instance variable"
1449%token <id> tCONSTANT "constant"
1450%token <id> tCVAR "class variable"
1451%token <id> tLABEL "label"
1452%token <node> tINTEGER "integer literal"
1453%token <node> tFLOAT "float literal"
1454%token <node> tRATIONAL "rational literal"
1455%token <node> tIMAGINARY "imaginary literal"
1456%token <node> tCHAR "char literal"
1457%token <node> tNTH_REF "numbered reference"
1458%token <node> tBACK_REF "back reference"
1459%token <node> tSTRING_CONTENT "literal content"
1460%token <num> tREGEXP_END
1461%token <num> tDUMNY_END "dummy end"
1463%type <node> singleton strings string string1 xstring regexp
1464%type <node> string_contents xstring_contents regexp_contents string_content
1465%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1466%type <node> literal numeric simple_numeric ssym dsym symbol cpath def_name defn_head defs_head
1467%type <node> top_compstmt top_stmts top_stmt begin_block
1468%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1469%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1470%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1471%type <node> args call_args opt_call_args
1472%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1473%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1474%type <node> command_rhs arg_rhs
1475%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1476%type <node> f_block_optarg f_block_opt
1477%type <node> f_arglist f_opt_paren_args f_paren_args f_args f_arg f_arg_item
1478%type <node> f_optarg f_marg f_marg_list f_margs f_rest_marg
1479%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1480%type <node> block_param opt_block_param block_param_def f_opt
1481%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1482%type <node> bv_decls opt_bv_decl bvar
1483%type <node> lambda f_larglist lambda_body brace_body do_body
1484%type <node> brace_block cmd_brace_block do_block lhs none fitem
1485%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1486%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1487%type <node> p_expr p_as p_alt p_expr_basic p_find
1488%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1489%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
1490%type <node> p_kwargs p_kwarg p_kw
1491%type <id> keyword_variable user_variable sym operation operation2 operation3
1492%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1493%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1494%type <id> p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label
1495%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var
1496 %type <ctxt> lex_ctxt /* keep <ctxt> in ripper */
1497%token END_OF_INPUT 0 "end-of-input"
1499/* escaped chars, should be ignored otherwise */
1500%token <id> '\\' "backslash"
1501%token tSP "escaped space"
1502%token <id> '\t' "escaped horizontal tab"
1503%token <id> '\f' "escaped form feed"
1504%token <id> '\r' "escaped carriage return"
1505%token <id> '\13' "escaped vertical tab"
1506%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1507%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1508%token tPOW RUBY_TOKEN(POW) "**"
1509%token tCMP RUBY_TOKEN(CMP) "<=>"
1510%token tEQ RUBY_TOKEN(EQ) "=="
1511%token tEQQ RUBY_TOKEN(EQQ) "==="
1512%token tNEQ RUBY_TOKEN(NEQ) "!="
1513%token tGEQ RUBY_TOKEN(GEQ) ">="
1514%token tLEQ RUBY_TOKEN(LEQ) "<="
1515%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1516%token tOROP RUBY_TOKEN(OROP) "||"
1517%token tMATCH RUBY_TOKEN(MATCH) "=~"
1518%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1519%token tDOT2 RUBY_TOKEN(DOT2) ".."
1520%token tDOT3 RUBY_TOKEN(DOT3) "..."
1521%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1522%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1523%token tAREF RUBY_TOKEN(AREF) "[]"
1524%token tASET RUBY_TOKEN(ASET) "[]="
1525%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1526%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1527%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1528%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1529%token tCOLON3 ":: at EXPR_BEG"
1530%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1533%token tLPAREN_ARG "( arg"
1537%token tLBRACE_ARG "{ arg"
1539%token tDSTAR "**arg"
1542%token tSYMBEG "symbol literal"
1543%token tSTRING_BEG "string literal"
1544%token tXSTRING_BEG "backtick literal"
1545%token tREGEXP_BEG "regexp literal"
1546%token tWORDS_BEG "word list"
1547%token tQWORDS_BEG "verbatim word list"
1548%token tSYMBOLS_BEG "symbol list"
1549%token tQSYMBOLS_BEG "verbatim symbol list"
1550%token tSTRING_END "terminator"
1551%token tSTRING_DEND "'}'"
1552%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1554%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
1555%token tHEREDOC_BEG tHEREDOC_END k__END__
1562%nonassoc tLBRACE_ARG
1564%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1565%left keyword_or keyword_and
1567%nonassoc keyword_defined
1569%left modifier_rescue
1571%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1574%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1575%left '>' tGEQ '<' tLEQ
1581%right tUMINUS_NUM tUMINUS
1583%right '!' '~' tUPLUS
1589 SET_LEX_STATE(EXPR_BEG);
1590 local_push(p, ifndef_ripper(1)+0);
1595 if ($2 && !compile_for_eval) {
1597 /* last expression should not be void */
1598 if (nd_type_p(node, NODE_BLOCK)) {
1599 while (node->nd_next) {
1600 node = node->nd_next;
1602 node = node->nd_head;
1604 node = remove_begin(node);
1607 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1609 /*% ripper[final]: program!($2) %*/
1614top_compstmt : top_stmts opt_terms
1616 $$ = void_stmts(p, $1);
1623 $$ = NEW_BEGIN(0, &@$);
1625 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1630 $$ = newline_node($1);
1632 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1634 | top_stmts terms top_stmt
1637 $$ = block_append(p, $1, newline_node($3));
1639 /*% ripper: stmts_add!($1, $3) %*/
1644 | keyword_BEGIN begin_block
1650begin_block : '{' top_compstmt '}'
1653 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1654 NEW_BEGIN($2, &@$));
1655 $$ = NEW_BEGIN(0, &@$);
1657 /*% ripper: BEGIN!($2) %*/
1663 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1668 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1670 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1677 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1679 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1683compstmt : stmts opt_terms
1685 $$ = void_stmts(p, $1);
1692 $$ = NEW_BEGIN(0, &@$);
1694 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1699 $$ = newline_node($1);
1701 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1703 | stmts terms stmt_or_begin
1706 $$ = block_append(p, $1, newline_node($3));
1708 /*% ripper: stmts_add!($1, $3) %*/
1718 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1726stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1729 $$ = NEW_ALIAS($2, $4, &@$);
1731 /*% ripper: alias!($2, $4) %*/
1733 | keyword_alias tGVAR tGVAR
1736 $$ = NEW_VALIAS($2, $3, &@$);
1738 /*% ripper: var_alias!($2, $3) %*/
1740 | keyword_alias tGVAR tBACK_REF
1745 buf[1] = (char)$3->nd_nth;
1746 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1748 /*% ripper: var_alias!($2, $3) %*/
1750 | keyword_alias tGVAR tNTH_REF
1752 static const char mesg[] = "can't make alias for the number variables";
1754 yyerror1(&@3, mesg);
1755 $$ = NEW_BEGIN(0, &@$);
1757 /*% ripper[error]: alias_error!(ERR_MESG(), $3) %*/
1759 | keyword_undef undef_list
1764 /*% ripper: undef!($2) %*/
1766 | stmt modifier_if expr_value
1769 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1772 /*% ripper: if_mod!($3, $1) %*/
1774 | stmt modifier_unless expr_value
1777 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1780 /*% ripper: unless_mod!($3, $1) %*/
1782 | stmt modifier_while expr_value
1785 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1786 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1789 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1792 /*% ripper: while_mod!($3, $1) %*/
1794 | stmt modifier_until expr_value
1797 if ($1 && nd_type_p($1, NODE_BEGIN)) {
1798 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1801 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1804 /*% ripper: until_mod!($3, $1) %*/
1806 | stmt modifier_rescue stmt
1810 YYLTYPE loc = code_loc_gen(&@2, &@3);
1811 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1812 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1814 /*% ripper: rescue_mod!($1, $3) %*/
1816 | keyword_END '{' compstmt '}'
1818 if (p->ctxt.in_def) {
1819 rb_warn0("END in method; use at_exit");
1823 NODE *scope = NEW_NODE(
1824 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1825 $$ = NEW_POSTEXE(scope, &@$);
1828 /*% ripper: END!($3) %*/
1831 | mlhs '=' lex_ctxt command_call
1835 $$ = node_assign(p, $1, $4, $3, &@$);
1837 /*% ripper: massign!($1, $4) %*/
1839 | lhs '=' lex_ctxt mrhs
1842 $$ = node_assign(p, $1, $4, $3, &@$);
1844 /*% ripper: assign!($1, $4) %*/
1846 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt
1849 YYLTYPE loc = code_loc_gen(&@5, &@6);
1850 $$ = node_assign(p, $1, NEW_RESCUE($4, NEW_RESBODY(0, remove_begin($6), 0, &loc), 0, &@$), $3, &@$);
1852 /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/
1854 | mlhs '=' lex_ctxt mrhs_arg
1857 $$ = node_assign(p, $1, $4, $3, &@$);
1859 /*% ripper: massign!($1, $4) %*/
1865 $$ = NEW_ERROR(&@$);
1870command_asgn : lhs '=' lex_ctxt command_rhs
1873 $$ = node_assign(p, $1, $4, $3, &@$);
1875 /*% ripper: assign!($1, $4) %*/
1877 | var_lhs tOP_ASGN lex_ctxt command_rhs
1880 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
1882 /*% ripper: opassign!($1, $2, $4) %*/
1884 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
1887 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
1889 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
1892 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1895 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1897 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1899 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs
1902 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
1904 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1906 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
1909 YYLTYPE loc = code_loc_gen(&@1, &@3);
1910 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
1912 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
1914 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
1917 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
1919 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
1921 | defn_head f_opt_paren_args '=' command
1923 endless_method_name(p, $<node>1, &@1);
1924 restore_defun(p, $<node>1->nd_defn);
1926 $$ = set_defun_body(p, $1, $2, $4, &@$);
1928 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1929 /*% ripper: def!(get_value($1), $2, $4) %*/
1932 | defn_head f_opt_paren_args '=' command modifier_rescue arg
1934 endless_method_name(p, $<node>1, &@1);
1935 restore_defun(p, $<node>1->nd_defn);
1937 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1938 $$ = set_defun_body(p, $1, $2, $4, &@$);
1940 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1941 /*% ripper: def!(get_value($1), $2, $4) %*/
1944 | defs_head f_opt_paren_args '=' command
1946 endless_method_name(p, $<node>1, &@1);
1947 restore_defun(p, $<node>1->nd_defn);
1949 $$ = set_defun_body(p, $1, $2, $4, &@$);
1953 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
1954 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1957 | defs_head f_opt_paren_args '=' command modifier_rescue arg
1959 endless_method_name(p, $<node>1, &@1);
1960 restore_defun(p, $<node>1->nd_defn);
1962 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
1963 $$ = set_defun_body(p, $1, $2, $4, &@$);
1967 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
1968 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
1971 | backref tOP_ASGN lex_ctxt command_rhs
1974 rb_backref_error(p, $1);
1975 $$ = NEW_BEGIN(0, &@$);
1977 /*% ripper[error]: backref_error(p, RNODE($1), assign!(var_field(p, $1), $4)) %*/
1981command_rhs : command_call %prec tOP_ASGN
1986 | command_call modifier_rescue stmt
1989 YYLTYPE loc = code_loc_gen(&@2, &@3);
1991 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1993 /*% ripper: rescue_mod!($1, $3) %*/
1999 | expr keyword_and expr
2001 $$ = logop(p, idAND, $1, $3, &@2, &@$);
2003 | expr keyword_or expr
2005 $$ = logop(p, idOR, $1, $3, &@2, &@$);
2007 | keyword_not opt_nl expr
2009 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2013 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2018 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2019 p->command_start = FALSE;
2021 p->ctxt.in_kwarg = 1;
2022 $<tbl>$ = push_pvtbl(p);
2025 $<tbl>$ = push_pktbl(p);
2029 pop_pktbl(p, $<tbl>4);
2030 pop_pvtbl(p, $<tbl>3);
2031 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2033 $$ = NEW_CASE3($1, NEW_IN($5, 0, 0, &@5), &@$);
2035 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2040 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
2041 p->command_start = FALSE;
2043 p->ctxt.in_kwarg = 1;
2044 $<tbl>$ = push_pvtbl(p);
2047 $<tbl>$ = push_pktbl(p);
2051 pop_pktbl(p, $<tbl>4);
2052 pop_pvtbl(p, $<tbl>3);
2053 p->ctxt.in_kwarg = $<ctxt>2.in_kwarg;
2055 $$ = NEW_CASE3($1, NEW_IN($5, NEW_TRUE(&@5), NEW_FALSE(&@5), &@5), &@$);
2057 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
2059 | arg %prec tLBRACE_ARG
2064 ID fname = get_id($1);
2065 ID cur_arg = p->cur_arg;
2066 YYSTYPE c = {.ctxt = p->ctxt};
2067 numparam_name(p, fname);
2069 NODE_NEW_TEMPORAL(NODE_SELF,
2070 /*head*/numparam_push(p),
2071 /*nth*/p->max_numparam,
2076 $<node>$ = NEW_NODE(NODE_SELF, /*vid*/cur_arg, /*mid*/fname, /*args*/save, &@$);
2079 $$ = NEW_RIPPER(fname, get_value($1), $$, &NULL_LOC);
2084defn_head : k_def def_name
2088 $$ = NEW_NODE(NODE_DEFN, 0, $$->nd_mid, $$, &@$);
2093defs_head : k_def singleton dot_or_colon
2095 SET_LEX_STATE(EXPR_FNAME);
2096 p->ctxt.in_argdef = 1;
2100 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
2103 $$ = NEW_NODE(NODE_DEFS, $2, $$->nd_mid, $$, &@$);
2105 VALUE ary = rb_ary_new_from_args(3, $2, $3, get_value($$));
2106 add_mark_object(p, ary);
2107 $<node>$->nd_rval = ary;
2120 $$ = NEW_ERROR(&@$);
2125expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
2131command_call : command
2135block_command : block_call
2136 | block_call call_op2 operation2 command_args
2139 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
2141 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
2145cmd_brace_block : tLBRACE_ARG brace_body '}'
2149 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
2150 nd_set_line($$, @1.end_pos.lineno);
2158 $$ = NEW_FCALL($1, 0, &@$);
2159 nd_set_line($$, p->tokline);
2165command : fcall command_args %prec tLOWEST
2169 nd_set_last_loc($1, @2.end_pos);
2172 /*% ripper: command!($1, $2) %*/
2174 | fcall command_args cmd_brace_block
2177 block_dup_check(p, $2, $3);
2179 $$ = method_add_block(p, $1, $3, &@$);
2181 nd_set_last_loc($1, @2.end_pos);
2183 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
2185 | primary_value call_op operation2 command_args %prec tLOWEST
2188 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
2190 /*% ripper: command_call!($1, $2, $3, $4) %*/
2192 | primary_value call_op operation2 command_args cmd_brace_block
2195 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
2197 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2199 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
2202 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
2204 /*% ripper: command_call!($1, $2, $3, $4) %*/
2206 | primary_value tCOLON2 operation2 command_args cmd_brace_block
2209 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
2211 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
2213 | keyword_super command_args
2216 $$ = NEW_SUPER($2, &@$);
2219 /*% ripper: super!($2) %*/
2221 | keyword_yield command_args
2224 $$ = new_yield(p, $2, &@$);
2227 /*% ripper: yield!($2) %*/
2229 | k_return call_args
2232 $$ = NEW_RETURN(ret_args(p, $2), &@$);
2234 /*% ripper: return!($2) %*/
2236 | keyword_break call_args
2239 $$ = NEW_BREAK(ret_args(p, $2), &@$);
2241 /*% ripper: break!($2) %*/
2243 | keyword_next call_args
2246 $$ = NEW_NEXT(ret_args(p, $2), &@$);
2248 /*% ripper: next!($2) %*/
2253 | tLPAREN mlhs_inner rparen
2258 /*% ripper: mlhs_paren!($2) %*/
2262mlhs_inner : mlhs_basic
2263 | tLPAREN mlhs_inner rparen
2266 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
2268 /*% ripper: mlhs_paren!($2) %*/
2272mlhs_basic : mlhs_head
2275 $$ = NEW_MASGN($1, 0, &@$);
2279 | mlhs_head mlhs_item
2282 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
2284 /*% ripper: mlhs_add!($1, $2) %*/
2286 | mlhs_head tSTAR mlhs_node
2289 $$ = NEW_MASGN($1, $3, &@$);
2291 /*% ripper: mlhs_add_star!($1, $3) %*/
2293 | mlhs_head tSTAR mlhs_node ',' mlhs_post
2296 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
2298 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
2303 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
2305 /*% ripper: mlhs_add_star!($1, Qnil) %*/
2307 | mlhs_head tSTAR ',' mlhs_post
2310 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
2312 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
2317 $$ = NEW_MASGN(0, $2, &@$);
2319 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
2321 | tSTAR mlhs_node ',' mlhs_post
2324 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
2326 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
2331 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
2333 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
2335 | tSTAR ',' mlhs_post
2338 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
2340 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
2344mlhs_item : mlhs_node
2345 | tLPAREN mlhs_inner rparen
2350 /*% ripper: mlhs_paren!($2) %*/
2354mlhs_head : mlhs_item ','
2357 $$ = NEW_LIST($1, &@1);
2359 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2361 | mlhs_head mlhs_item ','
2364 $$ = list_append(p, $1, $2);
2366 /*% ripper: mlhs_add!($1, $2) %*/
2370mlhs_post : mlhs_item
2373 $$ = NEW_LIST($1, &@$);
2375 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
2377 | mlhs_post ',' mlhs_item
2380 $$ = list_append(p, $1, $3);
2382 /*% ripper: mlhs_add!($1, $3) %*/
2386mlhs_node : user_variable
2389 $$ = assignable(p, $1, 0, &@$);
2391 /*% ripper: assignable(p, var_field(p, $1)) %*/
2396 $$ = assignable(p, $1, 0, &@$);
2398 /*% ripper: assignable(p, var_field(p, $1)) %*/
2400 | primary_value '[' opt_call_args rbracket
2403 $$ = aryset(p, $1, $3, &@$);
2405 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2407 | primary_value call_op tIDENTIFIER
2409 if ($2 == tANDDOT) {
2410 yyerror1(&@2, "&. inside multiple assignment destination");
2413 $$ = attrset(p, $1, $2, $3, &@$);
2415 /*% ripper: field!($1, $2, $3) %*/
2417 | primary_value tCOLON2 tIDENTIFIER
2420 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2422 /*% ripper: const_path_field!($1, $3) %*/
2424 | primary_value call_op tCONSTANT
2426 if ($2 == tANDDOT) {
2427 yyerror1(&@2, "&. inside multiple assignment destination");
2430 $$ = attrset(p, $1, $2, $3, &@$);
2432 /*% ripper: field!($1, $2, $3) %*/
2434 | primary_value tCOLON2 tCONSTANT
2437 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2439 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2444 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2446 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2451 rb_backref_error(p, $1);
2452 $$ = NEW_BEGIN(0, &@$);
2454 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2461 $$ = assignable(p, $1, 0, &@$);
2463 /*% ripper: assignable(p, var_field(p, $1)) %*/
2468 $$ = assignable(p, $1, 0, &@$);
2470 /*% ripper: assignable(p, var_field(p, $1)) %*/
2472 | primary_value '[' opt_call_args rbracket
2475 $$ = aryset(p, $1, $3, &@$);
2477 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
2479 | primary_value call_op tIDENTIFIER
2482 $$ = attrset(p, $1, $2, $3, &@$);
2484 /*% ripper: field!($1, $2, $3) %*/
2486 | primary_value tCOLON2 tIDENTIFIER
2489 $$ = attrset(p, $1, idCOLON2, $3, &@$);
2491 /*% ripper: field!($1, $2, $3) %*/
2493 | primary_value call_op tCONSTANT
2496 $$ = attrset(p, $1, $2, $3, &@$);
2498 /*% ripper: field!($1, $2, $3) %*/
2500 | primary_value tCOLON2 tCONSTANT
2503 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
2505 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
2510 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
2512 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2517 rb_backref_error(p, $1);
2518 $$ = NEW_BEGIN(0, &@$);
2520 /*% ripper[error]: backref_error(p, RNODE($1), var_field(p, $1)) %*/
2526 static const char mesg[] = "class/module name must be CONSTANT";
2528 yyerror1(&@1, mesg);
2530 /*% ripper[error]: class_name_error!(ERR_MESG(), $1) %*/
2535cpath : tCOLON3 cname
2538 $$ = NEW_COLON3($2, &@$);
2540 /*% ripper: top_const_ref!($2) %*/
2545 $$ = NEW_COLON2(0, $$, &@$);
2547 /*% ripper: const_ref!($1) %*/
2549 | primary_value tCOLON2 cname
2552 $$ = NEW_COLON2($1, $3, &@$);
2554 /*% ripper: const_path_ref!($1, $3) %*/
2563 SET_LEX_STATE(EXPR_ENDFN);
2572 $$ = NEW_LIT(ID2SYM($1), &@$);
2574 /*% ripper: symbol_literal!($1) %*/
2582 $$ = NEW_UNDEF($1, &@$);
2584 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2586 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2589 NODE *undef = NEW_UNDEF($4, &@4);
2590 $$ = block_append(p, $1, undef);
2592 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2596op : '|' { ifndef_ripper($$ = '|'); }
2597 | '^' { ifndef_ripper($$ = '^'); }
2598 | '&' { ifndef_ripper($$ = '&'); }
2599 | tCMP { ifndef_ripper($$ = tCMP); }
2600 | tEQ { ifndef_ripper($$ = tEQ); }
2601 | tEQQ { ifndef_ripper($$ = tEQQ); }
2602 | tMATCH { ifndef_ripper($$ = tMATCH); }
2603 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2604 | '>' { ifndef_ripper($$ = '>'); }
2605 | tGEQ { ifndef_ripper($$ = tGEQ); }
2606 | '<' { ifndef_ripper($$ = '<'); }
2607 | tLEQ { ifndef_ripper($$ = tLEQ); }
2608 | tNEQ { ifndef_ripper($$ = tNEQ); }
2609 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2610 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2611 | '+' { ifndef_ripper($$ = '+'); }
2612 | '-' { ifndef_ripper($$ = '-'); }
2613 | '*' { ifndef_ripper($$ = '*'); }
2614 | tSTAR { ifndef_ripper($$ = '*'); }
2615 | '/' { ifndef_ripper($$ = '/'); }
2616 | '%' { ifndef_ripper($$ = '%'); }
2617 | tPOW { ifndef_ripper($$ = tPOW); }
2618 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2619 | '!' { ifndef_ripper($$ = '!'); }
2620 | '~' { ifndef_ripper($$ = '~'); }
2621 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2622 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2623 | tAREF { ifndef_ripper($$ = tAREF); }
2624 | tASET { ifndef_ripper($$ = tASET); }
2625 | '`' { ifndef_ripper($$ = '`'); }
2628reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2629 | keyword_BEGIN | keyword_END
2630 | keyword_alias | keyword_and | keyword_begin
2631 | keyword_break | keyword_case | keyword_class | keyword_def
2632 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2633 | keyword_end | keyword_ensure | keyword_false
2634 | keyword_for | keyword_in | keyword_module | keyword_next
2635 | keyword_nil | keyword_not | keyword_or | keyword_redo
2636 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2637 | keyword_super | keyword_then | keyword_true | keyword_undef
2638 | keyword_when | keyword_yield | keyword_if | keyword_unless
2639 | keyword_while | keyword_until
2642arg : lhs '=' lex_ctxt arg_rhs
2645 $$ = node_assign(p, $1, $4, $3, &@$);
2647 /*% ripper: assign!($1, $4) %*/
2649 | var_lhs tOP_ASGN lex_ctxt arg_rhs
2652 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
2654 /*% ripper: opassign!($1, $2, $4) %*/
2656 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
2659 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$);
2661 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/
2663 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2666 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2668 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2670 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2673 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$);
2675 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2677 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
2680 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$);
2682 /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/
2684 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2687 YYLTYPE loc = code_loc_gen(&@1, &@3);
2688 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
2690 /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/
2692 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
2695 YYLTYPE loc = code_loc_gen(&@1, &@2);
2696 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
2698 /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/
2700 | backref tOP_ASGN lex_ctxt arg_rhs
2703 rb_backref_error(p, $1);
2704 $$ = NEW_BEGIN(0, &@$);
2706 /*% ripper[error]: backref_error(p, RNODE($1), opassign!(var_field(p, $1), $2, $4)) %*/
2713 $$ = NEW_DOT2($1, $3, &@$);
2715 /*% ripper: dot2!($1, $3) %*/
2722 $$ = NEW_DOT3($1, $3, &@$);
2724 /*% ripper: dot3!($1, $3) %*/
2730 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
2732 /*% ripper: dot2!($1, Qnil) %*/
2738 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
2740 /*% ripper: dot3!($1, Qnil) %*/
2746 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
2748 /*% ripper: dot2!(Qnil, $2) %*/
2754 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
2756 /*% ripper: dot3!(Qnil, $2) %*/
2760 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2764 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2768 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2772 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2776 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2780 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2782 | tUMINUS_NUM simple_numeric tPOW arg
2784 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2788 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2792 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2796 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2800 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2804 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2808 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2810 | rel_expr %prec tCMP
2813 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2817 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2821 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2825 $$ = match_op(p, $1, $3, &@2, &@$);
2829 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2833 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2837 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2841 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2845 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2849 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2853 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2855 | keyword_defined opt_nl {p->ctxt.in_defined = 1;} arg
2857 p->ctxt.in_defined = 0;
2858 $$ = new_defined(p, $4, &@$);
2860 | arg '?' arg opt_nl ':' arg
2864 $$ = new_if(p, $1, $3, $6, &@$);
2867 /*% ripper: ifop!($1, $3, $6) %*/
2869 | defn_head f_opt_paren_args '=' arg
2871 endless_method_name(p, $<node>1, &@1);
2872 restore_defun(p, $<node>1->nd_defn);
2874 $$ = set_defun_body(p, $1, $2, $4, &@$);
2876 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2877 /*% ripper: def!(get_value($1), $2, $4) %*/
2880 | defn_head f_opt_paren_args '=' arg modifier_rescue arg
2882 endless_method_name(p, $<node>1, &@1);
2883 restore_defun(p, $<node>1->nd_defn);
2885 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2886 $$ = set_defun_body(p, $1, $2, $4, &@$);
2888 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2889 /*% ripper: def!(get_value($1), $2, $4) %*/
2892 | defs_head f_opt_paren_args '=' arg
2894 endless_method_name(p, $<node>1, &@1);
2895 restore_defun(p, $<node>1->nd_defn);
2897 $$ = set_defun_body(p, $1, $2, $4, &@$);
2901 /*% ripper[$4]: bodystmt!($4, Qnil, Qnil, Qnil) %*/
2902 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2905 | defs_head f_opt_paren_args '=' arg modifier_rescue arg
2907 endless_method_name(p, $<node>1, &@1);
2908 restore_defun(p, $<node>1->nd_defn);
2910 $4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
2911 $$ = set_defun_body(p, $1, $2, $4, &@$);
2915 /*% ripper[$4]: bodystmt!(rescue_mod!($4, $6), Qnil, Qnil, Qnil) %*/
2916 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
2925relop : '>' {$$ = '>';}
2931rel_expr : arg relop arg %prec '>'
2933 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2935 | rel_expr relop arg %prec '>'
2937 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2938 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2960 | args ',' assocs trailer
2963 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2965 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2970 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2972 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2976arg_rhs : arg %prec tOP_ASGN
2981 | arg modifier_rescue arg
2985 $$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
2987 /*% ripper: rescue_mod!($1, $3) %*/
2991paren_args : '(' opt_call_args rparen
2996 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2998 | '(' args ',' args_forward rparen
3000 if (!check_forwarding_args(p)) {
3005 $$ = new_args_forward_call(p, $2, &@4, &@$);
3007 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
3010 | '(' args_forward rparen
3012 if (!check_forwarding_args(p)) {
3017 $$ = new_args_forward_call(p, 0, &@2, &@$);
3019 /*% ripper: arg_paren!($2) %*/
3024opt_paren_args : none
3034 | args ',' assocs ','
3037 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3039 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
3044 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3046 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
3054 $$ = NEW_LIST($1, &@$);
3056 /*% ripper: args_add!(args_new!, $1) %*/
3058 | args opt_block_arg
3061 $$ = arg_blk_pass($1, $2);
3063 /*% ripper: args_add_block!($1, $2) %*/
3065 | assocs opt_block_arg
3068 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
3069 $$ = arg_blk_pass($$, $2);
3071 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
3073 | args ',' assocs opt_block_arg
3076 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
3077 $$ = arg_blk_pass($$, $4);
3079 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
3082 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
3086 /* If call_args starts with a open paren '(' or '[',
3087 * look-ahead reading of the letters calls CMDARG_PUSH(0),
3088 * but the push must be done after CMDARG_PUSH(1).
3089 * So this code makes them consistent by first cancelling
3090 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
3091 * and finally redoing CMDARG_PUSH(0).
3095 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
3098 if (lookahead) CMDARG_POP();
3100 if (lookahead) CMDARG_PUSH(0);
3104 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
3105 * but the push must be done after CMDARG_POP() in the parser.
3106 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
3107 * CMDARG_POP() to pop 1 pushed by command_args,
3108 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
3115 if (lookahead) CMDARG_POP();
3117 if (lookahead) CMDARG_PUSH(0);
3122block_arg : tAMPER arg_value
3125 $$ = NEW_BLOCK_PASS($2, &@$);
3131 if (!local_id(p, idFWD_BLOCK)) {
3132 compile_error(p, "no anonymous block parameter");
3135 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$);
3137 /*% ripper: Qnil %*/
3141opt_block_arg : ',' block_arg
3155 $$ = NEW_LIST($1, &@$);
3157 /*% ripper: args_add!(args_new!, $1) %*/
3162 $$ = NEW_SPLAT($2, &@$);
3164 /*% ripper: args_add_star!(args_new!, $2) %*/
3168 if (!local_id(p, idFWD_REST) ||
3169 local_id(p, idFWD_ALL)) {
3170 compile_error(p, "no anonymous rest parameter");
3173 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@1), &@$);
3175 /*% ripper: args_add_star!(args_new!, Qnil) %*/
3177 | args ',' arg_value
3180 $$ = last_arg_append(p, $1, $3, &@$);
3182 /*% ripper: args_add!($1, $3) %*/
3184 | args ',' tSTAR arg_value
3187 $$ = rest_arg_append(p, $1, $4, &@$);
3189 /*% ripper: args_add_star!($1, $4) %*/
3193 if (!local_id(p, idFWD_REST) ||
3194 local_id(p, idFWD_ALL)) {
3195 compile_error(p, "no anonymous rest parameter");
3198 $$ = rest_arg_append(p, $1, NEW_LVAR(idFWD_REST, &@3), &@$);
3200 /*% ripper: args_add_star!($1, Qnil) %*/
3210mrhs : args ',' arg_value
3213 $$ = last_arg_append(p, $1, $3, &@$);
3215 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
3217 | args ',' tSTAR arg_value
3220 $$ = rest_arg_append(p, $1, $4, &@$);
3222 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
3227 $$ = NEW_SPLAT($2, &@$);
3229 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
3246 $$ = NEW_FCALL($1, 0, &@$);
3248 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
3259 set_line_body($3, @1.end_pos.lineno);
3260 $$ = NEW_BEGIN($3, &@$);
3261 nd_set_line($$, @1.end_pos.lineno);
3263 /*% ripper: begin!($3) %*/
3265 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
3268 $$ = NEW_BEGIN(0, &@$);
3270 /*% ripper: paren!(0) %*/
3272 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
3275 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3278 /*% ripper: paren!($2) %*/
3280 | tLPAREN compstmt ')'
3283 if (nd_type_p($2, NODE_SELF)) $2->nd_state = 0;
3286 /*% ripper: paren!($2) %*/
3288 | primary_value tCOLON2 tCONSTANT
3291 $$ = NEW_COLON2($1, $3, &@$);
3293 /*% ripper: const_path_ref!($1, $3) %*/
3298 $$ = NEW_COLON3($2, &@$);
3300 /*% ripper: top_const_ref!($2) %*/
3302 | tLBRACK aref_args ']'
3305 $$ = make_list($2, &@$);
3307 /*% ripper: array!(escape_Qundef($2)) %*/
3309 | tLBRACE assoc_list '}'
3312 $$ = new_hash(p, $2, &@$);
3313 $$->nd_brace = TRUE;
3315 /*% ripper: hash!(escape_Qundef($2)) %*/
3320 $$ = NEW_RETURN(0, &@$);
3322 /*% ripper: return0! %*/
3324 | keyword_yield '(' call_args rparen
3327 $$ = new_yield(p, $3, &@$);
3329 /*% ripper: yield!(paren!($3)) %*/
3331 | keyword_yield '(' rparen
3334 $$ = NEW_YIELD(0, &@$);
3336 /*% ripper: yield!(paren!(args_new!)) %*/
3341 $$ = NEW_YIELD(0, &@$);
3343 /*% ripper: yield0! %*/
3345 | keyword_defined opt_nl '(' {p->ctxt.in_defined = 1;} expr rparen
3347 p->ctxt.in_defined = 0;
3348 $$ = new_defined(p, $5, &@$);
3350 | keyword_not '(' expr rparen
3352 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3354 | keyword_not '(' rparen
3356 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
3361 $$ = method_add_block(p, $1, $2, &@$);
3363 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
3366 | method_call brace_block
3369 block_dup_check(p, $1->nd_args, $2);
3370 $$ = method_add_block(p, $1, $2, &@$);
3372 /*% ripper: method_add_block!($1, $2) %*/
3375 | k_if expr_value then
3381 $$ = new_if(p, $2, $4, $5, &@$);
3384 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
3386 | k_unless expr_value then
3392 $$ = new_unless(p, $2, $4, $5, &@$);
3395 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
3397 | k_while expr_value_do
3402 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
3405 /*% ripper: while!($2, $3) %*/
3407 | k_until expr_value_do
3412 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
3415 /*% ripper: until!($2, $3) %*/
3417 | k_case expr_value opt_terms
3419 $<val>$ = p->case_labels;
3420 p->case_labels = Qnil;
3425 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3426 p->case_labels = $<val>4;
3428 $$ = NEW_CASE($2, $5, &@$);
3431 /*% ripper: case!($2, $5) %*/
3435 $<val>$ = p->case_labels;
3441 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
3442 p->case_labels = $<val>3;
3444 $$ = NEW_CASE2($4, &@$);
3446 /*% ripper: case!(Qnil, $4) %*/
3448 | k_case expr_value opt_terms
3453 $$ = NEW_CASE3($2, $4, &@$);
3455 /*% ripper: case!($2, $4) %*/
3457 | k_for for_var keyword_in expr_value_do
3465 * e.each{|*x| a, b, c = x}
3469 * e.each{|x| a, = x}
3471 ID id = internal_id(p);
3472 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
3473 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
3474 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
3475 tbl->ids[0] = id; /* internal id */
3477 switch (nd_type($2)) {
3479 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
3480 $2->nd_value = internal_var;
3485 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
3486 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
3488 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
3489 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
3491 /* {|*internal_id| <m> = internal_id; ... } */
3492 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
3493 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
3494 $$ = NEW_FOR($4, scope, &@$);
3497 /*% ripper: for!($2, $4, $5) %*/
3499 | k_class cpath superclass
3501 if (p->ctxt.in_def) {
3502 YYLTYPE loc = code_loc_gen(&@1, &@2);
3503 yyerror1(&loc, "class definition in method body");
3505 p->ctxt.in_class = 1;
3512 $$ = NEW_CLASS($2, $5, $3, &@$);
3513 nd_set_line($$->nd_body, @6.end_pos.lineno);
3514 set_line_body($5, @3.end_pos.lineno);
3515 nd_set_line($$, @3.end_pos.lineno);
3517 /*% ripper: class!($2, $3, $5) %*/
3519 p->ctxt.in_class = $<ctxt>1.in_class;
3520 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3522 | k_class tLSHFT expr
3525 p->ctxt.in_class = 0;
3533 $$ = NEW_SCLASS($3, $6, &@$);
3534 nd_set_line($$->nd_body, @7.end_pos.lineno);
3535 set_line_body($6, nd_line($3));
3538 /*% ripper: sclass!($3, $6) %*/
3540 p->ctxt.in_def = $<ctxt>1.in_def;
3541 p->ctxt.in_class = $<ctxt>1.in_class;
3542 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3546 if (p->ctxt.in_def) {
3547 YYLTYPE loc = code_loc_gen(&@1, &@2);
3548 yyerror1(&loc, "module definition in method body");
3550 p->ctxt.in_class = 1;
3557 $$ = NEW_MODULE($2, $4, &@$);
3558 nd_set_line($$->nd_body, @5.end_pos.lineno);
3559 set_line_body($4, @2.end_pos.lineno);
3560 nd_set_line($$, @2.end_pos.lineno);
3562 /*% ripper: module!($2, $4) %*/
3564 p->ctxt.in_class = $<ctxt>1.in_class;
3565 p->ctxt.shareable_constant_value = $<ctxt>1.shareable_constant_value;
3571 push_end_expect_token_locations(p, &@1.beg_pos);
3577 restore_defun(p, $<node>1->nd_defn);
3579 $$ = set_defun_body(p, $1, $2, $4, &@$);
3581 /*% ripper: def!(get_value($1), $2, $4) %*/
3588 push_end_expect_token_locations(p, &@1.beg_pos);
3594 restore_defun(p, $<node>1->nd_defn);
3596 $$ = set_defun_body(p, $1, $2, $4, &@$);
3600 /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
3606 $$ = NEW_BREAK(0, &@$);
3608 /*% ripper: break!(args_new!) %*/
3613 $$ = NEW_NEXT(0, &@$);
3615 /*% ripper: next!(args_new!) %*/
3622 /*% ripper: redo! %*/
3627 $$ = NEW_RETRY(&@$);
3629 /*% ripper: retry! %*/
3633primary_value : primary
3640k_begin : keyword_begin
3642 token_info_push(p, "begin", &@$);
3644 push_end_expect_token_locations(p, &@1.beg_pos);
3652 token_info_push(p, "if", &@$);
3653 if (p->token_info && p->token_info->nonspc &&
3654 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3655 const char *tok = p->lex.ptok - rb_strlen_lit("if");
3656 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3657 beg += rb_strlen_lit("else");
3658 while (beg < tok && ISSPACE(*beg)) beg++;
3660 p->token_info->nonspc = 0;
3664 push_end_expect_token_locations(p, &@1.beg_pos);
3669k_unless : keyword_unless
3671 token_info_push(p, "unless", &@$);
3673 push_end_expect_token_locations(p, &@1.beg_pos);
3678k_while : keyword_while
3680 token_info_push(p, "while", &@$);
3682 push_end_expect_token_locations(p, &@1.beg_pos);
3687k_until : keyword_until
3689 token_info_push(p, "until", &@$);
3691 push_end_expect_token_locations(p, &@1.beg_pos);
3696k_case : keyword_case
3698 token_info_push(p, "case", &@$);
3700 push_end_expect_token_locations(p, &@1.beg_pos);
3707 token_info_push(p, "for", &@$);
3709 push_end_expect_token_locations(p, &@1.beg_pos);
3714k_class : keyword_class
3716 token_info_push(p, "class", &@$);
3719 push_end_expect_token_locations(p, &@1.beg_pos);
3724k_module : keyword_module
3726 token_info_push(p, "module", &@$);
3729 push_end_expect_token_locations(p, &@1.beg_pos);
3736 token_info_push(p, "def", &@$);
3737 p->ctxt.in_argdef = 1;
3743 token_info_push(p, "do", &@$);
3745 push_end_expect_token_locations(p, &@1.beg_pos);
3751k_do_block : keyword_do_block
3753 token_info_push(p, "do", &@$);
3755 push_end_expect_token_locations(p, &@1.beg_pos);
3760k_rescue : keyword_rescue
3762 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3766k_ensure : keyword_ensure
3768 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3772k_when : keyword_when
3774 token_info_warn(p, "when", p->token_info, 0, &@$);
3778k_else : keyword_else
3780 token_info *ptinfo_beg = p->token_info;
3781 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3782 token_info_warn(p, "else", p->token_info, same, &@$);
3785 e.next = ptinfo_beg->next;
3787 token_info_setup(&e, p->lex.pbeg, &@$);
3788 if (!e.nonspc) *ptinfo_beg = e;
3793k_elsif : keyword_elsif
3796 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3802 token_info_pop(p, "end", &@$);
3804 pop_end_expect_token_locations(p);
3809 compile_error(p, "syntax error, unexpected end-of-input");
3813k_return : keyword_return
3815 if (p->ctxt.in_class && !p->ctxt.in_def && !dyna_in_block(p))
3816 yyerror1(&@1, "Invalid return in class/module body");
3830 | k_elsif expr_value then
3835 $$ = new_if(p, $2, $4, $5, &@$);
3838 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3848 /*% ripper: else!($2) %*/
3859 $$ = assignable(p, $1, 0, &@$);
3860 mark_lvar_used(p, $$);
3862 /*% ripper: assignable(p, $1) %*/
3864 | tLPAREN f_margs rparen
3869 /*% ripper: mlhs_paren!($2) %*/
3876 $$ = NEW_LIST($1, &@$);
3878 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3880 | f_marg_list ',' f_marg
3883 $$ = list_append(p, $1, $3);
3885 /*% ripper: mlhs_add!($1, $3) %*/
3889f_margs : f_marg_list
3892 $$ = NEW_MASGN($1, 0, &@$);
3896 | f_marg_list ',' f_rest_marg
3899 $$ = NEW_MASGN($1, $3, &@$);
3901 /*% ripper: mlhs_add_star!($1, $3) %*/
3903 | f_marg_list ',' f_rest_marg ',' f_marg_list
3906 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3908 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3913 $$ = NEW_MASGN(0, $1, &@$);
3915 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3917 | f_rest_marg ',' f_marg_list
3920 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3922 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3926f_rest_marg : tSTAR f_norm_arg
3929 $$ = assignable(p, $2, 0, &@$);
3930 mark_lvar_used(p, $$);
3932 /*% ripper: assignable(p, $2) %*/
3937 $$ = NODE_SPECIAL_NO_NAME_REST;
3939 /*% ripper: Qnil %*/
3943f_any_kwrest : f_kwrest
3944 | f_no_kwarg {$$ = ID2VAL(idNil);}
3947f_eq : {p->ctxt.in_argdef = 0;} '=';
3949block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3951 $$ = new_args_tail(p, $1, $3, $4, &@3);
3953 | f_block_kwarg opt_f_block_arg
3955 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3957 | f_any_kwrest opt_f_block_arg
3959 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3963 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3967opt_block_args_tail : ',' block_args_tail
3973 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3979 /* magic number for rest_id in iseq_set_arguments() */
3981 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
3983 /*% ripper: excessed_comma! %*/
3987block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3989 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3991 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3993 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3995 | f_arg ',' f_block_optarg opt_block_args_tail
3997 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3999 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
4001 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4003 | f_arg ',' f_rest_arg opt_block_args_tail
4005 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4007 | f_arg excessed_comma
4009 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@2);
4010 $$ = new_args(p, $1, Qnone, $2, Qnone, $$, &@$);
4012 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
4014 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4016 | f_arg opt_block_args_tail
4018 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4020 | f_block_optarg ',' f_rest_arg opt_block_args_tail
4022 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4024 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
4026 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4028 | f_block_optarg opt_block_args_tail
4030 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4032 | f_block_optarg ',' f_arg opt_block_args_tail
4034 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4036 | f_rest_arg opt_block_args_tail
4038 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4040 | f_rest_arg ',' f_arg opt_block_args_tail
4042 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
4046 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
4050opt_block_param : none
4053 p->command_start = TRUE;
4057block_param_def : '|' opt_bv_decl '|'
4060 p->max_numparam = ORDINAL_PARAM;
4061 p->ctxt.in_argdef = 0;
4065 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
4067 | '|' block_param opt_bv_decl '|'
4070 p->max_numparam = ORDINAL_PARAM;
4071 p->ctxt.in_argdef = 0;
4075 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
4084 | opt_nl ';' bv_decls opt_nl
4094 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
4096 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
4101 new_bv(p, get_id($1));
4102 /*% ripper: get_value($1) %*/
4112 token_info_push(p, "->", &@1);
4113 $<vars>1 = dyna_push(p);
4114 $<num>$ = p->lex.lpar_beg;
4115 p->lex.lpar_beg = p->lex.paren_nest;
4118 $<num>$ = p->max_numparam;
4119 p->max_numparam = 0;
4122 $<node>$ = numparam_push(p);
4130 int max_numparam = p->max_numparam;
4131 p->lex.lpar_beg = $<num>2;
4132 p->max_numparam = $<num>3;
4134 $5 = args_with_numbered(p, $5, max_numparam);
4137 YYLTYPE loc = code_loc_gen(&@5, &@7);
4138 $$ = NEW_LAMBDA($5, $7, &loc);
4139 nd_set_line($$->nd_body, @7.end_pos.lineno);
4140 nd_set_line($$, @5.end_pos.lineno);
4141 nd_set_first_loc($$, @1.beg_pos);
4144 /*% ripper: lambda!($5, $7) %*/
4145 numparam_pop(p, $<node>4);
4146 dyna_pop(p, $<vars>1);
4150f_larglist : '(' f_args opt_bv_decl ')'
4152 p->ctxt.in_argdef = 0;
4155 p->max_numparam = ORDINAL_PARAM;
4157 /*% ripper: paren!($2) %*/
4161 p->ctxt.in_argdef = 0;
4163 if (!args_info_empty_p($1->nd_ainfo))
4164 p->max_numparam = ORDINAL_PARAM;
4170lambda_body : tLAMBEG compstmt '}'
4172 token_info_pop(p, "}", &@3);
4178 push_end_expect_token_locations(p, &@1.beg_pos);
4187do_block : k_do_block do_body k_end
4191 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4192 nd_set_line($$, @1.end_pos.lineno);
4197block_call : command do_block
4200 if (nd_type_p($1, NODE_YIELD)) {
4201 compile_error(p, "block given to yield");
4204 block_dup_check(p, $1->nd_args, $2);
4206 $$ = method_add_block(p, $1, $2, &@$);
4209 /*% ripper: method_add_block!($1, $2) %*/
4211 | block_call call_op2 operation2 opt_paren_args
4214 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4216 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4218 | block_call call_op2 operation2 opt_paren_args brace_block
4221 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4223 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
4225 | block_call call_op2 operation2 command_args do_block
4228 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
4230 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
4234method_call : fcall paren_args
4239 nd_set_last_loc($1, @2.end_pos);
4241 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
4243 | primary_value call_op operation2 opt_paren_args
4246 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
4247 nd_set_line($$, @3.end_pos.lineno);
4249 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
4251 | primary_value tCOLON2 operation2 paren_args
4254 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
4255 nd_set_line($$, @3.end_pos.lineno);
4257 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
4259 | primary_value tCOLON2 operation3
4262 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
4264 /*% ripper: call!($1, $2, $3) %*/
4266 | primary_value call_op paren_args
4269 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
4270 nd_set_line($$, @2.end_pos.lineno);
4272 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4274 | primary_value tCOLON2 paren_args
4277 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
4278 nd_set_line($$, @2.end_pos.lineno);
4280 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
4282 | keyword_super paren_args
4285 $$ = NEW_SUPER($2, &@$);
4287 /*% ripper: super!($2) %*/
4292 $$ = NEW_ZSUPER(&@$);
4294 /*% ripper: zsuper! %*/
4296 | primary_value '[' opt_call_args rbracket
4299 if ($1 && nd_type_p($1, NODE_SELF))
4300 $$ = NEW_FCALL(tAREF, $3, &@$);
4302 $$ = NEW_CALL($1, tAREF, $3, &@$);
4305 /*% ripper: aref!($1, escape_Qundef($3)) %*/
4309brace_block : '{' brace_body '}'
4313 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4314 nd_set_line($$, @1.end_pos.lineno);
4317 | k_do do_body k_end
4321 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
4322 nd_set_line($$, @1.end_pos.lineno);
4327brace_body : {$<vars>$ = dyna_push(p);}
4329 $<num>$ = p->max_numparam;
4330 p->max_numparam = 0;
4333 $<node>$ = numparam_push(p);
4335 opt_block_param compstmt
4337 int max_numparam = p->max_numparam;
4338 p->max_numparam = $<num>2;
4339 $4 = args_with_numbered(p, $4, max_numparam);
4341 $$ = NEW_ITER($4, $5, &@$);
4343 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
4344 numparam_pop(p, $<node>3);
4345 dyna_pop(p, $<vars>1);
4349do_body : {$<vars>$ = dyna_push(p);}
4351 $<num>$ = p->max_numparam;
4352 p->max_numparam = 0;
4355 $<node>$ = numparam_push(p);
4358 opt_block_param bodystmt
4360 int max_numparam = p->max_numparam;
4361 p->max_numparam = $<num>2;
4362 $4 = args_with_numbered(p, $4, max_numparam);
4364 $$ = NEW_ITER($4, $5, &@$);
4366 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
4368 numparam_pop(p, $<node>3);
4369 dyna_pop(p, $<vars>1);
4373case_args : arg_value
4376 check_literal_when(p, $1, &@1);
4377 $$ = NEW_LIST($1, &@$);
4379 /*% ripper: args_add!(args_new!, $1) %*/
4384 $$ = NEW_SPLAT($2, &@$);
4386 /*% ripper: args_add_star!(args_new!, $2) %*/
4388 | case_args ',' arg_value
4391 check_literal_when(p, $3, &@3);
4392 $$ = last_arg_append(p, $1, $3, &@$);
4394 /*% ripper: args_add!($1, $3) %*/
4396 | case_args ',' tSTAR arg_value
4399 $$ = rest_arg_append(p, $1, $4, &@$);
4401 /*% ripper: args_add_star!($1, $4) %*/
4405case_body : k_when case_args then
4410 $$ = NEW_WHEN($2, $4, $5, &@$);
4413 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
4421p_case_body : keyword_in
4423 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
4424 p->command_start = FALSE;
4426 p->ctxt.in_kwarg = 1;
4427 $<tbl>$ = push_pvtbl(p);
4430 $<tbl>$ = push_pktbl(p);
4434 pop_pktbl(p, $<tbl>3);
4435 pop_pvtbl(p, $<tbl>2);
4436 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4442 $$ = NEW_IN($4, $7, $8, &@$);
4444 /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
4452p_top_expr : p_top_expr_body
4453 | p_top_expr_body modifier_if expr_value
4456 $$ = new_if(p, $3, $1, 0, &@$);
4459 /*% ripper: if_mod!($3, $1) %*/
4461 | p_top_expr_body modifier_unless expr_value
4464 $$ = new_unless(p, $3, $1, 0, &@$);
4467 /*% ripper: unless_mod!($3, $1) %*/
4471p_top_expr_body : p_expr
4474 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4475 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
4479 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
4481 nd_set_first_loc($$, @1.beg_pos);
4487 $$ = new_find_pattern(p, Qnone, $1, &@$);
4491 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
4495 $$ = new_hash_pattern(p, Qnone, $1, &@$);
4502p_as : p_expr tASSOC p_variable
4505 NODE *n = NEW_LIST($1, &@$);
4506 n = list_append(p, n, $3);
4507 $$ = new_hash(p, n, &@$);
4509 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
4514p_alt : p_alt '|' p_expr_basic
4517 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
4519 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
4524p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
4525p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
4527p_expr_basic : p_value
4529 | p_const p_lparen p_args rparen
4531 pop_pktbl(p, $<tbl>2);
4532 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4534 nd_set_first_loc($$, @1.beg_pos);
4538 | p_const p_lparen p_find rparen
4540 pop_pktbl(p, $<tbl>2);
4541 $$ = new_find_pattern(p, $1, $3, &@$);
4543 nd_set_first_loc($$, @1.beg_pos);
4547 | p_const p_lparen p_kwargs rparen
4549 pop_pktbl(p, $<tbl>2);
4550 $$ = new_hash_pattern(p, $1, $3, &@$);
4552 nd_set_first_loc($$, @1.beg_pos);
4556 | p_const '(' rparen
4558 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4559 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4561 | p_const p_lbracket p_args rbracket
4563 pop_pktbl(p, $<tbl>2);
4564 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
4566 nd_set_first_loc($$, @1.beg_pos);
4570 | p_const p_lbracket p_find rbracket
4572 pop_pktbl(p, $<tbl>2);
4573 $$ = new_find_pattern(p, $1, $3, &@$);
4575 nd_set_first_loc($$, @1.beg_pos);
4579 | p_const p_lbracket p_kwargs rbracket
4581 pop_pktbl(p, $<tbl>2);
4582 $$ = new_hash_pattern(p, $1, $3, &@$);
4584 nd_set_first_loc($$, @1.beg_pos);
4588 | p_const '[' rbracket
4590 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4591 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
4593 | tLBRACK p_args rbracket
4595 $$ = new_array_pattern(p, Qnone, Qnone, $2, &@$);
4597 | tLBRACK p_find rbracket
4599 $$ = new_find_pattern(p, Qnone, $2, &@$);
4603 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
4604 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
4608 $<tbl>$ = push_pktbl(p);
4610 p->ctxt.in_kwarg = 0;
4614 pop_pktbl(p, $<tbl>2);
4615 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
4616 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4620 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4621 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4623 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4625 pop_pktbl(p, $<tbl>2);
4633 NODE *pre_args = NEW_LIST($1, &@$);
4634 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4636 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4641 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4646 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4648 VALUE pre_args = rb_ary_concat($1, get_value($2));
4649 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4652 | p_args_head p_rest
4654 $$ = new_array_pattern_tail(p, $1, 1, $2, Qnone, &@$);
4656 | p_args_head p_rest ',' p_args_post
4658 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
4663p_args_head : p_arg ','
4667 | p_args_head p_arg ','
4670 $$ = list_concat($1, $2);
4672 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4678 $$ = new_array_pattern_tail(p, Qnone, 1, $1, Qnone, &@$);
4680 | p_rest ',' p_args_post
4682 $$ = new_array_pattern_tail(p, Qnone, 1, $1, $3, &@$);
4686p_find : p_rest ',' p_args_post ',' p_rest
4688 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
4693p_rest : tSTAR tIDENTIFIER
4704 | p_args_post ',' p_arg
4707 $$ = list_concat($1, $3);
4709 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4716 $$ = NEW_LIST($1, &@$);
4718 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4722p_kwargs : p_kwarg ',' p_any_kwrest
4724 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4728 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4732 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4736 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4741 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4745 $$ = list_concat($1, $3);
4747 /*% ripper: rb_ary_push($1, $3) %*/
4751p_kw : p_kw_label p_expr
4753 error_duplicate_pattern_key(p, get_id($1), &@1);
4755 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
4757 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4761 error_duplicate_pattern_key(p, get_id($1), &@1);
4762 if ($1 && !is_local_id(get_id($1))) {
4763 yyerror1(&@1, "key must be valid as local variables");
4765 error_duplicate_pattern_variable(p, get_id($1), &@1);
4767 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4769 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4774 | tSTRING_BEG string_contents tLABEL_END
4776 YYLTYPE loc = code_loc_gen(&@1, &@3);
4778 if (!$2 || nd_type_p($2, NODE_STR)) {
4779 NODE *node = dsym_node(p, $2, &loc);
4780 $$ = SYM2ID(node->nd_lit);
4783 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4784 VALUE label = RNODE($2)->nd_cval;
4785 VALUE rval = RNODE($2)->nd_rval;
4786 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4787 RNODE($$)->nd_loc = loc;
4791 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4797p_kwrest : kwrest_mark tIDENTIFIER
4807p_kwnorest : kwrest_mark keyword_nil
4813p_any_kwrest : p_kwrest
4814 | p_kwnorest {$$ = ID2VAL(idNil);}
4817p_value : p_primitive
4818 | p_primitive tDOT2 p_primitive
4823 $$ = NEW_DOT2($1, $3, &@$);
4825 /*% ripper: dot2!($1, $3) %*/
4827 | p_primitive tDOT3 p_primitive
4832 $$ = NEW_DOT3($1, $3, &@$);
4834 /*% ripper: dot3!($1, $3) %*/
4840 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
4842 /*% ripper: dot2!($1, Qnil) %*/
4848 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
4850 /*% ripper: dot3!($1, Qnil) %*/
4855 | tBDOT2 p_primitive
4859 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
4861 /*% ripper: dot2!(Qnil, $2) %*/
4863 | tBDOT3 p_primitive
4867 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
4869 /*% ripper: dot3!(Qnil, $2) %*/
4873p_primitive : literal
4884 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4886 /*% ripper: var_ref!($1) %*/
4891p_variable : tIDENTIFIER
4894 error_duplicate_pattern_variable(p, $1, &@1);
4895 $$ = assignable(p, $1, 0, &@$);
4897 /*% ripper: assignable(p, var_field(p, $1)) %*/
4901p_var_ref : '^' tIDENTIFIER
4904 NODE *n = gettable(p, $2, &@$);
4905 if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
4906 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4910 /*% ripper: var_ref!($2) %*/
4915 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_BEGIN(0, &@$);
4917 /*% ripper: var_ref!($2) %*/
4921p_expr_ref : '^' tLPAREN expr_value rparen
4924 $$ = NEW_BEGIN($3, &@$);
4926 /*% ripper: begin!($3) %*/
4930p_const : tCOLON3 cname
4933 $$ = NEW_COLON3($2, &@$);
4935 /*% ripper: top_const_ref!($2) %*/
4937 | p_const tCOLON2 cname
4940 $$ = NEW_COLON2($1, $3, &@$);
4942 /*% ripper: const_path_ref!($1, $3) %*/
4947 $$ = gettable(p, $1, &@$);
4949 /*% ripper: var_ref!($1) %*/
4953opt_rescue : k_rescue exc_list exc_var then
4958 $$ = NEW_RESBODY($2,
4959 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5,
4972 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4980 $$ = NEW_LIST($1, &@$);
4982 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4987 if (!($$ = splat_array($1))) $$ = $1;
5001opt_ensure : k_ensure compstmt
5006 /*% ripper: ensure!($2) %*/
5020 node = NEW_STR(STR_NEW0(), &@$);
5021 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
5024 node = evstr2dstr(p, node);
5037 $$ = literal_concat(p, $1, $2, &@$);
5039 /*% ripper: string_concat!($1, $2) %*/
5043string1 : tSTRING_BEG string_contents tSTRING_END
5046 $$ = heredoc_dedent(p, $2);
5047 if ($$) nd_set_loc($$, &@$);
5049 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
5053xstring : tXSTRING_BEG xstring_contents tSTRING_END
5056 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
5058 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
5062regexp : tREGEXP_BEG regexp_contents tREGEXP_END
5064 $$ = new_regexp(p, $2, $3, &@$);
5068words : tWORDS_BEG ' ' word_list tSTRING_END
5071 $$ = make_list($3, &@$);
5073 /*% ripper: array!($3) %*/
5077word_list : /* none */
5082 /*% ripper: words_new! %*/
5084 | word_list word ' '
5087 $$ = list_append(p, $1, evstr2dstr(p, $2));
5089 /*% ripper: words_add!($1, $2) %*/
5093word : string_content
5094 /*% ripper[brace]: word_add!(word_new!, $1) %*/
5095 | word string_content
5098 $$ = literal_concat(p, $1, $2, &@$);
5100 /*% ripper: word_add!($1, $2) %*/
5104symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
5107 $$ = make_list($3, &@$);
5109 /*% ripper: array!($3) %*/
5113symbol_list : /* none */
5118 /*% ripper: symbols_new! %*/
5120 | symbol_list word ' '
5123 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
5125 /*% ripper: symbols_add!($1, $2) %*/
5129qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
5132 $$ = make_list($3, &@$);
5134 /*% ripper: array!($3) %*/
5138qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
5141 $$ = make_list($3, &@$);
5143 /*% ripper: array!($3) %*/
5147qword_list : /* none */
5152 /*% ripper: qwords_new! %*/
5154 | qword_list tSTRING_CONTENT ' '
5157 $$ = list_append(p, $1, $2);
5159 /*% ripper: qwords_add!($1, $2) %*/
5163qsym_list : /* none */
5168 /*% ripper: qsymbols_new! %*/
5170 | qsym_list tSTRING_CONTENT ' '
5173 $$ = symbol_append(p, $1, $2);
5175 /*% ripper: qsymbols_add!($1, $2) %*/
5179string_contents : /* none */
5184 /*% ripper: string_content! %*/
5187 $$ = ripper_new_yylval(p, 0, $$, 0);
5190 | string_contents string_content
5193 $$ = literal_concat(p, $1, $2, &@$);
5195 /*% ripper: string_add!($1, $2) %*/
5198 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
5199 !RNODE($1)->nd_cval) {
5200 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
5201 RNODE($1)->nd_rval = add_mark_object(p, $$);
5208xstring_contents: /* none */
5213 /*% ripper: xstring_new! %*/
5215 | xstring_contents string_content
5218 $$ = literal_concat(p, $1, $2, &@$);
5220 /*% ripper: xstring_add!($1, $2) %*/
5224regexp_contents: /* none */
5229 /*% ripper: regexp_new! %*/
5232 $$ = ripper_new_yylval(p, 0, $$, 0);
5235 | regexp_contents string_content
5238 NODE *head = $1, *tail = $2;
5246 switch (nd_type(head)) {
5248 nd_set_type(head, NODE_DSTR);
5253 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
5256 $$ = list_append(p, head, tail);
5259 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
5260 if (ripper_is_node_yylval(n1)) {
5261 s1 = RNODE(n1)->nd_cval;
5262 n1 = RNODE(n1)->nd_rval;
5264 if (ripper_is_node_yylval(n2)) {
5265 s2 = RNODE(n2)->nd_cval;
5266 n2 = RNODE(n2)->nd_rval;
5268 $$ = dispatch2(regexp_add, n1, n2);
5270 $$ = ripper_new_yylval(p, 0, $$, s2);
5276string_content : tSTRING_CONTENT
5277 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
5280 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
5281 $<strterm>$ = p->lex.strterm;
5283 SET_LEX_STATE(EXPR_BEG);
5287 p->lex.strterm = $<strterm>2;
5289 $$ = NEW_EVSTR($3, &@$);
5290 nd_set_line($$, @3.end_pos.lineno);
5292 /*% ripper: string_dvar!($3) %*/
5300 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
5301 $<strterm>$ = p->lex.strterm;
5305 $<num>$ = p->lex.state;
5306 SET_LEX_STATE(EXPR_BEG);
5309 $<num>$ = p->lex.brace_nest;
5310 p->lex.brace_nest = 0;
5313 $<num>$ = p->heredoc_indent;
5314 p->heredoc_indent = 0;
5316 compstmt tSTRING_DEND
5320 p->lex.strterm = $<strterm>3;
5321 SET_LEX_STATE($<num>4);
5322 p->lex.brace_nest = $<num>5;
5323 p->heredoc_indent = $<num>6;
5324 p->heredoc_line_indent = -1;
5326 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
5327 $$ = new_evstr(p, $7, &@$);
5329 /*% ripper: string_embexpr!($7) %*/
5336 $$ = NEW_GVAR($1, &@$);
5338 /*% ripper: var_ref!($1) %*/
5343 $$ = NEW_IVAR($1, &@$);
5345 /*% ripper: var_ref!($1) %*/
5350 $$ = NEW_CVAR($1, &@$);
5352 /*% ripper: var_ref!($1) %*/
5363 SET_LEX_STATE(EXPR_END);
5365 $$ = NEW_LIT(ID2SYM($2), &@$);
5367 /*% ripper: symbol_literal!(symbol!($2)) %*/
5375dsym : tSYMBEG string_contents tSTRING_END
5377 SET_LEX_STATE(EXPR_END);
5379 $$ = dsym_node(p, $2, &@$);
5381 /*% ripper: dyna_symbol!($2) %*/
5385numeric : simple_numeric
5386 | tUMINUS_NUM simple_numeric %prec tLOWEST
5390 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
5392 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
5396simple_numeric : tINTEGER
5407user_variable : tIDENTIFIER
5412keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
5413 | keyword_self {$$ = KWD2EID(self, $1);}
5414 | keyword_true {$$ = KWD2EID(true, $1);}
5415 | keyword_false {$$ = KWD2EID(false, $1);}
5416 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
5417 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
5418 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
5421var_ref : user_variable
5424 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5426 if (id_is_var(p, get_id($1))) {
5427 $$ = dispatch1(var_ref, $1);
5430 $$ = dispatch1(vcall, $1);
5437 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
5439 /*% ripper: var_ref!($1) %*/
5443var_lhs : user_variable
5446 $$ = assignable(p, $1, 0, &@$);
5448 /*% ripper: assignable(p, var_field(p, $1)) %*/
5453 $$ = assignable(p, $1, 0, &@$);
5455 /*% ripper: assignable(p, var_field(p, $1)) %*/
5465 SET_LEX_STATE(EXPR_BEG);
5466 p->command_start = TRUE;
5477 /*% ripper: Qnil %*/
5481f_opt_paren_args: f_paren_args
5484 p->ctxt.in_argdef = 0;
5485 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5486 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5490f_paren_args : '(' f_args rparen
5495 /*% ripper: paren!($2) %*/
5496 SET_LEX_STATE(EXPR_BEG);
5497 p->command_start = TRUE;
5498 p->ctxt.in_argdef = 0;
5502f_arglist : f_paren_args
5505 p->ctxt.in_kwarg = 1;
5506 p->ctxt.in_argdef = 1;
5507 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
5511 p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
5512 p->ctxt.in_argdef = 0;
5514 SET_LEX_STATE(EXPR_BEG);
5515 p->command_start = TRUE;
5519args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
5521 $$ = new_args_tail(p, $1, $3, $4, &@3);
5523 | f_kwarg opt_f_block_arg
5525 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
5527 | f_any_kwrest opt_f_block_arg
5529 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
5533 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
5537 add_forwarding_args(p);
5538 $$ = new_args_tail(p, Qnone, $1, ID2VAL(idFWD_BLOCK), &@1);
5540 ($$->nd_ainfo)->forwarding = 1;
5545opt_args_tail : ',' args_tail
5551 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5555f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
5557 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
5559 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5561 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5563 | f_arg ',' f_optarg opt_args_tail
5565 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
5567 | f_arg ',' f_optarg ',' f_arg opt_args_tail
5569 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
5571 | f_arg ',' f_rest_arg opt_args_tail
5573 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
5575 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
5577 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
5579 | f_arg opt_args_tail
5581 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
5583 | f_optarg ',' f_rest_arg opt_args_tail
5585 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
5587 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
5589 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
5591 | f_optarg opt_args_tail
5593 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
5595 | f_optarg ',' f_arg opt_args_tail
5597 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
5599 | f_rest_arg opt_args_tail
5601 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
5603 | f_rest_arg ',' f_arg opt_args_tail
5605 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5609 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5613 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5614 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5618args_forward : tBDOT3
5621#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
5627 /*% ripper: args_forward! %*/
5631f_bad_arg : tCONSTANT
5633 static const char mesg[] = "formal argument cannot be a constant";
5635 yyerror1(&@1, mesg);
5638 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5642 static const char mesg[] = "formal argument cannot be an instance variable";
5644 yyerror1(&@1, mesg);
5647 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5651 static const char mesg[] = "formal argument cannot be a global variable";
5653 yyerror1(&@1, mesg);
5656 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5660 static const char mesg[] = "formal argument cannot be a class variable";
5662 yyerror1(&@1, mesg);
5665 /*% ripper[error]: param_error!(ERR_MESG(), $1) %*/
5669f_norm_arg : f_bad_arg
5672 formal_argument(p, $1);
5673 p->max_numparam = ORDINAL_PARAM;
5678f_arg_asgn : f_norm_arg
5687f_arg_item : f_arg_asgn
5691 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5693 /*% ripper: get_value($1) %*/
5695 | tLPAREN f_margs rparen
5698 ID tid = internal_id(p);
5700 loc.beg_pos = @2.beg_pos;
5701 loc.end_pos = @2.beg_pos;
5703 if (dyna_in_block(p)) {
5704 $2->nd_value = NEW_DVAR(tid, &loc);
5707 $2->nd_value = NEW_LVAR(tid, &loc);
5709 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5712 /*% ripper: mlhs_paren!($2) %*/
5717 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5718 | f_arg ',' f_arg_item
5723 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5724 rb_discard_node(p, $3);
5726 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5733 arg_var(p, formal_argument(p, $1));
5734 p->cur_arg = get_id($1);
5735 p->max_numparam = ORDINAL_PARAM;
5736 p->ctxt.in_argdef = 0;
5741f_kw : f_label arg_value
5744 p->ctxt.in_argdef = 1;
5746 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5748 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5753 p->ctxt.in_argdef = 1;
5755 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5757 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5761f_block_kw : f_label primary_value
5763 p->ctxt.in_argdef = 1;
5765 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5767 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5771 p->ctxt.in_argdef = 1;
5773 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5775 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5779f_block_kwarg : f_block_kw
5784 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5786 | f_block_kwarg ',' f_block_kw
5789 $$ = kwd_append($1, $3);
5791 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5801 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5806 $$ = kwd_append($1, $3);
5808 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5816f_no_kwarg : p_kwnorest
5820 /*% ripper: nokw_param!(Qnil) %*/
5824f_kwrest : kwrest_mark tIDENTIFIER
5826 arg_var(p, shadowing_lvar(p, get_id($2)));
5830 /*% ripper: kwrest_param!($2) %*/
5834 arg_var(p, idFWD_KWREST);
5838 /*% ripper: kwrest_param!(Qnil) %*/
5842f_opt : f_arg_asgn f_eq arg_value
5845 p->ctxt.in_argdef = 1;
5847 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5849 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5853f_block_opt : f_arg_asgn f_eq primary_value
5856 p->ctxt.in_argdef = 1;
5858 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5860 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5864f_block_optarg : f_block_opt
5869 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5871 | f_block_optarg ',' f_block_opt
5874 $$ = opt_arg_append($1, $3);
5876 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5885 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5887 | f_optarg ',' f_opt
5890 $$ = opt_arg_append($1, $3);
5892 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5900f_rest_arg : restarg_mark tIDENTIFIER
5902 arg_var(p, shadowing_lvar(p, get_id($2)));
5906 /*% ripper: rest_param!($2) %*/
5910 arg_var(p, idFWD_REST);
5914 /*% ripper: rest_param!(Qnil) %*/
5922f_block_arg : blkarg_mark tIDENTIFIER
5924 arg_var(p, shadowing_lvar(p, get_id($2)));
5928 /*% ripper: blockarg!($2) %*/
5932 arg_var(p, idFWD_BLOCK);
5936 /*% ripper: blockarg!(Qnil) %*/
5940opt_f_block_arg : ',' f_block_arg
5955 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5958 switch (nd_type($3)) {
5967 yyerror1(&@3, "can't define singleton method for literals");
5975 /*% ripper: paren!($3) %*/
5985 /*% ripper: assoclist_from_args!($1) %*/
5990 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
6000 if (assocs->nd_head &&
6001 !tail->nd_head && nd_type_p(tail->nd_next, NODE_LIST) &&
6002 nd_type_p(tail->nd_next->nd_head, NODE_HASH)) {
6004 tail = tail->nd_next->nd_head->nd_head;
6006 assocs = list_concat(assocs, tail);
6010 /*% ripper: rb_ary_push($1, get_value($3)) %*/
6014assoc : arg_value tASSOC arg_value
6017 if (nd_type_p($1, NODE_STR)) {
6018 nd_set_type($1, NODE_LIT);
6019 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
6021 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6023 /*% ripper: assoc_new!($1, $3) %*/
6028 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
6030 /*% ripper: assoc_new!($1, $2) %*/
6035 NODE *val = gettable(p, $1, &@$);
6036 if (!val) val = NEW_BEGIN(0, &@$);
6037 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), val);
6039 /*% ripper: assoc_new!($1, Qnil) %*/
6041 | tSTRING_BEG string_contents tLABEL_END arg_value
6044 YYLTYPE loc = code_loc_gen(&@1, &@3);
6045 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6047 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
6052 if (nd_type_p($2, NODE_HASH) &&
6053 !($2->nd_head && $2->nd_head->nd_alen)) {
6054 static VALUE empty_hash;
6056 empty_hash = rb_obj_freeze(rb_hash_new());
6057 rb_gc_register_mark_object(empty_hash);
6059 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
6062 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6064 /*% ripper: assoc_splat!($2) %*/
6068 if (!local_id(p, idFWD_KWREST) ||
6069 local_id(p, idFWD_ALL)) {
6070 compile_error(p, "no anonymous keyword rest parameter");
6073 $$ = list_append(p, NEW_LIST(0, &@$),
6074 NEW_LVAR(idFWD_KWREST, &@$));
6076 /*% ripper: assoc_splat!(Qnil) %*/
6080operation : tIDENTIFIER
6085operation2 : operation
6089operation3 : tIDENTIFIER
6106opt_terms : /* none */
6117rbracket : opt_nl ']'
6127term : ';' {yyerrok;token_flush(p);}
6130 @$.end_pos = @$.beg_pos;
6136 | terms ';' {yyerrok;}
6148# define yylval (*p->lval)
6150static int regx_options(struct parser_params*);
6151static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6152static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6153static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6154static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6157# define set_yylval_node(x) { \
6159 rb_parser_set_location(p, &_cur_loc); \
6160 yylval.node = (x); \
6162# define set_yylval_str(x) \
6164 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6165 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6167# define set_yylval_literal(x) \
6169 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
6170 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
6172# define set_yylval_num(x) (yylval.num = (x))
6173# define set_yylval_id(x) (yylval.id = (x))
6174# define set_yylval_name(x) (yylval.id = (x))
6175# define yylval_id() (yylval.id)
6178ripper_yylval_id(struct parser_params *p, ID x)
6180 return ripper_new_yylval(p, x, ID2SYM(x), 0);
6182# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
6183# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
6184# define set_yylval_id(x) (void)(x)
6185# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
6186# define set_yylval_literal(x) add_mark_object(p, (x))
6187# define set_yylval_node(x) (yylval.val = ripper_new_yylval(p, 0, 0, STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)))
6188# define yylval_id() yylval.id
6189# define _cur_loc NULL_LOC /* dummy */
6192#define set_yylval_noname() set_yylval_id(keyword_nil)
6193#define has_delayed_token(p) (!NIL_P(p->delayed.token))
6196#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6197#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6200parser_has_token(struct parser_params *p)
6202 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);
6203 return p->lex.pcur > p->lex.ptok;
6207code_loc_to_ary(const rb_code_location_t *loc)
6209 VALUE ary = rb_ary_new_from_args(4,
6210 INT2NUM(loc->beg_pos.lineno), INT2NUM(loc->beg_pos.column),
6211 INT2NUM(loc->end_pos.lineno), INT2NUM(loc->end_pos.column));
6218parser_append_tokens(struct parser_params *p, VALUE str, enum yytokentype t, int line)
6223 ary = rb_ary_new2(4);
6224 token_id = p->token_id;
6225 rb_ary_push(ary, INT2FIX(token_id));
6226 rb_ary_push(ary, ID2SYM(parser_token2id(t)));
6227 rb_ary_push(ary, str);
6228 rb_ary_push(ary, code_loc_to_ary(p->yylloc));
6230 rb_ary_push(p->tokens, ary);
6234 rb_parser_printf(p, "Append tokens (line: %d) %"PRIsVALUE"\n", line, ary);
6239parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
6241 debug_token_line(p, "parser_dispatch_scan_event", line);
6243 if (!parser_has_token(p)) return;
6245 RUBY_SET_YYLLOC(*p->yylloc);
6247 if (p->keep_tokens) {
6248 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6249 parser_append_tokens(p, str, t, line);
6255#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
6257parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
6259 int saved_line = p->ruby_sourceline;
6260 const char *saved_tokp = p->lex.ptok;
6262 debug_token_line(p, "parser_dispatch_delayed_token", line);
6264 if (!has_delayed_token(p)) return;
6266 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
6268 if (p->keep_tokens) {
6269 p->ruby_sourceline = p->delayed.beg_line;
6270 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6271 parser_append_tokens(p, p->delayed.token, t, line);
6272 p->ruby_sourceline = saved_line;
6273 p->lex.ptok = saved_tokp;
6276 p->delayed.token = Qnil;
6279#define literal_flush(p, ptr) ((void)(ptr))
6281#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
6284intern_sym(const char *name)
6286 ID id = rb_intern_const(name);
6291ripper_has_scan_event(struct parser_params *p)
6293 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
6294 return p->lex.pcur > p->lex.ptok;
6298ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
6300 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
6301 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
6302 RUBY_SET_YYLLOC(*p->yylloc);
6308ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
6310 if (!ripper_has_scan_event(p)) return;
6311 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
6313#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
6316ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
6318 int saved_line = p->ruby_sourceline;
6319 const char *saved_tokp = p->lex.ptok;
6321 if (!has_delayed_token(p)) return;
6322 p->ruby_sourceline = p->delayed.beg_line;
6323 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
6324 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
6325 p->delayed.token = Qnil;
6326 p->ruby_sourceline = saved_line;
6327 p->lex.ptok = saved_tokp;
6329#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
6333is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
6335 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
6339parser_is_identchar(struct parser_params *p)
6341 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
6345parser_isascii(struct parser_params *p)
6347 return ISASCII(*(p->lex.pcur-1));
6351token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
6353 int column = 1, nonspc = 0, i;
6354 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
6356 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
6359 if (*ptr != ' ' && *ptr != '\t') {
6364 ptinfo->beg = loc->beg_pos;
6365 ptinfo->indent = column;
6366 ptinfo->nonspc = nonspc;
6370token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6374 if (!p->token_info_enabled) return;
6375 ptinfo = ALLOC(token_info);
6376 ptinfo->token = token;
6377 ptinfo->next = p->token_info;
6378 token_info_setup(ptinfo, p->lex.pbeg, loc);
6380 p->token_info = ptinfo;
6384token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
6386 token_info *ptinfo_beg = p->token_info;
6388 if (!ptinfo_beg) return;
6389 p->token_info = ptinfo_beg->next;
6391 /* indentation check of matched keywords (begin..end, if..end, etc.) */
6392 token_info_warn(p, token, ptinfo_beg, 1, loc);
6393 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6397token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
6399 token_info *ptinfo_beg = p->token_info;
6401 if (!ptinfo_beg) return;
6402 p->token_info = ptinfo_beg->next;
6404 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
6405 ptinfo_beg->beg.column != beg_pos.column ||
6406 strcmp(ptinfo_beg->token, token)) {
6407 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
6408 beg_pos.lineno, beg_pos.column, token,
6409 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
6413 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
6417token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
6419 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
6420 if (!p->token_info_enabled) return;
6421 if (!ptinfo_beg) return;
6422 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
6423 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
6424 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
6425 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
6426 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
6427 rb_warn3L(ptinfo_end->beg.lineno,
6428 "mismatched indentations at '%s' with '%s' at %d",
6429 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
6433parser_precise_mbclen(struct parser_params *p, const char *ptr)
6435 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
6436 if (!MBCLEN_CHARFOUND_P(len)) {
6437 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
6444static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
6447parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6450 int lineno = p->ruby_sourceline;
6454 else if (yylloc->beg_pos.lineno == lineno) {
6455 str = p->lex.lastline;
6460 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
6464parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6470 yylloc = RUBY_SET_YYLLOC(current);
6472 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
6473 p->ruby_sourceline != yylloc->end_pos.lineno)) {
6477 compile_error(p, "%s", msg);
6478 parser_show_error_line(p, yylloc);
6483parser_yyerror0(struct parser_params *p, const char *msg)
6486 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
6490ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
6493 const int max_line_margin = 30;
6494 const char *ptr, *ptr_end, *pt, *pb;
6495 const char *pre = "", *post = "", *pend;
6496 const char *code = "", *caret = "";
6498 const char *const pbeg = RSTRING_PTR(str);
6503 if (!yylloc) return;
6504 pend = RSTRING_END(str);
6505 if (pend > pbeg && pend[-1] == '\n') {
6506 if (--pend > pbeg && pend[-1] == '\r') --pend;
6510 if (lineno == yylloc->end_pos.lineno &&
6511 (pend - pbeg) > yylloc->end_pos.column) {
6512 pt = pbeg + yylloc->end_pos.column;
6516 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
6517 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
6519 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
6520 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
6522 len = ptr_end - ptr;
6525 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
6526 if (ptr > pbeg) pre = "...";
6528 if (ptr_end < pend) {
6529 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
6530 if (ptr_end < pend) post = "...";
6534 if (lineno == yylloc->beg_pos.lineno) {
6535 pb += yylloc->beg_pos.column;
6536 if (pb > pt) pb = pt;
6538 if (pb < ptr) pb = ptr;
6539 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
6542 if (RTEST(errbuf)) {
6543 mesg = rb_attr_get(errbuf, idMesg);
6544 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
6545 rb_str_cat_cstr(mesg, "\n");
6548 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
6550 if (!errbuf && rb_stderr_tty_p()) {
6551#define CSI_BEGIN "\033["
6554 CSI_BEGIN""CSI_SGR"%s" /* pre */
6555 CSI_BEGIN"1"CSI_SGR"%.*s"
6556 CSI_BEGIN"1;4"CSI_SGR"%.*s"
6557 CSI_BEGIN";1"CSI_SGR"%.*s"
6558 CSI_BEGIN""CSI_SGR"%s" /* post */
6561 (int)(pb - ptr), ptr,
6563 (int)(ptr_end - pt), pt,
6569 len = ptr_end - ptr;
6570 lim = pt < pend ? pt : pend;
6571 i = (int)(lim - ptr);
6572 buf = ALLOCA_N(char, i+2);
6577 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
6583 memset(p2, '~', (lim - ptr));
6587 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
6588 pre, (int)len, code, post,
6591 if (!errbuf) rb_write_error_str(mesg);
6595parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
6597 const char *pcur = 0, *ptok = 0;
6598 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
6599 p->ruby_sourceline == yylloc->end_pos.lineno) {
6602 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
6603 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
6605 parser_yyerror0(p, msg);
6614parser_yyerror0(struct parser_params *p, const char *msg)
6616 dispatch1(parse_error, STR_NEW2(msg));
6622parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
6629vtable_size(const struct vtable *tbl)
6631 if (!DVARS_TERMINAL_P(tbl)) {
6640static struct vtable *
6641vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
6643 struct vtable *tbl = ALLOC(struct vtable);
6646 tbl->tbl = ALLOC_N(ID, tbl->capa);
6650 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
6655#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
6658vtable_free_gen(struct parser_params *p, int line, const char *name,
6663 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
6666 if (!DVARS_TERMINAL_P(tbl)) {
6668 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
6670 ruby_sized_xfree(tbl, sizeof(*tbl));
6673#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
6676vtable_add_gen(struct parser_params *p, int line, const char *name,
6677 struct vtable *tbl, ID id)
6681 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
6682 line, name, (void *)tbl, rb_id2name(id));
6685 if (DVARS_TERMINAL_P(tbl)) {
6686 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
6689 if (tbl->pos == tbl->capa) {
6690 tbl->capa = tbl->capa * 2;
6691 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
6693 tbl->tbl[tbl->pos++] = id;
6695#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
6699vtable_pop_gen(struct parser_params *p, int line, const char *name,
6700 struct vtable *tbl, int n)
6703 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
6704 line, name, (void *)tbl, n);
6707 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
6712#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
6716vtable_included(const struct vtable * tbl, ID id)
6720 if (!DVARS_TERMINAL_P(tbl)) {
6721 for (i = 0; i < tbl->pos; i++) {
6722 if (tbl->tbl[i] == id) {
6730static void parser_prepare(struct parser_params *p);
6733static NODE *parser_append_options(struct parser_params *p, NODE *node);
6736debug_lines(VALUE fname)
6739 CONST_ID(script_lines, "SCRIPT_LINES__");
6740 if (rb_const_defined_at(rb_cObject, script_lines)) {
6741 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
6742 if (RB_TYPE_P(hash, T_HASH)) {
6743 VALUE lines = rb_ary_new();
6744 rb_hash_aset(hash, fname, lines);
6752e_option_supplied(struct parser_params *p)
6754 return strcmp(p->ruby_sourcefile, "-e") == 0;
6758yycompile0(VALUE arg)
6762 struct parser_params *p = (struct parser_params *)arg;
6765 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6766 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6767 if (p->debug_lines && p->ruby_sourceline > 0) {
6768 VALUE str = rb_default_rs;
6769 n = p->ruby_sourceline;
6771 rb_ary_push(p->debug_lines, str);
6775 if (!e_option_supplied(p)) {
6780 if (p->keep_script_lines || ruby_vm_keep_script_lines) {
6781 if (!p->debug_lines) {
6782 p->debug_lines = rb_ary_new();
6785 RB_OBJ_WRITE(p->ast, &p->ast->body.script_lines, p->debug_lines);
6789#define RUBY_DTRACE_PARSE_HOOK(name) \
6790 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6791 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6793 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6795 RUBY_DTRACE_PARSE_HOOK(END);
6799 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6800 if (n || p->error_p) {
6801 VALUE mesg = p->error_buffer;
6803 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6805 if (!p->error_tolerant) {
6806 rb_set_errinfo(mesg);
6810 tree = p->eval_tree;
6812 tree = NEW_NIL(&NULL_LOC);
6815 VALUE opt = p->compile_option;
6816 VALUE tokens = p->tokens;
6818 NODE *body = parser_append_options(p, tree->nd_body);
6819 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6820 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6821 prelude = block_append(p, p->eval_tree_begin, body);
6822 tree->nd_body = prelude;
6823 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6824 if (p->keep_tokens) {
6825 rb_obj_freeze(tokens);
6826 rb_ast_set_tokens(p->ast, tokens);
6829 p->ast->body.root = tree;
6830 if (!p->ast->body.script_lines) p->ast->body.script_lines = INT2FIX(p->line_count);
6835yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6839 p->ruby_sourcefile_string = Qnil;
6840 p->ruby_sourcefile = "(none)";
6843 p->ruby_sourcefile_string = rb_fstring(fname);
6844 p->ruby_sourcefile = StringValueCStr(fname);
6846 p->ruby_sourceline = line - 1;
6850 p->ast = ast = rb_ast_new();
6851 rb_suppress_tracing(yycompile0, (VALUE)p);
6853 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6864must_be_ascii_compatible(VALUE s)
6866 rb_encoding *enc = rb_enc_get(s);
6867 if (!rb_enc_asciicompat(enc)) {
6868 rb_raise(rb_eArgError, "invalid source encoding");
6874lex_get_str(struct parser_params *p, VALUE s)
6876 char *beg, *end, *start;
6879 beg = RSTRING_PTR(s);
6880 len = RSTRING_LEN(s);
6882 if (p->lex.gets_.ptr) {
6883 if (len == p->lex.gets_.ptr) return Qnil;
6884 beg += p->lex.gets_.ptr;
6885 len -= p->lex.gets_.ptr;
6887 end = memchr(beg, '\n', len);
6888 if (end) len = ++end - beg;
6889 p->lex.gets_.ptr += len;
6890 return rb_str_subseq(s, beg - start, len);
6894lex_getline(struct parser_params *p)
6896 VALUE line = (*p->lex.gets)(p, p->lex.input);
6897 if (NIL_P(line)) return line;
6898 must_be_ascii_compatible(line);
6899 if (RB_OBJ_FROZEN(line)) line = rb_str_dup(line); // needed for RubyVM::AST.of because script_lines in iseq is deep-frozen
6904static const rb_data_type_t parser_data_type;
6908parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6910 struct parser_params *p;
6912 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6914 p->lex.gets = lex_get_str;
6915 p->lex.gets_.ptr = 0;
6916 p->lex.input = rb_str_new_frozen(s);
6917 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6919 return yycompile(vparser, p, fname, line);
6923rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6925 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6929rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6931 must_be_ascii_compatible(s);
6932 return parser_compile_string(vparser, f, s, line);
6935VALUE rb_io_gets_internal(VALUE io);
6938lex_io_gets(struct parser_params *p, VALUE io)
6940 return rb_io_gets_internal(io);
6944rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6946 struct parser_params *p;
6948 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6950 p->lex.gets = lex_io_gets;
6951 p->lex.input = file;
6952 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6954 return yycompile(vparser, p, fname, start);
6958lex_generic_gets(struct parser_params *p, VALUE input)
6960 return (*p->lex.gets_.call)(input, p->line_count);
6964rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6966 struct parser_params *p;
6968 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6970 p->lex.gets = lex_generic_gets;
6971 p->lex.gets_.call = lex_gets;
6972 p->lex.input = input;
6973 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6975 return yycompile(vparser, p, fname, start);
6979#define STR_FUNC_ESCAPE 0x01
6980#define STR_FUNC_EXPAND 0x02
6981#define STR_FUNC_REGEXP 0x04
6982#define STR_FUNC_QWORDS 0x08
6983#define STR_FUNC_SYMBOL 0x10
6984#define STR_FUNC_INDENT 0x20
6985#define STR_FUNC_LABEL 0x40
6986#define STR_FUNC_LIST 0x4000
6987#define STR_FUNC_TERM 0x8000
6990 str_label = STR_FUNC_LABEL,
6992 str_dquote = (STR_FUNC_EXPAND),
6993 str_xquote = (STR_FUNC_EXPAND),
6994 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6995 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6996 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6997 str_ssym = (STR_FUNC_SYMBOL),
6998 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7002parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7006 str = rb_enc_str_new(ptr, len, enc);
7007 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7008 if (is_ascii_string(str)) {
7010 else if (rb_is_usascii_enc(enc0) && enc != rb_utf8_encoding()) {
7011 rb_enc_associate(str, rb_ascii8bit_encoding());
7018#define peek(p,c) peek_n(p, (c), 0)
7019#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7020#define peekc(p) peekc_n(p, 0)
7021#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7024add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7027 debug_token_line(p, "add_delayed_token", line);
7031 if (!has_delayed_token(p)) {
7032 p->delayed.token = rb_str_buf_new(end - tok);
7033 rb_enc_associate(p->delayed.token, p->enc);
7034 p->delayed.beg_line = p->ruby_sourceline;
7035 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7037 rb_str_buf_cat(p->delayed.token, tok, end - tok);
7038 p->delayed.end_line = p->ruby_sourceline;
7039 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7045nextline(struct parser_params *p, int set_encoding)
7047 VALUE v = p->lex.nextline;
7048 p->lex.nextline = 0;
7053 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
7057 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
7064 if (p->debug_lines) {
7065 if (set_encoding) rb_enc_associate(v, p->enc);
7066 rb_ary_push(p->debug_lines, v);
7071 else if (NIL_P(v)) {
7072 /* after here-document without terminator */
7075 add_delayed_token(p, p->lex.ptok, p->lex.pend, __LINE__);
7076 if (p->heredoc_end > 0) {
7077 p->ruby_sourceline = p->heredoc_end;
7080 p->ruby_sourceline++;
7081 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
7082 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
7084 p->lex.lastline = v;
7089parser_cr(struct parser_params *p, int c)
7091 if (peek(p, '\n')) {
7099nextc0(struct parser_params *p, int set_encoding)
7103 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
7104 if (nextline(p, set_encoding)) return -1;
7106 c = (unsigned char)*p->lex.pcur++;
7107 if (UNLIKELY(c == '\r')) {
7108 c = parser_cr(p, c);
7113#define nextc(p) nextc0(p, TRUE)
7116pushback(struct parser_params *p, int c)
7118 if (c == -1) return;
7120 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7125#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7127#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7128#define tok(p) (p)->tokenbuf
7129#define toklen(p) (p)->tokidx
7132looking_at_eol_p(struct parser_params *p)
7134 const char *ptr = p->lex.pcur;
7135 while (ptr < p->lex.pend) {
7136 int c = (unsigned char)*ptr++;
7137 int eol = (c == '\n' || c == '#');
7138 if (eol || !ISSPACE(c)) {
7146newtok(struct parser_params *p)
7149 p->tokline = p->ruby_sourceline;
7152 p->tokenbuf = ALLOC_N(char, 60);
7154 if (p->toksiz > 4096) {
7156 REALLOC_N(p->tokenbuf, char, 60);
7162tokspace(struct parser_params *p, int n)
7166 if (p->tokidx >= p->toksiz) {
7167 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7168 REALLOC_N(p->tokenbuf, char, p->toksiz);
7170 return &p->tokenbuf[p->tokidx-n];
7174tokadd(struct parser_params *p, int c)
7176 p->tokenbuf[p->tokidx++] = (char)c;
7177 if (p->tokidx >= p->toksiz) {
7179 REALLOC_N(p->tokenbuf, char, p->toksiz);
7184tok_hex(struct parser_params *p, size_t *numlen)
7188 c = scan_hex(p->lex.pcur, 2, numlen);
7190 yyerror0("invalid hex escape");
7194 p->lex.pcur += *numlen;
7198#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7201escaped_control_code(int c)
7227#define WARN_SPACE_CHAR(c, prefix) \
7228 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
7231tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7232 int regexp_literal, int wide)
7235 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7236 p->lex.pcur += numlen;
7237 if (p->lex.strterm == NULL ||
7238 (p->lex.strterm->flags & STRTERM_HEREDOC) ||
7239 (p->lex.strterm->u.literal.u1.func != str_regexp)) {
7240 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7241 literal_flush(p, p->lex.pcur);
7242 yyerror0("invalid Unicode escape");
7243 return wide && numlen > 0;
7245 if (codepoint > 0x10ffff) {
7246 literal_flush(p, p->lex.pcur);
7247 yyerror0("invalid Unicode codepoint (too large)");
7250 if ((codepoint & 0xfffff800) == 0xd800) {
7251 literal_flush(p, p->lex.pcur);
7252 yyerror0("invalid Unicode codepoint");
7256 if (regexp_literal) {
7257 tokcopy(p, (int)numlen);
7259 else if (codepoint >= 0x80) {
7260 rb_encoding *utf8 = rb_utf8_encoding();
7261 if (*encp && utf8 != *encp) {
7262 YYLTYPE loc = RUBY_INIT_YYLLOC();
7263 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7264 parser_show_error_line(p, &loc);
7268 tokaddmbc(p, codepoint, *encp);
7271 tokadd(p, codepoint);
7276static int tokadd_mbchar(struct parser_params *p, int c);
7279tokskip_mbchar(struct parser_params *p)
7281 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7283 p->lex.pcur += len - 1;
7288/* return value is for ?\u3042 */
7290tokadd_utf8(struct parser_params *p, rb_encoding **encp,
7291 int term, int symbol_literal, int regexp_literal)
7294 * If `term` is not -1, then we allow multiple codepoints in \u{}
7295 * upto `term` byte, otherwise we're parsing a character literal.
7296 * And then add the codepoints to the current token.
7298 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
7300 const int open_brace = '{', close_brace = '}';
7302 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
7304 if (peek(p, open_brace)) { /* handle \u{...} form */
7305 if (regexp_literal && p->lex.strterm->u.literal.u1.func == str_regexp) {
7307 * Skip parsing validation code and copy bytes as-is until term or
7308 * closing brace, in order to correctly handle extended regexps where
7309 * invalid unicode escapes are allowed in comments. The regexp parser
7310 * does its own validation and will catch any issues.
7312 tokadd(p, open_brace);
7313 while (++p->lex.pcur < p->lex.pend) {
7315 if (c == close_brace) {
7320 else if (c == term) {
7323 if (c == '\\' && p->lex.pcur + 1 < p->lex.pend) {
7327 tokadd_mbchar(p, c);
7331 const char *second = NULL;
7332 int c, last = nextc(p);
7333 if (p->lex.pcur >= p->lex.pend) goto unterminated;
7334 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
7335 while (c != close_brace) {
7336 if (c == term) goto unterminated;
7337 if (second == multiple_codepoints)
7338 second = p->lex.pcur;
7339 if (regexp_literal) tokadd(p, last);
7340 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
7343 while (ISSPACE(c = *p->lex.pcur)) {
7344 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
7347 if (term == -1 && !second)
7348 second = multiple_codepoints;
7351 if (c != close_brace) {
7354 yyerror0("unterminated Unicode escape");
7357 if (second && second != multiple_codepoints) {
7358 const char *pcur = p->lex.pcur;
7359 p->lex.pcur = second;
7360 dispatch_scan_event(p, tSTRING_CONTENT);
7363 yyerror0(multiple_codepoints);
7367 if (regexp_literal) tokadd(p, close_brace);
7371 else { /* handle \uxxxx form */
7372 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
7379#define ESCAPE_CONTROL 1
7380#define ESCAPE_META 2
7383read_escape(struct parser_params *p, int flags, rb_encoding **encp)
7388 switch (c = nextc(p)) {
7389 case '\\': /* Backslash */
7392 case 'n': /* newline */
7395 case 't': /* horizontal tab */
7398 case 'r': /* carriage-return */
7401 case 'f': /* form-feed */
7404 case 'v': /* vertical tab */
7407 case 'a': /* alarm(bell) */
7410 case 'e': /* escape */
7413 case '0': case '1': case '2': case '3': /* octal constant */
7414 case '4': case '5': case '6': case '7':
7416 c = scan_oct(p->lex.pcur, 3, &numlen);
7417 p->lex.pcur += numlen;
7420 case 'x': /* hex constant */
7421 c = tok_hex(p, &numlen);
7422 if (numlen == 0) return 0;
7425 case 'b': /* backspace */
7428 case 's': /* space */
7432 if (flags & ESCAPE_META) goto eof;
7433 if ((c = nextc(p)) != '-') {
7436 if ((c = nextc(p)) == '\\') {
7442 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
7444 else if (c == -1) goto eof;
7445 else if (!ISASCII(c)) {
7450 int c2 = escaped_control_code(c);
7452 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
7453 WARN_SPACE_CHAR(c2, "\\M-");
7456 WARN_SPACE_CHAR(c2, "\\C-\\M-");
7459 else if (ISCNTRL(c)) goto eof;
7460 return ((c & 0xff) | 0x80);
7464 if ((c = nextc(p)) != '-') {
7468 if (flags & ESCAPE_CONTROL) goto eof;
7469 if ((c = nextc(p))== '\\') {
7475 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
7479 else if (c == -1) goto eof;
7480 else if (!ISASCII(c)) {
7485 int c2 = escaped_control_code(c);
7488 if (flags & ESCAPE_META) {
7489 WARN_SPACE_CHAR(c2, "\\M-");
7492 WARN_SPACE_CHAR(c2, "");
7496 if (flags & ESCAPE_META) {
7497 WARN_SPACE_CHAR(c2, "\\M-\\C-");
7500 WARN_SPACE_CHAR(c2, "\\C-");
7504 else if (ISCNTRL(c)) goto eof;
7510 yyerror0("Invalid escape character syntax");
7511 dispatch_scan_event(p, tSTRING_CONTENT);
7520tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
7522 int len = rb_enc_codelen(c, enc);
7523 rb_enc_mbcput(c, tokspace(p, len), enc);
7527tokadd_escape(struct parser_params *p, rb_encoding **encp)
7532 switch (c = nextc(p)) {
7534 return 0; /* just ignore */
7536 case '0': case '1': case '2': case '3': /* octal constant */
7537 case '4': case '5': case '6': case '7':
7539 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
7540 if (numlen == 0) goto eof;
7541 p->lex.pcur += numlen;
7542 tokcopy(p, (int)numlen + 1);
7546 case 'x': /* hex constant */
7548 tok_hex(p, &numlen);
7549 if (numlen == 0) return -1;
7550 tokcopy(p, (int)numlen + 2);
7556 yyerror0("Invalid escape character syntax");
7568regx_options(struct parser_params *p)
7576 while (c = nextc(p), ISALPHA(c)) {
7578 options |= RE_OPTION_ONCE;
7580 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
7582 if (kc != rb_ascii8bit_encindex()) kcode = c;
7596 YYLTYPE loc = RUBY_INIT_YYLLOC();
7598 compile_error(p, "unknown regexp option%s - %*s",
7599 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
7600 parser_show_error_line(p, &loc);
7602 return options | RE_OPTION_ENCODING(kcode);
7606tokadd_mbchar(struct parser_params *p, int c)
7608 int len = parser_precise_mbclen(p, p->lex.pcur-1);
7609 if (len < 0) return -1;
7611 p->lex.pcur += --len;
7612 if (len > 0) tokcopy(p, len);
7617simple_re_meta(int c)
7620 case '$': case '*': case '+': case '.':
7621 case '?': case '^': case '|':
7622 case ')': case ']': case '}': case '>':
7630parser_update_heredoc_indent(struct parser_params *p, int c)
7632 if (p->heredoc_line_indent == -1) {
7633 if (c == '\n') p->heredoc_line_indent = 0;
7637 p->heredoc_line_indent++;
7640 else if (c == '\t') {
7641 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
7642 p->heredoc_line_indent = w * TAB_WIDTH;
7645 else if (c != '\n') {
7646 if (p->heredoc_indent > p->heredoc_line_indent) {
7647 p->heredoc_indent = p->heredoc_line_indent;
7649 p->heredoc_line_indent = -1;
7656parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
7658 YYLTYPE loc = RUBY_INIT_YYLLOC();
7659 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
7660 compile_error(p, "%s mixed within %s source", n1, n2);
7661 parser_show_error_line(p, &loc);
7665parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
7667 const char *pos = p->lex.pcur;
7669 parser_mixed_error(p, enc1, enc2);
7674tokadd_string(struct parser_params *p,
7675 int func, int term, int paren, long *nest,
7676 rb_encoding **encp, rb_encoding **enc)
7681 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
7682 int top_of_line = FALSE;
7685#define mixed_error(enc1, enc2) \
7686 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
7687#define mixed_escape(beg, enc1, enc2) \
7688 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
7690 while ((c = nextc(p)) != -1) {
7691 if (p->heredoc_indent > 0) {
7692 parser_update_heredoc_indent(p, c);
7695 if (top_of_line && heredoc_end == p->ruby_sourceline) {
7701 if (paren && c == paren) {
7704 else if (c == term) {
7705 if (!nest || !*nest) {
7711 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
7712 unsigned char c2 = *p->lex.pcur;
7713 if (c2 == '$' || c2 == '@' || c2 == '{') {
7718 else if (c == '\\') {
7722 if (func & STR_FUNC_QWORDS) break;
7723 if (func & STR_FUNC_EXPAND) {
7724 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
7735 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
7739 if ((func & STR_FUNC_EXPAND) == 0) {
7743 tokadd_utf8(p, enc, term,
7744 func & STR_FUNC_SYMBOL,
7745 func & STR_FUNC_REGEXP);
7749 if (c == -1) return -1;
7751 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
7754 if (func & STR_FUNC_REGEXP) {
7760 c = read_escape(p, 0, enc);
7764 snprintf(escbuf, sizeof(escbuf), "\\x%02X", c);
7765 for (i = 0; i < 4; i++) {
7766 tokadd(p, escbuf[i]);
7772 if (c == term && !simple_re_meta(c)) {
7777 if ((c = tokadd_escape(p, enc)) < 0)
7779 if (*enc && *enc != *encp) {
7780 mixed_escape(p->lex.ptok+2, *enc, *encp);
7784 else if (func & STR_FUNC_EXPAND) {
7786 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
7787 c = read_escape(p, 0, enc);
7789 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7790 /* ignore backslashed spaces in %w */
7792 else if (c != term && !(paren && c == paren)) {
7799 else if (!parser_isascii(p)) {
7804 else if (*enc != *encp) {
7805 mixed_error(*enc, *encp);
7808 if (tokadd_mbchar(p, c) == -1) return -1;
7811 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7819 else if (*enc != *encp) {
7820 mixed_error(*enc, *encp);
7826 top_of_line = (c == '\n');
7830 if (*enc) *encp = *enc;
7834static inline rb_strterm_t *
7835new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7837 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7840/* imemo_parser_strterm for literal */
7841#define NEW_STRTERM(func, term, paren) \
7842 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7846flush_string_content(struct parser_params *p, rb_encoding *enc)
7848 VALUE content = yylval.val;
7849 if (!ripper_is_node_yylval(content))
7850 content = ripper_new_yylval(p, 0, 0, content);
7851 if (has_delayed_token(p)) {
7852 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7854 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7856 dispatch_delayed_token(p, tSTRING_CONTENT);
7857 p->lex.ptok = p->lex.pcur;
7858 RNODE(content)->nd_rval = yylval.val;
7860 dispatch_scan_event(p, tSTRING_CONTENT);
7861 if (yylval.val != content)
7862 RNODE(content)->nd_rval = yylval.val;
7863 yylval.val = content;
7867flush_string_content(struct parser_params *p, rb_encoding *enc)
7869 if (has_delayed_token(p)) {
7870 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7872 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7873 p->delayed.end_line = p->ruby_sourceline;
7874 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
7876 dispatch_delayed_token(p, tSTRING_CONTENT);
7877 p->lex.ptok = p->lex.pcur;
7879 dispatch_scan_event(p, tSTRING_CONTENT);
7883RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7884/* this can be shared with ripper, since it's independent from struct
7887#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7888#define SPECIAL_PUNCT(idx) ( \
7889 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7890 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7891 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7892 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7893 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7895const unsigned int ruby_global_name_punct_bits[] = {
7904static enum yytokentype
7905parser_peek_variable_name(struct parser_params *p)
7908 const char *ptr = p->lex.pcur;
7910 if (ptr + 1 >= p->lex.pend) return 0;
7914 if ((c = *ptr) == '-') {
7915 if (++ptr >= p->lex.pend) return 0;
7918 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7919 return tSTRING_DVAR;
7923 if ((c = *ptr) == '@') {
7924 if (++ptr >= p->lex.pend) return 0;
7930 p->command_start = TRUE;
7931 return tSTRING_DBEG;
7935 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7936 return tSTRING_DVAR;
7940#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7941#define IS_END() IS_lex_state(EXPR_END_ANY)
7942#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7943#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7944#define IS_LABEL_POSSIBLE() (\
7945 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7947#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7948#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7950static inline enum yytokentype
7951parser_string_term(struct parser_params *p, int func)
7954 if (func & STR_FUNC_REGEXP) {
7955 set_yylval_num(regx_options(p));
7956 dispatch_scan_event(p, tREGEXP_END);
7957 SET_LEX_STATE(EXPR_END);
7960 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7962 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
7965 SET_LEX_STATE(EXPR_END);
7969static enum yytokentype
7970parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7972 int func = (int)quote->u1.func;
7973 int term = (int)quote->u3.term;
7974 int paren = (int)quote->u2.paren;
7976 rb_encoding *enc = p->enc;
7977 rb_encoding *base_enc = 0;
7980 if (func & STR_FUNC_TERM) {
7981 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7982 SET_LEX_STATE(EXPR_END);
7984 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7987 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7988 do {c = nextc(p);} while (ISSPACE(c));
7991 if (func & STR_FUNC_LIST) {
7992 quote->u1.func &= ~STR_FUNC_LIST;
7995 if (c == term && !quote->u0.nest) {
7996 if (func & STR_FUNC_QWORDS) {
7997 quote->u1.func |= STR_FUNC_TERM;
7998 pushback(p, c); /* dispatch the term at tSTRING_END */
7999 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8002 return parser_string_term(p, func);
8006 add_delayed_token(p, p->lex.ptok, p->lex.pcur, __LINE__);
8010 if ((func & STR_FUNC_EXPAND) && c == '#') {
8011 int t = parser_peek_variable_name(p);
8017 if (tokadd_string(p, func, term, paren, "e->u0.nest,
8018 &enc, &base_enc) == -1) {
8021# define unterminated_literal(mesg) yyerror0(mesg)
8023# define unterminated_literal(mesg) compile_error(p, mesg)
8025 literal_flush(p, p->lex.pcur);
8026 if (func & STR_FUNC_QWORDS) {
8027 /* no content to add, bailing out here */
8028 unterminated_literal("unterminated list meets end of file");
8032 if (func & STR_FUNC_REGEXP) {
8033 unterminated_literal("unterminated regexp meets end of file");
8036 unterminated_literal("unterminated string meets end of file");
8038 quote->u1.func |= STR_FUNC_TERM;
8043 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8044 set_yylval_str(lit);
8045 flush_string_content(p, enc);
8047 return tSTRING_CONTENT;
8050static enum yytokentype
8051heredoc_identifier(struct parser_params *p)
8054 * term_len is length of `<<"END"` except `END`,
8055 * in this case term_len is 4 (<, <, " and ").
8057 long len, offset = p->lex.pcur - p->lex.pbeg;
8058 int c = nextc(p), term, func = 0, quote = 0;
8059 enum yytokentype token = tSTRING_BEG;
8064 func = STR_FUNC_INDENT;
8067 else if (c == '~') {
8069 func = STR_FUNC_INDENT;
8075 func |= str_squote; goto quoted;
8077 func |= str_dquote; goto quoted;
8079 token = tXSTRING_BEG;
8080 func |= str_xquote; goto quoted;
8087 while ((c = nextc(p)) != term) {
8088 if (c == -1 || c == '\r' || c == '\n') {
8089 yyerror0("unterminated here document identifier");
8096 if (!parser_is_identchar(p)) {
8098 if (func & STR_FUNC_INDENT) {
8099 pushback(p, indent > 0 ? '~' : '-');
8105 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8106 if (n < 0) return 0;
8108 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8113 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8114 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8115 yyerror0("too long here document identifier");
8116 dispatch_scan_event(p, tHEREDOC_BEG);
8119 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
8120 p->lex.strterm->flags |= STRTERM_HEREDOC;
8121 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8122 here->offset = offset;
8123 here->sourceline = p->ruby_sourceline;
8124 here->length = (int)len;
8125 here->quote = quote;
8129 p->heredoc_indent = indent;
8130 p->heredoc_line_indent = 0;
8135heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8140 line = here->lastline;
8141 p->lex.lastline = line;
8142 p->lex.pbeg = RSTRING_PTR(line);
8143 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
8144 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8145 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8146 p->heredoc_end = p->ruby_sourceline;
8147 p->ruby_sourceline = (int)here->sourceline;
8148 if (p->eofp) p->lex.nextline = Qnil;
8153dedent_string(VALUE string, int width)
8159 RSTRING_GETMEM(string, str, len);
8160 for (i = 0; i < len && col < width; i++) {
8161 if (str[i] == ' ') {
8164 else if (str[i] == '\t') {
8165 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8166 if (n > width) break;
8174 rb_str_modify(string);
8175 str = RSTRING_PTR(string);
8176 if (RSTRING_LEN(string) != len)
8177 rb_fatal("literal string changed: %+"PRIsVALUE, string);
8178 MEMMOVE(str, str + i, char, len - i);
8179 rb_str_set_len(string, len - i);
8185heredoc_dedent(struct parser_params *p, NODE *root)
8187 NODE *node, *str_node, *prev_node;
8188 int indent = p->heredoc_indent;
8191 if (indent <= 0) return root;
8192 p->heredoc_indent = 0;
8193 if (!root) return root;
8195 prev_node = node = str_node = root;
8196 if (nd_type_p(root, NODE_LIST)) str_node = root->nd_head;
8199 VALUE lit = str_node->nd_lit;
8200 if (str_node->flags & NODE_FL_NEWLINE) {
8201 dedent_string(lit, indent);
8206 else if (!literal_concat0(p, prev_lit, lit)) {
8210 NODE *end = node->nd_end;
8211 node = prev_node->nd_next = node->nd_next;
8213 if (nd_type_p(prev_node, NODE_DSTR))
8214 nd_set_type(prev_node, NODE_STR);
8222 while ((node = (prev_node = node)->nd_next) != 0) {
8224 if (!nd_type_p(node, NODE_LIST)) break;
8225 if ((str_node = node->nd_head) != 0) {
8226 enum node_type type = nd_type(str_node);
8227 if (type == NODE_STR || type == NODE_DSTR) break;
8237heredoc_dedent(struct parser_params *p, VALUE array)
8239 int indent = p->heredoc_indent;
8241 if (indent <= 0) return array;
8242 p->heredoc_indent = 0;
8243 dispatch2(heredoc_dedent, array, INT2NUM(indent));
8249 * Ripper.dedent_string(input, width) -> Integer
8251 * USE OF RIPPER LIBRARY ONLY.
8253 * Strips up to +width+ leading whitespaces from +input+,
8254 * and returns the stripped column width.
8257parser_dedent_string(VALUE self, VALUE input, VALUE width)
8262 wid = NUM2UINT(width);
8263 col = dedent_string(input, wid);
8264 return INT2NUM(col);
8269whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
8271 const char *ptr = p->lex.pbeg;
8275 while (*ptr && ISSPACE(*ptr)) ptr++;
8277 n = p->lex.pend - (ptr + len);
8278 if (n < 0) return FALSE;
8279 if (n > 0 && ptr[len] != '\n') {
8280 if (ptr[len] != '\r') return FALSE;
8281 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
8283 return strncmp(eos, ptr, len) == 0;
8287word_match_p(struct parser_params *p, const char *word, long len)
8289 if (strncmp(p->lex.pcur, word, len)) return 0;
8290 if (p->lex.pcur + len == p->lex.pend) return 1;
8291 int c = (unsigned char)p->lex.pcur[len];
8292 if (ISSPACE(c)) return 1;
8294 case '\0': case '\004': case '\032': return 1;
8299#define NUM_SUFFIX_R (1<<0)
8300#define NUM_SUFFIX_I (1<<1)
8301#define NUM_SUFFIX_ALL 3
8304number_literal_suffix(struct parser_params *p, int mask)
8307 const char *lastp = p->lex.pcur;
8309 while ((c = nextc(p)) != -1) {
8310 if ((mask & NUM_SUFFIX_I) && c == 'i') {
8311 result |= (mask & NUM_SUFFIX_I);
8312 mask &= ~NUM_SUFFIX_I;
8313 /* r after i, rational of complex is disallowed */
8314 mask &= ~NUM_SUFFIX_R;
8317 if ((mask & NUM_SUFFIX_R) && c == 'r') {
8318 result |= (mask & NUM_SUFFIX_R);
8319 mask &= ~NUM_SUFFIX_R;
8322 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
8323 p->lex.pcur = lastp;
8324 literal_flush(p, p->lex.pcur);
8333static enum yytokentype
8334set_number_literal(struct parser_params *p, VALUE v,
8335 enum yytokentype type, int suffix)
8337 if (suffix & NUM_SUFFIX_I) {
8338 v = rb_complex_raw(INT2FIX(0), v);
8341 set_yylval_literal(v);
8342 SET_LEX_STATE(EXPR_END);
8346static enum yytokentype
8347set_integer_literal(struct parser_params *p, VALUE v, int suffix)
8349 enum yytokentype type = tINTEGER;
8350 if (suffix & NUM_SUFFIX_R) {
8351 v = rb_rational_raw1(v);
8354 return set_number_literal(p, v, type, suffix);
8359dispatch_heredoc_end(struct parser_params *p)
8362 if (has_delayed_token(p))
8363 dispatch_delayed_token(p, tSTRING_CONTENT);
8364 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8365 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
8366 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8372#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
8374parser_dispatch_heredoc_end(struct parser_params *p, int line)
8376 if (has_delayed_token(p))
8377 dispatch_delayed_token(p, tSTRING_CONTENT);
8379 if (p->keep_tokens) {
8380 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
8381 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
8382 parser_append_tokens(p, str, tHEREDOC_END, line);
8385 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
8391static enum yytokentype
8392here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
8394 int c, func, indent = 0;
8395 const char *eos, *ptr, *ptr_end;
8398 rb_encoding *enc = p->enc;
8399 rb_encoding *base_enc = 0;
8402 eos = RSTRING_PTR(here->lastline) + here->offset;
8404 indent = (func = here->func) & STR_FUNC_INDENT;
8406 if ((c = nextc(p)) == -1) {
8409 if (!has_delayed_token(p)) {
8410 dispatch_scan_event(p, tSTRING_CONTENT);
8413 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
8414 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
8415 int cr = ENC_CODERANGE_UNKNOWN;
8416 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
8417 if (cr != ENC_CODERANGE_7BIT &&
8418 rb_is_usascii_enc(p->enc) &&
8419 enc != rb_utf8_encoding()) {
8420 enc = rb_ascii8bit_encoding();
8423 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
8425 dispatch_delayed_token(p, tSTRING_CONTENT);
8429 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8430 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
8434 SET_LEX_STATE(EXPR_END);
8439 /* not beginning of line, cannot be the terminator */
8441 else if (p->heredoc_line_indent == -1) {
8442 /* `heredoc_line_indent == -1` means
8443 * - "after an interpolation in the same line", or
8444 * - "in a continuing line"
8446 p->heredoc_line_indent = 0;
8448 else if (whole_match_p(p, eos, len, indent)) {
8449 dispatch_heredoc_end(p);
8451 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8454 SET_LEX_STATE(EXPR_END);
8458 if (!(func & STR_FUNC_EXPAND)) {
8460 ptr = RSTRING_PTR(p->lex.lastline);
8461 ptr_end = p->lex.pend;
8462 if (ptr_end > ptr) {
8463 switch (ptr_end[-1]) {
8465 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
8474 if (p->heredoc_indent > 0) {
8476 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
8478 p->heredoc_line_indent = 0;
8482 rb_str_cat(str, ptr, ptr_end - ptr);
8484 str = STR_NEW(ptr, ptr_end - ptr);
8485 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
8487 if (p->heredoc_indent > 0) {
8490 if (nextc(p) == -1) {
8496 } while (!whole_match_p(p, eos, len, indent));
8499 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
8502 int t = parser_peek_variable_name(p);
8503 if (p->heredoc_line_indent != -1) {
8504 if (p->heredoc_indent > p->heredoc_line_indent) {
8505 p->heredoc_indent = p->heredoc_line_indent;
8507 p->heredoc_line_indent = -1;
8516 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
8517 if (p->eofp) goto error;
8521 if (c == '\\') p->heredoc_line_indent = -1;
8523 str = STR_NEW3(tok(p), toklen(p), enc, func);
8525 set_yylval_str(str);
8527 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8529 flush_string_content(p, enc);
8530 return tSTRING_CONTENT;
8532 tokadd(p, nextc(p));
8533 if (p->heredoc_indent > 0) {
8537 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
8538 if ((c = nextc(p)) == -1) goto error;
8539 } while (!whole_match_p(p, eos, len, indent));
8540 str = STR_NEW3(tok(p), toklen(p), enc, func);
8542 dispatch_heredoc_end(p);
8544 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
8547 heredoc_restore(p, &p->lex.strterm->u.heredoc);
8549 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
8550 set_yylval_str(str);
8552 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
8554 return tSTRING_CONTENT;
8560arg_ambiguous(struct parser_params *p, char c)
8564 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after `%c' operator", WARN_I(c));
8567 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
8570 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
8577formal_argument(struct parser_params *p, ID lhs)
8579formal_argument(struct parser_params *p, VALUE lhs)
8582 ID id = get_id(lhs);
8584 switch (id_type(id)) {
8588# define ERR(mesg) yyerror0(mesg)
8590# define ERR(mesg) (dispatch2(param_error, WARN_S(mesg), lhs), ripper_error(p))
8593 ERR("formal argument cannot be a constant");
8596 ERR("formal argument cannot be an instance variable");
8599 ERR("formal argument cannot be a global variable");
8602 ERR("formal argument cannot be a class variable");
8605 ERR("formal argument must be local variable");
8609 shadowing_lvar(p, id);
8614lvar_defined(struct parser_params *p, ID id)
8616 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
8619/* emacsen -*- hack */
8621parser_encode_length(struct parser_params *p, const char *name, long len)
8625 if (len > 5 && name[nlen = len - 5] == '-') {
8626 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
8629 if (len > 4 && name[nlen = len - 4] == '-') {
8630 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
8632 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
8633 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
8634 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
8641parser_set_encode(struct parser_params *p, const char *name)
8643 int idx = rb_enc_find_index(name);
8648 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
8650 excargs[0] = rb_eArgError;
8651 excargs[2] = rb_make_backtrace();
8652 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
8653 rb_exc_raise(rb_make_exception(3, excargs));
8655 enc = rb_enc_from_index(idx);
8656 if (!rb_enc_asciicompat(enc)) {
8657 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
8662 if (p->debug_lines) {
8663 VALUE lines = p->debug_lines;
8664 long i, n = RARRAY_LEN(lines);
8665 for (i = 0; i < n; ++i) {
8666 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
8673comment_at_top(struct parser_params *p)
8675 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
8676 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
8677 while (ptr < ptr_end) {
8678 if (!ISSPACE(*ptr)) return 0;
8684typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
8685typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
8687static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
8690magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
8692 if (!comment_at_top(p)) {
8695 parser_set_encode(p, val);
8699parser_get_bool(struct parser_params *p, const char *name, const char *val)
8703 if (STRCASECMP(val, "true") == 0) {
8708 if (STRCASECMP(val, "false") == 0) {
8713 return parser_invalid_pragma_value(p, name, val);
8717parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
8719 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
8724parser_set_token_info(struct parser_params *p, const char *name, const char *val)
8726 int b = parser_get_bool(p, name, val);
8727 if (b >= 0) p->token_info_enabled = b;
8731parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
8735 if (p->token_seen) {
8736 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
8740 b = parser_get_bool(p, name, val);
8743 if (!p->compile_option)
8744 p->compile_option = rb_obj_hide(rb_ident_hash_new());
8745 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
8750parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
8752 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
8753 if (*s == ' ' || *s == '\t') continue;
8754 if (*s == '#') break;
8755 rb_warning1("`%s' is ignored unless in comment-only line", WARN_S(name));
8761 if (STRCASECMP(val, "none") == 0) {
8762 p->ctxt.shareable_constant_value = shareable_none;
8767 if (STRCASECMP(val, "literal") == 0) {
8768 p->ctxt.shareable_constant_value = shareable_literal;
8773 if (STRCASECMP(val, "experimental_copy") == 0) {
8774 p->ctxt.shareable_constant_value = shareable_copy;
8777 if (STRCASECMP(val, "experimental_everything") == 0) {
8778 p->ctxt.shareable_constant_value = shareable_everything;
8783 parser_invalid_pragma_value(p, name, val);
8788parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
8790 int b = parser_get_bool(p, name, val);
8791 if (b >= 0) p->past_scope_enabled = b;
8795struct magic_comment {
8797 rb_magic_comment_setter_t func;
8798 rb_magic_comment_length_t length;
8801static const struct magic_comment magic_comments[] = {
8802 {"coding", magic_comment_encoding, parser_encode_length},
8803 {"encoding", magic_comment_encoding, parser_encode_length},
8804 {"frozen_string_literal", parser_set_compile_option_flag},
8805 {"shareable_constant_value", parser_set_shareable_constant_value},
8806 {"warn_indent", parser_set_token_info},
8808 {"warn_past_scope", parser_set_past_scope},
8813magic_comment_marker(const char *str, long len)
8820 if (str[i-1] == '*' && str[i-2] == '-') {
8826 if (i + 1 >= len) return 0;
8827 if (str[i+1] != '-') {
8830 else if (str[i-1] != '-') {
8846parser_magic_comment(struct parser_params *p, const char *str, long len)
8849 VALUE name = 0, val = 0;
8850 const char *beg, *end, *vbeg, *vend;
8851#define str_copy(_s, _p, _n) ((_s) \
8852 ? (void)(rb_str_resize((_s), (_n)), \
8853 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
8854 : (void)((_s) = STR_NEW((_p), (_n))))
8856 if (len <= 7) return FALSE;
8857 if (!!(beg = magic_comment_marker(str, len))) {
8858 if (!(end = magic_comment_marker(beg, str + len - beg)))
8862 len = end - beg - 3;
8865 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
8867 const struct magic_comment *mc = magic_comments;
8872 for (; len > 0 && *str; str++, --len) {
8874 case '\'': case '"': case ':': case ';':
8877 if (!ISSPACE(*str)) break;
8879 for (beg = str; len > 0; str++, --len) {
8881 case '\'': case '"': case ':': case ';':
8884 if (ISSPACE(*str)) break;
8889 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
8892 if (!indicator) return FALSE;
8896 do str++; while (--len > 0 && ISSPACE(*str));
8899 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
8912 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
8916 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8919 while (len > 0 && (ISSPACE(*str))) --len, str++;
8920 if (len) return FALSE;
8924 str_copy(name, beg, n);
8925 s = RSTRING_PTR(name);
8926 for (i = 0; i < n; ++i) {
8927 if (s[i] == '-') s[i] = '_';
8930 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8933 n = (*mc->length)(p, vbeg, n);
8935 str_copy(val, vbeg, n);
8936 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8939 } while (++mc < magic_comments + numberof(magic_comments));
8941 str_copy(val, vbeg, vend - vbeg);
8942 dispatch2(magic_comment, name, val);
8950set_file_encoding(struct parser_params *p, const char *str, const char *send)
8953 const char *beg = str;
8957 if (send - str <= 6) return;
8959 case 'C': case 'c': str += 6; continue;
8960 case 'O': case 'o': str += 5; continue;
8961 case 'D': case 'd': str += 4; continue;
8962 case 'I': case 'i': str += 3; continue;
8963 case 'N': case 'n': str += 2; continue;
8964 case 'G': case 'g': str += 1; continue;
8971 if (ISSPACE(*str)) break;
8974 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8979 if (++str >= send) return;
8980 } while (ISSPACE(*str));
8982 if (*str != '=' && *str != ':') return;
8987 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8988 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8989 parser_set_encode(p, RSTRING_PTR(s));
8990 rb_str_resize(s, 0);
8994parser_prepare(struct parser_params *p)
8996 int c = nextc0(p, FALSE);
8997 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9000 if (peek(p, '!')) p->has_shebang = 1;
9002 case 0xef: /* UTF-8 BOM marker */
9003 if (p->lex.pend - p->lex.pcur >= 2 &&
9004 (unsigned char)p->lex.pcur[0] == 0xbb &&
9005 (unsigned char)p->lex.pcur[1] == 0xbf) {
9006 p->enc = rb_utf8_encoding();
9009 if (p->debug_lines) {
9010 rb_enc_associate(p->lex.lastline, p->enc);
9013 p->lex.pbeg = p->lex.pcur;
9021 p->enc = rb_enc_get(p->lex.lastline);
9025#define ambiguous_operator(tok, op, syn) ( \
9026 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
9027 rb_warning0("even though it seems like "syn""))
9029#define ambiguous_operator(tok, op, syn) \
9030 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9032#define warn_balanced(tok, op, syn) ((void) \
9033 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9034 space_seen && !ISSPACE(c) && \
9035 (ambiguous_operator(tok, op, syn), 0)), \
9036 (enum yytokentype)(tok))
9039parse_rational(struct parser_params *p, char *str, int len, int seen_point)
9042 char *point = &str[seen_point];
9043 size_t fraclen = len-seen_point-1;
9044 memmove(point, point+1, fraclen+1);
9045 v = rb_cstr_to_inum(str, 10, FALSE);
9046 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
9049static enum yytokentype
9050no_digits(struct parser_params *p)
9052 yyerror0("numeric literal without digits");
9053 if (peek(p, '_')) nextc(p);
9054 /* dummy 0, for tUMINUS_NUM at numeric */
9055 return set_integer_literal(p, INT2FIX(0), 0);
9058static enum yytokentype
9059parse_numeric(struct parser_params *p, int c)
9061 int is_float, seen_point, seen_e, nondigit;
9064 is_float = seen_point = seen_e = nondigit = 0;
9065 SET_LEX_STATE(EXPR_END);
9067 if (c == '-' || c == '+') {
9072 int start = toklen(p);
9074 if (c == 'x' || c == 'X') {
9077 if (c != -1 && ISXDIGIT(c)) {
9080 if (nondigit) break;
9084 if (!ISXDIGIT(c)) break;
9087 } while ((c = nextc(p)) != -1);
9091 if (toklen(p) == start) {
9092 return no_digits(p);
9094 else if (nondigit) goto trailing_uc;
9095 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9096 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
9098 if (c == 'b' || c == 'B') {
9101 if (c == '0' || c == '1') {
9104 if (nondigit) break;
9108 if (c != '0' && c != '1') break;
9111 } while ((c = nextc(p)) != -1);
9115 if (toklen(p) == start) {
9116 return no_digits(p);
9118 else if (nondigit) goto trailing_uc;
9119 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9120 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
9122 if (c == 'd' || c == 'D') {
9125 if (c != -1 && ISDIGIT(c)) {
9128 if (nondigit) break;
9132 if (!ISDIGIT(c)) break;
9135 } while ((c = nextc(p)) != -1);
9139 if (toklen(p) == start) {
9140 return no_digits(p);
9142 else if (nondigit) goto trailing_uc;
9143 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9144 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9150 if (c == 'o' || c == 'O') {
9151 /* prefixed octal */
9153 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9154 return no_digits(p);
9157 if (c >= '0' && c <= '7') {
9162 if (nondigit) break;
9166 if (c < '0' || c > '9') break;
9167 if (c > '7') goto invalid_octal;
9170 } while ((c = nextc(p)) != -1);
9171 if (toklen(p) > start) {
9174 if (nondigit) goto trailing_uc;
9175 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9176 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
9183 if (c > '7' && c <= '9') {
9185 yyerror0("Invalid octal digit");
9187 else if (c == '.' || c == 'e' || c == 'E') {
9192 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9193 return set_integer_literal(p, INT2FIX(0), suffix);
9199 case '0': case '1': case '2': case '3': case '4':
9200 case '5': case '6': case '7': case '8': case '9':
9206 if (nondigit) goto trailing_uc;
9207 if (seen_point || seen_e) {
9212 if (c0 == -1 || !ISDIGIT(c0)) {
9218 seen_point = toklen(p);
9237 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9242 tokadd(p, nondigit);
9246 nondigit = (c == '-' || c == '+') ? c : 0;
9249 case '_': /* `_' in number just ignored */
9250 if (nondigit) goto decode_num;
9264 literal_flush(p, p->lex.pcur - 1);
9265 YYLTYPE loc = RUBY_INIT_YYLLOC();
9266 compile_error(p, "trailing `%c' in number", nondigit);
9267 parser_show_error_line(p, &loc);
9271 enum yytokentype type = tFLOAT;
9274 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
9275 if (suffix & NUM_SUFFIX_R) {
9277 v = parse_rational(p, tok(p), toklen(p), seen_point);
9280 double d = strtod(tok(p), 0);
9281 if (errno == ERANGE) {
9282 rb_warning1("Float %s out of range", WARN_S(tok(p)));
9287 return set_number_literal(p, v, type, suffix);
9289 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9290 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
9293static enum yytokentype
9294parse_qmark(struct parser_params *p, int space_seen)
9301 SET_LEX_STATE(EXPR_VALUE);
9306 compile_error(p, "incomplete character syntax");
9309 if (rb_enc_isspace(c, p->enc)) {
9311 int c2 = escaped_control_code(c);
9313 WARN_SPACE_CHAR(c2, "?");
9318 SET_LEX_STATE(EXPR_VALUE);
9323 if (!parser_isascii(p)) {
9324 if (tokadd_mbchar(p, c) == -1) return 0;
9326 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
9327 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
9329 const char *start = p->lex.pcur - 1, *ptr = start;
9331 int n = parser_precise_mbclen(p, ptr);
9332 if (n < 0) return -1;
9334 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
9335 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
9336 " a conditional operator, put a space after `?'",
9337 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
9341 else if (c == '\\') {
9344 enc = rb_utf8_encoding();
9345 tokadd_utf8(p, &enc, -1, 0, 0);
9347 else if (!ISASCII(c = peekc(p))) {
9349 if (tokadd_mbchar(p, c) == -1) return 0;
9352 c = read_escape(p, 0, &enc);
9360 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
9361 set_yylval_str(lit);
9362 SET_LEX_STATE(EXPR_END);
9366static enum yytokentype
9367parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
9370 const char *ptok = p->lex.pcur;
9378 if (c == -1) goto unterminated;
9381 if (!ISASCII(c)) goto unknown;
9386 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
9389 c = parser_precise_mbclen(p, p->lex.pcur);
9390 if (c < 0) return 0;
9392 yyerror0("unknown type of %string");
9398 compile_error(p, "unterminated quoted string meets end of file");
9402 if (term == '(') term = ')';
9403 else if (term == '[') term = ']';
9404 else if (term == '{') term = '}';
9405 else if (term == '<') term = '>';
9408 p->lex.ptok = ptok-1;
9411 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
9415 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
9419 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9423 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9427 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
9428 return tSYMBOLS_BEG;
9431 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
9432 return tQSYMBOLS_BEG;
9435 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
9436 return tXSTRING_BEG;
9439 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
9443 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
9444 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
9448 yyerror0("unknown type of %string");
9452 if ((c = nextc(p)) == '=') {
9454 SET_LEX_STATE(EXPR_BEG);
9457 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
9460 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9462 return warn_balanced('%', "%%", "string literal");
9466tokadd_ident(struct parser_params *p, int c)
9469 if (tokadd_mbchar(p, c) == -1) return -1;
9471 } while (parser_is_identchar(p));
9477tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
9479 ID ident = TOK_INTERN();
9481 set_yylval_name(ident);
9487parse_numvar(struct parser_params *p)
9491 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
9492 const unsigned long nth_ref_max =
9493 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
9494 /* NTH_REF is left-shifted to be ORed with back-ref flag and
9495 * turned into a Fixnum, in compile.c */
9497 if (overflow || n > nth_ref_max) {
9498 /* compile_error()? */
9499 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
9500 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
9507static enum yytokentype
9508parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
9510 const char *ptr = p->lex.pcur;
9513 SET_LEX_STATE(EXPR_END);
9514 p->lex.ptok = ptr - 1; /* from '$' */
9518 case '_': /* $_: last read line string */
9520 if (parser_is_identchar(p)) {
9528 case '~': /* $~: match-data */
9529 case '*': /* $*: argv */
9530 case '$': /* $$: pid */
9531 case '?': /* $?: last status */
9532 case '!': /* $!: error string */
9533 case '@': /* $@: error position */
9534 case '/': /* $/: input record separator */
9535 case '\\': /* $\: output record separator */
9536 case ';': /* $;: field separator */
9537 case ',': /* $,: output field separator */
9538 case '.': /* $.: last read line number */
9539 case '=': /* $=: ignorecase */
9540 case ':': /* $:: load path */
9541 case '<': /* $<: reading filename */
9542 case '>': /* $>: default output handle */
9543 case '\"': /* $": already loaded files */
9552 if (parser_is_identchar(p)) {
9553 if (tokadd_mbchar(p, c) == -1) return 0;
9561 set_yylval_name(TOK_INTERN());
9564 case '&': /* $&: last match */
9565 case '`': /* $`: string before last match */
9566 case '\'': /* $': string after last match */
9567 case '+': /* $+: string matches last paren. */
9568 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
9573 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
9576 case '1': case '2': case '3':
9577 case '4': case '5': case '6':
9578 case '7': case '8': case '9':
9583 } while (c != -1 && ISDIGIT(c));
9585 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
9587 c = parse_numvar(p);
9588 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
9592 if (!parser_is_identchar(p)) {
9593 YYLTYPE loc = RUBY_INIT_YYLLOC();
9594 if (c == -1 || ISSPACE(c)) {
9595 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
9599 compile_error(p, "`$%c' is not allowed as a global variable name", c);
9601 parser_show_error_line(p, &loc);
9602 set_yylval_noname();
9610 if (tokadd_ident(p, c)) return 0;
9611 SET_LEX_STATE(EXPR_END);
9612 tokenize_ident(p, last_state);
9618parser_numbered_param(struct parser_params *p, int n)
9620 if (n < 0) return false;
9622 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
9625 if (p->max_numparam == ORDINAL_PARAM) {
9626 compile_error(p, "ordinary parameter is defined");
9629 struct vtable *args = p->lvtbl->args;
9630 if (p->max_numparam < n) {
9631 p->max_numparam = n;
9633 while (n > args->pos) {
9634 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
9640static enum yytokentype
9641parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
9643 const char *ptr = p->lex.pcur;
9644 enum yytokentype result = tIVAR;
9645 register int c = nextc(p);
9648 p->lex.ptok = ptr - 1; /* from '@' */
9656 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
9657 if (c == -1 || !parser_is_identchar(p)) {
9659 RUBY_SET_YYLLOC(loc);
9660 if (result == tIVAR) {
9661 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
9664 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
9666 parser_show_error_line(p, &loc);
9667 set_yylval_noname();
9668 SET_LEX_STATE(EXPR_END);
9671 else if (ISDIGIT(c)) {
9673 RUBY_SET_YYLLOC(loc);
9674 if (result == tIVAR) {
9675 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
9678 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
9680 parser_show_error_line(p, &loc);
9681 set_yylval_noname();
9682 SET_LEX_STATE(EXPR_END);
9686 if (tokadd_ident(p, c)) return 0;
9687 tokenize_ident(p, last_state);
9691static enum yytokentype
9692parse_ident(struct parser_params *p, int c, int cmd_state)
9694 enum yytokentype result;
9695 int mb = ENC_CODERANGE_7BIT;
9696 const enum lex_state_e last_state = p->lex.state;
9698 int enforce_keyword_end = 0;
9701 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
9702 if (tokadd_mbchar(p, c) == -1) return 0;
9704 } while (parser_is_identchar(p));
9705 if ((c == '!' || c == '?') && !peek(p, '=')) {
9709 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
9710 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
9711 result = tIDENTIFIER;
9715 result = tCONSTANT; /* assume provisionally */
9720 if (IS_LABEL_POSSIBLE()) {
9721 if (IS_LABEL_SUFFIX(0)) {
9722 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
9724 set_yylval_name(TOK_INTERN());
9730 if (!NIL_P(peek_end_expect_token_locations(p))) {
9733 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
9735 end_loc = peek_end_expect_token_locations(p);
9736 lineno = NUM2INT(rb_ary_entry(end_loc, 0));
9737 column = NUM2INT(rb_ary_entry(end_loc, 1));
9740 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
9741 p->ruby_sourceline, beg_pos, lineno, column);
9744 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
9745 const struct kwtable *kw;
9747 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
9748 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
9749 enforce_keyword_end = 1;
9755 if (mb == ENC_CODERANGE_7BIT && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
9756 const struct kwtable *kw;
9758 /* See if it is a reserved word. */
9759 kw = rb_reserved_word(tok(p), toklen(p));
9761 enum lex_state_e state = p->lex.state;
9762 if (IS_lex_state_for(state, EXPR_FNAME)) {
9763 SET_LEX_STATE(EXPR_ENDFN);
9764 set_yylval_name(rb_intern2(tok(p), toklen(p)));
9767 SET_LEX_STATE(kw->state);
9768 if (IS_lex_state(EXPR_BEG)) {
9769 p->command_start = TRUE;
9771 if (kw->id[0] == keyword_do) {
9772 if (lambda_beginning_p()) {
9773 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
9774 return keyword_do_LAMBDA;
9776 if (COND_P()) return keyword_do_cond;
9777 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
9778 return keyword_do_block;
9781 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
9784 if (kw->id[0] != kw->id[1])
9785 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
9791 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
9793 SET_LEX_STATE(EXPR_CMDARG);
9796 SET_LEX_STATE(EXPR_ARG);
9799 else if (p->lex.state == EXPR_FNAME) {
9800 SET_LEX_STATE(EXPR_ENDFN);
9803 SET_LEX_STATE(EXPR_END);
9806 ident = tokenize_ident(p, last_state);
9807 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
9808 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
9809 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
9810 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
9811 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
9817warn_cr(struct parser_params *p)
9821 /* carried over with p->lex.nextline for nextc() */
9822 rb_warn0("encountered \\r in middle of line, treated as a mere space");
9826static enum yytokentype
9827parser_yylex(struct parser_params *p)
9833 enum lex_state_e last_state;
9834 int fallthru = FALSE;
9835 int token_seen = p->token_seen;
9837 if (p->lex.strterm) {
9838 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
9840 return here_document(p, &p->lex.strterm->u.heredoc);
9844 return parse_string(p, &p->lex.strterm->u.literal);
9847 cmd_state = p->command_start;
9848 p->command_start = FALSE;
9849 p->token_seen = TRUE;
9854 last_state = p->lex.state;
9855 switch (c = nextc(p)) {
9856 case '\0': /* NUL */
9857 case '\004': /* ^D */
9858 case '\032': /* ^Z */
9859 case -1: /* end of script. */
9862 if (!NIL_P(p->end_expect_token_locations) && RARRAY_LEN(p->end_expect_token_locations) > 0) {
9863 pop_end_expect_token_locations(p);
9864 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
9868 /* Set location for end-of-input because dispatch_scan_event is not called. */
9869 RUBY_SET_YYLLOC(*p->yylloc);
9876 case ' ': case '\t': case '\f':
9877 case '\13': /* '\v' */
9879 while ((c = nextc(p))) {
9884 case ' ': case '\t': case '\f':
9885 case '\13': /* '\v' */
9893 dispatch_scan_event(p, tSP);
9899 case '#': /* it's a comment */
9900 p->token_seen = token_seen;
9901 /* no magic_comment in shebang line */
9902 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
9903 if (comment_at_top(p)) {
9904 set_file_encoding(p, p->lex.pcur, p->lex.pend);
9908 dispatch_scan_event(p, tCOMMENT);
9912 p->token_seen = token_seen;
9913 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
9914 !IS_lex_state(EXPR_LABELED));
9915 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
9917 dispatch_scan_event(p, tIGNORED_NL);
9920 if (!c && p->ctxt.in_kwarg) {
9921 goto normal_newline;
9926 switch (c = nextc(p)) {
9927 case ' ': case '\t': case '\f': case '\r':
9928 case '\13': /* '\v' */
9934 dispatch_scan_event(p, tSP);
9940 dispatch_delayed_token(p, tIGNORED_NL);
9941 if (peek(p, '.') == (c == '&')) {
9943 dispatch_scan_event(p, tSP);
9948 p->ruby_sourceline--;
9949 p->lex.nextline = p->lex.lastline;
9950 case -1: /* EOF no decrement*/
9953 p->lex.ptok = p->lex.pcur;
9955 goto normal_newline;
9959 p->command_start = TRUE;
9960 SET_LEX_STATE(EXPR_BEG);
9964 if ((c = nextc(p)) == '*') {
9965 if ((c = nextc(p)) == '=') {
9966 set_yylval_id(idPow);
9967 SET_LEX_STATE(EXPR_BEG);
9972 rb_warning0("`**' interpreted as argument prefix");
9975 else if (IS_BEG()) {
9979 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9985 SET_LEX_STATE(EXPR_BEG);
9990 rb_warning0("`*' interpreted as argument prefix");
9993 else if (IS_BEG()) {
9997 c = warn_balanced('*', "*", "argument prefix");
10000 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10005 if (IS_AFTER_OPERATOR()) {
10006 SET_LEX_STATE(EXPR_ARG);
10012 SET_LEX_STATE(EXPR_BEG);
10025 /* skip embedded rd document */
10026 if (word_match_p(p, "begin", 5)) {
10027 int first_p = TRUE;
10030 dispatch_scan_event(p, tEMBDOC_BEG);
10034 dispatch_scan_event(p, tEMBDOC);
10039 compile_error(p, "embedded document meets end of file");
10042 if (c == '=' && word_match_p(p, "end", 3)) {
10048 dispatch_scan_event(p, tEMBDOC_END);
10053 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10054 if ((c = nextc(p)) == '=') {
10055 if ((c = nextc(p)) == '=') {
10064 else if (c == '>') {
10073 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10075 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10076 int token = heredoc_identifier(p);
10077 if (token) return token < 0 ? 0 : token;
10079 if (IS_AFTER_OPERATOR()) {
10080 SET_LEX_STATE(EXPR_ARG);
10083 if (IS_lex_state(EXPR_CLASS))
10084 p->command_start = TRUE;
10085 SET_LEX_STATE(EXPR_BEG);
10088 if ((c = nextc(p)) == '>') {
10095 if ((c = nextc(p)) == '=') {
10096 set_yylval_id(idLTLT);
10097 SET_LEX_STATE(EXPR_BEG);
10101 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10107 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10108 if ((c = nextc(p)) == '=') {
10112 if ((c = nextc(p)) == '=') {
10113 set_yylval_id(idGTGT);
10114 SET_LEX_STATE(EXPR_BEG);
10124 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10125 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10126 p->lex.ptok = p->lex.pcur-1;
10127 return tSTRING_BEG;
10130 if (IS_lex_state(EXPR_FNAME)) {
10131 SET_LEX_STATE(EXPR_ENDFN);
10134 if (IS_lex_state(EXPR_DOT)) {
10136 SET_LEX_STATE(EXPR_CMDARG);
10138 SET_LEX_STATE(EXPR_ARG);
10141 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10142 return tXSTRING_BEG;
10145 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10146 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10147 p->lex.ptok = p->lex.pcur-1;
10148 return tSTRING_BEG;
10151 return parse_qmark(p, space_seen);
10154 if ((c = nextc(p)) == '&') {
10155 SET_LEX_STATE(EXPR_BEG);
10156 if ((c = nextc(p)) == '=') {
10157 set_yylval_id(idANDOP);
10158 SET_LEX_STATE(EXPR_BEG);
10164 else if (c == '=') {
10165 set_yylval_id('&');
10166 SET_LEX_STATE(EXPR_BEG);
10169 else if (c == '.') {
10170 set_yylval_id(idANDDOT);
10171 SET_LEX_STATE(EXPR_DOT);
10175 if (IS_SPCARG(c)) {
10177 (c = peekc_n(p, 1)) == -1 ||
10178 !(c == '\'' || c == '"' ||
10179 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
10180 rb_warning0("`&' interpreted as argument prefix");
10184 else if (IS_BEG()) {
10188 c = warn_balanced('&', "&", "argument prefix");
10190 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10194 if ((c = nextc(p)) == '|') {
10195 SET_LEX_STATE(EXPR_BEG);
10196 if ((c = nextc(p)) == '=') {
10197 set_yylval_id(idOROP);
10198 SET_LEX_STATE(EXPR_BEG);
10202 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10210 set_yylval_id('|');
10211 SET_LEX_STATE(EXPR_BEG);
10214 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10220 if (IS_AFTER_OPERATOR()) {
10221 SET_LEX_STATE(EXPR_ARG);
10229 set_yylval_id('+');
10230 SET_LEX_STATE(EXPR_BEG);
10233 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10234 SET_LEX_STATE(EXPR_BEG);
10236 if (c != -1 && ISDIGIT(c)) {
10237 return parse_numeric(p, '+');
10241 SET_LEX_STATE(EXPR_BEG);
10243 return warn_balanced('+', "+", "unary operator");
10247 if (IS_AFTER_OPERATOR()) {
10248 SET_LEX_STATE(EXPR_ARG);
10256 set_yylval_id('-');
10257 SET_LEX_STATE(EXPR_BEG);
10261 SET_LEX_STATE(EXPR_ENDFN);
10264 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
10265 SET_LEX_STATE(EXPR_BEG);
10267 if (c != -1 && ISDIGIT(c)) {
10268 return tUMINUS_NUM;
10272 SET_LEX_STATE(EXPR_BEG);
10274 return warn_balanced('-', "-", "unary operator");
10277 int is_beg = IS_BEG();
10278 SET_LEX_STATE(EXPR_BEG);
10279 if ((c = nextc(p)) == '.') {
10280 if ((c = nextc(p)) == '.') {
10281 if (p->ctxt.in_argdef) {
10282 SET_LEX_STATE(EXPR_ENDARG);
10285 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
10286 rb_warn0("... at EOL, should be parenthesized?");
10288 else if (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest) {
10289 if (IS_lex_state_for(last_state, EXPR_LABEL))
10292 return is_beg ? tBDOT3 : tDOT3;
10295 return is_beg ? tBDOT2 : tDOT2;
10298 if (c != -1 && ISDIGIT(c)) {
10299 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
10300 parse_numeric(p, '.');
10301 if (ISDIGIT(prev)) {
10302 yyerror0("unexpected fraction part after numeric literal");
10305 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
10307 SET_LEX_STATE(EXPR_END);
10308 p->lex.ptok = p->lex.pcur;
10311 set_yylval_id('.');
10312 SET_LEX_STATE(EXPR_DOT);
10316 case '0': case '1': case '2': case '3': case '4':
10317 case '5': case '6': case '7': case '8': case '9':
10318 return parse_numeric(p, c);
10323 SET_LEX_STATE(EXPR_ENDFN);
10324 p->lex.paren_nest--;
10330 SET_LEX_STATE(EXPR_END);
10331 p->lex.paren_nest--;
10335 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
10336 if (!p->lex.brace_nest--) return tSTRING_DEND;
10339 SET_LEX_STATE(EXPR_END);
10340 p->lex.paren_nest--;
10346 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
10347 SET_LEX_STATE(EXPR_BEG);
10350 set_yylval_id(idCOLON2);
10351 SET_LEX_STATE(EXPR_DOT);
10354 if (IS_END() || ISSPACE(c) || c == '#') {
10356 c = warn_balanced(':', ":", "symbol literal");
10357 SET_LEX_STATE(EXPR_BEG);
10362 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
10365 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
10371 SET_LEX_STATE(EXPR_FNAME);
10376 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10377 return tREGEXP_BEG;
10379 if ((c = nextc(p)) == '=') {
10380 set_yylval_id('/');
10381 SET_LEX_STATE(EXPR_BEG);
10385 if (IS_SPCARG(c)) {
10386 arg_ambiguous(p, '/');
10387 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
10388 return tREGEXP_BEG;
10390 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10391 return warn_balanced('/', "/", "regexp literal");
10394 if ((c = nextc(p)) == '=') {
10395 set_yylval_id('^');
10396 SET_LEX_STATE(EXPR_BEG);
10399 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10404 SET_LEX_STATE(EXPR_BEG);
10405 p->command_start = TRUE;
10409 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10413 if (IS_AFTER_OPERATOR()) {
10414 if ((c = nextc(p)) != '@') {
10417 SET_LEX_STATE(EXPR_ARG);
10420 SET_LEX_STATE(EXPR_BEG);
10428 else if (!space_seen) {
10429 /* foo( ... ) => method call, no ambiguity */
10431 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
10434 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
10435 rb_warning0("parentheses after method name is interpreted as "
10436 "an argument list, not a decomposed argument");
10438 p->lex.paren_nest++;
10441 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10445 p->lex.paren_nest++;
10446 if (IS_AFTER_OPERATOR()) {
10447 if ((c = nextc(p)) == ']') {
10448 p->lex.paren_nest--;
10449 SET_LEX_STATE(EXPR_ARG);
10450 if ((c = nextc(p)) == '=') {
10457 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
10460 else if (IS_BEG()) {
10463 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
10466 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10472 ++p->lex.brace_nest;
10473 if (lambda_beginning_p())
10475 else if (IS_lex_state(EXPR_LABELED))
10476 c = tLBRACE; /* hash */
10477 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
10478 c = '{'; /* block (primary) */
10479 else if (IS_lex_state(EXPR_ENDARG))
10480 c = tLBRACE_ARG; /* block (expr) */
10482 c = tLBRACE; /* hash */
10483 if (c != tLBRACE) {
10484 p->command_start = TRUE;
10485 SET_LEX_STATE(EXPR_BEG);
10488 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
10490 ++p->lex.paren_nest; /* after lambda_beginning_p() */
10499 dispatch_scan_event(p, tSP);
10500 goto retry; /* skip \\n */
10502 if (c == ' ') return tSP;
10503 if (ISSPACE(c)) return c;
10508 return parse_percent(p, space_seen, last_state);
10511 return parse_gvar(p, last_state);
10514 return parse_atmark(p, last_state);
10517 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
10518 p->ruby__end__seen = 1;
10524 dispatch_scan_event(p, k__END__);
10532 if (!parser_is_identchar(p)) {
10533 compile_error(p, "Invalid char `\\x%02X' in expression", c);
10542 return parse_ident(p, c, cmd_state);
10545static enum yytokentype
10546yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
10548 enum yytokentype t;
10551 lval->val = Qundef;
10552 p->yylloc = yylloc;
10554 t = parser_yylex(p);
10556 if (has_delayed_token(p))
10557 dispatch_delayed_token(p, t);
10559 dispatch_scan_event(p, t);
10564#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
10567node_new_temporal(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
10569 NODE *n = rb_ast_newnode(p->ast, type);
10571 rb_node_init(n, type, a0, a1, a2);
10576node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
10578 NODE *n = node_new_temporal(p, type, a0, a1, a2);
10580 nd_set_loc(n, loc);
10581 nd_set_node_id(n, parser_get_node_id(p));
10586nd_set_loc(NODE *nd, const YYLTYPE *loc)
10589 nd_set_line(nd, loc->beg_pos.lineno);
10594static enum node_type
10595nodetype(NODE *node) /* for debug */
10597 return (enum node_type)nd_type(node);
10601nodeline(NODE *node)
10603 return nd_line(node);
10607newline_node(NODE *node)
10610 node = remove_begin(node);
10611 node->flags |= NODE_FL_NEWLINE;
10617fixpos(NODE *node, NODE *orig)
10621 nd_set_line(node, nd_line(orig));
10625parser_warning(struct parser_params *p, NODE *node, const char *mesg)
10627 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10631parser_warn(struct parser_params *p, NODE *node, const char *mesg)
10633 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
10637block_append(struct parser_params *p, NODE *head, NODE *tail)
10639 NODE *end, *h = head, *nd;
10641 if (tail == 0) return head;
10643 if (h == 0) return tail;
10644 switch (nd_type(h)) {
10651 parser_warning(p, h, "unused literal ignored");
10654 h = end = NEW_BLOCK(head, &head->nd_loc);
10664 switch (nd_type(nd)) {
10670 if (RTEST(ruby_verbose)) {
10671 parser_warning(p, tail, "statement not reached");
10679 if (!nd_type_p(tail, NODE_BLOCK)) {
10680 tail = NEW_BLOCK(tail, &tail->nd_loc);
10681 tail->nd_end = tail;
10683 end->nd_next = tail;
10684 h->nd_end = tail->nd_end;
10685 nd_set_last_loc(head, nd_last_loc(tail));
10689/* append item to the list */
10691list_append(struct parser_params *p, NODE *list, NODE *item)
10695 if (list == 0) return NEW_LIST(item, &item->nd_loc);
10696 if (list->nd_next) {
10697 last = list->nd_next->nd_end;
10703 list->nd_alen += 1;
10704 last->nd_next = NEW_LIST(item, &item->nd_loc);
10705 list->nd_next->nd_end = last->nd_next;
10707 nd_set_last_loc(list, nd_last_loc(item));
10712/* concat two lists */
10714list_concat(NODE *head, NODE *tail)
10718 if (head->nd_next) {
10719 last = head->nd_next->nd_end;
10725 head->nd_alen += tail->nd_alen;
10726 last->nd_next = tail;
10727 if (tail->nd_next) {
10728 head->nd_next->nd_end = tail->nd_next->nd_end;
10731 head->nd_next->nd_end = tail;
10734 nd_set_last_loc(head, nd_last_loc(tail));
10740literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
10742 if (NIL_P(tail)) return 1;
10743 if (!rb_enc_compatible(head, tail)) {
10744 compile_error(p, "string literal encodings differ (%s / %s)",
10745 rb_enc_name(rb_enc_get(head)),
10746 rb_enc_name(rb_enc_get(tail)));
10747 rb_str_resize(head, 0);
10748 rb_str_resize(tail, 0);
10751 rb_str_buf_append(head, tail);
10756string_literal_head(enum node_type htype, NODE *head)
10758 if (htype != NODE_DSTR) return Qfalse;
10759 if (head->nd_next) {
10760 head = head->nd_next->nd_end->nd_head;
10761 if (!head || !nd_type_p(head, NODE_STR)) return Qfalse;
10763 const VALUE lit = head->nd_lit;
10764 ASSUME(lit != Qfalse);
10768/* concat two string literals */
10770literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
10772 enum node_type htype;
10775 if (!head) return tail;
10776 if (!tail) return head;
10778 htype = nd_type(head);
10779 if (htype == NODE_EVSTR) {
10780 head = new_dstr(p, head, loc);
10783 if (p->heredoc_indent > 0) {
10786 nd_set_type(head, NODE_DSTR);
10788 return list_append(p, head, tail);
10793 switch (nd_type(tail)) {
10795 if ((lit = string_literal_head(htype, head)) != Qfalse) {
10799 lit = head->nd_lit;
10801 if (htype == NODE_STR) {
10802 if (!literal_concat0(p, lit, tail->nd_lit)) {
10804 rb_discard_node(p, head);
10805 rb_discard_node(p, tail);
10808 rb_discard_node(p, tail);
10811 list_append(p, head, tail);
10816 if (htype == NODE_STR) {
10817 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
10819 tail->nd_lit = head->nd_lit;
10820 rb_discard_node(p, head);
10823 else if (NIL_P(tail->nd_lit)) {
10825 head->nd_alen += tail->nd_alen - 1;
10826 if (!head->nd_next) {
10827 head->nd_next = tail->nd_next;
10829 else if (tail->nd_next) {
10830 head->nd_next->nd_end->nd_next = tail->nd_next;
10831 head->nd_next->nd_end = tail->nd_next->nd_end;
10833 rb_discard_node(p, tail);
10835 else if ((lit = string_literal_head(htype, head)) != Qfalse) {
10836 if (!literal_concat0(p, lit, tail->nd_lit))
10838 tail->nd_lit = Qnil;
10842 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
10847 if (htype == NODE_STR) {
10848 nd_set_type(head, NODE_DSTR);
10851 list_append(p, head, tail);
10858evstr2dstr(struct parser_params *p, NODE *node)
10860 if (nd_type_p(node, NODE_EVSTR)) {
10861 node = new_dstr(p, node, &node->nd_loc);
10867new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10872 switch (nd_type(node)) {
10874 nd_set_type(node, NODE_DSTR);
10882 return NEW_EVSTR(head, loc);
10886new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10888 VALUE lit = STR_NEW0();
10889 NODE *dstr = NEW_DSTR(lit, loc);
10890 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10891 return list_append(p, dstr, node);
10895call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
10896 const YYLTYPE *op_loc, const YYLTYPE *loc)
10901 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
10902 nd_set_line(expr, op_loc->beg_pos.lineno);
10907call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
10911 opcall = NEW_OPCALL(recv, id, 0, loc);
10912 nd_set_line(opcall, op_loc->beg_pos.lineno);
10917new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
10919 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
10920 nd_set_line(qcall, op_loc->beg_pos.lineno);
10925new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
10928 if (block) block_dup_check(p, args, block);
10929 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
10930 if (block) ret = method_add_block(p, ret, block, loc);
10935#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? (node)->nd_body : node)
10937match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
10940 int line = op_loc->beg_pos.lineno;
10944 if (node1 && (n = nd_once_body(node1)) != 0) {
10945 switch (nd_type(n)) {
10948 NODE *match = NEW_MATCH2(node1, node2, loc);
10949 nd_set_line(match, line);
10954 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
10955 const VALUE lit = n->nd_lit;
10956 NODE *match = NEW_MATCH2(node1, node2, loc);
10957 match->nd_args = reg_named_capture_assign(p, lit, loc);
10958 nd_set_line(match, line);
10964 if (node2 && (n = nd_once_body(node2)) != 0) {
10967 switch (nd_type(n)) {
10969 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
10972 match3 = NEW_MATCH3(node2, node1, loc);
10977 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
10978 nd_set_line(n, line);
10982# if WARN_PAST_SCOPE
10984past_dvar_p(struct parser_params *p, ID id)
10986 struct vtable *past = p->lvtbl->past;
10988 if (vtable_included(past, id)) return 1;
10996numparam_nested_p(struct parser_params *p)
10998 struct local_vars *local = p->lvtbl;
10999 NODE *outer = local->numparam.outer;
11000 NODE *inner = local->numparam.inner;
11001 if (outer || inner) {
11002 NODE *used = outer ? outer : inner;
11003 compile_error(p, "numbered parameter is already used in\n"
11004 "%s:%d: %s block here",
11005 p->ruby_sourcefile, nd_line(used),
11006 outer ? "outer" : "inner");
11007 parser_show_error_line(p, &used->nd_loc);
11014gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
11020 return NEW_SELF(loc);
11022 return NEW_NIL(loc);
11024 return NEW_TRUE(loc);
11025 case keyword_false:
11026 return NEW_FALSE(loc);
11027 case keyword__FILE__:
11029 VALUE file = p->ruby_sourcefile_string;
11031 file = rb_str_new(0, 0);
11033 file = rb_str_dup(file);
11034 node = NEW_STR(file, loc);
11035 RB_OBJ_WRITTEN(p->ast, Qnil, file);
11038 case keyword__LINE__:
11039 return NEW_LIT(INT2FIX(p->tokline), loc);
11040 case keyword__ENCODING__:
11041 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
11042 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11046 switch (id_type(id)) {
11048 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
11049 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
11050 if (id == p->cur_arg) {
11051 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11054 if (vidp) *vidp |= LVAR_USED;
11055 node = NEW_DVAR(id, loc);
11058 if (local_id_ref(p, id, &vidp)) {
11059 if (id == p->cur_arg) {
11060 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
11063 if (vidp) *vidp |= LVAR_USED;
11064 node = NEW_LVAR(id, loc);
11067 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
11068 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
11069 if (numparam_nested_p(p)) return 0;
11070 node = NEW_DVAR(id, loc);
11071 struct local_vars *local = p->lvtbl;
11072 if (!local->numparam.current) local->numparam.current = node;
11075# if WARN_PAST_SCOPE
11076 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
11077 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
11080 /* method call without arguments */
11081 return NEW_VCALL(id, loc);
11083 return NEW_GVAR(id, loc);
11085 return NEW_IVAR(id, loc);
11087 return NEW_CONST(id, loc);
11089 return NEW_CVAR(id, loc);
11091 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11096opt_arg_append(NODE *opt_list, NODE *opt)
11098 NODE *opts = opt_list;
11099 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11101 while (opts->nd_next) {
11102 opts = opts->nd_next;
11103 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
11105 opts->nd_next = opt;
11111kwd_append(NODE *kwlist, NODE *kw)
11114 opt_arg_append(kwlist, kw);
11120new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
11122 return NEW_DEFINED(remove_begin_all(expr), loc);
11126symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
11128 enum node_type type = nd_type(symbol);
11131 nd_set_type(symbol, NODE_DSYM);
11134 nd_set_type(symbol, NODE_LIT);
11135 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
11138 compile_error(p, "unexpected node as symbol: %s", ruby_node_name(type));
11140 return list_append(p, symbols, symbol);
11144new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
11150 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
11151 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
11154 switch (nd_type(node)) {
11157 VALUE src = node->nd_lit;
11158 nd_set_type(node, NODE_LIT);
11159 nd_set_loc(node, loc);
11160 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11165 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
11166 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11169 nd_set_type(node, NODE_DREGX);
11170 nd_set_loc(node, loc);
11171 node->nd_cflag = options & RE_OPTION_MASK;
11172 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
11173 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
11174 NODE *frag = list->nd_head;
11175 enum node_type type = nd_type(frag);
11176 if (type == NODE_STR || (type == NODE_DSTR && !frag->nd_next)) {
11177 VALUE tail = frag->nd_lit;
11178 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
11179 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
11180 if (!literal_concat0(p, lit, tail)) {
11181 return NEW_NIL(loc); /* dummy node on error */
11183 rb_str_resize(tail, 0);
11184 prev->nd_next = list->nd_next;
11185 rb_discard_node(p, list->nd_head);
11186 rb_discard_node(p, list);
11197 if (!node->nd_next) {
11198 VALUE src = node->nd_lit;
11199 nd_set_type(node, NODE_LIT);
11200 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
11202 if (options & RE_OPTION_ONCE) {
11203 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
11211new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
11214 return NEW_KW_ARG(0, (k), loc);
11218new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11221 VALUE lit = STR_NEW0();
11222 NODE *xstr = NEW_XSTR(lit, loc);
11223 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
11226 switch (nd_type(node)) {
11228 nd_set_type(node, NODE_XSTR);
11229 nd_set_loc(node, loc);
11232 nd_set_type(node, NODE_DXSTR);
11233 nd_set_loc(node, loc);
11236 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
11243check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
11247 if (!arg || !p->case_labels) return;
11249 lit = rb_node_case_when_optimizable_literal(arg);
11250 if (UNDEF_P(lit)) return;
11251 if (nd_type_p(arg, NODE_STR)) {
11252 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
11255 if (NIL_P(p->case_labels)) {
11256 p->case_labels = rb_obj_hide(rb_hash_new());
11259 VALUE line = rb_hash_lookup(p->case_labels, lit);
11260 if (!NIL_P(line)) {
11261 rb_warning1("duplicated `when' clause with line %d is ignored",
11266 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
11271id_is_var(struct parser_params *p, ID id)
11273 if (is_notop_id(id)) {
11274 switch (id & ID_SCOPE_MASK) {
11275 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
11278 if (dyna_in_block(p)) {
11279 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
11281 if (local_id(p, id)) return 1;
11282 /* method call without arguments */
11286 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
11291new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
11293 VALUE src = 0, err;
11295 if (ripper_is_node_yylval(re)) {
11296 src = RNODE(re)->nd_cval;
11297 re = RNODE(re)->nd_rval;
11299 if (ripper_is_node_yylval(opt)) {
11300 options = (int)RNODE(opt)->nd_tag;
11301 opt = RNODE(opt)->nd_rval;
11303 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
11304 compile_error(p, "%"PRIsVALUE, err);
11306 return dispatch2(regexp_literal, re, opt);
11308#endif /* !RIPPER */
11310static inline enum lex_state_e
11311parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
11314 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
11316 return p->lex.state = ls;
11320static const char rb_parser_lex_state_names[][8] = {
11321 "BEG", "END", "ENDARG", "ENDFN", "ARG",
11322 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
11323 "LABEL", "LABELED","FITEM",
11327append_lex_state_name(enum lex_state_e state, VALUE buf)
11330 unsigned int mask = 1;
11331 static const char none[] = "NONE";
11333 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
11334 if ((unsigned)state & mask) {
11336 rb_str_cat(buf, "|", 1);
11339 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
11343 rb_str_cat(buf, none, sizeof(none)-1);
11349flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
11351 VALUE mesg = p->debug_buffer;
11353 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
11354 p->debug_buffer = Qnil;
11355 rb_io_puts(1, &mesg, out);
11357 if (!NIL_P(str) && RSTRING_LEN(str)) {
11358 rb_io_write(p->debug_output, str);
11363rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
11364 enum lex_state_e to, int line)
11367 mesg = rb_str_new_cstr("lex_state: ");
11368 append_lex_state_name(from, mesg);
11369 rb_str_cat_cstr(mesg, " -> ");
11370 append_lex_state_name(to, mesg);
11371 rb_str_catf(mesg, " at line %d\n", line);
11372 flush_debug_buffer(p, p->debug_output, mesg);
11377rb_parser_lex_state_name(enum lex_state_e state)
11379 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
11383append_bitstack_value(stack_type stack, VALUE mesg)
11386 rb_str_cat_cstr(mesg, "0");
11389 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
11390 for (; mask && !(stack & mask); mask >>= 1) continue;
11391 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
11396rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
11397 const char *name, int line)
11399 VALUE mesg = rb_sprintf("%s: ", name);
11400 append_bitstack_value(stack, mesg);
11401 rb_str_catf(mesg, " at line %d\n", line);
11402 flush_debug_buffer(p, p->debug_output, mesg);
11406rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
11409 VALUE mesg = rb_str_new_cstr("internal parser error: ");
11412 rb_str_vcatf(mesg, fmt, ap);
11414 yyerror0(RSTRING_PTR(mesg));
11417 mesg = rb_str_new(0, 0);
11418 append_lex_state_name(p->lex.state, mesg);
11419 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
11420 rb_str_resize(mesg, 0);
11421 append_bitstack_value(p->cond_stack, mesg);
11422 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
11423 rb_str_resize(mesg, 0);
11424 append_bitstack_value(p->cmdarg_stack, mesg);
11425 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
11426 if (p->debug_output == rb_ractor_stdout())
11427 p->debug_output = rb_ractor_stderr();
11432rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
11434 yylloc->beg_pos.lineno = sourceline;
11435 yylloc->beg_pos.column = beg_pos;
11436 yylloc->end_pos.lineno = sourceline;
11437 yylloc->end_pos.column = end_pos;
11442rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
11444 int sourceline = here->sourceline;
11445 int beg_pos = (int)here->offset - here->quote
11446 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
11447 int end_pos = (int)here->offset + here->length + here->quote;
11449 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11453rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
11455 yylloc->beg_pos.lineno = p->delayed.beg_line;
11456 yylloc->beg_pos.column = p->delayed.beg_col;
11457 yylloc->end_pos.lineno = p->delayed.end_line;
11458 yylloc->end_pos.column = p->delayed.end_col;
11464rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
11466 int sourceline = p->ruby_sourceline;
11467 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11468 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
11469 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11473rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
11475 yylloc->end_pos = yylloc->beg_pos;
11481rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
11483 int sourceline = p->ruby_sourceline;
11484 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11485 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
11486 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11490rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
11492 int sourceline = p->ruby_sourceline;
11493 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
11494 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
11495 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
11497#endif /* !RIPPER */
11500assignable0(struct parser_params *p, ID id, const char **err)
11502 if (!id) return -1;
11505 *err = "Can't change the value of self";
11508 *err = "Can't assign to nil";
11511 *err = "Can't assign to true";
11513 case keyword_false:
11514 *err = "Can't assign to false";
11516 case keyword__FILE__:
11517 *err = "Can't assign to __FILE__";
11519 case keyword__LINE__:
11520 *err = "Can't assign to __LINE__";
11522 case keyword__ENCODING__:
11523 *err = "Can't assign to __ENCODING__";
11526 switch (id_type(id)) {
11528 if (dyna_in_block(p)) {
11529 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
11530 compile_error(p, "Can't assign to numbered parameter _%d",
11531 NUMPARAM_ID_TO_IDX(id));
11534 if (dvar_curr(p, id)) return NODE_DASGN;
11535 if (dvar_defined(p, id)) return NODE_DASGN;
11536 if (local_id(p, id)) return NODE_LASGN;
11541 if (!local_id(p, id)) local_var(p, id);
11545 case ID_GLOBAL: return NODE_GASGN;
11546 case ID_INSTANCE: return NODE_IASGN;
11548 if (!p->ctxt.in_def) return NODE_CDECL;
11549 *err = "dynamic constant assignment";
11551 case ID_CLASS: return NODE_CVASGN;
11553 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
11560assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
11562 const char *err = 0;
11563 int node_type = assignable0(p, id, &err);
11564 switch (node_type) {
11565 case NODE_DASGN: return NEW_DASGN(id, val, loc);
11566 case NODE_LASGN: return NEW_LASGN(id, val, loc);
11567 case NODE_GASGN: return NEW_GASGN(id, val, loc);
11568 case NODE_IASGN: return NEW_IASGN(id, val, loc);
11569 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
11570 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
11572 if (err) yyerror1(loc, err);
11573 return NEW_BEGIN(0, loc);
11577assignable(struct parser_params *p, VALUE lhs)
11579 const char *err = 0;
11580 assignable0(p, get_id(lhs), &err);
11581 if (err) lhs = assign_error(p, err, lhs);
11587is_private_local_id(ID name)
11590 if (name == idUScore) return 1;
11591 if (!is_local_id(name)) return 0;
11592 s = rb_id2str(name);
11594 return RSTRING_PTR(s)[0] == '_';
11598shadowing_lvar_0(struct parser_params *p, ID name)
11600 if (dyna_in_block(p)) {
11601 if (dvar_curr(p, name)) {
11602 if (is_private_local_id(name)) return 1;
11603 yyerror0("duplicated argument name");
11605 else if (dvar_defined(p, name) || local_id(p, name)) {
11606 vtable_add(p->lvtbl->vars, name);
11607 if (p->lvtbl->used) {
11608 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
11614 if (local_id(p, name)) {
11615 if (is_private_local_id(name)) return 1;
11616 yyerror0("duplicated argument name");
11623shadowing_lvar(struct parser_params *p, ID name)
11625 shadowing_lvar_0(p, name);
11630new_bv(struct parser_params *p, ID name)
11633 if (!is_local_id(name)) {
11634 compile_error(p, "invalid local variable - %"PRIsVALUE,
11638 if (!shadowing_lvar_0(p, name)) return;
11644aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
11646 return NEW_ATTRASGN(recv, tASET, idx, loc);
11650block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
11652 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
11653 compile_error(p, "both block arg and actual block given");
11658attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
11660 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
11661 return NEW_ATTRASGN(recv, id, 0, loc);
11665rb_backref_error(struct parser_params *p, NODE *node)
11667 switch (nd_type(node)) {
11669 compile_error(p, "Can't set variable $%ld", node->nd_nth);
11671 case NODE_BACK_REF:
11672 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
11678backref_error(struct parser_params *p, NODE *ref, VALUE expr)
11680 VALUE mesg = rb_str_new_cstr("Can't set variable ");
11681 rb_str_append(mesg, ref->nd_cval);
11682 return dispatch2(assign_error, mesg, expr);
11688arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11690 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
11691 switch (nd_type(node1)) {
11693 return list_append(p, node1, node2);
11694 case NODE_BLOCK_PASS:
11695 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
11696 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
11698 case NODE_ARGSPUSH:
11699 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
11700 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11701 nd_set_type(node1, NODE_ARGSCAT);
11704 if (!nd_type_p(node1->nd_body, NODE_LIST)) break;
11705 node1->nd_body = list_append(p, node1->nd_body, node2);
11706 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
11709 return NEW_ARGSPUSH(node1, node2, loc);
11713arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
11715 if (!node2) return node1;
11716 switch (nd_type(node1)) {
11717 case NODE_BLOCK_PASS:
11718 if (node1->nd_head)
11719 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
11721 node1->nd_head = NEW_LIST(node2, loc);
11723 case NODE_ARGSPUSH:
11724 if (!nd_type_p(node2, NODE_LIST)) break;
11725 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
11726 nd_set_type(node1, NODE_ARGSCAT);
11729 if (!nd_type_p(node2, NODE_LIST) ||
11730 !nd_type_p(node1->nd_body, NODE_LIST)) break;
11731 node1->nd_body = list_concat(node1->nd_body, node2);
11734 return NEW_ARGSCAT(node1, node2, loc);
11738last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
11741 if ((n1 = splat_array(args)) != 0) {
11742 return list_append(p, n1, last_arg);
11744 return arg_append(p, args, last_arg, loc);
11748rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
11751 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
11752 return list_concat(n1, rest_arg);
11754 return arg_concat(p, args, rest_arg, loc);
11758splat_array(NODE* node)
11760 if (nd_type_p(node, NODE_SPLAT)) node = node->nd_head;
11761 if (nd_type_p(node, NODE_LIST)) return node;
11766mark_lvar_used(struct parser_params *p, NODE *rhs)
11770 switch (nd_type(rhs)) {
11772 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
11773 if (vidp) *vidp |= LVAR_USED;
11777 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
11778 if (vidp) *vidp |= LVAR_USED;
11783 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
11784 mark_lvar_used(p, rhs->nd_head);
11792const_decl_path(struct parser_params *p, NODE **dest)
11795 if (!nd_type_p(n, NODE_CALL)) {
11796 const YYLTYPE *loc = &n->nd_loc;
11799 path = rb_id2str(n->nd_vid);
11803 path = rb_ary_new();
11804 for (; n && nd_type_p(n, NODE_COLON2); n = n->nd_head) {
11805 rb_ary_push(path, rb_id2str(n->nd_mid));
11807 if (n && nd_type_p(n, NODE_CONST)) {
11809 rb_ary_push(path, rb_id2str(n->nd_vid));
11811 else if (n && nd_type_p(n, NODE_COLON3)) {
11813 rb_ary_push(path, rb_str_new(0, 0));
11816 // expression::Name
11817 rb_ary_push(path, rb_str_new_cstr("..."));
11819 path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
11820 path = rb_fstring(path);
11822 *dest = n = NEW_LIT(path, loc);
11823 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11828extern VALUE rb_mRubyVMFrozenCore;
11831make_shareable_node(struct parser_params *p, NODE *value, bool copy, const YYLTYPE *loc)
11833 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11836 return NEW_CALL(fcore, rb_intern("make_shareable_copy"),
11837 NEW_LIST(value, loc), loc);
11840 return NEW_CALL(fcore, rb_intern("make_shareable"),
11841 NEW_LIST(value, loc), loc);
11846ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const YYLTYPE *loc)
11848 NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
11849 NODE *args = NEW_LIST(value, loc);
11850 args = list_append(p, args, const_decl_path(p, dest));
11851 return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
11854static int is_static_content(NODE *node);
11857shareable_literal_value(NODE *node)
11859 if (!node) return Qnil;
11860 enum node_type type = nd_type(node);
11869 return node->nd_lit;
11875#ifndef SHAREABLE_BARE_EXPRESSION
11876#define SHAREABLE_BARE_EXPRESSION 1
11880shareable_literal_constant(struct parser_params *p, enum shareability shareable,
11881 NODE **dest, NODE *value, const YYLTYPE *loc, size_t level)
11883# define shareable_literal_constant_next(n) \
11884 shareable_literal_constant(p, shareable, dest, (n), &(n)->nd_loc, level+1)
11887 if (!value) return 0;
11888 enum node_type type = nd_type(value);
11897 if (shareable == shareable_literal) {
11898 value = NEW_CALL(value, idUMinus, 0, loc);
11903 lit = rb_fstring(value->nd_lit);
11904 nd_set_type(value, NODE_LIT);
11905 RB_OBJ_WRITE(p->ast, &value->nd_lit, lit);
11909 lit = rb_ary_new();
11910 OBJ_FREEZE_RAW(lit);
11911 NODE *n = NEW_LIT(lit, loc);
11912 RB_OBJ_WRITTEN(p->ast, Qnil, n->nd_lit);
11916 lit = rb_ary_new();
11917 for (NODE *n = value; n; n = n->nd_next) {
11918 NODE *elt = n->nd_head;
11920 elt = shareable_literal_constant_next(elt);
11924 else if (RTEST(lit)) {
11930 VALUE e = shareable_literal_value(elt);
11932 rb_ary_push(lit, e);
11936 lit = Qnil; /* make shareable at runtime */
11943 if (!value->nd_brace) return 0;
11944 lit = rb_hash_new();
11945 for (NODE *n = value->nd_head; n; n = n->nd_next->nd_next) {
11946 NODE *key = n->nd_head;
11947 NODE *val = n->nd_next->nd_head;
11949 key = shareable_literal_constant_next(key);
11953 else if (RTEST(lit)) {
11954 rb_hash_clear(lit);
11959 val = shareable_literal_constant_next(val);
11961 n->nd_next->nd_head = val;
11963 else if (RTEST(lit)) {
11964 rb_hash_clear(lit);
11969 VALUE k = shareable_literal_value(key);
11970 VALUE v = shareable_literal_value(val);
11971 if (!UNDEF_P(k) && !UNDEF_P(v)) {
11972 rb_hash_aset(lit, k, v);
11975 rb_hash_clear(lit);
11976 lit = Qnil; /* make shareable at runtime */
11983 if (shareable == shareable_literal &&
11984 (SHAREABLE_BARE_EXPRESSION || level > 0)) {
11985 return ensure_shareable_node(p, dest, value, loc);
11990 /* Array or Hash */
11991 if (!lit) return 0;
11993 // if shareable_literal, all elements should have been ensured
11995 value = make_shareable_node(p, value, false, loc);
11998 value = NEW_LIT(rb_ractor_make_shareable(lit), loc);
11999 RB_OBJ_WRITTEN(p->ast, Qnil, value->nd_lit);
12003# undef shareable_literal_constant_next
12007shareable_constant_value(struct parser_params *p, enum shareability shareable,
12008 NODE *lhs, NODE *value, const YYLTYPE *loc)
12010 if (!value) return 0;
12011 switch (shareable) {
12012 case shareable_none:
12015 case shareable_literal:
12017 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
12018 if (lit) return lit;
12023 case shareable_copy:
12024 case shareable_everything:
12026 NODE *lit = shareable_literal_constant(p, shareable, &lhs, value, loc, 0);
12027 if (lit) return lit;
12028 return make_shareable_node(p, value, shareable == shareable_copy, loc);
12033 UNREACHABLE_RETURN(0);
12038node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
12040 if (!lhs) return 0;
12042 switch (nd_type(lhs)) {
12044 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
12053 lhs->nd_value = rhs;
12054 nd_set_loc(lhs, loc);
12057 case NODE_ATTRASGN:
12058 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
12059 nd_set_loc(lhs, loc);
12063 /* should not happen */
12071value_expr_check(struct parser_params *p, NODE *node)
12073 NODE *void_node = 0, *vn;
12076 rb_warning0("empty expression");
12079 switch (nd_type(node)) {
12085 return void_node ? void_node : node;
12088 if (!node->nd_body || !nd_type_p(node->nd_body, NODE_IN)) {
12089 compile_error(p, "unexpected node");
12092 if (node->nd_body->nd_body) {
12095 /* single line pattern matching */
12096 return void_node ? void_node : node;
12099 while (node->nd_next) {
12100 node = node->nd_next;
12102 node = node->nd_head;
12106 node = node->nd_body;
12111 if (!node->nd_body) {
12114 else if (!node->nd_else) {
12117 vn = value_expr_check(p, node->nd_body);
12118 if (!vn) return NULL;
12119 if (!void_node) void_node = vn;
12120 node = node->nd_else;
12125 node = node->nd_1st;
12131 mark_lvar_used(p, node);
12143value_expr_gen(struct parser_params *p, NODE *node)
12145 NODE *void_node = value_expr_check(p, node);
12147 yyerror1(&void_node->nd_loc, "void value expression");
12148 /* or "control never reach"? */
12154void_expr(struct parser_params *p, NODE *node)
12156 const char *useless = 0;
12158 if (!RTEST(ruby_verbose)) return;
12160 if (!node || !(node = nd_once_body(node))) return;
12161 switch (nd_type(node)) {
12163 switch (node->nd_mid) {
12182 useless = rb_id2name(node->nd_mid);
12193 case NODE_BACK_REF:
12194 useless = "a variable";
12197 useless = "a constant";
12203 useless = "a literal";
12228 useless = "defined?";
12233 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
12238void_stmts(struct parser_params *p, NODE *node)
12240 NODE *const n = node;
12241 if (!RTEST(ruby_verbose)) return n;
12242 if (!node) return n;
12243 if (!nd_type_p(node, NODE_BLOCK)) return n;
12245 while (node->nd_next) {
12246 void_expr(p, node->nd_head);
12247 node = node->nd_next;
12253remove_begin(NODE *node)
12255 NODE **n = &node, *n1 = node;
12256 while (n1 && nd_type_p(n1, NODE_BEGIN) && n1->nd_body) {
12257 *n = n1 = n1->nd_body;
12263remove_begin_all(NODE *node)
12265 NODE **n = &node, *n1 = node;
12266 while (n1 && nd_type_p(n1, NODE_BEGIN)) {
12267 *n = n1 = n1->nd_body;
12273reduce_nodes(struct parser_params *p, NODE **body)
12275 NODE *node = *body;
12278 *body = NEW_NIL(&NULL_LOC);
12281#define subnodes(n1, n2) \
12282 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
12283 (!node->n2) ? (body = &node->n1, 1) : \
12284 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
12287 int newline = (int)(node->flags & NODE_FL_NEWLINE);
12288 switch (nd_type(node)) {
12294 *body = node = node->nd_stts;
12295 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12298 *body = node = node->nd_body;
12299 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12302 body = &node->nd_end->nd_head;
12306 if (subnodes(nd_body, nd_else)) break;
12309 body = &node->nd_body;
12312 if (!subnodes(nd_body, nd_next)) goto end;
12315 if (!subnodes(nd_head, nd_resq)) goto end;
12318 if (node->nd_else) {
12319 body = &node->nd_resq;
12322 if (!subnodes(nd_head, nd_resq)) goto end;
12328 if (newline && node) node->flags |= NODE_FL_NEWLINE;
12335is_static_content(NODE *node)
12337 if (!node) return 1;
12338 switch (nd_type(node)) {
12340 if (!(node = node->nd_head)) break;
12343 if (!is_static_content(node->nd_head)) return 0;
12344 } while ((node = node->nd_next) != 0);
12359assign_in_cond(struct parser_params *p, NODE *node)
12361 switch (nd_type(node)) {
12373 if (!node->nd_value) return 1;
12374 if (is_static_content(node->nd_value)) {
12375 /* reports always */
12376 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
12387#define SWITCH_BY_COND_TYPE(t, w, arg) \
12389 case COND_IN_OP: break; \
12390 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
12391 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
12394static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
12397range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12399 enum node_type type;
12401 if (node == 0) return 0;
12403 type = nd_type(node);
12405 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
12406 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
12407 ID lineno = rb_intern("$.");
12408 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
12410 return cond0(p, node, COND_IN_FF, loc);
12414cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
12416 if (node == 0) return 0;
12417 if (!(node = nd_once_body(node))) return 0;
12418 assign_in_cond(p, node);
12420 switch (nd_type(node)) {
12424 SWITCH_BY_COND_TYPE(type, warn, "string ")
12428 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
12430 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
12434 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
12435 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
12440 node->nd_beg = range_op(p, node->nd_beg, loc);
12441 node->nd_end = range_op(p, node->nd_end, loc);
12442 if (nd_type_p(node, NODE_DOT2)) nd_set_type(node,NODE_FLIP2);
12443 else if (nd_type_p(node, NODE_DOT3)) nd_set_type(node, NODE_FLIP3);
12448 SWITCH_BY_COND_TYPE(type, warning, "symbol ")
12452 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
12453 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
12454 nd_set_type(node, NODE_MATCH);
12456 else if (node->nd_lit == Qtrue ||
12457 node->nd_lit == Qfalse) {
12458 /* booleans are OK, e.g., while true */
12460 else if (SYMBOL_P(node->nd_lit)) {
12464 SWITCH_BY_COND_TYPE(type, warning, "")
12473cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12475 if (node == 0) return 0;
12476 return cond0(p, node, COND_IN_COND, loc);
12480method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12482 if (node == 0) return 0;
12483 return cond0(p, node, COND_IN_OP, loc);
12487new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
12489 YYLTYPE loc = {*pos, *pos};
12490 return NEW_NIL(&loc);
12494new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12496 if (!cc) return right;
12497 cc = cond0(p, cc, COND_IN_COND, loc);
12498 return newline_node(NEW_IF(cc, left, right, loc));
12502new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
12504 if (!cc) return right;
12505 cc = cond0(p, cc, COND_IN_COND, loc);
12506 return newline_node(NEW_UNLESS(cc, left, right, loc));
12510logop(struct parser_params *p, ID id, NODE *left, NODE *right,
12511 const YYLTYPE *op_loc, const YYLTYPE *loc)
12513 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
12516 if (left && nd_type_p(left, type)) {
12517 NODE *node = left, *second;
12518 while ((second = node->nd_2nd) != 0 && nd_type_p(second, type)) {
12521 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
12522 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
12523 left->nd_loc.end_pos = loc->end_pos;
12526 op = NEW_NODE(type, left, right, 0, loc);
12527 nd_set_line(op, op_loc->beg_pos.lineno);
12532no_blockarg(struct parser_params *p, NODE *node)
12534 if (nd_type_p(node, NODE_BLOCK_PASS)) {
12535 compile_error(p, "block argument should not be given");
12540ret_args(struct parser_params *p, NODE *node)
12543 no_blockarg(p, node);
12544 if (nd_type_p(node, NODE_LIST)) {
12545 if (node->nd_next == 0) {
12546 node = node->nd_head;
12549 nd_set_type(node, NODE_VALUES);
12557new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12559 if (node) no_blockarg(p, node);
12561 return NEW_YIELD(node, loc);
12565negate_lit(struct parser_params *p, VALUE lit)
12567 if (FIXNUM_P(lit)) {
12568 return LONG2FIX(-FIX2LONG(lit));
12570 if (SPECIAL_CONST_P(lit)) {
12572 if (FLONUM_P(lit)) {
12573 return DBL2NUM(-RFLOAT_VALUE(lit));
12578 switch (BUILTIN_TYPE(lit)) {
12580 BIGNUM_NEGATE(lit);
12581 lit = rb_big_norm(lit);
12584 RATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
12587 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
12588 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
12591 lit = DBL2NUM(-RFLOAT_VALUE(lit));
12595 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
12596 rb_builtin_class_name(lit));
12603arg_blk_pass(NODE *node1, NODE *node2)
12606 if (!node1) return node2;
12607 node2->nd_head = node1;
12608 nd_set_first_lineno(node2, nd_first_lineno(node1));
12609 nd_set_first_column(node2, nd_first_column(node1));
12616args_info_empty_p(struct rb_args_info *args)
12618 if (args->pre_args_num) return false;
12619 if (args->post_args_num) return false;
12620 if (args->rest_arg) return false;
12621 if (args->opt_args) return false;
12622 if (args->block_arg) return false;
12623 if (args->kw_args) return false;
12624 if (args->kw_rest_arg) return false;
12629new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
12631 int saved_line = p->ruby_sourceline;
12632 struct rb_args_info *args = tail->nd_ainfo;
12634 if (args->forwarding) {
12636 yyerror1(&tail->nd_loc, "... after rest argument");
12639 rest_arg = idFWD_REST;
12642 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
12643 args->pre_init = pre_args ? pre_args->nd_next : 0;
12645 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
12646 args->post_init = post_args ? post_args->nd_next : 0;
12647 args->first_post_arg = post_args ? post_args->nd_pid : 0;
12649 args->rest_arg = rest_arg;
12651 args->opt_args = opt_args;
12653#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
12654 args->ruby2_keywords = args->forwarding;
12656 args->ruby2_keywords = 0;
12659 p->ruby_sourceline = saved_line;
12660 nd_set_loc(tail, loc);
12666new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
12668 int saved_line = p->ruby_sourceline;
12670 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12671 struct rb_args_info *args = ZALLOC(struct rb_args_info);
12672 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
12673 args->imemo = tmpbuf;
12674 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
12675 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12676 if (p->error_p) return node;
12678 args->block_arg = block;
12679 args->kw_args = kw_args;
12683 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
12684 * variable order: k1, kr1, k2, &b, internal_id, krest
12686 * variable order: kr1, k1, k2, internal_id, krest, &b
12688 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
12689 struct vtable *vtargs = p->lvtbl->args;
12690 NODE *kwn = kw_args;
12692 if (block) block = vtargs->tbl[vtargs->pos-1];
12693 vtable_pop(vtargs, !!block + !!kw_rest_arg);
12694 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
12696 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
12698 --required_kw_vars;
12699 kwn = kwn->nd_next;
12702 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
12703 ID vid = kwn->nd_body->nd_vid;
12704 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
12705 *required_kw_vars++ = vid;
12712 arg_var(p, kw_bits);
12713 if (kw_rest_arg) arg_var(p, kw_rest_arg);
12714 if (block) arg_var(p, block);
12716 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12717 args->kw_rest_arg->nd_cflag = kw_bits;
12719 else if (kw_rest_arg == idNil) {
12720 args->no_kwarg = 1;
12722 else if (kw_rest_arg) {
12723 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
12726 p->ruby_sourceline = saved_line;
12731args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
12733 if (max_numparam > NO_PARAM) {
12735 YYLTYPE loc = RUBY_INIT_YYLLOC();
12736 args = new_args_tail(p, 0, 0, 0, 0);
12737 nd_set_loc(args, &loc);
12739 args->nd_ainfo->pre_args_num = max_numparam;
12745new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
12747 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
12749 aryptn->nd_pconst = constant;
12752 NODE *pre_args = NEW_LIST(pre_arg, loc);
12753 if (apinfo->pre_args) {
12754 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
12757 apinfo->pre_args = pre_args;
12764new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
12766 int saved_line = p->ruby_sourceline;
12768 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12769 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
12770 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
12771 node = NEW_NODE(NODE_ARYPTN, 0, tmpbuf, apinfo, loc);
12772 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12774 apinfo->pre_args = pre_args;
12778 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
12781 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
12785 apinfo->rest_arg = NULL;
12788 apinfo->post_args = post_args;
12790 p->ruby_sourceline = saved_line;
12795new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
12797 fndptn->nd_pconst = constant;
12803new_find_pattern_tail(struct parser_params *p, ID pre_rest_arg, NODE *args, ID post_rest_arg, const YYLTYPE *loc)
12805 int saved_line = p->ruby_sourceline;
12807 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
12808 struct rb_fnd_pattern_info *fpinfo = ZALLOC(struct rb_fnd_pattern_info);
12809 rb_imemo_tmpbuf_set_ptr(tmpbuf, fpinfo);
12810 node = NEW_NODE(NODE_FNDPTN, 0, tmpbuf, fpinfo, loc);
12811 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
12813 fpinfo->pre_rest_arg = pre_rest_arg ? assignable(p, pre_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12814 fpinfo->args = args;
12815 fpinfo->post_rest_arg = post_rest_arg ? assignable(p, post_rest_arg, 0, loc) : NODE_SPECIAL_NO_NAME_REST;
12817 p->ruby_sourceline = saved_line;
12822new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
12824 hshptn->nd_pconst = constant;
12829new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
12831 int saved_line = p->ruby_sourceline;
12832 NODE *node, *kw_rest_arg_node;
12834 if (kw_rest_arg == idNil) {
12835 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
12837 else if (kw_rest_arg) {
12838 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
12841 kw_rest_arg_node = NULL;
12844 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
12846 p->ruby_sourceline = saved_line;
12851dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12856 return NEW_LIT(ID2SYM(idNULL), loc);
12859 switch (nd_type(node)) {
12861 nd_set_type(node, NODE_DSYM);
12862 nd_set_loc(node, loc);
12865 lit = node->nd_lit;
12866 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
12867 nd_set_type(node, NODE_LIT);
12868 nd_set_loc(node, loc);
12871 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
12878append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
12880 NODE *node = (NODE *)v;
12881 NODE **result = (NODE **)h;
12883 node->nd_next->nd_end = node->nd_next;
12884 node->nd_next->nd_next = 0;
12886 list_concat(*result, node);
12889 return ST_CONTINUE;
12893hash_literal_key_p(VALUE k)
12895 switch (OBJ_BUILTIN_TYPE(k)) {
12904literal_cmp(VALUE val, VALUE lit)
12906 if (val == lit) return 0;
12907 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
12908 return rb_iseq_cdhash_cmp(val, lit);
12912literal_hash(VALUE a)
12914 if (!hash_literal_key_p(a)) return (st_index_t)a;
12915 return rb_iseq_cdhash_hash(a);
12918static const struct st_hash_type literal_type = {
12924remove_duplicate_keys(struct parser_params *p, NODE *hash)
12926 st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
12928 NODE *last_expr = 0;
12929 rb_code_location_t loc = hash->nd_loc;
12930 while (hash && hash->nd_head && hash->nd_next) {
12931 NODE *head = hash->nd_head;
12932 NODE *value = hash->nd_next;
12933 NODE *next = value->nd_next;
12934 st_data_t key = (st_data_t)head;
12936 value->nd_next = 0;
12937 if (nd_type_p(head, NODE_LIT) &&
12938 st_delete(literal_keys, (key = (st_data_t)head->nd_lit, &key), &data)) {
12939 NODE *dup_value = ((NODE *)data)->nd_next;
12940 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
12941 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
12942 head->nd_lit, nd_line(head));
12943 if (dup_value == last_expr) {
12944 value->nd_head = block_append(p, dup_value->nd_head, value->nd_head);
12947 last_expr->nd_head = block_append(p, dup_value->nd_head, last_expr->nd_head);
12950 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
12951 last_expr = nd_type_p(head, NODE_LIT) ? value : head;
12954 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
12955 st_free_table(literal_keys);
12957 if (!result) result = hash;
12958 else list_concat(result, hash);
12960 result->nd_loc = loc;
12965new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
12967 if (hash) hash = remove_duplicate_keys(p, hash);
12968 return NEW_HASH(hash, loc);
12973error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
12975 if (is_private_local_id(id)) {
12978 if (st_is_member(p->pvtbl, id)) {
12979 yyerror1(loc, "duplicated variable name");
12982 st_insert(p->pvtbl, (st_data_t)id, 0);
12987error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
12990 p->pktbl = st_init_numtable();
12992 else if (st_is_member(p->pktbl, key)) {
12993 yyerror1(loc, "duplicated key name");
12996 st_insert(p->pktbl, (st_data_t)key, 0);
13001new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
13003 return NEW_HASH(hash, loc);
13005#endif /* !RIPPER */
13009new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13014 ID vid = lhs->nd_vid;
13015 YYLTYPE lhs_loc = lhs->nd_loc;
13016 int shareable = ctxt.shareable_constant_value;
13018 switch (nd_type(lhs)) {
13029 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13030 lhs->nd_value = rhs;
13031 nd_set_loc(lhs, loc);
13032 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
13033 if (is_notop_id(vid)) {
13034 switch (id_type(vid)) {
13038 asgn->nd_aid = vid;
13042 else if (op == tANDOP) {
13044 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13046 lhs->nd_value = rhs;
13047 nd_set_loc(lhs, loc);
13048 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
13052 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
13054 rhs = shareable_constant_value(p, shareable, lhs, rhs, &rhs->nd_loc);
13056 asgn->nd_value = rhs;
13057 nd_set_loc(asgn, loc);
13061 asgn = NEW_BEGIN(0, loc);
13067new_ary_op_assign(struct parser_params *p, NODE *ary,
13068 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
13072 args = make_list(args, args_loc);
13073 if (nd_type_p(args, NODE_BLOCK_PASS)) {
13074 args = NEW_ARGSCAT(args, rhs, loc);
13077 args = arg_concat(p, args, rhs, loc);
13079 asgn = NEW_OP_ASGN1(ary, op, args, loc);
13085new_attr_op_assign(struct parser_params *p, NODE *lhs,
13086 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
13090 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
13096new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13101 rhs = shareable_constant_value(p, ctxt.shareable_constant_value, lhs, rhs, loc);
13102 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
13105 asgn = NEW_BEGIN(0, loc);
13112const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
13114 if (p->ctxt.in_def) {
13115 yyerror1(loc, "dynamic constant assignment");
13117 return NEW_CDECL(0, 0, (path), loc);
13121const_decl(struct parser_params *p, VALUE path)
13123 if (p->ctxt.in_def) {
13124 path = assign_error(p, "dynamic constant assignment", path);
13130assign_error(struct parser_params *p, const char *mesg, VALUE a)
13132 a = dispatch2(assign_error, ERR_MESG(), a);
13138var_field(struct parser_params *p, VALUE a)
13140 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
13146new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
13148 NODE *result = head;
13150 NODE *tmp = rescue_else ? rescue_else : rescue;
13151 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
13153 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
13154 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
13156 else if (rescue_else) {
13157 result = block_append(p, result, rescue_else);
13160 result = NEW_ENSURE(result, ensure, loc);
13162 fixpos(result, head);
13168warn_unused_var(struct parser_params *p, struct local_vars *local)
13172 if (!local->used) return;
13173 cnt = local->used->pos;
13174 if (cnt != local->vars->pos) {
13175 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
13178 ID *v = local->vars->tbl;
13179 ID *u = local->used->tbl;
13180 for (int i = 0; i < cnt; ++i) {
13181 if (!v[i] || (u[i] & LVAR_USED)) continue;
13182 if (is_private_local_id(v[i])) continue;
13183 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
13189local_push(struct parser_params *p, int toplevel_scope)
13191 struct local_vars *local;
13192 int inherits_dvars = toplevel_scope && compile_for_eval;
13193 int warn_unused_vars = RTEST(ruby_verbose);
13195 local = ALLOC(struct local_vars);
13196 local->prev = p->lvtbl;
13197 local->args = vtable_alloc(0);
13198 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
13200 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
13201 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
13202 local->numparam.outer = 0;
13203 local->numparam.inner = 0;
13204 local->numparam.current = 0;
13206 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
13208# if WARN_PAST_SCOPE
13217vtable_chain_free(struct parser_params *p, struct vtable *table)
13219 while (!DVARS_TERMINAL_P(table)) {
13220 struct vtable *cur_table = table;
13221 table = cur_table->prev;
13222 vtable_free(cur_table);
13227local_free(struct parser_params *p, struct local_vars *local)
13229 vtable_chain_free(p, local->used);
13231# if WARN_PAST_SCOPE
13232 vtable_chain_free(p, local->past);
13235 vtable_chain_free(p, local->args);
13236 vtable_chain_free(p, local->vars);
13238 ruby_sized_xfree(local, sizeof(struct local_vars));
13242local_pop(struct parser_params *p)
13244 struct local_vars *local = p->lvtbl->prev;
13245 if (p->lvtbl->used) {
13246 warn_unused_var(p, p->lvtbl);
13249 local_free(p, p->lvtbl);
13257static rb_ast_id_table_t *
13258local_tbl(struct parser_params *p)
13260 int cnt_args = vtable_size(p->lvtbl->args);
13261 int cnt_vars = vtable_size(p->lvtbl->vars);
13262 int cnt = cnt_args + cnt_vars;
13264 rb_ast_id_table_t *tbl;
13266 if (cnt <= 0) return 0;
13267 tbl = rb_ast_new_local_table(p->ast, cnt);
13268 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
13269 /* remove IDs duplicated to warn shadowing */
13270 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
13271 ID id = p->lvtbl->vars->tbl[i];
13272 if (!vtable_included(p->lvtbl->args, id)) {
13273 tbl->ids[j++] = id;
13277 tbl = rb_ast_resize_latest_local_table(p->ast, j);
13284node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
13286 rb_ast_id_table_t *a0;
13290 n = NEW_NODE(type, a0, a1, a2, loc);
13297numparam_name(struct parser_params *p, ID id)
13299 if (!NUMPARAM_ID_P(id)) return;
13300 compile_error(p, "_%d is reserved for numbered parameter",
13301 NUMPARAM_ID_TO_IDX(id));
13305arg_var(struct parser_params *p, ID id)
13307 numparam_name(p, id);
13308 vtable_add(p->lvtbl->args, id);
13312local_var(struct parser_params *p, ID id)
13314 numparam_name(p, id);
13315 vtable_add(p->lvtbl->vars, id);
13316 if (p->lvtbl->used) {
13317 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
13322local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
13324 struct vtable *vars, *args, *used;
13326 vars = p->lvtbl->vars;
13327 args = p->lvtbl->args;
13328 used = p->lvtbl->used;
13330 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
13333 if (used) used = used->prev;
13336 if (vars && vars->prev == DVARS_INHERIT) {
13337 return rb_local_defined(id, p->parent_iseq);
13339 else if (vtable_included(args, id)) {
13343 int i = vtable_included(vars, id);
13344 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
13350local_id(struct parser_params *p, ID id)
13352 return local_id_ref(p, id, NULL);
13356check_forwarding_args(struct parser_params *p)
13358 if (local_id(p, idFWD_ALL)) return TRUE;
13359 compile_error(p, "unexpected ...");
13364add_forwarding_args(struct parser_params *p)
13366 arg_var(p, idFWD_REST);
13367#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13368 arg_var(p, idFWD_KWREST);
13370 arg_var(p, idFWD_BLOCK);
13371 arg_var(p, idFWD_ALL);
13376new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
13378 NODE *rest = NEW_LVAR(idFWD_REST, loc);
13379#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13380 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
13382 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc);
13383 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
13384#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
13385 args = arg_append(p, args, new_hash(p, kwrest, loc), loc);
13387 return arg_blk_pass(args, block);
13392numparam_push(struct parser_params *p)
13395 struct local_vars *local = p->lvtbl;
13396 NODE *inner = local->numparam.inner;
13397 if (!local->numparam.outer) {
13398 local->numparam.outer = local->numparam.current;
13400 local->numparam.inner = 0;
13401 local->numparam.current = 0;
13409numparam_pop(struct parser_params *p, NODE *prev_inner)
13412 struct local_vars *local = p->lvtbl;
13414 /* prefer first one */
13415 local->numparam.inner = prev_inner;
13417 else if (local->numparam.current) {
13418 /* current and inner are exclusive */
13419 local->numparam.inner = local->numparam.current;
13421 if (p->max_numparam > NO_PARAM) {
13422 /* current and outer are exclusive */
13423 local->numparam.current = local->numparam.outer;
13424 local->numparam.outer = 0;
13427 /* no numbered parameter */
13428 local->numparam.current = 0;
13433static const struct vtable *
13434dyna_push(struct parser_params *p)
13436 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
13437 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
13438 if (p->lvtbl->used) {
13439 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
13441 return p->lvtbl->args;
13445dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
13447 struct vtable *tmp = *vtblp;
13448 *vtblp = tmp->prev;
13449# if WARN_PAST_SCOPE
13450 if (p->past_scope_enabled) {
13451 tmp->prev = p->lvtbl->past;
13452 p->lvtbl->past = tmp;
13460dyna_pop_1(struct parser_params *p)
13462 struct vtable *tmp;
13464 if ((tmp = p->lvtbl->used) != 0) {
13465 warn_unused_var(p, p->lvtbl);
13466 p->lvtbl->used = p->lvtbl->used->prev;
13469 dyna_pop_vtable(p, &p->lvtbl->args);
13470 dyna_pop_vtable(p, &p->lvtbl->vars);
13474dyna_pop(struct parser_params *p, const struct vtable *lvargs)
13476 while (p->lvtbl->args != lvargs) {
13478 if (!p->lvtbl->args) {
13479 struct local_vars *local = p->lvtbl->prev;
13480 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
13488dyna_in_block(struct parser_params *p)
13490 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
13494dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
13496 struct vtable *vars, *args, *used;
13499 args = p->lvtbl->args;
13500 vars = p->lvtbl->vars;
13501 used = p->lvtbl->used;
13503 while (!DVARS_TERMINAL_P(vars)) {
13504 if (vtable_included(args, id)) {
13507 if ((i = vtable_included(vars, id)) != 0) {
13508 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
13513 if (!vidrefp) used = 0;
13514 if (used) used = used->prev;
13517 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
13518 return rb_dvar_defined(id, p->parent_iseq);
13525dvar_defined(struct parser_params *p, ID id)
13527 return dvar_defined_ref(p, id, NULL);
13531dvar_curr(struct parser_params *p, ID id)
13533 return (vtable_included(p->lvtbl->args, id) ||
13534 vtable_included(p->lvtbl->vars, id));
13538reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
13541 "regexp encoding option '%c' differs from source encoding '%s'",
13542 c, rb_enc_name(rb_enc_get(str)));
13547rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13549 int c = RE_OPTION_ENCODING_IDX(options);
13553 rb_char_to_option_kcode(c, &opt, &idx);
13554 if (idx != ENCODING_GET(str) &&
13555 !is_ascii_string(str)) {
13558 ENCODING_SET(str, idx);
13560 else if (RE_OPTION_ENCODING_NONE(options)) {
13561 if (!ENCODING_IS_ASCII8BIT(str) &&
13562 !is_ascii_string(str)) {
13566 rb_enc_associate(str, rb_ascii8bit_encoding());
13568 else if (rb_is_usascii_enc(p->enc)) {
13569 if (!is_ascii_string(str)) {
13570 /* raise in re.c */
13571 rb_enc_associate(str, rb_usascii_encoding());
13574 rb_enc_associate(str, rb_ascii8bit_encoding());
13584reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
13586 int c = rb_reg_fragment_setenc(p, str, options);
13587 if (c) reg_fragment_enc_error(p, str, c);
13591reg_fragment_check(struct parser_params* p, VALUE str, int options)
13594 reg_fragment_setenc(p, str, options);
13595 err = rb_reg_check_preprocess(str);
13597 err = rb_obj_as_string(err);
13598 compile_error(p, "%"PRIsVALUE, err);
13605 struct parser_params* parser;
13608 const YYLTYPE *loc;
13609} reg_named_capture_assign_t;
13612reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
13613 int back_num, int *back_refs, OnigRegex regex, void *arg0)
13615 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
13616 struct parser_params* p = arg->parser;
13617 rb_encoding *enc = arg->enc;
13618 long len = name_end - name;
13619 const char *s = (const char *)name;
13623 if (!len) return ST_CONTINUE;
13624 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
13625 return ST_CONTINUE;
13627 var = intern_cstr(s, len, enc);
13628 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
13629 if (!lvar_defined(p, var)) return ST_CONTINUE;
13631 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
13632 succ = arg->succ_block;
13633 if (!succ) succ = NEW_BEGIN(0, arg->loc);
13634 succ = block_append(p, succ, node);
13635 arg->succ_block = succ;
13636 return ST_CONTINUE;
13640reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
13642 reg_named_capture_assign_t arg;
13645 arg.enc = rb_enc_get(regexp);
13646 arg.succ_block = 0;
13648 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
13650 if (!arg.succ_block) return 0;
13651 return arg.succ_block->nd_next;
13655parser_reg_compile(struct parser_params* p, VALUE str, int options)
13657 reg_fragment_setenc(p, str, options);
13658 return rb_parser_reg_compile(p, str, options);
13662rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
13664 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
13668reg_compile(struct parser_params* p, VALUE str, int options)
13673 err = rb_errinfo();
13674 re = parser_reg_compile(p, str, options);
13676 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
13677 rb_set_errinfo(err);
13678 compile_error(p, "%"PRIsVALUE, m);
13685parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
13687 VALUE err = rb_errinfo();
13689 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
13690 int c = rb_reg_fragment_setenc(p, str, options);
13691 if (c) reg_fragment_enc_error(p, str, c);
13692 re = rb_parser_reg_compile(p, str, options);
13694 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
13695 rb_set_errinfo(err);
13703rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
13705 struct parser_params *p;
13706 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13707 p->do_print = print;
13709 p->do_chomp = chomp;
13710 p->do_split = split;
13714parser_append_options(struct parser_params *p, NODE *node)
13716 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
13717 const YYLTYPE *const LOC = &default_location;
13720 NODE *print = NEW_FCALL(rb_intern("print"),
13721 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
13723 node = block_append(p, node, print);
13727 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
13730 ID ifs = rb_intern("$;");
13731 ID fields = rb_intern("$F");
13732 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
13733 NODE *split = NEW_GASGN(fields,
13734 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
13735 rb_intern("split"), args, LOC),
13737 node = block_append(p, split, node);
13740 NODE *chomp = NEW_LIT(ID2SYM(rb_intern("chomp")), LOC);
13741 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
13742 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
13745 node = NEW_WHILE(NEW_FCALL(idGets, irs, LOC), node, 1, LOC);
13754 /* just to suppress unused-function warnings */
13760internal_id(struct parser_params *p)
13762 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
13764#endif /* !RIPPER */
13767parser_initialize(struct parser_params *p)
13769 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
13770 p->command_start = TRUE;
13771 p->ruby_sourcefile_string = Qnil;
13772 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
13774 p->delayed.token = Qnil;
13777 p->parsing_thread = Qnil;
13779 p->error_buffer = Qfalse;
13780 p->end_expect_token_locations = Qnil;
13784 p->debug_buffer = Qnil;
13785 p->debug_output = rb_ractor_stdout();
13786 p->enc = rb_utf8_encoding();
13790#define parser_mark ripper_parser_mark
13791#define parser_free ripper_parser_free
13795parser_mark(void *ptr)
13797 struct parser_params *p = (struct parser_params*)ptr;
13799 rb_gc_mark(p->lex.input);
13800 rb_gc_mark(p->lex.lastline);
13801 rb_gc_mark(p->lex.nextline);
13802 rb_gc_mark(p->ruby_sourcefile_string);
13803 rb_gc_mark((VALUE)p->lex.strterm);
13804 rb_gc_mark((VALUE)p->ast);
13805 rb_gc_mark(p->case_labels);
13806 rb_gc_mark(p->delayed.token);
13808 rb_gc_mark(p->debug_lines);
13809 rb_gc_mark(p->compile_option);
13810 rb_gc_mark(p->error_buffer);
13811 rb_gc_mark(p->end_expect_token_locations);
13812 rb_gc_mark(p->tokens);
13814 rb_gc_mark(p->value);
13815 rb_gc_mark(p->result);
13816 rb_gc_mark(p->parsing_thread);
13818 rb_gc_mark(p->debug_buffer);
13819 rb_gc_mark(p->debug_output);
13821 rb_gc_mark((VALUE)p->heap);
13826parser_free(void *ptr)
13828 struct parser_params *p = (struct parser_params*)ptr;
13829 struct local_vars *local, *prev;
13832 ruby_sized_xfree(p->tokenbuf, p->toksiz);
13835 for (local = p->lvtbl; local; local = prev) {
13836 prev = local->prev;
13837 local_free(p, local);
13841 token_info *ptinfo;
13842 while ((ptinfo = p->token_info) != 0) {
13843 p->token_info = ptinfo->next;
13851parser_memsize(const void *ptr)
13853 struct parser_params *p = (struct parser_params*)ptr;
13854 struct local_vars *local;
13855 size_t size = sizeof(*p);
13858 for (local = p->lvtbl; local; local = local->prev) {
13859 size += sizeof(*local);
13860 if (local->vars) size += local->vars->capa * sizeof(ID);
13865static const rb_data_type_t parser_data_type = {
13876 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
13880#undef rb_reserved_word
13882const struct kwtable *
13883rb_reserved_word(const char *str, unsigned int len)
13885 return reserved_word(str, len);
13891 struct parser_params *p;
13892 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
13893 &parser_data_type, p);
13894 parser_initialize(p);
13899rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
13901 struct parser_params *p;
13903 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13904 p->error_buffer = main ? Qfalse : Qnil;
13905 p->parent_iseq = base;
13910rb_parser_keep_script_lines(VALUE vparser)
13912 struct parser_params *p;
13914 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13915 p->keep_script_lines = 1;
13919rb_parser_error_tolerant(VALUE vparser)
13921 struct parser_params *p;
13923 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13924 p->error_tolerant = 1;
13925 p->end_expect_token_locations = rb_ary_new();
13929rb_parser_keep_tokens(VALUE vparser)
13931 struct parser_params *p;
13933 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13934 p->keep_tokens = 1;
13935 p->tokens = rb_ary_new();
13941#define rb_parser_end_seen_p ripper_parser_end_seen_p
13942#define rb_parser_encoding ripper_parser_encoding
13943#define rb_parser_get_yydebug ripper_parser_get_yydebug
13944#define rb_parser_set_yydebug ripper_parser_set_yydebug
13945#define rb_parser_get_debug_output ripper_parser_get_debug_output
13946#define rb_parser_set_debug_output ripper_parser_set_debug_output
13947static VALUE ripper_parser_end_seen_p(VALUE vparser);
13948static VALUE ripper_parser_encoding(VALUE vparser);
13949static VALUE ripper_parser_get_yydebug(VALUE self);
13950static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
13951static VALUE ripper_parser_get_debug_output(VALUE self);
13952static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
13956 * ripper.error? -> Boolean
13958 * Return true if parsed source has errors.
13961ripper_error_p(VALUE vparser)
13963 struct parser_params *p;
13965 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13966 return RBOOL(p->error_p);
13972 * ripper.end_seen? -> Boolean
13974 * Return true if parsed source ended by +\_\_END\_\_+.
13977rb_parser_end_seen_p(VALUE vparser)
13979 struct parser_params *p;
13981 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13982 return RBOOL(p->ruby__end__seen);
13987 * ripper.encoding -> encoding
13989 * Return encoding of the source.
13992rb_parser_encoding(VALUE vparser)
13994 struct parser_params *p;
13996 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
13997 return rb_enc_from_encoding(p->enc);
14003 * ripper.yydebug -> true or false
14008rb_parser_get_yydebug(VALUE self)
14010 struct parser_params *p;
14012 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14013 return RBOOL(p->debug);
14019 * ripper.yydebug = flag
14024rb_parser_set_yydebug(VALUE self, VALUE flag)
14026 struct parser_params *p;
14028 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14029 p->debug = RTEST(flag);
14035 * ripper.debug_output -> obj
14037 * Get debug output.
14040rb_parser_get_debug_output(VALUE self)
14042 struct parser_params *p;
14044 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14045 return p->debug_output;
14050 * ripper.debug_output = obj
14052 * Set debug output.
14055rb_parser_set_debug_output(VALUE self, VALUE output)
14057 struct parser_params *p;
14059 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14060 return p->debug_output = output;
14065#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
14066/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
14067 * potential memory leak */
14068#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
14069#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
14070 (new)->cnt = (cnt), (ptr))
14073rb_parser_malloc(struct parser_params *p, size_t size)
14075 size_t cnt = HEAPCNT(1, size);
14076 rb_imemo_tmpbuf_t *n = NEWHEAP();
14077 void *ptr = xmalloc(size);
14079 return ADD2HEAP(n, cnt, ptr);
14083rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
14085 size_t cnt = HEAPCNT(nelem, size);
14086 rb_imemo_tmpbuf_t *n = NEWHEAP();
14087 void *ptr = xcalloc(nelem, size);
14089 return ADD2HEAP(n, cnt, ptr);
14093rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
14095 rb_imemo_tmpbuf_t *n;
14096 size_t cnt = HEAPCNT(1, size);
14098 if (ptr && (n = p->heap) != NULL) {
14100 if (n->ptr == ptr) {
14101 n->ptr = ptr = xrealloc(ptr, size);
14102 if (n->cnt) n->cnt = cnt;
14105 } while ((n = n->next) != NULL);
14108 ptr = xrealloc(ptr, size);
14109 return ADD2HEAP(n, cnt, ptr);
14113rb_parser_free(struct parser_params *p, void *ptr)
14115 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
14117 while ((n = *prev) != NULL) {
14118 if (n->ptr == ptr) {
14128rb_parser_printf(struct parser_params *p, const char *fmt, ...)
14131 VALUE mesg = p->debug_buffer;
14133 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
14135 rb_str_vcatf(mesg, fmt, ap);
14137 if (RSTRING_END(mesg)[-1] == '\n') {
14138 rb_io_write(p->debug_output, mesg);
14139 p->debug_buffer = Qnil;
14144parser_compile_error(struct parser_params *p, const char *fmt, ...)
14148 rb_io_flush(p->debug_output);
14152 rb_syntax_error_append(p->error_buffer,
14153 p->ruby_sourcefile_string,
14154 p->ruby_sourceline,
14155 rb_long2int(p->lex.pcur - p->lex.pbeg),
14161count_char(const char *str, int c)
14164 while (str[n] == c) ++n;
14169 * strip enclosing double-quotes, same as the default yytnamerr except
14170 * for that single-quotes matching back-quotes do not stop stripping.
14172 * "\"`class' keyword\"" => "`class' keyword"
14174RUBY_FUNC_EXPORTED size_t
14175rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
14177 if (*yystr == '"') {
14178 size_t yyn = 0, bquote = 0;
14179 const char *yyp = yystr;
14185 bquote = count_char(yyp+1, '`') + 1;
14186 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
14194 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
14195 if (yyres) memcpy(yyres + yyn, yyp, bquote);
14201 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
14202 if (yyres) memcpy(yyres + yyn, yyp, 3);
14207 goto do_not_strip_quotes;
14210 goto do_not_strip_quotes;
14213 if (*++yyp != '\\')
14214 goto do_not_strip_quotes;
14215 /* Fall through. */
14230 do_not_strip_quotes: ;
14233 if (!yyres) return strlen(yystr);
14235 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
14243ripper_validate_object(VALUE self, VALUE x)
14245 if (x == Qfalse) return x;
14246 if (x == Qtrue) return x;
14247 if (NIL_P(x)) return x;
14249 rb_raise(rb_eArgError, "Qundef given");
14250 if (FIXNUM_P(x)) return x;
14251 if (SYMBOL_P(x)) return x;
14252 switch (BUILTIN_TYPE(x)) {
14262 if (!nd_type_p((NODE *)x, NODE_RIPPER)) {
14263 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
14265 x = ((NODE *)x)->nd_rval;
14268 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
14269 (void *)x, rb_obj_classname(x));
14271 if (!RBASIC_CLASS(x)) {
14272 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
14273 (void *)x, rb_builtin_type_name(TYPE(x)));
14279#define validate(x) ((x) = get_value(x))
14282ripper_dispatch0(struct parser_params *p, ID mid)
14284 return rb_funcall(p->value, mid, 0);
14288ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
14291 return rb_funcall(p->value, mid, 1, a);
14295ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
14299 return rb_funcall(p->value, mid, 2, a, b);
14303ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
14308 return rb_funcall(p->value, mid, 3, a, b, c);
14312ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
14318 return rb_funcall(p->value, mid, 4, a, b, c, d);
14322ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
14329 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
14333ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
14342 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
14346ripper_get_id(VALUE v)
14349 if (!RB_TYPE_P(v, T_NODE)) return 0;
14351 if (!nd_type_p(nd, NODE_RIPPER)) return 0;
14356ripper_get_value(VALUE v)
14359 if (UNDEF_P(v)) return Qnil;
14360 if (!RB_TYPE_P(v, T_NODE)) return v;
14362 if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
14363 return nd->nd_rval;
14367ripper_error(struct parser_params *p)
14373ripper_compile_error(struct parser_params *p, const char *fmt, ...)
14378 va_start(args, fmt);
14379 str = rb_vsprintf(fmt, args);
14381 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
14386ripper_lex_get_generic(struct parser_params *p, VALUE src)
14388 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
14389 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
14390 rb_raise(rb_eTypeError,
14391 "gets returned %"PRIsVALUE" (expected String or nil)",
14392 rb_obj_class(line));
14398ripper_lex_io_get(struct parser_params *p, VALUE src)
14400 return rb_io_gets(src);
14404ripper_s_allocate(VALUE klass)
14406 struct parser_params *p;
14407 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
14408 &parser_data_type, p);
14413#define ripper_initialized_p(r) ((r)->lex.input != 0)
14417 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
14419 * Create a new Ripper object.
14420 * _src_ must be a String, an IO, or an Object which has #gets method.
14422 * This method does not starts parsing.
14423 * See also Ripper#parse and Ripper.parse.
14426ripper_initialize(int argc, VALUE *argv, VALUE self)
14428 struct parser_params *p;
14429 VALUE src, fname, lineno;
14431 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14432 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
14433 if (RB_TYPE_P(src, T_FILE)) {
14434 p->lex.gets = ripper_lex_io_get;
14436 else if (rb_respond_to(src, id_gets)) {
14437 p->lex.gets = ripper_lex_get_generic;
14441 p->lex.gets = lex_get_str;
14443 p->lex.input = src;
14445 if (NIL_P(fname)) {
14446 fname = STR_NEW2("(ripper)");
14450 StringValueCStr(fname);
14451 fname = rb_str_new_frozen(fname);
14453 parser_initialize(p);
14455 p->ruby_sourcefile_string = fname;
14456 p->ruby_sourcefile = RSTRING_PTR(fname);
14457 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
14463ripper_parse0(VALUE parser_v)
14465 struct parser_params *p;
14467 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14469 p->ast = rb_ast_new();
14470 ripper_yyparse((void*)p);
14471 rb_ast_dispose(p->ast);
14477ripper_ensure(VALUE parser_v)
14479 struct parser_params *p;
14481 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
14482 p->parsing_thread = Qnil;
14490 * Start parsing and returns the value of the root action.
14493ripper_parse(VALUE self)
14495 struct parser_params *p;
14497 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14498 if (!ripper_initialized_p(p)) {
14499 rb_raise(rb_eArgError, "method called for uninitialized object");
14501 if (!NIL_P(p->parsing_thread)) {
14502 if (p->parsing_thread == rb_thread_current())
14503 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
14505 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
14507 p->parsing_thread = rb_thread_current();
14508 rb_ensure(ripper_parse0, self, ripper_ensure, self);
14515 * ripper.column -> Integer
14517 * Return column number of current parsing line.
14518 * This number starts from 0.
14521ripper_column(VALUE self)
14523 struct parser_params *p;
14526 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14527 if (!ripper_initialized_p(p)) {
14528 rb_raise(rb_eArgError, "method called for uninitialized object");
14530 if (NIL_P(p->parsing_thread)) return Qnil;
14531 col = p->lex.ptok - p->lex.pbeg;
14532 return LONG2NUM(col);
14537 * ripper.filename -> String
14539 * Return current parsing filename.
14542ripper_filename(VALUE self)
14544 struct parser_params *p;
14546 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14547 if (!ripper_initialized_p(p)) {
14548 rb_raise(rb_eArgError, "method called for uninitialized object");
14550 return p->ruby_sourcefile_string;
14555 * ripper.lineno -> Integer
14557 * Return line number of current parsing line.
14558 * This number starts from 1.
14561ripper_lineno(VALUE self)
14563 struct parser_params *p;
14565 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14566 if (!ripper_initialized_p(p)) {
14567 rb_raise(rb_eArgError, "method called for uninitialized object");
14569 if (NIL_P(p->parsing_thread)) return Qnil;
14570 return INT2NUM(p->ruby_sourceline);
14575 * ripper.state -> Integer
14577 * Return scanner state of current token.
14580ripper_state(VALUE self)
14582 struct parser_params *p;
14584 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14585 if (!ripper_initialized_p(p)) {
14586 rb_raise(rb_eArgError, "method called for uninitialized object");
14588 if (NIL_P(p->parsing_thread)) return Qnil;
14589 return INT2NUM(p->lex.state);
14594 * ripper.token -> String
14596 * Return the current token string.
14599ripper_token(VALUE self)
14601 struct parser_params *p;
14604 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
14605 if (!ripper_initialized_p(p)) {
14606 rb_raise(rb_eArgError, "method called for uninitialized object");
14608 if (NIL_P(p->parsing_thread)) return Qnil;
14609 pos = p->lex.ptok - p->lex.pbeg;
14610 len = p->lex.pcur - p->lex.ptok;
14611 return rb_str_subseq(p->lex.lastline, pos, len);
14617ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
14620 if (UNDEF_P(obj)) {
14621 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
14628ripper_value(VALUE self, VALUE obj)
14630 return ULONG2NUM(obj);
14636 * Ripper.lex_state_name(integer) -> string
14638 * Returns a string representation of lex_state.
14641ripper_lex_state_name(VALUE self, VALUE state)
14643 return rb_parser_lex_state_name(NUM2INT(state));
14649 ripper_init_eventids1();
14650 ripper_init_eventids2();
14651 id_warn = rb_intern_const("warn");
14652 id_warning = rb_intern_const("warning");
14653 id_gets = rb_intern_const("gets");
14654 id_assoc = rb_intern_const("=>");
14656 (void)yystpcpy; /* may not used in newer bison */
14666 Ripper = rb_define_class("Ripper", rb_cObject);
14667 /* version of Ripper */
14668 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
14669 rb_define_alloc_func(Ripper, ripper_s_allocate);
14670 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
14671 rb_define_method(Ripper, "parse", ripper_parse, 0);
14672 rb_define_method(Ripper, "column", ripper_column, 0);
14673 rb_define_method(Ripper, "filename", ripper_filename, 0);
14674 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
14675 rb_define_method(Ripper, "state", ripper_state, 0);
14676 rb_define_method(Ripper, "token", ripper_token, 0);
14677 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
14678 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
14679 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
14680 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
14681 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
14682 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
14683 rb_define_method(Ripper, "error?", ripper_error_p, 0);
14685 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
14686 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
14687 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
14690 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
14691 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
14693 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
14695<% @exprs.each do |expr, desc| -%>
14697 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
14699 ripper_init_eventids1_table(Ripper);
14700 ripper_init_eventids2_table(Ripper);
14703 /* Hack to let RDoc document SCRIPT_LINES__ */
14706 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
14707 * after the assignment will be added as an Array of lines with the file
14710 rb_define_global_const("SCRIPT_LINES__", Qnil);
14719 * c-file-style: "ruby"