Initial commit
This commit is contained in:
commit
56f54d9bcb
|
|
@ -0,0 +1 @@
|
||||||
|
/node_modules
|
||||||
|
|
@ -0,0 +1,544 @@
|
||||||
|
/// <reference types="tree-sitter-cli/dsl" />
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
module.exports = grammar({
|
||||||
|
name: "waltzing",
|
||||||
|
|
||||||
|
extras: ($) => [/\s/],
|
||||||
|
|
||||||
|
conflicts: ($) => [
|
||||||
|
[$.expression, $.pattern],
|
||||||
|
[$.rust_path, $.expression],
|
||||||
|
[$.struct_pattern, $.expression],
|
||||||
|
[$.html_element],
|
||||||
|
[$.self_closing_function_tag, $.container_function_tag],
|
||||||
|
],
|
||||||
|
|
||||||
|
rules: {
|
||||||
|
// Root rule
|
||||||
|
template: ($) => repeat($.template_element),
|
||||||
|
|
||||||
|
template_element: ($) =>
|
||||||
|
choice(
|
||||||
|
$.use_statement,
|
||||||
|
$.import_statement,
|
||||||
|
$.struct_definition,
|
||||||
|
$.enum_definition,
|
||||||
|
$.function_definition,
|
||||||
|
$.template_node,
|
||||||
|
),
|
||||||
|
|
||||||
|
// Rust imports
|
||||||
|
use_statement: ($) =>
|
||||||
|
seq("@use", $.rust_path, optional(seq("as", $.identifier))),
|
||||||
|
|
||||||
|
// Use token() to properly handle :: in paths
|
||||||
|
rust_path: ($) =>
|
||||||
|
token(
|
||||||
|
seq(
|
||||||
|
/[a-zA-Z_][a-zA-Z0-9_]*/,
|
||||||
|
repeat(seq("::", /[a-zA-Z_][a-zA-Z0-9_]*/)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Template imports
|
||||||
|
import_statement: ($) =>
|
||||||
|
seq("@import", $.string_literal, "as", $.identifier),
|
||||||
|
|
||||||
|
// Struct definition
|
||||||
|
struct_definition: ($) =>
|
||||||
|
seq(
|
||||||
|
"@struct",
|
||||||
|
optional($.attribute_list),
|
||||||
|
$.identifier,
|
||||||
|
optional($.generic_params),
|
||||||
|
"{",
|
||||||
|
repeat($.struct_field),
|
||||||
|
"}",
|
||||||
|
),
|
||||||
|
|
||||||
|
struct_field: ($) =>
|
||||||
|
seq(
|
||||||
|
$.identifier,
|
||||||
|
optional($.attribute_list),
|
||||||
|
":",
|
||||||
|
$.type_expression,
|
||||||
|
optional(","),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Enum definition
|
||||||
|
enum_definition: ($) =>
|
||||||
|
seq(
|
||||||
|
"@enum",
|
||||||
|
optional($.attribute_list),
|
||||||
|
$.identifier,
|
||||||
|
optional($.generic_params),
|
||||||
|
"{",
|
||||||
|
repeat($.enum_variant),
|
||||||
|
"}",
|
||||||
|
),
|
||||||
|
|
||||||
|
enum_variant: ($) =>
|
||||||
|
seq(
|
||||||
|
$.identifier,
|
||||||
|
optional(seq("(", repeat(seq($.type_expression, optional(","))), ")")),
|
||||||
|
optional(","),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Attribute list
|
||||||
|
attribute_list: ($) =>
|
||||||
|
seq("[", optional(seq($.attribute, repeat(seq(",", $.attribute)))), "]"),
|
||||||
|
|
||||||
|
attribute: ($) =>
|
||||||
|
seq($.identifier, optional(seq("(", $.attribute_content, ")"))),
|
||||||
|
|
||||||
|
attribute_content: ($) => /[^)]*/,
|
||||||
|
|
||||||
|
generic_params: ($) =>
|
||||||
|
seq("<", $.identifier, repeat(seq(",", $.identifier)), ">"),
|
||||||
|
|
||||||
|
type_expression: ($) => $.rust_type,
|
||||||
|
|
||||||
|
// Function definition
|
||||||
|
function_definition: ($) =>
|
||||||
|
seq("@func", $.identifier, $.parameter_list, $.content_block),
|
||||||
|
|
||||||
|
parameter_list: ($) =>
|
||||||
|
seq(
|
||||||
|
"(",
|
||||||
|
optional(
|
||||||
|
seq($.parameter, repeat(seq(",", $.parameter)), optional(",")),
|
||||||
|
),
|
||||||
|
")",
|
||||||
|
),
|
||||||
|
|
||||||
|
parameter: ($) =>
|
||||||
|
seq($.identifier, ":", $.rust_type, optional(seq("=", $.default_value))),
|
||||||
|
|
||||||
|
default_value: ($) => $.expression,
|
||||||
|
|
||||||
|
content_block: ($) => seq("{", repeat($.template_node), "}"),
|
||||||
|
|
||||||
|
// Template nodes
|
||||||
|
template_node: ($) =>
|
||||||
|
choice(
|
||||||
|
$.html_element,
|
||||||
|
$.function_tag,
|
||||||
|
$.template_expression,
|
||||||
|
$.template_control_flow,
|
||||||
|
$.comment,
|
||||||
|
$.embedded_language,
|
||||||
|
$.escape_at,
|
||||||
|
$.text_content,
|
||||||
|
),
|
||||||
|
|
||||||
|
// HTML elements
|
||||||
|
html_element: ($) =>
|
||||||
|
choice(
|
||||||
|
// Self-closing tag
|
||||||
|
seq("<", $.tag_name, repeat($.html_attribute), "/", ">"),
|
||||||
|
// Void elements (no closing tag needed)
|
||||||
|
seq("<", $.tag_name, repeat($.html_attribute), ">"),
|
||||||
|
// Full element with content and closing tag
|
||||||
|
seq(
|
||||||
|
"<",
|
||||||
|
$.tag_name,
|
||||||
|
repeat($.html_attribute),
|
||||||
|
">",
|
||||||
|
repeat($.template_node),
|
||||||
|
"</",
|
||||||
|
$.tag_name,
|
||||||
|
">",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
tag_name: ($) => /[a-zA-Z][a-zA-Z0-9-]*/,
|
||||||
|
|
||||||
|
html_attribute: ($) =>
|
||||||
|
seq($.attribute_name, optional(seq("=", $.attribute_value))),
|
||||||
|
|
||||||
|
attribute_name: ($) => /[a-zA-Z_:@][a-zA-Z0-9_:.-]*/,
|
||||||
|
|
||||||
|
attribute_value: ($) =>
|
||||||
|
choice(
|
||||||
|
$.string_literal,
|
||||||
|
$.template_expression,
|
||||||
|
seq("@", "{", $.expression, "}"),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Template expressions
|
||||||
|
template_expression: ($) =>
|
||||||
|
choice($.simple_expression, $.complex_expression, $.safe_expression),
|
||||||
|
|
||||||
|
simple_expression: ($) => seq("@", $.expression_path),
|
||||||
|
|
||||||
|
complex_expression: ($) => seq("@", "(", $.expression, ")"),
|
||||||
|
|
||||||
|
safe_expression: ($) =>
|
||||||
|
seq(
|
||||||
|
"@",
|
||||||
|
"safe",
|
||||||
|
"(",
|
||||||
|
$.expression,
|
||||||
|
optional(seq(",", $.expression)),
|
||||||
|
")",
|
||||||
|
),
|
||||||
|
|
||||||
|
expression_path: ($) =>
|
||||||
|
seq(
|
||||||
|
$.identifier,
|
||||||
|
repeat(
|
||||||
|
choice(
|
||||||
|
seq(".", $.identifier),
|
||||||
|
seq("[", $.expression, "]"),
|
||||||
|
seq("(", optional($.argument_list), ")"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Control flow
|
||||||
|
template_control_flow: ($) =>
|
||||||
|
choice(
|
||||||
|
$.if_statement,
|
||||||
|
$.for_loop,
|
||||||
|
$.match_statement,
|
||||||
|
$.break_statement,
|
||||||
|
$.continue_statement,
|
||||||
|
),
|
||||||
|
|
||||||
|
if_statement: ($) =>
|
||||||
|
seq(
|
||||||
|
"@if",
|
||||||
|
optional(seq("let", $.pattern, "=")),
|
||||||
|
$.expression,
|
||||||
|
$.content_block,
|
||||||
|
repeat($.else_if_branch),
|
||||||
|
optional($.else_branch),
|
||||||
|
),
|
||||||
|
|
||||||
|
else_if_branch: ($) =>
|
||||||
|
seq(
|
||||||
|
"else",
|
||||||
|
"if",
|
||||||
|
optional(seq("let", $.pattern, "=")),
|
||||||
|
$.expression,
|
||||||
|
$.content_block,
|
||||||
|
),
|
||||||
|
|
||||||
|
else_branch: ($) => seq("else", $.content_block),
|
||||||
|
|
||||||
|
for_loop: ($) =>
|
||||||
|
seq(
|
||||||
|
"@for",
|
||||||
|
optional(seq($.identifier, ":")),
|
||||||
|
$.pattern,
|
||||||
|
"in",
|
||||||
|
$.expression,
|
||||||
|
$.content_block,
|
||||||
|
),
|
||||||
|
|
||||||
|
match_statement: ($) =>
|
||||||
|
seq("@match", $.expression, "{", repeat($.match_arm), "}"),
|
||||||
|
|
||||||
|
match_arm: ($) =>
|
||||||
|
seq(
|
||||||
|
$.pattern,
|
||||||
|
optional(seq("if", $.expression)),
|
||||||
|
"=>",
|
||||||
|
$.content_block,
|
||||||
|
optional(","),
|
||||||
|
),
|
||||||
|
|
||||||
|
break_statement: ($) =>
|
||||||
|
seq("@break", optional(seq(":", $.identifier)), optional(";")),
|
||||||
|
|
||||||
|
continue_statement: ($) =>
|
||||||
|
seq("@continue", optional(seq(":", $.identifier)), optional(";")),
|
||||||
|
|
||||||
|
// Function tags
|
||||||
|
function_tag: ($) =>
|
||||||
|
choice($.self_closing_function_tag, $.container_function_tag),
|
||||||
|
|
||||||
|
self_closing_function_tag: ($) =>
|
||||||
|
seq(
|
||||||
|
"<@",
|
||||||
|
$.function_path,
|
||||||
|
repeat($.function_attribute),
|
||||||
|
optional("/"),
|
||||||
|
">",
|
||||||
|
),
|
||||||
|
|
||||||
|
container_function_tag: ($) =>
|
||||||
|
seq(
|
||||||
|
"<@",
|
||||||
|
$.function_path,
|
||||||
|
repeat($.function_attribute),
|
||||||
|
">",
|
||||||
|
repeat($.template_node),
|
||||||
|
"</@",
|
||||||
|
$.function_path,
|
||||||
|
">",
|
||||||
|
),
|
||||||
|
|
||||||
|
function_path: ($) =>
|
||||||
|
token(
|
||||||
|
seq(
|
||||||
|
/[a-zA-Z_][a-zA-Z0-9_]*/,
|
||||||
|
repeat(seq("::", /[a-zA-Z_][a-zA-Z0-9_]*/)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
|
||||||
|
function_attribute: ($) =>
|
||||||
|
choice(
|
||||||
|
$.attribute_reference,
|
||||||
|
$.named_function_attribute,
|
||||||
|
$.boolean_attribute,
|
||||||
|
),
|
||||||
|
|
||||||
|
attribute_reference: ($) => seq("@", optional("&"), $.identifier),
|
||||||
|
|
||||||
|
named_function_attribute: ($) =>
|
||||||
|
seq($.identifier, "=", $.function_attribute_value),
|
||||||
|
|
||||||
|
boolean_attribute: ($) => $.identifier,
|
||||||
|
|
||||||
|
function_attribute_value: ($) =>
|
||||||
|
choice($.string_literal, seq("@", $.expression_path), $.unquoted_value),
|
||||||
|
|
||||||
|
unquoted_value: ($) => /[^\s>=\/]+/,
|
||||||
|
|
||||||
|
// Patterns
|
||||||
|
pattern: ($) =>
|
||||||
|
choice(
|
||||||
|
$.wildcard_pattern,
|
||||||
|
$.tuple_pattern,
|
||||||
|
$.struct_pattern,
|
||||||
|
$.identifier_pattern,
|
||||||
|
$.literal,
|
||||||
|
),
|
||||||
|
|
||||||
|
wildcard_pattern: ($) => "_",
|
||||||
|
|
||||||
|
identifier_pattern: ($) => $.identifier,
|
||||||
|
|
||||||
|
struct_pattern: ($) =>
|
||||||
|
seq(
|
||||||
|
$.rust_path,
|
||||||
|
"{",
|
||||||
|
repeat(seq($.field_pattern, optional(","))),
|
||||||
|
optional(".."),
|
||||||
|
"}",
|
||||||
|
),
|
||||||
|
|
||||||
|
field_pattern: ($) => seq($.identifier, optional(seq(":", $.pattern))),
|
||||||
|
|
||||||
|
tuple_pattern: ($) =>
|
||||||
|
seq("(", $.pattern, repeat(seq(",", $.pattern)), optional(","), ")"),
|
||||||
|
|
||||||
|
// Expressions
|
||||||
|
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)),
|
||||||
|
|
||||||
|
primary_expression: ($) =>
|
||||||
|
choice(
|
||||||
|
$.literal,
|
||||||
|
$.rust_path,
|
||||||
|
$.method_call,
|
||||||
|
$.field_access,
|
||||||
|
$.index_access,
|
||||||
|
$.parenthesized_expression,
|
||||||
|
$.array_literal,
|
||||||
|
$.closure_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(
|
||||||
|
$.string_literal,
|
||||||
|
$.char_literal,
|
||||||
|
$.number_literal,
|
||||||
|
$.boolean_literal,
|
||||||
|
),
|
||||||
|
|
||||||
|
string_literal: ($) =>
|
||||||
|
seq('"', repeat(choice(/[^"\\]/, $.escape_sequence)), '"'),
|
||||||
|
|
||||||
|
char_literal: ($) => seq("'", choice(/[^'\\]/, $.escape_sequence), "'"),
|
||||||
|
|
||||||
|
escape_sequence: ($) =>
|
||||||
|
/\\[nrt\\'\"0]|\\x[0-9a-fA-F]{2}|\\u\{[0-9a-fA-F]+\}/,
|
||||||
|
|
||||||
|
number_literal: ($) => choice($.integer_literal, $.float_literal),
|
||||||
|
|
||||||
|
integer_literal: ($) =>
|
||||||
|
choice(
|
||||||
|
/[0-9][0-9_]*(i8|i16|i32|i64|i128|isize|u8|u16|u32|u64|u128|usize)?/,
|
||||||
|
/0x[0-9a-fA-F][0-9a-fA-F_]*(i8|i16|i32|i64|i128|isize|u8|u16|u32|u64|u128|usize)?/,
|
||||||
|
/0o[0-7][0-7_]*(i8|i16|i32|i64|i128|isize|u8|u16|u32|u64|u128|usize)?/,
|
||||||
|
/0b[01][01_]*(i8|i16|i32|i64|i128|isize|u8|u16|u32|u64|u128|usize)?/,
|
||||||
|
),
|
||||||
|
|
||||||
|
float_literal: ($) => /[0-9][0-9_]*\.[0-9_]*([eE][+-]?[0-9]+)?(f32|f64)?/,
|
||||||
|
|
||||||
|
boolean_literal: ($) => choice("true", "false"),
|
||||||
|
|
||||||
|
// Rust types
|
||||||
|
rust_type: ($) =>
|
||||||
|
choice(
|
||||||
|
$.primitive_type,
|
||||||
|
$.reference_type,
|
||||||
|
$.generic_type,
|
||||||
|
$.path_type,
|
||||||
|
$.tuple_type,
|
||||||
|
$.array_type,
|
||||||
|
$.slice_type,
|
||||||
|
),
|
||||||
|
|
||||||
|
primitive_type: ($) =>
|
||||||
|
choice(
|
||||||
|
"i8",
|
||||||
|
"i16",
|
||||||
|
"i32",
|
||||||
|
"i64",
|
||||||
|
"i128",
|
||||||
|
"isize",
|
||||||
|
"u8",
|
||||||
|
"u16",
|
||||||
|
"u32",
|
||||||
|
"u64",
|
||||||
|
"u128",
|
||||||
|
"usize",
|
||||||
|
"f32",
|
||||||
|
"f64",
|
||||||
|
"bool",
|
||||||
|
"char",
|
||||||
|
"str",
|
||||||
|
"String",
|
||||||
|
),
|
||||||
|
|
||||||
|
reference_type: ($) => seq("&", optional("mut"), $.rust_type),
|
||||||
|
|
||||||
|
generic_type: ($) =>
|
||||||
|
seq($.rust_path, "<", $.rust_type, repeat(seq(",", $.rust_type)), ">"),
|
||||||
|
|
||||||
|
path_type: ($) => $.rust_path,
|
||||||
|
|
||||||
|
tuple_type: ($) =>
|
||||||
|
seq(
|
||||||
|
"(",
|
||||||
|
optional(
|
||||||
|
seq($.rust_type, repeat(seq(",", $.rust_type)), optional(",")),
|
||||||
|
),
|
||||||
|
")",
|
||||||
|
),
|
||||||
|
|
||||||
|
array_type: ($) => seq("[", $.rust_type, ";", $.expression, "]"),
|
||||||
|
|
||||||
|
slice_type: ($) => seq("[", $.rust_type, "]"),
|
||||||
|
|
||||||
|
// Comments
|
||||||
|
comment: ($) => choice($.template_comment, $.html_comment),
|
||||||
|
|
||||||
|
// Template comment: @* ... *@
|
||||||
|
template_comment: ($) => seq("@*", optional($.comment_content), "*@"),
|
||||||
|
|
||||||
|
comment_content: ($) => /([^*]|\*[^@])*/,
|
||||||
|
|
||||||
|
// HTML comment: <!-- ... -->
|
||||||
|
html_comment: ($) => seq("<!--", optional($.html_comment_content), "-->"),
|
||||||
|
|
||||||
|
html_comment_content: ($) => /([^-]|-[^-]|--[^>])*/,
|
||||||
|
|
||||||
|
// Embedded language blocks: @```lang ... ```@
|
||||||
|
embedded_language: ($) =>
|
||||||
|
seq("@```", $.language_name, optional($.embedded_content), "```@"),
|
||||||
|
|
||||||
|
embedded_content: ($) => /([^`]|`[^`]|``[^`])*/,
|
||||||
|
|
||||||
|
language_name: ($) =>
|
||||||
|
choice("html", "css", "js", "javascript", "json", "alpine", "style"),
|
||||||
|
|
||||||
|
// Escape sequence for literal @
|
||||||
|
escape_at: ($) => "@@",
|
||||||
|
|
||||||
|
// Text content - use negative precedence to prefer other rules
|
||||||
|
// Exclude {} to avoid capturing control flow elements
|
||||||
|
text_content: ($) => token(prec(-1, /[^<@{}\s][^<@{}]*/)),
|
||||||
|
|
||||||
|
// Identifier
|
||||||
|
identifier: ($) => /[a-zA-Z_][a-zA-Z0-9_]*/,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
{
|
||||||
|
"name": "tree-sitter-waltzing",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "tree-sitter-waltzing",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"nan": "^2.17.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"tree-sitter-cli": "^0.24.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/nan": {
|
||||||
|
"version": "2.24.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/nan/-/nan-2.24.0.tgz",
|
||||||
|
"integrity": "sha512-Vpf9qnVW1RaDkoNKFUvfxqAbtI8ncb8OJlqZ9wwpXzWPEsvsB1nvdUi6oYrHIkQ1Y/tMDnr1h4nczS0VB9Xykg==",
|
||||||
|
"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==",
|
||||||
|
"dev": true,
|
||||||
|
"hasInstallScript": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"tree-sitter": "cli.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"name": "waltzing-tree-sitter",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Waltzing template language grammar for tree-sitter",
|
||||||
|
"main": "bindings/node",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.awesomike.com/dev/waltzing.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"tree-sitter",
|
||||||
|
"parser",
|
||||||
|
"waltzing",
|
||||||
|
"template"
|
||||||
|
],
|
||||||
|
"author": "Michael <michael@awesomike.com>",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"nan": "^2.17.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"tree-sitter-cli": "^0.24.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tree-sitter generate",
|
||||||
|
"test": "tree-sitter test",
|
||||||
|
"parse": "tree-sitter parse"
|
||||||
|
},
|
||||||
|
"tree-sitter": [
|
||||||
|
{
|
||||||
|
"scope": "source.waltzing",
|
||||||
|
"file-types": [
|
||||||
|
"wtz",
|
||||||
|
"html.wtz",
|
||||||
|
"css.wtz",
|
||||||
|
"js.wtz"
|
||||||
|
],
|
||||||
|
"highlights": "queries/highlights.scm",
|
||||||
|
"injection-regex": "waltzing"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,120 @@
|
||||||
|
; Waltzing Template Highlights
|
||||||
|
|
||||||
|
; Keywords
|
||||||
|
"@use" @keyword.import
|
||||||
|
"@import" @keyword.import
|
||||||
|
"@struct" @keyword.type
|
||||||
|
"@enum" @keyword.type
|
||||||
|
"@func" @keyword.function
|
||||||
|
"@if" @keyword.conditional
|
||||||
|
"@for" @keyword.repeat
|
||||||
|
"@match" @keyword.conditional
|
||||||
|
"@break" @keyword.control
|
||||||
|
"@continue" @keyword.control
|
||||||
|
"@safe" @keyword.function
|
||||||
|
"else" @keyword.conditional
|
||||||
|
"if" @keyword.conditional
|
||||||
|
"let" @keyword
|
||||||
|
"in" @keyword
|
||||||
|
"as" @keyword
|
||||||
|
"mut" @keyword
|
||||||
|
|
||||||
|
; Operators
|
||||||
|
(binary_operator) @operator
|
||||||
|
(unary_operator) @operator
|
||||||
|
"=>" @operator
|
||||||
|
"=" @operator
|
||||||
|
"::" @punctuation.delimiter
|
||||||
|
|
||||||
|
; Punctuation
|
||||||
|
"{" @punctuation.bracket
|
||||||
|
"}" @punctuation.bracket
|
||||||
|
"(" @punctuation.bracket
|
||||||
|
")" @punctuation.bracket
|
||||||
|
"[" @punctuation.bracket
|
||||||
|
"]" @punctuation.bracket
|
||||||
|
"<" @punctuation.bracket
|
||||||
|
">" @punctuation.bracket
|
||||||
|
"," @punctuation.delimiter
|
||||||
|
":" @punctuation.delimiter
|
||||||
|
";" @punctuation.delimiter
|
||||||
|
|
||||||
|
; Literals
|
||||||
|
(string_literal) @string
|
||||||
|
(char_literal) @character
|
||||||
|
(number_literal) @number
|
||||||
|
(integer_literal) @number
|
||||||
|
(float_literal) @number
|
||||||
|
(boolean_literal) @boolean
|
||||||
|
(escape_sequence) @string.escape
|
||||||
|
|
||||||
|
; Types
|
||||||
|
(primitive_type) @type.builtin
|
||||||
|
(rust_type) @type
|
||||||
|
(generic_params (identifier) @type)
|
||||||
|
(generic_type (rust_path) @type)
|
||||||
|
|
||||||
|
; Struct/Enum definitions
|
||||||
|
(struct_definition (identifier) @type.definition)
|
||||||
|
(enum_definition (identifier) @type.definition)
|
||||||
|
(struct_field (identifier) @property)
|
||||||
|
(enum_variant (identifier) @constructor)
|
||||||
|
|
||||||
|
; Functions
|
||||||
|
(function_definition (identifier) @function.definition)
|
||||||
|
(parameter (identifier) @variable.parameter)
|
||||||
|
|
||||||
|
; Function tags (component calls)
|
||||||
|
(function_path) @function
|
||||||
|
(self_closing_function_tag) @tag
|
||||||
|
(container_function_tag) @tag
|
||||||
|
"<@" @tag.delimiter
|
||||||
|
"</@" @tag.delimiter
|
||||||
|
|
||||||
|
; HTML elements
|
||||||
|
(tag_name) @tag
|
||||||
|
(html_element) @tag
|
||||||
|
(attribute_name) @attribute
|
||||||
|
(attribute_value) @string
|
||||||
|
|
||||||
|
; Paths and identifiers
|
||||||
|
(rust_path) @module
|
||||||
|
(use_statement (rust_path) @module)
|
||||||
|
(import_statement (string_literal) @string.special)
|
||||||
|
|
||||||
|
; Expressions
|
||||||
|
(simple_expression "@" @punctuation.special)
|
||||||
|
(complex_expression "@" @punctuation.special)
|
||||||
|
(expression_path (identifier) @variable)
|
||||||
|
(field_access (identifier) @property)
|
||||||
|
(method_call (identifier) @function.method)
|
||||||
|
|
||||||
|
; Patterns
|
||||||
|
(wildcard_pattern) @variable.builtin
|
||||||
|
(identifier_pattern (identifier) @variable)
|
||||||
|
(struct_pattern (rust_path) @type)
|
||||||
|
(field_pattern (identifier) @property)
|
||||||
|
|
||||||
|
; Control flow
|
||||||
|
(if_statement) @conditional
|
||||||
|
(for_loop) @repeat
|
||||||
|
(match_statement) @conditional
|
||||||
|
(match_arm) @conditional
|
||||||
|
|
||||||
|
; Comments
|
||||||
|
(template_comment) @comment
|
||||||
|
(html_comment) @comment
|
||||||
|
|
||||||
|
; Attributes (derive, etc.)
|
||||||
|
(attribute_list) @attribute
|
||||||
|
(attribute (identifier) @attribute)
|
||||||
|
|
||||||
|
; Escape sequence
|
||||||
|
(escape_at) @string.escape
|
||||||
|
|
||||||
|
; Embedded languages
|
||||||
|
(embedded_language) @embedded
|
||||||
|
(language_name) @label
|
||||||
|
|
||||||
|
; Text content
|
||||||
|
(text_content) @text
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,54 @@
|
||||||
|
#ifndef TREE_SITTER_ALLOC_H_
|
||||||
|
#define TREE_SITTER_ALLOC_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Allow clients to override allocation functions
|
||||||
|
#ifdef TREE_SITTER_REUSE_ALLOCATOR
|
||||||
|
|
||||||
|
extern void *(*ts_current_malloc)(size_t size);
|
||||||
|
extern void *(*ts_current_calloc)(size_t count, size_t size);
|
||||||
|
extern void *(*ts_current_realloc)(void *ptr, size_t size);
|
||||||
|
extern void (*ts_current_free)(void *ptr);
|
||||||
|
|
||||||
|
#ifndef ts_malloc
|
||||||
|
#define ts_malloc ts_current_malloc
|
||||||
|
#endif
|
||||||
|
#ifndef ts_calloc
|
||||||
|
#define ts_calloc ts_current_calloc
|
||||||
|
#endif
|
||||||
|
#ifndef ts_realloc
|
||||||
|
#define ts_realloc ts_current_realloc
|
||||||
|
#endif
|
||||||
|
#ifndef ts_free
|
||||||
|
#define ts_free ts_current_free
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#ifndef ts_malloc
|
||||||
|
#define ts_malloc malloc
|
||||||
|
#endif
|
||||||
|
#ifndef ts_calloc
|
||||||
|
#define ts_calloc calloc
|
||||||
|
#endif
|
||||||
|
#ifndef ts_realloc
|
||||||
|
#define ts_realloc realloc
|
||||||
|
#endif
|
||||||
|
#ifndef ts_free
|
||||||
|
#define ts_free free
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // TREE_SITTER_ALLOC_H_
|
||||||
|
|
@ -0,0 +1,291 @@
|
||||||
|
#ifndef TREE_SITTER_ARRAY_H_
|
||||||
|
#define TREE_SITTER_ARRAY_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "./alloc.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4101)
|
||||||
|
#elif defined(__GNUC__) || defined(__clang__)
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define Array(T) \
|
||||||
|
struct { \
|
||||||
|
T *contents; \
|
||||||
|
uint32_t size; \
|
||||||
|
uint32_t capacity; \
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initialize an array.
|
||||||
|
#define array_init(self) \
|
||||||
|
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
|
||||||
|
|
||||||
|
/// Create an empty array.
|
||||||
|
#define array_new() \
|
||||||
|
{ NULL, 0, 0 }
|
||||||
|
|
||||||
|
/// Get a pointer to the element at a given `index` in the array.
|
||||||
|
#define array_get(self, _index) \
|
||||||
|
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
||||||
|
|
||||||
|
/// Get a pointer to the first element in the array.
|
||||||
|
#define array_front(self) array_get(self, 0)
|
||||||
|
|
||||||
|
/// Get a pointer to the last element in the array.
|
||||||
|
#define array_back(self) array_get(self, (self)->size - 1)
|
||||||
|
|
||||||
|
/// Clear the array, setting its size to zero. Note that this does not free any
|
||||||
|
/// memory allocated for the array's contents.
|
||||||
|
#define array_clear(self) ((self)->size = 0)
|
||||||
|
|
||||||
|
/// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
|
||||||
|
/// less than the array's current capacity, this function has no effect.
|
||||||
|
#define array_reserve(self, new_capacity) \
|
||||||
|
_array__reserve((Array *)(self), array_elem_size(self), new_capacity)
|
||||||
|
|
||||||
|
/// Free any memory allocated for this array. Note that this does not free any
|
||||||
|
/// memory allocated for the array's contents.
|
||||||
|
#define array_delete(self) _array__delete((Array *)(self))
|
||||||
|
|
||||||
|
/// Push a new `element` onto the end of the array.
|
||||||
|
#define array_push(self, element) \
|
||||||
|
(_array__grow((Array *)(self), 1, array_elem_size(self)), \
|
||||||
|
(self)->contents[(self)->size++] = (element))
|
||||||
|
|
||||||
|
/// Increase the array's size by `count` elements.
|
||||||
|
/// New elements are zero-initialized.
|
||||||
|
#define array_grow_by(self, count) \
|
||||||
|
do { \
|
||||||
|
if ((count) == 0) break; \
|
||||||
|
_array__grow((Array *)(self), count, array_elem_size(self)); \
|
||||||
|
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \
|
||||||
|
(self)->size += (count); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/// Append all elements from one array to the end of another.
|
||||||
|
#define array_push_all(self, other) \
|
||||||
|
array_extend((self), (other)->size, (other)->contents)
|
||||||
|
|
||||||
|
/// Append `count` elements to the end of the array, reading their values from the
|
||||||
|
/// `contents` pointer.
|
||||||
|
#define array_extend(self, count, contents) \
|
||||||
|
_array__splice( \
|
||||||
|
(Array *)(self), array_elem_size(self), (self)->size, \
|
||||||
|
0, count, contents \
|
||||||
|
)
|
||||||
|
|
||||||
|
/// Remove `old_count` elements from the array starting at the given `index`. At
|
||||||
|
/// the same index, insert `new_count` new elements, reading their values from the
|
||||||
|
/// `new_contents` pointer.
|
||||||
|
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
||||||
|
_array__splice( \
|
||||||
|
(Array *)(self), array_elem_size(self), _index, \
|
||||||
|
old_count, new_count, new_contents \
|
||||||
|
)
|
||||||
|
|
||||||
|
/// Insert one `element` into the array at the given `index`.
|
||||||
|
#define array_insert(self, _index, element) \
|
||||||
|
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
|
||||||
|
|
||||||
|
/// Remove one element from the array at the given `index`.
|
||||||
|
#define array_erase(self, _index) \
|
||||||
|
_array__erase((Array *)(self), array_elem_size(self), _index)
|
||||||
|
|
||||||
|
/// Pop the last element off the array, returning the element by value.
|
||||||
|
#define array_pop(self) ((self)->contents[--(self)->size])
|
||||||
|
|
||||||
|
/// Assign the contents of one array to another, reallocating if necessary.
|
||||||
|
#define array_assign(self, other) \
|
||||||
|
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
|
||||||
|
|
||||||
|
/// Swap one array with another
|
||||||
|
#define array_swap(self, other) \
|
||||||
|
_array__swap((Array *)(self), (Array *)(other))
|
||||||
|
|
||||||
|
/// Get the size of the array contents
|
||||||
|
#define array_elem_size(self) (sizeof *(self)->contents)
|
||||||
|
|
||||||
|
/// Search a sorted array for a given `needle` value, using the given `compare`
|
||||||
|
/// callback to determine the order.
|
||||||
|
///
|
||||||
|
/// If an existing element is found to be equal to `needle`, then the `index`
|
||||||
|
/// out-parameter is set to the existing value's index, and the `exists`
|
||||||
|
/// out-parameter is set to true. Otherwise, `index` is set to an index where
|
||||||
|
/// `needle` should be inserted in order to preserve the sorting, and `exists`
|
||||||
|
/// is set to false.
|
||||||
|
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
||||||
|
_array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
||||||
|
|
||||||
|
/// Search a sorted array for a given `needle` value, using integer comparisons
|
||||||
|
/// of a given struct field (specified with a leading dot) to determine the order.
|
||||||
|
///
|
||||||
|
/// See also `array_search_sorted_with`.
|
||||||
|
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
||||||
|
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
|
||||||
|
|
||||||
|
/// Insert a given `value` into a sorted array, using the given `compare`
|
||||||
|
/// callback to determine the order.
|
||||||
|
#define array_insert_sorted_with(self, compare, value) \
|
||||||
|
do { \
|
||||||
|
unsigned _index, _exists; \
|
||||||
|
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
||||||
|
if (!_exists) array_insert(self, _index, value); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/// Insert a given `value` into a sorted array, using integer comparisons of
|
||||||
|
/// a given struct field (specified with a leading dot) to determine the order.
|
||||||
|
///
|
||||||
|
/// See also `array_search_sorted_by`.
|
||||||
|
#define array_insert_sorted_by(self, field, value) \
|
||||||
|
do { \
|
||||||
|
unsigned _index, _exists; \
|
||||||
|
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
||||||
|
if (!_exists) array_insert(self, _index, value); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
// Private
|
||||||
|
|
||||||
|
typedef Array(void) Array;
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_delete`.
|
||||||
|
static inline void _array__delete(Array *self) {
|
||||||
|
if (self->contents) {
|
||||||
|
ts_free(self->contents);
|
||||||
|
self->contents = NULL;
|
||||||
|
self->size = 0;
|
||||||
|
self->capacity = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_erase`.
|
||||||
|
static inline void _array__erase(Array *self, size_t element_size,
|
||||||
|
uint32_t index) {
|
||||||
|
assert(index < self->size);
|
||||||
|
char *contents = (char *)self->contents;
|
||||||
|
memmove(contents + index * element_size, contents + (index + 1) * element_size,
|
||||||
|
(self->size - index - 1) * element_size);
|
||||||
|
self->size--;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_reserve`.
|
||||||
|
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
|
||||||
|
if (new_capacity > self->capacity) {
|
||||||
|
if (self->contents) {
|
||||||
|
self->contents = ts_realloc(self->contents, new_capacity * element_size);
|
||||||
|
} else {
|
||||||
|
self->contents = ts_malloc(new_capacity * element_size);
|
||||||
|
}
|
||||||
|
self->capacity = new_capacity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_assign`.
|
||||||
|
static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
|
||||||
|
_array__reserve(self, element_size, other->size);
|
||||||
|
self->size = other->size;
|
||||||
|
memcpy(self->contents, other->contents, self->size * element_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_swap`.
|
||||||
|
static inline void _array__swap(Array *self, Array *other) {
|
||||||
|
Array swap = *other;
|
||||||
|
*other = *self;
|
||||||
|
*self = swap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_push` or `array_grow_by`.
|
||||||
|
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
|
||||||
|
uint32_t new_size = self->size + count;
|
||||||
|
if (new_size > self->capacity) {
|
||||||
|
uint32_t new_capacity = self->capacity * 2;
|
||||||
|
if (new_capacity < 8) new_capacity = 8;
|
||||||
|
if (new_capacity < new_size) new_capacity = new_size;
|
||||||
|
_array__reserve(self, element_size, new_capacity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This is not what you're looking for, see `array_splice`.
|
||||||
|
static inline void _array__splice(Array *self, size_t element_size,
|
||||||
|
uint32_t index, uint32_t old_count,
|
||||||
|
uint32_t new_count, const void *elements) {
|
||||||
|
uint32_t new_size = self->size + new_count - old_count;
|
||||||
|
uint32_t old_end = index + old_count;
|
||||||
|
uint32_t new_end = index + new_count;
|
||||||
|
assert(old_end <= self->size);
|
||||||
|
|
||||||
|
_array__reserve(self, element_size, new_size);
|
||||||
|
|
||||||
|
char *contents = (char *)self->contents;
|
||||||
|
if (self->size > old_end) {
|
||||||
|
memmove(
|
||||||
|
contents + new_end * element_size,
|
||||||
|
contents + old_end * element_size,
|
||||||
|
(self->size - old_end) * element_size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (new_count > 0) {
|
||||||
|
if (elements) {
|
||||||
|
memcpy(
|
||||||
|
(contents + index * element_size),
|
||||||
|
elements,
|
||||||
|
new_count * element_size
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
memset(
|
||||||
|
(contents + index * element_size),
|
||||||
|
0,
|
||||||
|
new_count * element_size
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self->size += new_count - old_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
||||||
|
/// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
|
||||||
|
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
||||||
|
do { \
|
||||||
|
*(_index) = start; \
|
||||||
|
*(_exists) = false; \
|
||||||
|
uint32_t size = (self)->size - *(_index); \
|
||||||
|
if (size == 0) break; \
|
||||||
|
int comparison; \
|
||||||
|
while (size > 1) { \
|
||||||
|
uint32_t half_size = size / 2; \
|
||||||
|
uint32_t mid_index = *(_index) + half_size; \
|
||||||
|
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
||||||
|
if (comparison <= 0) *(_index) = mid_index; \
|
||||||
|
size -= half_size; \
|
||||||
|
} \
|
||||||
|
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
||||||
|
if (comparison == 0) *(_exists) = true; \
|
||||||
|
else if (comparison < 0) *(_index) += 1; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
||||||
|
/// parameter by reference in order to work with the generic sorting function above.
|
||||||
|
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(pop)
|
||||||
|
#elif defined(__GNUC__) || defined(__clang__)
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // TREE_SITTER_ARRAY_H_
|
||||||
|
|
@ -0,0 +1,266 @@
|
||||||
|
#ifndef TREE_SITTER_PARSER_H_
|
||||||
|
#define TREE_SITTER_PARSER_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define ts_builtin_sym_error ((TSSymbol)-1)
|
||||||
|
#define ts_builtin_sym_end 0
|
||||||
|
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
#ifndef TREE_SITTER_API_H_
|
||||||
|
typedef uint16_t TSStateId;
|
||||||
|
typedef uint16_t TSSymbol;
|
||||||
|
typedef uint16_t TSFieldId;
|
||||||
|
typedef struct TSLanguage TSLanguage;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
TSFieldId field_id;
|
||||||
|
uint8_t child_index;
|
||||||
|
bool inherited;
|
||||||
|
} TSFieldMapEntry;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t index;
|
||||||
|
uint16_t length;
|
||||||
|
} TSFieldMapSlice;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool visible;
|
||||||
|
bool named;
|
||||||
|
bool supertype;
|
||||||
|
} TSSymbolMetadata;
|
||||||
|
|
||||||
|
typedef struct TSLexer TSLexer;
|
||||||
|
|
||||||
|
struct TSLexer {
|
||||||
|
int32_t lookahead;
|
||||||
|
TSSymbol result_symbol;
|
||||||
|
void (*advance)(TSLexer *, bool);
|
||||||
|
void (*mark_end)(TSLexer *);
|
||||||
|
uint32_t (*get_column)(TSLexer *);
|
||||||
|
bool (*is_at_included_range_start)(const TSLexer *);
|
||||||
|
bool (*eof)(const TSLexer *);
|
||||||
|
void (*log)(const TSLexer *, const char *, ...);
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TSParseActionTypeShift,
|
||||||
|
TSParseActionTypeReduce,
|
||||||
|
TSParseActionTypeAccept,
|
||||||
|
TSParseActionTypeRecover,
|
||||||
|
} TSParseActionType;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
struct {
|
||||||
|
uint8_t type;
|
||||||
|
TSStateId state;
|
||||||
|
bool extra;
|
||||||
|
bool repetition;
|
||||||
|
} shift;
|
||||||
|
struct {
|
||||||
|
uint8_t type;
|
||||||
|
uint8_t child_count;
|
||||||
|
TSSymbol symbol;
|
||||||
|
int16_t dynamic_precedence;
|
||||||
|
uint16_t production_id;
|
||||||
|
} reduce;
|
||||||
|
uint8_t type;
|
||||||
|
} TSParseAction;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t lex_state;
|
||||||
|
uint16_t external_lex_state;
|
||||||
|
} TSLexMode;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
TSParseAction action;
|
||||||
|
struct {
|
||||||
|
uint8_t count;
|
||||||
|
bool reusable;
|
||||||
|
} entry;
|
||||||
|
} TSParseActionEntry;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int32_t start;
|
||||||
|
int32_t end;
|
||||||
|
} TSCharacterRange;
|
||||||
|
|
||||||
|
struct TSLanguage {
|
||||||
|
uint32_t version;
|
||||||
|
uint32_t symbol_count;
|
||||||
|
uint32_t alias_count;
|
||||||
|
uint32_t token_count;
|
||||||
|
uint32_t external_token_count;
|
||||||
|
uint32_t state_count;
|
||||||
|
uint32_t large_state_count;
|
||||||
|
uint32_t production_id_count;
|
||||||
|
uint32_t field_count;
|
||||||
|
uint16_t max_alias_sequence_length;
|
||||||
|
const uint16_t *parse_table;
|
||||||
|
const uint16_t *small_parse_table;
|
||||||
|
const uint32_t *small_parse_table_map;
|
||||||
|
const TSParseActionEntry *parse_actions;
|
||||||
|
const char * const *symbol_names;
|
||||||
|
const char * const *field_names;
|
||||||
|
const TSFieldMapSlice *field_map_slices;
|
||||||
|
const TSFieldMapEntry *field_map_entries;
|
||||||
|
const TSSymbolMetadata *symbol_metadata;
|
||||||
|
const TSSymbol *public_symbol_map;
|
||||||
|
const uint16_t *alias_map;
|
||||||
|
const TSSymbol *alias_sequences;
|
||||||
|
const TSLexMode *lex_modes;
|
||||||
|
bool (*lex_fn)(TSLexer *, TSStateId);
|
||||||
|
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
|
||||||
|
TSSymbol keyword_capture_token;
|
||||||
|
struct {
|
||||||
|
const bool *states;
|
||||||
|
const TSSymbol *symbol_map;
|
||||||
|
void *(*create)(void);
|
||||||
|
void (*destroy)(void *);
|
||||||
|
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
|
||||||
|
unsigned (*serialize)(void *, char *);
|
||||||
|
void (*deserialize)(void *, const char *, unsigned);
|
||||||
|
} external_scanner;
|
||||||
|
const TSStateId *primary_state_ids;
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
|
||||||
|
uint32_t index = 0;
|
||||||
|
uint32_t size = len - index;
|
||||||
|
while (size > 1) {
|
||||||
|
uint32_t half_size = size / 2;
|
||||||
|
uint32_t mid_index = index + half_size;
|
||||||
|
TSCharacterRange *range = &ranges[mid_index];
|
||||||
|
if (lookahead >= range->start && lookahead <= range->end) {
|
||||||
|
return true;
|
||||||
|
} else if (lookahead > range->end) {
|
||||||
|
index = mid_index;
|
||||||
|
}
|
||||||
|
size -= half_size;
|
||||||
|
}
|
||||||
|
TSCharacterRange *range = &ranges[index];
|
||||||
|
return (lookahead >= range->start && lookahead <= range->end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Lexer Macros
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define UNUSED __pragma(warning(suppress : 4101))
|
||||||
|
#else
|
||||||
|
#define UNUSED __attribute__((unused))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define START_LEXER() \
|
||||||
|
bool result = false; \
|
||||||
|
bool skip = false; \
|
||||||
|
UNUSED \
|
||||||
|
bool eof = false; \
|
||||||
|
int32_t lookahead; \
|
||||||
|
goto start; \
|
||||||
|
next_state: \
|
||||||
|
lexer->advance(lexer, skip); \
|
||||||
|
start: \
|
||||||
|
skip = false; \
|
||||||
|
lookahead = lexer->lookahead;
|
||||||
|
|
||||||
|
#define ADVANCE(state_value) \
|
||||||
|
{ \
|
||||||
|
state = state_value; \
|
||||||
|
goto next_state; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ADVANCE_MAP(...) \
|
||||||
|
{ \
|
||||||
|
static const uint16_t map[] = { __VA_ARGS__ }; \
|
||||||
|
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
|
||||||
|
if (map[i] == lookahead) { \
|
||||||
|
state = map[i + 1]; \
|
||||||
|
goto next_state; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SKIP(state_value) \
|
||||||
|
{ \
|
||||||
|
skip = true; \
|
||||||
|
state = state_value; \
|
||||||
|
goto next_state; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ACCEPT_TOKEN(symbol_value) \
|
||||||
|
result = true; \
|
||||||
|
lexer->result_symbol = symbol_value; \
|
||||||
|
lexer->mark_end(lexer);
|
||||||
|
|
||||||
|
#define END_STATE() return result;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Parse Table Macros
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
|
||||||
|
|
||||||
|
#define STATE(id) id
|
||||||
|
|
||||||
|
#define ACTIONS(id) id
|
||||||
|
|
||||||
|
#define SHIFT(state_value) \
|
||||||
|
{{ \
|
||||||
|
.shift = { \
|
||||||
|
.type = TSParseActionTypeShift, \
|
||||||
|
.state = (state_value) \
|
||||||
|
} \
|
||||||
|
}}
|
||||||
|
|
||||||
|
#define SHIFT_REPEAT(state_value) \
|
||||||
|
{{ \
|
||||||
|
.shift = { \
|
||||||
|
.type = TSParseActionTypeShift, \
|
||||||
|
.state = (state_value), \
|
||||||
|
.repetition = true \
|
||||||
|
} \
|
||||||
|
}}
|
||||||
|
|
||||||
|
#define SHIFT_EXTRA() \
|
||||||
|
{{ \
|
||||||
|
.shift = { \
|
||||||
|
.type = TSParseActionTypeShift, \
|
||||||
|
.extra = true \
|
||||||
|
} \
|
||||||
|
}}
|
||||||
|
|
||||||
|
#define REDUCE(symbol_name, children, precedence, prod_id) \
|
||||||
|
{{ \
|
||||||
|
.reduce = { \
|
||||||
|
.type = TSParseActionTypeReduce, \
|
||||||
|
.symbol = symbol_name, \
|
||||||
|
.child_count = children, \
|
||||||
|
.dynamic_precedence = precedence, \
|
||||||
|
.production_id = prod_id \
|
||||||
|
}, \
|
||||||
|
}}
|
||||||
|
|
||||||
|
#define RECOVER() \
|
||||||
|
{{ \
|
||||||
|
.type = TSParseActionTypeRecover \
|
||||||
|
}}
|
||||||
|
|
||||||
|
#define ACCEPT_INPUT() \
|
||||||
|
{{ \
|
||||||
|
.type = TSParseActionTypeAccept \
|
||||||
|
}}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // TREE_SITTER_PARSER_H_
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"grammars": [
|
||||||
|
{
|
||||||
|
"name": "waltzing",
|
||||||
|
"camelcase": "Waltzing",
|
||||||
|
"scope": "source.waltzing",
|
||||||
|
"path": ".",
|
||||||
|
"file-types": [
|
||||||
|
"wtz",
|
||||||
|
"html.wtz",
|
||||||
|
"css.wtz",
|
||||||
|
"js.wtz"
|
||||||
|
],
|
||||||
|
"highlights": "queries/highlights.scm",
|
||||||
|
"injection-regex": "waltzing"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"version": "0.1.0",
|
||||||
|
"links": {
|
||||||
|
"repository": "https://git.awesomike.com/dev/waltzing"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue