at main 1.1 kB view raw
1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers 2// 3// SPDX-License-Identifier: MPL-2.0 4 5import Foundation 6 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 guard let token = nextSignificantToken() else { return } 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.nextToken() 28 while token?.kind.isTrivia == true { 29 token = lexer.nextToken() 30 } 31 32 guard let token else { return nil } 33 return Token(kind: token.kind, text: token.text) 34 } 35 36 /// Parses a single `import xyz` line. 37 private mutating func parseImportStatement() { 38 guard let next = nextSignificantToken() else { return } 39 guard next.kind == .identifier else { return } 40 imports.append(next.text) 41 } 42}