Files
ziran/cmd/compiler_source.zi
T

717 lines
28 KiB
Ziran

// Source scanning before declarations are parsed. All spans borrow source;
// callers own output storage. No native imports or allocation are needed.
SourceError :: enum s32 { None; Delimiter; DelimiterLine; Unterminated }
StatementKind :: enum s32 {
Unknown; BlockOpen; BlockClose; Declaration; Assignment; Expression;
If; While; For; Case; Return; Break; Continue; Defer; Unused; Unreachable; IfCase;
}
SourceRewrite :: struct {
error: SourceError
count: s64
line: s32
column: s32
marker_begin: s64
marker_end: s64
}
CommentScan :: struct { count: s64; depth: s32 }
ControlBlock :: struct {
valid: bool
head_end: s64
body_begin: s64
body_end: s64
tail_begin: s64
// The body continues on the following lines; no closer is on this line.
open: bool
}
ProcedureHeader :: struct { name: string; parameters: string; result: string }
MustUseModifier :: enum s32 { None; Required; Arguments; Duplicate; NoResult }
InliningModifier :: enum s32 { None; Inline; Cold; Arguments; Duplicate; Both }
TargetModifier :: enum s32 { None; Target; Arguments; Duplicate }
#scope_file
Byte :: (source: string, at: s64) -> u8 {
if at < 0 || at >= source.count { return 0 }
return source[at]
}
Space :: (byte: u8) -> bool { return byte == #char " " || (byte >= 9 && byte <= 13) }
Letter :: (byte: u8) -> bool {
return (byte >= #char "A" && byte <= #char "Z") || (byte >= #char "a" && byte <= #char "z") || byte == #char "_"
}
SkipSpace :: (source: string, at: s64) -> s64 {
while at < source.count && Space(source[at]) { at += 1 }
return at
}
Matches :: (source: string, at: s64, text: string) -> bool {
return at >= 0 && at <= source.count && text.count <= source.count - at &&
source[at:at + text.count] == text
}
Find :: (source: string, text: string) -> s64 {
at: s64 = 0
while at <= source.count {
if Matches(source, at, text) { return at }
at += 1
}
return -1
}
AfterQuote :: (source: string, at: s64) -> s64 {
quote := source[at]
at += 1
while at < source.count {
byte := source[at]
at += 1
if byte == #char "\\" && at < source.count { at += 1 }
else if byte == quote { break }
}
return at
}
// AfterQuote, or -1 when the literal does not close within this text.
ClosedQuote :: (source: string, at: s64) -> s64 {
quote := source[at]
at += 1
while at < source.count {
byte := source[at]
at += 1
if byte == #char "\\" && at < source.count { at += 1 }
else if byte == quote { return at }
}
return -1
}
Put :: (output: []u8, used: *s64, byte: u8) {
if <<used < output.count { output[<<used] = byte }
<<used += 1
}
PutScanned :: (output: []u8, used: *s64, byte: u8, gaps: *s64) {
Put(output, used, byte)
if byte == #char "\n" {
while <<gaps > 0 { Put(output, used, #char "\n"); <<gaps -= 1 }
}
}
PutRaw :: (output: []u8, used: *s64, byte: u8) {
escaped: u8 = 0
if byte == #char "\"" || byte == #char "\\" { escaped = byte }
else if byte == #char "\n" { escaped = #char "n" }
else if byte == #char "\r" { escaped = #char "r" }
else if byte == #char "\t" { escaped = #char "t" }
if escaped != 0 { Put(output, used, #char "\\"); Put(output, used, escaped) }
else if byte < #char " " || byte == 127 {
digits := "0123456789abcdef"
Put(output, used, #char "\\")
Put(output, used, #char "x")
Put(output, used, digits[byte >> 4])
Put(output, used, digits[byte & 15])
} else { Put(output, used, byte) }
}
#scope_export
SourceIdentifier :: (byte: u8) -> bool {
return Letter(byte) || (byte >= #char "0" && byte <= #char "9")
}
// First match outside quoted literals, optionally bounded by identifier bytes.
// Empty text has no match; a nonempty source may match an empty needle at 0.
FindUnquoted :: (source: string, needle: string, word: bool) -> s64 {
at: s64 = 0
while at < source.count {
if source[at] == #char "\"" || source[at] == #char "'" {
at = AfterQuote(source, at)
continue
}
if Matches(source, at, needle) && (!word ||
!SourceIdentifier(Byte(source, at - 1)) &&
!SourceIdentifier(Byte(source, at + needle.count))) { return at }
at += 1
}
return -1
}
StartsWord :: (source: string, word: string) -> bool {
if !Matches(source, 0, word) { return false }
byte := Byte(source, word.count)
return byte == 0 || byte == #char " " || byte == #char "\t" ||
byte == #char "(" || byte == #char "\"" || byte == #char "{"
}
IdentifierText :: (source: string, member_path: bool) -> bool {
if source.count == 0 || !Letter(source[0]) { return false }
at: s64 = 1
while at < source.count {
if SourceIdentifier(source[at]) { at += 1 }
else if member_path && source[at] == #char "." && Letter(Byte(source, at + 1)) {
at += 2
} else { return false }
}
return true
}
NonJaiControl :: (source: string, word: string) -> bool {
if !StartsWord(source, word) { return false }
at := SkipSpace(source, word.count)
byte := Byte(source, at)
if byte == #char "(" { return Find(source[at:], "{") >= 0 }
operators := ":=.[+-*/%&|^<>!"
for operator_byte: operators { if byte == operator_byte { return false } }
return byte == 0 || byte == #char ";" || byte == #char "{" || SourceIdentifier(byte)
}
LooksLikeLabel :: (source: string) -> bool {
at: s64 = 0
while SourceIdentifier(Byte(source, at)) { at += 1 }
if at == 0 || Byte(source, at) != #char ":" { return false }
at = SkipSpace(source, at + 1)
return at == source.count || source[at:] == ";"
}
ClassifyStatement :: (source: string) -> StatementKind {
if Byte(source, 0) == #char "}" { return .BlockClose }
if source == "{" { return .BlockOpen }
if Byte(source, 0) == #char "#" && StartsWord(source, "#asm") { return .BlockOpen }
if StartsWord(source, "if") || StartsWord(source, "else") { return .If }
if StartsWord(source, "while") { return .While }
if StartsWord(source, "for") { return .For }
if StartsWord(source, "case") || source == "case;" { return .Case }
if StartsWord(source, "return") || source == "return;" { return .Return }
if source == "unreachable" || source == "unreachable;" { return .Unreachable }
if StartsWord(source, "break") || source == "break;" { return .Break }
if StartsWord(source, "continue") || source == "continue;" { return .Continue }
if StartsWord(source, "defer") { return .Defer }
if StartsWord(source, "unused") { return .Unused }
colon := Find(source, ": ")
if colon < 0 { colon = Find(source, ": [") }
if colon > 0 && IdentifierText(source[0:colon], false) { return .Declaration }
// Classify foreign-style locals so the parser can reject them before
// unchecked declarations reach a native backend.
rejected: [11]string = .{"s8 ", "s16 ", "s32 ", "s64 ", "float32 ", "float64 ",
"bool ", "unsigned ", "long ", "const ", "struct "}
for prefix: rejected { if Matches(source, 0, prefix) { return .Declaration } }
at: s64 = 0
depth: s32 = 0
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at); continue }
if byte == #char "(" || byte == #char "[" || byte == #char "{" { depth += 1 }
else if byte == #char ")" || byte == #char "]" || byte == #char "}" {
if depth > 0 { depth -= 1 }
} else if depth == 0 && Matches(source, at, ":=") { return .Declaration }
else if byte == #char "=" && depth == 0 { return .Assignment }
at += 1
}
if Find(source, "(") >= 0 || Find(source, "+") >= 0 || Find(source, "-") >= 0 {
return .Expression
}
return .Unknown
}
NormalizeRecordSeparators :: (body: []u8, split_commas: bool) {
at: s64 = 0
quoted := false
nesting: s32 = 0
while at < body.count {
byte := body[at]
if quoted {
if byte == #char "\\" && at + 1 < body.count { at += 1 }
else if byte == #char "\"" { quoted = false }
} else if byte == #char "\"" { quoted = true }
else if byte == #char "[" || byte == #char "(" { nesting += 1 }
else if byte == #char "]" || byte == #char ")" { nesting -= 1 }
else if nesting == 0 && (byte == #char ";" || (split_commas && byte == #char ",")) {
body[at] = #char "\n"
}
at += 1
}
}
ContainsDirective :: (source: string, directive: string) -> bool {
at: s64 = 0
while at < source.count {
if source[at] == #char "\"" { at = AfterQuote(source, at) }
else if Matches(source, at, "//") { return false }
else if Matches(source, at, directive) &&
!SourceIdentifier(Byte(source, at + directive.count)) { return true }
else { at += 1 }
}
return false
}
// Empty output measures the required storage. A shorter buffer receives only
// its bounded prefix; count always reports the complete rewritten length.
// Raw bodies lose their physical newlines, which are restored after the next
// code newline so declaration spans after the delimiter stay in place.
RewriteRawStrings :: (source: string, output: []u8, marker_limit: s64) -> SourceRewrite {
result: SourceRewrite
at: s64 = 0
used: s64 = 0
gaps: s64 = 0
line: s32 = 1
column: s32 = 1
quote: u8 = 0
line_comment := false
comment_depth: s32 = 0
while at < source.count {
byte := source[at]
if !line_comment && quote == 0 &&
(Matches(source, at, "/*") || (comment_depth > 0 && Matches(source, at, "*/"))) {
if byte == #char "/" { comment_depth += 1 } else { comment_depth -= 1 }
Put(output, *used, byte)
Put(output, *used, source[at + 1])
at += 2
column += 2
continue
}
if !line_comment && comment_depth == 0 && quote == 0 {
if Matches(source, at, "//") { line_comment = true }
else if byte == #char "\"" || byte == #char "'" { quote = byte }
else if Matches(source, at, "#string") &&
(at == 0 || !SourceIdentifier(source[at - 1])) &&
(at + 7 == source.count || Space(source[at + 7])) {
result.line = line
result.column = column
cursor := at + 7
while Byte(source, cursor) == #char " " || Byte(source, cursor) == #char "\t" { cursor += 1 }
begin := cursor
while SourceIdentifier(Byte(source, cursor)) { cursor += 1 }
end := cursor
result.marker_begin = begin
result.marker_end = end
if end == begin || end - begin >= marker_limit || !Letter(Byte(source, begin)) {
result.error = .Delimiter
return result
}
while Byte(source, cursor) == #char " " || Byte(source, cursor) == #char "\t" { cursor += 1 }
if Byte(source, cursor) == #char "\r" { cursor += 1 }
if cursor >= source.count || source[cursor] != #char "\n" {
result.error = .DelimiterLine
return result
}
body := cursor + 1
close := body
marker := source[begin:end]
while close < source.count {
if Matches(source, close, marker) &&
!SourceIdentifier(Byte(source, close + marker.count)) { break }
while close < source.count && source[close] != #char "\n" { close += 1 }
if close < source.count { close += 1 }
}
if close >= source.count { result.error = .Unterminated; return result }
Put(output, *used, #char "\"")
cursor = body
while cursor < close { PutRaw(output, *used, source[cursor]); cursor += 1 }
Put(output, *used, #char "\"")
while at < close {
if source[at] == #char "\n" { line += 1; gaps += 1 }
at += 1
}
column = cast(s32)marker.count + 1
at += marker.count
continue
}
// An opening quote is copied below; it must not also close itself.
PutScanned(output, *used, byte, *gaps)
at += 1
} else if quote != 0 {
PutScanned(output, *used, byte, *gaps)
at += 1
if byte == #char "\\" && at < source.count {
column += 1
byte = source[at]
PutScanned(output, *used, byte, *gaps)
at += 1
} else if byte == quote { quote = 0 }
} else {
PutScanned(output, *used, byte, *gaps)
at += 1
}
if byte == #char "\n" { line += 1; column = 1; line_comment = false }
else { column += 1 }
}
result.count = used
return result
}
// The output may be the source's own storage: writes never pass unread bytes.
// Comment depth survives physical lines; ordinary quotes end at a newline.
StripComments :: (source: string, output: []u8, depth: s32) -> CommentScan {
at: s64 = 0
used: s64 = 0
quote: u8 = 0
while at < source.count {
byte := source[at]
if depth > 0 {
if Matches(source, at, "/*") { depth += 1; at += 2 }
else if Matches(source, at, "*/") {
depth -= 1
at += 2
if depth == 0 { Put(output, *used, #char " ") }
} else {
if byte == #char "\n" { Put(output, *used, byte) }
at += 1
}
} else if quote != 0 {
Put(output, *used, byte)
at += 1
if byte == #char "\\" && at < source.count {
Put(output, *used, source[at]); at += 1
} else if byte == quote || byte == #char "\n" { quote = 0 }
} else if byte == #char "\"" || byte == #char "'" {
quote = byte
Put(output, *used, byte)
at += 1
} else if Matches(source, at, "//") {
while at < source.count && source[at] != #char "\n" { at += 1 }
} else if Matches(source, at, "/*") { depth = 1; at += 2 }
else { Put(output, *used, byte); at += 1 }
}
return .{used, depth}
}
NetBlockBraces :: (source: string) -> s32 {
at: s64 = 0
parentheses: s32 = 0
result: s32 = 0
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at); continue }
if byte == #char "(" || byte == #char "[" { parentheses += 1 }
else if byte == #char ")" || byte == #char "]" {
if parentheses > 0 { parentheses -= 1 }
} else if parentheses == 0 {
if byte == #char "{" { result += 1 }
else if byte == #char "}" { result -= 1 }
}
at += 1
}
return result
}
StatementSeparator :: (source: string) -> s64 {
// '} }' closes two blocks: the first closer is a statement of its own.
if Byte(source, 0) == #char "}" && Byte(source, SkipSpace(source, 1)) == #char "}" { return 0 }
at: s64 = 0
parentheses: s32 = 0
brackets: s32 = 0
braces: s32 = 0
first_brace := Find(source, "{")
conditional := Find(source, "#ifx")
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at); continue }
if byte == #char "(" { parentheses += 1 }
else if byte == #char ")" { parentheses -= 1 }
else if byte == #char "[" { brackets += 1 }
else if byte == #char "]" { brackets -= 1 }
else if byte == #char "{" { braces += 1 }
else if byte == #char "}" && braces > 0 { braces -= 1 }
else if byte == #char ";" && parentheses == 0 && brackets == 0 && braces == 0 {
after := SkipSpace(source, at + 1)
if after < source.count &&
!(StartsWord(source, "for") && (first_brace < 0 || at < first_brace)) &&
!(conditional >= 0 && conditional < at && StartsWord(source[after:], "else")) { return at }
}
at += 1
}
return -1
}
SplitControlBlock :: (source: string) -> ControlBlock {
result: ControlBlock
if source.count < 4 { return result }
control: s64 = 0
if source[0] == #char "}" {
after := SkipSpace(source, 1)
if StartsWord(source[after:], "else") { control = after }
}
end := control
while end < source.count && SourceIdentifier(source[end]) && end - control < 15 { end += 1 }
word := source[control:end]
if word != "if" && word != "else" && word != "while" && word != "for" &&
word != "defer" && word != "switch" && word != "case" && word != "guard" && word != "do" &&
!(source[0] == #char "#" && StartsWord(source, "#asm")) {
return result
}
at: s64 = 0
parentheses: s32 = 0
expressions: s32 = 0
opening: s64 = 0
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at); continue }
if byte == #char "(" || byte == #char "[" { parentheses += 1 }
else if byte == #char ")" || byte == #char "]" { parentheses -= 1 }
else if byte == #char "{" && parentheses == 0 {
before := at
while before > 0 && Space(source[before - 1]) { before -= 1 }
if expressions > 0 || (before > 0 && source[before - 1] == #char "." &&
(before < 2 || source[before - 2] != #char ".")) { expressions += 1 }
else { opening = at; break }
} else if byte == #char "}" && parentheses == 0 && expressions > 0 { expressions -= 1 }
at += 1
}
if opening == 0 || expressions != 0 { return result }
depth: s32 = 1
at = opening + 1
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" {
at = ClosedQuote(source, at); if at < 0 { return result }; continue
}
if byte == #char "{" { depth += 1 }
else if byte == #char "}" { depth -= 1; if depth == 0 { break } }
at += 1
}
if at >= source.count {
// 'for y: 0..2 { for x: 0..2 {' leaves the body open on later lines;
// its first statement here becomes a line of its own.
if SkipSpace(source, opening + 1) >= source.count { return result }
return .{true, opening + 1, opening + 1, source.count, source.count, true}
}
tail := SkipSpace(source, at + 1)
if Byte(source, tail) == #char ";" { tail = SkipSpace(source, tail + 1) }
if tail < source.count && (word == "do" || word == "case" || word == "guard" ||
(StartsWord(source[tail:], "else") && word != "if" && word != "else")) { return result }
return .{true, opening + 1, opening + 1, at, tail, false}
}
ClosingParenthesis :: (source: string) -> s64 {
if source.count == 0 || source[0] != #char "(" { return -1 }
depth: s32 = 0
at: s64 = 0
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at); continue }
if byte == #char "(" { depth += 1 }
else if byte == #char ")" { depth -= 1; if depth == 0 { return at } }
at += 1
}
return -1
}
// Returned text borrows the input. Type spacing is normalized separately in
// caller-owned storage, so the scanner needs neither allocation nor native ABI.
ProcedureHeaderParts :: (source: string) -> ProcedureHeader {
header: ProcedureHeader = .{"", "", "void"}
colons := Find(source, "::")
if colons >= 0 {
begin: s64 = 0
while begin < colons && (source[begin] == #char " " || source[begin] == #char "\t") { begin += 1 }
end := begin
while end < colons && SourceIdentifier(source[end]) { end += 1 }
header.name = source[begin:end]
}
opening := Find(source, "(")
if opening < 0 { return header }
closing := ClosingParenthesis(source[opening:])
if closing < 0 { return header }
closing += opening
end := closing
while end > opening + 1 && Space(source[end - 1]) { end -= 1 }
header.parameters = source[opening + 1:end]
at := closing + 1
while at < source.count && (source[at] == #char " " || source[at] == #char "\t") { at += 1 }
if !Matches(source, at, "->") { return header }
at += 2
while at < source.count && (source[at] == #char " " || source[at] == #char "\t") { at += 1 }
end = at
while end < source.count && source[end] != #char "#" && source[end] != #char "{" { end += 1 }
while end > at && Space(source[end - 1]) { end -= 1 }
header.result = source[at:end]
return header
}
// Safe for in-place output: normalization only removes bytes. A short output
// receives its bounded prefix; the returned count measures the complete text.
NormalizeTypeSpacing :: (source: string, output: []u8) -> s64 {
used: s64 = 0
at: s64 = 0
quoted := false
while at < source.count {
byte := source[at]
Put(output, *used, byte)
at += 1
if quoted {
if byte == #char "\\" && at < source.count { Put(output, *used, source[at]); at += 1 }
else if byte == #char "\"" { quoted = false }
} else if byte == #char "\"" { quoted = true }
else if byte == #char "]" || byte == #char "*" {
next := at
while next < source.count && (source[next] == #char " " || source[next] == #char "\t") { next += 1 }
following := Byte(source, next)
if next > at && (Letter(following) || following == #char "[" || following == #char "$") { at = next }
}
}
return used
}
// The frontend's fixed buffers reserve one byte for a terminating null.
// Trim a truncated type before normalizing, as for a complete header part.
CopyProcedureText :: (source: string, output: []u8, normalize_type: bool) -> s64 {
if output.count == 0 { return 0 }
count := source.count
if count >= output.count { count = output.count - 1 }
used: s64 = 0
if normalize_type {
while count > 0 && Space(source[count - 1]) { count -= 1 }
used = NormalizeTypeSpacing(source[0:count], output[0:count])
} else {
for index: 0..count - 1 { output[index] = source[index] }
used = count
}
output[used] = 0
return used
}
OpeningBraceOutsideLiterals :: (source: string) -> s64 {
at: s64 = 0
while at < source.count {
byte := source[at]
if byte == #char "\\" { at += 1; if at < source.count { at += 1 } }
else if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at) }
else if byte == #char "{" { return at }
else { at += 1 }
}
return -1
}
BraceOutsideLiterals :: (source: string) -> bool {
return OpeningBraceOutsideLiterals(source) >= 0
}
LooksLikeProcedureHeader :: (source: string) -> bool {
if StartsWord(source, "#import") || Matches(source, 0, "#import,") { return false }
colons := Find(source, "::")
if colons < 0 { return false }
at := SkipSpace(source, colons + 2)
end := source.count
while end > at && Space(source[end - 1]) { end -= 1 }
body := source[at:end]
if StartsWord(body, "#as") { body = body[SkipSpace(body, 3):] }
if StartsWord(body, "#import") || Matches(body, 0, "#import,") ||
StartsWord(body, "#defined") || StartsWord(body, "#define") ||
StartsWord(body, "struct") || StartsWord(body, "enum") || StartsWord(body, "union") ||
Find(body, "#type") >= 0 || Byte(body, 0) != #char "(" { return false }
if Find(body, "{") >= 0 || Find(body, "->") >= 0 { return true }
// A parenthesized constant is a procedure only when it has a typed
// parameter list. Keep the declaration classifier's source rules here.
depth: s32 = 0
begin: s64 = -1
end = -1
at = 0
while at < body.count {
if body[at] == #char "(" { if depth == 0 { begin = at + 1 }; depth += 1 }
else if body[at] == #char ")" { depth -= 1; if depth == 0 { end = at; break } }
at += 1
}
if begin < 0 || end < 0 { return false }
while begin < end && (body[begin] == #char " " || body[begin] == #char "\t") { begin += 1 }
if begin >= end { return true }
byte := body[begin]
if byte == #char "\"" || byte == #char "'" || (byte >= #char "0" && byte <= #char "9") { return false }
return Find(body[begin:], "?") < 0 && Find(body[begin:], ":") >= 0
}
SplitProcedureBody :: (source: string) -> ControlBlock {
block: ControlBlock
opening := OpeningBraceOutsideLiterals(source)
if opening < 0 || Byte(source, source.count - 1) != #char "}" ||
!LooksLikeProcedureHeader(source) { return block }
depth: s32 = 0
at := opening
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at); continue }
if byte == #char "{" { depth += 1 }
else if byte == #char "}" {
depth -= 1
if depth < 0 || (depth == 0 && at != source.count - 1) { return block }
}
at += 1
}
if depth != 0 { return block }
return .{true, opening + 1, opening + 1, source.count - 1, source.count, false}
}
ProcedureMustUse :: (source: string, result_type: string) -> MustUseModifier {
opening := Find(source, "(")
if opening < 0 { return .None }
closing := ClosingParenthesis(source[opening:])
if closing < 0 { return .None }
closing += opening
tail := source[closing + 1:]
end := Find(tail, "{")
if end < 0 { end = Find(tail, ";") }
if end < 0 { end = tail.count }
count: s32 = 0
at: s64 = 0
while at < end {
if tail[at] == #char "\"" { at = AfterQuote(tail[0:end], at) }
else if Matches(tail[0:end], at, "#must") && !SourceIdentifier(Byte(tail, at + 5)) {
after := SkipSpace(tail, at + 5)
if after < end && tail[after] != #char "#" { return .Arguments }
count += 1
at += 5
} else { at += 1 }
}
if count > 1 { return .Duplicate }
if count == 0 { return .None }
if result_type == "void" { return .NoResult }
return .Required
}
// #inline or #cold between a procedure's parameters and its body.
ProcedureInlining :: (source: string) -> InliningModifier {
opening := Find(source, "(")
if opening < 0 { return .None }
closing := ClosingParenthesis(source[opening:])
if closing < 0 { return .None }
closing += opening
tail := source[closing + 1:]
end := Find(tail, "{")
if end < 0 { end = Find(tail, ";") }
if end < 0 { end = tail.count }
head := tail[0:end]
inline: s32 = 0
cold: s32 = 0
at: s64 = 0
while at < end {
length: s64 = 0
if Matches(head, at, "#inline") { length = 7 }
else if Matches(head, at, "#cold") { length = 5 }
if tail[at] == #char "\"" { at = AfterQuote(head, at) }
else if length > 0 && !SourceIdentifier(Byte(head, at + length)) {
after := SkipSpace(head, at + length)
if after < end && head[after] != #char "#" { return .Arguments }
if length == 7 { inline += 1 } else { cold += 1 }
at += length
} else { at += 1 }
}
if inline > 0 && cold > 0 { return .Both }
if inline > 1 || cold > 1 { return .Duplicate }
if inline == 1 { return .Inline }
if cold == 1 { return .Cold }
return .None
}
// #target between a procedure's parameters and its body.
ProcedureTarget :: (source: string) -> TargetModifier {
opening := Find(source, "(")
if opening < 0 { return .None }
closing := ClosingParenthesis(source[opening:])
if closing < 0 { return .None }
closing += opening
tail := source[closing + 1:]
end := Find(tail, "{")
if end < 0 { end = Find(tail, ";") }
if end < 0 { end = tail.count }
head := tail[0:end]
count: s32 = 0
at: s64 = 0
while at < end {
if head[at] == #char "\"" { at = AfterQuote(head, at) }
else if Matches(head, at, "#target") && !SourceIdentifier(Byte(head, at + 7)) {
after := SkipSpace(head, at + 7)
if after < end && head[after] != #char "#" { return .Arguments }
count += 1
at += 7
} else { at += 1 }
}
if count > 1 { return .Duplicate }
if count == 1 { return .Target }
return .None
}