package-type imports

This commit is contained in:
2026-06-10 00:08:33 +02:00
parent 087fdb45d5
commit d6e03b6f08
104 changed files with 1799 additions and 273 deletions
+39 -4
View File
@@ -15,6 +15,7 @@ keyword_kind :: proc(text: string) -> token.Kind {
switch text {
case "c": return .Keyword_C
case "func": return .Keyword_Func
case "import": return .Keyword_Import
case "return": return .Keyword_Return
case "void": return .Keyword_Void
case "int": return .Keyword_Int
@@ -36,7 +37,7 @@ append_token :: proc(
) {
append(&stream.items, token.Token{
kind=kind,
span=source.Span{start=start, end=end},
span=source.Span{file=source_file.id, start=start, end=end},
text=source_file.text[start:end],
diagnostic=diagnostic,
})
@@ -71,7 +72,7 @@ lex :: proc(
cursor += 1
append_token(&stream, source_file, .Colon_Colon, start, cursor)
} else {
id := source.add(diagnostics, source.Span{start=start, end=cursor}, "expected a second ':'")
id := source.add(diagnostics, source.Span{file=source_file.id, start=start, end=cursor}, "expected a second ':'")
append_token(&stream, source_file, .Invalid, start, cursor, id)
}
case '=':
@@ -80,6 +81,9 @@ lex :: proc(
case '+':
append_token(&stream, source_file, .Plus, cursor, cursor+1)
cursor += 1
case '.':
append_token(&stream, source_file, .Dot, cursor, cursor+1)
cursor += 1
case '(':
append_token(&stream, source_file, .Left_Paren, cursor, cursor+1)
cursor += 1
@@ -95,10 +99,41 @@ lex :: proc(
case ',':
append_token(&stream, source_file, .Comma, cursor, cursor+1)
cursor += 1
case '"':
start := cursor
cursor += 1
valid := true
for cursor < len(bytes) && bytes[cursor] != '"' && bytes[cursor] != '\n' {
if bytes[cursor] == '\\' {
cursor += 1
if cursor >= len(bytes) || (bytes[cursor] != '\\' && bytes[cursor] != '"') {
source.add(
diagnostics,
source.Span{file=source_file.id, start=max(cursor-1, start), end=min(cursor+1, len(bytes))},
"import strings only support '\\\\' and '\\\"' escapes",
)
valid = false
}
}
if cursor < len(bytes) && bytes[cursor] != '\n' {
cursor += 1
}
}
if cursor < len(bytes) && bytes[cursor] == '"' {
cursor += 1
append_token(&stream, source_file, .String if valid else .Invalid, start, cursor)
} else {
id := source.add(
diagnostics,
source.Span{file=source_file.id, start=start, end=cursor},
"unterminated import string",
)
append_token(&stream, source_file, .Invalid, start, cursor, id)
}
case ';':
id := source.add(
diagnostics,
source.Span{start=cursor, end=cursor+1},
source.Span{file=source_file.id, start=cursor, end=cursor+1},
"semicolons are invalid; terminate statements with a newline",
)
append_token(&stream, source_file, .Invalid, cursor, cursor+1, id)
@@ -120,7 +155,7 @@ lex :: proc(
} else {
id := source.addf(
diagnostics,
source.Span{start=cursor, end=cursor+1},
source.Span{file=source_file.id, start=cursor, end=cursor+1},
"invalid source byte 0x%02x",
value,
)