failing scanner

This commit is contained in:
2025-12-16 21:41:49 +01:00
parent 4e15e8eb7e
commit 9c329eaaaa
4 changed files with 60 additions and 1 deletions
+2
View File
@@ -324,6 +324,8 @@ bool scan_f32(scanner_t *s);
bool scan_identifier(scanner_t *s);
bool scan_string_literal(scanner_t *s);
bool scanner_print_errors(scanner_t *s, FILE *fp);
bool looks_like_float(scanner_t *s);
bool looks_like_int(scanner_t *s);
// string builder //////////////////////////////////////////////////////////////
typedef struct string_builder_node {
+18
View File
@@ -332,3 +332,21 @@ bool scanner_print_errors(scanner_t *s, FILE *fp) {
}
return s->errors != NULL;
}
bool looks_like_float(scanner_t *s) {
const char *cur = s->cur;
if (*cur == '+' || *cur == '-') cur++;
if (!isdigit((unsigned char)*(cur++))) return false;
while (isdigit((unsigned char)*(cur++)));
if (*cur != '.') return false;
cur++;
return isdigit((unsigned char)*cur);
}
bool looks_like_int(scanner_t *s) {
const char *cur = s->cur;
if (*cur == '+' || *cur == '-') {
cur++;
}
return isdigit(*cur);
}
-1
View File
@@ -1,5 +1,4 @@
#include <criterion/criterion.h>
#include <criterion/internal/assert.h>
#include <string.h>
#include "cig.h"
+40
View File
@@ -0,0 +1,40 @@
#include <assert.h>
#include <criterion/criterion.h>
#include "cig.h"
Test(scanner, looks_like_float) {
const char *buffer =
"1.0 true\n"
"12.34 true\n"
"0.5 true\n"
"9.99 true\n"
"+1.2 true\n"
"-3.14 true\n"
"1 false\n"
"42 false\n"
"+7 false\n"
"1. false\n"
".5 false\n"
"+. false\n"
"abc false\n"
"+x false\n";
with_borrow(allocator) {
scanner_t scanner = make_scanner("test", buffer, allocator);
while (!scan_eof(&scanner)) {
bool actual = looks_like_float(&scanner);
while (*scanner.cur++ != ' ');
bool expect;
if (scan_literal(&scanner, "true")) {
expect = true;
} else if (scan_literal(&scanner, "false")) {
expect = false;
} else {
assert(false && "invalid expectation");
}
scan_whitespace(&scanner);
cr_assert_eq(actual, expect);
}
}
}