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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
|
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision: 4740 $</version>
// </file>
using ICSharpCode.NRefactory.Visitors;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.NRefactory.Parser.CSharp
{
internal sealed partial class Parser : AbstractParser
{
Lexer lexer;
public Parser(ILexer lexer) : base(lexer)
{
this.lexer = (Lexer)lexer;
// due to anonymous methods, we always need a compilation unit, so
// create it in the constructor
compilationUnit = new CompilationUnit();
}
StringBuilder qualidentBuilder = new StringBuilder();
Token t {
[System.Diagnostics.DebuggerStepThrough]
get {
return lexer.Token;
}
}
Token la {
[System.Diagnostics.DebuggerStepThrough]
get {
return lexer.LookAhead;
}
}
public void Error(string s)
{
if (errDist >= MinErrDist) {
this.Errors.Error(la.line, la.col, s);
}
errDist = 0;
}
public override void Parse()
{
ParseRoot();
compilationUnit.AcceptVisitor(new SetParentVisitor(), null);
}
public override TypeReference ParseTypeReference ()
{
lexer.NextToken();
TypeReference type;
Type(out type);
return type;
}
public override Expression ParseExpression()
{
lexer.NextToken();
Location startLocation = la.Location;
Expression expr;
Expr(out expr);
// SEMICOLON HACK : without a trailing semicolon, parsing expressions does not work correctly
if (la.kind == Tokens.Semicolon) lexer.NextToken();
if (expr != null) {
expr.StartLocation = startLocation;
expr.EndLocation = t.EndLocation;
expr.AcceptVisitor(new SetParentVisitor(), null);
}
Expect(Tokens.EOF);
return expr;
}
public override BlockStatement ParseBlock()
{
lexer.NextToken();
compilationUnit = new CompilationUnit();
BlockStatement blockStmt = new BlockStatement();
blockStmt.StartLocation = la.Location;
compilationUnit.BlockStart(blockStmt);
while (la.kind != Tokens.EOF) {
Token oldLa = la;
Statement();
if (la == oldLa) {
// did not advance lexer position, we cannot parse this as a statement block
return null;
}
}
compilationUnit.BlockEnd();
blockStmt.EndLocation = t.EndLocation;
Expect(Tokens.EOF);
blockStmt.AcceptVisitor(new SetParentVisitor(), null);
return blockStmt;
}
public override List<INode> ParseTypeMembers()
{
lexer.NextToken();
compilationUnit = new CompilationUnit();
TypeDeclaration newType = new TypeDeclaration(Modifiers.None, null);
compilationUnit.BlockStart(newType);
ClassBody();
compilationUnit.BlockEnd();
Expect(Tokens.EOF);
newType.AcceptVisitor(new SetParentVisitor(), null);
return newType.Children;
}
// Begin ISTypeCast
bool IsTypeCast()
{
if (la.kind != Tokens.OpenParenthesis) {
return false;
}
bool isPossibleExpression = true;
lexer.StartPeek();
Token pt = lexer.Peek();
if (!IsTypeNameOrKWForTypeCast(ref pt, ref isPossibleExpression)) {
return false;
}
// ")"
if (pt.kind != Tokens.CloseParenthesis) {
return false;
}
if (isPossibleExpression) {
// check successor
pt = lexer.Peek();
return Tokens.CastFollower[pt.kind];
} else {
// not possibly an expression: don't check cast follower
return true;
}
}
/* !!! Proceeds from current peek position !!! */
bool IsTypeKWForTypeCast(ref Token pt)
{
if (Tokens.TypeKW[pt.kind]) {
pt = lexer.Peek();
return IsPointerOrDims(ref pt) && SkipQuestionMark(ref pt);
} else if (pt.kind == Tokens.Void) {
pt = lexer.Peek();
return IsPointerOrDims(ref pt);
}
return false;
}
/* !!! Proceeds from current peek position !!! */
bool IsTypeNameOrKWForTypeCast(ref Token pt, ref bool isPossibleExpression)
{
if (Tokens.TypeKW[pt.kind] || pt.kind == Tokens.Void) {
isPossibleExpression = false;
return IsTypeKWForTypeCast(ref pt);
} else {
return IsTypeNameForTypeCast(ref pt, ref isPossibleExpression);
}
}
bool IsTypeNameOrKWForTypeCast(ref Token pt)
{
bool tmp = false;
return IsTypeNameOrKWForTypeCast(ref pt, ref tmp);
}
// TypeName = ident [ "::" ident ] { ["<" TypeNameOrKW { "," TypeNameOrKW } ">" ] "." ident } ["?"] PointerOrDims
/* !!! Proceeds from current peek position !!! */
bool IsTypeNameForTypeCast(ref Token pt, ref bool isPossibleExpression)
{
// ident
if (!IsIdentifierToken(pt)) {
return false;
}
pt = Peek();
// "::" ident
if (pt.kind == Tokens.DoubleColon) {
pt = Peek();
if (!IsIdentifierToken(pt)) {
return false;
}
pt = Peek();
}
// { ["<" TypeNameOrKW { "," TypeNameOrKW } ">" ] "." ident }
while (true) {
if (pt.kind == Tokens.LessThan) {
do {
pt = Peek();
if (!IsTypeNameOrKWForTypeCast(ref pt)) {
return false;
}
} while (pt.kind == Tokens.Comma);
if (pt.kind != Tokens.GreaterThan) {
return false;
}
pt = Peek();
}
if (pt.kind != Tokens.Dot)
break;
pt = Peek();
if (pt.kind != Tokens.Identifier) {
return false;
}
pt = Peek();
}
// ["?"]
if (pt.kind == Tokens.Question) {
pt = Peek();
}
if (pt.kind == Tokens.Times || pt.kind == Tokens.OpenSquareBracket) {
isPossibleExpression = false;
return IsPointerOrDims(ref pt);
}
return true;
}
// END IsTypeCast
// Gets if the token is a possible token for an expression start
// Is used to determine if "a is Type ? token" a the start of a ternary
// expression or a type test for Nullable<Type>
bool IsPossibleExpressionStart(int token)
{
return Tokens.CastFollower[token] || Tokens.UnaryOp[token];
}
// ( { [TypeNameOrKWForTypeCast] ident "," } )
bool IsLambdaExpression()
{
if (la.kind != Tokens.OpenParenthesis) {
return false;
}
StartPeek();
Token pt = Peek();
while (pt.kind != Tokens.CloseParenthesis) {
if (pt.kind == Tokens.Out || pt.kind == Tokens.Ref) {
pt = Peek();
}
if (!IsTypeNameOrKWForTypeCast(ref pt)) {
return false;
}
if (IsIdentifierToken(pt)) {
// make ident optional: if implicitly typed lambda arguments are used, IsTypeNameForTypeCast
// has already accepted the identifier
pt = Peek();
}
if (pt.kind == Tokens.CloseParenthesis) {
break;
}
// require comma between parameters:
if (pt.kind == Tokens.Comma) {
pt = Peek();
} else {
return false;
}
}
pt = Peek();
return pt.kind == Tokens.LambdaArrow;
}
/* Checks whether the next sequences of tokens is a qualident *
* and returns the qualident string */
/* !!! Proceeds from current peek position !!! */
bool IsQualident(ref Token pt, out string qualident)
{
if (IsIdentifierToken(pt)) {
qualidentBuilder.Length = 0; qualidentBuilder.Append(pt.val);
pt = Peek();
while (pt.kind == Tokens.Dot || pt.kind == Tokens.DoubleColon) {
pt = Peek();
if (!IsIdentifierToken(pt)) {
qualident = String.Empty;
return false;
}
qualidentBuilder.Append('.');
qualidentBuilder.Append(pt.val);
pt = Peek();
}
qualident = qualidentBuilder.ToString();
return true;
}
qualident = String.Empty;
return false;
}
/* Skips generic type extensions */
/* !!! Proceeds from current peek position !!! */
/* skip: { "*" | "[" { "," } "]" } */
/* !!! Proceeds from current peek position !!! */
bool IsPointerOrDims (ref Token pt)
{
for (;;) {
if (pt.kind == Tokens.OpenSquareBracket) {
do pt = Peek();
while (pt.kind == Tokens.Comma);
if (pt.kind != Tokens.CloseSquareBracket) return false;
} else if (pt.kind != Tokens.Times) break;
pt = Peek();
}
return true;
}
/* Return the n-th token after the current lookahead token */
void StartPeek()
{
lexer.StartPeek();
}
Token Peek()
{
return lexer.Peek();
}
Token Peek (int n)
{
lexer.StartPeek();
Token x = la;
while (n > 0) {
x = lexer.Peek();
n--;
}
return x;
}
/*-----------------------------------------------------------------*
* Resolver routines to resolve LL(1) conflicts: * *
* These resolution routine return a boolean value that indicates *
* whether the alternative at hand shall be choosen or not. *
* They are used in IF ( ... ) expressions. *
*-----------------------------------------------------------------*/
/* True, if ident is followed by "=" */
bool IdentAndAsgn ()
{
return IsIdentifierToken(la) && Peek(1).kind == Tokens.Assign;
}
bool IdentAndDoubleColon ()
{
return IsIdentifierToken(la) && Peek(1).kind == Tokens.DoubleColon;
}
bool IsAssignment () { return IdentAndAsgn(); }
/* True, if ident is followed by ",", "=", "[" or ";" */
bool IsVarDecl () {
int peek = Peek(1).kind;
return IsIdentifierToken(la) &&
(peek == Tokens.Comma || peek == Tokens.Assign || peek == Tokens.Semicolon || peek == Tokens.OpenSquareBracket);
}
/* 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 && peek != Tokens.CloseSquareBracket;
}
/* True, if "void" is followed by "*" */
bool NotVoidPointer () {
return la.kind == Tokens.Void && Peek(1).kind != Tokens.Times;
}
/* True, if "checked" or "unchecked" are followed by "{" */
bool UnCheckedAndLBrace () {
return la.kind == Tokens.Checked || la.kind == Tokens.Unchecked &&
Peek(1).kind == Tokens.OpenCurlyBrace;
}
/* True, if "." is followed by an ident */
bool DotAndIdent () {
return la.kind == Tokens.Dot && IsIdentifierToken(Peek(1));
}
/* True, if ident is followed by ":" */
bool IdentAndColon () {
return IsIdentifierToken(la) && Peek(1).kind == Tokens.Colon;
}
bool IsLabel () { return IdentAndColon(); }
/* True, if ident is followed by "(" */
bool IdentAndLPar () {
return IsIdentifierToken(la) && Peek(1).kind == Tokens.OpenParenthesis;
}
/* True, if "catch" is followed by "(" */
bool CatchAndLPar () {
return la.kind == Tokens.Catch && Peek(1).kind == Tokens.OpenParenthesis;
}
bool IsTypedCatch () { return CatchAndLPar(); }
/* True, if "[" is followed by the ident "assembly" */
bool IsGlobalAttrTarget () {
Token pt = Peek(1);
return la.kind == Tokens.OpenSquareBracket &&
IsIdentifierToken(pt) && (pt.val == "assembly" || pt.val == "module");
}
/* True, if "[" is followed by "," or "]" */
bool LBrackAndCommaOrRBrack () {
int peek = Peek(1).kind;
return la.kind == Tokens.OpenSquareBracket &&
(peek == Tokens.Comma || peek == Tokens.CloseSquareBracket);
}
/* True, if "[" is followed by "," or "]" */
/* or if the current token is "*" */
bool TimesOrLBrackAndCommaOrRBrack () {
return la.kind == Tokens.Times || LBrackAndCommaOrRBrack();
}
bool IsPointerOrDims () { return TimesOrLBrackAndCommaOrRBrack(); }
bool IsPointer () { return la.kind == Tokens.Times; }
bool SkipGeneric(ref Token pt)
{
if (pt.kind == Tokens.LessThan) {
do {
pt = Peek();
if (!IsTypeNameOrKWForTypeCast(ref pt)) return false;
} while (pt.kind == Tokens.Comma);
if (pt.kind != Tokens.GreaterThan) return false;
pt = Peek();
}
return true;
}
bool SkipQuestionMark(ref Token pt)
{
if (pt.kind == Tokens.Question) {
pt = Peek();
}
return true;
}
/* True, if lookahead is a primitive type keyword, or */
/* if it is a type declaration followed by an ident */
bool IsLocalVarDecl ()
{
if (IsYieldStatement ()) {
return false;
}
if ((Tokens.TypeKW[la.kind] && Peek (1).kind != Tokens.Dot) || la.kind == Tokens.Void) {
return true;
}
StartPeek ();
Token pt = la;
bool result = IsTypeNameOrKWForTypeCast (ref pt) && IsIdentifierToken (pt);
if (Peek (1).kind == Tokens.Question) {
if (Peek (2).kind == Tokens.OpenSquareBracket) // array case: T?[,]
return true;
result &= Peek (2).kind == Tokens.Identifier;
result &= Peek (3).kind == Tokens.Semicolon || Peek (3).kind == Tokens.Comma || Peek (3).kind == Tokens.Assign;
}
return result;
}
/* True if lookahead is a type argument list (<...>) followed by
* one of "( ) ] } : ; , . ? == !=" */
bool IsGenericInSimpleNameOrMemberAccess()
{
Token t = la;
if (t.kind != Tokens.LessThan) return false;
StartPeek();
return SkipGeneric(ref t) && Tokens.GenericFollower[t.kind];
}
bool IsExplicitInterfaceImplementation()
{
StartPeek();
Token pt = la;
pt = Peek();
if (pt.kind == Tokens.Dot || pt.kind == Tokens.DoubleColon)
return true;
if (pt.kind == Tokens.LessThan) {
if (SkipGeneric(ref pt))
return pt.kind == Tokens.Dot;
}
return false;
}
/* True, if lookahead ident is "yield" and than follows a break or return */
bool IsYieldStatement () {
return la.kind == Tokens.Yield && (Peek(1).kind == Tokens.Return || Peek(1).kind == Tokens.Break);
}
/* True, if lookahead is a local attribute target specifier, *
* i.e. one of "event", "return", "field", "method", *
* "module", "param", "property", or "type" */
bool IsLocalAttrTarget () {
int cur = la.kind;
string val = la.val;
return (cur == Tokens.Event || cur == Tokens.Return ||
(Tokens.IdentifierTokens[cur] &&
(val == "field" || val == "method" || val == "module" ||
val == "param" || val == "property" || val == "type"))) &&
Peek(1).kind == Tokens.Colon;
}
bool IsShiftRight()
{
Token next = Peek(1);
// TODO : Add col test (seems not to work, lexer bug...) : && la.col == next.col - 1
return (la.kind == Tokens.GreaterThan && next.kind == Tokens.GreaterThan);
}
bool IsGenericExpression(Expression expr)
{
if (expr is IdentifierExpression)
return ((IdentifierExpression)expr).TypeArguments.Count > 0;
else if (expr is MemberReferenceExpression)
return ((MemberReferenceExpression)expr).TypeArguments.Count > 0;
else
return false;
}
bool ShouldConvertTargetExpressionToTypeReference(Expression targetExpr)
{
if (targetExpr is IdentifierExpression)
return ((IdentifierExpression)targetExpr).TypeArguments.Count > 0;
else if (targetExpr is MemberReferenceExpression)
return ((MemberReferenceExpression)targetExpr).TypeArguments.Count > 0;
else
return false;
}
TypeReference GetTypeReferenceFromExpression(Expression expr)
{
if (expr is TypeReferenceExpression)
return (expr as TypeReferenceExpression).TypeReference;
IdentifierExpression ident = expr as IdentifierExpression;
if (ident != null) {
return new TypeReference(ident.Identifier, ident.TypeArguments);
}
MemberReferenceExpression member = expr as MemberReferenceExpression;
if (member != null) {
TypeReference targetType = GetTypeReferenceFromExpression(member.TargetObject);
if (targetType != null) {
if (targetType.GenericTypes.Count == 0 && targetType.IsArrayType == false) {
TypeReference tr = new TypeReference(targetType.Type + "." + member.MemberName, member.TypeArguments);
tr.IsGlobal = targetType.IsGlobal;
return tr;
} else {
return new InnerClassTypeReference(targetType, member.MemberName, member.TypeArguments);
}
}
}
return null;
}
bool IsMostNegativeIntegerWithoutTypeSuffix()
{
Token token = la;
if (token.kind == Tokens.Literal) {
return token.val == "2147483648" || token.val == "9223372036854775808";
} else {
return false;
}
}
bool LastExpressionIsUnaryMinus(System.Collections.ArrayList expressions)
{
if (expressions.Count == 0) return false;
UnaryOperatorExpression uoe = expressions[expressions.Count - 1] as UnaryOperatorExpression;
if (uoe != null) {
return uoe.Op == UnaryOperatorType.Minus;
} else {
return false;
}
}
bool StartOfQueryExpression()
{
if (la.kind == Tokens.From) {
Token p = Peek(1);
if (IsIdentifierToken(p) || Tokens.TypeKW[p.kind])
return true;
}
return false;
}
static bool IsIdentifierToken(Token tk)
{
return Tokens.IdentifierTokens[tk.kind];
}
/// <summary>
/// Adds a child item to a collection stored in the parent node.
/// Also set's the item's parent to <paramref name="parent"/>.
/// Does nothing if item is null.
/// </summary>
static void SafeAdd<T>(INode parent, List<T> list, T item) where T : class, INode
{
Debug.Assert(parent != null);
Debug.Assert((parent is INullable) ? !(parent as INullable).IsNull : true);
if (item != null) {
list.Add(item);
item.Parent = parent;
}
}
internal static string GetReflectionNameForOperator(OverloadableOperatorType op)
{
switch (op) {
case OverloadableOperatorType.None:
return "op_unknown";
case OverloadableOperatorType.Add:
return "op_Addition";
case OverloadableOperatorType.BitNot:
return "op_OnesComplement";
case OverloadableOperatorType.BitwiseAnd:
return "op_BitwiseAnd";
case OverloadableOperatorType.BitwiseOr:
return "op_BitwiseOr";
case OverloadableOperatorType.Concat:
case OverloadableOperatorType.CType:
return "op_unknown";
case OverloadableOperatorType.Decrement:
return "op_Decrement";
case OverloadableOperatorType.Divide:
return "op_Division";
case OverloadableOperatorType.DivideInteger:
return "op_unknown";
case OverloadableOperatorType.Equality:
return "op_Equality";
case OverloadableOperatorType.ExclusiveOr:
return "op_ExclusiveOr";
case OverloadableOperatorType.GreaterThan:
return "op_GreaterThan";
case OverloadableOperatorType.GreaterThanOrEqual:
return "op_GreaterThanOrEqual";
case OverloadableOperatorType.Increment:
return "op_Increment";
case OverloadableOperatorType.InEquality:
return "op_Inequality";
case OverloadableOperatorType.IsFalse:
return "op_False";
case OverloadableOperatorType.IsTrue:
return "op_True";
case OverloadableOperatorType.LessThan:
return "op_LessThan";
case OverloadableOperatorType.LessThanOrEqual:
return "op_LessThanOrEqual";
case OverloadableOperatorType.Like:
return "op_unknown";
case OverloadableOperatorType.Modulus:
return "op_Modulus";
case OverloadableOperatorType.Multiply:
return "op_Multiply";
case OverloadableOperatorType.Not:
return "op_LogicalNot";
case OverloadableOperatorType.Power:
return "op_unknown";
case OverloadableOperatorType.ShiftLeft:
return "op_LeftShift";
case OverloadableOperatorType.ShiftRight:
return "op_RightShift";
case OverloadableOperatorType.Subtract:
return "op_Subtraction";
case OverloadableOperatorType.UnaryMinus:
return "op_UnaryNegation";
case OverloadableOperatorType.UnaryPlus:
return "op_UnaryPlus";
default:
throw new NotSupportedException("opeartor type:" + op);
}
}
}
}
|