1 // D grammar - according to http://dlang.org/grammar 2 3 module ddc.lexer.Lexer; 4 import ddc.lexer.tokenizer; 5 6 /** Lexem type constants */ 7 enum LexemType : ushort { 8 UNKNOWN, 9 // types 10 TYPE, 11 TYPE_CTORS, 12 TYPE_CTOR, 13 BASIC_TYPE, 14 BASIC_TYPE_X, 15 BASIC_TYPE_2, 16 IDENTIFIER_LIST, 17 IDENTIFIER, 18 TYPEOF, 19 // templates 20 TEMPLATE_INSTANCE, 21 EXPRESSION, 22 ALT_DECLARATOR, 23 } 24 25 class Lexem { 26 public @property LexemType type() { return LexemType.UNKNOWN; } 27 } 28 29 /** 30 Returns true for one of keywords: bool, byte, ubyte, short, ushort, int, uint, long, ulong, 31 char, wchar, dchar, float, double, real, ifloat, idouble, ireal, cfloat, cdouble, creal, void 32 */ 33 bool isBasicTypeXToken(Token token) { 34 if (token.type != TokenType.KEYWORD) 35 return false; 36 Keyword id = token.keyword; 37 return id == Keyword.BOOL 38 || id == Keyword.BYTE 39 || id == Keyword.UBYTE 40 || id == Keyword.SHORT 41 || id == Keyword.USHORT 42 || id == Keyword.INT 43 || id == Keyword.UINT 44 || id == Keyword.LONG 45 || id == Keyword.ULONG 46 || id == Keyword.CHAR 47 || id == Keyword.WCHAR 48 || id == Keyword.DCHAR 49 || id == Keyword.FLOAT 50 || id == Keyword.DOUBLE 51 || id == Keyword.REAL 52 || id == Keyword.IFLOAT 53 || id == Keyword.IDOUBLE 54 || id == Keyword.IREAL 55 || id == Keyword.CFLOAT 56 || id == Keyword.CDOUBLE 57 || id == Keyword.CREAL 58 || id == Keyword.VOID; 59 } 60 61 /** 62 Single token, one of keywords: bool, byte, ubyte, short, ushort, int, uint, long, ulong, 63 char, wchar, dchar, float, double, real, ifloat, idouble, ireal, cfloat, cdouble, creal, void 64 */ 65 class BasicTypeX : Lexem { 66 public Token _token; 67 public override @property LexemType type() { return LexemType.BASIC_TYPE_X; } 68 public this(Token token) 69 in { 70 assert(isBasicTypeXToken(token)); 71 } 72 body { 73 _token = token; 74 } 75 } 76 77 /** 78 Returns true for one of keywords: const, immutable, inout, shared 79 */ 80 bool isTypeCtorToken(Token token) { 81 if (token.type != TokenType.KEYWORD) 82 return false; 83 Keyword id = token.keyword; 84 return id == Keyword.CONST 85 || id == Keyword.IMMUTABLE 86 || id == Keyword.INOUT 87 || id == Keyword.SHARED; 88 } 89 90 /** 91 Single token, one of keywords: const, immutable, inout, shared 92 */ 93 class TypeCtor : Lexem { 94 public Token _token; 95 public override @property LexemType type() { return LexemType.TYPE_CTOR; } 96 public this(Token token) 97 in { 98 assert(isTypeCtorToken(token)); 99 } 100 body { 101 _token = token; 102 } 103 } 104 105 /** 106 Zero, one or several keywords: const, immutable, inout, shared 107 */ 108 class TypeCtors : Lexem { 109 public TypeCtor[] _list; 110 public override @property LexemType type() { return LexemType.TYPE_CTORS; } 111 public this(Token token) 112 in { 113 assert(isTypeCtorToken(token)); 114 } 115 body { 116 _list ~= new TypeCtor(token); 117 } 118 public void append(Token token) 119 in { 120 assert(isTypeCtorToken(token)); 121 } 122 body { 123 _list ~= new TypeCtor(token); 124 } 125 } 126 127 /** 128 Identifier. 129 */ 130 class Identifier : Lexem { 131 IdentToken _token; 132 public override @property LexemType type() { return LexemType.IDENTIFIER; } 133 public this(Token identifier) 134 in { 135 assert(identifier.type == TokenType.IDENTIFIER); 136 } 137 body { 138 _token = cast(IdentToken)identifier; 139 } 140 } 141 142 /** 143 Identifier list. 144 145 IdentifierList: 146 Identifier 147 Identifier . IdentifierList 148 TemplateInstance 149 TemplateInstance . IdentifierList 150 */ 151 class IdentifierList : Lexem { 152 public Identifier _identifier; 153 public IdentifierList _identifierList; 154 public TemplateInstance _templateInstance; 155 public override @property LexemType type() { return LexemType.IDENTIFIER_LIST; } 156 public this(Token ident, IdentifierList identifierList = null) 157 in { 158 assert(ident.type == TokenType.IDENTIFIER); 159 } 160 body { 161 _identifier = new Identifier(ident); 162 _identifierList = identifierList; 163 } 164 public this(TemplateInstance templateInstance, IdentifierList identifierList = null) 165 in { 166 } 167 body { 168 _templateInstance = templateInstance; 169 _identifierList = identifierList; 170 } 171 } 172 173 /** 174 Template instance. 175 176 TemplateInstance: 177 Identifier TemplateArguments 178 */ 179 class TemplateInstance : Lexem { 180 public override @property LexemType type() { return LexemType.TEMPLATE_INSTANCE; } 181 public this() 182 in { 183 } 184 body { 185 } 186 } 187 188 /** 189 Basic type. 190 191 BasicType: 192 BasicTypeX 193 . IdentifierList 194 IdentifierList 195 Typeof 196 Typeof . IdentifierList 197 TypeCtor ( Type ) 198 */ 199 class BasicType : Lexem { 200 public BasicTypeX _basicTypeX; 201 public IdentifierList _identifierList; 202 public Typeof _typeof; 203 public TypeCtor _typeCtor; 204 public Type _typeCtorType; 205 public bool _dotBeforeIdentifierList; 206 public override @property LexemType type() { return LexemType.BASIC_TYPE; } 207 public this() 208 in { 209 } 210 body { 211 } 212 } 213 214 215 216 /** 217 Typeof. 218 219 Typeof: 220 typeof ( Expression ) 221 typeof ( return ) 222 223 For typeof(return), _expression is null 224 */ 225 class Typeof : Lexem { 226 public Expression _expression; 227 public override @property LexemType type() { return LexemType.TYPEOF; } 228 public this(Expression expression) 229 in { 230 } 231 body { 232 _expression = expression; 233 } 234 } 235 236 /** 237 Type. 238 239 */ 240 class Type : Lexem { 241 public TypeCtors _typeCtors; 242 public BasicType _basicType; 243 public AltDeclarator _altDeclarator; 244 public override @property LexemType type() { return LexemType.TYPE; } 245 public this() 246 in { 247 } 248 body { 249 } 250 } 251 252 /** 253 Expression. 254 255 Expression: 256 */ 257 class Expression : Lexem { 258 public override @property LexemType type() { return LexemType.EXPRESSION; } 259 public this() 260 in { 261 } 262 body { 263 } 264 } 265 266 /** 267 AltDeclarator. 268 269 AltDeclarator: 270 */ 271 class AltDeclarator : Lexem { 272 public override @property LexemType type() { return LexemType.ALT_DECLARATOR; } 273 public this() 274 in { 275 } 276 body { 277 } 278 } 279 280 class Lexer 281 { 282 }