1 | // |
---|
2 | // Identifier.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; |
---|
28 | |
---|
29 | namespace ICSharpCode.NRefactory.CSharp |
---|
30 | { |
---|
31 | public class Identifier : AstNode |
---|
32 | { |
---|
33 | public new static readonly Identifier Null = new NullIdentifier (); |
---|
34 | sealed class NullIdentifier : Identifier |
---|
35 | { |
---|
36 | public override bool IsNull { |
---|
37 | get { |
---|
38 | return true; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
43 | { |
---|
44 | visitor.VisitNullNode(this); |
---|
45 | } |
---|
46 | |
---|
47 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
48 | { |
---|
49 | return visitor.VisitNullNode(this); |
---|
50 | } |
---|
51 | |
---|
52 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
53 | { |
---|
54 | return visitor.VisitNullNode(this, data); |
---|
55 | } |
---|
56 | |
---|
57 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
58 | { |
---|
59 | return other == null || other.IsNull; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | public override NodeType NodeType { |
---|
64 | get { |
---|
65 | return NodeType.Token; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | string name; |
---|
70 | public string Name { |
---|
71 | get { return this.name; } |
---|
72 | set { |
---|
73 | if (value == null) |
---|
74 | throw new ArgumentNullException("value"); |
---|
75 | ThrowIfFrozen(); |
---|
76 | this.name = value; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | TextLocation startLocation; |
---|
81 | public override TextLocation StartLocation { |
---|
82 | get { |
---|
83 | return startLocation; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | internal void SetStartLocation(TextLocation value) |
---|
88 | { |
---|
89 | ThrowIfFrozen(); |
---|
90 | this.startLocation = value; |
---|
91 | } |
---|
92 | |
---|
93 | const uint verbatimBit = 1u << AstNodeFlagsUsedBits; |
---|
94 | |
---|
95 | public bool IsVerbatim { |
---|
96 | get { |
---|
97 | return (flags & verbatimBit) != 0; |
---|
98 | } |
---|
99 | set { |
---|
100 | ThrowIfFrozen(); |
---|
101 | if (value) |
---|
102 | flags |= verbatimBit; |
---|
103 | else |
---|
104 | flags &= ~verbatimBit; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | public override TextLocation EndLocation { |
---|
109 | get { |
---|
110 | return new TextLocation (StartLocation.Line, StartLocation.Column + (Name ?? "").Length + (IsVerbatim ? 1 : 0)); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | Identifier () |
---|
115 | { |
---|
116 | this.name = string.Empty; |
---|
117 | } |
---|
118 | |
---|
119 | protected Identifier (string name, TextLocation location) |
---|
120 | { |
---|
121 | if (name == null) |
---|
122 | throw new ArgumentNullException("name"); |
---|
123 | this.Name = name; |
---|
124 | this.startLocation = location; |
---|
125 | } |
---|
126 | |
---|
127 | public static Identifier Create (string name) |
---|
128 | { |
---|
129 | return Create (name, TextLocation.Empty); |
---|
130 | } |
---|
131 | |
---|
132 | public static Identifier Create (string name, TextLocation location) |
---|
133 | { |
---|
134 | if (string.IsNullOrEmpty(name)) |
---|
135 | return Identifier.Null; |
---|
136 | if (name[0] == '@') |
---|
137 | return new Identifier (name.Substring (1), new TextLocation (location.Line, location.Column + 1)) { IsVerbatim = true }; |
---|
138 | else |
---|
139 | return new Identifier (name, location); |
---|
140 | } |
---|
141 | |
---|
142 | public static Identifier Create (string name, TextLocation location, bool isVerbatim) |
---|
143 | { |
---|
144 | if (string.IsNullOrEmpty (name)) |
---|
145 | return Identifier.Null; |
---|
146 | |
---|
147 | if (isVerbatim) |
---|
148 | return new Identifier (name, location) { IsVerbatim = true }; |
---|
149 | return new Identifier (name, location); |
---|
150 | } |
---|
151 | |
---|
152 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
153 | { |
---|
154 | visitor.VisitIdentifier (this); |
---|
155 | } |
---|
156 | |
---|
157 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
158 | { |
---|
159 | return visitor.VisitIdentifier (this); |
---|
160 | } |
---|
161 | |
---|
162 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
163 | { |
---|
164 | return visitor.VisitIdentifier (this, data); |
---|
165 | } |
---|
166 | |
---|
167 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
168 | { |
---|
169 | Identifier o = other as Identifier; |
---|
170 | return o != null && !o.IsNull && MatchString(this.Name, o.Name); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|