1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers
2// SPDX-FileCopyrightText: 2026 The Project Pterodactyl Developers
3//
4// SPDX-License-Identifier: MPL-2.0
5
6/// This is a quick parser to build up the dependency graph without full parsing.
7public struct ImportParser {
8 private var lexer: PterodactylSyntax.Lexer
9 public private(set) var imports: [String] = []
10
11 public init(input: String) {
12 self.lexer = PterodactylSyntax.Lexer(input: input)
13 }
14
15 public mutating func parseHeader() {
16 while true {
17 let token = nextSignificantToken()
18 switch token.kind {
19 case .keyword(.import): parseImportStatement()
20 default: return
21 }
22 }
23 }
24
25 /// Returns the next non-whitespace token.
26 private mutating func nextSignificantToken() -> Token {
27 var token = lexer.scan()
28 while token.kind.isTrivia || token.kind == .blockBegin || token.kind == .blockSep || token.kind == .blockEnd {
29 token = lexer.scan()
30 }
31
32 return Token(kind: token.kind, text: token.text)
33 }
34
35 /// Parses a single `import xyz` line.
36 private mutating func parseImportStatement() {
37 let next = nextSignificantToken()
38 guard next.kind == .identifier else { return }
39 imports.append(next.text)
40 }
41}