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, length
= strlen(s
), final_length
;
597 buf
= PyMem_MALLOC(length
+ 2);
602 for (current
= buf
; (c
= *s
++);) {
619 /* If this is exec input, add a newline to the end of the file if
620 there isn't one already. */
621 if (exec_input
&& *current
!= '\n') {
626 final_length
= current
- buf
;
627 if (final_length
< length
&& final_length
)
628 /* should never fail */
629 buf
= PyMem_REALLOC(buf
, final_length
+ 1);
633 /* Decode a byte string STR for use as the buffer of TOK.
634 Look for encoding declarations inside STR, and record them
638 decode_str(const char *input
, int single
, struct tok_state
*tok
)
640 PyObject
* utf8
= NULL
;
643 const char *newl
[2] = {NULL
, NULL
};
645 tok
->input
= str
= translate_newlines(input
, single
, tok
);
650 if (!check_bom(buf_getc
, buf_ungetc
, buf_setreadl
, tok
))
651 return error_ret(tok
);
652 str
= tok
->str
; /* string after BOM if any */
654 #ifdef Py_USING_UNICODE
655 if (tok
->enc
!= NULL
) {
656 utf8
= translate_into_utf8(str
, tok
->enc
);
658 return error_ret(tok
);
659 str
= PyString_AsString(utf8
);
662 for (s
= str
;; s
++) {
663 if (*s
== '\0') break;
664 else if (*s
== '\n') {
668 if (lineno
== 2) break;
672 /* need to check line 1 and 2 separately since check_coding_spec
673 assumes a single line as input */
675 if (!check_coding_spec(str
, newl
[0] - str
, tok
, buf_setreadl
))
676 return error_ret(tok
);
677 if (tok
->enc
== NULL
&& newl
[1]) {
678 if (!check_coding_spec(newl
[0]+1, newl
[1] - newl
[0],
680 return error_ret(tok
);
683 #ifdef Py_USING_UNICODE
684 if (tok
->enc
!= NULL
) {
685 assert(utf8
== NULL
);
686 utf8
= translate_into_utf8(str
, tok
->enc
);
688 return error_ret(tok
);
689 str
= PyString_AsString(utf8
);
692 assert(tok
->decoding_buffer
== NULL
);
693 tok
->decoding_buffer
= utf8
; /* CAUTION */
699 /* Set up tokenizer for string */
702 PyTokenizer_FromString(const char *str
, int exec_input
)
704 struct tok_state
*tok
= tok_new();
707 str
= (char *)decode_str(str
, exec_input
, tok
);
709 PyTokenizer_Free(tok
);
713 /* XXX: constify members. */
714 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= (char*)str
;
719 /* Set up tokenizer for file */
722 PyTokenizer_FromFile(FILE *fp
, char *ps1
, char *ps2
)
724 struct tok_state
*tok
= tok_new();
727 if ((tok
->buf
= (char *)PyMem_MALLOC(BUFSIZ
)) == NULL
) {
728 PyTokenizer_Free(tok
);
731 tok
->cur
= tok
->inp
= tok
->buf
;
732 tok
->end
= tok
->buf
+ BUFSIZ
;
735 tok
->nextprompt
= ps2
;
740 /* Free a tok_state structure */
743 PyTokenizer_Free(struct tok_state
*tok
)
745 if (tok
->encoding
!= NULL
)
746 PyMem_FREE(tok
->encoding
);
748 Py_XDECREF(tok
->decoding_readline
);
749 Py_XDECREF(tok
->decoding_buffer
);
751 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
)
752 PyMem_FREE(tok
->buf
);
754 PyMem_FREE((char *)tok
->input
);
758 #if !defined(PGEN) && defined(Py_USING_UNICODE)
760 tok_stdin_decode(struct tok_state
*tok
, char **inp
)
762 PyObject
*enc
, *sysstdin
, *decoded
, *utf8
;
763 const char *encoding
;
766 if (PySys_GetFile((char *)"stdin", NULL
) != stdin
)
768 sysstdin
= PySys_GetObject("stdin");
769 if (sysstdin
== NULL
|| !PyFile_Check(sysstdin
))
772 enc
= ((PyFileObject
*)sysstdin
)->f_encoding
;
773 if (enc
== NULL
|| !PyString_Check(enc
))
777 encoding
= PyString_AsString(enc
);
778 decoded
= PyUnicode_Decode(*inp
, strlen(*inp
), encoding
, NULL
);
782 utf8
= PyUnicode_AsEncodedString(decoded
, "utf-8", NULL
);
787 assert(PyString_Check(utf8
));
788 converted
= new_string(PyString_AS_STRING(utf8
),
789 PyString_GET_SIZE(utf8
));
791 if (converted
== NULL
)
796 if (tok
->encoding
!= NULL
)
797 PyMem_FREE(tok
->encoding
);
798 tok
->encoding
= new_string(encoding
, strlen(encoding
));
799 if (tok
->encoding
== NULL
)
811 /* Fallback to iso-8859-1: for backward compatibility */
818 /* Get next char, updating state; error code goes into tok->done */
821 tok_nextc(register struct tok_state
*tok
)
824 if (tok
->cur
!= tok
->inp
) {
825 return Py_CHARMASK(*tok
->cur
++); /* Fast path */
827 if (tok
->done
!= E_OK
)
829 if (tok
->fp
== NULL
) {
830 char *end
= strchr(tok
->inp
, '\n');
834 end
= strchr(tok
->inp
, '\0');
835 if (end
== tok
->inp
) {
840 if (tok
->start
== NULL
)
842 tok
->line_start
= tok
->cur
;
845 return Py_CHARMASK(*tok
->cur
++);
847 if (tok
->prompt
!= NULL
) {
848 char *newtok
= PyOS_Readline(stdin
, stdout
, tok
->prompt
);
849 if (tok
->nextprompt
!= NULL
)
850 tok
->prompt
= tok
->nextprompt
;
853 else if (*newtok
== '\0') {
857 #if !defined(PGEN) && defined(Py_USING_UNICODE)
858 else if (tok_stdin_decode(tok
, &newtok
) != 0)
861 else if (tok
->start
!= NULL
) {
862 size_t start
= tok
->start
- tok
->buf
;
863 size_t oldlen
= tok
->cur
- tok
->buf
;
864 size_t newlen
= oldlen
+ strlen(newtok
);
865 char *buf
= tok
->buf
;
866 buf
= (char *)PyMem_REALLOC(buf
, newlen
+1);
869 PyMem_FREE(tok
->buf
);
876 tok
->cur
= tok
->buf
+ oldlen
;
877 tok
->line_start
= tok
->cur
;
878 strcpy(tok
->buf
+ oldlen
, newtok
);
880 tok
->inp
= tok
->buf
+ newlen
;
881 tok
->end
= tok
->inp
+ 1;
882 tok
->start
= tok
->buf
+ start
;
886 if (tok
->buf
!= NULL
)
887 PyMem_FREE(tok
->buf
);
889 tok
->line_start
= tok
->buf
;
891 tok
->line_start
= tok
->buf
;
892 tok
->inp
= strchr(tok
->buf
, '\0');
893 tok
->end
= tok
->inp
+ 1;
900 if (tok
->start
== NULL
) {
901 if (tok
->buf
== NULL
) {
903 PyMem_MALLOC(BUFSIZ
);
904 if (tok
->buf
== NULL
) {
908 tok
->end
= tok
->buf
+ BUFSIZ
;
910 if (decoding_fgets(tok
->buf
, (int)(tok
->end
- tok
->buf
),
917 tok
->inp
= strchr(tok
->buf
, '\0');
918 done
= tok
->inp
[-1] == '\n';
922 cur
= tok
->cur
- tok
->buf
;
923 if (decoding_feof(tok
)) {
931 /* Read until '\n' or EOF */
933 Py_ssize_t curstart
= tok
->start
== NULL
? -1 :
934 tok
->start
- tok
->buf
;
935 Py_ssize_t curvalid
= tok
->inp
- tok
->buf
;
936 Py_ssize_t newsize
= curvalid
+ BUFSIZ
;
937 char *newbuf
= tok
->buf
;
938 newbuf
= (char *)PyMem_REALLOC(newbuf
,
940 if (newbuf
== NULL
) {
946 tok
->inp
= tok
->buf
+ curvalid
;
947 tok
->end
= tok
->buf
+ newsize
;
948 tok
->start
= curstart
< 0 ? NULL
:
950 if (decoding_fgets(tok
->inp
,
951 (int)(tok
->end
- tok
->inp
),
953 /* Break out early on decoding
954 errors, as tok->buf will be NULL
956 if (tok
->decoding_erred
)
958 /* Last line does not end in \n,
960 strcpy(tok
->inp
, "\n");
962 tok
->inp
= strchr(tok
->inp
, '\0');
963 done
= tok
->inp
[-1] == '\n';
965 if (tok
->buf
!= NULL
) {
966 tok
->cur
= tok
->buf
+ cur
;
967 tok
->line_start
= tok
->cur
;
968 /* replace "\r\n" with "\n" */
969 /* For Mac leave the \r, giving a syntax error */
971 if (pt
>= tok
->buf
&& *pt
== '\r') {
978 if (tok
->done
!= E_OK
) {
979 if (tok
->prompt
!= NULL
)
980 PySys_WriteStderr("\n");
989 /* Back-up one character */
992 tok_backup(register struct tok_state
*tok
, register int c
)
995 if (--tok
->cur
< tok
->buf
)
996 Py_FatalError("tok_backup: beginning of buffer");
1003 /* Return the token corresponding to a single character */
1006 PyToken_OneChar(int c
)
1009 case '(': return LPAR
;
1010 case ')': return RPAR
;
1011 case '[': return LSQB
;
1012 case ']': return RSQB
;
1013 case ':': return COLON
;
1014 case ',': return COMMA
;
1015 case ';': return SEMI
;
1016 case '+': return PLUS
;
1017 case '-': return MINUS
;
1018 case '*': return STAR
;
1019 case '/': return SLASH
;
1020 case '|': return VBAR
;
1021 case '&': return AMPER
;
1022 case '<': return LESS
;
1023 case '>': return GREATER
;
1024 case '=': return EQUAL
;
1025 case '.': return DOT
;
1026 case '%': return PERCENT
;
1027 case '`': return BACKQUOTE
;
1028 case '{': return LBRACE
;
1029 case '}': return RBRACE
;
1030 case '^': return CIRCUMFLEX
;
1031 case '~': return TILDE
;
1032 case '@': return AT
;
1039 PyToken_TwoChars(int c1
, int c2
)
1044 case '=': return EQEQUAL
;
1049 case '=': return NOTEQUAL
;
1054 case '>': return NOTEQUAL
;
1055 case '=': return LESSEQUAL
;
1056 case '<': return LEFTSHIFT
;
1061 case '=': return GREATEREQUAL
;
1062 case '>': return RIGHTSHIFT
;
1067 case '=': return PLUSEQUAL
;
1072 case '=': return MINEQUAL
;
1077 case '*': return DOUBLESTAR
;
1078 case '=': return STAREQUAL
;
1083 case '/': return DOUBLESLASH
;
1084 case '=': return SLASHEQUAL
;
1089 case '=': return VBAREQUAL
;
1094 case '=': return PERCENTEQUAL
;
1099 case '=': return AMPEREQUAL
;
1104 case '=': return CIRCUMFLEXEQUAL
;
1112 PyToken_ThreeChars(int c1
, int c2
, int c3
)
1120 return LEFTSHIFTEQUAL
;
1130 return RIGHTSHIFTEQUAL
;
1140 return DOUBLESTAREQUAL
;
1150 return DOUBLESLASHEQUAL
;
1160 indenterror(struct tok_state
*tok
)
1162 if (tok
->alterror
) {
1163 tok
->done
= E_TABSPACE
;
1164 tok
->cur
= tok
->inp
;
1167 if (tok
->altwarning
) {
1168 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1169 "in indentation\n", tok
->filename
);
1170 tok
->altwarning
= 0;
1176 /* Get next token, after space stripping etc. */
1179 tok_get(register struct tok_state
*tok
, char **p_start
, char **p_end
)
1184 *p_start
= *p_end
= NULL
;
1189 /* Get indentation level */
1191 register int col
= 0;
1192 register int altcol
= 0;
1198 else if (c
== '\t') {
1199 col
= (col
/tok
->tabsize
+ 1) * tok
->tabsize
;
1200 altcol
= (altcol
/tok
->alttabsize
+ 1)
1203 else if (c
== '\014') /* Control-L (formfeed) */
1204 col
= altcol
= 0; /* For Emacs users */
1209 if (c
== '#' || c
== '\n') {
1210 /* Lines with only whitespace and/or comments
1211 shouldn't affect the indentation and are
1212 not passed to the parser as NEWLINE tokens,
1213 except *totally* empty lines in interactive
1214 mode, which signal the end of a command group. */
1215 if (col
== 0 && c
== '\n' && tok
->prompt
!= NULL
)
1216 blankline
= 0; /* Let it through */
1218 blankline
= 1; /* Ignore completely */
1219 /* We can't jump back right here since we still
1220 may need to skip to the end of a comment */
1222 if (!blankline
&& tok
->level
== 0) {
1223 if (col
== tok
->indstack
[tok
->indent
]) {
1225 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1226 if (indenterror(tok
))
1230 else if (col
> tok
->indstack
[tok
->indent
]) {
1231 /* Indent -- always one */
1232 if (tok
->indent
+1 >= MAXINDENT
) {
1233 tok
->done
= E_TOODEEP
;
1234 tok
->cur
= tok
->inp
;
1237 if (altcol
<= tok
->altindstack
[tok
->indent
]) {
1238 if (indenterror(tok
))
1242 tok
->indstack
[++tok
->indent
] = col
;
1243 tok
->altindstack
[tok
->indent
] = altcol
;
1245 else /* col < tok->indstack[tok->indent] */ {
1246 /* Dedent -- any number, must be consistent */
1247 while (tok
->indent
> 0 &&
1248 col
< tok
->indstack
[tok
->indent
]) {
1252 if (col
!= tok
->indstack
[tok
->indent
]) {
1253 tok
->done
= E_DEDENT
;
1254 tok
->cur
= tok
->inp
;
1257 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1258 if (indenterror(tok
))
1265 tok
->start
= tok
->cur
;
1267 /* Return pending indents/dedents */
1268 if (tok
->pendin
!= 0) {
1269 if (tok
->pendin
< 0) {
1284 } while (c
== ' ' || c
== '\t' || c
== '\014');
1286 /* Set start of current token */
1287 tok
->start
= tok
->cur
- 1;
1289 /* Skip comment, while looking for tab-setting magic */
1291 static char *tabforms
[] = {
1292 "tab-width:", /* Emacs */
1293 ":tabstop=", /* vim, full form */
1294 ":ts=", /* vim, abbreviated form */
1295 "set tabsize=", /* will vi never die? */
1296 /* more templates can be added here to support other editors */
1302 *tp
++ = c
= tok_nextc(tok
);
1303 } while (c
!= EOF
&& c
!= '\n' &&
1304 (size_t)(tp
- cbuf
+ 1) < sizeof(cbuf
));
1307 cp
< tabforms
+ sizeof(tabforms
)/sizeof(tabforms
[0]);
1309 if ((tp
= strstr(cbuf
, *cp
))) {
1310 int newsize
= atoi(tp
+ strlen(*cp
));
1312 if (newsize
>= 1 && newsize
<= 40) {
1313 tok
->tabsize
= newsize
;
1316 "Tab size set to %d\n",
1321 while (c
!= EOF
&& c
!= '\n')
1325 /* Check for EOF and errors now */
1327 return tok
->done
== E_EOF
? ENDMARKER
: ERRORTOKEN
;
1330 /* Identifier (most frequent token!) */
1331 if (isalpha(c
) || c
== '_') {
1332 /* Process r"", u"" and ur"" */
1337 if (c
== 'r' || c
== 'R')
1339 if (c
== '"' || c
== '\'')
1345 if (c
== '"' || c
== '\'')
1351 if (c
== 'r' || c
== 'R')
1353 if (c
== '"' || c
== '\'')
1357 while (isalnum(c
) || c
== '_') {
1361 *p_start
= tok
->start
;
1369 if (blankline
|| tok
->level
> 0)
1371 *p_start
= tok
->start
;
1372 *p_end
= tok
->cur
- 1; /* Leave '\n' out of the string */
1377 /* Period or number starting with period? */
1385 *p_start
= tok
->start
;
1394 /* Hex, octal or binary -- maybe. */
1398 #ifndef WITHOUT_COMPLEX
1399 if (c
== 'j' || c
== 'J')
1402 if (c
== 'x' || c
== 'X') {
1407 tok
->done
= E_TOKEN
;
1413 } while (isxdigit(c
));
1415 else if (c
== 'o' || c
== 'O') {
1418 if (c
< '0' || c
>= '8') {
1419 tok
->done
= E_TOKEN
;
1425 } while ('0' <= c
&& c
< '8');
1427 else if (c
== 'b' || c
== 'B') {
1430 if (c
!= '0' && c
!= '1') {
1431 tok
->done
= E_TOKEN
;
1437 } while (c
== '0' || c
== '1');
1440 int found_decimal
= 0;
1441 /* Octal; c is first char of it */
1442 /* There's no 'isoctdigit' macro, sigh */
1443 while ('0' <= c
&& c
< '8') {
1450 } while (isdigit(c
));
1454 else if (c
== 'e' || c
== 'E')
1456 #ifndef WITHOUT_COMPLEX
1457 else if (c
== 'j' || c
== 'J')
1460 else if (found_decimal
) {
1461 tok
->done
= E_TOKEN
;
1466 if (c
== 'l' || c
== 'L')
1473 } while (isdigit(c
));
1474 if (c
== 'l' || c
== 'L')
1477 /* Accept floating point numbers. */
1483 } while (isdigit(c
));
1485 if (c
== 'e' || c
== 'E') {
1489 if (c
== '+' || c
== '-')
1492 tok
->done
= E_TOKEN
;
1498 } while (isdigit(c
));
1500 #ifndef WITHOUT_COMPLEX
1501 if (c
== 'j' || c
== 'J')
1502 /* Imaginary part */
1509 *p_start
= tok
->start
;
1516 if (c
== '\'' || c
== '"') {
1517 Py_ssize_t quote2
= tok
->cur
- tok
->start
+ 1;
1530 tok
->cont_line
= 1; /* multiline string. */
1532 else if (c
== EOF
) {
1537 tok
->cur
= tok
->inp
;
1540 else if (c
== quote
) {
1542 if (tok
->cur
- tok
->start
== quote2
) {
1551 if (!triple
|| tripcount
== 3)
1554 else if (c
== '\\') {
1559 tok
->cur
= tok
->inp
;
1566 *p_start
= tok
->start
;
1571 /* Line continuation */
1575 tok
->done
= E_LINECONT
;
1576 tok
->cur
= tok
->inp
;
1580 goto again
; /* Read next line */
1583 /* Check for two-character token */
1585 int c2
= tok_nextc(tok
);
1586 int token
= PyToken_TwoChars(c
, c2
);
1588 if (Py_Py3kWarningFlag
&& token
== NOTEQUAL
&& c
== '<') {
1589 if (PyErr_WarnExplicit(PyExc_DeprecationWarning
,
1590 "<> not supported in 3.x; use !=",
1591 tok
->filename
, tok
->lineno
,
1598 int c3
= tok_nextc(tok
);
1599 int token3
= PyToken_ThreeChars(c
, c2
, c3
);
1603 tok_backup(tok
, c3
);
1605 *p_start
= tok
->start
;
1609 tok_backup(tok
, c2
);
1612 /* Keep track of parentheses nesting level */
1626 /* Punctuation character */
1627 *p_start
= tok
->start
;
1629 return PyToken_OneChar(c
);
1633 PyTokenizer_Get(struct tok_state
*tok
, char **p_start
, char **p_end
)
1635 int result
= tok_get(tok
, p_start
, p_end
);
1636 if (tok
->decoding_erred
) {
1637 result
= ERRORTOKEN
;
1638 tok
->done
= E_DECODE
;
1643 /* This function is only called from parsetok. However, it cannot live
1644 there, as it must be empty for PGEN, and we can check for PGEN only
1647 #if defined(PGEN) || !defined(Py_USING_UNICODE)
1649 PyTokenizer_RestoreEncoding(struct tok_state
* tok
, int len
, int* offset
)
1654 #ifdef Py_USING_UNICODE
1656 dec_utf8(const char *enc
, const char *text
, size_t len
) {
1657 PyObject
*ret
= NULL
;
1658 PyObject
*unicode_text
= PyUnicode_DecodeUTF8(text
, len
, "replace");
1660 ret
= PyUnicode_AsEncodedString(unicode_text
, enc
, "replace");
1661 Py_DECREF(unicode_text
);
1669 PyTokenizer_RestoreEncoding(struct tok_state
* tok
, int len
, int *offset
)
1672 if (tok
->encoding
) {
1673 /* convert source to original encondig */
1674 PyObject
*lineobj
= dec_utf8(tok
->encoding
, tok
->buf
, len
);
1675 if (lineobj
!= NULL
) {
1676 int linelen
= PyString_Size(lineobj
);
1677 const char *line
= PyString_AsString(lineobj
);
1678 text
= PyObject_MALLOC(linelen
+ 1);
1679 if (text
!= NULL
&& line
!= NULL
) {
1681 strncpy(text
, line
, linelen
);
1682 text
[linelen
] = '\0';
1686 /* adjust error offset */
1688 PyObject
*offsetobj
= dec_utf8(tok
->encoding
,
1689 tok
->buf
, *offset
-1);
1691 *offset
= PyString_Size(offsetobj
) + 1;
1692 Py_DECREF(offsetobj
);
1701 #endif /* defined(Py_USING_UNICODE) */
1708 tok_dump(int type
, char *start
, char *end
)
1710 printf("%s", _PyParser_TokenNames
[type
]);
1711 if (type
== NAME
|| type
== NUMBER
|| type
== STRING
|| type
== OP
)
1712 printf("(%.*s)", (int)(end
- start
), start
);