Skip to content

Commit 5151f8d

Browse files
author
thomas.wouters
committed
Make 'as' an actual keyword when with's future statement is used. Not actually necessary for functionality, but good for transition. git-svn-id: http://svn.python.org/projects/python/trunk@42694 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 963355c commit 5151f8d

5 files changed

Lines changed: 212 additions & 206 deletions

File tree

Grammar/Grammar

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ import_stmt: import_name | import_from
6161
import_name: 'import' dotted_as_names
6262
import_from: ('from' ('.'* dotted_name | '.')
6363
'import' ('*' | '(' import_as_names ')' | import_as_names))
64-
import_as_name: NAME [NAME NAME]
65-
dotted_as_name: dotted_name [NAME NAME]
64+
import_as_name: NAME [('as' | NAME) NAME]
65+
dotted_as_name: dotted_name [('as' | NAME) NAME]
6666
import_as_names: import_as_name (',' import_as_name)* [',']
6767
dotted_as_names: dotted_as_name (',' dotted_as_name)*
6868
dotted_name: NAME ('.' NAME)*
@@ -80,7 +80,7 @@ try_stmt: ('try' ':' suite
8080
['finally' ':' suite] |
8181
'finally' ':' suite))
8282
with_stmt: 'with' test [ with_var ] ':' suite
83-
with_var: NAME expr
83+
with_var: ('as' | NAME) expr
8484
# NB compile.c makes sure that the default except clause is last
8585
except_clause: 'except' [test [',' test]]
8686
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

Parser/parser.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,20 @@ classify(parser_state *ps, int type, char *str)
144144
register label *l = g->g_ll.ll_label;
145145
register int i;
146146
for (i = n; i > 0; i--, l++) {
147-
if (l->lb_type == NAME && l->lb_str != NULL &&
148-
l->lb_str[0] == s[0] &&
149-
strcmp(l->lb_str, s) == 0) {
147+
if (l->lb_type != NAME || l->lb_str == NULL ||
148+
l->lb_str[0] != s[0] ||
149+
strcmp(l->lb_str, s) != 0)
150+
continue;
150151
#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
151-
if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT) &&
152-
s[0] == 'w' &&
153-
strcmp(s, "with") == 0)
154-
break; /* not a keyword */
155-
#endif
156-
D(printf("It's a keyword\n"));
157-
return n - i;
152+
if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
153+
if (s[0] == 'w' && strcmp(s, "with") == 0)
154+
break; /* not a keyword yet */
155+
else if (s[0] == 'a' && strcmp(s, "as") == 0)
156+
break; /* not a keyword yet */
158157
}
158+
#endif
159+
D(printf("It's a keyword\n"));
160+
return n - i;
159161
}
160162
}
161163

Parser/parsetok.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
176176
if (len == 4 && str[0] == 'w' && strcmp(str, "with") == 0)
177177
warn(with_msg, err_ret->filename, tok->lineno);
178178
else if (!(handling_import || handling_with) &&
179-
len == 2 &&
180-
str[0] == 'a' && strcmp(str, "as") == 0)
179+
len == 2 && str[0] == 'a' &&
180+
strcmp(str, "as") == 0)
181181
warn(as_msg, err_ret->filename, tok->lineno);
182182
}
183183
else if (type == NAME &&

Python/ast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,8 +2088,8 @@ static alias_ty
20882088
alias_for_import_name(struct compiling *c, const node *n)
20892089
{
20902090
/*
2091-
import_as_name: NAME [NAME NAME]
2092-
dotted_as_name: dotted_name [NAME NAME]
2091+
import_as_name: NAME ['as' NAME]
2092+
dotted_as_name: dotted_name ['as' NAME]
20932093
dotted_name: NAME ('.' NAME)*
20942094
*/
20952095
PyObject *str;

0 commit comments

Comments
 (0)