Browse Source

Combine emit()/emit_first() internally.

tags/v0.3
Ben Kurtovic 11 years ago
parent
commit
ebf99d722c
2 changed files with 13 additions and 54 deletions
  1. +7
    -53
      mwparserfromhell/parser/tokenizer.c
  2. +6
    -1
      mwparserfromhell/parser/tokenizer.h

+ 7
- 53
mwparserfromhell/parser/tokenizer.c View File

@@ -347,7 +347,7 @@ static void* Tokenizer_fail_route(Tokenizer* self)
/* /*
Write a token to the end of the current token stack. 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; PyObject* instance;


@@ -356,7 +356,8 @@ static int Tokenizer_emit(Tokenizer* self, PyObject* token)
instance = PyObject_CallObject(token, NULL); instance = PyObject_CallObject(token, NULL);
if (!instance) if (!instance)
return -1; 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); Py_DECREF(instance);
return -1; 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. 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; PyObject* instance;


@@ -381,55 +382,8 @@ static int Tokenizer_emit_kwargs(Tokenizer* self, PyObject* token,
Py_DECREF(kwargs); Py_DECREF(kwargs);
return -1; 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(instance);
Py_DECREF(kwargs); Py_DECREF(kwargs);
return -1; return -1;


+ 6
- 1
mwparserfromhell/parser/tokenizer.h View File

@@ -214,13 +214,18 @@ typedef struct {
} Tokenizer; } 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(self, delta) (*PyUnicode_AS_UNICODE(Tokenizer_read(self, delta)))
#define Tokenizer_READ_BACKWARDS(self, delta) \ #define Tokenizer_READ_BACKWARDS(self, delta) \
(*PyUnicode_AS_UNICODE(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_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: */ /* Macros for accessing HTML tag definitions: */




Loading…
Cancel
Save