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! */
95 /* Create and initialize a new tok_state structure */
97 static struct tok_state
*
100 struct tok_state
*tok
= (struct tok_state
*)PyMem_MALLOC(
101 sizeof(struct tok_state
));
104 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= tok
->start
= NULL
;
108 tok
->tabsize
= TABSIZE
;
110 tok
->indstack
[0] = 0;
113 tok
->prompt
= tok
->nextprompt
= NULL
;
116 tok
->filename
= NULL
;
120 tok
->altindstack
[0] = 0;
121 tok
->decoding_state
= 0;
122 tok
->decoding_erred
= 0;
123 tok
->read_coding_spec
= 0;
124 tok
->encoding
= NULL
;
127 tok
->decoding_readline
= NULL
;
128 tok
->decoding_buffer
= NULL
;
134 new_string(const char *s
, Py_ssize_t len
)
136 char* result
= (char *)PyMem_MALLOC(len
+ 1);
137 if (result
!= NULL
) {
138 memcpy(result
, s
, len
);
147 decoding_fgets(char *s
, int size
, struct tok_state
*tok
)
149 return fgets(s
, size
, tok
->fp
);
153 decoding_feof(struct tok_state
*tok
)
155 return feof(tok
->fp
);
159 decode_str(const char *str
, int exec_input
, struct tok_state
*tok
)
161 return new_string(str
, strlen(str
));
167 error_ret(struct tok_state
*tok
) /* XXX */
169 tok
->decoding_erred
= 1;
170 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
) /* see PyTokenizer_Free */
171 PyMem_FREE(tok
->buf
);
173 return NULL
; /* as if it were EOF */
178 get_normal_name(char *s
) /* for utf-8 and latin-1 */
182 for (i
= 0; i
< 12; i
++) {
192 if (strcmp(buf
, "utf-8") == 0 ||
193 strncmp(buf
, "utf-8-", 6) == 0)
195 else if (strcmp(buf
, "latin-1") == 0 ||
196 strcmp(buf
, "iso-8859-1") == 0 ||
197 strcmp(buf
, "iso-latin-1") == 0 ||
198 strncmp(buf
, "latin-1-", 8) == 0 ||
199 strncmp(buf
, "iso-8859-1-", 11) == 0 ||
200 strncmp(buf
, "iso-latin-1-", 12) == 0)
206 /* Return the coding spec in S, or NULL if none is found. */
209 get_coding_spec(const char *s
, Py_ssize_t size
)
212 /* Coding spec must be in a comment, and that comment must be
213 * the only statement on the source code line. */
214 for (i
= 0; i
< size
- 6; i
++) {
217 if (s
[i
] != ' ' && s
[i
] != '\t' && s
[i
] != '\014')
220 for (; i
< size
- 6; i
++) { /* XXX inefficient search */
221 const char* t
= s
+ i
;
222 if (strncmp(t
, "coding", 6) == 0) {
223 const char* begin
= NULL
;
225 if (t
[0] != ':' && t
[0] != '=')
229 } while (t
[0] == '\x20' || t
[0] == '\t');
232 while (Py_ISALNUM(t
[0]) ||
233 t
[0] == '-' || t
[0] == '_' || t
[0] == '.')
237 char* r
= new_string(begin
, t
- begin
);
238 char* q
= get_normal_name(r
);
241 r
= new_string(q
, strlen(q
));
250 /* Check whether the line contains a coding spec. If it does,
251 invoke the set_readline function for the new encoding.
252 This function receives the tok_state and the new encoding.
253 Return 1 on success, 0 on failure. */
256 check_coding_spec(const char* line
, Py_ssize_t size
, struct tok_state
*tok
,
257 int set_readline(struct tok_state
*, const char *))
263 /* It's a continuation line, so it can't be a coding spec. */
265 cs
= get_coding_spec(line
, size
);
267 tok
->read_coding_spec
= 1;
268 if (tok
->encoding
== NULL
) {
269 assert(tok
->decoding_state
== 1); /* raw */
270 if (strcmp(cs
, "utf-8") == 0 ||
271 strcmp(cs
, "iso-8859-1") == 0) {
274 #ifdef Py_USING_UNICODE
275 r
= set_readline(tok
, cs
);
278 tok
->decoding_state
= -1;
283 /* Without Unicode support, we cannot
284 process the coding spec. Since there
285 won't be any Unicode literals, that
290 } else { /* then, compare cs with BOM */
291 r
= (strcmp(tok
->encoding
, cs
) == 0);
299 PyErr_Format(PyExc_SyntaxError
, "encoding problem: %s", cs
);
304 /* See whether the file starts with a BOM. If it does,
305 invoke the set_readline function with the new encoding.
306 Return 1 on success, 0 on failure. */
309 check_bom(int get_char(struct tok_state
*),
310 void unget_char(int, struct tok_state
*),
311 int set_readline(struct tok_state
*, const char *),
312 struct tok_state
*tok
)
316 tok
->decoding_state
= 1;
319 } else if (ch1
== 0xEF) {
322 unget_char(ch2
, tok
);
323 unget_char(ch1
, tok
);
328 unget_char(ch3
, tok
);
329 unget_char(ch2
, tok
);
330 unget_char(ch1
, tok
);
334 /* Disable support for UTF-16 BOMs until a decision
335 is made whether this needs to be supported. */
336 } else if (ch1
== 0xFE) {
339 unget_char(ch2
, tok
);
340 unget_char(ch1
, tok
);
343 if (!set_readline(tok
, "utf-16-be"))
345 tok
->decoding_state
= -1;
346 } else if (ch1
== 0xFF) {
349 unget_char(ch2
, tok
);
350 unget_char(ch1
, tok
);
353 if (!set_readline(tok
, "utf-16-le"))
355 tok
->decoding_state
= -1;
358 unget_char(ch1
, tok
);
361 if (tok
->encoding
!= NULL
)
362 PyMem_FREE(tok
->encoding
);
363 tok
->encoding
= new_string("utf-8", 5); /* resulting is in utf-8 */
367 /* Read a line of text from TOK into S, using the stream in TOK.
368 Return NULL on failure, else S.
370 On entry, tok->decoding_buffer will be one of:
371 1) NULL: need to call tok->decoding_readline to get a new line
372 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
373 stored the result in tok->decoding_buffer
374 3) PyStringObject *: previous call to fp_readl did not have enough room
375 (in the s buffer) to copy entire contents of the line read
376 by tok->decoding_readline. tok->decoding_buffer has the overflow.
377 In this case, fp_readl is called in a loop (with an expanded buffer)
378 until the buffer ends with a '\n' (or until the end of the file is
379 reached): see tok_nextc and its calls to decoding_fgets.
383 fp_readl(char *s
, int size
, struct tok_state
*tok
)
385 #ifndef Py_USING_UNICODE
386 /* In a non-Unicode built, this should never be called. */
387 Py_FatalError("fp_readl should not be called in this build.");
388 return NULL
; /* Keep compiler happy (not reachable) */
390 PyObject
* utf8
= NULL
;
391 PyObject
* buf
= tok
->decoding_buffer
;
395 /* Ask for one less byte so we can terminate it */
400 buf
= PyObject_CallObject(tok
->decoding_readline
, NULL
);
402 return error_ret(tok
);
404 tok
->decoding_buffer
= NULL
;
405 if (PyString_CheckExact(buf
))
409 utf8
= PyUnicode_AsUTF8String(buf
);
412 return error_ret(tok
);
414 str
= PyString_AsString(utf8
);
415 utf8len
= PyString_GET_SIZE(utf8
);
416 if (utf8len
> size
) {
417 tok
->decoding_buffer
= PyString_FromStringAndSize(str
+size
, utf8len
-size
);
418 if (tok
->decoding_buffer
== NULL
) {
420 return error_ret(tok
);
424 memcpy(s
, str
, utf8len
);
428 return NULL
; /* EOF */
433 /* Set the readline function for TOK to a StreamReader's
434 readline function. The StreamReader is named ENC.
436 This function is called from check_bom and check_coding_spec.
438 ENC is usually identical to the future value of tok->encoding,
439 except for the (currently unsupported) case of UTF-16.
441 Return 1 on success, 0 on failure. */
444 fp_setreadl(struct tok_state
*tok
, const char* enc
)
446 PyObject
*reader
, *stream
, *readline
;
448 /* XXX: constify filename argument. */
449 stream
= PyFile_FromFile(tok
->fp
, (char*)tok
->filename
, "rb", NULL
);
453 reader
= PyCodec_StreamReader(enc
, stream
, NULL
);
458 readline
= PyObject_GetAttrString(reader
, "readline");
460 if (readline
== NULL
)
463 tok
->decoding_readline
= readline
;
467 /* Fetch the next byte from TOK. */
469 static int fp_getc(struct tok_state
*tok
) {
470 return getc(tok
->fp
);
473 /* Unfetch the last byte back into TOK. */
475 static void fp_ungetc(int c
, struct tok_state
*tok
) {
479 /* Read a line of input from TOK. Determine encoding
483 decoding_fgets(char *s
, int size
, struct tok_state
*tok
)
488 if (tok
->decoding_state
< 0) {
489 /* We already have a codec associated with
491 line
= fp_readl(s
, size
, tok
);
493 } else if (tok
->decoding_state
> 0) {
494 /* We want a 'raw' read. */
495 line
= Py_UniversalNewlineFgets(s
, size
,
499 /* We have not yet determined the encoding.
500 If an encoding is found, use the file-pointer
501 reader functions from now on. */
502 if (!check_bom(fp_getc
, fp_ungetc
, fp_setreadl
, tok
))
503 return error_ret(tok
);
504 assert(tok
->decoding_state
!= 0);
507 if (line
!= NULL
&& tok
->lineno
< 2 && !tok
->read_coding_spec
) {
508 if (!check_coding_spec(line
, strlen(line
), tok
, fp_setreadl
)) {
509 return error_ret(tok
);
513 /* The default encoding is ASCII, so make sure we don't have any
514 non-ASCII bytes in it. */
515 if (line
&& !tok
->encoding
) {
517 for (c
= (unsigned char *)line
; *c
; c
++)
525 /* Need to add 1 to the line number, since this line
526 has not been counted, yet. */
528 "Non-ASCII character '\\x%.2x' "
529 "in file %.200s on line %i, "
530 "but no encoding declared; "
531 "see http://www.python.org/peps/pep-0263.html for details",
532 badchar
, tok
->filename
, tok
->lineno
+ 1);
533 PyErr_SetString(PyExc_SyntaxError
, buf
);
534 return error_ret(tok
);
541 decoding_feof(struct tok_state
*tok
)
543 if (tok
->decoding_state
>= 0) {
544 return feof(tok
->fp
);
546 PyObject
* buf
= tok
->decoding_buffer
;
548 buf
= PyObject_CallObject(tok
->decoding_readline
, NULL
);
553 tok
->decoding_buffer
= buf
;
556 return PyObject_Length(buf
) == 0;
560 /* Fetch a byte from TOK, using the string buffer. */
563 buf_getc(struct tok_state
*tok
) {
564 return Py_CHARMASK(*tok
->str
++);
567 /* Unfetch a byte from TOK, using the string buffer. */
570 buf_ungetc(int c
, struct tok_state
*tok
) {
572 assert(Py_CHARMASK(*tok
->str
) == c
); /* tok->cur may point to read-only segment */
575 /* Set the readline function for TOK to ENC. For the string-based
576 tokenizer, this means to just record the encoding. */
579 buf_setreadl(struct tok_state
*tok
, const char* enc
) {
584 /* Return a UTF-8 encoding Python string object from the
585 C byte string STR, which is encoded with ENC. */
587 #ifdef Py_USING_UNICODE
589 translate_into_utf8(const char* str
, const char* enc
) {
591 PyObject
* buf
= PyUnicode_Decode(str
, strlen(str
), enc
, NULL
);
594 utf8
= PyUnicode_AsUTF8String(buf
);
602 translate_newlines(const char *s
, int exec_input
, struct tok_state
*tok
) {
603 int skip_next_lf
= 0, needed_length
= strlen(s
) + 2, final_length
;
606 buf
= PyMem_MALLOC(needed_length
);
611 for (current
= buf
; *s
; s
++, current
++) {
627 /* If this is exec input, add a newline to the end of the string if
628 there isn't one already. */
629 if (exec_input
&& c
!= '\n') {
634 final_length
= current
- buf
+ 1;
635 if (final_length
< needed_length
&& final_length
)
636 /* should never fail */
637 buf
= PyMem_REALLOC(buf
, final_length
);
641 /* Decode a byte string STR for use as the buffer of TOK.
642 Look for encoding declarations inside STR, and record them
646 decode_str(const char *input
, int single
, struct tok_state
*tok
)
648 PyObject
* utf8
= NULL
;
651 const char *newl
[2] = {NULL
, NULL
};
653 tok
->input
= str
= translate_newlines(input
, single
, tok
);
658 if (!check_bom(buf_getc
, buf_ungetc
, buf_setreadl
, tok
))
659 return error_ret(tok
);
660 str
= tok
->str
; /* string after BOM if any */
662 #ifdef Py_USING_UNICODE
663 if (tok
->enc
!= NULL
) {
664 utf8
= translate_into_utf8(str
, tok
->enc
);
666 return error_ret(tok
);
667 str
= PyString_AsString(utf8
);
670 for (s
= str
;; s
++) {
671 if (*s
== '\0') break;
672 else if (*s
== '\n') {
676 if (lineno
== 2) break;
680 /* need to check line 1 and 2 separately since check_coding_spec
681 assumes a single line as input */
683 if (!check_coding_spec(str
, newl
[0] - str
, tok
, buf_setreadl
))
684 return error_ret(tok
);
685 if (tok
->enc
== NULL
&& newl
[1]) {
686 if (!check_coding_spec(newl
[0]+1, newl
[1] - newl
[0],
688 return error_ret(tok
);
691 #ifdef Py_USING_UNICODE
692 if (tok
->enc
!= NULL
) {
693 assert(utf8
== NULL
);
694 utf8
= translate_into_utf8(str
, tok
->enc
);
696 return error_ret(tok
);
697 str
= PyString_AsString(utf8
);
700 assert(tok
->decoding_buffer
== NULL
);
701 tok
->decoding_buffer
= utf8
; /* CAUTION */
707 /* Set up tokenizer for string */
710 PyTokenizer_FromString(const char *str
, int exec_input
)
712 struct tok_state
*tok
= tok_new();
715 str
= (char *)decode_str(str
, exec_input
, tok
);
717 PyTokenizer_Free(tok
);
721 /* XXX: constify members. */
722 tok
->buf
= tok
->cur
= tok
->end
= tok
->inp
= (char*)str
;
727 /* Set up tokenizer for file */
730 PyTokenizer_FromFile(FILE *fp
, char *ps1
, char *ps2
)
732 struct tok_state
*tok
= tok_new();
735 if ((tok
->buf
= (char *)PyMem_MALLOC(BUFSIZ
)) == NULL
) {
736 PyTokenizer_Free(tok
);
739 tok
->cur
= tok
->inp
= tok
->buf
;
740 tok
->end
= tok
->buf
+ BUFSIZ
;
743 tok
->nextprompt
= ps2
;
748 /* Free a tok_state structure */
751 PyTokenizer_Free(struct tok_state
*tok
)
753 if (tok
->encoding
!= NULL
)
754 PyMem_FREE(tok
->encoding
);
756 Py_XDECREF(tok
->decoding_readline
);
757 Py_XDECREF(tok
->decoding_buffer
);
759 if (tok
->fp
!= NULL
&& tok
->buf
!= NULL
)
760 PyMem_FREE(tok
->buf
);
762 PyMem_FREE((char *)tok
->input
);
766 #if !defined(PGEN) && defined(Py_USING_UNICODE)
768 tok_stdin_decode(struct tok_state
*tok
, char **inp
)
770 PyObject
*enc
, *sysstdin
, *decoded
, *utf8
;
771 const char *encoding
;
774 if (PySys_GetFile((char *)"stdin", NULL
) != stdin
)
776 sysstdin
= PySys_GetObject("stdin");
777 if (sysstdin
== NULL
|| !PyFile_Check(sysstdin
))
780 enc
= ((PyFileObject
*)sysstdin
)->f_encoding
;
781 if (enc
== NULL
|| !PyString_Check(enc
))
785 encoding
= PyString_AsString(enc
);
786 decoded
= PyUnicode_Decode(*inp
, strlen(*inp
), encoding
, NULL
);
790 utf8
= PyUnicode_AsEncodedString(decoded
, "utf-8", NULL
);
795 assert(PyString_Check(utf8
));
796 converted
= new_string(PyString_AS_STRING(utf8
),
797 PyString_GET_SIZE(utf8
));
799 if (converted
== NULL
)
804 if (tok
->encoding
!= NULL
)
805 PyMem_FREE(tok
->encoding
);
806 tok
->encoding
= new_string(encoding
, strlen(encoding
));
807 if (tok
->encoding
== NULL
)
820 if (!PyErr_ExceptionMatches(PyExc_UnicodeDecodeError
)) {
824 /* Fallback to iso-8859-1: for backward compatibility */
830 /* Get next char, updating state; error code goes into tok->done */
833 tok_nextc(register struct tok_state
*tok
)
836 if (tok
->cur
!= tok
->inp
) {
837 return Py_CHARMASK(*tok
->cur
++); /* Fast path */
839 if (tok
->done
!= E_OK
)
841 if (tok
->fp
== NULL
) {
842 char *end
= strchr(tok
->inp
, '\n');
846 end
= strchr(tok
->inp
, '\0');
847 if (end
== tok
->inp
) {
852 if (tok
->start
== NULL
)
854 tok
->line_start
= tok
->cur
;
857 return Py_CHARMASK(*tok
->cur
++);
859 if (tok
->prompt
!= NULL
) {
860 char *newtok
= PyOS_Readline(stdin
, stdout
, tok
->prompt
);
861 if (tok
->nextprompt
!= NULL
)
862 tok
->prompt
= tok
->nextprompt
;
865 else if (*newtok
== '\0') {
869 #if !defined(PGEN) && defined(Py_USING_UNICODE)
870 else if (tok_stdin_decode(tok
, &newtok
) != 0)
873 else if (tok
->start
!= NULL
) {
874 size_t start
= tok
->start
- tok
->buf
;
875 size_t oldlen
= tok
->cur
- tok
->buf
;
876 size_t newlen
= oldlen
+ strlen(newtok
);
877 char *buf
= tok
->buf
;
878 buf
= (char *)PyMem_REALLOC(buf
, newlen
+1);
881 PyMem_FREE(tok
->buf
);
888 tok
->cur
= tok
->buf
+ oldlen
;
889 tok
->line_start
= tok
->cur
;
890 strcpy(tok
->buf
+ oldlen
, newtok
);
892 tok
->inp
= tok
->buf
+ newlen
;
893 tok
->end
= tok
->inp
+ 1;
894 tok
->start
= tok
->buf
+ start
;
898 if (tok
->buf
!= NULL
)
899 PyMem_FREE(tok
->buf
);
901 tok
->line_start
= tok
->buf
;
903 tok
->line_start
= tok
->buf
;
904 tok
->inp
= strchr(tok
->buf
, '\0');
905 tok
->end
= tok
->inp
+ 1;
912 if (tok
->start
== NULL
) {
913 if (tok
->buf
== NULL
) {
915 PyMem_MALLOC(BUFSIZ
);
916 if (tok
->buf
== NULL
) {
920 tok
->end
= tok
->buf
+ BUFSIZ
;
922 if (decoding_fgets(tok
->buf
, (int)(tok
->end
- tok
->buf
),
929 tok
->inp
= strchr(tok
->buf
, '\0');
930 done
= tok
->inp
[-1] == '\n';
934 cur
= tok
->cur
- tok
->buf
;
935 if (decoding_feof(tok
)) {
943 /* Read until '\n' or EOF */
945 Py_ssize_t curstart
= tok
->start
== NULL
? -1 :
946 tok
->start
- tok
->buf
;
947 Py_ssize_t curvalid
= tok
->inp
- tok
->buf
;
948 Py_ssize_t newsize
= curvalid
+ BUFSIZ
;
949 char *newbuf
= tok
->buf
;
950 newbuf
= (char *)PyMem_REALLOC(newbuf
,
952 if (newbuf
== NULL
) {
958 tok
->inp
= tok
->buf
+ curvalid
;
959 tok
->end
= tok
->buf
+ newsize
;
960 tok
->start
= curstart
< 0 ? NULL
:
962 if (decoding_fgets(tok
->inp
,
963 (int)(tok
->end
- tok
->inp
),
965 /* Break out early on decoding
966 errors, as tok->buf will be NULL
968 if (tok
->decoding_erred
)
970 /* Last line does not end in \n,
972 strcpy(tok
->inp
, "\n");
974 tok
->inp
= strchr(tok
->inp
, '\0');
975 done
= tok
->inp
[-1] == '\n';
977 if (tok
->buf
!= NULL
) {
978 tok
->cur
= tok
->buf
+ cur
;
979 tok
->line_start
= tok
->cur
;
980 /* replace "\r\n" with "\n" */
981 /* For Mac leave the \r, giving a syntax error */
983 if (pt
>= tok
->buf
&& *pt
== '\r') {
990 if (tok
->done
!= E_OK
) {
991 if (tok
->prompt
!= NULL
)
992 PySys_WriteStderr("\n");
1001 /* Back-up one character */
1004 tok_backup(register struct tok_state
*tok
, register int c
)
1007 if (--tok
->cur
< tok
->buf
)
1008 Py_FatalError("tok_backup: beginning of buffer");
1015 /* Return the token corresponding to a single character */
1018 PyToken_OneChar(int c
)
1021 case '(': return LPAR
;
1022 case ')': return RPAR
;
1023 case '[': return LSQB
;
1024 case ']': return RSQB
;
1025 case ':': return COLON
;
1026 case ',': return COMMA
;
1027 case ';': return SEMI
;
1028 case '+': return PLUS
;
1029 case '-': return MINUS
;
1030 case '*': return STAR
;
1031 case '/': return SLASH
;
1032 case '|': return VBAR
;
1033 case '&': return AMPER
;
1034 case '<': return LESS
;
1035 case '>': return GREATER
;
1036 case '=': return EQUAL
;
1037 case '.': return DOT
;
1038 case '%': return PERCENT
;
1039 case '`': return BACKQUOTE
;
1040 case '{': return LBRACE
;
1041 case '}': return RBRACE
;
1042 case '^': return CIRCUMFLEX
;
1043 case '~': return TILDE
;
1044 case '@': return AT
;
1051 PyToken_TwoChars(int c1
, int c2
)
1056 case '=': return EQEQUAL
;
1061 case '=': return NOTEQUAL
;
1066 case '>': return NOTEQUAL
;
1067 case '=': return LESSEQUAL
;
1068 case '<': return LEFTSHIFT
;
1073 case '=': return GREATEREQUAL
;
1074 case '>': return RIGHTSHIFT
;
1079 case '=': return PLUSEQUAL
;
1084 case '=': return MINEQUAL
;
1089 case '*': return DOUBLESTAR
;
1090 case '=': return STAREQUAL
;
1095 case '/': return DOUBLESLASH
;
1096 case '=': return SLASHEQUAL
;
1101 case '=': return VBAREQUAL
;
1106 case '=': return PERCENTEQUAL
;
1111 case '=': return AMPEREQUAL
;
1116 case '=': return CIRCUMFLEXEQUAL
;
1124 PyToken_ThreeChars(int c1
, int c2
, int c3
)
1132 return LEFTSHIFTEQUAL
;
1142 return RIGHTSHIFTEQUAL
;
1152 return DOUBLESTAREQUAL
;
1162 return DOUBLESLASHEQUAL
;
1172 indenterror(struct tok_state
*tok
)
1174 if (tok
->alterror
) {
1175 tok
->done
= E_TABSPACE
;
1176 tok
->cur
= tok
->inp
;
1179 if (tok
->altwarning
) {
1180 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1181 "in indentation\n", tok
->filename
);
1182 tok
->altwarning
= 0;
1187 /* Get next token, after space stripping etc. */
1190 tok_get(register struct tok_state
*tok
, char **p_start
, char **p_end
)
1195 *p_start
= *p_end
= NULL
;
1200 /* Get indentation level */
1202 register int col
= 0;
1203 register int altcol
= 0;
1209 else if (c
== '\t') {
1210 col
= (col
/tok
->tabsize
+ 1) * tok
->tabsize
;
1211 altcol
= (altcol
/tok
->alttabsize
+ 1)
1214 else if (c
== '\014') /* Control-L (formfeed) */
1215 col
= altcol
= 0; /* For Emacs users */
1220 if (c
== '#' || c
== '\n') {
1221 /* Lines with only whitespace and/or comments
1222 shouldn't affect the indentation and are
1223 not passed to the parser as NEWLINE tokens,
1224 except *totally* empty lines in interactive
1225 mode, which signal the end of a command group. */
1226 if (col
== 0 && c
== '\n' && tok
->prompt
!= NULL
)
1227 blankline
= 0; /* Let it through */
1229 blankline
= 1; /* Ignore completely */
1230 /* We can't jump back right here since we still
1231 may need to skip to the end of a comment */
1233 if (!blankline
&& tok
->level
== 0) {
1234 if (col
== tok
->indstack
[tok
->indent
]) {
1236 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1237 if (indenterror(tok
))
1241 else if (col
> tok
->indstack
[tok
->indent
]) {
1242 /* Indent -- always one */
1243 if (tok
->indent
+1 >= MAXINDENT
) {
1244 tok
->done
= E_TOODEEP
;
1245 tok
->cur
= tok
->inp
;
1248 if (altcol
<= tok
->altindstack
[tok
->indent
]) {
1249 if (indenterror(tok
))
1253 tok
->indstack
[++tok
->indent
] = col
;
1254 tok
->altindstack
[tok
->indent
] = altcol
;
1256 else /* col < tok->indstack[tok->indent] */ {
1257 /* Dedent -- any number, must be consistent */
1258 while (tok
->indent
> 0 &&
1259 col
< tok
->indstack
[tok
->indent
]) {
1263 if (col
!= tok
->indstack
[tok
->indent
]) {
1264 tok
->done
= E_DEDENT
;
1265 tok
->cur
= tok
->inp
;
1268 if (altcol
!= tok
->altindstack
[tok
->indent
]) {
1269 if (indenterror(tok
))
1276 tok
->start
= tok
->cur
;
1278 /* Return pending indents/dedents */
1279 if (tok
->pendin
!= 0) {
1280 if (tok
->pendin
< 0) {
1295 } while (c
== ' ' || c
== '\t' || c
== '\014');
1297 /* Set start of current token */
1298 tok
->start
= tok
->cur
- 1;
1300 /* Skip comment, while looking for tab-setting magic */
1302 static char *tabforms
[] = {
1303 "tab-width:", /* Emacs */
1304 ":tabstop=", /* vim, full form */
1305 ":ts=", /* vim, abbreviated form */
1306 "set tabsize=", /* will vi never die? */
1307 /* more templates can be added here to support other editors */
1313 *tp
++ = c
= tok_nextc(tok
);
1314 } while (c
!= EOF
&& c
!= '\n' &&
1315 (size_t)(tp
- cbuf
+ 1) < sizeof(cbuf
));
1318 cp
< tabforms
+ sizeof(tabforms
)/sizeof(tabforms
[0]);
1320 if ((tp
= strstr(cbuf
, *cp
))) {
1321 int newsize
= atoi(tp
+ strlen(*cp
));
1323 if (newsize
>= 1 && newsize
<= 40) {
1324 tok
->tabsize
= newsize
;
1327 "Tab size set to %d\n",
1332 while (c
!= EOF
&& c
!= '\n')
1336 /* Check for EOF and errors now */
1338 return tok
->done
== E_EOF
? ENDMARKER
: ERRORTOKEN
;
1341 /* Identifier (most frequent token!) */
1342 if (Py_ISALPHA(c
) || c
== '_') {
1343 /* Process r"", u"" and ur"" */
1348 if (c
== 'r' || c
== 'R')
1350 if (c
== '"' || c
== '\'')
1356 if (c
== '"' || c
== '\'')
1362 if (c
== 'r' || c
== 'R')
1364 if (c
== '"' || c
== '\'')
1368 while (c
!= EOF
&& (Py_ISALNUM(c
) || c
== '_')) {
1372 *p_start
= tok
->start
;
1380 if (blankline
|| tok
->level
> 0)
1382 *p_start
= tok
->start
;
1383 *p_end
= tok
->cur
- 1; /* Leave '\n' out of the string */
1388 /* Period or number starting with period? */
1396 *p_start
= tok
->start
;
1405 /* Hex, octal or binary -- maybe. */
1409 #ifndef WITHOUT_COMPLEX
1410 if (c
== 'j' || c
== 'J')
1413 if (c
== 'x' || c
== 'X') {
1418 tok
->done
= E_TOKEN
;
1424 } while (isxdigit(c
));
1426 else if (c
== 'o' || c
== 'O') {
1429 if (c
< '0' || c
>= '8') {
1430 tok
->done
= E_TOKEN
;
1436 } while ('0' <= c
&& c
< '8');
1438 else if (c
== 'b' || c
== 'B') {
1441 if (c
!= '0' && c
!= '1') {
1442 tok
->done
= E_TOKEN
;
1448 } while (c
== '0' || c
== '1');
1451 int found_decimal
= 0;
1452 /* Octal; c is first char of it */
1453 /* There's no 'isoctdigit' macro, sigh */
1454 while ('0' <= c
&& c
< '8') {
1461 } while (isdigit(c
));
1465 else if (c
== 'e' || c
== 'E')
1467 #ifndef WITHOUT_COMPLEX
1468 else if (c
== 'j' || c
== 'J')
1471 else if (found_decimal
) {
1472 tok
->done
= E_TOKEN
;
1477 if (c
== 'l' || c
== 'L')
1484 } while (isdigit(c
));
1485 if (c
== 'l' || c
== 'L')
1488 /* Accept floating point numbers. */
1494 } while (isdigit(c
));
1496 if (c
== 'e' || c
== 'E') {
1500 if (c
== '+' || c
== '-')
1503 tok
->done
= E_TOKEN
;
1509 } while (isdigit(c
));
1511 #ifndef WITHOUT_COMPLEX
1512 if (c
== 'j' || c
== 'J')
1513 /* Imaginary part */
1520 *p_start
= tok
->start
;
1527 if (c
== '\'' || c
== '"') {
1528 Py_ssize_t quote2
= tok
->cur
- tok
->start
+ 1;
1541 tok
->cont_line
= 1; /* multiline string. */
1543 else if (c
== EOF
) {
1548 tok
->cur
= tok
->inp
;
1551 else if (c
== quote
) {
1553 if (tok
->cur
- tok
->start
== quote2
) {
1562 if (!triple
|| tripcount
== 3)
1565 else if (c
== '\\') {
1570 tok
->cur
= tok
->inp
;
1577 *p_start
= tok
->start
;
1582 /* Line continuation */
1586 tok
->done
= E_LINECONT
;
1587 tok
->cur
= tok
->inp
;
1591 goto again
; /* Read next line */
1594 /* Check for two-character token */
1596 int c2
= tok_nextc(tok
);
1597 int token
= PyToken_TwoChars(c
, c2
);
1599 if (Py_Py3kWarningFlag
&& token
== NOTEQUAL
&& c
== '<') {
1600 if (PyErr_WarnExplicit(PyExc_DeprecationWarning
,
1601 "<> not supported in 3.x; use !=",
1602 tok
->filename
, tok
->lineno
,
1609 int c3
= tok_nextc(tok
);
1610 int token3
= PyToken_ThreeChars(c
, c2
, c3
);
1614 tok_backup(tok
, c3
);
1616 *p_start
= tok
->start
;
1620 tok_backup(tok
, c2
);
1623 /* Keep track of parentheses nesting level */
1637 /* Punctuation character */
1638 *p_start
= tok
->start
;
1640 return PyToken_OneChar(c
);
1644 PyTokenizer_Get(struct tok_state
*tok
, char **p_start
, char **p_end
)
1646 int result
= tok_get(tok
, p_start
, p_end
);
1647 if (tok
->decoding_erred
) {
1648 result
= ERRORTOKEN
;
1649 tok
->done
= E_DECODE
;
1654 /* This function is only called from parsetok. However, it cannot live
1655 there, as it must be empty for PGEN, and we can check for PGEN only
1658 #if defined(PGEN) || !defined(Py_USING_UNICODE)
1660 PyTokenizer_RestoreEncoding(struct tok_state
* tok
, int len
, int* offset
)
1665 #ifdef Py_USING_UNICODE
1667 dec_utf8(const char *enc
, const char *text
, size_t len
) {
1668 PyObject
*ret
= NULL
;
1669 PyObject
*unicode_text
= PyUnicode_DecodeUTF8(text
, len
, "replace");
1671 ret
= PyUnicode_AsEncodedString(unicode_text
, enc
, "replace");
1672 Py_DECREF(unicode_text
);
1680 PyTokenizer_RestoreEncoding(struct tok_state
* tok
, int len
, int *offset
)
1683 if (tok
->encoding
) {
1684 /* convert source to original encondig */
1685 PyObject
*lineobj
= dec_utf8(tok
->encoding
, tok
->buf
, len
);
1686 if (lineobj
!= NULL
) {
1687 int linelen
= PyString_Size(lineobj
);
1688 const char *line
= PyString_AsString(lineobj
);
1689 text
= PyObject_MALLOC(linelen
+ 1);
1690 if (text
!= NULL
&& line
!= NULL
) {
1692 strncpy(text
, line
, linelen
);
1693 text
[linelen
] = '\0';
1697 /* adjust error offset */
1699 PyObject
*offsetobj
= dec_utf8(tok
->encoding
,
1700 tok
->buf
, *offset
-1);
1702 *offset
= PyString_Size(offsetobj
) + 1;
1703 Py_DECREF(offsetobj
);
1712 #endif /* defined(Py_USING_UNICODE) */
1719 tok_dump(int type
, char *start
, char *end
)
1721 printf("%s", _PyParser_TokenNames
[type
]);
1722 if (type
== NAME
|| type
== NUMBER
|| type
== STRING
|| type
== OP
)
1723 printf("(%.*s)", (int)(end
- start
), start
);