comparison trunk/src/dil/Parser.d @ 492:9c208925a3d4

Added module ImportParser and new stuff from DMD2.008. Moved ImportParser from module Parser to its own module. Added a few methods from class Parser and simplified them. Now protection attributes are taken into consideration as well. Added class TemplateThisParameter. Adapted TypeofType so that typeof(return) can be recognized.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 06 Dec 2007 22:19:55 +0100
parents b991f49628a8
children d13502b6fa5f
comparison
equal deleted inserted replaced
491:b991f49628a8 492:9c208925a3d4
15 import dil.Enums; 15 import dil.Enums;
16 import common; 16 import common;
17 17
18 private alias TOK T; 18 private alias TOK T;
19 19
20 class ImportParser : Parser 20 /++
21 { 21 The Parser produces an abstract syntax tree (AST) by analyzing
22 this(char[] srcText, string fileName) 22 the tokens of the provided source code.
23 { 23 +/
24 super(srcText, fileName);
25 }
26
27 override Declarations start()
28 {
29 auto decls = new Declarations;
30 super.init();
31 if (token.type == T.Module)
32 decls ~= parseModuleDeclaration();
33 while (token.type != T.EOF)
34 {
35 if (token.type == T.Import)
36 {
37 auto decl = parseImportDeclaration();
38 assert(decl && decl.kind == NodeKind.ImportDeclaration);
39 super.imports ~= cast(ImportDeclaration)cast(void*)decl;
40 decls ~= decl;
41 }
42 else
43 nT();
44 }
45 return decls;
46 }
47 }
48
49 class Parser 24 class Parser
50 { 25 {
51 Lexer lx; 26 Lexer lx;
52 Token* token; /// Current non-whitespace token. 27 Token* token; /// Current non-whitespace token.
53 Token* prevToken; /// Previous non-whitespace token. 28 Token* prevToken; /// Previous non-whitespace token.
59 this(char[] srcText, string filePath, InformationManager infoMan = null) 34 this(char[] srcText, string filePath, InformationManager infoMan = null)
60 { 35 {
61 lx = new Lexer(srcText, filePath); 36 lx = new Lexer(srcText, filePath);
62 } 37 }
63 38
64 private void init() 39 protected void init()
65 { 40 {
66 nT(); 41 nT();
67 prevToken = token; 42 prevToken = token;
68 } 43 }
69 44
450 // ( TemplateParameterList ) ( ParameterList ) 425 // ( TemplateParameterList ) ( ParameterList )
451 tparams = parseTemplateParameterList(); 426 tparams = parseTemplateParameterList();
452 } 427 }
453 428
454 auto params = parseParameterList(); 429 auto params = parseParameterList();
430 version(D2)
431 {
432 switch (token.type)
433 {
434 case T.Const:
435 stc |= StorageClass.Const;
436 nT();
437 break;
438 case T.Invariant:
439 stc |= StorageClass.Invariant;
440 nT();
441 break;
442 default:
443 }
444 }
455 // ReturnType FunctionName ( ParameterList ) 445 // ReturnType FunctionName ( ParameterList )
456 auto funcBody = parseFunctionBody(); 446 auto funcBody = parseFunctionBody();
457 return set(new FunctionDeclaration(type, ident, tparams, params, funcBody), begin); 447 return set(new FunctionDeclaration(type, ident, tparams, params, funcBody, stc), begin);
458 } 448 }
459 type = parseDeclaratorSuffix(type); 449 type = parseDeclaratorSuffix(type);
460 } 450 }
461 } 451 }
462 452
1418 // TODO: only one parameter of type void* allowed. Check in parsing or semantic phase? 1408 // TODO: only one parameter of type void* allowed. Check in parsing or semantic phase?
1419 auto funcBody = parseFunctionBody(); 1409 auto funcBody = parseFunctionBody();
1420 return new DeleteDeclaration(parameters, funcBody); 1410 return new DeleteDeclaration(parameters, funcBody);
1421 } 1411 }
1422 1412
1413 Type parseTypeofType()
1414 {
1415 assert(token.type == T.Typeof);
1416 Type type;
1417 requireNext(T.LParen);
1418 switch (token.type)
1419 {
1420 version(D2)
1421 {
1422 case T.Return:
1423 nT();
1424 type = new TypeofType();
1425 break;
1426 }
1427 default:
1428 type = new TypeofType(parseExpression());
1429 }
1430 require(T.RParen);
1431 return type;
1432 }
1433
1423 /+ 1434 /+
1424 DotListExpression: 1435 DotListExpression:
1425 . DotListItems 1436 . DotListItems
1426 DotListItems 1437 DotListItems
1427 Typeof 1438 Typeof
1446 nT(); 1457 nT();
1447 identList ~= set(new DotExpression(), begin); 1458 identList ~= set(new DotExpression(), begin);
1448 } 1459 }
1449 else if (token.type == T.Typeof) 1460 else if (token.type == T.Typeof)
1450 { 1461 {
1451 requireNext(T.LParen); 1462 auto type = parseTypeofType();
1452 auto type = new TypeofType(parseExpression());
1453 require(T.RParen);
1454 set(type, begin); 1463 set(type, begin);
1455 identList ~= set(new TypeofExpression(type), begin); 1464 identList ~= set(new TypeofExpression(type), begin);
1456 if (token.type != T.Dot) 1465 if (token.type != T.Dot)
1457 goto Lreturn; 1466 goto Lreturn;
1458 nT(); 1467 nT();
1513 nT(); 1522 nT();
1514 identList ~= set(new DotType(), begin); 1523 identList ~= set(new DotType(), begin);
1515 } 1524 }
1516 else if (token.type == T.Typeof) 1525 else if (token.type == T.Typeof)
1517 { 1526 {
1518 requireNext(T.LParen); 1527 identList ~= set(parseTypeofType(), begin);
1519 auto type = new TypeofType(parseExpression());
1520 require(T.RParen);
1521 identList ~= set(type, begin);
1522 if (token.type != T.Dot) 1528 if (token.type != T.Dot)
1523 goto Lreturn; 1529 goto Lreturn;
1524 nT(); 1530 nT();
1525 } 1531 }
1526 1532
4320 // Declarator 4326 // Declarator
4321 ident = null; 4327 ident = null;
4322 goto LTemplateValueParameter; 4328 goto LTemplateValueParameter;
4323 } 4329 }
4324 break; 4330 break;
4331 version(D2)
4332 {
4333 case T.This:
4334 // TemplateThisParameter
4335 // this TemplateTypeParameter
4336 nT(); // Skip 'this' keyword.
4337 ident = requireId();
4338 parseSpecAndOrDefaultType();
4339 tp = new TemplateThisParameter(ident, specType, defType);
4340 break;
4341 }
4325 default: 4342 default:
4326 LTemplateValueParameter: 4343 LTemplateValueParameter:
4327 // TemplateValueParameter: 4344 // TemplateValueParameter:
4328 // Declarator 4345 // Declarator
4329 Expression specValue, defValue; 4346 Expression specValue, defValue;