changeset 420:ce644d724d87

Made Location a class and extended it with new members.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Fri, 28 Sep 2007 13:08:51 +0200
parents 89e40d43065d
children 1be8eaf4b5b0
files trunk/src/dil/Lexer.d
diffstat 1 files changed, 42 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/Lexer.d	Fri Sep 28 12:10:11 2007 +0200
+++ b/trunk/src/dil/Lexer.d	Fri Sep 28 13:08:51 2007 +0200
@@ -26,17 +26,50 @@
 
 const uint _Z_ = 26; /// Control+Z
 
-struct Location
+final class Location
 {
   size_t lineNum;
   char* filePath;
+  char* from, to; // Used to calculate column.
 
-  static Location opCall(size_t lineNum, typeof(filePath) filePath)
+  this(size_t lineNum, char* filePath)
+  {
+    this(lineNum, filePath, null, null);
+  }
+
+  this(size_t lineNum, char* filePath, char* from, char* to)
+  {
+    assert(from <= to);
+    this.lineNum  = lineNum;
+    this.filePath = filePath;
+    this.from     = from;
+    this.to       = to;
+  }
+
+  uint getColumn()
   {
-    Location l;
-    l.lineNum = lineNum;
-    l.filePath = filePath;
-    return l;
+    uint col = 1;
+    auto p = from;
+    for (; p <= to; ++p)
+    {
+      assert(*p != '\n' && *p != '\r');
+      if (*p & 128)
+      {
+        size_t idx;
+        dchar d;
+        try
+        {
+          d = std.utf.decode(p[0 .. to-p], idx);
+          assert(d != LSd && d != PSd);
+          p += idx -1;
+        }
+        catch (UtfException e)
+        { }
+      }
+
+      ++col;
+    }
+    return col;
   }
 }
 
@@ -93,7 +126,7 @@
     // Add first token to table.
     auto firstToken = this.head;
     peek(firstToken);
-    token2LocTable[firstToken] = Location(1, null);
+    token2LocTable[firstToken] = new Location(1, null);
   }
   }
 
@@ -248,7 +281,7 @@
       t.ws = pws;
       if (old_loc != this.loc)
         version(token2LocTable)
-          token2LocTable[&t] = Location(loc, null);
+          token2LocTable[&t] = new Location(loc, null);
     }
 
     // Scan token.
@@ -729,7 +762,7 @@
       t.ws = pws;
       if (old_loc != this.loc)
         version(token2LocTable)
-          token2LocTable[&t] = Location(loc, null);
+          token2LocTable[&t] = new Location(loc, null);
     }
 
     // Scan token.