Explorar o código

Fixed compilation warning and removed useless LEO comments.

Sander Mathijs van Veen %!s(int64=14) %!d(string=hai) anos
pai
achega
858c8461cb
Modificáronse 4 ficheiros con 36 adicións e 56 borrados
  1. 3 8
      src/c/bisondynlib-linux.c
  2. 1 6
      src/c/bisondynlib.h
  3. 13 4
      src/pyrex/bison_.pyx
  4. 19 38
      src/python/bison.py

+ 3 - 8
src/c/bisondynlib-linux.c

@@ -1,12 +1,9 @@
-//@+leo-ver=4
-//@+node:@file src/c/bisondynlib-linux.c
-//@@language c
 /*
  * Linux-specific dynamic library manipulation routines
  */
 
-#include <stdio.h>
 #include "bisondynlib.h"
+#include <stdio.h>
 #include <dlfcn.h>
 
 void *bisondynlib_open(char *filename)
@@ -41,7 +38,7 @@ char *bisondynlib_lookup_hash(void *handle)
 
 PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, int debug)
 {
-    void (*pparser)(PyObject *, void *, void *, int);
+    PyObject *(*pparser)(PyObject *, void *, void *, int);
     //PyObject *result;
 
     //printf("bisondynlib_run: looking up parser\n");
@@ -54,6 +51,7 @@ PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, in
     }
 
     (*pparser)(parser, cb, in, debug);
+
     //printf("bisondynlib_run: back from parser\n");
     //return result;
     Py_INCREF(Py_None);
@@ -83,6 +81,3 @@ int bisondynlib_build(char *libName, char *pyincdir)
     return 0;
 }
 */
-
-//@-node:@file src/c/bisondynlib-linux.c
-//@-leo

+ 1 - 6
src/c/bisondynlib.h

@@ -1,12 +1,9 @@
-//@+leo-ver=4
-//@+node:@file src/c/bisondynlib.h
-//@@language c
 /*
  * common interface to dynamic library routines
  */
 
-#include <stdio.h>
 #include "Python.h"
+#include <stdio.h>
 
 void *bisondynlib_open(char *filename);
 int bisondynlib_close(void *handle);
