tbsp - tree-based source-processing language tbsp is an awk-like language that operates on tree-sitter syntax trees. to motivate the need for such a program, we could begin by writing a markdown-to-html converter using tbsp and tree-sitter-md [0]. we need some markdown to begin with: # 1 heading content of first paragraph ## 1.1 heading content of nested paragraph for future reference, this markdown is parsed like so by tree-sitter-md (visualization generated by tree-viz [1]): document | section | | atx_heading | | | atx_h1_marker "#" | | | heading_content inline "1 heading" | | paragraph | | | inline "content of first paragraph" | | section | | | atx_heading | | | | atx_h2_marker "##" | | | | heading_content inline "1.1 heading" | | | paragraph | | | | inline "content of nested paragraph" onto the converter itself. every tbsp program is written as a collection of stanzas. typically, we start with a stanza like so: BEGIN { int depth = 0; print("\n"); print("
\n"); } the stanza begins with a "pattern", in this case, "BEGIN", and is followed a block of code. this block specifically, is executed right at the beginning, before traversing the parse tree. in this stanza, we set a "depth" variable to keep track of nesting of markdown headers, and begin our html document by printing the "" and "" tags. we can follow this stanza with an "END" stanza, that is executed after the traversal: END { print("\n"); print("\n"); } in this stanza, we close off the tags we opened at the start of the document. we can move onto the interesting bits of the conversion now: enter section { depth += 1; } leave section { depth -= 1; } the above stanzas begin with "enter" and "leave" clauses, followed by the name of a tree-sitter node kind: "section". the "section" identifier is visible in the tree-visualization above, it encompasses a markdown-section, and is created for every markdown header. to understand how tbsp executes above stanzas: document ... depth = 0 | section <-------- enter section (1) ... depth = 1 | | atx_heading | | | inline | | paragraph | | | inline | | section <----- enter section (2) ... depth = 2 | | | atx_heading | | | | inline | | | paragraph | | | | inline | | | <----------- leave section (2) ... depth = 1 | | <-------------- leave section (1) ... depth = 0 the following stanzas should be self-explanatory now: enter atx_heading { print("