Read() public méthode

public Read ( ) : int
Résultat int
Exemple #1
0
        static List <Object> ReadDelimitedList(char delim, PushbackTextReader r, bool isRecursive, object opts)
        {
            LineNumberingTextReader lntr = r as LineNumberingTextReader;
            int firstLine = lntr != null ? lntr.LineNumber : -1;

            List <Object> a = new List <object>();

            for (; ;)
            {
                int ch = r.Read();

                while (isWhitespace(ch))
                {
                    ch = r.Read();
                }

                if (ch == -1)
                {
                    if (firstLine < 0)
                    {
                        throw new EndOfStreamException("EOF while reading");
                    }
                    else
                    {
                        throw new EndOfStreamException("EOF while reading, starting at line " + firstLine);
                    }
                }

                if (ch == delim)
                {
                    break;
                }

                IFn macroFn = getMacro(ch);
                if (macroFn != null)
                {
                    Object mret = macroFn.invoke(r, (char)ch, opts);
                    //no op macros return the reader
                    if (mret != r)
                    {
                        a.Add(mret);
                    }
                }
                else
                {
                    Unread(r, ch);
                    object o = read(r, true, null, isRecursive, opts);
                    if (o != r)
                    {
                        a.Add(o);
                    }
                }
            }

            return(a);
        }
        static int readUnicodeChar(PushbackTextReader r, int initch, int radix, int length, bool exact)
        {
            int uc = CharValueInRadix(initch, radix);

            if (uc == -1)
            {
                throw new ArgumentException("Invalid digit: " + (char)initch);
            }
            int i = 1;

            for (; i < length; ++i)
            {
                int ch = r.Read();
                if (ch == -1 || isWhitespace(ch) || isMacro(ch))
                {
                    Unread(r, ch);
                    break;
                }
                int d = CharValueInRadix(ch, radix);
                if (d == -1)
                {
                    throw new ArgumentException("Invalid digit: " + (char)ch);
                }
                uc = uc * radix + d;
            }
            if (i != length && exact)
            {
                throw new ArgumentException("Invalid character length: " + i + ", should be: " + length);
            }
            return(uc);
        }
        static object readNumber(PushbackTextReader r, char initch)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(initch);

            for (; ;)
            {
                int ch = r.Read();
                if (ch == -1 || isWhitespace(ch) || isMacro(ch))
                {
                    Unread(r, ch);
                    break;
                }
                sb.Append((char)ch);
            }

            string s = sb.ToString();
            object n = MatchNumber(s);

            if (n == null)
            {
                throw new FormatException("Invalid number: " + s);
            }
            return(n);
        }
Exemple #4
0
        public static List <Object> readDelimitedList(char delim, PushbackTextReader r, bool isRecursive)
        {
            List <Object> a = new List <object>();

            for (; ;)
            {
                int ch = r.Read();

                while (isWhitespace(ch))
                {
                    ch = r.Read();
                }

                if (ch == -1)
                {
                    throw new EndOfStreamException("EOF while reading");
                }

                if (ch == delim)
                {
                    break;
                }

                IFn macroFn = getMacro(ch);
                if (macroFn != null)
                {
                    Object mret = macroFn.invoke(r, (char)ch);
                    //no op macros return the reader
                    if (mret != r)
                    {
                        a.Add(mret);
                    }
                }
                else
                {
                    Unread(r, ch);
                    object o = read(r, true, null, isRecursive);
                    if (o != r)
                    {
                        a.Add(o);
                    }
                }
            }

            return(a);
        }
            protected override object Read(PushbackTextReader r, char semicolon, object opts)
            {
                int ch;

                do
                {
                    ch = r.Read();
                } while (ch != -1 && ch != '\n' && ch != '\r');
                return(r);
            }
Exemple #6
0
            protected override object Read(PushbackTextReader r, char doublequote)
            {
                StringBuilder sb = new StringBuilder();

                for (int ch = r.Read(); ch != '"'; ch = r.Read())
                {
                    if (ch == -1)
                    {
                        throw new EndOfStreamException("EOF while reading regex");
                    }
                    sb.Append((char)ch);
                    if (ch == '\\')     //escape
                    {
                        ch = r.Read();
                        if (ch == -1)
                        {
                            throw new EndOfStreamException("EOF while reading regex");
                        }
                        sb.Append((char)ch);
                    }
                }
                return(new Regex(sb.ToString()));
            }
Exemple #7
0
            protected override object Read(PushbackTextReader r, char hash)
            {
                int ch = r.Read();

                if (ch == -1)
                {
                    throw new EndOfStreamException("EOF while reading character");
                }
                IFn fn = _dispatchMacros[ch];

                if (fn == null)
                {
                    throw new Exception(String.Format("No dispatch macro for: {0}", (char)ch));
                }
                return(fn.invoke(r, (char)ch));
            }
Exemple #8
0
        static string readToken(PushbackTextReader r, char initch)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(initch);

            for (; ;)
            {
                int ch = r.Read();
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    Unread(r, ch);
                    return(sb.ToString());
                }
                sb.Append((char)ch);
            }
        }
        static string readSimpleToken(PushbackTextReader r, char initch, bool leadConstituent)
        {
            if (leadConstituent && NonConstituent(initch))
            {
                throw new InvalidOperationException("Invalid leading characters: " + (char)initch);
            }

            StringBuilder sb = new StringBuilder();

            sb.Append(initch);

            for (; ;)
            {
                int ch = r.Read();
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    Unread(r, ch);
                    return(sb.ToString());
                }
                sb.Append((char)ch);
            }
        }
Exemple #10
0
            protected override object Read(PushbackTextReader r, char hash, object opts)
            {
                int ch = r.Read();

                if (ch == -1)
                {
                    throw new EndOfStreamException("EOF while reading character");
                }
                IFn fn = _dispatchMacros[ch];

                if (fn == null)
                {
                    // try tagged reader
                    if (Char.IsLetter((char)ch))
                    {
                        Unread(r, ch);
                        return(_taggedReader.invoke(r, (char)ch, opts));
                    }
                    throw new InvalidOperationException(String.Format("No dispatch macro for: {0}", (char)ch));
                }
                return(fn.invoke(r, (char)ch, opts));
            }
