This commit is contained in:
agra
2026-02-16 01:58:30 +02:00
parent b20676375d
commit c8ceceed0f
6 changed files with 104 additions and 236 deletions

View File

@@ -50,10 +50,6 @@ pub const Lexer = struct {
return self.lexString(start);
}
// Backtick (multi-line) string literals
if (c == '`') {
return self.lexBacktickString(start);
}
// Directives: #import, #insert, #run, #builtin, #foreign, #library, #string
if (c == '#') {
@@ -272,22 +268,6 @@ pub const Lexer = struct {
return self.makeToken(.invalid, start, self.index);
}
fn lexBacktickString(self: *Lexer, start: u32) Token {
self.index += 1; // skip opening `
while (self.index < self.source.len) {
const ch = self.source[self.index];
if (ch == '`') {
self.index += 1;
return self.makeToken(.string_literal, start, self.index);
}
if (ch == '\\') {
self.index += 1; // skip escape
}
self.index += 1;
}
// Unterminated string
return self.makeToken(.invalid, start, self.index);
}
/// Lex a #string heredoc. Called after "#string" has been matched.
/// Syntax: #string DELIM\n...content...\nDELIM
@@ -491,28 +471,12 @@ test "lex string" {
try std.testing.expectEqualStrings("\"Hello\"", tok.slice("\"Hello\""));
}
test "lex backtick string" {
const source: [:0]const u8 = "`Hello`";
test "lex multiline string" {
const source: [:0]const u8 = "\"line1\nline2\nline3\"";
var lex = Lexer.init(source);
const tok = lex.next();
try std.testing.expectEqual(Tag.string_literal, tok.tag);
try std.testing.expectEqualStrings("`Hello`", tok.slice(source));
}
test "lex backtick multiline string" {
const source: [:0]const u8 = "`line1\nline2\nline3`";
var lex = Lexer.init(source);
const tok = lex.next();
try std.testing.expectEqual(Tag.string_literal, tok.tag);
try std.testing.expectEqualStrings("`line1\nline2\nline3`", tok.slice(source));
}
test "lex backtick string with escape" {
const source: [:0]const u8 = "`hello\\`world`";
var lex = Lexer.init(source);
const tok = lex.next();
try std.testing.expectEqual(Tag.string_literal, tok.tag);
try std.testing.expectEqualStrings("`hello\\`world`", tok.slice(source));
try std.testing.expectEqualStrings("\"line1\nline2\nline3\"", tok.slice(source));
}
test "lex #string heredoc" {