コード例 #1
0
        private static int next(PushbackTextReader r)
        {
            int c = nextConsumed(r);

            r.Unread(c);
            return(c);
        }
コード例 #2
0
        private static string readstring(PushbackTextReader r)
        {
            int first = r.Read();

            if (first == -1)
            {
                throw new ArgumentException(BADEND + ", at line " + r.Line);
            }
            if (breakstring(first))
            {
                throw new ArgumentException(BADCHAR + ((char)first) + "', at line " + r.Line);
            }

            StringBuilder sb = new StringBuilder();

            if (first != '\'' && first != '"')
            {
                r.Unread(first);
                first = 0;
            }

            int c;

            while (true)
            {
                c = r.Read();
                if (c == '\\')
                {
                    c = r.Read();
                    if (c == -1)
                    {
                        throw new ArgumentException(BADEND + ", at line " + r.Line);
                    }
                }
                else if (first != 0 && c == first)
                {
                    break;
                }
                else if (first == 0)
                {
                    if (isWhite(c) || c == -1)
                    {
                        break;
                    }
                    if (breakstring(c))
                    {
                        r.Unread(c);
                        break;
                    }
                }
                else if (c == -1)
                {
                    throw new ArgumentException(BADEND + ", at line " + r.Line);
                }
                sb.Append((char)c);
            }
            return(sb.ToString());
        }
コード例 #3
0
        private static int swapComment(PushbackTextReader r)
        {
            int c;

            do
            {
                c = r.Read();
            }while(c != '\n' && c != -1);
            return(c);
        }
コード例 #4
0
        private static int nextConsumed(PushbackTextReader r)
        {
            int c;

            do
            {
                c = r.Read();
                if (c == '#')
                {
                    c = swapComment(r);
                }
            }while(isWhite(c));
            return(c);
        }
コード例 #5
0
        private static Hashtable readdict(PushbackTextReader r)
        {
            Hashtable h = new Hashtable();
            int       c = r.Read();

            if (c != '{')
            {
                throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line);
            }
            while (true)
            {
                c = next(r);
                if (c == '}')
                {
                    r.Read();
                    break;
                }
                string key = readstring(r);

                c = next(r);
                if (c != '=')
                {
                    throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line);
                }
                r.Read();

                next(r);
                object aValue = readobject(r);
                h[key] = aValue;

                c = next(r);
                if (c == '}')
                {
                    r.Read();
                    break;
                }
                if (c == ';' || c == ',')
                {
                    r.Read();
                    continue;
                }
            }
            return(h);
        }
コード例 #6
0
        private static byte[] readbytes(PushbackTextReader r)
        {
            MemoryStream bos = new MemoryStream();
            int          c   = r.Read();

            if (c != '<')
            {
                throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line);
            }

            char[] tab    = new char[2];
            byte[] outTab = new byte[1];
            int    Read;

            while (true)
            {
                Read = nextConsumed(r);
                if (Read == -1)
                {
                    throw new ArgumentException(BADEND + ", at line " + r.Line);
                }
                if (Read == '>')
                {
                    break;
                }
                tab[0] = (char)Read;
                Read   = nextConsumed(r);
                if (Read == -1)
                {
                    throw new ArgumentException(BADEND + ", at line " + r.Line);
                }
                tab[1] = (char)Read;

                outTab[0]   = hexaToInt(tab[0], r);
                outTab[0] <<= 4;
                outTab[0]  |= hexaToInt(tab[1], r);
                bos.Write(outTab, 0, 1);
            }
            return(bos.ToArray());
        }
コード例 #7
0
        private static byte hexaToInt(char c, PushbackTextReader r)
        {
            int n;

            if ('0' <= c && c <= '9')
            {
                n = c - '0';
            }
            else if ('a' <= c && c <= 'f')
            {
                n = c - 'a' + 10;
            }
            else if ('A' <= c && c <= 'F')
            {
                n = c - 'A' + 10;
            }
            else
            {
                throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line);
            }
            return((byte)n);
        }
コード例 #8
0
        private static object readobject(PushbackTextReader r)
        {
            int c = next(r);

            switch (c)
            {
            case -1:
                return(null);

            case '{':
                return(readdict(r));

            case '(':
                return(readarray(r));

            case '<':
                return(readbytes(r));

            default:
                return(readstring(r));
            }
        }
コード例 #9
0
        private static ArrayList readarray(PushbackTextReader r)
        {
            ArrayList v = new ArrayList();
            int       c = r.Read();

            if (c != '(')
            {
                throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line);
            }
            while (true)
            {
                c = next(r);
                if (c == ')')
                {
                    r.Read();
                    break;
                }

                object item = readobject(r);
                v.Add(item);

                c = next(r);
                if (c == ')')
                {
                    r.Read();
                    break;
                }
                if (c == ';' || c == ',')
                {
                    r.Read();
                    continue;
                }
                // be tolerant
                // throw new ArgumentException(BADCHAR + (char)c + "', at line " + r.Line);
            }
            return(v);
        }