[Mumps2Py:] [156] added code to colorize the mumps routine for easier comprehension.

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


Revision: 156
Author:   pgallot
Date:     2008-03-04 18:00:38 +0000 (Tue, 04 Mar 2008)

Log Message:
-----------
added code to colorize the mumps routine for easier comprehension.

Modified Paths:
--------------
    trunk/mumps2py_ui.pyw


Modified: trunk/mumps2py_ui.pyw
===================================================================
--- trunk/mumps2py_ui.pyw	2008-03-04 17:58:41 UTC (rev 155)
+++ trunk/mumps2py_ui.pyw	2008-03-04 18:00:38 UTC (rev 156)
@@ -21,6 +21,9 @@
 import mumps2py
 import mumps2py_test
 
+from mumps2py.tokens import LABEL, COMMENT, TEXTVAL, EMPTYLINE, ENTRYREF, \
+     STRINGLITERAL, NUMLITERAL
+
 from Tkinter import *
 import tkFileDialog
 
@@ -154,6 +157,50 @@
             self.before_window.insert(END, line)
         fileinput.close()
 
+    def colorize_routine(self):
+        """ colorize the current module in the 'before' window"""
+        if not self.current_module:
+            return
+
+       
+        textwin = self.before_window
+        the_module = self.current_module
+        tag = {LABEL:'label', COMMENT:'comment', TEXTVAL:'comment',
+               STRINGLITERAL:'string', NUMLITERAL: 'number',
+               ENTRYREF:'entryref'}
+        sub_token_list = (STRINGLITERAL, NUMLITERAL, ENTRYREF)
+
+        textwin.tag_configure('comment', foreground = '#008000')
+        textwin.tag_configure('command', foreground = '#C00000')
+        textwin.tag_configure('string', foreground = '#004080')
+        textwin.tag_configure('number', foreground = '#0080C0')
+        textwin.tag_configure('label', foreground = '#F08040')
+        textwin.tag_configure('entryref', foreground = '#C080C0')
+        
+        mod_start = the_module.start - 1
+        
+        for token in the_module.tokenized():
+            line = token.lineno - mod_start
+            if token.toktype in (LABEL, COMMENT, TEXTVAL):
+                idx_start = "%d.%d" % (line, token.start)
+                idx_end = "%d.%d" % (line, token.end)
+                textwin.tag_add(tag[token.toktype], idx_start, idx_end)
+            elif token.toktype != EMPTYLINE:
+                
+                idx_start = "%d.%d" % (line, token.cmd_start)
+                idx_end = "%d.%d" % (line, token.cmd_end)
+                textwin.tag_add('command', idx_start, idx_end)
+                
+                sub_tokens = {}
+                token.extract_subtokens(sub_tokens, sub_token_list )
+
+                for toktype in sub_token_list:
+                    if sub_tokens.get(toktype, None):
+                        for sub_token in sub_tokens[toktype]:
+                            idx_start = "%d.%d" % (line, sub_token.start)
+                            idx_end = "%d.%d" % (line, sub_token.end)
+                            textwin.tag_add(tag[toktype], idx_start, idx_end)
+                
     def __display_after(self, translation):
         """populate the after window with the translation of the Mumps module"""
         self.after_window.delete(0.0, END)
@@ -163,9 +210,10 @@
     def process_current_routine(self):
         """ parse and translate the current routine"""
         the_module = self.current_module
-        self.__display_before(the_module)
+        self.__display_before()
         try:
             mumps2py.parse_routine(the_module)
+            self.colorize_routine()
             translation = mumps2py.translate(the_module)
             self.__display_after(translation)
         except mumps2py.ParseError, err:
@@ -221,8 +269,6 @@
         else:
             return
 
-        from mumps2py.tokens import LABEL
-        
         title = the_module.mod_name + " labels"
         rpt_window = self.info_window(title)
         for token in the_module.tokenized():
@@ -238,8 +284,6 @@
         else:
             return
 
-        from mumps2py.tokens import ENTRYREF
-        
         title = the_module.mod_name + " entryrefs"
         rpt_window = self.info_window(title)
         entryrefs = {}
@@ -255,8 +299,6 @@
         if not self.current_module or not self.modules:
             return
 
-        from mumps2py.tokens import ENTRYREF
-        
         title = "routines referring to %s" % self.current_module.mod_name
         rpt_window = self.info_window(title)
         ref_dict = {}
@@ -453,6 +495,7 @@
             self.__display_before(self.current_module)
         else:
             self.current_module = None
+            self.list_mods()
                 
     def file_save(self):
         """ saves what is in the After window to a file."""


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