2 /* Tokenizer implementation */
5 #include "pgenheaders.h"
10 #include "tokenizer.h"
14 #include "unicodeobject.h"
15 #include "stringobject.h"
16 #include "fileobject.h"
22 extern char *PyOS_Readline(FILE *, FILE *, char *);
23 /* Return malloc'ed string including trailing \n;
24 empty malloc'ed string for EOF;
25 NULL if interrupted */
27 /* Don't ever change this -- it would break the portability of Python code */
31 static struct tok_state
*tok_new(void);
32 static int tok_nextc(struct tok_state
*tok
);
33 static void tok_backup(struct tok_state
*tok
, int c
);
37 char *_PyParser_TokenNames
[] = {
89 /* This table must match the #defines in token.h! */
96 /* Create and initialize a new tok_state structure */
98 static struct tok_state
*
101 struct tok_state
*tok
= (struct tok_state
*)PyMem_MALLOC(
102 sizeof(struct tok_state
));
105 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= tok
->start
= NULL
;
109 tok
->tabsize
= TABSIZE
;
111 tok
->indstack
[0] = 0;
114 tok
->prompt
= tok
->nextprompt
= NULL
;
117 tok
->filename
= NULL
;
121 tok
->altindstack
[0] = 0;
122 tok
->decoding_state
= 0;
123 tok
->decoding_erred
= 0;
124 tok
->read_coding_spec
= 0;
125 tok
->encoding
= NULL
;
128 tok
->decoding_readline
= NULL
;
129 tok
->decoding_buffer
= NULL
;
135 new_string(const char *s
, Py_ssize_t len
)
137 char* result
= (char *)PyMem_MALLOC(len
+ 1);
138 if (result
!= NULL
) {
139 memcpy(result
, s
, len
);
148 decoding_fgets(char *s
, int size
, struct tok_state
*tok
)
150 return fgets(s
, size
, tok
->fp
);
154 decoding_feof(struct tok_state
*tok
)
156 return feof(tok
->fp
);
160 decode_str(const char *str
, int exec_input
, struct tok_state
*tok
)
162 return new_string(str
, strlen(str
));
168 error_ret(struct tok_state
*tok
) /* XXX */
170 tok
->decoding_erred
= 1;
171 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
) /* see PyTokenizer_Free */
172 PyMem_FREE(tok
->buf
);
174 return NULL
; /* as if it were EOF */
179 get_normal_name(char *s
) /* for utf-8 and latin-1 */
183 for (i
= 0; i
< 12; i
++) {
193 if (strcmp(buf
, "utf-8") == 0 ||
194 strncmp(buf
, "utf-8-", 6) == 0)
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)
207 /* Return the coding spec in S, or NULL if none is found. */
210 get_coding_spec(const char *s
, Py_ssize_t size
)
213 /* Coding spec must be in a comment, and that comment must be
214 * the only statement on the source code line. */
215 for (i
= 0; i
< size
- 6; i
++) {
218 if (s
[i
] != ' ' && s
[i
] != '\t' && s
[i
] != '\014')
221 for (; i
< size
- 6; i
++) { /* XXX inefficient search */
222 const char* t
= s
+ i
;
223 if (strncmp(t
, "coding", 6) == 0) {
224 const char* begin
= NULL
;
226 if (t
[0] != ':' && t
[0] != '=')
230 } while (t
[0] == '\x20' || t
[0] == '\t');
233 while (isalnum(Py_CHARMASK(t
[0])) ||
234 t
[0] == '-' || t
[0] == '_' || t
[0] == '.')
238 char* r
= new_string(begin
, t
- begin
);
239 char* q
= get_normal_name(r
);
242 r
= new_string(q
, strlen(q
));
251 /* Check whether the line contains a coding spec. If it does,
252 invoke the set_readline function for the new encoding.
253 This function receives the tok_state and the new encoding.
254 Return 1 on success, 0 on failure. */
257 check_coding_spec(const char* line
, Py_ssize_t size
, struct tok_state
*tok
,
258 int set_readline(struct tok_state
*, const char *))
264 /* It's a continuation line, so it can't be a coding spec. */
266 cs
= get_coding_spec(line
, size
);
268 tok
->read_coding_spec
= 1;
269 if (tok
->encoding
== NULL
) {
270 assert(tok
->decoding_state
== 1); /* raw */
271 if (strcmp(cs
, "utf-8") == 0 ||
272 strcmp(cs
, "iso-8859-1") == 0) {
275 #ifdef Py_USING_UNICODE
276 r
= set_readline(tok
, cs
);
279 tok
->decoding_state
= -1;
284 /* Without Unicode support, we cannot
285 process the coding spec. Since there
286 won't be any Unicode literals, that
291 } else { /* then, compare cs with BOM */
292 r
= (strcmp(tok
->encoding
, cs
) == 0);
300 PyErr_Format(PyExc_SyntaxError
, "encoding problem: %s", cs
);
305 /* See whether the file starts with a BOM. If it does,
306 invoke the set_readline function with the new encoding.
307 Return 1 on success, 0 on failure. */
310 check_bom(int get_char(struct tok_state
*),
311 void unget_char(int, struct tok_state
*),
312 int set_readline(struct tok_state
*, const char *),
313 struct tok_state
*tok
)
315 int ch
= get_char(tok
);
316 tok
->decoding_state
= 1;
319 } else if (ch
== 0xEF) {
327 /* Disable support for UTF-16 BOMs until a decision
328 is made whether this needs to be supported. */
329 } else if (ch
== 0xFE) {
333 if (!set_readline(tok
, "utf-16-be"))
335 tok
->decoding_state
= -1;
336 } else if (ch
== 0xFF) {
340 if (!set_readline(tok
, "utf-16-le"))
342 tok
->decoding_state
= -1;
348 if (tok
->encoding
!= NULL
)
349 PyMem_FREE(tok
->encoding
);
350 tok
->encoding
= new_string("utf-8", 5); /* resulting is in utf-8 */
353 /* any token beginning with '\xEF', '\xFE', '\xFF' is a bad token */
354 unget_char(0xFF, tok
); /* XXX this will cause a syntax error */
358 /* Read a line of text from TOK into S, using the stream in TOK.
359 Return NULL on failure, else S.
361 On entry, tok->decoding_buffer will be one of:
362 1) NULL: need to call tok->decoding_readline to get a new line
363 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
364 stored the result in tok->decoding_buffer
365 3) PyStringObject *: previous call to fp_readl did not have enough room
366 (in the s buffer) to copy entire contents of the line read
367 by tok->decoding_readline. tok->decoding_buffer has the overflow.
368 In this case, fp_readl is called in a loop (with an expanded buffer)
369 until the buffer ends with a '\n' (or until the end of the file is
370 reached): see tok_nextc and its calls to decoding_fgets.
374 fp_readl(char *s
, int size
, struct tok_state
*tok
)
376 #ifndef Py_USING_UNICODE
377 /* In a non-Unicode built, this should never be called. */
378 Py_FatalError("fp_readl should not be called in this build.");
379 return NULL
; /* Keep compiler happy (not reachable) */
381 PyObject
* utf8
= NULL
;
382 PyObject
* buf
= tok
->decoding_buffer
;
386 /* Ask for one less byte so we can terminate it */
391 buf
= PyObject_CallObject(tok
->decoding_readline
, NULL
);
393 return error_ret(tok
);
395 tok
->decoding_buffer
= NULL
;
396 if (PyString_CheckExact(buf
))
400 utf8
= PyUnicode_AsUTF8String(buf
);
403 return error_ret(tok
);
405 str
= PyString_AsString(utf8
);
406 utf8len
= PyString_GET_SIZE(utf8
);
407 if (utf8len
> size
) {
408 tok
->decoding_buffer
= PyString_FromStringAndSize(str
+size
, utf8len
-size
);
409 if (tok
->decoding_buffer
== NULL
) {
411 return error_ret(tok
);
415 memcpy(s
, str
, utf8len
);
419 return NULL
; /* EOF */
424 /* Set the readline function for TOK to a StreamReader's
425 readline function. The StreamReader is named ENC.
427 This function is called from check_bom and check_coding_spec.
429 ENC is usually identical to the future value of tok->encoding,
430 except for the (currently unsupported) case of UTF-16.
432 Return 1 on success, 0 on failure. */
435 fp_setreadl(struct tok_state
*tok
, const char* enc
)
437 PyObject
*reader
, *stream
, *readline
;
439 /* XXX: constify filename argument. */
440 stream
= PyFile_FromFile(tok
->fp
, (char*)tok
->filename
, "rb", NULL
);
444 reader
= PyCodec_StreamReader(enc
, stream
, NULL
);
449 readline
= PyObject_GetAttrString(reader
, "readline");
451 if (readline
== NULL
)
454 tok
->decoding_readline
= readline
;
458 /* Fetch the next byte from TOK. */
460 static int fp_getc(struct tok_state
*tok
) {
461 return getc(tok
->fp
);
464 /* Unfetch the last byte back into TOK. */
466 static void fp_ungetc(int c
, struct tok_state
*tok
) {
470 /* Read a line of input from TOK. Determine encoding
474 decoding_fgets(char *s
, int size
, struct tok_state
*tok
)
479 if (tok
->decoding_state
< 0) {
480 /* We already have a codec associated with
482 line
= fp_readl(s
, size
, tok
);
484 } else if (tok
->decoding_state
> 0) {
485 /* We want a 'raw' read. */
486 line
= Py_UniversalNewlineFgets(s
, size
,
490 /* We have not yet determined the encoding.
491 If an encoding is found, use the file-pointer
492 reader functions from now on. */
493 if (!check_bom(fp_getc
, fp_ungetc
, fp_setreadl
, tok
))
494 return error_ret(tok
);
495 assert(tok
->decoding_state
!= 0);
498 if (line
!= NULL
&& tok
->lineno
< 2 && !tok
->read_coding_spec
) {
499 if (!check_coding_spec(line
, strlen(line
), tok
, fp_setreadl
)) {
500 return error_ret(tok
);
504 /* The default encoding is ASCII, so make sure we don't have any
505 non-ASCII bytes in it. */
506 if (line
&& !tok
->encoding
) {
508 for (c
= (unsigned char *)line
; *c
; c
++)
516 /* Need to add 1 to the line number, since this line
517 has not been counted, yet. */
519 "Non-ASCII character '\\x%.2x' "
520 "in file %.200s on line %i, "
521 "but no encoding declared; "
522 "see http://www.python.org/peps/pep-0263.html for details",
523 badchar
, tok
->filename
, tok
->lineno
+ 1);
524 PyErr_SetString(PyExc_SyntaxError
, buf
);
525 return error_ret(tok
);
532 decoding_feof(struct tok_state
*tok
)
534 if (tok
->decoding_state
>= 0) {
535 return feof(tok
->fp
);
537 PyObject
* buf
= tok
->decoding_buffer
;
539 buf
= PyObject_CallObject(tok
->decoding_readline
, NULL
);
544 tok
->decoding_buffer
= buf
;
547 return PyObject_Length(buf
) == 0;
551 /* Fetch a byte from TOK, using the string buffer. */
554 buf_getc(struct tok_state
*tok
) {
555 return Py_CHARMASK(*tok
->str
++);
558 /* Unfetch a byte from TOK, using the string buffer. */
561 buf_ungetc(int c
, struct tok_state
*tok
) {
563 assert(Py_CHARMASK(*tok
->str
) == c
); /* tok->cur may point to read-only segment */
566 /* Set the readline function for TOK to ENC. For the string-based
567 tokenizer, this means to just record the encoding. */
570 buf_setreadl(struct tok_state
*tok
, const char* enc
) {
575 /* Return a UTF-8 encoding Python string object from the
576 C byte string STR, which is encoded with ENC. */
578 #ifdef Py_USING_UNICODE
580 translate_into_utf8(const char* str
, const char* enc
) {
582 PyObject
* buf
= PyUnicode_Decode(str
, strlen(str
), enc
, NULL
);
585 utf8
= PyUnicode_AsUTF8String(buf
);
593 translate_newlines(const char *s
, int exec_input
, struct tok_state
*tok
) {
594 int skip_next_lf
= 0, needed_length
= strlen(s
) + 2, final_length
;
597 buf
= PyMem_MALLOC(needed_length
);
602 for (current
= buf
; *s
; s
++, current
++) {
618 /* If this is exec input, add a newline to the end of the string if
619 there isn't one already. */
620 if (exec_input
&& c
!= '\n') {
625 final_length
= current
- buf
+ 1;
626 if (final_length
< needed_length
&& final_length
)
627 /* should never fail */
628 buf
= PyMem_REALLOC(buf
, final_length
);
632 /* Decode a byte string STR for use as the buffer of TOK.
633 Look for encoding declarations inside STR, and record them
637 decode_str(const char *input
, int single
, struct tok_state
*tok
)
639 PyObject
* utf8
= NULL
;
642 const char *newl
[2] = {NULL
, NULL
};
644 tok
->input
= str
= translate_newlines(input
, single
, tok
);
649 if (!check_bom(buf_getc
, buf_ungetc
, buf_setreadl
, tok
))
650 return error_ret(tok
);
651 str
= tok
->str
; /* string after BOM if any */
653 #ifdef Py_USING_UNICODE
654 if (tok
->enc
!= NULL
) {
655 utf8
= translate_into_utf8(str
, tok
->enc
);
657 return error_ret(tok
);
658 str
= PyString_AsString(utf8
);
661 for (s
= str
;; s
++) {
662 if (*s
== '\0') break;
663 else if (*s
== '\n') {
667 if (lineno
== 2) break;
671 /* need to check line 1 and 2 separately since check_coding_spec
672 assumes a single line as input */
674 if (!check_coding_spec(str
, newl
[0] - str
, tok
, buf_setreadl
))
675 return error_ret(tok
);
676 if (tok
->enc
== NULL
&& newl
[1]) {
677 if (!check_coding_spec(newl
[0]+1, newl
[1] - newl
[0],
679 return error_ret(tok
);
682 #ifdef Py_USING_UNICODE
683 if (tok
->enc
!= NULL
) {
684 assert(utf8
== NULL
);
685 utf8
= translate_into_utf8(str
, tok
->enc
);
687 return error_ret(tok
);
688 str
= PyString_AsString(utf8
);
691 assert(tok
->decoding_buffer
== NULL
);
692 tok
->decoding_buffer
= utf8
; /* CAUTION */
698 /* Set up tokenizer for string */
701 PyTokenizer_FromString(const char *str
, int exec_input
)
703 struct tok_state
*tok
= tok_new();
706 str
= (char *)decode_str(str
, exec_input
, tok
);
708 PyTokenizer_Free(tok
);
712 /* XXX: constify members. */
713 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= (char*)str
;
718 /* Set up tokenizer for file */
721 PyTokenizer_FromFile(FILE *fp
, char *ps1
, char *ps2
)
723 struct tok_state
*tok
= tok_new();
726 if ((tok
->buf
= (char *)PyMem_MALLOC(BUFSIZ
)) == NULL
) {
727 PyTokenizer_Free(tok
);
730 tok
->cur
= tok
->inp
= tok
->buf
;
731 tok
->end
= tok
->buf
+ BUFSIZ
;
734 tok
->nextprompt
= ps2
;
739 /* Free a tok_state structure */
742 PyTokenizer_Free(struct tok_state
*tok
)
744 if (tok
->encoding
!= NULL
)
745 PyMem_FREE(tok
->encoding
);
747 Py_XDECREF(tok
->decoding_readline
);
748 Py_XDECREF(tok
->decoding_buffer
);
750 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
)
751 PyMem_FREE(tok
->buf
);
753 PyMem_FREE((char *)tok
->input
);
757 #if !defined(PGEN) && defined(Py_USING_UNICODE)
759 tok_stdin_decode(struct tok_state
*tok
, char **inp
)
761 PyObject
*enc
, *sysstdin
, *decoded
, *utf8
;
762 const char *encoding
;
765 if (PySys_GetFile((char *)"stdin", NULL
) != stdin
)
767 sysstdin
= PySys_GetObject("stdin");
768 if (sysstdin
== NULL
|| !PyFile_Check(sysstdin
))
771 enc
= ((PyFileObject
*)sysstdin
)->f_encoding
;
772 if (enc
== NULL
|| !PyString_Check(enc
))
776 encoding
= PyString_AsString(enc
);
777 decoded
= PyUnicode_Decode(*inp
, strlen(*inp
), encoding
, NULL
);
781 utf8
= PyUnicode_AsEncodedString(decoded
, "utf-8", NULL
);
786 assert(PyString_Check(utf8
));
787 converted
= new_string(PyString_AS_STRING(utf8
),
788 PyString_GET_SIZE(utf8
));
790 if (converted
== NULL
)
795 if (tok
->encoding
!= NULL
)
796 PyMem_FREE(tok
->encoding
);
797 tok
->encoding
= new_string(encoding
, strlen(encoding
));
798 if (tok
->encoding
== NULL
)
810 /* Fallback to iso-8859-1: for backward compatibility */
817 /* Get next char, updating state; error code goes into tok->done */
820 tok_nextc(register struct tok_state
*tok
)
823 if (tok
->cur
!= tok
->inp
) {
824 return Py_CHARMASK(*tok
->cur
++); /* Fast path */
826 if (tok
->done
!= E_OK
)
828 if (tok
->fp
== NULL
) {
829 char *end
= strchr(tok
->inp
, '\n');
833 end
= strchr(tok
->inp
, '\0');
834 if (end
== tok
->inp
) {
839 if (tok
->start
== NULL
)
841 tok
->line_start
= tok
->cur
;
844 return Py_CHARMASK(*tok
->cur
++);
846 if (tok
->prompt
!= NULL
) {
847 char *newtok
= PyOS_Readline(stdin
, stdout
, tok
->prompt
);
848 if (tok
->nextprompt
!= NULL
)
849 tok
->prompt
= tok
->nextprompt
;
852 else if (*newtok
== '\0') {
856 #if !defined(PGEN) && defined(Py_USING_UNICODE)
857 else if (tok_stdin_decode(tok
, &newtok
) != 0)
860 else if (tok
->start
!= NULL
) {
861 size_t start
= tok
->start
- tok
->buf
;
862 size_t oldlen
= tok
->cur
- tok
->buf
;
863 size_t newlen
= oldlen
+ strlen(newtok
);
864 char *buf
= tok
->buf
;
865 buf
= (char *)PyMem_REALLOC(buf
, newlen
+1);
868 PyMem_FREE(tok
->buf
);
875 tok
->cur
= tok
->buf
+ oldlen
;
876 tok
->line_start
= tok
->cur
;
877 strcpy(tok
->buf
+ oldlen
, newtok
);
879 tok
->inp
= tok
->buf
+ newlen
;
880 tok
->end
= tok
->inp
+ 1;
881 tok
->start
= tok
->buf
+ start
;
885 if (tok
->buf
!= NULL
)
886 PyMem_FREE(tok
->buf
);
888 tok
->line_start
= tok
->buf
;
890 tok
->line_start
= tok
->buf
;
891 tok
->inp
= strchr(tok
->buf
, '\0');
892 tok
->end
= tok
->inp
+ 1;
899 if (tok
->start
== NULL
) {
900 if (tok
->buf
== NULL
) {
902 PyMem_MALLOC(BUFSIZ
);
903 if (tok
->buf
== NULL
) {
907 tok
->end
= tok
->buf
+ BUFSIZ
;
909 if (decoding_fgets(tok
->buf
, (int)(tok
->end
- tok
->buf
),
916 tok
->inp
= strchr(tok
->buf
, '\0');
917 done
= tok
->inp
[-1] == '\n';
921 cur
= tok
->cur
- tok
->buf
;
922 if (decoding_feof(tok
)) {
930 /* Read until '\n' or EOF */
932 Py_ssize_t curstart
= tok
->start
== NULL
? -1 :
933 tok
->start
- tok
->buf
;
934 Py_ssize_t curvalid
= tok
->inp
- tok
->buf
;
935 Py_ssize_t newsize
= curvalid
+ BUFSIZ
;
936 char *newbuf
= tok
->buf
;
937 newbuf
= (char *)PyMem_REALLOC(newbuf
,
939 if (newbuf
== NULL
) {
945 tok
->inp
= tok
->buf
+ curvalid
;
946 tok
->end
= tok
->buf
+ newsize
;
947 tok
->start
= curstart
< 0 ? NULL
:
949 if (decoding_fgets(tok
->inp
,
950 (int)(tok
->end
- tok
->inp
),
952 /* Break out early on decoding
953 errors, as tok->buf will be NULL
955 if (tok
->decoding_erred
)
957 /* Last line does not end in \n,
959 strcpy(tok
->inp
, "\n");
961 tok
->inp
= strchr(tok
->inp
, '\0');
962 done
= tok
->inp
[-1] == '\n';
964 if (tok
->buf
!= NULL
) {
965 tok
->cur
= tok
->buf
+ cur
;
966 tok
->line_start
= tok
->cur
;
967 /* replace "\r\n" with "\n" */
968 /* For Mac leave the \r, giving a syntax error */
970 if (pt
>= tok
->buf
&& *pt
== '\r') {
977 if (tok
->done
!= E_OK
) {
978 if (tok
->prompt
!= NULL
)
979 PySys_WriteStderr("\n");
988 /* Back-up one character */
991 tok_backup(register struct tok_state
*tok
, register int c
)
994 if (--tok
->cur
< tok
->buf
)
995 Py_FatalError("tok_backup: beginning of buffer");
1002 /* Return the token corresponding to a single character */
1005 PyToken_OneChar(int c
)
1008 case '(': return LPAR
;
1009 case ')': return RPAR
;
1010 case '[': return LSQB
;
1011 case ']': return RSQB
;
1012 case ':': return COLON
;
1013 case ',': return COMMA
;
1014 case ';': return SEMI
;
1015 case '+': return PLUS
;
1016 case '-': return MINUS
;
1017 case '*': return STAR
;
1018 case '/': return SLASH
;
1019 case '|': return VBAR
;
1020 case '&': return AMPER
;
1021 case '<': return LESS
;
1022 case '>': return GREATER
;
1023 case '=': return EQUAL
;
1024 case '.': return DOT
;
1025 case '%': return PERCENT
;
1026 case '`': return BACKQUOTE
;
1027 case '{': return LBRACE
;
1028 case '}': return RBRACE
;
1029 case '^': return CIRCUMFLEX
;
1030 case '~': return TILDE
;
1031 case '@': return AT
;
1038 PyToken_TwoChars(int c1
, int c2
)
1043 case '=': return EQEQUAL
;
1048 case '=': return NOTEQUAL
;
1053 case '>': return NOTEQUAL
;
1054 case '=': return LESSEQUAL
;
1055 case '<': return LEFTSHIFT
;
1060 case '=': return GREATEREQUAL
;
1061 case '>': return RIGHTSHIFT
;
1066 case '=': return PLUSEQUAL
;
1071 case '=': return MINEQUAL
;
1076 case '*': return DOUBLESTAR
;
1077 case '=': return STAREQUAL
;
1082 case '/': return DOUBLESLASH
;
1083 case '=': return SLASHEQUAL
;
1088 case '=': return VBAREQUAL
;
1093 case '=': return PERCENTEQUAL
;
1098 case '=': return AMPEREQUAL
;
1103 case '=': return CIRCUMFLEXEQUAL
;
1111 PyToken_ThreeChars(int c1
, int c2
, int c3
)
1119 return LEFTSHIFTEQUAL
;
1129 return RIGHTSHIFTEQUAL
;
1139 return DOUBLESTAREQUAL
;
1149 return DOUBLESLASHEQUAL
;
1159 indenterror(struct tok_state
*tok
)
1161 if (tok
->alterror
) {
1162 tok
->done
= E_TABSPACE
;
1163 tok
->cur
= tok
->inp
;
1166 if (tok
->altwarning
) {
1167 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1168 "in indentation\n", tok
->filename
);
1169 tok
->altwarning
= 0;
1175 /* Get next token, after space stripping etc. */
1178 tok_get(register struct tok_state
*tok
, char **p_start
, char **p_end
)
1183 *p_start
= *p_end
= NULL
;
1188 /* Get indentation level */
1190 register int col
= 0;
1191 register int altcol
= 0;
1197 else if (c
== '\t') {
1198 col
= (col
/tok
->tabsize
+ 1) * tok
->tabsize
;
1199 altcol
= (altcol
/tok
->alttabsize
+ 1)
1202 else if (c
== '\014') /* Control-L (formfeed) */
1203 col
= altcol
= 0; /* For Emacs users */
1208 if (c
== '#' || c
== '\n') {
1209 /* Lines with only whitespace and/or comments
1210 shouldn't affect the indentation and are
1211 not passed to the parser as NEWLINE tokens,
1212 except *totally* empty lines in interactive
1213 mode, which signal the end of a command group. */
1214 if (col
== 0 && c
== '\n' && tok
->prompt
!= NULL
)
1215 blankline
= 0; /* Let it through */
1217 blankline
= 1; /* Ignore completely */
1218 /* We can't jump back right here since we still
1219 may need to skip to the end of a comment */
1221 if (!blankline
&& tok
->level
== 0) {
1222 if (col
== tok
->indstack
[tok
->indent
]) {
1224 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1225 if (indenterror(tok
))
1229 else if (col
> tok
->indstack
[tok
->indent
]) {
1230 /* Indent -- always one */
1231 if (tok
->indent
+1 >= MAXINDENT
) {
1232 tok
->done
= E_TOODEEP
;
1233 tok
->cur
= tok
->inp
;
1236 if (altcol
<= tok
->altindstack
[tok
->indent
]) {
1237 if (indenterror(tok
))
1241 tok
->indstack
[++tok
->indent
] = col
;
1242 tok
->altindstack
[tok
->indent
] = altcol
;
1244 else /* col < tok->indstack[tok->indent] */ {
1245 /* Dedent -- any number, must be consistent */
1246 while (tok
->indent
> 0 &&
1247 col
< tok
->indstack
[tok
->indent
]) {
1251 if (col
!= tok
->indstack
[tok
->indent
]) {
1252 tok
->done
= E_DEDENT
;
1253 tok
->cur
= tok
->inp
;
1256 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1257 if (indenterror(tok
))
1264 tok
->start
= tok
->cur
;
1266 /* Return pending indents/dedents */
1267 if (tok
->pendin
!= 0) {
1268 if (tok
->pendin
< 0) {
1283 } while (c
== ' ' || c
== '\t' || c
== '\014');
1285 /* Set start of current token */
1286 tok
->start
= tok
->cur
- 1;
1288 /* Skip comment, while looking for tab-setting magic */
1290 static char *tabforms
[] = {
1291 "tab-width:", /* Emacs */
1292 ":tabstop=", /* vim, full form */
1293 ":ts=", /* vim, abbreviated form */
1294 "set tabsize=", /* will vi never die? */
1295 /* more templates can be added here to support other editors */
1301 *tp
++ = c
= tok_nextc(tok
);
1302 } while (c
!= EOF
&& c
!= '\n' &&
1303 (size_t)(tp
- cbuf
+ 1) < sizeof(cbuf
));
1306 cp
< tabforms
+ sizeof(tabforms
)/sizeof(tabforms
[0]);
1308 if ((tp
= strstr(cbuf
, *cp
))) {
1309 int newsize
= atoi(tp
+ strlen(*cp
));
1311 if (newsize
>= 1 && newsize
<= 40) {
1312 tok
->tabsize
= newsize
;
1315 "Tab size set to %d\n",
1320 while (c
!= EOF
&& c
!= '\n')
1324 /* Check for EOF and errors now */
1326 return tok
->done
== E_EOF
? ENDMARKER
: ERRORTOKEN
;
1329 /* Identifier (most frequent token!) */
1330 if (isalpha(c
) || c
== '_') {
1331 /* Process r"", u"" and ur"" */
1336 if (c
== 'r' || c
== 'R')
1338 if (c
== '"' || c
== '\'')
1344 if (c
== '"' || c
== '\'')
1350 if (c
== 'r' || c
== 'R')
1352 if (c
== '"' || c
== '\'')
1356 while (isalnum(c
) || c
== '_') {
1360 *p_start
= tok
->start
;
1368 if (blankline
|| tok
->level
> 0)
1370 *p_start
= tok
->start
;
1371 *p_end
= tok
->cur
- 1; /* Leave '\n' out of the string */
1376 /* Period or number starting with period? */
1384 *p_start
= tok
->start
;
1393 /* Hex, octal or binary -- maybe. */
1397 #ifndef WITHOUT_COMPLEX
1398 if (c
== 'j' || c
== 'J')
1401 if (c
== 'x' || c
== 'X') {
1406 tok
->done
= E_TOKEN
;
1412 } while (isxdigit(c
));
1414 else if (c
== 'o' || c
== 'O') {
1417 if (c
< '0' || c
>= '8') {
1418 tok
->done
= E_TOKEN
;
1424 } while ('0' <= c
&& c
< '8');
1426 else if (c
== 'b' || c
== 'B') {
1429 if (c
!= '0' && c
!= '1') {
1430 tok
->done
= E_TOKEN
;
1436 } while (c
== '0' || c
== '1');
1439 int found_decimal
= 0;
1440 /* Octal; c is first char of it */
1441 /* There's no 'isoctdigit' macro, sigh */
1442 while ('0' <= c
&& c
< '8') {
1449 } while (isdigit(c
));
1453 else if (c
== 'e' || c
== 'E')
1455 #ifndef WITHOUT_COMPLEX
1456 else if (c
== 'j' || c
== 'J')
1459 else if (found_decimal
) {
1460 tok
->done
= E_TOKEN
;
1465 if (c
== 'l' || c
== 'L')
1472 } while (isdigit(c
));
1473 if (c
== 'l' || c
== 'L')
1476 /* Accept floating point numbers. */
1482 } while (isdigit(c
));
1484 if (c
== 'e' || c
== 'E') {
1488 if (c
== '+' || c
== '-')
1491 tok
->done
= E_TOKEN
;
1497 } while (isdigit(c
));
1499 #ifndef WITHOUT_COMPLEX
1500 if (c
== 'j' || c
== 'J')
1501 /* Imaginary part */
1508 *p_start
= tok
->start
;
1515 if (c
== '\'' || c
== '"') {
1516 Py_ssize_t quote2
= tok
->cur
- tok
->start
+ 1;
1529 tok
->cont_line
= 1; /* multiline string. */
1531 else if (c
== EOF
) {
1536 tok
->cur
= tok
->inp
;
1539 else if (c
== quote
) {
1541 if (tok
->cur
- tok
->start
== quote2
) {
1550 if (!triple
|| tripcount
== 3)
1553 else if (c
== '\\') {
1558 tok
->cur
= tok
->inp
;
1565 *p_start
= tok
->start
;
1570 /* Line continuation */
1574 tok
->done
= E_LINECONT
;
1575 tok
->cur
= tok
->inp
;
1579 goto again
; /* Read next line */
1582 /* Check for two-character token */
1584 int c2
= tok_nextc(tok
);
1585 int token
= PyToken_TwoChars(c
, c2
);
1587 if (Py_Py3kWarningFlag
&& token
== NOTEQUAL
&& c
== '<') {
1588 if (PyErr_WarnExplicit(PyExc_DeprecationWarning
,
1589 "<> not supported in 3.x; use !=",
1590 tok
->filename
, tok
->lineno
,
1597 int c3
= tok_nextc(tok
);
1598 int token3
= PyToken_ThreeChars(c
, c2
, c3
);
1602 tok_backup(tok
, c3
);
1604 *p_start
= tok
->start
;
1608 tok_backup(tok
, c2
);
1611 /* Keep track of parentheses nesting level */
1625 /* Punctuation character */
1626 *p_start
= tok
->start
;
1628 return PyToken_OneChar(c
);
1632 PyTokenizer_Get(struct tok_state
*tok
, char **p_start
, char **p_end
)
1634 int result
= tok_get(tok
, p_start
, p_end
);
1635 if (tok
->decoding_erred
) {
1636 result
= ERRORTOKEN
;
1637 tok
->done
= E_DECODE
;
1642 /* This function is only called from parsetok. However, it cannot live
1643 there, as it must be empty for PGEN, and we can check for PGEN only
1646 #if defined(PGEN) || !defined(Py_USING_UNICODE)
1648 PyTokenizer_RestoreEncoding(struct tok_state
* tok
, int len
, int* offset
)
1653 #ifdef Py_USING_UNICODE
1655 dec_utf8(const char *enc
, const char *text
, size_t len
) {
1656 PyObject
*ret
= NULL
;
1657 PyObject
*unicode_text
= PyUnicode_DecodeUTF8(text
, len
, "replace");
1659 ret
= PyUnicode_AsEncodedString(unicode_text
, enc
, "replace");
1660 Py_DECREF(unicode_text
);
1668 PyTokenizer_RestoreEncoding(struct tok_state
* tok
, int len
, int *offset
)
1671 if (tok
->encoding
) {
1672 /* convert source to original encondig */
1673 PyObject
*lineobj
= dec_utf8(tok
->encoding
, tok
->buf
, len
);
1674 if (lineobj
!= NULL
) {
1675 int linelen
= PyString_Size(lineobj
);
1676 const char *line
= PyString_AsString(lineobj
);
1677 text
= PyObject_MALLOC(linelen
+ 1);
1678 if (text
!= NULL
&& line
!= NULL
) {
1680 strncpy(text
, line
, linelen
);
1681 text
[linelen
] = '\0';
1685 /* adjust error offset */
1687 PyObject
*offsetobj
= dec_utf8(tok
->encoding
,
1688 tok
->buf
, *offset
-1);
1690 *offset
= PyString_Size(offsetobj
) + 1;
1691 Py_DECREF(offsetobj
);
1700 #endif /* defined(Py_USING_UNICODE) */
1707 tok_dump(int type
, char *start
, char *end
)
1709 printf("%s", _PyParser_TokenNames
[type
]);
1710 if (type
== NAME
|| type
== NUMBER
|| type
== STRING
|| type
== OP
)
1711 printf("(%.*s)", (int)(end
- start
), start
);