Skip to content

Commit 1eda738

Browse files
Revert "Keep a count of the number of open blocks."
This reverts commit c72487d.
1 parent 0656bf9 commit 1eda738

File tree

6 files changed

+6
-94
lines changed

6 files changed

+6
-94
lines changed

extensions/table.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,12 @@ static cmark_node *try_opening_table_header(cmark_syntax_extension *self,
311311
}
312312
}
313313

314-
assert(cmark_node_get_type(parent_container) == CMARK_NODE_PARAGRAPH);
315314
if (!cmark_node_set_type(parent_container, CMARK_NODE_TABLE)) {
316315
free_table_row(parser->mem, header_row);
317316
free_table_row(parser->mem, marker_row);
318317
return parent_container;
319318
}
320319

321-
// Update the node counts after parent_container changed type.
322-
assert(parent_container->next == NULL);
323-
decr_open_block_count(parser, CMARK_NODE_PARAGRAPH);
324-
incr_open_block_count(parser, CMARK_NODE_TABLE);
325-
326320
if (header_row->paragraph_offset) {
327321
try_inserting_table_header_paragraph(parser, parent_container, (unsigned char *)parent_string,
328322
header_row->paragraph_offset);

src/blocks.c

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,6 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
7070
static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
7171
bufsize_t bytes);
7272

73-
static void subtract_open_block_counts(cmark_parser *parser, cmark_node *node) {
74-
do {
75-
decr_open_block_count(parser, S_type(node));
76-
node->flags &= ~CMARK_NODE__OPEN_BLOCK;
77-
node = node->last_child;
78-
} while (node);
79-
}
80-
81-
static void add_open_block_counts(cmark_parser *parser, cmark_node *node) {
82-
do {
83-
incr_open_block_count(parser, S_type(node));
84-
node->flags |= CMARK_NODE__OPEN_BLOCK;
85-
node = node->last_child;
86-
} while (node);
87-
}
88-
8973
static cmark_node *make_block(cmark_mem *mem, cmark_node_type tag,
9074
int start_line, int start_column) {
9175
cmark_node *e;
@@ -145,7 +129,6 @@ static void cmark_parser_reset(cmark_parser *parser) {
145129
parser->refmap = cmark_reference_map_new(parser->mem);
146130
parser->root = document;
147131
parser->current = document;
148-
add_open_block_counts(parser, document);
149132

150133
parser->syntax_extensions = saved_exts;
151134
parser->inline_syntax_extensions = saved_inline_exts;
@@ -327,12 +310,6 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
327310
has_content = resolve_reference_link_definitions(parser, b);
328311
if (!has_content) {
329312
// remove blank node (former reference def)
330-
if (b->flags & CMARK_NODE__OPEN_BLOCK) {
331-
decr_open_block_count(parser, S_type(b));
332-
if (b->prev) {
333-
add_open_block_counts(parser, b->prev);
334-
}
335-
}
336313
cmark_node_free(b);
337314
}
338315
break;
@@ -405,15 +382,6 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
405382
return parent;
406383
}
407384

408-
// Recalculates the number of open blocks. Returns true if it matches what's currently stored
409-
// in parser. (Used to check that the counts in parser, which are updated incrementally, are
410-
// correct.)
411-
bool check_open_block_counts(cmark_parser *parser) {
412-
cmark_parser tmp_parser = {0}; // Only used for its open_block_counts field.
413-
add_open_block_counts(&tmp_parser, parser->root);
414-
return memcmp(tmp_parser.open_block_counts, parser->open_block_counts, sizeof(parser->open_block_counts)) == 0;
415-
}
416-
417385
// Add a node as child of another. Return pointer to child.
418386
static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
419387
cmark_node_type block_type, int start_column) {
@@ -432,14 +400,11 @@ static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
432400
if (parent->last_child) {
433401
parent->last_child->next = child;
434402
child->prev = parent->last_child;
435-
subtract_open_block_counts(parser, parent->last_child);
436403
} else {
437404
parent->first_child = child;
438405
child->prev = NULL;
439406
}
440407
parent->last_child = child;
441-
add_open_block_counts(parser, child);
442-
443408
return child;
444409
}
445410

@@ -1083,8 +1048,6 @@ static cmark_node *check_open_blocks(cmark_parser *parser, cmark_chunk *input,
10831048
cmark_node *container = parser->root;
10841049
cmark_node_type cont_type;
10851050

1086-
assert(check_open_block_counts(parser));
1087-
10881051
while (S_last_child_is_open(container)) {
10891052
container = container->last_child;
10901053
cont_type = S_type(container);
@@ -1230,9 +1193,8 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
12301193
has_content = resolve_reference_link_definitions(parser, *container);
12311194

12321195
if (has_content) {
1233-
cmark_node_set_type(*container, CMARK_NODE_HEADING);
1234-
decr_open_block_count(parser, CMARK_NODE_PARAGRAPH);
1235-
incr_open_block_count(parser, CMARK_NODE_HEADING);
1196+
1197+
(*container)->type = (uint16_t)CMARK_NODE_HEADING;
12361198
(*container)->as.heading.level = lev;
12371199
(*container)->as.heading.setext = true;
12381200
S_advance_offset(parser, input, input->len - 1 - parser->offset, false);
@@ -1516,7 +1478,6 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
15161478

15171479
parser->line_number++;
15181480

1519-
assert(parser->current->next == NULL);
15201481
last_matched_container = check_open_blocks(parser, &input, &all_matched);
15211482

15221483
if (!last_matched_container)

src/cmark-gfm.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ char *cmark_markdown_to_html(const char *text, size_t len, int options);
3737
#define CMARK_NODE_TYPE_MASK (0xc000)
3838
#define CMARK_NODE_VALUE_MASK (0x3fff)
3939

40-
/**
41-
* This is the maximum number of block types (CMARK_NODE_DOCUMENT,
42-
* CMARK_NODE_HEADING, ...). It needs to be bigger than the number of
43-
* hardcoded block types (below) to allow for extensions (see
44-
* cmark_syntax_extension_add_node). But it also determines the size of the
45-
* open_block_counts array in the cmark_parser struct, so we don't want it
46-
* to be excessively large.
47-
*/
48-
#define CMARK_NODE_TYPE_BLOCK_LIMIT 0x20
49-
5040
typedef enum {
5141
/* Error status */
5242
CMARK_NODE_NONE = 0x0000,

src/node.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ typedef struct {
5050

5151
enum cmark_node__internal_flags {
5252
CMARK_NODE__OPEN = (1 << 0),
53-
CMARK_NODE__OPEN_BLOCK = (1 << 1),
54-
CMARK_NODE__LAST_LINE_BLANK = (1 << 2),
55-
CMARK_NODE__LAST_LINE_CHECKED = (1 << 3),
53+
CMARK_NODE__LAST_LINE_BLANK = (1 << 1),
54+
CMARK_NODE__LAST_LINE_CHECKED = (1 << 2),
5655

5756
// Extensions can register custom flags by calling `cmark_register_node_flag`.
5857
// This is the starting value for the custom flags.
59-
CMARK_NODE__REGISTER_FIRST = (1 << 4),
58+
CMARK_NODE__REGISTER_FIRST = (1 << 3),
6059
};
6160

6261
typedef uint16_t cmark_node_internal_flags;

src/parser.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,8 @@ struct cmark_parser {
5050
cmark_llist *syntax_extensions;
5151
cmark_llist *inline_syntax_extensions;
5252
cmark_ispunct_func backslash_ispunct;
53-
54-
/**
55-
* The "open" blocks are the blocks visited by the loop in
56-
* check_open_blocks (blocks.c). I.e. the blocks in this list:
57-
*
58-
* parser->root->last_child->...->last_child
59-
*
60-
* open_block_counts is used to keep track of how many of each type of
61-
* node are currently in the open blocks list. Knowing these counts can
62-
* sometimes help to end the loop in check_open_blocks early, improving
63-
* efficiency.
64-
*
65-
* The count is stored at this offset: type - CMARK_NODE_TYPE_BLOCK - 1
66-
* For example, CMARK_NODE_LIST (0x8003) is stored at offset 2.
67-
*/
68-
size_t open_block_counts[CMARK_NODE_TYPE_BLOCK_LIMIT];
6953
};
7054

71-
static CMARK_INLINE void incr_open_block_count(cmark_parser *parser, cmark_node_type type) {
72-
assert(type > CMARK_NODE_TYPE_BLOCK);
73-
assert(type <= CMARK_NODE_TYPE_BLOCK + CMARK_NODE_TYPE_BLOCK_LIMIT);
74-
parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1]++;
75-
}
76-
77-
static CMARK_INLINE void decr_open_block_count(cmark_parser *parser, cmark_node_type type) {
78-
assert(type > CMARK_NODE_TYPE_BLOCK);
79-
assert(type <= CMARK_NODE_TYPE_BLOCK + CMARK_NODE_TYPE_BLOCK_LIMIT);
80-
assert(parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1] > 0);
81-
parser->open_block_counts[type - CMARK_NODE_TYPE_BLOCK - 1]--;
82-
}
83-
8455
#ifdef __cplusplus
8556
}
8657
#endif

src/syntax_extension.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ cmark_syntax_extension *cmark_syntax_extension_new(const char *name) {
2929
cmark_node_type cmark_syntax_extension_add_node(int is_inline) {
3030
cmark_node_type *ref = !is_inline ? &CMARK_NODE_LAST_BLOCK : &CMARK_NODE_LAST_INLINE;
3131

32-
if ((*ref & CMARK_NODE_VALUE_MASK) >= CMARK_NODE_TYPE_BLOCK_LIMIT) {
33-
// This assertion will fail if you try to register more extensions than
34-
// are currently allowed by CMARK_NODE_TYPE_BLOCK_MAXNUM. Try increasing
35-
// the limit.
32+
if ((*ref & CMARK_NODE_VALUE_MASK) == CMARK_NODE_VALUE_MASK) {
3633
assert(false);
3734
return (cmark_node_type) 0;
3835
}

0 commit comments

Comments
 (0)