1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46850 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-07-17 04:30:44 +00:00
parent 27e1b24776
commit 25a2e870d4

228
parse.y
View file

@ -7367,6 +7367,29 @@ parse_percent(struct parser_params *parser, const int space_seen, const enum lex
return '%'; return '%';
} }
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 static int
parse_gvar(struct parser_params *parser, const enum lex_state_e last_state) parse_gvar(struct parser_params *parser, const enum lex_state_e last_state)
{ {
@ -7457,7 +7480,11 @@ parse_gvar(struct parser_params *parser, const enum lex_state_e last_state)
case '0': case '0':
tokadd('$'); tokadd('$');
} }
return -c;
if (tokadd_ident(parser, c)) return 0;
lex_state = EXPR_END;
tokenize_ident(parser, last_state);
return tGVAR;
} }
static int static int
@ -7483,7 +7510,11 @@ parse_atmark(struct parser_params *parser, const enum lex_state_e last_state)
} }
return 0; return 0;
} }
return -c;
if (tokadd_ident(parser, c)) return 0;
lex_state = EXPR_END;
tokenize_ident(parser, last_state);
return result;
} }
static int static int
@ -7498,131 +7529,100 @@ parse_ident(struct parser_params *parser, int c, int cmd_state)
if (tokadd_mbchar(c) == -1) return 0; if (tokadd_mbchar(c) == -1) return 0;
c = nextc(); c = nextc();
} while (parser_is_identchar()); } while (parser_is_identchar());
switch (tok()[0]) { if ((c == '!' || c == '?') && !peek('=')) {
case '@': case '$': tokadd(c);
}
else {
pushback(c); pushback(c);
break;
default:
if ((c == '!' || c == '?') && !peek('=')) {
tokadd(c);
}
else {
pushback(c);
}
} }
tokfix(); tokfix();
switch (tok()[0]) { if (toklast() == '!' || toklast() == '?') {
case '$': result = tFID;
lex_state = EXPR_END; }
result = tGVAR; else {
break; if (IS_lex_state(EXPR_FNAME)) {
register int c = nextc();
case '@': if (c == '=' && !peek('~') && !peek('>') &&
lex_state = EXPR_END; (!peek('=') || (peek_n('>', 1)))) {
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;
}
else {
result = tIDENTIFIER; result = tIDENTIFIER;
} tokadd(c);
} tokfix();
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_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
if (cmd_state) {
lex_state = EXPR_CMDARG;
} }
else { else {
lex_state = EXPR_ARG; pushback(c);
} }
} }
else if (lex_state == EXPR_FNAME) { if (result == 0 && ISUPPER(tok()[0])) {
lex_state = EXPR_ENDFN; result = tCONSTANT;
} }
else { else {
lex_state = EXPR_END; result = tIDENTIFIER;
} }
} }
{ if (IS_LABEL_POSSIBLE()) {
ID ident = TOK_INTERN(); 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;
set_yylval_name(ident); /* See if it is a reserved word. */
if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) && kw = rb_reserved_word(tok(), toklen());
is_local_id(ident) && lvar_defined(ident)) { if (kw) {
lex_state = EXPR_END; 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_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
if (cmd_state) {
lex_state = EXPR_CMDARG;
}
else {
lex_state = EXPR_ARG;
}
}
else if (lex_state == EXPR_FNAME) {
lex_state = EXPR_ENDFN;
}
else {
lex_state = EXPR_END;
}
tokenize_ident(parser, last_state);
return result; return result;
} }
@ -8244,16 +8244,10 @@ parser_yylex(struct parser_params *parser)
return parse_percent(parser, space_seen, last_state); return parse_percent(parser, space_seen, last_state);
case '$': case '$':
c = parse_gvar(parser, last_state); return parse_gvar(parser, last_state);
if (c >= 0) return c;
c = -c;
break;
case '@': case '@':
c = parse_atmark(parser, last_state); return parse_atmark(parser, last_state);
if (c >= 0) return c;
c = -c;
break;
case '_': case '_':
if (was_bol() && whole_match_p("__END__", 7, 0)) { if (was_bol() && whole_match_p("__END__", 7, 0)) {