@@ -79,8 +79,8 @@ PyParser_New(grammar *g, int start)
7979 if (ps == NULL )
8080 return NULL ;
8181 ps -> p_grammar = g ;
82- #if 0 /* future keyword */
83- ps -> p_generators = 0 ;
82+ #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
83+ ps -> p_flags = 0 ;
8484#endif
8585 ps -> p_tree = PyNode_New (start );
8686 if (ps -> p_tree == NULL ) {
@@ -147,10 +147,10 @@ classify(parser_state *ps, int type, char *str)
147147 if (l -> lb_type == NAME && l -> lb_str != NULL &&
148148 l -> lb_str [0 ] == s [0 ] &&
149149 strcmp (l -> lb_str , s ) == 0 ) {
150- #if 0 /* future keyword */
151- if (!ps -> p_generators &&
152- s [0 ] == 'y ' &&
153- strcmp (s , "yield " ) == 0 )
150+ #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
151+ if (!( ps -> p_flags & CO_FUTURE_WITH_STATEMENT ) &&
152+ s [0 ] == 'w ' &&
153+ strcmp (s , "with " ) == 0 )
154154 break ; /* not a keyword */
155155#endif
156156 D (printf ("It's a keyword\n" ));
@@ -174,24 +174,35 @@ classify(parser_state *ps, int type, char *str)
174174 return -1 ;
175175}
176176
177- #if 0 /* future keyword */
177+ #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
178178static void
179179future_hack (parser_state * ps )
180180{
181181 node * n = ps -> p_stack .s_top -> s_parent ;
182182 node * ch ;
183183 int i ;
184184
185- if (strcmp (STR (CHILD (n , 0 )), "from" ) != 0 )
185+ /* from __future__ import ..., must have at least 4 children */
186+ n = CHILD (n , 0 );
187+ if (NCH (n ) < 4 )
188+ return ;
189+ ch = CHILD (n , 0 );
190+ if (STR (ch ) == NULL || strcmp (STR (ch ), "from" ) != 0 )
186191 return ;
187192 ch = CHILD (n , 1 );
188- if (strcmp (STR (CHILD (ch , 0 )), "__future__" ) != 0 )
193+ if (NCH (ch ) == 1 && STR (CHILD (ch , 0 )) &&
194+ strcmp (STR (CHILD (ch , 0 )), "__future__" ) != 0 )
189195 return ;
190196 for (i = 3 ; i < NCH (n ); i += 2 ) {
197+ /* XXX: assume we don't have parentheses in import:
198+ from __future__ import (x, y, z)
199+ */
191200 ch = CHILD (n , i );
201+ if (NCH (ch ) == 1 )
202+ ch = CHILD (ch , 0 );
192203 if (NCH (ch ) >= 1 && TYPE (CHILD (ch , 0 )) == NAME &&
193- strcmp (STR (CHILD (ch , 0 )), "generators " ) == 0 ) {
194- ps -> p_generators = 1 ;
204+ strcmp (STR (CHILD (ch , 0 )), "with_statement " ) == 0 ) {
205+ ps -> p_flags |= CO_FUTURE_WITH_STATEMENT ;
195206 break ;
196207 }
197208 }
@@ -255,7 +266,7 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
255266 "Direct pop.\n" ,
256267 d -> d_name ,
257268 ps -> p_stack .s_top -> s_state ));
258- #if 0 /* future keyword */
269+ #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
259270 if (d -> d_name [0 ] == 'i' &&
260271 strcmp (d -> d_name ,
261272 "import_stmt" ) == 0 )
@@ -273,7 +284,7 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
273284 }
274285
275286 if (s -> s_accept ) {
276- #if 0 /* future keyword */
287+ #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
277288 if (d -> d_name [0 ] == 'i' &&
278289 strcmp (d -> d_name , "import_stmt" ) == 0 )
279290 future_hack (ps );
0 commit comments