fix: dont throw an error on a single 0

This commit is contained in:
Fernando Araoz 2025-01-26 21:12:58 -05:00
parent d89918795e
commit db8756a1d5
2 changed files with 19 additions and 4 deletions

View File

@ -1,11 +1,10 @@
# Typed Hypertext Preprocessor
The latest rewrite of the THP programming language.
The latest & greatest rewrite of the THP programming language.
Now in Zig!
## TODO
- [ ] Rewrite lexer
- [ ] Rewrite parser
- [ ] Rewrite semantic analyzer
- [ ] Rewrite type checker
@ -37,4 +36,5 @@ Now in Zig!
- [x] Parse minimal module
- [x] Recover errors & generate error messages for the lexer
- [x] Serialize lex errors/tokens into JSON
- [x] Rewrite lexer

View File

@ -123,8 +123,10 @@ fn integer(
// if we hit eof, return the current integer
if (last_pos >= cap) {
// leading zero on an integer, throw an error
if (first_char == '0') {
// if there is a leading zero, two possibilities:
// - a number with a leading zero. throw an error
// - a single zero. valid
if (first_char == '0' and last_pos > start + 1) {
try err.init("Leading zero", start, start + 1, alloc);
try err.add_label("This decimal number has a leading zero.", start, last_pos);
err.set_help("If you want an octal number use '0o', otherwise remove the leading zero");
@ -298,6 +300,19 @@ test "int lexer 3" {
}
}
test "int lexer 4: should lex a single zero" {
const input = "0";
var err: errors.ErrorData = undefined;
const result = try integer(input, input.len, 0, &err, std.heap.page_allocator);
if (result) |tuple| {
const r = tuple[0];
try std.testing.expectEqualStrings("0", r.value);
} else {
try std.testing.expect(false);
}
}
test "should return null if not an integer" {
const input = "prosor prosor";
const result = try integer(input, input.len, 0, undefined, std.heap.page_allocator);