1 | // |
---|
2 | // SwitchStatement.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@novell.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2009 Novell, Inc (http://www.novell.com) |
---|
8 | // |
---|
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | // of this software and associated documentation files (the "Software"), to deal |
---|
11 | // in the Software without restriction, including without limitation the rights |
---|
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | // copies of the Software, and to permit persons to whom the Software is |
---|
14 | // furnished to do so, subject to the following conditions: |
---|
15 | // |
---|
16 | // The above copyright notice and this permission notice shall be included in |
---|
17 | // all copies or substantial portions of the Software. |
---|
18 | // |
---|
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | // THE SOFTWARE. |
---|
26 | |
---|
27 | using System.Collections.Generic; |
---|
28 | using System.Linq; |
---|
29 | |
---|
30 | namespace ICSharpCode.NRefactory.CSharp |
---|
31 | { |
---|
32 | /// <summary> |
---|
33 | /// switch (Expression) { SwitchSections } |
---|
34 | /// </summary> |
---|
35 | public class SwitchStatement : Statement |
---|
36 | { |
---|
37 | public static readonly TokenRole SwitchKeywordRole = new TokenRole ("switch"); |
---|
38 | public static readonly Role<SwitchSection> SwitchSectionRole = new Role<SwitchSection>("SwitchSection"); |
---|
39 | |
---|
40 | public CSharpTokenNode SwitchToken { |
---|
41 | get { return GetChildByRole (SwitchKeywordRole); } |
---|
42 | } |
---|
43 | |
---|
44 | public CSharpTokenNode LParToken { |
---|
45 | get { return GetChildByRole (Roles.LPar); } |
---|
46 | } |
---|
47 | |
---|
48 | public Expression Expression { |
---|
49 | get { return GetChildByRole (Roles.Expression); } |
---|
50 | set { SetChildByRole (Roles.Expression, value); } |
---|
51 | } |
---|
52 | |
---|
53 | public CSharpTokenNode RParToken { |
---|
54 | get { return GetChildByRole (Roles.RPar); } |
---|
55 | } |
---|
56 | |
---|
57 | public CSharpTokenNode LBraceToken { |
---|
58 | get { return GetChildByRole (Roles.LBrace); } |
---|
59 | } |
---|
60 | |
---|
61 | public AstNodeCollection<SwitchSection> SwitchSections { |
---|
62 | get { return GetChildrenByRole (SwitchSectionRole); } |
---|
63 | } |
---|
64 | |
---|
65 | public CSharpTokenNode RBraceToken { |
---|
66 | get { return GetChildByRole (Roles.RBrace); } |
---|
67 | } |
---|
68 | |
---|
69 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
70 | { |
---|
71 | visitor.VisitSwitchStatement (this); |
---|
72 | } |
---|
73 | |
---|
74 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
75 | { |
---|
76 | return visitor.VisitSwitchStatement (this); |
---|
77 | } |
---|
78 | |
---|
79 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
80 | { |
---|
81 | return visitor.VisitSwitchStatement (this, data); |
---|
82 | } |
---|
83 | |
---|
84 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
85 | { |
---|
86 | SwitchStatement o = other as SwitchStatement; |
---|
87 | return o != null && this.Expression.DoMatch(o.Expression, match) && this.SwitchSections.DoMatch(o.SwitchSections, match); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | public class SwitchSection : AstNode |
---|
92 | { |
---|
93 | #region PatternPlaceholder |
---|
94 | public static implicit operator SwitchSection(PatternMatching.Pattern pattern) |
---|
95 | { |
---|
96 | return pattern != null ? new PatternPlaceholder(pattern) : null; |
---|
97 | } |
---|
98 | |
---|
99 | sealed class PatternPlaceholder : SwitchSection, PatternMatching.INode |
---|
100 | { |
---|
101 | readonly PatternMatching.Pattern child; |
---|
102 | |
---|
103 | public PatternPlaceholder(PatternMatching.Pattern child) |
---|
104 | { |
---|
105 | this.child = child; |
---|
106 | } |
---|
107 | |
---|
108 | public override NodeType NodeType { |
---|
109 | get { return NodeType.Pattern; } |
---|
110 | } |
---|
111 | |
---|
112 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
113 | { |
---|
114 | visitor.VisitPatternPlaceholder(this, child); |
---|
115 | } |
---|
116 | |
---|
117 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
118 | { |
---|
119 | return visitor.VisitPatternPlaceholder(this, child); |
---|
120 | } |
---|
121 | |
---|
122 | public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
---|
123 | { |
---|
124 | return visitor.VisitPatternPlaceholder(this, child, data); |
---|
125 | } |
---|
126 | |
---|
127 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
128 | { |
---|
129 | return child.DoMatch(other, match); |
---|
130 | } |
---|
131 | |
---|
132 | bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) |
---|
133 | { |
---|
134 | return child.DoMatchCollection(role, pos, match, backtrackingInfo); |
---|
135 | } |
---|
136 | } |
---|
137 | #endregion |
---|
138 | |
---|
139 | public static readonly Role<CaseLabel> CaseLabelRole = new Role<CaseLabel>("CaseLabel"); |
---|
140 | |
---|
141 | public override NodeType NodeType { |
---|
142 | get { |
---|
143 | return NodeType.Unknown; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | public AstNodeCollection<CaseLabel> CaseLabels { |
---|
148 | get { return GetChildrenByRole (CaseLabelRole); } |
---|
149 | } |
---|
150 | |
---|
151 | public AstNodeCollection<Statement> Statements { |
---|
152 | get { return GetChildrenByRole (Roles.EmbeddedStatement); } |
---|
153 | } |
---|
154 | |
---|
155 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
156 | { |
---|
157 | visitor.VisitSwitchSection (this); |
---|
158 | } |
---|
159 | |
---|
160 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
161 | { |
---|
162 | return visitor.VisitSwitchSection (this); |
---|
163 | } |
---|
164 | |
---|
165 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
166 | { |
---|
167 | return visitor.VisitSwitchSection (this, data); |
---|
168 | } |
---|
169 | |
---|
170 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
171 | { |
---|
172 | SwitchSection o = other as SwitchSection; |
---|
173 | return o != null && this.CaseLabels.DoMatch(o.CaseLabels, match) && this.Statements.DoMatch(o.Statements, match); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | public class CaseLabel : AstNode |
---|
178 | { |
---|
179 | public static readonly TokenRole CaseKeywordRole = new TokenRole ("case"); |
---|
180 | public static readonly TokenRole DefaultKeywordRole = new TokenRole ("default"); |
---|
181 | |
---|
182 | public override NodeType NodeType { |
---|
183 | get { |
---|
184 | return NodeType.Unknown; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /// <summary> |
---|
189 | /// Gets or sets the expression. The expression can be null - if the expression is null, it's the default switch section. |
---|
190 | /// </summary> |
---|
191 | public Expression Expression { |
---|
192 | get { return GetChildByRole (Roles.Expression); } |
---|
193 | set { SetChildByRole (Roles.Expression, value); } |
---|
194 | } |
---|
195 | |
---|
196 | public CSharpTokenNode ColonToken { |
---|
197 | get { return GetChildByRole (Roles.Colon); } |
---|
198 | } |
---|
199 | |
---|
200 | public CaseLabel () |
---|
201 | { |
---|
202 | } |
---|
203 | |
---|
204 | public CaseLabel (Expression expression) |
---|
205 | { |
---|
206 | this.Expression = expression; |
---|
207 | } |
---|
208 | |
---|
209 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
210 | { |
---|
211 | visitor.VisitCaseLabel (this); |
---|
212 | } |
---|
213 | |
---|
214 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
215 | { |
---|
216 | return visitor.VisitCaseLabel (this); |
---|
217 | } |
---|
218 | |
---|
219 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
220 | { |
---|
221 | return visitor.VisitCaseLabel (this, data); |
---|
222 | } |
---|
223 | |
---|
224 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
225 | { |
---|
226 | CaseLabel o = other as CaseLabel; |
---|
227 | return o != null && this.Expression.DoMatch(o.Expression, match); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|