Exemplo n.º 1
0
        public static void REPL2(Stream input, Stream output, Stream err, IExecutor ex)
        {
            var codeReader   = new LineNumberingTextReader(new StreamReader(new PushbackInputStream(input)));
            var outputWriter = TextWriter.Synchronized(new StreamWriter(output));
            var errorWriter  = TextWriter.Synchronized(new StreamWriter(err));

            repl.invoke(codeReader, outputWriter, errorWriter, ex);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public void Setup()
 {
     _sr  = new StringReader(_sample);
     _rdr = new LineNumberingTextReader(_sr);
 }
Exemplo n.º 4
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 rawToken;
                    string token;
                    string mask;
                    bool   eofSeen;
                    readToken(r, (char)ch, true, 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);
            }
        }
Exemplo n.º 5
0
            protected override object Read(PushbackTextReader r, char caret, object opts)
            {
                int startLine = -1;
                int startCol  = -1;
                LineNumberingTextReader lntr = r as LineNumberingTextReader;

                if (lntr != null)
                {
                    startLine = lntr.LineNumber;
                    startCol  = lntr.ColumnNumber;
                }

                IPersistentMap metaAsMap;
                {
                    object meta = ReadAux(r, opts);

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

                object o = ReadAux(r, opts);

                if (o is IMeta)
                {
                    if (startLine != -1 && o is ISeq)
                    {
                        metaAsMap = metaAsMap.assoc(RT.LineKey, startLine)
                                    .assoc(RT.ColumnKey, startCol)
                                    .assoc(RT.SourceSpanKey, RT.map(
                                               RT.StartLineKey, startLine,
                                               RT.StartColumnKey, startCol,
                                               RT.EndLineKey, lntr.LineNumber,
                                               RT.EndColumnKey, lntr.ColumnNumber));
                    }

                    IReference iref = o as IReference;
                    if (iref != null)
                    {
                        iref.resetMeta(metaAsMap);
                        return(o);
                    }
                    object ometa = RT.meta(o);
                    for (ISeq s = RT.seq(metaAsMap); s != null; s = s.next())
                    {
                        IMapEntry kv = (IMapEntry)s.first();
                        ometa = RT.assoc(ometa, kv.key(), kv.val());
                    }
                    return(((IObj)o).withMeta((IPersistentMap)ometa));
                }
                else
                {
                    throw new ArgumentException("Metadata can only be applied to IMetas");
                }
            }