ruby-changes:34766
From: nobu <ko1@a...>
Date: Thu, 17 Jul 2014 13:31:07 +0900 (JST)
Subject: [ruby-changes:34766] nobu:r46850 (trunk): parse.y: separate from parse_ident
nobu 2014-07-17 13:30:44 +0900 (Thu, 17 Jul 2014) New Revision: 46850 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46850 Log: parse.y: separate from parse_ident * parse.y (tokadd_ident, tokenize_ident): extract from parse_ident(). * parse.y (parse_gvar, parse_atmark): move tokenization from parse_ident(). * parse.y (parse_ident): now deals with non-sigil names only. Modified files: trunk/parse.y Index: parse.y =================================================================== --- parse.y (revision 46849) +++ parse.y (revision 46850) @@ -7368,6 +7368,29 @@ parse_percent(struct parser_params *pars https://github.com/ruby/ruby/blob/trunk/parse.y#L7368 } static int +tokadd_ident(struct parser_params *parser, int c) +{ + do { + if (tokadd_mbchar(c) == -1) return -1; + c = nextc(); + } while (parser_is_identchar()); + pushback(c); + return 0; +} + +static void +tokenize_ident(struct parser_params *parser, const enum lex_state_e last_state) +{ + ID ident = TOK_INTERN(); + + set_yylval_name(ident); + if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) && + is_local_id(ident) && lvar_defined(ident)) { + lex_state = EXPR_END; + } +} + +static int parse_gvar(struct parser_params *parser, const enum lex_state_e last_state) { register int c; @@ -7457,7 +7480,11 @@ parse_gvar(struct parser_params *parser, https://github.com/ruby/ruby/blob/trunk/parse.y#L7480 case '0': tokadd('$'); } - return -c; + + if (tokadd_ident(parser, c)) return 0; + lex_state = EXPR_END; + tokenize_ident(parser, last_state); + return tGVAR; } static int @@ -7483,7 +7510,11 @@ parse_atmark(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L7510 } return 0; } - return -c; + + if (tokadd_ident(parser, c)) return 0; + lex_state = EXPR_END; + tokenize_ident(parser, last_state); + return result; } static int @@ -7498,131 +7529,100 @@ parse_ident(struct parser_params *parser https://github.com/ruby/ruby/blob/trunk/parse.y#L7529 if (tokadd_mbchar(c) == -1) return 0; c = nextc(); } while (parser_is_identchar()); - switch (tok()[0]) { - case '@': case '$': + if ((c == '!' || c == '?') && !peek('=')) { + tokadd(c); + } + else { pushback(c); - break; - default: - if ((c == '!' || c == '?') && !peek('=')) { - tokadd(c); - } - else { - pushback(c); - } } tokfix(); - switch (tok()[0]) { - case '$': - lex_state = EXPR_END; - result = tGVAR; - break; - - case '@': - lex_state = EXPR_END; - if (tok()[1] == '@') - result = tCVAR; - else - result = tIVAR; - break; - - default: - if (toklast() == '!' || toklast() == '?') { - result = tFID; - } - else { - if (IS_lex_state(EXPR_FNAME)) { - register int c = nextc(); - if (c == '=' && !peek('~') && !peek('>') && - (!peek('=') || (peek_n('>', 1)))) { - result = tIDENTIFIER; - tokadd(c); - tokfix(); - } - else { - pushback(c); - } - } - if (result == 0 && ISUPPER(tok()[0])) { - result = tCONSTANT; + if (toklast() == '!' || toklast() == '?') { + result = tFID; + } + else { + if (IS_lex_state(EXPR_FNAME)) { + register int c = nextc(); + if (c == '=' && !peek('~') && !peek('>') && + (!peek('=') || (peek_n('>', 1)))) { + result = tIDENTIFIER; + tokadd(c); + tokfix(); } else { - result = tIDENTIFIER; + pushback(c); } } + if (result == 0 && ISUPPER(tok()[0])) { + result = tCONSTANT; + } + else { + result = tIDENTIFIER; + } + } - if (IS_LABEL_POSSIBLE()) { - if (IS_LABEL_SUFFIX(0)) { - lex_state = EXPR_LABELARG; - nextc(); - set_yylval_name(TOK_INTERN()); - return tLABEL; - } - } - if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) { - const struct kwtable *kw; - - /* See if it is a reserved word. */ - kw = rb_reserved_word(tok(), toklen()); - if (kw) { - enum lex_state_e state = lex_state; - lex_state = kw->state; - if (IS_lex_state_for(state, EXPR_FNAME)) { - set_yylval_name(rb_intern(kw->name)); - return kw->id[0]; - } - if (IS_lex_state(EXPR_BEG)) { - command_start = TRUE; - } - if (kw->id[0] == keyword_do) { - if (lpar_beg && lpar_beg == paren_nest) { - lpar_beg = 0; - --paren_nest; - return keyword_do_LAMBDA; - } - if (COND_P()) return keyword_do_cond; - if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG)) - return keyword_do_block; - if (IS_lex_state_for(state, (EXPR_BEG | EXPR_ENDARG))) - return keyword_do_block; - return keyword_do; - } - if (IS_lex_state_for(state, (EXPR_BEG | EXPR_VALUE))) - return kw->id[0]; - else { - if (kw->id[0] != kw->id[1]) - lex_state = EXPR_BEG; - return kw->id[1]; - } - } + if (IS_LABEL_POSSIBLE()) { + if (IS_LABEL_SUFFIX(0)) { + lex_state = EXPR_LABELARG; + nextc(); + set_yylval_name(TOK_INTERN()); + return tLABEL; } + } + if (mb == ENC_CODERANGE_7BIT && !IS_lex_state(EXPR_DOT)) { + const struct kwtable *kw; - if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) { - if (cmd_state) { - lex_state = EXPR_CMDARG; + /* See if it is a reserved word. */ + kw = rb_reserved_word(tok(), toklen()); + if (kw) { + enum lex_state_e state = lex_state; + lex_state = kw->state; + if (IS_lex_state_for(state, EXPR_FNAME)) { + set_yylval_name(rb_intern(kw->name)); + return kw->id[0]; + } + if (IS_lex_state(EXPR_BEG)) { + command_start = TRUE; } + if (kw->id[0] == keyword_do) { + if (lpar_beg && lpar_beg == paren_nest) { + lpar_beg = 0; + --paren_nest; + return keyword_do_LAMBDA; + } + if (COND_P()) return keyword_do_cond; + if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG)) + return keyword_do_block; + if (IS_lex_state_for(state, (EXPR_BEG | EXPR_ENDARG))) + return keyword_do_block; + return keyword_do; + } + if (IS_lex_state_for(state, (EXPR_BEG | EXPR_VALUE))) + return kw->id[0]; else { - lex_state = EXPR_ARG; + if (kw->id[0] != kw->id[1]) + lex_state = EXPR_BEG; + return kw->id[1]; } } - else if (lex_state == EXPR_FNAME) { - lex_state = EXPR_ENDFN; + } + + if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) { + if (cmd_state) { + lex_state = EXPR_CMDARG; } else { - lex_state = EXPR_END; + lex_state = EXPR_ARG; } } - - { - ID ident = TOK_INTERN(); - - set_yylval_name(ident); - if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) && - is_local_id(ident) && lvar_defined(ident)) { - lex_state = EXPR_END; - } + else if (lex_state == EXPR_FNAME) { + lex_state = EXPR_ENDFN; + } + else { + lex_state = EXPR_END; } + tokenize_ident(parser, last_state); return result; } @@ -8244,16 +8244,10 @@ parser_yylex(struct parser_params *parse https://github.com/ruby/ruby/blob/trunk/parse.y#L8244 return parse_percent(parser, space_seen, last_state); case '$': - c = parse_gvar(parser, last_state); - if (c >= 0) return c; - c = -c; - break; + return parse_gvar(parser, last_state); case '@': - c = parse_atmark(parser, last_state); - if (c >= 0) return c; - c = -c; - break; + return parse_atmark(parser, last_state); case '_': if (was_bol() && whole_match_p("__END__", 7, 0)) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/