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

Skip to content

Commit 52f2c05

Browse files
committed
* parsermodule.c, Makefile, config.c: rudimentary interface to the Python
parser. * mappingobject.c (lookmapping): 'freeslot' was never used due to a bug in the code.
1 parent a3d78fb commit 52f2c05

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

Modules/config.c.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ extern void initHTML();
296296
#ifdef USE_XLIB
297297
extern void initXlib();
298298
#endif
299+
#ifdef USE_PARSER
300+
extern void initparser();
301+
#endif
299302
/* -- ADDMODULE MARKER 1 -- */
300303

301304
struct {
@@ -475,6 +478,10 @@ struct {
475478
{"Xlib", initXlib},
476479
#endif
477480

481+
#ifdef USE_PARSER
482+
{"parser", initparser},
483+
#endif
484+
478485
/* -- ADDMODULE MARKER 2 -- */
479486

480487
{0, 0} /* Sentinel */

Modules/parsermodule.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/***********************************************************
2+
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3+
Amsterdam, The Netherlands.
4+
5+
All Rights Reserved
6+
7+
Permission to use, copy, modify, and distribute this software and its
8+
documentation for any purpose and without fee is hereby granted,
9+
provided that the above copyright notice appear in all copies and that
10+
both that copyright notice and this permission notice appear in
11+
supporting documentation, and that the names of Stichting Mathematisch
12+
Centrum or CWI not be used in advertising or publicity pertaining to
13+
distribution of the software without specific, written prior permission.
14+
15+
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16+
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17+
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18+
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22+
23+
******************************************************************/
24+
25+
#include "allobjects.h"
26+
#include "node.h"
27+
#include "token.h"
28+
#include "pythonrun.h"
29+
#include "graminit.h"
30+
#include "errcode.h"
31+
32+
object *
33+
node2tuple(n)
34+
node *n;
35+
{
36+
if (n == NULL) {
37+
INCREF(None);
38+
return None;
39+
}
40+
if (ISNONTERMINAL(TYPE(n))) {
41+
int i;
42+
object *v, *w;
43+
v = newtupleobject(1 + NCH(n));
44+
if (v == NULL)
45+
return v;
46+
w = newintobject(TYPE(n));
47+
if (w == NULL) {
48+
DECREF(v);
49+
return NULL;
50+
}
51+
settupleitem(v, 0, w);
52+
for (i = 0; i < NCH(n); i++) {
53+
w = node2tuple(CHILD(n, i));
54+
if (w == NULL) {
55+
DECREF(v);
56+
return NULL;
57+
}
58+
settupleitem(v, i+1, w);
59+
}
60+
return v;
61+
}
62+
else if (ISTERMINAL(TYPE(n))) {
63+
return mkvalue("(is)", TYPE(n), STR(n));
64+
}
65+
else {
66+
err_setstr(SystemError, "unrecognized parse tree node type");
67+
return NULL;
68+
}
69+
}
70+
71+
static object *
72+
parser_parsefile(self, args)
73+
object *self;
74+
object *args;
75+
{
76+
char *filename;
77+
FILE *fp;
78+
node *n = NULL;
79+
int err;
80+
object *res;
81+
if (!getargs(args, "s", &filename))
82+
return NULL;
83+
fp = fopen(filename, "r");
84+
if (fp == NULL) {
85+
err_errno(IOError);
86+
return NULL;
87+
}
88+
err = parse_file(fp, filename, file_input, &n);
89+
fclose(fp);
90+
if (err != E_DONE) {
91+
err_input(err);
92+
return NULL;
93+
}
94+
res = node2tuple(n);
95+
freetree(n);
96+
return res;
97+
}
98+
99+
static struct methodlist parser_methods[] = {
100+
{"parsefile", parser_parsefile},
101+
{0, 0} /* Sentinel */
102+
};
103+
104+
void
105+
initparser()
106+
{
107+
initmodule("parser", parser_methods);
108+
}

Objects/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ lookmapping(mp, key, hash)
147147
return ep;
148148
}
149149
if (ep->me_key == dummy) {
150-
if (freeslot != NULL)
150+
if (freeslot == NULL)
151151
freeslot = ep;
152152
}
153153
else if (ep->me_hash == hash &&

Objects/mappingobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ lookmapping(mp, key, hash)
147147
return ep;
148148
}
149149
if (ep->me_key == dummy) {
150-
if (freeslot != NULL)
150+
if (freeslot == NULL)
151151
freeslot = ep;
152152
}
153153
else if (ep->me_hash == hash &&

0 commit comments

Comments
 (0)