[Mumps2Py:] [6] placing mumps2py files into a package directory. |
[ Thread Index |
Date Index
| More lists.mumps2py.org/discuss Archives
]
Revision: 6
Author: pgallot
Date: 2008-01-17 17:53:51 +0000 (Thu, 17 Jan 2008)
Log Message:
-----------
placing mumps2py files into a package directory.
Added Paths:
-----------
trunk/mumps2py/
trunk/mumps2py/__init__.py
trunk/mumps2py/mumps2py_config.py
trunk/mumps2py/mumps2tok.py
trunk/mumps2py/mumpsCL.py
trunk/mumps2py/mumps_module.py
trunk/mumps2py/tokens.py
trunk/mumps2py/tokprepass.py
Copied: trunk/mumps2py/__init__.py (from rev 1, trunk/__init__.py)
===================================================================
--- trunk/mumps2py/__init__.py (rev 0)
+++ trunk/mumps2py/__init__.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,24 @@
+# Copyright 2008 Patrick Gallot
+
+## This file is part of Mumps2Py.
+##
+## Mumps2Py is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Mumps2Py is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Mumps2Py. If not, see <http://www.gnu.org/licenses/>.
+
+"""Mumps2Py package module files"""
+
+__all__ = [ "tokens", "mumps_module", "mumps2tok", "tokprepass", "tok2python",
+ "mumpsCL", # mumpsCL is supposed to be standalone...
+#"mumps2py_config,
+]
+
Copied: trunk/mumps2py/mumps2py_config.py (from rev 1, trunk/mumps2py_config.py)
===================================================================
--- trunk/mumps2py/mumps2py_config.py (rev 0)
+++ trunk/mumps2py/mumps2py_config.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,3 @@
+"""
+mumps2py_config contains all code for reading and writing any configuration
+settings related to mumps2py's translation of Mumps code to Python."""
Copied: trunk/mumps2py/mumps2tok.py (from rev 1, trunk/mumps2tok.py)
===================================================================
--- trunk/mumps2py/mumps2tok.py (rev 0)
+++ trunk/mumps2py/mumps2tok.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,988 @@
+# Copyright 2008 Patrick Gallot
+
+## This file is part of Mumps2Py.
+##
+## Mumps2Py is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Mumps2Py is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Mumps2Py. If not, see <http://www.gnu.org/licenses/>.
+""" extract tokens from the Mumps source code """
+
+import re, fileinput
+from mumps_module import ModuleInfo
+from tokens import *
+
+GLOBALDB = 0 # debugging variable.
+
+class ParseError(Exception):
+ """ Exception class for errors relating to parsing Mumps code."""
+ def __init__(self, the_module, line, dscr = "", pos = 0, lineno = -1):
+ Exception.__init__(self)
+ self.module = the_module
+ self.line = line
+ self.lineno = lineno
+ self.pos = pos
+ self.dscr = dscr
+ print self.error_msg()
+
+ def error_msg(self):
+ """ returns a formated string containing information about the error"""
+ if self.pos:
+ errormsg = "\nError: %s %d:%d : %s\n\t%s\n[%s]" \
+ % (self.module.mod_name, self.lineno, self.pos,
+ self.dscr, self.line, self.line[self.pos:])
+ else:
+ errormsg = "\nError: %s %d: %s\n\t%s" \
+ % (self.module.mod_name, self.lineno, self.dscr,
+ self.line)
+ return errormsg
+
+MUMPS_RE_DICT = {
+ "var":re.compile(r"((?P<globalvar>\^{1})|(?P<pbr>[.]{1}))?(?P<var>[A-Za-z%][A-Za-z0-9]*)(?P<indexed>[(]{1})?"),
+ "indirection":re.compile(r"@"),
+ "op":re.compile(r"[-_/+']|([']?(([*]{1,2})|([]]{1,2})|\\|[[=><#&!/]))"),
+ "str":re.compile(r"[\"](.|[\"]{2})*?[\"]"),
+ "num":re.compile(r"([0-9]?[.]{1})?[0-9]+([eE][+-][0-9]+)?"),
+ "extr":re.compile(r"[$]{2}(?P<label>\w+)(\^(?P<routine>[%A-Za-z][A-Za-z0-9]*))?(?P<func>[(]{1})?"),
+ "intr":re.compile(r"[$]{1}(?P<intrinsic>[A-Za-z0-9]+)(?P<func>[(]{1})?"),
+ "patmat":re.compile(r"[?]"),
+ "patAtom":re.compile(r"(((?P<mnrp>\d+)?[.](?P<mxrp>\d+)?)|(?P<rpcount>\d+)){1}((?P<altlist>[(])|(?P<patcode>[ACELNPUacelnpu]+)|(?P<str>([\"]([\"]{2}|.)*?[\"])))"),
+ "nakedRef":re.compile(r"\^\("),
+ "subexpr":re.compile(r"[(]"),
+ "command":re.compile(r"(?P<indents>[ .]*)(?P<cmd>[A-Za-z]+)"),
+ "label":re.compile(r"^(\^)?[%a-zA-Z0-9][A-Za-z0-9]*"),
+ "comment":re.compile(r"[ ]*?[ .]*?;(?P<comment>.*)"),
+ "emptyline":re.compile(r"[ .]*\s*$"),
+ "cmd":re.compile(r"[. ]*([A-Za-z]+\w*?(?:[ :]|\s*$))"),
+ "cmdEnd":re.compile(r"[ ]|\s*$")
+ }
+
+def parse_entry_ref(mumps_module, line, pos):
+ """ parses a Mumps Entry Ref"""
+ #print "entry_ref:(%s)" % line[pos:]
+ #global GLOBALDB
+ #GLOBALDB=1
+
+ token = Token(ENTRYREF, pos)
+ if line[pos] == '@':
+ if MUMPS_RE_DICT["nakedRef"].match(line, pos + 1):
+ indirect = parse_expr(mumps_module, line, pos+1, r"([,+: ]|\s$)")
+ token.indirect = indirect
+ pos = indirect.end + 1
+ else:
+ indirect = parse_expr(mumps_module, line, pos, \
+ r"(\^|[ +),:]|\s*$)")
+ token.indirect = indirect
+ pos = indirect.end
+ if not MUMPS_RE_DICT["cmdEnd"].match(line, pos) and line[pos] == '+':
+ pos = pos + 1
+ offset = parse_expr(mumps_module, line, pos, r"(\^|[ ),:]|\s*$)")
+ token.offset = offset
+ 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
+ else:
+ re_name = re.compile(r"[A-Za-z%0-9][A-Za-z0-9]*")
+ m_name = re_name.match(line, pos)
+ if m_name:
+ token.label = m_name.group()
+ pos = m_name.end()
+ if not MUMPS_RE_DICT["cmdEnd"].match(line, pos) and line[pos] == '+':
+ pos = pos + 1
+ offset = parse_expr(mumps_module, line, pos, r"(\^|[ ):,]|\s*$)")
+ token.offset = offset
+ 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
+
+ #GLOBALDB=0
+ token.end = pos
+ return (token, pos)
+
+def parse_emptyline(mumps_module, line, startpos=0):
+ """ parses a line of Mumps code which contains practically nothing"""
+ token = Token(EMPTYLINE, startpos)
+ token.end = len(line)
+ mumps_module.add_token(token)
+ return token
+
+def parse_label(mumps_module, line):
+ """ parses a Mumps label"""
+ token = Token(LABEL, 0)
+ pos = 0
+ if line[0] == "^": # NOTE: I'm not completely sure of the semantics here...
+ token.externally_visible = True
+ pos = 1
+ else:
+ token.externally_visible = False
+
+ re_match = re.match(r"[%a-zA-Z0-9]+\w*", line[pos:])
+ token.val = line[pos:re_match.end() + pos]
+ pos = re_match.end() + pos
+
+ params = re.match(r"[(].*?[)]+?", line[pos:])
+ if params:
+ token.params = re.split(r"\W+", line[pos + 1:pos + params.end() - 1])
+ pos = pos + params.end()
+ else:
+ token.params = None
+ token.end = pos
+ mumps_module.add_token(token)
+ return token
+
+
+def parse_comment(mumps_module, line, startpos = 0):
+ """ parses a Mumps comment"""
+ re_match = MUMPS_RE_DICT["comment"].match(line, startpos)
+ token = Token(COMMENT, startpos)
+ token.val = re_match.group("comment")
+ token.end = len(line)
+ mumps_module.add_token(token)
+ return token
+
+def parse_expr(mumps_module, line, startpos, terminator = r"([ ]|\s*$)"):
+ """ parses a Mumps expression by recursive descent"""
+ 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())
+ if atom.group("rpcount"):
+ sub_token.repcount = int(atom.group("rpcount"))
+ else:
+ if atom.group("mnrp"):
+ sub_token.minrep = int(atom.group("mnrp"))
+ if atom.group("mxrp"):
+ sub_token.maxrep = int(atom.group("mxrp"))
+ if atom.group("str"):
+ sub_token.match_str = atom.group("str")
+ pos = atom.end()
+ elif atom.group("altlist"):
+ sub_token.alt_list = []
+ pos = atom.end()
+ while line[pos] != ")":
+ (sub_pat, pos) = consume_pattern(lineno, line, pos)
+ sub_token.alt_list.append(sub_pat)
+ if line[pos] == ",":
+ pos = pos + 1
+ pos = pos + 1
+ else:
+ sub_token.pat_code = atom.group("patcode")
+ pos = atom.end()
+ atom_list.append(sub_token)
+ atom = m_pat.match(line, pos)
+ return (atom_list, pos)
+
+ def consume_sub_expr(mobj):
+ """ parses a parens-enclosed sub-expression"""
+ token = Token(EXPR, mobj.start())
+ token.expr_list = [ \
+ parse_expr(mumps_module, line, mobj.end(), r"([)])"), ]
+ pos = token.expr_list[-1].end + 1
+ return (token, pos)
+
+ def consume_str(mobj):
+ """ parses a Mumps-string"""
+ token = Token(STRINGLITERAL, mobj.start())
+ token.val = mobj.group()
+ return (token, mobj.end())
+
+ def consume_num(mobj):
+ """ parses a number"""
+ token = Token(NUMLITERAL, mobj.start())
+ token.val = mobj.group()
+ return (token, mobj.end())
+
+ def consume_op(mobj):
+ """ parses a Mumps operator"""
+ op_type = line[mobj.start():mobj.end()]
+ token = Token(OP_TOKEN_DICT[op_type], mobj.start())
+ return (token, mobj.end())
+
+ def consume_unknown(mobj):
+ token = Token(UNKNOWNITEM, mobj.start())
+ token.item = mobj.group()
+ return (token, mobj.end())
+
+ def consume_naked_ref(mobj):
+ """ 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
+ return (token, pos)
+
+ def consume_indirection(mobj):
+ """ parses a Mumps indirection argument"""
+ token = Token(INDIRECTION, mobj.start())
+ expr = parse_expr(mumps_module, line, mobj.end(), \
+ r"(\\|[-@:,+=><#&!*/ ')]|\s*$)")
+ 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
+
+ return (token, pos)
+
+ def consume_var(mobj):
+ """ parses a Mumps variable"""
+ if mobj.group("globalvar"):
+ token = Token(GLOBALVAR, mobj.start())
+ else:
+ token = Token(LOCALVAR, mobj.start())
+ if mobj.group("pbr"):
+ token.pass_by_reference = True
+ token.varname = mobj.group("var")
+ pos = mobj.end("var")
+ if mobj.group("indexed"):
+ pos = pos + 1
+ 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):
+ """ parses a Mumps extrinsic function or variable"""
+ token = Token(USERFUNC, mobj.start())
+ pos = mobj.end()
+ if mobj.group("label"):
+ token.label = mobj.group("label")
+ 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
+ token.params = params
+ pos = pos + 1
+ else: # vars...
+ token.params = []
+ return (token, pos)
+
+ def consume_intrinsic(mobj):
+ """ parses a Mumps intrinsic function or variable"""
+ name = re_match.group("intrinsic")
+ pos = mobj.end()
+ if mobj.group("func"):
+ toktype = IntrinsicFuncDict[name]
+ token = Token(toktype, mobj.start())
+ if toktype == F_TEXT:
+ (params, end_pos) = consume_entry_ref(mumps_module, line, pos)
+ token.params = params
+ pos = end_pos + 1
+ else:
+ token.params = consume_actuallist(mumps_module, line, pos)
+ pos = token.params[-1].end + 1
+ else: # vars...
+ toktype = IntrinsicVarDict[name]
+ token = Token(toktype, mobj.start())
+
+ return (token, pos)
+
+ def consume_pattmatch(mobj):
+ """ parses mumps-style pattern-matchine patterns"""
+ pos = mobj.end()
+ token = Token(OPPATMATCH, mobj.start())
+ (token.atom_list, pos) = consume_pattern(mumps_module, line, pos)
+ token.end = pos
+ return (token, pos)
+
+
+ pattern_list = [
+ (MUMPS_RE_DICT["indirection"], consume_indirection, "indirection"),
+ (MUMPS_RE_DICT["var"], consume_var, "var"),
+ (MUMPS_RE_DICT["op"], consume_op, "op"),
+ (MUMPS_RE_DICT["str"], consume_str, "str"),
+ (MUMPS_RE_DICT["num"], consume_num, "num"),
+ (MUMPS_RE_DICT["extr"], consume_extrinsic, "extr"),
+ (MUMPS_RE_DICT["intr"], consume_intrinsic, "intr"),
+ (MUMPS_RE_DICT["patmat"], consume_pattmatch, "patmat"),
+ (MUMPS_RE_DICT["nakedRef"], consume_naked_ref, "nakedRef"),
+ (re.compile(r"(\^\$JOB)"), consume_unknown, "unknown"),
+ (MUMPS_RE_DICT["subexpr"], consume_sub_expr, "subexpr")]
+
+ pos = startpos
+ token = Token(EXPR, pos)
+ token.expr_list = []
+
+ mterminator = re.compile(terminator)
+ 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
+ (sub_token, end_pos) = consume(re_match)
+ pos = sub_token.end = end_pos
+ token.expr_list.append(sub_token)
+ break
+
+ if not pattern_match:
+ raise ParseError(mumps_module, line, "No Pattern match", pos )
+
+ if len(token.expr_list)==1:
+ token = token.expr_list[0]
+ else:
+ token.end = pos
+ return token
+
+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 parse_xecute(mumps_module, line, pos):
+ """ parses the Mumps Xecute command"""
+ expr_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ exprtok = parse_expr(mumps_module, line, pos, r"([ :,]|\s*$)")
+ pos = exprtok.end
+ if line[pos] == ":":
+ condtok = parse_expr(mumps_module, line, pos + 1, \
+ r"([ ,]|\s*$)")
+ exprtok.post_condition(condtok)
+ pos = condtok.end
+ if line[pos] == ",":
+ pos = pos + 1
+ expr_list.append(exprtok)
+
+ mumps_module.last_token().Xecute = expr_list
+ mumps_module.end_token(pos + 1)
+
+ def parse_write(mumps_module, line, pos):
+ """ parses the Mumps Write command"""
+ write_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ if line[pos] == '!':
+ sub_token = Token(FCC_NEWLINE, pos)
+ pos = pos + 1
+ elif line[pos] == '#':
+ sub_token = Token(FCC_NEWPAGE, pos)
+ pos = pos + 1
+ elif line[pos] == '?':
+ sub_token = Token(FCC_MOV, pos)
+ sub_token.offset = parse_expr(mumps_module, line, pos+1, \
+ r"([, ]|\s*$)")
+ pos = sub_token.offset.end
+ elif line[pos] == '*':
+ sub_token = Token(FCC_CHARVAL, pos)
+ sub_token.val = parse_expr(mumps_module, line, pos+1, \
+ r"([, ]|\s*$)")
+ pos = sub_token.val.end
+ else:
+ sub_token = parse_expr(mumps_module, line, pos, r"([, ]|\s*$)")
+ pos = sub_token.end
+ if line[pos] == ',':
+ pos = pos + 1
+ write_list.append(sub_token)
+
+ mumps_module.last_token().write_list = write_list
+ mumps_module.end_token(pos)
+
+ def parse_read(mumps_module, line, pos):
+ """ parses the Mumps Read command"""
+ read_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ if line[pos] == '!':
+ sub_token = Token(FCC_NEWLINE, pos)
+ pos = pos + 1
+ elif line[pos] == '#':
+ sub_token = Token(FCC_NEWPAGE, pos)
+ pos = pos + 1
+ elif line[pos] == '?':
+ sub_token = Token(FCC_MOV, pos)
+ sub_token.offset = parse_expr(mumps_module, line, pos + 1, \
+ r"([, ]|\s*$)")
+ pos = sub_token.offset.end
+ elif line[pos] == '"': # it's a string.
+ sub_token = parse_expr(mumps_module, line, pos, r"([, ]|\s*$)")
+ pos = sub_token.end
+ elif line[pos] == '*':
+ sub_token = Token(FCC_CHARVAL, pos)
+ char_val_name = parse_expr(mumps_module, line, pos + 1, \
+ r"([:, ]|\s*$)")
+ sub_token.char_val_name = char_val_name
+ pos = char_val_name.end
+ else:
+ sub_token = parse_expr(mumps_module, line, pos + 1, \
+ r"([#:, ]|\s*$)")
+ pos = sub_token.end
+ if not mterminated.match(line, pos) and line[pos] == '#':
+ max_chars = parse_expr(mumps_module, line, pos + 1, \
+ r"([:, ]|\s*$)")
+ sub_token.max_chars = max_chars
+ pos = max_chars.end
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ timeout = parse_expr(mumps_module, line, pos + 1, r"[ ,]|\s*$")
+ sub_token.timeout = timeout
+ pos = timeout.end
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+ read_list.append(sub_token)
+
+ mumps_module.last_token().read_list = read_list
+ mumps_module.end_token(pos)
+
+ def parse_view(mumps_module, line, pos):
+ """parses the Mumps View command"""
+ keyword_items = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ item = {}
+ keyword = parse_expr(mumps_module, line, pos, r"([:, ]|\s*$)")
+ item["keyword"] = keyword
+ pos = keyword.end
+ expressions = []
+ while not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ expr = parse_expr(mumps_module, line, pos, r"([:, ]|\s*$)")
+ pos = expr.end
+ expressions.append(expr)
+ item["expressions"] = expressions
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+ keyword_items.append(item)
+
+ mumps_module.last_token().keyword_items = keyword_items
+ mumps_module.end_token(pos)
+
+ def parse_merge(mumps_module, line, pos):
+ """parses the Mumps Merge Command"""
+ items = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ merge_item = {}
+ recipient = parse_expr(mumps_module, line, pos, r"([=, ]|\s*$)")
+ merge_item["l_item"] = recipient
+ pos = recipient.end
+ if not mterminated.match(line, pos) and line[pos] == '=':
+ pos = pos + 1
+ copy_item = parse_expr(mumps_module, line, pos, r"([, ]|\s*$)")
+ merge_item["r_item"] = copy_item
+ pos = copy_item.end
+ items.append(merge_item)
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+
+ mumps_module.last_token().items = items
+ mumps_module.end_token(pos)
+
+ def parse_devicecmd(mumps_module, line, pos):
+ """parses the Mumps Open, Close, and Use commands"""
+ device_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ device = {}
+ expr = parse_expr(mumps_module, line, pos + 1, r"([ :,]|\s*$)")
+ device["Expr"] = expr
+ pos = expr.end
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ if line[pos] == '(':
+ pos = pos + 1
+ keyword_list = []
+ while line[pos] != ')':
+ keyword = parse_expr(mumps_module, line, pos, r"([:)])")
+ keyword_list.append(keyword)
+ pos = keyword.end
+ if line[pos] == ':':
+ pos = pos + 1
+ device["keyword_list"] = keyword_list
+ pos = pos + 1
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ timeout = parse_expr(mumps_module, line, pos, r"[ ,]|\s*$")
+ device["time_out"] = timeout
+ pos = timeout.end
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+ device_list.append(device)
+
+ mumps_module.last_token().device_list = device_list
+ mumps_module.end_token(pos)
+
+ def parse_set(mumps_module, line, pos):
+ """ parses the Mumps Set command"""
+ var_set = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ var = {}
+ if line[pos] == '(':
+ pos = pos + 1
+ var_list = []
+ while line[pos] != ")":
+ varname = parse_expr(mumps_module, line, pos, r"([,)])")
+ var_list.append(varname)
+ pos = varname.end
+ if line[pos] == ',':
+ pos = pos + 1
+ var["var_names"] = var_list
+ pos = pos + 1
+ else:
+ varname = parse_expr(mumps_module, line, pos, r"([,= ]|\s*$)")
+ var["varname"] = varname
+ pos = varname.end
+ if not mterminated.match(line, pos) and line[pos] == '=':
+ pos = pos + 1
+ val = parse_expr(mumps_module, line, pos, r"([ ,]|\s*$)")
+ var["val"] = val
+ pos = val.end
+ else: # note: this is somewhat of an assumption, but...
+ del var["varname"]
+ var["indirection"] = varname
+ var_set.append(var)
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+
+ mumps_module.last_token().var_set = var_set
+ mumps_module.end_token(pos)
+
+ def parse_lock(mumps_module, line, pos):
+ """parses the Mumps Lock command"""
+ lock_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ lock_item = {}
+ if line[pos] in ('+', '-'):
+ lock_item["op"] = line[pos]
+ pos = pos + 1
+ if line[pos] == '(':
+ nref_list = []
+ pos = pos + 1
+ while line[pos] != ')':
+ nref = parse_expr(mumps_module, line, pos, r"([,)])")
+ pos = nref.end
+ nref_list.append(nref)
+ if line[pos] == ',':
+ pos = pos + 1
+ lock_item["nrefs"] = nref_list
+ else:
+ nref = parse_expr(mumps_module, line, pos, r"([ ,:]|\s*$)")
+ pos = nref.end
+ lock_item["nrefs"] = (nref, )
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ timeout = parse_expr(mumps_module, line, pos, r"[ ,]|\s*$")
+ lock_item["time_out"] = timeout
+ pos = timeout.end
+ lock_list.append(lock_item)
+ if line[pos] == ',':
+ pos = pos + 1
+
+ mumps_module.last_token().lock_list = lock_list
+ mumps_module.end_token(pos)
+
+ def parse_job(mumps_module, line, pos):
+ """parses the Mumps Job command"""
+ entry_ref_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ (entry_ref, pos) = consume_entry_ref(mumps_module, line, pos)
+ if not mterminated.match(line, pos) and line[pos] == '(':
+ pos = pos + 1
+ params = []
+ while line[pos] != ")":
+ paramtok = parse_expr(mumps_module, line, pos, r"([,)])")
+ params.append(paramtok)
+ pos = paramtok.end
+ if line[pos] == ',':
+ pos = pos + 1
+ entry_ref.params = params
+ pos = pos + 1
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ if line[pos] == '(':
+ pos = pos + 1
+ keyword_list = []
+ while line[pos] != ')':
+ keyword = parse_expr(mumps_module, line, pos, r"([:)])")
+ keyword_list.append(keyword)
+ pos = keyword.end
+ if line[pos] == ':':
+ pos = pos + 1
+ pos = pos + 1
+ entry_ref.keyword_list = keyword_list
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ timeout = parse_expr(mumps_module, line, pos, r"[ ,]|\s*$")
+ pos = timeout.end
+ entry_ref.timeout = timeout
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+ entry_ref_list.append(entry_ref)
+
+ mumps_module.last_token().entry_ref = entry_ref_list
+ mumps_module.end_token(pos)
+
+ def parse_quit(mumps_module, line, pos):
+ """parses the Mumps Quit command"""
+ condtok = parse_expr(mumps_module, line, pos)
+ mumps_module.last_token().Condition = condtok
+ mumps_module.end_token(condtok.end)
+
+ def parse_new(mumps_module, line, pos):
+ """parses the Mumps New command"""
+ new_list = []
+ excl_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ if line[pos] == '(':
+ pos = pos + 1
+ while line[pos] != ')':
+ vartok = parse_expr(mumps_module, line, pos, r"([,)])")
+ pos = vartok.end
+ excl_list.append(vartok)
+ if line[pos] == ',':
+ pos = pos + 1
+ pos = pos + 1 # eat the closing parens.
+ else:
+ vartok = parse_expr(mumps_module, line, pos, r"([ ,]|\s*$)")
+ pos = vartok.end
+ new_list.append(vartok)
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+
+ mumps_module.last_token().new_list = new_list
+ mumps_module.last_token().excl_list = excl_list
+ mumps_module.end_token(pos)
+
+ def parse_kill(mumps_module, line, pos):
+ """parses the Mumps Kill command"""
+ kill_list = []
+ excl_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ if line[pos] == '(':
+ pos = pos + 1
+ while line[pos] != ')':
+ vartok = parse_expr(mumps_module, line, pos, r"([,)])")
+ pos = vartok.end
+ excl_list.append(vartok)
+ if line[pos] == ',':
+ pos = pos + 1
+ pos = pos + 1 # eat the closing parens.
+ else:
+ vartok = parse_expr(mumps_module, line, pos, r"([ ,]|\s*$)")
+ pos = vartok.end
+ kill_list.append(vartok)
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+
+ mumps_module.last_token().kill_list = kill_list
+ mumps_module.last_token().excl_list = excl_list
+ mumps_module.end_token(pos)
+
+ def parse_if(mumps_module, line, pos):
+ """parses the Mumps If command"""
+ expr_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ exprtok = parse_expr(mumps_module, line, pos, r"([ ,]|\s*$)")
+ expr_list.append(exprtok)
+ pos = exprtok.end
+ if line[pos] == ",":
+ pos = pos + 1
+ mumps_module.last_token().condition_list = expr_list
+ mumps_module.end_token(pos + 1)
+
+ def parse_hang(mumps_module, line, pos):
+ """parses the Mumps Hang/Halt commands"""
+ timetok = parse_expr(mumps_module, line, pos)
+ mumps_module.last_token().HangTime = timetok
+ mumps_module.end_token(timetok.end)
+
+ def parse_goto(mumps_module, line, pos):
+ """parse the Mumps Goto command"""
+ #NOTE: a goto seems very similar to a do, but with no arguments passed.
+ entry_ref_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ (entry_ref, pos) = consume_entry_ref(mumps_module, line, pos)
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ condtok = parse_expr(mumps_module, line, pos, r"([ ,]|\s$)")
+ entry_ref.post_condition(condtok)
+ pos = condtok.end
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+ entry_ref_list.append(entry_ref)
+
+ mumps_module.last_token().entry_ref = entry_ref_list
+ mumps_module.end_token(pos)
+
+ def parse_for(mumps_module, line, pos):
+ """ parses the Mumps For command"""
+ loops = []
+ loop_incr_var = None
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ if not mterminated.match(line, pos):
+ loop_incr_var = parse_expr(mumps_module, line, pos, r"([=])")
+ pos = loop_incr_var.end + 1 # eat the equal sign.
+ while not mterminated.match(line, pos):
+ loop_vals = {}
+ loop_vals["initVal"] = parse_expr(mumps_module, line, pos, \
+ r"([,: ]|\s$)")
+ pos = loop_vals["initVal"].end
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ incr_val = parse_expr(mumps_module, line, pos, \
+ r"([ :,]|\s$)")
+ loop_vals["IncrVal"] = incr_val
+ pos = incr_val.end
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ terminal_val = parse_expr(mumps_module, line, pos, \
+ r"([ :,]|\s$)")
+ loop_vals["TermVal"] = terminal_val
+ pos = terminal_val.end
+ loops.append(loop_vals)
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+
+ mumps_module.last_token().loop_incr_var = loop_incr_var
+ mumps_module.last_token().for_loops = loops
+ mumps_module.end_token(pos)
+
+ def parse_do(mumps_module, line, pos):
+ """parses the Mumps Do command"""
+ entry_ref_list = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ (entry_ref, pos) = consume_entry_ref(mumps_module, line, pos)
+ if not mterminated.match(line, pos) and line[pos] == '(':
+ pos = pos + 1
+ params = []
+ while line[pos] != ")":
+ paramtok = parse_expr(mumps_module, line, pos, r"([,)])")
+ params.append(paramtok)
+ pos = paramtok.end
+ if line[pos] == ',':
+ pos = pos + 1
+ entry_ref.params = params
+ pos = pos + 1
+ if not mterminated.match(line, pos) and line[pos] == ':':
+ pos = pos + 1
+ condtok = parse_expr(mumps_module, line, pos, r"([ ,]|\s$)")
+ entry_ref.post_condition(condtok)
+ pos = condtok.end
+ if not mterminated.match(line, pos) and line[pos] == ',':
+ pos = pos + 1
+ entry_ref_list.append(entry_ref)
+
+ 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 = []
+ mterminated = MUMPS_RE_DICT["cmdEnd"]
+ while not mterminated.match(line, pos):
+ exprtok = parse_expr(mumps_module, line, pos, r"([ :,]|\s*$)")
+ pos = exprtok.end
+ if line[pos] == ":":
+ condtok = parse_expr(mumps_module, line, pos+1, r"([ ,]|\s*$)")
+ exprtok.post_condition(condtok)
+ pos = condtok.end
+ if line[pos] == ",":
+ pos = pos + 1
+ expr_list.append(exprtok)
+
+ mumps_module.last_token().Xecute = expr_list
+ mumps_module.end_token(pos + 1)
+
+ def parse_zquit(mumps_module, line, startpos=0):
+ """parse the ZQuit command"""
+ #for lack of better info, treat this just like quit?
+ condtok = parse_expr(mumps_module, line, startpos)
+ mumps_module.last_token().Condition = condtok
+ mumps_module.end_token(condtok.end)
+ #raise ParseError(mumps_module, line, "unhandled command", pos)
+
+
+ command_list = {BREAKCMD:parse_break,
+ CLOSECMD:parse_devicecmd,
+ DOCMD:parse_do,
+ FORCMD:parse_for,
+ GOTOCMD:parse_goto,
+ HANGCMD:parse_hang,
+ IFCMD:parse_if,
+ JOBCMD:parse_job,
+ KILLCMD:parse_kill,
+ LOCKCMD:parse_lock,
+ MERGECMD:parse_merge,
+ NEWCMD:parse_new,
+ OPENCMD:parse_devicecmd,
+ QUITCMD:parse_quit,
+ READCMD:parse_read,
+ SETCMD:parse_set,
+ USECMD:parse_devicecmd,
+ VIEWCMD:parse_view,
+ WRITECMD:parse_write,
+ XECUTECMD:parse_xecute,
+ ZQUITCMD:parse_zquit,
+ ZETRAPCMD:parse_goto, # NOTE: this is a guess...
+ COMMENT:parse_comment}
+
+
+ command = MUMPS_RE_DICT["command"]
+ re_match = command.match(line, startpos)
+ cmd = re_match.group("cmd").upper()
+ pos = re_match.end("cmd") + 1
+
+ if cmd in CMD_TOKEN_DICT:
+ toktype = CMD_TOKEN_DICT[cmd]
+ mumps_module.add_token(Token(toktype, startpos))
+
+ if re_match.group("indents"):
+ # count the periods for the nesting level...
+ mumps_module.indent_token(len(re.findall("[.]", \
+ line[startpos:re_match.start("cmd")])))
+ if line[re_match.end("cmd")] == ':':
+ condtok = parse_expr(mumps_module, line, pos)
+ 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)
+ else:
+ raise ParseError(mumps_module, line, "Unknown command", startpos)
+
+ return mumps_module.last_token()
+
+def parseMumps(mumps_module):
+ """ Parse all the code of given Mumps module."""
+ 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")]
+
+ 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),
+ pattern_match = False
+ pos = 0
+ for (pattern, parser, dscr) in pattern_list:
+ if pattern.match(line):
+ pattern_match = True
+ #print "<%s>" % dscr
+ token = parser(mumps_module, line)
+ token.line_no(fileinput.lineno())
+ 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
+ #print "<%s>" % dscr
+ token = parser(mumps_module, line, pos)
+ pos = token.end
+ break
+
+ if not inside_pattern_match:
+ raise ParseError(mumps_module, line, \
+ "no Matching pattern", pos, \
+ fileinput.lineno())
+
+ break
+
+ 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 parseForModules
+ MumpsFile = "fm22.rsa"
+ m2py_dir = ".\\out"
+
+ #Todo: test if dir exists, if not, create it...
+
+ f = open(MumpsFile)
+ s = f.readline()
+ f.close()
+
+ if re.search(r"CACHE FORMAT\^~Format=Cache.S~", s):
+ mods = parseForModules(MumpsFile, m2py_dir, 0)
+ try:
+ for module in mods:
+ parseMumps(module)
+ except ParseError, e:
+ print e.error_msg()
+ else:#todo: does this work ?
+ outputname = re.split(r"\..*$", MumpsFile)[0]
+
+ if __debug__:
+ print outputname
+ try:
+ parseMumps(ModuleInfo(MumpsFile, m2py_dir, outputname))
+ except ParseError, e:
+ print e.error_msg()
Copied: trunk/mumps2py/mumpsCL.py (from rev 1, trunk/mumpsCL.py)
===================================================================
--- trunk/mumps2py/mumpsCL.py (rev 0)
+++ trunk/mumps2py/mumpsCL.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,22 @@
+# Copyright 2008 Patrick Gallot
+
+## This file is part of Mumps2Py.
+##
+## Mumps2Py is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Mumps2Py is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Mumps2Py. If not, see <http://www.gnu.org/licenses/>.
+
+
+#TODO: first order of business, an iterator to use when translating
+# Mumps for-command. Unlike range, a Mumps iteration includes the terminal
+# value. F i:1:1:10 iterates from 1 to 10, inclusive. the equivalent would be
+# range(1, 11)
Copied: trunk/mumps2py/mumps_module.py (from rev 1, trunk/mumps_module.py)
===================================================================
--- trunk/mumps2py/mumps_module.py (rev 0)
+++ trunk/mumps2py/mumps_module.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,86 @@
+# Copyright 2008 Patrick Gallot
+
+## This file is part of Mumps2Py.
+##
+## Mumps2Py is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Mumps2Py is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Mumps2Py. If not, see <http://www.gnu.org/licenses/>.
+""" code relating to a Mumps module, primarily via the Module_info class."""
+import re, fileinput, os.path
+
+class ModuleInfo:
+ """ A ModuleInfo object represents one complete unit of Mumps source code."""
+ def __init__(self, inputfile, outputdir, name, startline = 1, endline = -1):
+ self.input_file = inputfile
+ if os.path.isabs(name):
+ outname = os.path.basename(name)
+ else:
+ outname = name
+ self.output_file = outputdir + "\\" + outname + ".py"
+ self.mod_name = outname
+ self.start = startline
+ self.end = endline
+ self.TokenList = []
+
+ def __str__(self):
+ return "Module %s" % self.mod_name
+
+ def add_token(self, newtoken):
+ """ add a new token to the token list."""
+ self.TokenList.append(newtoken)
+
+ def end_token(self, endpos):
+ """used to indicate where the last token added ended."""
+ self.TokenList[-1].end = endpos
+
+ def indent_token(self, nestlevel):
+ """sets the indent level for the last token added to this module."""
+ self.TokenList[-1].indentlevel = nestlevel
+
+ def post_condition(self, tvexprtoklist):
+ """adds the given post-condition to the last token added to this module"""
+ tok = self.TokenList[-1]
+ tok.post_condition(tvexprtoklist)
+
+ def last_token(self):
+ """returns the last Token added to this module's TokenList"""
+ return self.TokenList[-1]
+
+def parseForModules(inputfile, outputdir):
+ """parseForModules returns a list of ModuleInfo objects from a file
+in Cache Format, assuming that each Mumps module begins
+with a line containing modulename^INT^"""
+ modules = []
+ lastmodline = 2
+ oldmodname = None
+
+ modpattern = re.compile(r"\^INT\^")
+ modsplit = re.compile(r"\^")
+
+ for line in fileinput.input(inputfile):
+ lineno = fileinput.lineno()
+ if lineno < 3:
+ continue
+
+ if modpattern.search(line):
+ tmp = modsplit.split(line, 1)
+ if oldmodname:
+ modules.append(ModuleInfo(inputfile, outputdir, oldmodname, \
+ lastmodline + 1, lineno - 1))
+ if __debug__:
+ print oldmodname, lastmodline + 1, lineno - 1, "\t", \
+ lineno - lastmodline
+ oldmodname = tmp[0]
+ lastmodline = lineno
+
+ fileinput.close()
+ return modules
Copied: trunk/mumps2py/tokens.py (from rev 4, trunk/tokens.py)
===================================================================
--- trunk/mumps2py/tokens.py (rev 0)
+++ trunk/mumps2py/tokens.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,410 @@
+# Copyright 2008 Patrick Gallot
+
+## This file is part of Mumps2Py.
+##
+## Mumps2Py is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Mumps2Py is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Mumps2Py. If not, see <http://www.gnu.org/licenses/>.
+""" All the possible token types that can be extracted from Mumps code"""
+from types import *
+import re
+
+# IMPORTANT: Add a Token Type Constant, update the relevant dictionaries.
+
+# Token Type constants:
+# NOTE : each type of comand should have its own token.
+COMMENT, LABEL, BREAKCMD, CLOSECMD = 1, 2, 3, 4
+DOCMD, ELSECMD, FORCMD, GOTOCMD = 5, 6, 7, 8
+HANGCMD, IFCMD, JOBCMD, KILLCMD = 9, 10, 11, 12
+LOCKCMD, MERGECMD, NEWCMD, OPENCMD = 13, 14, 15, 16
+QUITCMD, READCMD, SETCMD, USECMD = 17, 18, 19, 20
+VIEWCMD, WRITECMD, XECUTECMD, ZQUITCMD = 21, 22, 23, 24
+ZETRAPCMD, EXPR, LOCALVAR, GLOBALVAR = 25, 26, 27, 28
+INTRINSICVAR, INTRINSICFUNC, USERFUNC = 29, 30, 31
+STRINGLITERAL, NUMLITERAL, INDIRECTION = 33, 34, 35
+UNKNOWNITEM, ENTRYREF = 36, 37
+FCC_NEWLINE, FCC_NEWPAGE, FCC_MOV, FCC_CHARVAL = 40, 41, 42, 43
+EMPTYLINE = 45
+
+# NOTE: OPERATORS have token values starting at 50.
+OPADD, OPSUB, OPMULT, OPEXP = 50, 51, 52, 53 # +,-,*, **
+OPFRACDIV, OPINTDIV, OPMODULO = 54, 55, 56 # \,/, #
+OPNOT, OPAND, OPOR = 57, 58, 59 # ',&, !
+OPCONCAT, OPGT, OPLT, OPNGT, OPNLT = 60, 61, 62, 63, 64 # _,>,<,'>,'<
+OPEQ, OPNEQ = 65, 66 # =, '=
+OPCONTAINS, OPFOLLOWS, OPSORTS = 67, 68, 69 # [,], ]]
+OPPATMATCH, OPINDIRECT = 70, 71 # ?, @
+OPPATATOM, OPNAKEDREF = 72, 73
+
+#NOTE: INTRINSIC FUNCs and VARs have token values starting at 100,150 respectively.
+F_ASCII, F_CHAR, F_DATA, F_EXTRACT = 100, 101, 102, 103
+F_FIND, F_FNUMBER, F_GET, F_JUSTIFY = 104, 105, 106, 107
+F_LENGTH, F_NAME, F_NEXT, F_ORDER = 108, 109, 110, 111
+F_PIECE, F_QLENGTH, F_QSUBSCRIPT, F_QUERY = 112, 113, 114, 115
+F_RANDOM, F_REVERSE, F_SELECT, F_STACK = 116, 117, 118, 119
+F_TEXT, F_TRANSLATE, F_VIEW = 120, 121, 122
+F_UNKNOWN = 123
+
+V_DEVICE, V_ECODE, V_ESTACK, V_ETRAP = 150, 151, 152, 153
+V_HOROLOG, V_IO, V_JOB, V_KEY = 154, 155, 156, 157
+V_PRINCIPAL, V_QUIT, V_REFERENCE, V_STACK = 158, 159, 160, 161
+V_STORAGE, V_SYSTEM, V_TEST, V_X, V_Y = 162, 163, 164, 165, 166
+V_ZCSTATUS, V_ZVER, V_ZVERSION, V_ZERROR = 167, 168, 169, 170
+# NOTE: I have no idea what these intrinsic vars are for:
+V_ZJOB, V_ZR, V_ZNSPACE, V_ZH = 171, 172, 173, 174
+V_ZTRAP = 175
+
+OP_TOKEN_DICT = {"+":OPADD, "-":OPSUB,
+ "*":OPMULT, "**":OPEXP,
+ "\\":OPFRACDIV, "/":OPINTDIV,
+ "#": OPMODULO, "'":OPNOT,
+ "&":OPAND, "!":OPOR,
+ ">":OPGT, "<":OPLT,
+ "'>":OPNGT, "'<":OPNLT,
+ "=":OPEQ, "'=":OPNEQ,
+ "[":OPCONTAINS,
+ "]":OPFOLLOWS,
+ "]]":OPSORTS,
+ "?":OPPATMATCH, "@":OPINDIRECT,
+ "_":OPCONCAT}
+
+
+IntrinsicVarDict = {"D":V_DEVICE, "DEVICE":V_DEVICE,
+ "EC":V_ECODE, "ECODE":V_ECODE,
+ "ES":V_ESTACK, "ESTACK":V_ESTACK,
+ "ET":V_ETRAP, "ETRAP":V_ETRAP,
+ "H":V_HOROLOG, "HOROLOG":V_HOROLOG,
+ "I":V_IO, "IO":V_IO,
+ "J":V_JOB, "JOB":V_JOB,
+ "K":V_KEY, "KEY":V_KEY,
+ "P":V_PRINCIPAL, "PRINCIPAL":V_PRINCIPAL,
+ "Q":V_QUIT, "QUIT":V_QUIT,
+ "R":V_REFERENCE, "REFERENCE":V_REFERENCE,
+ "ST":V_STACK, "STACK":V_STACK,
+ "S":V_STORAGE, "STORAGE":V_STORAGE,
+ "SY":V_SYSTEM, "SYSTEM":V_SYSTEM,
+ "T": V_TEST, "TEST":V_TEST,
+ "X":V_X,
+ "Y":V_Y,
+ "ZC":V_ZCSTATUS, "ZCSTATUS":V_ZCSTATUS,
+ "ZE":V_ZERROR, "ZERROR":V_ZERROR,
+ "ZJ":V_ZJOB, "ZJOB": V_ZJOB,
+ "ZVER":V_ZVER,
+ "ZR":V_ZR,
+ "ZNSPACE":V_ZNSPACE,
+ "ZH":V_ZH,
+ "ZT":V_ZTRAP, "ZTRAP":V_ZTRAP,
+ "ZV":V_ZVERSION, "ZVERSION":V_ZVERSION}
+
+IntrinsicFuncDict = {"A":F_ASCII, "ASCII":F_ASCII,
+ "C":F_CHAR, "CHAR":F_CHAR,
+ "D":F_DATA, "DATA":F_DATA,
+ "E":F_EXTRACT, "EXTRACT":F_EXTRACT,
+ "F":F_FIND, "FIND":F_FIND,
+ "FN":F_FNUMBER, "FNUMBER":F_FNUMBER,
+ "G":F_GET, "GET":F_GET,
+ "J":F_JUSTIFY, "JUSTIFY":F_JUSTIFY,
+ "L":F_LENGTH, "LENGTH":F_LENGTH,
+ "NA":F_NAME, "NAME":F_NAME,
+ "O":F_ORDER, "ORDER":F_ORDER,
+ "P":F_PIECE, "PIECE":F_PIECE,
+ "QL":F_QLENGTH, "QLENGTH":F_QLENGTH,
+ "QS":F_QSUBSCRIPT, "QSUBSCRIPT":F_QSUBSCRIPT,
+ "Q":F_QUERY, "QUERY":F_QUERY,
+ "R":F_RANDOM, "RANDOM": F_RANDOM,
+ "RE": F_REVERSE, "REVERSE":F_REVERSE,
+ "S":F_SELECT, "SELECT":F_SELECT,
+ "ST":F_STACK, "STACK":F_STACK,
+ "T":F_TEXT, "TEXT":F_TEXT,
+ "TR":F_TRANSLATE, "TRANSLATE":F_TRANSLATE,
+ "V":F_VIEW, "VIEW":F_VIEW,
+ "ZB":F_UNKNOWN,
+ "ZC":F_UNKNOWN,
+ "ZCONVERT":F_UNKNOWN,
+ "ZU":F_UNKNOWN,
+ "zjob":F_UNKNOWN}
+
+CMD_TOKEN_DICT = {"B":BREAKCMD, "BREAK":BREAKCMD,
+ "C":CLOSECMD, "CLOSE":CLOSECMD,
+ "D":DOCMD, "DO":DOCMD,
+ "E":ELSECMD, "ELSE":ELSECMD, # note, no arguments to the else command, thus no parser.
+ "F":FORCMD, "FOR":FORCMD,
+ "G":GOTOCMD, "GOTO":GOTOCMD,
+ "H":HANGCMD, "HANG":HANGCMD, "HALT":HANGCMD,
+ "I":IFCMD, "IF":IFCMD,
+ "J":JOBCMD, "JOB":JOBCMD,
+ "K":KILLCMD, "KILL":KILLCMD,
+ "L":LOCKCMD, "LOCK":LOCKCMD,
+ "M":MERGECMD, "MERGE":MERGECMD,
+ "N":NEWCMD, "NEW":NEWCMD,
+ "O":OPENCMD, "OPEN":OPENCMD,
+ "Q":QUITCMD, "QUIT":QUITCMD,
+ "R":READCMD, "READ":READCMD,
+ "S":SETCMD, "SET":SETCMD,
+ "U":USECMD, "USE":USECMD,
+ "V":VIEWCMD, "VIEW":VIEWCMD,
+ "W":WRITECMD, "WRITE":WRITECMD,
+ "X":XECUTECMD, "XECUTE":XECUTECMD,
+ "ZQ":ZQUITCMD, "ZQUIT":ZQUITCMD,
+ "ZETRAP":ZETRAPCMD, # not sure what this is exactly...
+ ";":COMMENT}
+
+TOKEN_REVERSE_DICT = {
+ V_DEVICE: "$DEVICE",
+ V_ECODE: "$ECODE",
+ V_ESTACK: "$ESTACK",
+ V_ETRAP: "$ETRAP",
+ V_HOROLOG: "$HOROLOG",
+ V_IO:"$IO",
+ V_JOB:"$JOB",
+ V_KEY:"$KEY",
+ V_PRINCIPAL:"$PRINCIPAL",
+ V_QUIT:"$QUIT",
+ V_REFERENCE:"$REFERENCE",
+ V_STACK:"$STACK",
+ V_STORAGE:"$STORAGE",
+ V_SYSTEM:"$SYSTEM",
+ V_TEST:"$TEST",
+ V_X:"$X",
+ V_Y:"$Y",
+ V_ZCSTATUS:"$ZCSTATUS",
+ V_ZERROR:"$ZERROR",
+ V_ZJOB:"$ZJOB",
+ V_ZVER:"Unknown Intrinsic variable",
+ V_ZR:"Unknown Intrinsic variable",
+ V_ZNSPACE:"Unknown Intrinsic variable",
+ V_ZH:"Unknown Intrinsic variable",
+ V_ZTRAP:"$ZTRAP",
+ V_ZVERSION:"$ZVERSION",
+ F_ASCII:"$ASCII",
+ F_CHAR:"$CHAR",
+ F_DATA:"$DATA",
+ F_EXTRACT:"$EXTRACT",
+ F_FIND:"$FIND",
+ F_FNUMBER:"$FNUMBER",
+ F_GET:"$GET",
+ F_JUSTIFY:"$JUSTIFY",
+ F_LENGTH:"$LENGTH",
+ F_NAME:"$NAME",
+ F_NEXT:"$NEXT",
+ F_ORDER:"$ORDER",
+ F_PIECE:"$PIECE",
+ F_QLENGTH:"$QLENGTH",
+ F_QSUBSCRIPT:"$QSUBSCRIPT",
+ F_QUERY:"$QUERY",
+ F_RANDOM:"$RANDOM",
+ F_REVERSE:"$REVERSE",
+ F_SELECT:"$SELECT",
+ F_STACK:"$STACK",
+ F_TEXT:"$TEXT",
+ F_TRANSLATE:"$TRANSLATE",
+ F_VIEW:"$VIEW",
+ F_UNKNOWN:"Unknown Intrinsic function",
+ OPADD:"+",
+ OPSUB:"-",
+ OPMULT:"*",
+ OPEXP:"**",
+ OPFRACDIV:"\\",
+ OPINTDIV:"/",
+ OPMODULO:"#",
+ OPNOT:"'",
+ OPAND:"AND",
+ OPOR:"OR",
+ OPCONCAT:"_",
+ OPGT:">",
+ OPLT:"<",
+ OPNGT:"'>",
+ OPNLT:"'<",
+ OPEQ:"=",
+ OPNEQ:"'=",
+ OPCONTAINS:"[",
+ OPFOLLOWS:"]",
+ OPSORTS:"]]",
+ OPPATMATCH:"?",
+ OPINDIRECT:"@",
+ OPPATATOM:"Pattern Atom",
+ OPNAKEDREF:"^(",
+ COMMENT:";",
+ LABEL:"Label",
+ BREAKCMD:"BREAK",
+ CLOSECMD:"CLOSE",
+ DOCMD:"DO",
+ ELSECMD:"ELSE",
+ FORCMD:"FOR",
+ GOTOCMD:"GOTO",
+ HANGCMD:"HANG",
+ IFCMD:"IF",
+ JOBCMD:"JOB",
+ KILLCMD:"KILL",
+ LOCKCMD:"LOCK",
+ MERGECMD:"MERGE",
+ NEWCMD:"NEW",
+ OPENCMD:"OPEN",
+ QUITCMD:"QUIT",
+ READCMD:"READ",
+ SETCMD:"SET",
+ USECMD:"USE",
+ VIEWCMD:"VIEW",
+ WRITECMD:"WRITE",
+ XECUTECMD:"XECUTE",
+ ZQUITCMD:"Unknown command",
+ ZETRAPCMD:"Unknown command",
+ EXPR:"Expression",
+ LOCALVAR:"Local Variable",
+ GLOBALVAR:"Global Variable",
+ INTRINSICVAR:"Intrinsic Variable",
+ INTRINSICFUNC:"Intrinsic Function",
+ USERFUNC:"User Function",
+ STRINGLITERAL:"String",
+ NUMLITERAL:"Number",
+ INDIRECTION:"Indirection Expression",
+ UNKNOWNITEM:"Unknown Item",
+ ENTRYREF:"Entry Reference",
+ LOCALVAR:"Local Var",
+ EMPTYLINE:"",
+ FCC_NEWLINE:"!",
+ FCC_NEWPAGE:"#",
+ FCC_MOV:"mov",
+ FCC_CHARVAL:"asc",
+ }
+
+
+def count_subtokens(count_dict, token):
+ """counts the differen types of tokens and subtokens."""
+ token_key = TOKEN_REVERSE_DICT.get(token.toktype,token.toktype)
+
+ for var in [key for key in token.__dict__.keys() if key \
+ not in ('start', 'end', 'toktype', 'lineno')]:
+
+ if isinstance(token.__dict__[var], DictType):
+ for (key, val) in token.__dict__[var].items():
+ if isinstance(val, Token):
+ count_subtokens(count_dict, val)
+ elif isinstance(token.__dict__[var], ListType):
+ for val in token.__dict__[var]:
+ if isinstance(val, DictType):
+ for (key, dval) in val.items():
+ if isinstance(dval, Token):
+ count_subtokens(count_dict, dval)
+ elif isinstance(val, Token):
+ count_subtokens(count_dict, val)
+ elif isinstance(token.__dict__[var], Token):
+ count_subtokens(count_dict, token.__dict__[var])
+
+ count_dict[token_key] = count_dict.get(token_key, 0) + 1
+
+
+class Token:
+ """ container for all the different types of MUMPS tokens."""
+ def __init__(self, tok_type, startpos):
+ """ initializes a Token object of a given type."""
+ self.toktype = tok_type
+ self.start = startpos
+ self.end = None
+ self.lineno = None
+ self.indentlevel = 0
+ self.post_condition_expr = None
+ self.params = None
+ self.val = None
+ self.varname = None
+ self.timeout = None
+ self.expr = None
+ self.minrep = None
+ self.maxrep = None
+ self.repcount = None
+ self.indirect = None
+
+ def __str__(self):
+ """returns a string expression of a token contents and subtokens"""
+ if TOKEN_REVERSE_DICT.has_key(self.toktype):
+ if self.lineno:
+ dscr = str(self.lineno) + ' ' + TOKEN_REVERSE_DICT[self.toktype]
+ else:
+ dscr = '\t ' + TOKEN_REVERSE_DICT[self.toktype]
+
+ for var in [key for key in self.__dict__.keys() \
+ if key not in \
+ ('start', 'end', 'toktype', 'lineno')]:
+ if isinstance(self.__dict__[var], DictType):
+ dscr = dscr + ' ' + str(var) + ':{'
+ for (key, val) in self.__dict__[var].items():
+ dscr = dscr + str(key)+':'+str(val)+' '
+ dscr = dscr+'} '
+ elif isinstance(self.__dict__[var], ListType):
+ dscr = "%s %s:%d( " % (dscr, str(var),
+ len(self.__dict__[var]))
+ for val in self.__dict__[var]:
+ if isinstance(val, DictType):
+ dscr = dscr + ' {'
+ for (key, dval) in val.items():
+ dscr = dscr + str(key)+':'+str(dval)+' '
+ dscr = dscr+'} '
+ else:
+ dscr = dscr + str(val) + ', '
+ dscr = dscr+') '
+ elif self.__dict__[var]: # skip over any 'None' values.
+ dscr = dscr + ' ' + str(var) + ':' + str(self.__dict__[var])
+
+ else:
+ dscr = str(self.toktype)
+
+ return dscr
+
+ def post_condition(self, tv_expr):
+ """set the token's post-condition expression."""
+ self.post_condition_expr = tv_expr
+
+ 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.
+and there is a dependency on this behavior in tokprepass."""
+ self.lineno = lineno
+
+ def is_str(self):
+ """returns true if the token is a string literal"""
+ return (self.toktype == STRINGLITERAL)
+
+ def is_num(self):
+ """returns true if the token is a numeric literal"""
+ return (self.toktype == NUMLITERAL)
+
+ def is_int(self):
+ """returns true if a numeric literal has no decimal"""
+ if (self.toktype == NUMLITERAL):
+ return not re.search(r"[.]",self.val)
+ else:
+ return False
+
+ def is_not(self):
+ """returns true if the token is the not operator"""
+ return (self.toktype == OPNOT)
+
+ def is_expr(self):
+ """returns true if the token is an expression"""
+ return (self.toktype == EXPR)
+
+ def is_unaryop(self):
+ """returns true if the token is a unary operator"""
+
+ return (self.toktype in (OPADD, OPSUB, OPNOT))
+
+ def is_binaryop(self):
+ """returns true if the token is a simple binary operator"""
+ return (self.toktype in (OPADD, OPSUB, OPMULT, OPEXP,
+ OPFRACDIV, OPINTDIV, OPMODULO,
+ OPGT, OPLT, OPNGT, OPNLT,
+ OPEQ, OPNEQ, OPAND, OPOR, OPCONCAT,
+ OPCONTAINS, OPFOLLOWS, OPSORTS, OPPATMATCH))
+
+ def count_subtokens(self, count_dict):
+ """counts the different types of (sub)tokens including itself."""
+ return count_subtokens(count_dict, self)
Copied: trunk/mumps2py/tokprepass.py (from rev 1, trunk/tokprepass.py)
===================================================================
--- trunk/mumps2py/tokprepass.py (rev 0)
+++ trunk/mumps2py/tokprepass.py 2008-01-17 17:53:51 UTC (rev 6)
@@ -0,0 +1,49 @@
+# Copyright 2008 Patrick Gallot
+
+## This file is part of Mumps2Py.
+##
+## Mumps2Py is free software: you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## Mumps2Py is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Mumps2Py. If not, see <http://www.gnu.org/licenses/>.
+
+"""
+tokprepass contains code for massaging the intermediate tokens before
+translating a module into Python.
+"""
+
+from mumps_module import ModuleInfo
+from tokens import *
+
+def fix_indent_levels(a_mumps_module):
+ """fixing the indent levels for FOR,IF, ELSE commands"""
+ new_indentlevel = 0
+ next_indentlevel = 0
+ for token in a_mumps_module.TokenList:
+ if token.lineno: # starting a new line...
+ new_indentlevel = token.indentlevel
+ next_indentlevel = token.indentlevel
+ if token.toktype in (FORCMD, IFCMD, ELSECMD):
+ # the scope of these commands is the remainder of the line
+ # so the remainder of the line is a nested block of code.
+ # that's the theory anyway.
+ next_indentlevel = new_indentlevel + 1
+
+ token.indentlevel = new_indentlevel
+ new_indentlevel = next_indentlevel
+## print "current indent level: %d, next command indent level: %d" % \
+## (token.indentlevel, new_indentlevel)
+
+
+def prepass(a_mumps_module):
+ """ Calls various functions to massage the tokens within a module."""
+
+ fix_indent_levels(a_mumps_module)