Beispiel #1
0
        public RqlConstantExpression(RqlToken token)
            : base(RqlExpressionType.Constant)
        {
            if (!token.IsConstant)
                throw new RqlParseException(token);

            this.Token = token;
        }
Beispiel #2
0
        public RqlIdentifierExpression(RqlToken token) : base(RqlExpressionType.Identifier)
        {
            if (!token.IsIdentifier)
            {
                throw new RqlParseException(token);
            }

            this.Token = token;
        }
Beispiel #3
0
        public RqlConstantExpression(RqlToken token) : base(RqlExpressionType.Constant)
        {
            if (!token.IsConstant)
            {
                throw new RqlParseException(token);
            }

            this.Token = token;
        }
Beispiel #4
0
        public RqlTupleExpression(RqlToken token, IList <RqlConstantExpression> constants) : base(RqlExpressionType.Tuple)
        {
            if (!token.IsLeftParen)
            {
                throw new RqlParseException(token);
            }

            this.Token     = token;
            this.Constants = new ReadOnlyCollection <RqlConstantExpression>(constants);
        }
Beispiel #5
0
        public RqlFunctionCallExpression(RqlToken token, IList <RqlExpression> arguments)
            : base(RqlExpressionType.FunctionCall)
        {
            if (!token.IsIdentifier)
            {
                throw new RqlParseException(token);
            }

            this.Token     = token;
            this.Arguments = new ReadOnlyCollection <RqlExpression>(arguments);
        }
Beispiel #6
0
        public RqlExpression Parse(string input)
        {
            tokenizer = new RqlTokenizer(input);

            RqlToken token = tokenizer.Next();

            if (!token.IsIdentifier)
            {
                throw new RqlParseException(token);
            }

            return(ParseExpression(token));
        }
Beispiel #7
0
        public RqlToken PeekNext()
        {
            if (stopToken != null)
            {
                return(stopToken);
            }

            if (nextToken == null)
            {
                nextToken = Next();
            }

            return(nextToken);
        }
Beispiel #8
0
        private RqlExpression ParseExpression(RqlToken functionToken)
        {
            RqlToken token = tokenizer.Next();

            if (!token.IsLeftParen)
                throw new RqlParseException(token);

            var arguments = new List<RqlExpression>();

            while (true)
            {
                token = tokenizer.Next();

                RqlExpression argument = null;

                if (token.IsIdentifier)
                {
                    if (tokenizer.PeekNext().IsLeftParen)
                        argument = ParseExpression(token);
                    else
                        argument = RqlExpression.Identifier(token);
                }
                else if (token.IsConstant)
                {
                    argument = RqlExpression.Constant(token);
                }
                else if (token.IsLeftParen)
                {
                    argument = ParseTuple(token);
                }
                else
                    throw new RqlParseException(token);

                arguments.Add(argument);

                token = tokenizer.Next();

                if (token.IsComma)
                    continue;
                else if (token.IsRightParen)
                    break;
                else
                    throw new RqlParseException(token);
            }

            return RqlExpression.FunctionCall(functionToken, arguments);
        }
Beispiel #9
0
        private RqlTupleExpression ParseTuple(RqlToken leftParenToken)
        {
            var  constants = new List <RqlConstantExpression>();
            Type itemType  = null;

            while (true)
            {
                RqlToken token = tokenizer.Next();

                if (token.IsConstant)
                {
                    if (itemType == null)
                    {
                        itemType = token.Data.GetType();
                    }
                    else if (token.Data.GetType() != itemType)
                    {
                        throw new RqlParseException(token, "Tuple items must all be of the same type");
                    }

                    constants.Add(RqlExpression.Constant(token));
                }
                else
                {
                    throw new RqlParseException(token);
                }

                token = tokenizer.Next();

                if (token.IsComma)
                {
                    continue;
                }
                else if (token.IsRightParen)
                {
                    break;
                }
                else
                {
                    throw new RqlParseException(token);
                }
            }

            return(RqlExpression.Tuple(leftParenToken, constants));
        }
Beispiel #10
0
        public RqlToken PeekNext()
        {
            if (stopToken != null)
                return stopToken;

            if (nextToken == null)
            {
                nextToken = Next();
            }

            return nextToken;
        }
