fix: make tree-sitter grammar regenerable

This commit is contained in:
Michael Netshipise 2026-05-13 11:13:30 +02:00
parent 6a0318bcb5
commit 083e3946e6
12 changed files with 21897 additions and 26529 deletions

View File

@ -1,16 +1,68 @@
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
/**
* Waltzing Template Engine - Tree-sitter Grammar
*
* VALIDATION RULES (enforced by the compiler, not this grammar):
*
* 1. Reserved variable names - Variables named `__wtz_target` or `out` declared
* via `@let` are not allowed. This includes:
* - Direct @let declarations: @let out = "value"
* - For loop iterators: @for out in items
* - Tuple patterns: @for (a, out) in items
* - If-let patterns: @if let Some(out) = opt
* - Match arm patterns: @match status { Some(out) => { ... } }
* Workaround: Use @(out) to reference a variable with that name.
*
* 2. Function name conflicts - If a function `foo` exists, you cannot also
* have `foo_to_stream` (and vice versa). This prevents conflicts with
* the auto-generated streaming functions.
*
* 3. Special keywords (NEW SYNTAX - recommended):
* - @Out - Output target type (includes &mut): &mut _WtzTarget
* - @out - Output reference (includes &mut): &mut __wtz_buffer or __wtz_target
* - @render(T1, T2, ...) - Render callback type: impl Fn(T1, T2, ..., &mut _WtzTarget)
*
* 4. Special keywords (DEPRECATED - still supported):
* - @Target - Compiler-injected type (resolves to _WtzTarget or _WtzWriter)
* - @target - Compiler-injected variable (resolves to __wtz_buffer or __wtz_target)
*
* These rules apply only to @let template variables and @fn template functions,
* not to CSS or JavaScript code.
*/
// REGEN STATUS — read before touching this grammar.
//
// `src/parser.c`, `src/grammar.json`, and `src/node-types.json` are generated
// from this file with tree-sitter-cli v0.25.10. Regeneration is expected to
// complete quickly on local hardware (last checked: 0.60s, <90 MB RSS).
//
// The previous grammar shape made `tree-sitter generate` explore an enormous
// LR/error-recovery state space. The important constraints that keep regen
// reliable are:
// • All template-content bodies go through `_template_nodes`.
// • HTML void elements are explicit; ordinary `<tag>` starts a full element.
// • Function tags require `/>` for self-closing form.
// • Rust expressions are intentionally opaque (`rust_expression`) except for
// literals, paths, inferred enum variants, template blocks, and render
// closures. The Waltzing compiler validates Rust expression semantics.
// • Multi-depth template comments/raw blocks are not modeled as 22 token
// variants; the common one-delimiter forms are tokenized for editor use.
//
// If any of those constraints are relaxed, run `tree-sitter generate` before
// committing and watch memory/time. A return to multi-GB RSS means the grammar
// has reintroduced exponential table construction.
module.exports = grammar({
name: "waltzing",
extras: ($) => [/\s/],
conflicts: ($) => [
[$.expression, $.pattern],
[$.rust_path, $.expression],
[$.html_element],
[$.self_closing_function_tag, $.container_function_tag],
// `|x|` — could be closure that returns `|x|` (no return type) or that's
// about to declare a `-> T` return type. cli 0.25 needs both parses.
[$.closure_type],
],
rules: {
@ -119,7 +171,9 @@ module.exports = grammar({
default_value: ($) => $.expression,
content_block: ($) => seq("{", repeat($.template_node), "}"),
content_block: ($) => seq("{", optional($._template_nodes), "}"),
_template_nodes: ($) => repeat1($.template_node),
// Template nodes
// Note: template_control_flow must come before template_expression
@ -144,14 +198,14 @@ module.exports = grammar({
// Self-closing tag
seq("<", $.tag_name, repeat($.attribute_or_control), "/", ">"),
// Void elements (no closing tag needed)
seq("<", $.tag_name, repeat($.attribute_or_control), ">"),
seq("<", alias($.void_tag_name, $.tag_name), repeat($.attribute_or_control), optional("/"), ">"),
// Full element with content and closing tag
seq(
"<",
$.tag_name,
repeat($.attribute_or_control),
">",
repeat($.template_node),
optional($._template_nodes),
"</",
$.tag_name,
">",
@ -161,8 +215,8 @@ module.exports = grammar({
// Allow either HTML attributes or control flow (@if/@for) in attribute position
attribute_or_control: ($) =>
choice(
$.html_attribute,
$.attribute_control_flow,
$.html_attribute,
),
// Control flow in attribute context - produces attributes conditionally
@ -195,11 +249,34 @@ module.exports = grammar({
tag_name: ($) => /[a-zA-Z][a-zA-Z0-9-]*/,
void_tag_name: ($) =>
token(prec(1, choice(
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr",
))),
html_attribute: ($) =>
seq($.attribute_name, optional(seq("=", $.attribute_value))),
// Attribute names: allow @ for directives like @click, but not @if/@for/@let
attribute_name: ($) => /[a-zA-Z_:@][a-zA-Z0-9_:.-]*/,
// Attribute names: allow @ directives like @click, while keeping @if/@for
// visible as `@` + keyword tokens for attribute_control_flow.
attribute_name: ($) =>
choice(
/[a-zA-Z_:][a-zA-Z0-9_:.-]*/,
seq("@", /[a-zA-Z_][a-zA-Z0-9_:.-]*/),
),
attribute_value: ($) =>
choice(
@ -210,7 +287,21 @@ module.exports = grammar({
// Template expressions
template_expression: ($) =>
choice($.simple_expression, $.complex_expression, $.safe_expression),
choice(
$.simple_expression,
$.complex_expression,
$.safe_expression,
$.format_expression,
$.matches_expression,
$.out_ref,
$.target_ref,
),
// Compiler-injected output reference - resolves to &mut __wtz_buffer or __wtz_target
out_ref: ($) => seq("@", "out"),
// Compiler-injected target reference - DEPRECATED, use out_ref
target_ref: ($) => seq("@", "target"),
// High-precedence token to match @identifier before @for/@if etc keywords
simple_expression: ($) =>
@ -231,6 +322,60 @@ module.exports = grammar({
")",
),
// Format expression: @format("template {}", arg1, arg2, ...)
// Compiles to Rust's format!() macro.
format_expression: ($) =>
seq(
"@",
"format",
"(",
$.string_literal,
repeat(seq(",", $.expression)),
")",
),
// Matches expression: @matches(value, pattern) or @matches(value, pattern if guard)
// Compiles to Rust's matches!() macro.
matches_expression: ($) =>
seq(
"@",
"matches",
"(",
$.expression,
",",
$.pattern,
optional(seq("if", $.expression)),
")",
),
// Inferred enum path: ::Variant — the enum type is resolved from the
// surrounding context (function parameter type). Tokenized so `::` and
// the variant name parse as one unit.
inferred_enum_path: ($) =>
token(seq("::", /[a-zA-Z_][a-zA-Z0-9_]*/)),
// Inferred enum tuple variant: ::Variant(arg1, arg2, ...)
inferred_enum_call: ($) =>
prec(4, seq($.inferred_enum_path, "(", optional($.argument_list), ")")),
// Inferred enum struct variant: ::Variant { field: value, ... }
inferred_enum_struct: ($) =>
prec(
4,
seq(
$.inferred_enum_path,
"{",
optional(seq(
$.struct_field_init,
repeat(seq(",", $.struct_field_init)),
optional(","),
)),
"}",
),
),
struct_field_init: ($) => seq($.identifier, ":", $.expression),
expression_path: ($) =>
seq(
$.identifier,
@ -256,9 +401,13 @@ module.exports = grammar({
),
// Let binding: @let name = expression
// Use simple_pattern to avoid confusion with if/match expressions containing { }
// Use simple_pattern to avoid confusion with if/match expressions
// containing { }. The `prec(3)` outranks `simple_expression`'s prec(2)
// macro form so that `@let n` commits to let_statement rather than
// parsing `@let` as `simple_expression` (= `@` + identifier `let`) and
// leaving the `= 42` orphaned.
let_statement: ($) =>
seq(seq("@", "let"), $.simple_pattern, "=", $.expression),
prec(3, seq(seq("@", "let"), $.simple_pattern, "=", $.expression, optional(";"))),
if_statement: ($) =>
seq(
@ -318,7 +467,7 @@ module.exports = grammar({
"<@",
$.function_path,
repeat($.function_attribute),
optional("/"),
"/",
">",
),
@ -328,7 +477,7 @@ module.exports = grammar({
$.function_path,
repeat($.function_attribute),
">",
repeat($.template_node),
optional($._template_nodes),
"</@",
$.function_path,
">",
@ -357,7 +506,7 @@ module.exports = grammar({
boolean_attribute: ($) => $.identifier,
function_attribute_value: ($) =>
choice($.string_literal, seq("@", $.expression_path), $.unquoted_value),
choice($.string_literal, seq("@", $.expression_path), $.render_closure, $.unquoted_value),
unquoted_value: ($) => /[^\s>=\/]+/,
@ -365,6 +514,7 @@ module.exports = grammar({
pattern: ($) =>
choice(
$.wildcard_pattern,
$.tuple_variant_pattern,
$.tuple_pattern,
$.struct_pattern,
$.literal,
@ -395,169 +545,64 @@ module.exports = grammar({
field_pattern: ($) => seq($.identifier, optional(seq(":", $.pattern))),
tuple_variant_pattern: ($) =>
seq(
$.rust_path,
"(",
optional(seq($.pattern, repeat(seq(",", $.pattern)), optional(","))),
")",
),
tuple_pattern: ($) =>
seq("(", $.pattern, repeat(seq(",", $.pattern)), optional(","), ")"),
// Expressions
//
// Rust expressions are intentionally kept mostly opaque here. Earlier
// versions modeled Rust expressions recursively (`binary_expression`,
// `method_call`, `statement_block`, etc.), but that gave the LR generator
// too many paths across every Waltzing template-content boundary. The
// compiler remains the authority for Rust expression validity; this grammar
// only needs a stable editor tree around the template syntax.
expression: ($) =>
choice($.binary_expression, $.unary_expression, $.primary_expression),
binary_expression: ($) =>
prec.left(1, seq($.expression, $.binary_operator, $.expression)),
unary_expression: ($) => prec(2, seq($.unary_operator, $.expression)),
choice(
$.template_block,
$.render_closure,
$.primary_expression,
),
primary_expression: ($) =>
choice(
// if/match expressions must come first to match the keywords before rust_path
$.if_expression,
$.match_expression,
$.literal,
$.macro_call,
$.method_call,
$.field_access,
$.index_access,
$.parenthesized_expression,
$.array_literal,
$.closure_expression,
// rust_path last as fallback for identifiers
$.inferred_enum_call,
$.inferred_enum_struct,
$.inferred_enum_path,
$.rust_path,
$.rust_expression,
),
// If expression (returns a value): if cond { expr } else { expr }
if_expression: ($) =>
prec.right(
10,
seq(
"if",
$.expression,
"{",
$.expression,
"}",
"else",
choice(
$.if_expression,
seq("{", $.expression, "}"),
),
),
),
rust_expression: ($) =>
token(prec(-1, /[^{}<@,;)\]\s][^{}<@,;)\]]*/)),
// Match expression (returns a value): match expr { pat => expr, ... }
match_expression: ($) =>
prec.right(
10,
seq(
"match",
$.expression,
"{",
repeat($.expression_match_arm),
"}",
),
),
// Template block - explicit template content in expression position
// Use @{ ... } to create template content as an expression
// Example: @let html = @{ <div>Hello</div> }
template_block: ($) => seq("@", "{", optional($._template_nodes), "}"),
expression_match_arm: ($) =>
// Render closure - inline closure that writes to output target
// Generates: |params..., __wtz_target: &mut _WtzTarget| { template_content }
// Auto-threading: calling a @() variable automatically appends @out
render_closure: ($) =>
seq(
$.pattern,
optional(seq("if", $.expression)),
"=>",
$.expression,
optional(","),
"@",
"(",
optional(seq($.parameter, repeat(seq(",", $.parameter)))),
")",
$.content_block,
),
// Rust macro calls: format!(), vec![], println!(), etc.
macro_call: ($) =>
prec(
5,
seq(
$.identifier,
"!",
choice(
seq("(", optional($.macro_args), ")"),
seq("[", optional($.macro_args), "]"),
seq("{", optional($.macro_args), "}"),
),
),
),
// Macro arguments
macro_args: ($) =>
seq(
$.macro_arg,
repeat(seq(",", $.macro_arg)),
optional(","),
),
// Macro argument - just use expression
macro_arg: ($) => $.expression,
method_call: ($) =>
prec.left(
3,
seq(
$.primary_expression,
".",
$.identifier,
"(",
optional($.argument_list),
")",
),
),
field_access: ($) =>
prec.left(3, seq($.primary_expression, ".", $.identifier)),
index_access: ($) =>
prec.left(3, seq($.primary_expression, "[", $.expression, "]")),
parenthesized_expression: ($) => seq("(", $.expression, ")"),
array_literal: ($) =>
seq(
"[",
optional(
seq($.expression, repeat(seq(",", $.expression)), optional(",")),
),
"]",
),
closure_expression: ($) =>
seq(
"|",
optional($.closure_params),
"|",
choice($.expression, $.content_block),
),
closure_params: ($) => seq($.identifier, repeat(seq(",", $.identifier))),
argument_list: ($) =>
seq($.expression, repeat(seq(",", $.expression)), optional(",")),
// Operators
binary_operator: ($) =>
choice(
"+",
"-",
"*",
"/",
"%",
"==",
"!=",
"<",
">",
"<=",
">=",
"&&",
"||",
"&",
"|",
"^",
"<<",
">>",
),
unary_operator: ($) => choice("!", "-", "*", "&"),
// Literals
literal: ($) =>
choice(
@ -599,8 +644,39 @@ module.exports = grammar({
$.tuple_type,
$.array_type,
$.slice_type,
$.closure_type,
$.render_type,
$.out_type,
$.target_type,
),
// Closure type: |[name: ]Type, ...| [-> ReturnType]
closure_type: ($) =>
seq(
"|",
optional(seq($.closure_param, repeat(seq(",", $.closure_param)))),
"|",
optional(seq("->", $.rust_type)),
),
closure_param: ($) =>
seq(optional(seq($.identifier, ":")), $.rust_type),
// Compiler-injected render callback type - generates impl Fn(T1, T2, ..., &mut _WtzTarget)
// Full syntax: @render(T1, T2, ...)
// Shorthand: @() or @(T1, T2) - equivalent to @render()/@render(T1, T2)
render_type: ($) =>
choice(
seq("@", "render", "(", optional(seq($.rust_type, repeat(seq(",", $.rust_type)))), ")"),
seq("@", "(", optional(seq($.rust_type, repeat(seq(",", $.rust_type)))), ")"),
),
// Compiler-injected output type - generates &mut _WtzTarget
out_type: ($) => seq("@", "Out"),
// Compiler-injected target type - DEPRECATED, use out_type
target_type: ($) => seq("@", "Target"),
primitive_type: ($) =>
choice(
"i8",
@ -646,10 +722,13 @@ module.exports = grammar({
// Comments
comment: ($) => choice($.template_comment, $.html_comment),
// Template comments: @* ... *@ with varying asterisk counts
template_comment: ($) => choice(
...Array.from({ length: 22 }, (_, i) => $[`template_comment_${i + 1}`])
),
// Template comments: @* ... *@.
//
// The compiler supports arbitrary delimiter depth (`@** ... **@`, etc.),
// but modeling each depth as a separate token made error-recovery table
// construction explode. Keep the common editor token here; deeper comments
// still parse as ordinary text/error recovery until a scanner is added.
template_comment: ($) => $.template_comment_1,
template_comment_1: ($) => /@\*([^*]|\*[^@])*\*@/,
template_comment_2: ($) => /@\*\*([^*]|\*[^*]|\*\*[^@])*\*\*@/,
template_comment_3: ($) => /@\*\*\*([^*]|\*[^*]|\*\*[^*]|\*\*\*[^@])*\*\*\*@/,
@ -676,10 +755,11 @@ module.exports = grammar({
// HTML comment: <!-- ... -->
html_comment: ($) => /<!--([^-]|-[^-]|--[^>])*-->/,
// Raw blocks: @# ... #@ with varying hash counts
raw_block: ($) => choice(
...Array.from({ length: 22 }, (_, i) => $[`raw_block_${i + 1}`])
),
// Raw blocks: @# ... #@.
//
// See template_comment: deeper delimiter variants are compiler-supported,
// but keeping 22 separate tokens prevents reliable parser regeneration.
raw_block: ($) => $.raw_block_1,
raw_block_1: ($) => /@#([^#]|#[^@])*#@/,
raw_block_2: ($) => /@##([^#]|#[^#]|##[^@])*##@/,
raw_block_3: ($) => /@###([^#]|#{1,2}[^#]|###[^@])*###@/,

8
package-lock.json generated
View File

@ -12,7 +12,7 @@
"nan": "^2.17.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.24.0"
"tree-sitter-cli": "^0.25.10"
}
},
"node_modules/nan": {
@ -22,9 +22,9 @@
"license": "MIT"
},
"node_modules/tree-sitter-cli": {
"version": "0.24.7",
"resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.24.7.tgz",
"integrity": "sha512-o4gnE82pVmMMhJbWwD6+I9yr4lXii5Ci5qEQ2pFpUbVy1YiD8cizTJaqdcznA0qEbo7l2OneI1GocChPrI4YGQ==",
"version": "0.25.10",
"resolved": "https://registry.npmjs.org/tree-sitter-cli/-/tree-sitter-cli-0.25.10.tgz",
"integrity": "sha512-KoebQguKMCIghisEOdA372TIbrUl0kdnfZ9YQIBRAeOvNSKe85XbU4LuFW7hduRUwJj0rAG7pX5wo9sZhbBF1g==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",

View File

@ -19,7 +19,7 @@
"nan": "^2.17.0"
},
"devDependencies": {
"tree-sitter-cli": "^0.24.0"
"tree-sitter-cli": "^0.25.10"
},
"scripts": {
"build": "tree-sitter generate",

View File

@ -8,19 +8,19 @@
(raw_block) @string.special
; Keywords - only in proper syntactic contexts
(use_statement "@use" @keyword)
(import_statement "@import" @keyword)
(struct_definition "@struct" @keyword)
(enum_definition "@enum" @keyword)
(function_definition "@fn" @keyword)
(let_statement "@let" @keyword)
(if_statement "@if" @keyword)
(for_loop "@for" @keyword)
(match_statement "@match" @keyword)
(break_statement "@break" @keyword)
(continue_statement "@continue" @keyword)
(attribute_if_statement "@if" @keyword)
(attribute_for_loop "@for" @keyword)
(use_statement "@" @keyword "use" @keyword)
(import_statement "@" @keyword "import" @keyword)
(struct_definition "@" @keyword "struct" @keyword)
(enum_definition "@" @keyword "enum" @keyword)
(function_definition "@" @keyword "fn" @keyword)
(let_statement "@" @keyword "let" @keyword)
(if_statement "@" @keyword "if" @keyword)
(for_loop "@" @keyword "for" @keyword)
(match_statement "@" @keyword "match" @keyword)
(break_statement "@" @keyword "break" @keyword)
(continue_statement "@" @keyword "continue" @keyword)
(attribute_if_statement "@" @keyword "if" @keyword)
(attribute_for_loop "@" @keyword "for" @keyword)
(else_if_branch "else" @keyword)
(else_if_branch "if" @keyword)
(else_branch "else" @keyword)
@ -39,15 +39,14 @@
; Functions
(function_definition (identifier) @fn)
(function_path) @fn
(method_call (identifier) @fn)
; Parameters
(parameter (identifier) @variable)
; Properties
(struct_field (identifier) @property)
(field_access (identifier) @property)
(field_pattern (identifier) @property)
(struct_field_init (identifier) @property)
; Constructors
(enum_variant (identifier) @constructor)
@ -65,8 +64,6 @@
(boolean_literal) @boolean
; Operators
(binary_operator) @operator
(unary_operator) @operator
"=>" @operator
"=" @operator

File diff suppressed because it is too large Load Diff

View File

@ -14,21 +14,6 @@
]
}
},
{
"type": "array_literal",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "array_type",
"named": true,
@ -143,6 +128,11 @@
]
}
},
{
"type": "attribute_name",
"named": true,
"fields": {}
},
{
"type": "attribute_or_control",
"named": true,
@ -200,30 +190,6 @@
]
}
},
{
"type": "binary_expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "binary_operator",
"named": true
},
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "binary_operator",
"named": true,
"fields": {}
},
{
"type": "boolean_attribute",
"named": true,
@ -275,30 +241,7 @@
}
},
{
"type": "closure_expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "closure_params",
"named": true
},
{
"type": "content_block",
"named": true
},
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "closure_params",
"type": "closure_param",
"named": true,
"fields": {},
"children": {
@ -308,6 +251,29 @@
{
"type": "identifier",
"named": true
},
{
"type": "rust_type",
"named": true
}
]
}
},
{
"type": "closure_type",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "closure_param",
"named": true
},
{
"type": "rust_type",
"named": true
}
]
}
@ -521,16 +487,16 @@
"multiple": false,
"required": true,
"types": [
{
"type": "binary_expression",
"named": true
},
{
"type": "primary_expression",
"named": true
},
{
"type": "unary_expression",
"type": "render_closure",
"named": true
},
{
"type": "template_block",
"named": true
}
]
@ -559,25 +525,6 @@
]
}
},
{
"type": "field_access",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "primary_expression",
"named": true
}
]
}
},
{
"type": "field_pattern",
"named": true,
@ -624,6 +571,25 @@
]
}
},
{
"type": "format_expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "string_literal",
"named": true
}
]
}
},
{
"type": "function_attribute",
"named": true,
@ -659,6 +625,10 @@
"type": "expression_path",
"named": true
},
{
"type": "render_closure",
"named": true
},
{
"type": "string_literal",
"named": true
@ -863,7 +833,7 @@
}
},
{
"type": "index_access",
"type": "inferred_enum_call",
"named": true,
"fields": {},
"children": {
@ -871,11 +841,30 @@
"required": true,
"types": [
{
"type": "expression",
"type": "argument_list",
"named": true
},
{
"type": "primary_expression",
"type": "inferred_enum_path",
"named": true
}
]
}
},
{
"type": "inferred_enum_struct",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "inferred_enum_path",
"named": true
},
{
"type": "struct_field_init",
"named": true
}
]
@ -904,7 +893,7 @@
"named": true
},
{
"type": "pattern",
"type": "simple_pattern",
"named": true
}
]
@ -980,7 +969,7 @@
}
},
{
"type": "method_call",
"type": "matches_expression",
"named": true,
"fields": {},
"children": {
@ -988,15 +977,11 @@
"required": true,
"types": [
{
"type": "argument_list",
"type": "expression",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "primary_expression",
"type": "pattern",
"named": true
}
]
@ -1040,6 +1025,16 @@
]
}
},
{
"type": "out_ref",
"named": true,
"fields": {}
},
{
"type": "out_type",
"named": true,
"fields": {}
},
{
"type": "parameter",
"named": true,
@ -1078,21 +1073,6 @@
]
}
},
{
"type": "parenthesized_expression",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "path_type",
"named": true,
@ -1132,6 +1112,10 @@
"type": "tuple_pattern",
"named": true
},
{
"type": "tuple_variant_pattern",
"named": true
},
{
"type": "wildcard_pattern",
"named": true
@ -1148,19 +1132,15 @@
"required": true,
"types": [
{
"type": "array_literal",
"type": "inferred_enum_call",
"named": true
},
{
"type": "closure_expression",
"type": "inferred_enum_path",
"named": true
},
{
"type": "field_access",
"named": true
},
{
"type": "index_access",
"type": "inferred_enum_struct",
"named": true
},
{
@ -1168,11 +1148,7 @@
"named": true
},
{
"type": "method_call",
"named": true
},
{
"type": "parenthesized_expression",
"type": "rust_expression",
"named": true
},
{
@ -1187,6 +1163,21 @@
"named": true,
"fields": {}
},
{
"type": "raw_block",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "raw_block_1",
"named": true
}
]
}
},
{
"type": "reference_type",
"named": true,
@ -1202,6 +1193,40 @@
]
}
},
{
"type": "render_closure",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "content_block",
"named": true
},
{
"type": "parameter",
"named": true
}
]
}
},
{
"type": "render_type",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "rust_type",
"named": true
}
]
}
},
{
"type": "rust_path",
"named": true,
@ -1219,10 +1244,18 @@
"type": "array_type",
"named": true
},
{
"type": "closure_type",
"named": true
},
{
"type": "generic_type",
"named": true
},
{
"type": "out_type",
"named": true
},
{
"type": "path_type",
"named": true
@ -1235,10 +1268,18 @@
"type": "reference_type",
"named": true
},
{
"type": "render_type",
"named": true
},
{
"type": "slice_type",
"named": true
},
{
"type": "target_type",
"named": true
},
{
"type": "tuple_type",
"named": true
@ -1286,7 +1327,7 @@
"fields": {},
"children": {
"multiple": false,
"required": true,
"required": false,
"types": [
{
"type": "expression_path",
@ -1402,6 +1443,25 @@
]
}
},
{
"type": "struct_field_init",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "struct_pattern",
"named": true,
@ -1421,6 +1481,16 @@
]
}
},
{
"type": "target_ref",
"named": true,
"fields": {}
},
{
"type": "target_type",
"named": true,
"fields": {}
},
{
"type": "template",
"named": true,
@ -1437,6 +1507,36 @@
]
}
},
{
"type": "template_block",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "template_node",
"named": true
}
]
}
},
{
"type": "template_comment",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "template_comment_1",
"named": true
}
]
}
},
{
"type": "template_control_flow",
"named": true,
@ -1519,6 +1619,18 @@
"type": "complex_expression",
"named": true
},
{
"type": "format_expression",
"named": true
},
{
"type": "matches_expression",
"named": true
},
{
"type": "out_ref",
"named": true
},
{
"type": "safe_expression",
"named": true
@ -1526,6 +1638,10 @@
{
"type": "simple_expression",
"named": true
},
{
"type": "target_ref",
"named": true
}
]
}
@ -1607,6 +1723,25 @@
]
}
},
{
"type": "tuple_variant_pattern",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "pattern",
"named": true
},
{
"type": "rust_path",
"named": true
}
]
}
},
{
"type": "type_expression",
"named": true,
@ -1622,30 +1757,6 @@
]
}
},
{
"type": "unary_expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "unary_operator",
"named": true
}
]
}
},
{
"type": "unary_operator",
"named": true,
"fields": {}
},
{
"type": "use_statement",
"named": true,
@ -1669,26 +1780,14 @@
"type": "!",
"named": false
},
{
"type": "!=",
"named": false
},
{
"type": "\"",
"named": false
},
{
"type": "%",
"named": false
},
{
"type": "&",
"named": false
},
{
"type": "&&",
"named": false
},
{
"type": "'",
"named": false
@ -1701,20 +1800,12 @@
"type": ")",
"named": false
},
{
"type": "*",
"named": false
},
{
"type": "+",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": "-",
"type": "->",
"named": false
},
{
@ -1749,14 +1840,6 @@
"type": "</@",
"named": false
},
{
"type": "<<",
"named": false
},
{
"type": "<=",
"named": false
},
{
"type": "<@",
"named": false
@ -1765,10 +1848,6 @@
"type": "=",
"named": false
},
{
"type": "==",
"named": false
},
{
"type": "=>",
"named": false
@ -1777,14 +1856,6 @@
"type": ">",
"named": false
},
{
"type": ">=",
"named": false
},
{
"type": ">>",
"named": false
},
{
"type": "@",
"named": false
@ -1794,53 +1865,17 @@
"named": false
},
{
"type": "@break",
"named": false
},
{
"type": "@continue",
"named": false
},
{
"type": "@enum",
"named": false
},
{
"type": "@fn",
"named": false
},
{
"type": "@for",
"named": false
},
{
"type": "@if",
"named": false
},
{
"type": "@import",
"named": false
},
{
"type": "@let",
"named": false
},
{
"type": "@match",
"named": false
},
{
"type": "@struct",
"named": false
},
{
"type": "@use",
"type": "Out",
"named": false
},
{
"type": "String",
"named": false
},
{
"type": "Target",
"named": false
},
{
"type": "[",
"named": false
@ -1849,10 +1884,6 @@
"type": "]",
"named": false
},
{
"type": "^",
"named": false
},
{
"type": "```@",
"named": false
@ -1869,18 +1900,22 @@
"type": "attribute_content",
"named": true
},
{
"type": "attribute_name",
"named": true
},
{
"type": "bool",
"named": false
},
{
"type": "break",
"named": false
},
{
"type": "char",
"named": false
},
{
"type": "continue",
"named": false
},
{
"type": "css",
"named": false
@ -1889,6 +1924,10 @@
"type": "else",
"named": false
},
{
"type": "enum",
"named": false
},
{
"type": "escape_at",
"named": true
@ -1913,6 +1952,18 @@
"type": "float_literal",
"named": true
},
{
"type": "fn",
"named": false
},
{
"type": "for",
"named": false
},
{
"type": "format",
"named": false
},
{
"type": "html",
"named": false
@ -1949,6 +2000,10 @@
"type": "if",
"named": false
},
{
"type": "import",
"named": false
},
{
"type": "import_path",
"named": true
@ -1957,6 +2012,10 @@
"type": "in",
"named": false
},
{
"type": "inferred_enum_path",
"named": true
},
{
"type": "isize",
"named": false
@ -1977,12 +2036,32 @@
"type": "let",
"named": false
},
{
"type": "match",
"named": false
},
{
"type": "matches",
"named": false
},
{
"type": "mut",
"named": false
},
{
"type": "raw_block",
"type": "out",
"named": false
},
{
"type": "raw_block_1",
"named": true
},
{
"type": "render",
"named": false
},
{
"type": "rust_expression",
"named": true
},
{
@ -1993,6 +2072,10 @@
"type": "str",
"named": false
},
{
"type": "struct",
"named": false
},
{
"type": "style",
"named": false
@ -2002,7 +2085,11 @@
"named": true
},
{
"type": "template_comment",
"type": "target",
"named": false
},
{
"type": "template_comment_1",
"named": true
},
{
@ -2037,6 +2124,10 @@
"type": "unquoted_value",
"named": true
},
{
"type": "use",
"named": false
},
{
"type": "usize",
"named": false
@ -2053,10 +2144,6 @@
"type": "|",
"named": false
},
{
"type": "||",
"named": false
},
{
"type": "}",
"named": false

45288
src/parser.c

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,6 @@ typedef uint16_t TSStateId;
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
typedef struct TSLanguageMetadata TSLanguageMetadata;
typedef struct TSLanguageMetadata {
uint8_t major_version;
uint8_t minor_version;

90
test/corpus/basic.txt Normal file
View File

@ -0,0 +1,90 @@
==================
empty function
==================
@fn empty() { }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list)
(content_block))))
==================
use statement
==================
@use std::collections::HashMap
---
(template
(template_element
(use_statement
(rust_path))))
==================
import with alias
==================
@import "/components/button" as btn
---
(template
(template_element
(import_statement
(string_literal)
(identifier))))
==================
struct definition
==================
@struct User {
name: String,
age: u32,
}
---
(template
(template_element
(struct_definition
(identifier)
(struct_field
(identifier)
(type_expression
(rust_type
(primitive_type))))
(struct_field
(identifier)
(type_expression
(rust_type
(primitive_type)))))))
==================
html element with text
==================
@fn greet() {
<h1>Hello</h1>
}
---
(template
(template_element
(function_definition
(identifier)
(parameter_list)
(content_block
(template_node
(html_element
(tag_name)
(template_node
(text_content))
(tag_name)))))))

