~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Ast/SyntaxTree.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// SyntaxTree.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 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
using System.Collections.Generic;
 
29
using ICSharpCode.NRefactory.CSharp.Resolver;
 
30
using ICSharpCode.NRefactory.CSharp.TypeSystem;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
using System.Threading;
 
33
using Mono.CSharp;
 
34
using System.IO;
 
35
using ICSharpCode.NRefactory.Editor;
 
36
 
 
37
namespace ICSharpCode.NRefactory.CSharp
 
38
{
 
39
        [Obsolete("CompilationUnit was renamed to SyntaxTree", true)]
 
40
        public class CompilationUnit {}
 
41
        
 
42
        public class SyntaxTree : AstNode
 
43
        {
 
44
                public static readonly Role<AstNode> MemberRole = new Role<AstNode>("Member", AstNode.Null);
 
45
                
 
46
                public override NodeType NodeType {
 
47
                        get {
 
48
                                return NodeType.Unknown;
 
49
                        }
 
50
                }
 
51
                
 
52
                string fileName;
 
53
                
 
54
                /// <summary>
 
55
                /// Gets/Sets the file name of this syntax tree.
 
56
                /// </summary>
 
57
                public string FileName {
 
58
                        get { return fileName; }
 
59
                        set {
 
60
                                ThrowIfFrozen();
 
61
                                fileName = value;
 
62
                        }
 
63
                }
 
64
                
 
65
                public AstNodeCollection<AstNode> Members {
 
66
                        get { return GetChildrenByRole(MemberRole); }
 
67
                }
 
68
 
 
69
                IList<string> conditionalSymbols = null;
 
70
 
 
71
                List<Error> errors = new List<Error> ();
 
72
                
 
73
                public List<Error> Errors {
 
74
                        get { return errors; }
 
75
                }
 
76
 
 
77
 
 
78
                /// <summary>
 
79
                /// Gets the conditional symbols used to parse the source file. Note that this list contains
 
80
                /// the conditional symbols at the start of the first token in the file - including the ones defined
 
81
                /// in the source file.
 
82
                /// </summary>
 
83
                public IList<string> ConditionalSymbols {
 
84
                        get {
 
85
                                return conditionalSymbols ?? EmptyList<string>.Instance;
 
86
                        }
 
87
                        internal set {
 
88
                                conditionalSymbols = value;
 
89
                        }
 
90
                }
 
91
 
 
92
                /// <summary>
 
93
                /// Gets the expression that was on top of the parse stack.
 
94
                /// This is the only way to get an expression that isn't part of a statment.
 
95
                /// (eg. when an error follows an expression).
 
96
                /// 
 
97
                /// This is used for code completion to 'get the expression before a token - like ., &lt;, ('.
 
98
                /// </summary>
 
99
                public AstNode TopExpression {
 
100
                        get;
 
101
                        internal set;
 
102
                }
 
103
                
 
104
                public SyntaxTree ()
 
105
                {
 
106
                }
 
107
                
 
108
                /// <summary>
 
109
                /// Gets all defined types in this syntax tree.
 
110
                /// </summary>
 
111
                /// <returns>
 
112
                /// A list containing <see cref="TypeDeclaration"/> or <see cref="DelegateDeclaration"/> nodes.
 
113
                /// </returns>
 
114
                public IEnumerable<EntityDeclaration> GetTypes(bool includeInnerTypes = false)
 
115
                {
 
116
                        Stack<AstNode> nodeStack = new Stack<AstNode> ();
 
117
                        nodeStack.Push(this);
 
118
                        while (nodeStack.Count > 0) {
 
119
                                var curNode = nodeStack.Pop();
 
120
                                if (curNode is TypeDeclaration || curNode is DelegateDeclaration) {
 
121
                                        yield return (EntityDeclaration)curNode;
 
122
                                }
 
123
                                foreach (var child in curNode.Children) {
 
124
                                        if (!(child is Statement || child is Expression) &&
 
125
                                            (child.Role != Roles.TypeMemberRole || ((child is TypeDeclaration || child is DelegateDeclaration) && includeInnerTypes)))
 
126
                                                nodeStack.Push (child);
 
127
                                }
 
128
                        }
 
129
                }
 
130
 
 
131
                
 
132
                protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 
133
                {
 
134
                        SyntaxTree o = other as SyntaxTree;
 
135
                        return o != null && this.Members.DoMatch(o.Members, match);
 
136
                }
 
137
                
 
138
                public override void AcceptVisitor (IAstVisitor visitor)
 
139
                {
 
140
                        visitor.VisitSyntaxTree (this);
 
141
                }
 
142
                
 
143
                public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
 
144
                {
 
145
                        return visitor.VisitSyntaxTree (this);
 
146
                }
 
147
                
 
148
                public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 
149
                {
 
150
                        return visitor.VisitSyntaxTree (this, data);
 
151
                }
 
152
                
 
153
                /// <summary>
 
154
                /// Converts this syntax tree into a parsed file that can be stored in the type system.
 
155
                /// </summary>
 
156
                public CSharpUnresolvedFile ToTypeSystem ()
 
157
                {
 
158
                        if (string.IsNullOrEmpty (this.FileName))
 
159
                                throw new InvalidOperationException ("Cannot use ToTypeSystem() on a syntax tree without file name.");
 
160
                        var v = new TypeSystemConvertVisitor (this.FileName);
 
161
                        v.VisitSyntaxTree (this);
 
162
                        return v.UnresolvedFile;
 
163
                }
 
164
                
 
165
                public static SyntaxTree Parse (string program, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
 
166
                {
 
167
                        var parser = new CSharpParser (settings);
 
168
                        return parser.Parse (program, fileName);
 
169
                }
 
170
                
 
171
                public static SyntaxTree Parse (TextReader reader, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
 
172
                {
 
173
                        var parser = new CSharpParser (settings);
 
174
                        return parser.Parse (reader, fileName);
 
175
                }
 
176
                
 
177
                public static SyntaxTree Parse (Stream stream, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
 
178
                {
 
179
                        var parser = new CSharpParser (settings);
 
180
                        return parser.Parse (stream, fileName);
 
181
                }
 
182
                
 
183
                public static SyntaxTree Parse (ITextSource textSource, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
 
184
                {
 
185
                        var parser = new CSharpParser (settings);
 
186
                        return parser.Parse (textSource, fileName);
 
187
                }
 
188
        }
 
189
}