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