Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,90 @@ PyObject* Parser_check_syntax(PyObject* self, PyObject* args)
return r;
}

// Parser.tokenize(query) + return tokens
PyObject* Parser_tokenize(PyObject* self, PyObject* args)
{
/* NB: the query passed in here is held by the parser. If further
* processing will done it must be Py_INCREF()'d and stored in the
* parser object so it can later be cleaned up.
*/
int rc;
char *query;
PyObject *token_tuple;
PyObject *list;
//PyObject *query_object;
//PyObject *query_unicode;
Parser *parser = (Parser *)self;
char * token_str;
struct gsp_sourcetoken *token;
int i;
int token_cnt;

query = NULL;

// get query from first argument
if (args == NULL || args == Py_None) {
PyErr_SetString(PyExc_TypeError, "tokenize() takes at least 1 argument");
return NULL;
}

rc = PyArg_ParseTuple(args, "s", &query);
/*if (!rc || query == NULL) {
rc = PyArg_ParseTuple(args, "u", &query);
// printf("parse u: %d / %p\n", rc, query);
}*/

if (!rc || query == NULL) {
PyErr_SetString(PyExc_TypeError, "tokenize() takes exactly one string argument");
return NULL;
}

rc = gsp_tokenize(parser->_parser, query);

if (rc != 0) {
PyErr_SetString(PyExc_BaseException, "tokenize() failed");
return NULL;
}

token_cnt = parser->_parser->number_of_token;
list = PyList_New(token_cnt);
if (!list) {
PyErr_SetString(PyExc_BaseException,
"tokenize() unable to create list");
return NULL;
}

for (i = 0; i < token_cnt; i++) {
token = &parser->_parser->sourcetokenlist[i];

// TODO: any unicode handling?
token_str = gsp_token_text(token);

if (!token_str) {
PyErr_SetString(PyExc_BaseException, "tokenize() invalid token");
Py_DECREF(list);
return NULL;
}

//printf("STR: %s\n", token_str);
//gsp_print_token(token);

token_tuple = Py_BuildValue("(is)", token->nCode, token_str);
free(token_str);

if (!token_tuple) {
PyErr_SetString(PyExc_BaseException, "tokenize() tuple failed");
Py_DECREF(list);
return NULL;
}

PyList_SET_ITEM(list, i, token_tuple);
}

return list;
}


// get nth statement
// Parser.get_statement(n)
PyObject* Parser_get_statement(PyObject* self, PyObject* args)
Expand Down Expand Up @@ -135,4 +219,4 @@ PyObject* Parser_get_statement_count(PyObject* self, PyObject* args)
n = PyLong_FromLong(parser->_parser->nStatement);

return n;
}
}
8 changes: 5 additions & 3 deletions Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ void Parser_init_type(PyObject *m);
void Parser_dealloc(Parser *self);
PyObject *Parser_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
PyObject *Parser_check_syntax(PyObject* self, PyObject* args);
PyObject *Parser_tokenize(PyObject* self, PyObject* args);
PyObject *Parser_get_statement(PyObject* self, PyObject* args);
PyObject *Parser_get_statement_count(PyObject* self, PyObject* args);
PyObject *Parser_get_tokens(PyObject* self, PyObject *args);
int Parser_init(Parser* self, PyObject* args, PyObject *kwds);

// Members/properties
Expand All @@ -31,9 +33,9 @@ static PyMemberDef Parser_members[] = {
// Object methods
static PyMethodDef Parser_methods[] = {
{"check_syntax", (PyCFunction)Parser_check_syntax, METH_VARARGS, "check_syntax(query)\nChecks syntax of the given *query*. Returns 0 if the query is valid.\n\n:type query: str\n:returns: int -- 0 for success" },
{"get_statement", (PyCFunction)Parser_get_statement, METH_VARARGS, "get_statement(n)\nAfter parsing a query string with :meth:`Parser.check_syntax` this function will return the *n*-th :class:`Statement` in that string.\n\n:type n: int\n:returns: Statement" },
{"get_statement_count", (PyCFunction)Parser_get_statement_count, METH_VARARGS, "get_statement_count()\nReturns the number of statements for the Parser object\n:returns: int" },

{"tokenize", (PyCFunction)Parser_tokenize, METH_VARARGS, "tokenize(query)\nTokenizes the given *query*. Returns a list of (tokenCode, tokenValue) "},
{"get_statement", (PyCFunction)Parser_get_statement, METH_VARARGS, "get_statement(n)\nAfter parsing a query string with :meth:`Parser.check_syntax` this function will return the *n*-th :class:`Statement` in that string.\n\n:type n: int\n:returns: Statement" },
{"get_statement_count", (PyCFunction)Parser_get_statement_count, METH_VARARGS, "get_statement_count()\nReturns the number of statements for the Parser object\n:returns: int" },
{NULL} /* Sentinel */
};

Expand Down