1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.VB.Ast;
using ICSharpCode.NRefactory.VB.Visitors;
namespace ICSharpCode.NRefactory.VB.Parser
{
internal partial class VBParser : IDisposable
{
VBLexer lexer;
Stack<AstNode> stack;
CompilationUnit compilationUnit;
int errDist = MinErrDist;
const int MinErrDist = 2;
const string ErrMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
public VBParser(VBLexer lexer)
{
this.errors = lexer.Errors;
errors.SynErr = new ErrorCodeProc(SynErr);
this.lexer = (VBLexer)lexer;
this.stack = new Stack<AstNode>();
}
#region Infrastructure
void NodeStart(AstNode node)
{
stack.Push(node);
}
void NodeEnd(AstNode currentNode, Role role)
{
AstNode node = stack.Pop();
Debug.Assert(currentNode == node);
stack.Peek().AddChildUntyped(node, role);
}
void AddTerminal(Role<VBTokenNode> role)
{
stack.Peek().AddChild(new VBTokenNode(t.Location, t.EndLocation), role);
}
void AddChild<T>(T childNode, Role<T> role) where T : AstNode
{
if (childNode != null) {
stack.Peek().AddChild(childNode, role);
}
}
StringBuilder qualidentBuilder = new StringBuilder();
Token t
{
[System.Diagnostics.DebuggerStepThrough]
get {
return lexer.Token;
}
}
Token la
{
[System.Diagnostics.DebuggerStepThrough]
get {
return lexer.LookAhead;
}
}
Token Peek(int n)
{
lexer.StartPeek();
Token x = la;
while (n > 0) {
x = lexer.Peek();
n--;
}
return x;
}
public void Error(string s)
{
if (errDist >= MinErrDist) {
this.Errors.Error(la.line, la.col, s);
}
errDist = 0;
}
#endregion
#region Parse
public void Parse()
{
ParseRoot();
}
public AstType ParseAstType()
{
// TODO
return null;
}
// public Expression ParseExpression()
// {
// lexer.SetInitialContext(SnippetType.Expression);
// lexer.NextToken();
// Location startLocation = la.Location;
// Expression expr;
// Expr(out expr);
// while (la.kind == Tokens.EOL) lexer.NextToken();
// if (expr != null) {
// expr.StartLocation = startLocation;
// expr.EndLocation = t.EndLocation;
// expr.AcceptVisitor(new SetParentVisitor(), null);
// }
// Expect(Tokens.EOF);
// return expr;
// }
// public BlockStatement ParseBlock()
// {
// lexer.NextToken();
// compilationUnit = new CompilationUnit();
//
// Location startLocation = la.Location;
// Statement st;
// Block(out st);
// if (st != null) {
// st.StartLocation = startLocation;
// if (t != null)
// st.EndLocation = t.EndLocation;
// else
// st.EndLocation = la.Location;
// st.AcceptVisitor(new SetParentVisitor(), null);
// }
// Expect(Tokens.EOF);
// return st as BlockStatement;
// }
// public List<AstNode> ParseTypeMembers()
// {
// lexer.NextToken();
// TypeDeclaration newType = new TypeDeclaration(Modifiers.None, null);
// BlockStart(newType);
// ClassBody(newType);
// BlockEnd();
// Expect(Tokens.EOF);
// newType.AcceptVisitor(new SetParentVisitor(), null);
// return newType.Children;
// }
#endregion
#region Conflict Resolvers
bool IsAliasImportsClause()
{
return IsIdentifierToken(la) && Peek(1).Kind == Tokens.Assign;
}
static bool IsIdentifierToken(Token tk)
{
return Tokens.IdentifierTokens[tk.kind] || tk.kind == Tokens.Identifier;
}
#endregion
/* True, if "." is followed by an ident */
bool DotAndIdentOrKw () {
int peek = Peek(1).kind;
return la.kind == Tokens.Dot && (peek == Tokens.Identifier || peek >= Tokens.AddHandler);
}
bool IsIdentifiedExpressionRange()
{
// t = Select
// la = Identifier
// Peek(1) = As or Assign
Token token = Peek(1);
return IsIdentifierToken(la) && (token.kind == Tokens.As || token.kind == Tokens.Assign);
}
bool IsQueryExpression()
{
return (la.kind == Tokens.From || la.kind == Tokens.Aggregate) && IsIdentifierToken(Peek(1));
}
bool IsEndStmtAhead()
{
int peek = Peek(1).kind;
return la.kind == Tokens.End && (peek == Tokens.EOL || peek == Tokens.Colon);
}
bool IsNotClosingParenthesis() {
return la.kind != Tokens.CloseParenthesis;
}
/*
True, if ident is followed by "=" or by ":" and "="
*/
bool IsNamedAssign() {
return Peek(1).kind == Tokens.ColonAssign;
}
bool IsObjectCreation() {
return la.kind == Tokens.As && Peek(1).kind == Tokens.New;
}
bool IsNewExpression() {
return la.kind == Tokens.New;
}
/*
True, if "<" is followed by the ident "assembly" or "module"
*/
bool IsGlobalAttrTarget () {
Token pt = Peek(1);
return la.kind == Tokens.LessThan && ( string.Equals(pt.val, "assembly", StringComparison.InvariantCultureIgnoreCase) || string.Equals(pt.val, "module", StringComparison.InvariantCultureIgnoreCase));
}
/*
True if the next token is a "(" and is followed by "," or ")"
*/
bool IsDims()
{
int peek = Peek(1).kind;
return la.kind == Tokens.OpenParenthesis
&& (peek == Tokens.Comma || peek == Tokens.CloseParenthesis);
}
/*
True if the next token is an identifier
*/
bool IsLoopVariableDeclaration()
{
if (!IsIdentifierToken(la))
return false;
lexer.StartPeek();
Token x = lexer.Peek();
if (x.kind == Tokens.OpenParenthesis) {
do {
x = lexer.Peek();
} while (x.kind == Tokens.Comma);
if (x.kind != Tokens.CloseParenthesis)
return false;
x = lexer.Peek();
}
return x.kind == Tokens.As || x.kind == Tokens.Assign;
}
bool IsSize()
{
return la.kind == Tokens.OpenParenthesis;
}
/*
True, if the comma is not a trailing one,
like the last one in: a, b, c,
*/
bool NotFinalComma() {
int peek = Peek(1).kind;
return la.kind == Tokens.Comma &&
peek != Tokens.CloseCurlyBrace;
}
/*
True, if the next token is "Else" and this one
if followed by "If"
*/
bool IsElseIf()
{
int peek = Peek(1).kind;
return la.kind == Tokens.Else && peek == Tokens.If;
}
/*
True if the next token is goto and this one is
followed by minus ("-") (this is allowd in in
error clauses)
*/
bool IsNegativeLabelName()
{
int peek = Peek(1).kind;
return la.kind == Tokens.GoTo && peek == Tokens.Minus;
}
/*
True if the next statement is a "Resume next" statement
*/
bool IsResumeNext()
{
int peek = Peek(1).kind;
return la.kind == Tokens.Resume && peek == Tokens.Next;
}
/// <summary>
/// Returns True, if ident/literal integer is followed by ":"
/// </summary>
bool IsLabel()
{
return (la.kind == Tokens.Identifier || la.kind == Tokens.LiteralInteger)
&& Peek(1).kind == Tokens.Colon;
}
/// <summary>
/// Returns true if a property declaration is an automatic property.
/// </summary>
bool IsAutomaticProperty()
{
lexer.StartPeek();
Token tn = la;
int braceCount = 0;
// look for attributes
while (tn.kind == Tokens.LessThan) {
while (braceCount > 0 || tn.kind != Tokens.GreaterThan) {
tn = lexer.Peek();
if (tn.kind == Tokens.OpenParenthesis)
braceCount++;
if (tn.kind == Tokens.CloseParenthesis)
braceCount--;
}
Debug.Assert(tn.kind == Tokens.GreaterThan);
tn = lexer.Peek();
}
// look for modifiers
var allowedTokens = new[] {
Tokens.Public, Tokens.Protected,
Tokens.Friend, Tokens.Private
};
while (allowedTokens.Contains(tn.kind))
tn = lexer.Peek();
if (tn.Kind != Tokens.Get && tn.Kind != Tokens.Set)
return true;
return false;
}
bool IsNotStatementSeparator()
{
return la.kind == Tokens.Colon && Peek(1).kind == Tokens.EOL;
}
static bool IsMustOverride(AttributedNode node)
{
return node.Modifiers.HasFlag(Modifiers.MustOverride);
}
/* Writes the type name represented through the expression into the string builder. */
/* Returns true when the expression was converted successfully, returns false when */
/* There was an unknown expression (e.g. TypeReferenceExpression) in it */
// bool WriteFullTypeName(StringBuilder b, Expression expr)
// {
// MemberReferenceExpression fre = expr as MemberReferenceExpression;
// if (fre != null) {
// bool result = WriteFullTypeName(b, fre.TargetObject);
// if (b.Length > 0) b.Append('.');
// b.Append(fre.MemberName);
// return result;
// } else if (expr is SimpleNameExpression) {
// b.Append(((SimpleNameExpression)expr).Identifier);
// return true;
// } else {
// return false;
// }
// }
void EnsureIsZero(Expression expr)
{
if (!(expr is PrimitiveExpression) || (expr as PrimitiveExpression).StringValue != "0")
Error("lower bound of array must be zero");
}
public bool ParseMethodBodies { get; set; }
public VBLexer Lexer {
get {
return lexer;
}
}
public Errors Errors {
get {
return errors;
}
}
public CompilationUnit CompilationUnit {
get {
return compilationUnit;
}
}
void SynErr(int n)
{
if (errDist >= MinErrDist) {
errors.SynErr(lexer.LookAhead.line, lexer.LookAhead.col, n);
}
errDist = 0;
}
void SemErr(string msg)
{
if (errDist >= MinErrDist) {
errors.Error(lexer.Token.line, lexer.Token.col, msg);
}
errDist = 0;
}
void Expect(int n)
{
if (lexer.LookAhead.kind == n) {
lexer.NextToken();
} else {
SynErr(n);
}
}
#region System.IDisposable interface implementation
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
public void Dispose()
{
errors = null;
if (lexer != null) {
lexer.Dispose();
}
lexer = null;
}
#endregion
}
}
|