Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2893_BNLR/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Ast/SyntaxTree.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 6.2 KB
Line 
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
27using System;
28using System.Collections.Generic;
29using ICSharpCode.NRefactory.CSharp.Resolver;
30using ICSharpCode.NRefactory.CSharp.TypeSystem;
31using ICSharpCode.NRefactory.TypeSystem;
32using System.Threading;
33using Mono.CSharp;
34using System.IO;
35using ICSharpCode.NRefactory.Editor;
36
37namespace 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      cancellationToken.ThrowIfCancellationRequested();
168      var parser = new CSharpParser (settings);
169      return parser.Parse (program, fileName);
170    }
171   
172    public static SyntaxTree Parse (TextReader reader, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
173    {
174      cancellationToken.ThrowIfCancellationRequested();
175      var parser = new CSharpParser (settings);
176      return parser.Parse (reader, fileName);
177    }
178   
179    public static SyntaxTree Parse (Stream stream, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
180    {
181      cancellationToken.ThrowIfCancellationRequested();
182      var parser = new CSharpParser (settings);
183      return parser.Parse (stream, fileName);
184    }
185   
186    public static SyntaxTree Parse (ITextSource textSource, string fileName = "", CompilerSettings settings = null, CancellationToken cancellationToken = default (CancellationToken))
187    {
188      cancellationToken.ThrowIfCancellationRequested();
189      var parser = new CSharpParser (settings);
190      return parser.Parse (textSource, fileName);
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.