blob: 5aeea290300b247c7f8f949335bc4c4bbf3affa6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Parser-tokenizer link implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028#include "tokenizer.h"
29#include "node.h"
30#include "grammar.h"
31#include "parser.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#include "parsetok.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033#include "errcode.h"
34
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
36/* Forward */
37static int parsetok PROTO((struct tok_state *, grammar *, int, node **));
38
39
40/* Parse input coming from a string. Return error code, print some errors. */
41
42int
43parsestring(s, g, start, n_ret)
44 char *s;
45 grammar *g;
46 int start;
47 node **n_ret;
48{
49 struct tok_state *tok = tok_setups(s);
50 int ret;
51
52 if (tok == NULL) {
53 fprintf(stderr, "no mem for tok_setups\n");
54 return E_NOMEM;
55 }
56 ret = parsetok(tok, g, start, n_ret);
57 if (ret == E_TOKEN || ret == E_SYNTAX) {
58 fprintf(stderr, "String parsing error at line %d\n",
59 tok->lineno);
60 }
61 tok_free(tok);
62 return ret;
63}
64
65
66/* Parse input coming from a file. Return error code, print some errors. */
67
68int
69parsefile(fp, filename, g, start, ps1, ps2, n_ret)
70 FILE *fp;
71 char *filename;
72 grammar *g;
73 int start;
74 char *ps1, *ps2;
75 node **n_ret;
76{
77 struct tok_state *tok = tok_setupf(fp, ps1, ps2);
78 int ret;
79
80 if (tok == NULL) {
81 fprintf(stderr, "no mem for tok_setupf\n");
82 return E_NOMEM;
83 }
84 ret = parsetok(tok, g, start, n_ret);
85 if (ret == E_TOKEN || ret == E_SYNTAX) {
86 char *p;
87 fprintf(stderr, "Parsing error: file %s, line %d:\n",
88 filename, tok->lineno);
89 *tok->inp = '\0';
90 if (tok->inp > tok->buf && tok->inp[-1] == '\n')
91 tok->inp[-1] = '\0';
92 fprintf(stderr, "%s\n", tok->buf);
93 for (p = tok->buf; p < tok->cur; p++) {
94 if (*p == '\t')
95 putc('\t', stderr);
96 else
97 putc(' ', stderr);
98 }
99 fprintf(stderr, "^\n");
100 }
101 tok_free(tok);
102 return ret;
103}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104
105
106/* Parse input coming from the given tokenizer structure.
107 Return error code. */
108
109static int
110parsetok(tok, g, start, n_ret)
111 struct tok_state *tok;
112 grammar *g;
113 int start;
114 node **n_ret;
115{
116 parser_state *ps;
117 int ret;
118
119 if ((ps = newparser(g, start)) == NULL) {
120 fprintf(stderr, "no mem for new parser\n");
121 return E_NOMEM;
122 }
123
124 for (;;) {
125 char *a, *b;
126 int type;
127 int len;
128 char *str;
129
130 type = tok_get(tok, &a, &b);
131 if (type == ERRORTOKEN) {
132 ret = tok->done;
133 break;
134 }
135 len = b - a;
136 str = NEW(char, len + 1);
137 if (str == NULL) {
138 fprintf(stderr, "no mem for next token\n");
139 ret = E_NOMEM;
140 break;
141 }
142 strncpy(str, a, len);
143 str[len] = '\0';
Guido van Rossum3f5da241990-12-20 15:06:42 +0000144 ret = addtoken(ps, (int)type, str, tok->lineno);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 if (ret != E_OK) {
Guido van Rossum840bcf11990-11-18 17:39:41 +0000146 if (ret == E_DONE) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 *n_ret = ps->p_tree;
Guido van Rossum840bcf11990-11-18 17:39:41 +0000148 ps->p_tree = NULL;
149 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 else if (tok->lineno <= 1 && tok->done == E_EOF)
151 ret = E_EOF;
152 break;
153 }
154 }
155
156 delparser(ps);
157 return ret;
158}