/* Tokenizer for MWParserFromHell Copyright (C) 2012-2013 Ben Kurtovic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "tokenizer.h" /* Given a context, return the heading level encoded within it. */ static int heading_level_from_context(int n) { int level; n /= LC_HEADING_LEVEL_1; for (level = 1; n > 1; n >>= 1) level++; return level; } /* Call the given function in tag_defs, using 'tag' as a parameter, and return its output as a bool. */ static int call_tag_def_func(const char* funcname, PyObject* tag) { PyObject* func = PyObject_GetAttrString(tag_defs, funcname); PyObject* result = PyObject_CallFunctionObjArgs(func, tag, NULL); int ans = (result == Py_True) ? 1 : 0; Py_DECREF(func); Py_DECREF(result); return ans; } /* Sanitize the name of a tag so it can be compared with others for equality. */ static PyObject* strip_tag_name(PyObject* token) { PyObject *text, *rstripped, *lowered; text = PyObject_GetAttrString(token, "text"); if (!text) return NULL; rstripped = PyObject_CallMethod(text, "rstrip", NULL); Py_DECREF(text); if (!rstripped) return NULL; lowered = PyObject_CallMethod(rstripped, "rstrip", NULL); Py_DECREF(rstripped); return lowered; } static Textbuffer* Textbuffer_new(void) { Textbuffer* buffer = malloc(sizeof(Textbuffer)); if (!buffer) { PyErr_NoMemory(); return NULL; } buffer->size = 0; buffer->data = malloc(sizeof(Py_UNICODE) * TEXTBUFFER_BLOCKSIZE); if (!buffer->data) { free(buffer); PyErr_NoMemory(); return NULL; } buffer->next = NULL; return buffer; } static void Textbuffer_dealloc(Textbuffer* self) { Textbuffer* next; while (self) { free(self->data); next = self->next; free(self); self = next; } } /* Write a Unicode codepoint to the given textbuffer. */ static int Textbuffer_write(Textbuffer** this, Py_UNICODE code) { Textbuffer* self = *this; if (self->size == TEXTBUFFER_BLOCKSIZE) { Textbuffer* new = Textbuffer_new(); if (!new) return -1; new->next = self; *this = self = new; } self->data[self->size] = code; self->size++; return 0; } /* Return the contents of the textbuffer as a Python Unicode object. */ static PyObject* Textbuffer_render(Textbuffer* self) { PyObject *result = PyUnicode_FromUnicode(self->data, self->size); PyObject *left, *concat; while (self->next) { self = self->next; left = PyUnicode_FromUnicode(self->data, self->size); concat = PyUnicode_Concat(left, result); Py_DECREF(left); Py_DECREF(result); result = concat; } return result; } static TagData* TagData_new(void) { TagData *self = malloc(sizeof(TagData)); #define ALLOC_BUFFER(name) \ name = Textbuffer_new(); \ if (!name) { \ TagData_dealloc(self); \ return NULL; \ } if (!self) { PyErr_NoMemory(); return NULL; } self->context = TAG_NAME; ALLOC_BUFFER(self->pad_first) ALLOC_BUFFER(self->pad_before_eq) ALLOC_BUFFER(self->pad_after_eq) self->reset = 0; return self; } static void TagData_dealloc(TagData* self) { #define DEALLOC_BUFFER(name) \ if (name) \ Textbuffer_dealloc(name); DEALLOC_BUFFER(self->pad_first); DEALLOC_BUFFER(self->pad_before_eq); DEALLOC_BUFFER(self->pad_after_eq); free(self); } static int TagData_reset_buffers(TagData* self) { #define RESET_BUFFER(name) \ Textbuffer_dealloc(name); \ name = Textbuffer_new(); \ if (!name) \ return -1; RESET_BUFFER(self->pad_first) RESET_BUFFER(self->pad_before_eq) RESET_BUFFER(self->pad_after_eq) return 0; } static PyObject* Tokenizer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { Tokenizer* self = (Tokenizer*) type->tp_alloc(type, 0); return (PyObject*) self; } static void Tokenizer_dealloc(Tokenizer* self) { Stack *this = self->topstack, *next; Py_XDECREF(self->text); while (this) { Py_DECREF(this->stack); Textbuffer_dealloc(this->textbuffer); next = this->next; free(this); this = next; } Py_TYPE(self)->tp_free((PyObject*) self); } static int Tokenizer_init(Tokenizer* self, PyObject* args, PyObject* kwds) { static char* kwlist[] = {NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist)) return -1; self->text = Py_None; Py_INCREF(Py_None); self->topstack = NULL; self->head = self->length = self->global = self->depth = self->cycles = 0; return 0; } /* Add a new token stack, context, and textbuffer to the list. */ static int Tokenizer_push(Tokenizer* self, int context) { Stack* top = malloc(sizeof(Stack)); if (!top) { PyErr_NoMemory(); return -1; } top->stack = PyList_New(0); top->context = context; top->textbuffer = Textbuffer_new(); if (!top->textbuffer) return -1; top->next = self->topstack; self->topstack = top; self->depth++; self->cycles++; return 0; } /* Push the textbuffer onto the stack as a Text node and clear it. */ static int Tokenizer_push_textbuffer(Tokenizer* self) { PyObject *text, *kwargs, *token; Textbuffer* buffer = self->topstack->textbuffer; if (buffer->size == 0 && !buffer->next) return 0; text = Textbuffer_render(buffer); if (!text) return -1; kwargs = PyDict_New(); if (!kwargs) { Py_DECREF(text); return -1; } PyDict_SetItemString(kwargs, "text", text); Py_DECREF(text); token = PyObject_Call(Text, NOARGS, kwargs); Py_DECREF(kwargs); if (!token) return -1; if (PyList_Append(self->topstack->stack, token)) { Py_DECREF(token); return -1; } Py_DECREF(token); Textbuffer_dealloc(buffer); self->topstack->textbuffer = Textbuffer_new(); if (!self->topstack->textbuffer) return -1; return 0; } /* Pop and deallocate the top token stack/context/textbuffer. */ static void Tokenizer_delete_top_of_stack(Tokenizer* self) { Stack* top = self->topstack; Py_DECREF(top->stack); Textbuffer_dealloc(top->textbuffer); self->topstack = top->next; free(top); self->depth--; } /* Pop the current stack/context/textbuffer, returing the stack. */ static PyObject* Tokenizer_pop(Tokenizer* self) { PyObject* stack; if (Tokenizer_push_textbuffer(self)) return NULL; stack = self->topstack->stack; Py_INCREF(stack); Tokenizer_delete_top_of_stack(self); return stack; } /* Pop the current stack/context/textbuffer, returing the stack. We will also replace the underlying stack's context with the current stack's. */ static PyObject* Tokenizer_pop_keeping_context(Tokenizer* self) { PyObject* stack; int context; if (Tokenizer_push_textbuffer(self)) return NULL; stack = self->topstack->stack; Py_INCREF(stack); context = self->topstack->context; Tokenizer_delete_top_of_stack(self); self->topstack->context = context; return stack; } /* Fail the current tokenization route. Discards the current stack/context/textbuffer and raises a BadRoute exception. */ static void* Tokenizer_fail_route(Tokenizer* self) { int context = self->topstack->context; PyObject* stack = Tokenizer_pop(self); Py_XDECREF(stack); FAIL_ROUTE(context); return NULL; } /* Write a token to the end of the current token stack. */ static int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first) { PyObject* instance; if (Tokenizer_push_textbuffer(self)) return -1; instance = PyObject_CallObject(token, NULL); if (!instance) return -1; if (first ? PyList_Insert(self->topstack->stack, 0, instance) : PyList_Append(self->topstack->stack, instance)) { Py_DECREF(instance); return -1; } Py_DECREF(instance); return 0; } /* Write a token to the end of the current token stack. */ static int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token, PyObject* kwargs, int first) { PyObject* instance; if (Tokenizer_push_textbuffer(self)) { Py_DECREF(kwargs); return -1; } instance = PyObject_Call(token, NOARGS, kwargs); if (!instance) { Py_DECREF(kwargs); return -1; } if (first ? PyList_Insert(self->topstack->stack, 0, instance): PyList_Append(self->topstack->stack, instance)) { Py_DECREF(instance); Py_DECREF(kwargs); return -1; } Py_DECREF(instance); Py_DECREF(kwargs); return 0; } /* Write a Unicode codepoint to the current textbuffer. */ static int Tokenizer_emit_char(Tokenizer* self, Py_UNICODE code) { return Textbuffer_write(&(self->topstack->textbuffer), code); } /* Write a string of text to the current textbuffer. */ static int Tokenizer_emit_text(Tokenizer* self, const char* text) { int i = 0; while (text[i]) { if (Tokenizer_emit_char(self, text[i])) return -1; i++; } return 0; } /* Write a series of tokens to the current stack at once. */ static int Tokenizer_emit_all(Tokenizer* self, PyObject* tokenlist) { int pushed = 0; PyObject *stack, *token, *left, *right, *text; Textbuffer* buffer; Py_ssize_t size; if (PyList_GET_SIZE(tokenlist) > 0) { token = PyList_GET_ITEM(tokenlist, 0); switch (PyObject_IsInstance(token, Text)) { case 0: break; case 1: { pushed = 1; buffer = self->topstack->textbuffer; if (buffer->size == 0 && !buffer->next) break; left = Textbuffer_render(buffer); if (!left) return -1; right = PyObject_GetAttrString(token, "text"); if (!right) return -1; text = PyUnicode_Concat(left, right); Py_DECREF(left); Py_DECREF(right); if (!text) return -1; if (PyObject_SetAttrString(token, "text", text)) { Py_DECREF(text); return -1; } Py_DECREF(text); Textbuffer_dealloc(buffer); self->topstack->textbuffer = Textbuffer_new(); if (!self->topstack->textbuffer) return -1; break; } case -1: return -1; } } if (!pushed) { if (Tokenizer_push_textbuffer(self)) return -1; } stack = self->topstack->stack; size = PyList_GET_SIZE(stack); if (PyList_SetSlice(stack, size, size, tokenlist)) return -1; return 0; } /* Pop the current stack, write text, and then write the stack. 'text' is a NULL-terminated array of chars. */ static int Tokenizer_emit_text_then_stack(Tokenizer* self, const char* text) { PyObject* stack = Tokenizer_pop(self); if (Tokenizer_emit_text(self, text)) { Py_DECREF(stack); return -1; } if (stack) { if (PyList_GET_SIZE(stack) > 0) { if (Tokenizer_emit_all(self, stack)) { Py_DECREF(stack); return -1; } } Py_DECREF(stack); } self->head--; return 0; } /* Read the value at a relative point in the wikicode, forwards. */ static PyObject* Tokenizer_read(Tokenizer* self, Py_ssize_t delta) { Py_ssize_t index = self->head + delta; if (index >= self->length) return EMPTY; return PyList_GET_ITEM(self->text, index); } /* Read the value at a relative point in the wikicode, backwards. */ static PyObject* Tokenizer_read_backwards(Tokenizer* self, Py_ssize_t delta) { Py_ssize_t index; if (delta > self->head) return EMPTY; index = self->head - delta; return PyList_GET_ITEM(self->text, index); } /* Parse a template at the head of the wikicode string. */ static int Tokenizer_parse_template(Tokenizer* self) { PyObject *template; Py_ssize_t reset = self->head; template = Tokenizer_parse(self, LC_TEMPLATE_NAME, 1); if (BAD_ROUTE) { self->head = reset; return 0; } if (!template) return -1; if (Tokenizer_emit_first(self, TemplateOpen)) { Py_DECREF(template); return -1; } if (Tokenizer_emit_all(self, template)) { Py_DECREF(template); return -1; } Py_DECREF(template); if (Tokenizer_emit(self, TemplateClose)) return -1; return 0; } /* Parse an argument at the head of the wikicode string. */ static int Tokenizer_parse_argument(Tokenizer* self) { PyObject *argument; Py_ssize_t reset = self->head; argument = Tokenizer_parse(self, LC_ARGUMENT_NAME, 1); if (BAD_ROUTE) { self->head = reset; return 0; } if (!argument) return -1; if (Tokenizer_emit_first(self, ArgumentOpen)) { Py_DECREF(argument); return -1; } if (Tokenizer_emit_all(self, argument)) { Py_DECREF(argument); return -1; } Py_DECREF(argument); if (Tokenizer_emit(self, ArgumentClose)) return -1; return 0; } /* Parse a template or argument at the head of the wikicode string. */ static int Tokenizer_parse_template_or_argument(Tokenizer* self) { unsigned int braces = 2, i; PyObject *tokenlist; self->head += 2; while (Tokenizer_READ(self, 0) == *"{" && braces < MAX_BRACES) { self->head++; braces++; } if (Tokenizer_push(self, 0)) return -1; while (braces) { if (braces == 1) { if (Tokenizer_emit_text_then_stack(self, "{")) return -1; return 0; } if (braces == 2) { if (Tokenizer_parse_template(self)) return -1; if (BAD_ROUTE) { RESET_ROUTE(); if (Tokenizer_emit_text_then_stack(self, "{{")) return -1; return 0; } break; } if (Tokenizer_parse_argument(self)) return -1; if (BAD_ROUTE) { RESET_ROUTE(); if (Tokenizer_parse_template(self)) return -1; if (BAD_ROUTE) { char text[MAX_BRACES + 1]; RESET_ROUTE(); for (i = 0; i < braces; i++) text[i] = *"{"; text[braces] = *""; if (Tokenizer_emit_text_then_stack(self, text)) { Py_XDECREF(text); return -1; } Py_XDECREF(text); return 0; } else braces -= 2; } else braces -= 3; if (braces) self->head++; } tokenlist = Tokenizer_pop(self); if (!tokenlist) return -1; if (Tokenizer_emit_all(self, tokenlist)) { Py_DECREF(tokenlist); return -1; } Py_DECREF(tokenlist); if (self->topstack->context & LC_FAIL_NEXT) self->topstack->context ^= LC_FAIL_NEXT; return 0; } /* Handle a template parameter at the head of the string. */ static int Tokenizer_handle_template_param(Tokenizer* self) { PyObject *stack; if (self->topstack->context & LC_TEMPLATE_NAME) self->topstack->context ^= LC_TEMPLATE_NAME; else if (self->topstack->context & LC_TEMPLATE_PARAM_VALUE) self->topstack->context ^= LC_TEMPLATE_PARAM_VALUE; if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) { stack = Tokenizer_pop_keeping_context(self); if (!stack) return -1; if (Tokenizer_emit_all(self, stack)) { Py_DECREF(stack); return -1; } Py_DECREF(stack); } else self->topstack->context |= LC_TEMPLATE_PARAM_KEY; if (Tokenizer_emit(self, TemplateParamSeparator)) return -1; if (Tokenizer_push(self, self->topstack->context)) return -1; return 0; } /* Handle a template parameter's value at the head of the string. */ static int Tokenizer_handle_template_param_value(Tokenizer* self) { PyObject *stack; stack = Tokenizer_pop_keeping_context(self); if (!stack) return -1; if (Tokenizer_emit_all(self, stack)) { Py_DECREF(stack); return -1; } Py_DECREF(stack); self->topstack->context ^= LC_TEMPLATE_PARAM_KEY; self->topstack->context |= LC_TEMPLATE_PARAM_VALUE; if (Tokenizer_emit(self, TemplateParamEquals)) return -1; return 0; } /* Handle the end of a template at the head of the string. */ static PyObject* Tokenizer_handle_template_end(Tokenizer* self) { PyObject* stack; if (self->topstack->context & LC_TEMPLATE_PARAM_KEY) { stack = Tokenizer_pop_keeping_context(self); if (!stack) return NULL; if (Tokenizer_emit_all(self, stack)) { Py_DECREF(stack); return NULL; } Py_DECREF(stack); } self->head++; stack = Tokenizer_pop(self); return stack; } /* Handle the separator between an argument's name and default. */ static int Tokenizer_handle_argument_separator(Tokenizer* self) { self->topstack->context ^= LC_ARGUMENT_NAME; self->topstack->context |= LC_ARGUMENT_DEFAULT; if (Tokenizer_emit(self, ArgumentSeparator)) return -1; return 0; } /* Handle the end of an argument at the head of the string. */ static PyObject* Tokenizer_handle_argument_end(Tokenizer* self) { PyObject* stack = Tokenizer_pop(self); self->head += 2; return stack; } /* Parse an internal wikilink at the head of the wikicode string. */ static int Tokenizer_parse_wikilink(Tokenizer* self) { Py_ssize_t reset; PyObject *wikilink; self->head += 2; reset = self->head - 1; wikilink = Tokenizer_parse(self, LC_WIKILINK_TITLE, 1); if (BAD_ROUTE) { RESET_ROUTE(); self->head = reset; if (Tokenizer_emit_text(self, "[[")) return -1; return 0; } if (!wikilink) return -1; if (Tokenizer_emit(self, WikilinkOpen)) { Py_DECREF(wikilink); return -1; } if (Tokenizer_emit_all(self, wikilink)) { Py_DECREF(wikilink); return -1; } Py_DECREF(wikilink); if (Tokenizer_emit(self, WikilinkClose)) return -1; if (self->topstack->context & LC_FAIL_NEXT) self->topstack->context ^= LC_FAIL_NEXT; return 0; } /* Handle the separator between a wikilink's title and its text. */ static int Tokenizer_handle_wikilink_separator(Tokenizer* self) { self->topstack->context ^= LC_WIKILINK_TITLE; self->topstack->context |= LC_WIKILINK_TEXT; if (Tokenizer_emit(self, WikilinkSeparator)) return -1; return 0; } /* Handle the end of a wikilink at the head of the string. */ static PyObject* Tokenizer_handle_wikilink_end(Tokenizer* self) { PyObject* stack = Tokenizer_pop(self); self->head += 1; return stack; } /* Parse a section heading at the head of the wikicode string. */ static int Tokenizer_parse_heading(Tokenizer* self) { Py_ssize_t reset = self->head; int best = 1, i, context, diff; HeadingData *heading; PyObject *level, *kwargs; self->global |= GL_HEADING; self->head += 1; while (Tokenizer_READ(self, 0) == *"=") { best++; self->head++; } context = LC_HEADING_LEVEL_1 << (best > 5 ? 5 : best - 1); heading = (HeadingData*) Tokenizer_parse(self, context, 1); if (BAD_ROUTE) { RESET_ROUTE(); self->head = reset + best - 1; for (i = 0; i < best; i++) { if (Tokenizer_emit_char(self, *"=")) return -1; } self->global ^= GL_HEADING; return 0; } #ifdef IS_PY3K level = PyLong_FromSsize_t(heading->level); #else level = PyInt_FromSsize_t(heading->level); #endif if (!level) { Py_DECREF(heading->title); free(heading); return -1; } kwargs = PyDict_New(); if (!kwargs) { Py_DECREF(level); Py_DECREF(heading->title); free(heading); return -1; } PyDict_SetItemString(kwargs, "level", level); Py_DECREF(level); if (Tokenizer_emit_kwargs(self, HeadingStart, kwargs)) { Py_DECREF(heading->title); free(heading); return -1; } if (heading->level < best) { diff = best - heading->level; for (i = 0; i < diff; i++) { if (Tokenizer_emit_char(self, *"=")) { Py_DECREF(heading->title); free(heading); return -1; } } } if (Tokenizer_emit_all(self, heading->title)) { Py_DECREF(heading->title); free(heading); return -1; } Py_DECREF(heading->title); free(heading); if (Tokenizer_emit(self, HeadingEnd)) return -1; self->global ^= GL_HEADING; return 0; } /* Handle the end of a section heading at the head of the string. */ static HeadingData* Tokenizer_handle_heading_end(Tokenizer* self) { Py_ssize_t reset = self->head, best; int i, current, level, diff; HeadingData *after, *heading; PyObject *stack; self->head += 1; best = 1; while (Tokenizer_READ(self, 0) == *"=") { best++; self->head++; } current = heading_level_from_context(self->topstack->context); level = current > best ? (best > 6 ? 6 : best) : (current > 6 ? 6 : current); after = (HeadingData*) Tokenizer_parse(self, self->topstack->context, 1); if (BAD_ROUTE) { RESET_ROUTE(); if (level < best) { diff = best - level; for (i = 0; i < diff; i++) { if (Tokenizer_emit_char(self, *"=")) return NULL; } } self->head = reset + best - 1; } else { for (i = 0; i < best; i++) { if (Tokenizer_emit_char(self, *"=")) { Py_DECREF(after->title); free(after); return NULL; } } if (Tokenizer_emit_all(self, after->title)) { Py_DECREF(after->title); free(after); return NULL; } Py_DECREF(after->title); level = after->level; free(after); } stack = Tokenizer_pop(self); if (!stack) return NULL; heading = malloc(sizeof(HeadingData)); if (!heading) { PyErr_NoMemory(); return NULL; } heading->title = stack; heading->level = level; return heading; } /* Actually parse an HTML entity and ensure that it is valid. */ static int Tokenizer_really_parse_entity(Tokenizer* self) { PyObject *kwargs, *textobj; Py_UNICODE this; int numeric, hexadecimal, i, j, zeroes, test; char *valid, *text, *buffer, *def; #define FAIL_ROUTE_AND_EXIT() { \ Tokenizer_fail_route(self); \ free(text); \ return 0; \ } if (Tokenizer_emit(self, HTMLEntityStart)) return -1; self->head++; this = Tokenizer_READ(self, 0); if (this == *"") { Tokenizer_fail_route(self); return 0; } if (this == *"#") { numeric = 1; if (Tokenizer_emit(self, HTMLEntityNumeric)) return -1; self->head++; this = Tokenizer_READ(self, 0); if (this == *"") { Tokenizer_fail_route(self); return 0; } if (this == *"x" || this == *"X") { hexadecimal = 1; kwargs = PyDict_New(); if (!kwargs) return -1; PyDict_SetItemString(kwargs, "char", Tokenizer_read(self, 0)); if (Tokenizer_emit_kwargs(self, HTMLEntityHex, kwargs)) return -1; self->head++; } else hexadecimal = 0; } else numeric = hexadecimal = 0; if (hexadecimal) valid = HEXDIGITS; else if (numeric) valid = DIGITS; else valid = ALPHANUM; text = calloc(MAX_ENTITY_SIZE, sizeof(char)); if (!text) { PyErr_NoMemory(); return -1; } i = 0; zeroes = 0; while (1) { this = Tokenizer_READ(self, 0); if (this == *";") { if (i == 0) FAIL_ROUTE_AND_EXIT() break; } if (i == 0 && this == *"0") { zeroes++; self->head++; continue; } if (i >= 8) FAIL_ROUTE_AND_EXIT() for (j = 0; j < NUM_MARKERS; j++) { if (this == *MARKERS[j]) FAIL_ROUTE_AND_EXIT() } j = 0; while (1) { if (!valid[j]) FAIL_ROUTE_AND_EXIT() if (this == valid[j]) break; j++; } text[i] = (char) this; self->head++; i++; } if (numeric) { sscanf(text, (hexadecimal ? "%x" : "%d"), &test); if (test < 1 || test > 0x10FFFF) FAIL_ROUTE_AND_EXIT() } else { i = 0; while (1) { def = entitydefs[i]; if (!def) // We've reached the end of the defs without finding it FAIL_ROUTE_AND_EXIT() if (strcmp(text, def) == 0) break; i++; } } if (zeroes) { buffer = calloc(strlen(text) + zeroes + 1, sizeof(char)); if (!buffer) { free(text); PyErr_NoMemory(); return -1; } for (i = 0; i < zeroes; i++) strcat(buffer, "0"); strcat(buffer, text); free(text); text = buffer; } textobj = PyUnicode_FromString(text); if (!textobj) { free(text); return -1; } free(text); kwargs = PyDict_New(); if (!kwargs) { Py_DECREF(textobj); return -1; } PyDict_SetItemString(kwargs, "text", textobj); Py_DECREF(textobj); if (Tokenizer_emit_kwargs(self, Text, kwargs)) return -1; if (Tokenizer_emit(self, HTMLEntityEnd)) return -1; return 0; } /* Parse an HTML entity at the head of the wikicode string. */ static int Tokenizer_parse_entity(Tokenizer* self) { Py_ssize_t reset = self->head; PyObject *tokenlist; if (Tokenizer_push(self, 0)) return -1; if (Tokenizer_really_parse_entity(self)) return -1; if (BAD_ROUTE) { RESET_ROUTE(); self->head = reset; if (Tokenizer_emit_char(self, *"&")) return -1; return 0; } tokenlist = Tokenizer_pop(self); if (!tokenlist) return -1; if (Tokenizer_emit_all(self, tokenlist)) { Py_DECREF(tokenlist); return -1; } Py_DECREF(tokenlist); return 0; } /* Parse an HTML comment at the head of the wikicode string. */ static int Tokenizer_parse_comment(Tokenizer* self) { Py_ssize_t reset = self->head + 3; PyObject *comment; Py_UNICODE this; self->head += 4; if (Tokenizer_push(self, 0)) return -1; while (1) { this = Tokenizer_READ(self, 0); if (this == *"") { comment = Tokenizer_pop(self); Py_XDECREF(comment); self->head = reset; return Tokenizer_emit_text(self, "