comparison trunk/src/Parser.d @ 117:79857de26e86

- Moved class Parameter to module Types. Added struct Parameters. - Implemented parseConstructorDeclaration(). - Added method stub parseStatements(). - Restructured parseBaseClasses() a bit.
author aziz
date Mon, 09 Jul 2007 16:36:05 +0000
parents f0c1883cdd4c
children 379f33cbd521
comparison
equal deleted inserted replaced
116:f0c1883cdd4c 117:79857de26e86
9 import Information; 9 import Information;
10 import Declarations; 10 import Declarations;
11 import Statements; 11 import Statements;
12 import Expressions; 12 import Expressions;
13 import Types; 13 import Types;
14
15 class Parameter
16 {
17 StorageClass stc;
18 Type type;
19 string ident;
20 Expression assignExpr;
21
22 this(StorageClass stc, Type type, string ident, Expression assignExpr)
23 {
24 this.stc = stc;
25 this.type = type;
26 this.ident = ident;
27 this.assignExpr = assignExpr;
28 }
29
30 bool isVariadic()
31 {
32 return !!(stc & StorageClass.Variadic);
33 }
34
35 bool isOnlyVariadic()
36 {
37 return stc == StorageClass.Variadic;
38 }
39 }
40 14
41 private alias TOK T; 15 private alias TOK T;
42 16
43 class Parser 17 class Parser
44 { 18 {
154 decl = parseInterfaceDeclaration(); 128 decl = parseInterfaceDeclaration();
155 break; 129 break;
156 case T.Struct, T.Union: 130 case T.Struct, T.Union:
157 decl = parseAggregateDeclaration(); 131 decl = parseAggregateDeclaration();
158 break; 132 break;
133 case T.This:
134 decl = parseConstructorDeclaration();
135 break;
159 case T.Module: 136 case T.Module:
160 // Error: module is optional and can only appear once at the top of the source file. 137 // Error: module is optional and can only appear once at the top of the source file.
161 break; 138 break;
162 default: 139 default:
163 } 140 }
141 return null;
142 }
143
144 Statement[] parseStatements()
145 {
164 return null; 146 return null;
165 } 147 }
166 148
167 Declaration parseAttributeSpecifier() 149 Declaration parseAttributeSpecifier()
168 { 150 {
355 BaseClass[] parseBaseClasses() 337 BaseClass[] parseBaseClasses()
356 { 338 {
357 assert(token.type == T.Colon); 339 assert(token.type == T.Colon);
358 340
359 BaseClass[] bases; 341 BaseClass[] bases;
360 Protection prot; 342
361 343 nT(); // Skip colon
362 nT();
363 344
364 while (1) 345 while (1)
365 { 346 {
366 prot = Protection.Public; 347 Protection prot = Protection.Public;
348 string name;
367 switch (token.type) 349 switch (token.type)
368 { 350 {
369 case T.Identifier: goto LisIdentifier; 351 case T.Identifier: goto LisIdentifier;
370 case T.Private: prot = Protection.Private; break; 352 case T.Private: prot = Protection.Private; break;
371 case T.Protected: prot = Protection.Protected; break; 353 case T.Protected: prot = Protection.Protected; break;
375 default: 357 default:
376 // TODO: issue error msg 358 // TODO: issue error msg
377 return bases; 359 return bases;
378 } 360 }
379 nT(); 361 nT();
380 string ident;
381 if (token.type == T.Identifier) 362 if (token.type == T.Identifier)
382 { 363 {
383 LisIdentifier: 364 LisIdentifier:
384 ident = token.identifier; 365 name = token.identifier;
385 nT(); 366 nT();
386 // TODO: handle template instantiations: class Foo : Bar!(int) 367 // TODO: handle template instantiations: class Foo : Bar!(int)
387 } 368 }
388 else 369 else
389 errorIfNot(T.Identifier); 370 errorIfNot(T.Identifier);
390 bases ~= new BaseClass(prot, ident); 371 bases ~= new BaseClass(prot, name);
391 if (token.type != T.Comma) 372 if (token.type != T.Comma)
392 break; 373 break;
393 } 374 }
394 return bases; 375 return bases;
395 } 376 }
477 458
478 if (tok == T.Struct) 459 if (tok == T.Struct)
479 return new StructDeclaration(name, decls, hasDefinition); 460 return new StructDeclaration(name, decls, hasDefinition);
480 else 461 else
481 return new UnionDeclaration(name, decls, hasDefinition); 462 return new UnionDeclaration(name, decls, hasDefinition);
463 }
464
465 Declaration parseConstructorDeclaration()
466 {
467 assert(token.type == T.This);
468 nT(); // Skip 'this' keyword.
469 auto parameters = parseParameters();
470 require(T.LBrace);
471 auto statements = parseStatements();
472 require(T.RBrace);
473 return new ConstructorDeclaration(parameters, statements);
482 } 474 }
483 475
484 /+++++++++++++++++++++++++++++ 476 /+++++++++++++++++++++++++++++
485 + Expression parsing methods + 477 + Expression parsing methods +
486 +++++++++++++++++++++++++++++/ 478 +++++++++++++++++++++++++++++/
1234 errorIfNot(T.Identifier); 1226 errorIfNot(T.Identifier);
1235 1227
1236 return t; 1228 return t;
1237 } 1229 }
1238 1230
1239 Parameter[] parseParameters() 1231 Parameters parseParameters()
1240 out(args) 1232 out(params)
1241 { 1233 {
1242 if (args.length > 1) 1234 if (params.length > 1)
1243 foreach (arg; args[0..$-1]) 1235 foreach (param; params.items[0..$-1])
1244 { 1236 {
1245 if (arg.isVariadic()) 1237 if (param.isVariadic())
1246 assert(0, "variadic arguments can only appear at the end of the parameter list."); 1238 assert(0, "variadic arguments can only appear at the end of the parameter list.");
1247 } 1239 }
1248 } 1240 }
1249 body 1241 body
1250 { 1242 {
1251 require(T.LParen); 1243 require(T.LParen);
1252 1244
1253 if (token.type == T.RParen) 1245 if (token.type == T.RParen)
1254 { 1246 {
1255 nT(); 1247 nT();
1256 return null; 1248 return Parameters.init;
1257 } 1249 }
1258 1250
1259 Parameter[] args; 1251 Parameters params;
1260 StorageClass stc; 1252 StorageClass stc;
1261 1253
1262 while (1) 1254 while (1)
1263 { 1255 {
1264 stc = StorageClass.In; 1256 stc = StorageClass.In;
1267 case T.In: stc = StorageClass.In; nT(); goto default; 1259 case T.In: stc = StorageClass.In; nT(); goto default;
1268 case T.Out: stc = StorageClass.Out; nT(); goto default; 1260 case T.Out: stc = StorageClass.Out; nT(); goto default;
1269 case T.Ref: stc = StorageClass.Ref; nT(); goto default; 1261 case T.Ref: stc = StorageClass.Ref; nT(); goto default;
1270 case T.Lazy: stc = StorageClass.Lazy; nT(); goto default; 1262 case T.Lazy: stc = StorageClass.Lazy; nT(); goto default;
1271 case T.Ellipses: 1263 case T.Ellipses:
1272 args ~= new Parameter(StorageClass.Variadic, null, null, null); 1264 params ~= new Parameter(StorageClass.Variadic, null, null, null);
1273 break; 1265 break;
1274 default: 1266 default:
1275 string ident; 1267 string ident;
1276 auto type = parseDeclarator(ident); 1268 auto type = parseDeclarator(ident);
1277 1269
1283 } 1275 }
1284 1276
1285 if (token.type == T.Ellipses) 1277 if (token.type == T.Ellipses)
1286 { 1278 {
1287 stc |= StorageClass.Variadic; 1279 stc |= StorageClass.Variadic;
1288 args ~= new Parameter(stc, type, ident, assignExpr); 1280 params ~= new Parameter(stc, type, ident, assignExpr);
1289 nT(); 1281 nT();
1290 break; 1282 break;
1291 } 1283 }
1292 1284
1293 args ~= new Parameter(stc, type, ident, assignExpr); 1285 params ~= new Parameter(stc, type, ident, assignExpr);
1294 if (token.type == T.Comma) 1286 if (token.type == T.Comma)
1295 { 1287 {
1296 nT(); 1288 nT();
1297 continue; 1289 continue;
1298 } 1290 }
1299 break; 1291 break;
1300 } 1292 }
1301 break; 1293 break;
1302 } 1294 }
1303 require(T.RParen); 1295 require(T.RParen);
1304 return args; 1296 return params;
1305 } 1297 }
1306 1298
1307 void errorIfNot(TOK tok) 1299 void errorIfNot(TOK tok)
1308 { 1300 {
1309 if (token.type != tok) 1301 if (token.type != tok)