[Mumps2Py:] [80] tried to generalize/clean-up argument indirection, entryref handling, and parameter list handling.

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


Revision: 80
Author:   pgallot
Date:     2008-02-04 05:16:42 +0000 (Mon, 04 Feb 2008)

Log Message:
-----------
tried to generalize/clean-up argument indirection, entryref handling, and parameter list handling.

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


Modified: trunk/mumps2py/mumps2tok.py
===================================================================
--- trunk/mumps2py/mumps2tok.py	2008-02-04 05:09:23 UTC (rev 79)
+++ trunk/mumps2py/mumps2tok.py	2008-02-04 05:16:42 UTC (rev 80)
@@ -64,7 +64,23 @@
     "cmdEnd":re.compile(r"[ ]|\s*$")
     }
 
-def parse_entry_ref(mumps_module, line, pos):
+def consume_actuallist(mumps_module, line, pos):
+    """ parses a list of parameters """
+    actuallist = []
+    while line[pos] != ")":
+        exprtok = parse_expr(mumps_module, line, pos, r"([:,)])")
+        actuallist.append(exprtok)
+        pos = exprtok.end
+        if line[pos] == ":":
+            pos = pos + 1
+            condtok = exprtok
+            exprtok = parse_expr(mumps_module, line, pos, r"[,)]")
+            exprtok.post_condition(condtok)
+        elif line[pos] == ",":
+            pos = pos + 1
+    return (pos + 1, actuallist)
+
+def consume_entry_ref(mumps_module, line, pos):
     """ parses a Mumps Entry Ref"""
     #print "entry_ref:(%s)" % line[pos:]
     #global GLOBALDB
@@ -104,10 +120,19 @@
             pos = offset.end
         if not MUMPS_RE_DICT["cmdEnd"].match(line, pos) and line[pos] == '^':
             pos = pos + 1
-            routine = parse_expr(mumps_module, line, pos, r"([ ),(:]|\s*$)")
-            token.routine = routine
-            pos = routine.end
-        
+            m_name = re_name.match(line, pos)
+            if not m_name:
+                routine = parse_expr(mumps_module, line, pos, r"([ ),(:]|\s*$)")
+                token.routine = routine
+                pos = routine.end
+            else:
+                token.routine = m_name.group()
+                pos = m_name.end()
+                if not MUMPS_RE_DICT["cmdEnd"].match(line, pos) and \
+                   line[pos] == '(':
+                    pos, token.params = \
+                         consume_actuallist(mumps_module, line, pos + 1)
+
     #GLOBALDB=0
     token.end = pos
     return (token, pos)
@@ -126,12 +151,9 @@
     token.val = re_match.group()
     pos = re_match.end()
     
-    params = re.match(r"[(].*?[)]", line[pos:])
-    if params:
-        token.params = re.split(r",", line[pos + 1:pos + params.end() - 1])
-        pos = pos + params.end()
-    else:
-        token.params = None
+    if not MUMPS_RE_DICT["cmdEnd"].match(line, pos) and line[pos] == '(':
+        pos, token.params = consume_actuallist(mumps_module, line, pos + 1)
+
     token.end = pos
     mumps_module.add_token(token)
     return token
@@ -157,30 +179,10 @@
     if GLOBALDB > 0:
         print "parse_expr %d [%s]" % (startpos, line[startpos:])
     
-    def consume_actuallist(mumps_module, line, pos):
-        """ parses a list of parameters """
-        actuallist = []
-        while line[pos] != ")":
-            exprtok = parse_expr(mumps_module, line, pos, r"([:,)])")
-            actuallist.append(exprtok)
-            pos = exprtok.end
-            if line[pos] == ":":
-                pos = pos + 1
-                condtok = exprtok
-                exprtok = parse_expr(mumps_module, line, pos, r"([,)])")
-                exprtok.post_condition(condtok)
-            elif line[pos] == ",":
-                pos = pos + 1
-        return actuallist
-
-    def consume_entry_ref(mumps_module, line, pos):
-        return parse_entry_ref(mumps_module, line, pos)
-
     def consume_pattern(lineno, line, pos):
         """ parses a mumps-style pattern-matchine pattern"""
         atom_list = []
         m_pat = MUMPS_RE_DICT["patAtom"]
-
         atom = m_pat.match(line, pos)
         while atom:
             sub_token = Token(OPPATATOM, atom.start())
@@ -256,8 +258,7 @@
         """ parses a Mumps naked reference"""
         token = Token(OPNAKEDREF, mobj.start())
         pos = mobj.end()
-        token.indices = consume_actuallist(mumps_module, line, pos)
-        pos = token.indices[-1].end + 1
+        pos, token.indices = consume_actuallist(mumps_module, line, pos)
         return (token, pos)
 
     def consume_indirection(mobj):
@@ -268,13 +269,8 @@
         token.expr = expr
         pos = expr.end
         if line[pos] == '@' and line[pos + 1] == '(':
-            indices = consume_actuallist(mumps_module, line, pos + 2)
-            token.indices = indices
-            if len(indices):
-                pos = indices[-1].end + 1
-            else:
-                pos = pos + 3
-                
+            pos, indices = consume_actuallist(mumps_module, line, pos + 2)
+            token.indices = indices               
         return (token, pos)
 
     def consume_var(mobj):
@@ -289,12 +285,8 @@
         pos = mobj.end("var")
         if mobj.group("indexed"):
             pos = pos + 1
-            indices = consume_actuallist(mumps_module, line, pos)
+            pos, indices = consume_actuallist(mumps_module, line, pos)
             token.indices = indices
-            if len(indices):
-                pos = indices[-1].end + 1
-            else:
-                pos = pos + 1
         return (token, pos)
 
     def consume_extrinsic(mobj):