Beispiel #11
0
        private RqlExpression ParseExpression(RqlToken functionToken)
        {
            RqlToken token = tokenizer.Next();

            if (!token.IsLeftParen)
            {
                throw new RqlParseException(token);
            }

            var arguments = new List <RqlExpression>();

            while (true)
            {
                token = tokenizer.Next();

                RqlExpression argument = null;

                if (token.IsIdentifier)
                {
                    if (tokenizer.PeekNext().IsLeftParen)
                    {
                        argument = ParseExpression(token);
                    }
                    else
                    {
                        argument = RqlExpression.Identifier(token);
                    }
                }
                else if (token.IsConstant)
                {
                    argument = RqlExpression.Constant(token);
                }
                else if (token.IsLeftParen)
                {
                    argument = ParseTuple(token);
                }
                else
                {
                    throw new RqlParseException(token);
                }

                arguments.Add(argument);

                token = tokenizer.Next();

                if (token.IsComma)
                {
                    continue;
                }
                else if (token.IsRightParen)
                {
                    break;
                }
                else
                {
                    throw new RqlParseException(token);
                }
            }

            return(RqlExpression.FunctionCall(functionToken, arguments));
        }
Beispiel #12
0
        public RqlToken Next()
        {
            if (stopToken != null)
                return stopToken;

            if (nextToken != null)
            {
                RqlToken token = nextToken;

                nextToken = null;
                return token;
            }

            char c = ReadChar();

            while (c != '\0' && Char.IsWhiteSpace(c))
                c = ReadChar();

            int tokenOffset = this.offset - 1;

            if (c == '\0')
                return (stopToken = RqlToken.End(offset));

            if (Char.IsLetter(c))
            {
                sb.Clear();
                sb.Append(c);

                c = ReadChar();

                bool lastCharWasDot = false;

                while (c != 0 && (Char.IsLetterOrDigit(c)) || c == '.')
                {
                    sb.Append(c);

                    if (c == '.')
                    {
                        if (lastCharWasDot)
                            goto Error;

                        lastCharWasDot = true;
                    }
                    else
                        lastCharWasDot = false;

                    c = ReadChar();
                }

                UnreadChar();

                string s = sb.ToString();

                if (s == "true")
                    return RqlToken.Constant(tokenOffset, (object)true);
                else if (s == "false")
                    return RqlToken.Constant(tokenOffset, (object)false);
                else if (s == "null")
                    return RqlToken.Constant(tokenOffset, null);
                else
                    return RqlToken.Identifier(tokenOffset, s);
            }
            else if (Char.IsDigit(c) || c == '-')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                if (sb[0] == '-' && !Char.IsDigit(c))
                    goto Error;

                while (Char.IsDigit(c))
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                if (c == '.')
                {
                    sb.Append(c);
                    c = ReadChar();

                    while (Char.IsDigit(c) )
                    {
                        sb.Append(c);
                        c = ReadChar();
                    }

                    UnreadChar();

                    Double n;

                    if (Double.TryParse(sb.ToString(), out n))
                        return RqlToken.Constant(tokenOffset, n);
                }
                else
                {
                    UnreadChar();

                    Int32 n;

                    if (Int32.TryParse(sb.ToString(), out n))
                        return RqlToken.Constant(tokenOffset, n);
                }
            }
            else if (c == '$')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (Char.IsLetterOrDigit(c))
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                UnreadChar();

                if (sb.Length > 0)
                    return RqlToken.Constant(tokenOffset, new RqlId(sb.ToString()));
            }
            else if (c == '@')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (c != 'Z')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                sb.Append(c);

                RqlDateTime dateTime;

                if (RqlDateTime.TryParse(sb.ToString(), out dateTime))
                    return RqlToken.Constant(tokenOffset, dateTime);
            }
            else if (c == '~')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (c != 'S')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                sb.Append(c);

                RqlTimeSpan timeSpan;

                if (RqlTimeSpan.TryParse(sb.ToString(), out timeSpan))
                    return RqlToken.Constant(tokenOffset, timeSpan);
            }
            else if (c == '\'')
            {
                sb.Clear();
                c = ReadChar();

                while (c != '\0' && c != '\'')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                if (c == '\'')
                    return RqlToken.Constant(tokenOffset, sb.ToString());
            }
            else
            {
                switch ((char)c)
                {
                    case ',':
                        return RqlToken.Comma(tokenOffset);
                    case '(':
                        return RqlToken.LeftParen(tokenOffset);
                    case ')':
                        return RqlToken.RightParen(tokenOffset);
                }
            }

            Error:
            return (stopToken = RqlToken.Error(tokenOffset));
        }
