forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.h
More file actions
172 lines (147 loc) · 4.98 KB
/
Copy pathstate.h
File metadata and controls
172 lines (147 loc) · 4.98 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
#ifndef _PY_LEXER_H_
#define _PY_LEXER_H_
#include "object.h"
#include "../tokenizer/source.h"
#define MAXINDENT 100 /* Max indentation level */
#define MAXLEVEL 200 /* Max parentheses level */
#define MAXFSTRINGLEVEL 150 /* Max f-string nesting level */
#define INSIDE_FSTRING(tok) (tok->tok_mode_stack_index > 0)
#define INSIDE_FSTRING_EXPR(tok) (tok->curly_bracket_expr_start_depth >= 0)
#define INSIDE_FSTRING_EXPR_AT_TOP(tok) \
(tok->curly_bracket_depth - tok->curly_bracket_expr_start_depth == 1)
struct token {
int level;
_PyTok_Span span;
_PyTok_Loc start_loc;
_PyTok_Loc end_loc;
PyObject *metadata;
};
enum tokenizer_mode_kind_t {
TOK_REGULAR_MODE,
TOK_FSTRING_MODE,
};
enum string_kind_t {
FSTRING,
TSTRING,
};
#define MAX_EXPR_NESTING 3
typedef struct _tokenizer_comments {
Py_ssize_t count;
Py_ssize_t capacity;
_PyTok_Span spans[];
} tokenizer_comments;
typedef struct _tokenizer_mode {
enum tokenizer_mode_kind_t kind;
int curly_bracket_depth;
int curly_bracket_expr_start_depth;
char quote;
int quote_size;
int raw;
_PyTok_Off start;
_PyTok_Off multi_line_start;
int first_line;
_PyTok_Span expr_span;
int in_debug;
int in_format_spec;
enum string_kind_t string_kind;
tokenizer_comments *comments;
} tokenizer_mode;
/* Tokenizer state */
struct tok_state {
/* Input state; buf <= cur <= inp */
/* NB an entire line is held in the buffer */
char *buf;
char *cur; /* Next character in buffer */
char *inp; /* End of data in buffer */
_PyTok_Off buf_offset; /* Logical offset of buf[0]. */
const char *start; /* Start of current token if not NULL */
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) */
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. */
const char* line_start; /* pointer to start of current line */
_PyTok_SourceText source;
struct _PyTok_Reader *reader;
int type_comments; /* Whether to look for type comments */
tokenizer_mode tok_mode_stack[MAXFSTRINGLEVEL];
int tok_mode_stack_index;
int tok_extra_tokens;
int comment_newline;
int implicit_newline;
#ifdef Py_DEBUG
int debug;
#endif
};
static inline _PyTok_Off
_PyLexer_BufferOffset(const struct tok_state *tok, const char *position)
{
assert(tok->buf != NULL);
assert(tok->inp >= tok->buf);
assert(position >= tok->buf && position <= tok->inp);
Py_ssize_t offset = position - tok->buf;
assert(tok->buf_offset <= PY_SSIZE_T_MAX - offset);
return tok->buf_offset + offset;
}
static inline char *
_PyLexer_BufferPointer(const struct tok_state *tok, _PyTok_Off offset)
{
assert(tok->buf != NULL);
assert(tok->inp >= tok->buf);
assert(offset >= tok->buf_offset);
assert(offset - tok->buf_offset <= tok->inp - tok->buf);
return tok->buf + (offset - tok->buf_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 != NULL);
assert(tok->cur >= tok->line_start);
Py_ssize_t column = tok->cur - tok->line_start;
assert(column <= INT_MAX);
return (int)column;
}
static inline _PyTok_Span
_PyLexer_BufferSpan(const struct tok_state *tok, const char *start,
const char *end)
{
if (start == NULL) {
assert(end == NULL);
return (_PyTok_Span){-1, -1};
}
assert(end != NULL);
assert(start <= end);
return _PyTok_SpanFromBounds(
_PyLexer_BufferOffset(tok, start),
_PyLexer_BufferOffset(tok, end));
}
int _PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end);
void _PyTokenizer_Free(struct tok_state *);
void _PyToken_Free(struct token *);
void _PyToken_Init(struct token *);
#endif