fix: dont throw an error on a single 0
This commit is contained in:
parent
d89918795e
commit
db8756a1d5
@ -1,11 +1,10 @@
|
|||||||
# Typed Hypertext Preprocessor
|
# Typed Hypertext Preprocessor
|
||||||
|
|
||||||
The latest rewrite of the THP programming language.
|
The latest & greatest rewrite of the THP programming language.
|
||||||
Now in Zig!
|
Now in Zig!
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- [ ] Rewrite lexer
|
|
||||||
- [ ] Rewrite parser
|
- [ ] Rewrite parser
|
||||||
- [ ] Rewrite semantic analyzer
|
- [ ] Rewrite semantic analyzer
|
||||||
- [ ] Rewrite type checker
|
- [ ] Rewrite type checker
|
||||||
@ -37,4 +36,5 @@ Now in Zig!
|
|||||||
- [x] Parse minimal module
|
- [x] Parse minimal module
|
||||||
- [x] Recover errors & generate error messages for the lexer
|
- [x] Recover errors & generate error messages for the lexer
|
||||||
- [x] Serialize lex errors/tokens into JSON
|
- [x] Serialize lex errors/tokens into JSON
|
||||||
|
- [x] Rewrite lexer
|
||||||
|
|
||||||
|
@ -123,8 +123,10 @@ fn integer(
|
|||||||
|
|
||||||
// if we hit eof, return the current integer
|
// if we hit eof, return the current integer
|
||||||
if (last_pos >= cap) {
|
if (last_pos >= cap) {
|
||||||
// leading zero on an integer, throw an error
|
// if there is a leading zero, two possibilities:
|
||||||
if (first_char == '0') {
|
// - 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.init("Leading zero", start, start + 1, alloc);
|
||||||
try err.add_label("This decimal number has a leading zero.", start, last_pos);
|
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");
|
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" {
|
test "should return null if not an integer" {
|
||||||
const input = "prosor prosor";
|
const input = "prosor prosor";
|
||||||
const result = try integer(input, input.len, 0, undefined, std.heap.page_allocator);
|
const result = try integer(input, input.len, 0, undefined, std.heap.page_allocator);
|
||||||
|
Loading…
Reference in New Issue
Block a user