[Mumps2Py:] [145] Important: the parseMumps function and ModuleInfo class have been renamed parse_routine and MRoutine respectively . |
[ Thread Index |
Date Index
| More lists.mumps2py.org/discuss Archives
]
Revision: 145
Author: pgallot
Date: 2008-02-29 19:33:27 +0000 (Fri, 29 Feb 2008)
Log Message:
-----------
Important: the parseMumps function and ModuleInfo class have been renamed parse_routine and MRoutine respectively.
Otherwise, lots of small clean-up in preparation for tagging version 0.2.0
Modified Paths:
--------------
trunk/mumps2py/__init__.py
trunk/mumps2py/mumps2tok.py
trunk/mumps2py/mumpsCL.py
trunk/mumps2py/mumps_module.py
trunk/mumps2py/tok2python.py
trunk/mumps2py/tokens.py
trunk/mumps2py/tokprepass.py
Modified: trunk/mumps2py/__init__.py
===================================================================
--- trunk/mumps2py/__init__.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/__init__.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -17,8 +17,9 @@
"""Mumps2Py package module files"""
-from mumps_module import parse_for_routines
-from mumps2tok import parseMumps, parse_command, parse_expr, ParseError
+from mumps2py_config import create_m2p_file
+from mumps_module import MRoutine, parse_for_routines
+from mumps2tok import ParseError
+from mumps2tok import parse_routine, parse_line_of_commands, parse_expr
from tok2python import Translation, translate, translate_expr, TranslationError
-from mumps2py_config import create_m2p_file
Modified: trunk/mumps2py/mumps2tok.py
===================================================================
--- trunk/mumps2py/mumps2tok.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/mumps2tok.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -29,6 +29,10 @@
self.dscr = dscr
print self.error_msg()
+ def line_no(self, lineno):
+ """set the linenumber on which exception occured."""
+ self.lineno = lineno
+
def error_msg(self):
""" returns a formated string containing information about the error"""
if self.pos:
@@ -47,7 +51,7 @@
"str":re.compile(r'"'),
"num":re.compile(r"([0-9]*[.])?[0-9]+([eE][+-][0-9]+)?"),
"extr":re.compile(r"[$]{2}"),
- "external":re.compile(r"[$][&](?P<package>[%a-zA-Z0-9][A-Za-z0-9]*[.]{1})?"),
+ "external":re.compile(r"[$][&](?P<pkg>[%a-zA-Z0-9][A-Za-z0-9]*[.]{1})?"),
"extglobal":re.compile(r"\^[|[]"),
"intr":re.compile(r"[$]{1}(?P<intrinsic>[A-Za-z0-9]+)(?P<func>[(]{1})?"),
"structsysvar":re.compile(r"\^[$]{1}(?P<var>[A-Za-z0-9]+)(?P<indexed>[(]{1})?"),
@@ -303,8 +307,8 @@
""" parses a call to an external (non-MUMPS) function"""
pos = mobj.end()
token = Token(EXTERNALFUNC, pos)
- if mobj.group("package"):
- token.package_name = mobj.group("package")[:-1]
+ if mobj.group("pkg"):
+ token.package_name = mobj.group("pkg")[:-1]
(entry_ref, end_pos) = parse_entry_ref(line, pos)
token.entry_ref = entry_ref
return (token, end_pos)
@@ -959,15 +963,35 @@
return cmd_token
-def parseMumps(routine, open_fileinput= None):
- """ Parse all the code of given Mumps module."""
+def parse_line_of_commands(line, startpos = 0, lineno = -1):
+ """ Parse all the Mumps commands in a line, starting from pos."""
+ token_list = []
+ pos = startpos
+ while not re.match(r"\s*$", line[pos:]):
+ if MUMPS_RE_DICT["comment"].match(line, pos):
+ token = parse_comment(line, pos)
+ elif MUMPS_RE_DICT["cmd"].match(line, pos):
+ token = parse_command(line, pos)
+ elif MUMPS_RE_DICT["emptyline"].match(line, pos):
+ token = parse_emptyline(line, pos)
+ else:
+ raise ParseError(line, "invalid command string", pos, lineno)
+
+ token.line_no(lineno)
+ token_list.append(token)
+ pos = token.end
+
+ return token_list
+
+def parse_routine(routine, open_fileinput= None):
+ """ Parse all the code of a given Mumps module."""
routine.empty_tokenlist()
pattern_list = [
- (MUMPS_RE_DICT["label"], parse_label, "label"),
- (MUMPS_RE_DICT["comment"], parse_comment, "comment"),
- (MUMPS_RE_DICT["emptyline"], parse_emptyline, "emptyline"),
- (MUMPS_RE_DICT["cmd"], parse_command, "cmd")]
+ (MUMPS_RE_DICT["label"], parse_label),
+ (MUMPS_RE_DICT["comment"], parse_comment),
+ (MUMPS_RE_DICT["emptyline"], parse_emptyline),
+ (MUMPS_RE_DICT["cmd"], parse_command)]
if open_fileinput:
input_file = open_fileinput
@@ -976,42 +1000,34 @@
try:
for line in input_file:
- if fileinput.lineno() < routine.start:
+ lineno = fileinput.lineno()
+ if lineno < routine.start:
continue
- if routine.end != -1 and fileinput.lineno() > routine.end:
+ if routine.end != -1 and lineno > routine.end:
break
#if __debug__: print "%d :%s" % (fileinput.lineno(), line),
pattern_match = False
pos = 0
- for (pattern, parser, dscr) in pattern_list:
+ for pattern, parser in pattern_list:
if pattern.match(line):
pattern_match = True
- token = parser(line)
- token.line_no(fileinput.lineno())
- routine.add_token(token)
- pos = token.end
-
- while not re.match(r"\s*$", line[pos:]):
- inside_pattern_match = False
- for (pattern, parser, dscr) in pattern_list[1:]:
- # no need to scan for labels...
- if pattern.match(line, pos):
- inside_pattern_match = True
- token = parser(line, pos)
- routine.add_token(token)
- pos = token.end
- break
-
- if not inside_pattern_match:
- raise ParseError(line, "no Matching pattern", pos, \
- fileinput.lineno())
- break
+ 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, \
- fileinput.lineno())
+ raise ParseError(line, "no Pattern match", pos, lineno)
finally:
if not open_fileinput:
fileinput.close()
@@ -1019,12 +1035,12 @@
if __name__ == '__main__':
from mumps_module import parse_for_routines
- def parsemodule(mods, mod_name):
+ def parsemodule(mod_list, mod_name):
"""parse the module specified by popup"""
if mods:
- for a_module in mods:
+ for a_module in mod_list:
if a_module.mod_name == mod_name:
- parseMumps(a_module)
+ parse_routine(a_module)
try:
#mods = parse_for_routines("../testfiles/vista.m2p", "./out")
@@ -1032,7 +1048,7 @@
#parsemodule(mods,"ZTMB")
for the_module in mods:
print the_module.mod_name
- parseMumps(the_module)
+ parse_routine(the_module)
the_module.empty_tokenlist() # keep memory usage down
- except ParseError, err:
- print err.error_msg()
+ except ParseError, err0:
+ print err0.error_msg()
Modified: trunk/mumps2py/mumpsCL.py
===================================================================
--- trunk/mumps2py/mumpsCL.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/mumpsCL.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -26,24 +26,28 @@
def M_eval(line):
"""parses, translates, and evaluates a string as a Mumps expression"""
- from mumps2py import parse_expr, Translation, translate_expr
+ from mumps2py import Translation, parse_expr, translate_expr
token = parse_expr(line, 0)
translation = Translation(None)
expression = translate_expr(translation, token)
return expression
+def M_xecute_eval(line):
+ """evaluates a string of Mumps commands"""
+ from mumps2py import MRoutine, parse_line_of_commands, translate
+
+ token_list = parse_line_of_commands(line)
+ tmp_module = MRoutine("", "", "temp")
+ tmp_module.add_tokens(token_list)
+ expression = translate(tmp_module)
+ return expression
+
def M_cmd_eval(cmd, arg_str):
""" evaluates a Mumps command using argument indirection"""
- from mumps2py import parse_command, Translation, translate
-
cmd_str = " %s %s" % (cmd, arg_str)
- token = parse_command(cmd_str)
- translation = Translation(None)
- expression = translate(translation, token)
- return expression
+ return M_xecute_eval(cmd_str)
-
def Mf_horolog(timestamp = None):
"""function that emulates Mumps $Horolog variable."""
if timestamp == None:
Modified: trunk/mumps2py/mumps_module.py
===================================================================
--- trunk/mumps2py/mumps_module.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/mumps_module.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -18,8 +18,8 @@
import re, fileinput, os.path
import mumps2py_config
-class ModuleInfo:
- """ A ModuleInfo object represents one Mumps routine."""
+class MRoutine:
+ """ A MRoutine object represents one Mumps routine."""
def __init__(self, inputfile, outputdir, name, startline = 1, endline = -1):
self.input_file = inputfile
if os.path.isabs(name):
@@ -44,10 +44,14 @@
while len(self.token_list):
self.token_list.pop()
- def add_token(self, newtoken):
+ def add_token(self, new_token):
""" add a new token to the token list."""
- self.token_list.append(newtoken)
+ self.token_list.append(new_token)
+ def add_tokens(self, new_token_list):
+ """ add a list of new tokens to the token list."""
+ self.token_list.extend(new_token_list)
+
def tokenized(self):
"""return the token list"""
return self.token_list
@@ -84,7 +88,7 @@
def parse_cache_routines(inputfile, outputdir):
- """parseForModules returns a list of ModuleInfo objects from a file
+ """parseForModules returns a list of MRoutine objects from a file
in Cache Format, assuming that each Mumps module begins
with a line containing modulename^INT^"""
modules = []
@@ -103,7 +107,7 @@
tmp = modsplit.split(line, 1)
if oldmodname:
modules.append( \
- ModuleInfo(inputfile, outputdir,
+ MRoutine(inputfile, outputdir,
oldmodname, lastmodline + 1, lineno - 1))
if __debug__:
print oldmodname, lastmodline + 1, lineno - 1, "\t", \
@@ -115,7 +119,7 @@
return modules
def parse_astextroutines(inputfile, outputdir):
- """returns a list of ModuleInfo object from a file which has
+ """returns a list of MRoutine object from a file which has
each routine separated by a blank line and the routine name given
on its own line before the routine body."""
@@ -140,7 +144,7 @@
if modmatch and emptypattern.match(last_line):
tmp = modmatch.group("name")
if oldmodname:
- modules.append( ModuleInfo(inputfile, outputdir, oldmodname,
+ modules.append( MRoutine(inputfile, outputdir, oldmodname,
lastmodline + 1, lineno - 1))
#if __debug__:
# print oldmodname, lastmodline + 1, lineno - 1, "\t", \
@@ -169,14 +173,14 @@
modules = []
for routine, start, end in info['index']:
if routine_filter.accept(routine):
- modules.append( ModuleInfo(inputfile, outputdir,
+ modules.append( MRoutine(inputfile, outputdir,
routine, start, end))
#if __debug__: print routine, start, end, "\t", end - start +1
return modules
def parse_for_routines(inputfile, outputdir):
- """returns a list of ModuleInfo objects"""
+ """returns a list of MRoutine objects"""
mfile = open(inputfile)
headerline = mfile.readline()
mfile.close()
@@ -190,4 +194,4 @@
if extension and extension == ".m2p":
return parse_from_m2p_cfg(inputfile)
else:
- return [ModuleInfo(inputfile, outputdir, outputname), ]
+ return [MRoutine(inputfile, outputdir, outputname), ]
Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/tok2python.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -898,8 +898,5 @@
translation.add_frontmatter()
except TranslationError, err:
print err.error_msg()
-# finally:
-# translation.code.append("incomplete!!!\n")
-# return translation.code
-
+ translation.code.append("incomplete!!!\n")
return translation.code
Modified: trunk/mumps2py/tokens.py
===================================================================
--- trunk/mumps2py/tokens.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/tokens.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -507,7 +507,7 @@
def line_no(self, lineno):
"""set the linenumber on which token occured.
-In practice, this is only done for the first command on a line.
+In practice, this is only done for commands on a line.
and there is a dependency on this behavior in tokprepass."""
self.lineno = lineno
Modified: trunk/mumps2py/tokprepass.py
===================================================================
--- trunk/mumps2py/tokprepass.py 2008-02-29 05:44:25 UTC (rev 144)
+++ trunk/mumps2py/tokprepass.py 2008-02-29 19:33:27 UTC (rev 145)
@@ -23,7 +23,8 @@
def fix_indent_levels(token, state):
"""fixing the indent levels for FOR,IF, ELSE commands"""
- if token.lineno: # starting a new line...
+ if token.lineno and token.lineno != state['lineno']: #starting a new line...
+ state['lineno'] = token.lineno
state['new'] = token.indentlevel
state['next'] = token.indentlevel
if token.toktype in (FORCMD, IFCMD, ELSECMD):
@@ -68,7 +69,7 @@
def prepass(a_mumps_module):
""" Calls various functions to massage the tokens within a module."""
- indentlevel_state = {'new':0, 'next':0}
+ indentlevel_state = {'new':0, 'next':0, 'lineno':0}
localvars_state = {'collection':{} }
mergevars_state = {'collection':{} }
for token in a_mumps_module.tokenized():