Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Parser-tokenizer link implementation */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | #include "tokenizer.h" |
| 6 | #include "node.h" |
| 7 | #include "grammar.h" |
| 8 | #include "parser.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | #include "parsetok.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | #include "errcode.h" |
| 11 | |
Guido van Rossum | 6135df6 | 1998-04-10 19:35:06 +0000 | [diff] [blame] | 12 | int Py_TabcheckFlag; |
| 13 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
| 15 | /* Forward */ |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 16 | static node *parsetok(struct tok_state *, grammar *, int, perrdetail *, int); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | |
| 18 | /* Parse input coming from a string. Return error code, print some errors. */ |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 19 | node * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 20 | PyParser_ParseString(char *s, grammar *g, int start, perrdetail *err_ret) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | { |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 22 | return PyParser_ParseStringFlags(s, g, start, err_ret, 0); |
| 23 | } |
| 24 | |
| 25 | node * |
| 26 | PyParser_ParseStringFlags(char *s, grammar *g, int start, |
| 27 | perrdetail *err_ret, int flags) |
| 28 | { |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 29 | struct tok_state *tok; |
| 30 | |
| 31 | err_ret->error = E_OK; |
| 32 | err_ret->filename = NULL; |
| 33 | err_ret->lineno = 0; |
| 34 | err_ret->offset = 0; |
| 35 | err_ret->text = NULL; |
Barry Warsaw | 38aa14a | 2000-08-18 05:04:08 +0000 | [diff] [blame] | 36 | err_ret->token = -1; |
| 37 | err_ret->expected = -1; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 39 | if ((tok = PyTokenizer_FromString(s)) == NULL) { |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 40 | err_ret->error = E_NOMEM; |
| 41 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 89ce454 | 1998-12-21 18:32:40 +0000 | [diff] [blame] | 44 | if (Py_TabcheckFlag || Py_VerboseFlag) { |
| 45 | tok->filename = "<string>"; |
| 46 | tok->altwarning = (tok->filename != NULL); |
| 47 | if (Py_TabcheckFlag >= 2) |
| 48 | tok->alterror++; |
| 49 | } |
| 50 | |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 51 | return parsetok(tok, g, start, err_ret, flags); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | /* Parse input coming from a file. Return error code, print some errors. */ |
| 56 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 57 | node * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 58 | PyParser_ParseFile(FILE *fp, char *filename, grammar *g, int start, |
| 59 | char *ps1, char *ps2, perrdetail *err_ret) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 60 | { |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 61 | return PyParser_ParseFileFlags(fp, filename, g, start, ps1, ps2, |
| 62 | err_ret, 0); |
| 63 | } |
| 64 | |
| 65 | node * |
| 66 | PyParser_ParseFileFlags(FILE *fp, char *filename, grammar *g, int start, |
| 67 | char *ps1, char *ps2, perrdetail *err_ret, int flags) |
| 68 | { |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 69 | struct tok_state *tok; |
| 70 | |
| 71 | err_ret->error = E_OK; |
| 72 | err_ret->filename = filename; |
| 73 | err_ret->lineno = 0; |
| 74 | err_ret->offset = 0; |
| 75 | err_ret->text = NULL; |
| 76 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 77 | if ((tok = PyTokenizer_FromFile(fp, ps1, ps2)) == NULL) { |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 78 | err_ret->error = E_NOMEM; |
| 79 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 80 | } |
Guido van Rossum | 6135df6 | 1998-04-10 19:35:06 +0000 | [diff] [blame] | 81 | if (Py_TabcheckFlag || Py_VerboseFlag) { |
| 82 | tok->filename = filename; |
| 83 | tok->altwarning = (filename != NULL); |
| 84 | if (Py_TabcheckFlag >= 2) |
| 85 | tok->alterror++; |
| 86 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 89 | return parsetok(tok, g, start, err_ret, flags); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 90 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | |
| 92 | /* Parse input coming from the given tokenizer structure. |
| 93 | Return error code. */ |
| 94 | |
Guido van Rossum | da62ecc | 2001-07-17 16:53:11 +0000 | [diff] [blame] | 95 | static char yield_msg[] = |
| 96 | "%s:%d: Warning: 'yield' will become a reserved keyword in the future\n"; |
| 97 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 98 | static node * |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 99 | parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, |
| 100 | int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | { |
| 102 | parser_state *ps; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 103 | node *n; |
Guido van Rossum | d8b1d37 | 1992-03-04 16:40:44 +0000 | [diff] [blame] | 104 | int started = 0; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 106 | if ((ps = PyParser_New(g, start)) == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | fprintf(stderr, "no mem for new parser\n"); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 108 | err_ret->error = E_NOMEM; |
| 109 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | } |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 111 | if (flags & PyPARSE_YIELD_IS_KEYWORD) |
| 112 | ps->p_generators = 1; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | for (;;) { |
| 115 | char *a, *b; |
| 116 | int type; |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 117 | size_t len; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | char *str; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 120 | type = PyTokenizer_Get(tok, &a, &b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | if (type == ERRORTOKEN) { |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 122 | err_ret->error = tok->done; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | break; |
| 124 | } |
Guido van Rossum | d8b1d37 | 1992-03-04 16:40:44 +0000 | [diff] [blame] | 125 | if (type == ENDMARKER && started) { |
| 126 | type = NEWLINE; /* Add an extra newline */ |
| 127 | started = 0; |
| 128 | } |
| 129 | else |
| 130 | started = 1; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 131 | len = b - a; /* XXX this may compute NULL - NULL */ |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 132 | str = PyMem_NEW(char, len + 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | if (str == NULL) { |
| 134 | fprintf(stderr, "no mem for next token\n"); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 135 | err_ret->error = E_NOMEM; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | break; |
| 137 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 138 | if (len > 0) |
| 139 | strncpy(str, a, len); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | str[len] = '\0'; |
Guido van Rossum | da62ecc | 2001-07-17 16:53:11 +0000 | [diff] [blame] | 141 | |
| 142 | /* Warn about yield as NAME */ |
| 143 | if (type == NAME && !ps->p_generators && |
| 144 | len == 5 && str[0] == 'y' && strcmp(str, "yield") == 0) |
| 145 | PySys_WriteStderr(yield_msg, |
| 146 | err_ret->filename==NULL ? |
| 147 | "<string>" : err_ret->filename, |
| 148 | tok->lineno); |
| 149 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 150 | if ((err_ret->error = |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 151 | PyParser_AddToken(ps, (int)type, str, tok->lineno, |
| 152 | &(err_ret->expected))) != E_OK) { |
Guido van Rossum | ff0ec52 | 1997-07-27 01:52:50 +0000 | [diff] [blame] | 153 | if (err_ret->error != E_DONE) |
| 154 | PyMem_DEL(str); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | break; |
Guido van Rossum | ff0ec52 | 1997-07-27 01:52:50 +0000 | [diff] [blame] | 156 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (err_ret->error == E_DONE) { |
| 160 | n = ps->p_tree; |
| 161 | ps->p_tree = NULL; |
| 162 | } |
| 163 | else |
| 164 | n = NULL; |
| 165 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 166 | PyParser_Delete(ps); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 167 | |
| 168 | if (n == NULL) { |
| 169 | if (tok->lineno <= 1 && tok->done == E_EOF) |
| 170 | err_ret->error = E_EOF; |
| 171 | err_ret->lineno = tok->lineno; |
| 172 | err_ret->offset = tok->cur - tok->buf; |
| 173 | if (tok->buf != NULL) { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 174 | size_t len = tok->inp - tok->buf; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 175 | err_ret->text = PyMem_NEW(char, len + 1); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 176 | if (err_ret->text != NULL) { |
Guido van Rossum | ec49827 | 1995-01-20 16:59:12 +0000 | [diff] [blame] | 177 | if (len > 0) |
| 178 | strncpy(err_ret->text, tok->buf, len); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 179 | err_ret->text[len] = '\0'; |
| 180 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 184 | PyTokenizer_Free(tok); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 185 | |
| 186 | return n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | } |