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
|
// 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.Linq;
using System.Text;
namespace ICSharpCode.NRefactory.VB.Parser
{
public partial class ExpressionFinder
{
Stack<Block> stack = new Stack<Block>();
StringBuilder output = new StringBuilder();
void PopContext()
{
if (stack.Any()) {
string indent = new string('\t', stack.Count - 1);
var item = stack.Pop();
item.isClosed = true;
Print(indent + "exit " + item.context);
} else {
Print("empty stack");
}
}
void PushContext(Context context, Token la, Token t)
{
string indent = new string('\t', stack.Count);
TextLocation l = la == null ? (t == null ? TextLocation.Empty : t.EndLocation) : la.Location;
stack.Push(new Block() { context = context, lastExpressionStart = l });
Print(indent + "enter " + context);
}
public ExpressionFinder(ExpressionFinderState state)
{
wasQualifierTokenAtStart = state.WasQualifierTokenAtStart;
nextTokenIsPotentialStartOfExpression = state.NextTokenIsPotentialStartOfExpression;
nextTokenIsStartOfImportsOrAccessExpression = state.NextTokenIsStartOfImportsOrAccessExpression;
readXmlIdentifier = state.ReadXmlIdentifier;
identifierExpected = state.IdentifierExpected;
stateStack = new Stack<int>(state.StateStack.Reverse());
stack = new Stack<Block>(state.BlockStack.Select(x => (Block)x.Clone()).Reverse());
currentState = state.CurrentState;
output = new StringBuilder();
}
void Print(string text)
{
//Console.WriteLine(text);
output.AppendLine(text);
}
public void SetContext(SnippetType type)
{
switch (type) {
case SnippetType.Expression:
currentState = startOfExpression;
break;
}
Advance();
}
public string Output {
get { return output.ToString(); }
}
public string Stacktrace {
get {
string text = "";
foreach (Block b in stack) {
text += b.ToString() + "\n";
}
return text;
}
}
public Block CurrentBlock {
get { return stack.Any() ? stack.Peek() : Block.Default; }
}
public bool IsIdentifierExpected {
get { return identifierExpected; }
}
void SetIdentifierExpected(Token la)
{
identifierExpected = true;
if (la != null)
CurrentBlock.lastExpressionStart = la.Location;
else if (t != null)
CurrentBlock.lastExpressionStart = t.EndLocation;
}
public bool InContext(Context expected)
{
return stack
.SkipWhile(f => f.context == Context.Expression)
.IsElement(fx => fx.context == expected);
}
public bool NextTokenIsPotentialStartOfExpression {
get { return nextTokenIsPotentialStartOfExpression; }
}
public bool ReadXmlIdentifier {
get { return readXmlIdentifier; }
set { readXmlIdentifier = value; }
}
public bool NextTokenIsStartOfImportsOrAccessExpression {
get { return nextTokenIsStartOfImportsOrAccessExpression; }
}
public bool WasQualifierTokenAtStart {
get { return wasQualifierTokenAtStart; }
}
public bool IsMissingModifier {
get { return isMissingModifier; }
}
public bool WasNormalAttribute {
get { return wasNormalAttribute; }
}
public int ActiveArgument {
get { return activeArgument; }
}
public List<Token> Errors {
get { return errors; }
}
public ExpressionFinderState Export()
{
return new ExpressionFinderState() {
WasQualifierTokenAtStart = wasQualifierTokenAtStart,
NextTokenIsPotentialStartOfExpression = nextTokenIsPotentialStartOfExpression,
NextTokenIsStartOfImportsOrAccessExpression = nextTokenIsStartOfImportsOrAccessExpression,
ReadXmlIdentifier = readXmlIdentifier,
IdentifierExpected = identifierExpected,
StateStack = new Stack<int>(stateStack.Reverse()),
BlockStack = new Stack<Block>(stack.Select(x => (Block)x.Clone()).Reverse()),
CurrentState = currentState
};
}
}
}
|