Exemple #11
0
            protected override object Read(PushbackTextReader r, char pct)
            {
                int ch = r.Read();

                Unread(r, ch);
                //% alone is first arg
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    return(registerArg(1));
                }
                //object n = ReadAux(r, true, null, true);
                object n = ReadAux(r);

                if (n.Equals(Compiler._AMP_))
                {
                    return(registerArg(-1));
                }
                if (!Util.IsNumeric(n))
                {
                    throw new ArgumentException("arg literal must be %, %& or %integer");
                }
                return(registerArg(Util.ConvertToInt(n)));
            }
Exemple #12
0
            protected override object Read(PushbackTextReader r, char comma)
            {
                int ch = r.Read();

                if (ch == -1)
                {
                    throw new EndOfStreamException("EOF while reading character");
                }
                if (ch == '@')
                {
                    //object o = read(r, true, null, true);
                    object o = ReadAux(r);
                    return(RT.list(UNQUOTE_SPLICING, o));
                }
                else
                {
                    Unread(r, ch);
                    //object o = read(r, true, null, true);
                    object o = ReadAux(r);
                    //return new Unquote(o);
                    // per Rev 1184
                    return(RT.list(UNQUOTE, o));
                }
            }
Exemple #13
0
            public override object invoke(object reader, object colon, object opts)
            {
                PushbackTextReader r = reader as PushbackTextReader;

                // Read ns symbol
                object osym = read(r, true, null, false, opts);
                Symbol sym  = osym as Symbol;

                if (sym == null || sym.Namespace != null)
                {
                    throw new Exception("Namespaced map must specify a valid namespace: " + osym);
                }
                string ns = sym.Name;

                // Read map
                int nextChar = r.Read();

                while (isWhitespace(nextChar))
                {
                    nextChar = r.Read();
                }
                if ('{' != nextChar)
                {
                    throw new Exception("Namespaced map must specify a map");
                }
                List <object> kvs = ReadDelimitedList('}', r, true, opts);

                if ((kvs.Count & 1) == 1)
                {
                    throw new Exception("Namespaced map literal must contain an even number of forms");
                }

                // Construct output map
                object[] a = new object[kvs.Count];
                // IPersistentMap m = RT.map();
                using (var iterator = kvs.GetEnumerator())
                {
                    for (int i = 0; iterator.MoveNext(); i += 2)
                    {
                        var key = iterator.Current;
                        iterator.MoveNext();
                        var val = iterator.Current;

                        Keyword kw = key as Keyword;
                        if (kw != null)
                        {
                            if (kw.Namespace == null)
                            {
                                key = Keyword.intern(ns, kw.Name);
                            }
                            else if (kw.Namespace.Equals("_"))
                            {
                                key = Keyword.intern(null, kw.Name);
                            }
                        }
                        else
                        {
                            Symbol s = key as Symbol;
                            if (s != null)
                            {
                                if (s.Namespace == null)
                                {
                                    key = Symbol.intern(ns, s.Name);
                                }
                                else if (s.Namespace.Equals("_"))
                                {
                                    key = Symbol.intern(null, s.Name);
                                }
                            }
                        }
                        a[i]     = key;
                        a[i + 1] = val;
                    }
                }
                return(RT.map(a));
            }
Exemple #14
0
        static object readNumber(PushbackTextReader r, char initch)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(initch);

            for (; ; )
            {
                int ch = r.Read();
                if (ch == -1 || isWhitespace(ch) || isMacro(ch))
                {
                    Unread(r, ch);
                    break;
                }
                sb.Append((char)ch);
            }

            string s = sb.ToString();
            object n = matchNumber(s);
            if (n == null)
                throw new FormatException("Invalid number: " + s);
            return n;
        }
Exemple #15
0
        // There is really no reason for the main entry point to have an isRecursive flag, is there?
        public static object read(PushbackTextReader r,
            bool eofIsError,
            object eofValue,
            bool isRecursive)
        {
            try
            {
                for (; ; )
                {
                    int ch = r.Read();

                    while (isWhitespace(ch))
                        ch = r.Read();

                    if (ch == -1)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading");
                        return eofValue;
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return RT.suppressRead() ? null : n;
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch);
                        if (RT.suppressRead())
                            return null;
                        // no op macros return the reader
                        if (ret == r)
                            continue;
                        return ret;
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return RT.suppressRead() ? null : n;
                        }
                        Unread(r, ch2);
                    }

                    string token = readToken(r, (char)ch);
                    return RT.suppressRead() ? null : interpretToken(token);
                }
            }
            catch (Exception e)
            {
                if (isRecursive || !(r is LineNumberingTextReader))
                    throw e;
                LineNumberingTextReader rdr = r as LineNumberingTextReader;
                throw new ReaderException(rdr.LineNumber, e);
            }
        }
Exemple #16
0
            protected override object Read(PushbackTextReader r, char doublequote)
            {
                StringBuilder sb = new StringBuilder();

                for (int ch = r.Read(); ch != '"'; ch = r.Read())
                {
                    if (ch == -1)
                    {
                        throw new EndOfStreamException("EOF while reading string");
                    }
                    if (ch == '\\')     //escape
                    {
                        ch = r.Read();
                        if (ch == -1)
                        {
                            throw new EndOfStreamException("EOF while reading string");
                        }
                        switch (ch)
                        {
                        case 't':
                            ch = '\t';
                            break;

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

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

                        case '\\':
                            break;

                        case '"':
                            break;

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

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

                        case 'u':
                            ch = r.Read();
                            if (CharValueInRadix(ch, 16) == -1)
                            {
                                throw new Exception("Invalid unicode escape: \\u" + (char)ch);
                            }
                            ch = readUnicodeChar((PushbackTextReader)r, ch, 16, 4, true);
                            break;

                        default:
                        {
                            if (CharValueInRadix(ch, 8) != -1)
                            {
                                ch = readUnicodeChar((PushbackTextReader)r, ch, 8, 3, false);
                                if (ch > 255)         //octal377
                                {
                                    throw new Exception("Octal escape sequence must be in range [0, 377].");
                                }
                            }
                            else
                            {
                                throw new Exception("Unsupported escape character: \\" + (char)ch);
                            }
                        }
                        break;
                        }
                    }
                    sb.Append((char)ch);
                }
                return(sb.ToString());
            }
        static void readToken(PushbackTextReader r, char initch, out String rawToken, out String token, out String mask, out bool eofSeen)
        {
            bool allowSymEscape = RT.booleanCast(RT.AllowSymbolEscapeVar.deref());

            bool rawMode = false;

            StringBuilder sbRaw = new StringBuilder();
            StringBuilder sbToken = new StringBuilder();
            StringBuilder sbMask = new StringBuilder();

            if (allowSymEscape && initch == '|')
            {
                rawMode = true;
                sbRaw.Append(initch);
            }
            else
            {
                sbRaw.Append(initch);
                sbToken.Append(initch);
                sbMask.Append(initch);
            }

            for (; ; )
            {
                int ch = r.Read();
                if (rawMode)
                {
                    if (ch == -1)
                    {
                        rawToken = sbRaw.ToString();
                        token = sbToken.ToString();
                        mask = sbMask.ToString();
                        eofSeen = true;
                        return;
                    }
                    if (ch == '|')
                    {
                        int ch2 = r.Read();
                        if (ch2 == '|')
                        {
                            sbRaw.Append('|');
                            sbToken.Append('|');
                            sbMask.Append('a');
                        }
                        else
                        {
                            r.Unread(ch2);
                            rawMode = false;
                            sbRaw.Append(ch);
                        }
                    }
                    else
                    {
                        sbRaw.Append((char)ch);
                        sbToken.Append((char)ch);
                        sbMask.Append('a');
                    }
                }
                else
                {
                    if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                    {
                        Unread(r, ch);
                        rawToken = sbRaw.ToString();
                        token = sbToken.ToString();
                        mask = sbMask.ToString();
                        eofSeen = false;
                        return;
                    }
                    else if (ch == '|' && allowSymEscape)
                    {
                        rawMode = true;
                        sbRaw.Append((char)ch);
                    }
                    else
                    {
                        sbRaw.Append((char)ch);
                        sbToken.Append((char)ch);
                        sbMask.Append((char)ch);
                    }
                }
            }
        }
            protected override object Read(PushbackTextReader r, char pct, object opts, object pendingForms)
            {
                //if (ARG_ENV.deref() == null)
                //    return interpretToken(readToken(r, '%'));
                if (ARG_ENV.deref() == null)
                {
                    return InterpretToken(readSimpleToken(r, '%'));
                }

                int ch = r.Read();
                Unread(r, ch);
                //% alone is first arg
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    return registerArg(1);
                }
                //object n = ReadAux(r, true, null, true, opts, pendingForms);
                object n = ReadAux(r, opts, EnsurePending(pendingForms));
                if (n.Equals(Compiler.AmpersandSym))
                    return registerArg(-1);
                if (!Util.IsNumeric(n))
                    throw new ArgumentException("arg literal must be %, %& or %integer");
                return registerArg(Util.ConvertToInt(n));
            }
Exemple #19
0
            protected override object Read(PushbackTextReader r, char c)
            {
                object recordName = read(r, true, null, false);
                Type recordType = RT.classForName(recordName.ToString());

                int ch = r.Read();
                char endch;
                bool shortForm = true;

                // A defrecord ctor can take two forms.  Check for map->R version first.
                if (ch == '{')
                {
                    endch = '}';
                    shortForm = false;
                }
                else if (ch == '[')
                    endch = ']';
                else
                    throw new Exception(String.Format("Unreadable constructor form starting with \"#{0}{1}\"", recordName, (char)ch));

                object[] recordEntries = readDelimitedList(endch, r, true).ToArray();
                object ret = null;
                ConstructorInfo[] allCtors = recordType.GetConstructors();

                if (shortForm)
                {
                    bool ctorFound = false;
                    foreach ( ConstructorInfo cinfo in allCtors )
                        if ( cinfo.GetParameters().Length == recordEntries.Length )
                            ctorFound = true;

                    if ( ! ctorFound )
                        throw new Exception(String.Format("Unexpected number of constructor arguments to {0}: got {1}",recordType.ToString(),recordEntries.Length));

                    ret = Reflector.InvokeConstructor(recordType,RT.SeqToArray<Object>(ResolveEach(recordEntries)));
                }
                else
                {
                    ret = Reflector.InvokeStaticMethod(recordType,"create",new Object[] { RT.map(RT.SeqToArray<object>(ResolveEach(recordEntries))) });
                }

                return ret;
            }
Exemple #20
0
            protected override object Read(PushbackTextReader r, char pct)
            {
                //if (ARG_ENV.deref() == null)
                //    return interpretToken(readToken(r, '%'));
                if (ARG_ENV.deref() == null)
                {
                    string token;
                    int lastSlashIndex;
                    if (readToken(r, '%', out token, out lastSlashIndex))
                        throw new EndOfStreamException("EOF while reading %-token");
                    else
                        return InterpretToken(token, lastSlashIndex);
                }

                int ch = r.Read();
                Unread(r, ch);
                //% alone is first arg
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    return registerArg(1);
                }
                //object n = ReadAux(r, true, null, true);
                object n = ReadAux(r);
                if (n.Equals(Compiler.AmpersandSym))
                    return registerArg(-1);
                if (!Util.IsNumeric(n))
                    throw new ArgumentException("arg literal must be %, %& or %integer");
                return registerArg(Util.ConvertToInt(n));
            }
Exemple #21
0
 protected override object Read(PushbackTextReader r, char hash, object opts)
 {
     int ch = r.Read();
     if (ch == -1)
         throw new EndOfStreamException("EOF while reading character");
     IFn fn = _dispatchMacros[ch];
     if (fn == null)
     {
         // try tagged reader
         if (Char.IsLetter((char)ch))
         {
             Unread(r, ch);
             return _taggedReader.invoke(r,(char)ch, opts);
         }
         throw new InvalidOperationException(String.Format("No dispatch macro for: {0}", (char)ch));
     }
     return fn.invoke(r, (char)ch, opts);
 }
Exemple #22
0
        static string readSimpleToken(PushbackTextReader r, char initch, bool leadConstituent)
        {
            if (leadConstituent && NonConstituent(initch))
                throw new InvalidOperationException("Invalid leading characters: " + (char)initch);

            StringBuilder sb = new StringBuilder();
            sb.Append(initch);

            for (; ; )
            {
                int ch = r.Read();
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    Unread(r, ch);
                    return sb.ToString();
                }
                sb.Append((char)ch);
            }
        }
Exemple #23
0
        public static object read(PushbackTextReader r,
            bool eofIsError,
            object eofValue,
            bool isRecursive,
            Object opts)
        {
            try
            {
                for (; ; )
                {
                    int ch = r.Read();

                    while (isWhitespace(ch))
                        ch = r.Read();

                    if (ch == -1)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading");
                        return eofValue;
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return RT.suppressRead() ? null : n;
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch, opts);
                        if (RT.suppressRead())
                            return null;
                        // no op macros return the reader
                        if (ret == r)
                            continue;
                        return ret;
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return RT.suppressRead() ? null : n;
                        }
                        Unread(r, ch2);
                    }

                    //string token = readToken(r, (char)ch);
                    //return RT.suppressRead() ? null : interpretToken(token);
                    string token;
                    int lastSlashIndex;
                    bool eofSeen = readToken(r, (char)ch, true, out token, out lastSlashIndex);
                    if (eofSeen)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading");
                        return eofValue;
                    }
                    return RT.suppressRead() ? null : InterpretToken(token, lastSlashIndex);
                }
            }
            catch (Exception e)
            {
                if (isRecursive)
                    throw;

                LineNumberingTextReader lntr = r as LineNumberingTextReader;
                if (lntr == null)
                    throw;

                throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
            }
        }
Exemple #24
0
        public static object read(PushbackTextReader r,
                                  bool eofIsError,
                                  object eofValue,
                                  bool isRecursive,
                                  Object opts)
        {
            try
            {
                for (; ;)
                {
                    int ch = r.Read();

                    while (isWhitespace(ch))
                    {
                        ch = r.Read();
                    }

                    if (ch == -1)
                    {
                        if (eofIsError)
                        {
                            throw new EndOfStreamException("EOF while reading");
                        }
                        return(eofValue);
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return(RT.suppressRead() ? null : n);
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch, opts);
                        if (RT.suppressRead())
                        {
                            return(null);
                        }
                        // no op macros return the reader
                        if (ret == r)
                        {
                            continue;
                        }
                        return(ret);
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return(RT.suppressRead() ? null : n);
                        }
                        Unread(r, ch2);
                    }

                    //string token = readToken(r, (char)ch);
                    //return RT.suppressRead() ? null : interpretToken(token);
                    readToken(r, (char)ch, true, out string rawToken, out string token, out string mask, out bool eofSeen);
                    if (eofSeen)
                    {
                        if (eofIsError)
                        {
                            throw new EndOfStreamException("EOF while reading symbol");
                        }
                        return(eofValue);
                    }
                    return(RT.suppressRead() ? null : InterpretToken(rawToken, token, mask));
                }
            }
            catch (Exception e)
            {
                if (isRecursive)
                {
                    throw;
                }

                if (r is LineNumberingTextReader lntr)
                {
                    throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
                }
                throw;
            }
        }
Exemple #25
0
        static List<Object> ReadDelimitedList(char delim, PushbackTextReader r, bool isRecursive)
        {
            LineNumberingTextReader lntr = r as LineNumberingTextReader;
            int firstLine = lntr != null  ? lntr.LineNumber : -1;

            List<Object> a = new List<object>();

            for (; ; )
            {
                int ch = r.Read();

                while (isWhitespace(ch))
                    ch = r.Read();

                if (ch == -1)
                {
                    if (firstLine < 0)
                        throw new EndOfStreamException("EOF while reading");
                    else
                        throw new EndOfStreamException("EOF while reading, starting at line " + firstLine);
                }

                if (ch == delim)
                {
                    break;
                }

                IFn macroFn = getMacro(ch);
                if (macroFn != null)
                {
                    Object mret = macroFn.invoke(r, (char)ch);
                    //no op macros return the reader
                    if (mret != r)
                        a.Add(mret);
                }
                else
                {
                    Unread(r, ch);
                    object o = read(r, true, null, isRecursive);
                    if (o != r)
                        a.Add(o);
                }
            }

            return a;
        }
            protected override object Read(PushbackTextReader r, char doublequote, object opts, object pendingForms)
            {
                StringBuilder sb = new StringBuilder();

                for (int ch = r.Read(); ch != '"'; ch = r.Read())
                {
                    if (ch == -1)
                        throw new EndOfStreamException("EOF while reading string");
                    if (ch == '\\')	//escape
                    {
                        ch = r.Read();
                        if (ch == -1)
                            throw new EndOfStreamException("EOF while reading string");
                        switch (ch)
                        {
                            case 't':
                                ch = '\t';
                                break;
                            case 'r':
                                ch = '\r';
                                break;
                            case 'n':
                                ch = '\n';
                                break;
                            case '\\':
                                break;
                            case '"':
                                break;
                            case 'b':
                                ch = '\b';
                                break;
                            case 'f':
                                ch = '\f';
                                break;
                            case 'u':
                                ch = r.Read();
                                if (CharValueInRadix(ch, 16) == -1)
                                    throw new InvalidOperationException("Invalid unicode escape: \\u" + (char)ch);
                                ch = readUnicodeChar((PushbackTextReader)r, ch, 16, 4, true);
                                break;
                            default:
                                {
                                    //if (CharValueInRadix(ch, 8) != -1)  -- this is correct, but we end up with different error message for 8,9 than JVM, so do the following to match:
                                    if (Char.IsDigit((char)ch))
                                    {
                                        ch = readUnicodeChar((PushbackTextReader)r, ch, 8, 3, false);
                                        if (ch > 255) //octal377
                                            throw new InvalidOperationException("Octal escape sequence must be in range [0, 377].");
                                    }
                                    else
                                        throw new InvalidOperationException("Unsupported escape character: \\" + (char)ch);
                                }
                                break;
                        }
                    }
                    sb.Append((char)ch);
                }
                return sb.ToString();
            }
Exemple #27
0
            static object ReadRecord(PushbackTextReader r, Symbol recordName)
            {
                Type recordType = RT.classForName(recordName.ToString());

                char endch;
                bool shortForm = true;

                int ch = r.Read();

                // flush whitespace
                //while (isWhitespace(ch))
                //    ch = r.Read();

                // A defrecord ctor can take two forms.  Check for map->R version first.
                if (ch == '{')
                {
                    endch = '}';
                    shortForm = false;
                }
                else if (ch == '[')
                    endch = ']';
                else
                    throw new ArgumentException(String.Format("Unreadable constructor form starting with \"#{0}{1}\"", recordName, (char)ch));

                object[] recordEntries = ReadDelimitedList(endch, r, true).ToArray();
                object ret = null;
                ConstructorInfo[] allCtors = recordType.GetConstructors();

                if (shortForm)
                {
                    bool ctorFound = false;
                    foreach ( ConstructorInfo cinfo in allCtors )
                        if ( cinfo.GetParameters().Length == recordEntries.Length )
                            ctorFound = true;

                    if ( ! ctorFound )
                        throw new ArgumentException(String.Format("Unexpected number of constructor arguments to {0}: got {1}", recordType.ToString(), recordEntries.Length));

                    ret = Reflector.InvokeConstructor(recordType,recordEntries);
                }
                else
                {
                    IPersistentMap vals = RT.map(recordEntries);
                    for (ISeq s = RT.keys(vals); s != null; s = s.next())
                    {
                        if (!(s.first() is Keyword))
                            throw new ArgumentException(String.Format("Unreadable defrecord form: key must be of type clojure.lang.Keyword, got {0}", s.first().ToString()));
                    }

                    ret = Reflector.InvokeStaticMethod(recordType, "create", new Object[] { vals });
                }

                return ret;
            }
        private static Object read(PushbackTextReader r, bool eofIsError, object eofValue, char? returnOn, object returnOnValue, bool isRecursive, object opts, object pendingForms)
        {
            if (UNKNOWN.Equals(RT.ReadEvalVar.deref()))
                throw new InvalidOperationException("Reading disallowed - *read-eval* bound to :unknown");

            opts = InstallPlatformFeature(opts);

            try
            {
                for (; ; )
                {
                    var pfl = pendingForms as LinkedList<Object>;
                    if (pfl != null && pfl.Count != 0)
                    {
                        object val = pfl.First.Value;
                        pfl.RemoveFirst();
                        return val;
                    }

                    int ch = r.Read();

                    while (isWhitespace(ch))
                        ch = r.Read();

                    if (ch == -1)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading");
                        return eofValue;
                    }

                    if (returnOn.HasValue && (returnOn.Value == ch))
                    {
                        return returnOnValue;
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return n;
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch, opts, pendingForms);
                        // no op macros return the reader
                        if (ret == r)
                            continue;
                        return ret;
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return n;
                        }
                        Unread(r, ch2);
                    }

                    //string token = readToken(r, (char)ch);
                    //return RT.suppressRead() ? null : interpretToken(token);
                    string rawToken;
                    string token;
                    string mask;
                    bool eofSeen;
                    readToken(r, (char)ch, out rawToken, out token, out mask, out eofSeen);
                    if (eofSeen)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading symbol");
                        return eofValue;
                    }
                    return InterpretToken(rawToken, token, mask);
                }
            }
            catch (Exception e)
            {
                if (isRecursive)
                    throw;

                LineNumberingTextReader lntr = r as LineNumberingTextReader;
                if (lntr == null)
                    throw;

                throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
            }
        }
Exemple #29
0
            protected override object Read(PushbackTextReader r, char pct)
            {
                if (ARG_ENV.deref() == null)
                    return interpretToken(readToken(r, '%'));

                int ch = r.Read();
                Unread(r, ch);
                //% alone is first arg
                if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    return registerArg(1);
                }
                //object n = ReadAux(r, true, null, true);
                object n = ReadAux(r);
                if (n.Equals(Compiler._AMP_))
                    return registerArg(-1);
                if (!Util.IsNumeric(n))
                    throw new ArgumentException("arg literal must be %, %& or %integer");
                return registerArg(Util.ConvertToInt(n));
            }
            protected override object Read(PushbackTextReader r, char c, object opts, object pendingForms)
            {
                checkConditionalAllowed(opts);

                int ch = r.Read();
                if (ch == -1)
                    throw new EndOfStreamException("EOF while reading character");

                bool splicing = false;

                if (ch == '@')
                {
                    splicing = true;
                    ch = r.Read();
                }

                while (isWhitespace(ch))
                    ch = r.Read();

                if (ch == -1)
                    throw new EndOfStreamException("EOF while reading character");

                if (ch != '(')
                    throw new InvalidOperationException("read-cond body must be a list");

                try
                {
                    Var.pushThreadBindings(RT.map(READ_COND_ENV, true /* RT.T */));

                    if (IsPreserveReadCond(opts))
                    {
                        IFn listReader = getMacro(ch); // should always be a list
                        Object form = listReader.invoke(r, (char)ch, opts, EnsurePending(pendingForms));

                        return ReaderConditional.create(form, splicing);
                    }
                    else
                    {
                        return readCondDelimited(r, splicing, opts, pendingForms);
                    }
                }
                finally
                {
                    Var.popThreadBindings();
                }
            }
Exemple #31
0
        // There is really no reason for the main entry point to have an isRecursive flag, is there?

        public static object read(PushbackTextReader r,
                                  bool eofIsError,
                                  object eofValue,
                                  bool isRecursive)
        {
            try
            {
                for (; ;)
                {
                    int ch = r.Read();

                    while (isWhitespace(ch))
                    {
                        ch = r.Read();
                    }

                    if (ch == -1)
                    {
                        if (eofIsError)
                        {
                            throw new EndOfStreamException("EOF while reading");
                        }
                        return(eofValue);
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return(RT.suppressRead() ? null : n);
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch);
                        if (RT.suppressRead())
                        {
                            return(null);
                        }
                        // no op macros return the reader
                        if (ret == r)
                        {
                            continue;
                        }
                        return(ret);
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return(RT.suppressRead() ? null : n);
                        }
                        Unread(r, ch2);
                    }

                    string token = readToken(r, (char)ch);
                    return(RT.suppressRead() ? null : interpretToken(token));
                }
            }
            catch (Exception e)
            {
                if (isRecursive || !(r is LineNumberingTextReader))
                {
                    throw e;
                }
                LineNumberingTextReader rdr = r as LineNumberingTextReader;
                throw new ReaderException(rdr.LineNumber, e);
            }
        }
Exemple #32
0
        static bool readToken(PushbackTextReader r, char initch, out string nameString, out int lastSlashIndex)
        {
            bool oddVertBarMode = false;
            lastSlashIndex = -1;
            bool allowSymEscape = RT.booleanCast(RT.AllowSymbolEscapeVar.deref());

            StringBuilder sb = new StringBuilder();

            if (allowSymEscape && initch == '|')
                oddVertBarMode = true;
            else
                sb.Append(initch);

            for (; ; )
            {
                int ch = r.Read();
                if (oddVertBarMode)
                {
                    if (ch == -1)
                    {
                        nameString = sb.ToString();
                        return true;
                    }
                    if (ch == '|')
                    {
                        int ch2 = r.Read();
                        if (ch2 == '|')
                            sb.Append('|');
                        else
                        {
                            r.Unread(ch2);
                            oddVertBarMode = false;
                        }
                    }
                    else
                        sb.Append((char)ch);
                }
                else
                {
                    if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                    {
                        Unread(r, ch);
                        nameString = sb.ToString();
                        return false;
                    }
                    else if (ch == '|' && allowSymEscape)
                    {
                        oddVertBarMode = true;
                    }
                    else
                    {
                        sb.Append((char)ch);
                        if (ch == '/')
                            lastSlashIndex = sb.Length - 1;
                    }
                }
            }
        }
Exemple #33
0
        static void readToken(PushbackTextReader r, char initch, bool leadConstituent, out String rawToken, out String token, out String mask, out bool eofSeen)
        {
            if (leadConstituent && NonConstituent(initch))
            {
                throw new InvalidOperationException("Invalid leading characters: " + (char)initch);
            }

            bool allowSymEscape = RT.booleanCast(RT.AllowSymbolEscapeVar.deref());

            bool rawMode = false;

            StringBuilder sbRaw   = new StringBuilder();
            StringBuilder sbToken = new StringBuilder();
            StringBuilder sbMask  = new StringBuilder();

            if (allowSymEscape && initch == '|')
            {
                rawMode = true;
                sbRaw.Append(initch);
            }
            else
            {
                sbRaw.Append(initch);
                sbToken.Append(initch);
                sbMask.Append(initch);
            }

            for (; ;)
            {
                int ch = r.Read();
                if (rawMode)
                {
                    if (ch == -1)
                    {
                        rawToken = sbRaw.ToString();
                        token    = sbToken.ToString();
                        mask     = sbMask.ToString();
                        eofSeen  = true;
                        return;
                    }
                    if (ch == '|')
                    {
                        int ch2 = r.Read();
                        if (ch2 == '|')
                        {
                            sbRaw.Append('|');
                            sbToken.Append('|');
                            sbMask.Append('a');
                        }
                        else
                        {
                            r.Unread(ch2);
                            rawMode = false;
                            sbRaw.Append(ch);
                        }
                    }
                    else
                    {
                        sbRaw.Append((char)ch);
                        sbToken.Append((char)ch);
                        sbMask.Append('a');
                    }
                }
                else
                {
                    if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                    {
                        Unread(r, ch);
                        rawToken = sbRaw.ToString();
                        token    = sbToken.ToString();
                        mask     = sbMask.ToString();
                        eofSeen  = false;
                        return;
                    }
                    else if (NonConstituent(ch))
                    {
                        throw new InvalidOperationException("Invalid constituent character: " + (char)ch);
                    }
                    else if (ch == '|' && allowSymEscape)
                    {
                        rawMode = true;
                        sbRaw.Append((char)ch);
                    }
                    else
                    {
                        sbRaw.Append((char)ch);
                        sbToken.Append((char)ch);
                        sbMask.Append((char)ch);
                    }
                }
            }
        }
Exemple #34
0
            protected override object Read(PushbackTextReader r, char hash)
            {
                int ch = r.Read();
                if (ch == -1)
                    throw new EndOfStreamException("EOF while reading character");
                IFn fn = _dispatchMacros[ch];
                // Try the ctor reader first
                if (fn == null)
                {
                    Unread(r, ch);
                    object result = _ctorReader.invoke(r,(char) ch);

                    if (result != null)
                        return result;
                    else
                        throw new InvalidOperationException(String.Format("No dispatch macro for: {0}", (char)ch));
                }
                return fn.invoke(r, (char)ch);
            }
Exemple #35
0
 static int readUnicodeChar(PushbackTextReader r, int initch, int radix, int length, bool exact)
 {
     int uc = CharValueInRadix(initch, radix);
     if (uc == -1)
         throw new ArgumentException("Invalid digit: " + initch);
     int i = 1;
     for (; i < length; ++i)
     {
         int ch = r.Read();
         if (ch == -1 || isWhitespace(ch) || isMacro(ch))
         {
             Unread(r, ch);
             break;
         }
         int d = CharValueInRadix(ch, radix);
         if (d == -1)
             throw new ArgumentException("Invalid digit: " + (char)ch);
         uc = uc * radix + d;
     }
     if (i != length && exact)
         throw new ArgumentException("Invalid character length: " + i + ", should be: " + length);
     return uc;
 }
Exemple #36
0
 protected override object Read(PushbackTextReader r, char comma)
 {
     int ch = r.Read();
     if (ch == -1)
         throw new EndOfStreamException("EOF while reading character");
     if (ch == '@')
     {
         //object o = read(r, true, null, true);
         object o = ReadAux(r);
         return RT.list(UNQUOTE_SPLICING, o);
     }
     else
     {
         Unread(r, ch);
         //object o = read(r, true, null, true);
         object o = ReadAux(r);
         //return new Unquote(o);
         // per Rev 1184
         return RT.list(UNQUOTE, o);
     }
 }
