blob: aaa31f37962076b52a71ba28dcecebaecea2eaa0 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_TOKENIZER_H
2#define Py_TOKENIZER_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00007#include "object.h"
Guido van Rossumf70e43a1991-02-19 12:39:46 +00008
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009/* Tokenizer interface */
10
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011#include "token.h" /* For token types */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013#define MAXINDENT 100 /* Max indentation level */
Serhiy Storchaka94cf3082018-12-17 17:34:14 +020014#define MAXLEVEL 200 /* Max parentheses level */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
Neil Schemenauer3f993c32007-09-21 20:50:26 +000016enum decoding_state {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 STATE_INIT,
Pablo Galindo261a4522021-03-28 23:48:05 +010018 STATE_SEEK_CODING,
19 STATE_NORMAL
Neil Schemenauer3f993c32007-09-21 20:50:26 +000020};
21
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022/* Tokenizer state */
23struct tok_state {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 /* Input state; buf <= cur <= inp <= end */
25 /* NB an entire line is held in the buffer */
26 char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL */
27 char *cur; /* Next character in buffer */
28 char *inp; /* End of data in buffer */
Pablo Galindocd8dcbc2021-03-14 04:38:40 +010029 int fp_interactive; /* If the file descriptor is interactive */
30 char *interactive_src_start; /* The start of the source parsed so far in interactive mode */
31 char *interactive_src_end; /* The end of the source parsed so far in interactive mode */
Andy Lester384f3c52020-02-27 20:44:52 -060032 const char *end; /* End of input buffer if buf != NULL */
33 const char *start; /* Start of current token if not NULL */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
35 /* NB If done != E_OK, cur must be == inp!!! */
36 FILE *fp; /* Rest of input; NULL if tokenizing a string */
37 int tabsize; /* Tab spacing */
38 int indent; /* Current indentation index */
39 int indstack[MAXINDENT]; /* Stack of indents */
40 int atbol; /* Nonzero if at begin of new line */
41 int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
Serhiy Storchakac6792272013-10-19 21:03:34 +030042 const char *prompt, *nextprompt; /* For interactive prompting */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 int lineno; /* Current line number */
Anthony Sottile995d9b92019-01-12 20:05:13 -080044 int first_lineno; /* First line of a single line or multi line string
45 expression (cf. issue 16806) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 int level; /* () [] {} Parentheses nesting level */
47 /* Used to allow free continuations inside them */
Serhiy Storchaka94cf3082018-12-17 17:34:14 +020048 char parenstack[MAXLEVEL];
49 int parenlinenostack[MAXLEVEL];
Pablo Galindod6d63712021-01-19 23:59:33 +000050 int parencolstack[MAXLEVEL];
Victor Stinner7f2fee32011-04-05 00:39:01 +020051 PyObject *filename;
Serhiy Storchaka94cf3082018-12-17 17:34:14 +020052 /* Stuff for checking on different tab sizes */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 int altindstack[MAXINDENT]; /* Stack of alternate indents */
54 /* Stuff for PEP 0263 */
55 enum decoding_state decoding_state;
56 int decoding_erred; /* whether erred in decoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 char *encoding; /* Source encoding. */
58 int cont_line; /* whether we are in a continuation line. */
59 const char* line_start; /* pointer to start of current line */
Anthony Sottile995d9b92019-01-12 20:05:13 -080060 const char* multi_line_start; /* pointer to start of first line of
61 a single line or multi line string
62 expression (cf. issue 16806) */
Victor Stinner22a351a2010-10-14 12:04:34 +000063 PyObject *decoding_readline; /* open(...).readline */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 PyObject *decoding_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 const char* enc; /* Encoding for the current str. */
Andy Lester384f3c52020-02-27 20:44:52 -060066 char* str;
67 char* input; /* Tokenizer's newline translated copy of the string. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -080068
69 int type_comments; /* Whether to look for type comments */
Guido van Rossum495da292019-03-07 12:38:08 -080070
71 /* async/await related fields (still needed depending on feature_version) */
72 int async_hacks; /* =1 if async/await aren't always keywords */
73 int async_def; /* =1 if tokens are inside an 'async def' body. */
74 int async_def_indent; /* Indentation level of the outermost 'async def'. */
75 int async_def_nl; /* =1 if the outermost 'async def' had at least one
76 NEWLINE token after it. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077};
78
Benjamin Petersonaeaa5922009-11-13 00:17:59 +000079extern struct tok_state *PyTokenizer_FromString(const char *, int);
80extern struct tok_state *PyTokenizer_FromUTF8(const char *, int);
Serhiy Storchakac6792272013-10-19 21:03:34 +030081extern struct tok_state *PyTokenizer_FromFile(FILE *, const char*,
82 const char *, const char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000083extern void PyTokenizer_Free(struct tok_state *);
Andy Lester384f3c52020-02-27 20:44:52 -060084extern int PyTokenizer_Get(struct tok_state *, const char **, const char **);
Guido van Rossuma3309961993-07-28 09:05:47 +000085
Pablo Galindof2cf1e32019-04-13 17:05:14 +010086#define tok_dump _Py_tok_dump
87
Guido van Rossuma3309961993-07-28 09:05:47 +000088#ifdef __cplusplus
89}
90#endif
91#endif /* !Py_TOKENIZER_H */