changeset 818:372fa4fbbb1d

Added error messages and applied fixes.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 13 Mar 2008 02:21:26 +0100
parents e6fb7ed87d3a
children 438ed3a72c9d
files src/dil/Messages.d src/dil/ModuleManager.d src/dil/SourceText.d src/dil/lexer/IdTable.d src/dil/lexer/Lexer.d src/dil/semantic/Module.d
diffstat 6 files changed, 55 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/dil/Messages.d	Wed Mar 12 23:33:14 2008 +0100
+++ b/src/dil/Messages.d	Thu Mar 13 02:21:26 2008 +0100
@@ -114,6 +114,8 @@
   auto UndefinedDDocMacro = "DDoc macro '{}' is undefined";
   auto UnterminatedDDocMacro = "DDoc macro '{}' has no closing ')'";
   // Lexer messages:
+  auto CantReadFile = "can't read module file";
+  auto InexistantFile = "module file doesn't exist";
   auto InvalidOctalEscapeSequence = "value of octal escape sequence is greater than 0xFF: '{}'";
   auto InvalidModuleName = "the file name '{}' can't be used a module name; it's an invalid or reserved D identifier.";
   // Parser messages:
@@ -155,7 +157,7 @@
   auto ExpectedIdentOrInt = "expected an identifier or an integer, not '{}'";
   auto MissingCatchOrFinally = "try statement is missing a catch or finally body.";
   // Semantic analysis:
-  auto CouldntLoadModule = "couldn't load module '{}'";
+  auto CouldntLoadModule = "couldn't import module '{}'";
   auto ConflictingModuleFiles = "module is in conflict with module '{}'";
   auto ConflictingModuleAndPackage = "module is in conflict with package '{}'";
   auto ModuleNotInPackage = "expected module to be in package '{}'";
--- a/src/dil/ModuleManager.d	Wed Mar 12 23:33:14 2008 +0100
+++ b/src/dil/ModuleManager.d	Thu Mar 13 02:21:26 2008 +0100
@@ -92,11 +92,14 @@
     splitPackageFQN(pckgFQN, prevFQN, lastPckgName);
     // Recursively build package hierarchy.
     auto parentPckg = getPackage(prevFQN); // E.g.: 'dil'
+
     // Create a new package.
     auto pckg = new Package(lastPckgName); // E.g.: 'ast'
     parentPckg.add(pckg); // 'dil'.add('ast')
+
     // Insert the package into the table.
     packageTable[pckgFQN] = pckg;
+
     return pckg;
   }
 
@@ -127,20 +130,22 @@
     Module* pModul = moduleFQNPath in moduleFQNPathTable;
     if (pModul)
       return *pModul;
+
     // Locate the module in the file system.
     auto moduleFilePath = findModuleFilePath(moduleFQNPath, importPaths);
-    if (moduleFilePath.length)
-    { // Load the found module file.
-      auto modul = loadModuleFile(moduleFilePath);
-      if (modul.getFQNPath() != moduleFQNPath)
-      { // Error: the requested module is not in the correct package.
-        auto location = modul.getModuleDeclToken().getErrorLocation();
-        auto msg = Format(MSG.ModuleNotInPackage, getPackageFQN(moduleFQNPath));
-        infoMan ~= new SemanticError(location, msg);
-      }
-      return modul;
+    if (!moduleFilePath.length)
+      return null;
+
+    // Load the found module file.
+    auto modul = loadModuleFile(moduleFilePath);
+    if (modul.getFQNPath() != moduleFQNPath)
+    { // Error: the requested module is not in the correct package.
+      auto location = modul.getModuleDeclToken().getErrorLocation();
+      auto msg = Format(MSG.ModuleNotInPackage, getPackageFQN(moduleFQNPath));
+      infoMan ~= new SemanticError(location, msg);
     }
-    return null;
+
+    return modul;
   }
 
   /// Returns e.g. 'dil.ast' for 'dil/ast/Node'.
--- a/src/dil/SourceText.d	Wed Mar 12 23:33:14 2008 +0100
+++ b/src/dil/SourceText.d	Thu Mar 13 02:21:26 2008 +0100
@@ -6,9 +6,12 @@
 
 import dil.Converter;
 import dil.Information;
+import dil.Location;
+import dil.Messages;
 import common;
 
 import tango.io.File;
+import tango.io.FilePath;
 
 /// Represents D source code.
 ///
@@ -45,6 +48,19 @@
     if (!infoMan)
       infoMan = new InfoManager;
     assert(filePath.length);
+
+    scope(failure)
+    {
+      if (!(new FilePath(this.filePath)).exists())
+        infoMan ~= new LexerError(new Location(filePath, 0),
+                                  MSG.InexistantFile);
+      else
+        infoMan ~= new LexerError(new Location(filePath, 0),
+                                  MSG.CantReadFile);
+      data = "\0";
+      return;
+    }
+
     // Read the file.
     auto rawdata = cast(ubyte[]) (new File(filePath)).read();
     // Convert the data.
--- a/src/dil/lexer/IdTable.d	Wed Mar 12 23:33:14 2008 +0100
+++ b/src/dil/lexer/IdTable.d	Thu Mar 13 02:21:26 2008 +0100
@@ -147,6 +147,12 @@
   {
     return genAnonymousID("__anonenum");
   }
+
+  /// Generates an identifier for a module which has got no valid name.
+  Identifier* genModuleID()
+  {
+    return genAnonymousID("__module");
+  }
 }
 
 unittest
--- a/src/dil/lexer/Lexer.d	Wed Mar 12 23:33:14 2008 +0100
+++ b/src/dil/lexer/Lexer.d	Thu Mar 13 02:21:26 2008 +0100
@@ -2544,9 +2544,12 @@
     return true;
   }
 
-  /// Returns true if str is a keyword or a special token (__FILE__, __LINE__ etc.)
+  /// Returns true if str is a keyword or
+  /// a special token (__FILE__, __LINE__ etc.)
   static bool isReservedIdentifier(char[] str)
   {
+    if (str.length == 0)
+      return false;
     auto id = IdTable.inStatic(str);
     if (id is null || id.kind == TOK.Identifier)
       return false; // str is not in the table or a normal identifier.
--- a/src/dil/semantic/Module.d	Wed Mar 12 23:33:14 2008 +0100
+++ b/src/dil/semantic/Module.d	Thu Mar 13 02:21:26 2008 +0100
@@ -95,25 +95,28 @@
     this.root = parser.start();
     this.imports = parser.imports;
 
-    if (root.children.length)
+    // Set the fully qualified name of this module.
+    if (this.root.children.length)
     { // moduleDecl will be null if first node isn't a ModuleDeclaration.
-      this.moduleDecl = root.children[0].Is!(ModuleDeclaration);
+      this.moduleDecl = this.root.children[0].Is!(ModuleDeclaration);
       if (this.moduleDecl)
-        this.setFQN(moduleDecl.getFQN());
+        this.setFQN(moduleDecl.getFQN()); // E.g.: dil.ast.Node
     }
 
     if (!this.moduleFQN.length)
-    { // Take base name of file path as module name.
-      auto str = (new FilePath(filePath)).name();
+    { // Take the base name of the file as the module name.
+      auto str = (new FilePath(filePath)).name(); // E.g.: Node
       if (!Lexer.isValidUnreservedIdentifier(str))
       {
-        auto location = parser.lexer.firstToken().getErrorLocation();
+        auto location = this.firstToken().getErrorLocation();
         auto msg = Format(MSG.InvalidModuleName, str);
         infoMan ~= new LexerError(location, msg);
-        str = "__module_name";
+        str = IdTable.genModuleID().str;
       }
       this.moduleFQN = this.moduleName = str;
     }
+    assert(this.moduleFQN.length);
+
     // Set the symbol name.
     this.name = IdTable.lookup(this.moduleName);
   }