changeset 607:2ed1e6d638cd

Making use of struct NewlineData.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 06 Jan 2008 15:23:40 +0100
parents e98d659f1c29
children fac9e8b258fc
files trunk/src/dil/lexer/Lexer.d trunk/src/dil/lexer/Token.d
diffstat 2 files changed, 39 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/lexer/Lexer.d	Sun Jan 06 01:19:29 2008 +0100
+++ b/trunk/src/dil/lexer/Lexer.d	Sun Jan 06 15:23:40 2008 +0100
@@ -38,23 +38,24 @@
   // Members used for error messages:
   InfoManager infoMan;
   LexerError[] errors;
-  /// Always points to the beginning of the current line.
+  /// Always points to the first character of the current line.
   char* lineBegin;
 //   Token* newline;     /// Current newline token.
   uint lineNum = 1;   /// Current, actual source text line number.
   uint lineNum_hline; /// Line number set by #line.
   uint inTokenString; /// > 0 if inside q{ }
-  char[] errorPath;   /// The path displayed in error messages.
+  NewlineData.FilePaths* filePaths;
 
   /++
     Construct a Lexer object.
     Params:
       text     = the UTF-8 source code.
       filePath = the path to the source code; used for error messages.
+      infoMan  = the information manager (for collecting error messages.)
   +/
   this(string text, string filePath, InfoManager infoMan = null)
   {
-    this.filePath = this.errorPath = filePath;
+    this.filePath = filePath;
     this.infoMan = infoMan;
 
     this.text = text;
@@ -72,14 +73,16 @@
     this.head.type = TOK.HEAD;
     this.head.start = this.head.end = this.p;
     this.token = this.head;
+    // Initialize this.filePaths.
+    newFilePath(this.filePath);
     // Add a newline as the first token after the head.
     auto newline = new Token;
     newline.type = TOK.Newline;
     newline.setWhitespaceFlag();
     newline.start = newline.end = this.p;
-    newline.filePath = this.errorPath;
-    newline.lineNum = 1;
-    newline.lineNum_hline = 0;
+    newline.newline.filePaths = this.filePaths;
+    newline.newline.oriLineNum = 1;
+    newline.newline.setLineNum = 0;
     // Link in.
     this.token.next = newline;
     newline.prev = this.token;
@@ -127,7 +130,7 @@
     switch (t.type)
     {
     case TOK.FILE:
-      t.str = this.errorPath;
+      t.str = this.filePaths.setPath;
       break;
     case TOK.LINE:
       t.uint_ = this.errorLineNumber(this.lineNum);
@@ -162,6 +165,15 @@
     }
   }
 
+  /// Sets a new file path.
+  void newFilePath(char[] newPath)
+  {
+    auto paths = new NewlineData.FilePaths;
+    paths.oriPath = this.filePath;
+    paths.setPath = newPath;
+    this.filePaths = paths;
+  }
+
   private void setLineBegin(char* p)
   {
     // Check that we can look behind one character.
@@ -257,9 +269,9 @@
 //         this.newline = &t;
         t.type = TOK.Newline;
         t.setWhitespaceFlag();
-        t.filePath = this.errorPath;
-        t.lineNum = lineNum;
-        t.lineNum_hline = lineNum_hline;
+        t.newline.filePaths = this.filePaths;
+        t.newline.oriLineNum = lineNum;
+        t.newline.setLineNum = lineNum_hline;
         t.end = p;
         return;
       default:
@@ -712,9 +724,9 @@
 //       this.newline = &t;
       t.type = TOK.Newline;
       t.setWhitespaceFlag();
-      t.filePath = this.errorPath;
-      t.lineNum = lineNum;
-      t.lineNum_hline = lineNum_hline;
+      t.newline.filePaths = this.filePaths;
+      t.newline.oriLineNum = lineNum;
+      t.newline.setLineNum = lineNum_hline;
       t.end = p;
       return;
     default:
@@ -2348,7 +2360,7 @@
     {
       this.lineNum_hline = this.lineNum - t.tokLineNum.uint_ + 1;
       if (t.tokLineFilespec)
-        this.errorPath = t.tokLineFilespec.str;
+        newFilePath(t.tokLineFilespec.str);
     }
     t.end = p;
 
@@ -2400,6 +2412,7 @@
               TypeInfo[] _arguments, void* _argptr)
   {
     lineNum = this.errorLineNumber(lineNum);
+    auto errorPath = this.filePaths.setPath;
     auto location = new Location(errorPath, lineNum, lineBegin, columnPos);
     auto msg = Format(_arguments, _argptr, GetMsg(mid));
     auto error = new LexerError(location, msg);
--- a/trunk/src/dil/lexer/Token.d	Sun Jan 06 01:19:29 2008 +0100
+++ b/trunk/src/dil/lexer/Token.d	Sun Jan 06 15:23:40 2008 +0100
@@ -33,15 +33,11 @@
   char* start; /// Start of token in source text.
   char* end;   /// Points one past the end of token in source text.
 
+  // Data associated with this token.
   union
   {
     /// For newline tokens.
-    struct
-    {
-      char[] filePath;
-      uint lineNum;
-      uint lineNum_hline;
-    }
+    NewlineData newline;
     /// For #line tokens.
     struct
     {
@@ -202,8 +198,8 @@
     // Find previous newline token.
     while (search_t.type != TOK.Newline)
       search_t = search_t.prev;
-    auto filePath  = search_t.filePath;
-    auto lineNum   = search_t.lineNum - search_t.lineNum_hline;
+    auto filePath  = search_t.newline.filePaths.setPath;
+    auto lineNum   = search_t.newline.oriLineNum - search_t.newline.setLineNum;
     auto lineBegin = search_t.end;
     // Determine actual line begin and line number.
     while (1)
@@ -307,14 +303,15 @@
 }
 }
 
-/++
-  Not used at the moment. Could be useful if more
-  info is needed about the location of nodes/tokens.
-+/
-struct NewlineInfo
+/// Stores data about newlines.
+struct NewlineData
 {
-  char[] oriPath;   /// Original path to the source text.
-  char[] setPath;   /// Path set by #line.
+  struct FilePaths
+  {
+    char[] oriPath;   /// Original path to the source text.
+    char[] setPath;   /// Path set by #line.
+  }
+  FilePaths* filePaths;
   uint oriLineNum;  /// Actual line number in the source text.
   uint setLineNum;  /// Delta line number set by #line.
 }