1/**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10**********************************************************************/
15# error needs pure parser
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
20#define YYLTYPE rb_code_location_t
21#define YYLTYPE_IS_DECLARED 1
25#include "ruby/encoding.h"
36#ifndef WARN_PAST_SCOPE
37# define WARN_PAST_SCOPE 0
42#define yydebug (p->debug) /* disable the global variable definition */
44#define YYMALLOC(size) rb_parser_malloc(p, (size))
45#define YYREALLOC(ptr, size) rb_parser_realloc(p, (ptr), (size))
46#define YYCALLOC(nelem, size) rb_parser_calloc(p, (nelem), (size))
47#define YYFREE(ptr) rb_parser_free(p, (ptr))
48#define YYFPRINTF rb_parser_printf
49#define YY_LOCATION_PRINT(File, loc) \
50 rb_parser_printf(p, "%d.%d-%d.%d", \
51 (loc).beg_pos.lineno, (loc).beg_pos.column,\
52 (loc).end_pos.lineno, (loc).end_pos.column)
53#define YYLLOC_DEFAULT(Current, Rhs, N) \
57 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
58 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
62 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
63 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
67#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
68 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
69#define RUBY_SET_YYLLOC_OF_NONE(Current) \
70 rb_parser_set_location_of_none(p, &(Current))
71#define RUBY_SET_YYLLOC(Current) \
72 rb_parser_set_location(p, &(Current))
73#define RUBY_INIT_YYLLOC() \
75 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
76 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
80 EXPR_BEG_bit, /* ignore newline, +/- is a sign. */
81 EXPR_END_bit, /* newline significant, +/- is an operator. */
82 EXPR_ENDARG_bit, /* ditto, and unbound braces. */
83 EXPR_ENDFN_bit, /* ditto, and unbound braces. */
84 EXPR_ARG_bit, /* newline significant, +/- is an operator. */
85 EXPR_CMDARG_bit, /* newline significant, +/- is an operator. */
86 EXPR_MID_bit, /* newline significant, +/- is an operator. */
87 EXPR_FNAME_bit, /* ignore newline, no reserved words. */
88 EXPR_DOT_bit, /* right after `.' or `::', no reserved words. */
89 EXPR_CLASS_bit, /* immediate after `class', no here document. */
90 EXPR_LABEL_bit, /* flag bit, label is allowed. */
91 EXPR_LABELED_bit, /* flag bit, just after a label. */
92 EXPR_FITEM_bit, /* symbol literal as FNAME. */
95/* examine combinations */
97#define DEF_EXPR(n) EXPR_##n = (1 << EXPR_##n##_bit)
111 EXPR_VALUE = EXPR_BEG,
112 EXPR_BEG_ANY = (EXPR_BEG | EXPR_MID | EXPR_CLASS),
113 EXPR_ARG_ANY = (EXPR_ARG | EXPR_CMDARG),
114 EXPR_END_ANY = (EXPR_END | EXPR_ENDARG | EXPR_ENDFN),
117#define IS_lex_state_for(x, ls) ((x) & (ls))
118#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
119#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
120#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
122# define SET_LEX_STATE(ls) \
125 rb_parser_trace_lex_state(p, p->lex.state, (ls), __LINE__) : \
126 (enum lex_state_e)(ls)))
128typedef VALUE stack_type;
130static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
132# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
133# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
134# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
135# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
136# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
138/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
139 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
140#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
141#define COND_POP() BITSTACK_POP(cond_stack)
142#define COND_P() BITSTACK_SET_P(cond_stack)
143#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
145/* A flag to identify keyword_do_block; "do" keyword after command_call.
146 Example: `foo 1, 2 do`. */
147#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
148#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
149#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
150#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
166 struct local_vars *prev;
169 NODE *outer, *inner, *current;
180#define NUMPARAM_ID_P(id) numparam_id_p(id)
181#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - tNUMPARAM_1 + 1)
182#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 + (idx) - 1))
186 if (!is_local_id(id)) return 0;
187 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
188 return idx > 0 && idx <= NUMPARAM_MAX;
190static void numparam_name(struct parser_params *p, ID id);
192#define DVARS_INHERIT ((void*)1)
193#define DVARS_TOPSCOPE NULL
194#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
196typedef struct token_info {
198 rb_code_position_t beg;
201 struct token_info *next;
204typedef struct rb_strterm_struct rb_strterm_t;
207 Structure of Lexer Buffer:
209 lex.pbeg lex.ptok lex.pcur lex.pend
211 |------------+------------+------------|
215struct parser_params {
216 rb_imemo_tmpbuf_t *heap;
221 rb_strterm_t *strterm;
222 VALUE (*gets)(struct parser_params*,VALUE);
233 VALUE (*call)(VALUE, int);
235 enum lex_state_e state;
236 /* track the nest level of any parens "()[]{}" */
238 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
240 /* track the nest level of only braces "{}" */
243 stack_type cond_stack;
244 stack_type cmdarg_stack;
250 int heredoc_line_indent;
252 struct local_vars *lvtbl;
256 int ruby_sourceline; /* current line no. */
257 const char *ruby_sourcefile; /* current source file */
258 VALUE ruby_sourcefile_string;
260 token_info *token_info;
262 VALUE compile_option;
274 unsigned int command_start:1;
275 unsigned int eofp: 1;
276 unsigned int ruby__end__seen: 1;
277 unsigned int debug: 1;
278 unsigned int has_shebang: 1;
279 unsigned int in_defined: 1;
280 unsigned int in_kwarg: 1;
281 unsigned int in_def: 1;
282 unsigned int in_class: 1;
283 unsigned int token_seen: 1;
284 unsigned int token_info_enabled: 1;
286 unsigned int past_scope_enabled: 1;
288 unsigned int error_p: 1;
289 unsigned int cr_seen: 1;
294 unsigned int do_print: 1;
295 unsigned int do_loop: 1;
296 unsigned int do_chomp: 1;
297 unsigned int do_split: 1;
298 unsigned int warn_location: 1;
300 NODE *eval_tree_begin;
304 const struct rb_iseq_struct *parent_iseq;
316 VALUE parsing_thread;
320#define intern_cstr(n,l,en) rb_intern3(n,l,en)
322#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
323#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
324#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
325#define STR_NEW3(ptr,len,e,func) parser_str_new((ptr),(len),(e),(func),p->enc)
326#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
329push_pvtbl(struct parser_params *p)
331 st_table *tbl = p->pvtbl;
332 p->pvtbl = st_init_numtable();
337pop_pvtbl(struct parser_params *p, st_table *tbl)
339 st_free_table(p->pvtbl);
344push_pktbl(struct parser_params *p)
346 st_table *tbl = p->pktbl;
352pop_pktbl(struct parser_params *p, st_table *tbl)
354 if (p->pktbl) st_free_table(p->pktbl);
358static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
359#define yyerror0(msg) parser_yyerror(p, NULL, (msg))
360#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
361#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
362#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
365#define compile_for_eval (0)
367#define compile_for_eval (p->parent_iseq != 0)
370#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
372#define CALL_Q_P(q) ((q) == TOKEN2VAL(tANDDOT))
373#define NODE_CALL_Q(q) (CALL_Q_P(q) ? NODE_QCALL : NODE_CALL)
374#define NEW_QCALL(q,r,m,a,loc) NEW_NODE(NODE_CALL_Q(q),r,m,a,loc)
376#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
378static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
382rb_discard_node(struct parser_params *p, NODE *n)
384 rb_ast_delete_node(p->ast, n);
390add_mark_object(struct parser_params *p, VALUE obj)
392 if (!SPECIAL_CONST_P(obj)
393 && !RB_TYPE_P(obj, T_NODE) /* Ripper jumbles NODE objects and other objects... */
395 rb_ast_add_mark_object(p->ast, obj);
400static NODE* node_newnode_with_locals(struct parser_params *, enum node_type, VALUE, VALUE, const rb_code_location_t*);
403static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE, const rb_code_location_t*);
404#define rb_node_newnode(type, a1, a2, a3, loc) node_newnode(p, (type), (a1), (a2), (a3), (loc))
406static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
409parser_get_node_id(struct parser_params *p)
411 int node_id = p->node_id;
418set_line_body(NODE *body, int line)
421 switch (nd_type(body)) {
424 nd_set_line(body, line);
428#define yyparse ruby_yyparse
430static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
431static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
432#define new_nil(loc) NEW_NIL(loc)
433static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
434static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
435static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
437static NODE *newline_node(NODE*);
438static void fixpos(NODE*,NODE*);
440static int value_expr_gen(struct parser_params*,NODE*);
441static void void_expr(struct parser_params*,NODE*);
442static NODE *remove_begin(NODE*);
443static NODE *remove_begin_all(NODE*);
444#define value_expr(node) value_expr_gen(p, (node) = remove_begin(node))
445static NODE *void_stmts(struct parser_params*,NODE*);
446static void reduce_nodes(struct parser_params*,NODE**);
447static void block_dup_check(struct parser_params*,NODE*,NODE*);
449static NODE *block_append(struct parser_params*,NODE*,NODE*);
450static NODE *list_append(struct parser_params*,NODE*,NODE*);
451static NODE *list_concat(NODE*,NODE*);
452static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
453static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
454static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
455static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
456static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
457static NODE *evstr2dstr(struct parser_params*,NODE*);
458static NODE *splat_array(NODE*);
459static void mark_lvar_used(struct parser_params *p, NODE *rhs);
461static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
462static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
463static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
464static 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);
465static 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;}
467static bool args_info_empty_p(struct rb_args_info *args);
468static NODE *new_args(struct parser_params*,NODE*,NODE*,ID,NODE*,NODE*,const YYLTYPE*);
469static NODE *new_args_tail(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
470static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
471static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc);
472static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
473static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
474static NODE *new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc);
476static NODE *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
477static NODE *args_with_numbered(struct parser_params*,NODE*,int);
479static VALUE negate_lit(struct parser_params*, VALUE);
480static NODE *ret_args(struct parser_params*,NODE*);
481static NODE *arg_blk_pass(NODE*,NODE*);
482static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
483static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
485static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
486static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
488static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
489static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
491static void rb_backref_error(struct parser_params*,NODE*);
492static NODE *node_assign(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
494static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
495static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc);
496static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc);
497static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc);
498static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
500static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
502static NODE *opt_arg_append(NODE*, NODE*);
503static NODE *kwd_append(NODE*, NODE*);
505static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
506static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
508static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
510static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
512#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
514static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
516static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
518static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
520static ID *local_tbl(struct parser_params*);
522static VALUE reg_compile(struct parser_params*, VALUE, int);
523static void reg_fragment_setenc(struct parser_params*, VALUE, int);
524static int reg_fragment_check(struct parser_params*, VALUE, int);
525static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
527static int literal_concat0(struct parser_params *p, VALUE head, VALUE tail);
528static NODE *heredoc_dedent(struct parser_params*,NODE*);
530static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
532#define get_id(id) (id)
533#define get_value(val) (val)
534#define get_num(num) (num)
536#define NODE_RIPPER NODE_CDECL
538static inline int ripper_is_node_yylval(VALUE n);
541ripper_new_yylval(struct parser_params *p, ID a, VALUE b, VALUE c)
543 if (ripper_is_node_yylval(c)) c = RNODE(c)->nd_cval;
544 add_mark_object(p, b);
545 add_mark_object(p, c);
546 return (VALUE)NEW_CDECL(a, b, c, &NULL_LOC);
550ripper_is_node_yylval(VALUE n)
552 return RB_TYPE_P(n, T_NODE) && nd_type(RNODE(n)) == NODE_RIPPER;
555#define value_expr(node) ((void)(node))
556#define remove_begin(node) (node)
557#define void_stmts(p,x) (x)
558#define rb_dvar_defined(id, base) 0
559#define rb_local_defined(id, base) 0
560static ID ripper_get_id(VALUE);
561#define get_id(id) ripper_get_id(id)
562static VALUE ripper_get_value(VALUE);
563#define get_value(val) ripper_get_value(val)
564#define get_num(num) (int)get_id(num)
565static VALUE assignable(struct parser_params*,VALUE);
566static int id_is_var(struct parser_params *p, ID id);
568#define method_cond(p,node,loc) (node)
569#define call_bin_op(p, recv,id,arg1,op_loc,loc) dispatch3(binary, (recv), STATIC_ID2SYM(id), (arg1))
570#define match_op(p,node1,node2,op_loc,loc) call_bin_op(0, (node1), idEqTilde, (node2), op_loc, loc)
571#define call_uni_op(p, recv,id,op_loc,loc) dispatch2(unary, STATIC_ID2SYM(id), (recv))
572#define logop(p,id,node1,node2,op_loc,loc) call_bin_op(0, (node1), (id), (node2), op_loc, loc)
574#define new_nil(loc) Qnil
576static VALUE new_regexp(struct parser_params *, VALUE, VALUE, const YYLTYPE *);
578static VALUE const_decl(struct parser_params *p, VALUE path);
580static VALUE var_field(struct parser_params *p, VALUE a);
581static VALUE assign_error(struct parser_params *p, VALUE a);
583static VALUE parser_reg_compile(struct parser_params*, VALUE, int, VALUE *);
587/* forward declaration */
588typedef struct rb_strterm_heredoc_struct rb_strterm_heredoc_t;
590RUBY_SYMBOL_EXPORT_BEGIN
591VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
592int rb_reg_fragment_setenc(struct parser_params*, VALUE, int);
593enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
594VALUE rb_parser_lex_state_name(enum lex_state_e state);
595void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
596PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
597YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
598YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
599YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
600RUBY_SYMBOL_EXPORT_END
602static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
603static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
604static ID formal_argument(struct parser_params*, ID);
605static ID shadowing_lvar(struct parser_params*,ID);
606static void new_bv(struct parser_params*,ID);
608static void local_push(struct parser_params*,int);
609static void local_pop(struct parser_params*);
610static void local_var(struct parser_params*, ID);
611static void arg_var(struct parser_params*, ID);
612static int local_id(struct parser_params *p, ID id);
613static int local_id_ref(struct parser_params*, ID, ID **);
615static ID internal_id(struct parser_params*);
618static const struct vtable *dyna_push(struct parser_params *);
619static void dyna_pop(struct parser_params*, const struct vtable *);
620static int dyna_in_block(struct parser_params*);
621#define dyna_var(p, id) local_var(p, id)
622static int dvar_defined(struct parser_params*, ID);
623static int dvar_defined_ref(struct parser_params*, ID, ID**);
624static int dvar_curr(struct parser_params*,ID);
626static int lvar_defined(struct parser_params*, ID);
628static NODE *numparam_push(struct parser_params *p);
629static void numparam_pop(struct parser_params *p, NODE *prev_inner);
632# define METHOD_NOT idNOT
634# define METHOD_NOT '!'
637#define idFWD_REST '*'
639#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
641#define idFWD_KWREST 0
643#define idFWD_BLOCK '&'
645#define RE_OPTION_ONCE (1<<16)
646#define RE_OPTION_ENCODING_SHIFT 8
647#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
648#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
649#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
650#define RE_OPTION_MASK 0xff
651#define RE_OPTION_ARG_ENCODING_NONE 32
653/* structs for managing terminator of string literal and heredocment */
654typedef struct rb_strterm_literal_struct {
661 long func; /* STR_FUNC_* (e.g., STR_FUNC_ESCAPE and STR_FUNC_EXPAND) */
665 long paren; /* '(' of `%q(...)` */
669 long term; /* ')' of `%q(...)` */
671} rb_strterm_literal_t;
673#define HERETERM_LENGTH_BITS ((SIZEOF_VALUE - 1) * CHAR_BIT - 1)
675struct rb_strterm_heredoc_struct {
676 VALUE lastline; /* the string of line that contains `<<"END"` */
677 long offset; /* the column of END in `<<"END"` */
678 int sourceline; /* lineno of the line that contains `<<"END"` */
679 unsigned length /* the length of END in `<<"END"` */
680#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
681 : HERETERM_LENGTH_BITS
682# define HERETERM_LENGTH_MAX ((1U << HERETERM_LENGTH_BITS) - 1)
684# define HERETERM_LENGTH_MAX UINT_MAX
687#if HERETERM_LENGTH_BITS < SIZEOF_INT * CHAR_BIT
695STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
697#define STRTERM_HEREDOC IMEMO_FL_USER0
699struct rb_strterm_struct {
702 rb_strterm_literal_t literal;
703 rb_strterm_heredoc_t heredoc;
709rb_strterm_mark(VALUE obj)
711 rb_strterm_t *strterm = (rb_strterm_t*)obj;
712 if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
713 rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
714 rb_gc_mark(heredoc->lastline);
719#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
720size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
722#define TOKEN2ID(tok) ( \
723 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
724 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
725 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
726 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
727 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
728 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
729 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
731/****** Ripper *******/
734#define RIPPER_VERSION "0.1.0"
736static inline VALUE intern_sym(const char *name);
738#include "eventids1.c"
739#include "eventids2.c"
741static VALUE ripper_dispatch0(struct parser_params*,ID);
742static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
743static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
744static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
745static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
746static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
747static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
748static void ripper_error(struct parser_params *p);
750#define dispatch0(n) ripper_dispatch0(p, TOKEN_PASTE(ripper_id_, n))
751#define dispatch1(n,a) ripper_dispatch1(p, TOKEN_PASTE(ripper_id_, n), (a))
752#define dispatch2(n,a,b) ripper_dispatch2(p, TOKEN_PASTE(ripper_id_, n), (a), (b))
753#define dispatch3(n,a,b,c) ripper_dispatch3(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
754#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
755#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
756#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))
758#define yyparse ripper_yyparse
760#define ID2VAL(id) STATIC_ID2SYM(id)
761#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
762#define KWD2EID(t, v) ripper_new_yylval(p, keyword_##t, get_value(v), 0)
764#define params_new(pars, opts, rest, pars2, kws, kwrest, blk) \
765 dispatch7(params, (pars), (opts), (rest), (pars2), (kws), (kwrest), (blk))
767#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
770new_args(struct parser_params *p, VALUE pre_args, VALUE opt_args, VALUE rest_arg, VALUE post_args, VALUE tail, YYLTYPE *loc)
772 NODE *t = (NODE *)tail;
773 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value, block = t->u3.value;
774 return params_new(pre_args, opt_args, rest_arg, post_args, kw_args, kw_rest_arg, escape_Qundef(block));
778new_args_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, VALUE block, YYLTYPE *loc)
780 NODE *t = rb_node_newnode(NODE_ARGS_AUX, kw_args, kw_rest_arg, block, &NULL_LOC);
781 add_mark_object(p, kw_args);
782 add_mark_object(p, kw_rest_arg);
783 add_mark_object(p, block);
788args_with_numbered(struct parser_params *p, VALUE args, int max_numparam)
794new_array_pattern(struct parser_params *p, VALUE constant, VALUE pre_arg, VALUE aryptn, const YYLTYPE *loc)
796 NODE *t = (NODE *)aryptn;
797 struct rb_ary_pattern_info *apinfo = t->nd_apinfo;
798 VALUE pre_args = Qnil, rest_arg = Qnil, post_args = Qnil;
801 pre_args = rb_ary_entry(apinfo->imemo, 0);
802 rest_arg = rb_ary_entry(apinfo->imemo, 1);
803 post_args = rb_ary_entry(apinfo->imemo, 2);
806 if (!NIL_P(pre_arg)) {
807 if (!NIL_P(pre_args)) {
808 rb_ary_unshift(pre_args, pre_arg);
811 pre_args = rb_ary_new_from_args(1, pre_arg);
814 return dispatch4(aryptn, constant, pre_args, rest_arg, post_args);
818new_array_pattern_tail(struct parser_params *p, VALUE pre_args, VALUE has_rest, VALUE rest_arg, VALUE post_args, const YYLTYPE *loc)
821 struct rb_ary_pattern_info *apinfo;
824 rest_arg = dispatch1(var_field, rest_arg ? rest_arg : Qnil);
830 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
831 apinfo = ZALLOC(struct rb_ary_pattern_info);
832 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
833 apinfo->imemo = rb_ary_new_from_args(4, pre_args, rest_arg, post_args, tmpbuf);
835 t = rb_node_newnode(NODE_ARYPTN, Qnil, Qnil, (VALUE)apinfo, &NULL_LOC);
836 RB_OBJ_WRITTEN(p->ast, Qnil, apinfo->imemo);
841#define new_hash(p,h,l) rb_ary_new_from_args(0)
844new_unique_key_hash(struct parser_params *p, VALUE ary, const YYLTYPE *loc)
850new_hash_pattern(struct parser_params *p, VALUE constant, VALUE hshptn, const YYLTYPE *loc)
852 NODE *t = (NODE *)hshptn;
853 VALUE kw_args = t->u1.value, kw_rest_arg = t->u2.value;
854 return dispatch3(hshptn, constant, kw_args, kw_rest_arg);
858new_hash_pattern_tail(struct parser_params *p, VALUE kw_args, VALUE kw_rest_arg, const YYLTYPE *loc)
862 kw_rest_arg = dispatch1(var_field, kw_rest_arg);
867 t = rb_node_newnode(NODE_HSHPTN, kw_args, kw_rest_arg, 0, &NULL_LOC);
869 add_mark_object(p, kw_args);
870 add_mark_object(p, kw_rest_arg);
874#define new_defined(p,expr,loc) dispatch1(defined, (expr))
876static VALUE heredoc_dedent(struct parser_params*,VALUE);
879#define ID2VAL(id) (id)
880#define TOKEN2VAL(t) ID2VAL(t)
881#define KWD2EID(t, v) keyword_##t
887# define ifndef_ripper(x) (x)
891# define ifndef_ripper(x)
894# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
895# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
896# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
897# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
898# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
899# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
900# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
901# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
902# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
903# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
904# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
905# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
906# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
907# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
908# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
909# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
910# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
911# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
912# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
913# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
915static ID id_warn, id_warning, id_gets, id_assoc;
916# define WARN_S_L(s,l) STR_NEW(s,l)
917# define WARN_S(s) STR_NEW2(s)
918# define WARN_I(i) INT2NUM(i)
919# define WARN_ID(i) rb_id2str(i)
920# define WARN_IVAL(i) i
922# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
923# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
924# ifdef HAVE_VA_ARGS_MACRO
925# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
927# define WARN_CALL rb_funcall
929# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
930# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
931# ifdef HAVE_VA_ARGS_MACRO
932# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
934# define WARNING_CALL rb_funcall
936PRINTF_ARGS(static void ripper_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
937# define compile_error ripper_compile_error
939# define WARN_S_L(s,l) s
942# define WARN_ID(i) rb_id2name(i)
943# define WARN_IVAL(i) NUM2INT(i)
944# define PRIsWARN PRIsVALUE
945# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
946# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
947# define WARN_CALL rb_compile_warn
948# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
949# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
950# define WARNING_CALL rb_compile_warning
951PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const char *fmt, ...), 2, 3);
952# define compile_error parser_compile_error
955static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
956static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
957static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
958static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
960#define WARN_EOL(tok) \
961 (looking_at_eol_p(p) ? \
962 (void)rb_warning0("`" tok "' at the end of line without an expression") : \
964static int looking_at_eol_p(struct parser_params *p);
969%define parse.error verbose
972 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
974 rb_parser_printf(p, "%"PRIsVALUE, RNODE($$)->nd_rval);
976} tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL tOP_ASGN
979 rb_parser_printf(p, "%+"PRIsVALUE, $$->nd_lit);
981 rb_parser_printf(p, "%+"PRIsVALUE, get_value($$));
983} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
986 rb_parser_printf(p, "$%ld", $$->nd_nth);
988 rb_parser_printf(p, "%"PRIsVALUE, $$);
993 rb_parser_printf(p, "$%c", (int)$$->nd_nth);
995 rb_parser_printf(p, "%"PRIsVALUE, $$);
999%lex-param {struct parser_params *p}
1000%parse-param {struct parser_params *p}
1003 RUBY_SET_YYLLOC_OF_NONE(@$);
1012 const struct vtable *vars;
1013 struct rb_strterm_struct *strterm;
1017 keyword_class "`class'"
1018 keyword_module "`module'"
1020 keyword_undef "`undef'"
1021 keyword_begin "`begin'"
1022 keyword_rescue "`rescue'"
1023 keyword_ensure "`ensure'"
1026 keyword_unless "`unless'"
1027 keyword_then "`then'"
1028 keyword_elsif "`elsif'"
1029 keyword_else "`else'"
1030 keyword_case "`case'"
1031 keyword_when "`when'"
1032 keyword_while "`while'"
1033 keyword_until "`until'"
1035 keyword_break "`break'"
1036 keyword_next "`next'"
1037 keyword_redo "`redo'"
1038 keyword_retry "`retry'"
1041 keyword_do_cond "`do' for condition"
1042 keyword_do_block "`do' for block"
1043 keyword_do_LAMBDA "`do' for lambda"
1044 keyword_return "`return'"
1045 keyword_yield "`yield'"
1046 keyword_super "`super'"
1047 keyword_self "`self'"
1049 keyword_true "`true'"
1050 keyword_false "`false'"
1054 modifier_if "`if' modifier"
1055 modifier_unless "`unless' modifier"
1056 modifier_while "`while' modifier"
1057 modifier_until "`until' modifier"
1058 modifier_rescue "`rescue' modifier"
1059 keyword_alias "`alias'"
1060 keyword_defined "`defined?'"
1061 keyword_BEGIN "`BEGIN'"
1063 keyword__LINE__ "`__LINE__'"
1064 keyword__FILE__ "`__FILE__'"
1065 keyword__ENCODING__ "`__ENCODING__'"
1067%token <id> tIDENTIFIER "local variable or method"
1068%token <id> tFID "method"
1069%token <id> tGVAR "global variable"
1070%token <id> tIVAR "instance variable"
1071%token <id> tCONSTANT "constant"
1072%token <id> tCVAR "class variable"
1074%token <node> tINTEGER "integer literal"
1075%token <node> tFLOAT "float literal"
1076%token <node> tRATIONAL "rational literal"
1077%token <node> tIMAGINARY "imaginary literal"
1078%token <node> tCHAR "char literal"
1079%token <node> tNTH_REF "numbered reference"
1080%token <node> tBACK_REF "back reference"
1081%token <node> tSTRING_CONTENT "literal content"
1082%token <num> tREGEXP_END
1084%type <node> singleton strings string string1 xstring regexp
1085%type <node> string_contents xstring_contents regexp_contents string_content
1086%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
1087%type <node> literal numeric simple_numeric ssym dsym symbol cpath
1088%type <node> top_compstmt top_stmts top_stmt begin_block
1089%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
1090%type <node> expr_value expr_value_do arg_value primary_value fcall rel_expr
1091%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
1092%type <node> args call_args opt_call_args
1093%type <node> paren_args opt_paren_args args_tail opt_args_tail block_args_tail opt_block_args_tail
1094%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
1095%type <node> command_rhs arg_rhs
1096%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
1097%type <node> f_block_optarg f_block_opt
1098%type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs f_rest_marg
1099%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
1100%type <node> block_param opt_block_param block_param_def f_opt
1101%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
1102%type <node> bv_decls opt_bv_decl bvar
1103%type <node> lambda f_larglist lambda_body brace_body do_body
1104%type <node> brace_block cmd_brace_block do_block lhs none fitem
1105%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
1106%type <node> p_case_body p_cases p_top_expr p_top_expr_body
1107%type <node> p_expr p_as p_alt p_expr_basic
1108%type <node> p_args p_args_head p_args_tail p_args_post p_arg
1109%type <node> p_value p_primitive p_variable p_var_ref p_const
1110%type <node> p_kwargs p_kwarg p_kw
1111%type <id> keyword_variable user_variable sym operation operation2 operation3
1112%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
1113%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
1114%type <id> p_kwrest p_kwnorest p_kw_label
1115%type <id> f_no_kwarg args_forward
1116%token END_OF_INPUT 0 "end-of-input"
1118/* escaped chars, should be ignored otherwise */
1119%token <id> '\\' "backslash"
1120%token tSP "escaped space"
1121%token <id> '\t' "escaped horizontal tab"
1122%token <id> '\f' "escaped form feed"
1123%token <id> '\r' "escaped carriage return"
1124%token <id> '\13' "escaped vertical tab"
1125%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
1126%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
1127%token tPOW RUBY_TOKEN(POW) "**"
1128%token tCMP RUBY_TOKEN(CMP) "<=>"
1129%token tEQ RUBY_TOKEN(EQ) "=="
1130%token tEQQ RUBY_TOKEN(EQQ) "==="
1131%token tNEQ RUBY_TOKEN(NEQ) "!="
1132%token tGEQ RUBY_TOKEN(GEQ) ">="
1133%token tLEQ RUBY_TOKEN(LEQ) "<="
1134%token tANDOP RUBY_TOKEN(ANDOP) "&&"
1135%token tOROP RUBY_TOKEN(OROP) "||"
1136%token tMATCH RUBY_TOKEN(MATCH) "=~"
1137%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
1138%token tDOT2 RUBY_TOKEN(DOT2) ".."
1139%token tDOT3 RUBY_TOKEN(DOT3) "..."
1140%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
1141%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
1142%token tAREF RUBY_TOKEN(AREF) "[]"
1143%token tASET RUBY_TOKEN(ASET) "[]="
1144%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
1145%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
1146%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
1147%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
1148%token tCOLON3 ":: at EXPR_BEG"
1149%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
1152%token tLPAREN_ARG "( arg"
1156%token tLBRACE_ARG "{ arg"
1158%token tDSTAR "**arg"
1161%token tSYMBEG "symbol literal"
1162%token tSTRING_BEG "string literal"
1163%token tXSTRING_BEG "backtick literal"
1164%token tREGEXP_BEG "regexp literal"
1165%token tWORDS_BEG "word list"
1166%token tQWORDS_BEG "verbatim word list"
1167%token tSYMBOLS_BEG "symbol list"
1168%token tQSYMBOLS_BEG "verbatim symbol list"
1169%token tSTRING_END "terminator"
1170%token tSTRING_DEND "'}'"
1171%token tSTRING_DBEG tSTRING_DVAR tLAMBEG tLABEL_END
1178%nonassoc tLBRACE_ARG
1180%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
1181%left keyword_or keyword_and
1183%nonassoc keyword_defined
1185%left modifier_rescue
1187%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
1190%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
1191%left '>' tGEQ '<' tLEQ
1197%right tUMINUS_NUM tUMINUS
1199%right '!' '~' tUPLUS
1205 SET_LEX_STATE(EXPR_BEG);
1206 local_push(p, ifndef_ripper(1)+0);
1211 if ($2 && !compile_for_eval) {
1213 /* last expression should not be void */
1214 if (nd_type(node) == NODE_BLOCK) {
1215 while (node->nd_next) {
1216 node = node->nd_next;
1218 node = node->nd_head;
1220 node = remove_begin(node);
1223 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
1225 /*% ripper[final]: program!($2) %*/
1230top_compstmt : top_stmts opt_terms
1232 $$ = void_stmts(p, $1);
1239 $$ = NEW_BEGIN(0, &@$);
1241 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1246 $$ = newline_node($1);
1248 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1250 | top_stmts terms top_stmt
1253 $$ = block_append(p, $1, newline_node($3));
1255 /*% ripper: stmts_add!($1, $3) %*/
1259 $$ = remove_begin($2);
1264 | keyword_BEGIN begin_block
1270begin_block : '{' top_compstmt '}'
1273 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
1274 NEW_BEGIN($2, &@$));
1275 $$ = NEW_BEGIN(0, &@$);
1277 /*% ripper: BEGIN!($2) %*/
1283 k_else {if (!$2) {yyerror1(&@3, "else without rescue is useless");}}
1288 $$ = new_bodystmt(p, $1, $2, $5, $6, &@$);
1290 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), escape_Qundef($5), escape_Qundef($6)) %*/
1297 $$ = new_bodystmt(p, $1, $2, 0, $3, &@$);
1299 /*% ripper: bodystmt!(escape_Qundef($1), escape_Qundef($2), Qnil, escape_Qundef($3)) %*/
1303compstmt : stmts opt_terms
1305 $$ = void_stmts(p, $1);
1312 $$ = NEW_BEGIN(0, &@$);
1314 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
1319 $$ = newline_node($1);
1321 /*% ripper: stmts_add!(stmts_new!, $1) %*/
1323 | stmts terms stmt_or_begin
1326 $$ = block_append(p, $1, newline_node($3));
1328 /*% ripper: stmts_add!($1, $3) %*/
1332 $$ = remove_begin($2);
1342 yyerror1(&@1, "BEGIN is permitted only at toplevel");
1350stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
1353 $$ = NEW_ALIAS($2, $4, &@$);
1355 /*% ripper: alias!($2, $4) %*/
1357 | keyword_alias tGVAR tGVAR
1360 $$ = NEW_VALIAS($2, $3, &@$);
1362 /*% ripper: var_alias!($2, $3) %*/
1364 | keyword_alias tGVAR tBACK_REF
1369 buf[1] = (char)$3->nd_nth;
1370 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$);
1372 /*% ripper: var_alias!($2, $3) %*/
1374 | keyword_alias tGVAR tNTH_REF
1377 yyerror1(&@3, "can't make alias for the number variables");
1378 $$ = NEW_BEGIN(0, &@$);
1380 /*% ripper[error]: alias_error!(var_alias!($2, $3)) %*/
1382 | keyword_undef undef_list
1387 /*% ripper: undef!($2) %*/
1389 | stmt modifier_if expr_value
1392 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
1395 /*% ripper: if_mod!($3, $1) %*/
1397 | stmt modifier_unless expr_value
1400 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
1403 /*% ripper: unless_mod!($3, $1) %*/
1405 | stmt modifier_while expr_value
1408 if ($1 && nd_type($1) == NODE_BEGIN) {
1409 $$ = NEW_WHILE(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1412 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$);
1415 /*% ripper: while_mod!($3, $1) %*/
1417 | stmt modifier_until expr_value
1420 if ($1 && nd_type($1) == NODE_BEGIN) {
1421 $$ = NEW_UNTIL(cond(p, $3, &@3), $1->nd_body, 0, &@$);
1424 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$);
1427 /*% ripper: until_mod!($3, $1) %*/
1429 | stmt modifier_rescue stmt
1433 YYLTYPE loc = code_loc_gen(&@2, &@3);
1434 resq = NEW_RESBODY(0, remove_begin($3), 0, &loc);
1435 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
1437 /*% ripper: rescue_mod!($1, $3) %*/
1439 | keyword_END '{' compstmt '}'
1442 rb_warn0("END in method; use at_exit");
1446 NODE *scope = NEW_NODE(
1447 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */, &@$);
1448 $$ = NEW_POSTEXE(scope, &@$);
1451 /*% ripper: END!($3) %*/
1454 | mlhs '=' command_call
1458 $$ = node_assign(p, $1, $3, &@$);
1460 /*% ripper: massign!($1, $3) %*/
1466 $$ = node_assign(p, $1, $3, &@$);
1468 /*% ripper: assign!($1, $3) %*/
1470 | mlhs '=' mrhs_arg modifier_rescue stmt
1473 YYLTYPE loc = code_loc_gen(&@4, &@5);
1475 $$ = node_assign(p, $1, NEW_RESCUE($3, NEW_RESBODY(0, remove_begin($5), 0, &loc), 0, &@$), &@$);
1477 /*% ripper: massign!($1, rescue_mod!($3, $5)) %*/
1482 $$ = node_assign(p, $1, $3, &@$);
1484 /*% ripper: massign!($1, $3) %*/
1489command_asgn : lhs '=' command_rhs
1492 $$ = node_assign(p, $1, $3, &@$);
1494 /*% ripper: assign!($1, $3) %*/
1496 | var_lhs tOP_ASGN command_rhs
1499 $$ = new_op_assign(p, $1, $2, $3, &@$);
1501 /*% ripper: opassign!($1, $2, $3) %*/
1503 | primary_value '[' opt_call_args rbracket tOP_ASGN command_rhs
1506 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
1508 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $6) %*/
1511 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs
1514 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1516 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
1518 | primary_value call_op tCONSTANT tOP_ASGN command_rhs
1521 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
1523 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
1525 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs
1528 YYLTYPE loc = code_loc_gen(&@1, &@3);
1529 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
1531 /*% ripper: opassign!(const_path_field!($1, $3), $4, $5) %*/
1533 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs
1536 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
1538 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $5) %*/
1540 | backref tOP_ASGN command_rhs
1543 rb_backref_error(p, $1);
1544 $$ = NEW_BEGIN(0, &@$);
1546 /*% ripper[error]: assign_error!(assign!(var_field(p, $1), $3)) %*/
1550command_rhs : command_call %prec tOP_ASGN
1555 | command_call modifier_rescue stmt
1558 YYLTYPE loc = code_loc_gen(&@2, &@3);
1560 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
1562 /*% ripper: rescue_mod!($1, $3) %*/
1568 | expr keyword_and expr
1570 $$ = logop(p, idAND, $1, $3, &@2, &@$);
1572 | expr keyword_or expr
1574 $$ = logop(p, idOR, $1, $3, &@2, &@$);
1576 | keyword_not opt_nl expr
1578 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
1582 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
1587 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
1588 p->command_start = FALSE;
1589 $<num>$ = p->in_kwarg;
1592 {$<tbl>$ = push_pvtbl(p);}
1594 {pop_pvtbl(p, $<tbl>4);}
1596 p->in_kwarg = !!$<num>3;
1598 $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$);
1600 /*% ripper: case!($1, in!($5, Qnil, Qnil)) %*/
1602 | arg %prec tLBRACE_ARG
1612expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
1618command_call : command
1622block_command : block_call
1623 | block_call call_op2 operation2 command_args
1626 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
1628 /*% ripper: method_add_arg!(call!($1, $2, $3), $4) %*/
1632cmd_brace_block : tLBRACE_ARG brace_body '}'
1636 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
1637 nd_set_line($$, @1.end_pos.lineno);
1645 $$ = NEW_FCALL($1, 0, &@$);
1646 nd_set_line($$, p->tokline);
1652command : fcall command_args %prec tLOWEST
1656 nd_set_last_loc($1, @2.end_pos);
1659 /*% ripper: command!($1, $2) %*/
1661 | fcall command_args cmd_brace_block
1664 block_dup_check(p, $2, $3);
1666 $$ = method_add_block(p, $1, $3, &@$);
1668 nd_set_last_loc($1, @2.end_pos);
1670 /*% ripper: method_add_block!(command!($1, $2), $3) %*/
1672 | primary_value call_op operation2 command_args %prec tLOWEST
1675 $$ = new_command_qcall(p, $2, $1, $3, $4, Qnull, &@3, &@$);
1677 /*% ripper: command_call!($1, $2, $3, $4) %*/
1679 | primary_value call_op operation2 command_args cmd_brace_block
1682 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
1684 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
1686 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
1689 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, Qnull, &@3, &@$);
1691 /*% ripper: command_call!($1, ID2VAL(idCOLON2), $3, $4) %*/
1693 | primary_value tCOLON2 operation2 command_args cmd_brace_block
1696 $$ = new_command_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, $5, &@3, &@$);
1698 /*% ripper: method_add_block!(command_call!($1, ID2VAL(idCOLON2), $3, $4), $5) %*/
1700 | keyword_super command_args
1703 $$ = NEW_SUPER($2, &@$);
1706 /*% ripper: super!($2) %*/
1708 | keyword_yield command_args
1711 $$ = new_yield(p, $2, &@$);
1714 /*% ripper: yield!($2) %*/
1716 | k_return call_args
1719 $$ = NEW_RETURN(ret_args(p, $2), &@$);
1721 /*% ripper: return!($2) %*/
1723 | keyword_break call_args
1726 $$ = NEW_BREAK(ret_args(p, $2), &@$);
1728 /*% ripper: break!($2) %*/
1730 | keyword_next call_args
1733 $$ = NEW_NEXT(ret_args(p, $2), &@$);
1735 /*% ripper: next!($2) %*/
1740 | tLPAREN mlhs_inner rparen
1745 /*% ripper: mlhs_paren!($2) %*/
1749mlhs_inner : mlhs_basic
1750 | tLPAREN mlhs_inner rparen
1753 $$ = NEW_MASGN(NEW_LIST($2, &@$), 0, &@$);
1755 /*% ripper: mlhs_paren!($2) %*/
1759mlhs_basic : mlhs_head
1762 $$ = NEW_MASGN($1, 0, &@$);
1766 | mlhs_head mlhs_item
1769 $$ = NEW_MASGN(list_append(p, $1,$2), 0, &@$);
1771 /*% ripper: mlhs_add!($1, $2) %*/
1773 | mlhs_head tSTAR mlhs_node
1776 $$ = NEW_MASGN($1, $3, &@$);
1778 /*% ripper: mlhs_add_star!($1, $3) %*/
1780 | mlhs_head tSTAR mlhs_node ',' mlhs_post
1783 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
1785 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
1790 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
1792 /*% ripper: mlhs_add_star!($1, Qnil) %*/
1794 | mlhs_head tSTAR ',' mlhs_post
1797 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
1799 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, Qnil), $4) %*/
1804 $$ = NEW_MASGN(0, $2, &@$);
1806 /*% ripper: mlhs_add_star!(mlhs_new!, $2) %*/
1808 | tSTAR mlhs_node ',' mlhs_post
1811 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
1813 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $2), $4) %*/
1818 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
1820 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
1822 | tSTAR ',' mlhs_post
1825 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
1827 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $3) %*/
1831mlhs_item : mlhs_node
1832 | tLPAREN mlhs_inner rparen
1837 /*% ripper: mlhs_paren!($2) %*/
1841mlhs_head : mlhs_item ','
1844 $$ = NEW_LIST($1, &@1);
1846 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
1848 | mlhs_head mlhs_item ','
1851 $$ = list_append(p, $1, $2);
1853 /*% ripper: mlhs_add!($1, $2) %*/
1857mlhs_post : mlhs_item
1860 $$ = NEW_LIST($1, &@$);
1862 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
1864 | mlhs_post ',' mlhs_item
1867 $$ = list_append(p, $1, $3);
1869 /*% ripper: mlhs_add!($1, $3) %*/
1873mlhs_node : user_variable
1876 $$ = assignable(p, $1, 0, &@$);
1878 /*% ripper: assignable(p, var_field(p, $1)) %*/
1883 $$ = assignable(p, $1, 0, &@$);
1885 /*% ripper: assignable(p, var_field(p, $1)) %*/
1887 | primary_value '[' opt_call_args rbracket
1890 $$ = aryset(p, $1, $3, &@$);
1892 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
1894 | primary_value call_op tIDENTIFIER
1896 if ($2 == tANDDOT) {
1897 yyerror1(&@2, "&. inside multiple assignment destination");
1900 $$ = attrset(p, $1, $2, $3, &@$);
1902 /*% ripper: field!($1, $2, $3) %*/
1904 | primary_value tCOLON2 tIDENTIFIER
1907 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1909 /*% ripper: const_path_field!($1, $3) %*/
1911 | primary_value call_op tCONSTANT
1913 if ($2 == tANDDOT) {
1914 yyerror1(&@2, "&. inside multiple assignment destination");
1917 $$ = attrset(p, $1, $2, $3, &@$);
1919 /*% ripper: field!($1, $2, $3) %*/
1921 | primary_value tCOLON2 tCONSTANT
1924 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1926 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
1931 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1933 /*% ripper: const_decl(p, top_const_field!($2)) %*/
1938 rb_backref_error(p, $1);
1939 $$ = NEW_BEGIN(0, &@$);
1941 /*% ripper[error]: assign_error!(var_field(p, $1)) %*/
1948 $$ = assignable(p, $1, 0, &@$);
1950 /*% ripper: assignable(p, var_field(p, $1)) %*/
1955 $$ = assignable(p, $1, 0, &@$);
1957 /*% ripper: assignable(p, var_field(p, $1)) %*/
1959 | primary_value '[' opt_call_args rbracket
1962 $$ = aryset(p, $1, $3, &@$);
1964 /*% ripper: aref_field!($1, escape_Qundef($3)) %*/
1966 | primary_value call_op tIDENTIFIER
1969 $$ = attrset(p, $1, $2, $3, &@$);
1971 /*% ripper: field!($1, $2, $3) %*/
1973 | primary_value tCOLON2 tIDENTIFIER
1976 $$ = attrset(p, $1, idCOLON2, $3, &@$);
1978 /*% ripper: field!($1, ID2VAL(idCOLON2), $3) %*/
1980 | primary_value call_op tCONSTANT
1983 $$ = attrset(p, $1, $2, $3, &@$);
1985 /*% ripper: field!($1, $2, $3) %*/
1987 | primary_value tCOLON2 tCONSTANT
1990 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
1992 /*% ripper: const_decl(p, const_path_field!($1, $3)) %*/
1997 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
1999 /*% ripper: const_decl(p, top_const_field!($2)) %*/
2004 rb_backref_error(p, $1);
2005 $$ = NEW_BEGIN(0, &@$);
2007 /*% ripper[error]: assign_error!(var_field(p, $1)) %*/
2014 yyerror1(&@1, "class/module name must be CONSTANT");
2016 /*% ripper[error]: class_name_error!($1) %*/
2021cpath : tCOLON3 cname
2024 $$ = NEW_COLON3($2, &@$);
2026 /*% ripper: top_const_ref!($2) %*/
2031 $$ = NEW_COLON2(0, $$, &@$);
2033 /*% ripper: const_ref!($1) %*/
2035 | primary_value tCOLON2 cname
2038 $$ = NEW_COLON2($1, $3, &@$);
2040 /*% ripper: const_path_ref!($1, $3) %*/
2049 SET_LEX_STATE(EXPR_ENDFN);
2058 $$ = NEW_LIT(ID2SYM($1), &@$);
2060 /*% ripper: symbol_literal!($1) %*/
2068 $$ = NEW_UNDEF($1, &@$);
2070 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
2072 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
2075 NODE *undef = NEW_UNDEF($4, &@4);
2076 $$ = block_append(p, $1, undef);
2078 /*% ripper: rb_ary_push($1, get_value($4)) %*/
2082op : '|' { ifndef_ripper($$ = '|'); }
2083 | '^' { ifndef_ripper($$ = '^'); }
2084 | '&' { ifndef_ripper($$ = '&'); }
2085 | tCMP { ifndef_ripper($$ = tCMP); }
2086 | tEQ { ifndef_ripper($$ = tEQ); }
2087 | tEQQ { ifndef_ripper($$ = tEQQ); }
2088 | tMATCH { ifndef_ripper($$ = tMATCH); }
2089 | tNMATCH { ifndef_ripper($$ = tNMATCH); }
2090 | '>' { ifndef_ripper($$ = '>'); }
2091 | tGEQ { ifndef_ripper($$ = tGEQ); }
2092 | '<' { ifndef_ripper($$ = '<'); }
2093 | tLEQ { ifndef_ripper($$ = tLEQ); }
2094 | tNEQ { ifndef_ripper($$ = tNEQ); }
2095 | tLSHFT { ifndef_ripper($$ = tLSHFT); }
2096 | tRSHFT { ifndef_ripper($$ = tRSHFT); }
2097 | '+' { ifndef_ripper($$ = '+'); }
2098 | '-' { ifndef_ripper($$ = '-'); }
2099 | '*' { ifndef_ripper($$ = '*'); }
2100 | tSTAR { ifndef_ripper($$ = '*'); }
2101 | '/' { ifndef_ripper($$ = '/'); }
2102 | '%' { ifndef_ripper($$ = '%'); }
2103 | tPOW { ifndef_ripper($$ = tPOW); }
2104 | tDSTAR { ifndef_ripper($$ = tDSTAR); }
2105 | '!' { ifndef_ripper($$ = '!'); }
2106 | '~' { ifndef_ripper($$ = '~'); }
2107 | tUPLUS { ifndef_ripper($$ = tUPLUS); }
2108 | tUMINUS { ifndef_ripper($$ = tUMINUS); }
2109 | tAREF { ifndef_ripper($$ = tAREF); }
2110 | tASET { ifndef_ripper($$ = tASET); }
2111 | '`' { ifndef_ripper($$ = '`'); }
2114reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
2115 | keyword_BEGIN | keyword_END
2116 | keyword_alias | keyword_and | keyword_begin
2117 | keyword_break | keyword_case | keyword_class | keyword_def
2118 | keyword_defined | keyword_do | keyword_else | keyword_elsif
2119 | keyword_end | keyword_ensure | keyword_false
2120 | keyword_for | keyword_in | keyword_module | keyword_next
2121 | keyword_nil | keyword_not | keyword_or | keyword_redo
2122 | keyword_rescue | keyword_retry | keyword_return | keyword_self
2123 | keyword_super | keyword_then | keyword_true | keyword_undef
2124 | keyword_when | keyword_yield | keyword_if | keyword_unless
2125 | keyword_while | keyword_until
2128arg : lhs '=' arg_rhs
2131 $$ = node_assign(p, $1, $3, &@$);
2133 /*% ripper: assign!($1, $3) %*/
2135 | var_lhs tOP_ASGN arg_rhs
2138 $$ = new_op_assign(p, $1, $2, $3, &@$);
2140 /*% ripper: opassign!($1, $2, $3) %*/
2142 | primary_value '[' opt_call_args rbracket tOP_ASGN arg_rhs
2146 $$ = new_ary_op_assign(p, $1, $3, $5, $6, &@3, &@$);
2148 /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $6) %*/
2150 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs
2154 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2156 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
2158 | primary_value call_op tCONSTANT tOP_ASGN arg_rhs
2162 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $5, &@$);
2164 /*% ripper: opassign!(field!($1, $2, $3), $4, $5) %*/
2166 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs
2170 $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $5, &@$);
2172 /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $5) %*/
2174 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs
2177 YYLTYPE loc = code_loc_gen(&@1, &@3);
2178 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $5, &@$);
2180 /*% ripper: opassign!(const_path_field!($1, $3), $4, $5) %*/
2182 | tCOLON3 tCONSTANT tOP_ASGN arg_rhs
2185 $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $4, &@$);
2187 /*% ripper: opassign!(top_const_field!($2), $3, $4) %*/
2189 | backref tOP_ASGN arg_rhs
2192 rb_backref_error(p, $1);
2193 $$ = NEW_BEGIN(0, &@$);
2195 /*% ripper[error]: assign_error!(opassign!(var_field(p, $1), $2, $3)) %*/
2202 $$ = NEW_DOT2($1, $3, &@$);
2204 /*% ripper: dot2!($1, $3) %*/
2211 $$ = NEW_DOT3($1, $3, &@$);
2213 /*% ripper: dot3!($1, $3) %*/
2219 loc.beg_pos = @2.end_pos;
2220 loc.end_pos = @2.end_pos;
2223 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
2225 /*% ripper: dot2!($1, Qnil) %*/
2231 loc.beg_pos = @2.end_pos;
2232 loc.end_pos = @2.end_pos;
2235 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
2237 /*% ripper: dot3!($1, Qnil) %*/
2243 loc.beg_pos = @1.beg_pos;
2244 loc.end_pos = @1.beg_pos;
2247 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
2249 /*% ripper: dot2!(Qnil, $2) %*/
2255 loc.beg_pos = @1.beg_pos;
2256 loc.end_pos = @1.beg_pos;
2259 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
2261 /*% ripper: dot3!(Qnil, $2) %*/
2265 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
2269 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
2273 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
2277 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
2281 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
2285 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
2287 | tUMINUS_NUM simple_numeric tPOW arg
2289 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
2293 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
2297 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
2301 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
2305 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
2309 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
2313 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
2315 | rel_expr %prec tCMP
2318 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
2322 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2326 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
2330 $$ = match_op(p, $1, $3, &@2, &@$);
2334 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
2338 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
2342 $$ = call_uni_op(p, $2, '~', &@1, &@$);
2346 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
2350 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
2354 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
2358 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
2360 | keyword_defined opt_nl {p->in_defined = 1;} arg
2363 $$ = new_defined(p, $4, &@$);
2365 | arg '?' arg opt_nl ':' arg
2369 $$ = new_if(p, $1, $3, $6, &@$);
2372 /*% ripper: ifop!($1, $3, $6) %*/
2380relop : '>' {$$ = '>';}
2386rel_expr : arg relop arg %prec '>'
2388 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2390 | rel_expr relop arg %prec '>'
2392 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
2393 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
2409 | args ',' assocs trailer
2412 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2414 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2419 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
2421 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2425arg_rhs : arg %prec tOP_ASGN
2430 | arg modifier_rescue arg
2433 YYLTYPE loc = code_loc_gen(&@2, &@3);
2435 $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
2437 /*% ripper: rescue_mod!($1, $3) %*/
2441paren_args : '(' opt_call_args rparen
2446 /*% ripper: arg_paren!(escape_Qundef($2)) %*/
2448 | '(' args ',' args_forward rparen
2450 if (!local_id(p, idFWD_REST) ||
2452 !local_id(p, idFWD_KWREST) ||
2454 !local_id(p, idFWD_BLOCK)) {
2455 compile_error(p, "unexpected ...");
2460 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@4), &@4);
2462 NODE *kwrest = list_append(p, NEW_LIST(0, &@4), NEW_LVAR(idFWD_KWREST, &@4));
2464 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@4), &@4);
2465 $$ = rest_arg_append(p, $2, splat, &@$);
2467 $$ = arg_append(p, $$, new_hash(p, kwrest, &@4), &@4);
2469 $$ = arg_blk_pass($$, block);
2471 /*% ripper: arg_paren!(args_add!($2, $4)) %*/
2474 | '(' args_forward rparen
2476 if (!local_id(p, idFWD_REST) ||
2478 !local_id(p, idFWD_KWREST) ||
2480 !local_id(p, idFWD_BLOCK)) {
2481 compile_error(p, "unexpected ...");
2486 NODE *splat = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@2), &@2);
2488 NODE *kwrest = list_append(p, NEW_LIST(0, &@2), NEW_LVAR(idFWD_KWREST, &@2));
2490 NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@2), &@2);
2492 $$ = arg_append(p, splat, new_hash(p, kwrest, &@2), &@2);
2496 $$ = arg_blk_pass($$, block);
2498 /*% ripper: arg_paren!($2) %*/
2503opt_paren_args : none
2513 | args ',' assocs ','
2516 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2518 /*% ripper: args_add!($1, bare_assoc_hash!($3)) %*/
2523 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2525 /*% ripper: args_add!(args_new!, bare_assoc_hash!($1)) %*/
2533 $$ = NEW_LIST($1, &@$);
2535 /*% ripper: args_add!(args_new!, $1) %*/
2537 | args opt_block_arg
2540 $$ = arg_blk_pass($1, $2);
2542 /*% ripper: args_add_block!($1, $2) %*/
2544 | assocs opt_block_arg
2547 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
2548 $$ = arg_blk_pass($$, $2);
2550 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($1)), $2) %*/
2552 | args ',' assocs opt_block_arg
2555 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
2556 $$ = arg_blk_pass($$, $4);
2558 /*% ripper: args_add_block!(args_add!($1, bare_assoc_hash!($3)), $4) %*/
2561 /*% ripper[brace]: args_add_block!(args_new!, $1) %*/
2565 /* If call_args starts with a open paren '(' or '[',
2566 * look-ahead reading of the letters calls CMDARG_PUSH(0),
2567 * but the push must be done after CMDARG_PUSH(1).
2568 * So this code makes them consistent by first cancelling
2569 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
2570 * and finally redoing CMDARG_PUSH(0).
2574 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
2577 if (lookahead) CMDARG_POP();
2579 if (lookahead) CMDARG_PUSH(0);
2583 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
2584 * but the push must be done after CMDARG_POP() in the parser.
2585 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
2586 * CMDARG_POP() to pop 1 pushed by command_args,
2587 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
2594 if (lookahead) CMDARG_POP();
2596 if (lookahead) CMDARG_PUSH(0);
2601block_arg : tAMPER arg_value
2604 $$ = NEW_BLOCK_PASS($2, &@$);
2610opt_block_arg : ',' block_arg
2623 $$ = NEW_LIST($1, &@$);
2625 /*% ripper: args_add!(args_new!, $1) %*/
2630 $$ = NEW_SPLAT($2, &@$);
2632 /*% ripper: args_add_star!(args_new!, $2) %*/
2634 | args ',' arg_value
2637 $$ = last_arg_append(p, $1, $3, &@$);
2639 /*% ripper: args_add!($1, $3) %*/
2641 | args ',' tSTAR arg_value
2644 $$ = rest_arg_append(p, $1, $4, &@$);
2646 /*% ripper: args_add_star!($1, $4) %*/
2654mrhs : args ',' arg_value
2657 $$ = last_arg_append(p, $1, $3, &@$);
2659 /*% ripper: mrhs_add!(mrhs_new_from_args!($1), $3) %*/
2661 | args ',' tSTAR arg_value
2664 $$ = rest_arg_append(p, $1, $4, &@$);
2666 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($1), $4) %*/
2671 $$ = NEW_SPLAT($2, &@$);
2673 /*% ripper: mrhs_add_star!(mrhs_new!, $2) %*/
2690 $$ = NEW_FCALL($1, 0, &@$);
2692 /*% ripper: method_add_arg!(fcall!($1), args_new!) %*/
2703 set_line_body($3, @1.end_pos.lineno);
2704 $$ = NEW_BEGIN($3, &@$);
2705 nd_set_line($$, @1.end_pos.lineno);
2707 /*% ripper: begin!($3) %*/
2709 | tLPAREN_ARG {SET_LEX_STATE(EXPR_ENDARG);} rparen
2712 $$ = NEW_BEGIN(0, &@$);
2714 /*% ripper: paren!(0) %*/
2716 | tLPAREN_ARG stmt {SET_LEX_STATE(EXPR_ENDARG);} rparen
2719 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2722 /*% ripper: paren!($2) %*/
2724 | tLPAREN compstmt ')'
2727 if (nd_type($2) == NODE_SELF) $2->nd_state = 0;
2730 /*% ripper: paren!($2) %*/
2732 | primary_value tCOLON2 tCONSTANT
2735 $$ = NEW_COLON2($1, $3, &@$);
2737 /*% ripper: const_path_ref!($1, $3) %*/
2742 $$ = NEW_COLON3($2, &@$);
2744 /*% ripper: top_const_ref!($2) %*/
2746 | tLBRACK aref_args ']'
2749 $$ = make_list($2, &@$);
2751 /*% ripper: array!(escape_Qundef($2)) %*/
2753 | tLBRACE assoc_list '}'
2756 $$ = new_hash(p, $2, &@$);
2757 $$->nd_brace = TRUE;
2759 /*% ripper: hash!(escape_Qundef($2)) %*/
2764 $$ = NEW_RETURN(0, &@$);
2766 /*% ripper: return0! %*/
2768 | keyword_yield '(' call_args rparen
2771 $$ = new_yield(p, $3, &@$);
2773 /*% ripper: yield!(paren!($3)) %*/
2775 | keyword_yield '(' rparen
2778 $$ = NEW_YIELD(0, &@$);
2780 /*% ripper: yield!(paren!(args_new!)) %*/
2785 $$ = NEW_YIELD(0, &@$);
2787 /*% ripper: yield0! %*/
2789 | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen
2792 $$ = new_defined(p, $5, &@$);
2794 | keyword_not '(' expr rparen
2796 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
2798 | keyword_not '(' rparen
2800 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
2805 $$ = method_add_block(p, $1, $2, &@$);
2807 /*% ripper: method_add_block!(method_add_arg!(fcall!($1), args_new!), $2) %*/
2810 | method_call brace_block
2813 block_dup_check(p, $1->nd_args, $2);
2814 $$ = method_add_block(p, $1, $2, &@$);
2816 /*% ripper: method_add_block!($1, $2) %*/
2820 token_info_push(p, "->", &@1);
2826 nd_set_first_loc($$, @1.beg_pos);
2829 | k_if expr_value then
2835 $$ = new_if(p, $2, $4, $5, &@$);
2838 /*% ripper: if!($2, $4, escape_Qundef($5)) %*/
2840 | k_unless expr_value then
2846 $$ = new_unless(p, $2, $4, $5, &@$);
2849 /*% ripper: unless!($2, $4, escape_Qundef($5)) %*/
2851 | k_while expr_value_do
2856 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$);
2859 /*% ripper: while!($2, $3) %*/
2861 | k_until expr_value_do
2866 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$);
2869 /*% ripper: until!($2, $3) %*/
2871 | k_case expr_value opt_terms
2873 $<val>$ = p->case_labels;
2874 p->case_labels = Qnil;
2879 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2880 p->case_labels = $<val>4;
2882 $$ = NEW_CASE($2, $5, &@$);
2885 /*% ripper: case!($2, $5) %*/
2889 $<val>$ = p->case_labels;
2895 if (RTEST(p->case_labels)) rb_hash_clear(p->case_labels);
2896 p->case_labels = $<val>3;
2898 $$ = NEW_CASE2($4, &@$);
2900 /*% ripper: case!(Qnil, $4) %*/
2902 | k_case expr_value opt_terms
2907 $$ = new_case3(p, $2, $4, &@$);
2909 /*% ripper: case!($2, $4) %*/
2911 | k_for for_var keyword_in expr_value_do
2919 * e.each{|*x| a, b, c = x}
2923 * e.each{|x| a, = x}
2925 ID id = internal_id(p);
2926 NODE *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
2927 NODE *args, *scope, *internal_var = NEW_DVAR(id, &@2);
2928 ID *tbl = ALLOC_N(ID, 3);
2929 tbl[0] = 1 /* length of local var table */; tbl[1] = id /* internal id */;
2930 rb_ast_add_local_table(p->ast, tbl);
2932 switch (nd_type($2)) {
2935 case NODE_DASGN_CURR: /* e.each {|internal_var| a = internal_var; ... } */
2936 $2->nd_value = internal_var;
2941 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
2942 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), &@2);
2944 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
2945 m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, &@2);
2947 /* {|*internal_id| <m> = internal_id; ... } */
2948 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
2949 scope = NEW_NODE(NODE_SCOPE, tbl, $5, args, &@$);
2950 $$ = NEW_FOR($4, scope, &@$);
2953 /*% ripper: for!($2, $4, $5) %*/
2955 | k_class cpath superclass
2958 YYLTYPE loc = code_loc_gen(&@1, &@2);
2959 yyerror1(&loc, "class definition in method body");
2961 $<num>1 = p->in_class;
2969 $$ = NEW_CLASS($2, $5, $3, &@$);
2970 nd_set_line($$->nd_body, @6.end_pos.lineno);
2971 set_line_body($5, @3.end_pos.lineno);
2972 nd_set_line($$, @3.end_pos.lineno);
2974 /*% ripper: class!($2, $3, $5) %*/
2976 p->in_class = $<num>1 & 1;
2978 | k_class tLSHFT expr
2980 $<num>$ = (p->in_class << 1) | p->in_def;
2990 $$ = NEW_SCLASS($3, $6, &@$);
2991 nd_set_line($$->nd_body, @7.end_pos.lineno);
2992 set_line_body($6, nd_line($3));
2995 /*% ripper: sclass!($3, $6) %*/
2997 p->in_def = $<num>4 & 1;
2998 p->in_class = ($<num>4 >> 1) & 1;
3003 YYLTYPE loc = code_loc_gen(&@1, &@2);
3004 yyerror1(&loc, "module definition in method body");
3006 $<num>1 = p->in_class;
3014 $$ = NEW_MODULE($2, $4, &@$);
3015 nd_set_line($$->nd_body, @5.end_pos.lineno);
3016 set_line_body($4, @2.end_pos.lineno);
3017 nd_set_line($$, @2.end_pos.lineno);
3019 /*% ripper: module!($2, $4) %*/
3021 p->in_class = $<num>1 & 1;
3025 numparam_name(p, get_id($2));
3027 $<id>$ = p->cur_arg;
3031 $<num>$ = p->in_def;
3039 NODE *body = remove_begin($6);
3040 reduce_nodes(p, &body);
3041 $$ = NEW_DEFN($2, $5, body, &@$);
3042 nd_set_line($$->nd_defn, @7.end_pos.lineno);
3043 set_line_body(body, @1.beg_pos.lineno);
3045 /*% ripper: def!($2, $5, $6) %*/
3047 p->in_def = $<num>4 & 1;
3048 p->cur_arg = $<id>3;
3050 | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
3052 numparam_name(p, get_id($5));
3053 $<num>4 = p->in_def;
3055 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3057 $<id>$ = p->cur_arg;
3065 NODE *body = remove_begin($8);
3066 reduce_nodes(p, &body);
3067 $$ = NEW_DEFS($2, $5, $7, body, &@$);
3068 nd_set_line($$->nd_defn, @9.end_pos.lineno);
3069 set_line_body(body, @1.beg_pos.lineno);
3071 /*% ripper: defs!($2, $3, $5, $7, $8) %*/
3073 p->in_def = $<num>4 & 1;
3074 p->cur_arg = $<id>6;
3079 $$ = NEW_BREAK(0, &@$);
3081 /*% ripper: break!(args_new!) %*/
3086 $$ = NEW_NEXT(0, &@$);
3088 /*% ripper: next!(args_new!) %*/
3095 /*% ripper: redo! %*/
3100 $$ = NEW_RETRY(&@$);
3102 /*% ripper: retry! %*/
3106primary_value : primary
3113k_begin : keyword_begin
3115 token_info_push(p, "begin", &@$);
3122 token_info_push(p, "if", &@$);
3123 if (p->token_info && p->token_info->nonspc &&
3124 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
3125 const char *tok = p->lex.ptok;
3126 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
3127 beg += rb_strlen_lit("else");
3128 while (beg < tok && ISSPACE(*beg)) beg++;
3130 p->token_info->nonspc = 0;
3136k_unless : keyword_unless
3138 token_info_push(p, "unless", &@$);
3142k_while : keyword_while
3144 token_info_push(p, "while", &@$);
3148k_until : keyword_until
3150 token_info_push(p, "until", &@$);
3154k_case : keyword_case
3156 token_info_push(p, "case", &@$);
3162 token_info_push(p, "for", &@$);
3166k_class : keyword_class
3168 token_info_push(p, "class", &@$);
3172k_module : keyword_module
3174 token_info_push(p, "module", &@$);
3180 token_info_push(p, "def", &@$);
3186 token_info_push(p, "do", &@$);
3190k_do_block : keyword_do_block
3192 token_info_push(p, "do", &@$);
3196k_rescue : keyword_rescue
3198 token_info_warn(p, "rescue", p->token_info, 1, &@$);
3202k_ensure : keyword_ensure
3204 token_info_warn(p, "ensure", p->token_info, 1, &@$);
3208k_when : keyword_when
3210 token_info_warn(p, "when", p->token_info, 0, &@$);
3214k_else : keyword_else
3216 token_info *ptinfo_beg = p->token_info;
3217 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
3218 token_info_warn(p, "else", p->token_info, same, &@$);
3221 e.next = ptinfo_beg->next;
3223 token_info_setup(&e, p->lex.pbeg, &@$);
3224 if (!e.nonspc) *ptinfo_beg = e;
3229k_elsif : keyword_elsif
3232 token_info_warn(p, "elsif", p->token_info, 1, &@$);
3238 token_info_pop(p, "end", &@$);
3242k_return : keyword_return
3244 if (p->in_class && !p->in_def && !dyna_in_block(p))
3245 yyerror1(&@1, "Invalid return in class/module body");
3259 | k_elsif expr_value then
3264 $$ = new_if(p, $2, $4, $5, &@$);
3267 /*% ripper: elsif!($2, $4, escape_Qundef($5)) %*/
3277 /*% ripper: else!($2) %*/
3288 $$ = assignable(p, $1, 0, &@$);
3289 mark_lvar_used(p, $$);
3291 /*% ripper: assignable(p, $1) %*/
3293 | tLPAREN f_margs rparen
3298 /*% ripper: mlhs_paren!($2) %*/
3305 $$ = NEW_LIST($1, &@$);
3307 /*% ripper: mlhs_add!(mlhs_new!, $1) %*/
3309 | f_marg_list ',' f_marg
3312 $$ = list_append(p, $1, $3);
3314 /*% ripper: mlhs_add!($1, $3) %*/
3318f_margs : f_marg_list
3321 $$ = NEW_MASGN($1, 0, &@$);
3325 | f_marg_list ',' f_rest_marg
3328 $$ = NEW_MASGN($1, $3, &@$);
3330 /*% ripper: mlhs_add_star!($1, $3) %*/
3332 | f_marg_list ',' f_rest_marg ',' f_marg_list
3335 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
3337 /*% ripper: mlhs_add_post!(mlhs_add_star!($1, $3), $5) %*/
3342 $$ = NEW_MASGN(0, $1, &@$);
3344 /*% ripper: mlhs_add_star!(mlhs_new!, $1) %*/
3346 | f_rest_marg ',' f_marg_list
3349 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
3351 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $1), $3) %*/
3355f_rest_marg : tSTAR f_norm_arg
3358 $$ = assignable(p, $2, 0, &@$);
3359 mark_lvar_used(p, $$);
3361 /*% ripper: assignable(p, $2) %*/
3366 $$ = NODE_SPECIAL_NO_NAME_REST;
3368 /*% ripper: Qnil %*/
3372block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg
3374 $$ = new_args_tail(p, $1, $3, $4, &@3);
3376 | f_block_kwarg opt_f_block_arg
3378 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
3380 | f_kwrest opt_f_block_arg
3382 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
3384 | f_no_kwarg opt_f_block_arg
3386 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
3390 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
3394opt_block_args_tail : ',' block_args_tail
3400 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
3404block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail
3406 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
3408 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3410 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
3412 | f_arg ',' f_block_optarg opt_block_args_tail
3414 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
3416 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail
3418 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
3420 | f_arg ',' f_rest_arg opt_block_args_tail
3422 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
3427 /* magic number for rest_id in iseq_set_arguments() */
3428 $$ = new_args(p, $1, Qnone, NODE_SPECIAL_EXCESSIVE_COMMA, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, &@1), &@$);
3430 /*% ripper: new_args(p, $1, Qnone, excessed_comma!, Qnone, new_args_tail(p, Qnone, Qnone, Qnone, NULL), NULL) %*/
3432 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail
3434 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
3436 | f_arg opt_block_args_tail
3438 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
3440 | f_block_optarg ',' f_rest_arg opt_block_args_tail
3442 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
3444 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail
3446 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
3448 | f_block_optarg opt_block_args_tail
3450 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
3452 | f_block_optarg ',' f_arg opt_block_args_tail
3454 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
3456 | f_rest_arg opt_block_args_tail
3458 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
3460 | f_rest_arg ',' f_arg opt_block_args_tail
3462 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
3466 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
3470opt_block_param : none
3473 p->command_start = TRUE;
3477block_param_def : '|' opt_bv_decl '|'
3480 p->max_numparam = ORDINAL_PARAM;
3484 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), escape_Qundef($2)) %*/
3486 | '|' block_param opt_bv_decl '|'
3489 p->max_numparam = ORDINAL_PARAM;
3493 /*% ripper: block_var!(escape_Qundef($2), escape_Qundef($3)) %*/
3502 | opt_nl ';' bv_decls opt_nl
3512 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
3514 /*% ripper[brace]: rb_ary_push($1, get_value($3)) %*/
3519 new_bv(p, get_id($1));
3520 /*% ripper: get_value($1) %*/
3529 $<vars>$ = dyna_push(p);
3532 $<num>$ = p->lex.lpar_beg;
3533 p->lex.lpar_beg = p->lex.paren_nest;
3536 $<num>$ = p->max_numparam;
3537 p->max_numparam = 0;
3540 $<node>$ = numparam_push(p);
3548 int max_numparam = p->max_numparam;
3549 p->lex.lpar_beg = $<num>2;
3550 p->max_numparam = $<num>3;
3552 $5 = args_with_numbered(p, $5, max_numparam);
3555 YYLTYPE loc = code_loc_gen(&@5, &@7);
3556 $$ = NEW_LAMBDA($5, $7, &loc);
3557 nd_set_line($$->nd_body, @7.end_pos.lineno);
3558 nd_set_line($$, @5.end_pos.lineno);
3561 /*% ripper: lambda!($5, $7) %*/
3562 numparam_pop(p, $<node>4);
3563 dyna_pop(p, $<vars>1);
3567f_larglist : '(' f_args opt_bv_decl ')'
3571 p->max_numparam = ORDINAL_PARAM;
3573 /*% ripper: paren!($2) %*/
3578 if (!args_info_empty_p($1->nd_ainfo))
3579 p->max_numparam = ORDINAL_PARAM;
3585lambda_body : tLAMBEG compstmt '}'
3587 token_info_pop(p, "}", &@3);
3590 | keyword_do_LAMBDA bodystmt k_end
3596do_block : k_do_block do_body k_end
3600 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3601 nd_set_line($$, @1.end_pos.lineno);
3606block_call : command do_block
3609 if (nd_type($1) == NODE_YIELD) {
3610 compile_error(p, "block given to yield");
3613 block_dup_check(p, $1->nd_args, $2);
3615 $$ = method_add_block(p, $1, $2, &@$);
3618 /*% ripper: method_add_block!($1, $2) %*/
3620 | block_call call_op2 operation2 opt_paren_args
3623 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3625 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3627 | block_call call_op2 operation2 opt_paren_args brace_block
3630 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3632 /*% ripper: opt_event(:method_add_block!, command_call!($1, $2, $3, $4), $5) %*/
3634 | block_call call_op2 operation2 command_args do_block
3637 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3639 /*% ripper: method_add_block!(command_call!($1, $2, $3, $4), $5) %*/
3643method_call : fcall paren_args
3648 nd_set_last_loc($1, @2.end_pos);
3650 /*% ripper: method_add_arg!(fcall!($1), $2) %*/
3652 | primary_value call_op operation2 opt_paren_args
3655 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3656 nd_set_line($$, @3.end_pos.lineno);
3658 /*% ripper: opt_event(:method_add_arg!, call!($1, $2, $3), $4) %*/
3660 | primary_value tCOLON2 operation2 paren_args
3663 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, $4, &@3, &@$);
3664 nd_set_line($$, @3.end_pos.lineno);
3666 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), $3), $4) %*/
3668 | primary_value tCOLON2 operation3
3671 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, $3, Qnull, &@3, &@$);
3673 /*% ripper: call!($1, ID2VAL(idCOLON2), $3) %*/
3675 | primary_value call_op paren_args
3678 $$ = new_qcall(p, $2, $1, ID2VAL(idCall), $3, &@2, &@$);
3679 nd_set_line($$, @2.end_pos.lineno);
3681 /*% ripper: method_add_arg!(call!($1, $2, ID2VAL(idCall)), $3) %*/
3683 | primary_value tCOLON2 paren_args
3686 $$ = new_qcall(p, ID2VAL(idCOLON2), $1, ID2VAL(idCall), $3, &@2, &@$);
3687 nd_set_line($$, @2.end_pos.lineno);
3689 /*% ripper: method_add_arg!(call!($1, ID2VAL(idCOLON2), ID2VAL(idCall)), $3) %*/
3691 | keyword_super paren_args
3694 $$ = NEW_SUPER($2, &@$);
3696 /*% ripper: super!($2) %*/
3701 $$ = NEW_ZSUPER(&@$);
3703 /*% ripper: zsuper! %*/
3705 | primary_value '[' opt_call_args rbracket
3708 if ($1 && nd_type($1) == NODE_SELF)
3709 $$ = NEW_FCALL(tAREF, $3, &@$);
3711 $$ = NEW_CALL($1, tAREF, $3, &@$);
3714 /*% ripper: aref!($1, escape_Qundef($3)) %*/
3718brace_block : '{' brace_body '}'
3722 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3723 nd_set_line($$, @1.end_pos.lineno);
3726 | k_do do_body k_end
3730 $$->nd_body->nd_loc = code_loc_gen(&@1, &@3);
3731 nd_set_line($$, @1.end_pos.lineno);
3736brace_body : {$<vars>$ = dyna_push(p);}
3738 $<num>$ = p->max_numparam;
3739 p->max_numparam = 0;
3742 $<node>$ = numparam_push(p);
3744 opt_block_param compstmt
3746 int max_numparam = p->max_numparam;
3747 p->max_numparam = $<num>2;
3748 $4 = args_with_numbered(p, $4, max_numparam);
3750 $$ = NEW_ITER($4, $5, &@$);
3752 /*% ripper: brace_block!(escape_Qundef($4), $5) %*/
3753 numparam_pop(p, $<node>3);
3754 dyna_pop(p, $<vars>1);
3758do_body : {$<vars>$ = dyna_push(p);}
3760 $<num>$ = p->max_numparam;
3761 p->max_numparam = 0;
3764 $<node>$ = numparam_push(p);
3767 opt_block_param bodystmt
3769 int max_numparam = p->max_numparam;
3770 p->max_numparam = $<num>2;
3771 $4 = args_with_numbered(p, $4, max_numparam);
3773 $$ = NEW_ITER($4, $5, &@$);
3775 /*% ripper: do_block!(escape_Qundef($4), $5) %*/
3777 numparam_pop(p, $<node>3);
3778 dyna_pop(p, $<vars>1);
3782case_args : arg_value
3785 check_literal_when(p, $1, &@1);
3786 $$ = NEW_LIST($1, &@$);
3788 /*% ripper: args_add!(args_new!, $1) %*/
3793 $$ = NEW_SPLAT($2, &@$);
3795 /*% ripper: args_add_star!(args_new!, $2) %*/
3797 | case_args ',' arg_value
3800 check_literal_when(p, $3, &@3);
3801 $$ = last_arg_append(p, $1, $3, &@$);
3803 /*% ripper: args_add!($1, $3) %*/
3805 | case_args ',' tSTAR arg_value
3808 $$ = rest_arg_append(p, $1, $4, &@$);
3810 /*% ripper: args_add_star!($1, $4) %*/
3814case_body : k_when case_args then
3819 $$ = NEW_WHEN($2, $4, $5, &@$);
3822 /*% ripper: when!($2, $4, escape_Qundef($5)) %*/
3830p_case_body : keyword_in
3832 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
3833 p->command_start = FALSE;
3834 $<num>$ = p->in_kwarg;
3837 {$<tbl>$ = push_pvtbl(p);}
3838 {$<tbl>$ = push_pktbl(p);}
3840 {pop_pktbl(p, $<tbl>4);}
3841 {pop_pvtbl(p, $<tbl>3);}
3843 p->in_kwarg = !!$<num>2;
3849 $$ = NEW_IN($5, $10, $11, &@$);
3851 /*% ripper: in!($5, $10, escape_Qundef($11)) %*/
3859p_top_expr : p_top_expr_body
3860 | p_top_expr_body modifier_if expr_value
3863 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3866 /*% ripper: if_mod!($3, $1) %*/
3868 | p_top_expr_body modifier_unless expr_value
3871 $$ = new_unless(p, $3, remove_begin($1), 0, &@$);
3874 /*% ripper: unless_mod!($3, $1) %*/
3878p_top_expr_body : p_expr
3881 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
3882 $$ = new_array_pattern(p, Qnone, get_value($1), $$, &@$);
3886 $$ = new_array_pattern(p, Qnone, get_value($1), $3, &@$);
3888 nd_set_first_loc($$, @1.beg_pos);
3894 $$ = new_array_pattern(p, Qnone, Qnone, $1, &@$);
3898 $$ = new_hash_pattern(p, Qnone, $1, &@$);
3905p_as : p_expr tASSOC p_variable
3908 NODE *n = NEW_LIST($1, &@$);
3909 n = list_append(p, n, $3);
3910 $$ = new_hash(p, n, &@$);
3912 /*% ripper: binary!($1, STATIC_ID2SYM((id_assoc)), $3) %*/
3917p_alt : p_alt '|' p_expr_basic
3920 $$ = NEW_NODE(NODE_OR, $1, $3, 0, &@$);
3922 /*% ripper: binary!($1, STATIC_ID2SYM(idOr), $3) %*/
3927p_lparen : '(' {$<tbl>$ = push_pktbl(p);};
3928p_lbracket : '[' {$<tbl>$ = push_pktbl(p);};
3930p_expr_basic : p_value
3931 | p_const p_lparen p_args rparen
3933 pop_pktbl(p, $<tbl>2);
3934 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3936 nd_set_first_loc($$, @1.beg_pos);
3940 | p_const p_lparen p_kwargs rparen
3942 pop_pktbl(p, $<tbl>2);
3943 $$ = new_hash_pattern(p, $1, $3, &@$);
3945 nd_set_first_loc($$, @1.beg_pos);
3949 | p_const '(' rparen
3951 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3952 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3954 | p_const p_lbracket p_args rbracket
3956 pop_pktbl(p, $<tbl>2);
3957 $$ = new_array_pattern(p, $1, Qnone, $3, &@$);
3959 nd_set_first_loc($$, @1.beg_pos);
3963 | p_const p_lbracket p_kwargs rbracket
3965 pop_pktbl(p, $<tbl>2);
3966 $$ = new_hash_pattern(p, $1, $3, &@$);
3968 nd_set_first_loc($$, @1.beg_pos);
3972 | p_const '[' rbracket
3974 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3975 $$ = new_array_pattern(p, $1, Qnone, $$, &@$);
3977 | tLBRACK {$<tbl>$ = push_pktbl(p);} p_args rbracket
3979 pop_pktbl(p, $<tbl>2);
3980 $$ = new_array_pattern(p, Qnone, Qnone, $3, &@$);
3984 $$ = new_array_pattern_tail(p, Qnone, 0, 0, Qnone, &@$);
3985 $$ = new_array_pattern(p, Qnone, Qnone, $$, &@$);
3989 $<tbl>$ = push_pktbl(p);
3990 $<num>1 = p->in_kwarg;
3995 pop_pktbl(p, $<tbl>2);
3996 p->in_kwarg = $<num>1;
3997 $$ = new_hash_pattern(p, Qnone, $3, &@$);
4001 $$ = new_hash_pattern_tail(p, Qnone, 0, &@$);
4002 $$ = new_hash_pattern(p, Qnone, $$, &@$);
4004 | tLPAREN {$<tbl>$ = push_pktbl(p);} p_expr rparen
4006 pop_pktbl(p, $<tbl>2);
4014 NODE *pre_args = NEW_LIST($1, &@$);
4015 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4017 $$ = new_array_pattern_tail(p, rb_ary_new_from_args(1, get_value($1)), 0, 0, Qnone, &@$);
4022 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4027 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, Qnone, &@$);
4029 VALUE pre_args = rb_ary_concat($1, get_value($2));
4030 $$ = new_array_pattern_tail(p, pre_args, 0, 0, Qnone, &@$);
4033 | p_args_head tSTAR tIDENTIFIER
4035 $$ = new_array_pattern_tail(p, $1, 1, $3, Qnone, &@$);
4037 | p_args_head tSTAR tIDENTIFIER ',' p_args_post
4039 $$ = new_array_pattern_tail(p, $1, 1, $3, $5, &@$);
4043 $$ = new_array_pattern_tail(p, $1, 1, 0, Qnone, &@$);
4045 | p_args_head tSTAR ',' p_args_post
4047 $$ = new_array_pattern_tail(p, $1, 1, 0, $4, &@$);
4052p_args_head : p_arg ','
4056 | p_args_head p_arg ','
4059 $$ = list_concat($1, $2);
4061 /*% ripper: rb_ary_concat($1, get_value($2)) %*/
4065p_args_tail : tSTAR tIDENTIFIER
4067 $$ = new_array_pattern_tail(p, Qnone, 1, $2, Qnone, &@$);
4069 | tSTAR tIDENTIFIER ',' p_args_post
4071 $$ = new_array_pattern_tail(p, Qnone, 1, $2, $4, &@$);
4075 $$ = new_array_pattern_tail(p, Qnone, 1, 0, Qnone, &@$);
4077 | tSTAR ',' p_args_post
4079 $$ = new_array_pattern_tail(p, Qnone, 1, 0, $3, &@$);
4084 | p_args_post ',' p_arg
4087 $$ = list_concat($1, $3);
4089 /*% ripper: rb_ary_concat($1, get_value($3)) %*/
4096 $$ = NEW_LIST($1, &@$);
4098 /*% ripper: rb_ary_new_from_args(1, get_value($1)) %*/
4102p_kwargs : p_kwarg ',' p_kwrest
4104 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
4108 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4112 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
4116 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), $1, &@$);
4118 | p_kwarg ',' p_kwnorest
4120 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), ID2VAL(idNil), &@$);
4124 $$ = new_hash_pattern_tail(p, new_hash(p, Qnone, &@$), ID2VAL(idNil), &@$);
4129 /*% ripper[brace]: rb_ary_new_from_args(1, $1) %*/
4133 $$ = list_concat($1, $3);
4135 /*% ripper: rb_ary_push($1, $3) %*/
4139p_kw : p_kw_label p_expr
4141 error_duplicate_pattern_key(p, get_id($1), &@1);
4143 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), $2);
4145 /*% ripper: rb_ary_new_from_args(2, get_value($1), get_value($2)) %*/
4149 error_duplicate_pattern_key(p, get_id($1), &@1);
4150 if ($1 && !is_local_id(get_id($1))) {
4151 yyerror1(&@1, "key must be valid as local variables");
4153 error_duplicate_pattern_variable(p, get_id($1), &@1);
4155 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@$), &@$), assignable(p, $1, 0, &@$));
4157 /*% ripper: rb_ary_new_from_args(2, get_value($1), Qnil) %*/
4162 | tSTRING_BEG string_contents tLABEL_END
4164 YYLTYPE loc = code_loc_gen(&@1, &@3);
4166 if (!$2 || nd_type($2) == NODE_STR) {
4167 NODE *node = dsym_node(p, $2, &loc);
4168 $$ = SYM2ID(node->nd_lit);
4171 if (ripper_is_node_yylval($2) && RNODE($2)->nd_cval) {
4172 VALUE label = RNODE($2)->nd_cval;
4173 VALUE rval = RNODE($2)->nd_rval;
4174 $$ = ripper_new_yylval(p, rb_intern_str(label), rval, label);
4175 RNODE($$)->nd_loc = loc;
4179 yyerror1(&loc, "symbol literal with interpolation is not allowed");
4185p_kwrest : kwrest_mark tIDENTIFIER
4195p_kwnorest : kwrest_mark keyword_nil
4201p_value : p_primitive
4202 | p_primitive tDOT2 p_primitive
4207 $$ = NEW_DOT2($1, $3, &@$);
4209 /*% ripper: dot2!($1, $3) %*/
4211 | p_primitive tDOT3 p_primitive
4216 $$ = NEW_DOT3($1, $3, &@$);
4218 /*% ripper: dot3!($1, $3) %*/
4224 loc.beg_pos = @2.end_pos;
4225 loc.end_pos = @2.end_pos;
4228 $$ = NEW_DOT2($1, new_nil(&loc), &@$);
4230 /*% ripper: dot2!($1, Qnil) %*/
4236 loc.beg_pos = @2.end_pos;
4237 loc.end_pos = @2.end_pos;
4240 $$ = NEW_DOT3($1, new_nil(&loc), &@$);
4242 /*% ripper: dot3!($1, Qnil) %*/
4247 | tBDOT2 p_primitive
4251 loc.beg_pos = @1.beg_pos;
4252 loc.end_pos = @1.beg_pos;
4255 $$ = NEW_DOT2(new_nil(&loc), $2, &@$);
4257 /*% ripper: dot2!(Qnil, $2) %*/
4259 | tBDOT3 p_primitive
4263 loc.beg_pos = @1.beg_pos;
4264 loc.end_pos = @1.beg_pos;
4267 $$ = NEW_DOT3(new_nil(&loc), $2, &@$);
4269 /*% ripper: dot3!(Qnil, $2) %*/
4273p_primitive : literal
4284 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4286 /*% ripper: var_ref!($1) %*/
4290 token_info_push(p, "->", &@1);
4296 nd_set_first_loc($$, @1.beg_pos);
4301p_variable : tIDENTIFIER
4304 error_duplicate_pattern_variable(p, $1, &@1);
4305 $$ = assignable(p, $1, 0, &@$);
4307 /*% ripper: assignable(p, var_field(p, $1)) %*/
4311p_var_ref : '^' tIDENTIFIER
4314 NODE *n = gettable(p, $2, &@$);
4315 if (!(nd_type(n) == NODE_LVAR || nd_type(n) == NODE_DVAR)) {
4316 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
4320 /*% ripper: var_ref!($2) %*/
4324p_const : tCOLON3 cname
4327 $$ = NEW_COLON3($2, &@$);
4329 /*% ripper: top_const_ref!($2) %*/
4331 | p_const tCOLON2 cname
4334 $$ = NEW_COLON2($1, $3, &@$);
4336 /*% ripper: const_path_ref!($1, $3) %*/
4341 $$ = gettable(p, $1, &@$);
4343 /*% ripper: var_ref!($1) %*/
4347opt_rescue : k_rescue exc_list exc_var then
4352 $$ = NEW_RESBODY($2,
4353 $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), &@3), $5) : $5,
4355 fixpos($$, $2?$2:$5);
4357 /*% ripper: rescue!(escape_Qundef($2), escape_Qundef($3), escape_Qundef($5), escape_Qundef($6)) %*/
4365 $$ = NEW_LIST($1, &@$);
4367 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
4372 if (!($$ = splat_array($1))) $$ = $1;
4386opt_ensure : k_ensure compstmt
4391 /*% ripper: ensure!($2) %*/
4405 node = NEW_STR(STR_NEW0(), &@$);
4406 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
4409 node = evstr2dstr(p, node);
4422 $$ = literal_concat(p, $1, $2, &@$);
4424 /*% ripper: string_concat!($1, $2) %*/
4428string1 : tSTRING_BEG string_contents tSTRING_END
4431 $$ = heredoc_dedent(p, $2);
4432 if ($$) nd_set_loc($$, &@$);
4434 /*% ripper: string_literal!(heredoc_dedent(p, $2)) %*/
4438xstring : tXSTRING_BEG xstring_contents tSTRING_END
4441 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
4443 /*% ripper: xstring_literal!(heredoc_dedent(p, $2)) %*/
4447regexp : tREGEXP_BEG regexp_contents tREGEXP_END
4449 $$ = new_regexp(p, $2, $3, &@$);
4453words : tWORDS_BEG ' ' word_list tSTRING_END
4456 $$ = make_list($3, &@$);
4458 /*% ripper: array!($3) %*/
4462word_list : /* none */
4467 /*% ripper: words_new! %*/
4469 | word_list word ' '
4472 $$ = list_append(p, $1, evstr2dstr(p, $2));
4474 /*% ripper: words_add!($1, $2) %*/
4478word : string_content
4479 /*% ripper[brace]: word_add!(word_new!, $1) %*/
4480 | word string_content
4483 $$ = literal_concat(p, $1, $2, &@$);
4485 /*% ripper: word_add!($1, $2) %*/
4489symbols : tSYMBOLS_BEG ' ' symbol_list tSTRING_END
4492 $$ = make_list($3, &@$);
4494 /*% ripper: array!($3) %*/
4498symbol_list : /* none */
4503 /*% ripper: symbols_new! %*/
4505 | symbol_list word ' '
4508 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
4510 /*% ripper: symbols_add!($1, $2) %*/
4514qwords : tQWORDS_BEG ' ' qword_list tSTRING_END
4517 $$ = make_list($3, &@$);
4519 /*% ripper: array!($3) %*/
4523qsymbols : tQSYMBOLS_BEG ' ' qsym_list tSTRING_END
4526 $$ = make_list($3, &@$);
4528 /*% ripper: array!($3) %*/
4532qword_list : /* none */
4537 /*% ripper: qwords_new! %*/
4539 | qword_list tSTRING_CONTENT ' '
4542 $$ = list_append(p, $1, $2);
4544 /*% ripper: qwords_add!($1, $2) %*/
4548qsym_list : /* none */
4553 /*% ripper: qsymbols_new! %*/
4555 | qsym_list tSTRING_CONTENT ' '
4558 $$ = symbol_append(p, $1, $2);
4560 /*% ripper: qsymbols_add!($1, $2) %*/
4564string_contents : /* none */
4569 /*% ripper: string_content! %*/
4572 $$ = ripper_new_yylval(p, 0, $$, 0);
4575 | string_contents string_content
4578 $$ = literal_concat(p, $1, $2, &@$);
4580 /*% ripper: string_add!($1, $2) %*/
4583 if (ripper_is_node_yylval($1) && ripper_is_node_yylval($2) &&
4584 !RNODE($1)->nd_cval) {
4585 RNODE($1)->nd_cval = RNODE($2)->nd_cval;
4586 RNODE($1)->nd_rval = add_mark_object(p, $$);
4593xstring_contents: /* none */
4598 /*% ripper: xstring_new! %*/
4600 | xstring_contents string_content
4603 $$ = literal_concat(p, $1, $2, &@$);
4605 /*% ripper: xstring_add!($1, $2) %*/
4609regexp_contents: /* none */
4614 /*% ripper: regexp_new! %*/
4617 $$ = ripper_new_yylval(p, 0, $$, 0);
4620 | regexp_contents string_content
4623 NODE *head = $1, *tail = $2;
4631 switch (nd_type(head)) {
4633 nd_set_type(head, NODE_DSTR);
4638 head = list_append(p, NEW_DSTR(Qnil, &@$), head);
4641 $$ = list_append(p, head, tail);
4644 VALUE s1 = 1, s2 = 0, n1 = $1, n2 = $2;
4645 if (ripper_is_node_yylval(n1)) {
4646 s1 = RNODE(n1)->nd_cval;
4647 n1 = RNODE(n1)->nd_rval;
4649 if (ripper_is_node_yylval(n2)) {
4650 s2 = RNODE(n2)->nd_cval;
4651 n2 = RNODE(n2)->nd_rval;
4653 $$ = dispatch2(regexp_add, n1, n2);
4655 $$ = ripper_new_yylval(p, 0, $$, s2);
4661string_content : tSTRING_CONTENT
4662 /*% ripper[brace]: ripper_new_yylval(p, 0, get_value($1), $1) %*/
4665 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
4666 $<strterm>$ = p->lex.strterm;
4668 SET_LEX_STATE(EXPR_BEG);
4672 p->lex.strterm = $<strterm>2;
4674 $$ = NEW_EVSTR($3, &@$);
4675 nd_set_line($$, @3.end_pos.lineno);
4677 /*% ripper: string_dvar!($3) %*/
4685 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
4686 $<strterm>$ = p->lex.strterm;
4690 $<num>$ = p->lex.state;
4691 SET_LEX_STATE(EXPR_BEG);
4694 $<num>$ = p->lex.brace_nest;
4695 p->lex.brace_nest = 0;
4698 $<num>$ = p->heredoc_indent;
4699 p->heredoc_indent = 0;
4701 compstmt tSTRING_DEND
4705 p->lex.strterm = $<strterm>3;
4706 SET_LEX_STATE($<num>4);
4707 p->lex.brace_nest = $<num>5;
4708 p->heredoc_indent = $<num>6;
4709 p->heredoc_line_indent = -1;
4711 if ($7) $7->flags &= ~NODE_FL_NEWLINE;
4712 $$ = new_evstr(p, $7, &@$);
4714 /*% ripper: string_embexpr!($7) %*/
4721 $$ = NEW_GVAR($1, &@$);
4723 /*% ripper: var_ref!($1) %*/
4728 $$ = NEW_IVAR($1, &@$);
4730 /*% ripper: var_ref!($1) %*/
4735 $$ = NEW_CVAR($1, &@$);
4737 /*% ripper: var_ref!($1) %*/
4748 SET_LEX_STATE(EXPR_END);
4750 $$ = NEW_LIT(ID2SYM($2), &@$);
4752 /*% ripper: symbol_literal!(symbol!($2)) %*/
4762dsym : tSYMBEG string_contents tSTRING_END
4764 SET_LEX_STATE(EXPR_END);
4766 $$ = dsym_node(p, $2, &@$);
4768 /*% ripper: dyna_symbol!($2) %*/
4772numeric : simple_numeric
4773 | tUMINUS_NUM simple_numeric %prec tLOWEST
4777 RB_OBJ_WRITE(p->ast, &$$->nd_lit, negate_lit(p, $$->nd_lit));
4779 /*% ripper: unary!(ID2VAL(idUMinus), $2) %*/
4783simple_numeric : tINTEGER
4789user_variable : tIDENTIFIER
4796keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
4797 | keyword_self {$$ = KWD2EID(self, $1);}
4798 | keyword_true {$$ = KWD2EID(true, $1);}
4799 | keyword_false {$$ = KWD2EID(false, $1);}
4800 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
4801 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
4802 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
4805var_ref : user_variable
4808 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4810 if (id_is_var(p, get_id($1))) {
4811 $$ = dispatch1(var_ref, $1);
4814 $$ = dispatch1(vcall, $1);
4821 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_BEGIN(0, &@$);
4823 /*% ripper: var_ref!($1) %*/
4827var_lhs : user_variable
4830 $$ = assignable(p, $1, 0, &@$);
4832 /*% ripper: assignable(p, var_field(p, $1)) %*/
4837 $$ = assignable(p, $1, 0, &@$);
4839 /*% ripper: assignable(p, var_field(p, $1)) %*/
4849 SET_LEX_STATE(EXPR_BEG);
4850 p->command_start = TRUE;
4861 /*% ripper: Qnil %*/
4865f_arglist : '(' f_args rparen
4870 /*% ripper: paren!($2) %*/
4871 SET_LEX_STATE(EXPR_BEG);
4872 p->command_start = TRUE;
4874 | '(' f_arg ',' args_forward rparen
4876 arg_var(p, idFWD_REST);
4878 arg_var(p, idFWD_KWREST);
4880 arg_var(p, idFWD_BLOCK);
4882 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@4);
4883 $$ = new_args(p, $2, Qnone, idFWD_REST, Qnone, $$, &@4);
4885 /*% ripper: paren!(params_new($2, Qnone, $4, Qnone, Qnone, Qnone, Qnone)) %*/
4886 SET_LEX_STATE(EXPR_BEG);
4887 p->command_start = TRUE;
4889 | '(' args_forward rparen
4891 arg_var(p, idFWD_REST);
4893 arg_var(p, idFWD_KWREST);
4895 arg_var(p, idFWD_BLOCK);
4897 $$ = new_args_tail(p, Qnone, idFWD_KWREST, idFWD_BLOCK, &@2);
4898 $$ = new_args(p, Qnone, Qnone, idFWD_REST, Qnone, $$, &@2);
4900 /*% ripper: paren!(params_new(Qnone, Qnone, $2, Qnone, Qnone, Qnone, Qnone)) %*/
4901 SET_LEX_STATE(EXPR_BEG);
4902 p->command_start = TRUE;
4905 $<num>$ = p->in_kwarg;
4907 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
4911 p->in_kwarg = !!$<num>1;
4913 SET_LEX_STATE(EXPR_BEG);
4914 p->command_start = TRUE;
4918args_tail : f_kwarg ',' f_kwrest opt_f_block_arg
4920 $$ = new_args_tail(p, $1, $3, $4, &@3);
4922 | f_kwarg opt_f_block_arg
4924 $$ = new_args_tail(p, $1, Qnone, $2, &@1);
4926 | f_kwrest opt_f_block_arg
4928 $$ = new_args_tail(p, Qnone, $1, $2, &@1);
4930 | f_no_kwarg opt_f_block_arg
4932 $$ = new_args_tail(p, Qnone, ID2VAL(idNil), $2, &@1);
4936 $$ = new_args_tail(p, Qnone, Qnone, $1, &@1);
4940opt_args_tail : ',' args_tail
4946 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
4950f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail
4952 $$ = new_args(p, $1, $3, $5, Qnone, $6, &@$);
4954 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4956 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
4958 | f_arg ',' f_optarg opt_args_tail
4960 $$ = new_args(p, $1, $3, Qnone, Qnone, $4, &@$);
4962 | f_arg ',' f_optarg ',' f_arg opt_args_tail
4964 $$ = new_args(p, $1, $3, Qnone, $5, $6, &@$);
4966 | f_arg ',' f_rest_arg opt_args_tail
4968 $$ = new_args(p, $1, Qnone, $3, Qnone, $4, &@$);
4970 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail
4972 $$ = new_args(p, $1, Qnone, $3, $5, $6, &@$);
4974 | f_arg opt_args_tail
4976 $$ = new_args(p, $1, Qnone, Qnone, Qnone, $2, &@$);
4978 | f_optarg ',' f_rest_arg opt_args_tail
4980 $$ = new_args(p, Qnone, $1, $3, Qnone, $4, &@$);
4982 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail
4984 $$ = new_args(p, Qnone, $1, $3, $5, $6, &@$);
4986 | f_optarg opt_args_tail
4988 $$ = new_args(p, Qnone, $1, Qnone, Qnone, $2, &@$);
4990 | f_optarg ',' f_arg opt_args_tail
4992 $$ = new_args(p, Qnone, $1, Qnone, $3, $4, &@$);
4994 | f_rest_arg opt_args_tail
4996 $$ = new_args(p, Qnone, Qnone, $1, Qnone, $2, &@$);
4998 | f_rest_arg ',' f_arg opt_args_tail
5000 $$ = new_args(p, Qnone, Qnone, $1, $3, $4, &@$);
5004 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $1, &@$);
5008 $$ = new_args_tail(p, Qnone, Qnone, Qnone, &@0);
5009 $$ = new_args(p, Qnone, Qnone, Qnone, Qnone, $$, &@0);
5013args_forward : tBDOT3
5018 /*% ripper: args_forward! %*/
5022f_bad_arg : tCONSTANT
5025 yyerror1(&@1, "formal argument cannot be a constant");
5028 /*% ripper[error]: param_error!($1) %*/
5033 yyerror1(&@1, "formal argument cannot be an instance variable");
5036 /*% ripper[error]: param_error!($1) %*/
5041 yyerror1(&@1, "formal argument cannot be a global variable");
5044 /*% ripper[error]: param_error!($1) %*/
5049 yyerror1(&@1, "formal argument cannot be a class variable");
5052 /*% ripper[error]: param_error!($1) %*/
5056f_norm_arg : f_bad_arg
5059 formal_argument(p, get_id($1));
5060 p->max_numparam = ORDINAL_PARAM;
5065f_arg_asgn : f_norm_arg
5074f_arg_item : f_arg_asgn
5078 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
5080 /*% ripper: get_value($1) %*/
5082 | tLPAREN f_margs rparen
5085 ID tid = internal_id(p);
5087 loc.beg_pos = @2.beg_pos;
5088 loc.end_pos = @2.beg_pos;
5090 if (dyna_in_block(p)) {
5091 $2->nd_value = NEW_DVAR(tid, &loc);
5094 $2->nd_value = NEW_LVAR(tid, &loc);
5096 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
5099 /*% ripper: mlhs_paren!($2) %*/
5104 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5105 | f_arg ',' f_arg_item
5110 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
5111 rb_discard_node(p, $3);
5113 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5121 arg_var(p, formal_argument(p, id));
5123 p->max_numparam = ORDINAL_PARAM;
5128f_kw : f_label arg_value
5132 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5134 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5140 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5142 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5146f_block_kw : f_label primary_value
5149 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
5151 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($2)) %*/
5156 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
5158 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), 0) %*/
5162f_block_kwarg : f_block_kw
5167 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5169 | f_block_kwarg ',' f_block_kw
5172 $$ = kwd_append($1, $3);
5174 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5184 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5189 $$ = kwd_append($1, $3);
5191 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5199f_no_kwarg : kwrest_mark keyword_nil
5203 /*% ripper: nokw_param!(Qnil) %*/
5207f_kwrest : kwrest_mark tIDENTIFIER
5209 arg_var(p, shadowing_lvar(p, get_id($2)));
5213 /*% ripper: kwrest_param!($2) %*/
5218 $$ = internal_id(p);
5221 /*% ripper: kwrest_param!(Qnil) %*/
5225f_opt : f_arg_asgn '=' arg_value
5229 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5231 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5235f_block_opt : f_arg_asgn '=' primary_value
5239 $$ = NEW_OPT_ARG(0, assignable(p, $1, $3, &@$), &@$);
5241 /*% ripper: rb_assoc_new(get_value(assignable(p, $1)), get_value($3)) %*/
5245f_block_optarg : f_block_opt
5250 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5252 | f_block_optarg ',' f_block_opt
5255 $$ = opt_arg_append($1, $3);
5257 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5266 /*% ripper: rb_ary_new3(1, get_value($1)) %*/
5268 | f_optarg ',' f_opt
5271 $$ = opt_arg_append($1, $3);
5273 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5281f_rest_arg : restarg_mark tIDENTIFIER
5283 arg_var(p, shadowing_lvar(p, get_id($2)));
5287 /*% ripper: rest_param!($2) %*/
5292 $$ = internal_id(p);
5295 /*% ripper: rest_param!(Qnil) %*/
5303f_block_arg : blkarg_mark tIDENTIFIER
5305 arg_var(p, shadowing_lvar(p, get_id($2)));
5309 /*% ripper: blockarg!($2) %*/
5313opt_f_block_arg : ',' f_block_arg
5328 | '(' {SET_LEX_STATE(EXPR_BEG);} expr rparen
5331 switch (nd_type($3)) {
5340 yyerror1(&@3, "can't define singleton method for literals");
5348 /*% ripper: paren!($3) %*/
5358 /*% ripper: assoclist_from_args!($1) %*/
5363 /*% ripper[brace]: rb_ary_new3(1, get_value($1)) %*/
5373 if (assocs->nd_head &&
5374 !tail->nd_head && nd_type(tail->nd_next) == NODE_LIST &&
5375 nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5377 tail = tail->nd_next->nd_head->nd_head;
5379 assocs = list_concat(assocs, tail);
5383 /*% ripper: rb_ary_push($1, get_value($3)) %*/
5387assoc : arg_value tASSOC arg_value
5390 if (nd_type($1) == NODE_STR) {
5391 nd_set_type($1, NODE_LIT);
5392 RB_OBJ_WRITE(p->ast, &$1->nd_lit, rb_fstring($1->nd_lit));
5394 $$ = list_append(p, NEW_LIST($1, &@$), $3);
5396 /*% ripper: assoc_new!($1, $3) %*/
5401 $$ = list_append(p, NEW_LIST(NEW_LIT(ID2SYM($1), &@1), &@$), $2);
5403 /*% ripper: assoc_new!($1, $2) %*/
5405 | tSTRING_BEG string_contents tLABEL_END arg_value
5408 YYLTYPE loc = code_loc_gen(&@1, &@3);
5409 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
5411 /*% ripper: assoc_new!(dyna_symbol!($2), $4) %*/
5416 if (nd_type($2) == NODE_HASH &&
5417 !($2->nd_head && $2->nd_head->nd_alen)) {
5418 static VALUE empty_hash;
5420 empty_hash = rb_obj_freeze(rb_hash_new());
5421 rb_gc_register_mark_object(empty_hash);
5423 $$ = list_append(p, NEW_LIST(0, &@$), NEW_LIT(empty_hash, &@$));
5426 $$ = list_append(p, NEW_LIST(0, &@$), $2);
5428 /*% ripper: assoc_splat!($2) %*/
5432operation : tIDENTIFIER
5437operation2 : tIDENTIFIER
5443operation3 : tIDENTIFIER
5460opt_terms : /* none */
5471rbracket : opt_nl ']'
5482term : ';' {yyerrok;token_flush(p);}
5483 | '\n' {token_flush(p);}
5487 | terms ';' {yyerrok;}
5499# define yylval (*p->lval)
5501static int regx_options(struct parser_params*);
5502static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
5503static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
5504static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
5505static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
5508# define set_yylval_node(x) { \
5510 rb_parser_set_location(p, &_cur_loc); \
5511 yylval.node = (x); \
5513# define set_yylval_str(x) \
5515 set_yylval_node(NEW_STR(x, &_cur_loc)); \
5516 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5518# define set_yylval_literal(x) \
5520 set_yylval_node(NEW_LIT(x, &_cur_loc)); \
5521 RB_OBJ_WRITTEN(p->ast, Qnil, x); \
5523# define set_yylval_num(x) (yylval.num = (x))
5524# define set_yylval_id(x) (yylval.id = (x))
5525# define set_yylval_name(x) (yylval.id = (x))
5526# define yylval_id() (yylval.id)
5529ripper_yylval_id(struct parser_params *p, ID x)
5531 return ripper_new_yylval(p, x, ID2SYM(x), 0);
5533# define set_yylval_str(x) (yylval.val = add_mark_object(p, (x)))
5534# define set_yylval_num(x) (yylval.val = ripper_new_yylval(p, (x), 0, 0))
5535# define set_yylval_id(x) (void)(x)
5536# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(p, x))
5537# define set_yylval_literal(x) add_mark_object(p, (x))
5538# define set_yylval_node(x) (void)(x)
5539# define yylval_id() yylval.id
5540# define _cur_loc NULL_LOC /* dummy */
5543#define set_yylval_noname() set_yylval_id(keyword_nil)
5546#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
5547#define dispatch_scan_event(p, t) ((void)0)
5548#define dispatch_delayed_token(p, t) ((void)0)
5549#define has_delayed_token(p) (0)
5551#define literal_flush(p, ptr) ((void)(ptr))
5553#define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val))
5556intern_sym(const char *name)
5558 ID id = rb_intern_const(name);
5563ripper_has_scan_event(struct parser_params *p)
5565 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
5566 return p->lex.pcur > p->lex.ptok;
5570ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
5572 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
5573 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
5579ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
5581 if (!ripper_has_scan_event(p)) return;
5582 add_mark_object(p, yylval_rval = ripper_scan_event_val(p, t));
5584#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
5587ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
5589 int saved_line = p->ruby_sourceline;
5590 const char *saved_tokp = p->lex.ptok;
5592 if (NIL_P(p->delayed.token)) return;
5593 p->ruby_sourceline = p->delayed.line;
5594 p->lex.ptok = p->lex.pbeg + p->delayed.col;
5595 add_mark_object(p, yylval_rval = ripper_dispatch1(p, ripper_token2eventid(t), p->delayed.token));
5596 p->delayed.token = Qnil;
5597 p->ruby_sourceline = saved_line;
5598 p->lex.ptok = saved_tokp;
5600#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
5601#define has_delayed_token(p) (!NIL_P(p->delayed.token))
5604#include "ruby/regex.h"
5605#include "ruby/util.h"
5608is_identchar(const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
5610 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
5614parser_is_identchar(struct parser_params *p)
5616 return !(p)->eofp && is_identchar(p->lex.pcur-1, p->lex.pend, p->enc);
5620parser_isascii(struct parser_params *p)
5622 return ISASCII(*(p->lex.pcur-1));
5626token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
5628 int column = 1, nonspc = 0, i;
5629 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
5631 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
5634 if (*ptr != ' ' && *ptr != '\t') {
5639 ptinfo->beg = loc->beg_pos;
5640 ptinfo->indent = column;
5641 ptinfo->nonspc = nonspc;
5645token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5649 if (!p->token_info_enabled) return;
5650 ptinfo = ALLOC(token_info);
5651 ptinfo->token = token;
5652 ptinfo->next = p->token_info;
5653 token_info_setup(ptinfo, p->lex.pbeg, loc);
5655 p->token_info = ptinfo;
5659token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
5661 token_info *ptinfo_beg = p->token_info;
5663 if (!ptinfo_beg) return;
5664 p->token_info = ptinfo_beg->next;
5666 /* indentation check of matched keywords (begin..end, if..end, etc.) */
5667 token_info_warn(p, token, ptinfo_beg, 1, loc);
5668 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
5672token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
5674 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
5675 if (!p->token_info_enabled) return;
5676 if (!ptinfo_beg) return;
5677 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
5678 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
5679 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
5680 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
5681 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
5682 rb_warn3L(ptinfo_end->beg.lineno,
5683 "mismatched indentations at '%s' with '%s' at %d",
5684 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
5688parser_precise_mbclen(struct parser_params *p, const char *ptr)
5690 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
5691 if (!MBCLEN_CHARFOUND_P(len)) {
5692 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
5699static void ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str);
5702parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5705 int lineno = p->ruby_sourceline;
5709 else if (yylloc->beg_pos.lineno == lineno) {
5710 str = p->lex.lastline;
5715 ruby_show_error_line(p->error_buffer, yylloc, lineno, str);
5719parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5724 yylloc = RUBY_SET_YYLLOC(current);
5726 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
5727 p->ruby_sourceline != yylloc->end_pos.lineno) ||
5728 (yylloc->beg_pos.lineno == yylloc->end_pos.lineno &&
5729 yylloc->beg_pos.column == yylloc->end_pos.column)) {
5732 compile_error(p, "%s", msg);
5733 parser_show_error_line(p, yylloc);
5738ruby_show_error_line(VALUE errbuf, const YYLTYPE *yylloc, int lineno, VALUE str)
5741 const int max_line_margin = 30;
5742 const char *ptr, *ptr_end, *pt, *pb;
5743 const char *pre = "", *post = "", *pend;
5744 const char *code = "", *caret = "";
5746 const char *const pbeg = RSTRING_PTR(str);
5751 if (!yylloc) return;
5752 pend = RSTRING_END(str);
5753 if (pend > pbeg && pend[-1] == '\n') {
5754 if (--pend > pbeg && pend[-1] == '\r') --pend;
5758 if (lineno == yylloc->end_pos.lineno &&
5759 (pend - pbeg) > yylloc->end_pos.column) {
5760 pt = pbeg + yylloc->end_pos.column;
5764 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
5765 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
5767 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
5768 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
5770 len = ptr_end - ptr;
5773 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_enc_get(str));
5774 if (ptr > pbeg) pre = "...";
5776 if (ptr_end < pend) {
5777 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_enc_get(str));
5778 if (ptr_end < pend) post = "...";
5782 if (lineno == yylloc->beg_pos.lineno) {
5783 pb += yylloc->beg_pos.column;
5784 if (pb > pt) pb = pt;
5786 if (pb < ptr) pb = ptr;
5787 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
5790 if (RTEST(errbuf)) {
5791 mesg = rb_attr_get(errbuf, idMesg);
5792 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
5793 rb_str_cat_cstr(mesg, "\n");
5796 mesg = rb_enc_str_new(0, 0, rb_enc_get(str));
5798 if (!errbuf && rb_stderr_tty_p()) {
5799#define CSI_BEGIN "\033["
5802 CSI_BEGIN""CSI_SGR"%s" /* pre */
5803 CSI_BEGIN"1"CSI_SGR"%.*s"
5804 CSI_BEGIN"1;4"CSI_SGR"%.*s"
5805 CSI_BEGIN";1"CSI_SGR"%.*s"
5806 CSI_BEGIN""CSI_SGR"%s" /* post */
5809 (int)(pb - ptr), ptr,
5811 (int)(ptr_end - pt), pt,
5817 len = ptr_end - ptr;
5818 lim = pt < pend ? pt : pend;
5819 i = (int)(lim - ptr);
5820 buf = ALLOCA_N(char, i+2);
5825 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
5831 memset(p2, '~', (lim - ptr));
5835 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
5836 pre, (int)len, code, post,
5839 if (!errbuf) rb_write_error_str(mesg);
5843parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
5845 const char *pcur = 0, *ptok = 0;
5847 p->ruby_sourceline == yylloc->beg_pos.lineno &&
5848 p->ruby_sourceline == yylloc->end_pos.lineno) {
5851 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
5852 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
5854 dispatch1(parse_error, STR_NEW2(msg));
5864parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
5871vtable_size(const struct vtable *tbl)
5873 if (!DVARS_TERMINAL_P(tbl)) {
5882static struct vtable *
5883vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
5885 struct vtable *tbl = ALLOC(struct vtable);
5888 tbl->tbl = ALLOC_N(ID, tbl->capa);
5892 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
5897#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
5900vtable_free_gen(struct parser_params *p, int line, const char *name,
5905 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
5908 if (!DVARS_TERMINAL_P(tbl)) {
5910 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
5912 ruby_sized_xfree(tbl, sizeof(tbl));
5915#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
5918vtable_add_gen(struct parser_params *p, int line, const char *name,
5919 struct vtable *tbl, ID id)
5923 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
5924 line, name, (void *)tbl, rb_id2name(id));
5927 if (DVARS_TERMINAL_P(tbl)) {
5928 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
5931 if (tbl->pos == tbl->capa) {
5932 tbl->capa = tbl->capa * 2;
5933 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
5935 tbl->tbl[tbl->pos++] = id;
5937#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
5941vtable_pop_gen(struct parser_params *p, int line, const char *name,
5942 struct vtable *tbl, int n)
5945 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
5946 line, name, (void *)tbl, n);
5949 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
5954#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
5958vtable_included(const struct vtable * tbl, ID id)
5962 if (!DVARS_TERMINAL_P(tbl)) {
5963 for (i = 0; i < tbl->pos; i++) {
5964 if (tbl->tbl[i] == id) {
5972static void parser_prepare(struct parser_params *p);
5975static NODE *parser_append_options(struct parser_params *p, NODE *node);
5978debug_lines(VALUE fname)
5981 CONST_ID(script_lines, "SCRIPT_LINES__");
5982 if (rb_const_defined_at(rb_cObject, script_lines)) {
5983 VALUE hash = rb_const_get_at(rb_cObject, script_lines);
5984 if (RB_TYPE_P(hash, T_HASH)) {
5985 VALUE lines = rb_ary_new();
5986 rb_hash_aset(hash, fname, lines);
5994e_option_supplied(struct parser_params *p)
5996 return strcmp(p->ruby_sourcefile, "-e") == 0;
6000yycompile0(VALUE arg)
6004 struct parser_params *p = (struct parser_params *)arg;
6007 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string)) {
6008 p->debug_lines = debug_lines(p->ruby_sourcefile_string);
6009 if (p->debug_lines && p->ruby_sourceline > 0) {
6010 VALUE str = STR_NEW0();
6011 n = p->ruby_sourceline;
6013 rb_ary_push(p->debug_lines, str);
6017 if (!e_option_supplied(p)) {
6023#define RUBY_DTRACE_PARSE_HOOK(name) \
6024 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
6025 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
6027 RUBY_DTRACE_PARSE_HOOK(BEGIN);
6029 RUBY_DTRACE_PARSE_HOOK(END);
6033 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
6034 p->lex.prevline = p->lex.lastline = p->lex.nextline = 0;
6035 if (n || p->error_p) {
6036 VALUE mesg = p->error_buffer;
6038 mesg = rb_class_new_instance(0, 0, rb_eSyntaxError);
6040 rb_set_errinfo(mesg);
6043 tree = p->eval_tree;
6045 tree = NEW_NIL(&NULL_LOC);
6048 VALUE opt = p->compile_option;
6050 NODE *body = parser_append_options(p, tree->nd_body);
6051 if (!opt) opt = rb_obj_hide(rb_ident_hash_new());
6052 rb_hash_aset(opt, rb_sym_intern_ascii_cstr("coverage_enabled"), cov);
6053 prelude = block_append(p, p->eval_tree_begin, body);
6054 tree->nd_body = prelude;
6055 RB_OBJ_WRITE(p->ast, &p->ast->body.compile_option, opt);
6057 p->ast->body.root = tree;
6058 p->ast->body.line_count = p->line_count;
6063yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
6067 p->ruby_sourcefile_string = Qnil;
6068 p->ruby_sourcefile = "(none)";
6071 p->ruby_sourcefile_string = rb_fstring(fname);
6072 p->ruby_sourcefile = StringValueCStr(fname);
6074 p->ruby_sourceline = line - 1;
6076 p->ast = ast = rb_ast_new();
6077 rb_suppress_tracing(yycompile0, (VALUE)p);
6079 RB_GC_GUARD(vparser); /* prohibit tail call optimization */
6086must_be_ascii_compatible(VALUE s)
6088 rb_encoding *enc = rb_enc_get(s);
6089 if (!rb_enc_asciicompat(enc)) {
6090 rb_raise(rb_eArgError, "invalid source encoding");
6096lex_get_str(struct parser_params *p, VALUE s)
6098 char *beg, *end, *start;
6101 beg = RSTRING_PTR(s);
6102 len = RSTRING_LEN(s);
6104 if (p->lex.gets_.ptr) {
6105 if (len == p->lex.gets_.ptr) return Qnil;
6106 beg += p->lex.gets_.ptr;
6107 len -= p->lex.gets_.ptr;
6109 end = memchr(beg, '\n', len);
6110 if (end) len = ++end - beg;
6111 p->lex.gets_.ptr += len;
6112 return rb_str_subseq(s, beg - start, len);
6116lex_getline(struct parser_params *p)
6118 VALUE line = (*p->lex.gets)(p, p->lex.input);
6119 if (NIL_P(line)) return line;
6120 must_be_ascii_compatible(line);
6122 if (p->debug_lines) {
6123 rb_enc_associate(line, p->enc);
6124 rb_ary_push(p->debug_lines, line);
6131static const rb_data_type_t parser_data_type;
6135parser_compile_string(VALUE vparser, VALUE fname, VALUE s, int line)
6137 struct parser_params *p;
6139 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6141 p->lex.gets = lex_get_str;
6142 p->lex.gets_.ptr = 0;
6143 p->lex.input = rb_str_new_frozen(s);
6144 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6146 return yycompile(vparser, p, fname, line);
6150rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
6152 return rb_parser_compile_string_path(vparser, rb_filesystem_str_new_cstr(f), s, line);
6156rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
6158 must_be_ascii_compatible(s);
6159 return parser_compile_string(vparser, f, s, line);
6162VALUE rb_io_gets_internal(VALUE io);
6165lex_io_gets(struct parser_params *p, VALUE io)
6167 return rb_io_gets_internal(io);
6171rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
6173 struct parser_params *p;
6175 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6177 p->lex.gets = lex_io_gets;
6178 p->lex.input = file;
6179 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6181 return yycompile(vparser, p, fname, start);
6185lex_generic_gets(struct parser_params *p, VALUE input)
6187 return (*p->lex.gets_.call)(input, p->line_count);
6191rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
6193 struct parser_params *p;
6195 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
6197 p->lex.gets = lex_generic_gets;
6198 p->lex.gets_.call = lex_gets;
6199 p->lex.input = input;
6200 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
6202 return yycompile(vparser, p, fname, start);
6206#define STR_FUNC_ESCAPE 0x01
6207#define STR_FUNC_EXPAND 0x02
6208#define STR_FUNC_REGEXP 0x04
6209#define STR_FUNC_QWORDS 0x08
6210#define STR_FUNC_SYMBOL 0x10
6211#define STR_FUNC_INDENT 0x20
6212#define STR_FUNC_LABEL 0x40
6213#define STR_FUNC_LIST 0x4000
6214#define STR_FUNC_TERM 0x8000
6217 str_label = STR_FUNC_LABEL,
6219 str_dquote = (STR_FUNC_EXPAND),
6220 str_xquote = (STR_FUNC_EXPAND),
6221 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
6222 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
6223 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
6224 str_ssym = (STR_FUNC_SYMBOL),
6225 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
6229parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
6233 str = rb_enc_str_new(ptr, len, enc);
6234 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
6235 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
6237 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
6238 rb_enc_associate(str, rb_ascii8bit_encoding());
6245#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
6246#define lex_eol_p(p) ((p)->lex.pcur >= (p)->lex.pend)
6247#define lex_eol_n_p(p,n) ((p)->lex.pcur+(n) >= (p)->lex.pend)
6248#define peek(p,c) peek_n(p, (c), 0)
6249#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
6250#define peekc(p) peekc_n(p, 0)
6251#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
6255add_delayed_token(struct parser_params *p, const char *tok, const char *end)
6258 if (!has_delayed_token(p)) {
6259 p->delayed.token = rb_str_buf_new(end - tok);
6260 rb_enc_associate(p->delayed.token, p->enc);
6261 p->delayed.line = p->ruby_sourceline;
6262 p->delayed.col = rb_long2int(tok - p->lex.pbeg);
6264 rb_str_buf_cat(p->delayed.token, tok, end - tok);
6269#define add_delayed_token(p, tok, end) ((void)(tok), (void)(end))
6273nextline(struct parser_params *p)
6275 VALUE v = p->lex.nextline;
6276 p->lex.nextline = 0;
6281 if (p->lex.pend > p->lex.pbeg && *(p->lex.pend-1) != '\n') {
6285 if (!p->lex.input || NIL_P(v = lex_getline(p))) {
6293 else if (NIL_P(v)) {
6294 /* after here-document without terminator */
6297 add_delayed_token(p, p->lex.ptok, p->lex.pend);
6298 if (p->heredoc_end > 0) {
6299 p->ruby_sourceline = p->heredoc_end;
6302 p->ruby_sourceline++;
6303 p->lex.pbeg = p->lex.pcur = RSTRING_PTR(v);
6304 p->lex.pend = p->lex.pcur + RSTRING_LEN(v);
6306 p->lex.prevline = p->lex.lastline;
6307 p->lex.lastline = v;
6312parser_cr(struct parser_params *p, int c)
6314 if (peek(p, '\n')) {
6318 else if (!p->cr_seen) {
6320 /* carried over with p->lex.nextline for nextc() */
6321 rb_warn0("encountered \\r in middle of line, treated as a mere space");
6327nextc(struct parser_params *p)
6331 if (UNLIKELY((p->lex.pcur == p->lex.pend) || p->eofp || RTEST(p->lex.nextline))) {
6332 if (nextline(p)) return -1;
6334 c = (unsigned char)*p->lex.pcur++;
6335 if (UNLIKELY(c == '\r')) {
6336 c = parser_cr(p, c);
6343pushback(struct parser_params *p, int c)
6345 if (c == -1) return;
6347 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
6352#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
6354#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
6355#define tok(p) (p)->tokenbuf
6356#define toklen(p) (p)->tokidx
6359looking_at_eol_p(struct parser_params *p)
6361 const char *ptr = p->lex.pcur;
6362 while (ptr < p->lex.pend) {
6363 int c = (unsigned char)*ptr++;
6364 int eol = (c == '\n' || c == '#');
6365 if (eol || !ISSPACE(c)) {
6373newtok(struct parser_params *p)
6376 p->tokline = p->ruby_sourceline;
6379 p->tokenbuf = ALLOC_N(char, 60);
6381 if (p->toksiz > 4096) {
6383 REALLOC_N(p->tokenbuf, char, 60);
6389tokspace(struct parser_params *p, int n)
6393 if (p->tokidx >= p->toksiz) {
6394 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
6395 REALLOC_N(p->tokenbuf, char, p->toksiz);
6397 return &p->tokenbuf[p->tokidx-n];
6401tokadd(struct parser_params *p, int c)
6403 p->tokenbuf[p->tokidx++] = (char)c;
6404 if (p->tokidx >= p->toksiz) {
6406 REALLOC_N(p->tokenbuf, char, p->toksiz);
6411tok_hex(struct parser_params *p, size_t *numlen)
6415 c = scan_hex(p->lex.pcur, 2, numlen);
6417 yyerror0("invalid hex escape");
6421 p->lex.pcur += *numlen;
6425#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
6428escaped_control_code(int c)
6454#define WARN_SPACE_CHAR(c, prefix) \
6455 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c2))
6458tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
6459 int regexp_literal, int wide)
6462 int codepoint = scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
6463 literal_flush(p, p->lex.pcur);
6464 p->lex.pcur += numlen;
6465 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
6466 yyerror0("invalid Unicode escape");
6467 return wide && numlen > 0;
6469 if (codepoint > 0x10ffff) {
6470 yyerror0("invalid Unicode codepoint (too large)");
6473 if ((codepoint & 0xfffff800) == 0xd800) {
6474 yyerror0("invalid Unicode codepoint");
6477 if (regexp_literal) {
6478 tokcopy(p, (int)numlen);
6480 else if (codepoint >= 0x80) {
6481 rb_encoding *utf8 = rb_utf8_encoding();
6482 if (*encp && utf8 != *encp) {
6483 YYLTYPE loc = RUBY_INIT_YYLLOC();
6484 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
6485 parser_show_error_line(p, &loc);
6489 tokaddmbc(p, codepoint, *encp);
6492 tokadd(p, codepoint);
6497/* return value is for ?\u3042 */
6499tokadd_utf8(struct parser_params *p, rb_encoding **encp,
6500 int term, int symbol_literal, int regexp_literal)
6503 * If `term` is not -1, then we allow multiple codepoints in \u{}
6504 * upto `term` byte, otherwise we're parsing a character literal.
6505 * And then add the codepoints to the current token.
6507 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
6509 const int open_brace = '{', close_brace = '}';
6511 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
6513 if (peek(p, open_brace)) { /* handle \u{...} form */
6514 const char *second = NULL;
6515 int c, last = nextc(p);
6516 if (p->lex.pcur >= p->lex.pend) goto unterminated;
6517 while (ISSPACE(c = *p->lex.pcur) && ++p->lex.pcur < p->lex.pend);
6518 while (c != close_brace) {
6519 if (c == term) goto unterminated;
6520 if (second == multiple_codepoints)
6521 second = p->lex.pcur;
6522 if (regexp_literal) tokadd(p, last);
6523 if (!tokadd_codepoint(p, encp, regexp_literal, TRUE)) {
6526 while (ISSPACE(c = *p->lex.pcur)) {
6527 if (++p->lex.pcur >= p->lex.pend) goto unterminated;
6530 if (term == -1 && !second)
6531 second = multiple_codepoints;
6534 if (c != close_brace) {
6537 yyerror0("unterminated Unicode escape");
6540 if (second && second != multiple_codepoints) {
6541 const char *pcur = p->lex.pcur;
6542 p->lex.pcur = second;
6543 dispatch_scan_event(p, tSTRING_CONTENT);
6546 yyerror0(multiple_codepoints);
6550 if (regexp_literal) tokadd(p, close_brace);
6553 else { /* handle \uxxxx form */
6554 if (!tokadd_codepoint(p, encp, regexp_literal, FALSE)) {
6561#define ESCAPE_CONTROL 1
6562#define ESCAPE_META 2
6565read_escape(struct parser_params *p, int flags, rb_encoding **encp)
6570 switch (c = nextc(p)) {
6571 case '\\': /* Backslash */
6574 case 'n': /* newline */
6577 case 't': /* horizontal tab */
6580 case 'r': /* carriage-return */
6583 case 'f': /* form-feed */
6586 case 'v': /* vertical tab */
6589 case 'a': /* alarm(bell) */
6592 case 'e': /* escape */
6595 case '0': case '1': case '2': case '3': /* octal constant */
6596 case '4': case '5': case '6': case '7':
6598 c = scan_oct(p->lex.pcur, 3, &numlen);
6599 p->lex.pcur += numlen;
6602 case 'x': /* hex constant */
6603 c = tok_hex(p, &numlen);
6604 if (numlen == 0) return 0;
6607 case 'b': /* backspace */
6610 case 's': /* space */
6614 if (flags & ESCAPE_META) goto eof;
6615 if ((c = nextc(p)) != '-') {
6618 if ((c = nextc(p)) == '\\') {
6619 if (peek(p, 'u')) goto eof;
6620 return read_escape(p, flags|ESCAPE_META, encp) | 0x80;
6622 else if (c == -1 || !ISASCII(c)) goto eof;
6624 int c2 = escaped_control_code(c);
6626 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
6627 WARN_SPACE_CHAR(c2, "\\M-");
6630 WARN_SPACE_CHAR(c2, "\\C-\\M-");
6633 else if (ISCNTRL(c)) goto eof;
6634 return ((c & 0xff) | 0x80);
6638 if ((c = nextc(p)) != '-') {
6642 if (flags & ESCAPE_CONTROL) goto eof;
6643 if ((c = nextc(p))== '\\') {
6644 if (peek(p, 'u')) goto eof;
6645 c = read_escape(p, flags|ESCAPE_CONTROL, encp);
6649 else if (c == -1 || !ISASCII(c)) goto eof;
6651 int c2 = escaped_control_code(c);
6654 if (flags & ESCAPE_META) {
6655 WARN_SPACE_CHAR(c2, "\\M-");
6658 WARN_SPACE_CHAR(c2, "");
6662 if (flags & ESCAPE_META) {
6663 WARN_SPACE_CHAR(c2, "\\M-\\C-");
6666 WARN_SPACE_CHAR(c2, "\\C-");
6670 else if (ISCNTRL(c)) goto eof;
6676 yyerror0("Invalid escape character syntax");
6686tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
6688 int len = rb_enc_codelen(c, enc);
6689 rb_enc_mbcput(c, tokspace(p, len), enc);
6693tokadd_escape(struct parser_params *p, rb_encoding **encp)
6700 switch (c = nextc(p)) {
6702 return 0; /* just ignore */
6704 case '0': case '1': case '2': case '3': /* octal constant */
6705 case '4': case '5': case '6': case '7':
6707 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
6708 if (numlen == 0) goto eof;
6709 p->lex.pcur += numlen;
6710 tokcopy(p, (int)numlen + 1);
6714 case 'x': /* hex constant */
6716 tok_hex(p, &numlen);
6717 if (numlen == 0) return -1;
6718 tokcopy(p, (int)numlen + 2);
6723 if (flags & ESCAPE_META) goto eof;
6724 if ((c = nextc(p)) != '-') {
6729 flags |= ESCAPE_META;
6733 if (flags & ESCAPE_CONTROL) goto eof;
6734 if ((c = nextc(p)) != '-') {
6742 if (flags & ESCAPE_CONTROL) goto eof;
6744 flags |= ESCAPE_CONTROL;
6746 if ((c = nextc(p)) == '\\') {
6749 else if (c == -1) goto eof;
6755 yyerror0("Invalid escape character syntax");
6767regx_options(struct parser_params *p)
6775 while (c = nextc(p), ISALPHA(c)) {
6777 options |= RE_OPTION_ONCE;
6779 else if (rb_char_to_option_kcode(c, &opt, &kc)) {
6781 if (kc != rb_ascii8bit_encindex()) kcode = c;
6795 YYLTYPE loc = RUBY_INIT_YYLLOC();
6797 compile_error(p, "unknown regexp option%s - %*s",
6798 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
6799 parser_show_error_line(p, &loc);
6801 return options | RE_OPTION_ENCODING(kcode);
6805tokadd_mbchar(struct parser_params *p, int c)
6807 int len = parser_precise_mbclen(p, p->lex.pcur-1);
6808 if (len < 0) return -1;
6810 p->lex.pcur += --len;
6811 if (len > 0) tokcopy(p, len);
6816simple_re_meta(int c)
6819 case '$': case '*': case '+': case '.':
6820 case '?': case '^': case '|':
6821 case ')': case ']': case '}': case '>':
6829parser_update_heredoc_indent(struct parser_params *p, int c)
6831 if (p->heredoc_line_indent == -1) {
6832 if (c == '\n') p->heredoc_line_indent = 0;
6836 p->heredoc_line_indent++;
6839 else if (c == '\t') {
6840 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
6841 p->heredoc_line_indent = w * TAB_WIDTH;
6844 else if (c != '\n') {
6845 if (p->heredoc_indent > p->heredoc_line_indent) {
6846 p->heredoc_indent = p->heredoc_line_indent;
6848 p->heredoc_line_indent = -1;
6855parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
6857 YYLTYPE loc = RUBY_INIT_YYLLOC();
6858 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
6859 compile_error(p, "%s mixed within %s source", n1, n2);
6860 parser_show_error_line(p, &loc);
6864parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
6866 const char *pos = p->lex.pcur;
6868 parser_mixed_error(p, enc1, enc2);
6873tokadd_string(struct parser_params *p,
6874 int func, int term, int paren, long *nest,
6875 rb_encoding **encp, rb_encoding **enc)
6880#define mixed_error(enc1, enc2) \
6881 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
6882#define mixed_escape(beg, enc1, enc2) \
6883 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
6885 while ((c = nextc(p)) != -1) {
6886 if (p->heredoc_indent > 0) {
6887 parser_update_heredoc_indent(p, c);
6890 if (paren && c == paren) {
6893 else if (c == term) {
6894 if (!nest || !*nest) {
6900 else if ((func & STR_FUNC_EXPAND) && c == '#' && p->lex.pcur < p->lex.pend) {
6901 int c2 = *p->lex.pcur;
6902 if (c2 == '$' || c2 == '@' || c2 == '{') {
6907 else if (c == '\\') {
6908 literal_flush(p, p->lex.pcur - 1);
6912 if (func & STR_FUNC_QWORDS) break;
6913 if (func & STR_FUNC_EXPAND) {
6914 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
6925 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
6929 if ((func & STR_FUNC_EXPAND) == 0) {
6933 tokadd_utf8(p, enc, term,
6934 func & STR_FUNC_SYMBOL,
6935 func & STR_FUNC_REGEXP);
6939 if (c == -1) return -1;
6941 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
6944 if (func & STR_FUNC_REGEXP) {
6945 if (c == term && !simple_re_meta(c)) {
6950 if ((c = tokadd_escape(p, enc)) < 0)
6952 if (*enc && *enc != *encp) {
6953 mixed_escape(p->lex.ptok+2, *enc, *encp);
6957 else if (func & STR_FUNC_EXPAND) {
6959 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
6960 c = read_escape(p, 0, enc);
6962 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6963 /* ignore backslashed spaces in %w */
6965 else if (c != term && !(paren && c == paren)) {
6972 else if (!parser_isascii(p)) {
6977 else if (*enc != *encp) {
6978 mixed_error(*enc, *encp);
6981 if (tokadd_mbchar(p, c) == -1) return -1;
6984 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
6992 else if (*enc != *encp) {
6993 mixed_error(*enc, *encp);
7000 if (*enc) *encp = *enc;
7004static inline rb_strterm_t *
7005new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0)
7007 return (rb_strterm_t*)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
7010/* imemo_parser_strterm for literal */
7011#define NEW_STRTERM(func, term, paren) \
7012 new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0)
7016flush_string_content(struct parser_params *p, rb_encoding *enc)
7018 VALUE content = yylval.val;
7019 if (!ripper_is_node_yylval(content))
7020 content = ripper_new_yylval(p, 0, 0, content);
7021 if (has_delayed_token(p)) {
7022 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
7024 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7026 dispatch_delayed_token(p, tSTRING_CONTENT);
7027 p->lex.ptok = p->lex.pcur;
7028 RNODE(content)->nd_rval = yylval.val;
7030 dispatch_scan_event(p, tSTRING_CONTENT);
7031 if (yylval.val != content)
7032 RNODE(content)->nd_rval = yylval.val;
7033 yylval.val = content;
7036#define flush_string_content(p, enc) ((void)(enc))
7039RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
7040/* this can be shared with ripper, since it's independent from struct
7043#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
7044#define SPECIAL_PUNCT(idx) ( \
7045 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
7046 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
7047 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
7048 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
7049 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
7051const unsigned int ruby_global_name_punct_bits[] = {
7060static enum yytokentype
7061parser_peek_variable_name(struct parser_params *p)
7064 const char *ptr = p->lex.pcur;
7066 if (ptr + 1 >= p->lex.pend) return 0;
7070 if ((c = *ptr) == '-') {
7071 if (++ptr >= p->lex.pend) return 0;
7074 else if (is_global_name_punct(c) || ISDIGIT(c)) {
7075 return tSTRING_DVAR;
7079 if ((c = *ptr) == '@') {
7080 if (++ptr >= p->lex.pend) return 0;
7086 p->command_start = TRUE;
7087 return tSTRING_DBEG;
7091 if (!ISASCII(c) || c == '_' || ISALPHA(c))
7092 return tSTRING_DVAR;
7096#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
7097#define IS_END() IS_lex_state(EXPR_END_ANY)
7098#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
7099#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
7100#define IS_LABEL_POSSIBLE() (\
7101 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
7103#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
7104#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
7106static inline enum yytokentype
7107parser_string_term(struct parser_params *p, int func)
7110 if (func & STR_FUNC_REGEXP) {
7111 set_yylval_num(regx_options(p));
7112 dispatch_scan_event(p, tREGEXP_END);
7113 SET_LEX_STATE(EXPR_END);
7116 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
7118 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
7121 SET_LEX_STATE(EXPR_END);
7125static enum yytokentype
7126parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
7128 int func = (int)quote->u1.func;
7129 int term = (int)quote->u3.term;
7130 int paren = (int)quote->u2.paren;
7132 rb_encoding *enc = p->enc;
7133 rb_encoding *base_enc = 0;
7136 if (func & STR_FUNC_TERM) {
7137 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
7138 SET_LEX_STATE(EXPR_END);
7140 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
7143 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
7144 do {c = nextc(p);} while (ISSPACE(c));
7147 if (func & STR_FUNC_LIST) {
7148 quote->u1.func &= ~STR_FUNC_LIST;
7151 if (c == term && !quote->u0.nest) {
7152 if (func & STR_FUNC_QWORDS) {
7153 quote->u1.func |= STR_FUNC_TERM;
7154 pushback(p, c); /* dispatch the term at tSTRING_END */
7155 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7158 return parser_string_term(p, func);
7162 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
7166 if ((func & STR_FUNC_EXPAND) && c == '#') {
7167 int t = parser_peek_variable_name(p);
7173 if (tokadd_string(p, func, term, paren, "e->u0.nest,
7174 &enc, &base_enc) == -1) {
7177# define unterminated_literal(mesg) yyerror0(mesg)
7179# define unterminated_literal(mesg) compile_error(p, mesg)
7181 literal_flush(p, p->lex.pcur);
7182 if (func & STR_FUNC_QWORDS) {
7183 /* no content to add, bailing out here */
7184 unterminated_literal("unterminated list meets end of file");
7188 if (func & STR_FUNC_REGEXP) {
7189 unterminated_literal("unterminated regexp meets end of file");
7192 unterminated_literal("unterminated string meets end of file");
7194 quote->u1.func |= STR_FUNC_TERM;
7199 lit = STR_NEW3(tok(p), toklen(p), enc, func);
7200 set_yylval_str(lit);
7201 flush_string_content(p, enc);
7203 return tSTRING_CONTENT;
7206static enum yytokentype
7207heredoc_identifier(struct parser_params *p)
7210 * term_len is length of `<<"END"` except `END`,
7211 * in this case term_len is 4 (<, <, " and ").
7213 long len, offset = p->lex.pcur - p->lex.pbeg;
7214 int c = nextc(p), term, func = 0, quote = 0;
7215 enum yytokentype token = tSTRING_BEG;
7220 func = STR_FUNC_INDENT;
7223 else if (c == '~') {
7225 func = STR_FUNC_INDENT;
7231 func |= str_squote; goto quoted;
7233 func |= str_dquote; goto quoted;
7235 token = tXSTRING_BEG;
7236 func |= str_xquote; goto quoted;
7243 while ((c = nextc(p)) != term) {
7244 if (c == -1 || c == '\r' || c == '\n') {
7245 yyerror(NULL, p, "unterminated here document identifier");
7252 if (!parser_is_identchar(p)) {
7254 if (func & STR_FUNC_INDENT) {
7255 pushback(p, indent > 0 ? '~' : '-');
7261 int n = parser_precise_mbclen(p, p->lex.pcur-1);
7262 if (n < 0) return 0;
7264 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
7269 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
7270 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
7271 yyerror(NULL, p, "too long here document identifier");
7272 dispatch_scan_event(p, tHEREDOC_BEG);
7275 p->lex.strterm = new_strterm(0, 0, 0, p->lex.lastline);
7276 p->lex.strterm->flags |= STRTERM_HEREDOC;
7277 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
7278 here->offset = offset;
7279 here->sourceline = p->ruby_sourceline;
7280 here->length = (int)len;
7281 here->quote = quote;
7285 p->heredoc_indent = indent;
7286 p->heredoc_line_indent = 0;
7291heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
7296 line = here->lastline;
7297 p->lex.lastline = line;
7298 p->lex.pbeg = RSTRING_PTR(line);
7299 p->lex.pend = p->lex.pbeg + RSTRING_LEN(line);
7300 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
7301 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
7302 p->heredoc_end = p->ruby_sourceline;
7303 p->ruby_sourceline = (int)here->sourceline;
7304 if (p->eofp) p->lex.nextline = Qnil;
7309dedent_string(VALUE string, int width)
7315 RSTRING_GETMEM(string, str, len);
7316 for (i = 0; i < len && col < width; i++) {
7317 if (str[i] == ' ') {
7320 else if (str[i] == '\t') {
7321 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
7322 if (n > width) break;
7330 rb_str_modify(string);
7331 str = RSTRING_PTR(string);
7332 if (RSTRING_LEN(string) != len)
7333 rb_fatal("literal string changed: %+"PRIsVALUE, string);
7334 MEMMOVE(str, str + i, char, len - i);
7335 rb_str_set_len(string, len - i);
7341heredoc_dedent(struct parser_params *p, NODE *root)
7343 NODE *node, *str_node, *prev_node;
7344 int indent = p->heredoc_indent;
7347 if (indent <= 0) return root;
7348 p->heredoc_indent = 0;
7349 if (!root) return root;
7351 prev_node = node = str_node = root;
7352 if (nd_type(root) == NODE_LIST) str_node = root->nd_head;
7355 VALUE lit = str_node->nd_lit;
7356 if (str_node->flags & NODE_FL_NEWLINE) {
7357 dedent_string(lit, indent);
7362 else if (!literal_concat0(p, prev_lit, lit)) {
7366 NODE *end = node->nd_end;
7367 node = prev_node->nd_next = node->nd_next;
7369 if (nd_type(prev_node) == NODE_DSTR)
7370 nd_set_type(prev_node, NODE_STR);
7378 while ((node = (prev_node = node)->nd_next) != 0) {
7380 if (nd_type(node) != NODE_LIST) break;
7381 if ((str_node = node->nd_head) != 0) {
7382 enum node_type type = nd_type(str_node);
7383 if (type == NODE_STR || type == NODE_DSTR) break;
7393heredoc_dedent(struct parser_params *p, VALUE array)
7395 int indent = p->heredoc_indent;
7397 if (indent <= 0) return array;
7398 p->heredoc_indent = 0;
7399 dispatch2(heredoc_dedent, array, INT2NUM(indent));
7405 * Ripper.dedent_string(input, width) -> Integer
7407 * USE OF RIPPER LIBRARY ONLY.
7409 * Strips up to +width+ leading whitespaces from +input+,
7410 * and returns the stripped column width.
7413parser_dedent_string(VALUE self, VALUE input, VALUE width)
7418 wid = NUM2UINT(width);
7419 col = dedent_string(input, wid);
7420 return INT2NUM(col);
7425whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
7427 const char *ptr = p->lex.pbeg;
7431 while (*ptr && ISSPACE(*ptr)) ptr++;
7433 n = p->lex.pend - (ptr + len);
7434 if (n < 0) return FALSE;
7435 if (n > 0 && ptr[len] != '\n') {
7436 if (ptr[len] != '\r') return FALSE;
7437 if (n <= 1 || ptr[len+1] != '\n') return FALSE;
7439 return strncmp(eos, ptr, len) == 0;
7443word_match_p(struct parser_params *p, const char *word, long len)
7445 if (strncmp(p->lex.pcur, word, len)) return 0;
7446 if (p->lex.pcur + len == p->lex.pend) return 1;
7447 int c = (unsigned char)p->lex.pcur[len];
7448 if (ISSPACE(c)) return 1;
7450 case '\0': case '\004': case '\032': return 1;
7455#define NUM_SUFFIX_R (1<<0)
7456#define NUM_SUFFIX_I (1<<1)
7457#define NUM_SUFFIX_ALL 3
7460number_literal_suffix(struct parser_params *p, int mask)
7463 const char *lastp = p->lex.pcur;
7465 while ((c = nextc(p)) != -1) {
7466 if ((mask & NUM_SUFFIX_I) && c == 'i') {
7467 result |= (mask & NUM_SUFFIX_I);
7468 mask &= ~NUM_SUFFIX_I;
7469 /* r after i, rational of complex is disallowed */
7470 mask &= ~NUM_SUFFIX_R;
7473 if ((mask & NUM_SUFFIX_R) && c == 'r') {
7474 result |= (mask & NUM_SUFFIX_R);
7475 mask &= ~NUM_SUFFIX_R;
7478 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
7479 p->lex.pcur = lastp;
7480 literal_flush(p, p->lex.pcur);
7489static enum yytokentype
7490set_number_literal(struct parser_params *p, VALUE v,
7491 enum yytokentype type, int suffix)
7493 if (suffix & NUM_SUFFIX_I) {
7494 v = rb_complex_raw(INT2FIX(0), v);
7497 set_yylval_literal(v);
7498 SET_LEX_STATE(EXPR_END);
7502static enum yytokentype
7503set_integer_literal(struct parser_params *p, VALUE v, int suffix)
7505 enum yytokentype type = tINTEGER;
7506 if (suffix & NUM_SUFFIX_R) {
7507 v = rb_rational_raw1(v);
7510 return set_number_literal(p, v, type, suffix);
7515dispatch_heredoc_end(struct parser_params *p)
7518 if (has_delayed_token(p))
7519 dispatch_delayed_token(p, tSTRING_CONTENT);
7520 str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
7521 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
7527#define dispatch_heredoc_end(p) ((void)0)
7530static enum yytokentype
7531here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
7533 int c, func, indent = 0;
7534 const char *eos, *ptr, *ptr_end;
7537 rb_encoding *enc = p->enc;
7538 rb_encoding *base_enc = 0;
7541 eos = RSTRING_PTR(here->lastline) + here->offset;
7543 indent = (func = here->func) & STR_FUNC_INDENT;
7545 if ((c = nextc(p)) == -1) {
7548 if (!has_delayed_token(p)) {
7549 dispatch_scan_event(p, tSTRING_CONTENT);
7552 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
7553 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
7554 int cr = ENC_CODERANGE_UNKNOWN;
7555 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
7556 if (cr != ENC_CODERANGE_7BIT &&
7557 p->enc == rb_usascii_encoding() &&
7558 enc != rb_utf8_encoding()) {
7559 enc = rb_ascii8bit_encoding();
7562 rb_enc_str_buf_cat(p->delayed.token, p->lex.ptok, len, enc);
7564 dispatch_delayed_token(p, tSTRING_CONTENT);
7568 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7569 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
7573 SET_LEX_STATE(EXPR_END);
7578 /* not beginning of line, cannot be the terminator */
7580 else if (p->heredoc_line_indent == -1) {
7581 /* `heredoc_line_indent == -1` means
7582 * - "after an interpolation in the same line", or
7583 * - "in a continuing line"
7585 p->heredoc_line_indent = 0;
7587 else if (whole_match_p(p, eos, len, indent)) {
7588 dispatch_heredoc_end(p);
7590 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7593 SET_LEX_STATE(EXPR_END);
7597 if (!(func & STR_FUNC_EXPAND)) {
7599 ptr = RSTRING_PTR(p->lex.lastline);
7600 ptr_end = p->lex.pend;
7601 if (ptr_end > ptr) {
7602 switch (ptr_end[-1]) {
7604 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
7613 if (p->heredoc_indent > 0) {
7615 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
7617 p->heredoc_line_indent = 0;
7621 rb_str_cat(str, ptr, ptr_end - ptr);
7623 str = STR_NEW(ptr, ptr_end - ptr);
7624 if (ptr_end < p->lex.pend) rb_str_cat(str, "\n", 1);
7626 if (p->heredoc_indent > 0) {
7629 if (nextc(p) == -1) {
7635 } while (!whole_match_p(p, eos, len, indent));
7638 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
7641 int t = parser_peek_variable_name(p);
7642 if (p->heredoc_line_indent != -1) {
7643 if (p->heredoc_indent > p->heredoc_line_indent) {
7644 p->heredoc_indent = p->heredoc_line_indent;
7646 p->heredoc_line_indent = -1;
7655 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
7656 if (p->eofp) goto error;
7660 if (c == '\\') p->heredoc_line_indent = -1;
7662 str = STR_NEW3(tok(p), toklen(p), enc, func);
7664 set_yylval_str(str);
7666 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7668 flush_string_content(p, enc);
7669 return tSTRING_CONTENT;
7671 tokadd(p, nextc(p));
7672 if (p->heredoc_indent > 0) {
7676 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
7677 if ((c = nextc(p)) == -1) goto error;
7678 } while (!whole_match_p(p, eos, len, indent));
7679 str = STR_NEW3(tok(p), toklen(p), enc, func);
7681 dispatch_heredoc_end(p);
7683 str = ripper_new_yylval(p, ripper_token2eventid(tSTRING_CONTENT),
7686 heredoc_restore(p, &p->lex.strterm->u.heredoc);
7688 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
7689 set_yylval_str(str);
7691 if (bol) yylval.node->flags |= NODE_FL_NEWLINE;
7693 return tSTRING_CONTENT;
7699arg_ambiguous(struct parser_params *p, char c)
7702 rb_warning1("ambiguous first argument; put parentheses or a space even after `%c' operator", WARN_I(c));
7704 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
7710formal_argument(struct parser_params *p, ID lhs)
7712 switch (id_type(lhs)) {
7717 yyerror0("formal argument cannot be a constant");
7720 yyerror0("formal argument cannot be an instance variable");
7723 yyerror0("formal argument cannot be a global variable");
7726 yyerror0("formal argument cannot be a class variable");
7729 yyerror0("formal argument must be local variable");
7733 lhs = dispatch1(param_error, lhs);
7738 shadowing_lvar(p, lhs);
7743lvar_defined(struct parser_params *p, ID id)
7745 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
7748/* emacsen -*- hack */
7750parser_encode_length(struct parser_params *p, const char *name, long len)
7754 if (len > 5 && name[nlen = len - 5] == '-') {
7755 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
7758 if (len > 4 && name[nlen = len - 4] == '-') {
7759 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
7761 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
7762 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
7763 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
7770parser_set_encode(struct parser_params *p, const char *name)
7772 int idx = rb_enc_find_index(name);
7777 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
7779 excargs[0] = rb_eArgError;
7780 excargs[2] = rb_make_backtrace();
7781 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
7782 rb_exc_raise(rb_make_exception(3, excargs));
7784 enc = rb_enc_from_index(idx);
7785 if (!rb_enc_asciicompat(enc)) {
7786 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
7791 if (p->debug_lines) {
7792 VALUE lines = p->debug_lines;
7793 long i, n = RARRAY_LEN(lines);
7794 for (i = 0; i < n; ++i) {
7795 rb_enc_associate_index(RARRAY_AREF(lines, i), idx);
7802comment_at_top(struct parser_params *p)
7804 const char *ptr = p->lex.pbeg, *ptr_end = p->lex.pcur - 1;
7805 if (p->line_count != (p->has_shebang ? 2 : 1)) return 0;
7806 while (ptr < ptr_end) {
7807 if (!ISSPACE(*ptr)) return 0;
7813typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
7814typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
7817magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
7819 if (!comment_at_top(p)) {
7822 parser_set_encode(p, val);
7826parser_get_bool(struct parser_params *p, const char *name, const char *val)
7830 if (strcasecmp(val, "true") == 0) {
7835 if (strcasecmp(val, "false") == 0) {
7840 rb_compile_warning(p->ruby_sourcefile, p->ruby_sourceline, "invalid value for %s: %s", name, val);
7845parser_set_token_info(struct parser_params *p, const char *name, const char *val)
7847 int b = parser_get_bool(p, name, val);
7848 if (b >= 0) p->token_info_enabled = b;
7852parser_set_compile_option_flag(struct parser_params *p, const char *name, const char *val)
7856 if (p->token_seen) {
7857 rb_warning1("`%s' is ignored after any tokens", WARN_S(name));
7861 b = parser_get_bool(p, name, val);
7864 if (!p->compile_option)
7865 p->compile_option = rb_obj_hide(rb_ident_hash_new());
7866 rb_hash_aset(p->compile_option, ID2SYM(rb_intern(name)),
7867 (b ? Qtrue : Qfalse));
7872parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
7874 int b = parser_get_bool(p, name, val);
7875 if (b >= 0) p->past_scope_enabled = b;
7879struct magic_comment {
7881 rb_magic_comment_setter_t func;
7882 rb_magic_comment_length_t length;
7885static const struct magic_comment magic_comments[] = {
7886 {"coding", magic_comment_encoding, parser_encode_length},
7887 {"encoding", magic_comment_encoding, parser_encode_length},
7888 {"frozen_string_literal", parser_set_compile_option_flag},
7889 {"warn_indent", parser_set_token_info},
7891 {"warn_past_scope", parser_set_past_scope},
7896magic_comment_marker(const char *str, long len)
7903 if (str[i-1] == '*' && str[i-2] == '-') {
7909 if (i + 1 >= len) return 0;
7910 if (str[i+1] != '-') {
7913 else if (str[i-1] != '-') {
7929parser_magic_comment(struct parser_params *p, const char *str, long len)
7932 VALUE name = 0, val = 0;
7933 const char *beg, *end, *vbeg, *vend;
7934#define str_copy(_s, _p, _n) ((_s) \
7935 ? (void)(rb_str_resize((_s), (_n)), \
7936 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
7937 : (void)((_s) = STR_NEW((_p), (_n))))
7939 if (len <= 7) return FALSE;
7940 if (!!(beg = magic_comment_marker(str, len))) {
7941 if (!(end = magic_comment_marker(beg, str + len - beg)))
7945 len = end - beg - 3;
7948 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
7950 const struct magic_comment *mc = magic_comments;
7955 for (; len > 0 && *str; str++, --len) {
7957 case '\'': case '"': case ':': case ';':
7960 if (!ISSPACE(*str)) break;
7962 for (beg = str; len > 0; str++, --len) {
7964 case '\'': case '"': case ':': case ';':
7967 if (ISSPACE(*str)) break;
7972 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
7975 if (!indicator) return FALSE;
7979 do str++; while (--len > 0 && ISSPACE(*str));
7982 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
7995 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
7999 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
8002 while (len > 0 && (ISSPACE(*str))) --len, str++;
8003 if (len) return FALSE;
8007 str_copy(name, beg, n);
8008 s = RSTRING_PTR(name);
8009 for (i = 0; i < n; ++i) {
8010 if (s[i] == '-') s[i] = '_';
8013 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
8016 n = (*mc->length)(p, vbeg, n);
8018 str_copy(val, vbeg, n);
8019 (*mc->func)(p, mc->name, RSTRING_PTR(val));
8022 } while (++mc < magic_comments + numberof(magic_comments));
8024 str_copy(val, vbeg, vend - vbeg);
8025 dispatch2(magic_comment, name, val);
8033set_file_encoding(struct parser_params *p, const char *str, const char *send)
8036 const char *beg = str;
8040 if (send - str <= 6) return;
8042 case 'C': case 'c': str += 6; continue;
8043 case 'O': case 'o': str += 5; continue;
8044 case 'D': case 'd': str += 4; continue;
8045 case 'I': case 'i': str += 3; continue;
8046 case 'N': case 'n': str += 2; continue;
8047 case 'G': case 'g': str += 1; continue;
8054 if (ISSPACE(*str)) break;
8057 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
8061 if (++str >= send) return;
8062 } while (ISSPACE(*str));
8064 if (*str != '=' && *str != ':') return;
8069 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
8070 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
8071 parser_set_encode(p, RSTRING_PTR(s));
8072 rb_str_resize(s, 0);
8076parser_prepare(struct parser_params *p)
8079 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
8082 if (peek(p, '!')) p->has_shebang = 1;
8084 case 0xef: /* UTF-8 BOM marker */
8085 if (p->lex.pend - p->lex.pcur >= 2 &&
8086 (unsigned char)p->lex.pcur[0] == 0xbb &&
8087 (unsigned char)p->lex.pcur[1] == 0xbf) {
8088 p->enc = rb_utf8_encoding();
8090 p->lex.pbeg = p->lex.pcur;
8098 p->enc = rb_enc_get(p->lex.lastline);
8102#define ambiguous_operator(tok, op, syn) ( \
8103 rb_warning0("`"op"' after local variable or literal is interpreted as binary operator"), \
8104 rb_warning0("even though it seems like "syn""))
8106#define ambiguous_operator(tok, op, syn) \
8107 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
8109#define warn_balanced(tok, op, syn) ((void) \
8110 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
8111 space_seen && !ISSPACE(c) && \
8112 (ambiguous_operator(tok, op, syn), 0)), \
8113 (enum yytokentype)(tok))
8116parse_rational(struct parser_params *p, char *str, int len, int seen_point)
8119 char *point = &str[seen_point];
8120 size_t fraclen = len-seen_point-1;
8121 memmove(point, point+1, fraclen+1);
8122 v = rb_cstr_to_inum(str, 10, FALSE);
8123 return rb_rational_new(v, rb_int_positive_pow(10, fraclen));
8126static enum yytokentype
8127no_digits(struct parser_params *p)
8129 yyerror0("numeric literal without digits");
8130 if (peek(p, '_')) nextc(p);
8131 /* dummy 0, for tUMINUS_NUM at numeric */
8132 return set_integer_literal(p, INT2FIX(0), 0);
8135static enum yytokentype
8136parse_numeric(struct parser_params *p, int c)
8138 int is_float, seen_point, seen_e, nondigit;
8141 is_float = seen_point = seen_e = nondigit = 0;
8142 SET_LEX_STATE(EXPR_END);
8144 if (c == '-' || c == '+') {
8149 int start = toklen(p);
8151 if (c == 'x' || c == 'X') {
8154 if (c != -1 && ISXDIGIT(c)) {
8157 if (nondigit) break;
8161 if (!ISXDIGIT(c)) break;
8164 } while ((c = nextc(p)) != -1);
8168 if (toklen(p) == start) {
8169 return no_digits(p);
8171 else if (nondigit) goto trailing_uc;
8172 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8173 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 16, FALSE), suffix);
8175 if (c == 'b' || c == 'B') {
8178 if (c == '0' || c == '1') {
8181 if (nondigit) break;
8185 if (c != '0' && c != '1') break;
8188 } while ((c = nextc(p)) != -1);
8192 if (toklen(p) == start) {
8193 return no_digits(p);
8195 else if (nondigit) goto trailing_uc;
8196 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8197 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 2, FALSE), suffix);
8199 if (c == 'd' || c == 'D') {
8202 if (c != -1 && ISDIGIT(c)) {
8205 if (nondigit) break;
8209 if (!ISDIGIT(c)) break;
8212 } while ((c = nextc(p)) != -1);
8216 if (toklen(p) == start) {
8217 return no_digits(p);
8219 else if (nondigit) goto trailing_uc;
8220 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8221 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8227 if (c == 'o' || c == 'O') {
8228 /* prefixed octal */
8230 if (c == -1 || c == '_' || !ISDIGIT(c)) {
8231 return no_digits(p);
8234 if (c >= '0' && c <= '7') {
8239 if (nondigit) break;
8243 if (c < '0' || c > '9') break;
8244 if (c > '7') goto invalid_octal;
8247 } while ((c = nextc(p)) != -1);
8248 if (toklen(p) > start) {
8251 if (nondigit) goto trailing_uc;
8252 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8253 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 8, FALSE), suffix);
8260 if (c > '7' && c <= '9') {
8262 yyerror0("Invalid octal digit");
8264 else if (c == '.' || c == 'e' || c == 'E') {
8269 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8270 return set_integer_literal(p, INT2FIX(0), suffix);
8276 case '0': case '1': case '2': case '3': case '4':
8277 case '5': case '6': case '7': case '8': case '9':
8283 if (nondigit) goto trailing_uc;
8284 if (seen_point || seen_e) {
8289 if (c0 == -1 || !ISDIGIT(c0)) {
8295 seen_point = toklen(p);
8314 if (c != '-' && c != '+' && !ISDIGIT(c)) {
8319 tokadd(p, nondigit);
8323 nondigit = (c == '-' || c == '+') ? c : 0;
8326 case '_': /* `_' in number just ignored */
8327 if (nondigit) goto decode_num;
8341 literal_flush(p, p->lex.pcur - 1);
8342 YYLTYPE loc = RUBY_INIT_YYLLOC();
8343 compile_error(p, "trailing `%c' in number", nondigit);
8344 parser_show_error_line(p, &loc);
8348 enum yytokentype type = tFLOAT;
8351 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
8352 if (suffix & NUM_SUFFIX_R) {
8354 v = parse_rational(p, tok(p), toklen(p), seen_point);
8357 double d = strtod(tok(p), 0);
8358 if (errno == ERANGE) {
8359 rb_warning1("Float %s out of range", WARN_S(tok(p)));
8364 return set_number_literal(p, v, type, suffix);
8366 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
8367 return set_integer_literal(p, rb_cstr_to_inum(tok(p), 10, FALSE), suffix);
8370static enum yytokentype
8371parse_qmark(struct parser_params *p, int space_seen)
8378 SET_LEX_STATE(EXPR_VALUE);
8383 compile_error(p, "incomplete character syntax");
8386 if (rb_enc_isspace(c, p->enc)) {
8388 int c2 = escaped_control_code(c);
8390 WARN_SPACE_CHAR(c2, "?");
8395 SET_LEX_STATE(EXPR_VALUE);
8400 if (!parser_isascii(p)) {
8401 if (tokadd_mbchar(p, c) == -1) return 0;
8403 else if ((rb_enc_isalnum(c, p->enc) || c == '_') &&
8404 p->lex.pcur < p->lex.pend && is_identchar(p->lex.pcur, p->lex.pend, p->enc)) {
8406 const char *start = p->lex.pcur - 1, *ptr = start;
8408 int n = parser_precise_mbclen(p, ptr);
8409 if (n < 0) return -1;
8411 } while (ptr < p->lex.pend && is_identchar(ptr, p->lex.pend, p->enc));
8412 rb_warn2("`?' just followed by `%.*s' is interpreted as" \
8413 " a conditional operator, put a space after `?'",
8414 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
8418 else if (c == '\\') {
8421 enc = rb_utf8_encoding();
8422 tokadd_utf8(p, &enc, -1, 0, 0);
8424 else if (!lex_eol_p(p) && !(c = *p->lex.pcur, ISASCII(c))) {
8426 if (tokadd_mbchar(p, c) == -1) return 0;
8429 c = read_escape(p, 0, &enc);
8437 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
8438 set_yylval_str(lit);
8439 SET_LEX_STATE(EXPR_END);
8443static enum yytokentype
8444parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
8447 const char *ptok = p->lex.pcur;
8455 if (c == -1 || !ISALNUM(c)) {
8461 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
8462 yyerror0("unknown type of %string");
8466 if (c == -1 || term == -1) {
8467 compile_error(p, "unterminated quoted string meets end of file");
8471 if (term == '(') term = ')';
8472 else if (term == '[') term = ']';
8473 else if (term == '{') term = '}';
8474 else if (term == '<') term = '>';
8477 p->lex.ptok = ptok-1;
8480 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
8484 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
8488 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8492 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8496 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
8497 return tSYMBOLS_BEG;
8500 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
8501 return tQSYMBOLS_BEG;
8504 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
8505 return tXSTRING_BEG;
8508 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
8512 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
8513 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
8517 yyerror0("unknown type of %string");
8521 if ((c = nextc(p)) == '=') {
8523 SET_LEX_STATE(EXPR_BEG);
8526 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
8529 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
8531 return warn_balanced('%', "%%", "string literal");
8535tokadd_ident(struct parser_params *p, int c)
8538 if (tokadd_mbchar(p, c) == -1) return -1;
8540 } while (parser_is_identchar(p));
8546tokenize_ident(struct parser_params *p, const enum lex_state_e last_state)
8548 ID ident = TOK_INTERN();
8550 set_yylval_name(ident);
8556parse_numvar(struct parser_params *p)
8560 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
8561 const unsigned long nth_ref_max =
8562 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
8563 /* NTH_REF is left-shifted to be ORed with back-ref flag and
8564 * turned into a Fixnum, in compile.c */
8566 if (overflow || n > nth_ref_max) {
8567 /* compile_error()? */
8568 rb_warn1("`%s' is too big for a number variable, always nil", WARN_S(tok(p)));
8569 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
8576static enum yytokentype
8577parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
8579 const char *ptr = p->lex.pcur;
8582 SET_LEX_STATE(EXPR_END);
8583 p->lex.ptok = ptr - 1; /* from '$' */
8587 case '_': /* $_: last read line string */
8589 if (parser_is_identchar(p)) {
8597 case '~': /* $~: match-data */
8598 case '*': /* $*: argv */
8599 case '$': /* $$: pid */
8600 case '?': /* $?: last status */
8601 case '!': /* $!: error string */
8602 case '@': /* $@: error position */
8603 case '/': /* $/: input record separator */
8604 case '\\': /* $\: output record separator */
8605 case ';': /* $;: field separator */
8606 case ',': /* $,: output field separator */
8607 case '.': /* $.: last read line number */
8608 case '=': /* $=: ignorecase */
8609 case ':': /* $:: load path */
8610 case '<': /* $<: reading filename */
8611 case '>': /* $>: default output handle */
8612 case '\"': /* $": already loaded files */
8621 if (parser_is_identchar(p)) {
8622 if (tokadd_mbchar(p, c) == -1) return 0;
8630 set_yylval_name(TOK_INTERN());
8633 case '&': /* $&: last match */
8634 case '`': /* $`: string before last match */
8635 case '\'': /* $': string after last match */
8636 case '+': /* $+: string matches last paren. */
8637 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
8642 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
8645 case '1': case '2': case '3':
8646 case '4': case '5': case '6':
8647 case '7': case '8': case '9':
8652 } while (c != -1 && ISDIGIT(c));
8654 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
8656 set_yylval_node(NEW_NTH_REF(parse_numvar(p), &_cur_loc));
8660 if (!parser_is_identchar(p)) {
8661 YYLTYPE loc = RUBY_INIT_YYLLOC();
8662 if (c == -1 || ISSPACE(c)) {
8663 compile_error(p, "`$' without identifiers is not allowed as a global variable name");
8667 compile_error(p, "`$%c' is not allowed as a global variable name", c);
8669 parser_show_error_line(p, &loc);
8670 set_yylval_noname();
8678 if (tokadd_ident(p, c)) return 0;
8679 SET_LEX_STATE(EXPR_END);
8680 tokenize_ident(p, last_state);
8686parser_numbered_param(struct parser_params *p, int n)
8688 if (n < 0) return false;
8690 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
8693 if (p->max_numparam == ORDINAL_PARAM) {
8694 compile_error(p, "ordinary parameter is defined");
8697 struct vtable *args = p->lvtbl->args;
8698 if (p->max_numparam < n) {
8699 p->max_numparam = n;
8701 while (n > args->pos) {
8702 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
8708static enum yytokentype
8709parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
8711 const char *ptr = p->lex.pcur;
8712 enum yytokentype result = tIVAR;
8713 register int c = nextc(p);
8716 p->lex.ptok = ptr - 1; /* from '@' */
8724 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
8725 if (c == -1 || !parser_is_identchar(p)) {
8727 RUBY_SET_YYLLOC(loc);
8728 if (result == tIVAR) {
8729 compile_error(p, "`@' without identifiers is not allowed as an instance variable name");
8732 compile_error(p, "`@@' without identifiers is not allowed as a class variable name");
8734 parser_show_error_line(p, &loc);
8735 set_yylval_noname();
8736 SET_LEX_STATE(EXPR_END);
8739 else if (ISDIGIT(c)) {
8741 RUBY_SET_YYLLOC(loc);
8742 if (result == tIVAR) {
8743 compile_error(p, "`@%c' is not allowed as an instance variable name", c);
8746 compile_error(p, "`@@%c' is not allowed as a class variable name", c);
8748 parser_show_error_line(p, &loc);
8749 set_yylval_noname();
8750 SET_LEX_STATE(EXPR_END);
8754 if (tokadd_ident(p, c)) return 0;
8755 tokenize_ident(p, last_state);
8759static enum yytokentype
8760parse_ident(struct parser_params *p, int c, int cmd_state)
8762 enum yytokentype result;
8763 int mb = ENC_CODERANGE_7BIT;
8764 const enum lex_state_e last_state = p->lex.state;
8768 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN;
8769 if (tokadd_mbchar(p, c) == -1) return 0;
8771 } while (parser_is_identchar(p));
8772 if ((c == '!' || c == '?') && !peek(p, '=')) {
8776 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
8777 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
8778 result = tIDENTIFIER;
8782 result = tCONSTANT; /* assume provisionally */
8787 if (IS_LABEL_POSSIBLE()) {
8788 if (IS_LABEL_SUFFIX(0)) {
8789 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8791 set_yylval_name(TOK_INTERN());
8795 if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) {
8796 const struct kwtable *kw;
8798 /* See if it is a reserved word. */
8799 kw = rb_reserved_word(tok(p), toklen(p));
8801 enum lex_state_e state = p->lex.state;
8802 if (IS_lex_state_for(state, EXPR_FNAME)) {
8803 SET_LEX_STATE(EXPR_ENDFN);
8804 set_yylval_name(rb_intern2(tok(p), toklen(p)));
8807 SET_LEX_STATE(kw->state);
8808 if (IS_lex_state(EXPR_BEG)) {
8809 p->command_start = TRUE;
8811 if (kw->id[0] == keyword_do) {
8812 if (lambda_beginning_p()) {
8813 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
8814 return keyword_do_LAMBDA;
8816 if (COND_P()) return keyword_do_cond;
8817 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
8818 return keyword_do_block;
8821 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED)))
8824 if (kw->id[0] != kw->id[1])
8825 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
8831 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
8833 SET_LEX_STATE(EXPR_CMDARG);
8836 SET_LEX_STATE(EXPR_ARG);
8839 else if (p->lex.state == EXPR_FNAME) {
8840 SET_LEX_STATE(EXPR_ENDFN);
8843 SET_LEX_STATE(EXPR_END);
8846 ident = tokenize_ident(p, last_state);
8847 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
8848 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
8849 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
8850 lvar_defined(p, ident)) {
8851 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
8856static enum yytokentype
8857parser_yylex(struct parser_params *p)
8863 enum lex_state_e last_state;
8864 int fallthru = FALSE;
8865 int token_seen = p->token_seen;
8867 if (p->lex.strterm) {
8868 if (p->lex.strterm->flags & STRTERM_HEREDOC) {
8869 return here_document(p, &p->lex.strterm->u.heredoc);
8873 return parse_string(p, &p->lex.strterm->u.literal);
8876 cmd_state = p->command_start;
8877 p->command_start = FALSE;
8878 p->token_seen = TRUE;
8880 last_state = p->lex.state;
8884 switch (c = nextc(p)) {
8885 case '\0': /* NUL */
8886 case '\004': /* ^D */
8887 case '\032': /* ^Z */
8888 case -1: /* end of script. */
8892 case ' ': case '\t': case '\f': case '\r':
8893 case '\13': /* '\v' */
8896 while ((c = nextc(p))) {
8898 case ' ': case '\t': case '\f': case '\r':
8899 case '\13': /* '\v' */
8907 dispatch_scan_event(p, tSP);
8911 case '#': /* it's a comment */
8912 p->token_seen = token_seen;
8913 /* no magic_comment in shebang line */
8914 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
8915 if (comment_at_top(p)) {
8916 set_file_encoding(p, p->lex.pcur, p->lex.pend);
8920 dispatch_scan_event(p, tCOMMENT);
8924 p->token_seen = token_seen;
8925 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
8926 !IS_lex_state(EXPR_LABELED));
8927 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
8929 dispatch_scan_event(p, tIGNORED_NL);
8932 if (!c && p->in_kwarg) {
8933 goto normal_newline;
8938 switch (c = nextc(p)) {
8939 case ' ': case '\t': case '\f': case '\r':
8940 case '\13': /* '\v' */
8945 if (space_seen) dispatch_scan_event(p, tSP);
8949 dispatch_delayed_token(p, tIGNORED_NL);
8950 if (peek(p, '.') == (c == '&')) {
8952 dispatch_scan_event(p, tSP);
8957 p->ruby_sourceline--;
8958 p->lex.nextline = p->lex.lastline;
8959 case -1: /* EOF no decrement*/
8961 if (p->lex.prevline && !p->eofp) p->lex.lastline = p->lex.prevline;
8962 p->lex.pbeg = RSTRING_PTR(p->lex.lastline);
8963 p->lex.pend = p->lex.pcur = p->lex.pbeg + RSTRING_LEN(p->lex.lastline);
8964 pushback(p, 1); /* always pushback */
8965 p->lex.ptok = p->lex.pcur;
8969 p->lex.ptok = p->lex.pcur;
8972 goto normal_newline;
8976 p->command_start = TRUE;
8977 SET_LEX_STATE(EXPR_BEG);
8981 if ((c = nextc(p)) == '*') {
8982 if ((c = nextc(p)) == '=') {
8983 set_yylval_id(idPow);
8984 SET_LEX_STATE(EXPR_BEG);
8989 rb_warning0("`**' interpreted as argument prefix");
8992 else if (IS_BEG()) {
8996 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
9002 SET_LEX_STATE(EXPR_BEG);
9007 rb_warning0("`*' interpreted as argument prefix");
9010 else if (IS_BEG()) {
9014 c = warn_balanced('*', "*", "argument prefix");
9017 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9022 if (IS_AFTER_OPERATOR()) {
9023 SET_LEX_STATE(EXPR_ARG);
9029 SET_LEX_STATE(EXPR_BEG);
9042 /* skip embedded rd document */
9043 if (word_match_p(p, "begin", 5)) {
9047 dispatch_scan_event(p, tEMBDOC_BEG);
9051 dispatch_scan_event(p, tEMBDOC);
9056 compile_error(p, "embedded document meets end of file");
9059 if (c == '=' && word_match_p(p, "end", 3)) {
9065 dispatch_scan_event(p, tEMBDOC_END);
9070 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9071 if ((c = nextc(p)) == '=') {
9072 if ((c = nextc(p)) == '=') {
9081 else if (c == '>') {
9090 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
9092 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
9093 int token = heredoc_identifier(p);
9094 if (token) return token < 0 ? 0 : token;
9096 if (IS_AFTER_OPERATOR()) {
9097 SET_LEX_STATE(EXPR_ARG);
9100 if (IS_lex_state(EXPR_CLASS))
9101 p->command_start = TRUE;
9102 SET_LEX_STATE(EXPR_BEG);
9105 if ((c = nextc(p)) == '>') {
9112 if ((c = nextc(p)) == '=') {
9113 set_yylval_id(idLTLT);
9114 SET_LEX_STATE(EXPR_BEG);
9118 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
9124 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9125 if ((c = nextc(p)) == '=') {
9129 if ((c = nextc(p)) == '=') {
9130 set_yylval_id(idGTGT);
9131 SET_LEX_STATE(EXPR_BEG);
9141 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9142 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
9143 p->lex.ptok = p->lex.pcur-1;
9147 if (IS_lex_state(EXPR_FNAME)) {
9148 SET_LEX_STATE(EXPR_ENDFN);
9151 if (IS_lex_state(EXPR_DOT)) {
9153 SET_LEX_STATE(EXPR_CMDARG);
9155 SET_LEX_STATE(EXPR_ARG);
9158 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
9159 return tXSTRING_BEG;
9162 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
9163 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
9164 p->lex.ptok = p->lex.pcur-1;
9168 return parse_qmark(p, space_seen);
9171 if ((c = nextc(p)) == '&') {
9172 SET_LEX_STATE(EXPR_BEG);
9173 if ((c = nextc(p)) == '=') {
9174 set_yylval_id(idANDOP);
9175 SET_LEX_STATE(EXPR_BEG);
9181 else if (c == '=') {
9183 SET_LEX_STATE(EXPR_BEG);
9186 else if (c == '.') {
9187 set_yylval_id(idANDDOT);
9188 SET_LEX_STATE(EXPR_DOT);
9194 (c = peekc_n(p, 1)) == -1 ||
9195 !(c == '\'' || c == '"' ||
9196 is_identchar((p->lex.pcur+1), p->lex.pend, p->enc))) {
9197 rb_warning0("`&' interpreted as argument prefix");
9201 else if (IS_BEG()) {
9205 c = warn_balanced('&', "&", "argument prefix");
9207 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9211 if ((c = nextc(p)) == '|') {
9212 SET_LEX_STATE(EXPR_BEG);
9213 if ((c = nextc(p)) == '=') {
9214 set_yylval_id(idOROP);
9215 SET_LEX_STATE(EXPR_BEG);
9219 if (IS_lex_state_for(last_state, EXPR_BEG)) {
9228 SET_LEX_STATE(EXPR_BEG);
9231 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
9237 if (IS_AFTER_OPERATOR()) {
9238 SET_LEX_STATE(EXPR_ARG);
9247 SET_LEX_STATE(EXPR_BEG);
9250 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
9251 SET_LEX_STATE(EXPR_BEG);
9253 if (c != -1 && ISDIGIT(c)) {
9254 return parse_numeric(p, '+');
9258 SET_LEX_STATE(EXPR_BEG);
9260 return warn_balanced('+', "+", "unary operator");
9264 if (IS_AFTER_OPERATOR()) {
9265 SET_LEX_STATE(EXPR_ARG);
9274 SET_LEX_STATE(EXPR_BEG);
9278 SET_LEX_STATE(EXPR_ENDFN);
9281 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
9282 SET_LEX_STATE(EXPR_BEG);
9284 if (c != -1 && ISDIGIT(c)) {
9289 SET_LEX_STATE(EXPR_BEG);
9291 return warn_balanced('-', "-", "unary operator");
9294 int is_beg = IS_BEG();
9295 SET_LEX_STATE(EXPR_BEG);
9296 if ((c = nextc(p)) == '.') {
9297 if ((c = nextc(p)) == '.') {
9298 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
9299 rb_warn0("... at EOL, should be parenthesized?");
9301 return is_beg ? tBDOT3 : tDOT3;
9304 return is_beg ? tBDOT2 : tDOT2;
9307 if (c != -1 && ISDIGIT(c)) {
9308 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
9309 parse_numeric(p, '.');
9310 if (ISDIGIT(prev)) {
9311 yyerror0("unexpected fraction part after numeric literal");
9314 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
9316 SET_LEX_STATE(EXPR_END);
9317 p->lex.ptok = p->lex.pcur;
9321 SET_LEX_STATE(EXPR_DOT);
9325 case '0': case '1': case '2': case '3': case '4':
9326 case '5': case '6': case '7': case '8': case '9':
9327 return parse_numeric(p, c);
9332 SET_LEX_STATE(EXPR_ENDFN);
9333 p->lex.paren_nest--;
9339 SET_LEX_STATE(EXPR_END);
9340 p->lex.paren_nest--;
9344 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
9345 if (!p->lex.brace_nest--) return tSTRING_DEND;
9348 SET_LEX_STATE(EXPR_END);
9349 p->lex.paren_nest--;
9355 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
9356 SET_LEX_STATE(EXPR_BEG);
9359 set_yylval_id(idCOLON2);
9360 SET_LEX_STATE(EXPR_DOT);
9363 if (IS_END() || ISSPACE(c) || c == '#') {
9365 c = warn_balanced(':', ":", "symbol literal");
9366 SET_LEX_STATE(EXPR_BEG);
9371 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
9374 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
9380 SET_LEX_STATE(EXPR_FNAME);
9385 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9388 if ((c = nextc(p)) == '=') {
9390 SET_LEX_STATE(EXPR_BEG);
9395 arg_ambiguous(p, '/');
9396 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
9399 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9400 return warn_balanced('/', "/", "regexp literal");
9403 if ((c = nextc(p)) == '=') {
9405 SET_LEX_STATE(EXPR_BEG);
9408 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
9413 SET_LEX_STATE(EXPR_BEG);
9414 p->command_start = TRUE;
9418 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9422 if (IS_AFTER_OPERATOR()) {
9423 if ((c = nextc(p)) != '@') {
9426 SET_LEX_STATE(EXPR_ARG);
9429 SET_LEX_STATE(EXPR_BEG);
9437 else if (!space_seen) {
9438 /* foo( ... ) => method call, no ambiguity */
9440 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
9443 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
9444 rb_warning0("parentheses after method name is interpreted as "
9445 "an argument list, not a decomposed argument");
9447 p->lex.paren_nest++;
9450 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9454 p->lex.paren_nest++;
9455 if (IS_AFTER_OPERATOR()) {
9456 if ((c = nextc(p)) == ']') {
9457 SET_LEX_STATE(EXPR_ARG);
9458 if ((c = nextc(p)) == '=') {
9465 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
9468 else if (IS_BEG()) {
9471 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
9474 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9480 ++p->lex.brace_nest;
9481 if (lambda_beginning_p())
9483 else if (IS_lex_state(EXPR_LABELED))
9484 c = tLBRACE; /* hash */
9485 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
9486 c = '{'; /* block (primary) */
9487 else if (IS_lex_state(EXPR_ENDARG))
9488 c = tLBRACE_ARG; /* block (expr) */
9490 c = tLBRACE; /* hash */
9492 p->command_start = TRUE;
9493 SET_LEX_STATE(EXPR_BEG);
9496 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
9498 ++p->lex.paren_nest; /* after lambda_beginning_p() */
9507 dispatch_scan_event(p, tSP);
9508 goto retry; /* skip \\n */
9510 if (c == ' ') return tSP;
9511 if (ISSPACE(c)) return c;
9516 return parse_percent(p, space_seen, last_state);
9519 return parse_gvar(p, last_state);
9522 return parse_atmark(p, last_state);
9525 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
9526 p->ruby__end__seen = 1;
9532 dispatch_scan_event(p, k__END__);
9540 if (!parser_is_identchar(p)) {
9541 compile_error(p, "Invalid char `\\x%02X' in expression", c);
9550 return parse_ident(p, c, cmd_state);
9553static enum yytokentype
9554yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
9560 t = parser_yylex(p);
9562 if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC))
9563 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc);
9565 RUBY_SET_YYLLOC(*yylloc);
9567 if (has_delayed_token(p))
9568 dispatch_delayed_token(p, t);
9570 dispatch_scan_event(p, t);
9575#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
9578node_newnode(struct parser_params *p, enum node_type type, VALUE a0, VALUE a1, VALUE a2, const rb_code_location_t *loc)
9580 NODE *n = rb_ast_newnode(p->ast, type);
9582 rb_node_init(n, type, a0, a1, a2);
9585 nd_set_node_id(n, parser_get_node_id(p));
9590nd_set_loc(NODE *nd, const YYLTYPE *loc)
9593 nd_set_line(nd, loc->beg_pos.lineno);
9598static enum node_type
9599nodetype(NODE *node) /* for debug */
9601 return (enum node_type)nd_type(node);
9607 return nd_line(node);
9611newline_node(NODE *node)
9614 node = remove_begin(node);
9615 node->flags |= NODE_FL_NEWLINE;
9621fixpos(NODE *node, NODE *orig)
9625 nd_set_line(node, nd_line(orig));
9629parser_warning(struct parser_params *p, NODE *node, const char *mesg)
9631 rb_compile_warning(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9635parser_warn(struct parser_params *p, NODE *node, const char *mesg)
9637 rb_compile_warn(p->ruby_sourcefile, nd_line(node), "%s", mesg);
9641block_append(struct parser_params *p, NODE *head, NODE *tail)
9643 NODE *end, *h = head, *nd;
9645 if (tail == 0) return head;
9647 if (h == 0) return tail;
9648 switch (nd_type(h)) {
9655 parser_warning(p, h, "unused literal ignored");
9658 h = end = NEW_BLOCK(head, &head->nd_loc);
9668 switch (nd_type(nd)) {
9674 if (RTEST(ruby_verbose)) {
9675 parser_warning(p, tail, "statement not reached");
9683 if (nd_type(tail) != NODE_BLOCK) {
9684 tail = NEW_BLOCK(tail, &tail->nd_loc);
9685 tail->nd_end = tail;
9687 end->nd_next = tail;
9688 h->nd_end = tail->nd_end;
9689 nd_set_last_loc(head, nd_last_loc(tail));
9693/* append item to the list */
9695list_append(struct parser_params *p, NODE *list, NODE *item)
9699 if (list == 0) return NEW_LIST(item, &item->nd_loc);
9700 if (list->nd_next) {
9701 last = list->nd_next->nd_end;
9708 last->nd_next = NEW_LIST(item, &item->nd_loc);
9709 list->nd_next->nd_end = last->nd_next;
9711 nd_set_last_loc(list, nd_last_loc(item));
9716/* concat two lists */
9718list_concat(NODE *head, NODE *tail)
9722 if (head->nd_next) {
9723 last = head->nd_next->nd_end;
9729 head->nd_alen += tail->nd_alen;
9730 last->nd_next = tail;
9731 if (tail->nd_next) {
9732 head->nd_next->nd_end = tail->nd_next->nd_end;
9735 head->nd_next->nd_end = tail;
9738 nd_set_last_loc(head, nd_last_loc(tail));
9744literal_concat0(struct parser_params *p, VALUE head, VALUE tail)
9746 if (NIL_P(tail)) return 1;
9747 if (!rb_enc_compatible(head, tail)) {
9748 compile_error(p, "string literal encodings differ (%s / %s)",
9749 rb_enc_name(rb_enc_get(head)),
9750 rb_enc_name(rb_enc_get(tail)));
9751 rb_str_resize(head, 0);
9752 rb_str_resize(tail, 0);
9755 rb_str_buf_append(head, tail);
9759/* concat two string literals */
9761literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
9763 enum node_type htype;
9767 if (!head) return tail;
9768 if (!tail) return head;
9770 htype = nd_type(head);
9771 if (htype == NODE_EVSTR) {
9772 NODE *node = NEW_DSTR(STR_NEW0(), loc);
9773 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
9774 head = list_append(p, node, head);
9777 if (p->heredoc_indent > 0) {
9780 nd_set_type(head, NODE_DSTR);
9782 return list_append(p, head, tail);
9787 switch (nd_type(tail)) {
9789 if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9790 nd_type(headlast) == NODE_STR) {
9792 lit = headlast->nd_lit;
9797 if (htype == NODE_STR) {
9798 if (!literal_concat0(p, lit, tail->nd_lit)) {
9800 rb_discard_node(p, head);
9801 rb_discard_node(p, tail);
9804 rb_discard_node(p, tail);
9807 list_append(p, head, tail);
9812 if (htype == NODE_STR) {
9813 if (!literal_concat0(p, head->nd_lit, tail->nd_lit))
9815 tail->nd_lit = head->nd_lit;
9816 rb_discard_node(p, head);
9819 else if (NIL_P(tail->nd_lit)) {
9821 head->nd_alen += tail->nd_alen - 1;
9822 head->nd_next->nd_end->nd_next = tail->nd_next;
9823 head->nd_next->nd_end = tail->nd_next->nd_end;
9824 rb_discard_node(p, tail);
9826 else if (htype == NODE_DSTR && (headlast = head->nd_next->nd_end->nd_head) &&
9827 nd_type(headlast) == NODE_STR) {
9828 lit = headlast->nd_lit;
9829 if (!literal_concat0(p, lit, tail->nd_lit))
9831 tail->nd_lit = Qnil;
9835 list_concat(head, NEW_NODE(NODE_LIST, NEW_STR(tail->nd_lit, loc), tail->nd_alen, tail->nd_next, loc));
9840 if (htype == NODE_STR) {
9841 nd_set_type(head, NODE_DSTR);
9844 list_append(p, head, tail);
9851evstr2dstr(struct parser_params *p, NODE *node)
9853 if (nd_type(node) == NODE_EVSTR) {
9854 NODE * dstr = NEW_DSTR(STR_NEW0(), &node->nd_loc);
9855 RB_OBJ_WRITTEN(p->ast, Qnil, dstr->nd_lit);
9856 node = list_append(p, dstr, node);
9862new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
9867 switch (nd_type(node)) {
9868 case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
9872 return NEW_EVSTR(head, loc);
9876call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
9877 const YYLTYPE *op_loc, const YYLTYPE *loc)
9882 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
9883 nd_set_line(expr, op_loc->beg_pos.lineno);
9888call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
9892 opcall = NEW_OPCALL(recv, id, 0, loc);
9893 nd_set_line(opcall, op_loc->beg_pos.lineno);
9898new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
9900 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
9901 nd_set_line(qcall, op_loc->beg_pos.lineno);
9906new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
9909 if (block) block_dup_check(p, args, block);
9910 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
9911 if (block) ret = method_add_block(p, ret, block, loc);
9916#define nd_once_body(node) (nd_type(node) == NODE_ONCE ? (node)->nd_body : node)
9918match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
9921 int line = op_loc->beg_pos.lineno;
9925 if (node1 && (n = nd_once_body(node1)) != 0) {
9926 switch (nd_type(n)) {
9929 NODE *match = NEW_MATCH2(node1, node2, loc);
9930 nd_set_line(match, line);
9935 if (RB_TYPE_P(n->nd_lit, T_REGEXP)) {
9936 const VALUE lit = n->nd_lit;
9937 NODE *match = NEW_MATCH2(node1, node2, loc);
9938 match->nd_args = reg_named_capture_assign(p, lit, loc);
9939 nd_set_line(match, line);
9945 if (node2 && (n = nd_once_body(node2)) != 0) {
9948 switch (nd_type(n)) {
9950 if (!RB_TYPE_P(n->nd_lit, T_REGEXP)) break;
9953 match3 = NEW_MATCH3(node2, node1, loc);
9958 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
9959 nd_set_line(n, line);
9965past_dvar_p(struct parser_params *p, ID id)
9967 struct vtable *past = p->lvtbl->past;
9969 if (vtable_included(past, id)) return 1;
9976/* As Ripper#warn does not have arguments for the location, so the
9977 * following messages cannot be separated */
9978#define WARN_LOCATION(type) do { \
9979 if (p->warn_location) { \
9981 VALUE file = rb_source_location(&line); \
9982 rb_warn3(type" in eval may not return location in binding;" \
9983 " use Binding#source_location instead\n" \
9984 "%"PRIsWARN":%d: warning: in `%"PRIsWARN"'", \
9985 file, WARN_I(line), rb_id2str(rb_frame_this_func())); \
9990numparam_nested_p(struct parser_params *p)
9992 struct local_vars *local = p->lvtbl;
9993 NODE *outer = local->numparam.outer;
9994 NODE *inner = local->numparam.inner;
9995 if (outer || inner) {
9996 NODE *used = outer ? outer : inner;
9997 compile_error(p, "numbered parameter is already used in\n"
9998 "%s:%d: %s block here",
9999 p->ruby_sourcefile, nd_line(used),
10000 outer ? "outer" : "inner");
10001 parser_show_error_line(p, &used->nd_loc);
10008gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
10014 return NEW_SELF(loc);
10016 return NEW_NIL(loc);
10018 return NEW_TRUE(loc);
10019 case keyword_false:
10020 return NEW_FALSE(loc);
10021 case keyword__FILE__:
10022 WARN_LOCATION("__FILE__");
10024 VALUE file = p->ruby_sourcefile_string;
10026 file = rb_str_new(0, 0);
10028 file = rb_str_dup(file);
10029 node = NEW_STR(file, loc);
10030 RB_OBJ_WRITTEN(p->ast, Qnil, file);
10033 case keyword__LINE__:
10034 WARN_LOCATION("__LINE__");
10035 return NEW_LIT(INT2FIX(p->tokline), loc);
10036 case keyword__ENCODING__:
10037 node = NEW_LIT(rb_enc_from_encoding(p->enc), loc);
10038 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10042 switch (id_type(id)) {
10044 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
10045 if (NUMPARAM_ID_P(id) && numparam_nested_p(p)) return 0;
10046 if (id == p->cur_arg) {
10047 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10050 if (vidp) *vidp |= LVAR_USED;
10051 node = NEW_DVAR(id, loc);
10054 if (local_id_ref(p, id, &vidp)) {
10055 if (id == p->cur_arg) {
10056 compile_error(p, "circular argument reference - %"PRIsWARN, rb_id2str(id));
10059 if (vidp) *vidp |= LVAR_USED;
10060 node = NEW_LVAR(id, loc);
10063 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
10064 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
10065 if (numparam_nested_p(p)) return 0;
10066 node = NEW_DVAR(id, loc);
10067 struct local_vars *local = p->lvtbl;
10068 if (!local->numparam.current) local->numparam.current = node;
10071# if WARN_PAST_SCOPE
10072 if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
10073 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
10076 /* method call without arguments */
10077 return NEW_VCALL(id, loc);
10079 return NEW_GVAR(id, loc);
10081 return NEW_IVAR(id, loc);
10083 return NEW_CONST(id, loc);
10085 return NEW_CVAR(id, loc);
10087 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10092opt_arg_append(NODE *opt_list, NODE *opt)
10094 NODE *opts = opt_list;
10095 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10097 while (opts->nd_next) {
10098 opts = opts->nd_next;
10099 opts->nd_loc.end_pos = opt->nd_loc.end_pos;
10101 opts->nd_next = opt;
10107kwd_append(NODE *kwlist, NODE *kw)
10110 NODE *kws = kwlist;
10111 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10112 while (kws->nd_next) {
10113 kws = kws->nd_next;
10114 kws->nd_loc.end_pos = kw->nd_loc.end_pos;
10122new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
10124 return NEW_DEFINED(remove_begin_all(expr), loc);
10128symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
10130 if (nd_type(symbol) == NODE_DSTR) {
10131 nd_set_type(symbol, NODE_DSYM);
10134 nd_set_type(symbol, NODE_LIT);
10135 RB_OBJ_WRITTEN(p->ast, Qnil, symbol->nd_lit = rb_str_intern(symbol->nd_lit));
10137 return list_append(p, symbols, symbol);
10141new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
10147 node = NEW_LIT(reg_compile(p, STR_NEW0(), options), loc);
10148 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit);
10151 switch (nd_type(node)) {
10154 VALUE src = node->nd_lit;
10155 nd_set_type(node, NODE_LIT);
10156 nd_set_loc(node, loc);
10157 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10162 node = NEW_NODE(NODE_DSTR, lit, 1, NEW_LIST(node, loc), loc);
10163 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10166 nd_set_type(node, NODE_DREGX);
10167 nd_set_loc(node, loc);
10168 node->nd_cflag = options & RE_OPTION_MASK;
10169 if (!NIL_P(node->nd_lit)) reg_fragment_check(p, node->nd_lit, options);
10170 for (list = (prev = node)->nd_next; list; list = list->nd_next) {
10171 if (nd_type(list->nd_head) == NODE_STR) {
10172 VALUE tail = list->nd_head->nd_lit;
10173 if (reg_fragment_check(p, tail, options) && prev && !NIL_P(prev->nd_lit)) {
10174 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
10175 if (!literal_concat0(p, lit, tail)) {
10176 return NEW_NIL(loc); /* dummy node on error */
10178 rb_str_resize(tail, 0);
10179 prev->nd_next = list->nd_next;
10180 rb_discard_node(p, list->nd_head);
10181 rb_discard_node(p, list);
10192 if (!node->nd_next) {
10193 VALUE src = node->nd_lit;
10194 nd_set_type(node, NODE_LIT);
10195 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = reg_compile(p, src, options));
10197 if (options & RE_OPTION_ONCE) {
10198 node = NEW_NODE(NODE_ONCE, 0, node, 0, loc);
10206new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
10209 return NEW_KW_ARG(0, (k), loc);
10213new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
10216 VALUE lit = STR_NEW0();
10217 NODE *xstr = NEW_XSTR(lit, loc);
10218 RB_OBJ_WRITTEN(p->ast, Qnil, lit);
10221 switch (nd_type(node)) {
10223 nd_set_type(node, NODE_XSTR);
10224 nd_set_loc(node, loc);
10227 nd_set_type(node, NODE_DXSTR);
10228 nd_set_loc(node, loc);
10231 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node, loc), loc);
10238check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
10242 if (!arg || !p->case_labels) return;
10244 lit = rb_node_case_when_optimizable_literal(arg);
10245 if (lit == Qundef) return;
10246 if (nd_type(arg) == NODE_STR) {
10247 RB_OBJ_WRITTEN(p->ast, Qnil, arg->nd_lit = lit);
10250 if (NIL_P(p->case_labels)) {
10251 p->case_labels = rb_obj_hide(rb_hash_new());
10254 VALUE line = rb_hash_lookup(p->case_labels, lit);
10255 if (!NIL_P(line)) {
10256 rb_warning1("duplicated `when' clause with line %d is ignored",
10261 rb_hash_aset(p->case_labels, lit, INT2NUM(p->ruby_sourceline));
10266id_is_var(struct parser_params *p, ID id)
10268 if (is_notop_id(id)) {
10269 switch (id & ID_SCOPE_MASK) {
10270 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
10273 if (dyna_in_block(p)) {
10274 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
10276 if (local_id(p, id)) return 1;
10277 /* method call without arguments */
10281 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
10286new_regexp(struct parser_params *p, VALUE re, VALUE opt, const YYLTYPE *loc)
10288 VALUE src = 0, err;
10290 if (ripper_is_node_yylval(re)) {
10291 src = RNODE(re)->nd_cval;
10292 re = RNODE(re)->nd_rval;
10294 if (ripper_is_node_yylval(opt)) {
10295 options = (int)RNODE(opt)->nd_tag;
10296 opt = RNODE(opt)->nd_rval;
10298 if (src && NIL_P(parser_reg_compile(p, src, options, &err))) {
10299 compile_error(p, "%"PRIsVALUE, err);
10301 return dispatch2(regexp_literal, re, opt);
10303#endif /* !RIPPER */
10307static const char rb_parser_lex_state_names[][8] = {
10308 "BEG", "END", "ENDARG", "ENDFN", "ARG",
10309 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
10310 "LABEL", "LABELED","FITEM",
10314append_lex_state_name(enum lex_state_e state, VALUE buf)
10317 unsigned int mask = 1;
10318 static const char none[] = "NONE";
10320 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
10321 if ((unsigned)state & mask) {
10323 rb_str_cat(buf, "|", 1);
10326 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
10330 rb_str_cat(buf, none, sizeof(none)-1);
10336flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
10338 VALUE mesg = p->debug_buffer;
10340 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
10341 p->debug_buffer = Qnil;
10342 rb_io_puts(1, &mesg, out);
10344 if (!NIL_P(str) && RSTRING_LEN(str)) {
10345 rb_io_write(p->debug_output, str);
10350rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
10351 enum lex_state_e to, int line)
10354 mesg = rb_str_new_cstr("lex_state: ");
10355 append_lex_state_name(from, mesg);
10356 rb_str_cat_cstr(mesg, " -> ");
10357 append_lex_state_name(to, mesg);
10358 rb_str_catf(mesg, " at line %d\n", line);
10359 flush_debug_buffer(p, p->debug_output, mesg);
10364rb_parser_lex_state_name(enum lex_state_e state)
10366 return rb_fstring(append_lex_state_name(state, rb_str_new(0, 0)));
10370append_bitstack_value(stack_type stack, VALUE mesg)
10373 rb_str_cat_cstr(mesg, "0");
10376 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
10377 for (; mask && !(stack & mask); mask >>= 1) continue;
10378 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
10383rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
10384 const char *name, int line)
10386 VALUE mesg = rb_sprintf("%s: ", name);
10387 append_bitstack_value(stack, mesg);
10388 rb_str_catf(mesg, " at line %d\n", line);
10389 flush_debug_buffer(p, p->debug_output, mesg);
10393rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
10396 VALUE mesg = rb_str_new_cstr("internal parser error: ");
10399 rb_str_vcatf(mesg, fmt, ap);
10401 parser_yyerror(p, NULL, RSTRING_PTR(mesg));
10404 mesg = rb_str_new(0, 0);
10405 append_lex_state_name(p->lex.state, mesg);
10406 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
10407 rb_str_resize(mesg, 0);
10408 append_bitstack_value(p->cond_stack, mesg);
10409 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
10410 rb_str_resize(mesg, 0);
10411 append_bitstack_value(p->cmdarg_stack, mesg);
10412 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
10413 if (p->debug_output == rb_stdout)
10414 p->debug_output = rb_stderr;
10419rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
10421 int sourceline = here->sourceline;
10422 int beg_pos = (int)here->offset - here->quote
10423 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
10424 int end_pos = (int)here->offset + here->length + here->quote;
10426 yylloc->beg_pos.lineno = sourceline;
10427 yylloc->beg_pos.column = beg_pos;
10428 yylloc->end_pos.lineno = sourceline;
10429 yylloc->end_pos.column = end_pos;
10434rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
10436 yylloc->beg_pos.lineno = p->ruby_sourceline;
10437 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10438 yylloc->end_pos.lineno = p->ruby_sourceline;
10439 yylloc->end_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10444rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
10446 yylloc->beg_pos.lineno = p->ruby_sourceline;
10447 yylloc->beg_pos.column = (int)(p->lex.ptok - p->lex.pbeg);
10448 yylloc->end_pos.lineno = p->ruby_sourceline;
10449 yylloc->end_pos.column = (int)(p->lex.pcur - p->lex.pbeg);
10452#endif /* !RIPPER */
10455assignable0(struct parser_params *p, ID id, const char **err)
10457 if (!id) return -1;
10460 *err = "Can't change the value of self";
10463 *err = "Can't assign to nil";
10466 *err = "Can't assign to true";
10468 case keyword_false:
10469 *err = "Can't assign to false";
10471 case keyword__FILE__:
10472 *err = "Can't assign to __FILE__";
10474 case keyword__LINE__:
10475 *err = "Can't assign to __LINE__";
10477 case keyword__ENCODING__:
10478 *err = "Can't assign to __ENCODING__";
10481 switch (id_type(id)) {
10483 if (dyna_in_block(p)) {
10484 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
10485 compile_error(p, "Can't assign to numbered parameter _%d",
10486 NUMPARAM_ID_TO_IDX(id));
10489 if (dvar_curr(p, id)) return NODE_DASGN_CURR;
10490 if (dvar_defined(p, id)) return NODE_DASGN;
10491 if (local_id(p, id)) return NODE_LASGN;
10493 return NODE_DASGN_CURR;
10496 if (!local_id(p, id)) local_var(p, id);
10500 case ID_GLOBAL: return NODE_GASGN;
10501 case ID_INSTANCE: return NODE_IASGN;
10503 if (!p->in_def) return NODE_CDECL;
10504 *err = "dynamic constant assignment";
10506 case ID_CLASS: return NODE_CVASGN;
10508 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
10515assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
10517 const char *err = 0;
10518 int node_type = assignable0(p, id, &err);
10519 switch (node_type) {
10520 case NODE_DASGN_CURR: return NEW_DASGN_CURR(id, val, loc);
10521 case NODE_DASGN: return NEW_DASGN(id, val, loc);
10522 case NODE_LASGN: return NEW_LASGN(id, val, loc);
10523 case NODE_GASGN: return NEW_GASGN(id, val, loc);
10524 case NODE_IASGN: return NEW_IASGN(id, val, loc);
10525 case NODE_CDECL: return NEW_CDECL(id, val, 0, loc);
10526 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
10528 if (err) yyerror1(loc, err);
10529 return NEW_BEGIN(0, loc);
10533assignable(struct parser_params *p, VALUE lhs)
10535 const char *err = 0;
10536 assignable0(p, get_id(lhs), &err);
10537 if (err) lhs = assign_error(p, lhs);
10543is_private_local_id(ID name)
10546 if (name == idUScore) return 1;
10547 if (!is_local_id(name)) return 0;
10548 s = rb_id2str(name);
10550 return RSTRING_PTR(s)[0] == '_';
10554shadowing_lvar_0(struct parser_params *p, ID name)
10556 if (is_private_local_id(name)) return 1;
10557 if (dyna_in_block(p)) {
10558 if (dvar_curr(p, name)) {
10559 yyerror0("duplicated argument name");
10561 else if (dvar_defined(p, name) || local_id(p, name)) {
10562 vtable_add(p->lvtbl->vars, name);
10563 if (p->lvtbl->used) {
10564 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
10570 if (local_id(p, name)) {
10571 yyerror0("duplicated argument name");
10578shadowing_lvar(struct parser_params *p, ID name)
10580 shadowing_lvar_0(p, name);
10585new_bv(struct parser_params *p, ID name)
10588 if (!is_local_id(name)) {
10589 compile_error(p, "invalid local variable - %"PRIsVALUE,
10593 if (!shadowing_lvar_0(p, name)) return;
10599aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
10601 return NEW_ATTRASGN(recv, tASET, idx, loc);
10605block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
10607 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) {
10608 compile_error(p, "both block arg and actual block given");
10613attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
10615 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
10616 return NEW_ATTRASGN(recv, id, 0, loc);
10620rb_backref_error(struct parser_params *p, NODE *node)
10622 switch (nd_type(node)) {
10624 compile_error(p, "Can't set variable $%ld", node->nd_nth);
10626 case NODE_BACK_REF:
10627 compile_error(p, "Can't set variable $%c", (int)node->nd_nth);
10633arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10635 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
10636 switch (nd_type(node1)) {
10638 return list_append(p, node1, node2);
10639 case NODE_BLOCK_PASS:
10640 node1->nd_head = arg_append(p, node1->nd_head, node2, loc);
10641 node1->nd_loc.end_pos = node1->nd_head->nd_loc.end_pos;
10643 case NODE_ARGSPUSH:
10644 node1->nd_body = list_append(p, NEW_LIST(node1->nd_body, &node1->nd_body->nd_loc), node2);
10645 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10646 nd_set_type(node1, NODE_ARGSCAT);
10649 if (nd_type(node1->nd_body) != NODE_LIST) break;
10650 node1->nd_body = list_append(p, node1->nd_body, node2);
10651 node1->nd_loc.end_pos = node1->nd_body->nd_loc.end_pos;
10654 return NEW_ARGSPUSH(node1, node2, loc);
10658arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
10660 if (!node2) return node1;
10661 switch (nd_type(node1)) {
10662 case NODE_BLOCK_PASS:
10663 if (node1->nd_head)
10664 node1->nd_head = arg_concat(p, node1->nd_head, node2, loc);
10666 node1->nd_head = NEW_LIST(node2, loc);
10668 case NODE_ARGSPUSH:
10669 if (nd_type(node2) != NODE_LIST) break;
10670 node1->nd_body = list_concat(NEW_LIST(node1->nd_body, loc), node2);
10671 nd_set_type(node1, NODE_ARGSCAT);
10674 if (nd_type(node2) != NODE_LIST ||
10675 nd_type(node1->nd_body) != NODE_LIST) break;
10676 node1->nd_body = list_concat(node1->nd_body, node2);
10679 return NEW_ARGSCAT(node1, node2, loc);
10683last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
10686 if ((n1 = splat_array(args)) != 0) {
10687 return list_append(p, n1, last_arg);
10689 return arg_append(p, args, last_arg, loc);
10693rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
10696 if ((nd_type(rest_arg) == NODE_LIST) && (n1 = splat_array(args)) != 0) {
10697 return list_concat(n1, rest_arg);
10699 return arg_concat(p, args, rest_arg, loc);
10703splat_array(NODE* node)
10705 if (nd_type(node) == NODE_SPLAT) node = node->nd_head;
10706 if (nd_type(node) == NODE_LIST) return node;
10711mark_lvar_used(struct parser_params *p, NODE *rhs)
10715 switch (nd_type(rhs)) {
10717 if (local_id_ref(p, rhs->nd_vid, &vidp)) {
10718 if (vidp) *vidp |= LVAR_USED;
10722 case NODE_DASGN_CURR:
10723 if (dvar_defined_ref(p, rhs->nd_vid, &vidp)) {
10724 if (vidp) *vidp |= LVAR_USED;
10729 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
10730 mark_lvar_used(p, rhs->nd_head);
10738node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc)
10740 if (!lhs) return 0;
10742 switch (nd_type(lhs)) {
10747 case NODE_DASGN_CURR:
10751 lhs->nd_value = rhs;
10752 nd_set_loc(lhs, loc);
10755 case NODE_ATTRASGN:
10756 lhs->nd_args = arg_append(p, lhs->nd_args, rhs, loc);
10757 nd_set_loc(lhs, loc);
10761 /* should not happen */
10769value_expr_check(struct parser_params *p, NODE *node)
10771 NODE *void_node = 0, *vn;
10774 rb_warning0("empty expression");
10777 switch (nd_type(node)) {
10783 return void_node ? void_node : node;
10786 if (!node->nd_body || nd_type(node->nd_body) != NODE_IN) {
10787 compile_error(p, "unexpected node");
10790 if (node->nd_body->nd_body) {
10793 /* single line pattern matching */
10794 return void_node ? void_node : node;
10797 while (node->nd_next) {
10798 node = node->nd_next;
10800 node = node->nd_head;
10804 node = node->nd_body;
10809 if (!node->nd_body) {
10812 else if (!node->nd_else) {
10815 vn = value_expr_check(p, node->nd_body);
10816 if (!vn) return NULL;
10817 if (!void_node) void_node = vn;
10818 node = node->nd_else;
10823 node = node->nd_1st;
10828 case NODE_DASGN_CURR:
10830 mark_lvar_used(p, node);
10842value_expr_gen(struct parser_params *p, NODE *node)
10844 NODE *void_node = value_expr_check(p, node);
10846 yyerror1(&void_node->nd_loc, "void value expression");
10847 /* or "control never reach"? */
10853void_expr(struct parser_params *p, NODE *node)
10855 const char *useless = 0;
10857 if (!RTEST(ruby_verbose)) return;
10859 if (!node || !(node = nd_once_body(node))) return;
10860 switch (nd_type(node)) {
10862 switch (node->nd_mid) {
10881 useless = rb_id2name(node->nd_mid);
10892 case NODE_BACK_REF:
10893 useless = "a variable";
10896 useless = "a constant";
10902 useless = "a literal";
10927 useless = "defined?";
10932 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
10937void_stmts(struct parser_params *p, NODE *node)
10939 NODE *const n = node;
10940 if (!RTEST(ruby_verbose)) return n;
10941 if (!node) return n;
10942 if (nd_type(node) != NODE_BLOCK) return n;
10944 while (node->nd_next) {
10945 void_expr(p, node->nd_head);
10946 node = node->nd_next;
10952remove_begin(NODE *node)
10954 NODE **n = &node, *n1 = node;
10955 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) {
10956 *n = n1 = n1->nd_body;
10962remove_begin_all(NODE *node)
10964 NODE **n = &node, *n1 = node;
10965 while (n1 && nd_type(n1) == NODE_BEGIN) {
10966 *n = n1 = n1->nd_body;
10972reduce_nodes(struct parser_params *p, NODE **body)
10974 NODE *node = *body;
10977 *body = NEW_NIL(&NULL_LOC);
10980#define subnodes(n1, n2) \
10981 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \
10982 (!node->n2) ? (body = &node->n1, 1) : \
10983 (reduce_nodes(p, &node->n1), body = &node->n2, 1))
10986 int newline = (int)(node->flags & NODE_FL_NEWLINE);
10987 switch (nd_type(node)) {
10993 *body = node = node->nd_stts;
10994 if (newline && node) node->flags |= NODE_FL_NEWLINE;
10997 *body = node = node->nd_body;
10998 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11001 body = &node->nd_end->nd_head;
11005 if (subnodes(nd_body, nd_else)) break;
11008 body = &node->nd_body;
11011 if (!subnodes(nd_body, nd_next)) goto end;
11014 if (!subnodes(nd_head, nd_resq)) goto end;
11017 if (node->nd_else) {
11018 body = &node->nd_resq;
11021 if (!subnodes(nd_head, nd_resq)) goto end;
11027 if (newline && node) node->flags |= NODE_FL_NEWLINE;
11034is_static_content(NODE *node)
11036 if (!node) return 1;
11037 switch (nd_type(node)) {
11039 if (!(node = node->nd_head)) break;
11042 if (!is_static_content(node->nd_head)) return 0;
11043 } while ((node = node->nd_next) != 0);
11058assign_in_cond(struct parser_params *p, NODE *node)
11060 switch (nd_type(node)) {
11064 case NODE_DASGN_CURR:
11073 if (!node->nd_value) return 1;
11074 if (is_static_content(node->nd_value)) {
11075 /* reports always */
11076 parser_warn(p, node->nd_value, "found `= literal' in conditional, should be ==");
11087#define SWITCH_BY_COND_TYPE(t, w, arg) \
11089 case COND_IN_OP: break; \
11090 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
11091 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
11094static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*);
11097range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11099 enum node_type type;
11101 if (node == 0) return 0;
11103 type = nd_type(node);
11105 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
11106 if (!e_option_supplied(p)) parser_warn(p, node, "integer literal in flip-flop");
11107 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$."), loc), loc), loc);
11109 return cond0(p, node, COND_IN_FF, loc);
11113cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc)
11115 if (node == 0) return 0;
11116 if (!(node = nd_once_body(node))) return 0;
11117 assign_in_cond(p, node);
11119 switch (nd_type(node)) {
11123 SWITCH_BY_COND_TYPE(type, warn, "string ")
11127 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ")
11129 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
11133 node->nd_1st = cond0(p, node->nd_1st, COND_IN_COND, loc);
11134 node->nd_2nd = cond0(p, node->nd_2nd, COND_IN_COND, loc);
11139 node->nd_beg = range_op(p, node->nd_beg, loc);
11140 node->nd_end = range_op(p, node->nd_end, loc);
11141 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
11142 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
11146 SWITCH_BY_COND_TYPE(type, warning, "string ")
11150 if (RB_TYPE_P(node->nd_lit, T_REGEXP)) {
11151 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ")
11152 nd_set_type(node, NODE_MATCH);
11154 else if (node->nd_lit == Qtrue ||
11155 node->nd_lit == Qfalse) {
11156 /* booleans are OK, e.g., while true */
11159 SWITCH_BY_COND_TYPE(type, warning, "")
11168cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11170 if (node == 0) return 0;
11171 return cond0(p, node, COND_IN_COND, loc);
11175method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11177 if (node == 0) return 0;
11178 return cond0(p, node, COND_IN_OP, loc);
11182new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11184 if (!cc) return right;
11185 cc = cond0(p, cc, COND_IN_COND, loc);
11186 return newline_node(NEW_IF(cc, left, right, loc));
11190new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
11192 if (!cc) return right;
11193 cc = cond0(p, cc, COND_IN_COND, loc);
11194 return newline_node(NEW_UNLESS(cc, left, right, loc));
11198logop(struct parser_params *p, ID id, NODE *left, NODE *right,
11199 const YYLTYPE *op_loc, const YYLTYPE *loc)
11201 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
11204 if (left && (enum node_type)nd_type(left) == type) {
11205 NODE *node = left, *second;
11206 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) {
11209 node->nd_2nd = NEW_NODE(type, second, right, 0, loc);
11210 nd_set_line(node->nd_2nd, op_loc->beg_pos.lineno);
11211 left->nd_loc.end_pos = loc->end_pos;
11214 op = NEW_NODE(type, left, right, 0, loc);
11215 nd_set_line(op, op_loc->beg_pos.lineno);
11220no_blockarg(struct parser_params *p, NODE *node)
11222 if (node && nd_type(node) == NODE_BLOCK_PASS) {
11223 compile_error(p, "block argument should not be given");
11228ret_args(struct parser_params *p, NODE *node)
11231 no_blockarg(p, node);
11232 if (nd_type(node) == NODE_LIST) {
11233 if (node->nd_next == 0) {
11234 node = node->nd_head;
11237 nd_set_type(node, NODE_VALUES);
11245new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11247 if (node) no_blockarg(p, node);
11249 return NEW_YIELD(node, loc);
11253negate_lit(struct parser_params *p, VALUE lit)
11255 if (FIXNUM_P(lit)) {
11256 return LONG2FIX(-FIX2LONG(lit));
11258 if (SPECIAL_CONST_P(lit)) {
11260 if (FLONUM_P(lit)) {
11261 return DBL2NUM(-RFLOAT_VALUE(lit));
11266 switch (BUILTIN_TYPE(lit)) {
11268 BIGNUM_NEGATE(lit);
11269 lit = rb_big_norm(lit);
11272 RRATIONAL_SET_NUM(lit, negate_lit(p, RRATIONAL(lit)->num));
11275 RCOMPLEX_SET_REAL(lit, negate_lit(p, RCOMPLEX(lit)->real));
11276 RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
11279 RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
11283 rb_parser_fatal(p, "unknown literal type (%s) passed to negate_lit",
11284 rb_builtin_class_name(lit));
11291arg_blk_pass(NODE *node1, NODE *node2)
11294 if (!node1) return node2;
11295 node2->nd_head = node1;
11296 nd_set_first_lineno(node2, nd_first_lineno(node1));
11297 nd_set_first_column(node2, nd_first_column(node1));
11304args_info_empty_p(struct rb_args_info *args)
11306 if (args->pre_args_num) return false;
11307 if (args->post_args_num) return false;
11308 if (args->rest_arg) return false;
11309 if (args->opt_args) return false;
11310 if (args->block_arg) return false;
11311 if (args->kw_args) return false;
11312 if (args->kw_rest_arg) return false;
11317new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, NODE *post_args, NODE *tail, const YYLTYPE *loc)
11319 int saved_line = p->ruby_sourceline;
11320 struct rb_args_info *args = tail->nd_ainfo;
11322 args->pre_args_num = pre_args ? rb_long2int(pre_args->nd_plen) : 0;
11323 args->pre_init = pre_args ? pre_args->nd_next : 0;
11325 args->post_args_num = post_args ? rb_long2int(post_args->nd_plen) : 0;
11326 args->post_init = post_args ? post_args->nd_next : 0;
11327 args->first_post_arg = post_args ? post_args->nd_pid : 0;
11329 args->rest_arg = rest_arg;
11331 args->opt_args = opt_args;
11333 args->ruby2_keywords = rest_arg == idFWD_REST;
11335 p->ruby_sourceline = saved_line;
11336 nd_set_loc(tail, loc);
11342new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *loc)
11344 int saved_line = p->ruby_sourceline;
11346 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11347 struct rb_args_info *args = ZALLOC(struct rb_args_info);
11348 rb_imemo_tmpbuf_set_ptr(tmpbuf, args);
11349 args->imemo = tmpbuf;
11350 node = NEW_NODE(NODE_ARGS, 0, 0, args, &NULL_LOC);
11351 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11352 if (p->error_p) return node;
11354 args->block_arg = block;
11355 args->kw_args = kw_args;
11359 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
11360 * variable order: k1, kr1, k2, &b, internal_id, krest
11362 * variable order: kr1, k1, k2, internal_id, krest, &b
11364 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
11365 struct vtable *vtargs = p->lvtbl->args;
11366 NODE *kwn = kw_args;
11368 vtable_pop(vtargs, !!block + !!kw_rest_arg);
11369 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
11371 if (!NODE_REQUIRED_KEYWORD_P(kwn->nd_body))
11373 --required_kw_vars;
11374 kwn = kwn->nd_next;
11377 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
11378 ID vid = kwn->nd_body->nd_vid;
11379 if (NODE_REQUIRED_KEYWORD_P(kwn->nd_body)) {
11380 *required_kw_vars++ = vid;
11387 arg_var(p, kw_bits);
11388 if (kw_rest_arg) arg_var(p, kw_rest_arg);
11389 if (block) arg_var(p, block);
11391 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11392 args->kw_rest_arg->nd_cflag = kw_bits;
11394 else if (kw_rest_arg == idNil) {
11395 args->no_kwarg = 1;
11397 else if (kw_rest_arg) {
11398 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, loc);
11401 p->ruby_sourceline = saved_line;
11406args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
11408 if (max_numparam > NO_PARAM) {
11410 YYLTYPE loc = RUBY_INIT_YYLLOC();
11411 args = new_args_tail(p, 0, 0, 0, 0);
11412 nd_set_loc(args, &loc);
11414 args->nd_ainfo->pre_args_num = max_numparam;
11420new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
11422 struct rb_ary_pattern_info *apinfo = aryptn->nd_apinfo;
11424 aryptn->nd_pconst = constant;
11427 NODE *pre_args = NEW_LIST(pre_arg, loc);
11428 if (apinfo->pre_args) {
11429 apinfo->pre_args = list_concat(pre_args, apinfo->pre_args);
11432 apinfo->pre_args = pre_args;
11439new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, ID rest_arg, NODE *post_args, const YYLTYPE *loc)
11441 int saved_line = p->ruby_sourceline;
11443 VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer();
11444 struct rb_ary_pattern_info *apinfo = ZALLOC(struct rb_ary_pattern_info);
11445 rb_imemo_tmpbuf_set_ptr(tmpbuf, apinfo);
11446 node = NEW_NODE(NODE_ARYPTN, 0, 0, apinfo, loc);
11447 apinfo->imemo = tmpbuf;
11448 RB_OBJ_WRITTEN(p->ast, Qnil, tmpbuf);
11450 apinfo->pre_args = pre_args;
11454 apinfo->rest_arg = assignable(p, rest_arg, 0, loc);
11457 apinfo->rest_arg = NODE_SPECIAL_NO_NAME_REST;
11461 apinfo->rest_arg = NULL;
11464 apinfo->post_args = post_args;
11466 p->ruby_sourceline = saved_line;
11471new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
11473 hshptn->nd_pconst = constant;
11478new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
11480 int saved_line = p->ruby_sourceline;
11481 NODE *node, *kw_rest_arg_node;
11483 if (kw_rest_arg == idNil) {
11484 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
11486 else if (kw_rest_arg) {
11487 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
11490 kw_rest_arg_node = NULL;
11493 node = NEW_NODE(NODE_HSHPTN, 0, kw_args, kw_rest_arg_node, loc);
11495 p->ruby_sourceline = saved_line;
11500new_case3(struct parser_params *p, NODE *val, NODE *pat, const YYLTYPE *loc)
11502 NODE *node = NEW_CASE3(val, pat, loc);
11504 if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_EXPERIMENTAL))
11505 rb_warn0L(nd_line(node), "Pattern matching is experimental, and the behavior may change in future versions of Ruby!");
11510dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
11515 return NEW_LIT(ID2SYM(idNULL), loc);
11518 switch (nd_type(node)) {
11520 nd_set_type(node, NODE_DSYM);
11521 nd_set_loc(node, loc);
11524 lit = node->nd_lit;
11525 RB_OBJ_WRITTEN(p->ast, Qnil, node->nd_lit = ID2SYM(rb_intern_str(lit)));
11526 nd_set_type(node, NODE_LIT);
11527 nd_set_loc(node, loc);
11530 node = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST(node, loc), loc);
11537append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
11539 NODE *node = (NODE *)v;
11540 NODE **result = (NODE **)h;
11542 node->nd_next->nd_end = node->nd_next;
11543 node->nd_next->nd_next = 0;
11545 list_concat(*result, node);
11548 return ST_CONTINUE;
11552remove_duplicate_keys(struct parser_params *p, NODE *hash)
11554 st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
11556 rb_code_location_t loc = hash->nd_loc;
11557 while (hash && hash->nd_head && hash->nd_next) {
11558 NODE *head = hash->nd_head;
11559 NODE *value = hash->nd_next;
11560 NODE *next = value->nd_next;
11561 VALUE key = (VALUE)head;
11563 if (nd_type(head) == NODE_LIT &&
11564 st_lookup(literal_keys, (key = head->nd_lit), &data)) {
11565 rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
11566 "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
11567 head->nd_lit, nd_line(head));
11568 head = ((NODE *)data)->nd_next;
11569 head->nd_head = block_append(p, head->nd_head, value->nd_head);
11572 st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
11576 st_foreach(literal_keys, append_literal_keys, (st_data_t)&result);
11577 st_free_table(literal_keys);
11579 if (!result) result = hash;
11580 else list_concat(result, hash);
11582 result->nd_loc = loc;
11587new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11589 if (hash) hash = remove_duplicate_keys(p, hash);
11590 return NEW_HASH(hash, loc);
11595error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
11597 if (is_private_local_id(id)) {
11600 if (st_is_member(p->pvtbl, id)) {
11601 yyerror1(loc, "duplicated variable name");
11604 st_insert(p->pvtbl, (st_data_t)id, 0);
11609error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
11612 p->pktbl = st_init_numtable();
11614 else if (st_is_member(p->pktbl, key)) {
11615 yyerror1(loc, "duplicated key name");
11618 st_insert(p->pktbl, (st_data_t)key, 0);
11623new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
11625 return NEW_HASH(hash, loc);
11627#endif /* !RIPPER */
11631new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11636 ID vid = lhs->nd_vid;
11637 YYLTYPE lhs_loc = lhs->nd_loc;
11639 lhs->nd_value = rhs;
11640 nd_set_loc(lhs, loc);
11641 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
11642 if (is_notop_id(vid)) {
11643 switch (id_type(vid)) {
11647 asgn->nd_aid = vid;
11651 else if (op == tANDOP) {
11652 lhs->nd_value = rhs;
11653 nd_set_loc(lhs, loc);
11654 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
11658 asgn->nd_value = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
11659 nd_set_loc(asgn, loc);
11663 asgn = NEW_BEGIN(0, loc);
11669new_ary_op_assign(struct parser_params *p, NODE *ary,
11670 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc)
11674 args = make_list(args, args_loc);
11675 if (nd_type(args) == NODE_BLOCK_PASS) {
11676 args = NEW_ARGSCAT(args, rhs, loc);
11679 args = arg_concat(p, args, rhs, loc);
11681 asgn = NEW_OP_ASGN1(ary, op, args, loc);
11687new_attr_op_assign(struct parser_params *p, NODE *lhs,
11688 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc)
11692 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc);
11698new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const YYLTYPE *loc)
11703 asgn = NEW_OP_CDECL(lhs, op, rhs, loc);
11706 asgn = NEW_BEGIN(0, loc);
11713const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
11716 yyerror1(loc, "dynamic constant assignment");
11718 return NEW_CDECL(0, 0, (path), loc);
11722const_decl(struct parser_params *p, VALUE path)
11725 path = dispatch1(assign_error, path);
11732assign_error(struct parser_params *p, VALUE a)
11734 a = dispatch1(assign_error, a);
11740var_field(struct parser_params *p, VALUE a)
11742 return ripper_new_yylval(p, get_id(a), dispatch1(var_field, a), 0);
11748new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
11750 NODE *result = head;
11752 NODE *tmp = rescue_else ? rescue_else : rescue;
11753 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
11755 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
11756 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
11758 else if (rescue_else) {
11759 result = block_append(p, result, rescue_else);
11762 result = NEW_ENSURE(result, ensure, loc);
11764 fixpos(result, head);
11770warn_unused_var(struct parser_params *p, struct local_vars *local)
11774 if (!local->used) return;
11775 cnt = local->used->pos;
11776 if (cnt != local->vars->pos) {
11777 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
11780 ID *v = local->vars->tbl;
11781 ID *u = local->used->tbl;
11782 for (int i = 0; i < cnt; ++i) {
11783 if (!v[i] || (u[i] & LVAR_USED)) continue;
11784 if (is_private_local_id(v[i])) continue;
11785 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
11791local_push(struct parser_params *p, int toplevel_scope)
11793 struct local_vars *local;
11794 int inherits_dvars = toplevel_scope && compile_for_eval;
11795 int warn_unused_vars = RTEST(ruby_verbose);
11797 local = ALLOC(struct local_vars);
11798 local->prev = p->lvtbl;
11799 local->args = vtable_alloc(0);
11800 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
11802 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
11803 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
11804 local->numparam.outer = 0;
11805 local->numparam.inner = 0;
11806 local->numparam.current = 0;
11808 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
11810# if WARN_PAST_SCOPE
11819local_pop(struct parser_params *p)
11821 struct local_vars *local = p->lvtbl->prev;
11822 if (p->lvtbl->used) {
11823 warn_unused_var(p, p->lvtbl);
11824 vtable_free(p->lvtbl->used);
11826# if WARN_PAST_SCOPE
11827 while (p->lvtbl->past) {
11828 struct vtable *past = p->lvtbl->past;
11829 p->lvtbl->past = past->prev;
11833 vtable_free(p->lvtbl->args);
11834 vtable_free(p->lvtbl->vars);
11837 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
11843local_tbl(struct parser_params *p)
11845 int cnt_args = vtable_size(p->lvtbl->args);
11846 int cnt_vars = vtable_size(p->lvtbl->vars);
11847 int cnt = cnt_args + cnt_vars;
11851 if (cnt <= 0) return 0;
11852 buf = ALLOC_N(ID, cnt + 2);
11853 MEMCPY(buf+1, p->lvtbl->args->tbl, ID, cnt_args);
11854 /* remove IDs duplicated to warn shadowing */
11855 for (i = 0, j = cnt_args+1; i < cnt_vars; ++i) {
11856 ID id = p->lvtbl->vars->tbl[i];
11857 if (!vtable_included(p->lvtbl->args, id)) {
11862 REALLOC_N(buf, ID, (cnt = j) + 2);
11865 rb_ast_add_local_table(p->ast, buf);
11871node_newnode_with_locals(struct parser_params *p, enum node_type type, VALUE a1, VALUE a2, const rb_code_location_t *loc)
11877 n = NEW_NODE(type, a0, a1, a2, loc);
11884numparam_name(struct parser_params *p, ID id)
11886 if (!NUMPARAM_ID_P(id)) return;
11887 rb_warn1("`_%d' is reserved for numbered parameter; consider another name",
11888 WARN_I(NUMPARAM_ID_TO_IDX(id)));
11892arg_var(struct parser_params *p, ID id)
11894 numparam_name(p, id);
11895 vtable_add(p->lvtbl->args, id);
11899local_var(struct parser_params *p, ID id)
11901 numparam_name(p, id);
11902 vtable_add(p->lvtbl->vars, id);
11903 if (p->lvtbl->used) {
11904 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
11909local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
11911 struct vtable *vars, *args, *used;
11913 vars = p->lvtbl->vars;
11914 args = p->lvtbl->args;
11915 used = p->lvtbl->used;
11917 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
11920 if (used) used = used->prev;
11923 if (vars && vars->prev == DVARS_INHERIT) {
11924 return rb_local_defined(id, p->parent_iseq);
11926 else if (vtable_included(args, id)) {
11930 int i = vtable_included(vars, id);
11931 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
11937local_id(struct parser_params *p, ID id)
11939 return local_id_ref(p, id, NULL);
11943numparam_push(struct parser_params *p)
11946 struct local_vars *local = p->lvtbl;
11947 NODE *inner = local->numparam.inner;
11948 if (!local->numparam.outer) {
11949 local->numparam.outer = local->numparam.current;
11951 local->numparam.inner = 0;
11952 local->numparam.current = 0;
11960numparam_pop(struct parser_params *p, NODE *prev_inner)
11963 struct local_vars *local = p->lvtbl;
11965 /* prefer first one */
11966 local->numparam.inner = prev_inner;
11968 else if (local->numparam.current) {
11969 /* current and inner are exclusive */
11970 local->numparam.inner = local->numparam.current;
11972 if (p->max_numparam > NO_PARAM) {
11973 /* current and outer are exclusive */
11974 local->numparam.current = local->numparam.outer;
11975 local->numparam.outer = 0;
11978 /* no numbered parameter */
11979 local->numparam.current = 0;
11984static const struct vtable *
11985dyna_push(struct parser_params *p)
11987 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
11988 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
11989 if (p->lvtbl->used) {
11990 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
11992 return p->lvtbl->args;
11996dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
11998 struct vtable *tmp = *vtblp;
11999 *vtblp = tmp->prev;
12000# if WARN_PAST_SCOPE
12001 if (p->past_scope_enabled) {
12002 tmp->prev = p->lvtbl->past;
12003 p->lvtbl->past = tmp;
12011dyna_pop_1(struct parser_params *p)
12013 struct vtable *tmp;
12015 if ((tmp = p->lvtbl->used) != 0) {
12016 warn_unused_var(p, p->lvtbl);
12017 p->lvtbl->used = p->lvtbl->used->prev;
12020 dyna_pop_vtable(p, &p->lvtbl->args);
12021 dyna_pop_vtable(p, &p->lvtbl->vars);
12025dyna_pop(struct parser_params *p, const struct vtable *lvargs)
12027 while (p->lvtbl->args != lvargs) {
12029 if (!p->lvtbl->args) {
12030 struct local_vars *local = p->lvtbl->prev;
12031 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
12039dyna_in_block(struct parser_params *p)
12041 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
12045dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
12047 struct vtable *vars, *args, *used;
12050 args = p->lvtbl->args;
12051 vars = p->lvtbl->vars;
12052 used = p->lvtbl->used;
12054 while (!DVARS_TERMINAL_P(vars)) {
12055 if (vtable_included(args, id)) {
12058 if ((i = vtable_included(vars, id)) != 0) {
12059 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
12064 if (!vidrefp) used = 0;
12065 if (used) used = used->prev;
12068 if (vars == DVARS_INHERIT) {
12069 return rb_dvar_defined(id, p->parent_iseq);
12076dvar_defined(struct parser_params *p, ID id)
12078 return dvar_defined_ref(p, id, NULL);
12082dvar_curr(struct parser_params *p, ID id)
12084 return (vtable_included(p->lvtbl->args, id) ||
12085 vtable_included(p->lvtbl->vars, id));
12089reg_fragment_enc_error(struct parser_params* p, VALUE str, int c)
12092 "regexp encoding option '%c' differs from source encoding '%s'",
12093 c, rb_enc_name(rb_enc_get(str)));
12098rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12100 int c = RE_OPTION_ENCODING_IDX(options);
12104 rb_char_to_option_kcode(c, &opt, &idx);
12105 if (idx != ENCODING_GET(str) &&
12106 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12109 ENCODING_SET(str, idx);
12111 else if (RE_OPTION_ENCODING_NONE(options)) {
12112 if (!ENCODING_IS_ASCII8BIT(str) &&
12113 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12117 rb_enc_associate(str, rb_ascii8bit_encoding());
12119 else if (p->enc == rb_usascii_encoding()) {
12120 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
12121 /* raise in re.c */
12122 rb_enc_associate(str, rb_usascii_encoding());
12125 rb_enc_associate(str, rb_ascii8bit_encoding());
12135reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
12137 int c = rb_reg_fragment_setenc(p, str, options);
12138 if (c) reg_fragment_enc_error(p, str, c);
12142reg_fragment_check(struct parser_params* p, VALUE str, int options)
12145 reg_fragment_setenc(p, str, options);
12146 err = rb_reg_check_preprocess(str);
12148 err = rb_obj_as_string(err);
12149 compile_error(p, "%"PRIsVALUE, err);
12156 struct parser_params* parser;
12159 const YYLTYPE *loc;
12160} reg_named_capture_assign_t;
12163reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
12164 int back_num, int *back_refs, OnigRegex regex, void *arg0)
12166 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
12167 struct parser_params* p = arg->parser;
12168 rb_encoding *enc = arg->enc;
12169 long len = name_end - name;
12170 const char *s = (const char *)name;
12174 if (!len) return ST_CONTINUE;
12175 if (rb_enc_symname_type(s, len, enc, (1U<<ID_LOCAL)) != ID_LOCAL)
12176 return ST_CONTINUE;
12178 var = intern_cstr(s, len, enc);
12179 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
12180 if (!lvar_defined(p, var)) return ST_CONTINUE;
12182 node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), arg->loc);
12183 succ = arg->succ_block;
12184 if (!succ) succ = NEW_BEGIN(0, arg->loc);
12185 succ = block_append(p, succ, node);
12186 arg->succ_block = succ;
12187 return ST_CONTINUE;
12191reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc)
12193 reg_named_capture_assign_t arg;
12196 arg.enc = rb_enc_get(regexp);
12197 arg.succ_block = 0;
12199 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
12201 if (!arg.succ_block) return 0;
12202 return arg.succ_block->nd_next;
12206parser_reg_compile(struct parser_params* p, VALUE str, int options)
12208 reg_fragment_setenc(p, str, options);
12209 return rb_parser_reg_compile(p, str, options);
12213rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
12215 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
12219reg_compile(struct parser_params* p, VALUE str, int options)
12224 err = rb_errinfo();
12225 re = parser_reg_compile(p, str, options);
12227 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
12228 rb_set_errinfo(err);
12229 compile_error(p, "%"PRIsVALUE, m);
12236parser_reg_compile(struct parser_params* p, VALUE str, int options, VALUE *errmsg)
12238 VALUE err = rb_errinfo();
12240 str = ripper_is_node_yylval(str) ? RNODE(str)->nd_cval : str;
12241 int c = rb_reg_fragment_setenc(p, str, options);
12242 if (c) reg_fragment_enc_error(p, str, c);
12243 re = rb_parser_reg_compile(p, str, options);
12245 *errmsg = rb_attr_get(rb_errinfo(), idMesg);
12246 rb_set_errinfo(err);
12254rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
12256 struct parser_params *p;
12257 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12258 p->do_print = print;
12260 p->do_chomp = chomp;
12261 p->do_split = split;
12265rb_parser_warn_location(VALUE vparser, int warn)
12267 struct parser_params *p;
12268 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12269 p->warn_location = warn;
12273parser_append_options(struct parser_params *p, NODE *node)
12275 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
12276 const YYLTYPE *const LOC = &default_location;
12279 NODE *print = NEW_FCALL(rb_intern("print"),
12280 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
12282 node = block_append(p, node, print);
12287 NODE *args = NEW_LIST(NEW_GVAR(rb_intern("$;"), LOC), LOC);
12288 NODE *split = NEW_GASGN(rb_intern("$F"),
12289 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12290 rb_intern("split"), args, LOC),
12292 node = block_append(p, split, node);
12295 NODE *chomp = NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
12296 rb_intern("chomp!"), 0, LOC);
12297 node = block_append(p, chomp, node);
12300 node = NEW_WHILE(NEW_VCALL(idGets, LOC), node, 1, LOC);
12309 /* just to suppress unused-function warnings */
12315internal_id(struct parser_params *p)
12317 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
12318 ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
12320 return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
12322#endif /* !RIPPER */
12325parser_initialize(struct parser_params *p)
12327 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
12328 p->command_start = TRUE;
12329 p->ruby_sourcefile_string = Qnil;
12330 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
12333 p->delayed.token = Qnil;
12335 p->parsing_thread = Qnil;
12337 p->error_buffer = Qfalse;
12339 p->debug_buffer = Qnil;
12340 p->debug_output = rb_stdout;
12341 p->enc = rb_utf8_encoding();
12345#define parser_mark ripper_parser_mark
12346#define parser_free ripper_parser_free
12350parser_mark(void *ptr)
12352 struct parser_params *p = (struct parser_params*)ptr;
12354 rb_gc_mark(p->lex.input);
12355 rb_gc_mark(p->lex.prevline);
12356 rb_gc_mark(p->lex.lastline);
12357 rb_gc_mark(p->lex.nextline);
12358 rb_gc_mark(p->ruby_sourcefile_string);
12359 rb_gc_mark((VALUE)p->lex.strterm);
12360 rb_gc_mark((VALUE)p->ast);
12361 rb_gc_mark(p->case_labels);
12363 rb_gc_mark(p->debug_lines);
12364 rb_gc_mark(p->compile_option);
12365 rb_gc_mark(p->error_buffer);
12367 rb_gc_mark(p->delayed.token);
12368 rb_gc_mark(p->value);
12369 rb_gc_mark(p->result);
12370 rb_gc_mark(p->parsing_thread);
12372 rb_gc_mark(p->debug_buffer);
12373 rb_gc_mark(p->debug_output);
12375 rb_gc_mark((VALUE)p->heap);
12380parser_free(void *ptr)
12382 struct parser_params *p = (struct parser_params*)ptr;
12383 struct local_vars *local, *prev;
12386 ruby_sized_xfree(p->tokenbuf, p->toksiz);
12388 for (local = p->lvtbl; local; local = prev) {
12389 if (local->vars) xfree(local->vars);
12390 prev = local->prev;
12394 token_info *ptinfo;
12395 while ((ptinfo = p->token_info) != 0) {
12396 p->token_info = ptinfo->next;
12404parser_memsize(const void *ptr)
12406 struct parser_params *p = (struct parser_params*)ptr;
12407 struct local_vars *local;
12408 size_t size = sizeof(*p);
12411 for (local = p->lvtbl; local; local = local->prev) {
12412 size += sizeof(*local);
12413 if (local->vars) size += local->vars->capa * sizeof(ID);
12418static const rb_data_type_t parser_data_type = {
12429 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
12433#undef rb_reserved_word
12435const struct kwtable *
12436rb_reserved_word(const char *str, unsigned int len)
12438 return reserved_word(str, len);
12444 struct parser_params *p;
12445 VALUE parser = TypedData_Make_Struct(0, struct parser_params,
12446 &parser_data_type, p);
12447 parser_initialize(p);
12452rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
12454 struct parser_params *p;
12456 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12457 p->error_buffer = main ? Qfalse : Qnil;
12458 p->parent_iseq = base;
12464#define rb_parser_end_seen_p ripper_parser_end_seen_p
12465#define rb_parser_encoding ripper_parser_encoding
12466#define rb_parser_get_yydebug ripper_parser_get_yydebug
12467#define rb_parser_set_yydebug ripper_parser_set_yydebug
12468#define rb_parser_get_debug_output ripper_parser_get_debug_output
12469#define rb_parser_set_debug_output ripper_parser_set_debug_output
12470static VALUE ripper_parser_end_seen_p(VALUE vparser);
12471static VALUE ripper_parser_encoding(VALUE vparser);
12472static VALUE ripper_parser_get_yydebug(VALUE self);
12473static VALUE ripper_parser_set_yydebug(VALUE self, VALUE flag);
12474static VALUE ripper_parser_get_debug_output(VALUE self);
12475static VALUE ripper_parser_set_debug_output(VALUE self, VALUE output);
12479 * ripper.error? -> Boolean
12481 * Return true if parsed source has errors.
12484ripper_error_p(VALUE vparser)
12486 struct parser_params *p;
12488 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12489 return p->error_p ? Qtrue : Qfalse;
12495 * ripper.end_seen? -> Boolean
12497 * Return true if parsed source ended by +\_\_END\_\_+.
12500rb_parser_end_seen_p(VALUE vparser)
12502 struct parser_params *p;
12504 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12505 return p->ruby__end__seen ? Qtrue : Qfalse;
12510 * ripper.encoding -> encoding
12512 * Return encoding of the source.
12515rb_parser_encoding(VALUE vparser)
12517 struct parser_params *p;
12519 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p);
12520 return rb_enc_from_encoding(p->enc);
12526 * ripper.yydebug -> true or false
12531rb_parser_get_yydebug(VALUE self)
12533 struct parser_params *p;
12535 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12536 return p->debug ? Qtrue : Qfalse;
12542 * ripper.yydebug = flag
12547rb_parser_set_yydebug(VALUE self, VALUE flag)
12549 struct parser_params *p;
12551 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12552 p->debug = RTEST(flag);
12558 * ripper.debug_output -> obj
12560 * Get debug output.
12563rb_parser_get_debug_output(VALUE self)
12565 struct parser_params *p;
12567 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12568 return p->debug_output;
12573 * ripper.debug_output = obj
12575 * Set debug output.
12578rb_parser_set_debug_output(VALUE self, VALUE output)
12580 struct parser_params *p;
12582 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12583 return p->debug_output = output;
12588#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
12589/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
12590 * potential memory leak */
12591#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
12592#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
12593 (new)->cnt = (cnt), (ptr))
12596rb_parser_malloc(struct parser_params *p, size_t size)
12598 size_t cnt = HEAPCNT(1, size);
12599 rb_imemo_tmpbuf_t *n = NEWHEAP();
12600 void *ptr = xmalloc(size);
12602 return ADD2HEAP(n, cnt, ptr);
12606rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
12608 size_t cnt = HEAPCNT(nelem, size);
12609 rb_imemo_tmpbuf_t *n = NEWHEAP();
12610 void *ptr = xcalloc(nelem, size);
12612 return ADD2HEAP(n, cnt, ptr);
12616rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
12618 rb_imemo_tmpbuf_t *n;
12619 size_t cnt = HEAPCNT(1, size);
12621 if (ptr && (n = p->heap) != NULL) {
12623 if (n->ptr == ptr) {
12624 n->ptr = ptr = xrealloc(ptr, size);
12625 if (n->cnt) n->cnt = cnt;
12628 } while ((n = n->next) != NULL);
12631 ptr = xrealloc(ptr, size);
12632 return ADD2HEAP(n, cnt, ptr);
12636rb_parser_free(struct parser_params *p, void *ptr)
12638 rb_imemo_tmpbuf_t **prev = &p->heap, *n;
12640 while ((n = *prev) != NULL) {
12641 if (n->ptr == ptr) {
12643 rb_gc_force_recycle((VALUE)n);
12653rb_parser_printf(struct parser_params *p, const char *fmt, ...)
12656 VALUE mesg = p->debug_buffer;
12658 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
12660 rb_str_vcatf(mesg, fmt, ap);
12662 if (RSTRING_END(mesg)[-1] == '\n') {
12663 rb_io_write(p->debug_output, mesg);
12664 p->debug_buffer = Qnil;
12669parser_compile_error(struct parser_params *p, const char *fmt, ...)
12673 rb_io_flush(p->debug_output);
12677 rb_syntax_error_append(p->error_buffer,
12678 p->ruby_sourcefile_string,
12679 p->ruby_sourceline,
12680 rb_long2int(p->lex.pcur - p->lex.pbeg),
12686count_char(const char *str, int c)
12689 while (str[n] == c) ++n;
12694 * strip enclosing double-quotes, same as the default yytnamerr except
12695 * for that single-quotes matching back-quotes do not stop stripping.
12697 * "\"`class' keyword\"" => "`class' keyword"
12699RUBY_FUNC_EXPORTED size_t
12700rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
12702 if (*yystr == '"') {
12703 size_t yyn = 0, bquote = 0;
12704 const char *yyp = yystr;
12710 bquote = count_char(yyp+1, '`') + 1;
12711 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
12719 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
12720 if (yyres) memcpy(yyres + yyn, yyp, bquote);
12726 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
12727 if (yyres) memcpy(yyres + yyn, yyp, 3);
12732 goto do_not_strip_quotes;
12735 goto do_not_strip_quotes;
12738 if (*++yyp != '\\')
12739 goto do_not_strip_quotes;
12740 /* Fall through. */
12755 do_not_strip_quotes: ;
12758 if (!yyres) return strlen(yystr);
12760 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
12768ripper_validate_object(VALUE self, VALUE x)
12770 if (x == Qfalse) return x;
12771 if (x == Qtrue) return x;
12772 if (x == Qnil) return x;
12774 rb_raise(rb_eArgError, "Qundef given");
12775 if (FIXNUM_P(x)) return x;
12776 if (SYMBOL_P(x)) return x;
12777 switch (BUILTIN_TYPE(x)) {
12787 if (nd_type((NODE *)x) != NODE_RIPPER) {
12788 rb_raise(rb_eArgError, "NODE given: %p", (void *)x);
12790 x = ((NODE *)x)->nd_rval;
12793 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)",
12794 (void *)x, rb_obj_classname(x));
12796 if (!RBASIC_CLASS(x)) {
12797 rb_raise(rb_eArgError, "hidden ruby object: %p (%s)",
12798 (void *)x, rb_builtin_type_name(TYPE(x)));
12804#define validate(x) ((x) = get_value(x))
12807ripper_dispatch0(struct parser_params *p, ID mid)
12809 return rb_funcall(p->value, mid, 0);
12813ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
12816 return rb_funcall(p->value, mid, 1, a);
12820ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
12824 return rb_funcall(p->value, mid, 2, a, b);
12828ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
12833 return rb_funcall(p->value, mid, 3, a, b, c);
12837ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
12843 return rb_funcall(p->value, mid, 4, a, b, c, d);
12847ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
12854 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
12858ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
12867 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
12871ripper_get_id(VALUE v)
12874 if (!RB_TYPE_P(v, T_NODE)) return 0;
12876 if (nd_type(nd) != NODE_RIPPER) return 0;
12881ripper_get_value(VALUE v)
12884 if (v == Qundef) return Qnil;
12885 if (!RB_TYPE_P(v, T_NODE)) return v;
12887 if (nd_type(nd) != NODE_RIPPER) return Qnil;
12888 return nd->nd_rval;
12892ripper_error(struct parser_params *p)
12898ripper_compile_error(struct parser_params *p, const char *fmt, ...)
12903 va_start(args, fmt);
12904 str = rb_vsprintf(fmt, args);
12906 rb_funcall(p->value, rb_intern("compile_error"), 1, str);
12911ripper_lex_get_generic(struct parser_params *p, VALUE src)
12913 VALUE line = rb_funcallv_public(src, id_gets, 0, 0);
12914 if (!NIL_P(line) && !RB_TYPE_P(line, T_STRING)) {
12915 rb_raise(rb_eTypeError,
12916 "gets returned %"PRIsVALUE" (expected String or nil)",
12917 rb_obj_class(line));
12923ripper_lex_io_get(struct parser_params *p, VALUE src)
12925 return rb_io_gets(src);
12929ripper_s_allocate(VALUE klass)
12931 struct parser_params *p;
12932 VALUE self = TypedData_Make_Struct(klass, struct parser_params,
12933 &parser_data_type, p);
12938#define ripper_initialized_p(r) ((r)->lex.input != 0)
12942 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper
12944 * Create a new Ripper object.
12945 * _src_ must be a String, an IO, or an Object which has #gets method.
12947 * This method does not starts parsing.
12948 * See also Ripper#parse and Ripper.parse.
12951ripper_initialize(int argc, VALUE *argv, VALUE self)
12953 struct parser_params *p;
12954 VALUE src, fname, lineno;
12956 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
12957 rb_scan_args(argc, argv, "12", &src, &fname, &lineno);
12958 if (RB_TYPE_P(src, T_FILE)) {
12959 p->lex.gets = ripper_lex_io_get;
12961 else if (rb_respond_to(src, id_gets)) {
12962 p->lex.gets = ripper_lex_get_generic;
12966 p->lex.gets = lex_get_str;
12968 p->lex.input = src;
12970 if (NIL_P(fname)) {
12971 fname = STR_NEW2("(ripper)");
12975 StringValueCStr(fname);
12976 fname = rb_str_new_frozen(fname);
12978 parser_initialize(p);
12980 p->ruby_sourcefile_string = fname;
12981 p->ruby_sourcefile = RSTRING_PTR(fname);
12982 p->ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1;
12988ripper_parse0(VALUE parser_v)
12990 struct parser_params *p;
12992 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
12994 p->ast = rb_ast_new();
12995 ripper_yyparse((void*)p);
12996 rb_ast_dispose(p->ast);
13002ripper_ensure(VALUE parser_v)
13004 struct parser_params *p;
13006 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, p);
13007 p->parsing_thread = Qnil;
13015 * Start parsing and returns the value of the root action.
13018ripper_parse(VALUE self)
13020 struct parser_params *p;
13022 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13023 if (!ripper_initialized_p(p)) {
13024 rb_raise(rb_eArgError, "method called for uninitialized object");
13026 if (!NIL_P(p->parsing_thread)) {
13027 if (p->parsing_thread == rb_thread_current())
13028 rb_raise(rb_eArgError, "Ripper#parse is not reentrant");
13030 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe");
13032 p->parsing_thread = rb_thread_current();
13033 rb_ensure(ripper_parse0, self, ripper_ensure, self);
13040 * ripper.column -> Integer
13042 * Return column number of current parsing line.
13043 * This number starts from 0.
13046ripper_column(VALUE self)
13048 struct parser_params *p;
13051 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13052 if (!ripper_initialized_p(p)) {
13053 rb_raise(rb_eArgError, "method called for uninitialized object");
13055 if (NIL_P(p->parsing_thread)) return Qnil;
13056 col = p->lex.ptok - p->lex.pbeg;
13057 return LONG2NUM(col);
13062 * ripper.filename -> String
13064 * Return current parsing filename.
13067ripper_filename(VALUE self)
13069 struct parser_params *p;
13071 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13072 if (!ripper_initialized_p(p)) {
13073 rb_raise(rb_eArgError, "method called for uninitialized object");
13075 return p->ruby_sourcefile_string;
13080 * ripper.lineno -> Integer
13082 * Return line number of current parsing line.
13083 * This number starts from 1.
13086ripper_lineno(VALUE self)
13088 struct parser_params *p;
13090 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13091 if (!ripper_initialized_p(p)) {
13092 rb_raise(rb_eArgError, "method called for uninitialized object");
13094 if (NIL_P(p->parsing_thread)) return Qnil;
13095 return INT2NUM(p->ruby_sourceline);
13100 * ripper.state -> Integer
13102 * Return scanner state of current token.
13105ripper_state(VALUE self)
13107 struct parser_params *p;
13109 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13110 if (!ripper_initialized_p(p)) {
13111 rb_raise(rb_eArgError, "method called for uninitialized object");
13113 if (NIL_P(p->parsing_thread)) return Qnil;
13114 return INT2NUM(p->lex.state);
13119 * ripper.token -> String
13121 * Return the current token string.
13124ripper_token(VALUE self)
13126 struct parser_params *p;
13129 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, p);
13130 if (!ripper_initialized_p(p)) {
13131 rb_raise(rb_eArgError, "method called for uninitialized object");
13133 if (NIL_P(p->parsing_thread)) return Qnil;
13134 pos = p->lex.ptok - p->lex.pbeg;
13135 len = p->lex.pcur - p->lex.ptok;
13136 return rb_str_subseq(p->lex.lastline, pos, len);
13142ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg)
13145 if (obj == Qundef) {
13146 rb_raise(rb_eArgError, "%"PRIsVALUE, msg);
13153ripper_value(VALUE self, VALUE obj)
13155 return ULONG2NUM(obj);
13161 * Ripper.lex_state_name(integer) -> string
13163 * Returns a string representation of lex_state.
13166ripper_lex_state_name(VALUE self, VALUE state)
13168 return rb_parser_lex_state_name(NUM2INT(state));
13174 ripper_init_eventids1();
13175 ripper_init_eventids2();
13176 id_warn = rb_intern_const("warn");
13177 id_warning = rb_intern_const("warning");
13178 id_gets = rb_intern_const("gets");
13179 id_assoc = rb_intern_const("=>");
13181 (void)yystpcpy; /* may not used in newer bison */
13191 Ripper = rb_define_class("Ripper", rb_cObject);
13192 /* version of Ripper */
13193 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION));
13194 rb_define_alloc_func(Ripper, ripper_s_allocate);
13195 rb_define_method(Ripper, "initialize", ripper_initialize, -1);
13196 rb_define_method(Ripper, "parse", ripper_parse, 0);
13197 rb_define_method(Ripper, "column", ripper_column, 0);
13198 rb_define_method(Ripper, "filename", ripper_filename, 0);
13199 rb_define_method(Ripper, "lineno", ripper_lineno, 0);
13200 rb_define_method(Ripper, "state", ripper_state, 0);
13201 rb_define_method(Ripper, "token", ripper_token, 0);
13202 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0);
13203 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0);
13204 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0);
13205 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1);
13206 rb_define_method(Ripper, "debug_output", rb_parser_get_debug_output, 0);
13207 rb_define_method(Ripper, "debug_output=", rb_parser_set_debug_output, 1);
13208 rb_define_method(Ripper, "error?", ripper_error_p, 0);
13210 rb_define_method(Ripper, "assert_Qundef", ripper_assert_Qundef, 2);
13211 rb_define_method(Ripper, "rawVALUE", ripper_value, 1);
13212 rb_define_method(Ripper, "validate_object", ripper_validate_object, 1);
13215 rb_define_singleton_method(Ripper, "dedent_string", parser_dedent_string, 2);
13216 rb_define_private_method(Ripper, "dedent_string", parser_dedent_string, 2);
13218 rb_define_singleton_method(Ripper, "lex_state_name", ripper_lex_state_name, 1);
13220<% @exprs.each do |expr, desc| -%>
13222 rb_define_const(Ripper, "<%=expr%>", INT2NUM(<%=expr%>));
13224 ripper_init_eventids1_table(Ripper);
13225 ripper_init_eventids2_table(Ripper);
13228 /* Hack to let RDoc document SCRIPT_LINES__ */
13231 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded
13232 * after the assignment will be added as an Array of lines with the file
13235 rb_define_global_const("SCRIPT_LINES__", Qnil);
13244 * c-file-style: "ruby"