-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Expand file tree
/
Copy pathstate.h
More file actions
191 lines (165 loc) · 5.75 KB
/
Copy pathstate.h
File metadata and controls
191 lines (165 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef _PY_LEXER_H_
#define _PY_LEXER_H_
#include "object.h"
#include "../tokenizer/source.h"
#include "../tokenizer/tokenizer.h"
#define MAXINDENT 100 /* Max indentation level */
#define MAXLEVEL 200 /* Max parentheses level */
#define MAXFTSTRINGLEVEL 150
#define FTSTRING_STACK_INLINE_CAPACITY 1
enum interactive_underflow_t {
/* Normal mode of operation: return a new token when asked in interactive mode */
IUNDERFLOW_NORMAL,
/* Forcefully return ENDMARKER when asked for a new token in interactive mode. This
* can be used to prevent the tokenizer to prompt the user for new tokens */
IUNDERFLOW_STOP,
};
typedef enum {
FTSTRING_MODE_MIDDLE,
FTSTRING_MODE_EXPRESSION,
FTSTRING_MODE_FORMAT_SPEC,
} ftstring_mode;
typedef enum {
FSTRING,
RAW_FSTRING,
TSTRING,
RAW_TSTRING,
} ftstring_kind;
#define MAX_EXPR_NESTING 3
typedef struct _tokenizer_comments {
Py_ssize_t count;
Py_ssize_t capacity;
_PyTok_Span spans[];
} tokenizer_comments;
typedef struct _ftstring_state {
ftstring_mode mode;
ftstring_kind kind;
char quote;
unsigned char quote_size;
unsigned char debug_expr;
unsigned char replacement_depth;
int paren_level;
_PyTok_Off start;
_PyTok_Loc start_loc;
_PyTok_Span expr_span;
tokenizer_comments *comments;
} ftstring_state;
static inline int
_PyLexer_IsTString(ftstring_kind kind)
{
return kind == TSTRING || kind == RAW_TSTRING;
}
static inline int
_PyLexer_IsRawString(ftstring_kind kind)
{
return kind == RAW_FSTRING || kind == RAW_TSTRING;
}
/* Tokenizer state */
struct tok_state {
_PyTok_Off buf_offset;
_PyTok_Off cur;
_PyTok_Off inp;
_PyTok_Off start;
_PyTok_Off line_start;
_PyTok_SourceText source;
int fp_interactive; /* If the file descriptor is interactive */
char *interactive_src_start; /* The start of the source parsed so far in interactive mode */
char *interactive_src_end; /* The end of the source parsed so far in interactive mode */
int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
/* NB If done != E_OK, cur must be == inp!!! */
FILE *fp; /* Rest of input; NULL if tokenizing a string */
int indent; /* Current indentation index */
int indstack[MAXINDENT]; /* Stack of indents */
int atbol; /* Nonzero if at begin of new line */
int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
const char *prompt; /* For interactive prompting */
int lineno; /* Current line number */
_PyTok_Loc start_loc;
int level; /* () [] {} Parentheses nesting level */
/* Used to allow free continuations inside them */
char parenstack[MAXLEVEL];
int parenlinenostack[MAXLEVEL];
int parencolstack[MAXLEVEL];
PyObject *filename;
PyObject *module;
/* Stuff for checking on different tab sizes */
int altindstack[MAXINDENT]; /* Stack of alternate indents */
/* Stuff for PEP 0263 */
char *encoding; /* Source encoding. */
char* str; /* Source string being tokenized (if tokenizing from a string)*/
struct _PyTok_Reader *reader;
int type_comments; /* Whether to look for type comments */
/* How to proceed when asked for a new token in interactive mode */
enum interactive_underflow_t interactive_underflow;
int report_warnings;
ftstring_state *ftstring_stack;
ftstring_state ftstring_stack_inline[FTSTRING_STACK_INLINE_CAPACITY];
int ftstring_depth;
int ftstring_capacity;
int tok_extra_tokens;
int comment_newline;
int implicit_newline;
#ifdef Py_DEBUG
int debug;
#endif
};
static inline ftstring_state *
_PyLexer_CurrentFTString(struct tok_state *tok)
{
assert(tok->ftstring_stack != NULL);
assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity);
if (tok->ftstring_depth == 0) {
return NULL;
}
return &tok->ftstring_stack[tok->ftstring_depth - 1];
}
static inline char
_PyLexer_StringPrefix(ftstring_kind kind)
{
return _PyLexer_IsTString(kind) ? 't' : 'f';
}
static inline int
_PyLexer_FTStringBracketDepth(const struct tok_state *tok,
const ftstring_state *state)
{
return tok->level - state->paren_level;
}
static inline _PyTok_Off
_PyLexer_BufferOffset(const struct tok_state *tok, const char *position)
{
const char *base = _PyTok_SourceData(&tok->source);
assert(position >= base && position <= base + tok->source.len);
return tok->source.base_offset + (position - base);
}
static inline const char *
_PyLexer_BufferPointer(const struct tok_state *tok, _PyTok_Off offset)
{
assert(offset >= tok->source.base_offset);
assert(offset - tok->source.base_offset <= tok->source.len);
return _PyTok_SourceData(&tok->source) + (offset - tok->source.base_offset);
}
static inline const char *
_PyLexer_BufferSpanView(const struct tok_state *tok, _PyTok_Span span,
Py_ssize_t *length)
{
assert(length != NULL);
assert(_PyTok_SpanIsValid(span));
*length = span.end - span.start;
(void)_PyLexer_BufferPointer(tok, span.end);
return _PyLexer_BufferPointer(tok, span.start);
}
static inline int
_PyLexer_ByteColumn(const struct tok_state *tok)
{
assert(tok->line_start >= 0);
assert(tok->cur >= tok->line_start);
Py_ssize_t column = tok->cur - tok->line_start;
assert(column <= INT_MAX);
return (int)column;
}
int _PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, _PyTok_Off start, _PyTok_Off end);
struct tok_state *_PyTokenizer_tok_new(void);
void _PyTokenizer_Free(struct tok_state *);
ftstring_state *_PyLexer_PushFTString(struct tok_state *);
void _PyLexer_PopFTString(struct tok_state *);
#endif