Пример #1
0
from node import NodeVisitor, ValueNode, ListNode, BinaryExpressionNode
from parser import atoms, precedence

atom_names = {v:"@%s" % k for (k,v) in atoms.iteritems()}

named_escapes = set(["\a", "\b", "\f", "\n", "\r", "\t", "\v"])

def escape(string, extras=""):
    rv = ""
    for c in string:
        if c in named_escapes:
            rv += c.encode("unicode_escape")
        elif c == "\\":
            rv += "\\\\"
        elif c < '\x20':
            rv += "\\x%02x" % ord(c)
        elif c in extras:
            rv += "\\" + c
        else:
            rv += c
    return rv.encode("utf8")


class ManifestSerializer(NodeVisitor):
    def __init__(self, skip_empty_data=False):
        self.skip_empty_data = skip_empty_data

    def serialize(self, root):
        self.indent = 2
        rv = "\n".join(self.visit(root))
        if rv[-1] != "\n":
Пример #2
0
from node import NodeVisitor, ValueNode, ListNode, BinaryExpressionNode
from parser import atoms, precedence

atom_names = {v: "@%s" % k for (k, v) in atoms.iteritems()}

named_escapes = set(["\a", "\b", "\f", "\n", "\r", "\t", "\v"])


def escape(string, extras=""):
    rv = ""
    for c in string:
        if c in named_escapes:
            rv += c.encode("unicode_escape")
        elif c == "\\":
            rv += "\\\\"
        elif c < '\x20':
            rv += "\\x%02x" % ord(c)
        elif c in extras:
            rv += "\\" + c
        else:
            rv += c
    return rv.encode("utf8")


class ManifestSerializer(NodeVisitor):
    def __init__(self, skip_empty_data=False):
        self.skip_empty_data = skip_empty_data

    def serialize(self, root):
        self.indent = 2
        rv = "\n".join(self.visit(root))