Repository navigation
Expand file tree
/
Copy pathtokenizer.h
More file actions
132 lines (120 loc) · 4.92 KB
/
Copy pathtokenizer.h
File metadata and controls
132 lines (120 loc) · 4.92 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
#ifndef Py_TOKENIZER_H
#define Py_TOKENIZER_H
#include "Python.h"
#include "types.h"
struct tok_state;
typedef enum {
_PyTokenizer_KEEP_NEWLINE, // Preserve the original line terminator.
_PyTokenizer_IMPLY_NEWLINE, // Add a missing line terminator.
_PyTokenizer_APPEND_NEWLINE, // Also append a blank line to terminated input.
} _PyTokenizer_Newline;
/* Reasons that the appended newline can change the token stream. */
enum {
_PyTokenizer_NEWLINE_STATEMENT = 1,
_PyTokenizer_NEWLINE_INDENT = 2,
_PyTokenizer_NEWLINE_CONTINUATION = 4,
};
/* Initialize before use. metadata owns a reference released by _PyToken_Free;
a consumer taking that reference must set metadata to NULL. */
struct token {
int type;
int level;
unsigned char is_raw;
unsigned char is_incomplete;
_PyTok_Span span;
_PyTok_Loc start_loc;
_PyTok_Loc end_loc;
PyObject *metadata;
};
typedef struct {
const char *text;
Py_ssize_t length;
_PyTok_Span line_span;
_PyTok_Off end_line_start;
int implicit_newline;
int at_eof;
} _PyToken_View;
/* Supplemental source context for a terminal error. location is the reporting
cursor, independent of the scanner cursor; lineno == 0 means absent.
The text span may cover multiple physical lines. */
typedef struct {
_PyTok_Loc location;
_PyTok_Span text_span;
} _PyTokenizer_Diagnostic;
typedef struct {
int status;
unsigned char implicit_newline; // _PyTokenizer_Newline
_PyTokenizer_Diagnostic diagnostic;
_PyTok_Loc location;
_PyTok_Off cursor;
_PyTok_Span input_span;
_PyTok_Span line_span;
int level;
char delimiter;
_PyTok_Loc delimiter_loc;
int in_formatted_string;
int is_interactive;
int is_file;
PyObject *filename;
PyObject *module;
const char *encoding;
} _PyTokenizer_Info;
/* Get replaces the initialized token, releasing its previous metadata.
Errors are returned as ERRORTOKEN, with or without a Python exception.
In incomplete-input mode, an unfinished string retains its token type,
sets is_incomplete and has E_INCOMPLETE status. Only f-string and t-string
middle tokens retain their text span. No Python exception is set. */
void _PyTokenizer_Get(struct tok_state *, struct token *);
void _PyTokenizer_Free(struct tok_state *);
int _PyTokenizer_Traverse(struct tok_state *, visitproc, void *);
void _PyTokenizer_raise_init_error(PyObject *filename);
void _PyToken_Init(struct token *);
static inline void
_PyToken_Free(struct token *token)
{
Py_CLEAR(token->metadata);
}
/* Views and borrowed snapshot references remain valid until the tokenizer is
mutated or freed. Source spans may be discarded when reading more input. */
_PyTokenizer_Info _PyTokenizer_GetInfo(const struct tok_state *);
int _PyTokenizer_GetStatus(const struct tok_state *);
int _PyTokenizer_GetNewlineRequirements(const struct tok_state *);
/* An absent token span has a nonnull empty text view. */
const char *_PyToken_TextView(
const struct tok_state *, const struct token *, Py_ssize_t *);
/* Use the token from the most recent Get. An absent span has nonnull empty text.
line_span covers the token's complete physical line range. end_line_start
is the logical offset of its final physical line. */
void _PyToken_GetView(
const struct tok_state *tok, const struct token *token,
_PyToken_View *view);
const char *_PyTokenizer_SpanView(
const struct tok_state *, _PyTok_Span, Py_ssize_t *);
/* For retained input: 1-based lines clamp to the first or final line.
The view excludes the newline and need not be NUL-terminated. */
const char *_PyTokenizer_LineView(
const struct tok_state *, Py_ssize_t, Py_ssize_t *);
/* Return NUL-terminated retained input, or NULL without setting an exception
for streaming input or interactive input before its first line. */
const char *_PyTokenizer_RetainedSource(const struct tok_state *);
void _PyTokenizer_SetContext(
struct tok_state *tok, PyObject *filename, PyObject *module);
void _PyTokenizer_SetOptions(
struct tok_state *tok, int extra_tokens, int type_comments);
void _PyTokenizer_AllowIncompleteInput(struct tok_state *, int);
void _PyTokenizer_ImplyDedents(struct tok_state *);
int _PyTokenizer_HasTrailingStatement(const struct tok_state *);
int _PyTokenizer_IsInteractive(const struct tok_state *);
void _PyTokenizer_StopInteractive(struct tok_state *);
struct tok_state *_PyTokenizer_FromString(
const char *input, int exec_input, int preserve_crlf);
struct tok_state *_PyTokenizer_FromUTF8(
const char *input, int exec_input, int preserve_crlf);
struct tok_state *_PyTokenizer_FromReadline(
PyObject *readline, const char *encoding);
struct tok_state *_PyTokenizer_FromFile(
FILE *fp, const char *encoding, const char *ps1, const char *ps2);
/* Return the declared encoding in PyMem-allocated storage, or NULL.
An exception is set on error. */
char *_PyTokenizer_FindEncodingFilename(int, PyObject *);
#endif /* !Py_TOKENIZER_H */