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