forked from tree-sitter-grammars/tree-sitter-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrammar.js
More file actions
146 lines (123 loc) · 3.22 KB
/
Copy pathgrammar.js
File metadata and controls
146 lines (123 loc) · 3.22 KB
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
144
145
146
/**
* @file Tree-sitter grammar definition
* @author Amaan Qureshi <amaanq12@gmail.com>
* @license MIT
* @see {@link https://tree-sitter.github.io/tree-sitter/creating-parsers}
*/
import HTML from 'tree-sitter-html/grammar.js';
export default grammar(HTML, {
name: 'vue',
externals: ($, original) => original.concat([
$._template_start_tag_name,
$._text_fragment,
$._interpolation_text,
]),
rules: {
_node: ($, original) => choice(
original,
$.template_element,
$.interpolation,
),
template_element: $ => seq(
alias($.template_start_tag, $.start_tag),
repeat($._node),
$.end_tag,
),
start_tag: $ => seq(
'<',
alias($._start_tag_name, $.tag_name),
repeat($._attribute),
'>',
),
script_start_tag: $ => seq(
'<',
alias($._script_start_tag_name, $.tag_name),
repeat($._attribute),
'>',
),
style_start_tag: $ => seq(
'<',
alias($._style_start_tag_name, $.tag_name),
repeat($._attribute),
'>',
),
template_start_tag: $ => seq(
'<',
alias($._template_start_tag_name, $.tag_name),
repeat($._attribute),
'>',
),
self_closing_tag: $ => seq(
'<',
alias(choice($._start_tag_name, $._template_start_tag_name), $.tag_name),
repeat($._attribute),
'/>',
),
text: $ => $._text_fragment,
interpolation: $ => seq(
'{{',
optional(alias($._interpolation_text, $.raw_text)),
'}}',
),
_attribute: $ => choice(
$.attribute,
$.directive_attribute,
),
directive_attribute: $ => prec.right(seq(
choice(
seq(
$.directive_name,
optional(seq(
immediateLiterals(1, ':'),
choice($.directive_value, $.dynamic_directive_value),
)),
),
repeat1(seq(
tokenLiterals(1, ':', '.', '@', '#'),
choice($.directive_value, $.dynamic_directive_value),
)),
),
optional($.directive_modifiers),
optional(seq(
'=',
choice($.attribute_value, $.quoted_attribute_value),
)),
)),
directive_name: _ => token(prec(1, /v-[^<>'"=/\s:.]+/)),
directive_value: _ => /[^<>"'/=\s:.]+/,
dynamic_directive_value: $ => seq(
token.immediate(prec(1, '[')),
optional($.dynamic_directive_inner_value),
token.immediate(']'),
),
dynamic_directive_inner_value: _ => token.immediate(/[^<>"'/=\s\]]+/),
directive_modifiers: $ => repeat1(seq(token.immediate(prec(1, '.')), $.directive_modifier)),
directive_modifier: _ => token.immediate(/[^<>"'/=\s.]+/),
},
});
/**
*
* Turns a list of rules into a choice of immediate rule
*
* @param {number} precedence
*
* @param {(RegExp|String)[]} literals
*
* @return {ChoiceRule}
*/
function immediateLiterals(precedence, ...literals) {
return choice(...literals.map(l => token.immediate(prec(precedence, l))));
}
/**
*
* Turns a list of rules into a choice of aliased token rules
*
* @param {number} precedence
*
* @param {(RegExp|String)[]} literals
*
* @return {ChoiceRule}
*/
function tokenLiterals(precedence, ...literals) {
return choice(...literals.map(l => token(prec(precedence, l))));
}