Annotation work
authorRobert Bradshaw <robertwb@math.washington.edu>
Tue, 15 Jan 2008 21:21:14 +0000 (13:21 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Tue, 15 Jan 2008 21:21:14 +0000 (13:21 -0800)
Cython/Compiler/Annotate.py
Cython/Compiler/Code.py
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py

index 278ca8e605ff3e940dce3e4e3a80082761ceb35b..e1f05e491a82f0da082f482a1b438276d4d72829 100644 (file)
@@ -7,6 +7,10 @@ from StringIO import StringIO
 
 from Code import CCodeWriter
 
+# need one-characters subsitutions (for now) so offsets aren't off
+special_chars = [('<', '\xF0', '&lt;'),
+                 ('>', '\xF1', '&gt;'), 
+                 ('&', '\xF2', '&amp;')]
 
 class AnnotationCCodeWriter(CCodeWriter):
 
@@ -29,14 +33,13 @@ class AnnotationCCodeWriter(CCodeWriter):
 #        if pos is not None:
 #            CCodeWriter.mark_pos(self, pos)
 #        return
-        print "marking", pos
         if self.last_pos:
             try:
                 code = self.code[self.last_pos[1]]
             except KeyError:
                 code = ""
             self.code[self.last_pos[1]] = code + self.buffer.getvalue()
-            self.buffer = StringIO()
+        self.buffer = StringIO()
         self.last_pos = pos
 
     def annotate(self, pos, item):
@@ -47,12 +50,10 @@ class AnnotationCCodeWriter(CCodeWriter):
         f = open(filename)
         lines = f.readlines()
         for k in range(len(lines)):
-            # there has to be a better way to do this
-            lines[k] = lines[k].replace('  ','\t ')
-            lines[k] = lines[k].replace('  ','\t\t')
-            # TODO: this is incorrect
-            lines[k] = lines[k].replace('<', '~')
-            lines[k] = lines[k].replace('>', '~')
+            line = lines[k]
+            for c, cc, html in special_chars:
+                line = line.replace(c, cc)
+            lines[k] = line
         f.close()
         all = []
         for pos, item in self.annotations:
@@ -86,6 +87,7 @@ body { font-family: courier; font-size: 12; }
 .py_api  { color: red; }
 .pyx_api  { color: #FF3000; }
 .py_macro_api  { color: #FF8000; }
+.error_goto  { color: #FF8000; }
 
 .tag  {  }
 
@@ -97,6 +99,8 @@ body { font-family: courier; font-size: 12; }
 .py_call { color: #FF0000; font-weight: bold; }
 .c_call  { color: #0000FF; }
 
+.line { margin: 0em }
+
 </style>
 <script>
 function toggleDiv(id) {
@@ -113,6 +117,7 @@ function toggleDiv(id) {
         py_c_api = re.compile('(Py[A-Z][a-z]+_[A-Z][a-z][A-Za-z_]+)')
         pyx_api = re.compile('(__Pyx[A-Za-z_]+)\(')
         py_marco_api = re.compile('(Py[A-Za-z]*_[A-Z][A-Z_]+)')
+        error_goto = re.compile(r'(if .*? \{__pyx_filename = .*goto __pyx_L\w+;\})')
         
         for line in lines:
 
@@ -125,16 +130,18 @@ function toggleDiv(id) {
             code, c_api_calls = py_c_api.subn(r"<span class='py_api'>\1</span>", code)
             code, pyx_api_calls = pyx_api.subn(r"<span class='pyx_api'>\1</span>(", code)
             code, macro_api_calls = py_marco_api.subn(r"<span class='py_macro_api'>\1</span>", code)
+            code, error_goto_calls = error_goto.subn(r"<span class='error_goto'>\1</span>", code)
             
             color = "FFFF%02x" % int(255/(1+(5*c_api_calls+2*pyx_api_calls+macro_api_calls)/10.0))
-            f.write("<div class='line' style='background-color: #%s' onclick='toggleDiv(\"line%s\")'>" % (color, k))
+            f.write("<pre class='line' style='background-color: #%s' onclick='toggleDiv(\"line%s\")'>" % (color, k))
 
             f.write(" %d: " % k)
-            line = line.replace('\t', '&nbsp;')
-            f.write(line)
+            for c, cc, html in special_chars:
+                line = line.replace(cc, html)
+            f.write(line.rstrip())
                 
-            f.write('</div>\n')
-            f.write("<div id='line%s' class='code' style='background-color: #%s'>%s</div>" % (k, color, code.replace('\n', '\n<br>')))
+            f.write('</pre>\n')
+            f.write("<pre id='line%s' class='code' style='background-color: #%s'>%s</pre>" % (k, color, code))
         f.write('</body></html>\n')
         f.close()
         
index 9dabc3aac5dce2d8ecbb9789a49d035a4347a516..ea5fcb6ce625ad9ecad43b22a52406396327b8b1 100644 (file)
@@ -98,6 +98,8 @@ class CCodeWriter:
         return "0x%02X%02X%02X%02X" % (tuple(pyversion) + (0,0,0,0))[:4]
 
     def mark_pos(self, pos):
+        if pos is None:
+            return
         file, line, col = pos
         contents = self.file_contents(file)
 
index 05befd1bba2df0066760019fbcf28e49b9b523e8..883f5c18730471000ac7d9780f039997a50863e4 100644 (file)
@@ -217,12 +217,14 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         self.generate_py_string_decls(env, code)
         self.generate_cached_builtins_decls(env, code)
         self.body.generate_function_definitions(env, code)
+        code.mark_pos(None)
         self.generate_interned_name_table(env, code)
         self.generate_py_string_table(env, code)
         self.generate_typeobj_definitions(env, code)
         self.generate_method_table(env, code)
         self.generate_filename_init_prototype(code)
         self.generate_module_init_func(modules[:-1], env, code)
+        code.mark_pos(None)
         self.generate_module_cleanup_func(env, code)
         self.generate_filename_table(code)
         self.generate_utility_functions(env, code)
@@ -1196,6 +1198,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
                 "};")
     
     def generate_interned_name_table(self, env, code):
+        code.mark_pos(None)
         items = env.intern_map.items()
         if items:
             items.sort()
@@ -1280,6 +1283,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
             self.generate_type_import_code_for_module(module, env, code)
 
         code.putln("/*--- Execution code ---*/")
+        code.mark_pos(None)
         self.body.generate_execution_code(code)
         
         if Options.generate_cleanup_code:
index 7f9807cb5f45ba72055c6f82a5c39ada57981a5f..d55b2faa413f15ae79f64e2c9f50d01cc91c7356 100644 (file)
@@ -88,8 +88,6 @@ class Node:
         # mro does the wrong thing
         if isinstance(self, BlockNode):
             self.body.annotate(code)
-        else:
-            print "skipping", self
 
 
 class BlockNode: