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