Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b9f8d6e | 1995-01-04 19:08:09 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Tokenizer implementation */ |
| 33 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | #include "pgenheaders.h" |
| 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include <ctype.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | #include "tokenizer.h" |
| 39 | #include "errcode.h" |
| 40 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 41 | extern char *PyOS_Readline Py_PROTO((char *)); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 42 | /* Return malloc'ed string including trailing \n; |
| 43 | empty malloc'ed string for EOF; |
| 44 | NULL if interrupted */ |
| 45 | |
Guido van Rossum | 4fe8729 | 1992-02-26 15:24:44 +0000 | [diff] [blame] | 46 | /* 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] | 47 | #define TABSIZE 8 |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | /* Forward */ |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 50 | static struct tok_state *tok_new Py_PROTO((void)); |
| 51 | static int tok_nextc Py_PROTO((struct tok_state *tok)); |
| 52 | static void tok_backup Py_PROTO((struct tok_state *tok, int c)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | /* Token names */ |
| 55 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 56 | char *_PyParser_TokenNames[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | "ENDMARKER", |
| 58 | "NAME", |
| 59 | "NUMBER", |
| 60 | "STRING", |
| 61 | "NEWLINE", |
| 62 | "INDENT", |
| 63 | "DEDENT", |
| 64 | "LPAR", |
| 65 | "RPAR", |
| 66 | "LSQB", |
| 67 | "RSQB", |
| 68 | "COLON", |
| 69 | "COMMA", |
| 70 | "SEMI", |
| 71 | "PLUS", |
| 72 | "MINUS", |
| 73 | "STAR", |
| 74 | "SLASH", |
| 75 | "VBAR", |
| 76 | "AMPER", |
| 77 | "LESS", |
| 78 | "GREATER", |
| 79 | "EQUAL", |
| 80 | "DOT", |
| 81 | "PERCENT", |
| 82 | "BACKQUOTE", |
| 83 | "LBRACE", |
| 84 | "RBRACE", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 85 | "EQEQUAL", |
| 86 | "NOTEQUAL", |
| 87 | "LESSEQUAL", |
| 88 | "GREATEREQUAL", |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 89 | "TILDE", |
| 90 | "CIRCUMFLEX", |
| 91 | "LEFTSHIFT", |
| 92 | "RIGHTSHIFT", |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 93 | "DOUBLESTAR", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 94 | /* This table must match the #defines in token.h! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | "OP", |
| 96 | "<ERRORTOKEN>", |
| 97 | "<N_TOKENS>" |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | /* Create and initialize a new tok_state structure */ |
| 102 | |
| 103 | static struct tok_state * |
| 104 | tok_new() |
| 105 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 106 | struct tok_state *tok = PyMem_NEW(struct tok_state, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | if (tok == NULL) |
| 108 | return NULL; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 109 | tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | tok->done = E_OK; |
| 111 | tok->fp = NULL; |
| 112 | tok->tabsize = TABSIZE; |
| 113 | tok->indent = 0; |
| 114 | tok->indstack[0] = 0; |
| 115 | tok->atbol = 1; |
| 116 | tok->pendin = 0; |
| 117 | tok->prompt = tok->nextprompt = NULL; |
| 118 | tok->lineno = 0; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 119 | tok->level = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | return tok; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* Set up tokenizer for string */ |
| 125 | |
| 126 | struct tok_state * |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 127 | PyTokenizer_FromString(str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | char *str; |
| 129 | { |
| 130 | struct tok_state *tok = tok_new(); |
| 131 | if (tok == NULL) |
| 132 | return NULL; |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 133 | tok->buf = tok->cur = tok->end = tok->inp = str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | return tok; |
| 135 | } |
| 136 | |
| 137 | |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 138 | /* Set up tokenizer for file */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | |
| 140 | struct tok_state * |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 141 | PyTokenizer_FromFile(fp, ps1, ps2) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | FILE *fp; |
| 143 | char *ps1, *ps2; |
| 144 | { |
| 145 | struct tok_state *tok = tok_new(); |
| 146 | if (tok == NULL) |
| 147 | return NULL; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 148 | if ((tok->buf = PyMem_NEW(char, BUFSIZ)) == NULL) { |
| 149 | PyMem_DEL(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
| 152 | tok->cur = tok->inp = tok->buf; |
| 153 | tok->end = tok->buf + BUFSIZ; |
| 154 | tok->fp = fp; |
| 155 | tok->prompt = ps1; |
| 156 | tok->nextprompt = ps2; |
| 157 | return tok; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /* Free a tok_state structure */ |
| 162 | |
| 163 | void |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 164 | PyTokenizer_Free(tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 165 | struct tok_state *tok; |
| 166 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | if (tok->fp != NULL && tok->buf != NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 168 | PyMem_DEL(tok->buf); |
| 169 | PyMem_DEL(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | /* Get next char, updating state; error code goes into tok->done */ |
| 174 | |
| 175 | static int |
| 176 | tok_nextc(tok) |
| 177 | register struct tok_state *tok; |
| 178 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | for (;;) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 180 | if (tok->cur != tok->inp) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 181 | return *tok->cur++; /* Fast path */ |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 183 | if (tok->done != E_OK) |
| 184 | return EOF; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 185 | if (tok->fp == NULL) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 186 | char *end = strchr(tok->inp, '\n'); |
| 187 | if (end != NULL) |
| 188 | end++; |
| 189 | else { |
| 190 | end = strchr(tok->inp, '\0'); |
| 191 | if (end == tok->inp) { |
| 192 | tok->done = E_EOF; |
| 193 | return EOF; |
| 194 | } |
| 195 | } |
| 196 | if (tok->start == NULL) |
| 197 | tok->buf = tok->cur; |
| 198 | tok->lineno++; |
| 199 | tok->inp = end; |
| 200 | return *tok->cur++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | if (tok->prompt != NULL) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 203 | char *new = PyOS_Readline(tok->prompt); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | if (tok->nextprompt != NULL) |
| 205 | tok->prompt = tok->nextprompt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 206 | if (new == NULL) |
| 207 | tok->done = E_INTR; |
| 208 | else if (*new == '\0') { |
| 209 | free(new); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | tok->done = E_EOF; |
| 211 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 212 | else if (tok->start != NULL) { |
| 213 | int start = tok->start - tok->buf; |
| 214 | int oldlen = tok->cur - tok->buf; |
| 215 | int newlen = oldlen + strlen(new); |
| 216 | char *buf = realloc(tok->buf, newlen+1); |
| 217 | tok->lineno++; |
| 218 | if (buf == NULL) { |
| 219 | free(tok->buf); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 220 | tok->buf = NULL; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 221 | free(new); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 222 | tok->done = E_NOMEM; |
| 223 | return EOF; |
| 224 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 225 | tok->buf = buf; |
| 226 | tok->cur = tok->buf + oldlen; |
| 227 | strcpy(tok->buf + oldlen, new); |
| 228 | free(new); |
| 229 | tok->inp = tok->buf + newlen; |
| 230 | tok->end = tok->inp + 1; |
| 231 | tok->start = tok->buf + start; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 232 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 233 | else { |
| 234 | tok->lineno++; |
| 235 | if (tok->buf != NULL) |
| 236 | free(tok->buf); |
| 237 | tok->buf = new; |
| 238 | tok->cur = tok->buf; |
| 239 | tok->inp = strchr(tok->buf, '\0'); |
| 240 | tok->end = tok->inp + 1; |
| 241 | } |
| 242 | } |
| 243 | else { |
| 244 | int done = 0; |
| 245 | int cur = 0; |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 246 | char *pt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 247 | if (tok->start == NULL) { |
| 248 | if (tok->buf == NULL) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 249 | tok->buf = PyMem_NEW(char, BUFSIZ); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 250 | if (tok->buf == NULL) { |
| 251 | tok->done = E_NOMEM; |
| 252 | return EOF; |
| 253 | } |
| 254 | tok->end = tok->buf + BUFSIZ; |
| 255 | } |
| 256 | if (fgets(tok->buf, (int)(tok->end - tok->buf), |
| 257 | tok->fp) == NULL) { |
| 258 | tok->done = E_EOF; |
| 259 | done = 1; |
| 260 | } |
| 261 | else { |
| 262 | tok->done = E_OK; |
| 263 | tok->inp = strchr(tok->buf, '\0'); |
| 264 | done = tok->inp[-1] == '\n'; |
| 265 | } |
| 266 | } |
| 267 | else { |
| 268 | cur = tok->cur - tok->buf; |
Guido van Rossum | 78c0535 | 1995-01-17 16:12:13 +0000 | [diff] [blame] | 269 | if (feof(tok->fp)) { |
| 270 | tok->done = E_EOF; |
| 271 | done = 1; |
| 272 | } |
| 273 | else |
| 274 | tok->done = E_OK; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 275 | } |
| 276 | tok->lineno++; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 277 | /* Read until '\n' or EOF */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 278 | while (!done) { |
| 279 | int curstart = tok->start == NULL ? -1 : |
| 280 | tok->start - tok->buf; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 281 | int curvalid = tok->inp - tok->buf; |
Guido van Rossum | 3f6bb86 | 1995-09-21 20:36:34 +0000 | [diff] [blame] | 282 | int newsize = curvalid + BUFSIZ; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 283 | char *newbuf = tok->buf; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 284 | PyMem_RESIZE(newbuf, char, newsize); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 285 | if (newbuf == NULL) { |
| 286 | tok->done = E_NOMEM; |
| 287 | tok->cur = tok->inp; |
| 288 | return EOF; |
| 289 | } |
| 290 | tok->buf = newbuf; |
| 291 | tok->inp = tok->buf + curvalid; |
| 292 | tok->end = tok->buf + newsize; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 293 | tok->start = curstart < 0 ? NULL : |
| 294 | tok->buf + curstart; |
| 295 | if (fgets(tok->inp, |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 296 | (int)(tok->end - tok->inp), |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 297 | tok->fp) == NULL) { |
| 298 | /* Last line does not end in \n, |
| 299 | fake one */ |
| 300 | strcpy(tok->inp, "\n"); |
| 301 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 302 | tok->inp = strchr(tok->inp, '\0'); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 303 | done = tok->inp[-1] == '\n'; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 304 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 305 | tok->cur = tok->buf + cur; |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 306 | #ifndef macintosh |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 307 | /* replace "\r\n" with "\n" */ |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 308 | /* For Mac we leave the \r, giving a syntax error */ |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 309 | pt = tok->inp - 2; |
| 310 | if (pt >= tok->buf && *pt == '\r') { |
| 311 | *pt++ = '\n'; |
| 312 | *pt = '\0'; |
| 313 | tok->inp = pt; |
| 314 | } |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 315 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 316 | } |
| 317 | if (tok->done != E_OK) { |
| 318 | if (tok->prompt != NULL) |
| 319 | fprintf(stderr, "\n"); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 320 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 321 | return EOF; |
| 322 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 323 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 324 | /*NOTREACHED*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
| 328 | /* Back-up one character */ |
| 329 | |
| 330 | static void |
| 331 | tok_backup(tok, c) |
| 332 | register struct tok_state *tok; |
| 333 | register int c; |
| 334 | { |
| 335 | if (c != EOF) { |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 336 | if (--tok->cur < tok->buf) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 337 | Py_FatalError("tok_backup: begin of buffer"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | if (*tok->cur != c) |
| 339 | *tok->cur = c; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* Return the token corresponding to a single character */ |
| 345 | |
| 346 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 347 | PyToken_OneChar(c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 348 | int c; |
| 349 | { |
| 350 | switch (c) { |
| 351 | case '(': return LPAR; |
| 352 | case ')': return RPAR; |
| 353 | case '[': return LSQB; |
| 354 | case ']': return RSQB; |
| 355 | case ':': return COLON; |
| 356 | case ',': return COMMA; |
| 357 | case ';': return SEMI; |
| 358 | case '+': return PLUS; |
| 359 | case '-': return MINUS; |
| 360 | case '*': return STAR; |
| 361 | case '/': return SLASH; |
| 362 | case '|': return VBAR; |
| 363 | case '&': return AMPER; |
| 364 | case '<': return LESS; |
| 365 | case '>': return GREATER; |
| 366 | case '=': return EQUAL; |
| 367 | case '.': return DOT; |
| 368 | case '%': return PERCENT; |
| 369 | case '`': return BACKQUOTE; |
| 370 | case '{': return LBRACE; |
| 371 | case '}': return RBRACE; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 372 | case '^': return CIRCUMFLEX; |
| 373 | case '~': return TILDE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 374 | default: return OP; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 379 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 380 | PyToken_TwoChars(c1, c2) |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 381 | int c1, c2; |
| 382 | { |
| 383 | switch (c1) { |
| 384 | case '=': |
| 385 | switch (c2) { |
| 386 | case '=': return EQEQUAL; |
| 387 | } |
| 388 | break; |
| 389 | case '!': |
| 390 | switch (c2) { |
| 391 | case '=': return NOTEQUAL; |
| 392 | } |
| 393 | break; |
| 394 | case '<': |
| 395 | switch (c2) { |
| 396 | case '>': return NOTEQUAL; |
| 397 | case '=': return LESSEQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 398 | case '<': return LEFTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 399 | } |
| 400 | break; |
| 401 | case '>': |
| 402 | switch (c2) { |
| 403 | case '=': return GREATEREQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 404 | case '>': return RIGHTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 405 | } |
| 406 | break; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 407 | case '*': |
| 408 | switch (c2) { |
| 409 | case '*': return DOUBLESTAR; |
| 410 | } |
| 411 | break; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 412 | } |
| 413 | return OP; |
| 414 | } |
| 415 | |
| 416 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 417 | /* Get next token, after space stripping etc. */ |
| 418 | |
| 419 | int |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 420 | PyTokenizer_Get(tok, p_start, p_end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 421 | register struct tok_state *tok; /* In/out: tokenizer state */ |
| 422 | char **p_start, **p_end; /* Out: point to start/end of token */ |
| 423 | { |
| 424 | register int c; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 425 | int blankline; |
| 426 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 427 | *p_start = *p_end = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 428 | nextline: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 429 | tok->start = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 430 | blankline = 0; |
| 431 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 432 | /* Get indentation level */ |
| 433 | if (tok->atbol) { |
| 434 | register int col = 0; |
| 435 | tok->atbol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | for (;;) { |
| 437 | c = tok_nextc(tok); |
| 438 | if (c == ' ') |
| 439 | col++; |
| 440 | else if (c == '\t') |
| 441 | col = (col/tok->tabsize + 1) * tok->tabsize; |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 442 | else if (c == '\014') /* Control-L (formfeed) */ |
| 443 | col = 0; /* For Emacs users */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 444 | else |
| 445 | break; |
| 446 | } |
| 447 | tok_backup(tok, c); |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 448 | if (c == '#' || c == '\n') { |
| 449 | /* Lines with only whitespace and/or comments |
| 450 | shouldn't affect the indentation and are |
| 451 | not passed to the parser as NEWLINE tokens, |
| 452 | except *totally* empty lines in interactive |
| 453 | mode, which signal the end of a command group. */ |
| 454 | if (col == 0 && c == '\n' && tok->prompt != NULL) |
| 455 | blankline = 0; /* Let it through */ |
| 456 | else |
| 457 | blankline = 1; /* Ignore completely */ |
| 458 | /* We can't jump back right here since we still |
| 459 | may need to skip to the end of a comment */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | } |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 461 | if (!blankline && tok->level == 0) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 462 | if (col == tok->indstack[tok->indent]) { |
| 463 | /* No change */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 464 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 465 | else if (col > tok->indstack[tok->indent]) { |
| 466 | /* Indent -- always one */ |
| 467 | if (tok->indent+1 >= MAXINDENT) { |
| 468 | fprintf(stderr, "excessive indent\n"); |
| 469 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 470 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 471 | return ERRORTOKEN; |
| 472 | } |
| 473 | tok->pendin++; |
| 474 | tok->indstack[++tok->indent] = col; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 475 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 476 | else /* col < tok->indstack[tok->indent] */ { |
| 477 | /* Dedent -- any number, must be consistent */ |
| 478 | while (tok->indent > 0 && |
| 479 | col < tok->indstack[tok->indent]) { |
| 480 | tok->indent--; |
| 481 | tok->pendin--; |
| 482 | } |
| 483 | if (col != tok->indstack[tok->indent]) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 484 | fprintf(stderr, |
| 485 | "inconsistent dedent\n"); |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 486 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 487 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 488 | return ERRORTOKEN; |
| 489 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 494 | tok->start = tok->cur; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 495 | |
| 496 | /* Return pending indents/dedents */ |
| 497 | if (tok->pendin != 0) { |
| 498 | if (tok->pendin < 0) { |
| 499 | tok->pendin++; |
| 500 | return DEDENT; |
| 501 | } |
| 502 | else { |
| 503 | tok->pendin--; |
| 504 | return INDENT; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | again: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 509 | tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 510 | /* Skip spaces */ |
| 511 | do { |
| 512 | c = tok_nextc(tok); |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 513 | } while (c == ' ' || c == '\t' || c == '\014'); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 514 | |
| 515 | /* Set start of current token */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 516 | tok->start = tok->cur - 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 517 | |
| 518 | /* Skip comment */ |
| 519 | if (c == '#') { |
| 520 | /* Hack to allow overriding the tabsize in the file. |
| 521 | This is also recognized by vi, when it occurs near the |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 522 | beginning or end of the file. (Will vi never die...?) |
| 523 | For Python it must be at the beginning of the file! */ |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 524 | /* XXX The real vi syntax is actually different :-( */ |
| 525 | /* XXX Should recognize Emacs syntax, too */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 526 | int x; |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 527 | if (sscanf(tok->cur, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 528 | " vi:set tabsize=%d:", &x) == 1 && |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 529 | x >= 1 && x <= 40) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 530 | /* fprintf(stderr, "# vi:set tabsize=%d:\n", x); */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 531 | tok->tabsize = x; |
| 532 | } |
| 533 | do { |
| 534 | c = tok_nextc(tok); |
| 535 | } while (c != EOF && c != '\n'); |
| 536 | } |
| 537 | |
| 538 | /* Check for EOF and errors now */ |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 539 | if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 540 | return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 541 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 542 | |
| 543 | /* Identifier (most frequent token!) */ |
| 544 | if (isalpha(c) || c == '_') { |
Guido van Rossum | 5026cb4 | 1997-04-25 17:32:00 +0000 | [diff] [blame] | 545 | switch (c) { |
| 546 | case 'r': |
| 547 | case 'R': |
| 548 | c = tok_nextc(tok); |
| 549 | if (c == '"' || c == '\'') |
| 550 | goto letter_quote; |
| 551 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 552 | while (isalnum(c) || c == '_') { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 553 | c = tok_nextc(tok); |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 554 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 555 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 556 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | *p_end = tok->cur; |
| 558 | return NAME; |
| 559 | } |
| 560 | |
| 561 | /* Newline */ |
| 562 | if (c == '\n') { |
| 563 | tok->atbol = 1; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 564 | if (blankline || tok->level > 0) |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 565 | goto nextline; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 566 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 567 | *p_end = tok->cur - 1; /* Leave '\n' out of the string */ |
| 568 | return NEWLINE; |
| 569 | } |
| 570 | |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 571 | #ifdef macintosh |
| 572 | if (c == '\r') { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 573 | fprintf(stderr, |
| 574 | "File contains \\r characters (incorrect line endings?)\n"); |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 575 | tok->done = E_TOKEN; |
| 576 | tok->cur = tok->inp; |
| 577 | return ERRORTOKEN; |
| 578 | } |
| 579 | #endif |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 580 | /* Period or number starting with period? */ |
| 581 | if (c == '.') { |
| 582 | c = tok_nextc(tok); |
| 583 | if (isdigit(c)) { |
| 584 | goto fraction; |
| 585 | } |
| 586 | else { |
| 587 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 588 | *p_start = tok->start; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 589 | *p_end = tok->cur; |
| 590 | return DOT; |
| 591 | } |
| 592 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 593 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 594 | /* Number */ |
| 595 | if (isdigit(c)) { |
| 596 | if (c == '0') { |
| 597 | /* Hex or octal */ |
| 598 | c = tok_nextc(tok); |
| 599 | if (c == '.') |
| 600 | goto fraction; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 601 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 602 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 603 | goto imaginary; |
| 604 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 605 | if (c == 'x' || c == 'X') { |
| 606 | /* Hex */ |
| 607 | do { |
| 608 | c = tok_nextc(tok); |
| 609 | } while (isxdigit(c)); |
| 610 | } |
| 611 | else { |
Guido van Rossum | 9430945 | 1991-12-10 14:01:05 +0000 | [diff] [blame] | 612 | /* XXX This is broken! E.g., |
| 613 | 09.9 should be accepted as float! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | /* Octal; c is first char of it */ |
| 615 | /* There's no 'isoctdigit' macro, sigh */ |
| 616 | while ('0' <= c && c < '8') { |
| 617 | c = tok_nextc(tok); |
| 618 | } |
| 619 | } |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 620 | if (c == 'l' || c == 'L') |
| 621 | c = tok_nextc(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 622 | } |
| 623 | else { |
| 624 | /* Decimal */ |
| 625 | do { |
| 626 | c = tok_nextc(tok); |
| 627 | } while (isdigit(c)); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 628 | if (c == 'l' || c == 'L') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 629 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 630 | else { |
| 631 | /* Accept floating point numbers. |
| 632 | XXX This accepts incomplete things like |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 633 | XXX 12e or 1e+; worry run-time */ |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 634 | if (c == '.') { |
| 635 | fraction: |
| 636 | /* Fraction */ |
| 637 | do { |
| 638 | c = tok_nextc(tok); |
| 639 | } while (isdigit(c)); |
| 640 | } |
| 641 | if (c == 'e' || c == 'E') { |
| 642 | /* Exponent part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 643 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 644 | if (c == '+' || c == '-') |
| 645 | c = tok_nextc(tok); |
| 646 | while (isdigit(c)) { |
| 647 | c = tok_nextc(tok); |
| 648 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 649 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 650 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 651 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 652 | /* Imaginary part */ |
| 653 | imaginary: |
| 654 | c = tok_nextc(tok); |
| 655 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 659 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 660 | *p_end = tok->cur; |
| 661 | return NUMBER; |
| 662 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 663 | |
| 664 | letter_quote: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 665 | /* String */ |
| 666 | if (c == '\'' || c == '"') { |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 667 | char *quote2 = tok->cur+1; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 668 | int quote = c; |
| 669 | int triple = 0; |
| 670 | int tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 671 | for (;;) { |
| 672 | c = tok_nextc(tok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 673 | if (c == '\n') { |
| 674 | if (!triple) { |
| 675 | tok->done = E_TOKEN; |
| 676 | tok_backup(tok, c); |
| 677 | return ERRORTOKEN; |
| 678 | } |
| 679 | tripcount = 0; |
| 680 | } |
| 681 | else if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 682 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 683 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 684 | return ERRORTOKEN; |
| 685 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 686 | else if (c == quote) { |
| 687 | tripcount++; |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 688 | if (tok->cur == quote2) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 689 | c = tok_nextc(tok); |
| 690 | if (c == quote) { |
| 691 | triple = 1; |
| 692 | tripcount = 0; |
| 693 | continue; |
| 694 | } |
| 695 | tok_backup(tok, c); |
| 696 | } |
| 697 | if (!triple || tripcount == 3) |
| 698 | break; |
| 699 | } |
| 700 | else if (c == '\\') { |
| 701 | tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 702 | c = tok_nextc(tok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 703 | if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 704 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 705 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 706 | return ERRORTOKEN; |
| 707 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 708 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 709 | else |
| 710 | tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 711 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 712 | *p_start = tok->start; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 713 | *p_end = tok->cur; |
| 714 | return STRING; |
| 715 | } |
| 716 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 717 | /* Line continuation */ |
| 718 | if (c == '\\') { |
| 719 | c = tok_nextc(tok); |
| 720 | if (c != '\n') { |
| 721 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 722 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 723 | return ERRORTOKEN; |
| 724 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 725 | goto again; /* Read next line */ |
| 726 | } |
| 727 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 728 | /* Check for two-character token */ |
| 729 | { |
| 730 | int c2 = tok_nextc(tok); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 731 | int token = PyToken_TwoChars(c, c2); |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 732 | if (token != OP) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 733 | *p_start = tok->start; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 734 | *p_end = tok->cur; |
| 735 | return token; |
| 736 | } |
| 737 | tok_backup(tok, c2); |
| 738 | } |
| 739 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 740 | /* Keep track of parentheses nesting level */ |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 741 | switch (c) { |
| 742 | case '(': |
| 743 | case '[': |
| 744 | case '{': |
| 745 | tok->level++; |
| 746 | break; |
| 747 | case ')': |
| 748 | case ']': |
| 749 | case '}': |
| 750 | tok->level--; |
| 751 | break; |
| 752 | } |
| 753 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 754 | /* Punctuation character */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 755 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 756 | *p_end = tok->cur; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 757 | return PyToken_OneChar(c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 761 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 762 | |
| 763 | void |
| 764 | tok_dump(type, start, end) |
| 765 | int type; |
| 766 | char *start, *end; |
| 767 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame^] | 768 | printf("%s", _PyParser_TokenNames[type]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 769 | if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 770 | printf("(%.*s)", (int)(end - start), start); |
| 771 | } |
| 772 | |
| 773 | #endif |