From e50ce195dc137eb0f672243f8d247a85f16b51e5 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 6 Nov 2023 20:36:57 +0100 Subject: [PATCH] Re-worked preproc rules --- include/ulas.h | 4 +--- src/preproc.c | 13 +++++++++---- src/test.c | 13 +++++++------ src/ulas.c | 2 -- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index 1f6eb37..1864a80 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -163,10 +163,8 @@ char *ulas_strndup(const char *src, size_t n); * A token rule returns true when a token should end * otherwise returns false */ -typedef bool (*ulas_tokrule)(char current); +typedef int (*ulas_tokrule)(int current); -// simple tokenizer at any space char -bool ulas_tokrulespace(char current); // tokenisze according to pre-defined rules // returns the amount of bytes of line that were diff --git a/src/preproc.c b/src/preproc.c index 7fe00db..b1102f0 100644 --- a/src/preproc.c +++ b/src/preproc.c @@ -5,8 +5,6 @@ #include #include -bool ulas_tokpreproc(char c) { return !isalnum(c) && c != '#'; } - char *ulas_preprocexpand(char *line, size_t linemax, const char *raw_line, size_t *n) { assert(*n <= linemax); @@ -18,7 +16,7 @@ char *ulas_preprocexpand(char *line, size_t linemax, const char *raw_line, // if so expand it // only expand macros if they match toks[0] though! // otherwise memcpy the read bytes 1:1 into the new string - while (ulas_tokline(tok, &praw_line, ULAS_TOKMAX, ulas_tokpreproc)) { + while (ulas_tokline(tok, &praw_line, ULAS_TOKMAX, isalnum)) { } // TODO: actually expand here... @@ -49,13 +47,20 @@ int ulas_preprocline(struct ulas_preproc *pp, FILE *dst, const char *raw_line, char tok[ULAS_TOKMAX]; // check if the first token is any of the valid preproc directives - if (ulas_tokline(tok, &pline, ULAS_TOKMAX, ulas_tokpreproc)) { + if (ulas_tokline(tok, &pline, ULAS_TOKMAX, isspace)) { + // not a preproc directive... + if (tok[0] != ULAS_TOK_PREPROC_BEGIN) { + goto found; + } for (size_t i = 0; dirstrs[i]; i++) { if (strncmp(dirstrs[i], tok, ULAS_TOKMAX) == 0) { found_dir = dirs[i]; goto found; } } + + ULASERR("Unknown preprocessor directive: %s\n", line); + return -1; } found: diff --git a/src/test.c b/src/test.c index a9f39ef..a5a627c 100644 --- a/src/test.c +++ b/src/test.c @@ -1,4 +1,5 @@ #include "ulas.h" +#include #include #include #include "preproc.h" @@ -32,13 +33,13 @@ void test_tok(void) { TESTBEGIN("tok"); - assert_tok("test", 4, "test tokens", ulas_tokrulespace); - assert_tok("test", 6, " test tokens", ulas_tokrulespace); - assert_tok("tokens", 6, "tokens", ulas_tokrulespace); - assert_tok("", 0, "", ulas_tokrulespace); - assert_tok("", -1, NULL, ulas_tokrulespace); + assert_tok("test", 4, "test tokens", isspace); + assert_tok("test", 6, " test tokens", isspace); + assert_tok("tokens", 6, "tokens", isspace); + assert_tok("", 0, "", isspace); + assert_tok("", -1, NULL, isspace); - assert_tokline(4, " test tokens with line", ulas_tokrulespace, + assert_tokline(4, " test tokens with line", isspace, {"test", "tokens", "with", "line"}); TESTEND("tok"); diff --git a/src/ulas.c b/src/ulas.c index 3a72d1a..a6576ca 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -49,8 +49,6 @@ int ulas_main(struct ulas_config cfg) { return 0; } -bool ulas_tokrulespace(char current) { return isspace(current); } - int ulas_tok(char *dst, const char *line, size_t n, ulas_tokrule rule) { if (!dst || !line || n == 0) { return -1; -- 2.30.2