public int nextToken()
        {
            if (pushedBack)
            {
                pushedBack = false;
                if (ttype != TT_NONE)
                {
                    return(ttype);
                }
            }

            _sval = null;
            int ch;

            // Skip whitespaced  Deal with EOL along the way.
            while (isWhitespace(ch = inRead.Read()))
            {
                if (ch == '\n' || ch == '\r')
                {
                    lineNumber++;

                    // Throw away \n if in combination with \r.
                    if (ch == '\r' && (ch = inRead.Read()) != '\n')
                    {
                        if (ch != TT_EOF)
                        {
                            inRead = inRead.Unread();
                        }
                    }
                    if (_eolSignificant)
                    {
                        return(ttype = TT_EOL);
                    }
                }
            }

            if (ch == '/')
            {
                if ((ch = inRead.Read()) == '/' && _slashSlash)
                {
                    while ((ch = inRead.Read()) != '\n' && ch != '\r' && ch != TT_EOF)
                    {
                        ;
                    }

                    if (ch != TT_EOF)
                    {
                        inRead = inRead.Unread();
                    }
                    return(nextToken()); // Recursive, but not too deep in normal cases
                }
                else if (ch == '*' && _slashStar)
                {
                    while (true)
                    {
                        ch = inRead.Read();
                        if (ch == '*')
                        {
                            if ((ch = inRead.Read()) == '/')
                            {
                                break;
                            }
                            else if (ch != TT_EOF)
                            {
                                inRead = inRead.Unread();
                            }
                        }
                        else if (ch == '\n' || ch == '\r')
                        {
                            lineNumber++;
                            if (ch == '\r' && (ch = inRead.Read()) != '\n')
                            {
                                if (ch != TT_EOF)
                                {
                                    inRead = inRead.Unread();
                                }
                            }
                        }
                        else if (ch == TT_EOF)
                        {
                            break;
                        }
                    }
                    return(nextToken()); // Recursive, but not too deep in normal cases
                }
                else
                {
                    if (ch != TT_EOF)
                    {
                        inRead = inRead.Unread();
                    }
                    ch = '/';
                }
            }

            if (ch == TT_EOF)
            {
                ttype = TT_EOF;
            }
            else if (isNumeric(ch))
            {
                Boolean isNegative = false;
                if (ch == '-')
                {
                    // Read ahead to see if this is an ordinary '-' rather than numeric.
                    ch = inRead.Read();
                    if (isNumeric(ch) && ch != '-')
                    {
                        isNegative = true;
                    }
                    else
                    {
                        if (ch != TT_EOF)
                        {
                            inRead = inRead.Unread();
                        }
                        return(ttype = '-');
                    }
                }

                StringBuilder tokbuf = new StringBuilder();
                tokbuf.Append((char)ch);

                int decCount = 0;
                while (isNumeric(ch = inRead.Read()) && ch != '-')
                {
                    if (ch == '.' && decCount++ > 0)
                    {
                        break;
                    }
                    else
                    {
                        tokbuf.Append((char)ch);
                    }
                }

                if (ch != TT_EOF)
                {
                    inRead = inRead.Unread();
                }
                ttype = TT_NUMBER;
                try
                {
                    _nval = Double.Parse(tokbuf.ToString());
                }
                catch (FormatException _)
                {
                    _nval = 0.0;
                }
                if (isNegative)
                {
                    _nval = -_nval;
                }
            }
            else if (isAlphabetic(ch))
            {
                StringBuilder tokbuf = new StringBuilder();
                tokbuf.Append((char)ch);
                while (isAlphabetic(ch = inRead.Read()) || isNumeric(ch))
                {
                    tokbuf.Append((char)ch);
                }
                if (ch != TT_EOF)
                {
                    inRead = inRead.Unread();
                }
                ttype = TT_WORD;
                _sval = tokbuf.ToString();
                if (_lowerCase)
                {
                    _sval = _sval.ToLower();
                }
            }
            else if (isComment(ch))
            {
                while ((ch = inRead.Read()) != '\n' && ch != '\r' && ch != TT_EOF)
                {
                    ;
                }

                if (ch != TT_EOF)
                {
                    inRead = inRead.Unread();
                }
                return(nextToken());    // Recursive, but not too deep in normal cases.
            }
            else if (isQuote(ch))
            {
                ttype = ch;
                StringBuilder tokbuf = new StringBuilder();
                while ((ch = inRead.Read()) != ttype && ch != '\n' && ch != '\r' &&
                       ch != TT_EOF)
                {
                    if (ch == '\\')
                    {
                        switch (ch = inRead.Read())
                        {
                        case 'a':
                            ch = 0x7;
                            break;

                        case 'b':
                            ch = '\b';
                            break;

                        case 'f':
                            ch = 0xC;
                            break;

                        case 'n':
                            ch = '\n';
                            break;

                        case 'r':
                            ch = '\r';
                            break;

                        case 't':
                            ch = '\t';
                            break;

                        case 'v':
                            ch = 0xB;
                            break;

                        case '\n':
                            ch = '\n';
                            break;

                        case '\r':
                            ch = '\r';
                            break;

                        case '\"':
                            break;

                        case '\'':
                            break;

                        case '\\':
                            break;

                        default:
                            int ch1, nextch;
                            if ((nextch = ch1 = ch) >= '0' && ch <= '7')
                            {
                                ch -= '0';
                                if ((nextch = inRead.Read()) >= '0' && nextch <= '7')
                                {
                                    ch = ch * 8 + nextch - '0';
                                    if ((nextch = inRead.Read()) >= '0' && nextch <= '7' &&
                                        ch1 >= '0' && ch1 <= '3')
                                    {
                                        ch     = ch * 8 + nextch - '0';
                                        nextch = inRead.Read();
                                    }
                                }
                            }

                            if (nextch != TT_EOF)
                            {
                                inRead = inRead.Unread();
                            }

                            break;
                        }
                    }

                    tokbuf.Append((char)ch);
                }

                // Throw away matching quote char.
                if (ch != ttype && ch != TT_EOF)
                {
                    inRead = inRead.Unread();
                }

                _sval = tokbuf.ToString();
            }
            else
            {
                ttype = ch;
            }

            return(ttype);
        }
Esempio n. 2
0
        /// <exception cref="System.IO.IOException"/>
        private string ReadField(string tag)
        {
            try
            {
                StringBuilder buf = new StringBuilder();
                while (true)
                {
                    char c = (char)stream.Read();
                    switch (c)
                    {
                    case ',':
                    {
                        return(buf.ToString());
                    }

                    case '}':
                    case '\n':
                    case '\r':
                    {
                        stream.Unread(c);
                        return(buf.ToString());
                    }

                    default:
                    {
                        buf.Append(c);
                        break;
                    }
                    }
                }
            }
            catch (IOException)
            {
                throw new IOException("Error reading " + tag);
            }
        }