Changed pointer syntax
This commit is contained in:
138
mpc.c
138
mpc.c
@@ -873,7 +873,7 @@ static mpc_err_t* mpc_stack_merger_err(mpc_stack_t* s, int n) {
|
||||
**
|
||||
** When this function was written in recursive form
|
||||
** it looked pretty nice. But I've since switched
|
||||
** it around to an akward while loop. It was an
|
||||
** it around to an awkward while loop. It was an
|
||||
** unfortunate change for code simplicity but it
|
||||
** is noble in the name of performance (and
|
||||
** not smashing the stack).
|
||||
@@ -1288,6 +1288,26 @@ mpc_parser_t* mpc_fail(const char* m) {
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
** As `snprintf` is not ANSI standard this
|
||||
** function `mpc_failf` should be considered
|
||||
** unsafe.
|
||||
**
|
||||
** You have a few options if this is going to be
|
||||
** trouble.
|
||||
**
|
||||
** - Ensure the format string does not exceed
|
||||
** the buffer length using precision specifiers
|
||||
** such as `%.512s`.
|
||||
**
|
||||
** - Patch this function in your code base to
|
||||
** use `snprintf` or whatever variant your
|
||||
** system supports.
|
||||
**
|
||||
** - Avoid it altogether.
|
||||
**
|
||||
*/
|
||||
|
||||
mpc_parser_t *mpc_failf(const char *fmt, ...) {
|
||||
|
||||
va_list va;
|
||||
@@ -1297,8 +1317,8 @@ mpc_parser_t* mpc_failf(const char* fmt, ...) {
|
||||
p->type = MPC_TYPE_FAIL;
|
||||
|
||||
va_start(va, fmt);
|
||||
buffer = malloc(1024);
|
||||
vsnprintf(buffer, 1023, fmt, va);
|
||||
buffer = malloc(2048);
|
||||
vsprintf(buffer, fmt, va);
|
||||
va_end(va);
|
||||
|
||||
buffer = realloc(buffer, strlen(buffer) + 1);
|
||||
@@ -1330,6 +1350,43 @@ mpc_parser_t* mpc_expect(mpc_parser_t* a, const char* expected) {
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
** As `snprintf` is not ANSI standard this
|
||||
** function `mpc_expectf` should be considered
|
||||
** unsafe.
|
||||
**
|
||||
** You have a few options if this is going to be
|
||||
** trouble.
|
||||
**
|
||||
** - Ensure the format string does not exceed
|
||||
** the buffer length using precision specifiers
|
||||
** such as `%.512s`.
|
||||
**
|
||||
** - Patch this function in your code base to
|
||||
** use `snprintf` or whatever variant your
|
||||
** system supports.
|
||||
**
|
||||
** - Avoid it altogether.
|
||||
**
|
||||
*/
|
||||
|
||||
mpc_parser_t *mpc_expectf(mpc_parser_t *a, const char *fmt, ...) {
|
||||
va_list va;
|
||||
char *buffer;
|
||||
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_EXPECT;
|
||||
|
||||
va_start(va, fmt);
|
||||
buffer = malloc(2048);
|
||||
vsprintf(buffer, fmt, va);
|
||||
va_end(va);
|
||||
|
||||
buffer = realloc(buffer, strlen(buffer) + 1);
|
||||
p->data.expect.x = a;
|
||||
p->data.expect.m = buffer;
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
** Basic Parsers
|
||||
@@ -1342,82 +1399,34 @@ mpc_parser_t* mpc_any(void) {
|
||||
}
|
||||
|
||||
mpc_parser_t *mpc_char(char c) {
|
||||
|
||||
char expected[4];
|
||||
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_SINGLE;
|
||||
p->data.single.x = c;
|
||||
|
||||
expected[0] = '\'';
|
||||
expected[1] = c;
|
||||
expected[2] = '\'';
|
||||
expected[3] = '\0';
|
||||
|
||||
return mpc_expect(p, expected);
|
||||
return mpc_expectf(p, "'%c'", c);
|
||||
}
|
||||
|
||||
mpc_parser_t *mpc_range(char s, char e) {
|
||||
|
||||
char expected[30];
|
||||
char buff[2];
|
||||
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_RANGE;
|
||||
p->data.range.x = s;
|
||||
p->data.range.y = e;
|
||||
|
||||
strcpy(expected, "character between '");
|
||||
buff[0] = s; buff[1] = '\0';
|
||||
strcat(expected, buff);
|
||||
strcat(expected, "' and '");
|
||||
buff[0] = e; buff[1] = '\0';
|
||||
strcat(expected, buff);
|
||||
strcat(expected, "'");
|
||||
|
||||
return mpc_expect(p, expected);
|
||||
return mpc_expectf(p, "character between '%c' and '%c'", s, e);
|
||||
}
|
||||
|
||||
mpc_parser_t *mpc_oneof(const char *s) {
|
||||
|
||||
char* expected;
|
||||
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_ONEOF;
|
||||
p->data.string.x = malloc(strlen(s) + 1);
|
||||
strcpy(p->data.string.x, s);
|
||||
|
||||
expected = malloc(strlen(s) + 10);
|
||||
strcpy(expected, "one of '");
|
||||
strcat(expected, s);
|
||||
strcat(expected, "'");
|
||||
|
||||
p = mpc_expect(p, expected);
|
||||
|
||||
free(expected);
|
||||
|
||||
return p;
|
||||
return mpc_expectf(p, "one of '%s'", s);
|
||||
}
|
||||
|
||||
mpc_parser_t *mpc_noneof(const char *s) {
|
||||
|
||||
char* expected;
|
||||
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_NONEOF;
|
||||
p->data.string.x = malloc(strlen(s) + 1);
|
||||
strcpy(p->data.string.x, s);
|
||||
|
||||
expected = malloc(strlen(s) + 11);
|
||||
strcpy(expected, "none of '");
|
||||
strcat(expected, s);
|
||||
strcat(expected, "'");
|
||||
|
||||
p = mpc_expect(p, expected);
|
||||
|
||||
free(expected);
|
||||
|
||||
return p;
|
||||
return mpc_expectf(p, "one of '%s'", s);
|
||||
|
||||
}
|
||||
|
||||
@@ -1425,30 +1434,15 @@ mpc_parser_t* mpc_satisfy(int(*f)(char)) {
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_SATISFY;
|
||||
p->data.satisfy.f = f;
|
||||
|
||||
return p;
|
||||
return mpc_expectf(p, "character satisfying function %p", f);
|
||||
}
|
||||
|
||||
mpc_parser_t *mpc_string(const char *s) {
|
||||
|
||||
char* expected;
|
||||
|
||||
mpc_parser_t *p = mpc_undefined();
|
||||
p->type = MPC_TYPE_STRING;
|
||||
p->data.string.x = malloc(strlen(s) + 1);
|
||||
strcpy(p->data.string.x, s);
|
||||
|
||||
expected = malloc(strlen(s) + 3);
|
||||
strcpy(expected, "\"");
|
||||
strcat(expected, s);
|
||||
strcat(expected, "\"");
|
||||
|
||||
p = mpc_expect(p, expected);
|
||||
|
||||
free(expected);
|
||||
|
||||
return p;
|
||||
|
||||
return mpc_expectf(p, "\"%s\"", s);
|
||||
}
|
||||
|
||||
/*
|
||||
|
1
mpc.h
1
mpc.h
@@ -99,6 +99,7 @@ mpc_parser_t* mpc_string(const char* s);
|
||||
*/
|
||||
|
||||
mpc_parser_t *mpc_expect(mpc_parser_t *a, const char *e);
|
||||
mpc_parser_t *mpc_expectf(mpc_parser_t *a, const char *fmt, ...);
|
||||
mpc_parser_t *mpc_apply(mpc_parser_t *a, mpc_apply_t f);
|
||||
mpc_parser_t *mpc_apply_to(mpc_parser_t *a, mpc_apply_to_t f, void *x);
|
||||
|
||||
|
Reference in New Issue
Block a user