public override PatternMatchResult GetMatch(List <Token> tokens, int @from = 0)
        {
            var openTokenMatch = TokenSearchUtils.FindToken(tokens, BlockStartToken, from);

            if (openTokenMatch.IsFullMatch)
            {
                var blockBeginIndex = openTokenMatch.Start;
                var openTokenCount  = 0;
                var closeTokenCount = 0;
                for (var i = blockBeginIndex; i < tokens.Count; i++)
                {
                    if (tokens[i].TokenType == BlockStartToken)
                    {
                        openTokenCount++;
                    }
                    else if (tokens[i].TokenType == BlockEndToken)
                    {
                        closeTokenCount++;
                    }

                    if (openTokenCount == closeTokenCount && openTokenCount != 0 && closeTokenCount != 0)
                    {
                        return(new PatternMatchResult(blockBeginIndex, i + 1));
                    }
                }
            }
            return(PatternMatchResult.Empty);
        }
Example #2
0
        public override PatternMatchResult GetMatch(List <Token> tokens, int @from = 0)
        {
            if (Seq.Count > 0)
            {
                var firstTokenMatch = TokenSearchUtils.FindToken(tokens, Seq[0], from);
                var seqStartIndex   = firstTokenMatch.Start;
                while (seqStartIndex + Seq.Count <= tokens.Count)
                {
                    var allMatches = true;
                    // from 1 !!!
                    for (var i = 1; i < Seq.Count; i++)
                    {
                        if (Seq[i] != tokens[seqStartIndex + i].TokenType)
                        {
                            allMatches = false;
                            break;
                        }
                    }

                    if (allMatches)
                    {
                        return(new PatternMatchResult(seqStartIndex, seqStartIndex + Seq.Count));
                    }
                    seqStartIndex++;
                    seqStartIndex = TokenSearchUtils.FindToken(tokens, Seq[0], seqStartIndex).Start;
                    if (seqStartIndex == -1)
                    {
                        return(PatternMatchResult.Empty);
                    }
                }
            }
            return(PatternMatchResult.Empty);
        }
Example #3
0
        public override PatternMatchResult GetMatch(List <Token> tokens, int @from = 0)
        {
            if (TokensToSearch.Count == 1)
            {
                return(TokenSearchUtils.FindToken(tokens, TokensToSearch[0], from));
            }
            var firstTokenIndex = int.MaxValue;

            foreach (var tokenType in TokensToSearch)
            {
                var lexMatchResult = TokenSearchUtils.FindToken(tokens, tokenType, from);
                if (lexMatchResult.IsFullMatch && lexMatchResult.Start < firstTokenIndex)
                {
                    firstTokenIndex = lexMatchResult.Start;
                }
            }

            if (firstTokenIndex == int.MaxValue)
            {
                return(PatternMatchResult.Empty);
            }
            return(new PatternMatchResult(firstTokenIndex, firstTokenIndex + 1));
        }
 public override PatternMatchResult GetMatch(List <Token> tokens, int @from = 0)
 {
     return(TokenSearchUtils.FindToken(tokens, Token, from));
 }