Ejemplo n.º 1
0
        private static IEnumerable <LiteralNode> Expand(INode bracketNode)
        {
            foreach (var childNode in bracketNode.ChildNodes)
            {
                switch (childNode.Token.TokenType)
                {
                case TokenType.Literal:
                    yield return((LiteralNode)childNode);

                    break;

                case TokenType.Range:
                    var min = (int)((LiteralNode)childNode.ChildNodes[0]).Token.Character;
                    var max = (int)((LiteralNode)childNode.ChildNodes[1]).Token.Character;
                    for (var i = min; i <= max; i++)
                    {
                        var c = (char)i;
                        yield return(NodeBuilder.BuildLiteralNode(TokenBuilder.BuildLiteralToken(c)));
                    }
                    break;

                case TokenType.BracketRight:
                    foreach (var node in Expand(childNode))
                    {
                        yield return(node);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public IToken Handle(IContext context)
        {
            context.EndState();
            switch (context.Current)
            {
            case 'd':
                return(TokenBuilder.BuildNumericToken());

            case 'w':
                return(TokenBuilder.BuildWordToken());

            case 's':
                return(TokenBuilder.BuildWhitespaceToken());

            case 'D':
                return(TokenBuilder.BuildNonNumericToken());

            case 'W':
                return(TokenBuilder.BuildNonWordToken());

            case 'S':
                return(TokenBuilder.BuildNonWhitespaceToken());

            default:
                return(TokenBuilder.BuildLiteralToken(context.Current));
            }
        }
Ejemplo n.º 3
0
        public void Visit(NotNode node)
        {
            var nodes = new LiteralNodeCollection();

            for (var i = 32; i < 126; i++)
            {
                nodes.Add(NodeBuilder.BuildLiteralNode(TokenBuilder.BuildLiteralToken((char)i)));
            }

            INode current = node;

            while (current.ChildNodes[0].Token.TokenType == TokenType.Concatenation)
            {
                current = node.ChildNodes[0];
            }

            foreach (var expandedNode in Expand(current))
            {
                if (nodes.Contains(expandedNode))
                {
                    nodes.Remove(expandedNode);
                }
            }

            var index = RandomNumberProvider.GetRandomNumber(0, nodes.Count);

            nodes[index].Accept(this);
        }
Ejemplo n.º 4
0
        public void Visit(RangeNode node)
        {
            var min     = (int)((LiteralNode)node.ChildNodes[0]).Token.Character;
            var max     = (int)((LiteralNode)node.ChildNodes[1]).Token.Character;
            var index   = RandomNumberProvider.GetRandomNumber(min, max + 1);
            var literal = new LiteralNode(TokenBuilder.BuildLiteralToken((char)index));

            literal.Accept(this);
        }
Ejemplo n.º 5
0
        public IToken Handle(IContext context)
        {
            switch (context.Current)
            {
            case '^':
                return(TokenBuilder.BuildNotToken());

            case '\\':
                context.ToState(new SetState());
                context.ToState(new EscapeState());
                break;

            default:
                context.ToState(new SetState());
                return(TokenBuilder.BuildLiteralToken(context.Current));
            }
            return(null);
        }
Ejemplo n.º 6
0
        public IToken Handle(IContext context)
        {
            switch (context.Current)
            {
            case '(':
                return(TokenBuilder.BuildParenthesisLeftToken());

            case ')':
                return(TokenBuilder.BuildParenthesisRightToken());

            case '{':
                context.ToState(new RepetitionState());
                break;

            case '\\':
                context.ToState(new EscapeState());
                break;

            case '[':
                context.ToState(new BeginSetState());
                return(TokenBuilder.BuildBracketLeftToken());

            case '*':
                return(TokenBuilder.BuildZeroOrMoreToken());

            case '?':
                return(TokenBuilder.BuildZeroOrOneToken());

            case '+':
                return(TokenBuilder.BuildOneOrMoreToken());

            case '|':
                return(TokenBuilder.BuildAlternationToken());

            case '.':
                return(TokenBuilder.BuildAnyToken());

            default:
                return(TokenBuilder.BuildLiteralToken(context.Current));
            }
            return(null);
        }
Ejemplo n.º 7
0
        public IToken Handle(IContext context)
        {
            switch (context.Current)
            {
            case ']':
                context.EndState();
                context.EndState();
                return(TokenBuilder.BuildBracketRightToken());

            case '-':
                return(TokenBuilder.BuildRangeToken());

            case '\\':
                context.ToState(new EscapeState());
                break;

            default:
                return(TokenBuilder.BuildLiteralToken(context.Current));
            }
            return(null);
        }
Ejemplo n.º 8
0
        public IToken Handle(IContext context)
        {
            var character = context.Current;

            switch (character)
            {
            case ',':
                if (_isParsingMinOccurs)
                {
                    if (_isParsingFirstValue)
                    {
                        //missing minOccurs
                        throw new ArgumentException();
                    }
                    //end
                    _minOccurs           = _currentNumber;
                    _currentNumber       = 0;
                    _isParsingMinOccurs  = false;
                    _isParsingFirstValue = true;
                }
                else
                {
                    //too many ,
                    throw new ArgumentException();
                }
                break;

            case '}':
                if (_isParsingMinOccurs)
                {
                    if (_isParsingFirstValue)
                    {
                        //missing minOccurs
                        throw new ArgumentException();
                    }
                    //minOccurs equals maxOccurs
                    _minOccurs = _currentNumber;
                    _maxOccurs = _currentNumber;
                }
                else
                {
                    if (_isParsingFirstValue)
                    {
                        //maxOccus = unlimited
                        _maxOccurs = -1;
                    }
                    else
                    {
                        //end
                        _maxOccurs = _currentNumber;
                    }
                }
                context.EndState();
                return(TokenBuilder.BuildRepetitionToken(_minOccurs, _maxOccurs));

            default:
                var result = 0;
                if (int.TryParse(character.ToString(), out result))
                {
                    _currentNumber       = _currentNumber * 10 + result;
                    _isParsingFirstValue = false;
                }
                else
                {
                    throw   new ArgumentException();
                }
                break;
            }
            return(null);
        }
Ejemplo n.º 9
0
 public Lexer(TokenBuilder tokenBuilder)
 {
     TokenBuilder = tokenBuilder;
 }