diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-08-16 13:51:35 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-08-16 13:51:35 -0400 |
commit | 1617c304c270996504ac5d285e3b417e3310f97f (patch) | |
tree | e878ff8109ff486e8bae684d29d9f7210ec88d62 /util |
Basic tokenization
Diffstat (limited to 'util')
-rw-r--r-- | util/err.c | 10 | ||||
-rw-r--r-- | util/files.c | 28 |
2 files changed, 38 insertions, 0 deletions
diff --git a/util/err.c b/util/err.c new file mode 100644 index 0000000..62886c4 --- /dev/null +++ b/util/err.c @@ -0,0 +1,10 @@ +typedef uint32_t LineNo; + +static void err_print(LineNo line, LineNo col, const char *fmt, ...) { + /* TODO: Color */ + va_list args; + fprintf(stderr, "Error at line %lu col %lu:\n", (unsigned long)line, (unsigned long)col); + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); +} diff --git a/util/files.c b/util/files.c new file mode 100644 index 0000000..0afa843 --- /dev/null +++ b/util/files.c @@ -0,0 +1,28 @@ +static int fpeekc(FILE *fp) { + int c = getc(fp); + if (c == EOF) + return c; + ungetc(c, fp); + return c; +} + +#define fnextc getc /* advance to the next character */ + +/* NOTE: Advances and returns # of characters advanced iff prefix is found. */ +static int fhasprefix(FILE *fp, const char *prefix) { + assert(*prefix); + long start = ftell(fp); + if (start == -1) + return 0; + const char *p = prefix; + while (*p) { + int c = getc(fp); + if (c != *p) { + /* wrong character / EOF */ + fseek(fp, start, SEEK_SET); + return 0; + } + p++; + } + return (int)(p - prefix); /* length of prefix */ +} |