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 | /* Tokenizer implementation */ |
| 3 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | #include "pgenheaders.h" |
| 6 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | #include "tokenizer.h" |
| 10 | #include "errcode.h" |
| 11 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 12 | extern char *PyOS_Readline(char *); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 13 | /* Return malloc'ed string including trailing \n; |
| 14 | empty malloc'ed string for EOF; |
| 15 | NULL if interrupted */ |
| 16 | |
Guido van Rossum | 4fe8729 | 1992-02-26 15:24:44 +0000 | [diff] [blame] | 17 | /* Don't ever change this -- it would break the portability of Python code */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | #define TABSIZE 8 |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 20 | /* Convert a possibly signed character to a nonnegative int */ |
| 21 | /* XXX This assumes characters are 8 bits wide */ |
| 22 | #ifdef __CHAR_UNSIGNED__ |
| 23 | #define Py_CHARMASK(c) (c) |
| 24 | #else |
| 25 | #define Py_CHARMASK(c) ((c) & 0xff) |
| 26 | #endif |
| 27 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 29 | static struct tok_state *tok_new(void); |
| 30 | static int tok_nextc(struct tok_state *tok); |
| 31 | static void tok_backup(struct tok_state *tok, int c); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | /* Token names */ |
| 34 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 35 | char *_PyParser_TokenNames[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | "ENDMARKER", |
| 37 | "NAME", |
| 38 | "NUMBER", |
| 39 | "STRING", |
| 40 | "NEWLINE", |
| 41 | "INDENT", |
| 42 | "DEDENT", |
| 43 | "LPAR", |
| 44 | "RPAR", |
| 45 | "LSQB", |
| 46 | "RSQB", |
| 47 | "COLON", |
| 48 | "COMMA", |
| 49 | "SEMI", |
| 50 | "PLUS", |
| 51 | "MINUS", |
| 52 | "STAR", |
| 53 | "SLASH", |
| 54 | "VBAR", |
| 55 | "AMPER", |
| 56 | "LESS", |
| 57 | "GREATER", |
| 58 | "EQUAL", |
| 59 | "DOT", |
| 60 | "PERCENT", |
| 61 | "BACKQUOTE", |
| 62 | "LBRACE", |
| 63 | "RBRACE", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 64 | "EQEQUAL", |
| 65 | "NOTEQUAL", |
| 66 | "LESSEQUAL", |
| 67 | "GREATEREQUAL", |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 68 | "TILDE", |
| 69 | "CIRCUMFLEX", |
| 70 | "LEFTSHIFT", |
| 71 | "RIGHTSHIFT", |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 72 | "DOUBLESTAR", |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 73 | "PLUSEQUAL", |
| 74 | "MINEQUAL", |
| 75 | "STAREQUAL", |
| 76 | "SLASHEQUAL", |
| 77 | "PERCENTEQUAL", |
| 78 | "AMPEREQUAL", |
| 79 | "VBAREQUAL", |
| 80 | "CIRCUMFLEXEQUAL", |
| 81 | "LEFTSHIFTEQUAL", |
| 82 | "RIGHTSHIFTEQUAL", |
| 83 | "DOUBLESTAREQUAL", |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 84 | "DOUBLESLASH", |
| 85 | "DOUBLESLASHEQUAL", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 86 | /* This table must match the #defines in token.h! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | "OP", |
| 88 | "<ERRORTOKEN>", |
| 89 | "<N_TOKENS>" |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | /* Create and initialize a new tok_state structure */ |
| 94 | |
| 95 | static struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 96 | tok_new(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 98 | struct tok_state *tok = PyMem_NEW(struct tok_state, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | if (tok == NULL) |
| 100 | return NULL; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 101 | tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | tok->done = E_OK; |
| 103 | tok->fp = NULL; |
| 104 | tok->tabsize = TABSIZE; |
| 105 | tok->indent = 0; |
| 106 | tok->indstack[0] = 0; |
| 107 | tok->atbol = 1; |
| 108 | tok->pendin = 0; |
| 109 | tok->prompt = tok->nextprompt = NULL; |
| 110 | tok->lineno = 0; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 111 | tok->level = 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 112 | tok->filename = NULL; |
| 113 | tok->altwarning = 0; |
| 114 | tok->alterror = 0; |
| 115 | tok->alttabsize = 1; |
| 116 | tok->altindstack[0] = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | return tok; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* Set up tokenizer for string */ |
| 122 | |
| 123 | struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 124 | PyTokenizer_FromString(char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | { |
| 126 | struct tok_state *tok = tok_new(); |
| 127 | if (tok == NULL) |
| 128 | return NULL; |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 129 | tok->buf = tok->cur = tok->end = tok->inp = str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | return tok; |
| 131 | } |
| 132 | |
| 133 | |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 134 | /* Set up tokenizer for file */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | |
| 136 | struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 137 | PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | { |
| 139 | struct tok_state *tok = tok_new(); |
| 140 | if (tok == NULL) |
| 141 | return NULL; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 142 | if ((tok->buf = PyMem_NEW(char, BUFSIZ)) == NULL) { |
| 143 | PyMem_DEL(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | tok->cur = tok->inp = tok->buf; |
| 147 | tok->end = tok->buf + BUFSIZ; |
| 148 | tok->fp = fp; |
| 149 | tok->prompt = ps1; |
| 150 | tok->nextprompt = ps2; |
| 151 | return tok; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* Free a tok_state structure */ |
| 156 | |
| 157 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 158 | PyTokenizer_Free(struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | if (tok->fp != NULL && tok->buf != NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 161 | PyMem_DEL(tok->buf); |
| 162 | PyMem_DEL(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | |
| 166 | /* Get next char, updating state; error code goes into tok->done */ |
| 167 | |
| 168 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 169 | tok_nextc(register struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | for (;;) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 172 | if (tok->cur != tok->inp) { |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 173 | return Py_CHARMASK(*tok->cur++); /* Fast path */ |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 174 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 175 | if (tok->done != E_OK) |
| 176 | return EOF; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | if (tok->fp == NULL) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 178 | char *end = strchr(tok->inp, '\n'); |
| 179 | if (end != NULL) |
| 180 | end++; |
| 181 | else { |
| 182 | end = strchr(tok->inp, '\0'); |
| 183 | if (end == tok->inp) { |
| 184 | tok->done = E_EOF; |
| 185 | return EOF; |
| 186 | } |
| 187 | } |
| 188 | if (tok->start == NULL) |
| 189 | tok->buf = tok->cur; |
| 190 | tok->lineno++; |
| 191 | tok->inp = end; |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 192 | return Py_CHARMASK(*tok->cur++); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | if (tok->prompt != NULL) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 195 | char *new = PyOS_Readline(tok->prompt); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | if (tok->nextprompt != NULL) |
| 197 | tok->prompt = tok->nextprompt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 198 | if (new == NULL) |
| 199 | tok->done = E_INTR; |
| 200 | else if (*new == '\0') { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 201 | PyMem_FREE(new); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | tok->done = E_EOF; |
| 203 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 204 | else if (tok->start != NULL) { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 205 | size_t start = tok->start - tok->buf; |
| 206 | size_t oldlen = tok->cur - tok->buf; |
| 207 | size_t newlen = oldlen + strlen(new); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 208 | char *buf = tok->buf; |
| 209 | PyMem_RESIZE(buf, char, newlen+1); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 210 | tok->lineno++; |
| 211 | if (buf == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 212 | PyMem_DEL(tok->buf); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 213 | tok->buf = NULL; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 214 | PyMem_FREE(new); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 215 | tok->done = E_NOMEM; |
| 216 | return EOF; |
| 217 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 218 | tok->buf = buf; |
| 219 | tok->cur = tok->buf + oldlen; |
| 220 | strcpy(tok->buf + oldlen, new); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 221 | PyMem_FREE(new); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 222 | tok->inp = tok->buf + newlen; |
| 223 | tok->end = tok->inp + 1; |
| 224 | tok->start = tok->buf + start; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 225 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 226 | else { |
| 227 | tok->lineno++; |
| 228 | if (tok->buf != NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 229 | PyMem_DEL(tok->buf); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 230 | tok->buf = new; |
| 231 | tok->cur = tok->buf; |
| 232 | tok->inp = strchr(tok->buf, '\0'); |
| 233 | tok->end = tok->inp + 1; |
| 234 | } |
| 235 | } |
| 236 | else { |
| 237 | int done = 0; |
| 238 | int cur = 0; |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 239 | char *pt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 240 | if (tok->start == NULL) { |
| 241 | if (tok->buf == NULL) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 242 | tok->buf = PyMem_NEW(char, BUFSIZ); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 243 | if (tok->buf == NULL) { |
| 244 | tok->done = E_NOMEM; |
| 245 | return EOF; |
| 246 | } |
| 247 | tok->end = tok->buf + BUFSIZ; |
| 248 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 249 | if (Py_UniversalNewlineFgets(tok->buf, (int)(tok->end - tok->buf), |
| 250 | tok->fp, NULL) == NULL) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 251 | tok->done = E_EOF; |
| 252 | done = 1; |
| 253 | } |
| 254 | else { |
| 255 | tok->done = E_OK; |
| 256 | tok->inp = strchr(tok->buf, '\0'); |
| 257 | done = tok->inp[-1] == '\n'; |
| 258 | } |
| 259 | } |
| 260 | else { |
| 261 | cur = tok->cur - tok->buf; |
Guido van Rossum | 78c0535 | 1995-01-17 16:12:13 +0000 | [diff] [blame] | 262 | if (feof(tok->fp)) { |
| 263 | tok->done = E_EOF; |
| 264 | done = 1; |
| 265 | } |
| 266 | else |
| 267 | tok->done = E_OK; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 268 | } |
| 269 | tok->lineno++; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 270 | /* Read until '\n' or EOF */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 271 | while (!done) { |
| 272 | int curstart = tok->start == NULL ? -1 : |
| 273 | tok->start - tok->buf; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 274 | int curvalid = tok->inp - tok->buf; |
Guido van Rossum | 3f6bb86 | 1995-09-21 20:36:34 +0000 | [diff] [blame] | 275 | int newsize = curvalid + BUFSIZ; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 276 | char *newbuf = tok->buf; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 277 | PyMem_RESIZE(newbuf, char, newsize); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 278 | if (newbuf == NULL) { |
| 279 | tok->done = E_NOMEM; |
| 280 | tok->cur = tok->inp; |
| 281 | return EOF; |
| 282 | } |
| 283 | tok->buf = newbuf; |
| 284 | tok->inp = tok->buf + curvalid; |
| 285 | tok->end = tok->buf + newsize; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 286 | tok->start = curstart < 0 ? NULL : |
| 287 | tok->buf + curstart; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 288 | if (Py_UniversalNewlineFgets(tok->inp, |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 289 | (int)(tok->end - tok->inp), |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 290 | tok->fp, NULL) == NULL) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 291 | /* Last line does not end in \n, |
| 292 | fake one */ |
| 293 | strcpy(tok->inp, "\n"); |
| 294 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 295 | tok->inp = strchr(tok->inp, '\0'); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 296 | done = tok->inp[-1] == '\n'; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 297 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 298 | tok->cur = tok->buf + cur; |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 299 | #ifndef macintosh |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 300 | /* replace "\r\n" with "\n" */ |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 301 | /* For Mac we leave the \r, giving a syntax error */ |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 302 | pt = tok->inp - 2; |
| 303 | if (pt >= tok->buf && *pt == '\r') { |
| 304 | *pt++ = '\n'; |
| 305 | *pt = '\0'; |
| 306 | tok->inp = pt; |
| 307 | } |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 308 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 309 | } |
| 310 | if (tok->done != E_OK) { |
| 311 | if (tok->prompt != NULL) |
Guido van Rossum | 6e73bf4 | 1998-08-25 18:13:04 +0000 | [diff] [blame] | 312 | PySys_WriteStderr("\n"); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 313 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | return EOF; |
| 315 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 317 | /*NOTREACHED*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
| 321 | /* Back-up one character */ |
| 322 | |
| 323 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 324 | tok_backup(register struct tok_state *tok, register int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | { |
| 326 | if (c != EOF) { |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 327 | if (--tok->cur < tok->buf) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 328 | Py_FatalError("tok_backup: begin of buffer"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | if (*tok->cur != c) |
| 330 | *tok->cur = c; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* Return the token corresponding to a single character */ |
| 336 | |
| 337 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 338 | PyToken_OneChar(int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 339 | { |
| 340 | switch (c) { |
| 341 | case '(': return LPAR; |
| 342 | case ')': return RPAR; |
| 343 | case '[': return LSQB; |
| 344 | case ']': return RSQB; |
| 345 | case ':': return COLON; |
| 346 | case ',': return COMMA; |
| 347 | case ';': return SEMI; |
| 348 | case '+': return PLUS; |
| 349 | case '-': return MINUS; |
| 350 | case '*': return STAR; |
| 351 | case '/': return SLASH; |
| 352 | case '|': return VBAR; |
| 353 | case '&': return AMPER; |
| 354 | case '<': return LESS; |
| 355 | case '>': return GREATER; |
| 356 | case '=': return EQUAL; |
| 357 | case '.': return DOT; |
| 358 | case '%': return PERCENT; |
| 359 | case '`': return BACKQUOTE; |
| 360 | case '{': return LBRACE; |
| 361 | case '}': return RBRACE; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 362 | case '^': return CIRCUMFLEX; |
| 363 | case '~': return TILDE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 364 | default: return OP; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 369 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 370 | PyToken_TwoChars(int c1, int c2) |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 371 | { |
| 372 | switch (c1) { |
| 373 | case '=': |
| 374 | switch (c2) { |
| 375 | case '=': return EQEQUAL; |
| 376 | } |
| 377 | break; |
| 378 | case '!': |
| 379 | switch (c2) { |
| 380 | case '=': return NOTEQUAL; |
| 381 | } |
| 382 | break; |
| 383 | case '<': |
| 384 | switch (c2) { |
| 385 | case '>': return NOTEQUAL; |
| 386 | case '=': return LESSEQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 387 | case '<': return LEFTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 388 | } |
| 389 | break; |
| 390 | case '>': |
| 391 | switch (c2) { |
| 392 | case '=': return GREATEREQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 393 | case '>': return RIGHTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 394 | } |
| 395 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 396 | case '+': |
| 397 | switch (c2) { |
| 398 | case '=': return PLUSEQUAL; |
| 399 | } |
| 400 | break; |
| 401 | case '-': |
| 402 | switch (c2) { |
| 403 | case '=': return MINEQUAL; |
| 404 | } |
| 405 | break; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 406 | case '*': |
| 407 | switch (c2) { |
| 408 | case '*': return DOUBLESTAR; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 409 | case '=': return STAREQUAL; |
| 410 | } |
| 411 | break; |
| 412 | case '/': |
| 413 | switch (c2) { |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 414 | case '/': return DOUBLESLASH; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 415 | case '=': return SLASHEQUAL; |
| 416 | } |
| 417 | break; |
| 418 | case '|': |
| 419 | switch (c2) { |
| 420 | case '=': return VBAREQUAL; |
| 421 | } |
| 422 | break; |
| 423 | case '%': |
| 424 | switch (c2) { |
| 425 | case '=': return PERCENTEQUAL; |
| 426 | } |
| 427 | break; |
| 428 | case '&': |
| 429 | switch (c2) { |
| 430 | case '=': return AMPEREQUAL; |
| 431 | } |
| 432 | break; |
| 433 | case '^': |
| 434 | switch (c2) { |
| 435 | case '=': return CIRCUMFLEXEQUAL; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 436 | } |
| 437 | break; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 438 | } |
| 439 | return OP; |
| 440 | } |
| 441 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 442 | int |
| 443 | PyToken_ThreeChars(int c1, int c2, int c3) |
| 444 | { |
| 445 | switch (c1) { |
| 446 | case '<': |
| 447 | switch (c2) { |
| 448 | case '<': |
| 449 | switch (c3) { |
| 450 | case '=': |
| 451 | return LEFTSHIFTEQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 452 | } |
| 453 | break; |
| 454 | } |
| 455 | break; |
| 456 | case '>': |
| 457 | switch (c2) { |
| 458 | case '>': |
| 459 | switch (c3) { |
| 460 | case '=': |
| 461 | return RIGHTSHIFTEQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 462 | } |
| 463 | break; |
| 464 | } |
| 465 | break; |
| 466 | case '*': |
| 467 | switch (c2) { |
| 468 | case '*': |
| 469 | switch (c3) { |
| 470 | case '=': |
| 471 | return DOUBLESTAREQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 472 | } |
| 473 | break; |
| 474 | } |
| 475 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 476 | case '/': |
| 477 | switch (c2) { |
| 478 | case '/': |
| 479 | switch (c3) { |
| 480 | case '=': |
| 481 | return DOUBLESLASHEQUAL; |
| 482 | } |
| 483 | break; |
| 484 | } |
| 485 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 486 | } |
| 487 | return OP; |
| 488 | } |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 489 | |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 490 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 491 | indenterror(struct tok_state *tok) |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 492 | { |
| 493 | if (tok->alterror) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 494 | tok->done = E_TABSPACE; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 495 | tok->cur = tok->inp; |
| 496 | return 1; |
| 497 | } |
| 498 | if (tok->altwarning) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 499 | PySys_WriteStderr("%s: inconsistent use of tabs and spaces " |
| 500 | "in indentation\n", tok->filename); |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 501 | tok->altwarning = 0; |
| 502 | } |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | /* Get next token, after space stripping etc. */ |
| 508 | |
| 509 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 510 | PyTokenizer_Get(register struct tok_state *tok, char **p_start, |
| 511 | char **p_end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 512 | { |
| 513 | register int c; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 514 | int blankline; |
| 515 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 516 | *p_start = *p_end = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 517 | nextline: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 518 | tok->start = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 519 | blankline = 0; |
| 520 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | /* Get indentation level */ |
| 522 | if (tok->atbol) { |
| 523 | register int col = 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 524 | register int altcol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 525 | tok->atbol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 526 | for (;;) { |
| 527 | c = tok_nextc(tok); |
| 528 | if (c == ' ') |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 529 | col++, altcol++; |
| 530 | else if (c == '\t') { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 531 | col = (col/tok->tabsize + 1) * tok->tabsize; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 532 | altcol = (altcol/tok->alttabsize + 1) |
| 533 | * tok->alttabsize; |
| 534 | } |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 535 | else if (c == '\014') /* Control-L (formfeed) */ |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 536 | col = altcol = 0; /* For Emacs users */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | else |
| 538 | break; |
| 539 | } |
| 540 | tok_backup(tok, c); |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 541 | if (c == '#' || c == '\n') { |
| 542 | /* Lines with only whitespace and/or comments |
| 543 | shouldn't affect the indentation and are |
| 544 | not passed to the parser as NEWLINE tokens, |
| 545 | except *totally* empty lines in interactive |
| 546 | mode, which signal the end of a command group. */ |
| 547 | if (col == 0 && c == '\n' && tok->prompt != NULL) |
| 548 | blankline = 0; /* Let it through */ |
| 549 | else |
| 550 | blankline = 1; /* Ignore completely */ |
| 551 | /* We can't jump back right here since we still |
| 552 | may need to skip to the end of a comment */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | } |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 554 | if (!blankline && tok->level == 0) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 555 | if (col == tok->indstack[tok->indent]) { |
| 556 | /* No change */ |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 557 | if (altcol != tok->altindstack[tok->indent]) { |
| 558 | if (indenterror(tok)) |
| 559 | return ERRORTOKEN; |
| 560 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 561 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 562 | else if (col > tok->indstack[tok->indent]) { |
| 563 | /* Indent -- always one */ |
| 564 | if (tok->indent+1 >= MAXINDENT) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 565 | tok->done = E_TOODEEP; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 566 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 567 | return ERRORTOKEN; |
| 568 | } |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 569 | if (altcol <= tok->altindstack[tok->indent]) { |
| 570 | if (indenterror(tok)) |
| 571 | return ERRORTOKEN; |
| 572 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 573 | tok->pendin++; |
| 574 | tok->indstack[++tok->indent] = col; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 575 | tok->altindstack[tok->indent] = altcol; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 576 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 577 | else /* col < tok->indstack[tok->indent] */ { |
| 578 | /* Dedent -- any number, must be consistent */ |
| 579 | while (tok->indent > 0 && |
| 580 | col < tok->indstack[tok->indent]) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 581 | tok->pendin--; |
Guido van Rossum | 54758fa | 1998-02-16 22:18:00 +0000 | [diff] [blame] | 582 | tok->indent--; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 583 | } |
| 584 | if (col != tok->indstack[tok->indent]) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 585 | tok->done = E_DEDENT; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 586 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 587 | return ERRORTOKEN; |
| 588 | } |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 589 | if (altcol != tok->altindstack[tok->indent]) { |
| 590 | if (indenterror(tok)) |
| 591 | return ERRORTOKEN; |
| 592 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 597 | tok->start = tok->cur; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 598 | |
| 599 | /* Return pending indents/dedents */ |
| 600 | if (tok->pendin != 0) { |
| 601 | if (tok->pendin < 0) { |
| 602 | tok->pendin++; |
| 603 | return DEDENT; |
| 604 | } |
| 605 | else { |
| 606 | tok->pendin--; |
| 607 | return INDENT; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | again: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 612 | tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 613 | /* Skip spaces */ |
| 614 | do { |
| 615 | c = tok_nextc(tok); |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 616 | } while (c == ' ' || c == '\t' || c == '\014'); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | |
| 618 | /* Set start of current token */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 619 | tok->start = tok->cur - 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 621 | /* Skip comment, while looking for tab-setting magic */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 622 | if (c == '#') { |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 623 | static char *tabforms[] = { |
| 624 | "tab-width:", /* Emacs */ |
| 625 | ":tabstop=", /* vim, full form */ |
| 626 | ":ts=", /* vim, abbreviated form */ |
| 627 | "set tabsize=", /* will vi never die? */ |
| 628 | /* more templates can be added here to support other editors */ |
| 629 | }; |
| 630 | char cbuf[80]; |
| 631 | char *tp, **cp; |
| 632 | tp = cbuf; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 633 | do { |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 634 | *tp++ = c = tok_nextc(tok); |
| 635 | } while (c != EOF && c != '\n' && |
| 636 | tp - cbuf + 1 < sizeof(cbuf)); |
| 637 | *tp = '\0'; |
| 638 | for (cp = tabforms; |
| 639 | cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]); |
| 640 | cp++) { |
| 641 | if ((tp = strstr(cbuf, *cp))) { |
| 642 | int newsize = atoi(tp + strlen(*cp)); |
| 643 | |
| 644 | if (newsize >= 1 && newsize <= 40) { |
| 645 | tok->tabsize = newsize; |
Guido van Rossum | 6c981ad | 2000-04-03 23:02:17 +0000 | [diff] [blame] | 646 | if (Py_VerboseFlag) |
| 647 | PySys_WriteStderr( |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 648 | "Tab size set to %d\n", |
| 649 | newsize); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | while (c != EOF && c != '\n') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 654 | c = tok_nextc(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* Check for EOF and errors now */ |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 658 | if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 659 | return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 660 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 661 | |
| 662 | /* Identifier (most frequent token!) */ |
| 663 | if (isalpha(c) || c == '_') { |
Guido van Rossum | 86016cb | 2000-03-10 22:56:54 +0000 | [diff] [blame] | 664 | /* Process r"", u"" and ur"" */ |
Guido van Rossum | 5026cb4 | 1997-04-25 17:32:00 +0000 | [diff] [blame] | 665 | switch (c) { |
| 666 | case 'r': |
| 667 | case 'R': |
| 668 | c = tok_nextc(tok); |
| 669 | if (c == '"' || c == '\'') |
| 670 | goto letter_quote; |
Guido van Rossum | 86016cb | 2000-03-10 22:56:54 +0000 | [diff] [blame] | 671 | break; |
| 672 | case 'u': |
| 673 | case 'U': |
| 674 | c = tok_nextc(tok); |
| 675 | if (c == 'r' || c == 'R') |
| 676 | c = tok_nextc(tok); |
| 677 | if (c == '"' || c == '\'') |
| 678 | goto letter_quote; |
| 679 | break; |
Guido van Rossum | 5026cb4 | 1997-04-25 17:32:00 +0000 | [diff] [blame] | 680 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 681 | while (isalnum(c) || c == '_') { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 682 | c = tok_nextc(tok); |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 683 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 684 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 685 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 686 | *p_end = tok->cur; |
| 687 | return NAME; |
| 688 | } |
| 689 | |
| 690 | /* Newline */ |
| 691 | if (c == '\n') { |
| 692 | tok->atbol = 1; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 693 | if (blankline || tok->level > 0) |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 694 | goto nextline; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 695 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 696 | *p_end = tok->cur - 1; /* Leave '\n' out of the string */ |
| 697 | return NEWLINE; |
| 698 | } |
| 699 | |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 700 | #ifdef macintosh |
| 701 | if (c == '\r') { |
Guido van Rossum | 6e73bf4 | 1998-08-25 18:13:04 +0000 | [diff] [blame] | 702 | PySys_WriteStderr( |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 703 | "File contains \\r characters (incorrect line endings?)\n"); |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 704 | tok->done = E_TOKEN; |
| 705 | tok->cur = tok->inp; |
| 706 | return ERRORTOKEN; |
| 707 | } |
| 708 | #endif |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 709 | /* Period or number starting with period? */ |
| 710 | if (c == '.') { |
| 711 | c = tok_nextc(tok); |
| 712 | if (isdigit(c)) { |
| 713 | goto fraction; |
| 714 | } |
| 715 | else { |
| 716 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 717 | *p_start = tok->start; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 718 | *p_end = tok->cur; |
| 719 | return DOT; |
| 720 | } |
| 721 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 723 | /* Number */ |
| 724 | if (isdigit(c)) { |
| 725 | if (c == '0') { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 726 | /* Hex or octal -- maybe. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 727 | c = tok_nextc(tok); |
| 728 | if (c == '.') |
| 729 | goto fraction; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 730 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 731 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 732 | goto imaginary; |
| 733 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 734 | if (c == 'x' || c == 'X') { |
| 735 | /* Hex */ |
| 736 | do { |
| 737 | c = tok_nextc(tok); |
| 738 | } while (isxdigit(c)); |
| 739 | } |
| 740 | else { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 741 | int found_decimal = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 742 | /* Octal; c is first char of it */ |
| 743 | /* There's no 'isoctdigit' macro, sigh */ |
| 744 | while ('0' <= c && c < '8') { |
| 745 | c = tok_nextc(tok); |
| 746 | } |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 747 | if (isdigit(c)) { |
| 748 | found_decimal = 1; |
| 749 | do { |
| 750 | c = tok_nextc(tok); |
| 751 | } while (isdigit(c)); |
| 752 | } |
| 753 | if (c == '.') |
| 754 | goto fraction; |
| 755 | else if (c == 'e' || c == 'E') |
| 756 | goto exponent; |
| 757 | #ifndef WITHOUT_COMPLEX |
| 758 | else if (c == 'j' || c == 'J') |
| 759 | goto imaginary; |
| 760 | #endif |
| 761 | else if (found_decimal) { |
| 762 | tok->done = E_TOKEN; |
| 763 | tok_backup(tok, c); |
| 764 | return ERRORTOKEN; |
| 765 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 766 | } |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 767 | if (c == 'l' || c == 'L') |
| 768 | c = tok_nextc(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 769 | } |
| 770 | else { |
| 771 | /* Decimal */ |
| 772 | do { |
| 773 | c = tok_nextc(tok); |
| 774 | } while (isdigit(c)); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 775 | if (c == 'l' || c == 'L') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 776 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 777 | else { |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 778 | /* Accept floating point numbers. */ |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 779 | if (c == '.') { |
| 780 | fraction: |
| 781 | /* Fraction */ |
| 782 | do { |
| 783 | c = tok_nextc(tok); |
| 784 | } while (isdigit(c)); |
| 785 | } |
| 786 | if (c == 'e' || c == 'E') { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 787 | exponent: |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 788 | /* Exponent part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 789 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 790 | if (c == '+' || c == '-') |
| 791 | c = tok_nextc(tok); |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 792 | if (!isdigit(c)) { |
| 793 | tok->done = E_TOKEN; |
| 794 | tok_backup(tok, c); |
| 795 | return ERRORTOKEN; |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 796 | } |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 797 | do { |
| 798 | c = tok_nextc(tok); |
| 799 | } while (isdigit(c)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 801 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 802 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 803 | /* Imaginary part */ |
| 804 | imaginary: |
| 805 | c = tok_nextc(tok); |
| 806 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 810 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 811 | *p_end = tok->cur; |
| 812 | return NUMBER; |
| 813 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 814 | |
| 815 | letter_quote: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 816 | /* String */ |
| 817 | if (c == '\'' || c == '"') { |
Guido van Rossum | 3568524 | 1998-02-16 15:42:50 +0000 | [diff] [blame] | 818 | int quote2 = tok->cur - tok->start + 1; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 819 | int quote = c; |
| 820 | int triple = 0; |
| 821 | int tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 822 | for (;;) { |
| 823 | c = tok_nextc(tok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 824 | if (c == '\n') { |
| 825 | if (!triple) { |
| 826 | tok->done = E_TOKEN; |
| 827 | tok_backup(tok, c); |
| 828 | return ERRORTOKEN; |
| 829 | } |
| 830 | tripcount = 0; |
| 831 | } |
| 832 | else if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 833 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 834 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 835 | return ERRORTOKEN; |
| 836 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 837 | else if (c == quote) { |
| 838 | tripcount++; |
Guido van Rossum | 3568524 | 1998-02-16 15:42:50 +0000 | [diff] [blame] | 839 | if (tok->cur - tok->start == quote2) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 840 | c = tok_nextc(tok); |
| 841 | if (c == quote) { |
| 842 | triple = 1; |
| 843 | tripcount = 0; |
| 844 | continue; |
| 845 | } |
| 846 | tok_backup(tok, c); |
| 847 | } |
| 848 | if (!triple || tripcount == 3) |
| 849 | break; |
| 850 | } |
| 851 | else if (c == '\\') { |
| 852 | tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 853 | c = tok_nextc(tok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 854 | if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 855 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 856 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 857 | return ERRORTOKEN; |
| 858 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 859 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 860 | else |
| 861 | tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 862 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 863 | *p_start = tok->start; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 864 | *p_end = tok->cur; |
| 865 | return STRING; |
| 866 | } |
| 867 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 868 | /* Line continuation */ |
| 869 | if (c == '\\') { |
| 870 | c = tok_nextc(tok); |
| 871 | if (c != '\n') { |
| 872 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 873 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 874 | return ERRORTOKEN; |
| 875 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 876 | goto again; /* Read next line */ |
| 877 | } |
| 878 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 879 | /* Check for two-character token */ |
| 880 | { |
| 881 | int c2 = tok_nextc(tok); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 882 | int token = PyToken_TwoChars(c, c2); |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 883 | if (token != OP) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 884 | int c3 = tok_nextc(tok); |
| 885 | int token3 = PyToken_ThreeChars(c, c2, c3); |
| 886 | if (token3 != OP) { |
| 887 | token = token3; |
| 888 | } else { |
| 889 | tok_backup(tok, c3); |
| 890 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 891 | *p_start = tok->start; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 892 | *p_end = tok->cur; |
| 893 | return token; |
| 894 | } |
| 895 | tok_backup(tok, c2); |
| 896 | } |
| 897 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 898 | /* Keep track of parentheses nesting level */ |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 899 | switch (c) { |
| 900 | case '(': |
| 901 | case '[': |
| 902 | case '{': |
| 903 | tok->level++; |
| 904 | break; |
| 905 | case ')': |
| 906 | case ']': |
| 907 | case '}': |
| 908 | tok->level--; |
| 909 | break; |
| 910 | } |
| 911 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 912 | /* Punctuation character */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 913 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 914 | *p_end = tok->cur; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 915 | return PyToken_OneChar(c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 919 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 920 | |
| 921 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 922 | tok_dump(int type, char *start, char *end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 923 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 924 | printf("%s", _PyParser_TokenNames[type]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 925 | if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 926 | printf("(%.*s)", (int)(end - start), start); |
| 927 | } |
| 928 | |
| 929 | #endif |