Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Packages/AstraUI/Sources/Markdown/SyntaxHighlighter.swift10// Module : Markdown11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2113// Last Modified : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation8990public enum TokenKind: Sendable, Hashable { case plain, keyword, string, comment, number, type, function, attribute, punctuation }9192public struct CodeToken: Sendable, Hashable {93 public var text: String94 public var kind: TokenKind95}9697public enum SyntaxHighlighter {98 struct Grammar {99 var keywords: Set<String>100 var lineComments: [String] = ["//"]101 var blockComment: (String, String)? = ("/*", "*/")102 var stringDelimiters: [String] = ["\"", "'"]103 var multilineStrings: [String] = []104 var attributePrefix: Character? = nil105 var caseInsensitive = false106 var typesAreCapitalized = true107 }108109 private static let cLike: Set<String> = ["if", "else", "for", "while", "do", "switch", "case", "default", "break", "continue", "return", "struct", "enum", "union", "typedef", "static", "const", "void", "int", "long", "short", "char", "float", "double", "unsigned", "signed", "sizeof", "goto", "extern", "inline", "volatile", "true", "false", "NULL", "nullptr", "class", "public", "private", "protected", "virtual", "override", "template", "typename", "namespace", "using", "new", "delete", "this", "throw", "try", "catch", "auto", "bool", "operator", "constexpr", "noexcept", "include", "define", "ifdef", "ifndef", "endif", "pragma"]110 private static let js: Set<String> = ["const", "let", "var", "function", "return", "if", "else", "for", "while", "do", "switch", "case", "default", "break", "continue", "class", "extends", "new", "this", "super", "import", "export", "from", "as", "async", "await", "try", "catch", "finally", "throw", "typeof", "instanceof", "in", "of", "null", "undefined", "true", "false", "yield", "static", "get", "set", "delete", "void", "interface", "type", "enum", "implements", "public", "private", "protected", "readonly", "declare", "namespace", "abstract", "keyof", "never", "unknown", "any", "string", "number", "boolean", "satisfies"]111 private static let python: Set<String> = ["def", "class", "return", "if", "elif", "else", "for", "while", "break", "continue", "pass", "import", "from", "as", "try", "except", "finally", "raise", "with", "lambda", "yield", "global", "nonlocal", "assert", "del", "in", "not", "and", "or", "is", "None", "True", "False", "async", "await", "match", "case", "self"]112 private static let swift: Set<String> = ["let", "var", "func", "return", "if", "else", "guard", "for", "while", "repeat", "switch", "case", "default", "break", "continue", "struct", "class", "enum", "protocol", "extension", "actor", "import", "init", "deinit", "self", "Self", "super", "nil", "true", "false", "throw", "throws", "rethrows", "try", "catch", "do", "defer", "async", "await", "in", "is", "as", "where", "static", "private", "fileprivate", "internal", "public", "open", "final", "override", "mutating", "nonmutating", "lazy", "weak", "unowned", "some", "any", "typealias", "associatedtype", "subscript", "operator", "inout", "indirect", "convenience", "required", "nonisolated", "isolated", "consuming", "borrowing", "macro"]113 private static let rust: Set<String> = ["fn", "let", "mut", "const", "static", "struct", "enum", "trait", "impl", "for", "in", "while", "loop", "if", "else", "match", "return", "break", "continue", "use", "mod", "pub", "crate", "self", "Self", "super", "as", "where", "async", "await", "move", "ref", "dyn", "unsafe", "extern", "type", "true", "false", "Some", "None", "Ok", "Err"]114 private static let go: Set<String> = ["func", "var", "const", "type", "struct", "interface", "map", "chan", "package", "import", "return", "if", "else", "for", "range", "switch", "case", "default", "break", "continue", "go", "defer", "select", "fallthrough", "goto", "nil", "true", "false", "make", "new", "len", "cap", "append"]115 private static let java: Set<String> = ["public", "private", "protected", "class", "interface", "enum", "extends", "implements", "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", "import", "package", "this", "super", "null", "true", "false", "var", "val", "fun", "when", "object", "companion", "data", "sealed", "override", "suspend", "record", "instanceof", "synchronized", "namespace", "using", "string", "readonly", "async", "await", "is", "as", "in", "out", "internal", "virtual", "struct", "get", "set"]116 private static let sql: Set<String> = ["select", "from", "where", "insert", "into", "values", "update", "set", "delete", "create", "table", "index", "view", "drop", "alter", "add", "column", "join", "left", "right", "inner", "outer", "full", "cross", "on", "as", "and", "or", "not", "null", "is", "in", "exists", "between", "like", "ilike", "group", "by", "order", "having", "limit", "offset", "union", "all", "distinct", "case", "when", "then", "else", "end", "primary", "key", "foreign", "references", "default", "constraint", "unique", "with", "returning", "begin", "commit", "rollback", "true", "false", "asc", "desc", "count", "sum", "avg", "min", "max", "coalesce", "cast", "over", "partition", "window", "if", "replace", "function", "trigger", "grant", "to"]117 private static let shell: Set<String> = ["if", "then", "else", "elif", "fi", "for", "in", "do", "done", "while", "until", "case", "esac", "function", "return", "exit", "export", "local", "readonly", "set", "unset", "echo", "cd", "source", "alias", "sudo", "true", "false", "trap", "shift", "eval", "exec", "test"]118 private static let ruby: Set<String> = ["def", "end", "class", "module", "if", "elsif", "else", "unless", "while", "until", "for", "in", "do", "return", "yield", "begin", "rescue", "ensure", "raise", "require", "include", "extend", "attr_accessor", "attr_reader", "self", "nil", "true", "false", "then", "case", "when", "and", "or", "not", "lambda", "proc", "puts", "super"]119 private static let rLang: Set<String> = ["function", "if", "else", "for", "while", "repeat", "break", "next", "return", "in", "TRUE", "FALSE", "NULL", "NA", "NaN", "Inf", "library", "require", "source"]120121 static func grammar(for language: String?) -> Grammar? {122 switch (language ?? "").lowercased() {123 case "swift": return Grammar(keywords: swift, multilineStrings: ["\"\"\""], attributePrefix: "@")124 case "js", "javascript", "jsx", "ts", "typescript", "tsx", "mjs", "cjs", "node": return Grammar(keywords: js, stringDelimiters: ["\"", "'", "`"], attributePrefix: "@")125 case "py", "python", "python3": return Grammar(keywords: python, lineComments: ["#"], blockComment: nil, multilineStrings: ["\"\"\"", "'''"], attributePrefix: "@")126 case "c", "h", "cpp", "c++", "cc", "hpp", "objc", "objective-c", "m", "mm", "metal", "glsl": return Grammar(keywords: cLike, attributePrefix: "#")127 case "rust", "rs": return Grammar(keywords: rust, stringDelimiters: ["\""], attributePrefix: "#")128 case "go", "golang": return Grammar(keywords: go, stringDelimiters: ["\"", "'", "`"])129 case "java", "kotlin", "kt", "scala", "cs", "csharp", "c#", "dart", "php", "groovy": return Grammar(keywords: java, attributePrefix: "@")130 case "sql", "psql", "postgres", "postgresql", "mysql", "sqlite", "plpgsql": return Grammar(keywords: sql, lineComments: ["--"], stringDelimiters: ["'"], caseInsensitive: true, typesAreCapitalized: false)131 case "sh", "bash", "zsh", "shell", "console", "fish", "dockerfile", "makefile", "make": return Grammar(keywords: shell, lineComments: ["#"], blockComment: nil, typesAreCapitalized: false)132 case "rb", "ruby": return Grammar(keywords: ruby, lineComments: ["#"], blockComment: nil, attributePrefix: "@")133 case "r": return Grammar(keywords: rLang, lineComments: ["#"], blockComment: nil, typesAreCapitalized: false)134 case "yaml", "yml", "toml", "ini", "conf", "env", "properties": return Grammar(keywords: ["true", "false", "null", "yes", "no", "on", "off"], lineComments: ["#"], blockComment: nil, typesAreCapitalized: false)135 case "json", "jsonc", "json5": return Grammar(keywords: ["true", "false", "null"], stringDelimiters: ["\""], typesAreCapitalized: false)136 case "html", "xml", "svg", "vue", "xhtml", "plist": return Grammar(keywords: [], lineComments: [], blockComment: ("<!--", "-->"), typesAreCapitalized: false)137 case "css", "scss", "less": return Grammar(keywords: ["important", "media", "import", "keyframes", "from", "to", "root", "var", "calc"], lineComments: [], attributePrefix: "@", typesAreCapitalized: false)138 case "tex", "latex": return Grammar(keywords: [], lineComments: ["%"], blockComment: nil, stringDelimiters: [], typesAreCapitalized: false)139 case "lua": return Grammar(keywords: ["local", "function", "end", "if", "then", "else", "elseif", "for", "while", "do", "repeat", "until", "return", "nil", "true", "false", "and", "or", "not", "in", "break"], lineComments: ["--"], blockComment: ("--[[", "]]"))140 case "", "text", "txt", "plain", "plaintext", "output", "log", "diff", "markdown", "md", "mermaid", "csv": return nil141 default: return Grammar(keywords: cLike.union(js).union(python), lineComments: ["//", "#"])142 }143 }144145 /// Splits `code` into coloured runs. Linear in the size of the input; unknown languages get a generic grammar and146 /// plain-text languages a single run.147 public static func tokens(_ code: String, language: String?) -> [CodeToken] {148 guard let g = grammar(for: language), code.utf8.count < 400_000 else { return [CodeToken(text: code, kind: .plain)] }149 let s = Array(code); let n = s.count; var i = 0150 var out: [CodeToken] = []; var plain = ""151 func flushPlain() { if !plain.isEmpty { out.append(CodeToken(text: plain, kind: .plain)); plain = "" } }152 func emit(_ range: Range<Int>, _ kind: TokenKind) { flushPlain(); out.append(CodeToken(text: String(s[range]), kind: kind)) }153 func starts(_ lit: String, at: Int) -> Bool { let a = Array(lit); guard at + a.count <= n else { return false }; for k in 0..<a.count where s[at + k] != a[k] { return false }; return true }154 let markup = g.blockComment?.0 == "<!--"155 while i < n {156 let c = s[i]157 if let bc = g.blockComment, starts(bc.0, at: i) {158 var j = i + bc.0.count; while j < n, !starts(bc.1, at: j) { j += 1 }159 let end = min(n, j + bc.1.count); emit(i..<end, .comment); i = end; continue160 }161 if let lc = g.lineComments.first(where: { starts($0, at: i) }) {162 // `#` only opens a comment at a word boundary (so that `a#b` or CSS colours inside values stay intact)163 if lc != "#" || i == 0 || s[i - 1].isWhitespace || s[i - 1] == ";" { var j = i; while j < n, s[j] != "\n" { j += 1 }; emit(i..<j, .comment); i = j; continue }164 }165 if let ms = g.multilineStrings.first(where: { starts($0, at: i) }) {166 var j = i + ms.count; while j < n, !starts(ms, at: j) { j += s[j] == "\\" ? 2 : 1 }167 let end = min(n, j + ms.count); emit(i..<end, .string); i = end; continue168 }169 if g.stringDelimiters.contains(String(c)) {170 var j = i + 1171 while j < n, s[j] != c { if s[j] == "\\" { j += 1 } else if s[j] == "\n" && c != "`" { break }; j += 1 }172 let end = min(n, j + 1); emit(i..<end, .string); i = end; continue173 }174 if markup, c == "<" {175 var j = i + 1; if j < n, s[j] == "/" { j += 1 }176 let nameStart = j; while j < n, s[j].isLetter || s[j].isNumber || s[j] == "-" || s[j] == ":" { j += 1 }177 if j > nameStart { emit(i..<nameStart, .punctuation); emit(nameStart..<j, .keyword); i = j; continue }178 }179 if c.isNumber, i == 0 || !(s[i - 1].isLetter || s[i - 1] == "_") {180 var j = i + 1; while j < n, s[j].isNumber || s[j].isLetter || s[j] == "." || s[j] == "_" { j += 1 }181 emit(i..<j, .number); i = j; continue182 }183 if let ap = g.attributePrefix, c == ap, i + 1 < n, s[i + 1].isLetter {184 var j = i + 1; while j < n, s[j].isLetter || s[j].isNumber || s[j] == "_" || s[j] == "-" { j += 1 }185 emit(i..<j, .attribute); i = j; continue186 }187 if c.isLetter || c == "_" {188 var j = i + 1; while j < n, s[j].isLetter || s[j].isNumber || s[j] == "_" { j += 1 }189 let word = String(s[i..<j])190 let isKeyword = g.caseInsensitive ? g.keywords.contains(word.lowercased()) : g.keywords.contains(word)191 if isKeyword { emit(i..<j, .keyword) }192 else if markup { var k = j; while k < n, s[k] == " " { k += 1 }; if k < n, s[k] == "=" { emit(i..<j, .attribute) } else { plain += word } }193 else if j < n, s[j] == "(" { emit(i..<j, .function) }194 else if g.typesAreCapitalized, let f = word.first, f.isUppercase, word.count > 1 { emit(i..<j, .type) }195 else { plain += word }196 i = j; continue197 }198 plain.append(c); i += 1199 }200 flushPlain()201 return out202 }203}204