View File

@ -0,0 +1,150 @@
==================
@if statement
==================
@fn show(x: bool) { @if x { <p>Yes</p> } }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(primitive_type))))
(content_block
(template_node
(template_control_flow
(if_statement
(expression
(primary_expression
(rust_path)))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(text_content))
(tag_name)))))))))))
==================
@for loop
==================
@fn loop(items: Vec<u32>) { @for n in items { <li>@n</li> } }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(generic_type
(rust_path)
(rust_type
(primitive_type))))))
(content_block
(template_node
(template_control_flow
(for_loop
(simple_pattern
(identifier_pattern
(identifier)))
(expression
(primary_expression
(rust_path)))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(template_expression
(simple_expression
(expression_path
(identifier)))))
(tag_name)))))))))))
==================
@match with literal and wildcard
==================
@fn pick(v: u32) { @match v { 0 => { <p>zero</p> }, _ => { <p>other</p> } } }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(primitive_type))))
(content_block
(template_node
(template_control_flow
(match_statement
(expression
(primary_expression
(rust_path)))
(match_arm
(pattern
(literal
(number_literal
(integer_literal))))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(text_content))
(tag_name)))))
(match_arm
(pattern
(wildcard_pattern))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(text_content))
(tag_name))))))))))))
==================
@if in HTML attribute position
==================
@fn cb(active: bool) { <input @if active { checked } /> }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(primitive_type))))
(content_block
(template_node
(html_element
(tag_name)
(attribute_or_control
(attribute_control_flow
(attribute_if_statement
(expression
(primary_expression
(rust_path)))
(attribute_or_control
(html_attribute
(attribute_name))))))))))))

