ยปCore Development>Code coverage>Parser/pgenmain.c

Python code coverage for Parser/pgenmain.c

#countcontent
1n/a
2n/a/* Parser generator main program */
3n/a
4n/a/* This expects a filename containing the grammar as argv[1] (UNIX)
5n/a or asks the console for such a file name (THINK C).
6n/a It writes its output on two files in the current directory:
7n/a - "graminit.c" gets the grammar as a bunch of initialized data
8n/a - "graminit.h" gets the grammar's non-terminals as #defines.
9n/a Error messages and status info during the generation process are
10n/a written to stdout, or sometimes to stderr. */
11n/a
12n/a/* XXX TO DO:
13n/a - check for duplicate definitions of names (instead of fatal err)
14n/a*/
15n/a
16n/a#define PGEN
17n/a
18n/a#include "Python.h"
19n/a#include "pgenheaders.h"
20n/a#include "grammar.h"
21n/a#include "node.h"
22n/a#include "parsetok.h"
23n/a#include "pgen.h"
24n/a
25n/aint Py_DebugFlag;
26n/aint Py_VerboseFlag;
27n/aint Py_IgnoreEnvironmentFlag;
28n/a
29n/a/* Forward */
30n/agrammar *getgrammar(const char *filename);
31n/a
32n/avoid Py_Exit(int) _Py_NO_RETURN;
33n/a
34n/avoid
35n/aPy_Exit(int sts)
36n/a{
37n/a exit(sts);
38n/a}
39n/a
40n/a#ifdef WITH_THREAD
41n/a/* Needed by obmalloc.c */
42n/aint PyGILState_Check(void)
43n/a{ return 1; }
44n/a#endif
45n/a
46n/avoid _PyMem_DumpTraceback(int fd, const void *ptr)
47n/a{}
48n/a
49n/aint
50n/amain(int argc, char **argv)
51n/a{
52n/a grammar *g;
53n/a FILE *fp;
54n/a char *filename, *graminit_h, *graminit_c;
55n/a
56n/a if (argc != 4) {
57n/a fprintf(stderr,
58n/a "usage: %s grammar graminit.h graminit.c\n", argv[0]);
59n/a Py_Exit(2);
60n/a }
61n/a filename = argv[1];
62n/a graminit_h = argv[2];
63n/a graminit_c = argv[3];
64n/a g = getgrammar(filename);
65n/a fp = fopen(graminit_c, "w");
66n/a if (fp == NULL) {
67n/a perror(graminit_c);
68n/a Py_Exit(1);
69n/a }
70n/a if (Py_DebugFlag)
71n/a printf("Writing %s ...\n", graminit_c);
72n/a printgrammar(g, fp);
73n/a fclose(fp);
74n/a fp = fopen(graminit_h, "w");
75n/a if (fp == NULL) {
76n/a perror(graminit_h);
77n/a Py_Exit(1);
78n/a }
79n/a if (Py_DebugFlag)
80n/a printf("Writing %s ...\n", graminit_h);
81n/a printnonterminals(g, fp);
82n/a fclose(fp);
83n/a freegrammar(g);
84n/a Py_Exit(0);
85n/a return 0; /* Make gcc -Wall happy */
86n/a}
87n/a
88n/agrammar *
89n/agetgrammar(const char *filename)
90n/a{
91n/a FILE *fp;
92n/a node *n;
93n/a grammar *g0, *g;
94n/a perrdetail err;
95n/a
96n/a fp = fopen(filename, "r");
97n/a if (fp == NULL) {
98n/a perror(filename);
99n/a Py_Exit(1);
100n/a }
101n/a g0 = meta_grammar();
102n/a n = PyParser_ParseFile(fp, filename, g0, g0->g_start,
103n/a (char *)NULL, (char *)NULL, &err);
104n/a fclose(fp);
105n/a if (n == NULL) {
106n/a fprintf(stderr, "Parsing error %d, line %d.\n",
107n/a err.error, err.lineno);
108n/a if (err.text != NULL) {
109n/a size_t len;
110n/a int i;
111n/a fprintf(stderr, "%s", err.text);
112n/a len = strlen(err.text);
113n/a if (len == 0 || err.text[len-1] != '\n')
114n/a fprintf(stderr, "\n");
115n/a for (i = 0; i < err.offset; i++) {
116n/a if (err.text[i] == '\t')
117n/a putc('\t', stderr);
118n/a else
119n/a putc(' ', stderr);
120n/a }
121n/a fprintf(stderr, "^\n");
122n/a PyObject_FREE(err.text);
123n/a }
124n/a Py_Exit(1);
125n/a }
126n/a g = pgen(n);
127n/a PyNode_Free(n);
128n/a if (g == NULL) {
129n/a printf("Bad grammar.\n");
130n/a Py_Exit(1);
131n/a }
132n/a return g;
133n/a}
134n/a
135n/a/* Can't happen in pgen */
136n/aPyObject*
137n/aPyErr_Occurred()
138n/a{
139n/a return 0;
140n/a}
141n/a
142n/avoid
143n/aPy_FatalError(const char *msg)
144n/a{
145n/a fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
146n/a Py_Exit(1);
147n/a}
148n/a
149n/a/* No-nonsense my_readline() for tokenizer.c */
150n/a
151n/achar *
152n/aPyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
153n/a{
154n/a size_t n = 1000;
155n/a char *p = (char *)PyMem_MALLOC(n);
156n/a char *q;
157n/a if (p == NULL)
158n/a return NULL;
159n/a fprintf(stderr, "%s", prompt);
160n/a q = fgets(p, n, sys_stdin);
161n/a if (q == NULL) {
162n/a *p = '\0';
163n/a return p;
164n/a }
165n/a n = strlen(p);
166n/a if (n > 0 && p[n-1] != '\n')
167n/a p[n-1] = '\n';
168n/a return (char *)PyMem_REALLOC(p, n+1);
169n/a}
170n/a
171n/a/* No-nonsense fgets */
172n/achar *
173n/aPy_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
174n/a{
175n/a return fgets(buf, n, stream);
176n/a}
177n/a
178n/a
179n/a#include <stdarg.h>
180n/a
181n/avoid
182n/aPySys_WriteStderr(const char *format, ...)
183n/a{
184n/a va_list va;
185n/a
186n/a va_start(va, format);
187n/a vfprintf(stderr, format, va);
188n/a va_end(va);
189n/a}