5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2010 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
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;
35
using ICSharpCode.NRefactory.Editor;
37
namespace ICSharpCode.NRefactory.CSharp
39
[Obsolete("CompilationUnit was renamed to SyntaxTree", true)]
40
public class CompilationUnit {}
42
public class SyntaxTree : AstNode
44
public static readonly Role<AstNode> MemberRole = new Role<AstNode>("Member", AstNode.Null);
46
public override NodeType NodeType {
48
return NodeType.Unknown;
55
/// Gets/Sets the file name of this syntax tree.
57
public string FileName {
58
get { return fileName; }
65
public AstNodeCollection<AstNode> Members {
66
get { return GetChildrenByRole(MemberRole); }
69
IList<string> conditionalSymbols = null;
71
List<Error> errors = new List<Error> ();
73
public List<Error> Errors {
74
get { return errors; }
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.
83
public IList<string> ConditionalSymbols {
85
return conditionalSymbols ?? EmptyList<string>.Instance;
88
conditionalSymbols = value;
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).
97
/// This is used for code completion to 'get the expression before a token - like ., <, ('.
99
public AstNode TopExpression {
109
/// Gets all defined types in this syntax tree.
112
/// A list containing <see cref="TypeDeclaration"/> or <see cref="DelegateDeclaration"/> nodes.
114
public IEnumerable<EntityDeclaration> GetTypes(bool includeInnerTypes = false)
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;
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);
132
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
134
SyntaxTree o = other as SyntaxTree;
135
return o != null && this.Members.DoMatch(o.Members, match);
138
public override void AcceptVisitor (IAstVisitor visitor)
140
visitor.VisitSyntaxTree (this);
143
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
145
return visitor.VisitSyntaxTree (this);
148
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
150
return visitor.VisitSyntaxTree (this, data);
154
/// Converts this syntax tree into a parsed file that can be stored in the type system.
156
public CSharpUnresolvedFile ToTypeSystem ()
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;
165
public static SyntaxTree Parse (string program, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
167
var parser = new CSharpParser (settings);
168
return parser.Parse (program, fileName);
171
public static SyntaxTree Parse (TextReader reader, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
173
var parser = new CSharpParser (settings);
174
return parser.Parse (reader, fileName);
177
public static SyntaxTree Parse (Stream stream, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
179
var parser = new CSharpParser (settings);
180
return parser.Parse (stream, fileName);
183
public static SyntaxTree Parse (ITextSource textSource, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
185
var parser = new CSharpParser (settings);
186
return parser.Parse (textSource, fileName);