@ast-grep/lang-html 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tag.h ADDED
@@ -0,0 +1,385 @@
1
+ #include "tree_sitter/array.h"
2
+
3
+ #include <string.h>
4
+
5
+ typedef enum {
6
+ AREA,
7
+ BASE,
8
+ BASEFONT,
9
+ BGSOUND,
10
+ BR,
11
+ COL,
12
+ COMMAND,
13
+ EMBED,
14
+ FRAME,
15
+ HR,
16
+ IMAGE,
17
+ IMG,
18
+ INPUT,
19
+ ISINDEX,
20
+ KEYGEN,
21
+ LINK,
22
+ MENUITEM,
23
+ META,
24
+ NEXTID,
25
+ PARAM,
26
+ SOURCE,
27
+ TRACK,
28
+ WBR,
29
+ END_OF_VOID_TAGS,
30
+
31
+ A,
32
+ ABBR,
33
+ ADDRESS,
34
+ ARTICLE,
35
+ ASIDE,
36
+ AUDIO,
37
+ B,
38
+ BDI,
39
+ BDO,
40
+ BLOCKQUOTE,
41
+ BODY,
42
+ BUTTON,
43
+ CANVAS,
44
+ CAPTION,
45
+ CITE,
46
+ CODE,
47
+ COLGROUP,
48
+ DATA,
49
+ DATALIST,
50
+ DD,
51
+ DEL,
52
+ DETAILS,
53
+ DFN,
54
+ DIALOG,
55
+ DIV,
56
+ DL,
57
+ DT,
58
+ EM,
59
+ FIELDSET,
60
+ FIGCAPTION,
61
+ FIGURE,
62
+ FOOTER,
63
+ FORM,
64
+ H1,
65
+ H2,
66
+ H3,
67
+ H4,
68
+ H5,
69
+ H6,
70
+ HEAD,
71
+ HEADER,
72
+ HGROUP,
73
+ HTML,
74
+ I,
75
+ IFRAME,
76
+ INS,
77
+ KBD,
78
+ LABEL,
79
+ LEGEND,
80
+ LI,
81
+ MAIN,
82
+ MAP,
83
+ MARK,
84
+ MATH,
85
+ MENU,
86
+ METER,
87
+ NAV,
88
+ NOSCRIPT,
89
+ OBJECT,
90
+ OL,
91
+ OPTGROUP,
92
+ OPTION,
93
+ OUTPUT,
94
+ P,
95
+ PICTURE,
96
+ PRE,
97
+ PROGRESS,
98
+ Q,
99
+ RB,
100
+ RP,
101
+ RT,
102
+ RTC,
103
+ RUBY,
104
+ S,
105
+ SAMP,
106
+ SCRIPT,
107
+ SECTION,
108
+ SELECT,
109
+ SLOT,
110
+ SMALL,
111
+ SPAN,
112
+ STRONG,
113
+ STYLE,
114
+ SUB,
115
+ SUMMARY,
116
+ SUP,
117
+ SVG,
118
+ TABLE,
119
+ TBODY,
120
+ TD,
121
+ TEMPLATE,
122
+ TEXTAREA,
123
+ TFOOT,
124
+ TH,
125
+ THEAD,
126
+ TIME,
127
+ TITLE,
128
+ TR,
129
+ U,
130
+ UL,
131
+ VAR,
132
+ VIDEO,
133
+
134
+ CUSTOM,
135
+
136
+ END_,
137
+ } TagType;
138
+
139
+ typedef Array(char) String;
140
+
141
+ typedef struct {
142
+ char tag_name[16];
143
+ TagType tag_type;
144
+ } TagMapEntry;
145
+
146
+ typedef struct {
147
+ TagType type;
148
+ String custom_tag_name;
149
+ } Tag;
150
+
151
+ static const TagMapEntry TAG_TYPES_BY_TAG_NAME[126] = {
152
+ {"AREA", AREA },
153
+ {"BASE", BASE },
154
+ {"BASEFONT", BASEFONT },
155
+ {"BGSOUND", BGSOUND },
156
+ {"BR", BR },
157
+ {"COL", COL },
158
+ {"COMMAND", COMMAND },
159
+ {"EMBED", EMBED },
160
+ {"FRAME", FRAME },
161
+ {"HR", HR },
162
+ {"IMAGE", IMAGE },
163
+ {"IMG", IMG },
164
+ {"INPUT", INPUT },
165
+ {"ISINDEX", ISINDEX },
166
+ {"KEYGEN", KEYGEN },
167
+ {"LINK", LINK },
168
+ {"MENUITEM", MENUITEM },
169
+ {"META", META },
170
+ {"NEXTID", NEXTID },
171
+ {"PARAM", PARAM },
172
+ {"SOURCE", SOURCE },
173
+ {"TRACK", TRACK },
174
+ {"WBR", WBR },
175
+ {"A", A },
176
+ {"ABBR", ABBR },
177
+ {"ADDRESS", ADDRESS },
178
+ {"ARTICLE", ARTICLE },
179
+ {"ASIDE", ASIDE },
180
+ {"AUDIO", AUDIO },
181
+ {"B", B },
182
+ {"BDI", BDI },
183
+ {"BDO", BDO },
184
+ {"BLOCKQUOTE", BLOCKQUOTE},
185
+ {"BODY", BODY },
186
+ {"BUTTON", BUTTON },
187
+ {"CANVAS", CANVAS },
188
+ {"CAPTION", CAPTION },
189
+ {"CITE", CITE },
190
+ {"CODE", CODE },
191
+ {"COLGROUP", COLGROUP },
192
+ {"DATA", DATA },
193
+ {"DATALIST", DATALIST },
194
+ {"DD", DD },
195
+ {"DEL", DEL },
196
+ {"DETAILS", DETAILS },
197
+ {"DFN", DFN },
198
+ {"DIALOG", DIALOG },
199
+ {"DIV", DIV },
200
+ {"DL", DL },
201
+ {"DT", DT },
202
+ {"EM", EM },
203
+ {"FIELDSET", FIELDSET },
204
+ {"FIGCAPTION", FIGCAPTION},
205
+ {"FIGURE", FIGURE },
206
+ {"FOOTER", FOOTER },
207
+ {"FORM", FORM },
208
+ {"H1", H1 },
209
+ {"H2", H2 },
210
+ {"H3", H3 },
211
+ {"H4", H4 },
212
+ {"H5", H5 },
213
+ {"H6", H6 },
214
+ {"HEAD", HEAD },
215
+ {"HEADER", HEADER },
216
+ {"HGROUP", HGROUP },
217
+ {"HTML", HTML },
218
+ {"I", I },
219
+ {"IFRAME", IFRAME },
220
+ {"INS", INS },
221
+ {"KBD", KBD },
222
+ {"LABEL", LABEL },
223
+ {"LEGEND", LEGEND },
224
+ {"LI", LI },
225
+ {"MAIN", MAIN },
226
+ {"MAP", MAP },
227
+ {"MARK", MARK },
228
+ {"MATH", MATH },
229
+ {"MENU", MENU },
230
+ {"METER", METER },
231
+ {"NAV", NAV },
232
+ {"NOSCRIPT", NOSCRIPT },
233
+ {"OBJECT", OBJECT },
234
+ {"OL", OL },
235
+ {"OPTGROUP", OPTGROUP },
236
+ {"OPTION", OPTION },
237
+ {"OUTPUT", OUTPUT },
238
+ {"P", P },
239
+ {"PICTURE", PICTURE },
240
+ {"PRE", PRE },
241
+ {"PROGRESS", PROGRESS },
242
+ {"Q", Q },
243
+ {"RB", RB },
244
+ {"RP", RP },
245
+ {"RT", RT },
246
+ {"RTC", RTC },
247
+ {"RUBY", RUBY },
248
+ {"S", S },
249
+ {"SAMP", SAMP },
250
+ {"SCRIPT", SCRIPT },
251
+ {"SECTION", SECTION },
252
+ {"SELECT", SELECT },
253
+ {"SLOT", SLOT },
254
+ {"SMALL", SMALL },
255
+ {"SPAN", SPAN },
256
+ {"STRONG", STRONG },
257
+ {"STYLE", STYLE },
258
+ {"SUB", SUB },
259
+ {"SUMMARY", SUMMARY },
260
+ {"SUP", SUP },
261
+ {"SVG", SVG },
262
+ {"TABLE", TABLE },
263
+ {"TBODY", TBODY },
264
+ {"TD", TD },
265
+ {"TEMPLATE", TEMPLATE },
266
+ {"TEXTAREA", TEXTAREA },
267
+ {"TFOOT", TFOOT },
268
+ {"TH", TH },
269
+ {"THEAD", THEAD },
270
+ {"TIME", TIME },
271
+ {"TITLE", TITLE },
272
+ {"TR", TR },
273
+ {"U", U },
274
+ {"UL", UL },
275
+ {"VAR", VAR },
276
+ {"VIDEO", VIDEO },
277
+ {"CUSTOM", CUSTOM },
278
+ };
279
+
280
+ static const TagType TAG_TYPES_NOT_ALLOWED_IN_PARAGRAPHS[] = {
281
+ ADDRESS, ARTICLE, ASIDE, BLOCKQUOTE, DETAILS, DIV, DL,
282
+ FIELDSET, FIGCAPTION, FIGURE, FOOTER, FORM, H1, H2,
283
+ H3, H4, H5, H6, HEADER, HR, MAIN,
284
+ NAV, OL, P, PRE, SECTION,
285
+ };
286
+
287
+ static TagType tag_type_for_name(const String *tag_name) {
288
+ for (int i = 0; i < 126; i++) {
289
+ const TagMapEntry *entry = &TAG_TYPES_BY_TAG_NAME[i];
290
+ if (
291
+ strlen(entry->tag_name) == tag_name->size &&
292
+ memcmp(tag_name->contents, entry->tag_name, tag_name->size) == 0
293
+ ) {
294
+ return entry->tag_type;
295
+ }
296
+ }
297
+ return CUSTOM;
298
+ }
299
+
300
+ static inline Tag tag_new() {
301
+ Tag tag;
302
+ tag.type = END_;
303
+ tag.custom_tag_name = (String) array_new();
304
+ return tag;
305
+ }
306
+
307
+ static inline Tag tag_for_name(String name) {
308
+ Tag tag = tag_new();
309
+ tag.type = tag_type_for_name(&name);
310
+ if (tag.type == CUSTOM) {
311
+ tag.custom_tag_name = name;
312
+ } else {
313
+ array_delete(&name);
314
+ }
315
+ return tag;
316
+ }
317
+
318
+ static inline void tag_free(Tag *tag) {
319
+ if (tag->type == CUSTOM) {
320
+ array_delete(&tag->custom_tag_name);
321
+ }
322
+ }
323
+
324
+ static inline bool tag_is_void(const Tag *self) {
325
+ return self->type < END_OF_VOID_TAGS;
326
+ }
327
+
328
+ static inline bool tag_eq(const Tag *self, const Tag *other) {
329
+ if (self->type != other->type) return false;
330
+ if (self->type == CUSTOM) {
331
+ if (self->custom_tag_name.size != other->custom_tag_name.size) {
332
+ return false;
333
+ }
334
+ if (memcmp(
335
+ self->custom_tag_name.contents,
336
+ other->custom_tag_name.contents,
337
+ self->custom_tag_name.size
338
+ ) != 0) {
339
+ return false;
340
+ }
341
+ }
342
+ return true;
343
+ }
344
+
345
+ static bool tag_can_contain(Tag *self, const Tag *other) {
346
+ TagType child = other->type;
347
+
348
+ switch (self->type) {
349
+ case LI:
350
+ return child != LI;
351
+
352
+ case DT:
353
+ case DD:
354
+ return child != DT && child != DD;
355
+
356
+ case P:
357
+ for (int i = 0; i < 26; i++) {
358
+ if (child == TAG_TYPES_NOT_ALLOWED_IN_PARAGRAPHS[i]) {
359
+ return false;
360
+ }
361
+ }
362
+ return true;
363
+
364
+ case COLGROUP:
365
+ return child == COL;
366
+
367
+ case RB:
368
+ case RT:
369
+ case RP:
370
+ return child != RB && child != RT && child != RP;
371
+
372
+ case OPTGROUP:
373
+ return child != OPTGROUP;
374
+
375
+ case TR:
376
+ return child != TR;
377
+
378
+ case TD:
379
+ case TH:
380
+ return child != TD && child != TH && child != TR;
381
+
382
+ default:
383
+ return true;
384
+ }
385
+ }
@@ -0,0 +1,54 @@
1
+ #ifndef TREE_SITTER_ALLOC_H_
2
+ #define TREE_SITTER_ALLOC_H_
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <stdbool.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+
12
+ // Allow clients to override allocation functions
13
+ #ifdef TREE_SITTER_REUSE_ALLOCATOR
14
+
15
+ extern void *(*ts_current_malloc)(size_t size);
16
+ extern void *(*ts_current_calloc)(size_t count, size_t size);
17
+ extern void *(*ts_current_realloc)(void *ptr, size_t size);
18
+ extern void (*ts_current_free)(void *ptr);
19
+
20
+ #ifndef ts_malloc
21
+ #define ts_malloc ts_current_malloc
22
+ #endif
23
+ #ifndef ts_calloc
24
+ #define ts_calloc ts_current_calloc
25
+ #endif
26
+ #ifndef ts_realloc
27
+ #define ts_realloc ts_current_realloc
28
+ #endif
29
+ #ifndef ts_free
30
+ #define ts_free ts_current_free
31
+ #endif
32
+
33
+ #else
34
+
35
+ #ifndef ts_malloc
36
+ #define ts_malloc malloc
37
+ #endif
38
+ #ifndef ts_calloc
39
+ #define ts_calloc calloc
40
+ #endif
41
+ #ifndef ts_realloc
42
+ #define ts_realloc realloc
43
+ #endif
44
+ #ifndef ts_free
45
+ #define ts_free free
46
+ #endif
47
+
48
+ #endif
49
+
50
+ #ifdef __cplusplus
51
+ }
52
+ #endif
53
+
54
+ #endif // TREE_SITTER_ALLOC_H_