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 shadow 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,         // YAML scalar value token
27    YAML_COMMENT,        // YAML inline comment token
28    YAML_DOCUMENT_START, // YAML document start marker (---) in shadow parser
29    YAML_DOCUMENT_END,   // YAML document end marker (...) in shadow parser
30
31    BLOCK_QUOTE_MARKER, // >
32    ALERT_MARKER,       // [!NOTE], [!TIP], etc.
33    IMAGE_LINK_START,   // ![
34    LIST_MARKER,        // - + *
35    TASK_CHECKBOX,      // [ ] or [x] or [X]
36    COMMENT_START,      // <!--
37    COMMENT_END,        // -->
38    ATTRIBUTE,          // {#label} for headings, math, etc.
39    // Structured children of a Pandoc `{...}` ATTRIBUTE. Each wraps the
40    // existing source bytes (markers/quotes included); the projector strips
41    // them. Absent on opaque ATTRIBUTE forms (MMD `[#id]`, raw-inline
42    // `{=format}`, fallback), which keep a single inner ATTRIBUTE token.
43    ATTR_ID,         // #id (token text includes the leading '#')
44    ATTR_CLASS,      // .class (token text includes the leading '.')
45    ATTR_KEY_VALUE,  // key=value (node grouping the pieces below)
46    ATTR_KEY,        // key (token, no '=')
47    ATTR_VALUE,      // value or "value"/'value' (token text includes quotes)
48    HORIZONTAL_RULE, // --- or *** or ___
49    BLANK_LINE,
50
51    // Links and images
52    LINK_START,           // [
53    LINK,                 // [text](url)
54    LINK_TEXT,            // text part of link
55    LINK_TEXT_END,        // ] closing link text
56    LINK_DEST_START,      // ( opening link destination
57    LINK_DEST,            // (url) or (url "title")
58    LINK_DEST_END,        // ) closing link destination
59    LINK_REF,             // [ref] in reference links
60    IMAGE_LINK,           // ![alt](url)
61    IMAGE_ALT,            // alt text in image
62    IMAGE_ALT_END,        // ] closing image alt
63    IMAGE_DEST_START,     // ( opening image destination
64    IMAGE_DEST_END,       // ) closing image destination
65    AUTO_LINK,            // <http://example.com>
66    AUTO_LINK_MARKER,     // < and >
67    REFERENCE_DEFINITION, // [label]: url "title"
68    FOOTNOTE_DEFINITION,  // [^id]: content
69    FOOTNOTE_REFERENCE,   // [^id]
70    FOOTNOTE_LABEL_START, // [^
71    FOOTNOTE_LABEL_ID,    // id in [^id] or [^id]:
72    FOOTNOTE_LABEL_END,   // ]
73    FOOTNOTE_LABEL_COLON, // :
74    REFERENCE_LABEL,      // [label] part
75    REFERENCE_URL,        // url part
76    REFERENCE_TITLE,      // "title" part
77
78    // Math
79    INLINE_MATH_MARKER,  // $
80    DISPLAY_MATH_MARKER, // $$
81    INLINE_MATH,
82    DISPLAY_MATH,
83    MATH_CONTENT,
84
85    // Footnotes
86    INLINE_FOOTNOTE_START, // ^[
87    INLINE_FOOTNOTE_END,   // ]
88    INLINE_FOOTNOTE,       // ^[text]
89
90    // Citations
91    CITATION,                // [@key] or @key
92    CITATION_MARKER,         // @ or -@
93    CITATION_KEY,            // The citation key identifier
94    CITATION_BRACE_OPEN,     // { for complex keys
95    CITATION_BRACE_CLOSE,    // } for complex keys
96    CITATION_CONTENT,        // Text content in bracketed citations
97    CITATION_SEPARATOR,      // ; between multiple citations
98    CROSSREF,                // Quarto cross-reference: @fig-*, @eq-*, etc.
99    CROSSREF_MARKER,         // @ or -@ for cross-references
100    CROSSREF_KEY,            // Cross-reference key identifier
101    CROSSREF_BRACE_OPEN,     // { for braced cross-reference keys
102    CROSSREF_BRACE_CLOSE,    // } for braced cross-reference keys
103    CROSSREF_BOOKDOWN_OPEN,  // \@ref(
104    CROSSREF_BOOKDOWN_CLOSE, // )
105
106    // Spans
107    BRACKETED_SPAN,     // [text]{.class}
108    SPAN_CONTENT,       // text inside span
109    SPAN_ATTRIBUTES,    // {.class key="val"}
110    SPAN_BRACKET_OPEN,  // [
111    SPAN_BRACKET_CLOSE, // ]
112
113    // Shortcodes (Quarto)
114    SHORTCODE,              // {{< name args >}} or {{{< name args >}}}
115    SHORTCODE_MARKER_OPEN,  // {{< or {{{<
116    SHORTCODE_MARKER_CLOSE, // >}} or >}}}
117    SHORTCODE_CONTENT,      // content between markers
118
119    // Code
120    INLINE_CODE,
121    INLINE_CODE_MARKER,  // ` or `` or ```
122    INLINE_CODE_CONTENT, // Literal inline code content
123    INLINE_EXEC,         // Inline executable code span variants
124    INLINE_EXEC_MARKER,  // Backtick markers delimiting inline executable code
125    INLINE_EXEC_LANG,    // Runtime marker (`r` or `{r}`)
126    INLINE_EXEC_CONTENT, // Executable inline code expression
127    CODE_FENCE_MARKER,   // ``` or ~~~
128    CODE_BLOCK,
129
130    // Raw inline spans
131    RAW_INLINE,         // `content`{=format}
132    RAW_INLINE_MARKER,  // ` markers
133    RAW_INLINE_FORMAT,  // format name (html, latex, etc.)
134    RAW_INLINE_CONTENT, // raw content
135
136    // Inline emphasis and formatting
137    EMPHASIS,           // *text* or _text_
138    STRONG,             // **text** or __text__
139    STRIKEOUT,          // ~~text~~
140    MARK,               // ==text==
141    SUPERSCRIPT,        // ^text^
142    SUBSCRIPT,          // ~text~
143    EMPHASIS_MARKER,    // * or _ (for emphasis)
144    STRONG_MARKER,      // ** or __ (for strong)
145    STRIKEOUT_MARKER,   // ~~ (for strikeout)
146    MARK_MARKER,        // == (for mark/highlight)
147    SUPERSCRIPT_MARKER, // ^ (for superscript)
148    SUBSCRIPT_MARKER,   // ~ (for subscript)
149
150    // Composite nodes
151    DOCUMENT,
152
153    // YAML nodes
154    YAML_METADATA,
155    YAML_METADATA_CONTENT,    // Content lines inside YAML metadata block
156    YAML_STREAM, // Shadow parser only: YAML 1.2 stream wrapper (zero or more YAML_DOCUMENT children + trivia)
157    YAML_DOCUMENT, // Shadow parser only: a single YAML document (markers + body)
158    YAML_BLOCK_MAP, // YAML block mapping container (prototype shadow parser)
159    YAML_BLOCK_MAP_ENTRY, // YAML block mapping entry (key: value)
160    YAML_BLOCK_MAP_KEY, // YAML block mapping key wrapper
161    YAML_BLOCK_MAP_VALUE, // YAML block mapping value wrapper
162    YAML_FLOW_MAP, // YAML flow mapping container ({key: value, ...})
163    YAML_FLOW_MAP_ENTRY, // YAML flow mapping entry
164    YAML_FLOW_MAP_KEY, // YAML flow mapping key wrapper
165    YAML_FLOW_MAP_VALUE, // YAML flow mapping value wrapper
166    YAML_FLOW_SEQUENCE, // YAML flow sequence container ([a, b, ...])
167    YAML_FLOW_SEQUENCE_ITEM, // YAML flow sequence item wrapper
168    YAML_BLOCK_SEQUENCE, // YAML block sequence container (- item ...)
169    YAML_BLOCK_SEQUENCE_ITEM, // YAML block sequence item wrapper
170    YAML_BLOCK_SEQ_ENTRY, // YAML block sequence entry marker (-)
171
172    PANDOC_TITLE_BLOCK,
173    MMD_TITLE_BLOCK,
174    FENCED_DIV,
175    PARAGRAPH,
176    PLAIN, // Inline content without paragraph break (tight lists, definition lists, table cells)
177    BLOCK_QUOTE,
178    ALERT,
179    LIST,
180    LIST_ITEM,
181    DEFINITION_LIST,
182    DEFINITION_ITEM,
183    TERM,
184    DEFINITION,
185    DEFINITION_MARKER, // : or ~
186    LINE_BLOCK,
187    LINE_BLOCK_LINE,
188    LINE_BLOCK_MARKER, // |
189    COMMENT,
190    FIGURE, // Standalone image (Pandoc figure)
191
192    // HTML blocks
193    HTML_BLOCK,         // Generic HTML block
194    HTML_BLOCK_TAG,     // Opening/closing tags
195    HTML_BLOCK_CONTENT, // Content between tags
196    // Pandoc-dialect lift: a matched <div ...>...</div> block.
197    HTML_BLOCK_DIV,
198    // Structural region inside an HTML opening tag holding the
199    // attribute-list bytes — i.e. everything between the tag name and
200    // the closing `>`, exclusive. Recognized by `AttributeNode::cast`,
201    // so the salsa anchor index sees `id`/`class`/key=val attrs from
202    // `<div id="x">` blocks via the same walk that handles fenced-div
203    // and heading attributes.
204    HTML_ATTRS,
205
206    // Inline raw HTML (CommonMark §6.6 / Pandoc raw_html). One node per HTML
207    // tag/comment/declaration/PI/CDATA span; child token holds the verbatim
208    // bytes of the span.
209    INLINE_HTML,
210    INLINE_HTML_CONTENT,
211    // Pandoc-dialect inline lift: a matched <span ...>...</span> tag pair,
212    // mirroring HTML_BLOCK_DIV at the inline level. The open tag's
213    // attribute region is exposed structurally as HTML_ATTRS so the
214    // existing AttributeNode walk picks up `<span id>` ids automatically.
215    INLINE_HTML_SPAN,
216
217    // TeX blocks
218    TEX_BLOCK, // Raw tex block (e.g., LaTeX commands)
219
220    // Headings
221    HEADING,
222    HEADING_CONTENT,
223    ATX_HEADING_MARKER,       // leading #####
224    SETEXT_HEADING_UNDERLINE, // ===== or -----
225
226    // LaTeX inline commands
227    LATEX_COMMAND, // \command{...}
228
229    // Tables
230    SIMPLE_TABLE,
231    MULTILINE_TABLE,
232    PIPE_TABLE,
233    GRID_TABLE,
234    TABLE_HEADER,
235    TABLE_FOOTER,
236    TABLE_SEPARATOR,
237    TABLE_ROW,
238    TABLE_CELL,
239    TABLE_CAPTION,
240    TABLE_CAPTION_PREFIX, // "Table: ", "table: ", or ": "
241
242    // Code block parts
243    CODE_FENCE_OPEN,
244    CODE_FENCE_CLOSE,
245    CODE_INFO,     // Raw info string (preserved for lossless formatting)
246    CODE_LANGUAGE, // Parsed language identifier (r, python, etc.)
247
248    // Chunk options (for executable chunks like {r, echo=TRUE})
249    CHUNK_OPTIONS,          // Container for all chunk options
250    CHUNK_OPTION,           // Single option (key=value pair)
251    CHUNK_OPTION_KEY,       // Option name (e.g., echo, fig.cap)
252    CHUNK_OPTION_VALUE,     // Option value (e.g., TRUE, "text")
253    CHUNK_OPTION_QUOTE,     // Quote character (" or ') if present
254    CHUNK_LABEL,            // Special case: unlabeled first option in {r mylabel}
255    HASHPIPE_YAML_PREAMBLE, // Hashpipe YAML option preamble region inside CODE_CONTENT
256    HASHPIPE_YAML_CONTENT,  // Content lines belonging to hashpipe YAML preamble
257    HASHPIPE_PREFIX,        // Hashpipe option marker prefix (e.g., #|, //|, --|)
258
259    CODE_CONTENT,
260
261    // Div parts
262    DIV_FENCE_OPEN,
263    DIV_FENCE_CLOSE,
264    DIV_INFO,
265    DIV_CONTENT,
266    EMOJI, // :alias:
267
268    // Bracket-shape pattern that did not resolve as a link/image.
269    // Distinct from LINK/IMAGE_LINK so downstream tools (linter, LSP) can
270    // walk a typed wrapper without the parser having to lie about
271    // resolution. `is_image()` on the typed wrapper distinguishes
272    // `[foo]` from `![foo]` shapes.
273    UNRESOLVED_REFERENCE,
274}
275
276impl From<SyntaxKind> for rowan::SyntaxKind {
277    fn from(kind: SyntaxKind) -> Self {
278        Self(kind as u16)
279    }
280}
281
282#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
283pub enum PanacheLanguage {}
284
285impl Language for PanacheLanguage {
286    type Kind = SyntaxKind;
287
288    fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
289        unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
290    }
291
292    fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
293        kind.into()
294    }
295}