Skip to main content

panache_parser/syntax/
kind.rs

1//! Syntax kinds and language definition for the Quarto/Pandoc CST.
2
3use rowan::Language;
4
5#[allow(non_camel_case_types)]
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7#[repr(u16)]
8pub enum SyntaxKind {
9    // Tokens
10    WHITESPACE = 0,
11    NEWLINE,
12    TEXT,
13    BACKSLASH,         // \ (for escaping)
14    ESCAPED_CHAR,      // Any escaped character
15    NONBREAKING_SPACE, // \<space>
16    HARD_LINE_BREAK,   // \<newline>
17    DIV_MARKER,        // :::
18
19    // YAML tokens (metadata and in-tree YAML CST parser)
20    YAML_METADATA_DELIM, // --- or ... (for YAML blocks)
21    YAML_KEY,            // YAML mapping key token
22    YAML_COLON,          // YAML mapping key-value separator
23    YAML_TAG,            // YAML explicit tag token (e.g. !!str)
24    YAML_ANCHOR,         // YAML anchor token (e.g. &name)
25    YAML_ALIAS,          // YAML alias token (e.g. *name)
26    YAML_SCALAR_TEXT,    // YAML scalar content fragment (one physical line of a scalar)
27    YAML_FLOW_INDICATOR, // YAML flow structural punctuation ([ ] { } ,)
28    YAML_DIRECTIVE,      // YAML directive line (%YAML, %TAG)
29    YAML_COMMENT,        // YAML inline comment token
30    YAML_LINE_PREFIX,    // Embedded-YAML per-line prefix trivia (hashpipe `#|`)
31    YAML_DOCUMENT_START, // YAML document start marker (---)
32    YAML_DOCUMENT_END,   // YAML document end marker (...)
33
34    BLOCK_QUOTE_MARKER, // >
35    ALERT_MARKER,       // [!NOTE], [!TIP], etc.
36    IMAGE_LINK_START,   // ![
37    LIST_MARKER,        // - + *
38    TASK_CHECKBOX,      // [ ] or [x] or [X]
39    COMMENT_START,      // <!--
40    COMMENT_END,        // -->
41    ATTRIBUTE,          // {#label} for headings, math, etc.
42    // Structured children of a Pandoc `{...}` ATTRIBUTE. Each wraps the
43    // existing source bytes (markers/quotes included); the projector strips
44    // them. Absent on opaque ATTRIBUTE forms (MMD `[#id]`, raw-inline
45    // `{=format}`, fallback), which keep a single inner ATTRIBUTE token.
46    ATTR_ID,         // #id (token text includes the leading '#')
47    ATTR_CLASS,      // .class (token text includes the leading '.')
48    ATTR_KEY_VALUE,  // key=value (node grouping the pieces below)
49    ATTR_KEY,        // key (token, no '=')
50    ATTR_VALUE,      // value or "value"/'value' (token text includes quotes)
51    HORIZONTAL_RULE, // --- or *** or ___
52    BLANK_LINE,
53
54    // Links and images
55    LINK_START,           // [
56    LINK,                 // [text](url)
57    LINK_TEXT,            // text part of link
58    LINK_TEXT_END,        // ] closing link text
59    LINK_DEST_START,      // ( opening link destination
60    LINK_DEST,            // (url) or (url "title")
61    LINK_DEST_END,        // ) closing link destination
62    LINK_REF,             // [ref] in reference links
63    IMAGE_LINK,           // ![alt](url)
64    IMAGE_ALT,            // alt text in image
65    IMAGE_ALT_END,        // ] closing image alt
66    IMAGE_DEST_START,     // ( opening image destination
67    IMAGE_DEST_END,       // ) closing image destination
68    AUTO_LINK,            // <http://example.com>
69    AUTO_LINK_MARKER,     // < and >
70    REFERENCE_DEFINITION, // [label]: url "title"
71    FOOTNOTE_DEFINITION,  // [^id]: content
72    FOOTNOTE_REFERENCE,   // [^id]
73    FOOTNOTE_LABEL_START, // [^
74    FOOTNOTE_LABEL_ID,    // id in [^id] or [^id]:
75    FOOTNOTE_LABEL_END,   // ]
76    FOOTNOTE_LABEL_COLON, // :
77    REFERENCE_LABEL,      // [label] part
78    REFERENCE_URL,        // url part
79    REFERENCE_TITLE,      // "title" part
80
81    // Wikilinks (Pandoc `wikilinks_title_{after,before}_pipe` extensions)
82    WIKI_LINK,       // [[url]] or [[url|title]]
83    IMAGE_WIKI_LINK, // ![[url]] or ![[url|title]]
84    WIKI_LINK_OPEN,  // [[ or ![[
85    WIKI_LINK_URL,   // URL slot (raw TEXT child, no inline parsing)
86    WIKI_LINK_PIPE,  // | separator
87    WIKI_LINK_TITLE, // title slot (raw TEXT child, no inline parsing)
88    WIKI_LINK_CLOSE, // ]]
89
90    // Math
91    INLINE_MATH_MARKER,  // $
92    DISPLAY_MATH_MARKER, // $$
93    INLINE_MATH,
94    DISPLAY_MATH,
95    MATH_CONTENT, // wrapper node for parsed TeX math content (subtree root)
96
97    // Math content (structural TeX CST under MATH_CONTENT)
98    MATH_GROUP,       // { ... } brace group (node)
99    MATH_ENVIRONMENT, // \begin{env} ... \end{env} (node)
100    MATH_GROUP_OPEN,  // {
101    MATH_GROUP_CLOSE, // }
102    MATH_COMMAND,     // \foo control word or \% control symbol
103    MATH_LINE_BREAK,  // \\
104    MATH_ALIGN,       // & alignment tab
105    MATH_SCRIPT,      // ^ or _
106    MATH_COMMENT,     // % to end of line (TeX comment)
107    // `+ - * = < >` operator atom; one token per char. Class/precedence are
108    // contextual (unary minus, `\mathbin`) and deferred to the formatter.
109    MATH_OPERATOR,
110    // Delimiters and punctuation whose TeX mathcode class is fixed at the
111    // character level (unlike operator class, which is contextual): `( [` open,
112    // `) ]` close, `, ;` punctuation. One token per char. The ambiguous `| . /`
113    // stay in MATH_TEXT โ€” their class needs macro context.
114    MATH_OPEN,    // ( or [
115    MATH_CLOSE,   // ) or ]
116    MATH_PUNCT,   // , or ;
117    MATH_TEXT,    // run of ordinary atoms
118    MATH_SPACE,   // run of spaces/tabs
119    MATH_NEWLINE, // newline within math content
120    // Bookdown equation label `(\#eq:label)`, recognized only when the
121    // `bookdown_equation_references` extension is enabled. A single token over
122    // the whole `(\#eq:...)` span so the indexer/LSP can target it precisely.
123    MATH_EQUATION_LABEL,
124
125    // Footnotes
126    INLINE_FOOTNOTE_START, // ^[
127    INLINE_FOOTNOTE_END,   // ]
128    INLINE_FOOTNOTE,       // ^[text]
129
130    // Citations
131    CITATION,                // [@key] or @key
132    CITATION_MARKER,         // @ or -@
133    CITATION_KEY,            // The citation key identifier
134    CITATION_BRACE_OPEN,     // { for complex keys
135    CITATION_BRACE_CLOSE,    // } for complex keys
136    CITATION_CONTENT,        // Text content in bracketed citations
137    CITATION_SEPARATOR,      // ; between multiple citations
138    CROSSREF,                // Quarto cross-reference: @fig-*, @eq-*, etc.
139    CROSSREF_MARKER,         // @ or -@ for cross-references
140    CROSSREF_KEY,            // Cross-reference key identifier
141    CROSSREF_BRACE_OPEN,     // { for braced cross-reference keys
142    CROSSREF_BRACE_CLOSE,    // } for braced cross-reference keys
143    CROSSREF_BOOKDOWN_OPEN,  // \@ref(
144    CROSSREF_BOOKDOWN_CLOSE, // )
145
146    // Spans
147    BRACKETED_SPAN,     // [text]{.class}
148    SPAN_CONTENT,       // text inside span
149    SPAN_ATTRIBUTES,    // {.class key="val"}
150    SPAN_BRACKET_OPEN,  // [
151    SPAN_BRACKET_CLOSE, // ]
152
153    // Shortcodes (Quarto)
154    SHORTCODE,              // {{< name args >}} or {{{< name args >}}}
155    SHORTCODE_MARKER_OPEN,  // {{< or {{{<
156    SHORTCODE_MARKER_CLOSE, // >}} or >}}}
157    SHORTCODE_CONTENT,      // content between markers
158
159    // Svelte template syntax (mdsvex). Opaque, lossless spans: the content
160    // between the braces is preserved verbatim. The parent kind records the
161    // category (block logic / tag / expression) by leading sigil.
162    SVELTE_BLOCK_LOGIC,  // {#if}, {:else}, {/each}, ...
163    SVELTE_TAG,          // {@html ...}, {@const ...}, {@debug ...}
164    SVELTE_EXPRESSION,   // {expr}
165    SVELTE_MARKER_OPEN,  // {
166    SVELTE_MARKER_CLOSE, // }
167    SVELTE_CONTENT,      // verbatim content between the braces
168    SVELTE_BLOCK,        // a standalone Svelte span occupying a whole line
169
170    // Code
171    INLINE_CODE,
172    INLINE_CODE_MARKER,  // ` or `` or ```
173    INLINE_CODE_CONTENT, // Literal inline code content
174    INLINE_EXEC,         // Inline executable code span variants
175    INLINE_EXEC_MARKER,  // Backtick markers delimiting inline executable code
176    INLINE_EXEC_LANG,    // Runtime marker (`r` or `{r}`)
177    INLINE_EXEC_CONTENT, // Executable inline code expression
178    CODE_FENCE_MARKER,   // ``` or ~~~
179    CODE_BLOCK,
180
181    // Raw inline spans
182    RAW_INLINE,         // `content`{=format}
183    RAW_INLINE_MARKER,  // ` markers
184    RAW_INLINE_FORMAT,  // format name (html, latex, etc.)
185    RAW_INLINE_CONTENT, // raw content
186
187    // Inline emphasis and formatting
188    EMPHASIS,           // *text* or _text_
189    STRONG,             // **text** or __text__
190    STRIKEOUT,          // ~~text~~
191    MARK,               // ==text==
192    SUPERSCRIPT,        // ^text^
193    SUBSCRIPT,          // ~text~
194    EMPHASIS_MARKER,    // * or _ (for emphasis)
195    STRONG_MARKER,      // ** or __ (for strong)
196    STRIKEOUT_MARKER,   // ~~ (for strikeout)
197    MARK_MARKER,        // == (for mark/highlight)
198    SUPERSCRIPT_MARKER, // ^ (for superscript)
199    SUBSCRIPT_MARKER,   // ~ (for subscript)
200
201    // Composite nodes
202    DOCUMENT,
203
204    // YAML nodes
205    YAML_METADATA,
206    YAML_METADATA_CONTENT,    // Content lines inside YAML metadata block
207    YAML_STREAM, // YAML 1.2 stream wrapper (zero or more YAML_DOCUMENT children + trivia)
208    YAML_DOCUMENT, // a single YAML document (markers + body)
209    YAML_SCALAR, // YAML scalar value node (wraps YAML_SCALAR_TEXT content + NEWLINE/prefix leaves)
210    YAML_BLOCK_MAP, // YAML block mapping container
211    YAML_BLOCK_MAP_ENTRY, // YAML block mapping entry (key: value)
212    YAML_BLOCK_MAP_KEY, // YAML block mapping key wrapper
213    YAML_BLOCK_MAP_VALUE, // YAML block mapping value wrapper
214    YAML_FLOW_MAP, // YAML flow mapping container ({key: value, ...})
215    YAML_FLOW_MAP_ENTRY, // YAML flow mapping entry
216    YAML_FLOW_MAP_KEY, // YAML flow mapping key wrapper
217    YAML_FLOW_MAP_VALUE, // YAML flow mapping value wrapper
218    YAML_FLOW_SEQUENCE, // YAML flow sequence container ([a, b, ...])
219    YAML_FLOW_SEQUENCE_ITEM, // YAML flow sequence item wrapper
220    YAML_BLOCK_SEQUENCE, // YAML block sequence container (- item ...)
221    YAML_BLOCK_SEQUENCE_ITEM, // YAML block sequence item wrapper
222    YAML_BLOCK_SEQ_ENTRY, // YAML block sequence entry marker (-)
223
224    PANDOC_TITLE_BLOCK,
225    MMD_TITLE_BLOCK,
226    FENCED_DIV,
227    // python-markdown admonition (`!!! note`) and pymdownx details
228    // (`???`/`???+`) container block. Content is 4-space indented and
229    // parsed recursively; closes on dedent (like a footnote definition).
230    ADMONITION,
231    // MyST directive container (```` ```{name} ```` or, with `colon_fence`,
232    // `:::{name}`). Fence-delimited like a fenced div, but its body is parsed
233    // recursively as markdown and the closer must match the opener's fence
234    // character and count.
235    MYST_DIRECTIVE,
236    PARAGRAPH,
237    PLAIN, // Inline content without paragraph break (tight lists, definition lists, table cells)
238    BLOCK_QUOTE,
239    ALERT,
240    LIST,
241    LIST_ITEM,
242    DEFINITION_LIST,
243    DEFINITION_ITEM,
244    TERM,
245    DEFINITION,
246    DEFINITION_MARKER, // : or ~
247    LINE_BLOCK,
248    LINE_BLOCK_LINE,
249    LINE_BLOCK_MARKER, // |
250    COMMENT,
251    FIGURE, // Standalone image (Pandoc figure)
252
253    // HTML blocks
254    HTML_BLOCK,         // Generic HTML block
255    HTML_BLOCK_TAG,     // Opening/closing tags
256    HTML_BLOCK_CONTENT, // Content between tags
257    // Pandoc-dialect lift: a matched <div ...>...</div> block.
258    HTML_BLOCK_DIV,
259    // Pandoc-dialect lift: a single-construct opaque HTML block that
260    // projects to exactly one `RawBlock "html"` โ€” an HTML comment
261    // (`<!-- -->`), a processing instruction (`<? ?>`), or a verbatim
262    // raw-text element (`<pre>`/`<script>`/`<style>`/`<textarea>`). The
263    // wrapper kind lets the pandoc-native projector route by CST kind
264    // instead of re-sniffing the leading bytes. CommonMark dialect keeps
265    // the opaque HTML_BLOCK shape.
266    HTML_BLOCK_RAW,
267    // Structural region inside an HTML opening tag holding the
268    // attribute-list bytes โ€” i.e. everything between the tag name and
269    // the closing `>`, exclusive. Recognized by `AttributeNode::cast`,
270    // so the salsa anchor index sees `id`/`class`/key=val attrs from
271    // `<div id="x">` blocks via the same walk that handles fenced-div
272    // and heading attributes.
273    HTML_ATTRS,
274
275    // Inline raw HTML (CommonMark ยง6.6 / Pandoc raw_html). One node per HTML
276    // tag/comment/declaration/PI/CDATA span; child token holds the verbatim
277    // bytes of the span.
278    INLINE_HTML,
279    INLINE_HTML_CONTENT,
280    // Pandoc-dialect inline lift: a matched <span ...>...</span> tag pair,
281    // mirroring HTML_BLOCK_DIV at the inline level. The open tag's
282    // attribute region is exposed structurally as HTML_ATTRS so the
283    // existing AttributeNode walk picks up `<span id>` ids automatically.
284    INLINE_HTML_SPAN,
285
286    // TeX blocks
287    TEX_BLOCK, // Raw tex block (e.g., LaTeX commands)
288
289    // Headings
290    HEADING,
291    HEADING_CONTENT,
292    ATX_HEADING_MARKER,       // leading #####
293    SETEXT_HEADING_UNDERLINE, // ===== or -----
294
295    // LaTeX inline commands
296    LATEX_COMMAND, // \command{...}
297
298    // Tables
299    SIMPLE_TABLE,
300    MULTILINE_TABLE,
301    PIPE_TABLE,
302    GRID_TABLE,
303    TABLE_HEADER,
304    TABLE_FOOTER,
305    TABLE_SEPARATOR,
306    TABLE_ROW,
307    TABLE_CELL,
308    TABLE_CAPTION,
309    TABLE_CAPTION_PREFIX, // "Table: ", "table: ", or ": "
310    // Separator-row markers (split out of the coalesced separator TEXT so
311    // alignment/width derivations read structure instead of re-scanning a
312    // string). One `TABLE_SEP_DELIM` covers both `|` and `+`.
313    TABLE_SEP_DELIM,      // single column delimiter: `|` (pipe) or `+` (grid)
314    TABLE_SEP_DASHES,     // a run of `-`
315    TABLE_SEP_EQUALS,     // a run of `=` (grid `+===+` header divider)
316    TABLE_SEP_COLON,      // single `:` alignment marker
317    TABLE_SEP_WHITESPACE, // interior spaces/tabs between dash runs
318
319    // Code block parts
320    CODE_FENCE_OPEN,
321    CODE_FENCE_CLOSE,
322    CODE_INFO,     // Raw info string (preserved for lossless formatting)
323    CODE_LANGUAGE, // Parsed language identifier (r, python, etc.)
324
325    // Chunk options (for executable chunks like {r, echo=TRUE})
326    CHUNK_OPTIONS,          // Container for all chunk options
327    CHUNK_OPTION,           // Single option (key=value pair)
328    CHUNK_OPTION_KEY,       // Option name (e.g., echo, fig.cap)
329    CHUNK_OPTION_VALUE,     // Option value (e.g., TRUE, "text")
330    CHUNK_OPTION_QUOTE,     // Quote character (" or ') if present
331    CHUNK_LABEL,            // Special case: unlabeled first option in {r mylabel}
332    HASHPIPE_YAML_PREAMBLE, // Hashpipe YAML option preamble region inside CODE_CONTENT
333    HASHPIPE_YAML_CONTENT,  // Content lines belonging to hashpipe YAML preamble
334    HASHPIPE_PREFIX,        // Hashpipe option marker prefix (e.g., #|, //|, --|)
335
336    CODE_CONTENT,
337
338    // Div parts
339    DIV_FENCE_OPEN,
340    DIV_FENCE_CLOSE,
341    DIV_INFO,
342    DIV_CONTENT,
343
344    // Admonition parts (`!!! type "title"` / `???`/`???+`)
345    ADMONITION_MARKER, // `!!!`, `???`, or `???+`
346    ADMONITION_TYPE,   // type/class words (e.g. `note`, `danger highlight`)
347    ADMONITION_TITLE,  // optional quoted title (e.g. `"Heads up"`)
348
349    // MyST directive parts
350    MYST_DIRECTIVE_OPEN,   // opener line node (fence + name + optional argument)
351    MYST_DIRECTIVE_CLOSE,  // closer line node (matching fence)
352    MYST_DIRECTIVE_FENCE,  // the fence run itself (```` ``` ````, `~~~`, or `:::`)
353    MYST_DIRECTIVE_NAME,   // the `{name}` token, braces included (lossless)
354    MYST_DIRECTIVE_ARG,    // argument text after `{name}` on the opener line
355    MYST_DIRECTIVE_OPTION, // one `:key: value` option line node
356    MYST_DIRECTIVE_OPTION_MARKER, // a `:` delimiter (leading and closing colon)
357    MYST_DIRECTIVE_OPTION_NAME, // the option key (e.g. `alt`)
358    MYST_DIRECTIVE_OPTION_VALUE, // the option value (e.g. `An image`)
359    MYST_DIRECTIVE_BODY,   // verbatim body of a code/math directive (raw, not reflowed)
360
361    // MyST inline role parts (`` {name}`content` ``)
362    MYST_ROLE,         // the whole role
363    MYST_ROLE_NAME,    // the `{name}` token, braces included
364    MYST_ROLE_MARKER,  // the backtick run delimiting the content
365    MYST_ROLE_CONTENT, // the literal content between the backticks
366
367    // MyST target / comment / substitution parts
368    MYST_TARGET,             // a `(label)=` target line
369    MYST_TARGET_LABEL,       // the label between `(` and `)=`
370    MYST_COMMENT,            // a `% ...` line comment
371    MYST_BLOCK_BREAK,        // a `+++` block break line
372    MYST_BLOCK_BREAK_MARKER, // the `+++` marker run
373    MYST_BLOCK_BREAK_META,   // optional trailing cell metadata after `+++`
374    MYST_SUBSTITUTION,       // an inline `{{ name }}` substitution
375    MYST_SUBSTITUTION_NAME,  // the substitution key between `{{` and `}}`
376
377    EMOJI, // :alias:
378
379    // Bracket-shape pattern that did not resolve as a link/image.
380    // Distinct from LINK/IMAGE_LINK so downstream tools (linter, LSP) can
381    // walk a typed wrapper without the parser having to lie about
382    // resolution. `is_image()` on the typed wrapper distinguishes
383    // `[foo]` from `![foo]` shapes.
384    UNRESOLVED_REFERENCE,
385}
386
387impl From<SyntaxKind> for rowan::SyntaxKind {
388    fn from(kind: SyntaxKind) -> Self {
389        Self(kind as u16)
390    }
391}
392
393#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
394pub enum PanacheLanguage {}
395
396impl Language for PanacheLanguage {
397    type Kind = SyntaxKind;
398
399    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
400        unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
401    }
402
403    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
404        kind.into()
405    }
406}