From ebf99d722c68d0a9e91f312fefcbce856519d159 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 14 Aug 2013 19:01:12 -0400 Subject: [PATCH] Combine emit()/emit_first() internally. --- mwparserfromhell/parser/tokenizer.c | 60 +++++-------------------------------- mwparserfromhell/parser/tokenizer.h | 7 ++++- 2 files changed, 13 insertions(+), 54 deletions(-) diff --git a/mwparserfromhell/parser/tokenizer.c b/mwparserfromhell/parser/tokenizer.c index cf11462..4df61d8 100644 --- a/mwparserfromhell/parser/tokenizer.c +++ b/mwparserfromhell/parser/tokenizer.c @@ -347,7 +347,7 @@ static void* Tokenizer_fail_route(Tokenizer* self) /* Write a token to the end of the current token stack. */ -static int Tokenizer_emit(Tokenizer* self, PyObject* token) +static int Tokenizer_emit_token(Tokenizer* self, PyObject* token, int first) { PyObject* instance; @@ -356,7 +356,8 @@ static int Tokenizer_emit(Tokenizer* self, PyObject* token) instance = PyObject_CallObject(token, NULL); if (!instance) return -1; - if (PyList_Append(self->topstack->stack, instance)) { + if (first ? PyList_Insert(self->topstack->stack, 0, instance) : + PyList_Append(self->topstack->stack, instance)) { Py_DECREF(instance); return -1; } @@ -367,8 +368,8 @@ static int Tokenizer_emit(Tokenizer* self, PyObject* token) /* Write a token to the end of the current token stack. */ -static int Tokenizer_emit_kwargs(Tokenizer* self, PyObject* token, - PyObject* kwargs) +static int Tokenizer_emit_token_kwargs(Tokenizer* self, PyObject* token, + PyObject* kwargs, int first) { PyObject* instance; @@ -381,55 +382,8 @@ static int Tokenizer_emit_kwargs(Tokenizer* self, PyObject* token, Py_DECREF(kwargs); return -1; } - if (PyList_Append(self->topstack->stack, instance)) { - Py_DECREF(instance); - Py_DECREF(kwargs); - return -1; - } - Py_DECREF(instance); - Py_DECREF(kwargs); - return 0; -} - -/* - Write a token to the beginning of the current token stack. -*/ -static int Tokenizer_emit_first(Tokenizer* self, PyObject* token) -{ - PyObject* instance; - - if (Tokenizer_push_textbuffer(self)) - return -1; - instance = PyObject_CallObject(token, NULL); - if (!instance) - return -1; - if (PyList_Insert(self->topstack->stack, 0, instance)) { - Py_DECREF(instance); - return -1; - } - Py_DECREF(instance); - return 0; -} - -/* - Write a token to the beginning of the current token stack, with kwargs. - Steals a reference to kwargs. -*/ -static int Tokenizer_emit_first_kwargs(Tokenizer* self, PyObject* token, - PyObject* kwargs) -{ - 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 (PyList_Insert(self->topstack->stack, 0, instance)) { + if (first ? PyList_Insert(self->topstack->stack, 0, instance): + PyList_Append(self->topstack->stack, instance)) { Py_DECREF(instance); Py_DECREF(kwargs); return -1; diff --git a/mwparserfromhell/parser/tokenizer.h b/mwparserfromhell/parser/tokenizer.h index 1ac9168..d5f755d 100644 --- a/mwparserfromhell/parser/tokenizer.h +++ b/mwparserfromhell/parser/tokenizer.h @@ -214,13 +214,18 @@ typedef struct { } Tokenizer; -/* Macros for accessing Tokenizer data: */ +/* Macros related to Tokenizer functions: */ #define Tokenizer_READ(self, delta) (*PyUnicode_AS_UNICODE(Tokenizer_read(self, delta))) #define Tokenizer_READ_BACKWARDS(self, delta) \ (*PyUnicode_AS_UNICODE(Tokenizer_read_backwards(self, delta))) #define Tokenizer_CAN_RECURSE(self) (self->depth < MAX_DEPTH && self->cycles < MAX_CYCLES) +#define Tokenizer_emit(self, token) Tokenizer_emit_token(self, token, 0) +#define Tokenizer_emit_first(self, token) Tokenizer_emit_token(self, token, 1) +#define Tokenizer_emit_kwargs(self, token, kwargs) Tokenizer_emit_token_kwargs(self, token, kwargs, 0) +#define Tokenizer_emit_first_kwargs(self, token, kwargs) Tokenizer_emit_token_kwargs(self, token, kwargs, 1) + /* Macros for accessing HTML tag definitions: */