示例#1
0
def fnReader(rdr, lparen):
    """Read an anonymous function #() from reader

    rdr -- a read/unread-able object
    lparen -- ignored

    Return an IPersistentList"""
    if ARG_ENV.deref() is not None:
        raise IllegalStateException("Nested #()s are not allowed")
    with threadBindings(RT.map(ARG_ENV, EMPTY_MAP)):
        rdr.back()
        form = read(rdr, True, None, True)
        drefed = ARG_ENV.deref()
        sargs = sorted(list(filter(lambda x: x != -1, drefed)))
        args = []
        if len(sargs):
            for x in range(1, int(str(sargs[-1])) + 1):
                if x in drefed:
                    args.append(drefed[x])
                else:
                    args.append(garg(x))
            retsym = drefed[-1]
            if retsym is not None:
                args.append(_AMP_)
                args.append(retsym)
        vargs = RT.vector(*args)
    return RT.list(_FN_, vargs, form)
示例#2
0
def fnReader(rdr, lparen):
    from clojure.lang.persistenthashmap import EMPTY
    from clojure.lang.var import popThreadBindings, pushThreadBindings

    if ARG_ENV.deref() is not None:
        raise IllegalStateException("Nested #()s are not allowed")
    pushThreadBindings(RT.map(ARG_ENV, EMPTY))
    rdr.back()
    form = read(rdr, True, None, True)
    drefed = ARG_ENV.deref()
    sargs = sorted(list(filter(lambda x: x != -1, drefed)))
    args = []
    if len(sargs):
        for x in range(1, int(str(sargs[-1])) + 1):
            if x in drefed:
                args.append(drefed[x])
            else:
                args.append(garg(x))
        retsym = drefed[-1]
        if retsym is not None:
            args.append(_AMP_)
            args.append(retsym)

    vargs = RT.vector(*args)
    popThreadBindings()
    return RT.list(_FN_, vargs, form)
示例#3
0
def metaReader(rdr, caret):
    from clojure.lang.symbol import Symbol
    from clojure.lang.cljkeyword import Keyword, TAG_KEY, T
    from clojure.lang.ipersistentmap import IPersistentMap
    line = rdr.lineCol()[0]
    meta = read(rdr, True, None, True)
    if isinstance(meta, Symbol) or isinstance(meta, str):
        meta = RT.map(TAG_KEY, meta)
    elif isinstance(meta, Keyword):
        meta = RT.map(meta, T)
    elif not isinstance(meta, IPersistentMap):
        raise ReaderException("Metadata must be Symbol,Keyword,String or Map")
    o = read(rdr, True, None, True)
    if not hasattr(o, "withMeta"):
        raise ReaderException("Cannot attach meta to a object without .withMeta")
    return o.withMeta(meta)
示例#4
0
 def __call__(self, r, backquote):
     pushThreadBindings(RT.map(GENSYM_ENV, EMPTY_MAP))
     try:
         self.rdr = r
         form = read(r, True, None, True)
         return self.syntaxQuote(form)
     finally:
         popThreadBindings()
示例#5
0
def varQuoteReader(rdr, singlequote):
    """Return the list (var next-object-read)

    rdr -- read/unread-able object
    singlequote -- ignored"""
    line = rdr.lineCol()[0]
    form = read(rdr, True, None, True)
    return RT.list(_VAR_, form).withMeta(RT.map(LINE_KEY, line))
示例#6
0
 def __call__(self, r, backquote):
     pushThreadBindings(RT.map(GENSYM_ENV, EMPTY_MAP))
     try:
         self.rdr = r
         form = read(r, True, None, True)
         return self.syntaxQuote(form)
     finally:
         popThreadBindings()
示例#7
0
def listReader(rdr, leftparen):
    """Read and return a possibly empty list () from rdr.

    rdr -- a read/unread-able object
    leftparen -- ignored"""
    startline = rdr.lineCol()[0]
    lst = readDelimitedList(')', rdr, True)
    lst = apply(RT.list, lst)
    return lst.withMeta(RT.map(LINE_KEY, startline))
示例#8
0
def metaReader(rdr, caret):
    """Read two objects from rdr. Return second with first as meta data.

    rdr -- read/unread-able object
    caret -- ignored

    May raise ReaderException."""
    line = rdr.lineCol()[0]
    meta = read(rdr, True, None, True)
    if isinstance(meta, (str, Symbol)):
        meta = RT.map(TAG_KEY, meta)
    elif isinstance(meta, Keyword):
        meta = RT.map(meta, T)
    elif not isinstance(meta, IPersistentMap):
        raise ReaderException("Metadata must be Symbol,Keyword,String or Map", rdr)
    o = read(rdr, True, None, True)
    if not hasattr(o, "withMeta"):
        # can't attach rdr to the exception here as it would point
        # to the *end* of the object just read'
        raise ReaderException("Cannot attach meta to a object without" " .withMeta")
    return o.withMeta(meta)
示例#9
0
def metaReader(rdr, caret):
    """Read two objects from rdr. Return second with first as meta data.

    rdr -- read/unread-able object
    caret -- ignored

    May raise ReaderException."""
    line = rdr.lineCol()[0]
    meta = read(rdr, True, None, True)
    if isinstance(meta, (str, Symbol)):
        meta = RT.map(TAG_KEY, meta)
    elif isinstance(meta, Keyword):
        meta = RT.map(meta, T)
    elif not isinstance(meta, IPersistentMap):
        raise ReaderException("Metadata must be Symbol,Keyword,String or Map",
                              rdr)
    o = read(rdr, True, None, True)
    if not hasattr(o, "withMeta"):
        # can't attach rdr to the exception here as it would point
        # to the *end* of the object just read'
        raise ReaderException("Cannot attach meta to a object without"
                              " .withMeta")
    return o.withMeta(meta)
示例#10
0
def varQuoteReader(rdr, singlequote):
    line = rdr.lineCol()[0]
    form = read(rdr, True, None, True)
    return RT.list(_VAR_, form).withMeta(RT.map(LINE_KEY, line))
示例#11
0
def listReader(rdr, leftparen):
    startline = rdr.lineCol()[0]
    lst = readDelimitedList(')', rdr, True)
    lst = apply(RT.list, lst)
    return lst.withMeta(RT.map(LINE_KEY, startline))
示例#12
0
 def __call__(self, r, backquote):
     with threadBindings(RT.map(GENSYM_ENV, EMPTY_MAP)):
         self.rdr = r
         form = read(r, True, None, True)
         return self.syntaxQuote(form)