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

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/Parser/VBNet/VBNetParser.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" email="daniel@danielgrunwald.de"/>
5
 
//     <version>$Revision: 4482 $</version>
6
 
// </file>
7
 
 
8
 
using ICSharpCode.OldNRefactory.Visitors;
9
 
using System;
10
 
using System.Collections.Generic;
11
 
using System.Diagnostics;
12
 
using System.Text;
13
 
using ICSharpCode.OldNRefactory.Ast;
14
 
 
15
 
namespace ICSharpCode.OldNRefactory.Parser.VB
16
 
{
17
 
        internal sealed partial class Parser : AbstractParser
18
 
        {
19
 
                Lexer lexer;
20
 
                
21
 
                public Parser(ILexer lexer) : base(lexer)
22
 
                {
23
 
                        this.lexer = (Lexer)lexer;
24
 
                }
25
 
                
26
 
                private StringBuilder qualidentBuilder = new StringBuilder();
27
 
 
28
 
                Token t
29
 
                {
30
 
                        [System.Diagnostics.DebuggerStepThrough]
31
 
                        get {
32
 
                                return lexer.Token;
33
 
                        }
34
 
                }
35
 
                Token la
36
 
                {
37
 
                        [System.Diagnostics.DebuggerStepThrough]
38
 
                        get {
39
 
                                return lexer.LookAhead;
40
 
                        }
41
 
                }
42
 
 
43
 
                Token Peek (int n)
44
 
                {
45
 
                        lexer.StartPeek();
46
 
                        Token x = la;
47
 
                        while (n > 0) {
48
 
                                x = lexer.Peek();
49
 
                                n--;
50
 
                        }
51
 
                        return x;
52
 
                }
53
 
 
54
 
                public void Error(string s)
55
 
                {
56
 
                        if (errDist >= MinErrDist) {
57
 
                                this.Errors.Error(la.line, la.col, s);
58
 
                        }
59
 
                        errDist = 0;
60
 
                }
61
 
 
62
 
                public override void Parse()
63
 
                {
64
 
                        ParseRoot();
65
 
                        compilationUnit.AcceptVisitor(new SetParentVisitor(), null);
66
 
                }
67
 
                
68
 
                public override TypeReference ParseTypeReference ()
69
 
                {
70
 
                        // TODO
71
 
                        return null;
72
 
                }
73
 
                
74
 
                public override Expression ParseExpression()
75
 
                {
76
 
                        lexer.NextToken();
77
 
                        Location startLocation = la.Location;
78
 
                        Expression expr;
79
 
                        Expr(out expr);
80
 
                        while (la.kind == Tokens.EOL) lexer.NextToken();
81
 
                        if (expr != null) {
82
 
                                expr.StartLocation = startLocation;
83
 
                                expr.EndLocation = t.EndLocation;
84
 
                                expr.AcceptVisitor(new SetParentVisitor(), null);
85
 
                        }
86
 
                        Expect(Tokens.EOF);
87
 
                        return expr;
88
 
                }
89
 
                
90
 
                public override BlockStatement ParseBlock()
91
 
                {
92
 
                        lexer.NextToken();
93
 
                        compilationUnit = new CompilationUnit();
94
 
                        
95
 
                        Location startLocation = la.Location;
96
 
                        Statement st;
97
 
                        Block(out st);
98
 
                        if (st != null) {
99
 
                                st.StartLocation = startLocation;
100
 
                                if (t != null)
101
 
                                        st.EndLocation = t.EndLocation;
102
 
                                else
103
 
                                        st.EndLocation = la.Location;
104
 
                                st.AcceptVisitor(new SetParentVisitor(), null);
105
 
                        }
106
 
                        Expect(Tokens.EOF);
107
 
                        return st as BlockStatement;
108
 
                }
109
 
                
110
 
                public override List<INode> ParseTypeMembers()
111
 
                {
112
 
                        lexer.NextToken();
113
 
                        compilationUnit = new CompilationUnit();
114
 
                        
115
 
                        TypeDeclaration newType = new TypeDeclaration(Modifiers.None, null);
116
 
                        compilationUnit.BlockStart(newType);
117
 
                        ClassBody(newType);
118
 
                        compilationUnit.BlockEnd();
119
 
                        Expect(Tokens.EOF);
120
 
                        newType.AcceptVisitor(new SetParentVisitor(), null);
121
 
                        return newType.Children;
122
 
                }
123
 
 
124
 
                bool LeaveBlock()
125
 
                {
126
 
                        int peek = Peek(1).kind;
127
 
                        return Tokens.BlockSucc[la.kind] && (la.kind != Tokens.End || peek == Tokens.EOL || peek == Tokens.Colon);
128
 
                }
129
 
 
130
 
                /* True, if "." is followed by an ident */
131
 
                bool DotAndIdentOrKw () {
132
 
                        int peek = Peek(1).kind;
133
 
                        return la.kind == Tokens.Dot && (peek == Tokens.Identifier || peek >= Tokens.AddHandler);
134
 
                }
135
 
 
136
 
                static bool IsIdentifierToken(Token tk)
137
 
                {
138
 
                        return Tokens.IdentifierTokens[tk.kind] || tk.kind == Tokens.Identifier;
139
 
                }
140
 
                
141
 
                bool IsIdentifiedExpressionRange()
142
 
                {
143
 
                        return la.kind == Tokens.As || la.kind == Tokens.Assign;
144
 
                }
145
 
                
146
 
                bool IsQueryExpression()
147
 
                {
148
 
                        return (la.kind == Tokens.From || la.kind == Tokens.Aggregate) && IsIdentifierToken(Peek(1));
149
 
                }
150
 
                
151
 
                bool IsEndStmtAhead()
152
 
                {
153
 
                        int peek = Peek(1).kind;
154
 
                        return la.kind == Tokens.End && (peek == Tokens.EOL || peek == Tokens.Colon);
155
 
                }
156
 
 
157
 
                bool IsNotClosingParenthesis() {
158
 
                        return la.kind != Tokens.CloseParenthesis;
159
 
                }
160
 
 
161
 
                /*
162
 
                        True, if ident is followed by "=" or by ":" and "="
163
 
                 */
164
 
                bool IsNamedAssign() {
165
 
                        if(Peek(1).kind == Tokens.Colon && Peek(2).kind == Tokens.Assign) return true;
166
 
                        return false;
167
 
                }
168
 
 
169
 
                bool IsObjectCreation() {
170
 
                        return la.kind == Tokens.As && Peek(1).kind == Tokens.New;
171
 
                }
172
 
 
173
 
                /*
174
 
                        True, if "<" is followed by the ident "assembly" or "module"
175
 
                 */
176
 
                bool IsGlobalAttrTarget () {
177
 
                        Token pt = Peek(1);
178
 
                        return la.kind == Tokens.LessThan && ( string.Equals(pt.val, "assembly", StringComparison.InvariantCultureIgnoreCase) || string.Equals(pt.val, "module", StringComparison.InvariantCultureIgnoreCase));
179
 
                }
180
 
 
181
 
                /*
182
 
                        True if the next token is a "(" and is followed by "," or ")"
183
 
                 */
184
 
                bool IsDims()
185
 
                {
186
 
                        int peek = Peek(1).kind;
187
 
                        return la.kind == Tokens.OpenParenthesis
188
 
                                && (peek == Tokens.Comma || peek == Tokens.CloseParenthesis);
189
 
                }
190
 
                
191
 
                /*
192
 
                        True if the next token is an identifier
193
 
                 */
194
 
                bool IsLoopVariableDeclaration()
195
 
                {
196
 
                        if (!IsIdentifierToken(la))
197
 
                                return false;
198
 
                        lexer.StartPeek();
199
 
                        Token x = lexer.Peek();
200
 
                        if (x.kind == Tokens.OpenParenthesis) {
201
 
                                do {
202
 
                                        x = lexer.Peek();
203
 
                                } while (x.kind == Tokens.Comma);
204
 
                                if (x.kind != Tokens.CloseParenthesis)
205
 
                                        return false;
206
 
                                x = lexer.Peek();
207
 
                        }
208
 
                        return x.kind == Tokens.As || x.kind == Tokens.Assign;
209
 
                }
210
 
 
211
 
                bool IsSize()
212
 
                {
213
 
                        return la.kind == Tokens.OpenParenthesis;
214
 
                }
215
 
 
216
 
                /*
217
 
                        True, if the comma is not a trailing one,
218
 
                        like the last one in: a, b, c,
219
 
                 */
220
 
                bool NotFinalComma() {
221
 
                        int peek = Peek(1).kind;
222
 
                        return la.kind == Tokens.Comma &&
223
 
                                peek != Tokens.CloseCurlyBrace;
224
 
                }
225
 
 
226
 
                /*
227
 
                        True, if the next token is "Else" and this one
228
 
                        if followed by "If"
229
 
                 */
230
 
                bool IsElseIf()
231
 
                {
232
 
                        int peek = Peek(1).kind;
233
 
                        return la.kind == Tokens.Else && peek == Tokens.If;
234
 
                }
235
 
 
236
 
                /*
237
 
        True if the next token is goto and this one is
238
 
        followed by minus ("-") (this is allowd in in
239
 
        error clauses)
240
 
                 */
241
 
                bool IsNegativeLabelName()
242
 
                {
243
 
                        int peek = Peek(1).kind;
244
 
                        return la.kind == Tokens.GoTo && peek == Tokens.Minus;
245
 
                }
246
 
 
247
 
                /*
248
 
        True if the next statement is a "Resume next" statement
249
 
                 */
250
 
                bool IsResumeNext()
251
 
                {
252
 
                        int peek = Peek(1).kind;
253
 
                        return la.kind == Tokens.Resume && peek == Tokens.Next;
254
 
                }
255
 
 
256
 
                /*
257
 
        True, if ident/literal integer is followed by ":"
258
 
                 */
259
 
                bool IsLabel()
260
 
                {
261
 
                        return (la.kind == Tokens.Identifier || la.kind == Tokens.LiteralInteger)
262
 
                                && Peek(1).kind == Tokens.Colon;
263
 
                }
264
 
 
265
 
                bool IsNotStatementSeparator()
266
 
                {
267
 
                        return la.kind == Tokens.Colon && Peek(1).kind == Tokens.EOL;
268
 
                }
269
 
 
270
 
                static bool IsMustOverride(ModifierList m)
271
 
                {
272
 
                        return m.Contains(Modifiers.Abstract);
273
 
                }
274
 
 
275
 
                /* Writes the type name represented through the expression into the string builder. */
276
 
                /* Returns true when the expression was converted successfully, returns false when */
277
 
                /* There was an unknown expression (e.g. TypeReferenceExpression) in it */
278
 
                bool WriteFullTypeName(StringBuilder b, Expression expr)
279
 
                {
280
 
                        MemberReferenceExpression fre = expr as MemberReferenceExpression;
281
 
                        if (fre != null) {
282
 
                                bool result = WriteFullTypeName(b, fre.TargetObject);
283
 
                                if (b.Length > 0) b.Append('.');
284
 
                                b.Append(fre.MemberName);
285
 
                                return result;
286
 
                        } else if (expr is IdentifierExpression) {
287
 
                                b.Append(((IdentifierExpression)expr).Identifier);
288
 
                                return true;
289
 
                        } else {
290
 
                                return false;
291
 
                        }
292
 
                }
293
 
 
294
 
                /*
295
 
                True, if lookahead is a local attribute target specifier,
296
 
                i.e. one of "event", "return", "field", "method",
297
 
                "module", "param", "property", or "type"
298
 
                 */
299
 
                bool IsLocalAttrTarget() {
300
 
                        // TODO
301
 
                        return false;
302
 
                }
303
 
                
304
 
                void EnsureIsZero(Expression expr)
305
 
                {
306
 
                        if (!(expr is PrimitiveExpression) || (expr as PrimitiveExpression).StringValue != "0")
307
 
                                Error("lower bound of array must be zero");
308
 
                }
309
 
                
310
 
                /// <summary>
311
 
                /// Adds a child item to a collection stored in the parent node.
312
 
                /// Also set's the item's parent to <paramref name="parent"/>.
313
 
                /// Does nothing if item is null.
314
 
                /// </summary>
315
 
                static void SafeAdd<T>(INode parent, List<T> list, T item) where T : class, INode
316
 
                {
317
 
                        Debug.Assert(parent != null);
318
 
                        Debug.Assert((parent is INullable) ? !(parent as INullable).IsNull : true);
319
 
                        if (item != null) {
320
 
                                list.Add(item);
321
 
                                item.Parent = parent;
322
 
                        }
323
 
                }
324
 
        }
325
 
}