1 | // |
---|
2 | // PreProcessorDirective.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@xamarin.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2011 Xamarin Inc. |
---|
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 | using System; |
---|
27 | using System.Collections.Generic; |
---|
28 | using System.Linq; |
---|
29 | |
---|
30 | namespace ICSharpCode.NRefactory.CSharp |
---|
31 | { |
---|
32 | public enum PreProcessorDirectiveType : byte |
---|
33 | { |
---|
34 | Invalid = 0, |
---|
35 | Region = 1, |
---|
36 | Endregion = 2, |
---|
37 | |
---|
38 | If = 3, |
---|
39 | Endif = 4, |
---|
40 | Elif = 5, |
---|
41 | Else = 6, |
---|
42 | |
---|
43 | Define = 7, |
---|
44 | Undef = 8, |
---|
45 | Error = 9, |
---|
46 | Warning = 10, |
---|
47 | Pragma = 11, |
---|
48 | Line = 12 |
---|
49 | } |
---|
50 | |
---|
51 | public class LinePreprocessorDirective : PreProcessorDirective |
---|
52 | { |
---|
53 | public int LineNumber { |
---|
54 | get; |
---|
55 | set; |
---|
56 | } |
---|
57 | |
---|
58 | public string FileName { |
---|
59 | get; |
---|
60 | set; |
---|
61 | } |
---|
62 | |
---|
63 | public LinePreprocessorDirective(TextLocation startLocation, TextLocation endLocation) : base (PreProcessorDirectiveType.Line, startLocation, endLocation) |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | public LinePreprocessorDirective(string argument = null) : base (PreProcessorDirectiveType.Line, argument) |
---|
68 | { |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | public class PragmaWarningPreprocessorDirective : PreProcessorDirective |
---|
73 | { |
---|
74 | public static readonly Role<PrimitiveExpression> WarningRole = new Role<PrimitiveExpression> ("Warning"); |
---|
75 | |
---|
76 | public static readonly TokenRole PragmaKeywordRole = new TokenRole ("#pragma"); |
---|
77 | public static readonly TokenRole WarningKeywordRole = new TokenRole ("warning"); |
---|
78 | public static readonly TokenRole DisableKeywordRole = new TokenRole ("disable"); |
---|
79 | public static readonly TokenRole RestoreKeywordRole = new TokenRole ("restore"); |
---|
80 | |
---|
81 | public bool Disable { |
---|
82 | get { |
---|
83 | return !DisableToken.IsNull; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | public CSharpTokenNode PragmaToken { |
---|
88 | get { return GetChildByRole (PragmaKeywordRole); } |
---|
89 | } |
---|
90 | |
---|
91 | public CSharpTokenNode WarningToken { |
---|
92 | get { return GetChildByRole (WarningKeywordRole); } |
---|
93 | } |
---|
94 | |
---|
95 | public CSharpTokenNode DisableToken { |
---|
96 | get { return GetChildByRole (DisableKeywordRole); } |
---|
97 | } |
---|
98 | |
---|
99 | public CSharpTokenNode RestoreToken { |
---|
100 | get { return GetChildByRole (RestoreKeywordRole); } |
---|
101 | } |
---|
102 | |
---|
103 | public AstNodeCollection<PrimitiveExpression> Warnings { |
---|
104 | get { return GetChildrenByRole(WarningRole); } |
---|
105 | } |
---|
106 | |
---|
107 | public override TextLocation EndLocation { |
---|
108 | get { |
---|
109 | var child = LastChild; |
---|
110 | if (child == null) |
---|
111 | return base.EndLocation; |
---|
112 | return child.EndLocation; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | public PragmaWarningPreprocessorDirective(TextLocation startLocation, TextLocation endLocation) : base (PreProcessorDirectiveType.Pragma, startLocation, endLocation) |
---|
117 | { |
---|
118 | } |
---|
119 | |
---|
120 | public PragmaWarningPreprocessorDirective(string argument = null) : base (PreProcessorDirectiveType.Pragma, argument) |
---|
121 | { |
---|
122 | } |
---|
123 | |
---|
124 | public bool IsDefined(int pragmaWarning) |
---|
125 | { |
---|
126 | return Warnings.Select(w => (int)w.Value).Any(n => n == pragmaWarning); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | public class PreProcessorDirective : AstNode |
---|
131 | { |
---|
132 | public override NodeType NodeType { |
---|
133 | get { |
---|
134 | return NodeType.Whitespace; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | public PreProcessorDirectiveType Type { |
---|
139 | get; |
---|
140 | set; |
---|
141 | } |
---|
142 | |
---|
143 | public string Argument { |
---|
144 | get; |
---|
145 | set; |
---|
146 | } |
---|
147 | |
---|
148 | /// <summary> |
---|
149 | /// For an '#if' directive, specifies whether the condition evaluated to true. |
---|
150 | /// </summary> |
---|
151 | public bool Take { |
---|
152 | get; |
---|
153 | set; |
---|
154 | } |
---|
155 | |
---|
156 | TextLocation startLocation; |
---|
157 | public override TextLocation StartLocation { |
---|
158 | get { |
---|
159 | return startLocation; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | TextLocation endLocation; |
---|
164 | public override TextLocation EndLocation { |
---|
165 | get { |
---|
166 | return endLocation; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | public PreProcessorDirective(PreProcessorDirectiveType type, TextLocation startLocation, TextLocation endLocation) |
---|
171 | { |
---|
172 | this.Type = type; |
---|
173 | this.startLocation = startLocation; |
---|
174 | this.endLocation = endLocation; |
---|
175 | } |
---|
176 | |
---|
177 | public PreProcessorDirective(PreProcessorDirectiveType type, string argument = null) |
---|
178 | { |
---|
179 | this.Type = type; |
---|
180 | this.Argument = argument; |
---|
181 | } |
---|
182 | |
---|
183 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
184 | { |
---|
185 | visitor.VisitPreProcessorDirective (this); |
---|
186 | } |
---|
187 | |
---|
188 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
189 | { |
---|
190 | return visitor.VisitPreProcessorDirective (this); |
---|
191 | } |
---|
192 | |
---|
193 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
194 | { |
---|
195 | return visitor.VisitPreProcessorDirective (this, data); |
---|
196 | } |
---|
197 | |
---|
198 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
199 | { |
---|
200 | PreProcessorDirective o = other as PreProcessorDirective; |
---|
201 | return o != null && Type == o.Type && MatchString(Argument, o.Argument); |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|