diff python/ppci/errors.py @ 148:e5263f74b287

Added c3 language frontend initial parser
author Windel Bouwman
date Fri, 01 Mar 2013 10:24:01 +0100
parents 4e79484a9d47
children b73bc14a3aa3
line wrap: on
line diff
--- a/python/ppci/errors.py	Fri Feb 22 10:33:48 2013 +0100
+++ b/python/ppci/errors.py	Fri Mar 01 10:24:01 2013 +0100
@@ -1,15 +1,21 @@
-""" Error handling routines """
+""" 
+   Error handling routines 
+   Diagnostic utils
+"""
+
+from collections import namedtuple
+
+SourceLocation = namedtuple('SourceLocation', ['row', 'col'])
+SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
 
 class CompilerException(Exception):
-  def __init__(self, msg, row=0, col=0, filename=None):
+  def __init__(self, msg, loc):
     self.msg = msg
-    self.row = row
-    self.col = col
-    self.filename = filename
+    self.loc = loc
   def __repr__(self):
-     return self.msg
+     return 'error {0} at {1}'.format(self.msg, self.loc)
   def __str__(self):
-     return self.msg
+     return 'error {0} at {1}'.format(self.msg, self.loc)
 
 class ErrorNode:
    def __init__(self, row, col, msg):
@@ -25,23 +31,31 @@
 def printError(source, e):
      def printLine(row, txt):
         print(str(row)+':'+txt)
-     if e.row == 0:
+     if e.loc.row == 0:
         print('Error: {0}'.format(e.msg))
      else:
         lines = source.split('\n')
-        prerow = e.row - 3
+        ro, co = e.loc.row, e.loc.col
+        prerow = ro - 2
         if prerow < 1:
            prerow = 1
-        afterrow = e.row + 3
+        afterrow = ro + 3
         if afterrow > len(lines):
            afterrow = len(lines)
 
         # print preceding source lines:
-        for r in range(prerow, e.row):
+        for r in range(prerow, ro):
            printLine(r, lines[r-1])
         # print source line containing error:
-        printLine(e.row, lines[e.row-1])
-        print(' '*(len(str(e.row)+':')+e.col-1) + '^ Error: {0}'.format(e.msg))
+        printLine(ro, lines[ro-1])
+        print(' '*(len(str(ro)+':')+co-1) + '^ Error: {0}'.format(e.msg))
         # print trailing source line:
-        for r in range(e.row+1, afterrow+1):
+        for r in range(ro+1, afterrow+1):
           printLine(r, lines[r-1])
+
+class Diagnostics:
+   def __init__(self):
+      self.diags = []
+   def diag(self, d):
+      self.diags.append(d)
+