Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | 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 | |
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); |
| 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 | |
| 68 | int |
| 69 | parsefile(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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | /* Parse input coming from the given tokenizer structure. |
| 107 | Return error code. */ |
| 108 | |
| 109 | static int |
| 110 | parsetok(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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 144 | ret = addtoken(ps, (int)type, str, tok->lineno); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | if (ret != E_OK) { |
Guido van Rossum | 840bcf1 | 1990-11-18 17:39:41 +0000 | [diff] [blame] | 146 | if (ret == E_DONE) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | *n_ret = ps->p_tree; |
Guido van Rossum | 840bcf1 | 1990-11-18 17:39:41 +0000 | [diff] [blame] | 148 | ps->p_tree = NULL; |
| 149 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | 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 | } |