* 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
#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);
// 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...
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:
#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");
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;