Mercurial > projects > dil
annotate trunk/src/dil/ast/Node.d @ 673:64fec49651cf
Renamed VariableDeclaration to VariablesDeclaration.
Removed TryCast and CastTo template functions.
Renamed Node.iS() to Node.Is().
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Fri, 18 Jan 2008 16:44:20 +0100 |
parents | f1325a4506de |
children | c4e3a34e40f1 |
rev | line source |
---|---|
142 | 1 /++ |
2 Author: Aziz Köksal | |
249 | 3 License: GPL3 |
142 | 4 +/ |
580
fa6d3c52757d
Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
543
diff
changeset
|
5 module dil.ast.Node; |
596
39fac5531b85
Moved dil.Token to dil.lexer.Token.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
6 |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
374
diff
changeset
|
7 import common; |
142 | 8 |
596
39fac5531b85
Moved dil.Token to dil.lexer.Token.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
9 public import dil.lexer.Token; |
635 | 10 public import dil.ast.NodesEnum; |
276
d6b2f7616ca5
- Added enum NodeKind to classify different nodes in the AST. Correspondingly added member 'kind' to class Node.
aziz
parents:
275
diff
changeset
|
11 |
281 | 12 /// This string is mixed into the constructor of a class that inherits from Node. |
276
d6b2f7616ca5
- Added enum NodeKind to classify different nodes in the AST. Correspondingly added member 'kind' to class Node.
aziz
parents:
275
diff
changeset
|
13 const string set_kind = `this.kind = mixin("NodeKind." ~ typeof(this).stringof);`; |
d6b2f7616ca5
- Added enum NodeKind to classify different nodes in the AST. Correspondingly added member 'kind' to class Node.
aziz
parents:
275
diff
changeset
|
14 |
142 | 15 class Node |
16 { | |
275 | 17 NodeCategory category; |
276
d6b2f7616ca5
- Added enum NodeKind to classify different nodes in the AST. Correspondingly added member 'kind' to class Node.
aziz
parents:
275
diff
changeset
|
18 NodeKind kind; |
292 | 19 Node[] children; |
189 | 20 Token* begin, end; |
21 | |
275 | 22 this(NodeCategory category) |
142 | 23 { |
671
f1325a4506de
Removed enum TID from dil.ast.Types.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
636
diff
changeset
|
24 assert(category != NodeCategory.Undefined); |
275 | 25 this.category = category; |
142 | 26 } |
189 | 27 |
28 void setTokens(Token* begin, Token* end) | |
29 { | |
30 this.begin = begin; | |
31 this.end = end; | |
32 } | |
489
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
33 |
543
c9579ea87228
Added ComplexExpression to enum NodeKind and setToks() to Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
34 Class setToks(Class)(Class node) |
c9579ea87228
Added ComplexExpression to enum NodeKind and setToks() to Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
35 { |
c9579ea87228
Added ComplexExpression to enum NodeKind and setToks() to Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
36 node.setTokens(this.begin, this.end); |
c9579ea87228
Added ComplexExpression to enum NodeKind and setToks() to Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
37 return node; |
c9579ea87228
Added ComplexExpression to enum NodeKind and setToks() to Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
38 } |
c9579ea87228
Added ComplexExpression to enum NodeKind and setToks() to Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
39 |
489
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
40 void addChild(Node child) |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
41 { |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
42 assert(child !is null, "failed in " ~ this.classinfo.name); |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
43 this.children ~= child; |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
44 } |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
45 |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
46 void addOptChild(Node child) |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
47 { |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
48 child is null || addChild(child); |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
49 } |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
50 |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
51 void addChildren(Node[] children) |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
52 { |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
53 assert(children !is null && delegate{ |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
54 foreach (child; children) |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
55 if (child is null) |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
56 return false; |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
57 return true; }(), |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
58 "failed in " ~ this.classinfo.name |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
59 ); |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
60 this.children ~= children; |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
61 } |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
62 |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
63 void addOptChildren(Node[] children) |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
64 { |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
65 children is null || addChildren(children); |
a7291d3ee9d7
Refactored classes that inherit from Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
488
diff
changeset
|
66 } |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
67 |
673
64fec49651cf
Renamed VariableDeclaration to VariablesDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
671
diff
changeset
|
68 Class Is(Class)() |
635 | 69 { |
70 if (kind == mixin("NodeKind." ~ typeof(Class).stringof)) | |
71 return cast(Class)cast(void*)this; | |
72 return null; | |
73 } | |
74 | |
636
15a0f37caabe
Added more visit() methods to dil.semantic.SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
635
diff
changeset
|
75 Class to(Class)() |
15a0f37caabe
Added more visit() methods to dil.semantic.SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
635
diff
changeset
|
76 { |
15a0f37caabe
Added more visit() methods to dil.semantic.SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
635
diff
changeset
|
77 return cast(Class)cast(void*)this; |
15a0f37caabe
Added more visit() methods to dil.semantic.SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
635
diff
changeset
|
78 } |
15a0f37caabe
Added more visit() methods to dil.semantic.SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
635
diff
changeset
|
79 |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
80 static bool isDoxygenComment(Token* token) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
81 { // Doxygen: '/+!' '/*!' '//!' |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
82 return token.type == TOK.Comment && token.start[2] == '!'; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
83 } |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
84 |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
85 static bool isDDocComment(Token* token) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
86 { // DDOC: '/++' '/**' '///' |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
87 return token.type == TOK.Comment && token.start[1] == token.start[2]; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
88 } |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
89 |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
90 /++ |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
91 Returns the surrounding documentation comment tokens. |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
92 Note: this function works correctly only if |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
93 the source text is syntactically correct. |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
94 +/ |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
95 Token*[] getDocComments(bool function(Token*) isDocComment = &isDDocComment) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
96 { |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
97 Token*[] comments; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
98 // Get preceding comments. |
613
7034d3f9e40c
Fixed Node.getDocComments().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
612
diff
changeset
|
99 auto token = begin; |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
100 // Scan backwards until we hit another declaration. |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
101 while (1) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
102 { |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
103 token = token.prev; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
104 if (token.type == TOK.LBrace || |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
105 token.type == TOK.RBrace || |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
106 token.type == TOK.Semicolon || |
618
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
107 token.type == TOK.HEAD || |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
108 (kind == NodeKind.EnumMember && token.type == TOK.Comma)) |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
109 break; |
618
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
110 |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
111 if (token.type == TOK.Comment) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
112 { |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
113 // Check that this comment doesn't belong to the previous declaration. |
618
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
114 if (kind == NodeKind.EnumMember && token.type == TOK.Comma) |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
115 break; |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
116 switch (token.prev.type) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
117 { |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
118 case TOK.Semicolon, TOK.RBrace: |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
119 break; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
120 default: |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
121 if (isDocComment(token)) |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
122 comments ~= token; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
123 } |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
124 } |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
125 } |
613
7034d3f9e40c
Fixed Node.getDocComments().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
612
diff
changeset
|
126 // Get single comment to the right. |
7034d3f9e40c
Fixed Node.getDocComments().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
612
diff
changeset
|
127 token = end.next; |
618
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
128 if (token.type == TOK.Comment && isDocComment(token)) |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
129 comments ~= token; |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
130 else if (kind == NodeKind.EnumMember) |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
131 { |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
132 token = end.nextNWS; |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
133 if (token.type == TOK.Comma) |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
134 { |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
135 token = token.next; |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
136 if (token.type == TOK.Comment && isDocComment(token)) |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
137 comments ~= token; |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
138 } |
07946b379006
Refactored the way dot expressions are parsed.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
613
diff
changeset
|
139 } |
612
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
140 return comments; |
c65b11c2074c
Added getDocComments() to class Node.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
141 } |
142 | 142 } |