diff --git a/src/01_lexic/root.zig b/src/01_lexic/root.zig index b3c026c..22fd9eb 100644 --- a/src/01_lexic/root.zig +++ b/src/01_lexic/root.zig @@ -25,6 +25,11 @@ pub fn tokenize(input: []const u8, alloc: std.mem.Allocator) !std.ArrayList(Toke const actual_next_pos = ignore_whitespace(input, current_pos); assert(current_pos <= actual_next_pos); + // if after processing whitespace we reach eof, exit + if (actual_next_pos == input_len) { + break; + } + // attempt to lex a number if (try number.lex(input, input_len, actual_next_pos)) |tuple| { assert(tuple[1] > current_pos); diff --git a/src/02_syntax/variable.zig b/src/02_syntax/variable.zig index d6478fa..09180ae 100644 --- a/src/02_syntax/variable.zig +++ b/src/02_syntax/variable.zig @@ -14,6 +14,7 @@ const VariableBinding = struct { expression: *expression.Expression, alloc: std.mem.Allocator, + /// Parses a variable binding fn init(target: *VariableBinding, tokens: *const TokenStream, pos: usize, allocator: std.mem.Allocator) ParseError!void { std.debug.assert(pos < tokens.items.len); @@ -57,7 +58,7 @@ const VariableBinding = struct { }; } - fn deinit(self: *@This()) void { + fn deinit(self: *VariableBinding) void { self.alloc.destroy(self.expression); } }; @@ -73,3 +74,31 @@ test "should parse a minimal var" { try std.testing.expectEqual(true, binding.is_mutable); } + +test "should fail is it doesnt start with var" { + const input = "different_token_stream()"; + const tokens = try lexic.tokenize(input, std.testing.allocator); + defer tokens.deinit(); + + var binding: VariableBinding = undefined; + binding.init(&tokens, 0, std.testing.allocator) catch |err| { + try std.testing.expectEqual(ParseError.Unmatched, err); + return; + }; + + try std.testing.expect(false); +} + +test "should fail if the idenfier is missing" { + const input = "var "; + const tokens = try lexic.tokenize(input, std.testing.allocator); + defer tokens.deinit(); + + var binding: VariableBinding = undefined; + binding.init(&tokens, 0, std.testing.allocator) catch |err| { + try std.testing.expectEqual(ParseError.Error, err); + return; + }; + + try std.testing.expect(false); +}