1 | // |
---|
2 | // NamespaceDeclaration.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 | using System.Collections.Generic; |
---|
27 | using System.Linq; |
---|
28 | using System.Text; |
---|
29 | using System; |
---|
30 | |
---|
31 | namespace ICSharpCode.NRefactory.CSharp |
---|
32 | { |
---|
33 | /// <summary> |
---|
34 | /// namespace Name { Members } |
---|
35 | /// </summary> |
---|
36 | public class NamespaceDeclaration : AstNode |
---|
37 | { |
---|
38 | public static readonly Role<AstNode> MemberRole = SyntaxTree.MemberRole; |
---|
39 | public static readonly Role<AstType> NamespaceNameRole = new Role<AstType>("NamespaceName", AstType.Null); |
---|
40 | |
---|
41 | public override NodeType NodeType { |
---|
42 | get { |
---|
43 | return NodeType.Unknown; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | public CSharpTokenNode NamespaceToken { |
---|
48 | get { return GetChildByRole(Roles.NamespaceKeyword); } |
---|
49 | } |
---|
50 | |
---|
51 | public AstType NamespaceName { |
---|
52 | get { return GetChildByRole(NamespaceNameRole) ?? AstType.Null; } |
---|
53 | set { SetChildByRole(NamespaceNameRole, value); } |
---|
54 | } |
---|
55 | |
---|
56 | public string Name { |
---|
57 | get { |
---|
58 | return UsingDeclaration.ConstructNamespace(NamespaceName); |
---|
59 | } |
---|
60 | set { |
---|
61 | var arr = value.Split('.'); |
---|
62 | NamespaceName = ConstructType(arr, arr.Length - 1); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | static AstType ConstructType(string[] arr, int i) |
---|
67 | { |
---|
68 | if (i < 0 || i >= arr.Length) |
---|
69 | throw new ArgumentOutOfRangeException("i"); |
---|
70 | if (i == 0) |
---|
71 | return new SimpleType(arr[i]); |
---|
72 | return new MemberType(ConstructType(arr, i - 1), arr[i]); |
---|
73 | } |
---|
74 | |
---|
75 | /// <summary> |
---|
76 | /// Gets the full namespace name (including any parent namespaces) |
---|
77 | /// </summary> |
---|
78 | public string FullName { |
---|
79 | get { |
---|
80 | NamespaceDeclaration parentNamespace = Parent as NamespaceDeclaration; |
---|
81 | if (parentNamespace != null) |
---|
82 | return BuildQualifiedName(parentNamespace.FullName, Name); |
---|
83 | return Name; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | public IEnumerable<string> Identifiers { |
---|
88 | get { |
---|
89 | var result = new Stack<string>(); |
---|
90 | AstType type = NamespaceName; |
---|
91 | while (type is MemberType) { |
---|
92 | var mt = (MemberType)type; |
---|
93 | result.Push(mt.MemberName); |
---|
94 | type = mt.Target; |
---|
95 | } |
---|
96 | if (type is SimpleType) |
---|
97 | result.Push(((SimpleType)type).Identifier); |
---|
98 | return result; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | public CSharpTokenNode LBraceToken { |
---|
103 | get { return GetChildByRole(Roles.LBrace); } |
---|
104 | } |
---|
105 | |
---|
106 | public AstNodeCollection<AstNode> Members { |
---|
107 | get { return GetChildrenByRole(MemberRole); } |
---|
108 | } |
---|
109 | |
---|
110 | public CSharpTokenNode RBraceToken { |
---|
111 | get { return GetChildByRole(Roles.RBrace); } |
---|
112 | } |
---|
113 | |
---|
114 | public NamespaceDeclaration() |
---|
115 | { |
---|
116 | } |
---|
117 | |
---|
118 | public NamespaceDeclaration(string name) |
---|
119 | { |
---|
120 | this.Name = name; |
---|
121 | } |
---|
122 | |
---|
123 | public static string BuildQualifiedName(string name1, string name2) |
---|
124 | { |
---|
125 | if (string.IsNullOrEmpty(name1)) |
---|
126 | return name2; |
---|
127 | if (string.IsNullOrEmpty(name2)) |
---|
128 | return name1; |
---|
129 | return name1 + "." + name2; |
---|
130 | } |
---|
131 | |
---|
132 | public void AddMember(AstNode child) |
---|
133 | { |
---|
134 | AddChild(child, MemberRole); |
---|
135 | } |
---|
136 | |
---|
137 | public override void AcceptVisitor(IAstVisitor visitor) |
---|
138 | { |
---|
139 | visitor.VisitNamespaceDeclaration(this); |
---|
140 | } |
---|
141 | |
---|
142 | public override T AcceptVisitor<T>(IAstVisitor<T> visitor) |
---|
143 | { |
---|
144 | return visitor.VisitNamespaceDeclaration(this); |
---|
145 | } |
---|
146 | |
---|
147 | public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
---|
148 | { |
---|
149 | return visitor.VisitNamespaceDeclaration(this, data); |
---|
150 | } |
---|
151 | |
---|
152 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
153 | { |
---|
154 | NamespaceDeclaration o = other as NamespaceDeclaration; |
---|
155 | return o != null && MatchString(this.Name, o.Name) && this.Members.DoMatch(o.Members, match); |
---|
156 | } |
---|
157 | } |
---|
158 | } ; |
---|