PageRenderTime 23ms CodeModel.GetById 8ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

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