# HG changeset patch # User Aziz K?ksal # Date 1199577337 -3600 # Node ID 9f96fd51cb412a8bbaeee378e7b811b0e1512b80 # Parent 041eae272362c349169f60c19b7cc380bb1c87b7 Moved dil.IdTable to dil.lexer.IdTable. diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/IdTable.d --- a/trunk/src/dil/IdTable.d Sun Jan 06 00:52:20 2008 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,142 +0,0 @@ -/++ - Author: Aziz Köksal - License: GPL3 -+/ -module dil.IdTable; - -import dil.lexer.TokensEnum; -import dil.IdentsGenerator; -import dil.lexer.Keywords; -import common; - -public import dil.lexer.Identifier; -public import dil.IdentsEnum; - -struct Ident -{ - const static - { - mixin(generateIdentMembers()); - } - - static Identifier*[] allIds() - { - return __allIds; - } -} - -/++ - Global table for hoarding and retrieving identifiers. -+/ -struct IdTable -{ -static: - /// A set of common, predefined identifiers for fast lookups. - private Identifier*[string] staticTable; - /// A table that grows with every newly found, unique identifier. - /// Access must be synchronized. - private Identifier*[string] growingTable; - - /// Initializes the static table. - static this() - { - // Load keywords and pre-defined identifiers into the static table. - foreach (ref k; keywords) - staticTable[k.str] = &k; - foreach (id; Ident.allIds()) - staticTable[id.str] = id; - staticTable.rehash; - } - - /// Looks in both tables. - Identifier* lookup(string idString) - { - auto id = inStatic(idString); - if (id) - return id; - return inGrowing(idString); - } - - /// Look up idString in the static table. - Identifier* inStatic(string idString) - { - auto id = idString in staticTable; - return id ? *id : null; - } - - alias Identifier* function(string idString) LookupFunction; - /// Look up idString in the growing table. - LookupFunction inGrowing = &_inGrowing_unsafe; // Default to unsafe function. - - /++ - Set the thread safety mode of this table. - Call this function only if you can be sure - that this table is not being accessed - (like during lexing, parsing and semantic phase.) - +/ - void setThreadsafe(bool b) - { - if (b) - IdTable.inGrowing = &_inGrowing_safe; - else - IdTable.inGrowing = &_inGrowing_unsafe; - } - - /++ - Returns the Identifier for idString. - Adds idString to the table if not found. - +/ - private Identifier* _inGrowing_unsafe(string idString) - out(id) - { assert(id !is null); } - body - { - auto id = idString in growingTable; - if (id) - return *id; - auto newID = Identifier(idString, TOK.Identifier); - growingTable[idString] = newID; - return newID; - } - - /++ - Returns the Identifier for idString. - Adds idString to the table if not found. - Access to the data structure is synchronized. - +/ - private Identifier* _inGrowing_safe(string idString) - { - synchronized - return _inGrowing_unsafe(idString); - } - - /+ - Identifier* addIdentifiers(char[][] idStrings) - { - auto ids = new Identifier*[idStrings.length]; - foreach (i, idString; idStrings) - { - Identifier** id = idString in tabulatedIds; - if (!id) - { - auto newID = Identifier(TOK.Identifier, idString); - tabulatedIds[idString] = newID; - id = &newID; - } - ids[i] = *id; - } - } - +/ -} - -unittest -{ - // TODO: write benchmark. - // Single table - - // Single table. synchronized - - // Two tables. - - // Two tables. synchronized -} diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/ast/Declarations.d --- a/trunk/src/dil/ast/Declarations.d Sun Jan 06 00:52:20 2008 +0100 +++ b/trunk/src/dil/ast/Declarations.d Sun Jan 06 00:55:37 2008 +0100 @@ -8,12 +8,12 @@ import dil.ast.Expressions; import dil.ast.Types; import dil.ast.Statements; -import dil.Enums; -import dil.IdTable; +import dil.lexer.IdTable; import dil.semantic.Scope; import dil.semantic.Analysis; import dil.semantic.Symbols; import dil.semantic.Types; +import dil.Enums; import dil.Messages; import common; diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/ast/Statements.d --- a/trunk/src/dil/ast/Statements.d Sun Jan 06 00:52:20 2008 +0100 +++ b/trunk/src/dil/ast/Statements.d Sun Jan 06 00:55:37 2008 +0100 @@ -8,7 +8,7 @@ import dil.ast.Expressions; import dil.ast.Declarations; import dil.ast.Types; -import dil.IdTable; +import dil.lexer.IdTable; import dil.semantic.Scope; import dil.semantic.Analysis; diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/lexer/IdTable.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trunk/src/dil/lexer/IdTable.d Sun Jan 06 00:55:37 2008 +0100 @@ -0,0 +1,142 @@ +/++ + Author: Aziz Köksal + License: GPL3 ++/ +module dil.lexer.IdTable; + +import dil.lexer.TokensEnum; +import dil.IdentsGenerator; +import dil.lexer.Keywords; +import common; + +public import dil.lexer.Identifier; +public import dil.IdentsEnum; + +struct Ident +{ + const static + { + mixin(generateIdentMembers()); + } + + static Identifier*[] allIds() + { + return __allIds; + } +} + +/++ + Global table for hoarding and retrieving identifiers. ++/ +struct IdTable +{ +static: + /// A set of common, predefined identifiers for fast lookups. + private Identifier*[string] staticTable; + /// A table that grows with every newly found, unique identifier. + /// Access must be synchronized. + private Identifier*[string] growingTable; + + /// Initializes the static table. + static this() + { + // Load keywords and pre-defined identifiers into the static table. + foreach (ref k; keywords) + staticTable[k.str] = &k; + foreach (id; Ident.allIds()) + staticTable[id.str] = id; + staticTable.rehash; + } + + /// Looks in both tables. + Identifier* lookup(string idString) + { + auto id = inStatic(idString); + if (id) + return id; + return inGrowing(idString); + } + + /// Look up idString in the static table. + Identifier* inStatic(string idString) + { + auto id = idString in staticTable; + return id ? *id : null; + } + + alias Identifier* function(string idString) LookupFunction; + /// Look up idString in the growing table. + LookupFunction inGrowing = &_inGrowing_unsafe; // Default to unsafe function. + + /++ + Set the thread safety mode of this table. + Call this function only if you can be sure + that this table is not being accessed + (like during lexing, parsing and semantic phase.) + +/ + void setThreadsafe(bool b) + { + if (b) + IdTable.inGrowing = &_inGrowing_safe; + else + IdTable.inGrowing = &_inGrowing_unsafe; + } + + /++ + Returns the Identifier for idString. + Adds idString to the table if not found. + +/ + private Identifier* _inGrowing_unsafe(string idString) + out(id) + { assert(id !is null); } + body + { + auto id = idString in growingTable; + if (id) + return *id; + auto newID = Identifier(idString, TOK.Identifier); + growingTable[idString] = newID; + return newID; + } + + /++ + Returns the Identifier for idString. + Adds idString to the table if not found. + Access to the data structure is synchronized. + +/ + private Identifier* _inGrowing_safe(string idString) + { + synchronized + return _inGrowing_unsafe(idString); + } + + /+ + Identifier* addIdentifiers(char[][] idStrings) + { + auto ids = new Identifier*[idStrings.length]; + foreach (i, idString; idStrings) + { + Identifier** id = idString in tabulatedIds; + if (!id) + { + auto newID = Identifier(TOK.Identifier, idString); + tabulatedIds[idString] = newID; + id = &newID; + } + ids[i] = *id; + } + } + +/ +} + +unittest +{ + // TODO: write benchmark. + // Single table + + // Single table. synchronized + + // Two tables. + + // Two tables. synchronized +} diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/lexer/Lexer.d --- a/trunk/src/dil/lexer/Lexer.d Sun Jan 06 00:52:20 2008 +0100 +++ b/trunk/src/dil/lexer/Lexer.d Sun Jan 06 00:55:37 2008 +0100 @@ -7,11 +7,11 @@ import dil.lexer.Token; import dil.lexer.Keywords; import dil.lexer.Identifier; +import dil.lexer.IdTable; import dil.Information; import dil.Messages; import dil.HtmlEntities; import dil.CompilerInfo; -import dil.IdTable; import dil.Unicode; import tango.stdc.stdlib : strtof, strtod, strtold; import tango.stdc.errno : errno, ERANGE; diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/parser/Parser.d --- a/trunk/src/dil/parser/Parser.d Sun Jan 06 00:52:20 2008 +0100 +++ b/trunk/src/dil/parser/Parser.d Sun Jan 06 00:55:37 2008 +0100 @@ -10,11 +10,11 @@ import dil.ast.Statements; import dil.ast.Expressions; import dil.ast.Types; +import dil.lexer.IdTable; import dil.Messages; import dil.Information; import dil.Enums; import dil.CompilerInfo; -import dil.IdTable; import common; /++ diff -r 041eae272362 -r 9f96fd51cb41 trunk/src/dil/semantic/Analysis.d --- a/trunk/src/dil/semantic/Analysis.d Sun Jan 06 00:52:20 2008 +0100 +++ b/trunk/src/dil/semantic/Analysis.d Sun Jan 06 00:55:37 2008 +0100 @@ -7,7 +7,7 @@ import dil.ast.Node; import dil.ast.Expressions; import dil.semantic.Scope; -import dil.IdTable; +import dil.lexer.IdTable; import common; /// Common semantics for pragma declarations and statements.