# HG changeset patch # User Aziz K?ksal # Date 1197388962 -3600 # Node ID 9076c4cea2a43b5efb35d546eac2e8a9dcb91ace # Parent fa63ef408790218fc06fe2296b949b8c15f8e617 Changed occur. of Token* to Identifier* and refactored Parser. Module dil.IdTable imports dil.Identifier and dil.IdentsEnum publicly. Removed file TokenIDs.d diff -r fa63ef408790 -r 9076c4cea2a4 trunk/src/dil/Declarations.d --- a/trunk/src/dil/Declarations.d Tue Dec 11 15:05:08 2007 +0100 +++ b/trunk/src/dil/Declarations.d Tue Dec 11 17:02:42 2007 +0100 @@ -10,6 +10,7 @@ import dil.Token; import dil.Enums; import dil.Scope; +import dil.Identifier; abstract class Declaration : Node { @@ -108,12 +109,12 @@ } /// FQN = fully qualified name -alias Token*[] ModuleFQN; // Identifier(.Identifier)* +alias Identifier*[] ModuleFQN; // Identifier(.Identifier)* class ModuleDeclaration : Declaration { - Token* moduleName; - Token*[] packages; + Identifier* moduleName; + Identifier*[] packages; this(ModuleFQN moduleFQN) { mixin(set_kind); @@ -134,7 +135,7 @@ char[] getName() { if (moduleName) - return moduleName.identifier; + return moduleName.str; return null; } @@ -143,7 +144,7 @@ char[] pname; foreach (pckg; packages) if (pckg) - pname ~= pckg.identifier ~ separator; + pname ~= pckg.str ~ separator; if (pname.length) pname = pname[0..$-1]; // Remove last separator return pname; @@ -152,12 +153,13 @@ class ImportDeclaration : Declaration { + private alias Identifier*[] Ids; ModuleFQN[] moduleFQNs; - Token*[] moduleAliases; - Token*[] bindNames; - Token*[] bindAliases; + Ids moduleAliases; + Ids bindNames; + Ids bindAliases; - this(ModuleFQN[] moduleFQNs, Token*[] moduleAliases, Token*[] bindNames, Token*[] bindAliases, bool isStatic) + this(ModuleFQN[] moduleFQNs, Ids moduleAliases, Ids bindNames, Ids bindAliases, bool isStatic) { mixin(set_kind); this.moduleFQNs = moduleFQNs; @@ -176,7 +178,7 @@ char[] FQN; foreach (ident; moduleFQN) if (ident) - FQN ~= ident.identifier ~ separator; + FQN ~= ident.str ~ separator; FQNs ~= FQN[0..$-1]; // Remove last separator } return FQNs; diff -r fa63ef408790 -r 9076c4cea2a4 trunk/src/dil/IdTable.d --- a/trunk/src/dil/IdTable.d Tue Dec 11 15:05:08 2007 +0100 +++ b/trunk/src/dil/IdTable.d Tue Dec 11 17:02:42 2007 +0100 @@ -3,13 +3,14 @@ License: GPL3 +/ module dil.IdTable; -import dil.Identifier; -import dil.IdentsEnum; import dil.TokensEnum; import dil.IdentsGenerator; import dil.Keywords; import common; +public import dil.Identifier; +public import dil.IdentsEnum; + struct Ident { const static diff -r fa63ef408790 -r 9076c4cea2a4 trunk/src/dil/Parser.d --- a/trunk/src/dil/Parser.d Tue Dec 11 15:05:08 2007 +0100 +++ b/trunk/src/dil/Parser.d Tue Dec 11 17:02:42 2007 +0100 @@ -14,7 +14,6 @@ import dil.Types; import dil.Enums; import dil.CompilerInfo; -import dil.IdentsEnum; import dil.IdTable; import common; @@ -161,7 +160,7 @@ do { nT(); - moduleFQN ~= requireId(); + moduleFQN ~= requireIdentifier("expected module identifier, not '{}'"); } while (token.type == T.Dot) require(T.Semicolon); return set(new ModuleDeclaration(moduleFQN), begin); @@ -935,35 +934,30 @@ assert(token.type == T.Import); ModuleFQN[] moduleFQNs; - Token*[] moduleAliases; - Token*[] bindNames; - Token*[] bindAliases; + Identifier*[] moduleAliases; + Identifier*[] bindNames; + Identifier*[] bindAliases; nT(); // Skip import keyword. while (1) { ModuleFQN moduleFQN; - Token* moduleAlias; - - moduleAlias = requireId(); + Identifier* moduleAlias; // AliasName = ModuleName - if (token.type == T.Assign) + if (peekNext() == T.Assign) { - nT(); - moduleFQN ~= requireId(); - } - else // import Identifier [^=] - { - moduleFQN ~= moduleAlias; - moduleAlias = null; + moduleAlias = requireIdentifier("expected alias module name, not '{}'"); + nT(); // Skip = } // Identifier(.Identifier)* - while (token.type == T.Dot) + while (1) { + moduleFQN ~= requireIdentifier("expected module identifier, not '{}'"); + if (token.type != T.Dot) + break; nT(); - moduleFQN ~= requireId(); } // Push identifiers. @@ -979,25 +973,18 @@ { // BindAlias = BindName(, BindAlias = BindName)*; // BindName(, BindName)*; - Token* bindName, bindAlias; do { nT(); - bindAlias = requireId(); - - if (token.type == T.Assign) + Identifier* bindAlias; + // BindAlias = BindName + if (peekNext() == T.Assign) { - nT(); - bindName = requireId(); + bindAlias = requireIdentifier("expected alias name, not '{}'"); + nT(); // Skip = } - else - { - bindName = bindAlias; - bindAlias = null; - } - // Push identifiers. - bindNames ~= bindName; + bindNames ~= requireIdentifier("expected an identifier, not '{}'"); bindAliases ~= bindAlias; } while (token.type == T.Comma) } @@ -4451,6 +4438,40 @@ require(tok); } + Identifier* requireIdentifier() + { + Identifier* id; + if (token.type == T.Identifier) + (id = token.ident), nT(); + else + error(MID.ExpectedButFound, "Identifier", token.srcText); + return id; + } + + /++ + Params: + errorMsg = an error that has no message ID yet. + +/ + Identifier* requireIdentifier(char[] errorMsg) + { + Identifier* id; + if (token.type == T.Identifier) + (id = token.ident), nT(); + else + error(token, errorMsg, token.srcText); + return id; + } + + Identifier* requireIdentifier(MID mid) + { + Identifier* id; + if (token.type == T.Identifier) + (id = token.ident), nT(); + else + error(mid, token.srcText); + return id; + } + Token* requireId() { if (token.type == T.Identifier) diff -r fa63ef408790 -r 9076c4cea2a4 trunk/src/dil/TokenIDs.d --- a/trunk/src/dil/TokenIDs.d Tue Dec 11 15:05:08 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,203 +0,0 @@ -/++ - Author: Aziz Köksal - License: GPL3 -+/ -module dil.TokenIDs; -import common; - -enum TOK : ushort -{ - Invalid, - - /// Flag for whitespace tokens that must be ignored in the parsing phase. - Whitespace = 0x8000, - Illegal = 1 | Whitespace, - Comment = 2 | Whitespace, - Shebang = 3 | Whitespace, - HashLine = 4 | Whitespace, - Filespec = 5 | Whitespace, - Newline = 6 | Whitespace, - Empty = 7, - - Identifier = 8, - String, - CharLiteral, WCharLiteral, DCharLiteral, - - // Special tokens - FILE, - LINE, - DATE, - TIME, - TIMESTAMP, - VENDOR, - VERSION, - - // Number literals - Int32, Int64, Uint32, Uint64, - // Floating point number scanner relies on this order. (FloatXY + 3 == ImaginaryXY) - Float32, Float64, Float80, - Imaginary32, Imaginary64, Imaginary80, - - - // Brackets - LParen, - RParen, - LBracket, - RBracket, - LBrace, - RBrace, - - Dot, Slice, Ellipses, - - // Floating point number operators - Unordered, - UorE, - UorG, - UorGorE, - UorL, - UorLorE, - LorEorG, - LorG, - - // Normal operators - Assign, Equal, NotEqual, Not, - LessEqual, Less, - GreaterEqual, Greater, - LShiftAssign, LShift, - RShiftAssign,RShift, - URShiftAssign, URShift, - OrAssign, OrLogical, OrBinary, - AndAssign, AndLogical, AndBinary, - PlusAssign, PlusPlus, Plus, - MinusAssign, MinusMinus, Minus, - DivAssign, Div, - MulAssign, Mul, - ModAssign, Mod, - XorAssign, Xor, - CatAssign, Catenate, - Tilde, - Identity, NotIdentity, - - Colon, - Semicolon, - Question, - Comma, - Dollar, - - /* Keywords: - NB.: Token.isKeyword() depends on this list being contiguous. - */ - Abstract,Alias,Align,Asm,Assert,Auto,Body, - Bool,Break,Byte,Case,Cast,Catch,Cdouble, - Cent,Cfloat,Char,Class,Const,Continue,Creal, - Dchar,Debug,Default,Delegate,Delete,Deprecated,Do, - Double,Else,Enum,Export,Extern,False,Final, - Finally,Float,For,Foreach,Foreach_reverse,Function,Goto, - Idouble,If,Ifloat,Import,In,Inout,Int, - Interface,Invariant,Ireal,Is,Lazy,Long,Macro/+D2.0+/, - Mixin,Module,New,Null,Out,Override,Package, - Pragma,Private,Protected,Public,Real,Ref/+D2.0+/,Return, - Scope,Short,Static,Struct,Super,Switch,Synchronized, - Template,This,Throw,Traits/+D2.0+/,True,Try,Typedef,Typeid, - Typeof,Ubyte,Ucent,Uint,Ulong,Union,Unittest, - Ushort,Version,Void,Volatile,Wchar,While,With, - - HEAD, // start of linked list - EOF, - MAX -} - -alias TOK.Abstract KeywordsBegin; -alias TOK.With KeywordsEnd; -alias TOK.FILE SpecialTokensBegin; -alias TOK.VERSION SpecialTokensEnd; - -/// A table mapping each TOK to a string. -const string[] tokToString = [ - "Invalid", - - "Illegal", - "Comment", - "#! /shebang/", - "#line", - `"filespec"`, - "Newline", - "Empty", - - "Identifier", - "String", - "CharLiteral", "WCharLiteral", "DCharLiteral", - - "__FILE__", - "__LINE__", - "__DATE__", - "__TIME__", - "__TIMESTAMP__", - "__VENDOR__", - "__VERSION__", - - "Int32", "Int64", "Uint32", "Uint64", - "Float32", "Float64", "Float80", - "Imaginary32", "Imaginary64", "Imaginary80", - - "(", - ")", - "[", - "]", - "{", - "}", - - ".", "..", "...", - - "!<>=", // Unordered - "!<>", // UorE - "!<=", // UorG - "!<", // UorGorE - "!>=", // UorL - "!>", // UorLorE - "<>=", // LorEorG - "<>", // LorG - - "=", "==", "!=", "!", - "<=", "<", - ">=", ">", - "<<=", "<<", - ">>=",">>", - ">>>=", ">>>", - "|=", "||", "|", - "&=", "&&", "&", - "+=", "++", "+", - "-=", "--", "-", - "/=", "/", - "*=", "*", - "%=", "%", - "^=", "^", - "~=", "~", - "~", - "is", "!is", - - ":", - ";", - "?", - ",", - "$", - - "abstract","alias","align","asm","assert","auto","body", - "bool","break","byte","case","cast","catch","cdouble", - "cent","cfloat","char","class","const","continue","creal", - "dchar","debug","default","delegate","delete","deprecated","do", - "double","else","enum","export","extern","false","final", - "finally","float","for","foreach","foreach_reverse","function","goto", - "idouble","if","ifloat","import","in","inout","int", - "interface","invariant","ireal","is","lazy","long","macro", - "mixin","module","new","null","out","override","package", - "pragma","private","protected","public","real","ref","return", - "scope","short","static","struct","super","switch","synchronized", - "template","this","throw","__traits","true","try","typedef","typeid", - "typeof","ubyte","ucent","uint","ulong","union","unittest", - "ushort","version","void","volatile","wchar","while","with", - - "HEAD", - "EOF" -]; -static assert(tokToString.length == TOK.MAX);