@@ -21,5 +18,3 @@ PyObject *bisondynlib_run(void *handle, PyObject *parser, void *cb, void *in, in
 /*
 int bisondynlib_build(char *libName, char *pyincdir);
 */
-//@-node:@file src/c/bisondynlib.h
-//@-leo

+ 13 - 4
src/pyrex/bison_.pyx

@@ -60,7 +60,7 @@ cdef extern from "stdarg.h":
         pass
     ctypedef struct fake_type:
         pass
-    void va_start(va_list, void* arg)
+    void va_start(va_list, int arg)
     void* va_arg(va_list, fake_type)
     void va_end(va_list)
     fake_type void_type "void *"
@@ -69,12 +69,14 @@ cdef extern from "stdarg.h":
 # Callback function which is invoked by target handlers
 # within the C yyparse() function.
 
+import signal
+
 cdef public object py_callback(object parser, char *target, int option, \
         int nargs, ...):
 
     cdef int i
     cdef va_list ap
-    va_start(ap, <void*>nargs)
+    va_start(ap, <int>nargs)
 
     cdef void *objptr
     cdef object obj
@@ -111,8 +113,15 @@ cdef public object py_callback(object parser, char *target, int option, \
         #    print 'py_callback: calling handler:', \
         #          (target, option, names, values)
 
+
+        # Set the signal handler and a timeout alarm
+        signal.signal(signal.SIGALRM, parser.handle_timeout)
+        signal.alarm(parser.timeout)
+
         res = parser._handle(target, option, names, values)
 
+        signal.alarm(0)
+
         #if parser.verbose:
         #    print 'py_callback: handler returned:', res
     except:
@@ -176,7 +185,7 @@ cdef class ParserEngine:
     # rules hash str embedded in bison parser lib
     cdef char *libHash
 
-    def __init__(self, parser, **kw):
+    def __init__(self, parser):
         """
         Creates a ParserEngine wrapper, and builds/loads the library.
 
@@ -319,8 +328,8 @@ cdef class ParserEngine:
         write("\n".join([
             "%{",
             '',
-            "#include <stdio.h>",
             '#include "Python.h"',
+            "#include <stdio.h>",
             "extern FILE *yyin;",
             "extern int yylineno;"
             "extern char *yytext;",

+ 19 - 38
src/python/bison.py

@@ -31,12 +31,15 @@ from bison_ import ParserEngine, unquoted
 #@+node:globals
 reSpaces = re.compile("\\s+")
 
-#@-node:globals
-#@+node:exceptions
+
 class ParserSyntaxError(Exception):
     pass
-#@-node:exceptions
-#@+node:class BisonError
+
+
+class TimeoutError(Exception):
+    pass
+
+
 class BisonError:
     """
     Flags an error to yyparse()
@@ -47,8 +50,8 @@ class BisonError:
 
     def __init__(self, value="syntax error"):
         self.value = value
-#@-node:class BisonError
-#@+node:class BisonNode
+
+
 class BisonNode:
     """
     Generic class for wrapping parse targets.
@@ -63,8 +66,7 @@ class BisonNode:
         - any keywords you want (except 'items'), with any type of value.
           keywords will be stored as attributes in the constructed object.
     """
-    #@    @+others
-    #@+node:__init__
+
     def __init__(self, **kw):
 
         self.__dict__.update(kw)
@@ -78,18 +80,12 @@ class BisonNode:
         # mirror this dict to simplify dumping
         self.kw = kw
 
-    #@-node:__init__
-    #@+node:__str__
     def __str__(self):
         return "<BisonNode:%s>" % self.target
 
-    #@-node:__str__
-    #@+node:__repr__
     def __repr__(self):
         return str(self)
 
-    #@-node:__repr__
-    #@+node:__getitem__
     def __getitem__(self, item):
         """
         Retrieves the ith value from this node, or child nodes
@@ -110,21 +106,17 @@ class BisonNode:
             return self.values[item[0]][item[1:]]
         else:
             raise TypeError("Can only index %s objects with an int or a list/tuple" % self.__class.__name__)
-    #@-node:__getitem__
-    #@+node:__len__
+
     def __len__(self):
 
         return len(self.values)
-    #@-node:__len__
-    #@+node:__getslice__
+
     def __getslice__(self, fromidx, toidx):
         return self.values[fromidx:toidx]
-    #@-node:__getslice__
-    #@+node:__iter__
+
     def __iter__(self):
         return iter(self.values)
-    #@-node:__iter__
-    #@+node:dump
+
     def dump(self, indent=0):
         """
         For debugging - prints a recursive dump of a parse tree node and its children
@@ -142,8 +134,6 @@ class BisonNode:
             else:
                 print indents + "  %s=%s" % (name, val)
 
-    #@-node:dump
-    #@+node:toxml
     def toxml(self):
         """
         Returns an xml serialisation of this node and its children, as a raw string
@@ -153,8 +143,6 @@ class BisonNode:
         """
         return self.toxmldoc().toxml()
 
-    #@-node:toxml
-    #@+node:toprettyxml
     def toprettyxml(self, indent='  ', newl='\n', encoding=None):
         """
         returns a human-readable xml serialisation of this node and its children
@@ -163,8 +151,6 @@ class BisonNode:
                                            newl=newl,
                                            encoding=encoding)
 
-    #@-node:toprettyxml
-    #@+node:toxmldoc
     def toxmldoc(self):
         """
         Returns the node and its children as an xml.dom.minidom.Document object
@@ -173,8 +159,6 @@ class BisonNode:
         d.appendChild(self.toxmlelem(d))
         return d
 
-    #@-node:toxmldoc
-    #@+node:toxmlelem
     def toxmlelem(self, docobj):
         """
         Returns a DOM Element object of this node and its children
@@ -209,10 +193,6 @@ class BisonNode:
         return x
 
 
-    #@-node:toxmlelem
-    #@-others
-#@-node:class BisonNode
-#@+node:class BisonParser
 class BisonParser(object):
     """
     Base parser class
@@ -221,8 +201,6 @@ class BisonParser(object):
     'on_TargetName', where 'TargetName' is the name of each target in
     your grammar (.y) file.
     """
-    #@    @+others
-    #@+node:attributes
     # ---------------------------------------
     # override these if you need to
 
@@ -250,6 +228,8 @@ class BisonParser(object):
 
     verbose = 0
 
+    timeout = 1  # Timeout in seconds after which a computation is terminated.
+
     file = None # default to sys.stdin
 
     last = None # last parsed target, top of parse tree
@@ -262,8 +242,6 @@ class BisonParser(object):
 
     defaultNodeClass = BisonNode # class to use by default for creating new parse nodes
 
-    #@-node:attributes
-    #@+node:__init__
     def __init__(self, **kw):
         """
         Abstract representation of parser
@@ -351,6 +329,9 @@ class BisonParser(object):
         # assumedly the last thing parsed is at the top of the tree
         return self.last
 
+    def handle_timeout(self, signum, frame):
+        raise TimeoutError('Computation exceeded timeout limit.')
+
     def run(self, **kw):
         """
         Runs the parser, and returns the top-most parse target.