[Mumps2Py:] [177] responsibility for loading a routine from disk has been moved from mumps2tok .py to mumps_module.py.

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 177
Author:   pgallot
Date:     2008-03-16 14:27:41 +0000 (Sun, 16 Mar 2008)

Log Message:
-----------
responsibility for loading a routine from disk has been moved from mumps2tok.py to mumps_module.py.

Modified Paths:
--------------
    trunk/mumps2py/mumps2tok.py
    trunk/mumps2py/mumps_module.py


Modified: trunk/mumps2py/mumps2tok.py
===================================================================
--- trunk/mumps2py/mumps2tok.py	2008-03-13 16:24:05 UTC (rev 176)
+++ trunk/mumps2py/mumps2tok.py	2008-03-16 14:27:41 UTC (rev 177)
@@ -16,7 +16,7 @@
 ##    along with Mumps2Py.  If not, see <http://www.gnu.org/licenses/>.
 """ extract tokens from the Mumps source code """
 
-import re, fileinput
+import re
 from tokens import *
 
 class ParseError(Exception):
@@ -1023,7 +1023,7 @@
 
     return token_list
 
-def parse_routine(routine, open_fileinput= None):
+def parse_routine(routine, open_fileinput = None):
     """ Parse all the code of a given Mumps module."""
     routine.empty_tokenlist()
     
@@ -1033,45 +1033,33 @@
         (MUMPS_RE_DICT["emptyline"], parse_emptyline),
         (MUMPS_RE_DICT["cmd"], parse_command)]
 
-    if open_fileinput:
-        input_file = open_fileinput
-    else:
-        input_file = fileinput.input(routine.input_file)
+    if routine.linebuffer_isempty():
+        routine.load(open_fileinput)
+        
+    for offset, line in enumerate(routine.loaded()):
+        lineno = routine.start + offset
+        #if __debug__: print "%d :%s" % (lineno, line),
+        pattern_match = False
+        pos = 0
+        for pattern, parser in pattern_list:
+            if pattern.match(line):
+                pattern_match = True
+                try:
+                    token = parser(line)
+                    token.line_no(lineno)
+                    routine.add_token(token)
+                    pos = token.end
 
-    try:
-        for line in input_file:
-            lineno = fileinput.lineno()
-            if lineno < routine.start:
-                continue
+                    toks = parse_line_of_commands(line, pos, lineno)
+                    routine.add_tokens(toks)
+                    break
+                except ParseError, err:
+                    err.line_no(lineno)
+                    raise err
 
-            if routine.end != -1 and lineno > routine.end:
-                break
+        if not pattern_match:
+            raise ParseError(line, "no Pattern match", pos, lineno)
 
-            #if __debug__: print "%d :%s" % (fileinput.lineno(), line),
-            pattern_match = False
-            pos = 0
-            for pattern, parser in pattern_list:
-                if pattern.match(line):
-                    pattern_match = True
-                    try:
-                        token = parser(line)
-                        token.line_no(lineno)
-                        routine.add_token(token)
-                        pos = token.end
-
-                        toks = parse_line_of_commands(line, pos, lineno)
-                        routine.add_tokens(toks)
-                        break
-                    except ParseError, err:
-                        err.line_no(lineno)
-                        raise err
-
-            if not pattern_match:
-                raise ParseError(line, "no Pattern match", pos, lineno)
-    finally:
-        if not open_fileinput:
-            fileinput.close()
-
 if __name__ == '__main__':
     from mumps_module import parse_for_routines
 
@@ -1090,6 +1078,6 @@
         for the_module in mods:
             print the_module.mod_name
             parse_routine(the_module)
-            the_module.empty_tokenlist() # keep memory usage down
+            the_module.empty() # keep memory usage down
     except ParseError, err0:
         print err0.error_msg()

Modified: trunk/mumps2py/mumps_module.py
===================================================================
--- trunk/mumps2py/mumps_module.py	2008-03-13 16:24:05 UTC (rev 176)
+++ trunk/mumps2py/mumps_module.py	2008-03-16 14:27:41 UTC (rev 177)
@@ -31,6 +31,7 @@
         self.start = startline
         self.end = endline
         self.token_list = []
+        self.line_buffer = []
 
     def __str__(self):
         return self.mod_name
@@ -39,11 +40,28 @@
         """ returns true if the Token List is empty."""
         return not len(self.token_list)
 
+    def linebuffer_isempty(self):
+        """ returns true if the line buffer is empty."""
+        return not len(self.line_buffer)
+
     def empty_tokenlist(self):
         """ clear out the Token List."""
         while len(self.token_list):
             self.token_list.pop()
+
+    def empty_linebuffer(self):
+        """clear out the line buffer."""
+        while len(self.line_buffer):
+            self.line_buffer.pop()
+
+    def empty(self):
+        """clear out the Token List and line buffer"""
+        while len(self.token_list):
+            self.token_list.pop()
+        while len(self.line_buffer):
+            self.line_buffer.pop()
         
+        
     def add_token(self, new_token):
         """ add a new token to the token list."""
         self.token_list.append(new_token)
@@ -56,6 +74,34 @@
         """return the token list"""
         return self.token_list
 
+    def loaded(self):
+        """return the line buffer"""
+        return self.line_buffer
+
+    def load(self, open_fileinput = None):
+        """loads the routine's line buffer from a file."""
+        if open_fileinput:
+            input_file = open_fileinput
+        else:
+            input_file = fileinput.input(self.input_file)
+
+        self.line_buffer = []
+        try:
+            for line in input_file:
+                lineno = fileinput.lineno()
+                if lineno < self.start:
+                    continue
+
+                if self.end != -1 and lineno > self.end:
+                    break
+
+                self.line_buffer.append(line)
+        finally:
+            if not open_fileinput:
+                fileinput.close()
+
+
+
 class RoutineFilter:
     """creates an object that will filter which routines to process,
 based on the criteria provided"""


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/