public ExpressionResult FindExpression(string text, int offset)
        {
            Init(text, offset);

            ExpressionFinder p = new ExpressionFinder();

            lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(text));
            Token t = lexer.NextToken();

            // put all tokens in front of targetPosition into the EF-Parser
            while (t.EndLocation < targetPosition)
            {
                p.InformToken(t);
                t = lexer.NextToken();
            }

            // put current token into EF-Parser if it cannot be continued (is simple operator)
            if (t.EndLocation == targetPosition && ((t.Kind <= Tokens.ColonAssign && t.Kind > Tokens.Identifier) || t.Kind == Tokens.EOL))
            {
                p.InformToken(t);
                t = lexer.NextToken();
            }

            // make sure semantic actions are executed
            p.Advance();

            // remember current state, we'll use it to determine the context
            var block = p.CurrentBlock;

            ExpressionContext context = p.IsIdentifierExpected && !p.IsMissingModifier ? ExpressionContext.IdentifierExpected : GetContext(block);

            BitArray expectedSet;

            try {
                expectedSet = p.GetExpectedSet();
            } catch (InvalidOperationException) {
                expectedSet = null;
            }

            // put current token into EF-Parser
            if (t.Location < targetPosition)
            {
                p.InformToken(t);
            }

            if (p.Errors.Any())
            {
                foreach (var e in p.Errors)
                {
                    LoggingService.Warn("not expected: " + e);
                }
            }

            if (p.NextTokenIsPotentialStartOfExpression)
            {
                return(new ExpressionResult("", new DomRegion(targetPosition.Line, targetPosition.Column), context, expectedSet));
            }

            int lastExpressionStartOffset = LocationToOffset(p.CurrentBlock.lastExpressionStart);

            if (lastExpressionStartOffset < 0)
            {
                return(new ExpressionResult("", new DomRegion(targetPosition.Line, targetPosition.Column), context, expectedSet));
            }

            return(MakeResult(text, lastExpressionStartOffset, offset, context, expectedSet));
        }
        public ExpressionResult FindFullExpression(string text, int offset)
        {
            Init(text, offset);

            ExpressionFinder p = new ExpressionFinder();

            lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(text));
            Token t;

            Block block = Block.Default;

            var expressionDelimiters = new[] { Tokens.EOL, Tokens.Colon, Tokens.Dot, Tokens.TripleDot, Tokens.DotAt };

            while (true)
            {
                t = lexer.NextToken();
                p.InformToken(t);

                if (block == Block.Default && t.EndLocation > targetPosition)
                {
                    block = p.CurrentBlock;
                }
                if (block != Block.Default && (block.isClosed || expressionDelimiters.Contains(t.Kind) && block == p.CurrentBlock))
                {
                    break;
                }
                if (t.Kind == Tokens.EOF)
                {
                    break;
                }
            }

            if (p.Errors.Any())
            {
                foreach (var e in p.Errors)
                {
                    LoggingService.Warn("not expected: " + e);
                }
            }

            BitArray expectedSet;

            try {
                expectedSet = p.GetExpectedSet();
            } catch (InvalidOperationException) {
                expectedSet = null;
            }

            int tokenOffset;

            if (t == null || t.Kind == Tokens.EOF)
            {
                tokenOffset = text.Length;
            }
            else
            {
                tokenOffset = LocationToOffset(t.Location);
            }

            int lastExpressionStartOffset = LocationToOffset(block.lastExpressionStart);

            if (lastExpressionStartOffset >= 0)
            {
                if (offset < tokenOffset)
                {
                    // offset is in front of this token
                    return(MakeResult(text, lastExpressionStartOffset, tokenOffset, GetContext(block), expectedSet));
                }
                else
                {
                    // offset is IN this token
                    return(MakeResult(text, lastExpressionStartOffset, offset, GetContext(block), expectedSet));
                }
            }
            else
            {
                return(new ExpressionResult(null, GetContext(block)));
            }
        }