Debian Codegraph @

src/lib/py_star_scan.c

raw symbols SCIP
1 /* Call our flex scanner */
2 #include "Python.h"
3 #include <string.h>
4
5 #define STAR_SCANNER
6 #include "star_scanner.h"
7
8 #if PY_MAJOR_VERSION >= 3
9 #define MOD_ERROR_VAL NULL
10 #define MOD_SUCCESS_VAL(val) val
11 #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
12 #define MOD_DEF(ob, name, doc, methods) \
13 static struct PyModuleDef moduledef = { \
14 PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
15 ob = PyModule_Create(&moduledef);
16 #else
17 #define MOD_ERROR_VAL
18 #define MOD_SUCCESS_VAL(val)
19 #define MOD_INIT(name) void init##name(void)
20 #define MOD_DEF(ob, name, doc, methods) \
21 ob = Py_InitModule3(name, methods, doc);
22 #endif
23
24 static PyObject * get_input(PyObject * self, PyObject * args);
25 static PyObject * flex_scan(PyObject * self,PyObject * args);
26 static PyObject * get_token(PyObject * self,PyObject * args);
27 static PyObject * drop_mem(PyObject * self,PyObject * args);
28 static PyObject * get_last_ten(PyObject * self,PyObject * args);
29
30 static PyMethodDef StarScanMethods[] = {
31 {"prepare",get_input, METH_VARARGS,"Prepare scanner input"},
32 {"scan", flex_scan, METH_VARARGS, "Get next token"},
33 {"token",get_token, METH_VARARGS, "Return i'th token"},
34 {"last_ten",get_last_ten, METH_VARARGS, "Return last 10 tokens"},
35 {"cleanup", drop_mem, METH_VARARGS, "Free used memory"},
36 {NULL,NULL,0,NULL}
37 };
38
39 //Module initialisation for Python 2 and 3
40 MOD_INIT(StarScan)
41 {
42 PyObject *m;
43 MOD_DEF(m,"StarScan","A tokeniser for Star files",
44 StarScanMethods)
45 if(m==NULL)
46 return MOD_ERROR_VAL;
47 token_list = NULL;
48 value_list = NULL;
49 line_no_list = NULL;
50 current_len = 0;
51 alloc_mem = 0;
52 #if PY_MAJOR_VERSION >= 3
53 return m;
54 #endif
55 }
56
57 /* We need to read from the text string that the Python
58 scanner uses, so we get a handle on the string and use
59 that to feed flex. We allow a Unicode string that is
60 UTF8 encoded. */
61
62 static PyObject *
63 get_input(PyObject * self, PyObject * args)
64 {
65 PyObject * str_arg; /* A python string object in theory */
66 int i;
67 if(!(PyArg_ParseTuple(args,"U",&str_arg))) return NULL;
68 #if PY_MAJOR_VERSION >= 3
69 input_string = PyUnicode_AsUTF8(str_arg);
70 #else
71 input_string = PyString_AsString(str_arg);
72 #endif
73 string_pos = 0;
74 in_string_len = strlen(input_string);
75 star_clear();
76 for(i=0;i<current_len;i++){
77 /* printf("New input, freeing value %d\n",i); */
78 Py_XDECREF(value_list[i]);
79 }
80 if(token_list!=NULL) {
81 /* printf("Freeing token_list\n"); */
82 free(token_list);}
83 if(value_list!=NULL) {
84 /* printf("Freeing value_list\n"); */
85 free(value_list);}
86 if(line_no_list!=NULL) {
87 free(line_no_list);}
88 alloc_mem = 0;
89 /* Now get our first block of storage */
90 token_list = (int *) malloc(MEM_ALLOC_SIZE*sizeof(int *));
91 value_list = (PyObject **) malloc(MEM_ALLOC_SIZE*sizeof(PyObject **));
92 line_no_list = (int *) malloc(MEM_ALLOC_SIZE*sizeof(int *));
93 alloc_mem += MEM_ALLOC_SIZE;
94 current_len = 0;
95 /* printf("New memory, val=%x,tok=%x\n",value_list,token_list);*/
96 return(Py_BuildValue(""));
97 }
98
99 /* Call our flex scanner and store the resulting token
100 in our internal list */
101
102 static PyObject *
103 flex_scan(PyObject * self, PyObject * args)
104 {
105 char * token_str; /* String token */
106 char * save_str; /* pointer to memory location */
107 size_t tok_len; /* token length (no zero byte) */
108 int tok_id; /* token id */
109 static char end_str[] = "END";
110 tok_id = star_scanner();
111 /* printf("Got token id %d i.e. %s\n",tok_id,tokens[tok_id]);*/
112 tok_len = yyleng;
113 if(tok_id == DEND) {
114 /* printf("End seen\n");*/
115 }
116 /* Get memory for storing token and value */
117 if(current_len+1>alloc_mem) {
118 token_list = (int *) realloc(token_list,(alloc_mem+MEM_ALLOC_SIZE)*sizeof(int *));
119 line_no_list = (int *) realloc(line_no_list,(alloc_mem+MEM_ALLOC_SIZE)*sizeof(int *));
120 value_list = (PyObject **) realloc(value_list,(alloc_mem+MEM_ALLOC_SIZE)*sizeof(PyObject **));
121 alloc_mem += MEM_ALLOC_SIZE;
122 /* printf("Expanded memory, val=%x,tok=%x\n",value_list,token_list);*/
123 }
124 /* store latest values */
125 save_str = (char *) malloc((yyleng+1)*sizeof(char *));
126 /* printf("Got memory for string %s length %d at %x\n",yytext,yyleng+1,save_str);*/
127 strncpy(save_str,yytext,yyleng+1);
128 value_list[current_len] = PyUnicode_FromString(save_str); /* new reference */
129 token_list[current_len] = tok_id;
130 line_no_list[current_len] = yylineno;
131 current_len++;
132 /* return(Py_BuildValue("(iiss)",0,0,token_str,yytext));*/
133 return(Py_BuildValue(""));
134 }
135
136 static PyObject *
137 get_token(PyObject * self, PyObject * args)
138 {
139 int list_pos;
140 if(!(PyArg_ParseTuple(args,"i",&list_pos))) return NULL;
141 /* printf("Getting token %d\n",list_pos);*/
142 if(list_pos==current_len) flex_scan(self,args);
143 if(list_pos<current_len)
144 return(Py_BuildValue("(iisO)",line_no_list[list_pos],0,tokens[token_list[list_pos]],value_list[list_pos]));
145 else {
146 PyErr_SetString(PyExc_IndexError,"No tokens left");
147 return NULL;
148 }
149 }
150
151 void
152 clear_mem(void)
153 {
154 int i;
155 for(i=0;i<current_len;i++){
156 printf("Clearing token %d\n",i);
157 Py_XDECREF(value_list[i]);
158 }
159 free(token_list);
160 free(value_list);
161 free(line_no_list);
162 current_len = 0;
163 alloc_mem = 0;
164 }
165
166 static PyObject *
167 drop_mem(PyObject * self, PyObject * args)
168 {
169 clear_mem();
170 return NULL;
171 }
172
173 /* For error reporting we need to supply the last 10 tokens */
174
175 static PyObject *
176 get_last_ten(PyObject * self, PyObject * args)
177 {
178 int start_pt;
179 int ret_list_pos,pos_ptr;
180 PyObject * newlist;
181 PyObject * newtuple; /* store each tuple */
182 if(current_len<=10) start_pt = 0;
183 else start_pt = current_len - 10;
184 newlist = PyList_New(current_len - start_pt);
185 for(ret_list_pos=0;start_pt+ret_list_pos<current_len;ret_list_pos++){
186 pos_ptr = start_pt+ret_list_pos;
187 /* printf("Build token %d\n",pos_ptr);*/
188 newtuple = Py_BuildValue("iisO",line_no_list[pos_ptr],0,tokens[token_list[pos_ptr]],
189 value_list[pos_ptr]);
190 /* printf("Set list pos %d\n",ret_list_pos);*/
191 PyList_SET_ITEM(newlist,ret_list_pos,newtuple);
192 }
193 return newlist;
194 }