/NRefactory/ICSharpCode.NRefactory.VB/Lexer/ExpressionFinder.cs

http://github.com/icsharpcode/ILSpy · C# · 154 lines · 125 code · 26 blank · 3 comment · 12 complexity · 2d8702b663665405c41ec7daf61d50b5 MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2. // This code is distributed under MIT X11 license (for details please see \doc\license.txt)
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace ICSharpCode.NRefactory.VB.Parser
  8. {
  9. public partial class ExpressionFinder
  10. {
  11. Stack<Block> stack = new Stack<Block>();
  12. StringBuilder output = new StringBuilder();
  13. void PopContext()
  14. {
  15. if (stack.Any()) {
  16. string indent = new string('\t', stack.Count - 1);
  17. var item = stack.Pop();
  18. item.isClosed = true;
  19. Print(indent + "exit " + item.context);
  20. } else {
  21. Print("empty stack");
  22. }
  23. }
  24. void PushContext(Context context, Token la, Token t)
  25. {
  26. string indent = new string('\t', stack.Count);
  27. TextLocation l = la == null ? (t == null ? TextLocation.Empty : t.EndLocation) : la.Location;
  28. stack.Push(new Block() { context = context, lastExpressionStart = l });
  29. Print(indent + "enter " + context);
  30. }
  31. public ExpressionFinder(ExpressionFinderState state)
  32. {
  33. wasQualifierTokenAtStart = state.WasQualifierTokenAtStart;
  34. nextTokenIsPotentialStartOfExpression = state.NextTokenIsPotentialStartOfExpression;
  35. nextTokenIsStartOfImportsOrAccessExpression = state.NextTokenIsStartOfImportsOrAccessExpression;
  36. readXmlIdentifier = state.ReadXmlIdentifier;
  37. identifierExpected = state.IdentifierExpected;
  38. stateStack = new Stack<int>(state.StateStack.Reverse());
  39. stack = new Stack<Block>(state.BlockStack.Select(x => (Block)x.Clone()).Reverse());
  40. currentState = state.CurrentState;
  41. output = new StringBuilder();
  42. }
  43. void Print(string text)
  44. {
  45. //Console.WriteLine(text);
  46. output.AppendLine(text);
  47. }
  48. public void SetContext(SnippetType type)
  49. {
  50. switch (type) {
  51. case SnippetType.Expression:
  52. currentState = startOfExpression;
  53. break;
  54. }
  55. Advance();
  56. }
  57. public string Output {
  58. get { return output.ToString(); }
  59. }
  60. public string Stacktrace {
  61. get {
  62. string text = "";
  63. foreach (Block b in stack) {
  64. text += b.ToString() + "\n";
  65. }
  66. return text;
  67. }
  68. }
  69. public Block CurrentBlock {
  70. get { return stack.Any() ? stack.Peek() : Block.Default; }
  71. }
  72. public bool IsIdentifierExpected {
  73. get { return identifierExpected; }
  74. }
  75. void SetIdentifierExpected(Token la)
  76. {
  77. identifierExpected = true;
  78. if (la != null)
  79. CurrentBlock.lastExpressionStart = la.Location;
  80. else if (t != null)
  81. CurrentBlock.lastExpressionStart = t.EndLocation;
  82. }
  83. public bool InContext(Context expected)
  84. {
  85. return stack
  86. .SkipWhile(f => f.context == Context.Expression)
  87. .IsElement(fx => fx.context == expected);
  88. }
  89. public bool NextTokenIsPotentialStartOfExpression {
  90. get { return nextTokenIsPotentialStartOfExpression; }
  91. }
  92. public bool ReadXmlIdentifier {
  93. get { return readXmlIdentifier; }
  94. set { readXmlIdentifier = value; }
  95. }
  96. public bool NextTokenIsStartOfImportsOrAccessExpression {
  97. get { return nextTokenIsStartOfImportsOrAccessExpression; }
  98. }
  99. public bool WasQualifierTokenAtStart {
  100. get { return wasQualifierTokenAtStart; }
  101. }
  102. public bool IsMissingModifier {
  103. get { return isMissingModifier; }
  104. }
  105. public bool WasNormalAttribute {
  106. get { return wasNormalAttribute; }
  107. }
  108. public int ActiveArgument {
  109. get { return activeArgument; }
  110. }
  111. public List<Token> Errors {
  112. get { return errors; }
  113. }
  114. public ExpressionFinderState Export()
  115. {
  116. return new ExpressionFinderState() {
  117. WasQualifierTokenAtStart = wasQualifierTokenAtStart,
  118. NextTokenIsPotentialStartOfExpression = nextTokenIsPotentialStartOfExpression,
  119. NextTokenIsStartOfImportsOrAccessExpression = nextTokenIsStartOfImportsOrAccessExpression,
  120. ReadXmlIdentifier = readXmlIdentifier,
  121. IdentifierExpected = identifierExpected,
  122. StateStack = new Stack<int>(stateStack.Reverse()),
  123. BlockStack = new Stack<Block>(stack.Select(x => (Block)x.Clone()).Reverse()),
  124. CurrentState = currentState
  125. };
  126. }
  127. }
  128. }