static DToken SearchBackward(TextDocument doc, int caretOffset, CodeLocation caret,out DToken lastToken) { var ttp = doc.GetText(0, caretOffset); var sr = new StringReader(ttp); var lexer = new Lexer(sr); lexer.NextToken(); var stk=new Stack<DToken>(); while (lexer.LookAhead.Kind!=DTokens.EOF) { if (lexer.LookAhead.Kind == DTokens.OpenParenthesis || lexer.LookAhead.Kind==DTokens.OpenSquareBracket || lexer.LookAhead.Kind==DTokens.OpenCurlyBrace) stk.Push(lexer.LookAhead); else if (lexer.LookAhead.Kind == DTokens.CloseParenthesis || lexer.LookAhead.Kind == DTokens.CloseSquareBracket || lexer.LookAhead.Kind == DTokens.CloseCurlyBrace) { if (stk.Peek().Kind == getOppositeBracketToken( lexer.LookAhead.Kind)) stk.Pop(); } lexer.NextToken(); } lastToken = lexer.CurrentToken; sr.Close(); lexer.Dispose(); if (stk.Count < 1) return null; return stk.Pop(); }
static DToken SearchForward(TextDocument doc, int caretOffset, CodeLocation caret, int searchedBracketToken) { var code = doc.GetText(caretOffset, doc.TextLength - caretOffset); var lexer = new Lexer(new System.IO.StringReader(code)); lexer.SetInitialLocation(caret); lexer.NextToken(); var stk = new Stack<DToken>(); while (lexer.LookAhead.Kind!=DTokens.EOF) { if (lexer.LookAhead.Kind == DTokens.OpenParenthesis || lexer.LookAhead.Kind == DTokens.OpenSquareBracket || lexer.LookAhead.Kind == DTokens.OpenCurlyBrace) stk.Push(lexer.LookAhead); else if (lexer.LookAhead.Kind == DTokens.CloseParenthesis || lexer.LookAhead.Kind == DTokens.CloseSquareBracket || lexer.LookAhead.Kind == DTokens.CloseCurlyBrace) { if(stk.Count != 0) stk.Pop(); else if (lexer.LookAhead.Kind == searchedBracketToken) return lexer.LookAhead; } lexer.NextToken(); } lexer.Dispose(); if (stk.Count < 1) return null; return stk.Pop(); }