# HG changeset patch # User Aziz K?ksal # Date 1205371286 -3600 # Node ID 372fa4fbbb1d68ab0372227b3e17b21596f00f24 # Parent e6fb7ed87d3a49a42b72932f2fadda2ce1faf689 Added error messages and applied fixes. diff -r e6fb7ed87d3a -r 372fa4fbbb1d src/dil/Messages.d --- 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 '{}'"; diff -r e6fb7ed87d3a -r 372fa4fbbb1d src/dil/ModuleManager.d --- 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'. diff -r e6fb7ed87d3a -r 372fa4fbbb1d src/dil/SourceText.d --- 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. diff -r e6fb7ed87d3a -r 372fa4fbbb1d src/dil/lexer/IdTable.d --- 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 diff -r e6fb7ed87d3a -r 372fa4fbbb1d src/dil/lexer/Lexer.d --- 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. diff -r e6fb7ed87d3a -r 372fa4fbbb1d src/dil/semantic/Module.d --- 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); }