import Foundation public struct PHPDeclaration: Sendable, Equatable { public var kind: String public var name: String public var parents: [String] public var interfaces: [String] public init(kind: String, name: String, parents: [String], interfaces: [String]) { self.kind = kind self.name = name self.parents = parents self.interfaces = interfaces } } public enum PHPSymbols { /// ripgrep pattern for declarations and `namespace X;` lines in one pass. public static let declarationPattern = #"^\s*(namespace\s+[A-Za-z_\\][\w\\]*\s*;|(final\s+|abstract\s+|readonly\s+)*(class|interface|trait|enum)\s+\w+)"# /// ripgrep pattern for an indented `use` inside a class body. public static let traitUsePattern = #"^\s+use\s+[A-Za-z_\\][\w\\ \t,]*[;{]"# /// The declaration on one line, or nil. `final`/`abstract`/`readonly` may precede the keyword. /// /// Only this line is read, so an `implements` list wrapped onto the next line yields /// fewer interfaces, never wrong ones. Namespaced parents are reduced to short names. public static func parseDeclaration(_ line: String) -> PHPDeclaration? { let declaration = /^[ \t]*(?:(?:final|abstract|readonly)[ \t]+)*(class|interface|trait|enum)[ \t]+([A-Za-z_]\w*)(.*)$/ guard let match = line.firstMatch(of: declaration) else { return nil } let rest = match.3 let extends = rest.firstMatch(of: /\bextends\s+([^{]*?)(?:\s+implements\b|\s*\{|$)/) let implements = rest.firstMatch(of: /\bimplements\s+([^{]*?)(?:\s*\{|$)/) return PHPDeclaration( kind: String(match.1), name: String(match.2), parents: extends.map { clauseNames($0.1) } ?? [], interfaces: implements.map { clauseNames($0.1) } ?? [] ) } /// The namespace a `namespace X;` line declares, without a leading backslash, or nil. public static func parseNamespace(_ line: String) -> String? { guard let match = line.firstMatch(of: /^[ \t]*namespace[ \t]+([A-Za-z_\\][\w\\]*)[ \t]*;/) else { return nil } let name = match.1 return String(name.hasPrefix("\\") ? name.dropFirst() : name) } /// The traits an indented `use A, B;` mixes in; `use function`, `use const` and anything else give []. public static func parseTraitUse(_ line: String) -> [String] { guard let match = line.firstMatch(of: /^[ \t]+use[ \t]+([A-Za-z_\\][\w\\ \t,]*?)[ \t]*[;{]/), line.firstMatch(of: /^\s*use\s+(function|const)\b/) == nil else { return [] } return clauseNames(match.1) } /// Short class names out of an `extends`/`implements`/`use` clause. private static func clauseNames(_ clause: Substring) -> [String] { clause.split(separator: ",", omittingEmptySubsequences: false).compactMap { part in let last = part.trimmingCharacters(in: .whitespaces).split(separator: "\\", omittingEmptySubsequences: false).last ?? "" let name = last.trimmingCharacters(in: .whitespaces) return isIdentifier(name) ? name : nil } } /// A column-0 `use` line: a namespace import, which reference search drops as noise. public static func isImportLine(_ line: String) -> Bool { line.range(of: #"^use[ \t]+[^;]*;?[ \t\r]*$"#, options: .regularExpression) != nil } /// True for a bare PHP identifier, the only thing a lookup accepts. public static func isIdentifier(_ name: String) -> Bool { name.range(of: #"^[A-Za-z_]\w*$"#, options: .regularExpression) != nil } /// Project declarations before `vendor/` ones, then by path, capped at `SymbolLimits.maxDefinitions`. public static func sortDefinitions(_ defs: [SymbolDef]) -> [SymbolDef] { let sorted = defs.sorted { let lv = $0.path.hasPrefix("vendor/"), rv = $1.path.hasPrefix("vendor/") return lv != rv ? !lv : $0.path < $1.path } return Array(sorted.prefix(SymbolLimits.maxDefinitions)) } /// The identifier around a UTF-16 offset, or "" when the caret is not on one. public static func word(in text: String, at offset: Int) -> String { let units = Array(text.utf16) guard offset >= 0, offset <= units.count else { return "" } func isWord(_ u: UInt16) -> Bool { (u >= 48 && u <= 57) || (u >= 65 && u <= 90) || (u >= 97 && u <= 122) || u == 95 } var start = offset while start > 0 && isWord(units[start - 1]) { start -= 1 } var end = offset while end < units.count && isWord(units[end]) { end += 1 } let word = String(decoding: units[start..