diff --git a/includes/pstring.h b/includes/pstring.h index 3522d32..86af6f6 100644 --- a/includes/pstring.h +++ b/includes/pstring.h @@ -3,11 +3,14 @@ #include #include +#include +#include typedef struct pstring PString; PString* CreateString(void); void StringAppend(const char* text, PString* string); +void StringVAppend(PString* string, const char* format, ...); void StringFree(PString* string); void StringTruncate(PString* string); char* StringToCString(PString* string); diff --git a/src/parser.c b/src/parser.c index d2c91fc..b617464 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2,7 +2,7 @@ #include "../includes/pstring.h" #include -#define ParserExpects(...) ParserExpectList(sizeof((TokenType[]){__VA_ARGS__})/sizeof(TokenType), __VA_ARGS__) +#define ParserExpects(...) ParserExpectList(sizeof((TokenType[]){__VA_ARGS__}) / sizeof(TokenType), __VA_ARGS__) static Array* Tokens; @@ -23,8 +23,7 @@ Token* ParserExpect(TokenType type); Token* ParserExpectList(int count, ...); Token* ParserWant(TokenType type); void ParserExpectLineEnd(void); -//Some more on atrributes: -//https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Attributes.html#Function%20Attributes +//Some more on attributes: https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Attributes.html#Function%20Attributes void ParserSync(char* format, ...) __attribute__ ((noreturn)); void ParserSyncExpect(TokenType expected, TokenType found) __attribute__ ((noreturn)); void ParserSyncMessage(char* message) __attribute__ ((noreturn)); @@ -214,15 +213,17 @@ Token* ParserExpectList(int count, ...) { //Are we at the end? if (i + 1 >= count) { - StringAppend("or ", MessageString); - StringAppend(buffer, MessageString); + StringVAppend(MessageString, "or %s", buffer); } else { - StringAppend(buffer, MessageString); - StringAppend(", ", MessageString); + StringVAppend(MessageString, "%s, ", buffer); } } + TokenTypeStringify(token->Type, buffer); + + StringVAppend(MessageString, " but got %s.", buffer); + ParserSyncMessage(StringToCString(MessageString)); } diff --git a/src/pstring.c b/src/pstring.c index fc493b9..dfae3ed 100644 --- a/src/pstring.c +++ b/src/pstring.c @@ -1,6 +1,6 @@ #include "../includes/pstring.h" -#define DEFUALT_SSTRING_SIZE 64 +#define DEFUALT_PSTRING_SIZE 64 struct pstring { char* Buffer; @@ -11,8 +11,8 @@ struct pstring { PString* CreateString(void) { PString* string = calloc(1, sizeof(PString)); - string->Buffer = calloc(DEFUALT_SSTRING_SIZE, sizeof(char)); - string->Capacity = DEFUALT_SSTRING_SIZE; + string->Buffer = calloc(DEFUALT_PSTRING_SIZE, sizeof(char)); + string->Capacity = DEFUALT_PSTRING_SIZE; return string; } @@ -40,6 +40,27 @@ void StringAppend(const char* text, PString* string) { string->Buffer[string->Length] = '\0'; } +void StringVAppend(PString* string, const char* format, ...) { + if (!string) return; + + va_list args; + va_start(args, format); + + int bytes = vsnprintf(NULL, 0, format, args) + 1; + + va_end(args); + + StringResize(bytes, string); + + va_start(args, format); + + (void) vsnprintf(&string->Buffer[string->Length], bytes, format, args); + + va_end(args); + + string->Length += bytes - 1; //Make sure Length points at the 'NUL' byte placed at the end. +} + void StringFree(PString* string) { if (!string) return;