1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity very high)
4 module dil.lexer.TokensEnum;
5 
6 import common;
7 
8 /// Enumeration of token kinds.
9 enum TOK : ushort
10 {
11   Invalid, /// Unitialized token.
12 
13   Illegal,  /// Unrecognized characters.
14   Comment,  /// // /**/ /++/
15   Shebang,  /// #!/bin/something
16   HashLine, /// #line
17   Filespec, /// "new/path"
18   Newline,  /// \n
19   Empty,    /// Special token with empty text.
20   LastWhitespace = Empty, /// End of whitespace tokens.
21 
22   Identifier,
23   String,
24   Character,
25 
26   SpecialID, /// __FILE__, __LINE__ etc.
27 
28   // Number literals
29   Int32, Int64, UInt32, UInt64,
30   // Floating point number scanner relies on this order.
31   // FloatXY + 3 == IFloatXY
32   Float32, Float64, Float80,
33   IFloat32, IFloat64, IFloat80,
34 
35   // Brackets
36   LParen,
37   RParen,
38   LBracket,
39   RBracket,
40   LBrace,
41   RBrace,
42 
43   // Dots
44   Dot, Dot2, Dot3,
45 
46   // Floating point number operators
47   Unordered,
48   UorE,
49   UorG,
50   UorGorE,
51   UorL,
52   UorLorE,
53   LorEorG,
54   LorG,
55 
56   // Normal operators
57   Equal,    Equal2,      EqlGreater,
58   Exclaim,  ExclaimEql,
59   Less,     LessEql,
60   Greater,  GreaterEql,
61   Less2,    Less2Eql,
62   Greater2, Greater2Eql,
63   Greater3, Greater3Eql,
64   Pipe,     PipeEql,     Pipe2,
65   Amp,      AmpEql,      Amp2,
66   Plus,     PlusEql,     Plus2,
67   Minus,    MinusEql,    Minus2,
68   Slash,    SlashEql,
69   Star,     StarEql,
70   Percent,  PercentEql,
71   Caret,    CaretEql,
72   Caret2,   Caret2Eql,
73   Tilde,    TildeEql,
74 
75   Colon,
76   Semicolon,
77   Question,
78   Comma,
79   Dollar,
80   At,
81 
82   /// Keywords:
83   /// NB.: Token.isKeyword() depends on this list being contiguous.
84   Abstract, Alias, Align, ArgTypes, Asm, Assert, Auto, Body, Break, Case, Cast,
85   Catch, Class, Const, Continue, Debug, Default, Delegate, Delete, Deprecated,
86   Do, Else, Enum, Export, Extern, False, Final, Finally, For, Foreach,
87   ForeachReverse, Function, Goto, Gshared, If, Immutable, Import, In, Inout,
88   Interface, Invariant, Is, Lazy, Macro, Mixin, Module, New, Nothrow, Null,
89   Out, OverloadSet, Override, Package, Parameters, Pragma, Private, Protected,
90   Public, Pure, Ref, Return, Shared, Scope, Static, Struct, Super, Switch,
91   Synchronized, Template, This, Throw, Traits, True, Try, Typedef, Typeid,
92   Typeof, Union, Unittest, Vector, Version, Volatile, While, With,
93   // Integral types.
94   Char,   Wchar,   Dchar, Bool,
95   Byte,   Ubyte,   Short, Ushort,
96   Int,    Uint,    Long,  Ulong,
97   Cent,   Ucent,
98   Float,  Double,  Real,
99   Ifloat, Idouble, Ireal,
100   Cfloat, Cdouble, Creal, Void,
101 
102   HEAD, // start of linked list
103   EOF,
104   MAX,
105 
106   // Some aliases for automatic code generation.
107   Overloadset = OverloadSet,
108   Foreach_reverse = ForeachReverse,
109 }
110 
111 alias KeywordsBegin = TOK.Abstract;
112 alias KeywordsEnd = TOK.Void;
113 alias IntegralTypeBegin = TOK.Char;
114 alias IntegralTypeEnd = TOK.Void;
115 
116 /// A table that maps each token kind to a string.
117 immutable string[TOK.MAX] tokToString = [
118   "Invalid",
119 
120   "Illegal",
121   "Comment",
122   "#!Shebang",
123   "#line",
124   "Filespec",
125   "Newline",
126   "Empty",
127 
128   "Identifier",
129   "String",
130   "Character",
131   "SpecialID",
132 
133   "Int32", "Int64", "UInt32", "UInt64",
134   "Float32", "Float64", "Float80",
135   "IFloat32", "IFloat64", "IFloat80",
136 
137   "(",
138   ")",
139   "[",
140   "]",
141   "{",
142   "}",
143 
144   ".", "..", "...",
145 
146   "!<>=", // Unordered
147   "!<>",  // UorE
148   "!<=",  // UorG
149   "!<",   // UorGorE
150   "!>=",  // UorL
151   "!>",   // UorLorE
152   "<>=",  // LorEorG
153   "<>",   // LorG
154 
155   "=",   "==", "=>",
156   "!",   "!=",
157   "<",   "<=",
158   ">",   ">=",
159   "<<",  "<<=",
160   ">>",  ">>=",
161   ">>>", ">>>=",
162   "|",   "|=", "||",
163   "&",   "&=", "&&",
164   "+",   "+=", "++",
165   "-",   "-=", "--",
166   "/",   "/=",
167   "*",   "*=",
168   "%",   "%=",
169   "^",   "^=",
170   "^^",  "^^=",
171   "~",   "~=",
172 
173   ":",
174   ";",
175   "?",
176   ",",
177   "$",
178   "@",
179 
180   "abstract","alias","align","__argTypes","asm","assert","auto","body",
181   "break","case","cast","catch",
182   "class","const","continue",
183   "debug","default","delegate","delete","deprecated","do",
184   "else","enum","export","extern","false","final",
185   "finally","for","foreach","foreach_reverse","function","goto","__gshared",
186   "if","immutable","import","in","inout",
187   "interface","invariant","is","lazy","macro",
188   "mixin","module","new","nothrow","null",
189   "out","__overloadset","override","package","__parameters",
190   "pragma","private","protected","public","pure","ref","return",
191   "shared","scope","static","struct","super","switch","synchronized",
192   "template","this","throw","__traits","true","try","typedef",
193   "typeid","typeof","union","unittest","__vector",
194   "version","volatile","while","with",
195   // Integral types.
196   "char",   "wchar",   "dchar", "bool",
197   "byte",   "ubyte",   "short", "ushort",
198   "int",    "uint",    "long",  "ulong",
199   "cent",   "ucent",
200   "float",  "double",  "real",
201   "ifloat", "idouble", "ireal",
202   "cfloat", "cdouble", "creal", "void",
203 
204   "HEAD",
205   "EOF"
206 ];
207 static assert(tokToString.length == TOK.EOF+1);
208 
209 /// Returns the string representation of a token kind.
210 cstring toString(TOK k)
211 {
212   return tokToString[k];
213 }
214 
215 /// A compile time associative array which maps Token strings to TOK values.
216 enum str2TOK = (){
217   TOK[string] aa;
218   TOK t;
219   foreach (s; tokToString)
220     aa[s] = t++;
221   return aa;
222 }();
223 
224 /// Converts a Token string to its respective TOK value at compile time.
225 template S2T(string s)
226 {
227   enum TOK S2T = str2TOK[s];
228 }
229 
230 /// Converts multiple Token strings and returns a Tuple of TOK values.
231 template S2T(Ss...)
232 {
233   static if (Ss.length == 1)
234     alias S2T = S2T!(Ss[0]);
235   else
236     alias S2T = Tuple!(S2T!(Ss[0]), S2T!(Ss[1..$]));
237 }