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

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/SnippetParser.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
 
// <file>
2
 
//     <copyright see="prj:///doc/copyright.txt"/>
3
 
//     <license see="prj:///doc/license.txt"/>
4
 
//     <owner name="Daniel Grunwald"/>
5
 
//     <version>$Revision: 4482 $</version>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Collections.Generic;
10
 
using System.Diagnostics;
11
 
using System.IO;
12
 
 
13
 
using ICSharpCode.OldNRefactory.Ast;
14
 
using ICSharpCode.OldNRefactory.Parser;
15
 
 
16
 
namespace ICSharpCode.OldNRefactory
17
 
{
18
 
        public enum SnippetType
19
 
        {
20
 
                None,
21
 
                CompilationUnit,
22
 
                Expression,
23
 
                Statements,
24
 
                TypeMembers
25
 
        }
26
 
        
27
 
        /// <summary>
28
 
        /// The snippet parser supports parsing code snippets that are not valid as a full compilation unit.
29
 
        /// </summary>
30
 
        public class SnippetParser
31
 
        {
32
 
                readonly SupportedLanguage language;
33
 
                
34
 
                public SnippetParser(SupportedLanguage language)
35
 
                {
36
 
                        this.language = language;
37
 
                }
38
 
                
39
 
                /// <summary>
40
 
                /// Gets the errors of the last call to Parse(). Returns null if parse was not yet called.
41
 
                /// </summary>
42
 
                public Errors Errors { get; private set; }
43
 
                
44
 
                /// <summary>
45
 
                /// Gets the specials of the last call to Parse(). Returns null if parse was not yet called.
46
 
                /// </summary>
47
 
                public List<ISpecial> Specials { get; private set; }
48
 
                
49
 
                /// <summary>
50
 
                /// Gets the snippet type of the last call to Parse(). Returns None if parse was not yet called.
51
 
                /// </summary>
52
 
                public SnippetType SnippetType { get; private set; }
53
 
                
54
 
                /// <summary>
55
 
                /// Parse the code. The result may be a CompilationUnit, an Expression, a list of statements or a list of class
56
 
                /// members.
57
 
                /// </summary>
58
 
                public INode Parse(string code)
59
 
                {
60
 
                        IParser parser = ParserFactory.CreateParser(language, new StringReader(code));
61
 
                        parser.Parse();
62
 
                        this.Errors = parser.Errors;
63
 
                        this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
64
 
                        this.SnippetType = SnippetType.CompilationUnit;
65
 
                        INode result = parser.CompilationUnit;
66
 
                        
67
 
                        if (this.Errors.Count > 0) {
68
 
                                if (language == SupportedLanguage.CSharp) {
69
 
                                        // SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
70
 
                                        parser = ParserFactory.CreateParser(language, new StringReader(code + ";"));
71
 
                                } else {
72
 
                                        parser = ParserFactory.CreateParser(language, new StringReader(code));
73
 
                                }
74
 
                                Expression expression = parser.ParseExpression();
75
 
                                if (expression != null && parser.Errors.Count < this.Errors.Count) {
76
 
                                        this.Errors = parser.Errors;
77
 
                                        this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
78
 
                                        this.SnippetType = SnippetType.Expression;
79
 
                                        result = expression;
80
 
                                }
81
 
                        }
82
 
                        if (this.Errors.Count > 0) {
83
 
                                parser = ParserFactory.CreateParser(language, new StringReader(code));
84
 
                                BlockStatement block = parser.ParseBlock();
85
 
                                if (block != null && parser.Errors.Count < this.Errors.Count) {
86
 
                                        this.Errors = parser.Errors;
87
 
                                        this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
88
 
                                        this.SnippetType = SnippetType.Statements;
89
 
                                        result = block;
90
 
                                }
91
 
                        }
92
 
                        if (this.Errors.Count > 0) {
93
 
                                parser = ParserFactory.CreateParser(language, new StringReader(code));
94
 
                                List<INode> members = parser.ParseTypeMembers();
95
 
                                if (members != null && members.Count > 0 && parser.Errors.Count < this.Errors.Count) {
96
 
                                        this.Errors = parser.Errors;
97
 
                                        this.Specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
98
 
                                        this.SnippetType = SnippetType.TypeMembers;
99
 
                                        result = new NodeListNode(members);
100
 
                                        result.StartLocation = members[0].StartLocation;
101
 
                                        result.EndLocation = members[members.Count - 1].EndLocation;
102
 
                                }
103
 
                        }
104
 
                        Debug.Assert(result is CompilationUnit || !result.StartLocation.IsEmpty);
105
 
                        Debug.Assert(result is CompilationUnit || !result.EndLocation.IsEmpty);
106
 
                        return result;
107
 
                }
108
 
                
109
 
                sealed class NodeListNode : INode
110
 
                {
111
 
                        List<INode> nodes;
112
 
                        
113
 
                        public NodeListNode(List<INode> nodes)
114
 
                        {
115
 
                                this.nodes = nodes;
116
 
                        }
117
 
                        
118
 
                        public INode Parent {
119
 
                                get { return null; }
120
 
                                set { throw new NotSupportedException(); }
121
 
                        }
122
 
                        
123
 
                        public List<INode> Children {
124
 
                                get { return nodes; }
125
 
                        }
126
 
                        
127
 
                        public Location StartLocation { get; set; }
128
 
                        public Location EndLocation { get; set; }
129
 
                        
130
 
                        public object UserData { get; set; }
131
 
                        
132
 
                        public object AcceptChildren(IAstVisitor visitor, object data)
133
 
                        {
134
 
                                foreach (INode n in nodes) {
135
 
                                        n.AcceptVisitor(visitor, data);
136
 
                                }
137
 
                                return null;
138
 
                        }
139
 
                        
140
 
                        public object AcceptVisitor(IAstVisitor visitor, object data)
141
 
                        {
142
 
                                return AcceptChildren(visitor, data);
143
 
                        }
144
 
                }
145
 
        }
146
 
}