159
test/corpus/features.txt Normal file
View File

@ -0,0 +1,159 @@
==================
format expression
==================
@fn fmt(name: String) { <p>@format("Hello {}", name)</p> }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(primitive_type))))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(template_expression
(format_expression
(string_literal)
(expression
(primary_expression
(rust_path))))))
(tag_name)))))))
==================
matches expression
==================
@fn check(v: Option<u32>) { <p>@matches(v, Some(_))</p> }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(generic_type
(rust_path)
(rust_type
(primitive_type))))))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(template_expression
(matches_expression
(expression
(primary_expression
(rust_path)))
(pattern
(tuple_variant_pattern
(rust_path)
(pattern
(wildcard_pattern)))))))
(tag_name)))))))
==================
inferred enum path
==================
@fn status(s: Status) { <p>@(::Active)</p> }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list
(parameter
(identifier)
(rust_type
(path_type
(rust_path)))))
(content_block
(template_node
(html_element
(tag_name)
(template_node
(template_expression
(complex_expression
(expression
(primary_expression
(inferred_enum_path))))))
(tag_name)))))))
==================
inferred enum tuple variant
==================
@fn call() { <p>@(::Some(1))</p> }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list)
(content_block
(template_node
(html_element
(tag_name)
(template_node
(template_expression
(complex_expression
(expression
(primary_expression
(inferred_enum_call
(inferred_enum_path)
(argument_list
(expression
(primary_expression
(literal
(number_literal
(integer_literal))))))))))))
(tag_name)))))))
==================
inferred enum struct variant
==================
@fn data() { @let x = ::User { name: "A" }; }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list)
(content_block
(template_node
(template_control_flow
(let_statement
(simple_pattern
(identifier_pattern
(identifier)))
(expression
(primary_expression
(inferred_enum_struct
(inferred_enum_path)
(struct_field_init
(identifier)
(expression
(primary_expression
(literal
(string_literal)))))))))))))))

View File

@ -0,0 +1,25 @@
==================
@let statement
==================
@fn calc() { @let n = 42; }
---
(template
(template_element
(function_definition
(identifier)
(parameter_list)
(content_block
(template_node
(template_control_flow
(let_statement
(simple_pattern
(identifier_pattern
(identifier)))
(expression
(primary_expression
(literal
(number_literal
(integer_literal))))))))))))