示例#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> TryGetElementaryExpression(ITokenStream tokenStream, out IExpression expression)
        {
            expression = null;
            var nextToken = tokenStream.LookAhead(0).GetType <RegexTokenType>();

            return(_regexTokenToExpressionMapper.TryGetValue(nextToken, out var tryGetElementaryExpression)
                ? tryGetElementaryExpression(tokenStream, out expression)
                : new FailureParseResult <ExpressionMetaData>(tokenStream.CurrentPosition));
        }
示例#4
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()));
        }
示例#5
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));
        }
示例#6
0
        private IParseResult <ExpressionMetaData> TryGetExpressionListSeparatedByToken <TExpression>(ITokenStream tokenStream, out IExpression expression)
        {
            expression = new EmptyExpression();

            if (tokenStream.LookAhead(0).GetType <RegexTokenType>() == RegexTokenType.EndOfStream)
            {
                return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, new ExpressionMetaData()));
            }

            var token = _expressionTokenTypeMapper[typeof(TExpression)];
            var subExpressionResult = TryGetSubExpressionList(token, tokenStream, out var subExpressionList);

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

            expression = CreateExpression <TExpression>(subExpressionList);
            return(new SuccessParseResult <ExpressionMetaData>(tokenStream.CurrentPosition, subExpressionResult.Value));
        }