Added reset_flex_buffer hook to support flushing flex's internal buffer.

parent 91979192
...@@ -6,12 +6,23 @@ ...@@ -6,12 +6,23 @@
#include <stdio.h> #include <stdio.h>
#include <dlfcn.h> #include <dlfcn.h>
void (*reset_flex_buffer)(void) = NULL;
void *bisondynlib_open(char *filename) void *bisondynlib_open(char *filename)
{ {
void *handle; void *handle;
dlerror();
handle = dlopen(filename, (RTLD_NOW|RTLD_GLOBAL)); handle = dlopen(filename, (RTLD_NOW|RTLD_GLOBAL));
dlerror();
if (!handle)
return NULL;
reset_flex_buffer = dlsym(handle, "reset_flex_buffer");
dlerror();
return handle; return handle;
} }
...@@ -20,6 +31,12 @@ int bisondynlib_close(void *handle) ...@@ -20,6 +31,12 @@ int bisondynlib_close(void *handle)
return dlclose(handle); return dlclose(handle);
} }
void bisondynlib_reset(void)
{
if (reset_flex_buffer)
reset_flex_buffer();
}
char *bisondynlib_err() char *bisondynlib_err()
{ {
return dlerror(); return dlerror();
...@@ -28,16 +45,19 @@ char *bisondynlib_err() ...@@ -28,16 +45,19 @@ char *bisondynlib_err()
char *bisondynlib_lookup_hash(void *handle) char *bisondynlib_lookup_hash(void *handle)
{ {
char **hash; char **hash;
dlerror();
hash = dlsym(handle, "rules_hash"); hash = dlsym(handle, "rules_hash");
/*
printf("bisondynlib_lookup_hash: hash=%s\n", *hash); dlerror();
*/
return *hash; return hash ? *hash : NULL;
} }
PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug) PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug)
{ {
if(!handle)
return NULL;
PyObject *(*pparser)(PyObject *, void *, void *, int); PyObject *(*pparser)(PyObject *, void *, void *, int);
pparser = bisondynlib_lookup_parser(handle); pparser = bisondynlib_lookup_parser(handle);
...@@ -60,12 +80,17 @@ PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, in ...@@ -60,12 +80,17 @@ PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, in
} }
/* /*
* function(void *) returns a pointer to a function(PyObject *, char *) returning PyObject* * function(void *) returns a pointer to a function(PyObject *, char *)
* returning PyObject*
*/ */
PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *, int) PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *, int)
{ {
PyObject *(*do_parse)(PyObject *, void *, void *, int) = dlsym(handle,
"do_parse");
dlerror(); dlerror();
return dlsym(handle, "do_parse");
return do_parse;
} }
/* /*
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
void *bisondynlib_open(char *filename); void *bisondynlib_open(char *filename);
int bisondynlib_close(void *handle); int bisondynlib_close(void *handle);
void bisondynlib_reset(void);
char *bisondynlib_err(void); char *bisondynlib_err(void);
PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *, int); PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *, int);
...@@ -14,7 +15,6 @@ PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *, ...@@ -14,7 +15,6 @@ PyObject *(*bisondynlib_lookup_parser(void *handle))(PyObject *, void *, void *,
char *bisondynlib_lookup_hash(void *handle); char *bisondynlib_lookup_hash(void *handle);
PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug); PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug);
/* /*
int bisondynlib_build(char *libName, char *pyincdir); int bisondynlib_build(char *libName, char *pyincdir);
*/ */
...@@ -53,6 +53,7 @@ cdef extern from "../c/bison_callback.h": ...@@ -53,6 +53,7 @@ cdef extern from "../c/bison_callback.h":
cdef extern from "../c/bisondynlib.h": cdef extern from "../c/bisondynlib.h":
void *bisondynlib_open(char *filename) void *bisondynlib_open(char *filename)
int bisondynlib_close(void *handle) int bisondynlib_close(void *handle)
void bisondynlib_reset()
char *bisondynlib_err() char *bisondynlib_err()
object (*bisondynlib_lookup_parser(void *handle))(object, char *) object (*bisondynlib_lookup_parser(void *handle))(object, char *)
char *bisondynlib_lookup_hash(void *handle) char *bisondynlib_lookup_hash(void *handle)
...@@ -121,6 +122,12 @@ cdef class ParserEngine: ...@@ -121,6 +122,12 @@ cdef class ParserEngine:
self.openCurrentLib() self.openCurrentLib()
def reset(self):
"""
Reset Flex's buffer and state.
"""
bisondynlib_reset()
def openCurrentLib(self): def openCurrentLib(self):
""" """
Tests if library exists and is current. If not, builds a fresh one. Tests if library exists and is current. If not, builds a fresh one.
......
...@@ -200,6 +200,9 @@ class BisonParser(object): ...@@ -200,6 +200,9 @@ class BisonParser(object):
def handle_timeout(self, signum, frame): def handle_timeout(self, signum, frame):
raise TimeoutError('Computation exceeded timeout limit.') raise TimeoutError('Computation exceeded timeout limit.')
def reset(self):
self.engine.reset()
def run(self, **kw): def run(self, **kw):
""" """
Runs the parser, and returns the top-most parse target. Runs the parser, and returns the top-most parse target.
...@@ -247,6 +250,7 @@ class BisonParser(object): ...@@ -247,6 +250,7 @@ class BisonParser(object):
while not self.file.closed: while not self.file.closed:
# do the parsing job, spew if error # do the parsing job, spew if error
self.last = None self.last = None
self.engine.reset()
try: try:
self.engine.runEngine(debug) self.engine.runEngine(debug)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment