Наследование: System.IO.TextReader, IDisposable
Пример #1
0
            protected override object Read(PushbackTextReader r, char quote)
            {
                //object o = read(r, true, null, true);
                object o = ReadAux(r);

                return(RT.list(_sym, o));
            }
Пример #2
0
            private static object ReadTagged(PushbackTextReader r, Symbol tag, IPersistentMap opts)
            {
                object o = ReadAux(r, opts);

                ILookup readers    = (ILookup)RT.get(opts, READERS);
                IFn     dataReader = (IFn)RT.get(readers, tag);

                if (dataReader == null)
                {
                    dataReader = (IFn)RT.get(RT.DefaultDataReadersVar.deref(), tag);
                }
                if (dataReader == null)
                {
                    IFn defaultReader = (IFn)RT.get(opts, DEFAULT);
                    if (defaultReader != null)
                    {
                        return(defaultReader.invoke(tag, o));
                    }
                    else
                    {
                        throw new InvalidOperationException("No reader function for tag " + tag.ToString());
                    }
                }
                else
                {
                    return(dataReader.invoke(o));
                }
            }
Пример #3
0
 static void Unread(PushbackTextReader r, int ch)
 {
     if (ch != -1)
     {
         r.Unread(ch);
     }
 }
Пример #4
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: " + (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);
        }
Пример #5
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);
        }
Пример #6
0
            protected override object Read(PushbackTextReader r, char leftparen)
            {
                int line = -1;

                if (r is LineNumberingTextReader)
                {
                    line = ((LineNumberingTextReader)r).LineNumber;
                }
                IList <Object> list = readDelimitedList(')', r, true);

                if (list.Count == 0)
                {
                    return(PersistentList.EMPTY);
                }
                IObj s = (IObj)PersistentList.create((IList)list);

                //		IObj s = (IObj) RT.seq(list);
                if (line != -1)
                {
                    return(s.withMeta(RT.map(RT.LINE_KEY, line)));
                }
                else
                {
                    return(s);
                }
            }
Пример #7
0
        public object LoadFromStream(PushbackTextReader rdr, bool addPrint)
        {
            object ret = null;
            object eofVal = new object();
            object form;
            while ((form = LispReader.read(rdr, false, eofVal, false)) != eofVal)
            {
                try
                {
                    LambdaExpression ast = Compiler.GenerateLambda(form, addPrint);
                    ret = ast.Compile().DynamicInvoke();
                }
                catch (Exception ex)
                {
                    if (addPrint)
                    {
                        Exception root = ex;
                        while (root.InnerException != null)
                            root = root.InnerException;

                        Console.WriteLine("Error evaluating {0}: {1}", form, root.Message);
                        Console.WriteLine(root.StackTrace);
                    }
                }
            }
            return ret;
        }
Пример #8
0
 protected override object Read(PushbackTextReader r, char leftbrace, object opts)
 {
     Object[] a = ReadDelimitedList('}', r, true, opts).ToArray();
     if ((a.Length & 1) == 1)
     {
         throw new ArgumentException("Map literal must contain an even number of forms");
     }
     return(RT.map(a));
 }
Пример #9
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);
        }
Пример #10
0
            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);
            }
Пример #11
0
            protected override object Read(PushbackTextReader r, char leftparen, object opts)
            {
                object name = read(r, true, null, false, opts);
                Symbol sym  = name as Symbol;

                if (sym == null)
                {
                    throw new InvalidOperationException("Reader tag must be a symbol");
                }
                return(ReadTagged(r, sym, (IPersistentMap)opts));
            }
Пример #12
0
            protected override object Read(PushbackTextReader r, char quote)
            {
                //object o = read(r, true, null, true);
                object o = ReadAux(r);

                //		if(o instanceof Symbol)
                //			{
                //			Object v = Compiler.maybeResolveIn(Compiler.currentNS(), (Symbol) o);
                //			if(v instanceof Var)
                //				return v;
                //			}
                return(RT.list(THE_VAR, o));
            }
Пример #13
0
 protected override object Read(PushbackTextReader r, char backquote)
 {
     try
     {
         Var.pushThreadBindings(RT.map(GENSYM_ENV, PersistentHashMap.EMPTY));
         //object form = read(r, true, null, true);
         object form = ReadAux(r);
         return(syntaxQuote(form));
     }
     finally
     {
         Var.popThreadBindings();
     }
 }
Пример #14
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));
            }
Пример #15
0
        internal object ParseFile()
        {
            IPersistentVector pv = PersistentVector.EMPTY;

            StringReader sr = new StringReader(_text);
            PushbackTextReader pr = new PushbackTextReader(sr);

            pv = pv.cons(Compiler.DO);

            object eofVal = new object();
            object form;
            while ((form = LispReader.read(pr, false, eofVal, false)) != eofVal)
                pv = pv.cons(form);

            return pv.seq();
        }
