diff --git a/config.yml b/config.yml index 421b8ee46..2363d9a77 100644 --- a/config.yml +++ b/config.yml @@ -32,8 +32,8 @@ errors: message: template: "Found %s when expecting %s at (%u:%u)." arguments: - - hb_string(token_type_to_friendly_string(found->type)) - - hb_string(token_type_to_friendly_string(expected_type)) + - token_type_to_friendly_string(found->type) + - token_type_to_friendly_string(expected_type) - found->location.start.line - found->location.start.column diff --git a/src/include/token.h b/src/include/token.h index a21d32719..e7ac9ccb7 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -12,7 +12,7 @@ token_T* token_init(hb_string_T value, token_type_T type, lexer_T* lexer); hb_string_T token_to_string(const token_T* token); hb_string_T token_type_to_string(token_type_T type); -const char* token_type_to_friendly_string(token_type_T type); +hb_string_T token_type_to_friendly_string(token_type_T type); char* token_types_to_friendly_string_va(token_type_T first_token, ...); char* token_types_to_friendly_string_valist(token_type_T first_token, va_list args); diff --git a/src/parser.c b/src/parser.c index cc07f9bd1..08b8431f7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -668,7 +668,7 @@ static AST_HTML_ATTRIBUTE_VALUE_NODE_T* parser_parse_html_attribute_value(parser append_unexpected_error( hb_string("Unexpected Token"), hb_string(expected), - hb_string(token_type_to_friendly_string(parser->current_token->type)), + token_type_to_friendly_string(parser->current_token->type), parser->current_token->location.start, parser->current_token->location.end, parser->allocator, diff --git a/src/parser_helpers.c b/src/parser_helpers.c index dea8fa1df..d4e78bd78 100644 --- a/src/parser_helpers.c +++ b/src/parser_helpers.c @@ -107,7 +107,7 @@ void parser_append_unexpected_error_impl( append_unexpected_error( hb_string(description), hb_string(expected), - hb_string(token_type_to_friendly_string(token->type)), + token_type_to_friendly_string(token->type), token->location.start, token->location.end, parser->allocator, @@ -129,7 +129,7 @@ void parser_append_unexpected_error_string( append_unexpected_error( hb_string(description), hb_string(expected), - hb_string(token_type_to_friendly_string(token->type)), + token_type_to_friendly_string(token->type), token->location.start, token->location.end, parser->allocator, diff --git a/src/token.c b/src/token.c index 12b2ff00d..02909ab1b 100644 --- a/src/token.c +++ b/src/token.c @@ -82,58 +82,54 @@ hb_string_T token_type_to_string(const token_type_T type) { case TOKEN_ERROR: return hb_string("TOKEN_ERROR"); case TOKEN_EOF: return hb_string("TOKEN_EOF"); } - - return hb_string("Unknown token_type_T"); } -const char* token_type_to_friendly_string(const token_type_T type) { +hb_string_T token_type_to_friendly_string(const token_type_T type) { switch (type) { - case TOKEN_WHITESPACE: return "whitespace"; - case TOKEN_NBSP: return "non-breaking space"; - case TOKEN_NEWLINE: return "a newline"; - case TOKEN_IDENTIFIER: return "an identifier"; - case TOKEN_HTML_DOCTYPE: return "``"; - case TOKEN_CDATA_START: return "``"; - case TOKEN_HTML_TAG_START: return "`<`"; - case TOKEN_HTML_TAG_END: return "`>`"; - case TOKEN_HTML_TAG_START_CLOSE: return "``"; - case TOKEN_HTML_COMMENT_START: return "``"; - case TOKEN_HTML_COMMENT_INVALID_END: return "`--!>`"; - case TOKEN_EQUALS: return "`=`"; - case TOKEN_QUOTE: return "a quote"; - case TOKEN_BACKTICK: return "a backtick"; - case TOKEN_BACKSLASH: return "`\\`"; - case TOKEN_DASH: return "`-`"; - case TOKEN_UNDERSCORE: return "`_`"; - case TOKEN_EXCLAMATION: return "`!`"; - case TOKEN_SLASH: return "`/`"; - case TOKEN_SEMICOLON: return "`;`"; - case TOKEN_COLON: return "`:`"; - case TOKEN_AT: return "`@`"; - case TOKEN_LT: return "`<`"; - case TOKEN_PERCENT: return "`%`"; - case TOKEN_AMPERSAND: return "`&`"; - case TOKEN_ERB_START: return "`<%`"; - case TOKEN_ERB_CONTENT: return "ERB content"; - case TOKEN_ERB_END: return "`%>`"; - case TOKEN_CHARACTER: return "a character"; - case TOKEN_ERROR: return "an error token"; - case TOKEN_EOF: return "end of file"; + case TOKEN_WHITESPACE: return hb_string("whitespace"); + case TOKEN_NBSP: return hb_string("non-breaking space"); + case TOKEN_NEWLINE: return hb_string("a newline"); + case TOKEN_IDENTIFIER: return hb_string("an identifier"); + case TOKEN_HTML_DOCTYPE: return hb_string("``"); + case TOKEN_CDATA_START: return hb_string("``"); + case TOKEN_HTML_TAG_START: return hb_string("`<`"); + case TOKEN_HTML_TAG_END: return hb_string("`>`"); + case TOKEN_HTML_TAG_START_CLOSE: return hb_string("``"); + case TOKEN_HTML_COMMENT_START: return hb_string("``"); + case TOKEN_HTML_COMMENT_INVALID_END: return hb_string("`--!>`"); + case TOKEN_EQUALS: return hb_string("`=`"); + case TOKEN_QUOTE: return hb_string("a quote"); + case TOKEN_BACKTICK: return hb_string("a backtick"); + case TOKEN_BACKSLASH: return hb_string("`\\`"); + case TOKEN_DASH: return hb_string("`-`"); + case TOKEN_UNDERSCORE: return hb_string("`_`"); + case TOKEN_EXCLAMATION: return hb_string("`!`"); + case TOKEN_SLASH: return hb_string("`/`"); + case TOKEN_SEMICOLON: return hb_string("`;`"); + case TOKEN_COLON: return hb_string("`:`"); + case TOKEN_AT: return hb_string("`@`"); + case TOKEN_LT: return hb_string("`<`"); + case TOKEN_PERCENT: return hb_string("`%`"); + case TOKEN_AMPERSAND: return hb_string("`&`"); + case TOKEN_ERB_START: return hb_string("`<%`"); + case TOKEN_ERB_CONTENT: return hb_string("ERB content"); + case TOKEN_ERB_END: return hb_string("`%>`"); + case TOKEN_CHARACTER: return hb_string("a character"); + case TOKEN_ERROR: return hb_string("an error token"); + case TOKEN_EOF: return hb_string("end of file"); } - - return "Unknown token type"; } char* token_types_to_friendly_string_valist(token_type_T first_token, va_list args) { if ((int) first_token == TOKEN_SENTINEL) { return herb_strdup(""); } size_t count = 0; - const char* names[32]; + hb_string_T names[32]; token_type_T current = first_token; 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 hb_buffer_init(&buffer, 128); for (size_t i = 0; i < count; i++) { - hb_buffer_append(&buffer, names[i]); + hb_buffer_append_string(&buffer, names[i]); if (i < count - 1) { if (count > 2) { hb_buffer_append(&buffer, ", "); } diff --git a/test/c/test_token.c b/test/c/test_token.c index 6e41707a3..13fd7c3a8 100644 --- a/test/c/test_token.c +++ b/test/c/test_token.c @@ -4,29 +4,30 @@ #include "../../src/include/lex_helpers.h" #include "../../src/include/token.h" #include "../../src/include/util/hb_allocator.h" +#include "../../src/include/util/hb_string.h" TEST(test_token) ck_assert(hb_string_equals(token_type_to_string(TOKEN_IDENTIFIER), hb_string("TOKEN_IDENTIFIER"))); END TEST(test_token_type_to_friendly_string) - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_IDENTIFIER), "an identifier"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_WHITESPACE), "whitespace"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_NEWLINE), "a newline"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_QUOTE), "a quote"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_CHARACTER), "a character"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_EOF), "end of file"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_START), "`<`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_END), "`>`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_SELF_CLOSE), "`/>`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_TAG_START_CLOSE), "``"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_EQUALS), "`=`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_SLASH), "`/`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_COLON), "`:`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_ERB_START), "`<%`"); - ck_assert_str_eq(token_type_to_friendly_string(TOKEN_ERB_END), "`%>`"); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_IDENTIFIER), hb_string("an identifier"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_WHITESPACE), hb_string("whitespace"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_NEWLINE), hb_string("a newline"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_QUOTE), hb_string("a quote"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_CHARACTER), hb_string("a character"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_EOF), hb_string("end of file"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_START), hb_string("`<`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_END), hb_string("`>`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_SELF_CLOSE), hb_string("`/>`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_TAG_START_CLOSE), hb_string("``"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_EQUALS), hb_string("`=`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_SLASH), hb_string("`/`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_COLON), hb_string("`:`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_ERB_START), hb_string("`<%`"))); + ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_ERB_END), hb_string("`%>`"))); END TEST(test_token_types_to_friendly_string)