Mercurial > projects > dil
changeset 779:8e6fed11bb68
Moved Settings.d and SettingsLoader.d to src/.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Wed, 20 Feb 2008 22:47:33 +0100 |
parents | 78be32e3e157 |
children | edd217e14736 |
files | trunk/src/Settings.d trunk/src/SettingsLoader.d trunk/src/cmd/DDoc.d trunk/src/cmd/Generate.d trunk/src/cmd/ImportGraph.d trunk/src/dil/Messages.d trunk/src/dil/Settings.d trunk/src/dil/SettingsLoader.d trunk/src/main.d |
diffstat | 9 files changed, 281 insertions(+), 275 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/src/Settings.d Wed Feb 20 22:47:33 2008 +0100 @@ -0,0 +1,29 @@ +/++ + Author: Aziz Köksal + License: GPL3 ++/ +module Settings; +import common; + +/// Global application settings. +struct GlobalSettings +{ +static: + /// Predefined version identifiers. + string[] versionIds; + /// Path to the language file. + string langFile = "lang_en.d"; + /// Language code of loaded messages catalogue. + string langCode = "en"; + /// Table of localized compiler messages. + string[] messages; + /// Array of import paths to look for modules. + string[] importPaths; + /// Array of DDoc macro file paths. + string[] ddocFilePaths; + string xmlMapFile = "xml_map.d"; + string htmlMapFile = "html_map.d"; + string lexerErrorFormat = "{0}({1},{2})L: {3}"; + string parserErrorFormat = "{0}({1},{2})P: {3}"; + string semanticErrorFormat = "{0}({1},{2})S: {3}"; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/src/SettingsLoader.d Wed Feb 20 22:47:33 2008 +0100 @@ -0,0 +1,243 @@ +/++ + Author: Aziz Köksal + License: GPL3 ++/ +module SettingsLoader; + +import Settings; +import dil.Messages; +import dil.ast.Node, dil.ast.Declarations, dil.ast.Expressions; +import dil.semantic.Module; +import dil.semantic.Pass1; +import dil.semantic.Symbol; +import dil.semantic.Symbols; +import dil.Information; +import dil.Compilation; +import common; + +import tango.io.FilePath; + +class SettingsLoader +{ + InfoManager infoMan; + Module mod; /// Current module. + + this(InfoManager infoMan) + { + this.infoMan = infoMan; + } + + static SettingsLoader opCall(InfoManager infoMan) + { + return new SettingsLoader(infoMan); + } + + void error(Token* token, char[] formatMsg, ...) + { + auto location = token.getErrorLocation(); + auto msg = Format(_arguments, _argptr, formatMsg); + infoMan ~= new SemanticError(location, msg); + } + + T getValue(T)(char[] name) + { + auto var = mod.lookup(name); + if (!var) // Returning T.init instead of null, because dmd gives an error. + return error(mod.firstToken, "variable '{}' is not defined", name), T.init; + auto t = var.node.begin; + if (!var.isVariable) + return error(t, "'{}' is not a variable declaration", name), T.init; + auto value = var.to!(Variable).value; + if (!value) + return error(t, "'{}' variable has no value set", name), T.init; + T val = value.Is!(T); // Try casting to T. + if (!val) + error(value.begin, "the value of '{}' is not of type {}", name, typeof(T).stringof); + return val; + } + + T castTo(T)(Node n) + { + char[] type; + is(T == StringExpression) && (type = "char[]"); + if (!n.Is!(T)) + error(n.begin, "expression is not of type {}", type); + return n.Is!(T); + } + + void load() + { + scope execPath = new FilePath(GetExecutableFilePath()); + execPath = new FilePath(execPath.folder()); + + // Load config.d + auto filePath = resolvePath(execPath, "config.d"); + mod = new Module(filePath, infoMan); + mod.parse(); + + if (mod.hasErrors) + return; + + auto context = new CompilationContext; + auto pass1 = new SemanticPass1(mod, context); + pass1.start(); + + if (auto array = getValue!(ArrayInitExpression)("version_ids")) + foreach (value; array.values) + if (auto str = castTo!(StringExpression)(value)) + GlobalSettings.versionIds ~= str.getString(); + if (auto val = getValue!(StringExpression)("langfile")) + GlobalSettings.langFile = val.getString(); + if (auto array = getValue!(ArrayInitExpression)("import_paths")) + foreach (value; array.values) + if (auto str = castTo!(StringExpression)(value)) + GlobalSettings.importPaths ~= str.getString(); + if (auto array = getValue!(ArrayInitExpression)("ddoc_files")) + foreach (value; array.values) + if (auto str = castTo!(StringExpression)(value)) + GlobalSettings.ddocFilePaths ~= resolvePath(execPath, str.getString()); + if (auto val = getValue!(StringExpression)("xml_map")) + GlobalSettings.xmlMapFile = val.getString(); + if (auto val = getValue!(StringExpression)("html_map")) + GlobalSettings.htmlMapFile = val.getString(); + if (auto val = getValue!(StringExpression)("lexer_error")) + GlobalSettings.lexerErrorFormat = val.getString(); + if (auto val = getValue!(StringExpression)("parser_error")) + GlobalSettings.parserErrorFormat = val.getString(); + if (auto val = getValue!(StringExpression)("semantic_error")) + GlobalSettings.semanticErrorFormat = val.getString(); + + // Load language file. + filePath = resolvePath(execPath, GlobalSettings.langFile); + mod = new Module(filePath); + mod.parse(); + + if (mod.hasErrors) + return; + + pass1 = new SemanticPass1(mod, context); + pass1.start(); + + if (auto array = getValue!(ArrayInitExpression)("messages")) + { + char[][] messages; + foreach (value; array.values) + if (auto str = castTo!(StringExpression)(value)) + messages ~= str.getString(); + if (messages.length != MID.max+1) + error(mod.firstToken, + "messages table in {} must exactly have {} entries, but not {}.", + filePath, MID.max+1, messages.length); + GlobalSettings.messages = messages; + dil.Messages.SetMessages(messages); + } + if (auto val = getValue!(StringExpression)("lang_code")) + GlobalSettings.langCode = val.getString(); + } +} + +class TagMapLoader : SettingsLoader +{ + this(InfoManager infoMan) + { + super(infoMan); + } + + static TagMapLoader opCall(InfoManager infoMan) + { + return new TagMapLoader(infoMan); + } + + string[string] load(string filePath) + { + mod = new Module(filePath, infoMan); + mod.parse(); + if (mod.hasErrors) + return null; + + auto context = new CompilationContext; + auto pass1 = new SemanticPass1(mod, context); + pass1.start(); + + string[string] map; + if (auto array = getValue!(ArrayInitExpression)("map")) + foreach (i, value; array.values) + { + auto key = array.keys[i]; + if (auto valExp = castTo!(StringExpression)(value)) + if (!key) + error(value.begin, "expected key : value"); + else if (auto keyExp = castTo!(StringExpression)(key)) + map[keyExp.getString()] = valExp.getString(); + } + return map; + } +} + +string resolvePath(FilePath execPath, string filePath) +{ + if ((new FilePath(filePath)).isAbsolute()) + return filePath; + return execPath.dup.append(filePath).toString(); +} + +version(DDoc) +{ + /// Returns the fully qualified path to this executable. + char[] GetExecutableFilePath(); +} +else version(Windows) +{ +private extern(Windows) uint GetModuleFileNameA(void*, char*, uint); + +char[] GetExecutableFilePath() +{ + alias GetModuleFileNameA GetModuleFileName; + char[] buffer = new char[256]; + uint count; + + while (1) + { + if (buffer is null) + return null; + + count = GetModuleFileName(null, buffer.ptr, buffer.length); + if (count == 0) + return null; + if (buffer.length != count && buffer[count] == 0) + break; + // Increase size of buffer + buffer.length = buffer.length * 2; + } + assert(buffer[count] == 0); + // Reduce buffer to the actual length of the string (excluding '\0'.) + if (count < buffer.length) + buffer.length = count; + return buffer; +} +} +else version(linux) +{ +private extern(C) size_t readlink(char* path, char* buf, size_t bufsize); + +char[] GetExecutableFilePath() +{ + char[] buffer = new char[256]; + size_t count; + + while (1) + { + // This won't work on very old Linux systems. + count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length); + if (count == -1) + return null; + if (count < buffer.length) + break; + buffer.length = buffer.length * 2; + } + buffer.length = count; + return buffer; +} +} +else + static assert(0, "GetExecutableFilePath() is not implemented on this platform.");
--- a/trunk/src/cmd/DDoc.d Wed Feb 20 22:09:29 2008 +0100 +++ b/trunk/src/cmd/DDoc.d Wed Feb 20 22:47:33 2008 +0100 @@ -89,7 +89,7 @@ // Create a macro environment for this module. mtable = new MacroTable(mtable); // Define runtime macros. - mtable.insert("MODPATH", mod.getFQNPath() ~ mod.fileExtension()); + mtable.insert("MODPATH", mod.getFQNPath() ~ "." ~ mod.fileExtension()); mtable.insert("TITLE", mod.getFQN()); mtable.insert("DOCFILENAME", mod.getFQN() ~ ".html");
--- a/trunk/src/cmd/Generate.d Wed Feb 20 22:09:29 2008 +0100 +++ b/trunk/src/cmd/Generate.d Wed Feb 20 22:47:33 2008 +0100 @@ -15,8 +15,8 @@ import dil.semantic.Module; import dil.SourceText; import dil.Information; -import dil.SettingsLoader; -import dil.Settings; +import SettingsLoader; +import Settings; import common; import tango.io.GrowBuffer;
--- a/trunk/src/cmd/ImportGraph.d Wed Feb 20 22:09:29 2008 +0100 +++ b/trunk/src/cmd/ImportGraph.d Wed Feb 20 22:47:33 2008 +0100 @@ -8,8 +8,8 @@ import dil.ast.Declarations; import dil.semantic.Module; import dil.parser.ImportParser; -import dil.Settings; import dil.SourceText; +import Settings; import common; import tango.text.Regex : RegExp = Regex;
--- a/trunk/src/dil/Messages.d Wed Feb 20 22:09:29 2008 +0100 +++ b/trunk/src/dil/Messages.d Wed Feb 20 22:47:33 2008 +0100 @@ -79,7 +79,7 @@ messages = new string[MID.max+1]; } -package void SetMessages(string[] msgs) +void SetMessages(string[] msgs) { assert(MID.max+1 == msgs.length); messages = msgs;
--- a/trunk/src/dil/Settings.d Wed Feb 20 22:09:29 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -/++ - Author: Aziz Köksal - License: GPL3 -+/ -module dil.Settings; -import common; - -/// Global application settings. -struct GlobalSettings -{ -static: - /// Predefined version identifiers. - string[] versionIds; - /// Path to the language file. - string langFile = "lang_en.d"; - /// Language code of loaded messages catalogue. - string langCode = "en"; - /// Table of localized compiler messages. - string[] messages; - /// Array of import paths to look for modules. - string[] importPaths; - /// Array of DDoc macro file paths. - string[] ddocFilePaths; - string xmlMapFile = "xml_map.d"; - string htmlMapFile = "html_map.d"; - string lexerErrorFormat = "{0}({1},{2})L: {3}"; - string parserErrorFormat = "{0}({1},{2})P: {3}"; - string semanticErrorFormat = "{0}({1},{2})S: {3}"; -}
--- a/trunk/src/dil/SettingsLoader.d Wed Feb 20 22:09:29 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,238 +0,0 @@ -/++ - Author: Aziz Köksal - License: GPL3 -+/ -module dil.SettingsLoader; - -import dil.Settings; -import dil.Messages; -import dil.ast.Node, dil.ast.Declarations, dil.ast.Expressions; -import dil.semantic.Module; -import dil.semantic.Pass1; -import dil.semantic.Symbol; -import dil.semantic.Symbols; -import dil.Information; -import dil.Compilation; -import common; - -import tango.io.FilePath; - -class SettingsLoader -{ - InfoManager infoMan; - Module mod; /// Current module. - - this(InfoManager infoMan) - { - this.infoMan = infoMan; - } - - static SettingsLoader opCall(InfoManager infoMan) - { - return new SettingsLoader(infoMan); - } - - void error(Token* token, char[] formatMsg, ...) - { - auto location = token.getErrorLocation(); - auto msg = Format(_arguments, _argptr, formatMsg); - infoMan ~= new SemanticError(location, msg); - } - - T getValue(T)(char[] name) - { - auto var = mod.lookup(name); - if (!var) // Returning T.init instead of null, because dmd gives an error. - return error(mod.firstToken, "variable '{}' is not defined", name), T.init; - auto t = var.node.begin; - if (!var.isVariable) - return error(t, "'{}' is not a variable declaration", name), T.init; - auto value = var.to!(Variable).value; - if (!value) - return error(t, "'{}' variable has no value set", name), T.init; - T val = value.Is!(T); // Try casting to T. - if (!val) - error(value.begin, "the value of '{}' is not of type {}", name, typeof(T).stringof); - return val; - } - - T castTo(T)(Node n) - { - char[] type; - is(T == StringExpression) && (type = "char[]"); - if (!n.Is!(T)) - error(n.begin, "expression is not of type {}", type); - return n.Is!(T); - } - - void load() - { - scope execPath = new FilePath(GetExecutableFilePath()); - execPath = new FilePath(execPath.folder()); - - // Load config.d - auto filePath = resolvePath(execPath, "config.d"); - mod = new Module(filePath, infoMan); - mod.parse(); - - if (mod.hasErrors) - return; - - auto context = new CompilationContext; - auto pass1 = new SemanticPass1(mod, context); - pass1.start(); - - if (auto array = getValue!(ArrayInitExpression)("version_ids")) - foreach (value; array.values) - if (auto str = castTo!(StringExpression)(value)) - GlobalSettings.versionIds ~= str.getString(); - if (auto val = getValue!(StringExpression)("langfile")) - GlobalSettings.langFile = val.getString(); - if (auto array = getValue!(ArrayInitExpression)("import_paths")) - foreach (value; array.values) - if (auto str = castTo!(StringExpression)(value)) - GlobalSettings.importPaths ~= str.getString(); - if (auto array = getValue!(ArrayInitExpression)("ddoc_files")) - foreach (value; array.values) - if (auto str = castTo!(StringExpression)(value)) - GlobalSettings.ddocFilePaths ~= resolvePath(execPath, str.getString()); - if (auto val = getValue!(StringExpression)("xml_map")) - GlobalSettings.xmlMapFile = val.getString(); - if (auto val = getValue!(StringExpression)("html_map")) - GlobalSettings.htmlMapFile = val.getString(); - if (auto val = getValue!(StringExpression)("lexer_error")) - GlobalSettings.lexerErrorFormat = val.getString(); - if (auto val = getValue!(StringExpression)("parser_error")) - GlobalSettings.parserErrorFormat = val.getString(); - if (auto val = getValue!(StringExpression)("semantic_error")) - GlobalSettings.semanticErrorFormat = val.getString(); - - // Load language file. - filePath = resolvePath(execPath, GlobalSettings.langFile); - mod = new Module(filePath); - mod.parse(); - - if (mod.hasErrors) - return; - - pass1 = new SemanticPass1(mod, context); - pass1.start(); - - if (auto array = getValue!(ArrayInitExpression)("messages")) - { - char[][] messages; - foreach (value; array.values) - if (auto str = castTo!(StringExpression)(value)) - messages ~= str.getString(); - if (messages.length != MID.max+1) - error(mod.firstToken, - "messages table in {} must exactly have {} entries, but not {}.", - filePath, MID.max+1, messages.length); - GlobalSettings.messages = messages; - dil.Messages.SetMessages(messages); - } - if (auto val = getValue!(StringExpression)("lang_code")) - GlobalSettings.langCode = val.getString(); - } -} - -class TagMapLoader : SettingsLoader -{ - this(InfoManager infoMan) - { - super(infoMan); - } - - static TagMapLoader opCall(InfoManager infoMan) - { - return new TagMapLoader(infoMan); - } - - string[string] load(string filePath) - { - mod = new Module(filePath, infoMan); - mod.parse(); - if (mod.hasErrors) - return null; - - auto context = new CompilationContext; - auto pass1 = new SemanticPass1(mod, context); - pass1.start(); - - string[string] map; - if (auto array = getValue!(ArrayInitExpression)("map")) - foreach (i, value; array.values) - { - auto key = array.keys[i]; - if (auto valExp = castTo!(StringExpression)(value)) - if (!key) - error(value.begin, "expected key : value"); - else if (auto keyExp = castTo!(StringExpression)(key)) - map[keyExp.getString()] = valExp.getString(); - } - return map; - } -} - -string resolvePath(FilePath execPath, string filePath) -{ - if ((new FilePath(filePath)).isAbsolute()) - return filePath; - return execPath.dup.append(filePath).toString(); -} - -version(Windows) -{ -private extern(Windows) uint GetModuleFileNameA(void*, char*, uint); -/// Get the fully qualified path to this executable. -char[] GetExecutableFilePath() -{ - alias GetModuleFileNameA GetModuleFileName; - char[] buffer = new char[256]; - uint count; - - while (1) - { - if (buffer is null) - return null; - - count = GetModuleFileName(null, buffer.ptr, buffer.length); - if (count == 0) - return null; - if (buffer.length != count && buffer[count] == 0) - break; - // Increase size of buffer - buffer.length = buffer.length * 2; - } - assert(buffer[count] == 0); - // Reduce buffer to the actual length of the string (excluding '\0'.) - if (count < buffer.length) - buffer.length = count; - return buffer; -} -} -else version(linux) -{ -private extern(C) size_t readlink(char* path, char* buf, size_t bufsize); -/// Get the fully qualified path to this executable. -char[] GetExecutableFilePath() -{ - char[] buffer = new char[256]; - size_t count; - - while (1) - { - // This won't work on very old Linux systems. - count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length); - if (count == -1) - return null; - if (count < buffer.length) - break; - buffer.length = buffer.length * 2; - } - buffer.length = count; - return buffer; -} -} -else - static assert(0, "GetExecutableFilePath() is not implemented on this platform.");
--- a/trunk/src/main.d Wed Feb 20 22:09:29 2008 +0100 +++ b/trunk/src/main.d Wed Feb 20 22:47:33 2008 +0100 @@ -19,8 +19,6 @@ import dil.translator.German; import dil.doc.Doc; import dil.Messages; -import dil.Settings; -import dil.SettingsLoader; import dil.CompilerInfo; import dil.Information; import dil.SourceText; @@ -30,6 +28,9 @@ import cmd.Statistics; import cmd.ImportGraph; import cmd.DDoc; + +import Settings; +import SettingsLoader; import common; import Integer = tango.text.convert.Integer; @@ -42,7 +43,7 @@ void main(char[][] args) { auto infoMan = new InfoManager(); - SettingsLoader(infoMan).load(); + SettingsLoader.SettingsLoader(infoMan).load(); if (infoMan.hasInfo) return printErrors(infoMan);