free(tok.lit.val.strv); \
}
+#define ASSERT_SYMBOL_TOTOK(expected_val, expected_rc, token) \
+ { \
+ int rc = 0; \
+ struct ulas_tok tok = ulas_totok((token), strlen(token), &rc); \
+ assert((expected_rc) == rc); \
+ assert(tok.type == ULAS_TOKSYMBOL); \
+ assert(tok.lit.type == ULAS_STR); \
+ assert(strcmp((expected_val), tok.lit.val.strv) == 0); \
+ free(tok.lit.val.strv); \
+ }
+
+#define ASSERT_UNEXPECTED_TOTOK(expected_rc, token) \
+ { \
+ int rc = 0; \
+ ulas_totok((token), strlen(token), &rc); \
+ assert((expected_rc) == rc); \
+ }
+
+
void test_totok(void) {
TESTBEGIN("totok");
// unterminated string
ASSERT_STR_TOTOK("test\n\"123\"", -1, "\"test\\n\\\"123\\\"");
+ // symbols
+ ASSERT_SYMBOL_TOTOK("_symbol123", 0, "_symbol123");
+ ASSERT_SYMBOL_TOTOK("symbol123", 0, "symbol123");
+
+ ASSERT_UNEXPECTED_TOTOK(-1, "1symbol123");
+
TESTEND("totok");
}
}
buf++;
break;
- } else if (ulas_isname(buf, n)) {
- // literal. we can resolve it now
- // because literals need to be able to be resolved
- // for every line, unless they are a label!
- // TODO: read and unescape striing between " and "
+ } else if (ulas_isname(buf - 1, n)) {
+ // literal token
+ // we resolve it later, will need to malloc here for now
tok.type = ULAS_TOKSYMBOL;
tok.lit.type = ULAS_STR;
+ tok.lit.val.strv = strndup(buf - 1, n);
+ buf += n - 1;
} else {
ULASERR("Unexpected token: %s\n", buf);
*rc = -1;