Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3ab37f

Browse files
committed
Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to
work. This includes some more code that used to be part of pgen in the main parser; I'm okay with that. I'll see if the Windows build needs work next.
1 parent 6e5be22 commit d3ab37f

6 files changed

Lines changed: 60 additions & 9 deletions

File tree

Include/parsetok.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ PyAPI_FUNC(node *) PyParser_ParseStringFlagsFilename(const char *,
3838
const char *,
3939
grammar *, int,
4040
perrdetail *, int);
41+
42+
/* Note that he following function is defined in pythonrun.c not parsetok.c. */
43+
PyAPI_FUNC(void) PyParser_SetError(perrdetail *);
44+
4145
#ifdef __cplusplus
4246
}
4347
#endif

Include/pgen.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef Py_PGEN_H
2+
#define Py_PGEN_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
8+
/* Parser generator interface */
9+
10+
extern grammar *meta_grammar(void);
11+
12+
struct _node;
13+
extern grammar *pgen(struct _node *);
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif
18+
#endif /* !Py_PGEN_H */

Makefile.pre.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,17 @@ POBJS= \
194194
Parser/parser.o \
195195
Parser/parsetok.o \
196196
Parser/bitset.o \
197-
Parser/metagrammar.o
197+
Parser/metagrammar.o \
198+
Parser/firstsets.o \
199+
Parser/grammar.o \
200+
Parser/pgen.o
198201

199202
PARSER_OBJS= $(POBJS) Parser/myreadline.o Parser/tokenizer.o
200203

201204
PGOBJS= \
202205
Objects/obmalloc.o \
203206
Python/mysnprintf.o \
204207
Parser/tokenizer_pgen.o \
205-
Parser/firstsets.o \
206-
Parser/grammar.o \
207-
Parser/pgen.o \
208208
Parser/printgrammar.o \
209209
Parser/pgenmain.o
210210

Parser/grammar.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ adddfa(grammar *g, int type, char *name)
3838
Py_FatalError("no mem to resize dfa in adddfa");
3939
d = &g->g_dfa[g->g_ndfas++];
4040
d->d_type = type;
41-
d->d_name = name;
41+
d->d_name = strdup(name);
4242
d->d_nstates = 0;
4343
d->d_state = NULL;
4444
d->d_initial = -1;
@@ -98,7 +98,10 @@ addlabel(labellist *ll, int type, char *str)
9898
Py_FatalError("no mem to resize labellist in addlabel");
9999
lb = &ll->ll_label[ll->ll_nlabels++];
100100
lb->lb_type = type;
101-
lb->lb_str = str; /* XXX strdup(str) ??? */
101+
lb->lb_str = strdup(str);
102+
if (Py_DebugFlag)
103+
printf("Label @ %08x, %d: %s\n", (unsigned)ll, ll->ll_nlabels,
104+
PyGrammar_LabelRepr(lb));
102105
return lb - ll->ll_label;
103106
}
104107

@@ -152,6 +155,7 @@ translabel(grammar *g, label *lb)
152155
lb->lb_str,
153156
g->g_dfa[i].d_type);
154157
lb->lb_type = g->g_dfa[i].d_type;
158+
free(lb->lb_str);
155159
lb->lb_str = NULL;
156160
return;
157161
}
@@ -162,6 +166,7 @@ translabel(grammar *g, label *lb)
162166
printf("Label %s is terminal %d.\n",
163167
lb->lb_str, i);
164168
lb->lb_type = i;
169+
free(lb->lb_str);
165170
lb->lb_str = NULL;
166171
return;
167172
}
@@ -173,18 +178,29 @@ translabel(grammar *g, label *lb)
173178
if (lb->lb_type == STRING) {
174179
if (isalpha((int)(lb->lb_str[1])) || lb->lb_str[1] == '_') {
175180
char *p;
181+
char *src;
182+
char *dest;
183+
size_t name_len;
176184
if (Py_DebugFlag)
177185
printf("Label %s is a keyword\n", lb->lb_str);
178186
lb->lb_type = NAME;
179-
lb->lb_str++;
180-
p = strchr(lb->lb_str, '\'');
187+
src = lb->lb_str + 1;
188+
p = strchr(src, '\'');
181189
if (p)
182-
*p = '\0';
190+
name_len = p - src;
191+
else
192+
name_len = strlen(src);
193+
dest = malloc(name_len + 1);
194+
strncpy(dest, src, name_len);
195+
dest[name_len] = '\0';
196+
free(lb->lb_str);
197+
lb->lb_str = dest;
183198
}
184199
else if (lb->lb_str[2] == lb->lb_str[0]) {
185200
int type = (int) PyToken_OneChar(lb->lb_str[1]);
186201
if (type != OP) {
187202
lb->lb_type = type;
203+
free(lb->lb_str);
188204
lb->lb_str = NULL;
189205
}
190206
else
@@ -196,6 +212,7 @@ translabel(grammar *g, label *lb)
196212
lb->lb_str[2]);
197213
if (type != OP) {
198214
lb->lb_type = type;
215+
free(lb->lb_str);
199216
lb->lb_str = NULL;
200217
}
201218
else
@@ -208,6 +225,7 @@ translabel(grammar *g, label *lb)
208225
lb->lb_str[3]);
209226
if (type != OP) {
210227
lb->lb_type = type;
228+
free(lb->lb_str);
211229
lb->lb_str = NULL;
212230
}
213231
else

Parser/metagrammar.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,9 @@ meta_grammar(void)
151151
{
152152
return &_PyParser_Grammar;
153153
}
154+
155+
grammar *
156+
Py_meta_grammar(void)
157+
{
158+
return meta_grammar();
159+
}

Parser/pgen.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,11 @@ pgen(node *n)
669669
return g;
670670
}
671671

672+
grammar *
673+
Py_pgen(node *n)
674+
{
675+
return pgen(n);
676+
}
672677

673678
/*
674679

0 commit comments

Comments
 (0)