summaryrefslogtreecommitdiffstats
path: root/Parser/parser.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-07-15 21:08:29 (GMT)
committerGuido van Rossum <guido@python.org>2001-07-15 21:08:29 (GMT)
commitb09f7ed6235783fca27a4f8730c4c33e0f53c16c (patch)
treed76806c53fd83557378dcd7395ce5309f707eb15 /Parser/parser.c
parent045ca7ae72368ce65fe7debbd343cf8133eea5e4 (diff)
downloadcpython-b09f7ed6235783fca27a4f8730c4c33e0f53c16c.zip
cpython-b09f7ed6235783fca27a4f8730c4c33e0f53c16c.tar.gz
cpython-b09f7ed6235783fca27a4f8730c4c33e0f53c16c.tar.bz2
Preliminary support for "from __future__ import generators" to enable
the yield statement. I figure we have to have this in before I can release 2.2a1 on Wednesday. Note: test_generators is currently broken, I'm counting on Tim to fix this.
Diffstat (limited to 'Parser/parser.c')
-rw-r--r--Parser/parser.c38
1 files changed, 35 insertions, 3 deletions
diff --git a/Parser/parser.c b/Parser/parser.c
index 6eaa925..753c43a 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -79,6 +79,7 @@ PyParser_New(grammar *g, int start)
if (ps == NULL)
return NULL;
ps->p_grammar = g;
+ ps->p_generators = 0;
ps->p_tree = PyNode_New(start);
if (ps->p_tree == NULL) {
PyMem_DEL(ps);
@@ -131,8 +132,9 @@ push(register stack *s, int type, dfa *d, int newstate, int lineno)
/* PARSER PROPER */
static int
-classify(grammar *g, int type, char *str)
+classify(parser_state *ps, int type, char *str)
{
+ grammar *g = ps->p_grammar;
register int n = g->g_ll.ll_nlabels;
if (type == NAME) {
@@ -143,6 +145,10 @@ classify(grammar *g, int type, char *str)
if (l->lb_type == NAME && l->lb_str != NULL &&
l->lb_str[0] == s[0] &&
strcmp(l->lb_str, s) == 0) {
+ if (!ps->p_generators &&
+ s[0] == 'y' &&
+ strcmp(s, "yield") == 0)
+ break; /* not a keyword */
D(printf("It's a keyword\n"));
return n - i;
}
@@ -164,6 +170,22 @@ classify(grammar *g, int type, char *str)
return -1;
}
+static void
+future_hack(parser_state *ps)
+{
+ node *n = ps->p_stack.s_top->s_parent;
+ node *ch;
+
+ if (strcmp(STR(CHILD(n, 0)), "from") != 0)
+ return;
+ ch = CHILD(n, 1);
+ if (strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
+ return;
+ ch = CHILD(n, 3);
+ if (NCH(ch) == 1 && strcmp(STR(CHILD(ch, 0)), "generators") == 0)
+ ps->p_generators = 1;
+}
+
int
PyParser_AddToken(register parser_state *ps, register int type, char *str,
int lineno, int *expected_ret)
@@ -174,7 +196,7 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
/* Find out which label this token is */
- ilabel = classify(ps->p_grammar, type, str);
+ ilabel = classify(ps, type, str);
if (ilabel < 0)
return E_SYNTAX;
@@ -217,7 +239,14 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
while (s = &d->d_state
[ps->p_stack.s_top->s_state],
s->s_accept && s->s_narcs == 1) {
- D(printf(" Direct pop.\n"));
+ D(printf(" DFA '%s', state %d: "
+ "Direct pop.\n",
+ d->d_name,
+ ps->p_stack.s_top->s_state));
+ if (d->d_name[0] == 'i' &&
+ strcmp(d->d_name,
+ "import_stmt") == 0)
+ future_hack(ps);
s_pop(&ps->p_stack);
if (s_empty(&ps->p_stack)) {
D(printf(" ACCEPT.\n"));
@@ -230,6 +259,9 @@ PyParser_AddToken(register parser_state *ps, register int type, char *str,
}
if (s->s_accept) {
+ if (d->d_name[0] == 'i' &&
+ strcmp(d->d_name, "import_stmt") == 0)
+ future_hack(ps);
/* Pop this dfa and try again */
s_pop(&ps->p_stack);
D(printf(" Pop ...\n"));