2
// NamespaceDeclaration.cs
5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using System.Collections.Generic;
31
namespace ICSharpCode.NRefactory.CSharp
34
/// namespace Name { Members }
36
public class NamespaceDeclaration : AstNode
38
public static readonly Role<AstNode> MemberRole = SyntaxTree.MemberRole;
40
public override NodeType NodeType {
42
return NodeType.Unknown;
46
public CSharpTokenNode NamespaceToken {
47
get { return GetChildByRole (Roles.NamespaceKeyword); }
52
StringBuilder builder = new StringBuilder ();
53
foreach (Identifier identifier in GetChildrenByRole (Roles.Identifier)) {
54
if (builder.Length > 0)
56
builder.Append (identifier.Name);
58
return builder.ToString ();
61
GetChildrenByRole(Roles.Identifier).ReplaceWith(value.Split('.').Select(ident => Identifier.Create (ident)));
65
public AstNodeCollection<Identifier> Identifiers {
66
get { return GetChildrenByRole (Roles.Identifier); }
70
/// Gets the full namespace name (including any parent namespaces)
72
public string FullName {
74
NamespaceDeclaration parentNamespace = Parent as NamespaceDeclaration;
75
if (parentNamespace != null)
76
return BuildQualifiedName (parentNamespace.FullName, Name);
81
public CSharpTokenNode LBraceToken {
82
get { return GetChildByRole (Roles.LBrace); }
85
public AstNodeCollection<AstNode> Members {
86
get { return GetChildrenByRole(MemberRole); }
89
public CSharpTokenNode RBraceToken {
90
get { return GetChildByRole (Roles.RBrace); }
93
public NamespaceDeclaration ()
97
public NamespaceDeclaration (string name)
102
public static string BuildQualifiedName (string name1, string name2)
104
if (string.IsNullOrEmpty (name1))
106
if (string.IsNullOrEmpty (name2))
108
return name1 + "." + name2;
111
public void AddMember (AstNode child)
113
AddChild (child, MemberRole);
116
public override void AcceptVisitor (IAstVisitor visitor)
118
visitor.VisitNamespaceDeclaration (this);
121
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
123
return visitor.VisitNamespaceDeclaration (this);
126
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
128
return visitor.VisitNamespaceDeclaration (this, data);
131
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
133
NamespaceDeclaration o = other as NamespaceDeclaration;
134
return o != null && MatchString(this.Name, o.Name) && this.Members.DoMatch(o.Members, match);