Skip to content

Commit 1833919

Browse files
authored
C: Migrate token_type_to_friendly_string to hb_string_T (#1316)
Follow up on #687, #1313, and #1314
1 parent 71af52f commit 1833919

File tree

6 files changed

+63
-66
lines changed

6 files changed

+63
-66
lines changed

config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ errors:
3232
message:
3333
template: "Found %s when expecting %s at (%u:%u)."
3434
arguments:
35-
- hb_string(token_type_to_friendly_string(found->type))
36-
- hb_string(token_type_to_friendly_string(expected_type))
35+
- token_type_to_friendly_string(found->type)
36+
- token_type_to_friendly_string(expected_type)
3737
- found->location.start.line
3838
- found->location.start.column
3939

src/include/token.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
token_T* token_init(hb_string_T value, token_type_T type, lexer_T* lexer);
1313
hb_string_T token_to_string(const token_T* token);
1414
hb_string_T token_type_to_string(token_type_T type);
15-
const char* token_type_to_friendly_string(token_type_T type);
15+
hb_string_T token_type_to_friendly_string(token_type_T type);
1616
char* token_types_to_friendly_string_va(token_type_T first_token, ...);
1717
char* token_types_to_friendly_string_valist(token_type_T first_token, va_list args);
1818

src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_html_attribute_value(parser
668668
append_unexpected_error(
669669
hb_string("Unexpected Token"),
670670
hb_string(expected),
671-
hb_string(token_type_to_friendly_string(parser->current_token->type)),
671+
token_type_to_friendly_string(parser->current_token->type),
672672
parser->current_token->location.start,
673673
parser->current_token->location.end,
674674
parser->allocator,

src/parser_helpers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void parser_append_unexpected_error_impl(
107107
append_unexpected_error(
108108
hb_string(description),
109109
hb_string(expected),
110-
hb_string(token_type_to_friendly_string(token->type)),
110+
token_type_to_friendly_string(token->type),
111111
token->location.start,
112112
token->location.end,
113113
parser->allocator,
@@ -129,7 +129,7 @@ void parser_append_unexpected_error_string(
129129
append_unexpected_error(
130130
hb_string(description),
131131
hb_string(expected),
132-
hb_string(token_type_to_friendly_string(token->type)),
132+
token_type_to_friendly_string(token->type),
133133
token->location.start,
134134
token->location.end,
135135
parser->allocator,

src/token.c

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -82,58 +82,54 @@ hb_string_T token_type_to_string(const token_type_T type) {
8282
case TOKEN_ERROR: return hb_string("TOKEN_ERROR");
8383
case TOKEN_EOF: return hb_string("TOKEN_EOF");
8484
}
85-
86-
return hb_string("Unknown token_type_T");
8785
}
8886

89-
const char* token_type_to_friendly_string(const token_type_T type) {
87+
hb_string_T token_type_to_friendly_string(const token_type_T type) {
9088
switch (type) {
91-
case TOKEN_WHITESPACE: return "whitespace";
92-
case TOKEN_NBSP: return "non-breaking space";
93-
case TOKEN_NEWLINE: return "a newline";
94-
case TOKEN_IDENTIFIER: return "an identifier";
95-
case TOKEN_HTML_DOCTYPE: return "`<!DOCTYPE`";
96-
case TOKEN_XML_DECLARATION: return "`<?xml`";
97-
case TOKEN_XML_DECLARATION_END: return "`?>`";
98-
case TOKEN_CDATA_START: return "`<![CDATA[`";
99-
case TOKEN_CDATA_END: return "`]]>`";
100-
case TOKEN_HTML_TAG_START: return "`<`";
101-
case TOKEN_HTML_TAG_END: return "`>`";
102-
case TOKEN_HTML_TAG_START_CLOSE: return "`</`";
103-
case TOKEN_HTML_TAG_SELF_CLOSE: return "`/>`";
104-
case TOKEN_HTML_COMMENT_START: return "`<!--`";
105-
case TOKEN_HTML_COMMENT_END: return "`-->`";
106-
case TOKEN_HTML_COMMENT_INVALID_END: return "`--!>`";
107-
case TOKEN_EQUALS: return "`=`";
108-
case TOKEN_QUOTE: return "a quote";
109-
case TOKEN_BACKTICK: return "a backtick";
110-
case TOKEN_BACKSLASH: return "`\\`";
111-
case TOKEN_DASH: return "`-`";
112-
case TOKEN_UNDERSCORE: return "`_`";
113-
case TOKEN_EXCLAMATION: return "`!`";
114-
case TOKEN_SLASH: return "`/`";
115-
case TOKEN_SEMICOLON: return "`;`";
116-
case TOKEN_COLON: return "`:`";
117-
case TOKEN_AT: return "`@`";
118-
case TOKEN_LT: return "`<`";
119-
case TOKEN_PERCENT: return "`%`";
120-
case TOKEN_AMPERSAND: return "`&`";
121-
case TOKEN_ERB_START: return "`<%`";
122-
case TOKEN_ERB_CONTENT: return "ERB content";
123-
case TOKEN_ERB_END: return "`%>`";
124-
case TOKEN_CHARACTER: return "a character";
125-
case TOKEN_ERROR: return "an error token";
126-
case TOKEN_EOF: return "end of file";
89+
case TOKEN_WHITESPACE: return hb_string("whitespace");
90+
case TOKEN_NBSP: return hb_string("non-breaking space");
91+
case TOKEN_NEWLINE: return hb_string("a newline");
92+
case TOKEN_IDENTIFIER: return hb_string("an identifier");
93+
case TOKEN_HTML_DOCTYPE: return hb_string("`<!DOCTYPE`");
94+
case TOKEN_XML_DECLARATION: return hb_string("`<?xml`");
95+
case TOKEN_XML_DECLARATION_END: return hb_string("`?>`");
96+
case TOKEN_CDATA_START: return hb_string("`<![CDATA[`");
97+
case TOKEN_CDATA_END: return hb_string("`]]>`");
98+
case TOKEN_HTML_TAG_START: return hb_string("`<`");
99+
case TOKEN_HTML_TAG_END: return hb_string("`>`");
100+
case TOKEN_HTML_TAG_START_CLOSE: return hb_string("`</`");
101+
case TOKEN_HTML_TAG_SELF_CLOSE: return hb_string("`/>`");
102+
case TOKEN_HTML_COMMENT_START: return hb_string("`<!--`");
103+
case TOKEN_HTML_COMMENT_END: return hb_string("`-->`");
104+
case TOKEN_HTML_COMMENT_INVALID_END: return hb_string("`--!>`");
105+
case TOKEN_EQUALS: return hb_string("`=`");
106+
case TOKEN_QUOTE: return hb_string("a quote");
107+
case TOKEN_BACKTICK: return hb_string("a backtick");
108+
case TOKEN_BACKSLASH: return hb_string("`\\`");
109+
case TOKEN_DASH: return hb_string("`-`");
110+
case TOKEN_UNDERSCORE: return hb_string("`_`");
111+
case TOKEN_EXCLAMATION: return hb_string("`!`");
112+
case TOKEN_SLASH: return hb_string("`/`");
113+
case TOKEN_SEMICOLON: return hb_string("`;`");
114+
case TOKEN_COLON: return hb_string("`:`");
115+
case TOKEN_AT: return hb_string("`@`");
116+
case TOKEN_LT: return hb_string("`<`");
117+
case TOKEN_PERCENT: return hb_string("`%`");
118+
case TOKEN_AMPERSAND: return hb_string("`&`");
119+
case TOKEN_ERB_START: return hb_string("`<%`");
120+
case TOKEN_ERB_CONTENT: return hb_string("ERB content");
121+
case TOKEN_ERB_END: return hb_string("`%>`");
122+
case TOKEN_CHARACTER: return hb_string("a character");
123+
case TOKEN_ERROR: return hb_string("an error token");
124+
case TOKEN_EOF: return hb_string("end of file");
127125
}
128-
129-
return "Unknown token type";
130126
}
131127

132128
char* token_types_to_friendly_string_valist(token_type_T first_token, va_list args) {
133129
if ((int) first_token == TOKEN_SENTINEL) { return herb_strdup(""); }
134130

135131
size_t count = 0;
136-
const char* names[32];
132+
hb_string_T names[32];
137133
token_type_T current = first_token;
138134

139135
while ((int) current != TOKEN_SENTINEL && count < 32) {
@@ -145,7 +141,7 @@ char* token_types_to_friendly_string_valist(token_type_T first_token, va_list ar
145141
hb_buffer_init(&buffer, 128);
146142

147143
for (size_t i = 0; i < count; i++) {
148-
hb_buffer_append(&buffer, names[i]);
144+
hb_buffer_append_string(&buffer, names[i]);
149145

150146
if (i < count - 1) {
151147
if (count > 2) { hb_buffer_append(&buffer, ", "); }

test/c/test_token.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,30 @@
44
#include "../../src/include/lex_helpers.h"
55
#include "../../src/include/token.h"
66
#include "../../src/include/util/hb_allocator.h"
7+
#include "../../src/include/util/hb_string.h"
78

89
TEST(test_token)
910
ck_assert(hb_string_equals(token_type_to_string(TOKEN_IDENTIFIER), hb_string("TOKEN_IDENTIFIER")));
1011
END
1112

1213
TEST(test_token_type_to_friendly_string)
13-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_IDENTIFIER), "an identifier");
14-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_WHITESPACE), "whitespace");
15-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_NEWLINE), "a newline");
16-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_QUOTE), "a quote");
17-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_CHARACTER), "a character");
18-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_EOF), "end of file");
19-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_START), "`<`");
20-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_END), "`>`");
21-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_SELF_CLOSE), "`/>`");
22-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_START_CLOSE), "`</`");
23-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_COMMENT_START), "`<!--`");
24-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_COMMENT_END), "`-->`");
25-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_EQUALS), "`=`");
26-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_SLASH), "`/`");
27-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_COLON), "`:`");
28-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_ERB_START), "`<%`");
29-
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_ERB_END), "`%>`");
14+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_IDENTIFIER), hb_string("an identifier")));
15+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_WHITESPACE), hb_string("whitespace")));
16+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_NEWLINE), hb_string("a newline")));
17+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_QUOTE), hb_string("a quote")));
18+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_CHARACTER), hb_string("a character")));
19+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_EOF), hb_string("end of file")));
20+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_START), hb_string("`<`")));
21+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_END), hb_string("`>`")));
22+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_SELF_CLOSE), hb_string("`/>`")));
23+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_START_CLOSE), hb_string("`</`")));
24+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_COMMENT_START), hb_string("`<!--`")));
25+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_COMMENT_END), hb_string("`-->`")));
26+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_EQUALS), hb_string("`=`")));
27+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_SLASH), hb_string("`/`")));
28+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_COLON), hb_string("`:`")));
29+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_ERB_START), hb_string("`<%`")));
30+
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_ERB_END), hb_string("`%>`")));
3031
END
3132

3233
TEST(test_token_types_to_friendly_string)

0 commit comments

Comments
 (0)