forked from tree-sitter/tree-sitter-html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
150 lines (126 loc) · 3.25 KB
/
grammar.js
File metadata and controls
150 lines (126 loc) · 3.25 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
147
148
149
150
/**
* @file HTML grammar for tree-sitter
* @author Max Brunsfeld <maxbrunsfeld@gmail.com>
* @author Amaan Qureshi <amaanq12@gmail.com>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: "html",
extras: $ => [
$.comment,
/\s+/,
],
externals: ($) => [
$._start_tag_name,
$._script_start_tag_name,
$._style_start_tag_name,
$._end_tag_name,
$.erroneous_end_tag_name,
"/>",
$._implicit_end_tag,
$.raw_text,
$.comment,
],
rules: {
document: $ => repeat($._node),
doctype: $ => seq(
'<!',
alias($._doctype, 'doctype'),
/[^>]+/,
'>',
),
_doctype: _ => /[Dd][Oo][Cc][Tt][Yy][Pp][Ee]/,
_node: $ => choice(
$.doctype,
$.entity,
$.text,
$.tribracket,
$.element,
$.script_element,
$.style_element,
$.erroneous_end_tag,
),
// We have to wrap the opening '{{{' token in `token(prec())` calls here to
// force the lexer to give precedence to this rule over the `text` rule. I'm
// not 100% how this works, but this was suggested by @ahlinc in this
// Discord thread:
// https://discord.com/channels/1063097320771698699/1063097321648312354
tribracket: ($) => seq(token(prec(1, '{{{')), $.tribracket_content, '}}}'),
tribracket_content: ($) => /[^}]*/,
element: $ => choice(
seq(
$.start_tag,
repeat($._node),
choice($.end_tag, $._implicit_end_tag),
),
$.self_closing_tag,
),
script_element: $ => seq(
alias($.script_start_tag, $.start_tag),
optional($.raw_text),
$.end_tag,
),
style_element: $ => seq(
alias($.style_start_tag, $.start_tag),
optional($.raw_text),
$.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),
'>',
),
self_closing_tag: $ => seq(
'<',
alias($._start_tag_name, $.tag_name),
repeat($.attribute),
'/>',
),
end_tag: $ => seq(
'</',
alias($._end_tag_name, $.tag_name),
'>',
),
erroneous_end_tag: $ => seq(
'</',
$.erroneous_end_tag_name,
'>',
),
attribute: $ => seq(
$.attribute_name,
optional(seq(
'=',
choice(
$.attribute_value,
$.quoted_attribute_value,
),
)),
),
attribute_name: _ => /[^<>"'/=\s]+/,
attribute_value: _ => /[^<>"'=\s]+/,
// An entity can be named, numeric (decimal), or numeric (hexacecimal). The
// longest entity name is 29 characters long, and the HTML spec says that
// no more will ever be added.
entity: _ => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});?/,
quoted_attribute_value: $ => choice(
seq('\'', optional(alias(/[^']+/, $.attribute_value)), '\''),
seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"'),
),
text: _ => /[^<>&\s]([^<>&]*[^<>&\s])?/,
},
});