-
Notifications
You must be signed in to change notification settings - Fork 8
/
grammar.js
143 lines (126 loc) · 3.88 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @file Astro grammar for tree-sitter
* @author Vir Chaudhury <[email protected]>
* @author Amaan Qureshi <[email protected]>
* @license MIT
*/
/* eslint-disable arrow-parens */
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
const HTML = require('tree-sitter-html/grammar');
module.exports = grammar(HTML, {
name: 'astro',
externals: ($, original) => original.concat([
$._html_interpolation_start,
$._html_interpolation_end,
$.frontmatter_js_block,
$.attribute_js_expr,
$.attribute_backtick_string,
$.permissible_text,
$._fragment_tag_delim,
]),
rules: {
document: $ => seq(
optional($.frontmatter),
repeat($._node),
),
_node: ($, _original) => choice(
// Same as original minus `.entity`
// and plus `.html_interpolation`.
// TODO add back entity highlights
$.doctype,
$.text,
$.element,
$.script_element,
$.style_element,
$.erroneous_end_tag,
$.html_interpolation,
),
// For use in HTML interpolations.
_node_with_permissible_text: $ => choice(
$.doctype,
$.element,
$.script_element,
$.style_element,
$.html_interpolation,
$.permissible_text,
),
// Allow fragment tags.
start_tag: ($, _) => seq(
'<',
choice(
seq(
alias($._start_tag_name, $.tag_name),
repeat($.attribute),
'>',
),
alias($._fragment_tag_delim, '>'),
),
),
end_tag: ($, _) => seq(
'</',
choice(
seq(
alias($._end_tag_name, $.tag_name),
'>',
),
alias($._fragment_tag_delim, '>'),
),
),
frontmatter: $ => seq(
token(prec(1, '---')),
optional($.frontmatter_js_block),
'---',
),
attribute: ($, original) => choice(
original,
seq(
$.attribute_name,
'=',
choice(
$.attribute_interpolation,
$.attribute_backtick_string,
),
),
// Astro directly allows attributes without equal signs,
// e.g. <div {...attrs} />
$.attribute_interpolation,
),
attribute_interpolation: $ => seq(
token(prec(1, '{')),
$.attribute_js_expr,
'}'
),
html_interpolation: $ => seq(
alias($._html_interpolation_start, '{'),
repeat($._node_with_permissible_text),
alias($._html_interpolation_end, '}'),
),
// Astro supports self-closing script/style tags.
self_closing_script_tag: $ => seq(
'<',
alias($._script_start_tag_name, $.tag_name),
repeat($.attribute),
'/>',
),
self_closing_style_tag: $ => seq(
'<',
alias($._style_start_tag_name, $.tag_name),
repeat($.attribute),
'/>'
),
script_element: ($, original) => choice(
original,
alias($.self_closing_script_tag, $.self_closing_tag),
),
style_element: ($, original) => choice(
original,
alias($.self_closing_style_tag, $.self_closing_tag),
),
/* Astro doesn't provide any way to escape curly braces apart from
* the standard HTML method of e.g. `&x30;` */
text: _ => /[^<>{}\s]([^<>{}]*[^<>{}\s])?/,
},
});