Exemple #37
0
 protected override object Read(PushbackTextReader r, char semicolon)
 {
     int ch;
     do
     {
         ch = r.Read();
     } while (ch != -1 && ch != '\n' && ch != '\r');
     return r;
 }
Exemple #38
0
        public static List<Object> readDelimitedList(char delim, PushbackTextReader r, bool isRecursive)
        {
            List<Object> a = new List<object>();

            for (; ; )
            {
                int ch = r.Read();

                while (isWhitespace(ch))
                    ch = r.Read();

                if (ch == -1)
                    throw new EndOfStreamException("EOF while reading");

                if (ch == delim)
                {
                    break;
                }

                IFn macroFn = getMacro(ch);
                if (macroFn != null)
                {
                    Object mret = macroFn.invoke(r, (char)ch);
                    //no op macros return the reader
                    if (mret != r)
                        a.Add(mret);
                }
                else
                {
                    Unread(r, ch);
                    object o = read(r, true, null, isRecursive);
                    if (o != r)
                        a.Add(o);
                }
            }

            return a;
        }
Exemple #39
0
 protected override object Read(PushbackTextReader r, char doublequote)
 {
     StringBuilder sb = new StringBuilder();
     for (int ch = r.Read(); ch != '"'; ch = r.Read())
     {
         if (ch == -1)
             throw new EndOfStreamException("EOF while reading regex");
         sb.Append((char)ch);
         if (ch == '\\')	//escape
         {
             ch = r.Read();
             if (ch == -1)
                 throw new EndOfStreamException("EOF while reading regex");
             sb.Append((char)ch);
         }
     }
     return new Regex(sb.ToString());
 }
Exemple #40
0
        static string readToken(PushbackTextReader r, char initch)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(initch);

            for(; ;)
            {
                int ch = r.Read();
                if(ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                {
                    Unread(r, ch);
                    return sb.ToString();
                }
                sb.Append((char) ch);
            }
        }
Exemple #41
0
        static bool readToken(PushbackTextReader r, char initch, bool leadConstituent, out string nameString, out int lastSlashIndex)
        {
            if (leadConstituent && NonConstituent(initch))
            {
                throw new InvalidOperationException("Invalid leading characters: " + (char)initch);
            }

            bool oddVertBarMode = false;

            lastSlashIndex = -1;
            bool allowSymEscape = RT.booleanCast(RT.AllowSymbolEscapeVar.deref());

            StringBuilder sb = new StringBuilder();

            if (allowSymEscape && initch == '|')
            {
                oddVertBarMode = true;
            }
            else
            {
                sb.Append(initch);
            }

            for (; ;)
            {
                int ch = r.Read();
                if (oddVertBarMode)
                {
                    if (ch == -1)
                    {
                        nameString = sb.ToString();
                        return(true);
                    }
                    if (ch == '|')
                    {
                        int ch2 = r.Read();
                        if (ch2 == '|')
                        {
                            sb.Append('|');
                        }
                        else
                        {
                            r.Unread(ch2);
                            oddVertBarMode = false;
                        }
                    }
                    else
                    {
                        sb.Append((char)ch);
                    }
                }
                else
                {
                    if (ch == -1 || isWhitespace(ch) || isTerminatingMacro(ch))
                    {
                        Unread(r, ch);
                        nameString = sb.ToString();
                        return(false);
                    }
                    else if (NonConstituent(ch))
                    {
                        throw new InvalidOperationException("Invalid constituent character: " + (char)ch);
                    }
                    else if (ch == '|' && allowSymEscape)
                    {
                        oddVertBarMode = true;
                    }
                    else
                    {
                        sb.Append((char)ch);
                        if (ch == '/')
                        {
                            lastSlashIndex = sb.Length - 1;
                        }
                    }
                }
            }
        }
Exemple #42
0
 protected override object Read(PushbackTextReader r, char backslash)
 {
     int ch = r.Read();
     if (ch == -1)
         throw new EndOfStreamException("EOF while reading character");
     String token = readToken(r, (char)ch);
     if (token.Length == 1)
         return token[0];
     else if (token.Equals("newline"))
         return '\n';
     else if (token.Equals("space"))
         return ' ';
     else if (token.Equals("tab"))
         return '\t';
     else if (token.Equals("backspace"))
         return '\b';
     else if (token.Equals("formfeed"))
         return '\f';
     else if (token.Equals("return"))
         return '\r';
     else if (token.StartsWith("u"))
     {
         char c = (char)readUnicodeChar(token, 1, 4, 16);
         if (c >= '\uD800' && c <= '\uDFFF') // surrogate code unit?
             throw new Exception("Invalid character constant: \\u" + ((int)c).ToString("X"));
         return c;
     }
     else if (token.StartsWith("o"))
     {
         int len = token.Length - 1;
         if (len > 3)
             throw new Exception("Invalid octal escape sequence length: " + len);
         int uc = readUnicodeChar(token, 1, len, 8);
         if (uc > 255) //octal377
             throw new Exception("Octal escape sequence must be in range [0, 377].");
         return (char)uc;
     }
     throw new Exception("Unsupported character: \\" + token);
 }
