Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Parser-tokenizer link implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | #include "tokenizer.h" |
| 29 | #include "node.h" |
| 30 | #include "grammar.h" |
| 31 | #include "parser.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | #include "parsetok.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | #include "errcode.h" |
| 34 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
| 36 | /* Forward */ |
| 37 | static 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 | |
| 42 | int |
| 43 | parsestring(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); |
Guido van Rossum | 326f582 | 1991-06-03 11:02:09 +0000 | [diff] [blame] | 57 | /* |
| 58 | XXX Need a more sophisticated way to report the line number. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | if (ret == E_TOKEN || ret == E_SYNTAX) { |
| 60 | fprintf(stderr, "String parsing error at line %d\n", |
| 61 | tok->lineno); |
| 62 | } |
Guido van Rossum | 326f582 | 1991-06-03 11:02:09 +0000 | [diff] [blame] | 63 | */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | tok_free(tok); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* Parse input coming from a file. Return error code, print some errors. */ |
| 70 | |
| 71 | int |
| 72 | parsefile(fp, filename, g, start, ps1, ps2, n_ret) |
| 73 | FILE *fp; |
| 74 | char *filename; |
| 75 | grammar *g; |
| 76 | int start; |
| 77 | char *ps1, *ps2; |
| 78 | node **n_ret; |
| 79 | { |
| 80 | struct tok_state *tok = tok_setupf(fp, ps1, ps2); |
| 81 | int ret; |
| 82 | |
| 83 | if (tok == NULL) { |
| 84 | fprintf(stderr, "no mem for tok_setupf\n"); |
| 85 | return E_NOMEM; |
| 86 | } |
Guido van Rossum | 91ece42 | 1992-03-25 22:32:00 +0000 | [diff] [blame] | 87 | #ifdef macintosh |
| 88 | { |
| 89 | int tabsize = guesstabsize(filename); |
| 90 | if (tabsize > 0) |
| 91 | tok->tabsize = tabsize; |
| 92 | } |
| 93 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | ret = parsetok(tok, g, start, n_ret); |
| 95 | if (ret == E_TOKEN || ret == E_SYNTAX) { |
| 96 | char *p; |
| 97 | fprintf(stderr, "Parsing error: file %s, line %d:\n", |
| 98 | filename, tok->lineno); |
Guido van Rossum | 56b07c8 | 1991-06-07 13:58:56 +0000 | [diff] [blame] | 99 | if (tok->buf == NULL) |
| 100 | fprintf(stderr, "(EOF)\n"); |
| 101 | else { |
| 102 | *tok->inp = '\0'; |
| 103 | if (tok->inp > tok->buf && tok->inp[-1] == '\n') |
| 104 | tok->inp[-1] = '\0'; |
| 105 | fprintf(stderr, "%s\n", tok->buf); |
| 106 | for (p = tok->buf; p < tok->cur; p++) { |
| 107 | if (*p == '\t') |
| 108 | putc('\t', stderr); |
| 109 | else |
| 110 | putc(' ', stderr); |
| 111 | } |
| 112 | fprintf(stderr, "^\n"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 113 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 114 | } |
| 115 | tok_free(tok); |
| 116 | return ret; |
| 117 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | /* Parse input coming from the given tokenizer structure. |
| 121 | Return error code. */ |
| 122 | |
| 123 | static int |
| 124 | parsetok(tok, g, start, n_ret) |
| 125 | struct tok_state *tok; |
| 126 | grammar *g; |
| 127 | int start; |
| 128 | node **n_ret; |
| 129 | { |
| 130 | parser_state *ps; |
| 131 | int ret; |
Guido van Rossum | d8b1d37 | 1992-03-04 16:40:44 +0000 | [diff] [blame] | 132 | int started = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | |
| 134 | if ((ps = newparser(g, start)) == NULL) { |
| 135 | fprintf(stderr, "no mem for new parser\n"); |
| 136 | return E_NOMEM; |
| 137 | } |
| 138 | |
| 139 | for (;;) { |
| 140 | char *a, *b; |
| 141 | int type; |
| 142 | int len; |
| 143 | char *str; |
| 144 | |
| 145 | type = tok_get(tok, &a, &b); |
| 146 | if (type == ERRORTOKEN) { |
| 147 | ret = tok->done; |
| 148 | break; |
| 149 | } |
Guido van Rossum | d8b1d37 | 1992-03-04 16:40:44 +0000 | [diff] [blame] | 150 | if (type == ENDMARKER && started) { |
| 151 | type = NEWLINE; /* Add an extra newline */ |
| 152 | started = 0; |
| 153 | } |
| 154 | else |
| 155 | started = 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | len = b - a; |
| 157 | str = NEW(char, len + 1); |
| 158 | if (str == NULL) { |
| 159 | fprintf(stderr, "no mem for next token\n"); |
| 160 | ret = E_NOMEM; |
| 161 | break; |
| 162 | } |
| 163 | strncpy(str, a, len); |
| 164 | str[len] = '\0'; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 165 | ret = addtoken(ps, (int)type, str, tok->lineno); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | if (ret != E_OK) { |
Guido van Rossum | 840bcf1 | 1990-11-18 17:39:41 +0000 | [diff] [blame] | 167 | if (ret == E_DONE) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | *n_ret = ps->p_tree; |
Guido van Rossum | 840bcf1 | 1990-11-18 17:39:41 +0000 | [diff] [blame] | 169 | ps->p_tree = NULL; |
| 170 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 171 | else { |
| 172 | *n_ret = NULL; |
| 173 | if (tok->lineno <= 1 && tok->done == E_EOF) |
| 174 | ret = E_EOF; |
| 175 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | delparser(ps); |
| 181 | return ret; |
| 182 | } |