|
Ruby
1.9.3p385(2013-02-06revision39114)
|
00001 /********************************************************************** 00002 00003 parse.y - 00004 00005 $Author: usa $ 00006 created at: Fri May 28 18:02:42 JST 1993 00007 00008 Copyright (C) 1993-2007 Yukihiro Matsumoto 00009 00010 **********************************************************************/ 00011 00012 %{ 00013 00014 #define YYDEBUG 1 00015 #define YYERROR_VERBOSE 1 00016 #define YYSTACK_USE_ALLOCA 0 00017 00018 #include "ruby/ruby.h" 00019 #include "ruby/st.h" 00020 #include "ruby/encoding.h" 00021 #include "internal.h" 00022 #include "node.h" 00023 #include "parse.h" 00024 #include "id.h" 00025 #include "regenc.h" 00026 #include <stdio.h> 00027 #include <errno.h> 00028 #include <ctype.h> 00029 00030 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0])) 00031 00032 #define YYMALLOC(size) rb_parser_malloc(parser, (size)) 00033 #define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size)) 00034 #define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size)) 00035 #define YYFREE(ptr) rb_parser_free(parser, (ptr)) 00036 #define malloc YYMALLOC 00037 #define realloc YYREALLOC 00038 #define calloc YYCALLOC 00039 #define free YYFREE 00040 00041 #ifndef RIPPER 00042 static ID register_symid(ID, const char *, long, rb_encoding *); 00043 #define REGISTER_SYMID(id, name) register_symid((id), (name), strlen(name), enc) 00044 #include "id.c" 00045 #endif 00046 00047 #define is_notop_id(id) ((id)>tLAST_TOKEN) 00048 #define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL) 00049 #define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL) 00050 #define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE) 00051 #define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET) 00052 #define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST) 00053 #define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS) 00054 #define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK) 00055 00056 #define is_asgn_or_id(id) ((is_notop_id(id)) && \ 00057 (((id)&ID_SCOPE_MASK) == ID_GLOBAL || \ 00058 ((id)&ID_SCOPE_MASK) == ID_INSTANCE || \ 00059 ((id)&ID_SCOPE_MASK) == ID_CLASS)) 00060 00061 enum lex_state_e { 00062 EXPR_BEG, /* ignore newline, +/- is a sign. */ 00063 EXPR_END, /* newline significant, +/- is an operator. */ 00064 EXPR_ENDARG, /* ditto, and unbound braces. */ 00065 EXPR_ENDFN, /* ditto, and unbound braces. */ 00066 EXPR_ARG, /* newline significant, +/- is an operator. */ 00067 EXPR_CMDARG, /* newline significant, +/- is an operator. */ 00068 EXPR_MID, /* newline significant, +/- is an operator. */ 00069 EXPR_FNAME, /* ignore newline, no reserved words. */ 00070 EXPR_DOT, /* right after `.' or `::', no reserved words. */ 00071 EXPR_CLASS, /* immediate after `class', no here document. */ 00072 EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */ 00073 EXPR_MAX_STATE 00074 }; 00075 00076 typedef VALUE stack_type; 00077 00078 # define BITSTACK_PUSH(stack, n) ((stack) = ((stack)<<1)|((n)&1)) 00079 # define BITSTACK_POP(stack) ((stack) = (stack) >> 1) 00080 # define BITSTACK_LEXPOP(stack) ((stack) = ((stack) >> 1) | ((stack) & 1)) 00081 # define BITSTACK_SET_P(stack) ((stack)&1) 00082 00083 #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n)) 00084 #define COND_POP() BITSTACK_POP(cond_stack) 00085 #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack) 00086 #define COND_P() BITSTACK_SET_P(cond_stack) 00087 00088 #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n)) 00089 #define CMDARG_POP() BITSTACK_POP(cmdarg_stack) 00090 #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack) 00091 #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack) 00092 00093 struct vtable { 00094 ID *tbl; 00095 int pos; 00096 int capa; 00097 struct vtable *prev; 00098 }; 00099 00100 struct local_vars { 00101 struct vtable *args; 00102 struct vtable *vars; 00103 struct vtable *used; 00104 struct local_vars *prev; 00105 }; 00106 00107 #define DVARS_INHERIT ((void*)1) 00108 #define DVARS_TOPSCOPE NULL 00109 #define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl)) 00110 #define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3) 00111 00112 static int 00113 vtable_size(const struct vtable *tbl) 00114 { 00115 if (POINTER_P(tbl)) { 00116 return tbl->pos; 00117 } 00118 else { 00119 return 0; 00120 } 00121 } 00122 00123 #define VTBL_DEBUG 0 00124 00125 static struct vtable * 00126 vtable_alloc(struct vtable *prev) 00127 { 00128 struct vtable *tbl = ALLOC(struct vtable); 00129 tbl->pos = 0; 00130 tbl->capa = 8; 00131 tbl->tbl = ALLOC_N(ID, tbl->capa); 00132 tbl->prev = prev; 00133 if (VTBL_DEBUG) printf("vtable_alloc: %p\n", (void *)tbl); 00134 return tbl; 00135 } 00136 00137 static void 00138 vtable_free(struct vtable *tbl) 00139 { 00140 if (VTBL_DEBUG)printf("vtable_free: %p\n", (void *)tbl); 00141 if (POINTER_P(tbl)) { 00142 if (tbl->tbl) { 00143 xfree(tbl->tbl); 00144 } 00145 xfree(tbl); 00146 } 00147 } 00148 00149 static void 00150 vtable_add(struct vtable *tbl, ID id) 00151 { 00152 if (!POINTER_P(tbl)) { 00153 rb_bug("vtable_add: vtable is not allocated (%p)", (void *)tbl); 00154 } 00155 if (VTBL_DEBUG) printf("vtable_add: %p, %s\n", (void *)tbl, rb_id2name(id)); 00156 00157 if (tbl->pos == tbl->capa) { 00158 tbl->capa = tbl->capa * 2; 00159 REALLOC_N(tbl->tbl, ID, tbl->capa); 00160 } 00161 tbl->tbl[tbl->pos++] = id; 00162 } 00163 00164 static int 00165 vtable_included(const struct vtable * tbl, ID id) 00166 { 00167 int i; 00168 00169 if (POINTER_P(tbl)) { 00170 for (i = 0; i < tbl->pos; i++) { 00171 if (tbl->tbl[i] == id) { 00172 return i+1; 00173 } 00174 } 00175 } 00176 return 0; 00177 } 00178 00179 00180 #ifndef RIPPER 00181 typedef struct token_info { 00182 const char *token; 00183 int linenum; 00184 int column; 00185 int nonspc; 00186 struct token_info *next; 00187 } token_info; 00188 #endif 00189 00190 /* 00191 Structure of Lexer Buffer: 00192 00193 lex_pbeg tokp lex_p lex_pend 00194 | | | | 00195 |-----------+--------------+------------| 00196 |<------------>| 00197 token 00198 */ 00199 struct parser_params { 00200 int is_ripper; 00201 NODE *heap; 00202 00203 YYSTYPE *parser_yylval; 00204 VALUE eofp; 00205 00206 NODE *parser_lex_strterm; 00207 enum lex_state_e parser_lex_state; 00208 stack_type parser_cond_stack; 00209 stack_type parser_cmdarg_stack; 00210 int parser_class_nest; 00211 int parser_paren_nest; 00212 int parser_lpar_beg; 00213 int parser_in_single; 00214 int parser_in_def; 00215 int parser_compile_for_eval; 00216 VALUE parser_cur_mid; 00217 int parser_in_defined; 00218 char *parser_tokenbuf; 00219 int parser_tokidx; 00220 int parser_toksiz; 00221 VALUE parser_lex_input; 00222 VALUE parser_lex_lastline; 00223 VALUE parser_lex_nextline; 00224 const char *parser_lex_pbeg; 00225 const char *parser_lex_p; 00226 const char *parser_lex_pend; 00227 int parser_heredoc_end; 00228 int parser_command_start; 00229 NODE *parser_deferred_nodes; 00230 long parser_lex_gets_ptr; 00231 VALUE (*parser_lex_gets)(struct parser_params*,VALUE); 00232 struct local_vars *parser_lvtbl; 00233 int parser_ruby__end__seen; 00234 int line_count; 00235 int has_shebang; 00236 char *parser_ruby_sourcefile; /* current source file */ 00237 int parser_ruby_sourceline; /* current line no. */ 00238 rb_encoding *enc; 00239 rb_encoding *utf8; 00240 00241 int parser_yydebug; 00242 00243 #ifndef RIPPER 00244 /* Ruby core only */ 00245 NODE *parser_eval_tree_begin; 00246 NODE *parser_eval_tree; 00247 VALUE debug_lines; 00248 VALUE coverage; 00249 int nerr; 00250 00251 int parser_token_info_enabled; 00252 token_info *parser_token_info; 00253 #else 00254 /* Ripper only */ 00255 VALUE parser_ruby_sourcefile_string; 00256 const char *tokp; 00257 VALUE delayed; 00258 int delayed_line; 00259 int delayed_col; 00260 00261 VALUE value; 00262 VALUE result; 00263 VALUE parsing_thread; 00264 int toplevel_p; 00265 #endif 00266 }; 00267 00268 #define UTF8_ENC() (parser->utf8 ? parser->utf8 : \ 00269 (parser->utf8 = rb_utf8_encoding())) 00270 #define STR_NEW(p,n) rb_enc_str_new((p),(n),parser->enc) 00271 #define STR_NEW0() rb_enc_str_new(0,0,parser->enc) 00272 #define STR_NEW2(p) rb_enc_str_new((p),strlen(p),parser->enc) 00273 #define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),parser->enc) 00274 #define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT) 00275 #define TOK_INTERN(mb) rb_intern3(tok(), toklen(), parser->enc) 00276 00277 static int parser_yyerror(struct parser_params*, const char*); 00278 #define yyerror(msg) parser_yyerror(parser, (msg)) 00279 00280 #define YYLEX_PARAM parser 00281 00282 #define lex_strterm (parser->parser_lex_strterm) 00283 #define lex_state (parser->parser_lex_state) 00284 #define cond_stack (parser->parser_cond_stack) 00285 #define cmdarg_stack (parser->parser_cmdarg_stack) 00286 #define class_nest (parser->parser_class_nest) 00287 #define paren_nest (parser->parser_paren_nest) 00288 #define lpar_beg (parser->parser_lpar_beg) 00289 #define in_single (parser->parser_in_single) 00290 #define in_def (parser->parser_in_def) 00291 #define compile_for_eval (parser->parser_compile_for_eval) 00292 #define cur_mid (parser->parser_cur_mid) 00293 #define in_defined (parser->parser_in_defined) 00294 #define tokenbuf (parser->parser_tokenbuf) 00295 #define tokidx (parser->parser_tokidx) 00296 #define toksiz (parser->parser_toksiz) 00297 #define lex_input (parser->parser_lex_input) 00298 #define lex_lastline (parser->parser_lex_lastline) 00299 #define lex_nextline (parser->parser_lex_nextline) 00300 #define lex_pbeg (parser->parser_lex_pbeg) 00301 #define lex_p (parser->parser_lex_p) 00302 #define lex_pend (parser->parser_lex_pend) 00303 #define heredoc_end (parser->parser_heredoc_end) 00304 #define command_start (parser->parser_command_start) 00305 #define deferred_nodes (parser->parser_deferred_nodes) 00306 #define lex_gets_ptr (parser->parser_lex_gets_ptr) 00307 #define lex_gets (parser->parser_lex_gets) 00308 #define lvtbl (parser->parser_lvtbl) 00309 #define ruby__end__seen (parser->parser_ruby__end__seen) 00310 #define ruby_sourceline (parser->parser_ruby_sourceline) 00311 #define ruby_sourcefile (parser->parser_ruby_sourcefile) 00312 #define current_enc (parser->enc) 00313 #define yydebug (parser->parser_yydebug) 00314 #ifdef RIPPER 00315 #else 00316 #define ruby_eval_tree (parser->parser_eval_tree) 00317 #define ruby_eval_tree_begin (parser->parser_eval_tree_begin) 00318 #define ruby_debug_lines (parser->debug_lines) 00319 #define ruby_coverage (parser->coverage) 00320 #endif 00321 00322 static int yylex(void*, void*); 00323 00324 #ifndef RIPPER 00325 #define yyparse ruby_yyparse 00326 00327 static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE); 00328 #define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3)) 00329 00330 static NODE *cond_gen(struct parser_params*,NODE*); 00331 #define cond(node) cond_gen(parser, (node)) 00332 static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*); 00333 #define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2)) 00334 00335 static NODE *newline_node(NODE*); 00336 static void fixpos(NODE*,NODE*); 00337 00338 static int value_expr_gen(struct parser_params*,NODE*); 00339 static void void_expr_gen(struct parser_params*,NODE*); 00340 static NODE *remove_begin(NODE*); 00341 #define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node)) 00342 #define void_expr0(node) void_expr_gen(parser, (node)) 00343 #define void_expr(node) void_expr0((node) = remove_begin(node)) 00344 static void void_stmts_gen(struct parser_params*,NODE*); 00345 #define void_stmts(node) void_stmts_gen(parser, (node)) 00346 static void reduce_nodes_gen(struct parser_params*,NODE**); 00347 #define reduce_nodes(n) reduce_nodes_gen(parser,(n)) 00348 static void block_dup_check_gen(struct parser_params*,NODE*,NODE*); 00349 #define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2)) 00350 00351 static NODE *block_append_gen(struct parser_params*,NODE*,NODE*); 00352 #define block_append(h,t) block_append_gen(parser,(h),(t)) 00353 static NODE *list_append_gen(struct parser_params*,NODE*,NODE*); 00354 #define list_append(l,i) list_append_gen(parser,(l),(i)) 00355 static NODE *list_concat_gen(struct parser_params*,NODE*,NODE*); 00356 #define list_concat(h,t) list_concat_gen(parser,(h),(t)) 00357 static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*); 00358 #define arg_append(h,t) arg_append_gen(parser,(h),(t)) 00359 static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*); 00360 #define arg_concat(h,t) arg_concat_gen(parser,(h),(t)) 00361 static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*); 00362 #define literal_concat(h,t) literal_concat_gen(parser,(h),(t)) 00363 static int literal_concat0(struct parser_params *, VALUE, VALUE); 00364 static NODE *new_evstr_gen(struct parser_params*,NODE*); 00365 #define new_evstr(n) new_evstr_gen(parser,(n)) 00366 static NODE *evstr2dstr_gen(struct parser_params*,NODE*); 00367 #define evstr2dstr(n) evstr2dstr_gen(parser,(n)) 00368 static NODE *splat_array(NODE*); 00369 00370 static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*); 00371 #define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1)) 00372 static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID); 00373 #define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id)) 00374 00375 static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,ID); 00376 #define new_args(f,o,r,p,b) new_args_gen(parser, (f),(o),(r),(p),(b)) 00377 00378 static NODE *negate_lit(NODE*); 00379 static NODE *ret_args_gen(struct parser_params*,NODE*); 00380 #define ret_args(node) ret_args_gen(parser, (node)) 00381 static NODE *arg_blk_pass(NODE*,NODE*); 00382 static NODE *new_yield_gen(struct parser_params*,NODE*); 00383 #define new_yield(node) new_yield_gen(parser, (node)) 00384 00385 static NODE *gettable_gen(struct parser_params*,ID); 00386 #define gettable(id) gettable_gen(parser,(id)) 00387 static NODE *assignable_gen(struct parser_params*,ID,NODE*); 00388 #define assignable(id,node) assignable_gen(parser, (id), (node)) 00389 00390 static NODE *aryset_gen(struct parser_params*,NODE*,NODE*); 00391 #define aryset(node1,node2) aryset_gen(parser, (node1), (node2)) 00392 static NODE *attrset_gen(struct parser_params*,NODE*,ID); 00393 #define attrset(node,id) attrset_gen(parser, (node), (id)) 00394 00395 static void rb_backref_error_gen(struct parser_params*,NODE*); 00396 #define rb_backref_error(n) rb_backref_error_gen(parser,(n)) 00397 static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*); 00398 #define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2)) 00399 00400 static NODE *match_op_gen(struct parser_params*,NODE*,NODE*); 00401 #define match_op(node1,node2) match_op_gen(parser, (node1), (node2)) 00402 00403 static ID *local_tbl_gen(struct parser_params*); 00404 #define local_tbl() local_tbl_gen(parser) 00405 00406 static void fixup_nodes(NODE **); 00407 00408 static VALUE reg_compile_gen(struct parser_params*, VALUE, int); 00409 #define reg_compile(str,options) reg_compile_gen(parser, (str), (options)) 00410 static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int); 00411 #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options)) 00412 static int reg_fragment_check_gen(struct parser_params*, VALUE, int); 00413 #define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options)) 00414 static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match); 00415 #define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match)) 00416 00417 #define get_id(id) (id) 00418 #define get_value(val) (val) 00419 #else 00420 #define remove_begin(node) (node) 00421 #define rb_dvar_defined(id) 0 00422 #define rb_local_defined(id) 0 00423 static ID ripper_get_id(VALUE); 00424 #define get_id(id) ripper_get_id(id) 00425 static VALUE ripper_get_value(VALUE); 00426 #define get_value(val) ripper_get_value(val) 00427 static VALUE assignable_gen(struct parser_params*,VALUE); 00428 #define assignable(lhs,node) assignable_gen(parser, (lhs)) 00429 static int id_is_var_gen(struct parser_params *parser, ID id); 00430 #define id_is_var(id) id_is_var_gen(parser, (id)) 00431 #endif /* !RIPPER */ 00432 00433 static ID formal_argument_gen(struct parser_params*, ID); 00434 #define formal_argument(id) formal_argument_gen(parser, (id)) 00435 static ID shadowing_lvar_gen(struct parser_params*,ID); 00436 #define shadowing_lvar(name) shadowing_lvar_gen(parser, (name)) 00437 static void new_bv_gen(struct parser_params*,ID); 00438 #define new_bv(id) new_bv_gen(parser, (id)) 00439 00440 static void local_push_gen(struct parser_params*,int); 00441 #define local_push(top) local_push_gen(parser,(top)) 00442 static void local_pop_gen(struct parser_params*); 00443 #define local_pop() local_pop_gen(parser) 00444 static int local_var_gen(struct parser_params*, ID); 00445 #define local_var(id) local_var_gen(parser, (id)); 00446 static int arg_var_gen(struct parser_params*, ID); 00447 #define arg_var(id) arg_var_gen(parser, (id)) 00448 static int local_id_gen(struct parser_params*, ID); 00449 #define local_id(id) local_id_gen(parser, (id)) 00450 static ID internal_id_gen(struct parser_params*); 00451 #define internal_id() internal_id_gen(parser) 00452 00453 static const struct vtable *dyna_push_gen(struct parser_params *); 00454 #define dyna_push() dyna_push_gen(parser) 00455 static void dyna_pop_gen(struct parser_params*, const struct vtable *); 00456 #define dyna_pop(node) dyna_pop_gen(parser, (node)) 00457 static int dyna_in_block_gen(struct parser_params*); 00458 #define dyna_in_block() dyna_in_block_gen(parser) 00459 #define dyna_var(id) local_var(id) 00460 static int dvar_defined_gen(struct parser_params*,ID,int); 00461 #define dvar_defined(id) dvar_defined_gen(parser, (id), 0) 00462 #define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1) 00463 static int dvar_curr_gen(struct parser_params*,ID); 00464 #define dvar_curr(id) dvar_curr_gen(parser, (id)) 00465 00466 static int lvar_defined_gen(struct parser_params*, ID); 00467 #define lvar_defined(id) lvar_defined_gen(parser, (id)) 00468 00469 #define RE_OPTION_ONCE (1<<16) 00470 #define RE_OPTION_ENCODING_SHIFT 8 00471 #define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT) 00472 #define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff) 00473 #define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE) 00474 #define RE_OPTION_MASK 0xff 00475 #define RE_OPTION_ARG_ENCODING_NONE 32 00476 00477 #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */ 00478 #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */ 00479 #define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1)) 00480 #define nd_func u1.id 00481 #if SIZEOF_SHORT == 2 00482 #define nd_term(node) ((signed short)(node)->u2.id) 00483 #else 00484 #define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2) 00485 #endif 00486 #define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2) 00487 #define nd_nest u3.cnt 00488 00489 /****** Ripper *******/ 00490 00491 #ifdef RIPPER 00492 #define RIPPER_VERSION "0.1.0" 00493 00494 #include "eventids1.c" 00495 #include "eventids2.c" 00496 static ID ripper_id_gets; 00497 00498 static VALUE ripper_dispatch0(struct parser_params*,ID); 00499 static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE); 00500 static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE); 00501 static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE); 00502 static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE); 00503 static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE); 00504 00505 #define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n)) 00506 #define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a)) 00507 #define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b)) 00508 #define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c)) 00509 #define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d)) 00510 #define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e)) 00511 00512 #define yyparse ripper_yyparse 00513 00514 #define ripper_intern(s) ID2SYM(rb_intern(s)) 00515 static VALUE ripper_id2sym(ID); 00516 #ifdef __GNUC__ 00517 #define ripper_id2sym(id) ((id) < 256 && rb_ispunct(id) ? \ 00518 ID2SYM(id) : ripper_id2sym(id)) 00519 #endif 00520 00521 #define arg_new() dispatch0(args_new) 00522 #define arg_add(l,a) dispatch2(args_add, (l), (a)) 00523 #define arg_add_star(l,a) dispatch2(args_add_star, (l), (a)) 00524 #define arg_add_block(l,b) dispatch2(args_add_block, (l), (b)) 00525 #define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b))) 00526 #define bare_assoc(v) dispatch1(bare_assoc_hash, (v)) 00527 #define arg_add_assocs(l,b) arg_add((l), bare_assoc(b)) 00528 00529 #define args2mrhs(a) dispatch1(mrhs_new_from_args, (a)) 00530 #define mrhs_new() dispatch0(mrhs_new) 00531 #define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a)) 00532 #define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a)) 00533 00534 #define mlhs_new() dispatch0(mlhs_new) 00535 #define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a)) 00536 #define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a)) 00537 00538 #define params_new(pars, opts, rest, pars2, blk) \ 00539 dispatch5(params, (pars), (opts), (rest), (pars2), (blk)) 00540 00541 #define blockvar_new(p,v) dispatch2(block_var, (p), (v)) 00542 #define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a)) 00543 #define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a)) 00544 00545 #define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a))) 00546 #define method_arg(m,a) dispatch2(method_add_arg,(m),(a)) 00547 #define method_add_block(m,b) dispatch2(method_add_block, (m), (b)) 00548 00549 #define escape_Qundef(x) ((x)==Qundef ? Qnil : (x)) 00550 00551 #define FIXME 0 00552 00553 #endif /* RIPPER */ 00554 00555 #ifndef RIPPER 00556 # define ifndef_ripper(x) (x) 00557 #else 00558 # define ifndef_ripper(x) 00559 #endif 00560 00561 #ifndef RIPPER 00562 # define rb_warn0(fmt) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt)) 00563 # define rb_warnI(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a)) 00564 # define rb_warnS(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a)) 00565 # define rb_warning0(fmt) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt)) 00566 # define rb_warningS(fmt,a) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt), (a)) 00567 #else 00568 # define rb_warn0(fmt) ripper_warn0(parser, (fmt)) 00569 # define rb_warnI(fmt,a) ripper_warnI(parser, (fmt), (a)) 00570 # define rb_warnS(fmt,a) ripper_warnS(parser, (fmt), (a)) 00571 # define rb_warning0(fmt) ripper_warning0(parser, (fmt)) 00572 # define rb_warningS(fmt,a) ripper_warningS(parser, (fmt), (a)) 00573 static void ripper_warn0(struct parser_params*, const char*); 00574 static void ripper_warnI(struct parser_params*, const char*, int); 00575 #if 0 00576 static void ripper_warnS(struct parser_params*, const char*, const char*); 00577 #endif 00578 static void ripper_warning0(struct parser_params*, const char*); 00579 static void ripper_warningS(struct parser_params*, const char*, const char*); 00580 #endif 00581 00582 #ifdef RIPPER 00583 static void ripper_compile_error(struct parser_params*, const char *fmt, ...); 00584 # define rb_compile_error ripper_compile_error 00585 # define compile_error ripper_compile_error 00586 # define PARSER_ARG parser, 00587 #else 00588 # define rb_compile_error rb_compile_error_with_enc 00589 # define compile_error parser->nerr++,rb_compile_error_with_enc 00590 # define PARSER_ARG ruby_sourcefile, ruby_sourceline, current_enc, 00591 #endif 00592 00593 /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150, 00594 for instance). This is too low for Ruby to parse some files, such as 00595 date/format.rb, therefore bump the value up to at least Bison's default. */ 00596 #ifdef OLD_YACC 00597 #ifndef YYMAXDEPTH 00598 #define YYMAXDEPTH 10000 00599 #endif 00600 #endif 00601 00602 #ifndef RIPPER 00603 static void token_info_push(struct parser_params*, const char *token); 00604 static void token_info_pop(struct parser_params*, const char *token); 00605 #define token_info_push(token) (RTEST(ruby_verbose) ? token_info_push(parser, (token)) : (void)0) 00606 #define token_info_pop(token) (RTEST(ruby_verbose) ? token_info_pop(parser, (token)) : (void)0) 00607 #else 00608 #define token_info_push(token) /* nothing */ 00609 #define token_info_pop(token) /* nothing */ 00610 #endif 00611 %} 00612 00613 %pure_parser 00614 %parse-param {struct parser_params *parser} 00615 00616 %union { 00617 VALUE val; 00618 NODE *node; 00619 ID id; 00620 int num; 00621 const struct vtable *vars; 00622 } 00623 00624 /*%%%*/ 00625 %token 00626 /*% 00627 %token <val> 00628 %*/ 00629 keyword_class 00630 keyword_module 00631 keyword_def 00632 keyword_undef 00633 keyword_begin 00634 keyword_rescue 00635 keyword_ensure 00636 keyword_end 00637 keyword_if 00638 keyword_unless 00639 keyword_then 00640 keyword_elsif 00641 keyword_else 00642 keyword_case 00643 keyword_when 00644 keyword_while 00645 keyword_until 00646 keyword_for 00647 keyword_break 00648 keyword_next 00649 keyword_redo 00650 keyword_retry 00651 keyword_in 00652 keyword_do 00653 keyword_do_cond 00654 keyword_do_block 00655 keyword_do_LAMBDA 00656 keyword_return 00657 keyword_yield 00658 keyword_super 00659 keyword_self 00660 keyword_nil 00661 keyword_true 00662 keyword_false 00663 keyword_and 00664 keyword_or 00665 keyword_not 00666 modifier_if 00667 modifier_unless 00668 modifier_while 00669 modifier_until 00670 modifier_rescue 00671 keyword_alias 00672 keyword_defined 00673 keyword_BEGIN 00674 keyword_END 00675 keyword__LINE__ 00676 keyword__FILE__ 00677 keyword__ENCODING__ 00678 00679 %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL 00680 %token <node> tINTEGER tFLOAT tSTRING_CONTENT tCHAR 00681 %token <node> tNTH_REF tBACK_REF 00682 %token <num> tREGEXP_END 00683 00684 %type <node> singleton strings string string1 xstring regexp 00685 %type <node> string_contents xstring_contents regexp_contents string_content 00686 %type <node> words qwords word_list qword_list word 00687 %type <node> literal numeric dsym cpath 00688 %type <node> top_compstmt top_stmts top_stmt 00689 %type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call 00690 %type <node> expr_value arg_value primary_value 00691 %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure 00692 %type <node> args call_args opt_call_args 00693 %type <node> paren_args opt_paren_args 00694 %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs 00695 %type <node> command_asgn mrhs superclass block_call block_command 00696 %type <node> f_block_optarg f_block_opt 00697 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs 00698 %type <node> assoc_list assocs assoc undef_list backref string_dvar for_var 00699 %type <node> block_param opt_block_param block_param_def f_opt 00700 %type <node> bv_decls opt_bv_decl bvar 00701 %type <node> lambda f_larglist lambda_body 00702 %type <node> brace_block cmd_brace_block do_block lhs none fitem 00703 %type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner 00704 %type <id> fsym keyword_variable user_variable sym symbol operation operation2 operation3 00705 %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg 00706 /*%%%*/ 00707 /*% 00708 %type <val> program reswords then do dot_or_colon 00709 %*/ 00710 %token tUPLUS /* unary+ */ 00711 %token tUMINUS /* unary- */ 00712 %token tPOW /* ** */ 00713 %token tCMP /* <=> */ 00714 %token tEQ /* == */ 00715 %token tEQQ /* === */ 00716 %token tNEQ /* != */ 00717 %token tGEQ /* >= */ 00718 %token tLEQ /* <= */ 00719 %token tANDOP tOROP /* && and || */ 00720 %token tMATCH tNMATCH /* =~ and !~ */ 00721 %token tDOT2 tDOT3 /* .. and ... */ 00722 %token tAREF tASET /* [] and []= */ 00723 %token tLSHFT tRSHFT /* << and >> */ 00724 %token tCOLON2 /* :: */ 00725 %token tCOLON3 /* :: at EXPR_BEG */ 00726 %token <id> tOP_ASGN /* +=, -= etc. */ 00727 %token tASSOC /* => */ 00728 %token tLPAREN /* ( */ 00729 %token tLPAREN_ARG /* ( */ 00730 %token tRPAREN /* ) */ 00731 %token tLBRACK /* [ */ 00732 %token tLBRACE /* { */ 00733 %token tLBRACE_ARG /* { */ 00734 %token tSTAR /* * */ 00735 %token tAMPER /* & */ 00736 %token tLAMBDA /* -> */ 00737 %token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG 00738 %token tSTRING_DBEG tSTRING_DVAR tSTRING_END tLAMBEG 00739 00740 /* 00741 * precedence table 00742 */ 00743 00744 %nonassoc tLOWEST 00745 %nonassoc tLBRACE_ARG 00746 00747 %nonassoc modifier_if modifier_unless modifier_while modifier_until 00748 %left keyword_or keyword_and 00749 %right keyword_not 00750 %nonassoc keyword_defined 00751 %right '=' tOP_ASGN 00752 %left modifier_rescue 00753 %right '?' ':' 00754 %nonassoc tDOT2 tDOT3 00755 %left tOROP 00756 %left tANDOP 00757 %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH 00758 %left '>' tGEQ '<' tLEQ 00759 %left '|' '^' 00760 %left '&' 00761 %left tLSHFT tRSHFT 00762 %left '+' '-' 00763 %left '*' '/' '%' 00764 %right tUMINUS_NUM tUMINUS 00765 %right tPOW 00766 %right '!' '~' tUPLUS 00767 00768 %nonassoc idNULL 00769 %nonassoc idRespond_to 00770 %nonassoc idIFUNC 00771 %nonassoc idCFUNC 00772 %nonassoc id_core_set_method_alias 00773 %nonassoc id_core_set_variable_alias 00774 %nonassoc id_core_undef_method 00775 %nonassoc id_core_define_method 00776 %nonassoc id_core_define_singleton_method 00777 %nonassoc id_core_set_postexe 00778 00779 %token tLAST_TOKEN 00780 00781 %% 00782 program : { 00783 lex_state = EXPR_BEG; 00784 /*%%%*/ 00785 local_push(compile_for_eval || rb_parse_in_main()); 00786 /*% 00787 local_push(0); 00788 %*/ 00789 } 00790 top_compstmt 00791 { 00792 /*%%%*/ 00793 if ($2 && !compile_for_eval) { 00794 /* last expression should not be void */ 00795 if (nd_type($2) != NODE_BLOCK) void_expr($2); 00796 else { 00797 NODE *node = $2; 00798 while (node->nd_next) { 00799 node = node->nd_next; 00800 } 00801 void_expr(node->nd_head); 00802 } 00803 } 00804 ruby_eval_tree = NEW_SCOPE(0, block_append(ruby_eval_tree, $2)); 00805 /*% 00806 $$ = $2; 00807 parser->result = dispatch1(program, $$); 00808 %*/ 00809 local_pop(); 00810 } 00811 ; 00812 00813 top_compstmt : top_stmts opt_terms 00814 { 00815 /*%%%*/ 00816 void_stmts($1); 00817 fixup_nodes(&deferred_nodes); 00818 /*% 00819 %*/ 00820 $$ = $1; 00821 } 00822 ; 00823 00824 top_stmts : none 00825 { 00826 /*%%%*/ 00827 $$ = NEW_BEGIN(0); 00828 /*% 00829 $$ = dispatch2(stmts_add, dispatch0(stmts_new), 00830 dispatch0(void_stmt)); 00831 %*/ 00832 } 00833 | top_stmt 00834 { 00835 /*%%%*/ 00836 $$ = newline_node($1); 00837 /*% 00838 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1); 00839 %*/ 00840 } 00841 | top_stmts terms top_stmt 00842 { 00843 /*%%%*/ 00844 $$ = block_append($1, newline_node($3)); 00845 /*% 00846 $$ = dispatch2(stmts_add, $1, $3); 00847 %*/ 00848 } 00849 | error top_stmt 00850 { 00851 $$ = remove_begin($2); 00852 } 00853 ; 00854 00855 top_stmt : stmt 00856 | keyword_BEGIN 00857 { 00858 if (in_def || in_single) { 00859 yyerror("BEGIN in method"); 00860 } 00861 /*%%%*/ 00862 /* local_push(0); */ 00863 /*% 00864 %*/ 00865 } 00866 '{' top_compstmt '}' 00867 { 00868 /*%%%*/ 00869 ruby_eval_tree_begin = block_append(ruby_eval_tree_begin, 00870 $4); 00871 /* NEW_PREEXE($4)); */ 00872 /* local_pop(); */ 00873 $$ = NEW_BEGIN(0); 00874 /*% 00875 $$ = dispatch1(BEGIN, $4); 00876 %*/ 00877 } 00878 ; 00879 00880 bodystmt : compstmt 00881 opt_rescue 00882 opt_else 00883 opt_ensure 00884 { 00885 /*%%%*/ 00886 $$ = $1; 00887 if ($2) { 00888 $$ = NEW_RESCUE($1, $2, $3); 00889 } 00890 else if ($3) { 00891 rb_warn0("else without rescue is useless"); 00892 $$ = block_append($$, $3); 00893 } 00894 if ($4) { 00895 if ($$) { 00896 $$ = NEW_ENSURE($$, $4); 00897 } 00898 else { 00899 $$ = block_append($4, NEW_NIL()); 00900 } 00901 } 00902 fixpos($$, $1); 00903 /*% 00904 $$ = dispatch4(bodystmt, 00905 escape_Qundef($1), 00906 escape_Qundef($2), 00907 escape_Qundef($3), 00908 escape_Qundef($4)); 00909 %*/ 00910 } 00911 ; 00912 00913 compstmt : stmts opt_terms 00914 { 00915 /*%%%*/ 00916 void_stmts($1); 00917 fixup_nodes(&deferred_nodes); 00918 /*% 00919 %*/ 00920 $$ = $1; 00921 } 00922 ; 00923 00924 stmts : none 00925 { 00926 /*%%%*/ 00927 $$ = NEW_BEGIN(0); 00928 /*% 00929 $$ = dispatch2(stmts_add, dispatch0(stmts_new), 00930 dispatch0(void_stmt)); 00931 %*/ 00932 } 00933 | stmt 00934 { 00935 /*%%%*/ 00936 $$ = newline_node($1); 00937 /*% 00938 $$ = dispatch2(stmts_add, dispatch0(stmts_new), $1); 00939 %*/ 00940 } 00941 | stmts terms stmt 00942 { 00943 /*%%%*/ 00944 $$ = block_append($1, newline_node($3)); 00945 /*% 00946 $$ = dispatch2(stmts_add, $1, $3); 00947 %*/ 00948 } 00949 | error stmt 00950 { 00951 $$ = remove_begin($2); 00952 } 00953 ; 00954 00955 stmt : keyword_alias fitem {lex_state = EXPR_FNAME;} fitem 00956 { 00957 /*%%%*/ 00958 $$ = NEW_ALIAS($2, $4); 00959 /*% 00960 $$ = dispatch2(alias, $2, $4); 00961 %*/ 00962 } 00963 | keyword_alias tGVAR tGVAR 00964 { 00965 /*%%%*/ 00966 $$ = NEW_VALIAS($2, $3); 00967 /*% 00968 $$ = dispatch2(var_alias, $2, $3); 00969 %*/ 00970 } 00971 | keyword_alias tGVAR tBACK_REF 00972 { 00973 /*%%%*/ 00974 char buf[2]; 00975 buf[0] = '$'; 00976 buf[1] = (char)$3->nd_nth; 00977 $$ = NEW_VALIAS($2, rb_intern2(buf, 2)); 00978 /*% 00979 $$ = dispatch2(var_alias, $2, $3); 00980 %*/ 00981 } 00982 | keyword_alias tGVAR tNTH_REF 00983 { 00984 /*%%%*/ 00985 yyerror("can't make alias for the number variables"); 00986 $$ = NEW_BEGIN(0); 00987 /*% 00988 $$ = dispatch2(var_alias, $2, $3); 00989 $$ = dispatch1(alias_error, $$); 00990 %*/ 00991 } 00992 | keyword_undef undef_list 00993 { 00994 /*%%%*/ 00995 $$ = $2; 00996 /*% 00997 $$ = dispatch1(undef, $2); 00998 %*/ 00999 } 01000 | stmt modifier_if expr_value 01001 { 01002 /*%%%*/ 01003 $$ = NEW_IF(cond($3), remove_begin($1), 0); 01004 fixpos($$, $3); 01005 /*% 01006 $$ = dispatch2(if_mod, $3, $1); 01007 %*/ 01008 } 01009 | stmt modifier_unless expr_value 01010 { 01011 /*%%%*/ 01012 $$ = NEW_UNLESS(cond($3), remove_begin($1), 0); 01013 fixpos($$, $3); 01014 /*% 01015 $$ = dispatch2(unless_mod, $3, $1); 01016 %*/ 01017 } 01018 | stmt modifier_while expr_value 01019 { 01020 /*%%%*/ 01021 if ($1 && nd_type($1) == NODE_BEGIN) { 01022 $$ = NEW_WHILE(cond($3), $1->nd_body, 0); 01023 } 01024 else { 01025 $$ = NEW_WHILE(cond($3), $1, 1); 01026 } 01027 /*% 01028 $$ = dispatch2(while_mod, $3, $1); 01029 %*/ 01030 } 01031 | stmt modifier_until expr_value 01032 { 01033 /*%%%*/ 01034 if ($1 && nd_type($1) == NODE_BEGIN) { 01035 $$ = NEW_UNTIL(cond($3), $1->nd_body, 0); 01036 } 01037 else { 01038 $$ = NEW_UNTIL(cond($3), $1, 1); 01039 } 01040 /*% 01041 $$ = dispatch2(until_mod, $3, $1); 01042 %*/ 01043 } 01044 | stmt modifier_rescue stmt 01045 { 01046 /*%%%*/ 01047 NODE *resq = NEW_RESBODY(0, remove_begin($3), 0); 01048 $$ = NEW_RESCUE(remove_begin($1), resq, 0); 01049 /*% 01050 $$ = dispatch2(rescue_mod, $1, $3); 01051 %*/ 01052 } 01053 | keyword_END '{' compstmt '}' 01054 { 01055 if (in_def || in_single) { 01056 rb_warn0("END in method; use at_exit"); 01057 } 01058 /*%%%*/ 01059 $$ = NEW_POSTEXE(NEW_NODE( 01060 NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */)); 01061 /*% 01062 $$ = dispatch1(END, $3); 01063 %*/ 01064 } 01065 | command_asgn 01066 | mlhs '=' command_call 01067 { 01068 /*%%%*/ 01069 value_expr($3); 01070 $1->nd_value = $3; 01071 $$ = $1; 01072 /*% 01073 $$ = dispatch2(massign, $1, $3); 01074 %*/ 01075 } 01076 | var_lhs tOP_ASGN command_call 01077 { 01078 /*%%%*/ 01079 value_expr($3); 01080 if ($1) { 01081 ID vid = $1->nd_vid; 01082 if ($2 == tOROP) { 01083 $1->nd_value = $3; 01084 $$ = NEW_OP_ASGN_OR(gettable(vid), $1); 01085 if (is_asgn_or_id(vid)) { 01086 $$->nd_aid = vid; 01087 } 01088 } 01089 else if ($2 == tANDOP) { 01090 $1->nd_value = $3; 01091 $$ = NEW_OP_ASGN_AND(gettable(vid), $1); 01092 } 01093 else { 01094 $$ = $1; 01095 $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3)); 01096 } 01097 } 01098 else { 01099 $$ = NEW_BEGIN(0); 01100 } 01101 /*% 01102 $$ = dispatch3(opassign, $1, $2, $3); 01103 %*/ 01104 } 01105 | primary_value '[' opt_call_args rbracket tOP_ASGN command_call 01106 { 01107 /*%%%*/ 01108 NODE *args; 01109 01110 value_expr($6); 01111 if (!$3) $3 = NEW_ZARRAY(); 01112 args = arg_concat($3, $6); 01113 if ($5 == tOROP) { 01114 $5 = 0; 01115 } 01116 else if ($5 == tANDOP) { 01117 $5 = 1; 01118 } 01119 $$ = NEW_OP_ASGN1($1, $5, args); 01120 fixpos($$, $1); 01121 /*% 01122 $$ = dispatch2(aref_field, $1, escape_Qundef($3)); 01123 $$ = dispatch3(opassign, $$, $5, $6); 01124 %*/ 01125 } 01126 | primary_value '.' tIDENTIFIER tOP_ASGN command_call 01127 { 01128 /*%%%*/ 01129 value_expr($5); 01130 if ($4 == tOROP) { 01131 $4 = 0; 01132 } 01133 else if ($4 == tANDOP) { 01134 $4 = 1; 01135 } 01136 $$ = NEW_OP_ASGN2($1, $3, $4, $5); 01137 fixpos($$, $1); 01138 /*% 01139 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3); 01140 $$ = dispatch3(opassign, $$, $4, $5); 01141 %*/ 01142 } 01143 | primary_value '.' tCONSTANT tOP_ASGN command_call 01144 { 01145 /*%%%*/ 01146 value_expr($5); 01147 if ($4 == tOROP) { 01148 $4 = 0; 01149 } 01150 else if ($4 == tANDOP) { 01151 $4 = 1; 01152 } 01153 $$ = NEW_OP_ASGN2($1, $3, $4, $5); 01154 fixpos($$, $1); 01155 /*% 01156 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3); 01157 $$ = dispatch3(opassign, $$, $4, $5); 01158 %*/ 01159 } 01160 | primary_value tCOLON2 tCONSTANT tOP_ASGN command_call 01161 { 01162 /*%%%*/ 01163 yyerror("constant re-assignment"); 01164 $$ = 0; 01165 /*% 01166 $$ = dispatch2(const_path_field, $1, $3); 01167 $$ = dispatch3(opassign, $$, $4, $5); 01168 $$ = dispatch1(assign_error, $$); 01169 %*/ 01170 } 01171 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call 01172 { 01173 /*%%%*/ 01174 value_expr($5); 01175 if ($4 == tOROP) { 01176 $4 = 0; 01177 } 01178 else if ($4 == tANDOP) { 01179 $4 = 1; 01180 } 01181 $$ = NEW_OP_ASGN2($1, $3, $4, $5); 01182 fixpos($$, $1); 01183 /*% 01184 $$ = dispatch3(field, $1, ripper_intern("::"), $3); 01185 $$ = dispatch3(opassign, $$, $4, $5); 01186 %*/ 01187 } 01188 | backref tOP_ASGN command_call 01189 { 01190 /*%%%*/ 01191 rb_backref_error($1); 01192 $$ = NEW_BEGIN(0); 01193 /*% 01194 $$ = dispatch2(assign, dispatch1(var_field, $1), $3); 01195 $$ = dispatch1(assign_error, $$); 01196 %*/ 01197 } 01198 | lhs '=' mrhs 01199 { 01200 /*%%%*/ 01201 value_expr($3); 01202 $$ = node_assign($1, $3); 01203 /*% 01204 $$ = dispatch2(assign, $1, $3); 01205 %*/ 01206 } 01207 | mlhs '=' arg_value 01208 { 01209 /*%%%*/ 01210 $1->nd_value = $3; 01211 $$ = $1; 01212 /*% 01213 $$ = dispatch2(massign, $1, $3); 01214 %*/ 01215 } 01216 | mlhs '=' mrhs 01217 { 01218 /*%%%*/ 01219 $1->nd_value = $3; 01220 $$ = $1; 01221 /*% 01222 $$ = dispatch2(massign, $1, $3); 01223 %*/ 01224 } 01225 | expr 01226 ; 01227 01228 command_asgn : lhs '=' command_call 01229 { 01230 /*%%%*/ 01231 value_expr($3); 01232 $$ = node_assign($1, $3); 01233 /*% 01234 $$ = dispatch2(assign, $1, $3); 01235 %*/ 01236 } 01237 | lhs '=' command_asgn 01238 { 01239 /*%%%*/ 01240 value_expr($3); 01241 $$ = node_assign($1, $3); 01242 /*% 01243 $$ = dispatch2(assign, $1, $3); 01244 %*/ 01245 } 01246 ; 01247 01248 01249 expr : command_call 01250 | expr keyword_and expr 01251 { 01252 /*%%%*/ 01253 $$ = logop(NODE_AND, $1, $3); 01254 /*% 01255 $$ = dispatch3(binary, $1, ripper_intern("and"), $3); 01256 %*/ 01257 } 01258 | expr keyword_or expr 01259 { 01260 /*%%%*/ 01261 $$ = logop(NODE_OR, $1, $3); 01262 /*% 01263 $$ = dispatch3(binary, $1, ripper_intern("or"), $3); 01264 %*/ 01265 } 01266 | keyword_not opt_nl expr 01267 { 01268 /*%%%*/ 01269 $$ = call_uni_op(cond($3), '!'); 01270 /*% 01271 $$ = dispatch2(unary, ripper_intern("not"), $3); 01272 %*/ 01273 } 01274 | '!' command_call 01275 { 01276 /*%%%*/ 01277 $$ = call_uni_op(cond($2), '!'); 01278 /*% 01279 $$ = dispatch2(unary, ripper_id2sym('!'), $2); 01280 %*/ 01281 } 01282 | arg 01283 ; 01284 01285 expr_value : expr 01286 { 01287 /*%%%*/ 01288 value_expr($1); 01289 $$ = $1; 01290 if (!$$) $$ = NEW_NIL(); 01291 /*% 01292 $$ = $1; 01293 %*/ 01294 } 01295 ; 01296 01297 command_call : command 01298 | block_command 01299 ; 01300 01301 block_command : block_call 01302 | block_call '.' operation2 command_args 01303 { 01304 /*%%%*/ 01305 $$ = NEW_CALL($1, $3, $4); 01306 /*% 01307 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3); 01308 $$ = method_arg($$, $4); 01309 %*/ 01310 } 01311 | block_call tCOLON2 operation2 command_args 01312 { 01313 /*%%%*/ 01314 $$ = NEW_CALL($1, $3, $4); 01315 /*% 01316 $$ = dispatch3(call, $1, ripper_intern("::"), $3); 01317 $$ = method_arg($$, $4); 01318 %*/ 01319 } 01320 ; 01321 01322 cmd_brace_block : tLBRACE_ARG 01323 { 01324 $<vars>1 = dyna_push(); 01325 /*%%%*/ 01326 $<num>$ = ruby_sourceline; 01327 /*% 01328 %*/ 01329 } 01330 opt_block_param 01331 compstmt 01332 '}' 01333 { 01334 /*%%%*/ 01335 $$ = NEW_ITER($3,$4); 01336 nd_set_line($$, $<num>2); 01337 /*% 01338 $$ = dispatch2(brace_block, escape_Qundef($3), $4); 01339 %*/ 01340 dyna_pop($<vars>1); 01341 } 01342 ; 01343 01344 command : operation command_args %prec tLOWEST 01345 { 01346 /*%%%*/ 01347 $$ = NEW_FCALL($1, $2); 01348 fixpos($$, $2); 01349 /*% 01350 $$ = dispatch2(command, $1, $2); 01351 %*/ 01352 } 01353 | operation command_args cmd_brace_block 01354 { 01355 /*%%%*/ 01356 block_dup_check($2,$3); 01357 $3->nd_iter = NEW_FCALL($1, $2); 01358 $$ = $3; 01359 fixpos($$, $2); 01360 /*% 01361 $$ = dispatch2(command, $1, $2); 01362 $$ = method_add_block($$, $3); 01363 %*/ 01364 } 01365 | primary_value '.' operation2 command_args %prec tLOWEST 01366 { 01367 /*%%%*/ 01368 $$ = NEW_CALL($1, $3, $4); 01369 fixpos($$, $1); 01370 /*% 01371 $$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4); 01372 %*/ 01373 } 01374 | primary_value '.' operation2 command_args cmd_brace_block 01375 { 01376 /*%%%*/ 01377 block_dup_check($4,$5); 01378 $5->nd_iter = NEW_CALL($1, $3, $4); 01379 $$ = $5; 01380 fixpos($$, $1); 01381 /*% 01382 $$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4); 01383 $$ = method_add_block($$, $5); 01384 %*/ 01385 } 01386 | primary_value tCOLON2 operation2 command_args %prec tLOWEST 01387 { 01388 /*%%%*/ 01389 $$ = NEW_CALL($1, $3, $4); 01390 fixpos($$, $1); 01391 /*% 01392 $$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4); 01393 %*/ 01394 } 01395 | primary_value tCOLON2 operation2 command_args cmd_brace_block 01396 { 01397 /*%%%*/ 01398 block_dup_check($4,$5); 01399 $5->nd_iter = NEW_CALL($1, $3, $4); 01400 $$ = $5; 01401 fixpos($$, $1); 01402 /*% 01403 $$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4); 01404 $$ = method_add_block($$, $5); 01405 %*/ 01406 } 01407 | keyword_super command_args 01408 { 01409 /*%%%*/ 01410 $$ = NEW_SUPER($2); 01411 fixpos($$, $2); 01412 /*% 01413 $$ = dispatch1(super, $2); 01414 %*/ 01415 } 01416 | keyword_yield command_args 01417 { 01418 /*%%%*/ 01419 $$ = new_yield($2); 01420 fixpos($$, $2); 01421 /*% 01422 $$ = dispatch1(yield, $2); 01423 %*/ 01424 } 01425 | keyword_return call_args 01426 { 01427 /*%%%*/ 01428 $$ = NEW_RETURN(ret_args($2)); 01429 /*% 01430 $$ = dispatch1(return, $2); 01431 %*/ 01432 } 01433 | keyword_break call_args 01434 { 01435 /*%%%*/ 01436 $$ = NEW_BREAK(ret_args($2)); 01437 /*% 01438 $$ = dispatch1(break, $2); 01439 %*/ 01440 } 01441 | keyword_next call_args 01442 { 01443 /*%%%*/ 01444 $$ = NEW_NEXT(ret_args($2)); 01445 /*% 01446 $$ = dispatch1(next, $2); 01447 %*/ 01448 } 01449 ; 01450 01451 mlhs : mlhs_basic 01452 | tLPAREN mlhs_inner rparen 01453 { 01454 /*%%%*/ 01455 $$ = $2; 01456 /*% 01457 $$ = dispatch1(mlhs_paren, $2); 01458 %*/ 01459 } 01460 ; 01461 01462 mlhs_inner : mlhs_basic 01463 | tLPAREN mlhs_inner rparen 01464 { 01465 /*%%%*/ 01466 $$ = NEW_MASGN(NEW_LIST($2), 0); 01467 /*% 01468 $$ = dispatch1(mlhs_paren, $2); 01469 %*/ 01470 } 01471 ; 01472 01473 mlhs_basic : mlhs_head 01474 { 01475 /*%%%*/ 01476 $$ = NEW_MASGN($1, 0); 01477 /*% 01478 $$ = $1; 01479 %*/ 01480 } 01481 | mlhs_head mlhs_item 01482 { 01483 /*%%%*/ 01484 $$ = NEW_MASGN(list_append($1,$2), 0); 01485 /*% 01486 $$ = mlhs_add($1, $2); 01487 %*/ 01488 } 01489 | mlhs_head tSTAR mlhs_node 01490 { 01491 /*%%%*/ 01492 $$ = NEW_MASGN($1, $3); 01493 /*% 01494 $$ = mlhs_add_star($1, $3); 01495 %*/ 01496 } 01497 | mlhs_head tSTAR mlhs_node ',' mlhs_post 01498 { 01499 /*%%%*/ 01500 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5)); 01501 /*% 01502 $1 = mlhs_add_star($1, $3); 01503 $$ = mlhs_add($1, $5); 01504 %*/ 01505 } 01506 | mlhs_head tSTAR 01507 { 01508 /*%%%*/ 01509 $$ = NEW_MASGN($1, -1); 01510 /*% 01511 $$ = mlhs_add_star($1, Qnil); 01512 %*/ 01513 } 01514 | mlhs_head tSTAR ',' mlhs_post 01515 { 01516 /*%%%*/ 01517 $$ = NEW_MASGN($1, NEW_POSTARG(-1, $4)); 01518 /*% 01519 $1 = mlhs_add_star($1, Qnil); 01520 $$ = mlhs_add($1, $4); 01521 %*/ 01522 } 01523 | tSTAR mlhs_node 01524 { 01525 /*%%%*/ 01526 $$ = NEW_MASGN(0, $2); 01527 /*% 01528 $$ = mlhs_add_star(mlhs_new(), $2); 01529 %*/ 01530 } 01531 | tSTAR mlhs_node ',' mlhs_post 01532 { 01533 /*%%%*/ 01534 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4)); 01535 /*% 01536 $2 = mlhs_add_star(mlhs_new(), $2); 01537 $$ = mlhs_add($2, $4); 01538 %*/ 01539 } 01540 | tSTAR 01541 { 01542 /*%%%*/ 01543 $$ = NEW_MASGN(0, -1); 01544 /*% 01545 $$ = mlhs_add_star(mlhs_new(), Qnil); 01546 %*/ 01547 } 01548 | tSTAR ',' mlhs_post 01549 { 01550 /*%%%*/ 01551 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3)); 01552 /*% 01553 $$ = mlhs_add_star(mlhs_new(), Qnil); 01554 $$ = mlhs_add($$, $3); 01555 %*/ 01556 } 01557 ; 01558 01559 mlhs_item : mlhs_node 01560 | tLPAREN mlhs_inner rparen 01561 { 01562 /*%%%*/ 01563 $$ = $2; 01564 /*% 01565 $$ = dispatch1(mlhs_paren, $2); 01566 %*/ 01567 } 01568 ; 01569 01570 mlhs_head : mlhs_item ',' 01571 { 01572 /*%%%*/ 01573 $$ = NEW_LIST($1); 01574 /*% 01575 $$ = mlhs_add(mlhs_new(), $1); 01576 %*/ 01577 } 01578 | mlhs_head mlhs_item ',' 01579 { 01580 /*%%%*/ 01581 $$ = list_append($1, $2); 01582 /*% 01583 $$ = mlhs_add($1, $2); 01584 %*/ 01585 } 01586 ; 01587 01588 mlhs_post : mlhs_item 01589 { 01590 /*%%%*/ 01591 $$ = NEW_LIST($1); 01592 /*% 01593 $$ = mlhs_add(mlhs_new(), $1); 01594 %*/ 01595 } 01596 | mlhs_post ',' mlhs_item 01597 { 01598 /*%%%*/ 01599 $$ = list_append($1, $3); 01600 /*% 01601 $$ = mlhs_add($1, $3); 01602 %*/ 01603 } 01604 ; 01605 01606 mlhs_node : user_variable 01607 { 01608 $$ = assignable($1, 0); 01609 } 01610 | keyword_variable 01611 { 01612 $$ = assignable($1, 0); 01613 } 01614 | primary_value '[' opt_call_args rbracket 01615 { 01616 /*%%%*/ 01617 $$ = aryset($1, $3); 01618 /*% 01619 $$ = dispatch2(aref_field, $1, escape_Qundef($3)); 01620 %*/ 01621 } 01622 | primary_value '.' tIDENTIFIER 01623 { 01624 /*%%%*/ 01625 $$ = attrset($1, $3); 01626 /*% 01627 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3); 01628 %*/ 01629 } 01630 | primary_value tCOLON2 tIDENTIFIER 01631 { 01632 /*%%%*/ 01633 $$ = attrset($1, $3); 01634 /*% 01635 $$ = dispatch2(const_path_field, $1, $3); 01636 %*/ 01637 } 01638 | primary_value '.' tCONSTANT 01639 { 01640 /*%%%*/ 01641 $$ = attrset($1, $3); 01642 /*% 01643 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3); 01644 %*/ 01645 } 01646 | primary_value tCOLON2 tCONSTANT 01647 { 01648 /*%%%*/ 01649 if (in_def || in_single) 01650 yyerror("dynamic constant assignment"); 01651 $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3)); 01652 /*% 01653 if (in_def || in_single) 01654 yyerror("dynamic constant assignment"); 01655 $$ = dispatch2(const_path_field, $1, $3); 01656 %*/ 01657 } 01658 | tCOLON3 tCONSTANT 01659 { 01660 /*%%%*/ 01661 if (in_def || in_single) 01662 yyerror("dynamic constant assignment"); 01663 $$ = NEW_CDECL(0, 0, NEW_COLON3($2)); 01664 /*% 01665 $$ = dispatch1(top_const_field, $2); 01666 %*/ 01667 } 01668 | backref 01669 { 01670 /*%%%*/ 01671 rb_backref_error($1); 01672 $$ = NEW_BEGIN(0); 01673 /*% 01674 $$ = dispatch1(var_field, $1); 01675 $$ = dispatch1(assign_error, $$); 01676 %*/ 01677 } 01678 ; 01679 01680 lhs : user_variable 01681 { 01682 $$ = assignable($1, 0); 01683 /*%%%*/ 01684 if (!$$) $$ = NEW_BEGIN(0); 01685 /*% 01686 $$ = dispatch1(var_field, $$); 01687 %*/ 01688 } 01689 | keyword_variable 01690 { 01691 $$ = assignable($1, 0); 01692 /*%%%*/ 01693 if (!$$) $$ = NEW_BEGIN(0); 01694 /*% 01695 $$ = dispatch1(var_field, $$); 01696 %*/ 01697 } 01698 | primary_value '[' opt_call_args rbracket 01699 { 01700 /*%%%*/ 01701 $$ = aryset($1, $3); 01702 /*% 01703 $$ = dispatch2(aref_field, $1, escape_Qundef($3)); 01704 %*/ 01705 } 01706 | primary_value '.' tIDENTIFIER 01707 { 01708 /*%%%*/ 01709 $$ = attrset($1, $3); 01710 /*% 01711 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3); 01712 %*/ 01713 } 01714 | primary_value tCOLON2 tIDENTIFIER 01715 { 01716 /*%%%*/ 01717 $$ = attrset($1, $3); 01718 /*% 01719 $$ = dispatch3(field, $1, ripper_intern("::"), $3); 01720 %*/ 01721 } 01722 | primary_value '.' tCONSTANT 01723 { 01724 /*%%%*/ 01725 $$ = attrset($1, $3); 01726 /*% 01727 $$ = dispatch3(field, $1, ripper_id2sym('.'), $3); 01728 %*/ 01729 } 01730 | primary_value tCOLON2 tCONSTANT 01731 { 01732 /*%%%*/ 01733 if (in_def || in_single) 01734 yyerror("dynamic constant assignment"); 01735 $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3)); 01736 /*% 01737 $$ = dispatch2(const_path_field, $1, $3); 01738 if (in_def || in_single) { 01739 $$ = dispatch1(assign_error, $$); 01740 } 01741 %*/ 01742 } 01743 | tCOLON3 tCONSTANT 01744 { 01745 /*%%%*/ 01746 if (in_def || in_single) 01747 yyerror("dynamic constant assignment"); 01748 $$ = NEW_CDECL(0, 0, NEW_COLON3($2)); 01749 /*% 01750 $$ = dispatch1(top_const_field, $2); 01751 if (in_def || in_single) { 01752 $$ = dispatch1(assign_error, $$); 01753 } 01754 %*/ 01755 } 01756 | backref 01757 { 01758 /*%%%*/ 01759 rb_backref_error($1); 01760 $$ = NEW_BEGIN(0); 01761 /*% 01762 $$ = dispatch1(assign_error, $1); 01763 %*/ 01764 } 01765 ; 01766 01767 cname : tIDENTIFIER 01768 { 01769 /*%%%*/ 01770 yyerror("class/module name must be CONSTANT"); 01771 /*% 01772 $$ = dispatch1(class_name_error, $1); 01773 %*/ 01774 } 01775 | tCONSTANT 01776 ; 01777 01778 cpath : tCOLON3 cname 01779 { 01780 /*%%%*/ 01781 $$ = NEW_COLON3($2); 01782 /*% 01783 $$ = dispatch1(top_const_ref, $2); 01784 %*/ 01785 } 01786 | cname 01787 { 01788 /*%%%*/ 01789 $$ = NEW_COLON2(0, $$); 01790 /*% 01791 $$ = dispatch1(const_ref, $1); 01792 %*/ 01793 } 01794 | primary_value tCOLON2 cname 01795 { 01796 /*%%%*/ 01797 $$ = NEW_COLON2($1, $3); 01798 /*% 01799 $$ = dispatch2(const_path_ref, $1, $3); 01800 %*/ 01801 } 01802 ; 01803 01804 fname : tIDENTIFIER 01805 | tCONSTANT 01806 | tFID 01807 | op 01808 { 01809 lex_state = EXPR_ENDFN; 01810 $$ = $1; 01811 } 01812 | reswords 01813 { 01814 lex_state = EXPR_ENDFN; 01815 /*%%%*/ 01816 $$ = $<id>1; 01817 /*% 01818 $$ = $1; 01819 %*/ 01820 } 01821 ; 01822 01823 fsym : fname 01824 | symbol 01825 ; 01826 01827 fitem : fsym 01828 { 01829 /*%%%*/ 01830 $$ = NEW_LIT(ID2SYM($1)); 01831 /*% 01832 $$ = dispatch1(symbol_literal, $1); 01833 %*/ 01834 } 01835 | dsym 01836 ; 01837 01838 undef_list : fitem 01839 { 01840 /*%%%*/ 01841 $$ = NEW_UNDEF($1); 01842 /*% 01843 $$ = rb_ary_new3(1, $1); 01844 %*/ 01845 } 01846 | undef_list ',' {lex_state = EXPR_FNAME;} fitem 01847 { 01848 /*%%%*/ 01849 $$ = block_append($1, NEW_UNDEF($4)); 01850 /*% 01851 rb_ary_push($1, $4); 01852 %*/ 01853 } 01854 ; 01855 01856 op : '|' { ifndef_ripper($$ = '|'); } 01857 | '^' { ifndef_ripper($$ = '^'); } 01858 | '&' { ifndef_ripper($$ = '&'); } 01859 | tCMP { ifndef_ripper($$ = tCMP); } 01860 | tEQ { ifndef_ripper($$ = tEQ); } 01861 | tEQQ { ifndef_ripper($$ = tEQQ); } 01862 | tMATCH { ifndef_ripper($$ = tMATCH); } 01863 | tNMATCH { ifndef_ripper($$ = tNMATCH); } 01864 | '>' { ifndef_ripper($$ = '>'); } 01865 | tGEQ { ifndef_ripper($$ = tGEQ); } 01866 | '<' { ifndef_ripper($$ = '<'); } 01867 | tLEQ { ifndef_ripper($$ = tLEQ); } 01868 | tNEQ { ifndef_ripper($$ = tNEQ); } 01869 | tLSHFT { ifndef_ripper($$ = tLSHFT); } 01870 | tRSHFT { ifndef_ripper($$ = tRSHFT); } 01871 | '+' { ifndef_ripper($$ = '+'); } 01872 | '-' { ifndef_ripper($$ = '-'); } 01873 | '*' { ifndef_ripper($$ = '*'); } 01874 | tSTAR { ifndef_ripper($$ = '*'); } 01875 | '/' { ifndef_ripper($$ = '/'); } 01876 | '%' { ifndef_ripper($$ = '%'); } 01877 | tPOW { ifndef_ripper($$ = tPOW); } 01878 | '!' { ifndef_ripper($$ = '!'); } 01879 | '~' { ifndef_ripper($$ = '~'); } 01880 | tUPLUS { ifndef_ripper($$ = tUPLUS); } 01881 | tUMINUS { ifndef_ripper($$ = tUMINUS); } 01882 | tAREF { ifndef_ripper($$ = tAREF); } 01883 | tASET { ifndef_ripper($$ = tASET); } 01884 | '`' { ifndef_ripper($$ = '`'); } 01885 ; 01886 01887 reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ 01888 | keyword_BEGIN | keyword_END 01889 | keyword_alias | keyword_and | keyword_begin 01890 | keyword_break | keyword_case | keyword_class | keyword_def 01891 | keyword_defined | keyword_do | keyword_else | keyword_elsif 01892 | keyword_end | keyword_ensure | keyword_false 01893 | keyword_for | keyword_in | keyword_module | keyword_next 01894 | keyword_nil | keyword_not | keyword_or | keyword_redo 01895 | keyword_rescue | keyword_retry | keyword_return | keyword_self 01896 | keyword_super | keyword_then | keyword_true | keyword_undef 01897 | keyword_when | keyword_yield | keyword_if | keyword_unless 01898 | keyword_while | keyword_until 01899 ; 01900 01901 arg : lhs '=' arg 01902 { 01903 /*%%%*/ 01904 value_expr($3); 01905 $$ = node_assign($1, $3); 01906 /*% 01907 $$ = dispatch2(assign, $1, $3); 01908 %*/ 01909 } 01910 | lhs '=' arg modifier_rescue arg 01911 { 01912 /*%%%*/ 01913 value_expr($3); 01914 $3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0); 01915 $$ = node_assign($1, $3); 01916 /*% 01917 $$ = dispatch2(assign, $1, dispatch2(rescue_mod, $3, $5)); 01918 %*/ 01919 } 01920 | var_lhs tOP_ASGN arg 01921 { 01922 /*%%%*/ 01923 value_expr($3); 01924 if ($1) { 01925 ID vid = $1->nd_vid; 01926 if ($2 == tOROP) { 01927 $1->nd_value = $3; 01928 $$ = NEW_OP_ASGN_OR(gettable(vid), $1); 01929 if (is_asgn_or_id(vid)) { 01930 $$->nd_aid = vid; 01931 } 01932 } 01933 else if ($2 == tANDOP) { 01934 $1->nd_value = $3; 01935 $$ = NEW_OP_ASGN_AND(gettable(vid), $1); 01936 } 01937 else { 01938 $$ = $1; 01939 $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3)); 01940 } 01941 } 01942 else { 01943 $$ = NEW_BEGIN(0); 01944 } 01945 /*% 01946 $$ = dispatch3(opassign, $1, $2, $3); 01947 %*/ 01948 } 01949 | var_lhs tOP_ASGN arg modifier_rescue arg 01950 { 01951 /*%%%*/ 01952 value_expr($3); 01953 $3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0); 01954 if ($1) { 01955 ID vid = $1->nd_vid; 01956 if ($2 == tOROP) { 01957 $1->nd_value = $3; 01958 $$ = NEW_OP_ASGN_OR(gettable(vid), $1); 01959 if (is_asgn_or_id(vid)) { 01960 $$->nd_aid = vid; 01961 } 01962 } 01963 else if ($2 == tANDOP) { 01964 $1->nd_value = $3; 01965 $$ = NEW_OP_ASGN_AND(gettable(vid), $1); 01966 } 01967 else { 01968 $$ = $1; 01969 $$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3)); 01970 } 01971 } 01972 else { 01973 $$ = NEW_BEGIN(0); 01974 } 01975 /*% 01976 $3 = dispatch2(rescue_mod, $3, $5); 01977 $$ = dispatch3(opassign, $1, $2, $3); 01978 %*/ 01979 } 01980 | primary_value '[' opt_call_args rbracket tOP_ASGN arg 01981 { 01982 /*%%%*/ 01983 NODE *args; 01984 01985 value_expr($6); 01986 if (!$3) $3 = NEW_ZARRAY(); 01987 if (nd_type($3) == NODE_BLOCK_PASS) { 01988 args = NEW_ARGSCAT($3, $6); 01989 } 01990 else { 01991 args = arg_concat($3, $6); 01992 } 01993 if ($5 == tOROP) { 01994 $5 = 0; 01995 } 01996 else if ($5 == tANDOP) { 01997 $5 = 1; 01998 } 01999 $$ = NEW_OP_ASGN1($1, $5, args); 02000 fixpos($$, $1); 02001 /*% 02002 $1 = dispatch2(aref_field, $1, escape_Qundef($3)); 02003 $$ = dispatch3(opassign, $1, $5, $6); 02004 %*/ 02005 } 02006 | primary_value '.' tIDENTIFIER tOP_ASGN arg 02007 { 02008 /*%%%*/ 02009 value_expr($5); 02010 if ($4 == tOROP) { 02011 $4 = 0; 02012 } 02013 else if ($4 == tANDOP) { 02014 $4 = 1; 02015 } 02016 $$ = NEW_OP_ASGN2($1, $3, $4, $5); 02017 fixpos($$, $1); 02018 /*% 02019 $1 = dispatch3(field, $1, ripper_id2sym('.'), $3); 02020 $$ = dispatch3(opassign, $1, $4, $5); 02021 %*/ 02022 } 02023 | primary_value '.' tCONSTANT tOP_ASGN arg 02024 { 02025 /*%%%*/ 02026 value_expr($5); 02027 if ($4 == tOROP) { 02028 $4 = 0; 02029 } 02030 else if ($4 == tANDOP) { 02031 $4 = 1; 02032 } 02033 $$ = NEW_OP_ASGN2($1, $3, $4, $5); 02034 fixpos($$, $1); 02035 /*% 02036 $1 = dispatch3(field, $1, ripper_id2sym('.'), $3); 02037 $$ = dispatch3(opassign, $1, $4, $5); 02038 %*/ 02039 } 02040 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg 02041 { 02042 /*%%%*/ 02043 value_expr($5); 02044 if ($4 == tOROP) { 02045 $4 = 0; 02046 } 02047 else if ($4 == tANDOP) { 02048 $4 = 1; 02049 } 02050 $$ = NEW_OP_ASGN2($1, $3, $4, $5); 02051 fixpos($$, $1); 02052 /*% 02053 $1 = dispatch3(field, $1, ripper_intern("::"), $3); 02054 $$ = dispatch3(opassign, $1, $4, $5); 02055 %*/ 02056 } 02057 | primary_value tCOLON2 tCONSTANT tOP_ASGN arg 02058 { 02059 /*%%%*/ 02060 yyerror("constant re-assignment"); 02061 $$ = NEW_BEGIN(0); 02062 /*% 02063 $$ = dispatch2(const_path_field, $1, $3); 02064 $$ = dispatch3(opassign, $$, $4, $5); 02065 $$ = dispatch1(assign_error, $$); 02066 %*/ 02067 } 02068 | tCOLON3 tCONSTANT tOP_ASGN arg 02069 { 02070 /*%%%*/ 02071 yyerror("constant re-assignment"); 02072 $$ = NEW_BEGIN(0); 02073 /*% 02074 $$ = dispatch1(top_const_field, $2); 02075 $$ = dispatch3(opassign, $$, $3, $4); 02076 $$ = dispatch1(assign_error, $$); 02077 %*/ 02078 } 02079 | backref tOP_ASGN arg 02080 { 02081 /*%%%*/ 02082 rb_backref_error($1); 02083 $$ = NEW_BEGIN(0); 02084 /*% 02085 $$ = dispatch1(var_field, $1); 02086 $$ = dispatch3(opassign, $$, $2, $3); 02087 $$ = dispatch1(assign_error, $$); 02088 %*/ 02089 } 02090 | arg tDOT2 arg 02091 { 02092 /*%%%*/ 02093 value_expr($1); 02094 value_expr($3); 02095 $$ = NEW_DOT2($1, $3); 02096 if (nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) && 02097 nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) { 02098 deferred_nodes = list_append(deferred_nodes, $$); 02099 } 02100 /*% 02101 $$ = dispatch2(dot2, $1, $3); 02102 %*/ 02103 } 02104 | arg tDOT3 arg 02105 { 02106 /*%%%*/ 02107 value_expr($1); 02108 value_expr($3); 02109 $$ = NEW_DOT3($1, $3); 02110 if (nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) && 02111 nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) { 02112 deferred_nodes = list_append(deferred_nodes, $$); 02113 } 02114 /*% 02115 $$ = dispatch2(dot3, $1, $3); 02116 %*/ 02117 } 02118 | arg '+' arg 02119 { 02120 /*%%%*/ 02121 $$ = call_bin_op($1, '+', $3); 02122 /*% 02123 $$ = dispatch3(binary, $1, ID2SYM('+'), $3); 02124 %*/ 02125 } 02126 | arg '-' arg 02127 { 02128 /*%%%*/ 02129 $$ = call_bin_op($1, '-', $3); 02130 /*% 02131 $$ = dispatch3(binary, $1, ID2SYM('-'), $3); 02132 %*/ 02133 } 02134 | arg '*' arg 02135 { 02136 /*%%%*/ 02137 $$ = call_bin_op($1, '*', $3); 02138 /*% 02139 $$ = dispatch3(binary, $1, ID2SYM('*'), $3); 02140 %*/ 02141 } 02142 | arg '/' arg 02143 { 02144 /*%%%*/ 02145 $$ = call_bin_op($1, '/', $3); 02146 /*% 02147 $$ = dispatch3(binary, $1, ID2SYM('/'), $3); 02148 %*/ 02149 } 02150 | arg '%' arg 02151 { 02152 /*%%%*/ 02153 $$ = call_bin_op($1, '%', $3); 02154 /*% 02155 $$ = dispatch3(binary, $1, ID2SYM('%'), $3); 02156 %*/ 02157 } 02158 | arg tPOW arg 02159 { 02160 /*%%%*/ 02161 $$ = call_bin_op($1, tPOW, $3); 02162 /*% 02163 $$ = dispatch3(binary, $1, ripper_intern("**"), $3); 02164 %*/ 02165 } 02166 | tUMINUS_NUM tINTEGER tPOW arg 02167 { 02168 /*%%%*/ 02169 $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0); 02170 /*% 02171 $$ = dispatch3(binary, $2, ripper_intern("**"), $4); 02172 $$ = dispatch2(unary, ripper_intern("-@"), $$); 02173 %*/ 02174 } 02175 | tUMINUS_NUM tFLOAT tPOW arg 02176 { 02177 /*%%%*/ 02178 $$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0); 02179 /*% 02180 $$ = dispatch3(binary, $2, ripper_intern("**"), $4); 02181 $$ = dispatch2(unary, ripper_intern("-@"), $$); 02182 %*/ 02183 } 02184 | tUPLUS arg 02185 { 02186 /*%%%*/ 02187 $$ = call_uni_op($2, tUPLUS); 02188 /*% 02189 $$ = dispatch2(unary, ripper_intern("+@"), $2); 02190 %*/ 02191 } 02192 | tUMINUS arg 02193 { 02194 /*%%%*/ 02195 $$ = call_uni_op($2, tUMINUS); 02196 /*% 02197 $$ = dispatch2(unary, ripper_intern("-@"), $2); 02198 %*/ 02199 } 02200 | arg '|' arg 02201 { 02202 /*%%%*/ 02203 $$ = call_bin_op($1, '|', $3); 02204 /*% 02205 $$ = dispatch3(binary, $1, ID2SYM('|'), $3); 02206 %*/ 02207 } 02208 | arg '^' arg 02209 { 02210 /*%%%*/ 02211 $$ = call_bin_op($1, '^', $3); 02212 /*% 02213 $$ = dispatch3(binary, $1, ID2SYM('^'), $3); 02214 %*/ 02215 } 02216 | arg '&' arg 02217 { 02218 /*%%%*/ 02219 $$ = call_bin_op($1, '&', $3); 02220 /*% 02221 $$ = dispatch3(binary, $1, ID2SYM('&'), $3); 02222 %*/ 02223 } 02224 | arg tCMP arg 02225 { 02226 /*%%%*/ 02227 $$ = call_bin_op($1, tCMP, $3); 02228 /*% 02229 $$ = dispatch3(binary, $1, ripper_intern("<=>"), $3); 02230 %*/ 02231 } 02232 | arg '>' arg 02233 { 02234 /*%%%*/ 02235 $$ = call_bin_op($1, '>', $3); 02236 /*% 02237 $$ = dispatch3(binary, $1, ID2SYM('>'), $3); 02238 %*/ 02239 } 02240 | arg tGEQ arg 02241 { 02242 /*%%%*/ 02243 $$ = call_bin_op($1, tGEQ, $3); 02244 /*% 02245 $$ = dispatch3(binary, $1, ripper_intern(">="), $3); 02246 %*/ 02247 } 02248 | arg '<' arg 02249 { 02250 /*%%%*/ 02251 $$ = call_bin_op($1, '<', $3); 02252 /*% 02253 $$ = dispatch3(binary, $1, ID2SYM('<'), $3); 02254 %*/ 02255 } 02256 | arg tLEQ arg 02257 { 02258 /*%%%*/ 02259 $$ = call_bin_op($1, tLEQ, $3); 02260 /*% 02261 $$ = dispatch3(binary, $1, ripper_intern("<="), $3); 02262 %*/ 02263 } 02264 | arg tEQ arg 02265 { 02266 /*%%%*/ 02267 $$ = call_bin_op($1, tEQ, $3); 02268 /*% 02269 $$ = dispatch3(binary, $1, ripper_intern("=="), $3); 02270 %*/ 02271 } 02272 | arg tEQQ arg 02273 { 02274 /*%%%*/ 02275 $$ = call_bin_op($1, tEQQ, $3); 02276 /*% 02277 $$ = dispatch3(binary, $1, ripper_intern("==="), $3); 02278 %*/ 02279 } 02280 | arg tNEQ arg 02281 { 02282 /*%%%*/ 02283 $$ = call_bin_op($1, tNEQ, $3); 02284 /*% 02285 $$ = dispatch3(binary, $1, ripper_intern("!="), $3); 02286 %*/ 02287 } 02288 | arg tMATCH arg 02289 { 02290 /*%%%*/ 02291 $$ = match_op($1, $3); 02292 if (nd_type($1) == NODE_LIT && TYPE($1->nd_lit) == T_REGEXP) { 02293 $$ = reg_named_capture_assign($1->nd_lit, $$); 02294 } 02295 /*% 02296 $$ = dispatch3(binary, $1, ripper_intern("=~"), $3); 02297 %*/ 02298 } 02299 | arg tNMATCH arg 02300 { 02301 /*%%%*/ 02302 $$ = call_bin_op($1, tNMATCH, $3); 02303 /*% 02304 $$ = dispatch3(binary, $1, ripper_intern("!~"), $3); 02305 %*/ 02306 } 02307 | '!' arg 02308 { 02309 /*%%%*/ 02310 $$ = call_uni_op(cond($2), '!'); 02311 /*% 02312 $$ = dispatch2(unary, ID2SYM('!'), $2); 02313 %*/ 02314 } 02315 | '~' arg 02316 { 02317 /*%%%*/ 02318 $$ = call_uni_op($2, '~'); 02319 /*% 02320 $$ = dispatch2(unary, ID2SYM('~'), $2); 02321 %*/ 02322 } 02323 | arg tLSHFT arg 02324 { 02325 /*%%%*/ 02326 $$ = call_bin_op($1, tLSHFT, $3); 02327 /*% 02328 $$ = dispatch3(binary, $1, ripper_intern("<<"), $3); 02329 %*/ 02330 } 02331 | arg tRSHFT arg 02332 { 02333 /*%%%*/ 02334 $$ = call_bin_op($1, tRSHFT, $3); 02335 /*% 02336 $$ = dispatch3(binary, $1, ripper_intern(">>"), $3); 02337 %*/ 02338 } 02339 | arg tANDOP arg 02340 { 02341 /*%%%*/ 02342 $$ = logop(NODE_AND, $1, $3); 02343 /*% 02344 $$ = dispatch3(binary, $1, ripper_intern("&&"), $3); 02345 %*/ 02346 } 02347 | arg tOROP arg 02348 { 02349 /*%%%*/ 02350 $$ = logop(NODE_OR, $1, $3); 02351 /*% 02352 $$ = dispatch3(binary, $1, ripper_intern("||"), $3); 02353 %*/ 02354 } 02355 | keyword_defined opt_nl {in_defined = 1;} arg 02356 { 02357 /*%%%*/ 02358 in_defined = 0; 02359 $$ = NEW_DEFINED($4); 02360 /*% 02361 in_defined = 0; 02362 $$ = dispatch1(defined, $4); 02363 %*/ 02364 } 02365 | arg '?' arg opt_nl ':' arg 02366 { 02367 /*%%%*/ 02368 value_expr($1); 02369 $$ = NEW_IF(cond($1), $3, $6); 02370 fixpos($$, $1); 02371 /*% 02372 $$ = dispatch3(ifop, $1, $3, $6); 02373 %*/ 02374 } 02375 | primary 02376 { 02377 $$ = $1; 02378 } 02379 ; 02380 02381 arg_value : arg 02382 { 02383 /*%%%*/ 02384 value_expr($1); 02385 $$ = $1; 02386 if (!$$) $$ = NEW_NIL(); 02387 /*% 02388 $$ = $1; 02389 %*/ 02390 } 02391 ; 02392 02393 aref_args : none 02394 | args trailer 02395 { 02396 $$ = $1; 02397 } 02398 | args ',' assocs trailer 02399 { 02400 /*%%%*/ 02401 $$ = arg_append($1, NEW_HASH($3)); 02402 /*% 02403 $$ = arg_add_assocs($1, $3); 02404 %*/ 02405 } 02406 | assocs trailer 02407 { 02408 /*%%%*/ 02409 $$ = NEW_LIST(NEW_HASH($1)); 02410 /*% 02411 $$ = arg_add_assocs(arg_new(), $1); 02412 %*/ 02413 } 02414 ; 02415 02416 paren_args : '(' opt_call_args rparen 02417 { 02418 /*%%%*/ 02419 $$ = $2; 02420 /*% 02421 $$ = dispatch1(arg_paren, escape_Qundef($2)); 02422 %*/ 02423 } 02424 ; 02425 02426 opt_paren_args : none 02427 | paren_args 02428 ; 02429 02430 opt_call_args : none 02431 | call_args 02432 | args ',' 02433 { 02434 $$ = $1; 02435 } 02436 | args ',' assocs ',' 02437 { 02438 /*%%%*/ 02439 $$ = arg_append($1, NEW_HASH($3)); 02440 /*% 02441 $$ = arg_add_assocs($1, $3); 02442 %*/ 02443 } 02444 | assocs ',' 02445 { 02446 /*%%%*/ 02447 $$ = NEW_LIST(NEW_HASH($1)); 02448 /*% 02449 $$ = arg_add_assocs(arg_new(), $1); 02450 %*/ 02451 } 02452 ; 02453 02454 call_args : command 02455 { 02456 /*%%%*/ 02457 value_expr($1); 02458 $$ = NEW_LIST($1); 02459 /*% 02460 $$ = arg_add(arg_new(), $1); 02461 %*/ 02462 } 02463 | args opt_block_arg 02464 { 02465 /*%%%*/ 02466 $$ = arg_blk_pass($1, $2); 02467 /*% 02468 $$ = arg_add_optblock($1, $2); 02469 %*/ 02470 } 02471 | assocs opt_block_arg 02472 { 02473 /*%%%*/ 02474 $$ = NEW_LIST(NEW_HASH($1)); 02475 $$ = arg_blk_pass($$, $2); 02476 /*% 02477 $$ = arg_add_assocs(arg_new(), $1); 02478 $$ = arg_add_optblock($$, $2); 02479 %*/ 02480 } 02481 | args ',' assocs opt_block_arg 02482 { 02483 /*%%%*/ 02484 $$ = arg_append($1, NEW_HASH($3)); 02485 $$ = arg_blk_pass($$, $4); 02486 /*% 02487 $$ = arg_add_optblock(arg_add_assocs($1, $3), $4); 02488 %*/ 02489 } 02490 | block_arg 02491 /*%c%*/ 02492 /*%c 02493 { 02494 $$ = arg_add_block(arg_new(), $1); 02495 } 02496 %*/ 02497 ; 02498 02499 command_args : { 02500 $<val>$ = cmdarg_stack; 02501 CMDARG_PUSH(1); 02502 } 02503 call_args 02504 { 02505 /* CMDARG_POP() */ 02506 cmdarg_stack = $<val>1; 02507 $$ = $2; 02508 } 02509 ; 02510 02511 block_arg : tAMPER arg_value 02512 { 02513 /*%%%*/ 02514 $$ = NEW_BLOCK_PASS($2); 02515 /*% 02516 $$ = $2; 02517 %*/ 02518 } 02519 ; 02520 02521 opt_block_arg : ',' block_arg 02522 { 02523 $$ = $2; 02524 } 02525 | none 02526 { 02527 $$ = 0; 02528 } 02529 ; 02530 02531 args : arg_value 02532 { 02533 /*%%%*/ 02534 $$ = NEW_LIST($1); 02535 /*% 02536 $$ = arg_add(arg_new(), $1); 02537 %*/ 02538 } 02539 | tSTAR arg_value 02540 { 02541 /*%%%*/ 02542 $$ = NEW_SPLAT($2); 02543 /*% 02544 $$ = arg_add_star(arg_new(), $2); 02545 %*/ 02546 } 02547 | args ',' arg_value 02548 { 02549 /*%%%*/ 02550 NODE *n1; 02551 if ((n1 = splat_array($1)) != 0) { 02552 $$ = list_append(n1, $3); 02553 } 02554 else { 02555 $$ = arg_append($1, $3); 02556 } 02557 /*% 02558 $$ = arg_add($1, $3); 02559 %*/ 02560 } 02561 | args ',' tSTAR arg_value 02562 { 02563 /*%%%*/ 02564 NODE *n1; 02565 if ((nd_type($4) == NODE_ARRAY) && (n1 = splat_array($1)) != 0) { 02566 $$ = list_concat(n1, $4); 02567 } 02568 else { 02569 $$ = arg_concat($1, $4); 02570 } 02571 /*% 02572 $$ = arg_add_star($1, $4); 02573 %*/ 02574 } 02575 ; 02576 02577 mrhs : args ',' arg_value 02578 { 02579 /*%%%*/ 02580 NODE *n1; 02581 if ((n1 = splat_array($1)) != 0) { 02582 $$ = list_append(n1, $3); 02583 } 02584 else { 02585 $$ = arg_append($1, $3); 02586 } 02587 /*% 02588 $$ = mrhs_add(args2mrhs($1), $3); 02589 %*/ 02590 } 02591 | args ',' tSTAR arg_value 02592 { 02593 /*%%%*/ 02594 NODE *n1; 02595 if (nd_type($4) == NODE_ARRAY && 02596 (n1 = splat_array($1)) != 0) { 02597 $$ = list_concat(n1, $4); 02598 } 02599 else { 02600 $$ = arg_concat($1, $4); 02601 } 02602 /*% 02603 $$ = mrhs_add_star(args2mrhs($1), $4); 02604 %*/ 02605 } 02606 | tSTAR arg_value 02607 { 02608 /*%%%*/ 02609 $$ = NEW_SPLAT($2); 02610 /*% 02611 $$ = mrhs_add_star(mrhs_new(), $2); 02612 %*/ 02613 } 02614 ; 02615 02616 primary : literal 02617 | strings 02618 | xstring 02619 | regexp 02620 | words 02621 | qwords 02622 | var_ref 02623 | backref 02624 | tFID 02625 { 02626 /*%%%*/ 02627 $$ = NEW_FCALL($1, 0); 02628 /*% 02629 $$ = method_arg(dispatch1(fcall, $1), arg_new()); 02630 %*/ 02631 } 02632 | k_begin 02633 { 02634 /*%%%*/ 02635 $<num>$ = ruby_sourceline; 02636 /*% 02637 %*/ 02638 } 02639 bodystmt 02640 k_end 02641 { 02642 /*%%%*/ 02643 if ($3 == NULL) { 02644 $$ = NEW_NIL(); 02645 } 02646 else { 02647 if (nd_type($3) == NODE_RESCUE || 02648 nd_type($3) == NODE_ENSURE) 02649 nd_set_line($3, $<num>2); 02650 $$ = NEW_BEGIN($3); 02651 } 02652 nd_set_line($$, $<num>2); 02653 /*% 02654 $$ = dispatch1(begin, $3); 02655 %*/ 02656 } 02657 | tLPAREN_ARG expr {lex_state = EXPR_ENDARG;} rparen 02658 { 02659 rb_warning0("(...) interpreted as grouped expression"); 02660 /*%%%*/ 02661 $$ = $2; 02662 /*% 02663 $$ = dispatch1(paren, $2); 02664 %*/ 02665 } 02666 | tLPAREN compstmt ')' 02667 { 02668 /*%%%*/ 02669 $$ = $2; 02670 /*% 02671 $$ = dispatch1(paren, $2); 02672 %*/ 02673 } 02674 | primary_value tCOLON2 tCONSTANT 02675 { 02676 /*%%%*/ 02677 $$ = NEW_COLON2($1, $3); 02678 /*% 02679 $$ = dispatch2(const_path_ref, $1, $3); 02680 %*/ 02681 } 02682 | tCOLON3 tCONSTANT 02683 { 02684 /*%%%*/ 02685 $$ = NEW_COLON3($2); 02686 /*% 02687 $$ = dispatch1(top_const_ref, $2); 02688 %*/ 02689 } 02690 | tLBRACK aref_args ']' 02691 { 02692 /*%%%*/ 02693 if ($2 == 0) { 02694 $$ = NEW_ZARRAY(); /* zero length array*/ 02695 } 02696 else { 02697 $$ = $2; 02698 } 02699 /*% 02700 $$ = dispatch1(array, escape_Qundef($2)); 02701 %*/ 02702 } 02703 | tLBRACE assoc_list '}' 02704 { 02705 /*%%%*/ 02706 $$ = NEW_HASH($2); 02707 /*% 02708 $$ = dispatch1(hash, escape_Qundef($2)); 02709 %*/ 02710 } 02711 | keyword_return 02712 { 02713 /*%%%*/ 02714 $$ = NEW_RETURN(0); 02715 /*% 02716 $$ = dispatch0(return0); 02717 %*/ 02718 } 02719 | keyword_yield '(' call_args rparen 02720 { 02721 /*%%%*/ 02722 $$ = new_yield($3); 02723 /*% 02724 $$ = dispatch1(yield, dispatch1(paren, $3)); 02725 %*/ 02726 } 02727 | keyword_yield '(' rparen 02728 { 02729 /*%%%*/ 02730 $$ = NEW_YIELD(0, Qfalse); 02731 /*% 02732 $$ = dispatch1(yield, dispatch1(paren, arg_new())); 02733 %*/ 02734 } 02735 | keyword_yield 02736 { 02737 /*%%%*/ 02738 $$ = NEW_YIELD(0, Qfalse); 02739 /*% 02740 $$ = dispatch0(yield0); 02741 %*/ 02742 } 02743 | keyword_defined opt_nl '(' {in_defined = 1;} expr rparen 02744 { 02745 /*%%%*/ 02746 in_defined = 0; 02747 $$ = NEW_DEFINED($5); 02748 /*% 02749 in_defined = 0; 02750 $$ = dispatch1(defined, $5); 02751 %*/ 02752 } 02753 | keyword_not '(' expr rparen 02754 { 02755 /*%%%*/ 02756 $$ = call_uni_op(cond($3), '!'); 02757 /*% 02758 $$ = dispatch2(unary, ripper_intern("not"), $3); 02759 %*/ 02760 } 02761 | keyword_not '(' rparen 02762 { 02763 /*%%%*/ 02764 $$ = call_uni_op(cond(NEW_NIL()), '!'); 02765 /*% 02766 $$ = dispatch2(unary, ripper_intern("not"), Qnil); 02767 %*/ 02768 } 02769 | operation brace_block 02770 { 02771 /*%%%*/ 02772 $2->nd_iter = NEW_FCALL($1, 0); 02773 $$ = $2; 02774 fixpos($2->nd_iter, $2); 02775 /*% 02776 $$ = method_arg(dispatch1(fcall, $1), arg_new()); 02777 $$ = method_add_block($$, $2); 02778 %*/ 02779 } 02780 | method_call 02781 | method_call brace_block 02782 { 02783 /*%%%*/ 02784 block_dup_check($1->nd_args, $2); 02785 $2->nd_iter = $1; 02786 $$ = $2; 02787 fixpos($$, $1); 02788 /*% 02789 $$ = method_add_block($1, $2); 02790 %*/ 02791 } 02792 | tLAMBDA lambda 02793 { 02794 $$ = $2; 02795 } 02796 | k_if expr_value then 02797 compstmt 02798 if_tail 02799 k_end 02800 { 02801 /*%%%*/ 02802 $$ = NEW_IF(cond($2), $4, $5); 02803 fixpos($$, $2); 02804 /*% 02805 $$ = dispatch3(if, $2, $4, escape_Qundef($5)); 02806 %*/ 02807 } 02808 | k_unless expr_value then 02809 compstmt 02810 opt_else 02811 k_end 02812 { 02813 /*%%%*/ 02814 $$ = NEW_UNLESS(cond($2), $4, $5); 02815 fixpos($$, $2); 02816 /*% 02817 $$ = dispatch3(unless, $2, $4, escape_Qundef($5)); 02818 %*/ 02819 } 02820 | k_while {COND_PUSH(1);} expr_value do {COND_POP();} 02821 compstmt 02822 k_end 02823 { 02824 /*%%%*/ 02825 $$ = NEW_WHILE(cond($3), $6, 1); 02826 fixpos($$, $3); 02827 /*% 02828 $$ = dispatch2(while, $3, $6); 02829 %*/ 02830 } 02831 | k_until {COND_PUSH(1);} expr_value do {COND_POP();} 02832 compstmt 02833 k_end 02834 { 02835 /*%%%*/ 02836 $$ = NEW_UNTIL(cond($3), $6, 1); 02837 fixpos($$, $3); 02838 /*% 02839 $$ = dispatch2(until, $3, $6); 02840 %*/ 02841 } 02842 | k_case expr_value opt_terms 02843 case_body 02844 k_end 02845 { 02846 /*%%%*/ 02847 $$ = NEW_CASE($2, $4); 02848 fixpos($$, $2); 02849 /*% 02850 $$ = dispatch2(case, $2, $4); 02851 %*/ 02852 } 02853 | k_case opt_terms case_body k_end 02854 { 02855 /*%%%*/ 02856 $$ = NEW_CASE(0, $3); 02857 /*% 02858 $$ = dispatch2(case, Qnil, $3); 02859 %*/ 02860 } 02861 | k_for for_var keyword_in 02862 {COND_PUSH(1);} 02863 expr_value do 02864 {COND_POP();} 02865 compstmt 02866 k_end 02867 { 02868 /*%%%*/ 02869 /* 02870 * for a, b, c in e 02871 * #=> 02872 * e.each{|*x| a, b, c = x 02873 * 02874 * for a in e 02875 * #=> 02876 * e.each{|x| a, = x} 02877 */ 02878 ID id = internal_id(); 02879 ID *tbl = ALLOC_N(ID, 2); 02880 NODE *m = NEW_ARGS_AUX(0, 0); 02881 NODE *args, *scope; 02882 02883 if (nd_type($2) == NODE_MASGN) { 02884 /* if args.length == 1 && args[0].kind_of?(Array) 02885 * args = args[0] 02886 * end 02887 */ 02888 NODE *one = NEW_LIST(NEW_LIT(INT2FIX(1))); 02889 NODE *zero = NEW_LIST(NEW_LIT(INT2FIX(0))); 02890 m->nd_next = block_append( 02891 NEW_IF( 02892 NEW_NODE(NODE_AND, 02893 NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("length"), 0), 02894 rb_intern("=="), one), 02895 NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero), 02896 rb_intern("kind_of?"), NEW_LIST(NEW_LIT(rb_cArray))), 02897 0), 02898 NEW_DASGN_CURR(id, 02899 NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero)), 02900 0), 02901 node_assign($2, NEW_DVAR(id))); 02902 02903 args = new_args(m, 0, id, 0, 0); 02904 } 02905 else { 02906 if (nd_type($2) == NODE_LASGN || 02907 nd_type($2) == NODE_DASGN || 02908 nd_type($2) == NODE_DASGN_CURR) { 02909 $2->nd_value = NEW_DVAR(id); 02910 m->nd_plen = 1; 02911 m->nd_next = $2; 02912 args = new_args(m, 0, 0, 0, 0); 02913 } 02914 else { 02915 m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id)); 02916 args = new_args(m, 0, id, 0, 0); 02917 } 02918 } 02919 scope = NEW_NODE(NODE_SCOPE, tbl, $8, args); 02920 tbl[0] = 1; tbl[1] = id; 02921 $$ = NEW_FOR(0, $5, scope); 02922 fixpos($$, $2); 02923 /*% 02924 $$ = dispatch3(for, $2, $5, $8); 02925 %*/ 02926 } 02927 | k_class cpath superclass 02928 { 02929 if (in_def || in_single) 02930 yyerror("class definition in method body"); 02931 local_push(0); 02932 /*%%%*/ 02933 $<num>$ = ruby_sourceline; 02934 /*% 02935 %*/ 02936 } 02937 bodystmt 02938 k_end 02939 { 02940 /*%%%*/ 02941 $$ = NEW_CLASS($2, $5, $3); 02942 nd_set_line($$, $<num>4); 02943 /*% 02944 $$ = dispatch3(class, $2, $3, $5); 02945 %*/ 02946 local_pop(); 02947 } 02948 | k_class tLSHFT expr 02949 { 02950 $<num>$ = in_def; 02951 in_def = 0; 02952 } 02953 term 02954 { 02955 $<num>$ = in_single; 02956 in_single = 0; 02957 local_push(0); 02958 } 02959 bodystmt 02960 k_end 02961 { 02962 /*%%%*/ 02963 $$ = NEW_SCLASS($3, $7); 02964 fixpos($$, $3); 02965 /*% 02966 $$ = dispatch2(sclass, $3, $7); 02967 %*/ 02968 local_pop(); 02969 in_def = $<num>4; 02970 in_single = $<num>6; 02971 } 02972 | k_module cpath 02973 { 02974 if (in_def || in_single) 02975 yyerror("module definition in method body"); 02976 local_push(0); 02977 /*%%%*/ 02978 $<num>$ = ruby_sourceline; 02979 /*% 02980 %*/ 02981 } 02982 bodystmt 02983 k_end 02984 { 02985 /*%%%*/ 02986 $$ = NEW_MODULE($2, $4); 02987 nd_set_line($$, $<num>3); 02988 /*% 02989 $$ = dispatch2(module, $2, $4); 02990 %*/ 02991 local_pop(); 02992 } 02993 | k_def fname 02994 { 02995 $<id>$ = cur_mid; 02996 cur_mid = $2; 02997 in_def++; 02998 local_push(0); 02999 } 03000 f_arglist 03001 bodystmt 03002 k_end 03003 { 03004 /*%%%*/ 03005 NODE *body = remove_begin($5); 03006 reduce_nodes(&body); 03007 $$ = NEW_DEFN($2, $4, body, NOEX_PRIVATE); 03008 nd_set_line($$, $<num>1); 03009 /*% 03010 $$ = dispatch3(def, $2, $4, $5); 03011 %*/ 03012 local_pop(); 03013 in_def--; 03014 cur_mid = $<id>3; 03015 } 03016 | k_def singleton dot_or_colon {lex_state = EXPR_FNAME;} fname 03017 { 03018 in_single++; 03019 lex_state = EXPR_ENDFN; /* force for args */ 03020 local_push(0); 03021 } 03022 f_arglist 03023 bodystmt 03024 k_end 03025 { 03026 /*%%%*/ 03027 NODE *body = remove_begin($8); 03028 reduce_nodes(&body); 03029 $$ = NEW_DEFS($2, $5, $7, body); 03030 nd_set_line($$, $<num>1); 03031 /*% 03032 $$ = dispatch5(defs, $2, $3, $5, $7, $8); 03033 %*/ 03034 local_pop(); 03035 in_single--; 03036 } 03037 | keyword_break 03038 { 03039 /*%%%*/ 03040 $$ = NEW_BREAK(0); 03041 /*% 03042 $$ = dispatch1(break, arg_new()); 03043 %*/ 03044 } 03045 | keyword_next 03046 { 03047 /*%%%*/ 03048 $$ = NEW_NEXT(0); 03049 /*% 03050 $$ = dispatch1(next, arg_new()); 03051 %*/ 03052 } 03053 | keyword_redo 03054 { 03055 /*%%%*/ 03056 $$ = NEW_REDO(); 03057 /*% 03058 $$ = dispatch0(redo); 03059 %*/ 03060 } 03061 | keyword_retry 03062 { 03063 /*%%%*/ 03064 $$ = NEW_RETRY(); 03065 /*% 03066 $$ = dispatch0(retry); 03067 %*/ 03068 } 03069 ; 03070 03071 primary_value : primary 03072 { 03073 /*%%%*/ 03074 value_expr($1); 03075 $$ = $1; 03076 if (!$$) $$ = NEW_NIL(); 03077 /*% 03078 $$ = $1; 03079 %*/ 03080 } 03081 ; 03082 03083 k_begin : keyword_begin 03084 { 03085 token_info_push("begin"); 03086 } 03087 ; 03088 03089 k_if : keyword_if 03090 { 03091 token_info_push("if"); 03092 } 03093 ; 03094 03095 k_unless : keyword_unless 03096 { 03097 token_info_push("unless"); 03098 } 03099 ; 03100 03101 k_while : keyword_while 03102 { 03103 token_info_push("while"); 03104 } 03105 ; 03106 03107 k_until : keyword_until 03108 { 03109 token_info_push("until"); 03110 } 03111 ; 03112 03113 k_case : keyword_case 03114 { 03115 token_info_push("case"); 03116 } 03117 ; 03118 03119 k_for : keyword_for 03120 { 03121 token_info_push("for"); 03122 } 03123 ; 03124 03125 k_class : keyword_class 03126 { 03127 token_info_push("class"); 03128 } 03129 ; 03130 03131 k_module : keyword_module 03132 { 03133 token_info_push("module"); 03134 } 03135 ; 03136 03137 k_def : keyword_def 03138 { 03139 token_info_push("def"); 03140 /*%%%*/ 03141 $<num>$ = ruby_sourceline; 03142 /*% 03143 %*/ 03144 } 03145 ; 03146 03147 k_end : keyword_end 03148 { 03149 token_info_pop("end"); 03150 } 03151 ; 03152 03153 then : term 03154 /*%c%*/ 03155 /*%c 03156 { $$ = Qnil; } 03157 %*/ 03158 | keyword_then 03159 | term keyword_then 03160 /*%c%*/ 03161 /*%c 03162 { $$ = $2; } 03163 %*/ 03164 ; 03165 03166 do : term 03167 /*%c%*/ 03168 /*%c 03169 { $$ = Qnil; } 03170 %*/ 03171 | keyword_do_cond 03172 ; 03173 03174 if_tail : opt_else 03175 | keyword_elsif expr_value then 03176 compstmt 03177 if_tail 03178 { 03179 /*%%%*/ 03180 $$ = NEW_IF(cond($2), $4, $5); 03181 fixpos($$, $2); 03182 /*% 03183 $$ = dispatch3(elsif, $2, $4, escape_Qundef($5)); 03184 %*/ 03185 } 03186 ; 03187 03188 opt_else : none 03189 | keyword_else compstmt 03190 { 03191 /*%%%*/ 03192 $$ = $2; 03193 /*% 03194 $$ = dispatch1(else, $2); 03195 %*/ 03196 } 03197 ; 03198 03199 for_var : lhs 03200 | mlhs 03201 ; 03202 03203 f_marg : f_norm_arg 03204 { 03205 $$ = assignable($1, 0); 03206 /*%%%*/ 03207 /*% 03208 $$ = dispatch1(mlhs_paren, $$); 03209 %*/ 03210 } 03211 | tLPAREN f_margs rparen 03212 { 03213 /*%%%*/ 03214 $$ = $2; 03215 /*% 03216 $$ = dispatch1(mlhs_paren, $2); 03217 %*/ 03218 } 03219 ; 03220 03221 f_marg_list : f_marg 03222 { 03223 /*%%%*/ 03224 $$ = NEW_LIST($1); 03225 /*% 03226 $$ = mlhs_add(mlhs_new(), $1); 03227 %*/ 03228 } 03229 | f_marg_list ',' f_marg 03230 { 03231 /*%%%*/ 03232 $$ = list_append($1, $3); 03233 /*% 03234 $$ = mlhs_add($1, $3); 03235 %*/ 03236 } 03237 ; 03238 03239 f_margs : f_marg_list 03240 { 03241 /*%%%*/ 03242 $$ = NEW_MASGN($1, 0); 03243 /*% 03244 $$ = $1; 03245 %*/ 03246 } 03247 | f_marg_list ',' tSTAR f_norm_arg 03248 { 03249 $$ = assignable($4, 0); 03250 /*%%%*/ 03251 $$ = NEW_MASGN($1, $$); 03252 /*% 03253 $$ = mlhs_add_star($1, $$); 03254 %*/ 03255 } 03256 | f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list 03257 { 03258 $$ = assignable($4, 0); 03259 /*%%%*/ 03260 $$ = NEW_MASGN($1, NEW_POSTARG($$, $6)); 03261 /*% 03262 $$ = mlhs_add_star($1, $$); 03263 %*/ 03264 } 03265 | f_marg_list ',' tSTAR 03266 { 03267 /*%%%*/ 03268 $$ = NEW_MASGN($1, -1); 03269 /*% 03270 $$ = mlhs_add_star($1, Qnil); 03271 %*/ 03272 } 03273 | f_marg_list ',' tSTAR ',' f_marg_list 03274 { 03275 /*%%%*/ 03276 $$ = NEW_MASGN($1, NEW_POSTARG(-1, $5)); 03277 /*% 03278 $$ = mlhs_add_star($1, $5); 03279 %*/ 03280 } 03281 | tSTAR f_norm_arg 03282 { 03283 $$ = assignable($2, 0); 03284 /*%%%*/ 03285 $$ = NEW_MASGN(0, $$); 03286 /*% 03287 $$ = mlhs_add_star(mlhs_new(), $$); 03288 %*/ 03289 } 03290 | tSTAR f_norm_arg ',' f_marg_list 03291 { 03292 $$ = assignable($2, 0); 03293 /*%%%*/ 03294 $$ = NEW_MASGN(0, NEW_POSTARG($$, $4)); 03295 /*% 03296 #if 0 03297 TODO: Check me 03298 #endif 03299 $$ = mlhs_add_star($$, $4); 03300 %*/ 03301 } 03302 | tSTAR 03303 { 03304 /*%%%*/ 03305 $$ = NEW_MASGN(0, -1); 03306 /*% 03307 $$ = mlhs_add_star(mlhs_new(), Qnil); 03308 %*/ 03309 } 03310 | tSTAR ',' f_marg_list 03311 { 03312 /*%%%*/ 03313 $$ = NEW_MASGN(0, NEW_POSTARG(-1, $3)); 03314 /*% 03315 $$ = mlhs_add_star(mlhs_new(), Qnil); 03316 %*/ 03317 } 03318 ; 03319 03320 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_f_block_arg 03321 { 03322 /*%%%*/ 03323 $$ = new_args($1, $3, $5, 0, $6); 03324 /*% 03325 $$ = params_new($1, $3, $5, Qnil, escape_Qundef($6)); 03326 %*/ 03327 } 03328 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg 03329 { 03330 /*%%%*/ 03331 $$ = new_args($1, $3, $5, $7, $8); 03332 /*% 03333 $$ = params_new($1, $3, $5, $7, escape_Qundef($8)); 03334 %*/ 03335 } 03336 | f_arg ',' f_block_optarg opt_f_block_arg 03337 { 03338 /*%%%*/ 03339 $$ = new_args($1, $3, 0, 0, $4); 03340 /*% 03341 $$ = params_new($1, $3, Qnil, Qnil, escape_Qundef($4)); 03342 %*/ 03343 } 03344 | f_arg ',' f_block_optarg ',' f_arg opt_f_block_arg 03345 { 03346 /*%%%*/ 03347 $$ = new_args($1, $3, 0, $5, $6); 03348 /*% 03349 $$ = params_new($1, $3, Qnil, $5, escape_Qundef($6)); 03350 %*/ 03351 } 03352 | f_arg ',' f_rest_arg opt_f_block_arg 03353 { 03354 /*%%%*/ 03355 $$ = new_args($1, 0, $3, 0, $4); 03356 /*% 03357 $$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4)); 03358 %*/ 03359 } 03360 | f_arg ',' 03361 { 03362 /*%%%*/ 03363 $$ = new_args($1, 0, 1, 0, 0); 03364 /*% 03365 $$ = params_new($1, Qnil, Qnil, Qnil, Qnil); 03366 dispatch1(excessed_comma, $$); 03367 %*/ 03368 } 03369 | f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg 03370 { 03371 /*%%%*/ 03372 $$ = new_args($1, 0, $3, $5, $6); 03373 /*% 03374 $$ = params_new($1, Qnil, $3, $5, escape_Qundef($6)); 03375 %*/ 03376 } 03377 | f_arg opt_f_block_arg 03378 { 03379 /*%%%*/ 03380 $$ = new_args($1, 0, 0, 0, $2); 03381 /*% 03382 $$ = params_new($1, Qnil,Qnil, Qnil, escape_Qundef($2)); 03383 %*/ 03384 } 03385 | f_block_optarg ',' f_rest_arg opt_f_block_arg 03386 { 03387 /*%%%*/ 03388 $$ = new_args(0, $1, $3, 0, $4); 03389 /*% 03390 $$ = params_new(Qnil, $1, $3, Qnil, escape_Qundef($4)); 03391 %*/ 03392 } 03393 | f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg 03394 { 03395 /*%%%*/ 03396 $$ = new_args(0, $1, $3, $5, $6); 03397 /*% 03398 $$ = params_new(Qnil, $1, $3, $5, escape_Qundef($6)); 03399 %*/ 03400 } 03401 | f_block_optarg opt_f_block_arg 03402 { 03403 /*%%%*/ 03404 $$ = new_args(0, $1, 0, 0, $2); 03405 /*% 03406 $$ = params_new(Qnil, $1, Qnil, Qnil,escape_Qundef($2)); 03407 %*/ 03408 } 03409 | f_block_optarg ',' f_arg opt_f_block_arg 03410 { 03411 /*%%%*/ 03412 $$ = new_args(0, $1, 0, $3, $4); 03413 /*% 03414 $$ = params_new(Qnil, $1, Qnil, $3, escape_Qundef($4)); 03415 %*/ 03416 } 03417 | f_rest_arg opt_f_block_arg 03418 { 03419 /*%%%*/ 03420 $$ = new_args(0, 0, $1, 0, $2); 03421 /*% 03422 $$ = params_new(Qnil, Qnil, $1, Qnil, escape_Qundef($2)); 03423 %*/ 03424 } 03425 | f_rest_arg ',' f_arg opt_f_block_arg 03426 { 03427 /*%%%*/ 03428 $$ = new_args(0, 0, $1, $3, $4); 03429 /*% 03430 $$ = params_new(Qnil, Qnil, $1, $3, escape_Qundef($4)); 03431 %*/ 03432 } 03433 | f_block_arg 03434 { 03435 /*%%%*/ 03436 $$ = new_args(0, 0, 0, 0, $1); 03437 /*% 03438 $$ = params_new(Qnil, Qnil, Qnil, Qnil, $1); 03439 %*/ 03440 } 03441 ; 03442 03443 opt_block_param : none 03444 | block_param_def 03445 { 03446 command_start = TRUE; 03447 } 03448 ; 03449 03450 block_param_def : '|' opt_bv_decl '|' 03451 { 03452 /*%%%*/ 03453 $$ = 0; 03454 /*% 03455 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil), 03456 escape_Qundef($2)); 03457 %*/ 03458 } 03459 | tOROP 03460 { 03461 /*%%%*/ 03462 $$ = 0; 03463 /*% 03464 $$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil), 03465 Qnil); 03466 %*/ 03467 } 03468 | '|' block_param opt_bv_decl '|' 03469 { 03470 /*%%%*/ 03471 $$ = $2; 03472 /*% 03473 $$ = blockvar_new(escape_Qundef($2), escape_Qundef($3)); 03474 %*/ 03475 } 03476 ; 03477 03478 03479 opt_bv_decl : none 03480 | ';' bv_decls 03481 { 03482 /*%%%*/ 03483 $$ = 0; 03484 /*% 03485 $$ = $2; 03486 %*/ 03487 } 03488 ; 03489 03490 bv_decls : bvar 03491 /*%c%*/ 03492 /*%c 03493 { 03494 $$ = rb_ary_new3(1, $1); 03495 } 03496 %*/ 03497 | bv_decls ',' bvar 03498 /*%c%*/ 03499 /*%c 03500 { 03501 rb_ary_push($$, $3); 03502 } 03503 %*/ 03504 ; 03505 03506 bvar : tIDENTIFIER 03507 { 03508 new_bv(get_id($1)); 03509 /*%%%*/ 03510 /*% 03511 $$ = get_value($1); 03512 %*/ 03513 } 03514 | f_bad_arg 03515 { 03516 $$ = 0; 03517 } 03518 ; 03519 03520 lambda : { 03521 $<vars>$ = dyna_push(); 03522 } 03523 { 03524 $<num>$ = lpar_beg; 03525 lpar_beg = ++paren_nest; 03526 } 03527 f_larglist 03528 lambda_body 03529 { 03530 lpar_beg = $<num>2; 03531 /*%%%*/ 03532 $$ = $3; 03533 $$->nd_body = NEW_SCOPE($3->nd_head, $4); 03534 /*% 03535 $$ = dispatch2(lambda, $3, $4); 03536 %*/ 03537 dyna_pop($<vars>1); 03538 } 03539 ; 03540 03541 f_larglist : '(' f_args opt_bv_decl rparen 03542 { 03543 /*%%%*/ 03544 $$ = NEW_LAMBDA($2); 03545 /*% 03546 $$ = dispatch1(paren, $2); 03547 %*/ 03548 } 03549 | f_args 03550 { 03551 /*%%%*/ 03552 $$ = NEW_LAMBDA($1); 03553 /*% 03554 $$ = $1; 03555 %*/ 03556 } 03557 ; 03558 03559 lambda_body : tLAMBEG compstmt '}' 03560 { 03561 $$ = $2; 03562 } 03563 | keyword_do_LAMBDA compstmt keyword_end 03564 { 03565 $$ = $2; 03566 } 03567 ; 03568 03569 do_block : keyword_do_block 03570 { 03571 $<vars>1 = dyna_push(); 03572 /*%%%*/ 03573 $<num>$ = ruby_sourceline; 03574 /*% %*/ 03575 } 03576 opt_block_param 03577 compstmt 03578 keyword_end 03579 { 03580 /*%%%*/ 03581 $$ = NEW_ITER($3,$4); 03582 nd_set_line($$, $<num>2); 03583 /*% 03584 $$ = dispatch2(do_block, escape_Qundef($3), $4); 03585 %*/ 03586 dyna_pop($<vars>1); 03587 } 03588 ; 03589 03590 block_call : command do_block 03591 { 03592 /*%%%*/ 03593 if (nd_type($1) == NODE_YIELD) { 03594 compile_error(PARSER_ARG "block given to yield"); 03595 } 03596 else { 03597 block_dup_check($1->nd_args, $2); 03598 } 03599 $2->nd_iter = $1; 03600 $$ = $2; 03601 fixpos($$, $1); 03602 /*% 03603 $$ = method_add_block($1, $2); 03604 %*/ 03605 } 03606 | block_call '.' operation2 opt_paren_args 03607 { 03608 /*%%%*/ 03609 $$ = NEW_CALL($1, $3, $4); 03610 /*% 03611 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3); 03612 $$ = method_optarg($$, $4); 03613 %*/ 03614 } 03615 | block_call tCOLON2 operation2 opt_paren_args 03616 { 03617 /*%%%*/ 03618 $$ = NEW_CALL($1, $3, $4); 03619 /*% 03620 $$ = dispatch3(call, $1, ripper_intern("::"), $3); 03621 $$ = method_optarg($$, $4); 03622 %*/ 03623 } 03624 ; 03625 03626 method_call : operation paren_args 03627 { 03628 /*%%%*/ 03629 $$ = NEW_FCALL($1, $2); 03630 fixpos($$, $2); 03631 /*% 03632 $$ = method_arg(dispatch1(fcall, $1), $2); 03633 %*/ 03634 } 03635 | primary_value '.' operation2 opt_paren_args 03636 { 03637 /*%%%*/ 03638 $$ = NEW_CALL($1, $3, $4); 03639 fixpos($$, $1); 03640 /*% 03641 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3); 03642 $$ = method_optarg($$, $4); 03643 %*/ 03644 } 03645 | primary_value tCOLON2 operation2 paren_args 03646 { 03647 /*%%%*/ 03648 $$ = NEW_CALL($1, $3, $4); 03649 fixpos($$, $1); 03650 /*% 03651 $$ = dispatch3(call, $1, ripper_id2sym('.'), $3); 03652 $$ = method_optarg($$, $4); 03653 %*/ 03654 } 03655 | primary_value tCOLON2 operation3 03656 { 03657 /*%%%*/ 03658 $$ = NEW_CALL($1, $3, 0); 03659 /*% 03660 $$ = dispatch3(call, $1, ripper_intern("::"), $3); 03661 %*/ 03662 } 03663 | primary_value '.' paren_args 03664 { 03665 /*%%%*/ 03666 $$ = NEW_CALL($1, rb_intern("call"), $3); 03667 fixpos($$, $1); 03668 /*% 03669 $$ = dispatch3(call, $1, ripper_id2sym('.'), 03670 ripper_intern("call")); 03671 $$ = method_optarg($$, $3); 03672 %*/ 03673 } 03674 | primary_value tCOLON2 paren_args 03675 { 03676 /*%%%*/ 03677 $$ = NEW_CALL($1, rb_intern("call"), $3); 03678 fixpos($$, $1); 03679 /*% 03680 $$ = dispatch3(call, $1, ripper_intern("::"), 03681 ripper_intern("call")); 03682 $$ = method_optarg($$, $3); 03683 %*/ 03684 } 03685 | keyword_super paren_args 03686 { 03687 /*%%%*/ 03688 $$ = NEW_SUPER($2); 03689 /*% 03690 $$ = dispatch1(super, $2); 03691 %*/ 03692 } 03693 | keyword_super 03694 { 03695 /*%%%*/ 03696 $$ = NEW_ZSUPER(); 03697 /*% 03698 $$ = dispatch0(zsuper); 03699 %*/ 03700 } 03701 | primary_value '[' opt_call_args rbracket 03702 { 03703 /*%%%*/ 03704 if ($1 && nd_type($1) == NODE_SELF) 03705 $$ = NEW_FCALL(tAREF, $3); 03706 else 03707 $$ = NEW_CALL($1, tAREF, $3); 03708 fixpos($$, $1); 03709 /*% 03710 $$ = dispatch2(aref, $1, escape_Qundef($3)); 03711 %*/ 03712 } 03713 ; 03714 03715 brace_block : '{' 03716 { 03717 $<vars>1 = dyna_push(); 03718 /*%%%*/ 03719 $<num>$ = ruby_sourceline; 03720 /*% 03721 %*/ 03722 } 03723 opt_block_param 03724 compstmt '}' 03725 { 03726 /*%%%*/ 03727 $$ = NEW_ITER($3,$4); 03728 nd_set_line($$, $<num>2); 03729 /*% 03730 $$ = dispatch2(brace_block, escape_Qundef($3), $4); 03731 %*/ 03732 dyna_pop($<vars>1); 03733 } 03734 | keyword_do 03735 { 03736 $<vars>1 = dyna_push(); 03737 /*%%%*/ 03738 $<num>$ = ruby_sourceline; 03739 /*% 03740 %*/ 03741 } 03742 opt_block_param 03743 compstmt keyword_end 03744 { 03745 /*%%%*/ 03746 $$ = NEW_ITER($3,$4); 03747 nd_set_line($$, $<num>2); 03748 /*% 03749 $$ = dispatch2(do_block, escape_Qundef($3), $4); 03750 %*/ 03751 dyna_pop($<vars>1); 03752 } 03753 ; 03754 03755 case_body : keyword_when args then 03756 compstmt 03757 cases 03758 { 03759 /*%%%*/ 03760 $$ = NEW_WHEN($2, $4, $5); 03761 /*% 03762 $$ = dispatch3(when, $2, $4, escape_Qundef($5)); 03763 %*/ 03764 } 03765 ; 03766 03767 cases : opt_else 03768 | case_body 03769 ; 03770 03771 opt_rescue : keyword_rescue exc_list exc_var then 03772 compstmt 03773 opt_rescue 03774 { 03775 /*%%%*/ 03776 if ($3) { 03777 $3 = node_assign($3, NEW_ERRINFO()); 03778 $5 = block_append($3, $5); 03779 } 03780 $$ = NEW_RESBODY($2, $5, $6); 03781 fixpos($$, $2?$2:$5); 03782 /*% 03783 $$ = dispatch4(rescue, 03784 escape_Qundef($2), 03785 escape_Qundef($3), 03786 escape_Qundef($5), 03787 escape_Qundef($6)); 03788 %*/ 03789 } 03790 | none 03791 ; 03792 03793 exc_list : arg_value 03794 { 03795 /*%%%*/ 03796 $$ = NEW_LIST($1); 03797 /*% 03798 $$ = rb_ary_new3(1, $1); 03799 %*/ 03800 } 03801 | mrhs 03802 { 03803 /*%%%*/ 03804 if (!($$ = splat_array($1))) $$ = $1; 03805 /*% 03806 $$ = $1; 03807 %*/ 03808 } 03809 | none 03810 ; 03811 03812 exc_var : tASSOC lhs 03813 { 03814 $$ = $2; 03815 } 03816 | none 03817 ; 03818 03819 opt_ensure : keyword_ensure compstmt 03820 { 03821 /*%%%*/ 03822 $$ = $2; 03823 /*% 03824 $$ = dispatch1(ensure, $2); 03825 %*/ 03826 } 03827 | none 03828 ; 03829 03830 literal : numeric 03831 | symbol 03832 { 03833 /*%%%*/ 03834 $$ = NEW_LIT(ID2SYM($1)); 03835 /*% 03836 $$ = dispatch1(symbol_literal, $1); 03837 %*/ 03838 } 03839 | dsym 03840 ; 03841 03842 strings : string 03843 { 03844 /*%%%*/ 03845 NODE *node = $1; 03846 if (!node) { 03847 node = NEW_STR(STR_NEW0()); 03848 } 03849 else { 03850 node = evstr2dstr(node); 03851 } 03852 $$ = node; 03853 /*% 03854 $$ = $1; 03855 %*/ 03856 } 03857 ; 03858 03859 string : tCHAR 03860 | string1 03861 | string string1 03862 { 03863 /*%%%*/ 03864 $$ = literal_concat($1, $2); 03865 /*% 03866 $$ = dispatch2(string_concat, $1, $2); 03867 %*/ 03868 } 03869 ; 03870 03871 string1 : tSTRING_BEG string_contents tSTRING_END 03872 { 03873 /*%%%*/ 03874 $$ = $2; 03875 /*% 03876 $$ = dispatch1(string_literal, $2); 03877 %*/ 03878 } 03879 ; 03880 03881 xstring : tXSTRING_BEG xstring_contents tSTRING_END 03882 { 03883 /*%%%*/ 03884 NODE *node = $2; 03885 if (!node) { 03886 node = NEW_XSTR(STR_NEW0()); 03887 } 03888 else { 03889 switch (nd_type(node)) { 03890 case NODE_STR: 03891 nd_set_type(node, NODE_XSTR); 03892 break; 03893 case NODE_DSTR: 03894 nd_set_type(node, NODE_DXSTR); 03895 break; 03896 default: 03897 node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node)); 03898 break; 03899 } 03900 } 03901 $$ = node; 03902 /*% 03903 $$ = dispatch1(xstring_literal, $2); 03904 %*/ 03905 } 03906 ; 03907 03908 regexp : tREGEXP_BEG regexp_contents tREGEXP_END 03909 { 03910 /*%%%*/ 03911 int options = $3; 03912 NODE *node = $2; 03913 NODE *list, *prev; 03914 if (!node) { 03915 node = NEW_LIT(reg_compile(STR_NEW0(), options)); 03916 } 03917 else switch (nd_type(node)) { 03918 case NODE_STR: 03919 { 03920 VALUE src = node->nd_lit; 03921 nd_set_type(node, NODE_LIT); 03922 node->nd_lit = reg_compile(src, options); 03923 } 03924 break; 03925 default: 03926 node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node)); 03927 case NODE_DSTR: 03928 if (options & RE_OPTION_ONCE) { 03929 nd_set_type(node, NODE_DREGX_ONCE); 03930 } 03931 else { 03932 nd_set_type(node, NODE_DREGX); 03933 } 03934 node->nd_cflag = options & RE_OPTION_MASK; 03935 if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options); 03936 for (list = (prev = node)->nd_next; list; list = list->nd_next) { 03937 if (nd_type(list->nd_head) == NODE_STR) { 03938 VALUE tail = list->nd_head->nd_lit; 03939 if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) { 03940 VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit; 03941 if (!literal_concat0(parser, lit, tail)) { 03942 node = 0; 03943 break; 03944 } 03945 rb_str_resize(tail, 0); 03946 prev->nd_next = list->nd_next; 03947 rb_gc_force_recycle((VALUE)list->nd_head); 03948 rb_gc_force_recycle((VALUE)list); 03949 list = prev; 03950 } 03951 else { 03952 prev = list; 03953 } 03954 } 03955 else { 03956 prev = 0; 03957 } 03958 } 03959 if (!node->nd_next) { 03960 VALUE src = node->nd_lit; 03961 nd_set_type(node, NODE_LIT); 03962 node->nd_lit = reg_compile(src, options); 03963 } 03964 break; 03965 } 03966 $$ = node; 03967 /*% 03968 $$ = dispatch2(regexp_literal, $2, $3); 03969 %*/ 03970 } 03971 ; 03972 03973 words : tWORDS_BEG ' ' tSTRING_END 03974 { 03975 /*%%%*/ 03976 $$ = NEW_ZARRAY(); 03977 /*% 03978 $$ = dispatch0(words_new); 03979 $$ = dispatch1(array, $$); 03980 %*/ 03981 } 03982 | tWORDS_BEG word_list tSTRING_END 03983 { 03984 /*%%%*/ 03985 $$ = $2; 03986 /*% 03987 $$ = dispatch1(array, $2); 03988 %*/ 03989 } 03990 ; 03991 03992 word_list : /* none */ 03993 { 03994 /*%%%*/ 03995 $$ = 0; 03996 /*% 03997 $$ = dispatch0(words_new); 03998 %*/ 03999 } 04000 | word_list word ' ' 04001 { 04002 /*%%%*/ 04003 $$ = list_append($1, evstr2dstr($2)); 04004 /*% 04005 $$ = dispatch2(words_add, $1, $2); 04006 %*/ 04007 } 04008 ; 04009 04010 word : string_content 04011 /*%c%*/ 04012 /*%c 04013 { 04014 $$ = dispatch0(word_new); 04015 $$ = dispatch2(word_add, $$, $1); 04016 } 04017 %*/ 04018 | word string_content 04019 { 04020 /*%%%*/ 04021 $$ = literal_concat($1, $2); 04022 /*% 04023 $$ = dispatch2(word_add, $1, $2); 04024 %*/ 04025 } 04026 ; 04027 04028 qwords : tQWORDS_BEG ' ' tSTRING_END 04029 { 04030 /*%%%*/ 04031 $$ = NEW_ZARRAY(); 04032 /*% 04033 $$ = dispatch0(qwords_new); 04034 $$ = dispatch1(array, $$); 04035 %*/ 04036 } 04037 | tQWORDS_BEG qword_list tSTRING_END 04038 { 04039 /*%%%*/ 04040 $$ = $2; 04041 /*% 04042 $$ = dispatch1(array, $2); 04043 %*/ 04044 } 04045 ; 04046 04047 qword_list : /* none */ 04048 { 04049 /*%%%*/ 04050 $$ = 0; 04051 /*% 04052 $$ = dispatch0(qwords_new); 04053 %*/ 04054 } 04055 | qword_list tSTRING_CONTENT ' ' 04056 { 04057 /*%%%*/ 04058 $$ = list_append($1, $2); 04059 /*% 04060 $$ = dispatch2(qwords_add, $1, $2); 04061 %*/ 04062 } 04063 ; 04064 04065 string_contents : /* none */ 04066 { 04067 /*%%%*/ 04068 $$ = 0; 04069 /*% 04070 $$ = dispatch0(string_content); 04071 %*/ 04072 } 04073 | string_contents string_content 04074 { 04075 /*%%%*/ 04076 $$ = literal_concat($1, $2); 04077 /*% 04078 $$ = dispatch2(string_add, $1, $2); 04079 %*/ 04080 } 04081 ; 04082 04083 xstring_contents: /* none */ 04084 { 04085 /*%%%*/ 04086 $$ = 0; 04087 /*% 04088 $$ = dispatch0(xstring_new); 04089 %*/ 04090 } 04091 | xstring_contents string_content 04092 { 04093 /*%%%*/ 04094 $$ = literal_concat($1, $2); 04095 /*% 04096 $$ = dispatch2(xstring_add, $1, $2); 04097 %*/ 04098 } 04099 ; 04100 04101 regexp_contents: /* none */ 04102 { 04103 /*%%%*/ 04104 $$ = 0; 04105 /*% 04106 $$ = dispatch0(regexp_new); 04107 %*/ 04108 } 04109 | regexp_contents string_content 04110 { 04111 /*%%%*/ 04112 NODE *head = $1, *tail = $2; 04113 if (!head) { 04114 $$ = tail; 04115 } 04116 else if (!tail) { 04117 $$ = head; 04118 } 04119 else { 04120 switch (nd_type(head)) { 04121 case NODE_STR: 04122 nd_set_type(head, NODE_DSTR); 04123 break; 04124 case NODE_DSTR: 04125 break; 04126 default: 04127 head = list_append(NEW_DSTR(Qnil), head); 04128 break; 04129 } 04130 $$ = list_append(head, tail); 04131 } 04132 /*% 04133 $$ = dispatch2(regexp_add, $1, $2); 04134 %*/ 04135 } 04136 ; 04137 04138 string_content : tSTRING_CONTENT 04139 | tSTRING_DVAR 04140 { 04141 $<node>$ = lex_strterm; 04142 lex_strterm = 0; 04143 lex_state = EXPR_BEG; 04144 } 04145 string_dvar 04146 { 04147 /*%%%*/ 04148 lex_strterm = $<node>2; 04149 $$ = NEW_EVSTR($3); 04150 /*% 04151 lex_strterm = $<node>2; 04152 $$ = dispatch1(string_dvar, $3); 04153 %*/ 04154 } 04155 | tSTRING_DBEG 04156 { 04157 $<val>1 = cond_stack; 04158 $<val>$ = cmdarg_stack; 04159 cond_stack = 0; 04160 cmdarg_stack = 0; 04161 } 04162 { 04163 $<node>$ = lex_strterm; 04164 lex_strterm = 0; 04165 lex_state = EXPR_BEG; 04166 } 04167 compstmt '}' 04168 { 04169 cond_stack = $<val>1; 04170 cmdarg_stack = $<val>2; 04171 lex_strterm = $<node>3; 04172 /*%%%*/ 04173 if ($4) $4->flags &= ~NODE_FL_NEWLINE; 04174 $$ = new_evstr($4); 04175 /*% 04176 $$ = dispatch1(string_embexpr, $4); 04177 %*/ 04178 } 04179 ; 04180 04181 string_dvar : tGVAR 04182 { 04183 /*%%%*/ 04184 $$ = NEW_GVAR($1); 04185 /*% 04186 $$ = dispatch1(var_ref, $1); 04187 %*/ 04188 } 04189 | tIVAR 04190 { 04191 /*%%%*/ 04192 $$ = NEW_IVAR($1); 04193 /*% 04194 $$ = dispatch1(var_ref, $1); 04195 %*/ 04196 } 04197 | tCVAR 04198 { 04199 /*%%%*/ 04200 $$ = NEW_CVAR($1); 04201 /*% 04202 $$ = dispatch1(var_ref, $1); 04203 %*/ 04204 } 04205 | backref 04206 ; 04207 04208 symbol : tSYMBEG sym 04209 { 04210 lex_state = EXPR_END; 04211 /*%%%*/ 04212 $$ = $2; 04213 /*% 04214 $$ = dispatch1(symbol, $2); 04215 %*/ 04216 } 04217 ; 04218 04219 sym : fname 04220 | tIVAR 04221 | tGVAR 04222 | tCVAR 04223 ; 04224 04225 dsym : tSYMBEG xstring_contents tSTRING_END 04226 { 04227 lex_state = EXPR_END; 04228 /*%%%*/ 04229 if (!($$ = $2)) { 04230 $$ = NEW_LIT(ID2SYM(rb_intern(""))); 04231 } 04232 else { 04233 VALUE lit; 04234 04235 switch (nd_type($$)) { 04236 case NODE_DSTR: 04237 nd_set_type($$, NODE_DSYM); 04238 break; 04239 case NODE_STR: 04240 lit = $$->nd_lit; 04241 $$->nd_lit = ID2SYM(rb_intern_str(lit)); 04242 nd_set_type($$, NODE_LIT); 04243 break; 04244 default: 04245 $$ = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST($$)); 04246 break; 04247 } 04248 } 04249 /*% 04250 $$ = dispatch1(dyna_symbol, $2); 04251 %*/ 04252 } 04253 ; 04254 04255 numeric : tINTEGER 04256 | tFLOAT 04257 | tUMINUS_NUM tINTEGER %prec tLOWEST 04258 { 04259 /*%%%*/ 04260 $$ = negate_lit($2); 04261 /*% 04262 $$ = dispatch2(unary, ripper_intern("-@"), $2); 04263 %*/ 04264 } 04265 | tUMINUS_NUM tFLOAT %prec tLOWEST 04266 { 04267 /*%%%*/ 04268 $$ = negate_lit($2); 04269 /*% 04270 $$ = dispatch2(unary, ripper_intern("-@"), $2); 04271 %*/ 04272 } 04273 ; 04274 04275 user_variable : tIDENTIFIER 04276 | tIVAR 04277 | tGVAR 04278 | tCONSTANT 04279 | tCVAR 04280 ; 04281 04282 keyword_variable: keyword_nil {ifndef_ripper($$ = keyword_nil);} 04283 | keyword_self {ifndef_ripper($$ = keyword_self);} 04284 | keyword_true {ifndef_ripper($$ = keyword_true);} 04285 | keyword_false {ifndef_ripper($$ = keyword_false);} 04286 | keyword__FILE__ {ifndef_ripper($$ = keyword__FILE__);} 04287 | keyword__LINE__ {ifndef_ripper($$ = keyword__LINE__);} 04288 | keyword__ENCODING__ {ifndef_ripper($$ = keyword__ENCODING__);} 04289 ; 04290 04291 var_ref : user_variable 04292 { 04293 /*%%%*/ 04294 if (!($$ = gettable($1))) $$ = NEW_BEGIN(0); 04295 /*% 04296 if (id_is_var(get_id($1))) { 04297 $$ = dispatch1(var_ref, $1); 04298 } 04299 else { 04300 $$ = dispatch1(vcall, $1); 04301 } 04302 %*/ 04303 } 04304 | keyword_variable 04305 { 04306 /*%%%*/ 04307 if (!($$ = gettable($1))) $$ = NEW_BEGIN(0); 04308 /*% 04309 $$ = dispatch1(var_ref, $1); 04310 %*/ 04311 } 04312 ; 04313 04314 var_lhs : user_variable 04315 { 04316 $$ = assignable($1, 0); 04317 /*%%%*/ 04318 /*% 04319 $$ = dispatch1(var_field, $$); 04320 %*/ 04321 } 04322 | keyword_variable 04323 { 04324 $$ = assignable($1, 0); 04325 /*%%%*/ 04326 /*% 04327 $$ = dispatch1(var_field, $$); 04328 %*/ 04329 } 04330 ; 04331 04332 backref : tNTH_REF 04333 | tBACK_REF 04334 ; 04335 04336 superclass : term 04337 { 04338 /*%%%*/ 04339 $$ = 0; 04340 /*% 04341 $$ = Qnil; 04342 %*/ 04343 } 04344 | '<' 04345 { 04346 lex_state = EXPR_BEG; 04347 } 04348 expr_value term 04349 { 04350 $$ = $3; 04351 } 04352 | error term 04353 { 04354 /*%%%*/ 04355 yyerrok; 04356 $$ = 0; 04357 /*% 04358 yyerrok; 04359 $$ = Qnil; 04360 %*/ 04361 } 04362 ; 04363 04364 f_arglist : '(' f_args rparen 04365 { 04366 /*%%%*/ 04367 $$ = $2; 04368 /*% 04369 $$ = dispatch1(paren, $2); 04370 %*/ 04371 lex_state = EXPR_BEG; 04372 command_start = TRUE; 04373 } 04374 | f_args term 04375 { 04376 $$ = $1; 04377 lex_state = EXPR_BEG; 04378 command_start = TRUE; 04379 } 04380 ; 04381 04382 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg 04383 { 04384 /*%%%*/ 04385 $$ = new_args($1, $3, $5, 0, $6); 04386 /*% 04387 $$ = params_new($1, $3, $5, Qnil, escape_Qundef($6)); 04388 %*/ 04389 } 04390 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg 04391 { 04392 /*%%%*/ 04393 $$ = new_args($1, $3, $5, $7, $8); 04394 /*% 04395 $$ = params_new($1, $3, $5, $7, escape_Qundef($8)); 04396 %*/ 04397 } 04398 | f_arg ',' f_optarg opt_f_block_arg 04399 { 04400 /*%%%*/ 04401 $$ = new_args($1, $3, 0, 0, $4); 04402 /*% 04403 $$ = params_new($1, $3, Qnil, Qnil, escape_Qundef($4)); 04404 %*/ 04405 } 04406 | f_arg ',' f_optarg ',' f_arg opt_f_block_arg 04407 { 04408 /*%%%*/ 04409 $$ = new_args($1, $3, 0, $5, $6); 04410 /*% 04411 $$ = params_new($1, $3, Qnil, $5, escape_Qundef($6)); 04412 %*/ 04413 } 04414 | f_arg ',' f_rest_arg opt_f_block_arg 04415 { 04416 /*%%%*/ 04417 $$ = new_args($1, 0, $3, 0, $4); 04418 /*% 04419 $$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4)); 04420 %*/ 04421 } 04422 | f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg 04423 { 04424 /*%%%*/ 04425 $$ = new_args($1, 0, $3, $5, $6); 04426 /*% 04427 $$ = params_new($1, Qnil, $3, $5, escape_Qundef($6)); 04428 %*/ 04429 } 04430 | f_arg opt_f_block_arg 04431 { 04432 /*%%%*/ 04433 $$ = new_args($1, 0, 0, 0, $2); 04434 /*% 04435 $$ = params_new($1, Qnil, Qnil, Qnil,escape_Qundef($2)); 04436 %*/ 04437 } 04438 | f_optarg ',' f_rest_arg opt_f_block_arg 04439 { 04440 /*%%%*/ 04441 $$ = new_args(0, $1, $3, 0, $4); 04442 /*% 04443 $$ = params_new(Qnil, $1, $3, Qnil, escape_Qundef($4)); 04444 %*/ 04445 } 04446 | f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg 04447 { 04448 /*%%%*/ 04449 $$ = new_args(0, $1, $3, $5, $6); 04450 /*% 04451 $$ = params_new(Qnil, $1, $3, $5, escape_Qundef($6)); 04452 %*/ 04453 } 04454 | f_optarg opt_f_block_arg 04455 { 04456 /*%%%*/ 04457 $$ = new_args(0, $1, 0, 0, $2); 04458 /*% 04459 $$ = params_new(Qnil, $1, Qnil, Qnil,escape_Qundef($2)); 04460 %*/ 04461 } 04462 | f_optarg ',' f_arg opt_f_block_arg 04463 { 04464 /*%%%*/ 04465 $$ = new_args(0, $1, 0, $3, $4); 04466 /*% 04467 $$ = params_new(Qnil, $1, Qnil, $3, escape_Qundef($4)); 04468 %*/ 04469 } 04470 | f_rest_arg opt_f_block_arg 04471 { 04472 /*%%%*/ 04473 $$ = new_args(0, 0, $1, 0, $2); 04474 /*% 04475 $$ = params_new(Qnil, Qnil, $1, Qnil,escape_Qundef($2)); 04476 %*/ 04477 } 04478 | f_rest_arg ',' f_arg opt_f_block_arg 04479 { 04480 /*%%%*/ 04481 $$ = new_args(0, 0, $1, $3, $4); 04482 /*% 04483 $$ = params_new(Qnil, Qnil, $1, $3, escape_Qundef($4)); 04484 %*/ 04485 } 04486 | f_block_arg 04487 { 04488 /*%%%*/ 04489 $$ = new_args(0, 0, 0, 0, $1); 04490 /*% 04491 $$ = params_new(Qnil, Qnil, Qnil, Qnil, $1); 04492 %*/ 04493 } 04494 | /* none */ 04495 { 04496 /*%%%*/ 04497 $$ = new_args(0, 0, 0, 0, 0); 04498 /*% 04499 $$ = params_new(Qnil, Qnil, Qnil, Qnil, Qnil); 04500 %*/ 04501 } 04502 ; 04503 04504 f_bad_arg : tCONSTANT 04505 { 04506 /*%%%*/ 04507 yyerror("formal argument cannot be a constant"); 04508 $$ = 0; 04509 /*% 04510 $$ = dispatch1(param_error, $1); 04511 %*/ 04512 } 04513 | tIVAR 04514 { 04515 /*%%%*/ 04516 yyerror("formal argument cannot be an instance variable"); 04517 $$ = 0; 04518 /*% 04519 $$ = dispatch1(param_error, $1); 04520 %*/ 04521 } 04522 | tGVAR 04523 { 04524 /*%%%*/ 04525 yyerror("formal argument cannot be a global variable"); 04526 $$ = 0; 04527 /*% 04528 $$ = dispatch1(param_error, $1); 04529 %*/ 04530 } 04531 | tCVAR 04532 { 04533 /*%%%*/ 04534 yyerror("formal argument cannot be a class variable"); 04535 $$ = 0; 04536 /*% 04537 $$ = dispatch1(param_error, $1); 04538 %*/ 04539 } 04540 ; 04541 04542 f_norm_arg : f_bad_arg 04543 | tIDENTIFIER 04544 { 04545 formal_argument(get_id($1)); 04546 $$ = $1; 04547 } 04548 ; 04549 04550 f_arg_item : f_norm_arg 04551 { 04552 arg_var(get_id($1)); 04553 /*%%%*/ 04554 $$ = NEW_ARGS_AUX($1, 1); 04555 /*% 04556 $$ = get_value($1); 04557 %*/ 04558 } 04559 | tLPAREN f_margs rparen 04560 { 04561 ID tid = internal_id(); 04562 arg_var(tid); 04563 /*%%%*/ 04564 if (dyna_in_block()) { 04565 $2->nd_value = NEW_DVAR(tid); 04566 } 04567 else { 04568 $2->nd_value = NEW_LVAR(tid); 04569 } 04570 $$ = NEW_ARGS_AUX(tid, 1); 04571 $$->nd_next = $2; 04572 /*% 04573 $$ = dispatch1(mlhs_paren, $2); 04574 %*/ 04575 } 04576 ; 04577 04578 f_arg : f_arg_item 04579 /*%c%*/ 04580 /*%c 04581 { 04582 $$ = rb_ary_new3(1, $1); 04583 } 04584 c%*/ 04585 | f_arg ',' f_arg_item 04586 { 04587 /*%%%*/ 04588 $$ = $1; 04589 $$->nd_plen++; 04590 $$->nd_next = block_append($$->nd_next, $3->nd_next); 04591 rb_gc_force_recycle((VALUE)$3); 04592 /*% 04593 $$ = rb_ary_push($1, $3); 04594 %*/ 04595 } 04596 ; 04597 04598 f_opt : tIDENTIFIER '=' arg_value 04599 { 04600 arg_var(formal_argument(get_id($1))); 04601 $$ = assignable($1, $3); 04602 /*%%%*/ 04603 $$ = NEW_OPT_ARG(0, $$); 04604 /*% 04605 $$ = rb_assoc_new($$, $3); 04606 %*/ 04607 } 04608 ; 04609 04610 f_block_opt : tIDENTIFIER '=' primary_value 04611 { 04612 arg_var(formal_argument(get_id($1))); 04613 $$ = assignable($1, $3); 04614 /*%%%*/ 04615 $$ = NEW_OPT_ARG(0, $$); 04616 /*% 04617 $$ = rb_assoc_new($$, $3); 04618 %*/ 04619 } 04620 ; 04621 04622 f_block_optarg : f_block_opt 04623 { 04624 /*%%%*/ 04625 $$ = $1; 04626 /*% 04627 $$ = rb_ary_new3(1, $1); 04628 %*/ 04629 } 04630 | f_block_optarg ',' f_block_opt 04631 { 04632 /*%%%*/ 04633 NODE *opts = $1; 04634 04635 while (opts->nd_next) { 04636 opts = opts->nd_next; 04637 } 04638 opts->nd_next = $3; 04639 $$ = $1; 04640 /*% 04641 $$ = rb_ary_push($1, $3); 04642 %*/ 04643 } 04644 ; 04645 04646 f_optarg : f_opt 04647 { 04648 /*%%%*/ 04649 $$ = $1; 04650 /*% 04651 $$ = rb_ary_new3(1, $1); 04652 %*/ 04653 } 04654 | f_optarg ',' f_opt 04655 { 04656 /*%%%*/ 04657 NODE *opts = $1; 04658 04659 while (opts->nd_next) { 04660 opts = opts->nd_next; 04661 } 04662 opts->nd_next = $3; 04663 $$ = $1; 04664 /*% 04665 $$ = rb_ary_push($1, $3); 04666 %*/ 04667 } 04668 ; 04669 04670 restarg_mark : '*' 04671 | tSTAR 04672 ; 04673 04674 f_rest_arg : restarg_mark tIDENTIFIER 04675 { 04676 /*%%%*/ 04677 if (!is_local_id($2)) 04678 yyerror("rest argument must be local variable"); 04679 /*% %*/ 04680 arg_var(shadowing_lvar(get_id($2))); 04681 /*%%%*/ 04682 $$ = $2; 04683 /*% 04684 $$ = dispatch1(rest_param, $2); 04685 %*/ 04686 } 04687 | restarg_mark 04688 { 04689 /*%%%*/ 04690 $$ = internal_id(); 04691 arg_var($$); 04692 /*% 04693 $$ = dispatch1(rest_param, Qnil); 04694 %*/ 04695 } 04696 ; 04697 04698 blkarg_mark : '&' 04699 | tAMPER 04700 ; 04701 04702 f_block_arg : blkarg_mark tIDENTIFIER 04703 { 04704 /*%%%*/ 04705 if (!is_local_id($2)) 04706 yyerror("block argument must be local variable"); 04707 else if (!dyna_in_block() && local_id($2)) 04708 yyerror("duplicated block argument name"); 04709 /*% %*/ 04710 arg_var(shadowing_lvar(get_id($2))); 04711 /*%%%*/ 04712 $$ = $2; 04713 /*% 04714 $$ = dispatch1(blockarg, $2); 04715 %*/ 04716 } 04717 ; 04718 04719 opt_f_block_arg : ',' f_block_arg 04720 { 04721 $$ = $2; 04722 } 04723 | none 04724 { 04725 /*%%%*/ 04726 $$ = 0; 04727 /*% 04728 $$ = Qundef; 04729 %*/ 04730 } 04731 ; 04732 04733 singleton : var_ref 04734 { 04735 /*%%%*/ 04736 value_expr($1); 04737 $$ = $1; 04738 if (!$$) $$ = NEW_NIL(); 04739 /*% 04740 $$ = $1; 04741 %*/ 04742 } 04743 | '(' {lex_state = EXPR_BEG;} expr rparen 04744 { 04745 /*%%%*/ 04746 if ($3 == 0) { 04747 yyerror("can't define singleton method for ()."); 04748 } 04749 else { 04750 switch (nd_type($3)) { 04751 case NODE_STR: 04752 case NODE_DSTR: 04753 case NODE_XSTR: 04754 case NODE_DXSTR: 04755 case NODE_DREGX: 04756 case NODE_LIT: 04757 case NODE_ARRAY: 04758 case NODE_ZARRAY: 04759 yyerror("can't define singleton method for literals"); 04760 default: 04761 value_expr($3); 04762 break; 04763 } 04764 } 04765 $$ = $3; 04766 /*% 04767 $$ = dispatch1(paren, $3); 04768 %*/ 04769 } 04770 ; 04771 04772 assoc_list : none 04773 | assocs trailer 04774 { 04775 /*%%%*/ 04776 $$ = $1; 04777 /*% 04778 $$ = dispatch1(assoclist_from_args, $1); 04779 %*/ 04780 } 04781 ; 04782 04783 assocs : assoc 04784 /*%c%*/ 04785 /*%c 04786 { 04787 $$ = rb_ary_new3(1, $1); 04788 } 04789 %*/ 04790 | assocs ',' assoc 04791 { 04792 /*%%%*/ 04793 $$ = list_concat($1, $3); 04794 /*% 04795 $$ = rb_ary_push($1, $3); 04796 %*/ 04797 } 04798 ; 04799 04800 assoc : arg_value tASSOC arg_value 04801 { 04802 /*%%%*/ 04803 $$ = list_append(NEW_LIST($1), $3); 04804 /*% 04805 $$ = dispatch2(assoc_new, $1, $3); 04806 %*/ 04807 } 04808 | tLABEL arg_value 04809 { 04810 /*%%%*/ 04811 $$ = list_append(NEW_LIST(NEW_LIT(ID2SYM($1))), $2); 04812 /*% 04813 $$ = dispatch2(assoc_new, $1, $2); 04814 %*/ 04815 } 04816 ; 04817 04818 operation : tIDENTIFIER 04819 | tCONSTANT 04820 | tFID 04821 ; 04822 04823 operation2 : tIDENTIFIER 04824 | tCONSTANT 04825 | tFID 04826 | op 04827 ; 04828 04829 operation3 : tIDENTIFIER 04830 | tFID 04831 | op 04832 ; 04833 04834 dot_or_colon : '.' 04835 /*%c%*/ 04836 /*%c 04837 { $$ = $<val>1; } 04838 %*/ 04839 | tCOLON2 04840 /*%c%*/ 04841 /*%c 04842 { $$ = $<val>1; } 04843 %*/ 04844 ; 04845 04846 opt_terms : /* none */ 04847 | terms 04848 ; 04849 04850 opt_nl : /* none */ 04851 | '\n' 04852 ; 04853 04854 rparen : opt_nl ')' 04855 ; 04856 04857 rbracket : opt_nl ']' 04858 ; 04859 04860 trailer : /* none */ 04861 | '\n' 04862 | ',' 04863 ; 04864 04865 term : ';' {yyerrok;} 04866 | '\n' 04867 ; 04868 04869 terms : term 04870 | terms ';' {yyerrok;} 04871 ; 04872 04873 none : /* none */ 04874 { 04875 /*%%%*/ 04876 $$ = 0; 04877 /*% 04878 $$ = Qundef; 04879 %*/ 04880 } 04881 ; 04882 %% 04883 # undef parser 04884 # undef yylex 04885 # undef yylval 04886 # define yylval (*((YYSTYPE*)(parser->parser_yylval))) 04887 04888 static int parser_regx_options(struct parser_params*); 04889 static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**); 04890 static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc); 04891 static int parser_parse_string(struct parser_params*,NODE*); 04892 static int parser_here_document(struct parser_params*,NODE*); 04893 04894 04895 # define nextc() parser_nextc(parser) 04896 # define pushback(c) parser_pushback(parser, (c)) 04897 # define newtok() parser_newtok(parser) 04898 # define tokspace(n) parser_tokspace(parser, (n)) 04899 # define tokadd(c) parser_tokadd(parser, (c)) 04900 # define tok_hex(numlen) parser_tok_hex(parser, (numlen)) 04901 # define read_escape(flags,e) parser_read_escape(parser, (flags), (e)) 04902 # define tokadd_escape(e) parser_tokadd_escape(parser, (e)) 04903 # define regx_options() parser_regx_options(parser) 04904 # define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,(f),(t),(p),(n),(e)) 04905 # define parse_string(n) parser_parse_string(parser,(n)) 04906 # define tokaddmbc(c, enc) parser_tokaddmbc(parser, (c), (enc)) 04907 # define here_document(n) parser_here_document(parser,(n)) 04908 # define heredoc_identifier() parser_heredoc_identifier(parser) 04909 # define heredoc_restore(n) parser_heredoc_restore(parser,(n)) 04910 # define whole_match_p(e,l,i) parser_whole_match_p(parser,(e),(l),(i)) 04911 04912 #ifndef RIPPER 04913 # define set_yylval_str(x) (yylval.node = NEW_STR(x)) 04914 # define set_yylval_num(x) (yylval.num = (x)) 04915 # define set_yylval_id(x) (yylval.id = (x)) 04916 # define set_yylval_name(x) (yylval.id = (x)) 04917 # define set_yylval_literal(x) (yylval.node = NEW_LIT(x)) 04918 # define set_yylval_node(x) (yylval.node = (x)) 04919 # define yylval_id() (yylval.id) 04920 #else 04921 static inline VALUE 04922 ripper_yylval_id(ID x) 04923 { 04924 return (VALUE)NEW_LASGN(x, ID2SYM(x)); 04925 } 04926 # define set_yylval_str(x) (void)(x) 04927 # define set_yylval_num(x) (void)(x) 04928 # define set_yylval_id(x) (void)(x) 04929 # define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x)) 04930 # define set_yylval_literal(x) (void)(x) 04931 # define set_yylval_node(x) (void)(x) 04932 # define yylval_id() yylval.id 04933 #endif 04934 04935 #ifndef RIPPER 04936 #define ripper_flush(p) (void)(p) 04937 #else 04938 #define ripper_flush(p) ((p)->tokp = (p)->parser_lex_p) 04939 04940 #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)) 04941 04942 static int 04943 ripper_has_scan_event(struct parser_params *parser) 04944 { 04945 04946 if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp"); 04947 return lex_p > parser->tokp; 04948 } 04949 04950 static VALUE 04951 ripper_scan_event_val(struct parser_params *parser, int t) 04952 { 04953 VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp); 04954 VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str); 04955 ripper_flush(parser); 04956 return rval; 04957 } 04958 04959 static void 04960 ripper_dispatch_scan_event(struct parser_params *parser, int t) 04961 { 04962 if (!ripper_has_scan_event(parser)) return; 04963 yylval_rval = ripper_scan_event_val(parser, t); 04964 } 04965 04966 static void 04967 ripper_dispatch_ignored_scan_event(struct parser_params *parser, int t) 04968 { 04969 if (!ripper_has_scan_event(parser)) return; 04970 (void)ripper_scan_event_val(parser, t); 04971 } 04972 04973 static void 04974 ripper_dispatch_delayed_token(struct parser_params *parser, int t) 04975 { 04976 int saved_line = ruby_sourceline; 04977 const char *saved_tokp = parser->tokp; 04978 04979 ruby_sourceline = parser->delayed_line; 04980 parser->tokp = lex_pbeg + parser->delayed_col; 04981 yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed); 04982 parser->delayed = Qnil; 04983 ruby_sourceline = saved_line; 04984 parser->tokp = saved_tokp; 04985 } 04986 #endif /* RIPPER */ 04987 04988 #include "ruby/regex.h" 04989 #include "ruby/util.h" 04990 04991 /* We remove any previous definition of `SIGN_EXTEND_CHAR', 04992 since ours (we hope) works properly with all combinations of 04993 machines, compilers, `char' and `unsigned char' argument types. 04994 (Per Bothner suggested the basic approach.) */ 04995 #undef SIGN_EXTEND_CHAR 04996 #if __STDC__ 04997 # define SIGN_EXTEND_CHAR(c) ((signed char)(c)) 04998 #else /* not __STDC__ */ 04999 /* As in Harbison and Steele. */ 05000 # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128) 05001 #endif 05002 05003 #define parser_encoding_name() (parser->enc->name) 05004 #define parser_mbclen() mbclen((lex_p-1),lex_pend,parser->enc) 05005 #define parser_precise_mbclen() rb_enc_precise_mbclen((lex_p-1),lex_pend,parser->enc) 05006 #define is_identchar(p,e,enc) (rb_enc_isalnum(*(p),(enc)) || (*(p)) == '_' || !ISASCII(*(p))) 05007 #define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,parser->enc)) 05008 05009 #define parser_isascii() ISASCII(*(lex_p-1)) 05010 05011 #ifndef RIPPER 05012 static int 05013 token_info_get_column(struct parser_params *parser, const char *token) 05014 { 05015 int column = 1; 05016 const char *p, *pend = lex_p - strlen(token); 05017 for (p = lex_pbeg; p < pend; p++) { 05018 if (*p == '\t') { 05019 column = (((column - 1) / 8) + 1) * 8; 05020 } 05021 column++; 05022 } 05023 return column; 05024 } 05025 05026 static int 05027 token_info_has_nonspaces(struct parser_params *parser, const char *token) 05028 { 05029 const char *p, *pend = lex_p - strlen(token); 05030 for (p = lex_pbeg; p < pend; p++) { 05031 if (*p != ' ' && *p != '\t') { 05032 return 1; 05033 } 05034 } 05035 return 0; 05036 } 05037 05038 #undef token_info_push 05039 static void 05040 token_info_push(struct parser_params *parser, const char *token) 05041 { 05042 token_info *ptinfo; 05043 05044 if (!parser->parser_token_info_enabled) return; 05045 ptinfo = ALLOC(token_info); 05046 ptinfo->token = token; 05047 ptinfo->linenum = ruby_sourceline; 05048 ptinfo->column = token_info_get_column(parser, token); 05049 ptinfo->nonspc = token_info_has_nonspaces(parser, token); 05050 ptinfo->next = parser->parser_token_info; 05051 05052 parser->parser_token_info = ptinfo; 05053 } 05054 05055 #undef token_info_pop 05056 static void 05057 token_info_pop(struct parser_params *parser, const char *token) 05058 { 05059 int linenum; 05060 token_info *ptinfo = parser->parser_token_info; 05061 05062 if (!ptinfo) return; 05063 parser->parser_token_info = ptinfo->next; 05064 if (token_info_get_column(parser, token) == ptinfo->column) { /* OK */ 05065 goto finish; 05066 } 05067 linenum = ruby_sourceline; 05068 if (linenum == ptinfo->linenum) { /* SKIP */ 05069 goto finish; 05070 } 05071 if (token_info_has_nonspaces(parser, token) || ptinfo->nonspc) { /* SKIP */ 05072 goto finish; 05073 } 05074 if (parser->parser_token_info_enabled) { 05075 rb_compile_warn(ruby_sourcefile, linenum, 05076 "mismatched indentations at '%s' with '%s' at %d", 05077 token, ptinfo->token, ptinfo->linenum); 05078 } 05079 05080 finish: 05081 xfree(ptinfo); 05082 } 05083 #endif /* RIPPER */ 05084 05085 static int 05086 parser_yyerror(struct parser_params *parser, const char *msg) 05087 { 05088 #ifndef RIPPER 05089 const int max_line_margin = 30; 05090 const char *p, *pe; 05091 char *buf; 05092 long len; 05093 int i; 05094 05095 compile_error(PARSER_ARG "%s", msg); 05096 p = lex_p; 05097 while (lex_pbeg <= p) { 05098 if (*p == '\n') break; 05099 p--; 05100 } 05101 p++; 05102 05103 pe = lex_p; 05104 while (pe < lex_pend) { 05105 if (*pe == '\n') break; 05106 pe++; 05107 } 05108 05109 len = pe - p; 05110 if (len > 4) { 05111 char *p2; 05112 const char *pre = "", *post = ""; 05113 05114 if (len > max_line_margin * 2 + 10) { 05115 if (lex_p - p > max_line_margin) { 05116 p = rb_enc_prev_char(p, lex_p - max_line_margin, pe, rb_enc_get(lex_lastline)); 05117 pre = "..."; 05118 } 05119 if (pe - lex_p > max_line_margin) { 05120 pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, pe, rb_enc_get(lex_lastline)); 05121 post = "..."; 05122 } 05123 len = pe - p; 05124 } 05125 buf = ALLOCA_N(char, len+2); 05126 MEMCPY(buf, p, char, len); 05127 buf[len] = '\0'; 05128 rb_compile_error_append("%s%s%s", pre, buf, post); 05129 05130 i = (int)(lex_p - p); 05131 p2 = buf; pe = buf + len; 05132 05133 while (p2 < pe) { 05134 if (*p2 != '\t') *p2 = ' '; 05135 p2++; 05136 } 05137 buf[i] = '^'; 05138 buf[i+1] = '\0'; 05139 rb_compile_error_append("%s%s", pre, buf); 05140 } 05141 #else 05142 dispatch1(parse_error, STR_NEW2(msg)); 05143 #endif /* !RIPPER */ 05144 return 0; 05145 } 05146 05147 static void parser_prepare(struct parser_params *parser); 05148 05149 #ifndef RIPPER 05150 static VALUE 05151 debug_lines(const char *f) 05152 { 05153 ID script_lines; 05154 CONST_ID(script_lines, "SCRIPT_LINES__"); 05155 if (rb_const_defined_at(rb_cObject, script_lines)) { 05156 VALUE hash = rb_const_get_at(rb_cObject, script_lines); 05157 if (TYPE(hash) == T_HASH) { 05158 VALUE fname = rb_external_str_new_with_enc(f, strlen(f), rb_filesystem_encoding()); 05159 VALUE lines = rb_ary_new(); 05160 rb_hash_aset(hash, fname, lines); 05161 return lines; 05162 } 05163 } 05164 return 0; 05165 } 05166 05167 static VALUE 05168 coverage(const char *f, int n) 05169 { 05170 VALUE coverages = rb_get_coverages(); 05171 if (RTEST(coverages) && RBASIC(coverages)->klass == 0) { 05172 VALUE fname = rb_external_str_new_with_enc(f, strlen(f), rb_filesystem_encoding()); 05173 VALUE lines = rb_ary_new2(n); 05174 int i; 05175 RBASIC(lines)->klass = 0; 05176 for (i = 0; i < n; i++) RARRAY_PTR(lines)[i] = Qnil; 05177 RARRAY(lines)->as.heap.len = n; 05178 rb_hash_aset(coverages, fname, lines); 05179 return lines; 05180 } 05181 return 0; 05182 } 05183 05184 static int 05185 e_option_supplied(struct parser_params *parser) 05186 { 05187 return strcmp(ruby_sourcefile, "-e") == 0; 05188 } 05189 05190 static VALUE 05191 yycompile0(VALUE arg, int tracing) 05192 { 05193 int n; 05194 NODE *tree; 05195 struct parser_params *parser = (struct parser_params *)arg; 05196 05197 if (!compile_for_eval && rb_safe_level() == 0) { 05198 ruby_debug_lines = debug_lines(ruby_sourcefile); 05199 if (ruby_debug_lines && ruby_sourceline > 0) { 05200 VALUE str = STR_NEW0(); 05201 n = ruby_sourceline; 05202 do { 05203 rb_ary_push(ruby_debug_lines, str); 05204 } while (--n); 05205 } 05206 05207 if (!e_option_supplied(parser)) { 05208 ruby_coverage = coverage(ruby_sourcefile, ruby_sourceline); 05209 } 05210 } 05211 05212 parser_prepare(parser); 05213 deferred_nodes = 0; 05214 #ifndef RIPPER 05215 parser->parser_token_info_enabled = !compile_for_eval && RTEST(ruby_verbose); 05216 #endif 05217 n = yyparse((void*)parser); 05218 ruby_debug_lines = 0; 05219 ruby_coverage = 0; 05220 compile_for_eval = 0; 05221 05222 lex_strterm = 0; 05223 lex_p = lex_pbeg = lex_pend = 0; 05224 lex_lastline = lex_nextline = 0; 05225 if (parser->nerr) { 05226 return 0; 05227 } 05228 tree = ruby_eval_tree; 05229 if (!tree) { 05230 tree = NEW_NIL(); 05231 } 05232 else if (ruby_eval_tree_begin) { 05233 tree->nd_body = NEW_PRELUDE(ruby_eval_tree_begin, tree->nd_body); 05234 } 05235 return (VALUE)tree; 05236 } 05237 05238 static NODE* 05239 yycompile(struct parser_params *parser, const char *f, int line) 05240 { 05241 ruby_sourcefile = ruby_strdup(f); 05242 ruby_sourceline = line - 1; 05243 return (NODE *)ruby_suppress_tracing(yycompile0, (VALUE)parser, TRUE); 05244 } 05245 #endif /* !RIPPER */ 05246 05247 static rb_encoding * 05248 must_be_ascii_compatible(VALUE s) 05249 { 05250 rb_encoding *enc = rb_enc_get(s); 05251 if (!rb_enc_asciicompat(enc)) { 05252 rb_raise(rb_eArgError, "invalid source encoding"); 05253 } 05254 return enc; 05255 } 05256 05257 static VALUE 05258 lex_get_str(struct parser_params *parser, VALUE s) 05259 { 05260 char *beg, *end, *pend; 05261 rb_encoding *enc = must_be_ascii_compatible(s); 05262 05263 beg = RSTRING_PTR(s); 05264 if (lex_gets_ptr) { 05265 if (RSTRING_LEN(s) == lex_gets_ptr) return Qnil; 05266 beg += lex_gets_ptr; 05267 } 05268 pend = RSTRING_PTR(s) + RSTRING_LEN(s); 05269 end = beg; 05270 while (end < pend) { 05271 if (*end++ == '\n') break; 05272 } 05273 lex_gets_ptr = end - RSTRING_PTR(s); 05274 return rb_enc_str_new(beg, end - beg, enc); 05275 } 05276 05277 static VALUE 05278 lex_getline(struct parser_params *parser) 05279 { 05280 VALUE line = (*parser->parser_lex_gets)(parser, parser->parser_lex_input); 05281 if (NIL_P(line)) return line; 05282 must_be_ascii_compatible(line); 05283 #ifndef RIPPER 05284 if (ruby_debug_lines) { 05285 rb_enc_associate(line, parser->enc); 05286 rb_ary_push(ruby_debug_lines, line); 05287 } 05288 if (ruby_coverage) { 05289 rb_ary_push(ruby_coverage, Qnil); 05290 } 05291 #endif 05292 return line; 05293 } 05294 05295 #ifdef RIPPER 05296 static rb_data_type_t parser_data_type; 05297 #else 05298 static const rb_data_type_t parser_data_type; 05299 05300 static NODE* 05301 parser_compile_string(volatile VALUE vparser, const char *f, VALUE s, int line) 05302 { 05303 struct parser_params *parser; 05304 NODE *node; 05305 volatile VALUE tmp; 05306 05307 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); 05308 lex_gets = lex_get_str; 05309 lex_gets_ptr = 0; 05310 lex_input = s; 05311 lex_pbeg = lex_p = lex_pend = 0; 05312 compile_for_eval = rb_parse_in_eval(); 05313 05314 node = yycompile(parser, f, line); 05315 tmp = vparser; /* prohibit tail call optimization */ 05316 05317 return node; 05318 } 05319 05320 NODE* 05321 rb_compile_string(const char *f, VALUE s, int line) 05322 { 05323 must_be_ascii_compatible(s); 05324 return parser_compile_string(rb_parser_new(), f, s, line); 05325 } 05326 05327 NODE* 05328 rb_parser_compile_string(volatile VALUE vparser, const char *f, VALUE s, int line) 05329 { 05330 must_be_ascii_compatible(s); 05331 return parser_compile_string(vparser, f, s, line); 05332 } 05333 05334 NODE* 05335 rb_compile_cstr(const char *f, const char *s, int len, int line) 05336 { 05337 VALUE str = rb_str_new(s, len); 05338 return parser_compile_string(rb_parser_new(), f, str, line); 05339 } 05340 05341 NODE* 05342 rb_parser_compile_cstr(volatile VALUE vparser, const char *f, const char *s, int len, int line) 05343 { 05344 VALUE str = rb_str_new(s, len); 05345 return parser_compile_string(vparser, f, str, line); 05346 } 05347 05348 static VALUE 05349 lex_io_gets(struct parser_params *parser, VALUE io) 05350 { 05351 return rb_io_gets(io); 05352 } 05353 05354 NODE* 05355 rb_compile_file(const char *f, VALUE file, int start) 05356 { 05357 VALUE volatile vparser = rb_parser_new(); 05358 05359 return rb_parser_compile_file(vparser, f, file, start); 05360 } 05361 05362 NODE* 05363 rb_parser_compile_file(volatile VALUE vparser, const char *f, VALUE file, int start) 05364 { 05365 struct parser_params *parser; 05366 volatile VALUE tmp; 05367 NODE *node; 05368 05369 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); 05370 lex_gets = lex_io_gets; 05371 lex_input = file; 05372 lex_pbeg = lex_p = lex_pend = 0; 05373 compile_for_eval = rb_parse_in_eval(); 05374 05375 node = yycompile(parser, f, start); 05376 tmp = vparser; /* prohibit tail call optimization */ 05377 05378 return node; 05379 } 05380 #endif /* !RIPPER */ 05381 05382 #define STR_FUNC_ESCAPE 0x01 05383 #define STR_FUNC_EXPAND 0x02 05384 #define STR_FUNC_REGEXP 0x04 05385 #define STR_FUNC_QWORDS 0x08 05386 #define STR_FUNC_SYMBOL 0x10 05387 #define STR_FUNC_INDENT 0x20 05388 05389 enum string_type { 05390 str_squote = (0), 05391 str_dquote = (STR_FUNC_EXPAND), 05392 str_xquote = (STR_FUNC_EXPAND), 05393 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND), 05394 str_sword = (STR_FUNC_QWORDS), 05395 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND), 05396 str_ssym = (STR_FUNC_SYMBOL), 05397 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND) 05398 }; 05399 05400 static VALUE 05401 parser_str_new(const char *p, long n, rb_encoding *enc, int func, rb_encoding *enc0) 05402 { 05403 VALUE str; 05404 05405 str = rb_enc_str_new(p, n, enc); 05406 if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) { 05407 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) { 05408 } 05409 else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) { 05410 rb_enc_associate(str, rb_ascii8bit_encoding()); 05411 } 05412 } 05413 05414 return str; 05415 } 05416 05417 #define lex_goto_eol(parser) ((parser)->parser_lex_p = (parser)->parser_lex_pend) 05418 #define lex_eol_p() (lex_p >= lex_pend) 05419 #define peek(c) peek_n((c), 0) 05420 #define peek_n(c,n) (lex_p+(n) < lex_pend && (c) == (unsigned char)lex_p[n]) 05421 05422 static inline int 05423 parser_nextc(struct parser_params *parser) 05424 { 05425 int c; 05426 05427 if (lex_p == lex_pend) { 05428 VALUE v = lex_nextline; 05429 lex_nextline = 0; 05430 if (!v) { 05431 if (parser->eofp) 05432 return -1; 05433 05434 if (!lex_input || NIL_P(v = lex_getline(parser))) { 05435 parser->eofp = Qtrue; 05436 lex_goto_eol(parser); 05437 return -1; 05438 } 05439 } 05440 { 05441 #ifdef RIPPER 05442 if (parser->tokp < lex_pend) { 05443 if (NIL_P(parser->delayed)) { 05444 parser->delayed = rb_str_buf_new(1024); 05445 rb_enc_associate(parser->delayed, parser->enc); 05446 rb_str_buf_cat(parser->delayed, 05447 parser->tokp, lex_pend - parser->tokp); 05448 parser->delayed_line = ruby_sourceline; 05449 parser->delayed_col = (int)(parser->tokp - lex_pbeg); 05450 } 05451 else { 05452 rb_str_buf_cat(parser->delayed, 05453 parser->tokp, lex_pend - parser->tokp); 05454 } 05455 } 05456 #endif 05457 if (heredoc_end > 0) { 05458 ruby_sourceline = heredoc_end; 05459 heredoc_end = 0; 05460 } 05461 ruby_sourceline++; 05462 parser->line_count++; 05463 lex_pbeg = lex_p = RSTRING_PTR(v); 05464 lex_pend = lex_p + RSTRING_LEN(v); 05465 ripper_flush(parser); 05466 lex_lastline = v; 05467 } 05468 } 05469 c = (unsigned char)*lex_p++; 05470 if (c == '\r' && peek('\n')) { 05471 lex_p++; 05472 c = '\n'; 05473 } 05474 05475 return c; 05476 } 05477 05478 static void 05479 parser_pushback(struct parser_params *parser, int c) 05480 { 05481 if (c == -1) return; 05482 lex_p--; 05483 if (lex_p > lex_pbeg && lex_p[0] == '\n' && lex_p[-1] == '\r') { 05484 lex_p--; 05485 } 05486 } 05487 05488 #define was_bol() (lex_p == lex_pbeg + 1) 05489 05490 #define tokfix() (tokenbuf[tokidx]='\0') 05491 #define tok() tokenbuf 05492 #define toklen() tokidx 05493 #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0) 05494 05495 static char* 05496 parser_newtok(struct parser_params *parser) 05497 { 05498 tokidx = 0; 05499 if (!tokenbuf) { 05500 toksiz = 60; 05501 tokenbuf = ALLOC_N(char, 60); 05502 } 05503 if (toksiz > 4096) { 05504 toksiz = 60; 05505 REALLOC_N(tokenbuf, char, 60); 05506 } 05507 return tokenbuf; 05508 } 05509 05510 static char * 05511 parser_tokspace(struct parser_params *parser, int n) 05512 { 05513 tokidx += n; 05514 05515 if (tokidx >= toksiz) { 05516 do {toksiz *= 2;} while (toksiz < tokidx); 05517 REALLOC_N(tokenbuf, char, toksiz); 05518 } 05519 return &tokenbuf[tokidx-n]; 05520 } 05521 05522 static void 05523 parser_tokadd(struct parser_params *parser, int c) 05524 { 05525 tokenbuf[tokidx++] = (char)c; 05526 if (tokidx >= toksiz) { 05527 toksiz *= 2; 05528 REALLOC_N(tokenbuf, char, toksiz); 05529 } 05530 } 05531 05532 static int 05533 parser_tok_hex(struct parser_params *parser, size_t *numlen) 05534 { 05535 int c; 05536 05537 c = scan_hex(lex_p, 2, numlen); 05538 if (!*numlen) { 05539 yyerror("invalid hex escape"); 05540 return 0; 05541 } 05542 lex_p += *numlen; 05543 return c; 05544 } 05545 05546 #define tokcopy(n) memcpy(tokspace(n), lex_p - (n), (n)) 05547 05548 static int 05549 parser_tokadd_utf8(struct parser_params *parser, rb_encoding **encp, 05550 int string_literal, int symbol_literal, int regexp_literal) 05551 { 05552 /* 05553 * If string_literal is true, then we allow multiple codepoints 05554 * in \u{}, and add the codepoints to the current token. 05555 * Otherwise we're parsing a character literal and return a single 05556 * codepoint without adding it 05557 */ 05558 05559 int codepoint; 05560 size_t numlen; 05561 05562 if (regexp_literal) { tokadd('\\'); tokadd('u'); } 05563 05564 if (peek('{')) { /* handle \u{...} form */ 05565 do { 05566 if (regexp_literal) { tokadd(*lex_p); } 05567 nextc(); 05568 codepoint = scan_hex(lex_p, 6, &numlen); 05569 if (numlen == 0) { 05570 yyerror("invalid Unicode escape"); 05571 return 0; 05572 } 05573 if (codepoint > 0x10ffff) { 05574 yyerror("invalid Unicode codepoint (too large)"); 05575 return 0; 05576 } 05577 lex_p += numlen; 05578 if (regexp_literal) { 05579 tokcopy((int)numlen); 05580 } 05581 else if (codepoint >= 0x80) { 05582 *encp = UTF8_ENC(); 05583 if (string_literal) tokaddmbc(codepoint, *encp); 05584 } 05585 else if (string_literal) { 05586 tokadd(codepoint); 05587 } 05588 } while (string_literal && (peek(' ') || peek('\t'))); 05589 05590 if (!peek('}')) { 05591 yyerror("unterminated Unicode escape"); 05592 return 0; 05593 } 05594 05595 if (regexp_literal) { tokadd('}'); } 05596 nextc(); 05597 } 05598 else { /* handle \uxxxx form */ 05599 codepoint = scan_hex(lex_p, 4, &numlen); 05600 if (numlen < 4) { 05601 yyerror("invalid Unicode escape"); 05602 return 0; 05603 } 05604 lex_p += 4; 05605 if (regexp_literal) { 05606 tokcopy(4); 05607 } 05608 else if (codepoint >= 0x80) { 05609 *encp = UTF8_ENC(); 05610 if (string_literal) tokaddmbc(codepoint, *encp); 05611 } 05612 else if (string_literal) { 05613 tokadd(codepoint); 05614 } 05615 } 05616 05617 return codepoint; 05618 } 05619 05620 #define ESCAPE_CONTROL 1 05621 #define ESCAPE_META 2 05622 05623 static int 05624 parser_read_escape(struct parser_params *parser, int flags, 05625 rb_encoding **encp) 05626 { 05627 int c; 05628 size_t numlen; 05629 05630 switch (c = nextc()) { 05631 case '\\': /* Backslash */ 05632 return c; 05633 05634 case 'n': /* newline */ 05635 return '\n'; 05636 05637 case 't': /* horizontal tab */ 05638 return '\t'; 05639 05640 case 'r': /* carriage-return */ 05641 return '\r'; 05642 05643 case 'f': /* form-feed */ 05644 return '\f'; 05645 05646 case 'v': /* vertical tab */ 05647 return '\13'; 05648 05649 case 'a': /* alarm(bell) */ 05650 return '\007'; 05651 05652 case 'e': /* escape */ 05653 return 033; 05654 05655 case '0': case '1': case '2': case '3': /* octal constant */ 05656 case '4': case '5': case '6': case '7': 05657 pushback(c); 05658 c = scan_oct(lex_p, 3, &numlen); 05659 lex_p += numlen; 05660 return c; 05661 05662 case 'x': /* hex constant */ 05663 c = tok_hex(&numlen); 05664 if (numlen == 0) return 0; 05665 return c; 05666 05667 case 'b': /* backspace */ 05668 return '\010'; 05669 05670 case 's': /* space */ 05671 return ' '; 05672 05673 case 'M': 05674 if (flags & ESCAPE_META) goto eof; 05675 if ((c = nextc()) != '-') { 05676 pushback(c); 05677 goto eof; 05678 } 05679 if ((c = nextc()) == '\\') { 05680 if (peek('u')) goto eof; 05681 return read_escape(flags|ESCAPE_META, encp) | 0x80; 05682 } 05683 else if (c == -1 || !ISASCII(c)) goto eof; 05684 else { 05685 return ((c & 0xff) | 0x80); 05686 } 05687 05688 case 'C': 05689 if ((c = nextc()) != '-') { 05690 pushback(c); 05691 goto eof; 05692 } 05693 case 'c': 05694 if (flags & ESCAPE_CONTROL) goto eof; 05695 if ((c = nextc())== '\\') { 05696 if (peek('u')) goto eof; 05697 c = read_escape(flags|ESCAPE_CONTROL, encp); 05698 } 05699 else if (c == '?') 05700 return 0177; 05701 else if (c == -1 || !ISASCII(c)) goto eof; 05702 return c & 0x9f; 05703 05704 eof: 05705 case -1: 05706 yyerror("Invalid escape character syntax"); 05707 return '\0'; 05708 05709 default: 05710 return c; 05711 } 05712 } 05713 05714 static void 05715 parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc) 05716 { 05717 int len = rb_enc_codelen(c, enc); 05718 rb_enc_mbcput(c, tokspace(len), enc); 05719 } 05720 05721 static int 05722 parser_tokadd_escape(struct parser_params *parser, rb_encoding **encp) 05723 { 05724 int c; 05725 int flags = 0; 05726 size_t numlen; 05727 05728 first: 05729 switch (c = nextc()) { 05730 case '\n': 05731 return 0; /* just ignore */ 05732 05733 case '0': case '1': case '2': case '3': /* octal constant */ 05734 case '4': case '5': case '6': case '7': 05735 { 05736 ruby_scan_oct(--lex_p, 3, &numlen); 05737 if (numlen == 0) goto eof; 05738 lex_p += numlen; 05739 tokcopy((int)numlen + 1); 05740 } 05741 return 0; 05742 05743 case 'x': /* hex constant */ 05744 { 05745 tok_hex(&numlen); 05746 if (numlen == 0) return -1; 05747 tokcopy((int)numlen + 2); 05748 } 05749 return 0; 05750 05751 case 'M': 05752 if (flags & ESCAPE_META) goto eof; 05753 if ((c = nextc()) != '-') { 05754 pushback(c); 05755 goto eof; 05756 } 05757 tokcopy(3); 05758 flags |= ESCAPE_META; 05759 goto escaped; 05760 05761 case 'C': 05762 if (flags & ESCAPE_CONTROL) goto eof; 05763 if ((c = nextc()) != '-') { 05764 pushback(c); 05765 goto eof; 05766 } 05767 tokcopy(3); 05768 goto escaped; 05769 05770 case 'c': 05771 if (flags & ESCAPE_CONTROL) goto eof; 05772 tokcopy(2); 05773 flags |= ESCAPE_CONTROL; 05774 escaped: 05775 if ((c = nextc()) == '\\') { 05776 goto first; 05777 } 05778 else if (c == -1) goto eof; 05779 tokadd(c); 05780 return 0; 05781 05782 eof: 05783 case -1: 05784 yyerror("Invalid escape character syntax"); 05785 return -1; 05786 05787 default: 05788 tokadd('\\'); 05789 tokadd(c); 05790 } 05791 return 0; 05792 } 05793 05794 static int 05795 parser_regx_options(struct parser_params *parser) 05796 { 05797 int kcode = 0; 05798 int kopt = 0; 05799 int options = 0; 05800 int c, opt, kc; 05801 05802 newtok(); 05803 while (c = nextc(), ISALPHA(c)) { 05804 if (c == 'o') { 05805 options |= RE_OPTION_ONCE; 05806 } 05807 else if (rb_char_to_option_kcode(c, &opt, &kc)) { 05808 if (kc >= 0) { 05809 if (kc != rb_ascii8bit_encindex()) kcode = c; 05810 kopt = opt; 05811 } 05812 else { 05813 options |= opt; 05814 } 05815 } 05816 else { 05817 tokadd(c); 05818 } 05819 } 05820 options |= kopt; 05821 pushback(c); 05822 if (toklen()) { 05823 tokfix(); 05824 compile_error(PARSER_ARG "unknown regexp option%s - %s", 05825 toklen() > 1 ? "s" : "", tok()); 05826 } 05827 return options | RE_OPTION_ENCODING(kcode); 05828 } 05829 05830 static void 05831 dispose_string(VALUE str) 05832 { 05833 /* TODO: should use another API? */ 05834 if (RBASIC(str)->flags & RSTRING_NOEMBED) 05835 xfree(RSTRING_PTR(str)); 05836 rb_gc_force_recycle(str); 05837 } 05838 05839 static int 05840 parser_tokadd_mbchar(struct parser_params *parser, int c) 05841 { 05842 int len = parser_precise_mbclen(); 05843 if (!MBCLEN_CHARFOUND_P(len)) { 05844 compile_error(PARSER_ARG "invalid multibyte char (%s)", parser_encoding_name()); 05845 return -1; 05846 } 05847 tokadd(c); 05848 lex_p += --len; 05849 if (len > 0) tokcopy(len); 05850 return c; 05851 } 05852 05853 #define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c)) 05854 05855 static int 05856 parser_tokadd_string(struct parser_params *parser, 05857 int func, int term, int paren, long *nest, 05858 rb_encoding **encp) 05859 { 05860 int c; 05861 int has_nonascii = 0; 05862 rb_encoding *enc = *encp; 05863 char *errbuf = 0; 05864 static const char mixed_msg[] = "%s mixed within %s source"; 05865 05866 #define mixed_error(enc1, enc2) if (!errbuf) { \ 05867 size_t len = sizeof(mixed_msg) - 4; \ 05868 len += strlen(rb_enc_name(enc1)); \ 05869 len += strlen(rb_enc_name(enc2)); \ 05870 errbuf = ALLOCA_N(char, len); \ 05871 snprintf(errbuf, len, mixed_msg, \ 05872 rb_enc_name(enc1), \ 05873 rb_enc_name(enc2)); \ 05874 yyerror(errbuf); \ 05875 } 05876 #define mixed_escape(beg, enc1, enc2) do { \ 05877 const char *pos = lex_p; \ 05878 lex_p = (beg); \ 05879 mixed_error((enc1), (enc2)); \ 05880 lex_p = pos; \ 05881 } while (0) 05882 05883 while ((c = nextc()) != -1) { 05884 if (paren && c == paren) { 05885 ++*nest; 05886 } 05887 else if (c == term) { 05888 if (!nest || !*nest) { 05889 pushback(c); 05890 break; 05891 } 05892 --*nest; 05893 } 05894 else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) { 05895 int c2 = *lex_p; 05896 if (c2 == '$' || c2 == '@' || c2 == '{') { 05897 pushback(c); 05898 break; 05899 } 05900 } 05901 else if (c == '\\') { 05902 const char *beg = lex_p - 1; 05903 c = nextc(); 05904 switch (c) { 05905 case '\n': 05906 if (func & STR_FUNC_QWORDS) break; 05907 if (func & STR_FUNC_EXPAND) continue; 05908 tokadd('\\'); 05909 break; 05910 05911 case '\\': 05912 if (func & STR_FUNC_ESCAPE) tokadd(c); 05913 break; 05914 05915 case 'u': 05916 if ((func & STR_FUNC_EXPAND) == 0) { 05917 tokadd('\\'); 05918 break; 05919 } 05920 parser_tokadd_utf8(parser, &enc, 1, 05921 func & STR_FUNC_SYMBOL, 05922 func & STR_FUNC_REGEXP); 05923 if (has_nonascii && enc != *encp) { 05924 mixed_escape(beg, enc, *encp); 05925 } 05926 continue; 05927 05928 default: 05929 if (c == -1) return -1; 05930 if (!ISASCII(c)) { 05931 if ((func & STR_FUNC_EXPAND) == 0) tokadd('\\'); 05932 goto non_ascii; 05933 } 05934 if (func & STR_FUNC_REGEXP) { 05935 pushback(c); 05936 if ((c = tokadd_escape(&enc)) < 0) 05937 return -1; 05938 if (has_nonascii && enc != *encp) { 05939 mixed_escape(beg, enc, *encp); 05940 } 05941 continue; 05942 } 05943 else if (func & STR_FUNC_EXPAND) { 05944 pushback(c); 05945 if (func & STR_FUNC_ESCAPE) tokadd('\\'); 05946 c = read_escape(0, &enc); 05947 } 05948 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { 05949 /* ignore backslashed spaces in %w */ 05950 } 05951 else if (c != term && !(paren && c == paren)) { 05952 tokadd('\\'); 05953 pushback(c); 05954 continue; 05955 } 05956 } 05957 } 05958 else if (!parser_isascii()) { 05959 non_ascii: 05960 has_nonascii = 1; 05961 if (enc != *encp) { 05962 mixed_error(enc, *encp); 05963 continue; 05964 } 05965 if (tokadd_mbchar(c) == -1) return -1; 05966 continue; 05967 } 05968 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { 05969 pushback(c); 05970 break; 05971 } 05972 if (c & 0x80) { 05973 has_nonascii = 1; 05974 if (enc != *encp) { 05975 mixed_error(enc, *encp); 05976 continue; 05977 } 05978 } 05979 tokadd(c); 05980 } 05981 *encp = enc; 05982 return c; 05983 } 05984 05985 #define NEW_STRTERM(func, term, paren) \ 05986 rb_node_newnode(NODE_STRTERM, (func), (term) | ((paren) << (CHAR_BIT * 2)), 0) 05987 05988 #ifdef RIPPER 05989 static void 05990 ripper_flush_string_content(struct parser_params *parser, rb_encoding *enc) 05991 { 05992 if (!NIL_P(parser->delayed)) { 05993 ptrdiff_t len = lex_p - parser->tokp; 05994 if (len > 0) { 05995 rb_enc_str_buf_cat(parser->delayed, parser->tokp, len, enc); 05996 } 05997 ripper_dispatch_delayed_token(parser, tSTRING_CONTENT); 05998 parser->tokp = lex_p; 05999 } 06000 } 06001 06002 #define flush_string_content(enc) ripper_flush_string_content(parser, (enc)) 06003 #else 06004 #define flush_string_content(enc) ((void)(enc)) 06005 #endif 06006 06007 static int 06008 parser_parse_string(struct parser_params *parser, NODE *quote) 06009 { 06010 int func = (int)quote->nd_func; 06011 int term = nd_term(quote); 06012 int paren = nd_paren(quote); 06013 int c, space = 0; 06014 rb_encoding *enc = parser->enc; 06015 06016 if (func == -1) return tSTRING_END; 06017 c = nextc(); 06018 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) { 06019 do {c = nextc();} while (ISSPACE(c)); 06020 space = 1; 06021 } 06022 if (c == term && !quote->nd_nest) { 06023 if (func & STR_FUNC_QWORDS) { 06024 quote->nd_func = -1; 06025 return ' '; 06026 } 06027 if (!(func & STR_FUNC_REGEXP)) return tSTRING_END; 06028 set_yylval_num(regx_options()); 06029 return tREGEXP_END; 06030 } 06031 if (space) { 06032 pushback(c); 06033 return ' '; 06034 } 06035 newtok(); 06036 if ((func & STR_FUNC_EXPAND) && c == '#') { 06037 switch (c = nextc()) { 06038 case '$': 06039 case '@': 06040 pushback(c); 06041 return tSTRING_DVAR; 06042 case '{': 06043 return tSTRING_DBEG; 06044 } 06045 tokadd('#'); 06046 } 06047 pushback(c); 06048 if (tokadd_string(func, term, paren, "e->nd_nest, 06049 &enc) == -1) { 06050 ruby_sourceline = nd_line(quote); 06051 if (func & STR_FUNC_REGEXP) { 06052 if (parser->eofp) 06053 compile_error(PARSER_ARG "unterminated regexp meets end of file"); 06054 return tREGEXP_END; 06055 } 06056 else { 06057 if (parser->eofp) 06058 compile_error(PARSER_ARG "unterminated string meets end of file"); 06059 return tSTRING_END; 06060 } 06061 } 06062 06063 tokfix(); 06064 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func)); 06065 flush_string_content(enc); 06066 06067 return tSTRING_CONTENT; 06068 } 06069 06070 static int 06071 parser_heredoc_identifier(struct parser_params *parser) 06072 { 06073 int c = nextc(), term, func = 0; 06074 long len; 06075 06076 if (c == '-') { 06077 c = nextc(); 06078 func = STR_FUNC_INDENT; 06079 } 06080 switch (c) { 06081 case '\'': 06082 func |= str_squote; goto quoted; 06083 case '"': 06084 func |= str_dquote; goto quoted; 06085 case '`': 06086 func |= str_xquote; 06087 quoted: 06088 newtok(); 06089 tokadd(func); 06090 term = c; 06091 while ((c = nextc()) != -1 && c != term) { 06092 if (tokadd_mbchar(c) == -1) return 0; 06093 } 06094 if (c == -1) { 06095 compile_error(PARSER_ARG "unterminated here document identifier"); 06096 return 0; 06097 } 06098 break; 06099 06100 default: 06101 if (!parser_is_identchar()) { 06102 pushback(c); 06103 if (func & STR_FUNC_INDENT) { 06104 pushback('-'); 06105 } 06106 return 0; 06107 } 06108 newtok(); 06109 term = '"'; 06110 tokadd(func |= str_dquote); 06111 do { 06112 if (tokadd_mbchar(c) == -1) return 0; 06113 } while ((c = nextc()) != -1 && parser_is_identchar()); 06114 pushback(c); 06115 break; 06116 } 06117 06118 tokfix(); 06119 #ifdef RIPPER 06120 ripper_dispatch_scan_event(parser, tHEREDOC_BEG); 06121 #endif 06122 len = lex_p - lex_pbeg; 06123 lex_goto_eol(parser); 06124 lex_strterm = rb_node_newnode(NODE_HEREDOC, 06125 STR_NEW(tok(), toklen()), /* nd_lit */ 06126 len, /* nd_nth */ 06127 lex_lastline); /* nd_orig */ 06128 nd_set_line(lex_strterm, ruby_sourceline); 06129 ripper_flush(parser); 06130 return term == '`' ? tXSTRING_BEG : tSTRING_BEG; 06131 } 06132 06133 static void 06134 parser_heredoc_restore(struct parser_params *parser, NODE *here) 06135 { 06136 VALUE line; 06137 06138 line = here->nd_orig; 06139 lex_lastline = line; 06140 lex_pbeg = RSTRING_PTR(line); 06141 lex_pend = lex_pbeg + RSTRING_LEN(line); 06142 lex_p = lex_pbeg + here->nd_nth; 06143 heredoc_end = ruby_sourceline; 06144 ruby_sourceline = nd_line(here); 06145 dispose_string(here->nd_lit); 06146 rb_gc_force_recycle((VALUE)here); 06147 ripper_flush(parser); 06148 } 06149 06150 static int 06151 parser_whole_match_p(struct parser_params *parser, 06152 const char *eos, long len, int indent) 06153 { 06154 const char *p = lex_pbeg; 06155 long n; 06156 06157 if (indent) { 06158 while (*p && ISSPACE(*p)) p++; 06159 } 06160 n = lex_pend - (p + len); 06161 if (n < 0 || (n > 0 && p[len] != '\n' && p[len] != '\r')) return FALSE; 06162 return strncmp(eos, p, len) == 0; 06163 } 06164 06165 #ifdef RIPPER 06166 static void 06167 ripper_dispatch_heredoc_end(struct parser_params *parser) 06168 { 06169 if (!NIL_P(parser->delayed)) 06170 ripper_dispatch_delayed_token(parser, tSTRING_CONTENT); 06171 lex_goto_eol(parser); 06172 ripper_dispatch_ignored_scan_event(parser, tHEREDOC_END); 06173 } 06174 06175 #define dispatch_heredoc_end() ripper_dispatch_heredoc_end(parser) 06176 #else 06177 #define dispatch_heredoc_end() ((void)0) 06178 #endif 06179 06180 static int 06181 parser_here_document(struct parser_params *parser, NODE *here) 06182 { 06183 int c, func, indent = 0; 06184 const char *eos, *p, *pend; 06185 long len; 06186 VALUE str = 0; 06187 rb_encoding *enc = parser->enc; 06188 06189 eos = RSTRING_PTR(here->nd_lit); 06190 len = RSTRING_LEN(here->nd_lit) - 1; 06191 indent = (func = *eos++) & STR_FUNC_INDENT; 06192 06193 if ((c = nextc()) == -1) { 06194 error: 06195 compile_error(PARSER_ARG "can't find string \"%s\" anywhere before EOF", eos); 06196 #ifdef RIPPER 06197 if (NIL_P(parser->delayed)) { 06198 ripper_dispatch_scan_event(parser, tSTRING_CONTENT); 06199 } 06200 else { 06201 if (str || 06202 ((len = lex_p - parser->tokp) > 0 && 06203 (str = STR_NEW3(parser->tokp, len, enc, func), 1))) { 06204 rb_str_append(parser->delayed, str); 06205 } 06206 ripper_dispatch_delayed_token(parser, tSTRING_CONTENT); 06207 } 06208 lex_goto_eol(parser); 06209 #endif 06210 restore: 06211 heredoc_restore(lex_strterm); 06212 lex_strterm = 0; 06213 return 0; 06214 } 06215 if (was_bol() && whole_match_p(eos, len, indent)) { 06216 dispatch_heredoc_end(); 06217 heredoc_restore(lex_strterm); 06218 return tSTRING_END; 06219 } 06220 06221 if (!(func & STR_FUNC_EXPAND)) { 06222 do { 06223 p = RSTRING_PTR(lex_lastline); 06224 pend = lex_pend; 06225 if (pend > p) { 06226 switch (pend[-1]) { 06227 case '\n': 06228 if (--pend == p || pend[-1] != '\r') { 06229 pend++; 06230 break; 06231 } 06232 case '\r': 06233 --pend; 06234 } 06235 } 06236 if (str) 06237 rb_str_cat(str, p, pend - p); 06238 else 06239 str = STR_NEW(p, pend - p); 06240 if (pend < lex_pend) rb_str_cat(str, "\n", 1); 06241 lex_goto_eol(parser); 06242 if (nextc() == -1) { 06243 if (str) dispose_string(str); 06244 goto error; 06245 } 06246 } while (!whole_match_p(eos, len, indent)); 06247 } 06248 else { 06249 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/ 06250 newtok(); 06251 if (c == '#') { 06252 switch (c = nextc()) { 06253 case '$': 06254 case '@': 06255 pushback(c); 06256 return tSTRING_DVAR; 06257 case '{': 06258 return tSTRING_DBEG; 06259 } 06260 tokadd('#'); 06261 } 06262 do { 06263 pushback(c); 06264 if ((c = tokadd_string(func, '\n', 0, NULL, &enc)) == -1) { 06265 if (parser->eofp) goto error; 06266 goto restore; 06267 } 06268 if (c != '\n') { 06269 set_yylval_str(STR_NEW3(tok(), toklen(), enc, func)); 06270 flush_string_content(enc); 06271 return tSTRING_CONTENT; 06272 } 06273 tokadd(nextc()); 06274 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/ 06275 if ((c = nextc()) == -1) goto error; 06276 } while (!whole_match_p(eos, len, indent)); 06277 str = STR_NEW3(tok(), toklen(), enc, func); 06278 } 06279 dispatch_heredoc_end(); 06280 heredoc_restore(lex_strterm); 06281 lex_strterm = NEW_STRTERM(-1, 0, 0); 06282 set_yylval_str(str); 06283 return tSTRING_CONTENT; 06284 } 06285 06286 #include "lex.c" 06287 06288 static void 06289 arg_ambiguous_gen(struct parser_params *parser) 06290 { 06291 #ifndef RIPPER 06292 rb_warning0("ambiguous first argument; put parentheses or even spaces"); 06293 #else 06294 dispatch0(arg_ambiguous); 06295 #endif 06296 } 06297 #define arg_ambiguous() (arg_ambiguous_gen(parser), 1) 06298 06299 static ID 06300 formal_argument_gen(struct parser_params *parser, ID lhs) 06301 { 06302 #ifndef RIPPER 06303 if (!is_local_id(lhs)) 06304 yyerror("formal argument must be local variable"); 06305 #endif 06306 shadowing_lvar(lhs); 06307 return lhs; 06308 } 06309 06310 static int 06311 lvar_defined_gen(struct parser_params *parser, ID id) 06312 { 06313 return (dyna_in_block() && dvar_defined_get(id)) || local_id(id); 06314 } 06315 06316 /* emacsen -*- hack */ 06317 static long 06318 parser_encode_length(struct parser_params *parser, const char *name, long len) 06319 { 06320 long nlen; 06321 06322 if (len > 5 && name[nlen = len - 5] == '-') { 06323 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0) 06324 return nlen; 06325 } 06326 if (len > 4 && name[nlen = len - 4] == '-') { 06327 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0) 06328 return nlen; 06329 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 && 06330 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0)) 06331 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */ 06332 return nlen; 06333 } 06334 return len; 06335 } 06336 06337 static void 06338 parser_set_encode(struct parser_params *parser, const char *name) 06339 { 06340 int idx = rb_enc_find_index(name); 06341 rb_encoding *enc; 06342 VALUE excargs[3]; 06343 06344 if (idx < 0) { 06345 excargs[1] = rb_sprintf("unknown encoding name: %s", name); 06346 error: 06347 excargs[0] = rb_eArgError; 06348 excargs[2] = rb_make_backtrace(); 06349 rb_ary_unshift(excargs[2], rb_sprintf("%s:%d", ruby_sourcefile, ruby_sourceline)); 06350 rb_exc_raise(rb_make_exception(3, excargs)); 06351 } 06352 enc = rb_enc_from_index(idx); 06353 if (!rb_enc_asciicompat(enc)) { 06354 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc)); 06355 goto error; 06356 } 06357 parser->enc = enc; 06358 #ifndef RIPPER 06359 if (ruby_debug_lines) { 06360 long i, n = RARRAY_LEN(ruby_debug_lines); 06361 const VALUE *p = RARRAY_PTR(ruby_debug_lines); 06362 for (i = 0; i < n; ++i) { 06363 rb_enc_associate_index(*p, idx); 06364 } 06365 } 06366 #endif 06367 } 06368 06369 static int 06370 comment_at_top(struct parser_params *parser) 06371 { 06372 const char *p = lex_pbeg, *pend = lex_p - 1; 06373 if (parser->line_count != (parser->has_shebang ? 2 : 1)) return 0; 06374 while (p < pend) { 06375 if (!ISSPACE(*p)) return 0; 06376 p++; 06377 } 06378 return 1; 06379 } 06380 06381 #ifndef RIPPER 06382 typedef long (*rb_magic_comment_length_t)(struct parser_params *parser, const char *name, long len); 06383 typedef void (*rb_magic_comment_setter_t)(struct parser_params *parser, const char *name, const char *val); 06384 06385 static void 06386 magic_comment_encoding(struct parser_params *parser, const char *name, const char *val) 06387 { 06388 if (!comment_at_top(parser)) { 06389 return; 06390 } 06391 parser_set_encode(parser, val); 06392 } 06393 06394 static void 06395 parser_set_token_info(struct parser_params *parser, const char *name, const char *val) 06396 { 06397 int *p = &parser->parser_token_info_enabled; 06398 06399 switch (*val) { 06400 case 't': case 'T': 06401 if (strcasecmp(val, "true") == 0) { 06402 *p = TRUE; 06403 return; 06404 } 06405 break; 06406 case 'f': case 'F': 06407 if (strcasecmp(val, "false") == 0) { 06408 *p = FALSE; 06409 return; 06410 } 06411 break; 06412 } 06413 rb_compile_warning(ruby_sourcefile, ruby_sourceline, "invalid value for %s: %s", name, val); 06414 } 06415 06416 struct magic_comment { 06417 const char *name; 06418 rb_magic_comment_setter_t func; 06419 rb_magic_comment_length_t length; 06420 }; 06421 06422 static const struct magic_comment magic_comments[] = { 06423 {"coding", magic_comment_encoding, parser_encode_length}, 06424 {"encoding", magic_comment_encoding, parser_encode_length}, 06425 {"warn_indent", parser_set_token_info}, 06426 }; 06427 #endif 06428 06429 static const char * 06430 magic_comment_marker(const char *str, long len) 06431 { 06432 long i = 2; 06433 06434 while (i < len) { 06435 switch (str[i]) { 06436 case '-': 06437 if (str[i-1] == '*' && str[i-2] == '-') { 06438 return str + i + 1; 06439 } 06440 i += 2; 06441 break; 06442 case '*': 06443 if (i + 1 >= len) return 0; 06444 if (str[i+1] != '-') { 06445 i += 4; 06446 } 06447 else if (str[i-1] != '-') { 06448 i += 2; 06449 } 06450 else { 06451 return str + i + 2; 06452 } 06453 break; 06454 default: 06455 i += 3; 06456 break; 06457 } 06458 } 06459 return 0; 06460 } 06461 06462 static int 06463 parser_magic_comment(struct parser_params *parser, const char *str, long len) 06464 { 06465 VALUE name = 0, val = 0; 06466 const char *beg, *end, *vbeg, *vend; 06467 #define str_copy(_s, _p, _n) ((_s) \ 06468 ? (void)(rb_str_resize((_s), (_n)), \ 06469 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \ 06470 : (void)((_s) = STR_NEW((_p), (_n)))) 06471 06472 if (len <= 7) return FALSE; 06473 if (!(beg = magic_comment_marker(str, len))) return FALSE; 06474 if (!(end = magic_comment_marker(beg, str + len - beg))) return FALSE; 06475 str = beg; 06476 len = end - beg - 3; 06477 06478 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */ 06479 while (len > 0) { 06480 #ifndef RIPPER 06481 const struct magic_comment *p = magic_comments; 06482 #endif 06483 char *s; 06484 int i; 06485 long n = 0; 06486 06487 for (; len > 0 && *str; str++, --len) { 06488 switch (*str) { 06489 case '\'': case '"': case ':': case ';': 06490 continue; 06491 } 06492 if (!ISSPACE(*str)) break; 06493 } 06494 for (beg = str; len > 0; str++, --len) { 06495 switch (*str) { 06496 case '\'': case '"': case ':': case ';': 06497 break; 06498 default: 06499 if (ISSPACE(*str)) break; 06500 continue; 06501 } 06502 break; 06503 } 06504 for (end = str; len > 0 && ISSPACE(*str); str++, --len); 06505 if (!len) break; 06506 if (*str != ':') continue; 06507 06508 do str++; while (--len > 0 && ISSPACE(*str)); 06509 if (!len) break; 06510 if (*str == '"') { 06511 for (vbeg = ++str; --len > 0 && *str != '"'; str++) { 06512 if (*str == '\\') { 06513 --len; 06514 ++str; 06515 } 06516 } 06517 vend = str; 06518 if (len) { 06519 --len; 06520 ++str; 06521 } 06522 } 06523 else { 06524 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++); 06525 vend = str; 06526 } 06527 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++; 06528 06529 n = end - beg; 06530 str_copy(name, beg, n); 06531 s = RSTRING_PTR(name); 06532 for (i = 0; i < n; ++i) { 06533 if (s[i] == '-') s[i] = '_'; 06534 } 06535 #ifndef RIPPER 06536 do { 06537 if (STRNCASECMP(p->name, s, n) == 0) { 06538 n = vend - vbeg; 06539 if (p->length) { 06540 n = (*p->length)(parser, vbeg, n); 06541 } 06542 str_copy(val, vbeg, n); 06543 (*p->func)(parser, s, RSTRING_PTR(val)); 06544 break; 06545 } 06546 } while (++p < magic_comments + numberof(magic_comments)); 06547 #else 06548 dispatch2(magic_comment, name, val); 06549 #endif 06550 } 06551 06552 return TRUE; 06553 } 06554 06555 static void 06556 set_file_encoding(struct parser_params *parser, const char *str, const char *send) 06557 { 06558 int sep = 0; 06559 const char *beg = str; 06560 VALUE s; 06561 06562 for (;;) { 06563 if (send - str <= 6) return; 06564 switch (str[6]) { 06565 case 'C': case 'c': str += 6; continue; 06566 case 'O': case 'o': str += 5; continue; 06567 case 'D': case 'd': str += 4; continue; 06568 case 'I': case 'i': str += 3; continue; 06569 case 'N': case 'n': str += 2; continue; 06570 case 'G': case 'g': str += 1; continue; 06571 case '=': case ':': 06572 sep = 1; 06573 str += 6; 06574 break; 06575 default: 06576 str += 6; 06577 if (ISSPACE(*str)) break; 06578 continue; 06579 } 06580 if (STRNCASECMP(str-6, "coding", 6) == 0) break; 06581 } 06582 for (;;) { 06583 do { 06584 if (++str >= send) return; 06585 } while (ISSPACE(*str)); 06586 if (sep) break; 06587 if (*str != '=' && *str != ':') return; 06588 sep = 1; 06589 str++; 06590 } 06591 beg = str; 06592 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send); 06593 s = rb_str_new(beg, parser_encode_length(parser, beg, str - beg)); 06594 parser_set_encode(parser, RSTRING_PTR(s)); 06595 rb_str_resize(s, 0); 06596 } 06597 06598 static void 06599 parser_prepare(struct parser_params *parser) 06600 { 06601 int c = nextc(); 06602 switch (c) { 06603 case '#': 06604 if (peek('!')) parser->has_shebang = 1; 06605 break; 06606 case 0xef: /* UTF-8 BOM marker */ 06607 if (lex_pend - lex_p >= 2 && 06608 (unsigned char)lex_p[0] == 0xbb && 06609 (unsigned char)lex_p[1] == 0xbf) { 06610 parser->enc = rb_utf8_encoding(); 06611 lex_p += 2; 06612 lex_pbeg = lex_p; 06613 return; 06614 } 06615 break; 06616 case EOF: 06617 return; 06618 } 06619 pushback(c); 06620 parser->enc = rb_enc_get(lex_lastline); 06621 } 06622 06623 #define IS_ARG() (lex_state == EXPR_ARG || lex_state == EXPR_CMDARG) 06624 #define IS_END() (lex_state == EXPR_END || lex_state == EXPR_ENDARG || lex_state == EXPR_ENDFN) 06625 #define IS_BEG() (lex_state == EXPR_BEG || lex_state == EXPR_MID || lex_state == EXPR_VALUE || lex_state == EXPR_CLASS) 06626 #define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c)) 06627 #define IS_LABEL_POSSIBLE() ((lex_state == EXPR_BEG && !cmd_state) || IS_ARG()) 06628 #define IS_LABEL_SUFFIX(n) (peek_n(':',(n)) && !peek_n(':', (n)+1)) 06629 06630 #ifndef RIPPER 06631 #define ambiguous_operator(op, syn) ( \ 06632 rb_warning0("`"op"' after local variable is interpreted as binary operator"), \ 06633 rb_warning0("even though it seems like "syn"")) 06634 #else 06635 #define ambiguous_operator(op, syn) dispatch2(operator_ambiguous, ripper_intern(op), rb_str_new_cstr(syn)) 06636 #endif 06637 #define warn_balanced(op, syn) ((void) \ 06638 (last_state != EXPR_CLASS && last_state != EXPR_DOT && \ 06639 last_state != EXPR_FNAME && last_state != EXPR_ENDFN && \ 06640 last_state != EXPR_ENDARG && \ 06641 space_seen && !ISSPACE(c) && \ 06642 (ambiguous_operator(op, syn), 0))) 06643 06644 static int 06645 parser_yylex(struct parser_params *parser) 06646 { 06647 register int c; 06648 int space_seen = 0; 06649 int cmd_state; 06650 enum lex_state_e last_state; 06651 rb_encoding *enc; 06652 int mb; 06653 #ifdef RIPPER 06654 int fallthru = FALSE; 06655 #endif 06656 06657 if (lex_strterm) { 06658 int token; 06659 if (nd_type(lex_strterm) == NODE_HEREDOC) { 06660 token = here_document(lex_strterm); 06661 if (token == tSTRING_END) { 06662 lex_strterm = 0; 06663 lex_state = EXPR_END; 06664 } 06665 } 06666 else { 06667 token = parse_string(lex_strterm); 06668 if (token == tSTRING_END || token == tREGEXP_END) { 06669 rb_gc_force_recycle((VALUE)lex_strterm); 06670 lex_strterm = 0; 06671 lex_state = EXPR_END; 06672 } 06673 } 06674 return token; 06675 } 06676 cmd_state = command_start; 06677 command_start = FALSE; 06678 retry: 06679 last_state = lex_state; 06680 switch (c = nextc()) { 06681 case '\0': /* NUL */ 06682 case '\004': /* ^D */ 06683 case '\032': /* ^Z */ 06684 case -1: /* end of script. */ 06685 return 0; 06686 06687 /* white spaces */ 06688 case ' ': case '\t': case '\f': case '\r': 06689 case '\13': /* '\v' */ 06690 space_seen = 1; 06691 #ifdef RIPPER 06692 while ((c = nextc())) { 06693 switch (c) { 06694 case ' ': case '\t': case '\f': case '\r': 06695 case '\13': /* '\v' */ 06696 break; 06697 default: 06698 goto outofloop; 06699 } 06700 } 06701 outofloop: 06702 pushback(c); 06703 ripper_dispatch_scan_event(parser, tSP); 06704 #endif 06705 goto retry; 06706 06707 case '#': /* it's a comment */ 06708 /* no magic_comment in shebang line */ 06709 if (!parser_magic_comment(parser, lex_p, lex_pend - lex_p)) { 06710 if (comment_at_top(parser)) { 06711 set_file_encoding(parser, lex_p, lex_pend); 06712 } 06713 } 06714 lex_p = lex_pend; 06715 #ifdef RIPPER 06716 ripper_dispatch_scan_event(parser, tCOMMENT); 06717 fallthru = TRUE; 06718 #endif 06719 /* fall through */ 06720 case '\n': 06721 switch (lex_state) { 06722 case EXPR_BEG: 06723 case EXPR_FNAME: 06724 case EXPR_DOT: 06725 case EXPR_CLASS: 06726 case EXPR_VALUE: 06727 #ifdef RIPPER 06728 if (!fallthru) { 06729 ripper_dispatch_scan_event(parser, tIGNORED_NL); 06730 } 06731 fallthru = FALSE; 06732 #endif 06733 goto retry; 06734 default: 06735 break; 06736 } 06737 while ((c = nextc())) { 06738 switch (c) { 06739 case ' ': case '\t': case '\f': case '\r': 06740 case '\13': /* '\v' */ 06741 space_seen = 1; 06742 break; 06743 case '.': { 06744 if ((c = nextc()) != '.') { 06745 pushback(c); 06746 pushback('.'); 06747 goto retry; 06748 } 06749 } 06750 default: 06751 --ruby_sourceline; 06752 lex_nextline = lex_lastline; 06753 case -1: /* EOF no decrement*/ 06754 lex_goto_eol(parser); 06755 #ifdef RIPPER 06756 if (c != -1) { 06757 parser->tokp = lex_p; 06758 } 06759 #endif 06760 goto normal_newline; 06761 } 06762 } 06763 normal_newline: 06764 command_start = TRUE; 06765 lex_state = EXPR_BEG; 06766 return '\n'; 06767 06768 case '*': 06769 if ((c = nextc()) == '*') { 06770 if ((c = nextc()) == '=') { 06771 set_yylval_id(tPOW); 06772 lex_state = EXPR_BEG; 06773 return tOP_ASGN; 06774 } 06775 pushback(c); 06776 c = tPOW; 06777 } 06778 else { 06779 if (c == '=') { 06780 set_yylval_id('*'); 06781 lex_state = EXPR_BEG; 06782 return tOP_ASGN; 06783 } 06784 pushback(c); 06785 if (IS_SPCARG(c)) { 06786 rb_warning0("`*' interpreted as argument prefix"); 06787 c = tSTAR; 06788 } 06789 else if (IS_BEG()) { 06790 c = tSTAR; 06791 } 06792 else { 06793 warn_balanced("*", "argument prefix"); 06794 c = '*'; 06795 } 06796 } 06797 switch (lex_state) { 06798 case EXPR_FNAME: case EXPR_DOT: 06799 lex_state = EXPR_ARG; break; 06800 default: 06801 lex_state = EXPR_BEG; break; 06802 } 06803 return c; 06804 06805 case '!': 06806 c = nextc(); 06807 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) { 06808 lex_state = EXPR_ARG; 06809 if (c == '@') { 06810 return '!'; 06811 } 06812 } 06813 else { 06814 lex_state = EXPR_BEG; 06815 } 06816 if (c == '=') { 06817 return tNEQ; 06818 } 06819 if (c == '~') { 06820 return tNMATCH; 06821 } 06822 pushback(c); 06823 return '!'; 06824 06825 case '=': 06826 if (was_bol()) { 06827 /* skip embedded rd document */ 06828 if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) { 06829 #ifdef RIPPER 06830 int first_p = TRUE; 06831 06832 lex_goto_eol(parser); 06833 ripper_dispatch_scan_event(parser, tEMBDOC_BEG); 06834 #endif 06835 for (;;) { 06836 lex_goto_eol(parser); 06837 #ifdef RIPPER 06838 if (!first_p) { 06839 ripper_dispatch_scan_event(parser, tEMBDOC); 06840 } 06841 first_p = FALSE; 06842 #endif 06843 c = nextc(); 06844 if (c == -1) { 06845 compile_error(PARSER_ARG "embedded document meets end of file"); 06846 return 0; 06847 } 06848 if (c != '=') continue; 06849 if (strncmp(lex_p, "end", 3) == 0 && 06850 (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) { 06851 break; 06852 } 06853 } 06854 lex_goto_eol(parser); 06855 #ifdef RIPPER 06856 ripper_dispatch_scan_event(parser, tEMBDOC_END); 06857 #endif 06858 goto retry; 06859 } 06860 } 06861 06862 switch (lex_state) { 06863 case EXPR_FNAME: case EXPR_DOT: 06864 lex_state = EXPR_ARG; break; 06865 default: 06866 lex_state = EXPR_BEG; break; 06867 } 06868 if ((c = nextc()) == '=') { 06869 if ((c = nextc()) == '=') { 06870 return tEQQ; 06871 } 06872 pushback(c); 06873 return tEQ; 06874 } 06875 if (c == '~') { 06876 return tMATCH; 06877 } 06878 else if (c == '>') { 06879 return tASSOC; 06880 } 06881 pushback(c); 06882 return '='; 06883 06884 case '<': 06885 last_state = lex_state; 06886 c = nextc(); 06887 if (c == '<' && 06888 lex_state != EXPR_DOT && 06889 lex_state != EXPR_CLASS && 06890 !IS_END() && 06891 (!IS_ARG() || space_seen)) { 06892 int token = heredoc_identifier(); 06893 if (token) return token; 06894 } 06895 switch (lex_state) { 06896 case EXPR_FNAME: case EXPR_DOT: 06897 lex_state = EXPR_ARG; break; 06898 default: 06899 lex_state = EXPR_BEG; break; 06900 } 06901 if (c == '=') { 06902 if ((c = nextc()) == '>') { 06903 return tCMP; 06904 } 06905 pushback(c); 06906 return tLEQ; 06907 } 06908 if (c == '<') { 06909 if ((c = nextc()) == '=') { 06910 set_yylval_id(tLSHFT); 06911 lex_state = EXPR_BEG; 06912 return tOP_ASGN; 06913 } 06914 pushback(c); 06915 warn_balanced("<<", "here document"); 06916 return tLSHFT; 06917 } 06918 pushback(c); 06919 return '<'; 06920 06921 case '>': 06922 switch (lex_state) { 06923 case EXPR_FNAME: case EXPR_DOT: 06924 lex_state = EXPR_ARG; break; 06925 default: 06926 lex_state = EXPR_BEG; break; 06927 } 06928 if ((c = nextc()) == '=') { 06929 return tGEQ; 06930 } 06931 if (c == '>') { 06932 if ((c = nextc()) == '=') { 06933 set_yylval_id(tRSHFT); 06934 lex_state = EXPR_BEG; 06935 return tOP_ASGN; 06936 } 06937 pushback(c); 06938 return tRSHFT; 06939 } 06940 pushback(c); 06941 return '>'; 06942 06943 case '"': 06944 lex_strterm = NEW_STRTERM(str_dquote, '"', 0); 06945 return tSTRING_BEG; 06946 06947 case '`': 06948 if (lex_state == EXPR_FNAME) { 06949 lex_state = EXPR_ENDFN; 06950 return c; 06951 } 06952 if (lex_state == EXPR_DOT) { 06953 if (cmd_state) 06954 lex_state = EXPR_CMDARG; 06955 else 06956 lex_state = EXPR_ARG; 06957 return c; 06958 } 06959 lex_strterm = NEW_STRTERM(str_xquote, '`', 0); 06960 return tXSTRING_BEG; 06961 06962 case '\'': 06963 lex_strterm = NEW_STRTERM(str_squote, '\'', 0); 06964 return tSTRING_BEG; 06965 06966 case '?': 06967 if (IS_END()) { 06968 lex_state = EXPR_VALUE; 06969 return '?'; 06970 } 06971 c = nextc(); 06972 if (c == -1) { 06973 compile_error(PARSER_ARG "incomplete character syntax"); 06974 return 0; 06975 } 06976 if (rb_enc_isspace(c, parser->enc)) { 06977 if (!IS_ARG()) { 06978 int c2 = 0; 06979 switch (c) { 06980 case ' ': 06981 c2 = 's'; 06982 break; 06983 case '\n': 06984 c2 = 'n'; 06985 break; 06986 case '\t': 06987 c2 = 't'; 06988 break; 06989 case '\v': 06990 c2 = 'v'; 06991 break; 06992 case '\r': 06993 c2 = 'r'; 06994 break; 06995 case '\f': 06996 c2 = 'f'; 06997 break; 06998 } 06999 if (c2) { 07000 rb_warnI("invalid character syntax; use ?\\%c", c2); 07001 } 07002 } 07003 ternary: 07004 pushback(c); 07005 lex_state = EXPR_VALUE; 07006 return '?'; 07007 } 07008 newtok(); 07009 enc = parser->enc; 07010 if (!parser_isascii()) { 07011 if (tokadd_mbchar(c) == -1) return 0; 07012 } 07013 else if ((rb_enc_isalnum(c, parser->enc) || c == '_') && 07014 lex_p < lex_pend && is_identchar(lex_p, lex_pend, parser->enc)) { 07015 goto ternary; 07016 } 07017 else if (c == '\\') { 07018 if (peek('u')) { 07019 nextc(); 07020 c = parser_tokadd_utf8(parser, &enc, 0, 0, 0); 07021 if (0x80 <= c) { 07022 tokaddmbc(c, enc); 07023 } 07024 else { 07025 tokadd(c); 07026 } 07027 } 07028 else if (!lex_eol_p() && !(c = *lex_p, ISASCII(c))) { 07029 nextc(); 07030 if (tokadd_mbchar(c) == -1) return 0; 07031 } 07032 else { 07033 c = read_escape(0, &enc); 07034 tokadd(c); 07035 } 07036 } 07037 else { 07038 tokadd(c); 07039 } 07040 tokfix(); 07041 set_yylval_str(STR_NEW3(tok(), toklen(), enc, 0)); 07042 lex_state = EXPR_END; 07043 return tCHAR; 07044 07045 case '&': 07046 if ((c = nextc()) == '&') { 07047 lex_state = EXPR_BEG; 07048 if ((c = nextc()) == '=') { 07049 set_yylval_id(tANDOP); 07050 lex_state = EXPR_BEG; 07051 return tOP_ASGN; 07052 } 07053 pushback(c); 07054 return tANDOP; 07055 } 07056 else if (c == '=') { 07057 set_yylval_id('&'); 07058 lex_state = EXPR_BEG; 07059 return tOP_ASGN; 07060 } 07061 pushback(c); 07062 if (IS_SPCARG(c)) { 07063 rb_warning0("`&' interpreted as argument prefix"); 07064 c = tAMPER; 07065 } 07066 else if (IS_BEG()) { 07067 c = tAMPER; 07068 } 07069 else { 07070 warn_balanced("&", "argument prefix"); 07071 c = '&'; 07072 } 07073 switch (lex_state) { 07074 case EXPR_FNAME: case EXPR_DOT: 07075 lex_state = EXPR_ARG; break; 07076 default: 07077 lex_state = EXPR_BEG; 07078 } 07079 return c; 07080 07081 case '|': 07082 if ((c = nextc()) == '|') { 07083 lex_state = EXPR_BEG; 07084 if ((c = nextc()) == '=') { 07085 set_yylval_id(tOROP); 07086 lex_state = EXPR_BEG; 07087 return tOP_ASGN; 07088 } 07089 pushback(c); 07090 return tOROP; 07091 } 07092 if (c == '=') { 07093 set_yylval_id('|'); 07094 lex_state = EXPR_BEG; 07095 return tOP_ASGN; 07096 } 07097 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) { 07098 lex_state = EXPR_ARG; 07099 } 07100 else { 07101 lex_state = EXPR_BEG; 07102 } 07103 pushback(c); 07104 return '|'; 07105 07106 case '+': 07107 c = nextc(); 07108 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) { 07109 lex_state = EXPR_ARG; 07110 if (c == '@') { 07111 return tUPLUS; 07112 } 07113 pushback(c); 07114 return '+'; 07115 } 07116 if (c == '=') { 07117 set_yylval_id('+'); 07118 lex_state = EXPR_BEG; 07119 return tOP_ASGN; 07120 } 07121 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous())) { 07122 lex_state = EXPR_BEG; 07123 pushback(c); 07124 if (c != -1 && ISDIGIT(c)) { 07125 c = '+'; 07126 goto start_num; 07127 } 07128 return tUPLUS; 07129 } 07130 lex_state = EXPR_BEG; 07131 pushback(c); 07132 warn_balanced("+", "unary operator"); 07133 return '+'; 07134 07135 case '-': 07136 c = nextc(); 07137 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) { 07138 lex_state = EXPR_ARG; 07139 if (c == '@') { 07140 return tUMINUS; 07141 } 07142 pushback(c); 07143 return '-'; 07144 } 07145 if (c == '=') { 07146 set_yylval_id('-'); 07147 lex_state = EXPR_BEG; 07148 return tOP_ASGN; 07149 } 07150 if (c == '>') { 07151 lex_state = EXPR_ARG; 07152 return tLAMBDA; 07153 } 07154 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous())) { 07155 lex_state = EXPR_BEG; 07156 pushback(c); 07157 if (c != -1 && ISDIGIT(c)) { 07158 return tUMINUS_NUM; 07159 } 07160 return tUMINUS; 07161 } 07162 lex_state = EXPR_BEG; 07163 pushback(c); 07164 warn_balanced("-", "unary operator"); 07165 return '-'; 07166 07167 case '.': 07168 lex_state = EXPR_BEG; 07169 if ((c = nextc()) == '.') { 07170 if ((c = nextc()) == '.') { 07171 return tDOT3; 07172 } 07173 pushback(c); 07174 return tDOT2; 07175 } 07176 pushback(c); 07177 if (c != -1 && ISDIGIT(c)) { 07178 yyerror("no .<digit> floating literal anymore; put 0 before dot"); 07179 } 07180 lex_state = EXPR_DOT; 07181 return '.'; 07182 07183 start_num: 07184 case '0': case '1': case '2': case '3': case '4': 07185 case '5': case '6': case '7': case '8': case '9': 07186 { 07187 int is_float, seen_point, seen_e, nondigit; 07188 07189 is_float = seen_point = seen_e = nondigit = 0; 07190 lex_state = EXPR_END; 07191 newtok(); 07192 if (c == '-' || c == '+') { 07193 tokadd(c); 07194 c = nextc(); 07195 } 07196 if (c == '0') { 07197 #define no_digits() do {yyerror("numeric literal without digits"); return 0;} while (0) 07198 int start = toklen(); 07199 c = nextc(); 07200 if (c == 'x' || c == 'X') { 07201 /* hexadecimal */ 07202 c = nextc(); 07203 if (c != -1 && ISXDIGIT(c)) { 07204 do { 07205 if (c == '_') { 07206 if (nondigit) break; 07207 nondigit = c; 07208 continue; 07209 } 07210 if (!ISXDIGIT(c)) break; 07211 nondigit = 0; 07212 tokadd(c); 07213 } while ((c = nextc()) != -1); 07214 } 07215 pushback(c); 07216 tokfix(); 07217 if (toklen() == start) { 07218 no_digits(); 07219 } 07220 else if (nondigit) goto trailing_uc; 07221 set_yylval_literal(rb_cstr_to_inum(tok(), 16, FALSE)); 07222 return tINTEGER; 07223 } 07224 if (c == 'b' || c == 'B') { 07225 /* binary */ 07226 c = nextc(); 07227 if (c == '0' || c == '1') { 07228 do { 07229 if (c == '_') { 07230 if (nondigit) break; 07231 nondigit = c; 07232 continue; 07233 } 07234 if (c != '0' && c != '1') break; 07235 nondigit = 0; 07236 tokadd(c); 07237 } while ((c = nextc()) != -1); 07238 } 07239 pushback(c); 07240 tokfix(); 07241 if (toklen() == start) { 07242 no_digits(); 07243 } 07244 else if (nondigit) goto trailing_uc; 07245 set_yylval_literal(rb_cstr_to_inum(tok(), 2, FALSE)); 07246 return tINTEGER; 07247 } 07248 if (c == 'd' || c == 'D') { 07249 /* decimal */ 07250 c = nextc(); 07251 if (c != -1 && ISDIGIT(c)) { 07252 do { 07253 if (c == '_') { 07254 if (nondigit) break; 07255 nondigit = c; 07256 continue; 07257 } 07258 if (!ISDIGIT(c)) break; 07259 nondigit = 0; 07260 tokadd(c); 07261 } while ((c = nextc()) != -1); 07262 } 07263 pushback(c); 07264 tokfix(); 07265 if (toklen() == start) { 07266 no_digits(); 07267 } 07268 else if (nondigit) goto trailing_uc; 07269 set_yylval_literal(rb_cstr_to_inum(tok(), 10, FALSE)); 07270 return tINTEGER; 07271 } 07272 if (c == '_') { 07273 /* 0_0 */ 07274 goto octal_number; 07275 } 07276 if (c == 'o' || c == 'O') { 07277 /* prefixed octal */ 07278 c = nextc(); 07279 if (c == -1 || c == '_' || !ISDIGIT(c)) { 07280 no_digits(); 07281 } 07282 } 07283 if (c >= '0' && c <= '7') { 07284 /* octal */ 07285 octal_number: 07286 do { 07287 if (c == '_') { 07288 if (nondigit) break; 07289 nondigit = c; 07290 continue; 07291 } 07292 if (c < '0' || c > '9') break; 07293 if (c > '7') goto invalid_octal; 07294 nondigit = 0; 07295 tokadd(c); 07296 } while ((c = nextc()) != -1); 07297 if (toklen() > start) { 07298 pushback(c); 07299 tokfix(); 07300 if (nondigit) goto trailing_uc; 07301 set_yylval_literal(rb_cstr_to_inum(tok(), 8, FALSE)); 07302 return tINTEGER; 07303 } 07304 if (nondigit) { 07305 pushback(c); 07306 goto trailing_uc; 07307 } 07308 } 07309 if (c > '7' && c <= '9') { 07310 invalid_octal: 07311 yyerror("Invalid octal digit"); 07312 } 07313 else if (c == '.' || c == 'e' || c == 'E') { 07314 tokadd('0'); 07315 } 07316 else { 07317 pushback(c); 07318 set_yylval_literal(INT2FIX(0)); 07319 return tINTEGER; 07320 } 07321 } 07322 07323 for (;;) { 07324 switch (c) { 07325 case '0': case '1': case '2': case '3': case '4': 07326 case '5': case '6': case '7': case '8': case '9': 07327 nondigit = 0; 07328 tokadd(c); 07329 break; 07330 07331 case '.': 07332 if (nondigit) goto trailing_uc; 07333 if (seen_point || seen_e) { 07334 goto decode_num; 07335 } 07336 else { 07337 int c0 = nextc(); 07338 if (c0 == -1 || !ISDIGIT(c0)) { 07339 pushback(c0); 07340 goto decode_num; 07341 } 07342 c = c0; 07343 } 07344 tokadd('.'); 07345 tokadd(c); 07346 is_float++; 07347 seen_point++; 07348 nondigit = 0; 07349 break; 07350 07351 case 'e': 07352 case 'E': 07353 if (nondigit) { 07354 pushback(c); 07355 c = nondigit; 07356 goto decode_num; 07357 } 07358 if (seen_e) { 07359 goto decode_num; 07360 } 07361 tokadd(c); 07362 seen_e++; 07363 is_float++; 07364 nondigit = c; 07365 c = nextc(); 07366 if (c != '-' && c != '+') continue; 07367 tokadd(c); 07368 nondigit = c; 07369 break; 07370 07371 case '_': /* `_' in number just ignored */ 07372 if (nondigit) goto decode_num; 07373 nondigit = c; 07374 break; 07375 07376 default: 07377 goto decode_num; 07378 } 07379 c = nextc(); 07380 } 07381 07382 decode_num: 07383 pushback(c); 07384 if (nondigit) { 07385 char tmp[30]; 07386 trailing_uc: 07387 snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit); 07388 yyerror(tmp); 07389 } 07390 tokfix(); 07391 if (is_float) { 07392 double d = strtod(tok(), 0); 07393 if (errno == ERANGE) { 07394 rb_warningS("Float %s out of range", tok()); 07395 errno = 0; 07396 } 07397 set_yylval_literal(DBL2NUM(d)); 07398 return tFLOAT; 07399 } 07400 set_yylval_literal(rb_cstr_to_inum(tok(), 10, FALSE)); 07401 return tINTEGER; 07402 } 07403 07404 case ')': 07405 case ']': 07406 paren_nest--; 07407 case '}': 07408 COND_LEXPOP(); 07409 CMDARG_LEXPOP(); 07410 if (c == ')') 07411 lex_state = EXPR_ENDFN; 07412 else 07413 lex_state = EXPR_ENDARG; 07414 return c; 07415 07416 case ':': 07417 c = nextc(); 07418 if (c == ':') { 07419 if (IS_BEG() || lex_state == EXPR_CLASS || IS_SPCARG(-1)) { 07420 lex_state = EXPR_BEG; 07421 return tCOLON3; 07422 } 07423 lex_state = EXPR_DOT; 07424 return tCOLON2; 07425 } 07426 if (IS_END() || ISSPACE(c)) { 07427 pushback(c); 07428 warn_balanced(":", "symbol literal"); 07429 lex_state = EXPR_BEG; 07430 return ':'; 07431 } 07432 switch (c) { 07433 case '\'': 07434 lex_strterm = NEW_STRTERM(str_ssym, c, 0); 07435 break; 07436 case '"': 07437 lex_strterm = NEW_STRTERM(str_dsym, c, 0); 07438 break; 07439 default: 07440 pushback(c); 07441 break; 07442 } 07443 lex_state = EXPR_FNAME; 07444 return tSYMBEG; 07445 07446 case '/': 07447 if (IS_BEG()) { 07448 lex_strterm = NEW_STRTERM(str_regexp, '/', 0); 07449 return tREGEXP_BEG; 07450 } 07451 if ((c = nextc()) == '=') { 07452 set_yylval_id('/'); 07453 lex_state = EXPR_BEG; 07454 return tOP_ASGN; 07455 } 07456 pushback(c); 07457 if (IS_SPCARG(c)) { 07458 (void)arg_ambiguous(); 07459 lex_strterm = NEW_STRTERM(str_regexp, '/', 0); 07460 return tREGEXP_BEG; 07461 } 07462 switch (lex_state) { 07463 case EXPR_FNAME: case EXPR_DOT: 07464 lex_state = EXPR_ARG; break; 07465 default: 07466 lex_state = EXPR_BEG; break; 07467 } 07468 warn_balanced("/", "regexp literal"); 07469 return '/'; 07470 07471 case '^': 07472 if ((c = nextc()) == '=') { 07473 set_yylval_id('^'); 07474 lex_state = EXPR_BEG; 07475 return tOP_ASGN; 07476 } 07477 switch (lex_state) { 07478 case EXPR_FNAME: case EXPR_DOT: 07479 lex_state = EXPR_ARG; break; 07480 default: 07481 lex_state = EXPR_BEG; break; 07482 } 07483 pushback(c); 07484 return '^'; 07485 07486 case ';': 07487 lex_state = EXPR_BEG; 07488 command_start = TRUE; 07489 return ';'; 07490 07491 case ',': 07492 lex_state = EXPR_BEG; 07493 return ','; 07494 07495 case '~': 07496 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) { 07497 if ((c = nextc()) != '@') { 07498 pushback(c); 07499 } 07500 lex_state = EXPR_ARG; 07501 } 07502 else { 07503 lex_state = EXPR_BEG; 07504 } 07505 return '~'; 07506 07507 case '(': 07508 if (IS_BEG()) { 07509 c = tLPAREN; 07510 } 07511 else if (IS_SPCARG(-1)) { 07512 c = tLPAREN_ARG; 07513 } 07514 paren_nest++; 07515 COND_PUSH(0); 07516 CMDARG_PUSH(0); 07517 lex_state = EXPR_BEG; 07518 return c; 07519 07520 case '[': 07521 paren_nest++; 07522 if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) { 07523 lex_state = EXPR_ARG; 07524 if ((c = nextc()) == ']') { 07525 if ((c = nextc()) == '=') { 07526 return tASET; 07527 } 07528 pushback(c); 07529 return tAREF; 07530 } 07531 pushback(c); 07532 return '['; 07533 } 07534 else if (IS_BEG()) { 07535 c = tLBRACK; 07536 } 07537 else if (IS_ARG() && space_seen) { 07538 c = tLBRACK; 07539 } 07540 lex_state = EXPR_BEG; 07541 COND_PUSH(0); 07542 CMDARG_PUSH(0); 07543 return c; 07544 07545 case '{': 07546 if (lpar_beg && lpar_beg == paren_nest) { 07547 lex_state = EXPR_BEG; 07548 lpar_beg = 0; 07549 --paren_nest; 07550 COND_PUSH(0); 07551 CMDARG_PUSH(0); 07552 return tLAMBEG; 07553 } 07554 if (IS_ARG() || lex_state == EXPR_END || lex_state == EXPR_ENDFN) 07555 c = '{'; /* block (primary) */ 07556 else if (lex_state == EXPR_ENDARG) 07557 c = tLBRACE_ARG; /* block (expr) */ 07558 else 07559 c = tLBRACE; /* hash */ 07560 COND_PUSH(0); 07561 CMDARG_PUSH(0); 07562 lex_state = EXPR_BEG; 07563 if (c != tLBRACE) command_start = TRUE; 07564 return c; 07565 07566 case '\\': 07567 c = nextc(); 07568 if (c == '\n') { 07569 space_seen = 1; 07570 #ifdef RIPPER 07571 ripper_dispatch_scan_event(parser, tSP); 07572 #endif 07573 goto retry; /* skip \\n */ 07574 } 07575 pushback(c); 07576 return '\\'; 07577 07578 case '%': 07579 if (IS_BEG()) { 07580 int term; 07581 int paren; 07582 07583 c = nextc(); 07584 quotation: 07585 if (c == -1 || !ISALNUM(c)) { 07586 term = c; 07587 c = 'Q'; 07588 } 07589 else { 07590 term = nextc(); 07591 if (rb_enc_isalnum(term, parser->enc) || !parser_isascii()) { 07592 yyerror("unknown type of %string"); 07593 return 0; 07594 } 07595 } 07596 if (c == -1 || term == -1) { 07597 compile_error(PARSER_ARG "unterminated quoted string meets end of file"); 07598 return 0; 07599 } 07600 paren = term; 07601 if (term == '(') term = ')'; 07602 else if (term == '[') term = ']'; 07603 else if (term == '{') term = '}'; 07604 else if (term == '<') term = '>'; 07605 else paren = 0; 07606 07607 switch (c) { 07608 case 'Q': 07609 lex_strterm = NEW_STRTERM(str_dquote, term, paren); 07610 return tSTRING_BEG; 07611 07612 case 'q': 07613 lex_strterm = NEW_STRTERM(str_squote, term, paren); 07614 return tSTRING_BEG; 07615 07616 case 'W': 07617 lex_strterm = NEW_STRTERM(str_dword, term, paren); 07618 do {c = nextc();} while (ISSPACE(c)); 07619 pushback(c); 07620 return tWORDS_BEG; 07621 07622 case 'w': 07623 lex_strterm = NEW_STRTERM(str_sword, term, paren); 07624 do {c = nextc();} while (ISSPACE(c)); 07625 pushback(c); 07626 return tQWORDS_BEG; 07627 07628 case 'x': 07629 lex_strterm = NEW_STRTERM(str_xquote, term, paren); 07630 return tXSTRING_BEG; 07631 07632 case 'r': 07633 lex_strterm = NEW_STRTERM(str_regexp, term, paren); 07634 return tREGEXP_BEG; 07635 07636 case 's': 07637 lex_strterm = NEW_STRTERM(str_ssym, term, paren); 07638 lex_state = EXPR_FNAME; 07639 return tSYMBEG; 07640 07641 default: 07642 yyerror("unknown type of %string"); 07643 return 0; 07644 } 07645 } 07646 if ((c = nextc()) == '=') { 07647 set_yylval_id('%'); 07648 lex_state = EXPR_BEG; 07649 return tOP_ASGN; 07650 } 07651 if (IS_SPCARG(c)) { 07652 goto quotation; 07653 } 07654 switch (lex_state) { 07655 case EXPR_FNAME: case EXPR_DOT: 07656 lex_state = EXPR_ARG; break; 07657 default: 07658 lex_state = EXPR_BEG; break; 07659 } 07660 pushback(c); 07661 warn_balanced("%%", "string literal"); 07662 return '%'; 07663 07664 case '$': 07665 lex_state = EXPR_END; 07666 newtok(); 07667 c = nextc(); 07668 switch (c) { 07669 case '_': /* $_: last read line string */ 07670 c = nextc(); 07671 if (parser_is_identchar()) { 07672 tokadd('$'); 07673 tokadd('_'); 07674 break; 07675 } 07676 pushback(c); 07677 c = '_'; 07678 /* fall through */ 07679 case '~': /* $~: match-data */ 07680 case '*': /* $*: argv */ 07681 case '$': /* $$: pid */ 07682 case '?': /* $?: last status */ 07683 case '!': /* $!: error string */ 07684 case '@': /* $@: error position */ 07685 case '/': /* $/: input record separator */ 07686 case '\\': /* $\: output record separator */ 07687 case ';': /* $;: field separator */ 07688 case ',': /* $,: output field separator */ 07689 case '.': /* $.: last read line number */ 07690 case '=': /* $=: ignorecase */ 07691 case ':': /* $:: load path */ 07692 case '<': /* $<: reading filename */ 07693 case '>': /* $>: default output handle */ 07694 case '\"': /* $": already loaded files */ 07695 tokadd('$'); 07696 tokadd(c); 07697 tokfix(); 07698 set_yylval_name(rb_intern(tok())); 07699 return tGVAR; 07700 07701 case '-': 07702 tokadd('$'); 07703 tokadd(c); 07704 c = nextc(); 07705 if (parser_is_identchar()) { 07706 if (tokadd_mbchar(c) == -1) return 0; 07707 } 07708 else { 07709 pushback(c); 07710 } 07711 gvar: 07712 tokfix(); 07713 set_yylval_name(rb_intern(tok())); 07714 return tGVAR; 07715 07716 case '&': /* $&: last match */ 07717 case '`': /* $`: string before last match */ 07718 case '\'': /* $': string after last match */ 07719 case '+': /* $+: string matches last paren. */ 07720 if (last_state == EXPR_FNAME) { 07721 tokadd('$'); 07722 tokadd(c); 07723 goto gvar; 07724 } 07725 set_yylval_node(NEW_BACK_REF(c)); 07726 return tBACK_REF; 07727 07728 case '1': case '2': case '3': 07729 case '4': case '5': case '6': 07730 case '7': case '8': case '9': 07731 tokadd('$'); 07732 do { 07733 tokadd(c); 07734 c = nextc(); 07735 } while (c != -1 && ISDIGIT(c)); 07736 pushback(c); 07737 if (last_state == EXPR_FNAME) goto gvar; 07738 tokfix(); 07739 set_yylval_node(NEW_NTH_REF(atoi(tok()+1))); 07740 return tNTH_REF; 07741 07742 default: 07743 if (!parser_is_identchar()) { 07744 pushback(c); 07745 return '$'; 07746 } 07747 case '0': 07748 tokadd('$'); 07749 } 07750 break; 07751 07752 case '@': 07753 c = nextc(); 07754 newtok(); 07755 tokadd('@'); 07756 if (c == '@') { 07757 tokadd('@'); 07758 c = nextc(); 07759 } 07760 if (c != -1 && ISDIGIT(c)) { 07761 if (tokidx == 1) { 07762 compile_error(PARSER_ARG "`@%c' is not allowed as an instance variable name", c); 07763 } 07764 else { 07765 compile_error(PARSER_ARG "`@@%c' is not allowed as a class variable name", c); 07766 } 07767 return 0; 07768 } 07769 if (!parser_is_identchar()) { 07770 pushback(c); 07771 return '@'; 07772 } 07773 break; 07774 07775 case '_': 07776 if (was_bol() && whole_match_p("__END__", 7, 0)) { 07777 ruby__end__seen = 1; 07778 parser->eofp = Qtrue; 07779 #ifndef RIPPER 07780 return -1; 07781 #else 07782 lex_goto_eol(parser); 07783 ripper_dispatch_scan_event(parser, k__END__); 07784 return 0; 07785 #endif 07786 } 07787 newtok(); 07788 break; 07789 07790 default: 07791 if (!parser_is_identchar()) { 07792 rb_compile_error(PARSER_ARG "Invalid char `\\x%02X' in expression", c); 07793 goto retry; 07794 } 07795 07796 newtok(); 07797 break; 07798 } 07799 07800 mb = ENC_CODERANGE_7BIT; 07801 do { 07802 if (!ISASCII(c)) mb = ENC_CODERANGE_UNKNOWN; 07803 if (tokadd_mbchar(c) == -1) return 0; 07804 c = nextc(); 07805 } while (parser_is_identchar()); 07806 switch (tok()[0]) { 07807 case '@': case '$': 07808 pushback(c); 07809 break; 07810 default: 07811 if ((c == '!' || c == '?') && !peek('=')) { 07812 tokadd(c); 07813 } 07814 else { 07815 pushback(c); 07816 } 07817 } 07818 tokfix(); 07819 07820 { 07821 int result = 0; 07822 07823 last_state = lex_state; 07824 switch (tok()[0]) { 07825 case '$': 07826 lex_state = EXPR_END; 07827 result = tGVAR; 07828 break; 07829 case '@': 07830 lex_state = EXPR_END; 07831 if (tok()[1] == '@') 07832 result = tCVAR; 07833 else 07834 result = tIVAR; 07835 break; 07836 07837 default: 07838 if (toklast() == '!' || toklast() == '?') { 07839 result = tFID; 07840 } 07841 else { 07842 if (lex_state == EXPR_FNAME) { 07843 if ((c = nextc()) == '=' && !peek('~') && !peek('>') && 07844 (!peek('=') || (peek_n('>', 1)))) { 07845 result = tIDENTIFIER; 07846 tokadd(c); 07847 tokfix(); 07848 } 07849 else { 07850 pushback(c); 07851 } 07852 } 07853 if (result == 0 && ISUPPER(tok()[0])) { 07854 result = tCONSTANT; 07855 } 07856 else { 07857 result = tIDENTIFIER; 07858 } 07859 } 07860 07861 if (IS_LABEL_POSSIBLE()) { 07862 if (IS_LABEL_SUFFIX(0)) { 07863 lex_state = EXPR_BEG; 07864 nextc(); 07865 set_yylval_name(TOK_INTERN(!ENC_SINGLE(mb))); 07866 return tLABEL; 07867 } 07868 } 07869 if (mb == ENC_CODERANGE_7BIT && lex_state != EXPR_DOT) { 07870 const struct kwtable *kw; 07871 07872 /* See if it is a reserved word. */ 07873 kw = rb_reserved_word(tok(), toklen()); 07874 if (kw) { 07875 enum lex_state_e state = lex_state; 07876 lex_state = kw->state; 07877 if (state == EXPR_FNAME) { 07878 set_yylval_name(rb_intern(kw->name)); 07879 return kw->id[0]; 07880 } 07881 if (kw->id[0] == keyword_do) { 07882 command_start = TRUE; 07883 if (lpar_beg && lpar_beg == paren_nest) { 07884 lpar_beg = 0; 07885 --paren_nest; 07886 return keyword_do_LAMBDA; 07887 } 07888 if (COND_P()) return keyword_do_cond; 07889 if (CMDARG_P() && state != EXPR_CMDARG) 07890 return keyword_do_block; 07891 if (state == EXPR_ENDARG || state == EXPR_BEG) 07892 return keyword_do_block; 07893 return keyword_do; 07894 } 07895 if (state == EXPR_BEG || state == EXPR_VALUE) 07896 return kw->id[0]; 07897 else { 07898 if (kw->id[0] != kw->id[1]) 07899 lex_state = EXPR_BEG; 07900 return kw->id[1]; 07901 } 07902 } 07903 } 07904 07905 if (IS_BEG() || 07906 lex_state == EXPR_DOT || 07907 IS_ARG()) { 07908 if (cmd_state) { 07909 lex_state = EXPR_CMDARG; 07910 } 07911 else { 07912 lex_state = EXPR_ARG; 07913 } 07914 } 07915 else if (lex_state == EXPR_FNAME) { 07916 lex_state = EXPR_ENDFN; 07917 } 07918 else { 07919 lex_state = EXPR_END; 07920 } 07921 } 07922 { 07923 ID ident = TOK_INTERN(!ENC_SINGLE(mb)); 07924 07925 set_yylval_name(ident); 07926 if (last_state != EXPR_DOT && last_state != EXPR_FNAME && 07927 is_local_id(ident) && lvar_defined(ident)) { 07928 lex_state = EXPR_END; 07929 } 07930 } 07931 return result; 07932 } 07933 } 07934 07935 #if YYPURE 07936 static int 07937 yylex(void *lval, void *p) 07938 #else 07939 yylex(void *p) 07940 #endif 07941 { 07942 struct parser_params *parser = (struct parser_params*)p; 07943 int t; 07944 07945 #if YYPURE 07946 parser->parser_yylval = lval; 07947 parser->parser_yylval->val = Qundef; 07948 #endif 07949 t = parser_yylex(parser); 07950 #ifdef RIPPER 07951 if (!NIL_P(parser->delayed)) { 07952 ripper_dispatch_delayed_token(parser, t); 07953 return t; 07954 } 07955 if (t != 0) 07956 ripper_dispatch_scan_event(parser, t); 07957 #endif 07958 07959 return t; 07960 } 07961 07962 #ifndef RIPPER 07963 static NODE* 07964 node_newnode(struct parser_params *parser, enum node_type type, VALUE a0, VALUE a1, VALUE a2) 07965 { 07966 NODE *n = (rb_node_newnode)(type, a0, a1, a2); 07967 nd_set_line(n, ruby_sourceline); 07968 return n; 07969 } 07970 07971 enum node_type 07972 nodetype(NODE *node) /* for debug */ 07973 { 07974 return (enum node_type)nd_type(node); 07975 } 07976 07977 int 07978 nodeline(NODE *node) 07979 { 07980 return nd_line(node); 07981 } 07982 07983 static NODE* 07984 newline_node(NODE *node) 07985 { 07986 if (node) { 07987 node = remove_begin(node); 07988 node->flags |= NODE_FL_NEWLINE; 07989 } 07990 return node; 07991 } 07992 07993 static void 07994 fixpos(NODE *node, NODE *orig) 07995 { 07996 if (!node) return; 07997 if (!orig) return; 07998 if (orig == (NODE*)1) return; 07999 nd_set_line(node, nd_line(orig)); 08000 } 08001 08002 static void 08003 parser_warning(struct parser_params *parser, NODE *node, const char *mesg) 08004 { 08005 rb_compile_warning(ruby_sourcefile, nd_line(node), "%s", mesg); 08006 } 08007 #define parser_warning(node, mesg) parser_warning(parser, (node), (mesg)) 08008 08009 static void 08010 parser_warn(struct parser_params *parser, NODE *node, const char *mesg) 08011 { 08012 rb_compile_warn(ruby_sourcefile, nd_line(node), "%s", mesg); 08013 } 08014 #define parser_warn(node, mesg) parser_warn(parser, (node), (mesg)) 08015 08016 static NODE* 08017 block_append_gen(struct parser_params *parser, NODE *head, NODE *tail) 08018 { 08019 NODE *end, *h = head, *nd; 08020 08021 if (tail == 0) return head; 08022 08023 if (h == 0) return tail; 08024 switch (nd_type(h)) { 08025 case NODE_LIT: 08026 case NODE_STR: 08027 case NODE_SELF: 08028 case NODE_TRUE: 08029 case NODE_FALSE: 08030 case NODE_NIL: 08031 parser_warning(h, "unused literal ignored"); 08032 return tail; 08033 default: 08034 h = end = NEW_BLOCK(head); 08035 end->nd_end = end; 08036 fixpos(end, head); 08037 head = end; 08038 break; 08039 case NODE_BLOCK: 08040 end = h->nd_end; 08041 break; 08042 } 08043 08044 nd = end->nd_head; 08045 switch (nd_type(nd)) { 08046 case NODE_RETURN: 08047 case NODE_BREAK: 08048 case NODE_NEXT: 08049 case NODE_REDO: 08050 case NODE_RETRY: 08051 if (RTEST(ruby_verbose)) { 08052 parser_warning(nd, "statement not reached"); 08053 } 08054 break; 08055 08056 default: 08057 break; 08058 } 08059 08060 if (nd_type(tail) != NODE_BLOCK) { 08061 tail = NEW_BLOCK(tail); 08062 tail->nd_end = tail; 08063 } 08064 end->nd_next = tail; 08065 h->nd_end = tail->nd_end; 08066 return head; 08067 } 08068 08069 /* append item to the list */ 08070 static NODE* 08071 list_append_gen(struct parser_params *parser, NODE *list, NODE *item) 08072 { 08073 NODE *last; 08074 08075 if (list == 0) return NEW_LIST(item); 08076 if (list->nd_next) { 08077 last = list->nd_next->nd_end; 08078 } 08079 else { 08080 last = list; 08081 } 08082 08083 list->nd_alen += 1; 08084 last->nd_next = NEW_LIST(item); 08085 list->nd_next->nd_end = last->nd_next; 08086 return list; 08087 } 08088 08089 /* concat two lists */ 08090 static NODE* 08091 list_concat_gen(struct parser_params *parser, NODE *head, NODE *tail) 08092 { 08093 NODE *last; 08094 08095 if (head->nd_next) { 08096 last = head->nd_next->nd_end; 08097 } 08098 else { 08099 last = head; 08100 } 08101 08102 head->nd_alen += tail->nd_alen; 08103 last->nd_next = tail; 08104 if (tail->nd_next) { 08105 head->nd_next->nd_end = tail->nd_next->nd_end; 08106 } 08107 else { 08108 head->nd_next->nd_end = tail; 08109 } 08110 08111 return head; 08112 } 08113 08114 static int 08115 literal_concat0(struct parser_params *parser, VALUE head, VALUE tail) 08116 { 08117 if (NIL_P(tail)) return 1; 08118 if (!rb_enc_compatible(head, tail)) { 08119 compile_error(PARSER_ARG "string literal encodings differ (%s / %s)", 08120 rb_enc_name(rb_enc_get(head)), 08121 rb_enc_name(rb_enc_get(tail))); 08122 rb_str_resize(head, 0); 08123 rb_str_resize(tail, 0); 08124 return 0; 08125 } 08126 rb_str_buf_append(head, tail); 08127 return 1; 08128 } 08129 08130 /* concat two string literals */ 08131 static NODE * 08132 literal_concat_gen(struct parser_params *parser, NODE *head, NODE *tail) 08133 { 08134 enum node_type htype; 08135 08136 if (!head) return tail; 08137 if (!tail) return head; 08138 08139 htype = nd_type(head); 08140 if (htype == NODE_EVSTR) { 08141 NODE *node = NEW_DSTR(Qnil); 08142 head = list_append(node, head); 08143 } 08144 switch (nd_type(tail)) { 08145 case NODE_STR: 08146 if (htype == NODE_STR) { 08147 if (!literal_concat0(parser, head->nd_lit, tail->nd_lit)) { 08148 error: 08149 rb_gc_force_recycle((VALUE)head); 08150 rb_gc_force_recycle((VALUE)tail); 08151 return 0; 08152 } 08153 rb_gc_force_recycle((VALUE)tail); 08154 } 08155 else { 08156 list_append(head, tail); 08157 } 08158 break; 08159 08160 case NODE_DSTR: 08161 if (htype == NODE_STR) { 08162 if (!literal_concat0(parser, head->nd_lit, tail->nd_lit)) 08163 goto error; 08164 tail->nd_lit = head->nd_lit; 08165 rb_gc_force_recycle((VALUE)head); 08166 head = tail; 08167 } 08168 else if (NIL_P(tail->nd_lit)) { 08169 head->nd_alen += tail->nd_alen - 1; 08170 head->nd_next->nd_end->nd_next = tail->nd_next; 08171 head->nd_next->nd_end = tail->nd_next->nd_end; 08172 rb_gc_force_recycle((VALUE)tail); 08173 } 08174 else { 08175 nd_set_type(tail, NODE_ARRAY); 08176 tail->nd_head = NEW_STR(tail->nd_lit); 08177 list_concat(head, tail); 08178 } 08179 break; 08180 08181 case NODE_EVSTR: 08182 if (htype == NODE_STR) { 08183 nd_set_type(head, NODE_DSTR); 08184 head->nd_alen = 1; 08185 } 08186 list_append(head, tail); 08187 break; 08188 } 08189 return head; 08190 } 08191 08192 static NODE * 08193 evstr2dstr_gen(struct parser_params *parser, NODE *node) 08194 { 08195 if (nd_type(node) == NODE_EVSTR) { 08196 node = list_append(NEW_DSTR(Qnil), node); 08197 } 08198 return node; 08199 } 08200 08201 static NODE * 08202 new_evstr_gen(struct parser_params *parser, NODE *node) 08203 { 08204 NODE *head = node; 08205 08206 if (node) { 08207 switch (nd_type(node)) { 08208 case NODE_STR: case NODE_DSTR: case NODE_EVSTR: 08209 return node; 08210 } 08211 } 08212 return NEW_EVSTR(head); 08213 } 08214 08215 static NODE * 08216 call_bin_op_gen(struct parser_params *parser, NODE *recv, ID id, NODE *arg1) 08217 { 08218 value_expr(recv); 08219 value_expr(arg1); 08220 return NEW_CALL(recv, id, NEW_LIST(arg1)); 08221 } 08222 08223 static NODE * 08224 call_uni_op_gen(struct parser_params *parser, NODE *recv, ID id) 08225 { 08226 value_expr(recv); 08227 return NEW_CALL(recv, id, 0); 08228 } 08229 08230 static NODE* 08231 match_op_gen(struct parser_params *parser, NODE *node1, NODE *node2) 08232 { 08233 value_expr(node1); 08234 value_expr(node2); 08235 if (node1) { 08236 switch (nd_type(node1)) { 08237 case NODE_DREGX: 08238 case NODE_DREGX_ONCE: 08239 return NEW_MATCH2(node1, node2); 08240 08241 case NODE_LIT: 08242 if (TYPE(node1->nd_lit) == T_REGEXP) { 08243 return NEW_MATCH2(node1, node2); 08244 } 08245 } 08246 } 08247 08248 if (node2) { 08249 switch (nd_type(node2)) { 08250 case NODE_DREGX: 08251 case NODE_DREGX_ONCE: 08252 return NEW_MATCH3(node2, node1); 08253 08254 case NODE_LIT: 08255 if (TYPE(node2->nd_lit) == T_REGEXP) { 08256 return NEW_MATCH3(node2, node1); 08257 } 08258 } 08259 } 08260 08261 return NEW_CALL(node1, tMATCH, NEW_LIST(node2)); 08262 } 08263 08264 static NODE* 08265 gettable_gen(struct parser_params *parser, ID id) 08266 { 08267 if (id == keyword_self) { 08268 return NEW_SELF(); 08269 } 08270 else if (id == keyword_nil) { 08271 return NEW_NIL(); 08272 } 08273 else if (id == keyword_true) { 08274 return NEW_TRUE(); 08275 } 08276 else if (id == keyword_false) { 08277 return NEW_FALSE(); 08278 } 08279 else if (id == keyword__FILE__) { 08280 return NEW_STR(rb_external_str_new_with_enc(ruby_sourcefile, strlen(ruby_sourcefile), 08281 rb_filesystem_encoding())); 08282 } 08283 else if (id == keyword__LINE__) { 08284 return NEW_LIT(INT2FIX(ruby_sourceline)); 08285 } 08286 else if (id == keyword__ENCODING__) { 08287 return NEW_LIT(rb_enc_from_encoding(parser->enc)); 08288 } 08289 else if (is_local_id(id)) { 08290 if (dyna_in_block() && dvar_defined(id)) return NEW_DVAR(id); 08291 if (local_id(id)) return NEW_LVAR(id); 08292 /* method call without arguments */ 08293 return NEW_VCALL(id); 08294 } 08295 else if (is_global_id(id)) { 08296 return NEW_GVAR(id); 08297 } 08298 else if (is_instance_id(id)) { 08299 return NEW_IVAR(id); 08300 } 08301 else if (is_const_id(id)) { 08302 return NEW_CONST(id); 08303 } 08304 else if (is_class_id(id)) { 08305 return NEW_CVAR(id); 08306 } 08307 compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2name(id)); 08308 return 0; 08309 } 08310 #else /* !RIPPER */ 08311 static int 08312 id_is_var_gen(struct parser_params *parser, ID id) 08313 { 08314 if (is_notop_id(id)) { 08315 switch (id & ID_SCOPE_MASK) { 08316 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS: 08317 return 1; 08318 case ID_LOCAL: 08319 if (dyna_in_block() && dvar_defined(id)) return 1; 08320 if (local_id(id)) return 1; 08321 /* method call without arguments */ 08322 return 0; 08323 } 08324 } 08325 compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2name(id)); 08326 return 0; 08327 } 08328 #endif /* !RIPPER */ 08329 08330 #ifdef RIPPER 08331 static VALUE 08332 assignable_gen(struct parser_params *parser, VALUE lhs) 08333 #else 08334 static NODE* 08335 assignable_gen(struct parser_params *parser, ID id, NODE *val) 08336 #endif 08337 { 08338 #ifdef RIPPER 08339 ID id = get_id(lhs); 08340 # define assignable_result(x) get_value(lhs) 08341 # define parser_yyerror(parser, x) dispatch1(assign_error, lhs) 08342 #else 08343 # define assignable_result(x) (x) 08344 #endif 08345 if (!id) return assignable_result(0); 08346 if (id == keyword_self) { 08347 yyerror("Can't change the value of self"); 08348 } 08349 else if (id == keyword_nil) { 08350 yyerror("Can't assign to nil"); 08351 } 08352 else if (id == keyword_true) { 08353 yyerror("Can't assign to true"); 08354 } 08355 else if (id == keyword_false) { 08356 yyerror("Can't assign to false"); 08357 } 08358 else if (id == keyword__FILE__) { 08359 yyerror("Can't assign to __FILE__"); 08360 } 08361 else if (id == keyword__LINE__) { 08362 yyerror("Can't assign to __LINE__"); 08363 } 08364 else if (id == keyword__ENCODING__) { 08365 yyerror("Can't assign to __ENCODING__"); 08366 } 08367 else if (is_local_id(id)) { 08368 if (dyna_in_block()) { 08369 if (dvar_curr(id)) { 08370 return assignable_result(NEW_DASGN_CURR(id, val)); 08371 } 08372 else if (dvar_defined(id)) { 08373 return assignable_result(NEW_DASGN(id, val)); 08374 } 08375 else if (local_id(id)) { 08376 return assignable_result(NEW_LASGN(id, val)); 08377 } 08378 else { 08379 dyna_var(id); 08380 return assignable_result(NEW_DASGN_CURR(id, val)); 08381 } 08382 } 08383 else { 08384 if (!local_id(id)) { 08385 local_var(id); 08386 } 08387 return assignable_result(NEW_LASGN(id, val)); 08388 } 08389 } 08390 else if (is_global_id(id)) { 08391 return assignable_result(NEW_GASGN(id, val)); 08392 } 08393 else if (is_instance_id(id)) { 08394 return assignable_result(NEW_IASGN(id, val)); 08395 } 08396 else if (is_const_id(id)) { 08397 if (!in_def && !in_single) 08398 return assignable_result(NEW_CDECL(id, val, 0)); 08399 yyerror("dynamic constant assignment"); 08400 } 08401 else if (is_class_id(id)) { 08402 return assignable_result(NEW_CVASGN(id, val)); 08403 } 08404 else { 08405 compile_error(PARSER_ARG "identifier %s is not valid to set", rb_id2name(id)); 08406 } 08407 return assignable_result(0); 08408 #undef assignable_result 08409 #undef parser_yyerror 08410 } 08411 08412 #define LVAR_USED ((int)1 << (sizeof(int) * CHAR_BIT - 1)) 08413 08414 static ID 08415 shadowing_lvar_gen(struct parser_params *parser, ID name) 08416 { 08417 if (idUScore == name) return name; 08418 if (dyna_in_block()) { 08419 if (dvar_curr(name)) { 08420 yyerror("duplicated argument name"); 08421 } 08422 else if (dvar_defined_get(name) || local_id(name)) { 08423 rb_warningS("shadowing outer local variable - %s", rb_id2name(name)); 08424 vtable_add(lvtbl->vars, name); 08425 if (lvtbl->used) { 08426 vtable_add(lvtbl->used, (ID)ruby_sourceline | LVAR_USED); 08427 } 08428 } 08429 } 08430 else { 08431 if (local_id(name)) { 08432 yyerror("duplicated argument name"); 08433 } 08434 } 08435 return name; 08436 } 08437 08438 static void 08439 new_bv_gen(struct parser_params *parser, ID name) 08440 { 08441 if (!name) return; 08442 if (!is_local_id(name)) { 08443 compile_error(PARSER_ARG "invalid local variable - %s", 08444 rb_id2name(name)); 08445 return; 08446 } 08447 shadowing_lvar(name); 08448 dyna_var(name); 08449 } 08450 08451 #ifndef RIPPER 08452 static NODE * 08453 aryset_gen(struct parser_params *parser, NODE *recv, NODE *idx) 08454 { 08455 if (recv && nd_type(recv) == NODE_SELF) 08456 recv = (NODE *)1; 08457 return NEW_ATTRASGN(recv, tASET, idx); 08458 } 08459 08460 static void 08461 block_dup_check_gen(struct parser_params *parser, NODE *node1, NODE *node2) 08462 { 08463 if (node2 && node1 && nd_type(node1) == NODE_BLOCK_PASS) { 08464 compile_error(PARSER_ARG "both block arg and actual block given"); 08465 } 08466 } 08467 08468 ID 08469 rb_id_attrset(ID id) 08470 { 08471 id &= ~ID_SCOPE_MASK; 08472 id |= ID_ATTRSET; 08473 return id; 08474 } 08475 08476 static NODE * 08477 attrset_gen(struct parser_params *parser, NODE *recv, ID id) 08478 { 08479 if (recv && nd_type(recv) == NODE_SELF) 08480 recv = (NODE *)1; 08481 return NEW_ATTRASGN(recv, rb_id_attrset(id), 0); 08482 } 08483 08484 static void 08485 rb_backref_error_gen(struct parser_params *parser, NODE *node) 08486 { 08487 switch (nd_type(node)) { 08488 case NODE_NTH_REF: 08489 compile_error(PARSER_ARG "Can't set variable $%ld", node->nd_nth); 08490 break; 08491 case NODE_BACK_REF: 08492 compile_error(PARSER_ARG "Can't set variable $%c", (int)node->nd_nth); 08493 break; 08494 } 08495 } 08496 08497 static NODE * 08498 arg_concat_gen(struct parser_params *parser, NODE *node1, NODE *node2) 08499 { 08500 if (!node2) return node1; 08501 switch (nd_type(node1)) { 08502 case NODE_BLOCK_PASS: 08503 if (node1->nd_head) 08504 node1->nd_head = arg_concat(node1->nd_head, node2); 08505 else 08506 node1->nd_head = NEW_LIST(node2); 08507 return node1; 08508 case NODE_ARGSPUSH: 08509 if (nd_type(node2) != NODE_ARRAY) break; 08510 node1->nd_body = list_concat(NEW_LIST(node1->nd_body), node2); 08511 nd_set_type(node1, NODE_ARGSCAT); 08512 return node1; 08513 case NODE_ARGSCAT: 08514 if (nd_type(node2) != NODE_ARRAY || 08515 nd_type(node1->nd_body) != NODE_ARRAY) break; 08516 node1->nd_body = list_concat(node1->nd_body, node2); 08517 return node1; 08518 } 08519 return NEW_ARGSCAT(node1, node2); 08520 } 08521 08522 static NODE * 08523 arg_append_gen(struct parser_params *parser, NODE *node1, NODE *node2) 08524 { 08525 if (!node1) return NEW_LIST(node2); 08526 switch (nd_type(node1)) { 08527 case NODE_ARRAY: 08528 return list_append(node1, node2); 08529 case NODE_BLOCK_PASS: 08530 node1->nd_head = arg_append(node1->nd_head, node2); 08531 return node1; 08532 case NODE_ARGSPUSH: 08533 node1->nd_body = list_append(NEW_LIST(node1->nd_body), node2); 08534 nd_set_type(node1, NODE_ARGSCAT); 08535 return node1; 08536 } 08537 return NEW_ARGSPUSH(node1, node2); 08538 } 08539 08540 static NODE * 08541 splat_array(NODE* node) 08542 { 08543 if (nd_type(node) == NODE_SPLAT) node = node->nd_head; 08544 if (nd_type(node) == NODE_ARRAY) return node; 08545 return 0; 08546 } 08547 08548 static NODE * 08549 node_assign_gen(struct parser_params *parser, NODE *lhs, NODE *rhs) 08550 { 08551 if (!lhs) return 0; 08552 08553 switch (nd_type(lhs)) { 08554 case NODE_GASGN: 08555 case NODE_IASGN: 08556 case NODE_IASGN2: 08557 case NODE_LASGN: 08558 case NODE_DASGN: 08559 case NODE_DASGN_CURR: 08560 case NODE_MASGN: 08561 case NODE_CDECL: 08562 case NODE_CVASGN: 08563 lhs->nd_value = rhs; 08564 break; 08565 08566 case NODE_ATTRASGN: 08567 case NODE_CALL: 08568 lhs->nd_args = arg_append(lhs->nd_args, rhs); 08569 break; 08570 08571 default: 08572 /* should not happen */ 08573 break; 08574 } 08575 08576 return lhs; 08577 } 08578 08579 static int 08580 value_expr_gen(struct parser_params *parser, NODE *node) 08581 { 08582 int cond = 0; 08583 08584 if (!node) { 08585 rb_warning0("empty expression"); 08586 } 08587 while (node) { 08588 switch (nd_type(node)) { 08589 case NODE_DEFN: 08590 case NODE_DEFS: 08591 parser_warning(node, "void value expression"); 08592 return FALSE; 08593 08594 case NODE_RETURN: 08595 case NODE_BREAK: 08596 case NODE_NEXT: 08597 case NODE_REDO: 08598 case NODE_RETRY: 08599 if (!cond) yyerror("void value expression"); 08600 /* or "control never reach"? */ 08601 return FALSE; 08602 08603 case NODE_BLOCK: 08604 while (node->nd_next) { 08605 node = node->nd_next; 08606 } 08607 node = node->nd_head; 08608 break; 08609 08610 case NODE_BEGIN: 08611 node = node->nd_body; 08612 break; 08613 08614 case NODE_IF: 08615 if (!node->nd_body) { 08616 node = node->nd_else; 08617 break; 08618 } 08619 else if (!node->nd_else) { 08620 node = node->nd_body; 08621 break; 08622 } 08623 if (!value_expr(node->nd_body)) return FALSE; 08624 node = node->nd_else; 08625 break; 08626 08627 case NODE_AND: 08628 case NODE_OR: 08629 cond = 1; 08630 node = node->nd_2nd; 08631 break; 08632 08633 default: 08634 return TRUE; 08635 } 08636 } 08637 08638 return TRUE; 08639 } 08640 08641 static void 08642 void_expr_gen(struct parser_params *parser, NODE *node) 08643 { 08644 const char *useless = 0; 08645 08646 if (!RTEST(ruby_verbose)) return; 08647 08648 if (!node) return; 08649 switch (nd_type(node)) { 08650 case NODE_CALL: 08651 switch (node->nd_mid) { 08652 case '+': 08653 case '-': 08654 case '*': 08655 case '/': 08656 case '%': 08657 case tPOW: 08658 case tUPLUS: 08659 case tUMINUS: 08660 case '|': 08661 case '^': 08662 case '&': 08663 case tCMP: 08664 case '>': 08665 case tGEQ: 08666 case '<': 08667 case tLEQ: 08668 case tEQ: 08669 case tNEQ: 08670 useless = rb_id2name(node->nd_mid); 08671 break; 08672 } 08673 break; 08674 08675 case NODE_LVAR: 08676 case NODE_DVAR: 08677 case NODE_GVAR: 08678 case NODE_IVAR: 08679 case NODE_CVAR: 08680 case NODE_NTH_REF: 08681 case NODE_BACK_REF: 08682 useless = "a variable"; 08683 break; 08684 case NODE_CONST: 08685 useless = "a constant"; 08686 break; 08687 case NODE_LIT: 08688 case NODE_STR: 08689 case NODE_DSTR: 08690 case NODE_DREGX: 08691 case NODE_DREGX_ONCE: 08692 useless = "a literal"; 08693 break; 08694 case NODE_COLON2: 08695 case NODE_COLON3: 08696 useless = "::"; 08697 break; 08698 case NODE_DOT2: 08699 useless = ".."; 08700 break; 08701 case NODE_DOT3: 08702 useless = "..."; 08703 break; 08704 case NODE_SELF: 08705 useless = "self"; 08706 break; 08707 case NODE_NIL: 08708 useless = "nil"; 08709 break; 08710 case NODE_TRUE: 08711 useless = "true"; 08712 break; 08713 case NODE_FALSE: 08714 useless = "false"; 08715 break; 08716 case NODE_DEFINED: 08717 useless = "defined?"; 08718 break; 08719 } 08720 08721 if (useless) { 08722 int line = ruby_sourceline; 08723 08724 ruby_sourceline = nd_line(node); 08725 rb_warnS("possibly useless use of %s in void context", useless); 08726 ruby_sourceline = line; 08727 } 08728 } 08729 08730 static void 08731 void_stmts_gen(struct parser_params *parser, NODE *node) 08732 { 08733 if (!RTEST(ruby_verbose)) return; 08734 if (!node) return; 08735 if (nd_type(node) != NODE_BLOCK) return; 08736 08737 for (;;) { 08738 if (!node->nd_next) return; 08739 void_expr0(node->nd_head); 08740 node = node->nd_next; 08741 } 08742 } 08743 08744 static NODE * 08745 remove_begin(NODE *node) 08746 { 08747 NODE **n = &node, *n1 = node; 08748 while (n1 && nd_type(n1) == NODE_BEGIN && n1->nd_body) { 08749 *n = n1 = n1->nd_body; 08750 } 08751 return node; 08752 } 08753 08754 static void 08755 reduce_nodes_gen(struct parser_params *parser, NODE **body) 08756 { 08757 NODE *node = *body; 08758 08759 if (!node) { 08760 *body = NEW_NIL(); 08761 return; 08762 } 08763 #define subnodes(n1, n2) \ 08764 ((!node->n1) ? (node->n2 ? (body = &node->n2, 1) : 0) : \ 08765 (!node->n2) ? (body = &node->n1, 1) : \ 08766 (reduce_nodes(&node->n1), body = &node->n2, 1)) 08767 08768 while (node) { 08769 int newline = (int)(node->flags & NODE_FL_NEWLINE); 08770 switch (nd_type(node)) { 08771 end: 08772 case NODE_NIL: 08773 *body = 0; 08774 return; 08775 case NODE_RETURN: 08776 *body = node = node->nd_stts; 08777 if (newline && node) node->flags |= NODE_FL_NEWLINE; 08778 continue; 08779 case NODE_BEGIN: 08780 *body = node = node->nd_body; 08781 if (newline && node) node->flags |= NODE_FL_NEWLINE; 08782 continue; 08783 case NODE_BLOCK: 08784 body = &node->nd_end->nd_head; 08785 break; 08786 case NODE_IF: 08787 if (subnodes(nd_body, nd_else)) break; 08788 return; 08789 case NODE_CASE: 08790 body = &node->nd_body; 08791 break; 08792 case NODE_WHEN: 08793 if (!subnodes(nd_body, nd_next)) goto end; 08794 break; 08795 case NODE_ENSURE: 08796 if (!subnodes(nd_head, nd_resq)) goto end; 08797 break; 08798 case NODE_RESCUE: 08799 if (node->nd_else) { 08800 body = &node->nd_resq; 08801 break; 08802 } 08803 if (!subnodes(nd_head, nd_resq)) goto end; 08804 break; 08805 default: 08806 return; 08807 } 08808 node = *body; 08809 if (newline && node) node->flags |= NODE_FL_NEWLINE; 08810 } 08811 08812 #undef subnodes 08813 } 08814 08815 static int 08816 assign_in_cond(struct parser_params *parser, NODE *node) 08817 { 08818 switch (nd_type(node)) { 08819 case NODE_MASGN: 08820 yyerror("multiple assignment in conditional"); 08821 return 1; 08822 08823 case NODE_LASGN: 08824 case NODE_DASGN: 08825 case NODE_DASGN_CURR: 08826 case NODE_GASGN: 08827 case NODE_IASGN: 08828 break; 08829 08830 default: 08831 return 0; 08832 } 08833 08834 if (!node->nd_value) return 1; 08835 switch (nd_type(node->nd_value)) { 08836 case NODE_LIT: 08837 case NODE_STR: 08838 case NODE_NIL: 08839 case NODE_TRUE: 08840 case NODE_FALSE: 08841 /* reports always */ 08842 parser_warn(node->nd_value, "found = in conditional, should be =="); 08843 return 1; 08844 08845 case NODE_DSTR: 08846 case NODE_XSTR: 08847 case NODE_DXSTR: 08848 case NODE_EVSTR: 08849 case NODE_DREGX: 08850 default: 08851 break; 08852 } 08853 return 1; 08854 } 08855 08856 static void 08857 warn_unless_e_option(struct parser_params *parser, NODE *node, const char *str) 08858 { 08859 if (!e_option_supplied(parser)) parser_warn(node, str); 08860 } 08861 08862 static void 08863 warning_unless_e_option(struct parser_params *parser, NODE *node, const char *str) 08864 { 08865 if (!e_option_supplied(parser)) parser_warning(node, str); 08866 } 08867 08868 static void 08869 fixup_nodes(NODE **rootnode) 08870 { 08871 NODE *node, *next, *head; 08872 08873 for (node = *rootnode; node; node = next) { 08874 enum node_type type; 08875 VALUE val; 08876 08877 next = node->nd_next; 08878 head = node->nd_head; 08879 rb_gc_force_recycle((VALUE)node); 08880 *rootnode = next; 08881 switch (type = nd_type(head)) { 08882 case NODE_DOT2: 08883 case NODE_DOT3: 08884 val = rb_range_new(head->nd_beg->nd_lit, head->nd_end->nd_lit, 08885 type == NODE_DOT3); 08886 rb_gc_force_recycle((VALUE)head->nd_beg); 08887 rb_gc_force_recycle((VALUE)head->nd_end); 08888 nd_set_type(head, NODE_LIT); 08889 head->nd_lit = val; 08890 break; 08891 default: 08892 break; 08893 } 08894 } 08895 } 08896 08897 static NODE *cond0(struct parser_params*,NODE*); 08898 08899 static NODE* 08900 range_op(struct parser_params *parser, NODE *node) 08901 { 08902 enum node_type type; 08903 08904 if (node == 0) return 0; 08905 08906 type = nd_type(node); 08907 value_expr(node); 08908 if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) { 08909 warn_unless_e_option(parser, node, "integer literal in conditional range"); 08910 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(rb_intern("$.")))); 08911 } 08912 return cond0(parser, node); 08913 } 08914 08915 static int 08916 literal_node(NODE *node) 08917 { 08918 if (!node) return 1; /* same as NODE_NIL */ 08919 switch (nd_type(node)) { 08920 case NODE_LIT: 08921 case NODE_STR: 08922 case NODE_DSTR: 08923 case NODE_EVSTR: 08924 case NODE_DREGX: 08925 case NODE_DREGX_ONCE: 08926 case NODE_DSYM: 08927 return 2; 08928 case NODE_TRUE: 08929 case NODE_FALSE: 08930 case NODE_NIL: 08931 return 1; 08932 } 08933 return 0; 08934 } 08935 08936 static NODE* 08937 cond0(struct parser_params *parser, NODE *node) 08938 { 08939 if (node == 0) return 0; 08940 assign_in_cond(parser, node); 08941 08942 switch (nd_type(node)) { 08943 case NODE_DSTR: 08944 case NODE_EVSTR: 08945 case NODE_STR: 08946 rb_warn0("string literal in condition"); 08947 break; 08948 08949 case NODE_DREGX: 08950 case NODE_DREGX_ONCE: 08951 warning_unless_e_option(parser, node, "regex literal in condition"); 08952 return NEW_MATCH2(node, NEW_GVAR(rb_intern("$_"))); 08953 08954 case NODE_AND: 08955 case NODE_OR: 08956 node->nd_1st = cond0(parser, node->nd_1st); 08957 node->nd_2nd = cond0(parser, node->nd_2nd); 08958 break; 08959 08960 case NODE_DOT2: 08961 case NODE_DOT3: 08962 node->nd_beg = range_op(parser, node->nd_beg); 08963 node->nd_end = range_op(parser, node->nd_end); 08964 if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2); 08965 else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3); 08966 if (!e_option_supplied(parser)) { 08967 int b = literal_node(node->nd_beg); 08968 int e = literal_node(node->nd_end); 08969 if ((b == 1 && e == 1) || (b + e >= 2 && RTEST(ruby_verbose))) { 08970 parser_warn(node, "range literal in condition"); 08971 } 08972 } 08973 break; 08974 08975 case NODE_DSYM: 08976 parser_warning(node, "literal in condition"); 08977 break; 08978 08979 case NODE_LIT: 08980 if (TYPE(node->nd_lit) == T_REGEXP) { 08981 warn_unless_e_option(parser, node, "regex literal in condition"); 08982 nd_set_type(node, NODE_MATCH); 08983 } 08984 else { 08985 parser_warning(node, "literal in condition"); 08986 } 08987 default: 08988 break; 08989 } 08990 return node; 08991 } 08992 08993 static NODE* 08994 cond_gen(struct parser_params *parser, NODE *node) 08995 { 08996 if (node == 0) return 0; 08997 return cond0(parser, node); 08998 } 08999 09000 static NODE* 09001 logop_gen(struct parser_params *parser, enum node_type type, NODE *left, NODE *right) 09002 { 09003 value_expr(left); 09004 if (left && (enum node_type)nd_type(left) == type) { 09005 NODE *node = left, *second; 09006 while ((second = node->nd_2nd) != 0 && (enum node_type)nd_type(second) == type) { 09007 node = second; 09008 } 09009 node->nd_2nd = NEW_NODE(type, second, right, 0); 09010 return left; 09011 } 09012 return NEW_NODE(type, left, right, 0); 09013 } 09014 09015 static void 09016 no_blockarg(struct parser_params *parser, NODE *node) 09017 { 09018 if (node && nd_type(node) == NODE_BLOCK_PASS) { 09019 compile_error(PARSER_ARG "block argument should not be given"); 09020 } 09021 } 09022 09023 static NODE * 09024 ret_args_gen(struct parser_params *parser, NODE *node) 09025 { 09026 if (node) { 09027 no_blockarg(parser, node); 09028 if (nd_type(node) == NODE_ARRAY) { 09029 if (node->nd_next == 0) { 09030 node = node->nd_head; 09031 } 09032 else { 09033 nd_set_type(node, NODE_VALUES); 09034 } 09035 } 09036 } 09037 return node; 09038 } 09039 09040 static NODE * 09041 new_yield_gen(struct parser_params *parser, NODE *node) 09042 { 09043 long state = Qtrue; 09044 09045 if (node) { 09046 no_blockarg(parser, node); 09047 if (node && nd_type(node) == NODE_SPLAT) { 09048 state = Qtrue; 09049 } 09050 } 09051 else { 09052 state = Qfalse; 09053 } 09054 return NEW_YIELD(node, state); 09055 } 09056 09057 static NODE* 09058 negate_lit(NODE *node) 09059 { 09060 switch (TYPE(node->nd_lit)) { 09061 case T_FIXNUM: 09062 node->nd_lit = LONG2FIX(-FIX2LONG(node->nd_lit)); 09063 break; 09064 case T_BIGNUM: 09065 node->nd_lit = rb_funcall(node->nd_lit,tUMINUS,0,0); 09066 break; 09067 case T_FLOAT: 09068 RFLOAT(node->nd_lit)->float_value = -RFLOAT_VALUE(node->nd_lit); 09069 break; 09070 default: 09071 break; 09072 } 09073 return node; 09074 } 09075 09076 static NODE * 09077 arg_blk_pass(NODE *node1, NODE *node2) 09078 { 09079 if (node2) { 09080 node2->nd_head = node1; 09081 return node2; 09082 } 09083 return node1; 09084 } 09085 09086 static NODE* 09087 new_args_gen(struct parser_params *parser, NODE *m, NODE *o, ID r, NODE *p, ID b) 09088 { 09089 int saved_line = ruby_sourceline; 09090 NODE *node; 09091 NODE *i1, *i2 = 0; 09092 09093 node = NEW_ARGS(m ? m->nd_plen : 0, o); 09094 i1 = m ? m->nd_next : 0; 09095 node->nd_next = NEW_ARGS_AUX(r, b); 09096 09097 if (p) { 09098 i2 = p->nd_next; 09099 node->nd_next->nd_next = NEW_ARGS_AUX(p->nd_pid, p->nd_plen); 09100 } 09101 else if (i1) { 09102 node->nd_next->nd_next = NEW_ARGS_AUX(0, 0); 09103 } 09104 if (i1 || i2) { 09105 node->nd_next->nd_next->nd_next = NEW_NODE(NODE_AND, i1, i2, 0); 09106 } 09107 ruby_sourceline = saved_line; 09108 return node; 09109 } 09110 #endif /* !RIPPER */ 09111 09112 static void 09113 warn_unused_var(struct parser_params *parser, struct local_vars *local) 09114 { 09115 int i, cnt; 09116 ID *v, *u; 09117 09118 if (!local->used) return; 09119 v = local->vars->tbl; 09120 u = local->used->tbl; 09121 cnt = local->used->pos; 09122 if (cnt != local->vars->pos) { 09123 rb_bug("local->used->pos != local->vars->pos"); 09124 } 09125 for (i = 0; i < cnt; ++i) { 09126 if (!v[i] || (u[i] & LVAR_USED)) continue; 09127 if (idUScore == v[i]) continue; 09128 rb_compile_warn(ruby_sourcefile, (int)u[i], "assigned but unused variable - %s", rb_id2name(v[i])); 09129 } 09130 } 09131 09132 static void 09133 local_push_gen(struct parser_params *parser, int inherit_dvars) 09134 { 09135 struct local_vars *local; 09136 09137 local = ALLOC(struct local_vars); 09138 local->prev = lvtbl; 09139 local->args = vtable_alloc(0); 09140 local->vars = vtable_alloc(inherit_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE); 09141 local->used = !inherit_dvars && RTEST(ruby_verbose) ? vtable_alloc(0) : 0; 09142 lvtbl = local; 09143 } 09144 09145 static void 09146 local_pop_gen(struct parser_params *parser) 09147 { 09148 struct local_vars *local = lvtbl->prev; 09149 if (lvtbl->used) { 09150 warn_unused_var(parser, lvtbl); 09151 vtable_free(lvtbl->used); 09152 } 09153 vtable_free(lvtbl->args); 09154 vtable_free(lvtbl->vars); 09155 xfree(lvtbl); 09156 lvtbl = local; 09157 } 09158 09159 #ifndef RIPPER 09160 static ID* 09161 vtable_tblcpy(ID *buf, const struct vtable *src) 09162 { 09163 int i, cnt = vtable_size(src); 09164 09165 if (cnt > 0) { 09166 buf[0] = cnt; 09167 for (i = 0; i < cnt; i++) { 09168 buf[i] = src->tbl[i]; 09169 } 09170 return buf; 09171 } 09172 return 0; 09173 } 09174 09175 static ID* 09176 local_tbl_gen(struct parser_params *parser) 09177 { 09178 int cnt = vtable_size(lvtbl->args) + vtable_size(lvtbl->vars); 09179 ID *buf; 09180 09181 if (cnt <= 0) return 0; 09182 buf = ALLOC_N(ID, cnt + 1); 09183 vtable_tblcpy(buf+1, lvtbl->args); 09184 vtable_tblcpy(buf+vtable_size(lvtbl->args)+1, lvtbl->vars); 09185 buf[0] = cnt; 09186 return buf; 09187 } 09188 #endif 09189 09190 static int 09191 arg_var_gen(struct parser_params *parser, ID id) 09192 { 09193 vtable_add(lvtbl->args, id); 09194 return vtable_size(lvtbl->args) - 1; 09195 } 09196 09197 static int 09198 local_var_gen(struct parser_params *parser, ID id) 09199 { 09200 vtable_add(lvtbl->vars, id); 09201 if (lvtbl->used) { 09202 vtable_add(lvtbl->used, (ID)ruby_sourceline); 09203 } 09204 return vtable_size(lvtbl->vars) - 1; 09205 } 09206 09207 static int 09208 local_id_gen(struct parser_params *parser, ID id) 09209 { 09210 struct vtable *vars, *args, *used; 09211 09212 vars = lvtbl->vars; 09213 args = lvtbl->args; 09214 used = lvtbl->used; 09215 09216 while (vars && POINTER_P(vars->prev)) { 09217 vars = vars->prev; 09218 args = args->prev; 09219 if (used) used = used->prev; 09220 } 09221 09222 if (vars && vars->prev == DVARS_INHERIT) { 09223 return rb_local_defined(id); 09224 } 09225 else if (vtable_included(args, id)) { 09226 return 1; 09227 } 09228 else { 09229 int i = vtable_included(vars, id); 09230 if (i && used) used->tbl[i-1] |= LVAR_USED; 09231 return i != 0; 09232 } 09233 } 09234 09235 static const struct vtable * 09236 dyna_push_gen(struct parser_params *parser) 09237 { 09238 lvtbl->args = vtable_alloc(lvtbl->args); 09239 lvtbl->vars = vtable_alloc(lvtbl->vars); 09240 if (lvtbl->used) { 09241 lvtbl->used = vtable_alloc(lvtbl->used); 09242 } 09243 return lvtbl->args; 09244 } 09245 09246 static void 09247 dyna_pop_1(struct parser_params *parser) 09248 { 09249 struct vtable *tmp; 09250 09251 if ((tmp = lvtbl->used) != 0) { 09252 warn_unused_var(parser, lvtbl); 09253 lvtbl->used = lvtbl->used->prev; 09254 vtable_free(tmp); 09255 } 09256 tmp = lvtbl->args; 09257 lvtbl->args = lvtbl->args->prev; 09258 vtable_free(tmp); 09259 tmp = lvtbl->vars; 09260 lvtbl->vars = lvtbl->vars->prev; 09261 vtable_free(tmp); 09262 } 09263 09264 static void 09265 dyna_pop_gen(struct parser_params *parser, const struct vtable *lvargs) 09266 { 09267 while (lvtbl->args != lvargs) { 09268 dyna_pop_1(parser); 09269 if (!lvtbl->args) { 09270 struct local_vars *local = lvtbl->prev; 09271 xfree(lvtbl); 09272 lvtbl = local; 09273 } 09274 } 09275 dyna_pop_1(parser); 09276 } 09277 09278 static int 09279 dyna_in_block_gen(struct parser_params *parser) 09280 { 09281 return POINTER_P(lvtbl->vars) && lvtbl->vars->prev != DVARS_TOPSCOPE; 09282 } 09283 09284 static int 09285 dvar_defined_gen(struct parser_params *parser, ID id, int get) 09286 { 09287 struct vtable *vars, *args, *used; 09288 int i; 09289 09290 args = lvtbl->args; 09291 vars = lvtbl->vars; 09292 used = lvtbl->used; 09293 09294 while (POINTER_P(vars)) { 09295 if (vtable_included(args, id)) { 09296 return 1; 09297 } 09298 if ((i = vtable_included(vars, id)) != 0) { 09299 if (used) used->tbl[i-1] |= LVAR_USED; 09300 return 1; 09301 } 09302 args = args->prev; 09303 vars = vars->prev; 09304 if (get) used = 0; 09305 if (used) used = used->prev; 09306 } 09307 09308 if (vars == DVARS_INHERIT) { 09309 return rb_dvar_defined(id); 09310 } 09311 09312 return 0; 09313 } 09314 09315 static int 09316 dvar_curr_gen(struct parser_params *parser, ID id) 09317 { 09318 return (vtable_included(lvtbl->args, id) || 09319 vtable_included(lvtbl->vars, id)); 09320 } 09321 09322 #ifndef RIPPER 09323 static void 09324 reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options) 09325 { 09326 int c = RE_OPTION_ENCODING_IDX(options); 09327 09328 if (c) { 09329 int opt, idx; 09330 rb_char_to_option_kcode(c, &opt, &idx); 09331 if (idx != ENCODING_GET(str) && 09332 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { 09333 goto error; 09334 } 09335 ENCODING_SET(str, idx); 09336 } 09337 else if (RE_OPTION_ENCODING_NONE(options)) { 09338 if (!ENCODING_IS_ASCII8BIT(str) && 09339 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { 09340 c = 'n'; 09341 goto error; 09342 } 09343 rb_enc_associate(str, rb_ascii8bit_encoding()); 09344 } 09345 else if (parser->enc == rb_usascii_encoding()) { 09346 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { 09347 /* raise in re.c */ 09348 rb_enc_associate(str, rb_usascii_encoding()); 09349 } 09350 else { 09351 rb_enc_associate(str, rb_ascii8bit_encoding()); 09352 } 09353 } 09354 return; 09355 09356 error: 09357 compile_error(PARSER_ARG 09358 "regexp encoding option '%c' differs from source encoding '%s'", 09359 c, rb_enc_name(rb_enc_get(str))); 09360 } 09361 09362 static int 09363 reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options) 09364 { 09365 VALUE err; 09366 reg_fragment_setenc(str, options); 09367 err = rb_reg_check_preprocess(str); 09368 if (err != Qnil) { 09369 err = rb_obj_as_string(err); 09370 compile_error(PARSER_ARG "%s", RSTRING_PTR(err)); 09371 RB_GC_GUARD(err); 09372 return 0; 09373 } 09374 return 1; 09375 } 09376 09377 typedef struct { 09378 struct parser_params* parser; 09379 rb_encoding *enc; 09380 NODE *succ_block; 09381 NODE *fail_block; 09382 int num; 09383 } reg_named_capture_assign_t; 09384 09385 static int 09386 reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end, 09387 int back_num, int *back_refs, OnigRegex regex, void *arg0) 09388 { 09389 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0; 09390 struct parser_params* parser = arg->parser; 09391 rb_encoding *enc = arg->enc; 09392 long len = name_end - name; 09393 const char *s = (const char *)name; 09394 ID var; 09395 09396 arg->num++; 09397 09398 if (arg->succ_block == 0) { 09399 arg->succ_block = NEW_BEGIN(0); 09400 arg->fail_block = NEW_BEGIN(0); 09401 } 09402 09403 if (!len || (*name != '_' && ISASCII(*name) && !rb_enc_islower(*name, enc)) || 09404 (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) || 09405 !rb_enc_symname2_p(s, len, enc)) { 09406 return ST_CONTINUE; 09407 } 09408 var = rb_intern3(s, len, enc); 09409 if (dvar_defined(var) || local_id(var)) { 09410 rb_warningS("named capture conflicts a local variable - %s", 09411 rb_id2name(var)); 09412 } 09413 arg->succ_block = block_append(arg->succ_block, 09414 newline_node(node_assign(assignable(var,0), 09415 NEW_CALL( 09416 gettable(rb_intern("$~")), 09417 idAREF, 09418 NEW_LIST(NEW_LIT(ID2SYM(var)))) 09419 ))); 09420 arg->fail_block = block_append(arg->fail_block, 09421 newline_node(node_assign(assignable(var,0), NEW_LIT(Qnil)))); 09422 return ST_CONTINUE; 09423 } 09424 09425 static NODE * 09426 reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match) 09427 { 09428 reg_named_capture_assign_t arg; 09429 09430 arg.parser = parser; 09431 arg.enc = rb_enc_get(regexp); 09432 arg.succ_block = 0; 09433 arg.fail_block = 0; 09434 arg.num = 0; 09435 onig_foreach_name(RREGEXP(regexp)->ptr, reg_named_capture_assign_iter, (void*)&arg); 09436 09437 if (arg.num == 0) 09438 return match; 09439 09440 return 09441 block_append( 09442 newline_node(match), 09443 NEW_IF(gettable(rb_intern("$~")), 09444 block_append( 09445 newline_node(arg.succ_block), 09446 newline_node( 09447 NEW_CALL( 09448 gettable(rb_intern("$~")), 09449 rb_intern("begin"), 09450 NEW_LIST(NEW_LIT(INT2FIX(0)))))), 09451 block_append( 09452 newline_node(arg.fail_block), 09453 newline_node( 09454 NEW_LIT(Qnil))))); 09455 } 09456 09457 static VALUE 09458 reg_compile_gen(struct parser_params* parser, VALUE str, int options) 09459 { 09460 VALUE re; 09461 VALUE err; 09462 09463 reg_fragment_setenc(str, options); 09464 err = rb_errinfo(); 09465 re = rb_reg_compile(str, options & RE_OPTION_MASK, ruby_sourcefile, ruby_sourceline); 09466 if (NIL_P(re)) { 09467 ID mesg = rb_intern("mesg"); 09468 VALUE m = rb_attr_get(rb_errinfo(), mesg); 09469 rb_set_errinfo(err); 09470 if (!NIL_P(err)) { 09471 rb_str_append(rb_str_cat(rb_attr_get(err, mesg), "\n", 1), m); 09472 } 09473 else { 09474 compile_error(PARSER_ARG "%s", RSTRING_PTR(m)); 09475 } 09476 return Qnil; 09477 } 09478 return re; 09479 } 09480 09481 void 09482 rb_gc_mark_parser(void) 09483 { 09484 } 09485 09486 NODE* 09487 rb_parser_append_print(VALUE vparser, NODE *node) 09488 { 09489 NODE *prelude = 0; 09490 NODE *scope = node; 09491 struct parser_params *parser; 09492 09493 if (!node) return node; 09494 09495 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); 09496 09497 node = node->nd_body; 09498 09499 if (nd_type(node) == NODE_PRELUDE) { 09500 prelude = node; 09501 node = node->nd_body; 09502 } 09503 09504 node = block_append(node, 09505 NEW_FCALL(rb_intern("print"), 09506 NEW_ARRAY(NEW_GVAR(rb_intern("$_"))))); 09507 if (prelude) { 09508 prelude->nd_body = node; 09509 scope->nd_body = prelude; 09510 } 09511 else { 09512 scope->nd_body = node; 09513 } 09514 09515 return scope; 09516 } 09517 09518 NODE * 09519 rb_parser_while_loop(VALUE vparser, NODE *node, int chop, int split) 09520 { 09521 NODE *prelude = 0; 09522 NODE *scope = node; 09523 struct parser_params *parser; 09524 09525 if (!node) return node; 09526 09527 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); 09528 09529 node = node->nd_body; 09530 09531 if (nd_type(node) == NODE_PRELUDE) { 09532 prelude = node; 09533 node = node->nd_body; 09534 } 09535 if (split) { 09536 node = block_append(NEW_GASGN(rb_intern("$F"), 09537 NEW_CALL(NEW_GVAR(rb_intern("$_")), 09538 rb_intern("split"), 0)), 09539 node); 09540 } 09541 if (chop) { 09542 node = block_append(NEW_CALL(NEW_GVAR(rb_intern("$_")), 09543 rb_intern("chop!"), 0), node); 09544 } 09545 09546 node = NEW_OPT_N(node); 09547 09548 if (prelude) { 09549 prelude->nd_body = node; 09550 scope->nd_body = prelude; 09551 } 09552 else { 09553 scope->nd_body = node; 09554 } 09555 09556 return scope; 09557 } 09558 09559 static const struct { 09560 ID token; 09561 const char *name; 09562 } op_tbl[] = { 09563 {tDOT2, ".."}, 09564 {tDOT3, "..."}, 09565 {'+', "+(binary)"}, 09566 {'-', "-(binary)"}, 09567 {tPOW, "**"}, 09568 {tUPLUS, "+@"}, 09569 {tUMINUS, "-@"}, 09570 {tCMP, "<=>"}, 09571 {tGEQ, ">="}, 09572 {tLEQ, "<="}, 09573 {tEQ, "=="}, 09574 {tEQQ, "==="}, 09575 {tNEQ, "!="}, 09576 {tMATCH, "=~"}, 09577 {tNMATCH, "!~"}, 09578 {tAREF, "[]"}, 09579 {tASET, "[]="}, 09580 {tLSHFT, "<<"}, 09581 {tRSHFT, ">>"}, 09582 {tCOLON2, "::"}, 09583 }; 09584 09585 #define op_tbl_count numberof(op_tbl) 09586 09587 #ifndef ENABLE_SELECTOR_NAMESPACE 09588 #define ENABLE_SELECTOR_NAMESPACE 0 09589 #endif 09590 09591 static struct symbols { 09592 ID last_id; 09593 st_table *sym_id; 09594 st_table *id_str; 09595 #if ENABLE_SELECTOR_NAMESPACE 09596 st_table *ivar2_id; 09597 st_table *id_ivar2; 09598 #endif 09599 VALUE op_sym[tLAST_TOKEN]; 09600 } global_symbols = {tLAST_ID}; 09601 09602 static const struct st_hash_type symhash = { 09603 rb_str_hash_cmp, 09604 rb_str_hash, 09605 }; 09606 09607 #if ENABLE_SELECTOR_NAMESPACE 09608 struct ivar2_key { 09609 ID id; 09610 VALUE klass; 09611 }; 09612 09613 static int 09614 ivar2_cmp(struct ivar2_key *key1, struct ivar2_key *key2) 09615 { 09616 if (key1->id == key2->id && key1->klass == key2->klass) { 09617 return 0; 09618 } 09619 return 1; 09620 } 09621 09622 static int 09623 ivar2_hash(struct ivar2_key *key) 09624 { 09625 return (key->id << 8) ^ (key->klass >> 2); 09626 } 09627 09628 static const struct st_hash_type ivar2_hash_type = { 09629 ivar2_cmp, 09630 ivar2_hash, 09631 }; 09632 #endif 09633 09634 void 09635 Init_sym(void) 09636 { 09637 global_symbols.sym_id = st_init_table_with_size(&symhash, 1000); 09638 global_symbols.id_str = st_init_numtable_with_size(1000); 09639 #if ENABLE_SELECTOR_NAMESPACE 09640 global_symbols.ivar2_id = st_init_table_with_size(&ivar2_hash_type, 1000); 09641 global_symbols.id_ivar2 = st_init_numtable_with_size(1000); 09642 #endif 09643 09644 Init_id(); 09645 } 09646 09647 void 09648 rb_gc_mark_symbols(void) 09649 { 09650 rb_mark_tbl(global_symbols.id_str); 09651 rb_gc_mark_locations(global_symbols.op_sym, 09652 global_symbols.op_sym + tLAST_TOKEN); 09653 } 09654 #endif /* !RIPPER */ 09655 09656 static ID 09657 internal_id_gen(struct parser_params *parser) 09658 { 09659 ID id = (ID)vtable_size(lvtbl->args) + (ID)vtable_size(lvtbl->vars); 09660 id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1; 09661 return ID_INTERNAL | (id << ID_SCOPE_SHIFT); 09662 } 09663 09664 #ifndef RIPPER 09665 static int 09666 is_special_global_name(const char *m, const char *e, rb_encoding *enc) 09667 { 09668 int mb = 0; 09669 09670 if (m >= e) return 0; 09671 switch (*m) { 09672 case '~': case '*': case '$': case '?': case '!': case '@': 09673 case '/': case '\\': case ';': case ',': case '.': case '=': 09674 case ':': case '<': case '>': case '\"': 09675 case '&': case '`': case '\'': case '+': 09676 case '0': 09677 ++m; 09678 break; 09679 case '-': 09680 ++m; 09681 if (m < e && is_identchar(m, e, enc)) { 09682 if (!ISASCII(*m)) mb = 1; 09683 m += rb_enc_mbclen(m, e, enc); 09684 } 09685 break; 09686 default: 09687 if (!rb_enc_isdigit(*m, enc)) return 0; 09688 do { 09689 if (!ISASCII(*m)) mb = 1; 09690 ++m; 09691 } while (m < e && rb_enc_isdigit(*m, enc)); 09692 } 09693 return m == e ? mb + 1 : 0; 09694 } 09695 09696 int 09697 rb_symname_p(const char *name) 09698 { 09699 return rb_enc_symname_p(name, rb_ascii8bit_encoding()); 09700 } 09701 09702 int 09703 rb_enc_symname_p(const char *name, rb_encoding *enc) 09704 { 09705 return rb_enc_symname2_p(name, strlen(name), enc); 09706 } 09707 09708 int 09709 rb_enc_symname2_p(const char *name, long len, rb_encoding *enc) 09710 { 09711 const char *m = name; 09712 const char *e = m + len; 09713 int localid = FALSE; 09714 09715 if (!m || len <= 0) return FALSE; 09716 switch (*m) { 09717 case '\0': 09718 return FALSE; 09719 09720 case '$': 09721 if (is_special_global_name(++m, e, enc)) return TRUE; 09722 goto id; 09723 09724 case '@': 09725 if (*++m == '@') ++m; 09726 goto id; 09727 09728 case '<': 09729 switch (*++m) { 09730 case '<': ++m; break; 09731 case '=': if (*++m == '>') ++m; break; 09732 default: break; 09733 } 09734 break; 09735 09736 case '>': 09737 switch (*++m) { 09738 case '>': case '=': ++m; break; 09739 } 09740 break; 09741 09742 case '=': 09743 switch (*++m) { 09744 case '~': ++m; break; 09745 case '=': if (*++m == '=') ++m; break; 09746 default: return FALSE; 09747 } 09748 break; 09749 09750 case '*': 09751 if (*++m == '*') ++m; 09752 break; 09753 09754 case '+': case '-': 09755 if (*++m == '@') ++m; 09756 break; 09757 09758 case '|': case '^': case '&': case '/': case '%': case '~': case '`': 09759 ++m; 09760 break; 09761 09762 case '[': 09763 if (*++m != ']') return FALSE; 09764 if (*++m == '=') ++m; 09765 break; 09766 09767 case '!': 09768 if (len == 1) return TRUE; 09769 switch (*++m) { 09770 case '=': case '~': ++m; break; 09771 default: return FALSE; 09772 } 09773 break; 09774 09775 default: 09776 localid = !rb_enc_isupper(*m, enc); 09777 id: 09778 if (m >= e || (*m != '_' && !rb_enc_isalpha(*m, enc) && ISASCII(*m))) 09779 return FALSE; 09780 while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc); 09781 if (localid) { 09782 switch (*m) { 09783 case '!': case '?': case '=': ++m; 09784 } 09785 } 09786 break; 09787 } 09788 return m == e; 09789 } 09790 09791 static ID 09792 register_symid(ID id, const char *name, long len, rb_encoding *enc) 09793 { 09794 VALUE str = rb_enc_str_new(name, len, enc); 09795 OBJ_FREEZE(str); 09796 st_add_direct(global_symbols.sym_id, (st_data_t)str, id); 09797 st_add_direct(global_symbols.id_str, id, (st_data_t)str); 09798 return id; 09799 } 09800 09801 ID 09802 rb_intern3(const char *name, long len, rb_encoding *enc) 09803 { 09804 const char *m = name; 09805 const char *e = m + len; 09806 unsigned char c; 09807 VALUE str; 09808 ID id; 09809 long last; 09810 int mb; 09811 st_data_t data; 09812 struct RString fake_str; 09813 fake_str.basic.flags = T_STRING|RSTRING_NOEMBED; 09814 fake_str.basic.klass = rb_cString; 09815 fake_str.as.heap.len = len; 09816 fake_str.as.heap.ptr = (char *)name; 09817 fake_str.as.heap.aux.capa = len; 09818 str = (VALUE)&fake_str; 09819 rb_enc_associate(str, enc); 09820 OBJ_FREEZE(str); 09821 09822 if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) { 09823 rb_raise(rb_eEncodingError, "invalid encoding symbol"); 09824 } 09825 09826 if (st_lookup(global_symbols.sym_id, str, &data)) 09827 return (ID)data; 09828 09829 if (rb_cString && !rb_enc_asciicompat(enc)) { 09830 id = ID_JUNK; 09831 goto new_id; 09832 } 09833 last = len-1; 09834 id = 0; 09835 switch (*m) { 09836 case '$': 09837 id |= ID_GLOBAL; 09838 if ((mb = is_special_global_name(++m, e, enc)) != 0) { 09839 if (!--mb) enc = rb_ascii8bit_encoding(); 09840 goto new_id; 09841 } 09842 break; 09843 case '@': 09844 if (m[1] == '@') { 09845 m++; 09846 id |= ID_CLASS; 09847 } 09848 else { 09849 id |= ID_INSTANCE; 09850 } 09851 m++; 09852 break; 09853 default: 09854 c = m[0]; 09855 if (c != '_' && rb_enc_isascii(c, enc) && rb_enc_ispunct(c, enc)) { 09856 /* operators */ 09857 int i; 09858 09859 if (len == 1) { 09860 id = c; 09861 goto id_register; 09862 } 09863 for (i = 0; i < op_tbl_count; i++) { 09864 if (*op_tbl[i].name == *m && 09865 strcmp(op_tbl[i].name, m) == 0) { 09866 id = op_tbl[i].token; 09867 goto id_register; 09868 } 09869 } 09870 } 09871 09872 if (m[last] == '=') { 09873 /* attribute assignment */ 09874 id = rb_intern3(name, last, enc); 09875 if (id > tLAST_TOKEN && !is_attrset_id(id)) { 09876 enc = rb_enc_get(rb_id2str(id)); 09877 id = rb_id_attrset(id); 09878 goto id_register; 09879 } 09880 id = ID_ATTRSET; 09881 } 09882 else if (rb_enc_isupper(m[0], enc)) { 09883 id = ID_CONST; 09884 } 09885 else { 09886 id = ID_LOCAL; 09887 } 09888 break; 09889 } 09890 mb = 0; 09891 if (!rb_enc_isdigit(*m, enc)) { 09892 while (m <= name + last && is_identchar(m, e, enc)) { 09893 if (ISASCII(*m)) { 09894 m++; 09895 } 09896 else { 09897 mb = 1; 09898 m += rb_enc_mbclen(m, e, enc); 09899 } 09900 } 09901 } 09902 if (m - name < len) id = ID_JUNK; 09903 if (enc != rb_usascii_encoding()) { 09904 /* 09905 * this clause makes sense only when called from other than 09906 * rb_intern_str() taking care of code-range. 09907 */ 09908 if (!mb) { 09909 for (; m <= name + len; ++m) { 09910 if (!ISASCII(*m)) goto mbstr; 09911 } 09912 enc = rb_usascii_encoding(); 09913 } 09914 mbstr:; 09915 } 09916 new_id: 09917 if (global_symbols.last_id >= ~(ID)0 >> (ID_SCOPE_SHIFT+RUBY_SPECIAL_SHIFT)) { 09918 if (len > 20) { 09919 rb_raise(rb_eRuntimeError, "symbol table overflow (symbol %.20s...)", 09920 name); 09921 } 09922 else { 09923 rb_raise(rb_eRuntimeError, "symbol table overflow (symbol %.*s)", 09924 (int)len, name); 09925 } 09926 } 09927 id |= ++global_symbols.last_id << ID_SCOPE_SHIFT; 09928 id_register: 09929 return register_symid(id, name, len, enc); 09930 } 09931 09932 ID 09933 rb_intern2(const char *name, long len) 09934 { 09935 return rb_intern3(name, len, rb_usascii_encoding()); 09936 } 09937 09938 #undef rb_intern 09939 ID 09940 rb_intern(const char *name) 09941 { 09942 return rb_intern2(name, strlen(name)); 09943 } 09944 09945 ID 09946 rb_intern_str(VALUE str) 09947 { 09948 rb_encoding *enc; 09949 ID id; 09950 09951 if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) { 09952 enc = rb_usascii_encoding(); 09953 } 09954 else { 09955 enc = rb_enc_get(str); 09956 } 09957 id = rb_intern3(RSTRING_PTR(str), RSTRING_LEN(str), enc); 09958 RB_GC_GUARD(str); 09959 return id; 09960 } 09961 09962 VALUE 09963 rb_id2str(ID id) 09964 { 09965 st_data_t data; 09966 09967 if (id < tLAST_TOKEN) { 09968 int i = 0; 09969 09970 if (id < INT_MAX && rb_ispunct((int)id)) { 09971 VALUE str = global_symbols.op_sym[i = (int)id]; 09972 if (!str) { 09973 char name[2]; 09974 name[0] = (char)id; 09975 name[1] = 0; 09976 str = rb_usascii_str_new(name, 1); 09977 OBJ_FREEZE(str); 09978 global_symbols.op_sym[i] = str; 09979 } 09980 return str; 09981 } 09982 for (i = 0; i < op_tbl_count; i++) { 09983 if (op_tbl[i].token == id) { 09984 VALUE str = global_symbols.op_sym[i]; 09985 if (!str) { 09986 str = rb_usascii_str_new2(op_tbl[i].name); 09987 OBJ_FREEZE(str); 09988 global_symbols.op_sym[i] = str; 09989 } 09990 return str; 09991 } 09992 } 09993 } 09994 09995 if (st_lookup(global_symbols.id_str, id, &data)) { 09996 VALUE str = (VALUE)data; 09997 if (RBASIC(str)->klass == 0) 09998 RBASIC(str)->klass = rb_cString; 09999 return str; 10000 } 10001 10002 if (is_attrset_id(id)) { 10003 ID id2 = (id & ~ID_SCOPE_MASK) | ID_LOCAL; 10004 VALUE str; 10005 10006 while (!(str = rb_id2str(id2))) { 10007 if (!is_local_id(id2)) return 0; 10008 id2 = (id & ~ID_SCOPE_MASK) | ID_CONST; 10009 } 10010 str = rb_str_dup(str); 10011 rb_str_cat(str, "=", 1); 10012 rb_intern_str(str); 10013 if (st_lookup(global_symbols.id_str, id, &data)) { 10014 VALUE str = (VALUE)data; 10015 if (RBASIC(str)->klass == 0) 10016 RBASIC(str)->klass = rb_cString; 10017 return str; 10018 } 10019 } 10020 return 0; 10021 } 10022 10023 const char * 10024 rb_id2name(ID id) 10025 { 10026 VALUE str = rb_id2str(id); 10027 10028 if (!str) return 0; 10029 return RSTRING_PTR(str); 10030 } 10031 10032 static int 10033 symbols_i(VALUE sym, ID value, VALUE ary) 10034 { 10035 rb_ary_push(ary, ID2SYM(value)); 10036 return ST_CONTINUE; 10037 } 10038 10039 /* 10040 * call-seq: 10041 * Symbol.all_symbols => array 10042 * 10043 * Returns an array of all the symbols currently in Ruby's symbol 10044 * table. 10045 * 10046 * Symbol.all_symbols.size #=> 903 10047 * Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink, 10048 * :chown, :EOFError, :$;, :String, 10049 * :LOCK_SH, :"setuid?", :$<, 10050 * :default_proc, :compact, :extend, 10051 * :Tms, :getwd, :$=, :ThreadGroup, 10052 * :wait2, :$>] 10053 */ 10054 10055 VALUE 10056 rb_sym_all_symbols(void) 10057 { 10058 VALUE ary = rb_ary_new2(global_symbols.sym_id->num_entries); 10059 10060 st_foreach(global_symbols.sym_id, symbols_i, ary); 10061 return ary; 10062 } 10063 10064 int 10065 rb_is_const_id(ID id) 10066 { 10067 return is_const_id(id); 10068 } 10069 10070 int 10071 rb_is_class_id(ID id) 10072 { 10073 return is_class_id(id); 10074 } 10075 10076 int 10077 rb_is_instance_id(ID id) 10078 { 10079 return is_instance_id(id); 10080 } 10081 10082 int 10083 rb_is_local_id(ID id) 10084 { 10085 return is_local_id(id); 10086 } 10087 10088 int 10089 rb_is_junk_id(ID id) 10090 { 10091 return is_junk_id(id); 10092 } 10093 10094 #endif /* !RIPPER */ 10095 10096 static void 10097 parser_initialize(struct parser_params *parser) 10098 { 10099 parser->eofp = Qfalse; 10100 10101 parser->parser_lex_strterm = 0; 10102 parser->parser_cond_stack = 0; 10103 parser->parser_cmdarg_stack = 0; 10104 parser->parser_class_nest = 0; 10105 parser->parser_paren_nest = 0; 10106 parser->parser_lpar_beg = 0; 10107 parser->parser_in_single = 0; 10108 parser->parser_in_def = 0; 10109 parser->parser_in_defined = 0; 10110 parser->parser_compile_for_eval = 0; 10111 parser->parser_cur_mid = 0; 10112 parser->parser_tokenbuf = NULL; 10113 parser->parser_tokidx = 0; 10114 parser->parser_toksiz = 0; 10115 parser->parser_heredoc_end = 0; 10116 parser->parser_command_start = TRUE; 10117 parser->parser_deferred_nodes = 0; 10118 parser->parser_lex_pbeg = 0; 10119 parser->parser_lex_p = 0; 10120 parser->parser_lex_pend = 0; 10121 parser->parser_lvtbl = 0; 10122 parser->parser_ruby__end__seen = 0; 10123 parser->parser_ruby_sourcefile = 0; 10124 #ifndef RIPPER 10125 parser->is_ripper = 0; 10126 parser->parser_eval_tree_begin = 0; 10127 parser->parser_eval_tree = 0; 10128 #else 10129 parser->is_ripper = 1; 10130 parser->parser_ruby_sourcefile_string = Qnil; 10131 parser->delayed = Qnil; 10132 10133 parser->result = Qnil; 10134 parser->parsing_thread = Qnil; 10135 parser->toplevel_p = TRUE; 10136 #endif 10137 #ifdef YYMALLOC 10138 parser->heap = NULL; 10139 #endif 10140 parser->enc = rb_usascii_encoding(); 10141 } 10142 10143 #ifdef RIPPER 10144 #define parser_mark ripper_parser_mark 10145 #define parser_free ripper_parser_free 10146 #endif 10147 10148 static void 10149 parser_mark(void *ptr) 10150 { 10151 struct parser_params *p = (struct parser_params*)ptr; 10152 10153 rb_gc_mark((VALUE)p->parser_lex_strterm); 10154 rb_gc_mark((VALUE)p->parser_deferred_nodes); 10155 rb_gc_mark(p->parser_lex_input); 10156 rb_gc_mark(p->parser_lex_lastline); 10157 rb_gc_mark(p->parser_lex_nextline); 10158 #ifndef RIPPER 10159 rb_gc_mark((VALUE)p->parser_eval_tree_begin) ; 10160 rb_gc_mark((VALUE)p->parser_eval_tree) ; 10161 rb_gc_mark(p->debug_lines); 10162 #else 10163 rb_gc_mark(p->parser_ruby_sourcefile_string); 10164 rb_gc_mark(p->delayed); 10165 rb_gc_mark(p->value); 10166 rb_gc_mark(p->result); 10167 rb_gc_mark(p->parsing_thread); 10168 #endif 10169 #ifdef YYMALLOC 10170 rb_gc_mark((VALUE)p->heap); 10171 #endif 10172 } 10173 10174 static void 10175 parser_free(void *ptr) 10176 { 10177 struct parser_params *p = (struct parser_params*)ptr; 10178 struct local_vars *local, *prev; 10179 10180 if (p->parser_tokenbuf) { 10181 xfree(p->parser_tokenbuf); 10182 } 10183 for (local = p->parser_lvtbl; local; local = prev) { 10184 if (local->vars) xfree(local->vars); 10185 prev = local->prev; 10186 xfree(local); 10187 } 10188 #ifndef RIPPER 10189 xfree(p->parser_ruby_sourcefile); 10190 #endif 10191 xfree(p); 10192 } 10193 10194 static size_t 10195 parser_memsize(const void *ptr) 10196 { 10197 struct parser_params *p = (struct parser_params*)ptr; 10198 struct local_vars *local; 10199 size_t size = sizeof(*p); 10200 10201 if (!ptr) return 0; 10202 size += p->parser_toksiz; 10203 for (local = p->parser_lvtbl; local; local = local->prev) { 10204 size += sizeof(*local); 10205 if (local->vars) size += local->vars->capa * sizeof(ID); 10206 } 10207 #ifndef RIPPER 10208 if (p->parser_ruby_sourcefile) { 10209 size += strlen(p->parser_ruby_sourcefile) + 1; 10210 } 10211 #endif 10212 return size; 10213 } 10214 10215 static 10216 #ifndef RIPPER 10217 const 10218 #endif 10219 rb_data_type_t parser_data_type = { 10220 "parser", 10221 { 10222 parser_mark, 10223 parser_free, 10224 parser_memsize, 10225 }, 10226 }; 10227 10228 #ifndef RIPPER 10229 #undef rb_reserved_word 10230 10231 const struct kwtable * 10232 rb_reserved_word(const char *str, unsigned int len) 10233 { 10234 return reserved_word(str, len); 10235 } 10236 10237 static struct parser_params * 10238 parser_new(void) 10239 { 10240 struct parser_params *p; 10241 10242 p = ALLOC_N(struct parser_params, 1); 10243 MEMZERO(p, struct parser_params, 1); 10244 parser_initialize(p); 10245 return p; 10246 } 10247 10248 VALUE 10249 rb_parser_new(void) 10250 { 10251 struct parser_params *p = parser_new(); 10252 10253 return TypedData_Wrap_Struct(0, &parser_data_type, p); 10254 } 10255 10256 /* 10257 * call-seq: 10258 * ripper#end_seen? -> Boolean 10259 * 10260 * Return true if parsed source ended by +\_\_END\_\_+. 10261 */ 10262 VALUE 10263 rb_parser_end_seen_p(VALUE vparser) 10264 { 10265 struct parser_params *parser; 10266 10267 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); 10268 return ruby__end__seen ? Qtrue : Qfalse; 10269 } 10270 10271 /* 10272 * call-seq: 10273 * ripper#encoding -> encoding 10274 * 10275 * Return encoding of the source. 10276 */ 10277 VALUE 10278 rb_parser_encoding(VALUE vparser) 10279 { 10280 struct parser_params *parser; 10281 10282 TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, parser); 10283 return rb_enc_from_encoding(parser->enc); 10284 } 10285 10286 /* 10287 * call-seq: 10288 * ripper.yydebug -> true or false 10289 * 10290 * Get yydebug. 10291 */ 10292 VALUE 10293 rb_parser_get_yydebug(VALUE self) 10294 { 10295 struct parser_params *parser; 10296 10297 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10298 return yydebug ? Qtrue : Qfalse; 10299 } 10300 10301 /* 10302 * call-seq: 10303 * ripper.yydebug = flag 10304 * 10305 * Set yydebug. 10306 */ 10307 VALUE 10308 rb_parser_set_yydebug(VALUE self, VALUE flag) 10309 { 10310 struct parser_params *parser; 10311 10312 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10313 yydebug = RTEST(flag); 10314 return flag; 10315 } 10316 10317 #ifdef YYMALLOC 10318 #define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE)) 10319 #define NEWHEAP() rb_node_newnode(NODE_ALLOCA, 0, (VALUE)parser->heap, 0) 10320 #define ADD2HEAP(n, c, p) ((parser->heap = (n))->u1.node = (p), \ 10321 (n)->u3.cnt = (c), (p)) 10322 10323 void * 10324 rb_parser_malloc(struct parser_params *parser, size_t size) 10325 { 10326 size_t cnt = HEAPCNT(1, size); 10327 NODE *n = NEWHEAP(); 10328 void *ptr = xmalloc(size); 10329 10330 return ADD2HEAP(n, cnt, ptr); 10331 } 10332 10333 void * 10334 rb_parser_calloc(struct parser_params *parser, size_t nelem, size_t size) 10335 { 10336 size_t cnt = HEAPCNT(nelem, size); 10337 NODE *n = NEWHEAP(); 10338 void *ptr = xcalloc(nelem, size); 10339 10340 return ADD2HEAP(n, cnt, ptr); 10341 } 10342 10343 void * 10344 rb_parser_realloc(struct parser_params *parser, void *ptr, size_t size) 10345 { 10346 NODE *n; 10347 size_t cnt = HEAPCNT(1, size); 10348 10349 if (ptr && (n = parser->heap) != NULL) { 10350 do { 10351 if (n->u1.node == ptr) { 10352 n->u1.node = ptr = xrealloc(ptr, size); 10353 if (n->u3.cnt) n->u3.cnt = cnt; 10354 return ptr; 10355 } 10356 } while ((n = n->u2.node) != NULL); 10357 } 10358 n = NEWHEAP(); 10359 ptr = xrealloc(ptr, size); 10360 return ADD2HEAP(n, cnt, ptr); 10361 } 10362 10363 void 10364 rb_parser_free(struct parser_params *parser, void *ptr) 10365 { 10366 NODE **prev = &parser->heap, *n; 10367 10368 while ((n = *prev) != NULL) { 10369 if (n->u1.node == ptr) { 10370 *prev = n->u2.node; 10371 rb_gc_force_recycle((VALUE)n); 10372 break; 10373 } 10374 prev = &n->u2.node; 10375 } 10376 xfree(ptr); 10377 } 10378 #endif 10379 #endif 10380 10381 #ifdef RIPPER 10382 #ifdef RIPPER_DEBUG 10383 extern int rb_is_pointer_to_heap(VALUE); 10384 10385 /* :nodoc: */ 10386 static VALUE 10387 ripper_validate_object(VALUE self, VALUE x) 10388 { 10389 if (x == Qfalse) return x; 10390 if (x == Qtrue) return x; 10391 if (x == Qnil) return x; 10392 if (x == Qundef) 10393 rb_raise(rb_eArgError, "Qundef given"); 10394 if (FIXNUM_P(x)) return x; 10395 if (SYMBOL_P(x)) return x; 10396 if (!rb_is_pointer_to_heap(x)) 10397 rb_raise(rb_eArgError, "invalid pointer: %p", x); 10398 switch (TYPE(x)) { 10399 case T_STRING: 10400 case T_OBJECT: 10401 case T_ARRAY: 10402 case T_BIGNUM: 10403 case T_FLOAT: 10404 return x; 10405 case T_NODE: 10406 if (nd_type(x) != NODE_LASGN) { 10407 rb_raise(rb_eArgError, "NODE given: %p", x); 10408 } 10409 return ((NODE *)x)->nd_rval; 10410 default: 10411 rb_raise(rb_eArgError, "wrong type of ruby object: %p (%s)", 10412 x, rb_obj_classname(x)); 10413 } 10414 return x; 10415 } 10416 #endif 10417 10418 #define validate(x) ((x) = get_value(x)) 10419 10420 static VALUE 10421 ripper_dispatch0(struct parser_params *parser, ID mid) 10422 { 10423 return rb_funcall(parser->value, mid, 0); 10424 } 10425 10426 static VALUE 10427 ripper_dispatch1(struct parser_params *parser, ID mid, VALUE a) 10428 { 10429 validate(a); 10430 return rb_funcall(parser->value, mid, 1, a); 10431 } 10432 10433 static VALUE 10434 ripper_dispatch2(struct parser_params *parser, ID mid, VALUE a, VALUE b) 10435 { 10436 validate(a); 10437 validate(b); 10438 return rb_funcall(parser->value, mid, 2, a, b); 10439 } 10440 10441 static VALUE 10442 ripper_dispatch3(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c) 10443 { 10444 validate(a); 10445 validate(b); 10446 validate(c); 10447 return rb_funcall(parser->value, mid, 3, a, b, c); 10448 } 10449 10450 static VALUE 10451 ripper_dispatch4(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d) 10452 { 10453 validate(a); 10454 validate(b); 10455 validate(c); 10456 validate(d); 10457 return rb_funcall(parser->value, mid, 4, a, b, c, d); 10458 } 10459 10460 static VALUE 10461 ripper_dispatch5(struct parser_params *parser, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e) 10462 { 10463 validate(a); 10464 validate(b); 10465 validate(c); 10466 validate(d); 10467 validate(e); 10468 return rb_funcall(parser->value, mid, 5, a, b, c, d, e); 10469 } 10470 10471 static const struct kw_assoc { 10472 ID id; 10473 const char *name; 10474 } keyword_to_name[] = { 10475 {keyword_class, "class"}, 10476 {keyword_module, "module"}, 10477 {keyword_def, "def"}, 10478 {keyword_undef, "undef"}, 10479 {keyword_begin, "begin"}, 10480 {keyword_rescue, "rescue"}, 10481 {keyword_ensure, "ensure"}, 10482 {keyword_end, "end"}, 10483 {keyword_if, "if"}, 10484 {keyword_unless, "unless"}, 10485 {keyword_then, "then"}, 10486 {keyword_elsif, "elsif"}, 10487 {keyword_else, "else"}, 10488 {keyword_case, "case"}, 10489 {keyword_when, "when"}, 10490 {keyword_while, "while"}, 10491 {keyword_until, "until"}, 10492 {keyword_for, "for"}, 10493 {keyword_break, "break"}, 10494 {keyword_next, "next"}, 10495 {keyword_redo, "redo"}, 10496 {keyword_retry, "retry"}, 10497 {keyword_in, "in"}, 10498 {keyword_do, "do"}, 10499 {keyword_do_cond, "do"}, 10500 {keyword_do_block, "do"}, 10501 {keyword_return, "return"}, 10502 {keyword_yield, "yield"}, 10503 {keyword_super, "super"}, 10504 {keyword_self, "self"}, 10505 {keyword_nil, "nil"}, 10506 {keyword_true, "true"}, 10507 {keyword_false, "false"}, 10508 {keyword_and, "and"}, 10509 {keyword_or, "or"}, 10510 {keyword_not, "not"}, 10511 {modifier_if, "if"}, 10512 {modifier_unless, "unless"}, 10513 {modifier_while, "while"}, 10514 {modifier_until, "until"}, 10515 {modifier_rescue, "rescue"}, 10516 {keyword_alias, "alias"}, 10517 {keyword_defined, "defined?"}, 10518 {keyword_BEGIN, "BEGIN"}, 10519 {keyword_END, "END"}, 10520 {keyword__LINE__, "__LINE__"}, 10521 {keyword__FILE__, "__FILE__"}, 10522 {keyword__ENCODING__, "__ENCODING__"}, 10523 {0, NULL} 10524 }; 10525 10526 static const char* 10527 keyword_id_to_str(ID id) 10528 { 10529 const struct kw_assoc *a; 10530 10531 for (a = keyword_to_name; a->id; a++) { 10532 if (a->id == id) 10533 return a->name; 10534 } 10535 return NULL; 10536 } 10537 10538 #undef ripper_id2sym 10539 static VALUE 10540 ripper_id2sym(ID id) 10541 { 10542 const char *name; 10543 char buf[8]; 10544 10545 if (id <= 256) { 10546 buf[0] = (char)id; 10547 buf[1] = '\0'; 10548 return ID2SYM(rb_intern2(buf, 1)); 10549 } 10550 if ((name = keyword_id_to_str(id))) { 10551 return ID2SYM(rb_intern(name)); 10552 } 10553 switch (id) { 10554 case tOROP: 10555 name = "||"; 10556 break; 10557 case tANDOP: 10558 name = "&&"; 10559 break; 10560 default: 10561 name = rb_id2name(id); 10562 if (!name) { 10563 rb_bug("cannot convert ID to string: %ld", (unsigned long)id); 10564 } 10565 return ID2SYM(id); 10566 } 10567 return ID2SYM(rb_intern(name)); 10568 } 10569 10570 static ID 10571 ripper_get_id(VALUE v) 10572 { 10573 NODE *nd; 10574 if (!RB_TYPE_P(v, T_NODE)) return 0; 10575 nd = (NODE *)v; 10576 if (nd_type(nd) != NODE_LASGN) return 0; 10577 return nd->nd_vid; 10578 } 10579 10580 static VALUE 10581 ripper_get_value(VALUE v) 10582 { 10583 NODE *nd; 10584 if (v == Qundef) return Qnil; 10585 if (!RB_TYPE_P(v, T_NODE)) return v; 10586 nd = (NODE *)v; 10587 if (nd_type(nd) != NODE_LASGN) return Qnil; 10588 return nd->nd_rval; 10589 } 10590 10591 static void 10592 ripper_compile_error(struct parser_params *parser, const char *fmt, ...) 10593 { 10594 VALUE str; 10595 va_list args; 10596 10597 va_start(args, fmt); 10598 str = rb_vsprintf(fmt, args); 10599 va_end(args); 10600 rb_funcall(parser->value, rb_intern("compile_error"), 1, str); 10601 } 10602 10603 static void 10604 ripper_warn0(struct parser_params *parser, const char *fmt) 10605 { 10606 rb_funcall(parser->value, rb_intern("warn"), 1, STR_NEW2(fmt)); 10607 } 10608 10609 static void 10610 ripper_warnI(struct parser_params *parser, const char *fmt, int a) 10611 { 10612 rb_funcall(parser->value, rb_intern("warn"), 2, 10613 STR_NEW2(fmt), INT2NUM(a)); 10614 } 10615 10616 #if 0 10617 static void 10618 ripper_warnS(struct parser_params *parser, const char *fmt, const char *str) 10619 { 10620 rb_funcall(parser->value, rb_intern("warn"), 2, 10621 STR_NEW2(fmt), STR_NEW2(str)); 10622 } 10623 #endif 10624 10625 static void 10626 ripper_warning0(struct parser_params *parser, const char *fmt) 10627 { 10628 rb_funcall(parser->value, rb_intern("warning"), 1, STR_NEW2(fmt)); 10629 } 10630 10631 static void 10632 ripper_warningS(struct parser_params *parser, const char *fmt, const char *str) 10633 { 10634 rb_funcall(parser->value, rb_intern("warning"), 2, 10635 STR_NEW2(fmt), STR_NEW2(str)); 10636 } 10637 10638 static VALUE 10639 ripper_lex_get_generic(struct parser_params *parser, VALUE src) 10640 { 10641 return rb_funcall(src, ripper_id_gets, 0); 10642 } 10643 10644 static VALUE 10645 ripper_s_allocate(VALUE klass) 10646 { 10647 struct parser_params *p; 10648 VALUE self; 10649 10650 p = ALLOC_N(struct parser_params, 1); 10651 MEMZERO(p, struct parser_params, 1); 10652 self = TypedData_Wrap_Struct(klass, &parser_data_type, p); 10653 p->value = self; 10654 return self; 10655 } 10656 10657 #define ripper_initialized_p(r) ((r)->parser_lex_input != 0) 10658 10659 /* 10660 * call-seq: 10661 * Ripper.new(src, filename="(ripper)", lineno=1) -> ripper 10662 * 10663 * Create a new Ripper object. 10664 * _src_ must be a String, an IO, or an Object which has #gets method. 10665 * 10666 * This method does not starts parsing. 10667 * See also Ripper#parse and Ripper.parse. 10668 */ 10669 static VALUE 10670 ripper_initialize(int argc, VALUE *argv, VALUE self) 10671 { 10672 struct parser_params *parser; 10673 VALUE src, fname, lineno; 10674 10675 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10676 rb_scan_args(argc, argv, "12", &src, &fname, &lineno); 10677 if (rb_obj_respond_to(src, ripper_id_gets, 0)) { 10678 parser->parser_lex_gets = ripper_lex_get_generic; 10679 } 10680 else { 10681 StringValue(src); 10682 parser->parser_lex_gets = lex_get_str; 10683 } 10684 parser->parser_lex_input = src; 10685 parser->eofp = Qfalse; 10686 if (NIL_P(fname)) { 10687 fname = STR_NEW2("(ripper)"); 10688 } 10689 else { 10690 StringValue(fname); 10691 } 10692 parser_initialize(parser); 10693 10694 parser->parser_ruby_sourcefile_string = fname; 10695 parser->parser_ruby_sourcefile = RSTRING_PTR(fname); 10696 parser->parser_ruby_sourceline = NIL_P(lineno) ? 0 : NUM2INT(lineno) - 1; 10697 10698 return Qnil; 10699 } 10700 10701 struct ripper_args { 10702 struct parser_params *parser; 10703 int argc; 10704 VALUE *argv; 10705 }; 10706 10707 static VALUE 10708 ripper_parse0(VALUE parser_v) 10709 { 10710 struct parser_params *parser; 10711 10712 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser); 10713 parser_prepare(parser); 10714 ripper_yyparse((void*)parser); 10715 return parser->result; 10716 } 10717 10718 static VALUE 10719 ripper_ensure(VALUE parser_v) 10720 { 10721 struct parser_params *parser; 10722 10723 TypedData_Get_Struct(parser_v, struct parser_params, &parser_data_type, parser); 10724 parser->parsing_thread = Qnil; 10725 return Qnil; 10726 } 10727 10728 /* 10729 * call-seq: 10730 * ripper#parse 10731 * 10732 * Start parsing and returns the value of the root action. 10733 */ 10734 static VALUE 10735 ripper_parse(VALUE self) 10736 { 10737 struct parser_params *parser; 10738 10739 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10740 if (!ripper_initialized_p(parser)) { 10741 rb_raise(rb_eArgError, "method called for uninitialized object"); 10742 } 10743 if (!NIL_P(parser->parsing_thread)) { 10744 if (parser->parsing_thread == rb_thread_current()) 10745 rb_raise(rb_eArgError, "Ripper#parse is not reentrant"); 10746 else 10747 rb_raise(rb_eArgError, "Ripper#parse is not multithread-safe"); 10748 } 10749 parser->parsing_thread = rb_thread_current(); 10750 rb_ensure(ripper_parse0, self, ripper_ensure, self); 10751 10752 return parser->result; 10753 } 10754 10755 /* 10756 * call-seq: 10757 * ripper#column -> Integer 10758 * 10759 * Return column number of current parsing line. 10760 * This number starts from 0. 10761 */ 10762 static VALUE 10763 ripper_column(VALUE self) 10764 { 10765 struct parser_params *parser; 10766 long col; 10767 10768 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10769 if (!ripper_initialized_p(parser)) { 10770 rb_raise(rb_eArgError, "method called for uninitialized object"); 10771 } 10772 if (NIL_P(parser->parsing_thread)) return Qnil; 10773 col = parser->tokp - parser->parser_lex_pbeg; 10774 return LONG2NUM(col); 10775 } 10776 10777 /* 10778 * call-seq: 10779 * ripper#filename -> String 10780 * 10781 * Return current parsing filename. 10782 */ 10783 static VALUE 10784 ripper_filename(VALUE self) 10785 { 10786 struct parser_params *parser; 10787 10788 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10789 if (!ripper_initialized_p(parser)) { 10790 rb_raise(rb_eArgError, "method called for uninitialized object"); 10791 } 10792 return parser->parser_ruby_sourcefile_string; 10793 } 10794 10795 /* 10796 * call-seq: 10797 * ripper#lineno -> Integer 10798 * 10799 * Return line number of current parsing line. 10800 * This number starts from 1. 10801 */ 10802 static VALUE 10803 ripper_lineno(VALUE self) 10804 { 10805 struct parser_params *parser; 10806 10807 TypedData_Get_Struct(self, struct parser_params, &parser_data_type, parser); 10808 if (!ripper_initialized_p(parser)) { 10809 rb_raise(rb_eArgError, "method called for uninitialized object"); 10810 } 10811 if (NIL_P(parser->parsing_thread)) return Qnil; 10812 return INT2NUM(parser->parser_ruby_sourceline); 10813 } 10814 10815 #ifdef RIPPER_DEBUG 10816 /* :nodoc: */ 10817 static VALUE 10818 ripper_assert_Qundef(VALUE self, VALUE obj, VALUE msg) 10819 { 10820 StringValue(msg); 10821 if (obj == Qundef) { 10822 rb_raise(rb_eArgError, "%s", RSTRING_PTR(msg)); 10823 } 10824 return Qnil; 10825 } 10826 10827 /* :nodoc: */ 10828 static VALUE 10829 ripper_value(VALUE self, VALUE obj) 10830 { 10831 return ULONG2NUM(obj); 10832 } 10833 #endif 10834 10835 10836 void 10837 InitVM_ripper(void) 10838 { 10839 parser_data_type.parent = RTYPEDDATA_TYPE(rb_parser_new()); 10840 } 10841 10842 void 10843 Init_ripper(void) 10844 { 10845 VALUE Ripper; 10846 10847 InitVM(ripper); 10848 Ripper = rb_define_class("Ripper", rb_cObject); 10849 rb_define_const(Ripper, "Version", rb_usascii_str_new2(RIPPER_VERSION)); 10850 rb_define_alloc_func(Ripper, ripper_s_allocate); 10851 rb_define_method(Ripper, "initialize", ripper_initialize, -1); 10852 rb_define_method(Ripper, "parse", ripper_parse, 0); 10853 rb_define_method(Ripper, "column", ripper_column, 0); 10854 rb_define_method(Ripper, "filename", ripper_filename, 0); 10855 rb_define_method(Ripper, "lineno", ripper_lineno, 0); 10856 rb_define_method(Ripper, "end_seen?", rb_parser_end_seen_p, 0); 10857 rb_define_method(Ripper, "encoding", rb_parser_encoding, 0); 10858 rb_define_method(Ripper, "yydebug", rb_parser_get_yydebug, 0); 10859 rb_define_method(Ripper, "yydebug=", rb_parser_set_yydebug, 1); 10860 #ifdef RIPPER_DEBUG 10861 rb_define_method(rb_mKernel, "assert_Qundef", ripper_assert_Qundef, 2); 10862 rb_define_method(rb_mKernel, "rawVALUE", ripper_value, 1); 10863 rb_define_method(rb_mKernel, "validate_object", ripper_validate_object, 1); 10864 #endif 10865 10866 ripper_id_gets = rb_intern("gets"); 10867 ripper_init_eventids1(Ripper); 10868 ripper_init_eventids2(Ripper); 10869 /* ensure existing in symbol table */ 10870 (void)rb_intern("||"); 10871 (void)rb_intern("&&"); 10872 10873 # if 0 10874 /* Hack to let RDoc document SCRIPT_LINES__ */ 10875 10876 /* 10877 * When a Hash is assigned to +SCRIPT_LINES__+ the contents of files loaded 10878 * after the assignment will be added as an Array of lines with the file 10879 * name as the key. 10880 */ 10881 rb_define_global_const("SCRIPT_LINES__", Qnil); 10882 #endif 10883 10884 } 10885 #endif /* RIPPER */ 10886
1.7.6.1