Exemple #43
0
            protected override object Read(PushbackTextReader r, char backslash, object opts)
            {
                int ch = r.Read();

                if (ch == -1)
                {
                    throw new EndOfStreamException("EOF while reading character");
                }
                String token = readSimpleToken(r, (char)ch, false);

                if (token.Length == 1)
                {
                    return(token[0]);
                }
                else if (token.Equals("newline"))
                {
                    return('\n');
                }
                else if (token.Equals("space"))
                {
                    return(' ');
                }
                else if (token.Equals("tab"))
                {
                    return('\t');
                }
                else if (token.Equals("backspace"))
                {
                    return('\b');
                }
                else if (token.Equals("formfeed"))
                {
                    return('\f');
                }
                else if (token.Equals("return"))
                {
                    return('\r');
                }
                else if (token.StartsWith("u"))
                {
                    char c = (char)readUnicodeChar(token, 1, 4, 16);
                    if (c >= '\uD800' && c <= '\uDFFF') // surrogate code unit?
                    {
                        throw new InvalidOperationException("Invalid character constant: \\u" + ((int)c).ToString("x"));
                    }
                    return(c);
                }
                else if (token.StartsWith("o"))
                {
                    int len = token.Length - 1;
                    if (len > 3)
                    {
                        throw new InvalidOperationException("Invalid octal escape sequence length: " + len);
                    }
                    int uc = readUnicodeChar(token, 1, len, 8);
                    if (uc > 255) //octal377
                    {
                        throw new InvalidOperationException("Octal escape sequence must be in range [0, 377].");
                    }
                    return((char)uc);
                }
                throw new InvalidOperationException("Unsupported character: \\" + token);
            }
Exemple #44
0
 protected override object Read(PushbackTextReader r, char hash)
 {
     int ch = r.Read();
     if (ch == -1)
         throw new EndOfStreamException("EOF while reading character");
     IFn fn = _dispatchMacros[ch];
     if (fn == null)
         throw new Exception(String.Format("No dispatch macro for: {0}", (char)ch));
     return fn.invoke(r, (char)ch);
 }
Exemple #45
0
            protected override object Read(PushbackTextReader r, char doublequote, object opts)
            {
                StringBuilder sb = new StringBuilder();

                for (int ch = r.Read(); ch != '"'; ch = r.Read())
                {
                    if (ch == -1)
                    {
                        throw new EndOfStreamException("EOF while reading string");
                    }
                    if (ch == '\\')     //escape
                    {
                        ch = r.Read();
                        if (ch == -1)
                        {
                            throw new EndOfStreamException("EOF while reading string");
                        }
                        switch (ch)
                        {
                        case 't':
                            ch = '\t';
                            break;

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

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

                        case '\\':
                            break;

                        case '"':
                            break;

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

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

                        case 'u':
                            ch = r.Read();
                            if (CharValueInRadix(ch, 16) == -1)
                            {
                                throw new InvalidOperationException("Invalid unicode escape: \\u" + (char)ch);
                            }
                            ch = readUnicodeChar((PushbackTextReader)r, ch, 16, 4, true);
                            break;

                        default:
                        {
                            //if (CharValueInRadix(ch, 8) != -1)  -- this is correct, but we end up with different error message for 8,9 than JVM, so do the following to match:
                            if (Char.IsDigit((char)ch))
                            {
                                ch = readUnicodeChar((PushbackTextReader)r, ch, 8, 3, false);
                                if (ch > 255)         //octal377
                                {
                                    throw new InvalidOperationException("Octal escape sequence must be in range [0, 377].");
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException("Unsupported escape character: \\" + (char)ch);
                            }
                        }
                        break;
                        }
                    }
                    sb.Append((char)ch);
                }
                return(sb.ToString());
            }
Exemple #46
0
            protected override object Read(PushbackTextReader r, char doublequote)
            {
                StringBuilder sb = new StringBuilder();

                for (int ch = r.Read(); ch != '"'; ch = r.Read())
                {
                    if (ch == -1)
                        throw new EndOfStreamException("EOF while reading string");
                    if (ch == '\\')	//escape
                    {
                        ch = r.Read();
                        if (ch == -1)
                            throw new EndOfStreamException("EOF while reading string");
                        switch (ch)
                        {
                            case 't':
                                ch = '\t';
                                break;
                            case 'r':
                                ch = '\r';
                                break;
                            case 'n':
                                ch = '\n';
                                break;
                            case '\\':
                                break;
                            case '"':
                                break;
                            case 'b':
                                ch = '\b';
                                break;
                            case 'f':
                                ch = '\f';
                                break;
                            case 'u':
                                ch = r.Read();
                                if (CharValueInRadix(ch, 16) == -1)
                                    throw new Exception("Invalid unicode escape: \\u" + (char)ch);
                                ch = readUnicodeChar((PushbackTextReader)r, ch, 16, 4, true);
                                break;
                            default:
                                {
                                    if (CharValueInRadix(ch, 8) != -1)
                                    {
                                        ch = readUnicodeChar((PushbackTextReader)r, ch, 8, 3, false);
                                        if (ch > 255) //octal377
                                            throw new Exception("Octal escape sequence must be in range [0, 377].");
                                    }
                                    else
                                        throw new Exception("Unsupported escape character: \\" + (char)ch);
                                }
                                break;
                        }
                    }
                    sb.Append((char)ch);
                }
                return sb.ToString();
            }
Exemple #47
0
        public static object read(PushbackTextReader r,
            bool eofIsError,
            object eofValue,
            bool isRecursive)
        {
            if (UNKNOWN.Equals(RT.ReadEvalVar.deref()))
                throw new InvalidOperationException("Reading disallowed - *read-eval* bound to :unknown");

            try
            {
                for (; ; )
                {
                    int ch = r.Read();

                    while (isWhitespace(ch))
                        ch = r.Read();

                    if (ch == -1)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading");
                        return eofValue;
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return RT.suppressRead() ? null : n;
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch);
                        if (RT.suppressRead())
                            return null;
                        // no op macros return the reader
                        if (ret == r)
                            continue;
                        return ret;
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return RT.suppressRead() ? null : n;
                        }
                        Unread(r, ch2);
                    }

                    //string token = readToken(r, (char)ch);
                    //return RT.suppressRead() ? null : interpretToken(token);
                    string rawToken;
                    string token;
                    string mask;
                    bool eofSeen;
                    readToken(r, (char)ch, out rawToken, out token, out mask, out eofSeen);
                    if (eofSeen)
                    {
                        if (eofIsError)
                            throw new EndOfStreamException("EOF while reading symbol");
                        return eofValue;
                    }
                    return RT.suppressRead() ? null : InterpretToken(rawToken, token, mask);
                }
            }
            catch (Exception e)
            {
                if (isRecursive)
                    throw;

                LineNumberingTextReader lntr = r as LineNumberingTextReader;
                if (lntr == null)
                    throw;

                throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
            }
        }