Skip to content

Commit f0438a5

Browse files
authored
fix serialize/deserialize tree-sitter-superhtml (#65)
1 parent 295d622 commit f0438a5

File tree

4 files changed

+171
-10
lines changed

4 files changed

+171
-10
lines changed

tree-sitter-superhtml/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tree-sitter-superhtml/src/scanner.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ static unsigned serialize(Scanner *scanner, char *buffer) {
4343
if (size + 2 + name_length >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
4444
break;
4545
}
46-
buffer[size++] = (char)tag.type;
47-
buffer[size++] = (char)name_length;
46+
buffer[size++] = (uint8_t)tag.type;
47+
buffer[size++] = (uint8_t)name_length;
4848
strncpy(&buffer[size], tag.custom_tag_name.contents, name_length);
4949
size += name_length;
5050
} else {
5151
if (size + 1 >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
5252
break;
5353
}
54-
buffer[size++] = (char)tag.type;
54+
buffer[size++] = (uint8_t)tag.type;
5555
}
5656
}
5757

@@ -81,7 +81,7 @@ static void deserialize(Scanner *scanner, const char *buffer, unsigned length) {
8181
unsigned iter = 0;
8282
for (iter = 0; iter < serialized_tag_count; iter++) {
8383
Tag tag = tag_new();
84-
tag.type = (TagType)buffer[size++];
84+
tag.type = (TagType)(uint8_t)buffer[size++];
8585
if (tag.type == CUSTOM) {
8686
uint16_t name_length = (uint8_t)buffer[size++];
8787
array_reserve(&tag.custom_tag_name, name_length);

tree-sitter-superhtml/src/tag.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ typedef enum {
132132
UL,
133133
VAR,
134134
VIDEO,
135+
CTX,
135136

136137
CUSTOM,
137138

@@ -150,7 +151,7 @@ typedef struct {
150151
String custom_tag_name;
151152
} Tag;
152153

153-
static const TagMapEntry TAG_TYPES_BY_TAG_NAME[128] = {
154+
static const TagMapEntry TAG_TYPES_BY_TAG_NAME[129] = {
154155
{"AREA", AREA },
155156
{"BASE", BASE },
156157
{"BASEFONT", BASEFONT },
@@ -278,6 +279,7 @@ static const TagMapEntry TAG_TYPES_BY_TAG_NAME[128] = {
278279
{"UL", UL },
279280
{"VAR", VAR },
280281
{"VIDEO", VIDEO },
282+
{"CTX", CTX },
281283
{"CUSTOM", CUSTOM },
282284
};
283285

@@ -289,7 +291,8 @@ static const TagType TAG_TYPES_NOT_ALLOWED_IN_PARAGRAPHS[] = {
289291
};
290292

291293
static TagType tag_type_for_name(const String *tag_name) {
292-
for (int i = 0; i < 126; i++) {
294+
int tag_names_size = sizeof TAG_TYPES_BY_TAG_NAME / sizeof TAG_TYPES_BY_TAG_NAME[0];
295+
for (int i = 0; i < tag_names_size; i++) {
293296
const TagMapEntry *entry = &TAG_TYPES_BY_TAG_NAME[i];
294297
if (
295298
strlen(entry->tag_name) == tag_name->size &&
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
==================================
2+
Ctx Tag
3+
==================================
4+
5+
<ctx about="$site.page('about')">
6+
<a href="$ctx.about.link()" :text="$ctx.about.title"></a>
7+
</ctx>
8+
9+
---
10+
11+
(document
12+
(element
13+
(start_tag
14+
(tag_name)
15+
(attribute
16+
(attribute_name)
17+
(quoted_attribute_value
18+
(attribute_value))))
19+
(element
20+
(start_tag
21+
(tag_name)
22+
(attribute
23+
(attribute_name)
24+
(quoted_attribute_value
25+
(attribute_value)))
26+
(attribute
27+
(attribute_name)
28+
(quoted_attribute_value
29+
(attribute_value))))
30+
(end_tag
31+
(tag_name)))
32+
(end_tag
33+
(tag_name))))
34+
35+
==================================
36+
Super Tag
37+
==================================
38+
39+
<!DOCTYPE html>
40+
<html>
41+
<head>
42+
<meta charset="UTF-8">
43+
<title id="title"><super></title>
44+
</head>
45+
<body id="main">
46+
<super>
47+
</body>
48+
</html>
49+
50+
---
51+
52+
(document
53+
(doctype)
54+
(element
55+
(start_tag
56+
(tag_name))
57+
(element
58+
(start_tag
59+
(tag_name))
60+
(element
61+
(start_tag
62+
(tag_name)
63+
(attribute
64+
(attribute_name)
65+
(quoted_attribute_value
66+
(attribute_value)))))
67+
(element
68+
(start_tag
69+
(tag_name)
70+
(attribute
71+
(attribute_name)
72+
(quoted_attribute_value
73+
(attribute_value))))
74+
(element
75+
(start_tag
76+
(tag_name)))
77+
(end_tag
78+
(tag_name)))
79+
(end_tag
80+
(tag_name)))
81+
(element
82+
(start_tag
83+
(tag_name)
84+
(attribute
85+
(attribute_name)
86+
(quoted_attribute_value
87+
(attribute_value))))
88+
(element
89+
(start_tag
90+
(tag_name)))
91+
(end_tag
92+
(tag_name)))
93+
(end_tag
94+
(tag_name))))
95+
96+
==================================
97+
Extend Tag
98+
==================================
99+
100+
<extend template="base.shtml">
101+
102+
<title id="title" :text="$site.title"></title>
103+
104+
<body id="main">
105+
<h1 :text="$page.title"></h1>
106+
<div :html="$page.content()"></div>
107+
</body>
108+
109+
---
110+
111+
(document
112+
(element
113+
(start_tag
114+
(tag_name)
115+
(attribute
116+
(attribute_name)
117+
(quoted_attribute_value
118+
(attribute_value)))))
119+
(element
120+
(start_tag
121+
(tag_name)
122+
(attribute
123+
(attribute_name)
124+
(quoted_attribute_value
125+
(attribute_value)))
126+
(attribute
127+
(attribute_name)
128+
(quoted_attribute_value
129+
(attribute_value))))
130+
(end_tag
131+
(tag_name)))
132+
(element
133+
(start_tag
134+
(tag_name)
135+
(attribute
136+
(attribute_name)
137+
(quoted_attribute_value
138+
(attribute_value))))
139+
(element
140+
(start_tag
141+
(tag_name)
142+
(attribute
143+
(attribute_name)
144+
(quoted_attribute_value
145+
(attribute_value))))
146+
(end_tag
147+
(tag_name)))
148+
(element
149+
(start_tag
150+
(tag_name)
151+
(attribute
152+
(attribute_name)
153+
(quoted_attribute_value
154+
(attribute_value))))
155+
(end_tag
156+
(tag_name)))
157+
(end_tag
158+
(tag_name))))

0 commit comments

Comments
 (0)