2 /* Tokenizer implementation */
5 #include "pgenheaders.h"
10 #include "tokenizer.h"
14 #include "unicodeobject.h"
15 #include "stringobject.h"
16 #include "fileobject.h"
21 extern char *PyOS_Readline(char *);
22 /* Return malloc'ed string including trailing \n;
23 empty malloc'ed string for EOF;
24 NULL if interrupted */
26 /* Don't ever change this -- it would break the portability of Python code */
29 /* Convert a possibly signed character to a nonnegative int */
30 /* XXX This assumes characters are 8 bits wide */
31 #ifdef __CHAR_UNSIGNED__
32 #define Py_CHARMASK(c) (c)
34 #define Py_CHARMASK(c) ((c) & 0xff)
38 static struct tok_state
*tok_new(void);
39 static int tok_nextc(struct tok_state
*tok
);
40 static void tok_backup(struct tok_state
*tok
, int c
);
44 char *_PyParser_TokenNames
[] = {
95 /* This table must match the #defines in token.h! */
102 /* Create and initialize a new tok_state structure */
104 static struct tok_state
*
107 struct tok_state
*tok
= PyMem_NEW(struct tok_state
, 1);
110 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= tok
->start
= NULL
;
113 tok
->tabsize
= TABSIZE
;
115 tok
->indstack
[0] = 0;
118 tok
->prompt
= tok
->nextprompt
= NULL
;
121 tok
->filename
= NULL
;
125 tok
->altindstack
[0] = 0;
126 tok
->decoding_state
= 0;
127 tok
->decoding_erred
= 0;
128 tok
->read_coding_spec
= 0;
129 tok
->issued_encoding_warning
= 0;
130 tok
->encoding
= NULL
;
133 tok
->decoding_readline
= NULL
;
134 tok
->decoding_buffer
= NULL
;
142 decoding_fgets(char *s
, int size
, struct tok_state
*tok
)
144 return fgets(s
, size
, tok
->fp
);
148 decoding_feof(struct tok_state
*tok
)
150 return feof(tok
->fp
);
154 decode_str(const char *str
, struct tok_state
*tok
)
162 error_ret(struct tok_state
*tok
) /* XXX */
164 tok
->decoding_erred
= 1;
165 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
) /* see PyTokenizer_Free */
168 return NULL
; /* as if it were EOF */
172 new_string(const char *s
, int len
)
174 char* result
= PyMem_NEW(char, len
+ 1);
175 if (result
!= NULL
) {
176 memcpy(result
, s
, len
);
183 get_normal_name(char *s
) /* for utf-8 and latin-1 */
187 for (i
= 0; i
< 12; i
++) {
189 if (c
== '\0') break;
190 else if (c
== '_') buf
[i
] = '-';
191 else buf
[i
] = tolower(c
);
194 if (strcmp(buf
, "utf-8") == 0 ||
195 strncmp(buf
, "utf-8-", 6) == 0) return "utf-8";
196 else if (strcmp(buf
, "latin-1") == 0 ||
197 strcmp(buf
, "iso-8859-1") == 0 ||
198 strcmp(buf
, "iso-latin-1") == 0 ||
199 strncmp(buf
, "latin-1-", 8) == 0 ||
200 strncmp(buf
, "iso-8859-1-", 11) == 0 ||
201 strncmp(buf
, "iso-latin-1-", 12) == 0) return "iso-8859-1";
205 /* Return the coding spec in S, or NULL if none is found. */
208 get_coding_spec(const char *s
, int size
)
211 /* Coding spec must be in a comment, and that comment must be
212 * the only statement on the source code line. */
213 for (i
= 0; i
< size
- 6; i
++) {
216 if (s
[i
] != ' ' && s
[i
] != '\t' && s
[i
] != '\014')
219 for (; i
< size
- 6; i
++) { /* XXX inefficient search */
220 const char* t
= s
+ i
;
221 if (strncmp(t
, "coding", 6) == 0) {
222 const char* begin
= NULL
;
224 if (t
[0] != ':' && t
[0] != '=')
228 } while (t
[0] == '\x20' || t
[0] == '\t');
231 while (isalnum(t
[0]) || t
[0] == '-' || t
[0] == '_' ||
236 char* r
= new_string(begin
, t
- begin
);
237 char* q
= get_normal_name(r
);
240 r
= new_string(q
, strlen(q
));
249 /* Check whether the line contains a coding spec. If it does,
250 invoke the set_readline function for the new encoding.
251 This function receives the tok_state and the new encoding.
252 Return 1 on success, 0 on failure. */
255 check_coding_spec(const char* line
, int size
, struct tok_state
*tok
,
256 int set_readline(struct tok_state
*, const char *))
262 /* It's a continuation line, so it can't be a coding spec. */
264 cs
= get_coding_spec(line
, size
);
266 tok
->read_coding_spec
= 1;
267 if (tok
->encoding
== NULL
) {
268 assert(tok
->decoding_state
== 1); /* raw */
269 if (strcmp(cs
, "utf-8") == 0 ||
270 strcmp(cs
, "iso-8859-1") == 0) {
273 #ifdef Py_USING_UNICODE
274 r
= set_readline(tok
, cs
);
277 tok
->decoding_state
= -1;
280 /* Without Unicode support, we cannot
281 process the coding spec. Since there
282 won't be any Unicode literals, that
286 } else { /* then, compare cs with BOM */
287 r
= (strcmp(tok
->encoding
, cs
) == 0);
294 /* See whether the file starts with a BOM. If it does,
295 invoke the set_readline function with the new encoding.
296 Return 1 on success, 0 on failure. */
299 check_bom(int get_char(struct tok_state
*),
300 void unget_char(int, struct tok_state
*),
301 int set_readline(struct tok_state
*, const char *),
302 struct tok_state
*tok
)
304 int ch
= get_char(tok
);
305 tok
->decoding_state
= 1;
308 } else if (ch
== 0xEF) {
309 ch
= get_char(tok
); if (ch
!= 0xBB) goto NON_BOM
;
310 ch
= get_char(tok
); if (ch
!= 0xBF) goto NON_BOM
;
312 /* Disable support for UTF-16 BOMs until a decision
313 is made whether this needs to be supported. */
314 } else if (ch
== 0xFE) {
315 ch
= get_char(tok
); if (ch
!= 0xFF) goto NON_BOM
;
316 if (!set_readline(tok
, "utf-16-be")) return 0;
317 tok
->decoding_state
= -1;
318 } else if (ch
== 0xFF) {
319 ch
= get_char(tok
); if (ch
!= 0xFE) goto NON_BOM
;
320 if (!set_readline(tok
, "utf-16-le")) return 0;
321 tok
->decoding_state
= -1;
327 tok
->encoding
= new_string("utf-8", 5); /* resulting is in utf-8 */
330 /* any token beginning with '\xEF', '\xFE', '\xFF' is a bad token */
331 unget_char(0xFF, tok
); /* XXX this will cause a syntax error */
335 /* Read a line of text from TOK into S, using the stream in TOK.
336 Return NULL on failure, else S. */
339 fp_readl(char *s
, int size
, struct tok_state
*tok
)
341 #ifndef Py_USING_UNICODE
342 /* In a non-Unicode built, this should never be called. */
343 Py_FatalError("fp_readl should not be called in this build.");
344 return NULL
; /* Keep compiler happy (not reachable) */
347 PyObject
* buf
= tok
->decoding_buffer
;
349 PyObject
*args
= PyTuple_New(0);
351 return error_ret(tok
);
352 buf
= PyObject_Call(tok
->decoding_readline
, args
, NULL
);
355 return error_ret(tok
);
357 tok
->decoding_buffer
= NULL
;
359 utf8
= PyUnicode_AsUTF8String(buf
);
362 return error_ret(tok
);
364 const char* str
= PyString_AsString(utf8
);
365 assert(strlen(str
) < (size_t)size
); /* XXX */
368 if (s
[0] == '\0') return NULL
; /* EOF */
374 /* Set the readline function for TOK to a StreamReader's
375 readline function. The StreamReader is named ENC.
377 This function is called from check_bom and check_coding_spec.
379 ENC is usually identical to the future value of tok->encoding,
380 except for the (currently unsupported) case of UTF-16.
382 Return 1 on success, 0 on failure. */
385 fp_setreadl(struct tok_state
*tok
, const char* enc
)
387 PyObject
*reader
, *stream
, *readline
;
389 stream
= PyFile_FromFile(tok
->fp
, tok
->filename
, "rb", NULL
);
393 reader
= PyCodec_StreamReader(enc
, stream
, NULL
);
398 readline
= PyObject_GetAttrString(reader
, "readline");
400 if (readline
== NULL
)
403 tok
->decoding_readline
= readline
;
407 /* Fetch the next byte from TOK. */
409 static int fp_getc(struct tok_state
*tok
) {
410 return getc(tok
->fp
);
413 /* Unfetch the last byte back into TOK. */
415 static void fp_ungetc(int c
, struct tok_state
*tok
) {
419 /* Read a line of input from TOK. Determine encoding
423 decoding_fgets(char *s
, int size
, struct tok_state
*tok
)
426 int warn
= 0, badchar
= 0;
428 if (tok
->decoding_state
< 0) {
429 /* We already have a codec associated with
431 line
= fp_readl(s
, size
, tok
);
433 } else if (tok
->decoding_state
> 0) {
434 /* We want a 'raw' read. */
435 line
= Py_UniversalNewlineFgets(s
, size
,
440 /* We have not yet determined the encoding.
441 If an encoding is found, use the file-pointer
442 reader functions from now on. */
443 if (!check_bom(fp_getc
, fp_ungetc
, fp_setreadl
, tok
))
444 return error_ret(tok
);
445 assert(tok
->decoding_state
!= 0);
448 if (line
!= NULL
&& tok
->lineno
< 2 && !tok
->read_coding_spec
) {
449 if (!check_coding_spec(line
, strlen(line
), tok
, fp_setreadl
)) {
450 return error_ret(tok
);
454 if (warn
&& line
&& !tok
->issued_encoding_warning
&& !tok
->encoding
) {
456 for (c
= (unsigned char *)line
; *c
; c
++)
464 sprintf(buf
, "Non-ASCII character '\\x%.2x', "
465 "but no declared encoding", badchar
);
466 /* Need to add 1 to the line number, since this line
467 has not been counted, yet. */
468 PyErr_WarnExplicit(PyExc_DeprecationWarning
,
469 buf
, tok
->filename
, tok
->lineno
+ 1,
471 tok
->issued_encoding_warning
= 1;
478 decoding_feof(struct tok_state
*tok
)
480 if (tok
->decoding_state
>= 0) {
481 return feof(tok
->fp
);
483 PyObject
* buf
= tok
->decoding_buffer
;
485 PyObject
*args
= PyTuple_New(0);
490 buf
= PyObject_Call(tok
->decoding_readline
,
497 tok
->decoding_buffer
= buf
;
500 return PyObject_Length(buf
) == 0;
504 /* Fetch a byte from TOK, using the string buffer. */
506 static int buf_getc(struct tok_state
*tok
) {
510 /* Unfetch a byte from TOK, using the string buffer. */
512 static void buf_ungetc(int c
, struct tok_state
*tok
) {
514 assert(*tok
->str
== c
); /* tok->cur may point to read-only segment */
517 /* Set the readline function for TOK to ENC. For the string-based
518 tokenizer, this means to just record the encoding. */
520 static int buf_setreadl(struct tok_state
*tok
, const char* enc
) {
525 /* Return a UTF-8 encoding Python string object from the
526 C byte string STR, which is encoded with ENC. */
528 #ifdef Py_USING_UNICODE
530 translate_into_utf8(const char* str
, const char* enc
) {
532 PyObject
* buf
= PyUnicode_Decode(str
, strlen(str
), enc
, NULL
);
535 utf8
= PyUnicode_AsUTF8String(buf
);
541 /* Decode a byte string STR for use as the buffer of TOK.
542 Look for encoding declarations inside STR, and record them
546 decode_str(const char *str
, struct tok_state
*tok
)
548 PyObject
* utf8
= NULL
;
553 if (!check_bom(buf_getc
, buf_ungetc
, buf_setreadl
, tok
))
555 str
= tok
->str
; /* string after BOM if any */
557 #ifdef Py_USING_UNICODE
558 if (tok
->enc
!= NULL
) {
559 utf8
= translate_into_utf8(str
, tok
->enc
);
562 str
= PyString_AsString(utf8
);
565 for (s
= str
;; s
++) {
566 if (*s
== '\0') break;
567 else if (*s
== '\n') {
569 if (lineno
== 2) break;
573 if (!check_coding_spec(str
, s
- str
, tok
, buf_setreadl
))
575 #ifdef Py_USING_UNICODE
576 if (tok
->enc
!= NULL
) {
577 assert(utf8
== NULL
);
578 utf8
= translate_into_utf8(str
, tok
->enc
);
581 str
= PyString_AsString(utf8
);
584 assert(tok
->decoding_buffer
== NULL
);
585 tok
->decoding_buffer
= utf8
; /* CAUTION */
591 /* Set up tokenizer for string */
594 PyTokenizer_FromString(char *str
)
596 struct tok_state
*tok
= tok_new();
599 str
= (char *)decode_str(str
, tok
);
602 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= str
;
607 /* Set up tokenizer for file */
610 PyTokenizer_FromFile(FILE *fp
, char *ps1
, char *ps2
)
612 struct tok_state
*tok
= tok_new();
615 if ((tok
->buf
= PyMem_NEW(char, BUFSIZ
)) == NULL
) {
619 tok
->cur
= tok
->inp
= tok
->buf
;
620 tok
->end
= tok
->buf
+ BUFSIZ
;
623 tok
->nextprompt
= ps2
;
628 /* Free a tok_state structure */
631 PyTokenizer_Free(struct tok_state
*tok
)
633 if (tok
->encoding
!= NULL
)
634 PyMem_DEL(tok
->encoding
);
636 Py_XDECREF(tok
->decoding_readline
);
637 Py_XDECREF(tok
->decoding_buffer
);
639 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
)
645 /* Get next char, updating state; error code goes into tok->done */
648 tok_nextc(register struct tok_state
*tok
)
651 if (tok
->cur
!= tok
->inp
) {
652 return Py_CHARMASK(*tok
->cur
++); /* Fast path */
654 if (tok
->done
!= E_OK
)
656 if (tok
->fp
== NULL
) {
657 char *end
= strchr(tok
->inp
, '\n');
661 end
= strchr(tok
->inp
, '\0');
662 if (end
== tok
->inp
) {
667 if (tok
->start
== NULL
)
671 return Py_CHARMASK(*tok
->cur
++);
673 if (tok
->prompt
!= NULL
) {
674 char *new = PyOS_Readline(tok
->prompt
);
675 if (tok
->nextprompt
!= NULL
)
676 tok
->prompt
= tok
->nextprompt
;
679 else if (*new == '\0') {
683 else if (tok
->start
!= NULL
) {
684 size_t start
= tok
->start
- tok
->buf
;
685 size_t oldlen
= tok
->cur
- tok
->buf
;
686 size_t newlen
= oldlen
+ strlen(new);
687 char *buf
= tok
->buf
;
688 PyMem_RESIZE(buf
, char, newlen
+1);
698 tok
->cur
= tok
->buf
+ oldlen
;
699 strcpy(tok
->buf
+ oldlen
, new);
701 tok
->inp
= tok
->buf
+ newlen
;
702 tok
->end
= tok
->inp
+ 1;
703 tok
->start
= tok
->buf
+ start
;
707 if (tok
->buf
!= NULL
)
711 tok
->inp
= strchr(tok
->buf
, '\0');
712 tok
->end
= tok
->inp
+ 1;
719 if (tok
->start
== NULL
) {
720 if (tok
->buf
== NULL
) {
721 tok
->buf
= PyMem_NEW(char, BUFSIZ
);
722 if (tok
->buf
== NULL
) {
726 tok
->end
= tok
->buf
+ BUFSIZ
;
728 if (decoding_fgets(tok
->buf
, (int)(tok
->end
- tok
->buf
),
735 tok
->inp
= strchr(tok
->buf
, '\0');
736 done
= tok
->inp
[-1] == '\n';
740 cur
= tok
->cur
- tok
->buf
;
741 if (decoding_feof(tok
)) {
749 /* Read until '\n' or EOF */
751 int curstart
= tok
->start
== NULL
? -1 :
752 tok
->start
- tok
->buf
;
753 int curvalid
= tok
->inp
- tok
->buf
;
754 int newsize
= curvalid
+ BUFSIZ
;
755 char *newbuf
= tok
->buf
;
756 PyMem_RESIZE(newbuf
, char, newsize
);
757 if (newbuf
== NULL
) {
763 tok
->inp
= tok
->buf
+ curvalid
;
764 tok
->end
= tok
->buf
+ newsize
;
765 tok
->start
= curstart
< 0 ? NULL
:
767 if (decoding_fgets(tok
->inp
,
768 (int)(tok
->end
- tok
->inp
),
770 /* Last line does not end in \n,
772 strcpy(tok
->inp
, "\n");
774 tok
->inp
= strchr(tok
->inp
, '\0');
775 done
= tok
->inp
[-1] == '\n';
777 tok
->cur
= tok
->buf
+ cur
;
779 /* replace "\r\n" with "\n" */
780 /* For Mac we leave the \r, giving a syntax error */
782 if (pt
>= tok
->buf
&& *pt
== '\r') {
789 if (tok
->done
!= E_OK
) {
790 if (tok
->prompt
!= NULL
)
791 PySys_WriteStderr("\n");
800 /* Back-up one character */
803 tok_backup(register struct tok_state
*tok
, register int c
)
806 if (--tok
->cur
< tok
->buf
)
807 Py_FatalError("tok_backup: begin of buffer");
814 /* Return the token corresponding to a single character */
817 PyToken_OneChar(int c
)
820 case '(': return LPAR
;
821 case ')': return RPAR
;
822 case '[': return LSQB
;
823 case ']': return RSQB
;
824 case ':': return COLON
;
825 case ',': return COMMA
;
826 case ';': return SEMI
;
827 case '+': return PLUS
;
828 case '-': return MINUS
;
829 case '*': return STAR
;
830 case '/': return SLASH
;
831 case '|': return VBAR
;
832 case '&': return AMPER
;
833 case '<': return LESS
;
834 case '>': return GREATER
;
835 case '=': return EQUAL
;
836 case '.': return DOT
;
837 case '%': return PERCENT
;
838 case '`': return BACKQUOTE
;
839 case '{': return LBRACE
;
840 case '}': return RBRACE
;
841 case '^': return CIRCUMFLEX
;
842 case '~': return TILDE
;
849 PyToken_TwoChars(int c1
, int c2
)
854 case '=': return EQEQUAL
;
859 case '=': return NOTEQUAL
;
864 case '>': return NOTEQUAL
;
865 case '=': return LESSEQUAL
;
866 case '<': return LEFTSHIFT
;
871 case '=': return GREATEREQUAL
;
872 case '>': return RIGHTSHIFT
;
877 case '=': return PLUSEQUAL
;
882 case '=': return MINEQUAL
;
887 case '*': return DOUBLESTAR
;
888 case '=': return STAREQUAL
;
893 case '/': return DOUBLESLASH
;
894 case '=': return SLASHEQUAL
;
899 case '=': return VBAREQUAL
;
904 case '=': return PERCENTEQUAL
;
909 case '=': return AMPEREQUAL
;
914 case '=': return CIRCUMFLEXEQUAL
;
922 PyToken_ThreeChars(int c1
, int c2
, int c3
)
930 return LEFTSHIFTEQUAL
;
940 return RIGHTSHIFTEQUAL
;
950 return DOUBLESTAREQUAL
;
960 return DOUBLESLASHEQUAL
;
970 indenterror(struct tok_state
*tok
)
973 tok
->done
= E_TABSPACE
;
977 if (tok
->altwarning
) {
978 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
979 "in indentation\n", tok
->filename
);
986 /* Get next token, after space stripping etc. */
989 tok_get(register struct tok_state
*tok
, char **p_start
, char **p_end
)
994 *p_start
= *p_end
= NULL
;
999 /* Get indentation level */
1001 register int col
= 0;
1002 register int altcol
= 0;
1008 else if (c
== '\t') {
1009 col
= (col
/tok
->tabsize
+ 1) * tok
->tabsize
;
1010 altcol
= (altcol
/tok
->alttabsize
+ 1)
1013 else if (c
== '\014') /* Control-L (formfeed) */
1014 col
= altcol
= 0; /* For Emacs users */
1019 if (c
== '#' || c
== '\n') {
1020 /* Lines with only whitespace and/or comments
1021 shouldn't affect the indentation and are
1022 not passed to the parser as NEWLINE tokens,
1023 except *totally* empty lines in interactive
1024 mode, which signal the end of a command group. */
1025 if (col
== 0 && c
== '\n' && tok
->prompt
!= NULL
)
1026 blankline
= 0; /* Let it through */
1028 blankline
= 1; /* Ignore completely */
1029 /* We can't jump back right here since we still
1030 may need to skip to the end of a comment */
1032 if (!blankline
&& tok
->level
== 0) {
1033 if (col
== tok
->indstack
[tok
->indent
]) {
1035 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1036 if (indenterror(tok
))
1040 else if (col
> tok
->indstack
[tok
->indent
]) {
1041 /* Indent -- always one */
1042 if (tok
->indent
+1 >= MAXINDENT
) {
1043 tok
->done
= E_TOODEEP
;
1044 tok
->cur
= tok
->inp
;
1047 if (altcol
<= tok
->altindstack
[tok
->indent
]) {
1048 if (indenterror(tok
))
1052 tok
->indstack
[++tok
->indent
] = col
;
1053 tok
->altindstack
[tok
->indent
] = altcol
;
1055 else /* col < tok->indstack[tok->indent] */ {
1056 /* Dedent -- any number, must be consistent */
1057 while (tok
->indent
> 0 &&
1058 col
< tok
->indstack
[tok
->indent
]) {
1062 if (col
!= tok
->indstack
[tok
->indent
]) {
1063 tok
->done
= E_DEDENT
;
1064 tok
->cur
= tok
->inp
;
1067 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1068 if (indenterror(tok
))
1075 tok
->start
= tok
->cur
;
1077 /* Return pending indents/dedents */
1078 if (tok
->pendin
!= 0) {
1079 if (tok
->pendin
< 0) {
1094 } while (c
== ' ' || c
== '\t' || c
== '\014');
1096 /* Set start of current token */
1097 tok
->start
= tok
->cur
- 1;
1099 /* Skip comment, while looking for tab-setting magic */
1101 static char *tabforms
[] = {
1102 "tab-width:", /* Emacs */
1103 ":tabstop=", /* vim, full form */
1104 ":ts=", /* vim, abbreviated form */
1105 "set tabsize=", /* will vi never die? */
1106 /* more templates can be added here to support other editors */
1112 *tp
++ = c
= tok_nextc(tok
);
1113 } while (c
!= EOF
&& c
!= '\n' &&
1114 tp
- cbuf
+ 1 < sizeof(cbuf
));
1117 cp
< tabforms
+ sizeof(tabforms
)/sizeof(tabforms
[0]);
1119 if ((tp
= strstr(cbuf
, *cp
))) {
1120 int newsize
= atoi(tp
+ strlen(*cp
));
1122 if (newsize
>= 1 && newsize
<= 40) {
1123 tok
->tabsize
= newsize
;
1126 "Tab size set to %d\n",
1131 while (c
!= EOF
&& c
!= '\n')
1135 /* Check for EOF and errors now */
1137 return tok
->done
== E_EOF
? ENDMARKER
: ERRORTOKEN
;
1140 /* Identifier (most frequent token!) */
1141 if (isalpha(c
) || c
== '_') {
1142 /* Process r"", u"" and ur"" */
1147 if (c
== '"' || c
== '\'')
1153 if (c
== 'r' || c
== 'R')
1155 if (c
== '"' || c
== '\'')
1159 while (isalnum(c
) || c
== '_') {
1163 *p_start
= tok
->start
;
1171 if (blankline
|| tok
->level
> 0)
1173 *p_start
= tok
->start
;
1174 *p_end
= tok
->cur
- 1; /* Leave '\n' out of the string */
1182 "File contains \\r characters (incorrect line endings?)\n");
1183 tok
->done
= E_TOKEN
;
1184 tok
->cur
= tok
->inp
;
1188 /* Period or number starting with period? */
1196 *p_start
= tok
->start
;
1205 /* Hex or octal -- maybe. */
1209 #ifndef WITHOUT_COMPLEX
1210 if (c
== 'j' || c
== 'J')
1213 if (c
== 'x' || c
== 'X') {
1217 } while (isxdigit(c
));
1220 int found_decimal
= 0;
1221 /* Octal; c is first char of it */
1222 /* There's no 'isoctdigit' macro, sigh */
1223 while ('0' <= c
&& c
< '8') {
1230 } while (isdigit(c
));
1234 else if (c
== 'e' || c
== 'E')
1236 #ifndef WITHOUT_COMPLEX
1237 else if (c
== 'j' || c
== 'J')
1240 else if (found_decimal
) {
1241 tok
->done
= E_TOKEN
;
1246 if (c
== 'l' || c
== 'L')
1253 } while (isdigit(c
));
1254 if (c
== 'l' || c
== 'L')
1257 /* Accept floating point numbers. */
1263 } while (isdigit(c
));
1265 if (c
== 'e' || c
== 'E') {
1269 if (c
== '+' || c
== '-')
1272 tok
->done
= E_TOKEN
;
1278 } while (isdigit(c
));
1280 #ifndef WITHOUT_COMPLEX
1281 if (c
== 'j' || c
== 'J')
1282 /* Imaginary part */
1289 *p_start
= tok
->start
;
1296 if (c
== '\'' || c
== '"') {
1297 int quote2
= tok
->cur
- tok
->start
+ 1;
1310 tok
->cont_line
= 1; /* multiline string. */
1312 else if (c
== EOF
) {
1317 tok
->cur
= tok
->inp
;
1320 else if (c
== quote
) {
1322 if (tok
->cur
- tok
->start
== quote2
) {
1331 if (!triple
|| tripcount
== 3)
1334 else if (c
== '\\') {
1339 tok
->cur
= tok
->inp
;
1346 *p_start
= tok
->start
;
1351 /* Line continuation */
1355 tok
->done
= E_TOKEN
;
1356 tok
->cur
= tok
->inp
;
1360 goto again
; /* Read next line */
1363 /* Check for two-character token */
1365 int c2
= tok_nextc(tok
);
1366 int token
= PyToken_TwoChars(c
, c2
);
1368 int c3
= tok_nextc(tok
);
1369 int token3
= PyToken_ThreeChars(c
, c2
, c3
);
1373 tok_backup(tok
, c3
);
1375 *p_start
= tok
->start
;
1379 tok_backup(tok
, c2
);
1382 /* Keep track of parentheses nesting level */
1396 /* Punctuation character */
1397 *p_start
= tok
->start
;
1399 return PyToken_OneChar(c
);
1403 PyTokenizer_Get(struct tok_state
*tok
, char **p_start
, char **p_end
)
1405 int result
= tok_get(tok
, p_start
, p_end
);
1406 if (tok
->decoding_erred
) {
1407 result
= ERRORTOKEN
;
1408 tok
->done
= E_DECODE
;
1416 tok_dump(int type
, char *start
, char *end
)
1418 printf("%s", _PyParser_TokenNames
[type
]);
1419 if (type
== NAME
|| type
== NUMBER
|| type
== STRING
|| type
== OP
)
1420 printf("(%.*s)", (int)(end
- start
), start
);