This commit was manufactured by cvs2svn to create tag 'release101'.
[python/dscho.git] / Parser / parsetok.c
blob70b4fb11d36d501c496efb408b1f5302363c8b95
1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* Parser-tokenizer link implementation */
27 #include "pgenheaders.h"
28 #include "tokenizer.h"
29 #include "node.h"
30 #include "grammar.h"
31 #include "parser.h"
32 #include "parsetok.h"
33 #include "errcode.h"
36 /* Forward */
37 static node *parsetok PROTO((struct tok_state *, grammar *, int,
38 perrdetail *));
40 /* Parse input coming from a string. Return error code, print some errors. */
42 node *
43 parsestring(s, g, start, err_ret)
44 char *s;
45 grammar *g;
46 int start;
47 perrdetail *err_ret;
49 struct tok_state *tok;
51 err_ret->error = E_OK;
52 err_ret->filename = NULL;
53 err_ret->lineno = 0;
54 err_ret->offset = 0;
55 err_ret->text = NULL;
57 if ((tok = tok_setups(s)) == NULL) {
58 err_ret->error = E_NOMEM;
59 return NULL;
62 return parsetok(tok, g, start, err_ret);
66 /* Parse input coming from a file. Return error code, print some errors. */
68 node *
69 parsefile(fp, filename, g, start, ps1, ps2, err_ret)
70 FILE *fp;
71 char *filename;
72 grammar *g;
73 int start;
74 char *ps1, *ps2;
75 perrdetail *err_ret;
77 struct tok_state *tok;
79 err_ret->error = E_OK;
80 err_ret->filename = filename;
81 err_ret->lineno = 0;
82 err_ret->offset = 0;
83 err_ret->text = NULL;
85 if ((tok = tok_setupf(fp, ps1, ps2)) == NULL) {
86 err_ret->error = E_NOMEM;
87 return NULL;
90 #ifdef macintosh
92 int tabsize = guesstabsize(filename);
93 if (tabsize > 0)
94 tok->tabsize = tabsize;
96 #endif
98 return parsetok(tok, g, start, err_ret);
101 /* Parse input coming from the given tokenizer structure.
102 Return error code. */
104 static node *
105 parsetok(tok, g, start, err_ret)
106 struct tok_state *tok;
107 grammar *g;
108 int start;
109 perrdetail *err_ret;
111 parser_state *ps;
112 node *n;
113 int started = 0;
115 if ((ps = newparser(g, start)) == NULL) {
116 fprintf(stderr, "no mem for new parser\n");
117 err_ret->error = E_NOMEM;
118 return NULL;
121 for (;;) {
122 char *a, *b;
123 int type;
124 int len;
125 char *str;
127 type = tok_get(tok, &a, &b);
128 if (type == ERRORTOKEN) {
129 err_ret->error = tok->done;
130 break;
132 if (type == ENDMARKER && started) {
133 type = NEWLINE; /* Add an extra newline */
134 started = 0;
136 else
137 started = 1;
138 len = b - a;
139 str = NEW(char, len + 1);
140 if (str == NULL) {
141 fprintf(stderr, "no mem for next token\n");
142 err_ret->error = E_NOMEM;
143 break;
145 strncpy(str, a, len);
146 str[len] = '\0';
147 if ((err_ret->error =
148 addtoken(ps, (int)type, str, tok->lineno)) != E_OK)
149 break;
152 if (err_ret->error == E_DONE) {
153 n = ps->p_tree;
154 ps->p_tree = NULL;
156 else
157 n = NULL;
159 delparser(ps);
161 if (n == NULL) {
162 if (tok->lineno <= 1 && tok->done == E_EOF)
163 err_ret->error = E_EOF;
164 err_ret->lineno = tok->lineno;
165 err_ret->offset = tok->cur - tok->buf;
166 if (tok->buf != NULL) {
167 int len = tok->inp - tok->buf;
168 err_ret->text = malloc(len + 1);
169 if (err_ret->text != NULL) {
170 strncpy(err_ret->text, tok->buf, len+1);
171 err_ret->text[len] = '\0';
176 tok_free(tok);
178 return n;