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>
8
using ICSharpCode.OldNRefactory.Visitors;
10
using System.Collections.Generic;
11
using System.Diagnostics;
13
using ICSharpCode.OldNRefactory.Ast;
15
namespace ICSharpCode.OldNRefactory.Parser.VB
17
internal sealed partial class Parser : AbstractParser
21
public Parser(ILexer lexer) : base(lexer)
23
this.lexer = (Lexer)lexer;
26
private StringBuilder qualidentBuilder = new StringBuilder();
30
[System.Diagnostics.DebuggerStepThrough]
37
[System.Diagnostics.DebuggerStepThrough]
39
return lexer.LookAhead;
54
public void Error(string s)
56
if (errDist >= MinErrDist) {
57
this.Errors.Error(la.line, la.col, s);
62
public override void Parse()
65
compilationUnit.AcceptVisitor(new SetParentVisitor(), null);
68
public override TypeReference ParseTypeReference ()
74
public override Expression ParseExpression()
77
Location startLocation = la.Location;
80
while (la.kind == Tokens.EOL) lexer.NextToken();
82
expr.StartLocation = startLocation;
83
expr.EndLocation = t.EndLocation;
84
expr.AcceptVisitor(new SetParentVisitor(), null);
90
public override BlockStatement ParseBlock()
93
compilationUnit = new CompilationUnit();
95
Location startLocation = la.Location;
99
st.StartLocation = startLocation;
101
st.EndLocation = t.EndLocation;
103
st.EndLocation = la.Location;
104
st.AcceptVisitor(new SetParentVisitor(), null);
107
return st as BlockStatement;
110
public override List<INode> ParseTypeMembers()
113
compilationUnit = new CompilationUnit();
115
TypeDeclaration newType = new TypeDeclaration(Modifiers.None, null);
116
compilationUnit.BlockStart(newType);
118
compilationUnit.BlockEnd();
120
newType.AcceptVisitor(new SetParentVisitor(), null);
121
return newType.Children;
126
int peek = Peek(1).kind;
127
return Tokens.BlockSucc[la.kind] && (la.kind != Tokens.End || peek == Tokens.EOL || peek == Tokens.Colon);
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);
136
static bool IsIdentifierToken(Token tk)
138
return Tokens.IdentifierTokens[tk.kind] || tk.kind == Tokens.Identifier;
141
bool IsIdentifiedExpressionRange()
143
return la.kind == Tokens.As || la.kind == Tokens.Assign;
146
bool IsQueryExpression()
148
return (la.kind == Tokens.From || la.kind == Tokens.Aggregate) && IsIdentifierToken(Peek(1));
151
bool IsEndStmtAhead()
153
int peek = Peek(1).kind;
154
return la.kind == Tokens.End && (peek == Tokens.EOL || peek == Tokens.Colon);
157
bool IsNotClosingParenthesis() {
158
return la.kind != Tokens.CloseParenthesis;
162
True, if ident is followed by "=" or by ":" and "="
164
bool IsNamedAssign() {
165
if(Peek(1).kind == Tokens.Colon && Peek(2).kind == Tokens.Assign) return true;
169
bool IsObjectCreation() {
170
return la.kind == Tokens.As && Peek(1).kind == Tokens.New;
174
True, if "<" is followed by the ident "assembly" or "module"
176
bool IsGlobalAttrTarget () {
178
return la.kind == Tokens.LessThan && ( string.Equals(pt.val, "assembly", StringComparison.InvariantCultureIgnoreCase) || string.Equals(pt.val, "module", StringComparison.InvariantCultureIgnoreCase));
182
True if the next token is a "(" and is followed by "," or ")"
186
int peek = Peek(1).kind;
187
return la.kind == Tokens.OpenParenthesis
188
&& (peek == Tokens.Comma || peek == Tokens.CloseParenthesis);
192
True if the next token is an identifier
194
bool IsLoopVariableDeclaration()
196
if (!IsIdentifierToken(la))
199
Token x = lexer.Peek();
200
if (x.kind == Tokens.OpenParenthesis) {
203
} while (x.kind == Tokens.Comma);
204
if (x.kind != Tokens.CloseParenthesis)
208
return x.kind == Tokens.As || x.kind == Tokens.Assign;
213
return la.kind == Tokens.OpenParenthesis;
217
True, if the comma is not a trailing one,
218
like the last one in: a, b, c,
220
bool NotFinalComma() {
221
int peek = Peek(1).kind;
222
return la.kind == Tokens.Comma &&
223
peek != Tokens.CloseCurlyBrace;
227
True, if the next token is "Else" and this one
232
int peek = Peek(1).kind;
233
return la.kind == Tokens.Else && peek == Tokens.If;
237
True if the next token is goto and this one is
238
followed by minus ("-") (this is allowd in in
241
bool IsNegativeLabelName()
243
int peek = Peek(1).kind;
244
return la.kind == Tokens.GoTo && peek == Tokens.Minus;
248
True if the next statement is a "Resume next" statement
252
int peek = Peek(1).kind;
253
return la.kind == Tokens.Resume && peek == Tokens.Next;
257
True, if ident/literal integer is followed by ":"
261
return (la.kind == Tokens.Identifier || la.kind == Tokens.LiteralInteger)
262
&& Peek(1).kind == Tokens.Colon;
265
bool IsNotStatementSeparator()
267
return la.kind == Tokens.Colon && Peek(1).kind == Tokens.EOL;
270
static bool IsMustOverride(ModifierList m)
272
return m.Contains(Modifiers.Abstract);
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)
280
MemberReferenceExpression fre = expr as MemberReferenceExpression;
282
bool result = WriteFullTypeName(b, fre.TargetObject);
283
if (b.Length > 0) b.Append('.');
284
b.Append(fre.MemberName);
286
} else if (expr is IdentifierExpression) {
287
b.Append(((IdentifierExpression)expr).Identifier);
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"
299
bool IsLocalAttrTarget() {
304
void EnsureIsZero(Expression expr)
306
if (!(expr is PrimitiveExpression) || (expr as PrimitiveExpression).StringValue != "0")
307
Error("lower bound of array must be zero");
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.
315
static void SafeAdd<T>(INode parent, List<T> list, T item) where T : class, INode
317
Debug.Assert(parent != null);
318
Debug.Assert((parent is INullable) ? !(parent as INullable).IsNull : true);
321
item.Parent = parent;