Added StringVAppend to append via a format and va_args list.
This commit is contained in:
@@ -3,11 +3,14 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef struct pstring PString;
|
typedef struct pstring PString;
|
||||||
|
|
||||||
PString* CreateString(void);
|
PString* CreateString(void);
|
||||||
void StringAppend(const char* text, PString* string);
|
void StringAppend(const char* text, PString* string);
|
||||||
|
void StringVAppend(PString* string, const char* format, ...);
|
||||||
void StringFree(PString* string);
|
void StringFree(PString* string);
|
||||||
void StringTruncate(PString* string);
|
void StringTruncate(PString* string);
|
||||||
char* StringToCString(PString* string);
|
char* StringToCString(PString* string);
|
||||||
|
|||||||
+8
-7
@@ -2,7 +2,7 @@
|
|||||||
#include "../includes/pstring.h"
|
#include "../includes/pstring.h"
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
#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;
|
static Array* Tokens;
|
||||||
|
|
||||||
@@ -23,8 +23,7 @@ Token* ParserExpect(TokenType type);
|
|||||||
Token* ParserExpectList(int count, ...);
|
Token* ParserExpectList(int count, ...);
|
||||||
Token* ParserWant(TokenType type);
|
Token* ParserWant(TokenType type);
|
||||||
void ParserExpectLineEnd(void);
|
void ParserExpectLineEnd(void);
|
||||||
//Some more on atrributes:
|
//Some more on attributes: https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Attributes.html#Function%20Attributes
|
||||||
//https://gcc.gnu.org/onlinedocs/gcc-3.1/gcc/Function-Attributes.html#Function%20Attributes
|
|
||||||
void ParserSync(char* format, ...) __attribute__ ((noreturn));
|
void ParserSync(char* format, ...) __attribute__ ((noreturn));
|
||||||
void ParserSyncExpect(TokenType expected, TokenType found) __attribute__ ((noreturn));
|
void ParserSyncExpect(TokenType expected, TokenType found) __attribute__ ((noreturn));
|
||||||
void ParserSyncMessage(char* message) __attribute__ ((noreturn));
|
void ParserSyncMessage(char* message) __attribute__ ((noreturn));
|
||||||
@@ -214,15 +213,17 @@ Token* ParserExpectList(int count, ...) {
|
|||||||
|
|
||||||
//Are we at the end?
|
//Are we at the end?
|
||||||
if (i + 1 >= count) {
|
if (i + 1 >= count) {
|
||||||
StringAppend("or ", MessageString);
|
StringVAppend(MessageString, "or %s", buffer);
|
||||||
StringAppend(buffer, MessageString);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
StringAppend(buffer, MessageString);
|
StringVAppend(MessageString, "%s, ", buffer);
|
||||||
StringAppend(", ", MessageString);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TokenTypeStringify(token->Type, buffer);
|
||||||
|
|
||||||
|
StringVAppend(MessageString, " but got %s.", buffer);
|
||||||
|
|
||||||
ParserSyncMessage(StringToCString(MessageString));
|
ParserSyncMessage(StringToCString(MessageString));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+24
-3
@@ -1,6 +1,6 @@
|
|||||||
#include "../includes/pstring.h"
|
#include "../includes/pstring.h"
|
||||||
|
|
||||||
#define DEFUALT_SSTRING_SIZE 64
|
#define DEFUALT_PSTRING_SIZE 64
|
||||||
|
|
||||||
struct pstring {
|
struct pstring {
|
||||||
char* Buffer;
|
char* Buffer;
|
||||||
@@ -11,8 +11,8 @@ struct pstring {
|
|||||||
PString* CreateString(void) {
|
PString* CreateString(void) {
|
||||||
PString* string = calloc(1, sizeof(PString));
|
PString* string = calloc(1, sizeof(PString));
|
||||||
|
|
||||||
string->Buffer = calloc(DEFUALT_SSTRING_SIZE, sizeof(char));
|
string->Buffer = calloc(DEFUALT_PSTRING_SIZE, sizeof(char));
|
||||||
string->Capacity = DEFUALT_SSTRING_SIZE;
|
string->Capacity = DEFUALT_PSTRING_SIZE;
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
@@ -40,6 +40,27 @@ void StringAppend(const char* text, PString* string) {
|
|||||||
string->Buffer[string->Length] = '\0';
|
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) {
|
void StringFree(PString* string) {
|
||||||
if (!string) return;
|
if (!string) return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user