Changed pointer syntax

This commit is contained in:
Daniel Holden
2014-01-20 21:32:46 +00:00
parent 26b6216d7a
commit f518fb5a1d
2 changed files with 588 additions and 593 deletions

138
mpc.c
View File

@@ -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 ** When this function was written in recursive form
** it looked pretty nice. But I've since switched ** 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 ** unfortunate change for code simplicity but it
** is noble in the name of performance (and ** is noble in the name of performance (and
** not smashing the stack). ** not smashing the stack).
@@ -1288,6 +1288,26 @@ mpc_parser_t* mpc_fail(const char* m) {
return p; 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, ...) { mpc_parser_t *mpc_failf(const char *fmt, ...) {
va_list va; va_list va;
@@ -1297,8 +1317,8 @@ mpc_parser_t* mpc_failf(const char* fmt, ...) {
p->type = MPC_TYPE_FAIL; p->type = MPC_TYPE_FAIL;
va_start(va, fmt); va_start(va, fmt);
buffer = malloc(1024); buffer = malloc(2048);
vsnprintf(buffer, 1023, fmt, va); vsprintf(buffer, fmt, va);
va_end(va); va_end(va);
buffer = realloc(buffer, strlen(buffer) + 1); buffer = realloc(buffer, strlen(buffer) + 1);
@@ -1330,6 +1350,43 @@ mpc_parser_t* mpc_expect(mpc_parser_t* a, const char* expected) {
return p; 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 ** Basic Parsers
@@ -1342,82 +1399,34 @@ mpc_parser_t* mpc_any(void) {
} }
mpc_parser_t *mpc_char(char c) { mpc_parser_t *mpc_char(char c) {
char expected[4];
mpc_parser_t *p = mpc_undefined(); mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_SINGLE; p->type = MPC_TYPE_SINGLE;
p->data.single.x = c; p->data.single.x = c;
return mpc_expectf(p, "'%c'", c);
expected[0] = '\'';
expected[1] = c;
expected[2] = '\'';
expected[3] = '\0';
return mpc_expect(p, expected);
} }
mpc_parser_t *mpc_range(char s, char e) { mpc_parser_t *mpc_range(char s, char e) {
char expected[30];
char buff[2];
mpc_parser_t *p = mpc_undefined(); mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_RANGE; p->type = MPC_TYPE_RANGE;
p->data.range.x = s; p->data.range.x = s;
p->data.range.y = e; p->data.range.y = e;
return mpc_expectf(p, "character between '%c' and '%c'", s, 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);
} }
mpc_parser_t *mpc_oneof(const char *s) { mpc_parser_t *mpc_oneof(const char *s) {
char* expected;
mpc_parser_t *p = mpc_undefined(); mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_ONEOF; p->type = MPC_TYPE_ONEOF;
p->data.string.x = malloc(strlen(s) + 1); p->data.string.x = malloc(strlen(s) + 1);
strcpy(p->data.string.x, s); strcpy(p->data.string.x, s);
return mpc_expectf(p, "one of '%s'", s);
expected = malloc(strlen(s) + 10);
strcpy(expected, "one of '");
strcat(expected, s);
strcat(expected, "'");
p = mpc_expect(p, expected);
free(expected);
return p;
} }
mpc_parser_t *mpc_noneof(const char *s) { mpc_parser_t *mpc_noneof(const char *s) {
char* expected;
mpc_parser_t *p = mpc_undefined(); mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_NONEOF; p->type = MPC_TYPE_NONEOF;
p->data.string.x = malloc(strlen(s) + 1); p->data.string.x = malloc(strlen(s) + 1);
strcpy(p->data.string.x, s); strcpy(p->data.string.x, s);
return mpc_expectf(p, "one of '%s'", s);
expected = malloc(strlen(s) + 11);
strcpy(expected, "none of '");
strcat(expected, s);
strcat(expected, "'");
p = mpc_expect(p, expected);
free(expected);
return p;
} }
@@ -1425,30 +1434,15 @@ mpc_parser_t* mpc_satisfy(int(*f)(char)) {
mpc_parser_t *p = mpc_undefined(); mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_SATISFY; p->type = MPC_TYPE_SATISFY;
p->data.satisfy.f = f; p->data.satisfy.f = f;
return mpc_expectf(p, "character satisfying function %p", f);
return p;
} }
mpc_parser_t *mpc_string(const char *s) { mpc_parser_t *mpc_string(const char *s) {
char* expected;
mpc_parser_t *p = mpc_undefined(); mpc_parser_t *p = mpc_undefined();
p->type = MPC_TYPE_STRING; p->type = MPC_TYPE_STRING;
p->data.string.x = malloc(strlen(s) + 1); p->data.string.x = malloc(strlen(s) + 1);
strcpy(p->data.string.x, s); strcpy(p->data.string.x, s);
return mpc_expectf(p, "\"%s\"", s);
expected = malloc(strlen(s) + 3);
strcpy(expected, "\"");
strcat(expected, s);
strcat(expected, "\"");
p = mpc_expect(p, expected);
free(expected);
return p;
} }
/* /*

1
mpc.h
View File

@@ -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_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(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); mpc_parser_t *mpc_apply_to(mpc_parser_t *a, mpc_apply_to_t f, void *x);