diff --git a/crafting-interpreters/zlox/src/tokenizer.zig b/crafting-interpreters/zlox/src/tokenizer.zig index 5664d66..88626a6 100644 --- a/crafting-interpreters/zlox/src/tokenizer.zig +++ b/crafting-interpreters/zlox/src/tokenizer.zig @@ -312,6 +312,56 @@ fn expectTokenTypes(source: []const u8, expected: []const TokenType) !void { try std.testing.expectEqual(TokenType.eof, tokens[tokens.len - 1].type); } +fn expectTokenLines(source: []const u8, expected_lines: []const usize) !void { + const gpa = std.testing.allocator; + const tokens = try scanForTest(gpa, source); + defer gpa.free(tokens); + + // The last entry of `expected_lines` is the line of the eof token. + try std.testing.expectEqual(expected_lines.len, tokens.len); + for (expected_lines, tokens) |expected_line, token| { + try std.testing.expectEqual(expected_line, token.line); + } +} + +/// Expects `source` to be exactly one string token whose literal is `expected` and points into `source`. +fn expectString(source: []const u8, expected: []const u8) !void { + const gpa = std.testing.allocator; + const tokens = try scanForTest(gpa, source); + defer gpa.free(tokens); + + try std.testing.expectEqual(2, tokens.len); + try std.testing.expectEqual(TokenType.string, tokens[0].type); + // The lexeme keeps the quotes, the literal value drops them. + try std.testing.expectEqualStrings(source, tokens[0].lexeme); + try std.testing.expectEqualStrings(expected, tokens[0].literal.?.string); + try std.testing.expectEqual(source.ptr + 1, tokens[0].literal.?.string.ptr); +} + +/// Expects `source` to be exactly one number token whose lexeme is all of `source`. +fn expectNumber(source: []const u8, expected: f64) !void { + const gpa = std.testing.allocator; + const tokens = try scanForTest(gpa, source); + defer gpa.free(tokens); + + try std.testing.expectEqual(2, tokens.len); + try std.testing.expectEqual(TokenType.number, tokens[0].type); + try std.testing.expectEqualStrings(source, tokens[0].lexeme); + try std.testing.expectEqual(expected, tokens[0].literal.?.number); +} + +/// Expects the formatted token at `index` to equal `expected`. +fn expectFormat(source: []const u8, index: usize, expected: []const u8) !void { + const gpa = std.testing.allocator; + const tokens = try scanForTest(gpa, source); + defer gpa.free(tokens); + + var buffer: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buffer); + try writer.print("{f}", .{tokens[index]}); + try std.testing.expectEqualStrings(expected, writer.buffered()); +} + test "empty source produces only eof" { const gpa = std.testing.allocator; const tokens = try scanForTest(gpa, ""); @@ -331,25 +381,10 @@ test "single-character tokens" { }); } -test "one-or-two-character tokens pick the longer form when possible" { - try expectTokenTypes("!=", &.{.bang_equal}); - try expectTokenTypes("==", &.{.equal_equal}); - try expectTokenTypes(">=", &.{.greater_equal}); - try expectTokenTypes("<=", &.{.less_equal}); +test "operators pick the longer form when possible, matching greedily from the left" { + try expectTokenTypes("!= == >= <=", &.{ .bang_equal, .equal_equal, .greater_equal, .less_equal }); + try expectTokenTypes("! = > <", &.{ .bang, .equal, .greater, .less }); - try expectTokenTypes("!", &.{.bang}); - try expectTokenTypes("=", &.{.equal}); - try expectTokenTypes(">", &.{.greater}); - try expectTokenTypes("<", &.{.less}); -} - -test "operator at end of input does not read out of bounds" { - // `match` must check `isAtEnd` before indexing; these end right after the first character. - try expectTokenTypes("(!", &.{ .left_paren, .bang }); - try expectTokenTypes("(<", &.{ .left_paren, .less }); -} - -test "two-character operators are matched greedily, left to right" { // `===` is `==` then `=`, not `=` then `==`. try expectTokenTypes("===", &.{ .equal_equal, .equal }); try expectTokenTypes("!==", &.{ .bang_equal, .equal }); @@ -359,297 +394,130 @@ test "two-character operators are matched greedily, left to right" { try expectTokenTypes("!!", &.{ .bang, .bang }); } -test "lexemes are slices of the source" { - const gpa = std.testing.allocator; - const source = "(!=,<)"; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - const expected = [_][]const u8{ "(", "!=", ",", "<", ")", "" }; - try std.testing.expectEqual(expected.len, tokens.len); - for (expected, tokens) |lexeme, token| { - try std.testing.expectEqualStrings(lexeme, token.lexeme); - } - // The first lexeme must point into the source, not into a copy. - try std.testing.expectEqual(source.ptr, tokens[0].lexeme.ptr); -} - -test "every token on a single line reports line 1" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "(){}"); - defer gpa.free(tokens); - - for (tokens) |token| { - try std.testing.expectEqual(1, token.line); - } -} - -test "token format" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "+"); - defer gpa.free(tokens); - - var buffer: [64]u8 = undefined; - var writer: std.Io.Writer = .fixed(&buffer); - try writer.print("{f}", .{tokens[tokens.len - 1]}); - try std.testing.expectEqualStrings("eof", writer.buffered()); +test "whitespace is skipped and produces no tokens" { + try expectTokenTypes(" \t\r\n ", &.{}); + try expectTokenTypes("( \t )", &.{ .left_paren, .right_paren }); + // Whitespace splits a two-character operator in two. + try expectTokenTypes("!\t=", &.{ .bang, .equal }); + try expectTokenTypes("= =", &.{ .equal, .equal }); } -test "slash is a token, a double slash is a comment" { - try expectTokenTypes("/", &.{.slash}); +test "a double slash starts a comment that runs to the end of the line and no further" { try expectTokenTypes("/ /", &.{ .slash, .slash }); try expectTokenTypes("//", &.{}); try expectTokenTypes("// (){}", &.{}); -} - -test "slash at end of input does not read out of bounds" { - try expectTokenTypes("(/", &.{ .left_paren, .slash }); -} - -test "comment runs to the end of the line and no further" { try expectTokenTypes("( // ignored ) }\n)", &.{ .left_paren, .right_paren }); try expectTokenTypes("// first\n// second\n+", &.{.plus}); // A comment that ends the file without a trailing newline. try expectTokenTypes("+ // trailing", &.{.plus}); -} - -test "comment does not swallow the token before it" { + // The comment does not swallow the token before it or the next line. try expectTokenTypes("/// still a comment\n/", &.{.slash}); try expectTokenTypes("(//x\n/)", &.{ .left_paren, .slash, .right_paren }); } -test "whitespace is skipped and produces no tokens" { - try expectTokenTypes(" \t\r\n ", &.{}); - try expectTokenTypes("( \t )", &.{ .left_paren, .right_paren }); - try expectTokenTypes("!\t=", &.{ .bang, .equal }); - try expectTokenTypes("= =", &.{ .equal, .equal }); -} - -test "lexeme of a slash excludes surrounding whitespace" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, " / "); - defer gpa.free(tokens); - - try std.testing.expectEqual(2, tokens.len); - try std.testing.expectEqualStrings("/", tokens[0].lexeme); -} - -fn expectTokenLines(source: []const u8, expected_lines: []const usize) !void { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - // The last entry of `expected_lines` is the line of the eof token. - try std.testing.expectEqual(expected_lines.len, tokens.len); - for (expected_lines, tokens) |expected_line, token| { - try std.testing.expectEqual(expected_line, token.line); - } +test "tokens at the end of input do not read out of bounds" { + // Each of these peeks or matches one character past the end. + try expectTokenTypes("(!", &.{ .left_paren, .bang }); + try expectTokenTypes("(<", &.{ .left_paren, .less }); + try expectTokenTypes("(/", &.{ .left_paren, .slash }); + try expectTokenTypes("9", &.{.number}); + try expectTokenTypes("1.5", &.{.number}); + try expectTokenTypes("(x", &.{ .left_paren, .identifier }); + try expectTokenTypes("(nil", &.{ .left_paren, .nil }); } -test "newlines advance the line counter" { +test "line numbers" { + // Newlines advance the counter, and eof reports the line after a trailing newline. + try expectTokenLines("(){}", &.{ 1, 1, 1, 1, 1 }); try expectTokenLines("(\n)\n+", &.{ 1, 2, 3, 3 }); try expectTokenLines("\n\n\n(", &.{ 4, 4 }); try expectTokenLines("(\n\n)", &.{ 1, 3, 3 }); -} - -test "eof reports the line after a trailing newline" { try expectTokenLines("(\n", &.{ 1, 2 }); try expectTokenLines("\n", &.{2}); -} - -test "comments do not eat the newline that ends them" { + // A comment does not eat the newline that ends it. try expectTokenLines("// one\n// two\n(", &.{ 3, 3 }); try expectTokenLines("( // one\n) // two\n+", &.{ 1, 2, 3, 3 }); -} - -test "carriage returns and tabs do not count as lines" { + // Carriage returns and tabs do not count as lines. try expectTokenLines("(\r\n)", &.{ 1, 2, 2 }); try expectTokenLines("(\t\r)", &.{ 1, 1, 1 }); + // A multiline string reports the line it ends on, since the token is added after the closing quote. + try expectTokenLines("\"a\nb\" +", &.{ 2, 2, 2 }); + try expectTokenLines("\"a\nb\"\n+", &.{ 2, 3, 3 }); + try expectTokenLines("1\n2.5\n3", &.{ 1, 2, 3, 3 }); + try expectTokenLines("a\nb\nreturn", &.{ 1, 2, 3, 3 }); } -test "multiline program scans with correct types and lines" { +test "lexemes are slices of the source and exclude surrounding text" { const gpa = std.testing.allocator; - const source = - \\// a tiny program - \\( ) { - \\ == / ; // end - \\} - ; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - const expected_types = [_]TokenType{ - .left_paren, .right_paren, .left_brace, .equal_equal, .slash, .semicolon, .right_brace, .eof, + const cases = [_]struct { source: []const u8, index: usize, offset: usize, lexeme: []const u8 }{ + .{ .source = "(!=,<)", .index = 1, .offset = 1, .lexeme = "!=" }, + .{ .source = " / ", .index = 0, .offset = 2, .lexeme = "/" }, + .{ .source = " 42 ", .index = 0, .offset = 2, .lexeme = "42" }, + .{ .source = " foo_bar1 ", .index = 0, .offset = 2, .lexeme = "foo_bar1" }, + .{ .source = " \"lox\" ", .index = 0, .offset = 1, .lexeme = "\"lox\"" }, }; - const expected_lines = [_]usize{ 2, 2, 2, 3, 3, 3, 4, 4 }; - try std.testing.expectEqual(expected_types.len, tokens.len); - for (expected_types, expected_lines, tokens) |expected_type, expected_line, token| { - try std.testing.expectEqual(expected_type, token.type); - try std.testing.expectEqual(expected_line, token.line); + for (cases) |case| { + const tokens = try scanForTest(gpa, case.source); + defer gpa.free(tokens); + + try std.testing.expectEqualStrings(case.lexeme, tokens[case.index].lexeme); + // The lexeme must point into the source, not into a copy. + try std.testing.expectEqual(case.source.ptr + case.offset, tokens[case.index].lexeme.ptr); } } -test "string literal token" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "\"lox\""); - defer gpa.free(tokens); - - try std.testing.expectEqual(2, tokens.len); - try std.testing.expectEqual(TokenType.string, tokens[0].type); - // The lexeme keeps the quotes, the literal value drops them. - try std.testing.expectEqualStrings("\"lox\"", tokens[0].lexeme); - try std.testing.expectEqualStrings("lox", tokens[0].literal.?.string); +test "string literals" { + try expectString("\"lox\"", "lox"); + try expectString("\"\"", ""); + // A newline stays in the value. + try expectString("\"a\nb\"", "a\nb"); } -test "empty string literal" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "\"\""); - defer gpa.free(tokens); - - try std.testing.expectEqual(2, tokens.len); - try std.testing.expectEqualStrings("\"\"", tokens[0].lexeme); - try std.testing.expectEqualStrings("", tokens[0].literal.?.string); +test "string escapes are kept as written and do not end the string early" { + // The scanner does not unescape: the literal still contains the backslashes. + try expectString("\"a\\\"b\"", "a\\\"b"); + try expectString("\"a\\\\\"", "a\\\\"); + try expectString("\"a\\nb\"", "a\\nb"); + // An escaped backslash before the closing quote must not swallow that quote. + try expectTokenTypes("\"a\\\\\" +", &.{ .string, .plus }); + try expectTokenTypes("\"a\\\"\" +", &.{ .string, .plus }); } -test "string contents are not scanned as tokens" { +test "string and comment contents are not scanned as tokens" { try expectTokenTypes("\"( // != \"", &.{.string}); + try expectTokenTypes("\"while true\"", &.{.string}); try expectTokenTypes("(\"a\"+\"b\")", &.{ .left_paren, .string, .plus, .string, .right_paren }); + try expectString("\"123\"", "123"); + try expectTokenTypes("// 123", &.{}); + try expectTokenTypes("// var x = nil;", &.{}); + try expectTokenTypes("var // class\nx", &.{ .@"var", .identifier }); } -test "string literal is a slice of the source" { - const gpa = std.testing.allocator; - const source = "\"abc\""; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - try std.testing.expectEqual(source.ptr + 1, tokens[0].literal.?.string.ptr); -} - -test "multiline string keeps its newline and advances the line counter" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "\"a\nb\" +"); - defer gpa.free(tokens); - - try std.testing.expectEqual(3, tokens.len); - try std.testing.expectEqualStrings("a\nb", tokens[0].literal.?.string); - // The token is added after the closing quote, so it reports the line it ends on. - try std.testing.expectEqual(2, tokens[0].line); - try std.testing.expectEqual(2, tokens[1].line); - try expectTokenLines("\"a\nb\"\n+", &.{ 2, 3, 3 }); -} - -test "only string tokens carry a literal" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "(\"x\")"); - defer gpa.free(tokens); - - try std.testing.expectEqual(null, tokens[0].literal); - try std.testing.expect(tokens[1].literal != null); - try std.testing.expectEqual(null, tokens[2].literal); - try std.testing.expectEqual(null, tokens[3].literal); -} - -test "token format with and without literal" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "+ \"hi\""); - defer gpa.free(tokens); - - var buffer: [128]u8 = undefined; - var writer: std.Io.Writer = .fixed(&buffer); - try writer.print("{f}", .{tokens[0]}); - try std.testing.expectEqualStrings(".plus +", writer.buffered()); - - writer = .fixed(&buffer); - try writer.print("{f}", .{tokens[1]}); - try std.testing.expectEqualStrings(".string \"hi\" .{ .string = \"hi\" }", writer.buffered()); -} - -test "escaped quote does not end the string" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "\"a\\\"b\" +"); - defer gpa.free(tokens); - - try std.testing.expectEqual(3, tokens.len); - try std.testing.expectEqual(TokenType.string, tokens[0].type); - try std.testing.expectEqual(TokenType.plus, tokens[1].type); - // The scanner does not unescape: the literal still contains the backslash. - try std.testing.expectEqualStrings("a\\\"b", tokens[0].literal.?.string); -} - -test "escaped backslash before the closing quote ends the string" { - const gpa = std.testing.allocator; - // Source is `"a\\" +`: the `\\` is one escaped backslash, so the second quote closes the string. - const tokens = try scanForTest(gpa, "\"a\\\\\" +"); - defer gpa.free(tokens); - - try std.testing.expectEqual(3, tokens.len); - try std.testing.expectEqualStrings("a\\\\", tokens[0].literal.?.string); - try std.testing.expectEqual(TokenType.plus, tokens[1].type); -} - -test "backslash before an ordinary character is kept as is" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "\"a\\nb\""); - defer gpa.free(tokens); - - try std.testing.expectEqual(2, tokens.len); - try std.testing.expectEqualStrings("a\\nb", tokens[0].literal.?.string); -} - -fn expectNumber(source: []const u8, expected: f64) !void { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - try std.testing.expectEqual(2, tokens.len); - try std.testing.expectEqual(TokenType.number, tokens[0].type); - try std.testing.expectEqualStrings(source, tokens[0].lexeme); - try std.testing.expectEqual(expected, tokens[0].literal.?.number); -} - -test "integer literals" { +test "number literals" { try expectNumber("0", 0); try expectNumber("7", 7); try expectNumber("1234", 1234); -} - -test "fractional literals" { try expectNumber("12.34", 12.34); try expectNumber("0.5", 0.5); try expectNumber("3.0", 3.0); -} - -test "leading zeros are accepted" { + // Leading zeros are accepted. try expectNumber("007", 7); try expectNumber("00.5", 0.5); } -test "number at end of input does not read out of bounds" { - try expectNumber("9", 9); - try expectNumber("1.5", 1.5); -} - -test "trailing dot is not part of the number" { +test "dots next to numbers only join a number when a digit follows" { // The book's rule: a fractional part needs a digit after the dot. try expectTokenTypes("123.", &.{ .number, .dot }); try expectTokenTypes("123.;", &.{ .number, .dot, .semicolon }); -} - -test "leading dot is not part of the number" { try expectTokenTypes(".5", &.{ .dot, .number }); -} - -test "a second dot ends the number" { - // `1.2.3` is a number `1.2`, a dot, and a number `3`. + // A second dot ends the number: `1.2`, a dot, and `3`. try expectTokenTypes("1.2.3", &.{ .number, .dot, .number }); } -test "minus is a separate token from the number" { +test "numbers next to other tokens" { + // Minus is a separate token, never part of the number. try expectTokenTypes("-1", &.{ .minus, .number }); try expectTokenTypes("1-2", &.{ .number, .minus, .number }); -} - -test "numbers next to other tokens" { try expectTokenTypes("(1+2.5)*3", &.{ .left_paren, .number, .plus, .number, .right_paren, .star, .number, }); @@ -657,45 +525,12 @@ test "numbers next to other tokens" { try expectTokenTypes("1 2", &.{ .number, .number }); } -test "number lexemes exclude surrounding text and are source slices" { - const gpa = std.testing.allocator; - const source = " 42 "; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - try std.testing.expectEqualStrings("42", tokens[0].lexeme); - try std.testing.expectEqual(source.ptr + 2, tokens[0].lexeme.ptr); -} - -test "number inside a string or comment is not scanned" { - try expectTokenTypes("\"123\"", &.{.string}); - try expectTokenTypes("// 123", &.{}); - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "\"123\""); - defer gpa.free(tokens); - try std.testing.expectEqualStrings("123", tokens[0].literal.?.string); -} - -test "number token reports its line" { - try expectTokenLines("1\n2.5\n3", &.{ 1, 2, 3, 3 }); -} - -test "number token format" { - const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "12.5"); - defer gpa.free(tokens); - - var buffer: [128]u8 = undefined; - var writer: std.Io.Writer = .fixed(&buffer); - try writer.print("{f}", .{tokens[0]}); - try std.testing.expectEqualStrings(".number 12.5 .{ .number = 12.5 }", writer.buffered()); -} - test "number too large for f64 becomes infinity instead of an error" { const gpa = std.testing.allocator; const huge: [400]u8 = @splat('9'); const tokens = try scanForTest(gpa, &huge); defer gpa.free(tokens); + try std.testing.expectEqual(2, tokens.len); try std.testing.expect(std.math.isInf(tokens[0].literal.?.number)); } @@ -726,58 +561,28 @@ test "keyword table covers exactly the keyword token types" { } test "identifiers" { - try expectTokenTypes("x", &.{.identifier}); - try expectTokenTypes("_", &.{.identifier}); - try expectTokenTypes("_private", &.{.identifier}); - try expectTokenTypes("camelCase", &.{.identifier}); - try expectTokenTypes("snake_case_2", &.{.identifier}); - try expectTokenTypes("X9", &.{.identifier}); -} - -test "identifier lexeme is the whole name and has no literal" { - const gpa = std.testing.allocator; - const source = " foo_bar1 "; - const tokens = try scanForTest(gpa, source); - defer gpa.free(tokens); - - try std.testing.expectEqual(2, tokens.len); - try std.testing.expectEqualStrings("foo_bar1", tokens[0].lexeme); - try std.testing.expectEqual(source.ptr + 2, tokens[0].lexeme.ptr); - try std.testing.expectEqual(null, tokens[0].literal); -} - -test "identifiers that merely start with a keyword are identifiers" { - try expectTokenTypes("android", &.{.identifier}); - try expectTokenTypes("classy", &.{.identifier}); - try expectTokenTypes("orchid", &.{.identifier}); - try expectTokenTypes("format", &.{.identifier}); - try expectTokenTypes("nil_", &.{.identifier}); - try expectTokenTypes("true1", &.{.identifier}); - try expectTokenTypes("_while", &.{.identifier}); -} - -test "identifiers that end in or contain a keyword are identifiers" { - try expectTokenTypes("thisis", &.{.identifier}); - try expectTokenTypes("mywhile", &.{.identifier}); - try expectTokenTypes("superclass", &.{.identifier}); -} - -test "keywords are case sensitive" { - try expectTokenTypes("And", &.{.identifier}); - try expectTokenTypes("CLASS", &.{.identifier}); - try expectTokenTypes("True", &.{.identifier}); - try expectTokenTypes("Nil", &.{.identifier}); + for ([_][]const u8{ "x", "_", "_private", "camelCase", "snake_case_2", "X9", "a1" }) |name| { + try expectTokenTypes(name, &.{.identifier}); + } } -test "keyword prefixes shorter than the keyword are identifiers" { - try expectTokenTypes("fu", &.{.identifier}); - try expectTokenTypes("whil", &.{.identifier}); - try expectTokenTypes("i", &.{.identifier}); - try expectTokenTypes("o", &.{.identifier}); +test "words that merely resemble a keyword are identifiers" { + const words = [_][]const u8{ + // Start with a keyword. + "android", "classy", "orchid", "format", "nil_", "true1", "_while", + // End with or contain a keyword. + "thisis", "mywhile", "superclass", + // Keywords are case sensitive. + "And", "CLASS", "True", "Nil", + // Shorter than a keyword. + "fu", "whil", "i", "o", + }; + for (words) |word| { + try expectTokenTypes(word, &.{.identifier}); + } } test "a digit ends a number but continues an identifier" { - try expectTokenTypes("a1", &.{.identifier}); try expectTokenTypes("1a", &.{ .number, .identifier }); try expectTokenTypes("12abc", &.{ .number, .identifier }); try expectTokenTypes("1.5x", &.{ .number, .identifier }); @@ -789,44 +594,35 @@ test "identifiers and keywords next to other tokens" { try expectTokenTypes("f(x,y)", &.{ .identifier, .left_paren, .identifier, .comma, .identifier, .right_paren }); try expectTokenTypes("a.b", &.{ .identifier, .dot, .identifier }); try expectTokenTypes("a/b", &.{ .identifier, .slash, .identifier }); - try expectTokenTypes("this.x", &.{ .this, .dot, .identifier }); try expectTokenTypes("a-b", &.{ .identifier, .minus, .identifier }); -} - -test "adjacent words are separate tokens" { + try expectTokenTypes("this.x", &.{ .this, .dot, .identifier }); + try expectTokenTypes("while(true)", &.{ .@"while", .left_paren, .true, .right_paren }); + // Adjacent words are separate tokens. try expectTokenTypes("if else", &.{ .@"if", .@"else" }); try expectTokenTypes("a\tb\nc", &.{ .identifier, .identifier, .identifier }); - try expectTokenTypes("while(true)", &.{ .@"while", .left_paren, .true, .right_paren }); } -test "keywords inside strings and comments are not scanned" { - try expectTokenTypes("\"while true\"", &.{.string}); - try expectTokenTypes("// var x = nil;", &.{}); - try expectTokenTypes("var // class\nx", &.{ .@"var", .identifier }); -} - -test "identifier at end of input does not read out of bounds" { - try expectTokenTypes("(x", &.{ .left_paren, .identifier }); - try expectTokenTypes("(nil", &.{ .left_paren, .nil }); -} - -test "identifier tokens report their line" { - try expectTokenLines("a\nb\nreturn", &.{ 1, 2, 3, 3 }); -} - -test "identifier token format" { +test "only string and number tokens carry a literal" { const gpa = std.testing.allocator; - const tokens = try scanForTest(gpa, "foo while"); + const tokens = try scanForTest(gpa, "(\"x\" 1 y)"); defer gpa.free(tokens); - var buffer: [64]u8 = undefined; - var writer: std.Io.Writer = .fixed(&buffer); - try writer.print("{f}", .{tokens[0]}); - try std.testing.expectEqualStrings(".identifier foo", writer.buffered()); + try std.testing.expectEqual(6, tokens.len); + try std.testing.expectEqual(null, tokens[0].literal); + try std.testing.expectEqualStrings("x", tokens[1].literal.?.string); + try std.testing.expectEqual(1, tokens[2].literal.?.number); + try std.testing.expectEqual(null, tokens[3].literal); + try std.testing.expectEqual(null, tokens[4].literal); + try std.testing.expectEqual(null, tokens[5].literal); +} - writer = .fixed(&buffer); - try writer.print("{f}", .{tokens[1]}); - try std.testing.expectEqualStrings(".while while", writer.buffered()); +test "token format" { + try expectFormat("", 0, "eof"); + try expectFormat("+ \"hi\"", 0, ".plus +"); + try expectFormat("+ \"hi\"", 1, ".string \"hi\" .{ .string = \"hi\" }"); + try expectFormat("12.5", 0, ".number 12.5 .{ .number = 12.5 }"); + try expectFormat("foo while", 0, ".identifier foo"); + try expectFormat("foo while", 1, ".while while"); } test "small program scans end to end" {