feat(package): hemar: add indexes to tree-sitter

This commit is contained in:
2025-11-28 10:29:59 +00:00
parent 4b4ea7a1d2
commit 48077aaccc
6 changed files with 724 additions and 400 deletions

View File

@@ -4,15 +4,16 @@ module.exports = grammar({
rules: { rules: {
source_file: $ => repeat($.element), source_file: $ => repeat($.element),
element: $ => choice($.interpolation, $.segment, $.text), element: $ => choice($.interpolation, $.segment, $.text, $.actual_bracket),
interpolation: $ => seq("{[", $.path, "]}"), interpolation: $ => seq("{[", $.path, "]}"),
segment: $ => seq($.for, repeat($.element), $.done), segment: $ => seq($.for, repeat($.element), $.done),
for: $ => seq("{[", "for", $.string, "in", $.path, "]}"), for: $ => seq("{[", "for", $.string, "in", $.path, "]}"),
done: $ => seq("{[", "done", "]}"), done: $ => seq("{[", "done", "]}"),
actual_bracket: $ => seq("{[", "{[", "]}"),
//include: $ => seq("include", $.path), //include: $ => seq("include", $.path),
//call: $ => seq("call", $.string, "in", $.language), //call: $ => seq("call", $.string, "in", $.language),
//call_end: $ => seq("end", $.string), //call_end: $ => seq("end", $.string),
@@ -22,16 +23,34 @@ module.exports = grammar({
path: $ => choice( path: $ => choice(
".", ".",
seq( seq(
$.string, choice(
repeat(seq(".", $.string)), $.string,
$.index,
),
repeat(seq(".", choice(
$.string,
$.index,
))),
), ),
), ),
index: $ => seq(
'[',
choice(
/\d/,
/[1-9]\d*/,
/-[1-9]/,
/-[1-9]\d*/
),
']',
),
// anything but space // anything but space
string: $ => choice( string: $ => choice(
// no whitespace, ], \, ., " // no whitespace, [, ], {, }, \, ., "
token(prec(-1, /[^] .\\"]+/)), token(prec(-1, /[^]\[{} \n\t\r.\\"]+/)),
// " ... " with "" = escaped " // " ... " with "" = escaped "
// if you need json string rules (?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[0-9A-Fa-f]{4})*
token(prec(-1, /"([^"]|"")*"/)), token(prec(-1, /"([^"]|"")*"/)),
), ),

View File

@@ -1,8 +1,8 @@
(interpolation) @keyword (interpolation) @keyword
(segment) @keyword (segment) @keyword
(for "for" @keyword) (for "for" @keyword)
(for "in" @keyword) (for "in" @keyword)
(done "done" @keyword) (done "done" @keyword)
(path) @field (path) @field

View File

@@ -23,6 +23,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "text" "name": "text"
},
{
"type": "SYMBOL",
"name": "actual_bracket"
} }
] ]
}, },
@@ -109,6 +113,23 @@
} }
] ]
}, },
"actual_bracket": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{["
},
{
"type": "STRING",
"value": "{["
},
{
"type": "STRING",
"value": "]}"
}
]
},
"path": { "path": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@@ -120,8 +141,17 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "CHOICE",
"name": "string" "members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "index"
}
]
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
@@ -133,8 +163,17 @@
"value": "." "value": "."
}, },
{ {
"type": "SYMBOL", "type": "CHOICE",
"name": "string" "members": [
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "index"
}
]
} }
] ]
} }
@@ -143,6 +182,40 @@
} }
] ]
}, },
"index": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "["
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\d"
},
{
"type": "PATTERN",
"value": "[1-9]\\d*"
},
{
"type": "PATTERN",
"value": "-[1-9]"
},
{
"type": "PATTERN",
"value": "-[1-9]\\d*"
}
]
},
{
"type": "STRING",
"value": "]"
}
]
},
"string": { "string": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@@ -153,7 +226,7 @@
"value": -1, "value": -1,
"content": { "content": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^] .\\\\\"]+" "value": "[^]\\[{} \\n\\t\\r.\\\\\"]+"
} }
} }
}, },

View File

@@ -1,4 +1,9 @@
[ [
{
"type": "actual_bracket",
"named": true,
"fields": {}
},
{ {
"type": "done", "type": "done",
"named": true, "named": true,
@@ -12,6 +17,10 @@
"multiple": false, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{
"type": "actual_bracket",
"named": true
},
{ {
"type": "interpolation", "type": "interpolation",
"named": true "named": true
@@ -46,6 +55,11 @@
] ]
} }
}, },
{
"type": "index",
"named": true,
"fields": {}
},
{ {
"type": "interpolation", "type": "interpolation",
"named": true, "named": true,
@@ -69,6 +83,10 @@
"multiple": true, "multiple": true,
"required": false, "required": false,
"types": [ "types": [
{
"type": "index",
"named": true
},
{ {
"type": "string", "type": "string",
"named": true "named": true
@@ -124,6 +142,14 @@
"type": ".", "type": ".",
"named": false "named": false
}, },
{
"type": "[",
"named": false
},
{
"type": "]",
"named": false
},
{ {
"type": "]}", "type": "]}",
"named": false "named": false

File diff suppressed because it is too large Load Diff

View File

@@ -112,3 +112,25 @@ segment with text
(element (element
(text)) (text))
(done)))) (done))))
==================
brace
==================
{[ {[ ]}
---
(source_file
(element
(actual_bracket)))
==================
indexes
==================
{[ [1].[0].[12].[-2].[-3] ]}
---
(source_file
(element
(interpolation
(path
(index)
(index)
(index)
(index)
(index))))))