changeset 814:49e32b5bc161

Added isValidUnreservedIdentifier() to Lexer. Added the msg InvalidModuleName.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 12 Mar 2008 17:01:27 +0100
parents 1abffc396594
children 615c1386b18d
files src/dil/Messages.d src/dil/lexer/Lexer.d src/dil/semantic/Module.d src/main.d
diffstat 4 files changed, 19 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/dil/Messages.d	Wed Mar 12 00:49:17 2008 +0100
+++ b/src/dil/Messages.d	Wed Mar 12 17:01:27 2008 +0100
@@ -115,6 +115,7 @@
   auto UnterminatedDDocMacro = "DDoc macro '{}' has no closing ')'";
   // Lexer messages:
   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:
   auto InvalidUTF8SequenceInString = "invalid UTF-8 sequence in string literal: '{0}'";
   auto ModuleDeclarationNotFirst = "a module declaration is only allowed as the first declaration in a file";
--- a/src/dil/lexer/Lexer.d	Wed Mar 12 00:49:17 2008 +0100
+++ b/src/dil/lexer/Lexer.d	Wed Mar 12 17:01:27 2008 +0100
@@ -2547,14 +2547,16 @@
   /// Returns true if str is a keyword or a special token (__FILE__, __LINE__ etc.)
   static bool isReservedIdentifier(char[] str)
   {
-    if (!isIdentifierString(str))
-      return false; // str is not a valid identifier.
-
     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.
+    return true;
+  }
 
-    return true;
+  /// Returns true if this is a valid identifier and if it's not reserved.
+  static bool isValidUnreservedIdentifier(char[] str)
+  {
+    return isIdentifierString(str) && !isReservedIdentifier(str);
   }
 
   /// Returns true if the current character to be decoded is
--- a/src/dil/semantic/Module.d	Wed Mar 12 00:49:17 2008 +0100
+++ b/src/dil/semantic/Module.d	Wed Mar 12 17:01:27 2008 +0100
@@ -10,6 +10,8 @@
 import dil.lexer.Lexer;
 import dil.semantic.Symbol;
 import dil.semantic.Symbols;
+import dil.Location;
+import dil.Messages;
 import dil.Information;
 import dil.SourceText;
 import common;
@@ -102,8 +104,13 @@
     if (!this.moduleFQN.length)
     { // Take base name of file path as module name.
       auto str = (new FilePath(filePath)).name();
-      if (Lexer.isReservedIdentifier(str))
-        throw new Exception("'"~str~"' is not a valid module name; it's a reserved or invalid D identifier.");
+      if (!Lexer.isValidUnreservedIdentifier(str))
+      {
+        auto location = parser.lexer.firstToken().getErrorLocation();
+        auto msg = Format(MSG.InvalidModuleName, str);
+        infoMan ~= new LexerError(location, msg);
+        str = "_";
+      }
       this.moduleFQN = this.moduleName = str;
     }
   }
--- a/src/main.d	Wed Mar 12 00:49:17 2008 +0100
+++ b/src/main.d	Wed Mar 12 17:01:27 2008 +0100
@@ -334,7 +334,7 @@
 version(D2)
   cc.addVersionId("D_Version2");
   foreach (versionId; GlobalSettings.versionIds)
-    if (!Lexer.isReservedIdentifier(versionId))
+    if (Lexer.isValidUnreservedIdentifier(versionId))
       cc.versionIds[versionId] = true;
   return cc;
 }
@@ -348,7 +348,7 @@
       auto val = arg[7..$];
       if (isdigit(val[0]))
         context.debugLevel = Integer.toInt(val);
-      else if (!Lexer.isReservedIdentifier(val))
+      else if (Lexer.isValidUnreservedIdentifier(val))
         context.addDebugId(val);
     }
     else
@@ -359,7 +359,7 @@
     auto val = arg[9..$];
     if (isdigit(val[0]))
       context.versionLevel = Integer.toInt(val);
-    else if (!Lexer.isReservedIdentifier(val))
+    else if (Lexer.isValidUnreservedIdentifier(val))
       context.addVersionId(val);
   }
   else