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