Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/include/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/parser_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
82 changes: 39 additions & 43 deletions src/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "`<!DOCTYPE`";
case TOKEN_XML_DECLARATION: return "`<?xml`";
case TOKEN_XML_DECLARATION_END: return "`?>`";
case TOKEN_CDATA_START: return "`<![CDATA[`";
case TOKEN_CDATA_END: return "`]]>`";
case TOKEN_HTML_TAG_START: return "`<`";
case TOKEN_HTML_TAG_END: return "`>`";
case TOKEN_HTML_TAG_START_CLOSE: return "`</`";
case TOKEN_HTML_TAG_SELF_CLOSE: return "`/>`";
case TOKEN_HTML_COMMENT_START: return "`<!--`";
case TOKEN_HTML_COMMENT_END: 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("`<!DOCTYPE`");
case TOKEN_XML_DECLARATION: return hb_string("`<?xml`");
case TOKEN_XML_DECLARATION_END: return hb_string("`?>`");
case TOKEN_CDATA_START: return hb_string("`<![CDATA[`");
case TOKEN_CDATA_END: 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_TAG_SELF_CLOSE: return hb_string("`/>`");
case TOKEN_HTML_COMMENT_START: return hb_string("`<!--`");
case TOKEN_HTML_COMMENT_END: 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) {
Expand All @@ -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, ", "); }
Expand Down
35 changes: 18 additions & 17 deletions test/c/test_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_HTML_COMMENT_START), "`<!--`");
ck_assert_str_eq(token_type_to_friendly_string(TOKEN_HTML_COMMENT_END), "`-->`");
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_HTML_COMMENT_START), hb_string("`<!--`")));
ck_assert(hb_string_equals(token_type_to_friendly_string(TOKEN_HTML_COMMENT_END), 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)
Expand Down
Loading