Exemplo n.º 1
0
 public static Exception UnexpectedToken(
     string expectedToken,
     string currentToken,
     int pos)
 {
     return(LexicalException.CreateException("Expected {0}, but actual token at the position {2} is {1}.", (object)expectedToken, (object)currentToken, (object)pos));
 }
Exemplo n.º 2
0
        void ReadString(char escape)
        {
            while (!this.reader.End)   //while (this.pos < this.buffer.Length)
            {
                char ch = this.reader.Read();
                if (ch == escape)
                {
                    if (!this.reader.End && escape == this.reader.Current)     //if (this.pos < this.buffer.Length && escape == this.reader.Current)
                    {
                        this.reader.Read();
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (this.reader.End)     //if (this.pos >= this.buffer.Length)
            {
                throw LexicalException.InvalidString(this.TokenString);
            }

            this.token = Token.StringConst;
        }
Exemplo n.º 3
0
 private void CheckToken(Token token)
 {
     if (token != this.token)
     {
         throw LexicalException.UnexpectedToken(Utils.TokenToString(this.token), Utils.TokenToString(token), this.reader.Position);
     }
 }
Exemplo n.º 4
0
 private void ReadDate()
 {
     do
     {
         int num1 = (int)this.reader.Read();
     }while (!this.reader.End && '#' != this.reader.Current);
     if (this.reader.End || '#' != this.reader.Current)
     {
         throw LexicalException.InvalidDate(this.TokenString);
     }
     this.token = Token.Date;
     int num2 = (int)this.reader.Read();
 }
Exemplo n.º 5
0
        private void ReadHex()
        {
            while (char.IsLetterOrDigit(this.reader.Current))
            {
                int num = (int)this.reader.Read();
            }
            string text = this.reader.GetString(this.startPos + 2, this.reader.Position);

            foreach (char ch in text)
            {
                if (!Utils.IsHexDigit(ch))
                {
                    throw LexicalException.InvalidHex(text);
                }
            }
        }
Exemplo n.º 6
0
        void ReadDate()
        {
            do
            {
                this.reader.Read();
            }while (!this.reader.End && '#' != this.reader.Current); //while (this.pos < this.buffer.Length && '#' != this.reader.Current);

            if (this.reader.End || '#' != this.reader.Current)       //if (this.pos >= this.buffer.Length || '#' != this.reader.Current)
            {
                // Date is invalid
                throw LexicalException.InvalidDate(this.TokenString);
            }

            this.token = Token.Date;
            this.reader.Read();
        }
Exemplo n.º 7
0
 private void ReadName(char endChar, char escape, string charsToEscape)
 {
     do
     {
         if ((int)escape == (int)this.reader.Current && this.reader.CanRead && charsToEscape.IndexOf(this.reader.Peek()) != -1)
         {
             int num1 = (int)this.reader.Read();
         }
         int num2 = (int)this.reader.Read();
     }while (!this.reader.End && (int)this.reader.Current != (int)endChar);
     if (this.reader.End)
     {
         throw LexicalException.InvalidName(this.TokenString);
     }
     this.token = Token.Name;
     int num = (int)this.reader.Read();
 }
Exemplo n.º 8
0
        void ReadHex()
        {
            while (char.IsLetterOrDigit(this.reader.Current))
            {
                this.reader.Read();
            }

            string s = this.reader.GetString(this.startPos + 2, this.reader.Position); // skip leading 0x

            foreach (char ch in s)
            {
                if (!Utils.IsHexDigit(ch))
                {
                    throw LexicalException.InvalidHex(s);
                }
            }
        }
Exemplo n.º 9
0
        void ReadName(char endChar, char escape, string charsToEscape)
        {
            do
            {
                if (escape == this.reader.Current &&
                    this.reader.CanRead && //(this.pos + 1 < this.buffer.Length)
                    (charsToEscape.IndexOf(this.reader.Peek()) != -1))
                {
                    this.reader.Read();
                }
                this.reader.Read();
            }while (!this.reader.End && this.reader.Current != endChar); //while (this.pos < this.buffer.Length || this.reader.Current != endChar);

            if (this.reader.End)                                         //if (this.pos >= this.buffer.Length)
            {
                throw LexicalException.InvalidName(this.TokenString);
            }

            this.token = Token.Name;
            this.reader.Read();
        }
Exemplo n.º 10
0
 private void ReadString(char escape)
 {
     while (!this.reader.End)
     {
         if ((int)this.reader.Read() == (int)escape)
         {
             if (!this.reader.End && (int)escape == (int)this.reader.Current)
             {
                 int num = (int)this.reader.Read();
             }
             else
             {
                 break;
             }
         }
     }
     if (this.reader.End)
     {
         throw LexicalException.InvalidString(this.TokenString);
     }
     this.token = Token.StringConst;
 }
Exemplo n.º 11
0
 public static Exception InvalidHex(string text)
 {
     return(LexicalException.CreateException("The expression contains invalid hexadecimal number: '{0}'.", (object)text));
 }
Exemplo n.º 12
0
 public static Exception InvalidName(string name)
 {
     return(LexicalException.CreateException("The expression contains invalid name: '{0}'.", (object)name));
 }
Exemplo n.º 13
0
 public static Exception InvalidString(string str)
 {
     return(LexicalException.CreateException("The expression contains an invalid string constant: {0}.", (object)str));
 }
Exemplo n.º 14
0
 public static Exception UnknownToken(string token, int position)
 {
     return(LexicalException.CreateException("Cannot interpret token '{0}' at position {1}.", (object)token, (object)position));
 }
Exemplo n.º 15
0
        public Token Read()
        {
            bool toStop = true;

            this.op    = null;
            this.token = Token.None;

            do
            {
                toStop        = true;
                this.startPos = this.reader.Position;

                char ch = this.reader.Read();
                switch (ch)
                {
                case '\0':
                    this.token = Token.EOF;
                    break;

                case ' ':
                case '\t':
                case '\n':
                case '\r':
                    this.ReadWhiteSpaces();
                    toStop = false;
                    break;

                case '(':
                    this.token = Token.LeftParen;
                    break;

                case ')':
                    this.token = Token.RightParen;
                    break;

                case '+':
                    this.op    = Operator.Plus;
                    this.token = Token.BinaryOp;
                    break;

                case '-':
                    this.op    = Operator.Minus;
                    this.token = Token.BinaryOp;
                    break;

                case '*':
                    this.op    = Operator.Multiply;
                    this.token = Token.BinaryOp;
                    break;

                case '/':
                    this.op    = Operator.Divide;
                    this.token = Token.BinaryOp;
                    break;

                case '%':
                    this.op    = Operator.Modulo;
                    this.token = Token.BinaryOp;
                    break;

                case '&':
                    this.op    = Operator.BitwiseAnd;
                    this.token = Token.BinaryOp;
                    break;

                case '|':
                    this.op    = Operator.BitwiseOr;
                    this.token = Token.BinaryOp;
                    break;

                case '^':
                    this.op    = Operator.BitwiseXor;
                    this.token = Token.BinaryOp;
                    break;

                case '~':
                    this.op    = Operator.BitwiseNot;
                    this.token = Token.BinaryOp;
                    break;

                case '=':
                    this.op    = Operator.EqualTo;
                    this.token = Token.BinaryOp;
                    break;

                case '<':
                    this.token = Token.BinaryOp;
                    this.ReadWhiteSpaces();
                    if ('=' == this.reader.Current)
                    {
                        this.op = Operator.LessOrEqual;
                        this.reader.Read();
                    }
                    else if ('>' == this.reader.Current)
                    {
                        this.op = Operator.NotEqual;
                        this.reader.Read();
                    }
                    else
                    {
                        this.op = Operator.LessThen;
                    }
                    break;

                case '>':
                    this.token = Token.BinaryOp;
                    this.ReadWhiteSpaces();
                    if ('=' == this.reader.Current)
                    {
                        this.op = Operator.GreaterOrEqual;
                        this.reader.Read();
                    }
                    else
                    {
                        this.op = Operator.GreaterThen;
                    }
                    break;

                case '#':   // date
                    this.ReadDate();
                    this.CheckToken(Token.Date);
                    break;

                case '\'':  // string const
                    this.ReadString('\'');
                    this.CheckToken(Token.StringConst);
                    break;

                case '"':  // string const
                    this.ReadString('"');
                    this.CheckToken(Token.StringConst);
                    break;

                case '`':
                    this.ReadName('`', '`', "`");
                    this.CheckToken(Token.Name);
                    break;

                case '[':
                    this.ReadName(']', Lexer.Escape, @"]\");
                    this.CheckToken(Token.Name);
                    break;

                case '.':
                    if (OperandType.None == this.client.PrevOperand)
                    {
                        this.ReadNumber();
                    }
                    else
                    {
                        this.token = Token.Dot;
                    }
                    break;

                case ',':
                    this.token = Token.ListSeparator;
                    break;


                case '@':
                    this.token = Token.Parameter;
                    break;

                //case '!':
                //        break;

                default:
                    if ('0' == ch && ('x' == this.reader.Current || 'X' == this.reader.Current))
                    {
                        this.reader.Read();

                        this.ReadHex();
                        this.token = Token.NumericHex;
                    }
                    else if (char.IsDigit(ch))
                    {
                        this.ReadNumber();
                    }
                    else
                    {
                        this.ReadReserved();
                        if (Token.None == this.token)
                        {
                            if (char.IsLetter(ch) || '_' == ch)
                            {
                                this.ReadName();
                                if (Token.None != this.token)
                                {
                                    this.CheckToken(Token.Name);
                                    break;
                                }
                            }
                            this.token = Token.Unknown;
                            throw LexicalException.UnknownToken(this.TokenString, this.startPos + 1);
                        }
                    }
                    break;
                }
            } while (!toStop);

            return(this.token);
        }
Exemplo n.º 16
0
 public static Exception InvalidDate(string date)
 {
     return(LexicalException.CreateException("The expression contains invalid date constant '{0}'.", (object)date));
 }
Exemplo n.º 17
0
        public Token Read()
        {
            this.op    = (Operator)null;
            this.token = Token.None;
            bool flag;

            do
            {
                flag          = true;
                this.startPos = this.reader.Position;
                char c = this.reader.Read();
                switch (c)
                {
                case char.MinValue:
                    this.token = Token.EOF;
                    break;

                case '\t':
                case '\n':
                case '\r':
                case ' ':
                    this.ReadWhiteSpaces();
                    flag = false;
                    break;

                case '"':
                    this.ReadString('"');
                    this.CheckToken(Token.StringConst);
                    break;

                case '#':
                    this.ReadDate();
                    this.CheckToken(Token.Date);
                    break;

                case '%':
                    this.op    = Operator.Modulo;
                    this.token = Token.BinaryOp;
                    break;

                case '&':
                    this.op    = Operator.BitwiseAnd;
                    this.token = Token.BinaryOp;
                    break;

                case '\'':
                    this.ReadString('\'');
                    this.CheckToken(Token.StringConst);
                    break;

                case '(':
                    this.token = Token.LeftParen;
                    break;

                case ')':
                    this.token = Token.RightParen;
                    break;

                case '*':
                    this.op    = Operator.Multiply;
                    this.token = Token.BinaryOp;
                    break;

                case '+':
                    this.op    = Operator.Plus;
                    this.token = Token.BinaryOp;
                    break;

                case ',':
                    this.token = Token.ListSeparator;
                    break;

                case '-':
                    this.op    = Operator.Minus;
                    this.token = Token.BinaryOp;
                    break;

                case '.':
                    if (this.client.PrevOperand == OperandType.None)
                    {
                        this.ReadNumber();
                        break;
                    }
                    this.token = Token.Dot;
                    break;

                case '/':
                    this.op    = Operator.Divide;
                    this.token = Token.BinaryOp;
                    break;

                case '<':
                    this.token = Token.BinaryOp;
                    this.ReadWhiteSpaces();
                    if ('=' == this.reader.Current)
                    {
                        this.op = Operator.LessOrEqual;
                        int num = (int)this.reader.Read();
                        break;
                    }
                    if ('>' == this.reader.Current)
                    {
                        this.op = Operator.NotEqual;
                        int num = (int)this.reader.Read();
                        break;
                    }
                    this.op = Operator.LessThen;
                    break;

                case '=':
                    this.op    = Operator.EqualTo;
                    this.token = Token.BinaryOp;
                    break;

                case '>':
                    this.token = Token.BinaryOp;
                    this.ReadWhiteSpaces();
                    if ('=' == this.reader.Current)
                    {
                        this.op = Operator.GreaterOrEqual;
                        int num = (int)this.reader.Read();
                        break;
                    }
                    this.op = Operator.GreaterThen;
                    break;

                case '@':
                    this.token = Token.Parameter;
                    break;

                case '[':
                    this.ReadName(']', '\\', "]\\");
                    this.CheckToken(Token.Name);
                    break;

                case '^':
                    this.op    = Operator.BitwiseXor;
                    this.token = Token.BinaryOp;
                    break;

                case '`':
                    this.ReadName('`', '`', "`");
                    this.CheckToken(Token.Name);
                    break;

                case '|':
                    this.op    = Operator.BitwiseOr;
                    this.token = Token.BinaryOp;
                    break;

                case '~':
                    this.op    = Operator.BitwiseNot;
                    this.token = Token.BinaryOp;
                    break;

                default:
                    if ('0' == c && ('x' == this.reader.Current || 'X' == this.reader.Current))
                    {
                        int num = (int)this.reader.Read();
                        this.ReadHex();
                        this.token = Token.NumericHex;
                        break;
                    }
                    if (char.IsDigit(c))
                    {
                        this.ReadNumber();
                        break;
                    }
                    this.ReadReserved();
                    if (this.token == Token.None)
                    {
                        if (char.IsLetter(c) || '_' == c)
                        {
                            this.ReadName();
                            if (this.token != Token.None)
                            {
                                this.CheckToken(Token.Name);
                                break;
                            }
                        }
                        this.token = Token.Unknown;
                        throw LexicalException.UnknownToken(this.TokenString, this.startPos + 1);
                    }
                    break;
                }
            }while (!flag);
            return(this.token);
        }