Exemple #1
0
        public IExpression Parse(string expression)
        {
            var tokenizer = new ExpressionTokenizer();

            tokens = tokenizer.Tokenize(expression).GetEnumerator();
            NextToken();

            var parsedPath = Expression();

            if (lookAhead != ExpressionToken.Epsilon)
            {
                throw UnexpectedTokenException();
            }

            return(parsedPath);
        }
Exemple #2
0
        private IExpression MultiplicativeExpression()
        {
            var expression = UnaryExpression();

            // A regex pattern is unexpected (and not allowed) at this point,
            // so instead we assume to be in the middle of a double division
            // arithmetic operation, e.g. "60/3/5" where "/3/" is interpreted as a regex pattern.
            // Hence the regex pattern must be split into smaller tokens.
            if (lookAhead.Type == ExpressionTokenType.RegexPattern)
            {
                var regexTokenizer = new ExpressionTokenizer();
                foreach (var token in regexTokenizer.Tokenize(lookAhead.Sequence, ExpressionTokenizationOptions.ExcludeRegex))
                {
                    priorityTokenQueue.Enqueue(token);
                }
                NextToken();
            }

            while (lookAhead.Type == ExpressionTokenType.Multiplication ||
                   lookAhead.Type == ExpressionTokenType.Division ||
                   lookAhead.Type == ExpressionTokenType.Remainder)
            {
                var type = lookAhead.Type;
                NextToken();
                var rightOperand = UnaryExpression();

                switch (type)
                {
                case ExpressionTokenType.Multiplication:
                    expression = new Multiplication(expression, rightOperand);
                    break;

                case ExpressionTokenType.Division:
                    expression = new Division(expression, rightOperand);
                    break;

                case ExpressionTokenType.Remainder:
                    expression = new Remainder(expression, rightOperand);
                    break;
                }
            }

            return(expression);
        }