Example #1
0
def nest(words, fname):
    """Nest start/end tags into blocks recursively.
    """
    # stack = [Tag('-program')]
    stack = []

    def prstack():  # pragma:nocover
        """Print stack contents.
        """
        print "STACK:...."
        for _item in stack:
            print _item

    while words:
        word = words.pop(0)
        # print "\WORD:", word
        # prstack()

        if is_endtag(word):
            tagname = name(word)[3:]
            # print "REDUCE", tagname
            block = []
            while 1:
                item = stack.pop()
                found = isinstance(item, Tag) and item.matches(word)
                # print '    POPPED', item, 'FOUND' if found else ""
                if found:
                    stack.append(Block(item.name, item, block[::-1]))
                    break
                else:
                    block.append(item)
        elif is_tag(word):
            # print "SHIFT TAG", name(word)
            tag = make_tag(words, name(word), content(word), fname)
            stack.append(tag)
        else:
            # print "SHIFT VAL", word
            stack.append(Value(content(word)))

    return Block('program', Tag('program'), stack)
Example #2
0
 def matches(self, endtag):
     return name(endtag)[3:] == self.name