Example #1
0
File: _ast.py Project: dreid/edn
def unparse(obj):
    """Turn an abstract edn element into a string.

    Returns a valid edn string representing 'obj'.
    """
    builder = _Builder()
    return coerceToTerm(obj).build(builder)
 def test_coerce(self):
     self.assertEqual(
         coerceToTerm({
             3: 4,
             "a": character('x'),
             (2, 3): [4, 5]
         }), parseTerm('{"a": \'x\', 3: 4, [2, 3]: [4, 5]}'))
Example #3
0
 def rule_unknown_term(self):
     tt, _ = self.rule_anything()
     oldInput = self.input
     self.input = InputStream.fromIterable(tt.args)
     newargs, e = self.many(self.rule_transform)
     self.end()
     self.input = oldInput
     return Term(tt.tag, None, tuple(coerceToTerm(a) for a in newargs)), e
Example #4
0
 def rule_unknown_term(self):
     tt, _ = self.rule_anything()
     oldInput = self.input
     self.input = InputStream.fromIterable(tt.args)
     newargs, e = self.many(self.rule_transform)
     self.end()
     self.input = oldInput
     return Term(tt.tag, None, tuple(coerceToTerm(a) for a in newargs)), e
Example #5
0
File: _ast.py Project: dreid/edn
def unparse_stream(input_elements, output_stream):
    """Write abstract edn elements out as edn to a file-like object.

    Elements will be separated by UNIX newlines.  This may change in future
    versions.
    """
    separator = u'\n'.encode('utf8')
    builder = _Builder()
    for element in input_elements:
        output_stream.write(coerceToTerm(element).build(builder))
        output_stream.write(separator)
Example #6
0
def coerceToQuasiMatch(val, isFunctorHole, tag):
    if isFunctorHole:
        if val is None:
            result = Term(Tag("null"), None, None)
        elif isinstance(val, Term):
            if len(val.args) != 0:
                return None
            else:
                result = val
        elif isinstance(val, basestring):
            result = Term(Tag(val), None, None)
        elif isinstance(val, bool):
            result = Term(Tag(["false", "true"][val]), None, None)
        else:
            return None
    else:
        result = coerceToTerm(val)
    if tag is not None and result.tag != tag:
        return None
    return result
Example #7
0
def coerceToQuasiMatch(val, isFunctorHole, tag):
    if isFunctorHole:
        if val is None:
            result = Term(Tag("null"), None, None, None)
        elif isinstance(val, Term):
            if len(val.args) != 0:
                return None
            else:
                result = val
        elif isinstance(val, basestring):
            result = Term(Tag(val), None, None, None)
        elif isinstance(val, bool):
            result = Term(Tag(["false", "true"][val]), None, None, None)
        else:
            return None
    else:
        result = coerceToTerm(val)
    if tag is not None and result.tag != tag:
        return None
    return result
Example #8
0
 def backpatch(self, fromIdx, toIdx):
     old = self.instrs[fromIdx]
     self.instrs[fromIdx] = Term(old.tag, None, [coerceToTerm(toIdx)])
Example #9
0
 def test_coerce(self):
     self.assertEqual(
         coerceToTerm({3: 4, "a": character('x'), (2, 3): [4, 5]}),
         parseTerm('{"a": \'x\', 3: 4, [2, 3]: [4, 5]}'))
Example #10
0
 def backpatch(self, fromIdx, toIdx):
     old = self.instrs[fromIdx]
     self.instrs[fromIdx] = Term(old.tag, None, [coerceToTerm(toIdx)])