Example #1
0
        public static string ReadHexProgressive(string s, ref double d, out int digits)
        {
            digits = 0;

            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];

                if (LexerUtils.CharIsHexDigit(c))
                {
                    int v = LexerUtils.HexDigit2Value(c);
                    d *= 16.0;
                    d += v;
                    ++digits;
                }
                else
                {
                    return(s.Substring(i));
                }
            }

            return(string.Empty);
        }
Example #2
0
        private Token ReadNumberToken(int fromLine, int fromCol)
        {
            StringBuilder text = new StringBuilder(32);

            //INT : Digit+
            //HEX : '0' [xX] HexDigit+
            //FLOAT : Digit+ '.' Digit* ExponentPart?
            //		| '.' Digit+ ExponentPart?
            //		| Digit+ ExponentPart
            //HEX_FLOAT : '0' [xX] HexDigit+ '.' HexDigit* HexExponentPart?
            //			| '0' [xX] '.' HexDigit+ HexExponentPart?
            //			| '0' [xX] HexDigit+ HexExponentPart
            //
            // ExponentPart : [eE] [+-]? Digit+
            // HexExponentPart : [pP] [+-]? Digit+

            bool isHex               = false;
            bool dotAdded            = false;
            bool exponentPart        = false;
            bool exponentSignAllowed = false;

            text.Append(CursorChar());

            char secondChar = CursorCharNext();

            if (secondChar == 'x' || secondChar == 'X')
            {
                isHex = true;
                text.Append(CursorChar());
                CursorCharNext();
            }

            for (char c = CursorChar(); CursorNotEof(); c = CursorCharNext())
            {
                if (exponentSignAllowed && (c == '+' || c == '-'))
                {
                    exponentSignAllowed = false;
                    text.Append(c);
                }
                else if (char.IsDigit(c))
                {
                    text.Append(c);
                }
                else if (c == '.' && !dotAdded)
                {
                    dotAdded = true;
                    text.Append(c);
                }
                else if (LexerUtils.CharIsHexDigit(c) && isHex && !exponentPart)
                {
                    text.Append(c);
                }
                else if (c == 'e' || c == 'E' || (isHex && (c == 'p' || c == 'P')))
                {
                    text.Append(c);
                    exponentPart        = true;
                    exponentSignAllowed = true;
                    dotAdded            = true;
                }
                else
                {
                    break;
                }
            }

            TokenType numberType = TokenType.Number;

            if (isHex && (dotAdded || exponentPart))
            {
                numberType = TokenType.Number_HexFloat;
            }
            else if (isHex)
            {
                numberType = TokenType.Number_Hex;
            }

            return(CreateToken(numberType, fromLine, fromCol, text.ToString()));
        }
Example #3
0
        private Token ReadNumberToken(int fromLine, int fromCol, bool leadingDot)
        {
            //INT : Digit+
            //HEX : '0' [xX] HexDigit+
            //FLOAT : Digit+ '.' Digit* ExponentPart?
            //		| '.' Digit+ ExponentPart?
            //		| Digit+ ExponentPart
            //HEX_FLOAT : '0' [xX] HexDigit+ '.' HexDigit* HexExponentPart?
            //			| '0' [xX] '.' HexDigit+ HexExponentPart?
            //			| '0' [xX] HexDigit+ HexExponentPart
            //
            // ExponentPart : [eE] [+-]? Digit+
            // HexExponentPart : [pP] [+-]? Digit+

            using (var sb = ZString.CreateStringBuilder())
            {
                bool isHex               = false;
                bool dotAdded            = false;
                bool exponentPart        = false;
                bool exponentSignAllowed = false;

                if (leadingDot)
                {
                    sb.Append("0.");
                }
                else if (this.CursorChar() == '0')
                {
                    sb.Append(this.CursorChar());
                    char secondChar = this.CursorCharNext();

                    if (secondChar == 'x' || secondChar == 'X')
                    {
                        isHex = true;
                        sb.Append(this.CursorChar());
                        this.CursorCharNext();
                    }
                }

                for (char c = this.CursorChar(); this.CursorNotEof(); c = this.CursorCharNext())
                {
                    if (exponentSignAllowed && (c == '+' || c == '-'))
                    {
                        exponentSignAllowed = false;
                        sb.Append(c);
                    }
                    else if (LexerUtils.CharIsDigit(c))
                    {
                        sb.Append(c);
                    }
                    else if (c == '.' && !dotAdded)
                    {
                        dotAdded = true;
                        sb.Append(c);
                    }
                    else if (LexerUtils.CharIsHexDigit(c) && isHex && !exponentPart)
                    {
                        sb.Append(c);
                    }
                    else if (c == 'e' || c == 'E' || (isHex && (c == 'p' || c == 'P')))
                    {
                        sb.Append(c);
                        exponentPart        = true;
                        exponentSignAllowed = true;
                        dotAdded            = true;
                    }
                    else
                    {
                        break;
                    }
                }

                var numberType = TokenType.Number;

                if (isHex && (dotAdded || exponentPart))
                {
                    numberType = TokenType.Number_HexFloat;
                }
                else if (isHex)
                {
                    numberType = TokenType.Number_Hex;
                }

                return(this.CreateToken(numberType, fromLine, fromCol, sb.ToString()));
            }
        }