/* Call our flex scanner */ #include "Python.h" #include #define STAR_SCANNER #include "star_scanner.h" #if PY_MAJOR_VERSION >= 3 #define MOD_ERROR_VAL NULL #define MOD_SUCCESS_VAL(val) val #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void) #define MOD_DEF(ob, name, doc, methods) \ static struct PyModuleDef moduledef = { \ PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \ ob = PyModule_Create(&moduledef); #else #define MOD_ERROR_VAL #define MOD_SUCCESS_VAL(val) #define MOD_INIT(name) void init##name(void) #define MOD_DEF(ob, name, doc, methods) \ ob = Py_InitModule3(name, methods, doc); #endif static PyObject * get_input(PyObject * self, PyObject * args); static PyObject * flex_scan(PyObject * self,PyObject * args); static PyObject * get_token(PyObject * self,PyObject * args); static PyObject * drop_mem(PyObject * self,PyObject * args); static PyObject * get_last_ten(PyObject * self,PyObject * args); static PyMethodDef StarScanMethods[] = { {"prepare",get_input, METH_VARARGS,"Prepare scanner input"}, {"scan", flex_scan, METH_VARARGS, "Get next token"}, {"token",get_token, METH_VARARGS, "Return i'th token"}, {"last_ten",get_last_ten, METH_VARARGS, "Return last 10 tokens"}, {"cleanup", drop_mem, METH_VARARGS, "Free used memory"}, {NULL,NULL,0,NULL} }; //Module initialisation for Python 2 and 3 MOD_INIT(StarScan) { PyObject *m; MOD_DEF(m,"StarScan","A tokeniser for Star files", StarScanMethods) if(m==NULL) return MOD_ERROR_VAL; token_list = NULL; value_list = NULL; line_no_list = NULL; current_len = 0; alloc_mem = 0; #if PY_MAJOR_VERSION >= 3 return m; #endif } /* We need to read from the text string that the Python scanner uses, so we get a handle on the string and use that to feed flex. We allow a Unicode string that is UTF8 encoded. */ static PyObject * get_input(PyObject * self, PyObject * args) { PyObject * str_arg; /* A python string object in theory */ int i; if(!(PyArg_ParseTuple(args,"U",&str_arg))) return NULL; #if PY_MAJOR_VERSION >= 3 input_string = PyUnicode_AsUTF8(str_arg); #else input_string = PyString_AsString(str_arg); #endif string_pos = 0; in_string_len = strlen(input_string); star_clear(); for(i=0;ialloc_mem) { token_list = (int *) realloc(token_list,(alloc_mem+MEM_ALLOC_SIZE)*sizeof(int *)); line_no_list = (int *) realloc(line_no_list,(alloc_mem+MEM_ALLOC_SIZE)*sizeof(int *)); value_list = (PyObject **) realloc(value_list,(alloc_mem+MEM_ALLOC_SIZE)*sizeof(PyObject **)); alloc_mem += MEM_ALLOC_SIZE; /* printf("Expanded memory, val=%x,tok=%x\n",value_list,token_list);*/ } /* store latest values */ save_str = (char *) malloc((yyleng+1)*sizeof(char *)); /* printf("Got memory for string %s length %d at %x\n",yytext,yyleng+1,save_str);*/ strncpy(save_str,yytext,yyleng+1); value_list[current_len] = PyUnicode_FromString(save_str); /* new reference */ token_list[current_len] = tok_id; line_no_list[current_len] = yylineno; current_len++; /* return(Py_BuildValue("(iiss)",0,0,token_str,yytext));*/ return(Py_BuildValue("")); } static PyObject * get_token(PyObject * self, PyObject * args) { int list_pos; if(!(PyArg_ParseTuple(args,"i",&list_pos))) return NULL; /* printf("Getting token %d\n",list_pos);*/ if(list_pos==current_len) flex_scan(self,args); if(list_pos