changeset 412:fb31af0fda73

Added struct Location, and token2LocTable to Lexer. The token2LocTable member could be used to give the column of where a parser or lexer error occurred. And maybe it could be useful for statistics, too.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 25 Sep 2007 16:44:27 +0200
parents cca83c0c00fd
children 0fd78fdcb982
files trunk/src/dil/Lexer.d
diffstat 1 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/Lexer.d	Tue Sep 25 15:04:41 2007 +0200
+++ b/trunk/src/dil/Lexer.d	Tue Sep 25 16:44:27 2007 +0200
@@ -26,6 +26,20 @@
 
 const uint _Z_ = 26; /// Control+Z
 
+struct Location
+{
+  size_t lineNum;
+  char* filePath;
+
+  static Location opCall(size_t lineNum, typeof(filePath) filePath)
+  {
+    Location l;
+    l.lineNum = lineNum;
+    l.filePath = filePath;
+    return l;
+  }
+}
+
 class Lexer
 {
   Token* head; /// The head of the doubly linked token list.
@@ -49,6 +63,10 @@
 
   Identifier[string] idtable;
 
+  version(token2LocTable)
+    /// Maps every token that starts a new line to a Location.
+    Location[Token*] token2LocTable;
+
   this(string text, string fileName)
   {
     this.fileName = fileName;
@@ -69,6 +87,13 @@
     this.head.type = TOK.HEAD;
     this.token = this.head;
     scanShebang();
+  version(token2LocTable)
+  {
+    // Add first token to table.
+    auto firstToken = this.head;
+    peek(firstToken);
+    token2LocTable[firstToken] = Location(1, null);
+  }
   }
 
   ~this()
@@ -184,13 +209,14 @@
       case '\n':
         ++p;
         ++loc;
+      version(token2LocTable)
+        token2LocTable[&t] = Location(loc, null);
         continue;
       case LS[0]:
         if (p[1] == LS[1] && (p[2] == LS[2] || p[2] == PS[2]))
         {
-          p += 3;
-          ++loc;
-          continue;
+          ++p; ++p;
+          goto case '\n';
         }
         // goto default;
       default: