示例#1
0
        private IParseResult <ExpressionMetaData> TryGetGroupExpression(ITokenStream tokenStream, out IExpression expression)
        {
            tokenStream.Pop();

            var closePrecedenceToken = tokenStream.LookAhead(0).GetType <RegexTokenType>();

            if (closePrecedenceToken == RegexTokenType.ClosePrecedence)
            {
                tokenStream.Pop();
                expression = new EmptyExpression();
                return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData()));
            }

            var unionExpressionResult = TryGetUnionExpression <UnionExpression>(tokenStream, out expression);

            if (!unionExpressionResult.IsSuccess)
            {
                return(unionExpressionResult);
            }

            closePrecedenceToken = tokenStream.LookAhead(0).GetType <RegexTokenType>();

            if (closePrecedenceToken != RegexTokenType.ClosePrecedence)
            {
                return(new FailureParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, RegSeedErrorType.ClosePrecedenceExpected));
            }

            tokenStream.Pop();
            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, unionExpressionResult.Value));
        }
示例#2
0
        private static IntegerInterval GetRepeatConcatenationInterval(ITokenStream tokenStream)
        {
            var nextToken = tokenStream.LookAhead(0).GetType <RegexTokenType>();

            return(nextToken != RegexTokenType.IntegerRange
                ? new IntegerInterval(1)
                : tokenStream.Pop().GetValue <IntegerInterval>());
        }
示例#3
0
        private IParseResult <ExpressionMetaData> TryGetCharacterClassExpression(ITokenStream tokenStream, out IExpression expression)
        {
            expression = null;

            var nextTokenType = tokenStream.Pop().GetType <RegexTokenType>();

            var getComplement = nextTokenType == RegexTokenType.OpenNegateCharacterClass;

            nextTokenType = tokenStream.LookAhead(0).GetType <RegexTokenType>();

            var characters = new List <string>();

            while (nextTokenType == RegexTokenType.Character || nextTokenType == RegexTokenType.CharacterRange)
            {
                var nextToken = tokenStream.Pop();

                if (nextToken.GetType <RegexTokenType>() == RegexTokenType.Character)
                {
                    characters.Add(nextToken.GetValue <string>());
                }
                else
                {
                    characters.AddRange(nextToken.GetValue <CharacterRange>().Characters);
                }

                nextTokenType = tokenStream.LookAhead(0).GetType <RegexTokenType>();
            }

            if (nextTokenType != RegexTokenType.CloseCharacterClass)
            {
                return(new FailureParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, RegSeedErrorType.CloseCharacterClassExpected));
            }

            tokenStream.Pop();

            var characterClassExpression = new CharacterClassExpression(_alphabet, _random, _maxCharClassInverseLength);

            characterClassExpression.AddCharacters(characters);

            expression = getComplement ? characterClassExpression.GetComplement() : characterClassExpression;
            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData()));
        }
示例#4
0
        private IParseResult <ExpressionMetaData> TryGetAnyCharacterExpression(ITokenStream tokenStream, out IExpression expression)
        {
            tokenStream.Pop();

            var characterExpression = new CharacterClassExpression(_alphabet, _random, _maxCharClassInverseLength);

            characterExpression.AddCharacters(_alphabet.GetAllCharacters());

            expression = characterExpression;

            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData()));
        }
示例#5
0
        private IParseResult <ExpressionMetaData> TryGetSingleCharacterExpression(ITokenStream tokenStream, out IExpression expression)
        {
            expression = null;
            var tokenValue = tokenStream.Pop().GetValue <string>();
            var characterClassExpression = new CharacterClassExpression(_alphabet, _random, _maxCharClassInverseLength);

            characterClassExpression.AddCharacters(new List <string> {
                tokenValue
            });

            expression = characterClassExpression;

            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData()));
        }
示例#6
0
        private IParseResult <ExpressionMetaData> TryGetInverseElementaryExpression(ITokenStream tokenStream, out IExpression expression)
        {
            expression = null;
            tokenStream.Pop();
            var elementaryResult = TryGetElementaryExpression(tokenStream, out var toInvertExpression);

            if (!elementaryResult.IsSuccess)
            {
                return(elementaryResult);
            }

            expression = toInvertExpression.GetInverse();
            expression.MaxExpansionRange = IntegerInterval.MaxInterval;

            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, elementaryResult.Value));
        }
示例#7
0
        private IParseResult <ExpressionMetaData> TryGetSubExpressionList(RegexTokenType token, ITokenStream tokenStream, out List <IExpression> subExpressionList)
        {
            subExpressionList = null;

            var subExpressionResult = _regexTokenToExpressionListMapper[token](tokenStream, out var subExpression);

            if (!subExpressionResult.IsSuccess)
            {
                return(subExpressionResult);
            }

            subExpressionList = new List <IExpression> {
                subExpression
            };

            var nextToken = tokenStream.LookAhead(0).GetType <RegexTokenType>();
            var metaData  = subExpressionResult.Value;

            while (nextToken == token)
            {
                if (token == RegexTokenType.Intersection)
                {
                    metaData.HasIntersection = true;
                }

                tokenStream.Pop();

                subExpressionResult = _regexTokenToExpressionListMapper[token](tokenStream, out subExpression);

                if (!subExpressionResult.IsSuccess)
                {
                    return(subExpressionResult);
                }

                metaData.UpdateWith(subExpressionResult.Value);
                subExpressionList.Add(subExpression);
                nextToken = tokenStream.LookAhead(0).GetType <RegexTokenType>();
            }

            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, metaData));
        }