示例#1
0
文件: tree.py 项目: aecay/lovett
def _postprocess_parsed(l):
    if not isinstance(l[0], str):
        # Root node
        tree = None
        id = None
        try:
            while True:
                v = l.pop()
                if v[0] == 'ID':
                    id = v[1]
                elif v[0] == "METADATA":
                    # TODO
                    pass
                else:
                    if tree:
                        raise ParseError("Too many children of root node (or label-less node)")
                    tree = v
        except IndexError:
            pass
        try:
            r = _postprocess_parsed(tree)
            # TODO: We should instead insert a hash-based id.
            # TODO: think about the differece between id and fingerprint (for
            # backwards compatibility: fingerprint is the hash-based one,
            # which is better)
            r.metadata.id = id or ABSENT_ID
            return r
        except ParseError as e:
            print("error in id: %s" % id)
            raise e
    if len(l) < 2:
        raise ParseError("malformed tree: node has too few children: %s" % l)
    if isinstance(l[1], str):
        m = {}
        # Simple leaf
        if len(l) != 2:
            raise ParseError("malformed tree: leaf has too many children: %s" % l)
        label = l[0]
        text = l[1]
        if util.is_trace_string(l[1]):
            text, idx_type, index = util.label_and_index(text)
            if index is not None:
                m['INDEX'] = index
                m['IDX-TYPE'] = idx_type
        else:
            label, idx_type, index = util.label_and_index(label)
            if index is not None:
                m['INDEX'] = index
                m['IDX-TYPE'] = idx_type
        return Leaf(label, text, m)
    # Regular node
    m = {}
    label, idx_type, index = util.label_and_index(l[0])
    if index is not None:
        m['INDEX'] = index
        m['IDX-TYPE'] = idx_type
    return NonTerminal(label, map(lambda x: _postprocess_parsed(x), l[1:]), m)
示例#2
0
文件: tree.py 项目: aecay/lovett
 def __init__(self, label, metadata=None):
     self.parent = None
     self.metadata = Metadata(metadata or {})
     label, idxtype, idx = util.label_and_index(label)
     self._label = label
     if idx is not None:
         self.metadata.index = idx
         # TODO: what's the standard name
         self.metadata.idx_type = idxtype