SPB Git

Repository Public

SyntaxHighlighter.swift 13.2 KB 200 lines swift

1//2//  SP Chat iOS3//  File: SPChat-iOS/Components/SyntaxHighlighter.swift4//  Description: Small native tokenizer that colours code for the common languages models write.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//910import Foundation11import SwiftUI12import UIKit1314enum SyntaxHighlighter {15    enum Token { case plain, comment, string, number, keyword, type, function, tag, property }1617    struct Theme: Sendable {18        // code blocks follow the theme (surface / surface-2): each token has a dark and a light value19        private static func dyn(_ dark: (Double, Double, Double), _ light: (Double, Double, Double)) -> Color {20            Color(uiColor: UIColor { t in21                let c = t.userInterfaceStyle == .dark ? dark : light22                return UIColor(red: c.0, green: c.1, blue: c.2, alpha: 1)23            })24        }25        var plain = dyn((0.90, 0.90, 0.93), (0.13, 0.13, 0.16))26        var comment = dyn((0.50, 0.52, 0.60), (0.47, 0.48, 0.54))27        var string = dyn((0.62, 0.86, 0.62), (0.10, 0.48, 0.28))28        var number = dyn((0.98, 0.72, 0.47), (0.70, 0.36, 0.05))29        var keyword = dyn((0.74, 0.66, 1.00), (0.35, 0.27, 0.88))30        var type = dyn((0.49, 0.80, 0.99), (0.05, 0.40, 0.66))31        var function = dyn((0.98, 0.85, 0.52), (0.55, 0.38, 0.02))32        var tag = dyn((0.98, 0.56, 0.60), (0.76, 0.18, 0.26))33        var property = dyn((0.55, 0.87, 0.87), (0.04, 0.45, 0.47))34        func color(_ t: Token) -> Color {35            switch t { case .plain: plain; case .comment: comment; case .string: string; case .number: number; case .keyword: keyword; case .type: type; case .function: function; case .tag: tag; case .property: property }36        }37    }3839    private struct Grammar {40        var keywords: Set<String>41        var lineComments: [String] = ["//"]42        var blockComment: (String, String)? = ("/*", "*/")43        var backtickStrings = false44        var tripleQuotes = false45        var markup = false46        var caseInsensitive = false47    }4849    private static func words(_ s: String) -> Set<String> { Set(s.split(separator: " ").map(String.init)) }5051    private static let grammars: [String: Grammar] = {52        let cLike = "if else for while do switch case default break continue return goto sizeof struct union enum typedef static const volatile extern inline void int char short long float double signed unsigned bool true false NULL nullptr class public private protected virtual override template typename namespace using new delete try catch throw auto this operator friend constexpr noexcept include define ifdef ifndef endif pragma"53        let js = "const let var function return if else for while do switch case default break continue new delete typeof instanceof in of class extends super this import export from as async await yield try catch finally throw null undefined true false void static get set interface type enum implements public private protected readonly declare namespace abstract keyof never unknown any string number boolean object satisfies"54        var g: [String: Grammar] = [:]55        g["swift"] = Grammar(keywords: words("import let var func return if else guard switch case default for while repeat in break continue fallthrough struct class enum protocol extension actor init deinit self Self super nil true false throws throw rethrows try catch do defer async await static final private fileprivate internal public open mutating nonmutating override lazy weak unowned some any where as is typealias associatedtype inout operator subscript get set willSet didSet nonisolated isolated consuming borrowing indirect convenience required"))56        g["python"] = Grammar(keywords: words("def class return if elif else for while in not and or is None True False import from as with try except finally raise lambda yield pass break continue global nonlocal assert del async await match case self"), lineComments: ["#"], blockComment: nil, tripleQuotes: true)57        g["javascript"] = Grammar(keywords: words(js), backtickStrings: true)58        g["json"] = Grammar(keywords: words("true false null"), lineComments: [], blockComment: nil)59        g["bash"] = Grammar(keywords: words("if then else elif fi for while until do done case esac in function return exit local export readonly declare unset shift source alias echo cd set trap eval exec"), lineComments: ["#"], blockComment: nil)60        g["sql"] = Grammar(keywords: words("select from where and or not in is null as join left right inner outer full cross on group by order having limit offset insert into values update set delete create table index view drop alter add column primary key foreign references unique default check constraint union all distinct case when then else end with returning exists between like ilike asc desc count sum avg min max coalesce cast begin commit rollback true false"), lineComments: ["--"], caseInsensitive: true)61        g["c"] = Grammar(keywords: words(cLike))62        g["go"] = Grammar(keywords: words("package import func return if else for range switch case default break continue go defer select chan map struct interface type var const nil true false fallthrough goto string int int64 int32 float64 bool byte rune error any make new len cap append"), backtickStrings: true)63        g["rust"] = Grammar(keywords: words("fn let mut const static return if else match for while loop in break continue struct enum trait impl pub use mod crate self Self super as where async await move ref dyn unsafe extern type true false Some None Ok Err Box Vec String Option Result"))64        g["java"] = Grammar(keywords: words("package import class interface enum extends implements public private protected static final abstract void int long double float boolean char byte short new return if else for while do switch case default break continue try catch finally throw throws this super null true false var record sealed permits synchronized volatile transient instanceof fun val when object companion data override suspend lateinit"))65        g["ruby"] = Grammar(keywords: words("def end class module if elsif else unless while until for in do return yield begin rescue ensure raise self nil true false and or not then case when require include attr_accessor attr_reader puts lambda proc"), lineComments: ["#"], blockComment: nil)66        g["yaml"] = Grammar(keywords: words("true false null yes no on off"), lineComments: ["#"], blockComment: nil)67        g["html"] = Grammar(keywords: [], lineComments: [], blockComment: ("<!--", "-->"), markup: true)68        g["css"] = Grammar(keywords: words("important media import supports keyframes from to and not only screen print"), lineComments: [])69        g["r"] = Grammar(keywords: words("function if else for while repeat break next return TRUE FALSE NULL NA library require in"), lineComments: ["#"], blockComment: nil)70        g["latex"] = Grammar(keywords: [], lineComments: ["%"], blockComment: nil)71        return g72    }()7374    private static let aliases: [String: String] = [75        "js": "javascript", "jsx": "javascript", "ts": "javascript", "tsx": "javascript", "typescript": "javascript", "mjs": "javascript", "node": "javascript",76        "py": "python", "python3": "python", "sh": "bash", "zsh": "bash", "shell": "bash", "console": "bash",77        "c++": "c", "cpp": "c", "h": "c", "hpp": "c", "objc": "c", "objective-c": "c", "cs": "java", "csharp": "java", "kotlin": "java", "kt": "java", "scala": "java", "dart": "java", "php": "java",78        "golang": "go", "rs": "rust", "rb": "ruby", "yml": "yaml", "toml": "yaml", "ini": "yaml", "xml": "html", "svg": "html", "vue": "html", "postgres": "sql", "postgresql": "sql", "mysql": "sql", "sqlite": "sql",79        "scss": "css", "less": "css", "jsonc": "json", "json5": "json", "tex": "latex", "dockerfile": "bash", "makefile": "bash",80    ]8182    static func canonicalLanguage(_ lang: String?) -> String? {83        guard let l = lang?.lowercased(), !l.isEmpty else { return nil }84        let k = aliases[l] ?? l85        return grammars[k] != nil ? k : nil86    }8788    /// Highlights `code`. Unknown languages and very large blocks are returned uncoloured (still monospaced).89    static func highlight(_ code: String, language: String?, theme: Theme = Theme()) -> AttributedString {90        var out = AttributedString()91        guard let key = canonicalLanguage(language), let g = grammars[key], code.utf8.count <= 60_000 else {92            var a = AttributedString(code); a.foregroundColor = theme.plain93            return a94        }95        for (token, text) in tokenize(code, g, isJSON: key == "json") {96            var a = AttributedString(text); a.foregroundColor = theme.color(token)97            out.append(a)98        }99        return out100    }101102    private static func tokenize(_ code: String, _ g: Grammar, isJSON: Bool) -> [(Token, String)] {103        let c = Array(code); let n = c.count104        var out: [(Token, String)] = []105        var plain = ""106        var i = 0107        func emit(_ t: Token, _ s: String) {108            if t == .plain { plain += s; return }109            if !plain.isEmpty { out.append((.plain, plain)); plain = "" }110            out.append((t, s))111        }112        func starts(_ s: String, at i: Int) -> Bool {113            let a = Array(s); guard i + a.count <= n else { return false }114            for k in 0..<a.count where c[i + k] != a[k] { return false }115            return true116        }117        while i < n {118            let ch = c[i]119            // comments120            if let (open, close) = g.blockComment, starts(open, at: i) {121                var j = i + open.count122                while j < n, !starts(close, at: j) { j += 1 }123                j = min(n, j + close.count)124                emit(.comment, String(c[i..<j])); i = j; continue125            }126            if let lc = g.lineComments.first(where: { starts($0, at: i) }), lc != "#" || i == 0 || c[i - 1].isWhitespace {127                var j = i128                while j < n, c[j] != "\n" { j += 1 }129                emit(.comment, String(c[i..<j])); i = j; continue130            }131            // markup tags132            if g.markup, ch == "<", i + 1 < n, c[i + 1].isLetter || c[i + 1] == "/" || c[i + 1] == "!" {133                var j = i + 1134                while j < n, c[j] == "/" || c[j] == "!" || c[j].isLetter || c[j].isNumber || c[j] == "-" || c[j] == ":" { j += 1 }135                emit(.tag, String(c[i..<j])); i = j136                // attributes up to ">"137                while i < n, c[i] != ">" {138                    if c[i] == "\"" || c[i] == "'" {139                        let q = c[i]; var k = i + 1140                        while k < n, c[k] != q { k += 1 }141                        k = min(n, k + 1); emit(.string, String(c[i..<k])); i = k142                    } else if c[i].isLetter {143                        var k = i144                        while k < n, c[k].isLetter || c[k].isNumber || c[k] == "-" || c[k] == ":" { k += 1 }145                        emit(.property, String(c[i..<k])); i = k146                    } else { emit(.plain, String(c[i])); i += 1 }147                }148                if i < n { emit(.tag, ">"); i += 1 }149                continue150            }151            // strings152            if ch == "\"" || ch == "'" || (ch == "`" && g.backtickStrings) {153                if g.tripleQuotes, i + 2 < n, c[i + 1] == ch, c[i + 2] == ch {154                    var j = i + 3155                    while j + 2 < n, !(c[j] == ch && c[j + 1] == ch && c[j + 2] == ch) { j += 1 }156                    j = min(n, j + 3); emit(.string, String(c[i..<j])); i = j; continue157                }158                var j = i + 1159                while j < n, c[j] != ch {160                    if c[j] == "\\" { j += 1 }161                    if j < n, c[j] == "\n", ch != "`" { break }162                    j += 1163                }164                j = min(n, j + 1)165                var token: Token = .string166                if isJSON {     // a string followed by ":" is a key167                    var k = j168                    while k < n, c[k] == " " { k += 1 }169                    if k < n, c[k] == ":" { token = .property }170                }171                emit(token, String(c[i..<j])); i = j; continue172            }173            // numbers174            if ch.isNumber, i == 0 || !(c[i - 1].isLetter || c[i - 1] == "_") {175                var j = i176                while j < n, c[j].isNumber || c[j].isLetter || c[j] == "." || c[j] == "_" { j += 1 }177                emit(.number, String(c[i..<j])); i = j; continue178            }179            // identifiers180            if ch.isLetter || ch == "_" || ch == "@" || ch == "$" {181                var j = i + 1182                while j < n, c[j].isLetter || c[j].isNumber || c[j] == "_" { j += 1 }183                let word = String(c[i..<j])184                let lookup = g.caseInsensitive ? word.lowercased() : word185                var k = j186                while k < n, c[k] == " " { k += 1 }187                if g.keywords.contains(lookup) { emit(.keyword, word) }188                else if ch == "@" { emit(.property, word) }189                else if k < n, c[k] == "(" { emit(.function, word) }190                else if let f = word.first, f.isUppercase, word.count > 1, !g.markup { emit(.type, word) }191                else { emit(.plain, word) }192                i = j; continue193            }194            emit(.plain, String(ch)); i += 1195        }196        if !plain.isEmpty { out.append((.plain, plain)) }197        return out198    }199}200
Files main
Menu

Theme