|
| 1 | +#include "Python.h" |
| 2 | +#include "../Parser/tokenizer.h" |
| 3 | + |
| 4 | +static struct PyModuleDef _tokenizemodule; |
| 5 | + |
| 6 | +typedef struct { |
| 7 | + PyTypeObject* TokenizerIter; |
| 8 | +} tokenize_state; |
| 9 | + |
| 10 | +static tokenize_state* |
| 11 | +get_tokenize_state(PyObject* module) |
| 12 | +{ |
| 13 | + return (tokenize_state*)PyModule_GetState(module); |
| 14 | +} |
| 15 | + |
| 16 | +#define _tokenize_get_state_by_type(type) \ |
| 17 | + get_tokenize_state(_PyType_GetModuleByDef(type, &_tokenizemodule)) |
| 18 | + |
| 19 | +#include "clinic/Python-tokenize.c.h" |
| 20 | + |
| 21 | +/*[clinic input] |
| 22 | +module _tokenizer |
| 23 | +class _tokenizer.tokenizeriter "tokenizeriterobject *" "_tokenize_get_state_by_type(type)->TokenizerIter" |
| 24 | +[clinic start generated code]*/ |
| 25 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=96d98ee2fef7a8bc]*/ |
| 26 | + |
| 27 | +typedef struct { |
| 28 | + PyObject_HEAD |
| 29 | + struct tok_state* tok; |
| 30 | +} tokenizeriterobject; |
| 31 | + |
| 32 | +/*[clinic input] |
| 33 | +@classmethod |
| 34 | +_tokenizer.tokenizeriter.__new__ as tokenizeriter_new |
| 35 | +
|
| 36 | + source: str |
| 37 | +[clinic start generated code]*/ |
| 38 | + |
| 39 | +static PyObject * |
| 40 | +tokenizeriter_new_impl(PyTypeObject *type, const char *source) |
| 41 | +/*[clinic end generated code: output=7fd9f46cf9263cbb input=4384b368407375c6]*/ |
| 42 | +{ |
| 43 | + tokenizeriterobject* self = (tokenizeriterobject*)type->tp_alloc(type, 0); |
| 44 | + if (self == NULL) { |
| 45 | + return NULL; |
| 46 | + } |
| 47 | + PyObject* filename = PyUnicode_FromString("<string>"); |
| 48 | + if (filename == NULL) { |
| 49 | + return NULL; |
| 50 | + } |
| 51 | + self->tok = PyTokenizer_FromUTF8(source, 1); |
| 52 | + if (self->tok == NULL) { |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + self->tok->filename = filename; |
| 56 | + return (PyObject*)self; |
| 57 | +} |
| 58 | + |
| 59 | +static PyObject* |
| 60 | +tokenizeriter_next(tokenizeriterobject* it) |
| 61 | +{ |
| 62 | + const char* start; |
| 63 | + const char* end; |
| 64 | + int type = PyTokenizer_Get(it->tok, &start, &end); |
| 65 | + if (type == ERRORTOKEN && PyErr_Occurred()) { |
| 66 | + return NULL; |
| 67 | + } |
| 68 | + if (type == ERRORTOKEN || type == ENDMARKER) { |
| 69 | + PyErr_SetString(PyExc_StopIteration, "EOF"); |
| 70 | + return NULL; |
| 71 | + } |
| 72 | + PyObject* str = NULL; |
| 73 | + if (start == NULL || end == NULL) { |
| 74 | + str = PyUnicode_FromString(""); |
| 75 | + } else { |
| 76 | + str = PyUnicode_FromStringAndSize(start, end - start); |
| 77 | + } |
| 78 | + if (str == NULL) { |
| 79 | + return NULL; |
| 80 | + } |
| 81 | + |
| 82 | + Py_ssize_t size = it->tok->inp - it->tok->buf; |
| 83 | + PyObject* line = PyUnicode_DecodeUTF8(it->tok->buf, size, "replace"); |
| 84 | + if (line == NULL) { |
| 85 | + Py_DECREF(str); |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + const char* line_start = type == STRING ? it->tok->multi_line_start : it->tok->line_start; |
| 89 | + int lineno = type == STRING ? it->tok->first_lineno : it->tok->lineno; |
| 90 | + int end_lineno = it->tok->lineno; |
| 91 | + int col_offset = -1; |
| 92 | + int end_col_offset = -1; |
| 93 | + if (start != NULL && start >= line_start) { |
| 94 | + col_offset = (int)(start - line_start); |
| 95 | + } |
| 96 | + if (end != NULL && end >= it->tok->line_start) { |
| 97 | + end_col_offset = (int)(end - it->tok->line_start); |
| 98 | + } |
| 99 | + |
| 100 | + return Py_BuildValue("(NiiiiiN)", str, type, lineno, end_lineno, col_offset, end_col_offset, line); |
| 101 | +} |
| 102 | + |
| 103 | +static void |
| 104 | +tokenizeriter_dealloc(tokenizeriterobject* it) |
| 105 | +{ |
| 106 | + PyTypeObject* tp = Py_TYPE(it); |
| 107 | + PyTokenizer_Free(it->tok); |
| 108 | + tp->tp_free(it); |
| 109 | + Py_DECREF(tp); |
| 110 | +} |
| 111 | + |
| 112 | +static PyType_Slot tokenizeriter_slots[] = { |
| 113 | + {Py_tp_new, tokenizeriter_new}, |
| 114 | + {Py_tp_dealloc, tokenizeriter_dealloc}, |
| 115 | + {Py_tp_getattro, PyObject_GenericGetAttr}, |
| 116 | + {Py_tp_iter, PyObject_SelfIter}, |
| 117 | + {Py_tp_iternext, tokenizeriter_next}, |
| 118 | + {0, NULL}, |
| 119 | +}; |
| 120 | + |
| 121 | +static PyType_Spec tokenizeriter_spec = { |
| 122 | + .name = "_tokenize.TokenizerIter", |
| 123 | + .basicsize = sizeof(tokenizeriterobject), |
| 124 | + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE), |
| 125 | + .slots = tokenizeriter_slots, |
| 126 | +}; |
| 127 | + |
| 128 | + |
| 129 | +static int |
| 130 | +tokenizemodule_exec(PyObject* m) |
| 131 | +{ |
| 132 | + tokenize_state* state = get_tokenize_state(m); |
| 133 | + if (state == NULL) { |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + state->TokenizerIter = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 138 | + m, &tokenizeriter_spec, NULL); |
| 139 | + if (state->TokenizerIter == NULL) { |
| 140 | + return -1; |
| 141 | + } |
| 142 | + if (PyModule_AddType(m, state->TokenizerIter) < 0) { |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
| 149 | +static PyMethodDef tokenize_methods[] = { |
| 150 | + {NULL, NULL, 0, NULL} /* Sentinel */ |
| 151 | +}; |
| 152 | + |
| 153 | +static PyModuleDef_Slot tokenizemodule_slots[] = { |
| 154 | + {Py_mod_exec, tokenizemodule_exec}, |
| 155 | + {0, NULL} |
| 156 | +}; |
| 157 | + |
| 158 | +static int |
| 159 | +tokenizemodule_traverse(PyObject *m, visitproc visit, void *arg) |
| 160 | +{ |
| 161 | + tokenize_state *state = get_tokenize_state(m); |
| 162 | + Py_VISIT(state->TokenizerIter); |
| 163 | + return 0; |
| 164 | +} |
| 165 | + |
| 166 | +static int |
| 167 | +tokenizemodule_clear(PyObject *m) |
| 168 | +{ |
| 169 | + tokenize_state *state = get_tokenize_state(m); |
| 170 | + Py_CLEAR(state->TokenizerIter); |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
| 174 | +static void |
| 175 | +tokenizemodule_free(void *m) |
| 176 | +{ |
| 177 | + tokenizemodule_clear((PyObject *)m); |
| 178 | +} |
| 179 | + |
| 180 | +static struct PyModuleDef _tokenizemodule = { |
| 181 | + PyModuleDef_HEAD_INIT, |
| 182 | + .m_name = "_tokenize", |
| 183 | + .m_size = sizeof(tokenize_state), |
| 184 | + .m_slots = tokenizemodule_slots, |
| 185 | + .m_methods = tokenize_methods, |
| 186 | + .m_traverse = tokenizemodule_traverse, |
| 187 | + .m_clear = tokenizemodule_clear, |
| 188 | + .m_free = tokenizemodule_free, |
| 189 | +}; |
| 190 | + |
| 191 | +PyMODINIT_FUNC |
| 192 | +PyInit__tokenize(void) |
| 193 | +{ |
| 194 | + return PyModuleDef_Init(&_tokenizemodule); |
| 195 | +} |
0 commit comments