Mercurial > cpython-lru_cache-weakref
annotate Parser/tokenizer.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 1c9a84e0981d |
| children | e9108e97a5a0 |
| 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 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3364
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Tokenizer implementation */ |
| 33 | |
| 86 | 34 #include "pgenheaders.h" |
| 35 | |
| 6 | 36 #include <ctype.h> |
| 37 | |
| 38 #include "tokenizer.h" | |
| 39 #include "errcode.h" | |
| 40 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
41 extern char *PyOS_Readline Py_PROTO((char *)); |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
42 /* 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
|
43 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
|
44 NULL if interrupted */ |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
45 |
|
678
205b947b1d3c
Make tabs always 8 spaces wide -- it's more portable.
Guido van Rossum <guido@python.org>
parents:
477
diff
changeset
|
46 /* Don't ever change this -- it would break the portability of Python code */ |
| 6 | 47 #define TABSIZE 8 |
| 48 | |
| 86 | 49 /* Forward */ |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
50 static struct tok_state *tok_new Py_PROTO((void)); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
51 static int tok_nextc Py_PROTO((struct tok_state *tok)); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
52 static void tok_backup Py_PROTO((struct tok_state *tok, int c)); |
| 86 | 53 |
| 6 | 54 /* Token names */ |
| 55 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
56 char *_PyParser_TokenNames[] = { |
| 6 | 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", | |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
85 "EQEQUAL", |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
86 "NOTEQUAL", |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
87 "LESSEQUAL", |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
88 "GREATEREQUAL", |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
89 "TILDE", |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
90 "CIRCUMFLEX", |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
91 "LEFTSHIFT", |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
92 "RIGHTSHIFT", |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
93 "DOUBLESTAR", |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
94 /* This table must match the #defines in token.h! */ |
| 6 | 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 { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
106 struct tok_state *tok = PyMem_NEW(struct tok_state, 1); |
| 6 | 107 if (tok == NULL) |
| 108 return NULL; | |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
109 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
| 6 | 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; | |
|
1284
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
119 tok->level = 0; |
| 6 | 120 return tok; |
| 121 } | |
| 122 | |
| 123 | |
| 124 /* Set up tokenizer for string */ | |
| 125 | |
| 126 struct tok_state * | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
127 PyTokenizer_FromString(str) |
| 6 | 128 char *str; |
| 129 { | |
| 130 struct tok_state *tok = tok_new(); | |
| 131 if (tok == NULL) | |
| 132 return NULL; | |
|
1897
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
133 tok->buf = tok->cur = tok->end = tok->inp = str; |
| 6 | 134 return tok; |
| 135 } | |
| 136 | |
| 137 | |
|
326
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
138 /* Set up tokenizer for file */ |
| 6 | 139 |
| 140 struct tok_state * | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
141 PyTokenizer_FromFile(fp, ps1, ps2) |
| 6 | 142 FILE *fp; |
| 143 char *ps1, *ps2; | |
| 144 { | |
| 145 struct tok_state *tok = tok_new(); | |
| 146 if (tok == NULL) | |
| 147 return NULL; | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
148 if ((tok->buf = PyMem_NEW(char, BUFSIZ)) == NULL) { |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
149 PyMem_DEL(tok); |
| 6 | 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 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
164 PyTokenizer_Free(tok) |
| 6 | 165 struct tok_state *tok; |
| 166 { | |
| 167 if (tok->fp != NULL && tok->buf != NULL) | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
168 PyMem_DEL(tok->buf); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
169 PyMem_DEL(tok); |
| 6 | 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 { | |
| 179 for (;;) { | |
|
1897
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
180 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
|
181 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
|
182 } |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
183 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
|
184 return EOF; |
| 6 | 185 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
|
186 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
|
187 if (end != NULL) |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
188 end++; |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
189 else { |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
190 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
|
191 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
|
192 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
|
193 return EOF; |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
194 } |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
195 } |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
196 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
|
197 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
|
198 tok->lineno++; |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
199 tok->inp = end; |
|
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
200 return *tok->cur++; |
| 6 | 201 } |
| 202 if (tok->prompt != NULL) { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
203 char *new = PyOS_Readline(tok->prompt); |
| 6 | 204 if (tok->nextprompt != NULL) |
| 205 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
|
206 if (new == NULL) |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
207 tok->done = E_INTR; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
208 else if (*new == '\0') { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
209 free(new); |
| 6 | 210 tok->done = E_EOF; |
| 211 } | |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 tok->lineno++; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
218 if (buf == NULL) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
219 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
|
220 tok->buf = NULL; |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
221 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
|
222 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
|
223 return EOF; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
224 } |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
225 tok->buf = buf; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
226 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
|
227 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
|
228 free(new); |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
233 else { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
234 tok->lineno++; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
235 if (tok->buf != NULL) |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
236 free(tok->buf); |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
237 tok->buf = new; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
238 tok->cur = tok->buf; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
239 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
|
240 tok->end = tok->inp + 1; |
| 6 | 241 } |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
242 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
243 else { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
244 int done = 0; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
245 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
|
246 char *pt; |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
247 if (tok->start == NULL) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
248 if (tok->buf == NULL) { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
249 tok->buf = PyMem_NEW(char, BUFSIZ); |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
250 if (tok->buf == 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_NOMEM; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
252 return EOF; |
|
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 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
|
255 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
256 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
|
257 tok->fp) == NULL) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
258 tok->done = E_EOF; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
259 done = 1; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
260 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
261 else { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
262 tok->done = E_OK; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
263 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
|
264 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
|
265 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
266 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
267 else { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
268 cur = tok->cur - tok->buf; |
|
2149
6060d7fb07e3
fix loop on unterminated triple quotes
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
269 if (feof(tok->fp)) { |
|
6060d7fb07e3
fix loop on unterminated triple quotes
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
270 tok->done = E_EOF; |
|
6060d7fb07e3
fix loop on unterminated triple quotes
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
271 done = 1; |
|
6060d7fb07e3
fix loop on unterminated triple quotes
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
272 } |
|
6060d7fb07e3
fix loop on unterminated triple quotes
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
273 else |
|
6060d7fb07e3
fix loop on unterminated triple quotes
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
274 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
|
275 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
276 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
|
277 /* 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
|
278 while (!done) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
279 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
|
280 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
|
281 int curvalid = tok->inp - tok->buf; |
|
3116
db139a4112f3
fix bogus resize length in nextc
Guido van Rossum <guido@python.org>
parents:
2801
diff
changeset
|
282 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
|
283 char *newbuf = tok->buf; |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
284 PyMem_RESIZE(newbuf, char, newsize); |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
285 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
|
286 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
|
287 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
|
288 return EOF; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
289 } |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 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
|
294 tok->buf + curstart; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
295 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
|
296 (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
|
297 tok->fp) == NULL) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
298 /* 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
|
299 fake one */ |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
300 strcpy(tok->inp, "\n"); |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
301 } |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
302 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
|
303 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
|
304 } |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
305 tok->cur = tok->buf + cur; |
|
5311
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
306 #ifndef macintosh |
|
2732
daa150b3a6e4
replace "\r\n" with "\n" at line end (Jim Ahlstrom)
Guido van Rossum <guido@python.org>
parents:
2149
diff
changeset
|
307 /* replace "\r\n" with "\n" */ |
|
5311
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
308 /* For Mac we leave the \r, giving a syntax error */ |
|
2732
daa150b3a6e4
replace "\r\n" with "\n" at line end (Jim Ahlstrom)
Guido van Rossum <guido@python.org>
parents:
2149
diff
changeset
|
309 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
|
310 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
|
311 *pt++ = '\n'; |
|
daa150b3a6e4
replace "\r\n" with "\n" at line end (Jim Ahlstrom)
Guido van Rossum <guido@python.org>
parents:
2149
diff
changeset
|
312 *pt = '\0'; |
|
daa150b3a6e4
replace "\r\n" with "\n" at line end (Jim Ahlstrom)
Guido van Rossum <guido@python.org>
parents:
2149
diff
changeset
|
313 tok->inp = pt; |
|
daa150b3a6e4
replace "\r\n" with "\n" at line end (Jim Ahlstrom)
Guido van Rossum <guido@python.org>
parents:
2149
diff
changeset
|
314 } |
|
5311
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
315 #endif |
| 6 | 316 } |
| 317 if (tok->done != E_OK) { | |
| 318 if (tok->prompt != NULL) | |
| 319 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
|
320 tok->cur = tok->inp; |
| 6 | 321 return EOF; |
| 322 } | |
| 323 } | |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
324 /*NOTREACHED*/ |
| 6 | 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) { | |
|
2044
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1897
diff
changeset
|
336 if (--tok->cur < tok->buf) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
337 Py_FatalError("tok_backup: begin of buffer"); |
| 6 | 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 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
347 PyToken_OneChar(c) |
| 6 | 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; | |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
372 case '^': return CIRCUMFLEX; |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
373 case '~': return TILDE; |
| 6 | 374 default: return OP; |
| 375 } | |
| 376 } | |
| 377 | |
| 378 | |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
379 int |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
380 PyToken_TwoChars(c1, c2) |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
381 int c1, c2; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
382 { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
383 switch (c1) { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
384 case '=': |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
385 switch (c2) { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
386 case '=': return EQEQUAL; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
387 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
388 break; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
389 case '!': |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
390 switch (c2) { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
391 case '=': return NOTEQUAL; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
392 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
393 break; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
394 case '<': |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
395 switch (c2) { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
396 case '>': return NOTEQUAL; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
397 case '=': return LESSEQUAL; |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
398 case '<': return LEFTSHIFT; |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
399 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
400 break; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
401 case '>': |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
402 switch (c2) { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
403 case '=': return GREATEREQUAL; |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
404 case '>': return RIGHTSHIFT; |
|
400
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 break; |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
407 case '*': |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
408 switch (c2) { |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
409 case '*': return DOUBLESTAR; |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
410 } |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
411 break; |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
412 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
413 return OP; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
414 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
415 |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
416 |
| 6 | 417 /* Get next token, after space stripping etc. */ |
| 418 | |
| 419 int | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
420 PyTokenizer_Get(tok, p_start, p_end) |
| 6 | 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; | |
|
326
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
425 int blankline; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
426 |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
427 *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
|
428 nextline: |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
429 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
|
430 blankline = 0; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
431 |
| 6 | 432 /* Get indentation level */ |
| 433 if (tok->atbol) { | |
| 434 register int col = 0; | |
| 435 tok->atbol = 0; | |
| 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; | |
|
2801
e911f2fb179c
ignore control-l in whitespace
Guido van Rossum <guido@python.org>
parents:
2732
diff
changeset
|
442 else if (c == '\014') /* Control-L (formfeed) */ |
|
e911f2fb179c
ignore control-l in whitespace
Guido van Rossum <guido@python.org>
parents:
2732
diff
changeset
|
443 col = 0; /* For Emacs users */ |
| 6 | 444 else |
| 445 break; | |
| 446 } | |
| 447 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
|
448 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
|
449 /* 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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 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
|
456 else |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
457 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
|
458 /* 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
|
459 may need to skip to the end of a comment */ |
| 6 | 460 } |
|
1284
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
461 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
|
462 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
|
463 /* No change */ |
| 6 | 464 } |
|
326
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
465 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
|
466 /* 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
|
467 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
|
468 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
|
469 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
|
470 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
|
471 return ERRORTOKEN; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
472 } |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
473 tok->pendin++; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
474 tok->indstack[++tok->indent] = col; |
| 6 | 475 } |
|
326
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
476 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
|
477 /* 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
|
478 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
|
479 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
|
480 tok->indent--; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
481 tok->pendin--; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
482 } |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
483 if (col != tok->indstack[tok->indent]) { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
484 fprintf(stderr, |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
485 "inconsistent dedent\n"); |
|
326
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
486 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
|
487 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
|
488 return ERRORTOKEN; |
|
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
489 } |
| 6 | 490 } |
| 491 } | |
| 492 } | |
| 493 | |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
494 tok->start = tok->cur; |
| 6 | 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: | |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
509 tok->start = NULL; |
| 6 | 510 /* Skip spaces */ |
| 511 do { | |
| 512 c = tok_nextc(tok); | |
|
2801
e911f2fb179c
ignore control-l in whitespace
Guido van Rossum <guido@python.org>
parents:
2732
diff
changeset
|
513 } while (c == ' ' || c == '\t' || c == '\014'); |
| 6 | 514 |
| 515 /* 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
|
516 tok->start = tok->cur - 1; |
| 6 | 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 | |
|
326
ff0862e3d63a
Completely ignore lines with only a newline token on them, except
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
522 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
|
523 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
|
524 /* 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
|
525 /* XXX Should recognize Emacs syntax, too */ |
| 6 | 526 int x; |
|
1897
3fbda8768855
* Parser/tokenizer.c (tok_nextc): count line numbers when parsing
Guido van Rossum <guido@python.org>
parents:
1835
diff
changeset
|
527 if (sscanf(tok->cur, |
| 86 | 528 " vi:set tabsize=%d:", &x) == 1 && |
| 6 | 529 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
|
530 /* fprintf(stderr, "# vi:set tabsize=%d:\n", x); */ |
| 6 | 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 */ | |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
539 if (c == EOF) { |
| 6 | 540 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN; |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
541 } |
| 6 | 542 |
| 543 /* Identifier (most frequent token!) */ | |
| 544 if (isalpha(c) || c == '_') { | |
|
5341
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
545 switch (c) { |
|
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
546 case 'r': |
|
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
547 case 'R': |
|
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
548 c = tok_nextc(tok); |
|
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
549 if (c == '"' || c == '\'') |
|
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
550 goto letter_quote; |
|
67e5237ce141
Now that the string-sig has settled on r"obin" strings, restrict the
Guido van Rossum <guido@python.org>
parents:
5311
diff
changeset
|
551 } |
|
5265
3c1788ab9761
Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
552 while (isalnum(c) || c == '_') { |
| 6 | 553 c = tok_nextc(tok); |
|
5265
3c1788ab9761
Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
554 } |
| 6 | 555 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
|
556 *p_start = tok->start; |
| 6 | 557 *p_end = tok->cur; |
| 558 return NAME; | |
| 559 } | |
| 560 | |
| 561 /* Newline */ | |
| 562 if (c == '\n') { | |
| 563 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
|
564 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
|
565 goto nextline; |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
566 *p_start = tok->start; |
| 6 | 567 *p_end = tok->cur - 1; /* Leave '\n' out of the string */ |
| 568 return NEWLINE; | |
| 569 } | |
| 570 | |
|
5311
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
571 #ifdef macintosh |
|
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
572 if (c == '\r') { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
573 fprintf(stderr, |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
574 "File contains \\r characters (incorrect line endings?)\n"); |
|
5311
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
575 tok->done = E_TOKEN; |
|
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
576 tok->cur = tok->inp; |
|
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
577 return ERRORTOKEN; |
|
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
578 } |
|
782597cf6151
(Jack:) On the Mac, give syntax error on \r.
Guido van Rossum <guido@python.org>
parents:
5265
diff
changeset
|
579 #endif |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
580 /* Period or number starting with period? */ |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
581 if (c == '.') { |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
582 c = tok_nextc(tok); |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
583 if (isdigit(c)) { |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
584 goto fraction; |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
585 } |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
586 else { |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
587 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
|
588 *p_start = tok->start; |
|
410
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
589 *p_end = tok->cur; |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
590 return DOT; |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
591 } |
|
fd061dae9e57
Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents:
400
diff
changeset
|
592 } |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
593 |
| 6 | 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; | |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
601 #ifndef WITHOUT_COMPLEX |
|
3364
bc17e8313ea0
use only j for imaginary constants
Guido van Rossum <guido@python.org>
parents:
3333
diff
changeset
|
602 if (c == 'j' || c == 'J') |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
603 goto imaginary; |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
604 #endif |
| 6 | 605 if (c == 'x' || c == 'X') { |
| 606 /* Hex */ | |
| 607 do { | |
| 608 c = tok_nextc(tok); | |
| 609 } while (isxdigit(c)); | |
| 610 } | |
| 611 else { | |
|
477
436c81030853
Add warning XXX that 09.9 isn't accepted.
Guido van Rossum <guido@python.org>
parents:
410
diff
changeset
|
612 /* 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
|
613 09.9 should be accepted as float! */ |
| 6 | 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 } | |
|
236
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
620 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
|
621 c = tok_nextc(tok); |
| 6 | 622 } |
| 623 else { | |
| 624 /* Decimal */ | |
| 625 do { | |
| 626 c = tok_nextc(tok); | |
| 627 } 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
|
628 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
|
629 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
|
630 else { |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
631 /* 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
|
632 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
|
633 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
|
634 if (c == '.') { |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
635 fraction: |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
636 /* Fraction */ |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
637 do { |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
638 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
|
639 } while (isdigit(c)); |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
640 } |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
641 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
|
642 /* Exponent part */ |
| 6 | 643 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
|
644 if (c == '+' || c == '-') |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
645 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
|
646 while (isdigit(c)) { |
|
0a92b6a1b9c0
Added recognition of 'l' or 'L' as long integer suffix
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
647 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
|
648 } |
| 6 | 649 } |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
650 #ifndef WITHOUT_COMPLEX |
|
3364
bc17e8313ea0
use only j for imaginary constants
Guido van Rossum <guido@python.org>
parents:
3333
diff
changeset
|
651 if (c == 'j' || c == 'J') |
|
3333
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
652 /* Imaginary part */ |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
653 imaginary: |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
654 c = tok_nextc(tok); |
|
4b5bff95804e
changes for pow(**) and complex
Guido van Rossum <guido@python.org>
parents:
3116
diff
changeset
|
655 #endif |
| 6 | 656 } |
| 657 } | |
| 658 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
|
659 *p_start = tok->start; |
| 6 | 660 *p_end = tok->cur; |
| 661 return NUMBER; | |
| 662 } | |
|
5265
3c1788ab9761
Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
663 |
|
3c1788ab9761
Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
664 letter_quote: |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
665 /* String */ |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
666 if (c == '\'' || c == '"') { |
|
5265
3c1788ab9761
Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
667 char *quote2 = tok->cur+1; |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
668 int quote = c; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
669 int triple = 0; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
670 int tripcount = 0; |
| 6 | 671 for (;;) { |
| 672 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
|
673 if (c == '\n') { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
674 if (!triple) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
675 tok->done = E_TOKEN; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
676 tok_backup(tok, c); |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
677 return ERRORTOKEN; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
678 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
679 tripcount = 0; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
680 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
681 else if (c == EOF) { |
| 6 | 682 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
|
683 tok->cur = tok->inp; |
| 6 | 684 return ERRORTOKEN; |
| 685 } | |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
686 else if (c == quote) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
687 tripcount++; |
|
5265
3c1788ab9761
Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
688 if (tok->cur == quote2) { |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
689 c = tok_nextc(tok); |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
690 if (c == quote) { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
691 triple = 1; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
692 tripcount = 0; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
693 continue; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
694 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
695 tok_backup(tok, c); |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
696 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
697 if (!triple || tripcount == 3) |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
698 break; |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
699 } |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
700 else if (c == '\\') { |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
701 tripcount = 0; |
| 6 | 702 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
|
703 if (c == EOF) { |
| 6 | 704 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
|
705 tok->cur = tok->inp; |
| 6 | 706 return ERRORTOKEN; |
| 707 } | |
| 708 } | |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
709 else |
|
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
710 tripcount = 0; |
| 6 | 711 } |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
712 *p_start = tok->start; |
|
1364
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
713 *p_end = tok->cur; |
|
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
714 return STRING; |
|
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
715 } |
|
132602d854d8
Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
716 |
| 6 | 717 /* Line continuation */ |
| 718 if (c == '\\') { | |
| 719 c = tok_nextc(tok); | |
| 720 if (c != '\n') { | |
| 721 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
|
722 tok->cur = tok->inp; |
| 6 | 723 return ERRORTOKEN; |
| 724 } | |
| 725 goto again; /* Read next line */ | |
| 726 } | |
| 727 | |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
728 /* 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
|
729 { |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
730 int c2 = tok_nextc(tok); |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
731 int token = PyToken_TwoChars(c, c2); |
|
400
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
732 if (token != OP) { |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
733 *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
|
734 *p_end = tok->cur; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
735 return token; |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
736 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
737 tok_backup(tok, c2); |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
738 } |
|
d189a2f681a6
Added 2-char tokens and new versions of comparisons
Guido van Rossum <guido@python.org>
parents:
326
diff
changeset
|
739 |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
740 /* 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
|
741 switch (c) { |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
742 case '(': |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
743 case '[': |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
744 case '{': |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
745 tok->level++; |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
746 break; |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
747 case ')': |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
748 case ']': |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
749 case '}': |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
750 tok->level--; |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
751 break; |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
752 } |
|
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
753 |
| 6 | 754 /* Punctuation character */ |
|
1835
d799889c01e3
* Parser/tokenizer.c: backup over illegal newline in string
Guido van Rossum <guido@python.org>
parents:
1364
diff
changeset
|
755 *p_start = tok->start; |
| 6 | 756 *p_end = tok->cur; |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
757 return PyToken_OneChar(c); |
| 6 | 758 } |
| 759 | |
| 760 | |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
761 #ifdef Py_DEBUG |
| 6 | 762 |
| 763 void | |
| 764 tok_dump(type, start, end) | |
| 765 int type; | |
| 766 char *start, *end; | |
| 767 { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5341
diff
changeset
|
768 printf("%s", _PyParser_TokenNames[type]); |
| 6 | 769 if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 770 printf("(%.*s)", (int)(end - start), start); | |
| 771 } | |
| 772 | |
| 773 #endif |
