Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassRegistry/SwiftSourceScanner.swift8// Module : SPGlassRegistry9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Lightweight lexical analysis of Swift sources, used to infer which registry items a file needs.44///45/// This is not a parser. It finds declared type names and `glass…` functions, and the identifiers a file46/// mentions outside comments and string literals. Inference may add a dependency that is not strictly47/// needed; it never misses a type that is referenced by name.48public enum SwiftSourceScanner {49 /// Names a file makes available to other files of the module: types that are not private, and50 /// functions or properties whose name starts with `glass` (the view modifiers).51 public static func declaredSymbols(in source: String) -> [String] {52 let code = strippingCommentsAndStrings(source)53 var symbols: [String] = []54 let type = /(?m)^[ \t]*((?:(?:@\w+(?:\([^)\n]*\))?|public|open|internal|package|private|fileprivate|final|indirect|nonisolated)\s+)*)(?:struct|enum|class|protocol|actor|typealias)\s+([A-Z][A-Za-z0-9_]*)/55 for match in code.matches(of: type) {56 let modifiers = String(match.output.1)57 if modifiers.contains("private") { continue }58 symbols.append(String(match.output.2))59 }60 let member = /(?m)^[ \t]*(?:public\s+)?(?:static\s+)?(?:func|var)\s+(glass[A-Z][A-Za-z0-9_]*)/61 for match in code.matches(of: member) {62 symbols.append(String(match.output.1))63 }64 var seen: Set<String> = []65 return symbols.filter { seen.insert($0).inserted }66 }6768 /// Public type names only, for the `symbols` field of a manifest.69 public static func publicTypes(in source: String) -> [String] {70 let code = strippingCommentsAndStrings(source)71 let type = /(?m)^[ \t]*(?:@\w+(?:\([^)\n]*\))?\s+)*(?:public|open)\s+(?:(?:final|indirect|nonisolated)\s+)*(?:struct|enum|class|protocol|actor|typealias)\s+([A-Z][A-Za-z0-9_]*)/72 var seen: Set<String> = []73 return code.matches(of: type).map { String($0.output.1) }.filter { seen.insert($0).inserted }74 }7576 /// Every identifier mentioned in code (not in comments or string literals).77 public static func referencedIdentifiers(in source: String) -> Set<String> {78 let code = strippingCommentsAndStrings(source)79 return Set(code.matches(of: /[A-Za-z_][A-Za-z0-9_]*/).map { String($0.output) })80 }8182 /// Modules imported by a file.83 public static func imports(in source: String) -> [String] {84 source.matches(of: /(?m)^[ \t]*(?:@_exported\s+|@testable\s+)?import\s+([A-Za-z_][A-Za-z0-9_]*)/).map { String($0.output.1) }85 }8687 /// Replaces comments and string literal contents with spaces, keeping line structure.88 public static func strippingCommentsAndStrings(_ source: String) -> String {89 var output = String.UnicodeScalarView()90 let scalars = Array(source.unicodeScalars)91 var index = 092 var blockDepth = 093 var inLine = false, inString = false, inMultiline = false94 func peek(_ offset: Int) -> Unicode.Scalar? { index + offset < scalars.count ? scalars[index + offset] : nil }95 while index < scalars.count {96 let scalar = scalars[index]97 if inLine {98 if scalar == "\n" { inLine = false; output.append(scalar) } else { output.append(" ") }99 index += 1100 } else if blockDepth > 0 {101 if scalar == "*", peek(1) == "/" { blockDepth -= 1; output.append(" "); output.append(" "); index += 2 }102 else if scalar == "/", peek(1) == "*" { blockDepth += 1; output.append(" "); output.append(" "); index += 2 }103 else { output.append(scalar == "\n" ? "\n" : " "); index += 1 }104 } else if inMultiline {105 if scalar == "\"", peek(1) == "\"", peek(2) == "\"" { inMultiline = false; output.append(contentsOf: "\"\"\"".unicodeScalars); index += 3 }106 else if scalar == "\\" { output.append(" "); output.append(peek(1) == "\n" ? "\n" : " "); index += 2 }107 else { output.append(scalar == "\n" ? "\n" : " "); index += 1 }108 } else if inString {109 if scalar == "\\" { output.append(" "); output.append(" "); index += 2 }110 else if scalar == "\"" || scalar == "\n" { inString = false; output.append(scalar); index += 1 }111 else { output.append(" "); index += 1 }112 } else if scalar == "/", peek(1) == "/" {113 inLine = true; output.append(" "); output.append(" "); index += 2114 } else if scalar == "/", peek(1) == "*" {115 blockDepth = 1; output.append(" "); output.append(" "); index += 2116 } else if scalar == "\"", peek(1) == "\"", peek(2) == "\"" {117 inMultiline = true; output.append(contentsOf: "\"\"\"".unicodeScalars); index += 3118 } else if scalar == "\"" {119 inString = true; output.append(scalar); index += 1120 } else {121 output.append(scalar); index += 1122 }123 }124 return String(output)125 }126}127128/// Prepares a package source file for source mode, where every GlassKit file lives in the host's own module.129public enum SourceTransform {130 /// Removes the imports of GlassKit modules, which do not exist in source mode. Everything else is kept131 /// byte for byte, including the header and its licence line.132 public static func forSourceMode(_ source: String, moduleNames: Set<String>) -> String {133 var lines = source.components(separatedBy: "\n")134 lines.removeAll { line in135 guard let match = line.wholeMatch(of: /[ \t]*(?:@_exported\s+)?import\s+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\r?/) else { return false }136 return moduleNames.contains(String(match.output.1))137 }138 return lines.joined(separator: "\n")139 }140}141