Re-worked preproc rules
authorLukas Krickl <lukas@krickl.dev>
Mon, 6 Nov 2023 19:36:57 +0000 (20:36 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 6 Nov 2023 19:36:57 +0000 (20:36 +0100)
include/ulas.h
src/preproc.c
src/test.c
src/ulas.c

index 1f6eb37c918b22ef47538e20044bb9749e64c5bc..1864a80e8e422b6796140b567c37be8e8a1033fe 100644 (file)
@@ -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
index 7fe00dbcc2d1f3dd9ee92326f8d021e7bd9573cf..b1102f0b3a1db5a4fd4365822d1438d5f9b392ea 100644 (file)
@@ -5,8 +5,6 @@
 #include <stdio.h>
 #include <assert.h>
 
-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:
 
index a9f39ef58a792b06fcc15ed98494422027bfc34b..a5a627c04ef16f53f7a97484bc27be6223088df9 100644 (file)
@@ -1,4 +1,5 @@
 #include "ulas.h"
+#include <ctype.h>
 #include <stdio.h>
 #include <assert.h>
 #include "preproc.h"
 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");
index 3a72d1abbc873c6db27451b23b8cf8b0452fd0f1..a6576cac1bd74b52001e9fbdd9e08005201839b2 100644 (file)
@@ -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;