Пример #16
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);
            }
        }
Пример #17
0
            //static ListReader _listReader = new ListReader();

            protected override object Read(PushbackTextReader r, char lparen)
            {
                if (ARG_ENV.deref() != null)
                {
                    throw new InvalidOperationException("Nested #()s are not allowed");
                }
                try
                {
                    Var.pushThreadBindings(RT.map(ARG_ENV, PersistentTreeMap.EMPTY));
                    r.Unread('(');
                    ////object form = ReadAux(r, true, null, true);
                    object form = ReadAux(r);
                    //object form = _listReader.invoke(r, '(');

                    IPersistentVector args    = PersistentVector.EMPTY;
                    PersistentTreeMap argsyms = (PersistentTreeMap)ARG_ENV.deref();
                    ISeq rargs = argsyms.rseq();
                    if (rargs != null)
                    {
                        int higharg = (int)((IMapEntry)rargs.first()).key();
                        if (higharg > 0)
                        {
                            for (int i = 1; i <= higharg; ++i)
                            {
                                object sym = argsyms.valAt(i);
                                if (sym == null)
                                {
                                    sym = garg(i);
                                }
                                args = args.cons(sym);
                            }
                        }
                        object restsym = argsyms.valAt(-1);
                        if (restsym != null)
                        {
                            args = args.cons(Compiler._AMP_);
                            args = args.cons(restsym);
                        }
                    }
                    return(RT.list(Compiler.FN, args, form));
                }
                finally
                {
                    Var.popThreadBindings();
                }
            }
Пример #18
0
            protected override object Read(PushbackTextReader r, char c, object opts)
            {
                object o = read(r, true, null, true, opts);

                Symbol oSym = o as Symbol;

                if (oSym == null)
                {
                    throw new Exception("Invalid token: ##" + o);
                }
                if (!_specials.containsKey(oSym))
                {
                    throw new Exception("Unknown symbolic value: ##" + o);
                }

                return(_specials.valAt(oSym));
            }
Пример #19
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);
        }
Пример #20
0
            protected override object Read(PushbackTextReader r, char eq)
            {
                if (!RT.booleanCast(RT.READEVAL.deref()))
                {
                    throw new Exception("EvalReader not allowed when *read-eval* is false.");
                }
                Object o = read(r, true, null, true);

                if (o is Symbol)
                {
                    return(RT.classForName(o.ToString()));
                }
                else if (o is IPersistentList)
                {
                    Symbol fs = (Symbol)RT.first(o);
                    if (fs.Equals(THE_VAR))
                    {
                        Symbol vs = (Symbol)RT.second(o);
                        return(RT.var(vs.Namespace, vs.Name));  //Compiler.resolve((Symbol) RT.second(o),true);
                    }
                    if (fs.Name.EndsWith("."))
                    {
                        Object[] args = RT.toArray(RT.next(o));
                        return(Reflector.InvokeConstructor(RT.classForName(fs.Name.Substring(0, fs.Name.Length - 1)), args));
                    }
                    if (Compiler.NamesStaticMember(fs))
                    {
                        Object[] args = RT.toArray(RT.next(o));
                        return(Reflector.InvokeStaticMethod(fs.Namespace, fs.Name, args));
                    }
                    Object v = Compiler.maybeResolveIn(Compiler.CurrentNamespace, fs);
                    if (v is Var)
                    {
                        return(((IFn)v).applyTo(RT.next(o)));
                    }
                    throw new Exception("Can't resolve " + fs);
                }
                else
                {
                    throw new ArgumentException("Unsupported #= form");
                }
            }
Пример #21
0
            protected override object Read(PushbackTextReader r, char caret)
            {
                int line = -1;

                if (r is LineNumberingTextReader)
                {
                    line = ((LineNumberingTextReader)r).LineNumber;
                }
                //object meta = read(r, true, null, true);
                object meta = ReadAux(r);

                if (meta is Symbol || meta is Keyword || meta is String)
                {
                    meta = RT.map(RT.TAG_KEY, meta);
                }
                else if (!(meta is IPersistentMap))
                {
                    throw new ArgumentException("Metadata must be Symbol,Keyword,String or Map");
                }

                //object o = read(r, true, null, true);
                object o = ReadAux(r);

                if (o is IMeta)
                {
                    if (line != -1 && o is ISeq)
                    {
                        meta = ((IPersistentMap)meta).assoc(RT.LINE_KEY, line);
                    }
                    if (o is IReference)
                    {
                        ((IReference)o).resetMeta((IPersistentMap)meta);
                        return(o);
                    }
                    return(((IObj)o).withMeta((IPersistentMap)meta));
                }
                else
                {
                    throw new ArgumentException("Metadata can only be applied to IMetas");
                }
            }
Пример #22
0
            protected override object Read(PushbackTextReader r, char leftparen, object opts)
            {
                //int startLine = -1;
                //int startCol = -1;
                //LineNumberingTextReader lntr = r as LineNumberingTextReader;

                //if (lntr != null)
                //{
                //    startLine = lntr.LineNumber;
                //    startCol = lntr.ColumnNumber;
                //}
                IList <Object> list = ReadDelimitedList(')', r, true, opts);

                if (list.Count == 0)
                {
                    return(PersistentList.EMPTY);
                }
                IObj s = (IObj)PersistentList.create((IList)list);

                return(s);
            }
Пример #23
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);
            }
        }
Пример #24
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));
            }
Пример #25
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()));
            }
Пример #26
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)));
            }
Пример #27
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));
                }
            }
Пример #28
0
 private static object ReadAux(PushbackTextReader r)
 {
     return read(r, true, null, true);
 }
Пример #29
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;
        }
Пример #30
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));
            }
Пример #31
0
 protected override object Read(PushbackTextReader r, char eq)
 {
     if (!RT.booleanCast(RT.READEVAL.deref()))
     {
         throw new Exception("EvalReader not allowed when *read-eval* is false.");
     }
     Object o = read(r, true, null, true);
     if (o is Symbol)
     {
         return RT.classForName(o.ToString());
     }
     else if (o is IPersistentList)
     {
         Symbol fs = (Symbol)RT.first(o);
         if (fs.Equals(THE_VAR))
         {
             Symbol vs = (Symbol)RT.second(o);
             return RT.var(vs.Namespace, vs.Name);  //Compiler.resolve((Symbol) RT.second(o),true);
         }
         if (fs.Name.EndsWith("."))
         {
             Object[] args = RT.toArray(RT.next(o));
             return Reflector.InvokeConstructor(RT.classForName(fs.Name.Substring(0, fs.Name.Length - 1)), args);
         }
         if (Compiler.NamesStaticMember(fs))
         {
             Object[] args = RT.toArray(RT.next(o));
             return Reflector.InvokeStaticMethod(fs.Namespace, fs.Name, args);
         }
         Object v = Compiler.maybeResolveIn(Compiler.CurrentNamespace, fs);
         if (v is Var)
         {
             return ((IFn)v).applyTo(RT.next(o));
         }
         throw new Exception("Can't resolve " + fs);
     }
     else
         throw new ArgumentException("Unsupported #= form");
 }
Пример #32
0
 protected override object Read(PushbackTextReader r, char underscore)
 {
     ReadAux(r);
     return r;
 }
Пример #33
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);
 }
Пример #34
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;
 }
Пример #35
0
 protected override object Read(PushbackTextReader r, char leftparen)
 {
     int line = -1;
     if (r is LineNumberingTextReader)
         line = ((LineNumberingTextReader)r).LineNumber;
     IList<Object> list = readDelimitedList(')', r, true);
     if (list.Count == 0)
         return PersistentList.EMPTY;
     IObj s = (IObj)PersistentList.create((IList)list);
     //		IObj s = (IObj) RT.seq(list);
     if (line != -1)
         return s.withMeta(RT.map(RT.LINE_KEY, line));
     else
         return s;
 }
Пример #36
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();
            }
Пример #37
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());
 }
Пример #38
0
            protected override object Read(PushbackTextReader r, char caret)
            {
                int line = -1;
                if (r is LineNumberingTextReader)
                    line = ((LineNumberingTextReader)r).LineNumber;
                //object meta = read(r, true, null, true);
                object meta = ReadAux(r);
                if (meta is Symbol || meta is Keyword || meta is String)
                    meta = RT.map(RT.TAG_KEY, meta);
                else if (!(meta is IPersistentMap))
                    throw new ArgumentException("Metadata must be Symbol,Keyword,String or Map");

                //object o = read(r, true, null, true);
                object o = ReadAux(r);
                if (o is IMeta)
                {
                    if (line != -1 && o is ISeq)
                        meta = ((IPersistentMap)meta).assoc(RT.LINE_KEY, line);
                    if (o is IReference)
                    {
                        ((IReference)o).resetMeta((IPersistentMap)meta);
                        return o;
                    }
                    return ((IObj)o).withMeta((IPersistentMap)meta);
                }
                else
                    throw new ArgumentException("Metadata can only be applied to IMetas");
            }
Пример #39
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;
            }
        }
Пример #40
0
 public static Object read(PushbackTextReader r, IPersistentMap opts)
 {
     return(read(r, !opts.containsKey(EOF), opts.valAt(EOF), false, opts));
 }
Пример #41
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;
        }
Пример #42
0
        static public Object readString(String s, IPersistentMap opts)
        {
            PushbackTextReader r = new PushbackTextReader(new System.IO.StringReader(s));

            return(read(r, opts));
        }
Пример #43
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);
            }
        }
Пример #44
0
 protected override object Read(PushbackTextReader r, char quote)
 {
     //object o = read(r, true, null, true);
     object o = ReadAux(r);
     return RT.list(_sym, o);
 }
Пример #45
0
 static void Unread(PushbackTextReader r, int ch)
 {
     if (ch != -1)
         r.Unread(ch);
 }
Пример #46
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));
            }
Пример #47
0
 protected override object Read(PushbackTextReader r, char semicolon)
 {
     int ch;
     do
     {
         ch = r.Read();
     } while (ch != -1 && ch != '\n' && ch != '\r');
     return r;
 }
Пример #48
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);
     }
 }
Пример #49
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);
 }
Пример #50
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);
            }
        }
Пример #51
0
            //static ListReader _listReader = new ListReader();
            protected override object Read(PushbackTextReader r, char lparen)
            {
                if (ARG_ENV.deref() != null)
                    throw new InvalidOperationException("Nested #()s are not allowed");
                try
                {
                    Var.pushThreadBindings(RT.map(ARG_ENV, PersistentTreeMap.EMPTY));
                    r.Unread('(');
                    ////object form = ReadAux(r, true, null, true);
                    object form = ReadAux(r);
                    //object form = _listReader.invoke(r, '(');

                    IPersistentVector args = PersistentVector.EMPTY;
                    PersistentTreeMap argsyms = (PersistentTreeMap)ARG_ENV.deref();
                    ISeq rargs = argsyms.rseq();
                    if (rargs != null)
                    {
                        int higharg = (int)((IMapEntry)rargs.first()).key();
                        if (higharg > 0)
                        {
                            for (int i = 1; i <= higharg; ++i)
                            {
                                object sym = argsyms.valAt(i);
                                if (sym == null)
                                    sym = garg(i);
                                args = args.cons(sym);
                            }
                        }
                        object restsym = argsyms.valAt(-1);
                        if (restsym != null)
                        {
                            args = args.cons(Compiler._AMP_);
                            args = args.cons(restsym);
                        }
                    }
                    return RT.list(Compiler.FN, args, form);
                }
                finally
                {
                    Var.popThreadBindings();
                }
            }
Пример #52
0
 protected override object Read(PushbackTextReader r, char underscore, object opts)
 {
     ReadAux(r, opts);
     return(r);
 }
Пример #53
0
 protected override object Read(PushbackTextReader r, char leftbrace)
 {
     //return PersistentHashMap.create(readDelimitedList('}', r, true));
     return RT.map(readDelimitedList('}', r, true).ToArray());
 }
Пример #54
0
 protected override object Read(PushbackTextReader r, char leftparen)
 {
     return LazilyPersistentVector.create(readDelimitedList(']', r, true));
 }
Пример #55
0
 protected abstract object Read(PushbackTextReader r, char c);
Пример #56
0
 protected override object Read(PushbackTextReader reader, char rightdelim)
 {
     throw new Exception("Unmatched delimiter: " + rightdelim);
 }
Пример #57
0
 protected override object Read(PushbackTextReader r, char leftbracket)
 {
     return PersistentHashSet.create1(readDelimitedList('}', r, true));
 }
Пример #58
0
 protected override object Read(PushbackTextReader reader, char leftangle)
 {
     throw new Exception("Unreadable form");
 }
Пример #59
0
 protected override object Read(PushbackTextReader r, char backquote)
 {
     try
     {
         Var.pushThreadBindings(RT.map(GENSYM_ENV, PersistentHashMap.EMPTY));
         //object form = read(r, true, null, true);
         object form = ReadAux(r);
         return syntaxQuote(form);
     }
     finally
     {
         Var.popThreadBindings();
     }
 }
Пример #60
0
 protected override object Read(PushbackTextReader r, char quote)
 {
     //object o = read(r, true, null, true);
     object o = ReadAux(r);
     //		if(o instanceof Symbol)
     //			{
     //			Object v = Compiler.maybeResolveIn(Compiler.currentNS(), (Symbol) o);
     //			if(v instanceof Var)
     //				return v;
     //			}
     return RT.list(THE_VAR, o);
 }