const GO = require("tree-sitter-go/grammar") // NOTE(vincent): we base this grammar on the Go grammar because a templ file is basically a Go file except for components and css expressions. // // If you see a rule mentioned below but do _not_ see it in the rules, go look at the Go grammar. module.exports = grammar(GO, { name: 'templ', externals: $ => [ $.css_property_value, $.element_text, $.element_comment, $.style_element_text, $.script_block_text, $.script_element_text, $.switch_element_text, ], conflicts: ($, original) => [ ...original, [$._expression, $.dynamic_class_attribute_value], ], rules: { _top_level_declaration: ($, original) => choice( original, $.component_declaration, $.css_declaration, $.script_declaration, ), // This matches a templ expression: // //
// { first.Name } //
// } rawgo_block: $ => seq( '{{', optional($._statement_list), '}}', ), // package_identifier: $ => alias($.identifier, $.package_identifier), _component_identifier: $ => alias($.identifier, $.component_identifier), _css_identifier: $ => alias($.identifier, $.css_identifier), _script_identifier: $ => alias($.identifier, $.script_identifier), element_identifier: $ => /[a-zA-Z0-9\-]+/, // Taken from https://github.com/tree-sitter/tree-sitter-html/blob/master/grammar.js attribute_name: _ => /[^<>"'/=\s]+/, attribute_value: _ => /[^{}<>"'=\s]+/, quoted_attribute_value: $ => choice( seq('\'', optional(alias(/[^']+/, $.attribute_value)), '\''), seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"'), ), text: _ => /[^<>&{}\s]([^<>&{}]*[^<>&\s{}])?/, // Taken from https://github.com/tree-sitter/tree-sitter-go/blob/master/grammar.js literal_value: $ => seq( '{', optional( seq( commaSep(choice($.literal_element, $.keyed_element)), optional(','))), '}', ), literal_element: $ => choice($._expression, $.literal_value), }, }); // Taken from https://github.com/tree-sitter/tree-sitter-go/blob/master/grammar.js#L909-L915 function commaSep1(rule) { return seq(rule, repeat(seq(',', rule))) } function commaSep(rule) { return optional(commaSep1(rule)) }