Skip to content

Commit d3ae37f

Browse files
TwoLaidTwoLaid
authored andcommitted
Merge pull request timogasda#4 from KenMacD/tokenize
Add tokenize to parser.
2 parents 07eb3a7 + 9d4b74f commit d3ae37f

2 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎Parser.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,90 @@ PyObject* Parser_check_syntax(PyObject* self, PyObject* args)
9595
return r;
9696
}
9797

98+
// Parser.tokenize(query) + return tokens
99+
PyObject* Parser_tokenize(PyObject* self, PyObject* args)
100+
{
101+
/* NB: the query passed in here is held by the parser. If further
102+
* processing will done it must be Py_INCREF()'d and stored in the
103+
* parser object so it can later be cleaned up.
104+
*/
105+
int rc;
106+
char *query;
107+
PyObject *token_tuple;
108+
PyObject *list;
109+
//PyObject *query_object;
110+
//PyObject *query_unicode;
111+
Parser *parser = (Parser *)self;
112+
char * token_str;
113+
struct gsp_sourcetoken *token;
114+
int i;
115+
int token_cnt;
116+
117+
query = NULL;
118+
119+
// get query from first argument
120+
if (args == NULL || args == Py_None) {
121+
PyErr_SetString(PyExc_TypeError, "tokenize() takes at least 1 argument");
122+
return NULL;
123+
}
124+
125+
rc = PyArg_ParseTuple(args, "s", &query);
126+
/*if (!rc || query == NULL) {
127+
rc = PyArg_ParseTuple(args, "u", &query);
128+
// printf("parse u: %d / %p\n", rc, query);
129+
}*/
130+
131+
if (!rc || query == NULL) {
132+
PyErr_SetString(PyExc_TypeError, "tokenize() takes exactly one string argument");
133+
return NULL;
134+
}
135+
136+
rc = gsp_tokenize(parser->_parser, query);
137+
138+
if (rc != 0) {
139+
PyErr_SetString(PyExc_BaseException, "tokenize() failed");
140+
return NULL;
141+
}
142+
143+
token_cnt = parser->_parser->number_of_token;
144+
list = PyList_New(token_cnt);
145+
if (!list) {
146+
PyErr_SetString(PyExc_BaseException,
147+
"tokenize() unable to create list");
148+
return NULL;
149+
}
150+
151+
for (i = 0; i < token_cnt; i++) {
152+
token = &parser->_parser->sourcetokenlist[i];
153+
154+
// TODO: any unicode handling?
155+
token_str = gsp_token_text(token);
156+
157+
if (!token_str) {
158+
PyErr_SetString(PyExc_BaseException, "tokenize() invalid token");
159+
Py_DECREF(list);
160+
return NULL;
161+
}
162+
163+
//printf("STR: %s\n", token_str);
164+
//gsp_print_token(token);
165+
166+
token_tuple = Py_BuildValue("(is)", token->nCode, token_str);
167+
free(token_str);
168+
169+
if (!token_tuple) {
170+
PyErr_SetString(PyExc_BaseException, "tokenize() tuple failed");
171+
Py_DECREF(list);
172+
return NULL;
173+
}
174+
175+
PyList_SET_ITEM(list, i, token_tuple);
176+
}
177+
178+
return list;
179+
}
180+
181+
98182
// get nth statement
99183
// Parser.get_statement(n)
100184
PyObject* Parser_get_statement(PyObject* self, PyObject* args)

‎Parser.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ void Parser_init_type(PyObject *m);
1818
void Parser_dealloc(Parser *self);
1919
PyObject *Parser_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2020
PyObject *Parser_check_syntax(PyObject* self, PyObject* args);
21+
PyObject *Parser_tokenize(PyObject* self, PyObject* args);
2122
PyObject *Parser_get_statement(PyObject* self, PyObject* args);
2223
PyObject *Parser_get_statement_count(PyObject* self, PyObject* args);
24+
PyObject *Parser_get_tokens(PyObject* self, PyObject *args);
2325
int Parser_init(Parser* self, PyObject* args, PyObject *kwds);
2426

2527
// Members/properties
@@ -31,9 +33,9 @@ static PyMemberDef Parser_members[] = {
3133
// Object methods
3234
static PyMethodDef Parser_methods[] = {
3335
{"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" },
34-
{"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" },
35-
{"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" },
36-
36+
{"tokenize", (PyCFunction)Parser_tokenize, METH_VARARGS, "tokenize(query)\nTokenizes the given *query*. Returns a list of (tokenCode, tokenValue) "},
37+
{"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" },
38+
{"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" },
3739
{NULL} /* Sentinel */
3840
};
3941

0 commit comments

Comments
 (0)