Beispiel #1
0
        // Parse a number - either double, hex integer, decimal interer or octal integer
        // Don't need to handle negatives as these are handled by Token.subtract unary operator
        public Token ParseNumber()
        {
            // Base 10 by default
            int b=10;
            int startPos = p.position;

            if (p.current == '0')
            {
                if (p.CharAtOffset(1) == 'x' || p.CharAtOffset(1) == 'X')
                {
                    p.SkipForward(2);
                    b = 16;
                    startPos = p.position;
                    while (HexDigit(p.current) >= 0)
                        p.SkipForward(1);
                }
                else
                {
                    while (p.current == '0')
                        p.SkipForward(1);

                    if (p.current != '.' && p.current != 'e' && p.current != 'E')
                    {
                        b = 8;
                        while (OctalDigit(p.current) >= 0)
                            p.SkipForward(1);
                    }
                }
            }

            if (b==10)
            {
                // Leading digits
                while (DecimalDigit(p.current) >= 0)
                    p.SkipForward(1);

                // Decimal point
                if (p.current == '.' && DecimalDigit(p.CharAtOffset(1)) >= 0)
                {
                    b = 0;
                    p.SkipForward(2);
                    while (DecimalDigit(p.current) >= 0)
                        p.SkipForward(1);
                }

                // Exponent
                if (p.current == 'E' || p.current == 'e')
                {
                    b = 0;
                    p.SkipForward(1);
                    if (p.current == '+' || p.current == '-')
                        p.SkipForward(1);

                    while (DecimalDigit(p.current) >= 0)
                    {
                        p.SkipForward(1);
                    }
                }
            }

            string str = p.Substring(startPos, p.position - startPos);
            if (b == 0)
            {
                double temp;
                if (!double.TryParse(str, out temp))
                {
                    p.position = m_tokenStart;
                    throw new CompileError(string.Format("Syntax error - incorrectly formatted decimal literal `{0}`", str), this);
                }

                // Need to store both the parsed value and the original value.
                // Original value is used to re-write on render without losing/changing
                // the value
                DoubleLiteral l = new DoubleLiteral();
                l.Value = temp;
                l.Original = str;

                m_literal = l;
                return Token.literal;
            }
            else
            {
                try
                {
                    m_literal = Convert.ToInt64(str, b);
                    return Token.literal;
                }
                catch (Exception)
                {
                    p.position = m_tokenStart;
                    throw new CompileError(string.Format("Syntax error - incorrectly formatted integer literal `{0}`", str), this);
                }
            }
        }
Beispiel #2
0
        // Parse a number - either double, hex integer, decimal interer or octal integer
        // Don't need to handle negatives as these are handled by Token.subtract unary operator
        public Token ParseNumber()
        {
            // Base 10 by default
            int b        = 10;
            int startPos = p.position;

            if (p.current == '0')
            {
                if (p.CharAtOffset(1) == 'x' || p.CharAtOffset(1) == 'X')
                {
                    p.SkipForward(2);
                    b        = 16;
                    startPos = p.position;
                    while (HexDigit(p.current) >= 0)
                    {
                        p.SkipForward(1);
                    }
                }
                else
                {
                    while (p.current == '0')
                    {
                        p.SkipForward(1);
                    }

                    if (p.current != '.' && p.current != 'e' && p.current != 'E')
                    {
                        b = 8;
                        while (OctalDigit(p.current) >= 0)
                        {
                            p.SkipForward(1);
                        }
                    }
                }
            }

            if (b == 10)
            {
                // Leading digits
                while (DecimalDigit(p.current) >= 0)
                {
                    p.SkipForward(1);
                }

                // Decimal point
                if (p.current == '.' && DecimalDigit(p.CharAtOffset(1)) >= 0)
                {
                    b = 0;
                    p.SkipForward(2);
                    while (DecimalDigit(p.current) >= 0)
                    {
                        p.SkipForward(1);
                    }
                }

                // Exponent
                if (p.current == 'E' || p.current == 'e')
                {
                    b = 0;
                    p.SkipForward(1);
                    if (p.current == '+' || p.current == '-')
                    {
                        p.SkipForward(1);
                    }

                    while (DecimalDigit(p.current) >= 0)
                    {
                        p.SkipForward(1);
                    }
                }
            }


            string str = p.Substring(startPos, p.position - startPos);

            if (b == 0)
            {
                double temp;
                if (!double.TryParse(str, out temp))
                {
                    p.position = m_tokenStart;
                    throw new CompileError(string.Format("Syntax error - incorrectly formatted decimal literal `{0}`", str), this);
                }

                // Need to store both the parsed value and the original value.
                // Original value is used to re-write on render without losing/changing
                // the value
                DoubleLiteral l = new DoubleLiteral();
                l.Value    = temp;
                l.Original = str;

                m_literal = l;
                return(Token.literal);
            }
            else
            {
                try
                {
                    m_literal = Convert.ToInt64(str, b);
                    return(Token.literal);
                }
                catch (Exception)
                {
                    p.position = m_tokenStart;
                    throw new CompileError(string.Format("Syntax error - incorrectly formatted integer literal `{0}`", str), this);
                }
            }
        }