Ejemplo n.º 1
0
        Token GetIdentifier(StringReader reader)
        {
            StringBuilder identifierBuilder = new StringBuilder();

            char symbol;

            while (true)
            {
                int peekResult = reader.Peek();
                if (peekResult == -1)
                {
                    break;
                }
                symbol = (char)peekResult;

                if (ParseRules.IsValidIdentifierChar(symbol))
                {
                    identifierBuilder.Append(symbol);
                    reader.Read();
                    continue;
                }

                if (ParseRules.IsStopForIdentifierOrLiteralChar(symbol))
                {
                    break;
                }

                throw new ArgumentException(String.Format("Unexpected character in identifier token: \"{0}\"", symbol));
            }

            return(new IdentifierToken(identifierBuilder.ToString()));
        }
Ejemplo n.º 2
0
        Token GetNumber(StringReader reader)
        {
            StringBuilder literalBuilder = new StringBuilder();

            char symbol, lastSymbol = (char)0;
            bool pointWasPut = false, exponentSignWasPut = false, exponentWasPut = false;

            while (true)
            {
                int peekResult = reader.Peek();
                if (peekResult == -1)
                {
                    break;
                }
                symbol = (char)peekResult;

                if (ParseRules.IsValidNumberChar(symbol, lastSymbol, pointWasPut, exponentSignWasPut, exponentWasPut))
                {
                    if (ParseRules.IsDecimalPointChar(symbol))
                    {
                        pointWasPut = true;
                    }
                    if (ParseRules.IsSignChar(symbol))
                    {
                        exponentSignWasPut = true;
                    }
                    if (ParseRules.IsExponentChar(symbol))
                    {
                        exponentWasPut = true;
                    }

                    literalBuilder.Append(symbol);
                    lastSymbol = symbol;
                    reader.Read();
                    continue;
                }

                if (ParseRules.IsStopForIdentifierOrLiteralChar(symbol))
                {
                    break;
                }

                throw new ArgumentException(String.Format("Unexpected character in number token: \"{0}\"", symbol));
            }

            return(new NumberToken(Double.Parse(literalBuilder.ToString(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture)));
        }