/src/cpython/Parser/lexer/state.h
Line | Count | Source |
1 | | #ifndef _PY_LEXER_H_ |
2 | | #define _PY_LEXER_H_ |
3 | | |
4 | | #include "object.h" |
5 | | #include "../tokenizer/source.h" |
6 | | #include "../tokenizer/tokenizer.h" |
7 | | |
8 | 10.0k | #define MAXINDENT 100 /* Max indentation level */ |
9 | 153k | #define MAXLEVEL 200 /* Max parentheses level */ |
10 | 20.2k | #define MAXFTSTRINGLEVEL 150 |
11 | 65.0k | #define FTSTRING_STACK_INLINE_CAPACITY 1 |
12 | | |
13 | | typedef enum { |
14 | | FTSTRING_MODE_MIDDLE, |
15 | | FTSTRING_MODE_EXPRESSION, |
16 | | FTSTRING_MODE_FORMAT_SPEC, |
17 | | } ftstring_mode; |
18 | | |
19 | | typedef enum { |
20 | | FSTRING, |
21 | | RAW_FSTRING, |
22 | | TSTRING, |
23 | | RAW_TSTRING, |
24 | | } ftstring_kind; |
25 | | |
26 | 30.8k | #define MAX_EXPR_NESTING 3 |
27 | | |
28 | | typedef struct _tokenizer_comments { |
29 | | Py_ssize_t count; |
30 | | Py_ssize_t capacity; |
31 | | _PyTok_Span spans[]; |
32 | | } tokenizer_comments; |
33 | | |
34 | | typedef struct _ftstring_state { |
35 | | ftstring_mode mode; |
36 | | ftstring_kind kind; |
37 | | char quote; |
38 | | unsigned char quote_size; |
39 | | unsigned char debug_expr; |
40 | | unsigned char replacement_depth; |
41 | | int paren_level; |
42 | | _PyTok_Off start; |
43 | | _PyTok_Loc start_loc; |
44 | | _PyTok_Span expr_span; |
45 | | tokenizer_comments *comments; |
46 | | } ftstring_state; |
47 | | |
48 | | static inline int |
49 | | _PyLexer_IsTString(ftstring_kind kind) |
50 | 86.7k | { |
51 | 86.7k | return kind == TSTRING || kind == RAW_TSTRING; |
52 | 86.7k | } Unexecuted instantiation: reader.c:_PyLexer_IsTString Unexecuted instantiation: helpers.c:_PyLexer_IsTString Unexecuted instantiation: pegen.c:_PyLexer_IsTString lexer.c:_PyLexer_IsTString Line | Count | Source | 50 | 6 | { | 51 | 6 | return kind == TSTRING || kind == RAW_TSTRING; | 52 | 6 | } |
Unexecuted instantiation: layout.c:_PyLexer_IsTString Unexecuted instantiation: number.c:_PyLexer_IsTString Unexecuted instantiation: state.c:_PyLexer_IsTString string.c:_PyLexer_IsTString Line | Count | Source | 50 | 86.7k | { | 51 | 86.7k | return kind == TSTRING || kind == RAW_TSTRING; | 52 | 86.7k | } |
Unexecuted instantiation: decoder.c:_PyLexer_IsTString Unexecuted instantiation: api.c:_PyLexer_IsTString Unexecuted instantiation: action_helpers.c:_PyLexer_IsTString |
53 | | |
54 | | static inline int |
55 | | _PyLexer_IsRawString(ftstring_kind kind) |
56 | 58.1k | { |
57 | 58.1k | return kind == RAW_FSTRING || kind == RAW_TSTRING; |
58 | 58.1k | } Unexecuted instantiation: reader.c:_PyLexer_IsRawString Unexecuted instantiation: helpers.c:_PyLexer_IsRawString Unexecuted instantiation: pegen.c:_PyLexer_IsRawString Unexecuted instantiation: lexer.c:_PyLexer_IsRawString Unexecuted instantiation: layout.c:_PyLexer_IsRawString Unexecuted instantiation: number.c:_PyLexer_IsRawString Unexecuted instantiation: state.c:_PyLexer_IsRawString string.c:_PyLexer_IsRawString Line | Count | Source | 56 | 58.1k | { | 57 | 58.1k | return kind == RAW_FSTRING || kind == RAW_TSTRING; | 58 | 58.1k | } |
Unexecuted instantiation: decoder.c:_PyLexer_IsRawString Unexecuted instantiation: api.c:_PyLexer_IsRawString Unexecuted instantiation: action_helpers.c:_PyLexer_IsRawString |
59 | | |
60 | | typedef struct { |
61 | | int column; |
62 | | int alternate_column; |
63 | | } indentation_level; |
64 | | |
65 | | typedef struct { |
66 | | int depth; |
67 | | int pending; |
68 | | int at_bol; |
69 | | int comment_newline; |
70 | | indentation_level stack[MAXINDENT]; |
71 | | } lexer_layout_state; |
72 | | |
73 | | /* Tokenizer state */ |
74 | | struct tok_state { |
75 | | _PyTok_Off buf_offset; |
76 | | _PyTok_Off cur; |
77 | | _PyTok_Off inp; |
78 | | _PyTok_Off start; |
79 | | _PyTok_Off line_start; |
80 | | _PyTok_SourceText source; |
81 | | int done; /* E_OK normally, E_EOF at EOF, otherwise error code */ |
82 | | /* NB If done != E_OK, cur must be == inp!!! */ |
83 | | lexer_layout_state layout; |
84 | | int lineno; /* Current line number */ |
85 | | _PyTok_Loc start_loc; |
86 | | _PyTokenizer_Diagnostic diagnostic; |
87 | | int level; /* () [] {} Parentheses nesting level */ |
88 | | /* Used to allow free continuations inside them */ |
89 | | char parenstack[MAXLEVEL]; |
90 | | int parenlinenostack[MAXLEVEL]; |
91 | | int parencolstack[MAXLEVEL]; |
92 | | PyObject *filename; |
93 | | PyObject *module; |
94 | | struct _PyTok_Reader *reader; |
95 | | |
96 | | int type_comments; /* Whether to look for type comments */ |
97 | | |
98 | | ftstring_state *ftstring_stack; |
99 | | ftstring_state ftstring_stack_inline[FTSTRING_STACK_INLINE_CAPACITY]; |
100 | | int ftstring_depth; |
101 | | int ftstring_capacity; |
102 | | int tok_extra_tokens; |
103 | | int implicit_newline; |
104 | | #ifdef Py_DEBUG |
105 | | int debug; |
106 | | #endif |
107 | | int barry_as_bdfl; |
108 | | }; |
109 | | |
110 | | static inline ftstring_state * |
111 | | _PyLexer_CurrentFTString(struct tok_state *tok) |
112 | 2.18M | { |
113 | 2.18M | assert(tok->ftstring_stack != NULL); |
114 | 2.18M | assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity); |
115 | 2.18M | if (tok->ftstring_depth == 0) { |
116 | 1.98M | return NULL; |
117 | 1.98M | } |
118 | 202k | return &tok->ftstring_stack[tok->ftstring_depth - 1]; |
119 | 2.18M | } reader.c:_PyLexer_CurrentFTString Line | Count | Source | 112 | 304k | { | 113 | 304k | assert(tok->ftstring_stack != NULL); | 114 | 304k | assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity); | 115 | 304k | if (tok->ftstring_depth == 0) { | 116 | 300k | return NULL; | 117 | 300k | } | 118 | 4.24k | return &tok->ftstring_stack[tok->ftstring_depth - 1]; | 119 | 304k | } |
Unexecuted instantiation: helpers.c:_PyLexer_CurrentFTString Unexecuted instantiation: pegen.c:_PyLexer_CurrentFTString lexer.c:_PyLexer_CurrentFTString Line | Count | Source | 112 | 1.86M | { | 113 | 1.86M | assert(tok->ftstring_stack != NULL); | 114 | 1.86M | assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity); | 115 | 1.86M | if (tok->ftstring_depth == 0) { | 116 | 1.67M | return NULL; | 117 | 1.67M | } | 118 | 184k | return &tok->ftstring_stack[tok->ftstring_depth - 1]; | 119 | 1.86M | } |
Unexecuted instantiation: layout.c:_PyLexer_CurrentFTString Unexecuted instantiation: number.c:_PyLexer_CurrentFTString state.c:_PyLexer_CurrentFTString Line | Count | Source | 112 | 13.5k | { | 113 | 13.5k | assert(tok->ftstring_stack != NULL); | 114 | 13.5k | assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity); | 115 | 13.5k | if (tok->ftstring_depth == 0) { | 116 | 0 | return NULL; | 117 | 0 | } | 118 | 13.5k | return &tok->ftstring_stack[tok->ftstring_depth - 1]; | 119 | 13.5k | } |
string.c:_PyLexer_CurrentFTString Line | Count | Source | 112 | 406 | { | 113 | 406 | assert(tok->ftstring_stack != NULL); | 114 | 406 | assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity); | 115 | 406 | if (tok->ftstring_depth == 0) { | 116 | 361 | return NULL; | 117 | 361 | } | 118 | 45 | return &tok->ftstring_stack[tok->ftstring_depth - 1]; | 119 | 406 | } |
Unexecuted instantiation: decoder.c:_PyLexer_CurrentFTString Unexecuted instantiation: api.c:_PyLexer_CurrentFTString Unexecuted instantiation: action_helpers.c:_PyLexer_CurrentFTString |
120 | | |
121 | | static inline char |
122 | | _PyLexer_StringPrefix(ftstring_kind kind) |
123 | 430 | { |
124 | 430 | return _PyLexer_IsTString(kind) ? 't' : 'f'; |
125 | 430 | } Unexecuted instantiation: reader.c:_PyLexer_StringPrefix Unexecuted instantiation: helpers.c:_PyLexer_StringPrefix Unexecuted instantiation: pegen.c:_PyLexer_StringPrefix lexer.c:_PyLexer_StringPrefix Line | Count | Source | 123 | 6 | { | 124 | 6 | return _PyLexer_IsTString(kind) ? 't' : 'f'; | 125 | 6 | } |
Unexecuted instantiation: layout.c:_PyLexer_StringPrefix Unexecuted instantiation: number.c:_PyLexer_StringPrefix Unexecuted instantiation: state.c:_PyLexer_StringPrefix string.c:_PyLexer_StringPrefix Line | Count | Source | 123 | 424 | { | 124 | 424 | return _PyLexer_IsTString(kind) ? 't' : 'f'; | 125 | 424 | } |
Unexecuted instantiation: decoder.c:_PyLexer_StringPrefix Unexecuted instantiation: api.c:_PyLexer_StringPrefix Unexecuted instantiation: action_helpers.c:_PyLexer_StringPrefix |
126 | | |
127 | | static inline int |
128 | | _PyLexer_FTStringBracketDepth(const struct tok_state *tok, |
129 | | const ftstring_state *state) |
130 | 92.7k | { |
131 | 92.7k | return tok->level - state->paren_level; |
132 | 92.7k | } Unexecuted instantiation: reader.c:_PyLexer_FTStringBracketDepth Unexecuted instantiation: helpers.c:_PyLexer_FTStringBracketDepth Unexecuted instantiation: pegen.c:_PyLexer_FTStringBracketDepth lexer.c:_PyLexer_FTStringBracketDepth Line | Count | Source | 130 | 24.2k | { | 131 | 24.2k | return tok->level - state->paren_level; | 132 | 24.2k | } |
Unexecuted instantiation: layout.c:_PyLexer_FTStringBracketDepth Unexecuted instantiation: number.c:_PyLexer_FTStringBracketDepth Unexecuted instantiation: state.c:_PyLexer_FTStringBracketDepth string.c:_PyLexer_FTStringBracketDepth Line | Count | Source | 130 | 68.4k | { | 131 | 68.4k | return tok->level - state->paren_level; | 132 | 68.4k | } |
Unexecuted instantiation: decoder.c:_PyLexer_FTStringBracketDepth Unexecuted instantiation: api.c:_PyLexer_FTStringBracketDepth Unexecuted instantiation: action_helpers.c:_PyLexer_FTStringBracketDepth |
133 | | |
134 | | static inline int |
135 | | _PyLexer_ByteColumn(const struct tok_state *tok) |
136 | 5.69M | { |
137 | 5.69M | assert(tok->line_start >= 0); |
138 | 5.69M | assert(tok->cur >= tok->line_start); |
139 | 5.69M | Py_ssize_t column = tok->cur - tok->line_start; |
140 | | assert(column <= INT_MAX); |
141 | 5.69M | return (int)column; |
142 | 5.69M | } Unexecuted instantiation: reader.c:_PyLexer_ByteColumn Unexecuted instantiation: helpers.c:_PyLexer_ByteColumn Unexecuted instantiation: pegen.c:_PyLexer_ByteColumn lexer.c:_PyLexer_ByteColumn Line | Count | Source | 136 | 3.85M | { | 137 | 3.85M | assert(tok->line_start >= 0); | 138 | 3.85M | assert(tok->cur >= tok->line_start); | 139 | 3.85M | Py_ssize_t column = tok->cur - tok->line_start; | 140 | | assert(column <= INT_MAX); | 141 | 3.85M | return (int)column; | 142 | 3.85M | } |
Unexecuted instantiation: layout.c:_PyLexer_ByteColumn Unexecuted instantiation: number.c:_PyLexer_ByteColumn state.c:_PyLexer_ByteColumn Line | Count | Source | 136 | 1.78M | { | 137 | 1.78M | assert(tok->line_start >= 0); | 138 | 1.78M | assert(tok->cur >= tok->line_start); | 139 | 1.78M | Py_ssize_t column = tok->cur - tok->line_start; | 140 | | assert(column <= INT_MAX); | 141 | 1.78M | return (int)column; | 142 | 1.78M | } |
string.c:_PyLexer_ByteColumn Line | Count | Source | 136 | 58.1k | { | 137 | 58.1k | assert(tok->line_start >= 0); | 138 | 58.1k | assert(tok->cur >= tok->line_start); | 139 | 58.1k | Py_ssize_t column = tok->cur - tok->line_start; | 140 | | assert(column <= INT_MAX); | 141 | 58.1k | return (int)column; | 142 | 58.1k | } |
Unexecuted instantiation: decoder.c:_PyLexer_ByteColumn Unexecuted instantiation: api.c:_PyLexer_ByteColumn Unexecuted instantiation: action_helpers.c:_PyLexer_ByteColumn |
143 | | |
144 | | int _PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, _PyTok_Off start, _PyTok_Off end); |
145 | | |
146 | | void _PyLexer_ImplyDedents(struct tok_state *); |
147 | | |
148 | | void _PyTokenizer_Free(struct tok_state *); |
149 | | ftstring_state *_PyLexer_PushFTString(struct tok_state *); |
150 | | void _PyLexer_PopFTString(struct tok_state *); |
151 | | |
152 | | |
153 | | #endif |