From 26f5fed3219ed90e14778dc31be74e6a72cc6538 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sun, 17 Nov 2024 08:08:57 -0500 Subject: [PATCH] chore: small changes --- CHANGELOG.md | 3 ++- build.zig.zon | 4 ++-- src/01_lexic/number.zig | 25 ++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae5136..99ae05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,8 @@ Now in Zig! ## v0.0.1 -- [ ] +- [x] Lex integers & floating point numbers +- [ ] diff --git a/build.zig.zon b/build.zig.zon index c1fc163..a002ff1 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,12 +10,12 @@ // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. - .version = "0.0.0", + .version = "0.0.1", // This field is optional. // This is currently advisory only; Zig does not yet do anything // with this value. - //.minimum_zig_version = "0.11.0", + .minimum_zig_version = "0.13.0", // This field is optional. // Each dependency must either provide a `url` and `hash`, or a `path`. diff --git a/src/01_lexic/number.zig b/src/01_lexic/number.zig index b79a018..9904828 100644 --- a/src/01_lexic/number.zig +++ b/src/01_lexic/number.zig @@ -172,12 +172,18 @@ fn scientific(input: []const u8, cap: usize, token_start: usize, exp_pos: usize) return LexError.IncompleteScientificNumber; } current_pos += 1; + const digits_start = current_pos; - // lex digits + // lex at least 1 digit while (current_pos < cap and utils.is_decimal_digit(input[current_pos])) { current_pos += 1; } + // if there is no difference, no extra digits were lexed. + if (digits_start == current_pos) { + return LexError.IncompleteScientificNumber; + } + // return the scientific number return .{ Token.init(input[token_start..current_pos], TokenType.Float, token_start), @@ -455,6 +461,23 @@ test "should fail on incomplete scientific number" { try std.testing.expect(false); } +test "should fail on incomplete scientific number 2" { + const input = "123e+"; + const result = lex(input, input.len, 0) catch |err| { + try std.testing.expect(err == token.LexError.IncompleteScientificNumber); + return; + }; + + if (result) |tuple| { + const r = tuple[0]; + std.debug.print("{s}\n", .{r.value}); + } else { + std.debug.print("nil returned", .{}); + } + + try std.testing.expect(false); +} + test "should lex floating scientific number" { const input = "0.58e+3"; const result = try lex(input, input.len, 0);