@@ -306,11 +298,8 @@
         if mobj.group("routine"):
             token.routine = mobj.group("routine")
         if mobj.group("func"):
-            params = consume_actuallist(mumps_module, line, pos)
-            if len(params):
-                pos = params[-1].end
+            pos, params = consume_actuallist(mumps_module, line, pos)
             token.params = params
-            pos = pos + 1
         else: # vars...
             token.params = []
         return (token, pos)
@@ -329,8 +318,7 @@
             else:
                 if toktype == F_UNKNOWN:
                     token.name = name
-                token.params = consume_actuallist(mumps_module, line, pos)
-                pos = token.params[-1].end + 1
+                pos, token.params = consume_actuallist(mumps_module, line, pos)
         else: # vars...
             toktype = INTRINSIC_VARS.get(name, V_UNKNOWN)
             token = Token(toktype, mobj.start())
@@ -346,7 +334,6 @@
         token.end = pos
         return (token, pos)
 
-
     pattern_list = [
         (MUMPS_RE_DICT["indirection"], consume_indirection, "indirection"),
         (MUMPS_RE_DICT["var"], consume_var, "var"), 
@@ -368,7 +355,6 @@
     while not mterminator.match(line, pos):
         pattern_match = False
         for (pattern, consume, dscr) in pattern_list:
-
             re_match = pattern.match(line, pos)
             if re_match:
                 pattern_match = True
@@ -394,9 +380,20 @@
 def parse_command(mumps_module, line, startpos=0):
     """parses all known Mumps commands"""
 
-    def consume_entry_ref(mumps_module, line, pos):
-        return parse_entry_ref(mumps_module, line, pos)
-    
+    def consume_arg_indirection(mumps_module, line, pos):
+        """ parses an argument indirection expression"""
+        mterminated = MUMPS_RE_DICT["cmdEnd"]
+        if not mterminated.match(line, pos) and \
+           line[pos:pos+2] == '@(':
+            indirection = parse_expr(mumps_module, line, pos, r"([ ,]|\s*$)")
+
+            new_pos = indirection.end
+            if mterminated.match(line, new_pos):
+                mumps_module.last_token().indirection = indirection
+                mumps_module.end_token(new_pos)
+                return True
+        return False
+         
     def parse_xecute(mumps_module, line, pos):
         """ parses the Mumps Xecute command"""
         expr_list = []
@@ -597,8 +594,8 @@
                 var["val"] = val
                 pos = val.end
             else: # note: this is somewhat of an assumption, but...
+                var["indirection"] = varname
                 del var["varname"]
-                var["indirection"] = varname
             var_set.append(var)
             if not mterminated.match(line, pos) and line[pos] == ',':
                 pos = pos + 1
@@ -846,7 +843,6 @@
         mumps_module.last_token().entry_ref = entry_ref_list
         mumps_module.end_token(pos)
         
-
     def parse_break(mumps_module, line, pos):
         """parse the Mumps Break command"""
         expr_list = []
@@ -875,7 +871,6 @@
         mumps_module.last_token().val = val
         mumps_module.end_token(pos)
         
-
     command_list = {BREAKCMD:parse_break,
                    CLOSECMD:parse_devicecmd,
                    DOCMD:parse_do,
@@ -900,7 +895,6 @@
                    UNKNOWNCMD:consume_unknowncommand,
                    COMMENT:parse_comment}
 
-
     command = MUMPS_RE_DICT["command"]
     re_match = command.match(line, startpos)
     cmd = re_match.group("cmd").upper()
@@ -919,11 +913,12 @@
         mumps_module.post_condition(condtok)
         pos = condtok.end + 1
     
-    if toktype in command_list:
-        parse_func = command_list[toktype]
-        parse_func(mumps_module, line, pos)
-    else:
-        mumps_module.end_token(pos)
+    if not consume_arg_indirection(mumps_module, line, pos):
+        if toktype in command_list:
+            parse_func = command_list[toktype]
+            parse_func(mumps_module, line, pos)
+        else:
+            mumps_module.end_token(pos)
 
     return mumps_module.last_token()
 
@@ -939,14 +934,13 @@
 
     try:
         for line in fileinput.input(mumps_module.input_file):
-
             if fileinput.lineno() < mumps_module.start:
                 continue
 
             if mumps_module.end != -1 and fileinput.lineno() > mumps_module.end:
                 break
 
-            #print "%d :%s" % (fileinput.lineno(), line),
+            #if __debug__: print "%d :%s" % (fileinput.lineno(), line),
             pattern_match = False
             pos = 0
             for (pattern, parser, dscr) in pattern_list:
@@ -975,16 +969,25 @@
             if not pattern_match:
                 raise ParseError(mumps_module, line, "no Pattern match", pos, \
                                  fileinput.lineno())
-
-        fileinput.close()
     finally:
         fileinput.close()
 
 if __name__ == '__main__':
     from mumps_module import parse_for_routines
 
+    def parsemodule(mods, mod_name):
+        """parse the module specified by popup"""
+        if mods:
+            for the_module in mods:
+                if the_module.mod_name == mod_name:
+                    parseMumps(the_module)
+
     try:
-        for module in parse_for_routines("fm22.rsa", "./out"):
-            parseMumps(module)
+        #mods = parse_for_routines("../testfiles/vista.m2p", "./out")
+        mods = parse_for_routines("../testfiles/fm22.m2p", "./out")
+        #parsemodule(mods,"ACKQUTL1")
+        for the_module in mods:
+            print the_module.mod_name
+            parseMumps(the_module)
     except ParseError, err:
         print err.error_msg()


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