[ create a new paste ] login | about

Link: http://codepad.org/F7SaXBiD    [ raw code | fork ]

C, pasted on Oct 31:
/* compact stack (remove NULL holes) and insert one node into another */
static void stack_telescope(node_ptr first, node_ptr *s)
{
        node_ptr *t = s - 1;
        DBG("telescope from rw %d", s - h_stack);
        for (; s <= h_pos; s++) {
                if (!*s)
                        continue;
                n_insert(first, *s);
                first = *++t = *s;
        }
        h_pos = t;
}

static void hew_stacks(node_ptr format, node_ptr *s, node_ptr *f)
{
        for (; s <= h_pos; s++) {
                if (!n_is(*s, IS_FORMAT|IS_PHRASING)) {
                        /* furthest block separator */
                        n_cut(*s);
                        n_insert_between(n_dup(format), *s);
                } else if (!n_is(*s, ON_FORMAT)) {
                        /* element not on fstack */
                        n_unmark(*s, ON_STACK);
                        s[0] = NULL;
                } else {
                        /* element on both fstack and stack */
                        *f++ = *s = n_dup(*s);
                        n_mark(*s, ON_STACK|ON_FORMAT);
                }
        }
}

static void close_format_tag(uns tag)
{
        node_ptr *f, *s;

        /* lookup tags on both stacks */
        f = fstack_lookup_scope(tag);
        if (!f) {
                parser_error("close_format: tag not on format stack");
                return;
        }
        s = stack_lookup_scope(tag);
        if (!s) {
                parser_error("close_format: tag not on html stack");
                s = stack_lookup(tag);
                if (!s)
                        fstack_remove(f);
                return;
        }

        /* f and s are in scope, find lowest furthest block separator */
        while (s <= h_pos && n_is(h_pos[0], IS_FORMAT | IS_PHRASING))
                stack_pop();

        if (s > h_pos) {
                /* no furthest block */
                fstack_remove(f);
                return;
        }

        /* hew stack and duplicate nodes in between */
        hew_stacks(s[0], s+1, f+1);

        /* remove closed tag and coresponding formatting element */
        n_unmark(s[0], ON_STACK);
        s[0] = NULL;
        fstack_remove(f);

        /* telescope rest of the stack into common ancestor */
        stack_telescope(s[-1], s);
}


Create a new paste based on this one


Comments: