# Questions about libcmark

**URL:** <https://talk.commonmark.org/t/questions-about-libcmark/4192>\
**Category:** Implementation\
**Created:** [June 6, 2022, 9:57pm UTC](https://talk.commonmark.org/t/questions-about-libcmark/4192 "2022-06-06T21:57:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HiPhish](https://talk.commonmark.org/user_avatar/talk.commonmark.org/hiphish/32/2542_2.png) [@HiPhish](https://talk.commonmark.org/u/HiPhish)\
**Post date:** [June 6, 2022, 9:57pm UTC](https://talk.commonmark.org/t/questions-about-libcmark/4192/1 "2022-06-06T21:57:26Z")

</div>

Hello,

I am in the process of creating a Common Lisp bindings library for the reference implementation and I have a few questions about libcmark. I am using the `cmark(3)` man page for reference. While I am at it, I also want to thank you for providing a man page, I hate how in recent years many projects host their documentation exclusively online instead.

First of all, there are a few node types which are just aliases for other types:

```auto
CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT,
CMARK_NODE_LAST_BLOCK = CMARK_NODE_THEMATIC_BREAK,
CMARK_NODE_FIRST_INLINE = CMARK_NODE_TEXT,
CMARK_NODE_LAST_INLINE = CMARK_NODE_IMAGE

```

What is up with these? Are they deprecated and only there for backwards compatibility? Are they subclasses? Which ones should I expose in my library?

Second, from the description of the iterator it sounds like depth-first order, starting at the root. Am I correct in my understanding?

Third, if I understand the streaming parser correctly, the it only differs from the simple parser in that it allows to feed the document piecewise to the parser. But we still need to feed the entire document into the parser before we can start parsing, right?

Fourth, what does node consolidation (`cmark_consolidate_text_nodes`) do?

Fifth, when does the `CMARK_EVENT_NONE` even occur? It is defined as one of the iterator event, but there is no description. Is it the initial event value of the iterator?

I think that’s all for now, I might have more questions later. The library is actually mostly done, I just need to write more tests for the parser and iron out some details. There are two systems (what other languages call packages): `libcmark` consists of low-level 1:1 binding with all the advantages and disadvantages, while `cmark` is a lispy high-level library. It uses native classes to represent nodes rather than wrapping C pointers to make memory management easier. When a native node gets garbage-collected I would have to free its corresponding foreign node, but there is no standard way of specifying finalizers in Common Lisp. There are ways around that, but in the end I found it less messy to just use libcmark for its parser and keep the rest native. If someone really wants a wrapper around foreign pointers he can build his own high-level system on top of `libcmark` and ignore `cmark`, hence why I split the system in two.

---

<div class="post-metadata">

**Author:** ![jgm](https://talk.commonmark.org/user_avatar/talk.commonmark.org/jgm/32/1360_2.png) [@jgm](https://talk.commonmark.org/u/jgm)\
**Post date:** [June 6, 2022, 11:42pm UTC](https://talk.commonmark.org/t/questions-about-libcmark/4192/2 "2022-06-06T23:42:52Z")

</div>

Alejandro Sanchez via CommonMark Discussion  
[noreply@talk.commonmark.org](mailto:noreply@talk.commonmark.org) writes:

> ```auto
> CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT,
> CMARK_NODE_LAST_BLOCK = CMARK_NODE_THEMATIC_BREAK,
> CMARK_NODE_FIRST_INLINE = CMARK_NODE_TEXT,
> CMARK_NODE_LAST_INLINE = CMARK_NODE_IMAGE
> 
> ```
> 
> What is up with these? Are they deprecated and only there for backwards compatibility? Are they subclasses? Which ones should I expose in my library?

They’re used to determine whether a node is block-level or  
inline-level. See e.g. S\_is\_block in src/node.c.

> Second, from the description of the iterator it sounds like depth-first order, starting at the root. Am I correct in my understanding?

Yes, basically, depending on what you mean by that.  
For each node you’ll get a CMARK\_EVENT\_ENTER event on that  
node, then it will descend to any children, and then when  
the children are all done you’ll get CMARK\_EVENT\_EXIT on the  
original node.

> Third, if I understand the streaming parser correctly, the it only differs from the simple parser in that it allows to feed the document piecewise to the parser. But we still need to feed the entire document into the parser before we can start parsing, right?

Well, with the streaming parser, some block-level parsing does actually  
get done every time you push in a chunk of text. But you won’t  
get a final result until you call cmark\_parser\_finish. This is  
because inline parsing can’t be done until we know what all the reference  
definitions are, and we don’t know this til we’ve seen the whole  
document.

> Fourth, what does node consolidation (`cmark_consolidate_text_nodes`) do?

It will combine two adjacent text nodes, e.g. with contents “FOO”  
and “BAR”, into a single text node with “FOOBAR”.

> Fifth, when does the `CMARK_EVENT_NONE` even occur? It is defined as one of the iterator event, but there is no description. Is it the initial event value of the iterator?

Exactly.

> I think that’s all for now, I might have more questions later. The library is actually mostly done, I just need to write more tests for the parser and iron out some details. There are two systems (what other languages call packages): `libcmark` consists of low-level 1:1 binding with all the advantages and disadvantages, while `cmark` is a lispy high-level library. It uses native classes to represent nodes rather than wrapping C pointers to make memory management easier. When a native node gets garbage-collected I would have to free its corresponding foreign node, but there is no standard way of specifying finalizers in Common Lisp. There are ways around that, but in the end I found it less messy to just use libcmark for its parser and keep the rest native. If someone really wants a wrapper around foreign pointers he can build his own high-level system on top of `libcmark` and ignore `cmark`, hence why I split the system in two.

Sounds good!

---

<div class="post-metadata">

**Author:** ![HiPhish](https://talk.commonmark.org/user_avatar/talk.commonmark.org/hiphish/32/2542_2.png) [@HiPhish](https://talk.commonmark.org/u/HiPhish)\
**Post date:** [June 7, 2022, 8:42pm UTC](https://talk.commonmark.org/t/questions-about-libcmark/4192/3 "2022-06-07T20:42:24Z")

</div>

Thank you for your answers

> [@jgm](#):
>
> It will combine two adjacent text nodes, e.g. with contents “FOO”  
> and “BAR”, into a single text node with “FOOBAR”.

After reading the source code it seems that the tree is mutated as the iterator walks over it and it will merge any number of adjacent sibling text nodes, not just two. The last node remains and the other ones are dropped from the tree. What happens if someone has references to those nodes? Do those become dangling pointers then?