Beispiel #13
0
 public RqlTupleExpression(RqlToken token) : this(token, new RqlConstantExpression[0])
 {
 }
Beispiel #14
0
        public RqlFunctionCallExpression(RqlToken token, IList<RqlExpression> arguments)
            : base(RqlExpressionType.FunctionCall)
        {
            if (!token.IsIdentifier)
                throw new RqlParseException(token);

            this.Token = token;
            this.Arguments = new ReadOnlyCollection<RqlExpression>(arguments);
        }
Beispiel #15
0
        private RqlTupleExpression ParseTuple(RqlToken leftParenToken)
        {
            var constants = new List<RqlConstantExpression>();
            Type itemType = null;

            while (true)
            {
                RqlToken token = tokenizer.Next();

                if (token.IsConstant)
                {
                    if (itemType == null)
                        itemType = token.Data.GetType();
                    else if (token.Data.GetType() != itemType)
                        throw new RqlParseException(token, "Tuple items must all be of the same type");

                    constants.Add(RqlExpression.Constant(token));
                }
                else
                    throw new RqlParseException(token);

                token = tokenizer.Next();

                if (token.IsComma)
                    continue;
                else if (token.IsRightParen)
                    break;
                else
                    throw new RqlParseException(token);
            }

            return RqlExpression.Tuple(leftParenToken, constants);
        }
Beispiel #16
0
 public static RqlConstantExpression Constant(RqlToken token)
 {
     return new RqlConstantExpression(token);
 }
Beispiel #17
0
 public static RqlConstantExpression Constant(RqlToken token)
 {
     return(new RqlConstantExpression(token));
 }
Beispiel #18
0
 public static RqlTupleExpression Tuple(RqlToken token, IList <RqlConstantExpression> constants)
 {
     return(new RqlTupleExpression(token, constants));
 }
Beispiel #19
0
 public RqlTupleExpression(RqlToken token)
     : this(token, new RqlConstantExpression[0])
 {
 }
Beispiel #20
0
 public RqlParseException(RqlToken token)
 {
     this.ErrorOffset = token.Offset;
 }
Beispiel #21
0
 public static RqlTupleExpression Tuple(RqlToken token, IList<RqlConstantExpression> constants)
 {
     return new RqlTupleExpression(token, constants);
 }
Beispiel #22
0
 public static RqlIdentifierExpression Identifier(RqlToken token)
 {
     return new RqlIdentifierExpression(token);
 }
Beispiel #23
0
 public static RqlFunctionCallExpression FunctionCall(RqlToken token, IList<RqlExpression> arguments)
 {
     return new RqlFunctionCallExpression(token, arguments);
 }
Beispiel #24
0
        public RqlToken Next()
        {
            if (stopToken != null)
            {
                return(stopToken);
            }

            if (nextToken != null)
            {
                RqlToken token = nextToken;

                nextToken = null;
                return(token);
            }

            char c = ReadChar();

            while (c != '\0' && Char.IsWhiteSpace(c))
            {
                c = ReadChar();
            }

            int tokenOffset = this.offset - 1;

            if (c == '\0')
            {
                return(stopToken = RqlToken.End(offset));
            }

            if (Char.IsLetter(c))
            {
                sb.Clear();
                sb.Append(c);

                c = ReadChar();

                bool lastCharWasDot = false;

                while (c != 0 && (Char.IsLetterOrDigit(c)) || c == '.')
                {
                    sb.Append(c);

                    if (c == '.')
                    {
                        if (lastCharWasDot)
                        {
                            goto Error;
                        }

                        lastCharWasDot = true;
                    }
                    else
                    {
                        lastCharWasDot = false;
                    }

                    c = ReadChar();
                }

                UnreadChar();

                string s = sb.ToString();

                if (s == "true")
                {
                    return(RqlToken.Constant(tokenOffset, (object)true));
                }
                else if (s == "false")
                {
                    return(RqlToken.Constant(tokenOffset, (object)false));
                }
                else if (s == "null")
                {
                    return(RqlToken.Constant(tokenOffset, null));
                }
                else
                {
                    return(RqlToken.Identifier(tokenOffset, s));
                }
            }
            else if (Char.IsDigit(c) || c == '-')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                if (sb[0] == '-' && !Char.IsDigit(c))
                {
                    goto Error;
                }

                while (Char.IsDigit(c))
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                if (c == '.')
                {
                    sb.Append(c);
                    c = ReadChar();

                    while (Char.IsDigit(c))
                    {
                        sb.Append(c);
                        c = ReadChar();
                    }

                    UnreadChar();

                    Double n;

                    if (Double.TryParse(sb.ToString(), out n))
                    {
                        return(RqlToken.Constant(tokenOffset, n));
                    }
                }
                else
                {
                    UnreadChar();

                    Int32 n;

                    if (Int32.TryParse(sb.ToString(), out n))
                    {
                        return(RqlToken.Constant(tokenOffset, n));
                    }
                }
            }
            else if (c == '$')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (Char.IsLetterOrDigit(c))
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                UnreadChar();

                if (sb.Length > 0)
                {
                    return(RqlToken.Constant(tokenOffset, new RqlId(sb.ToString())));
                }
            }
            else if (c == '@')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (c != 'Z')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                sb.Append(c);

                RqlDateTime dateTime;

                if (RqlDateTime.TryParse(sb.ToString(), out dateTime))
                {
                    return(RqlToken.Constant(tokenOffset, dateTime));
                }
            }
            else if (c == '~')
            {
                sb.Clear();
                sb.Append(c);
                c = ReadChar();

                while (c != 'S')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                sb.Append(c);

                RqlTimeSpan timeSpan;

                if (RqlTimeSpan.TryParse(sb.ToString(), out timeSpan))
                {
                    return(RqlToken.Constant(tokenOffset, timeSpan));
                }
            }
            else if (c == '\'')
            {
                sb.Clear();
                c = ReadChar();

                while (c != '\0' && c != '\'')
                {
                    sb.Append(c);
                    c = ReadChar();
                }

                if (c == '\'')
                {
                    return(RqlToken.Constant(tokenOffset, sb.ToString()));
                }
            }
            else
            {
                switch ((char)c)
                {
                case ',':
                    return(RqlToken.Comma(tokenOffset));

                case '(':
                    return(RqlToken.LeftParen(tokenOffset));

                case ')':
                    return(RqlToken.RightParen(tokenOffset));
                }
            }

Error:
            return(stopToken = RqlToken.Error(tokenOffset));
        }
Beispiel #25
0
 public static RqlFunctionCallExpression FunctionCall(RqlToken token, IList <RqlExpression> arguments)
 {
     return(new RqlFunctionCallExpression(token, arguments));
 }
Beispiel #26
0
 public RqlParseException(RqlToken token, string message) : base(message)
 {
     this.ErrorOffset = token.Offset;
 }
Beispiel #27
0
        public RqlIdentifierExpression(RqlToken token)
            : base(RqlExpressionType.Identifier)
        {
            if (!token.IsIdentifier)
                throw new RqlParseException(token);

            this.Token = token;
        }
Beispiel #28
0
 public RqlParseException(RqlToken token, string message)
     : base(message)
 {
     this.ErrorOffset = token.Offset;
 }
Beispiel #29
0
        public RqlTupleExpression(RqlToken token, IList<RqlConstantExpression> constants)
            : base(RqlExpressionType.Tuple)
        {
            if (!token.IsLeftParen)
                throw new RqlParseException(token);

            this.Token = token;
            this.Constants = new ReadOnlyCollection<RqlConstantExpression>(constants);
        }
Beispiel #30
0
 public RqlParseException(RqlToken token)
 {
     this.ErrorOffset = token.Offset;
 }
Beispiel #31
0
 public static RqlIdentifierExpression Identifier(RqlToken token)
 {
     return(new RqlIdentifierExpression(token));
 }