improves plak

This commit is contained in:
2026-10-17 17:33:13 +02:00
parent 7b891819b0
commit a92d6f8871
633 changed files with 17623 additions and 7556 deletions
+114
View File
@@ -0,0 +1,114 @@
/// The kinds of code that get their own colour.
public enum CodeTokenKind: Sendable, Equatable {
case keyword
case string
case number
case comment
case variable
case tag
/// An attribute name in XML, a key in JSON.
case attribute
}
/// A highlighted piece of code; the range counts UTF-16 units, as NSRange does.
public struct CodeToken: Sendable, Equatable {
public let kind: CodeTokenKind
public let range: Range<Int>
public init(_ kind: CodeTokenKind, _ range: Range<Int>) {
self.kind = kind
self.range = range
}
}
/// The highlighted pieces of a code body; text between them is plain code.
public enum CodeHighlighter {
public static func tokens(_ body: String, kind: FenceKind) -> [CodeToken] {
switch kind {
case .php: PHPTokenizer.tokens(body)
case .json: JSONTokenizer.tokens(body)
case .xml: XMLTokenizer.tokens(body)
case .csv: []
}
}
}
/// Walks the UTF-16 units of a code body; all syntax that matters is ASCII.
struct CodeScanner {
let units: [UInt16]
var index = 0
private(set) var tokens: [CodeToken] = []
init(_ text: String) {
units = Array(text.utf16)
}
var atEnd: Bool {
index >= units.count
}
func peek(_ offset: Int = 0) -> UInt16? {
index + offset < units.count ? units[index + offset] : nil
}
func peekIs(_ character: Character, _ offset: Int = 0) -> Bool {
peek(offset) == character.utf16.first
}
func starts(with text: String) -> Bool {
var offset = 0
for unit in text.utf16 {
guard peek(offset) == unit else { return false }
offset += 1
}
return true
}
func text(from start: Int) -> String {
String(decoding: units[start..<index], as: UTF16.self)
}
mutating func add(_ kind: CodeTokenKind, from start: Int) {
if index > start { tokens.append(CodeToken(kind, start..<index)) }
}
mutating func skip(while condition: (UInt16) -> Bool) {
while let unit = peek(), condition(unit) { index += 1 }
}
/// Moves past `end`, or to the end when it never comes.
mutating func skip(past end: String) {
while !atEnd, !starts(with: end) { index += 1 }
index = min(units.count, index + end.utf16.count)
}
/// From an opening quote to past its closing quote.
mutating func skipQuoted(escapes: Bool) {
let quote = units[index]
index += 1
while let unit = peek() {
index += 1
if escapes, unit == 0x5C {
index = min(units.count, index + 1)
} else if unit == quote {
return
}
}
}
static func isLetter(_ unit: UInt16) -> Bool {
(unit >= 0x41 && unit <= 0x5A) || (unit >= 0x61 && unit <= 0x7A) || unit == 0x5F || unit >= 0x80
}
static func isDigit(_ unit: UInt16) -> Bool {
unit >= 0x30 && unit <= 0x39
}
static func isWord(_ unit: UInt16) -> Bool {
isLetter(unit) || isDigit(unit)
}
static func isSpace(_ unit: UInt16) -> Bool {
unit == 0x20 || unit == 0x09 || unit == 0x0A || unit == 0x0D
}
}
+27
View File
@@ -0,0 +1,27 @@
/// JSON: keys apart from string values, numbers, and true, false and null.
enum JSONTokenizer {
static func tokens(_ body: String) -> [CodeToken] {
var scanner = CodeScanner(body)
while let unit = scanner.peek() {
let start = scanner.index
if scanner.peekIs("\"") {
scanner.skipQuoted(escapes: true)
let end = scanner.index
scanner.skip(while: CodeScanner.isSpace)
let isKey = scanner.peekIs(":")
scanner.index = end
scanner.add(isKey ? .attribute : .string, from: start)
} else if scanner.peekIs("-") || CodeScanner.isDigit(unit) {
scanner.index += 1
scanner.skip { CodeScanner.isDigit($0) || $0 == 0x2E || $0 == 0x65 || $0 == 0x45 || $0 == 0x2B || $0 == 0x2D }
scanner.add(.number, from: start)
} else if CodeScanner.isLetter(unit) {
scanner.skip(while: CodeScanner.isWord)
if ["true", "false", "null"].contains(scanner.text(from: start)) { scanner.add(.keyword, from: start) }
} else {
scanner.index += 1
}
}
return scanner.tokens
}
}
+72
View File
@@ -0,0 +1,72 @@
/// PHP: keywords, strings and heredocs, comments, `$variables`, numbers and the open and close tags.
enum PHPTokenizer {
static let keywords: Set<String> = [
"abstract", "and", "array", "as", "bool", "break", "callable", "case", "catch", "class", "clone", "const",
"continue", "declare", "default", "do", "echo", "else", "elseif", "empty", "enddeclare", "endfor",
"endforeach", "endif", "endswitch", "endwhile", "enum", "extends", "false", "final", "finally", "float", "fn",
"for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "instanceof",
"insteadof", "int", "interface", "isset", "iterable", "list", "match", "mixed", "namespace", "never", "new",
"null", "object", "or", "parent", "print", "private", "protected", "public", "readonly", "require",
"require_once", "return", "self", "static", "string", "switch", "throw", "trait", "true", "try", "unset", "use",
"var", "void", "while", "xor", "yield",
]
static func tokens(_ body: String) -> [CodeToken] {
var scanner = CodeScanner(body)
while let unit = scanner.peek() {
let start = scanner.index
if scanner.starts(with: "<?php") || scanner.starts(with: "<?=") || scanner.starts(with: "?>") {
scanner.index += scanner.starts(with: "<?php") ? 5 : scanner.starts(with: "<?=") ? 3 : 2
scanner.add(.tag, from: start)
} else if scanner.starts(with: "//") || (scanner.peekIs("#") && !scanner.peekIs("[", 1)) {
scanner.skip { $0 != 0x0A }
scanner.add(.comment, from: start)
} else if scanner.starts(with: "/*") {
scanner.skip(past: "*/")
scanner.add(.comment, from: start)
} else if scanner.starts(with: "<<<") {
heredoc(&scanner)
scanner.add(.string, from: start)
} else if scanner.peekIs("'") || scanner.peekIs("\"") {
scanner.skipQuoted(escapes: true)
scanner.add(.string, from: start)
} else if scanner.peekIs("$"), let next = scanner.peek(1), CodeScanner.isLetter(next) {
scanner.index += 1
scanner.skip(while: CodeScanner.isWord)
scanner.add(.variable, from: start)
} else if CodeScanner.isDigit(unit) {
scanner.skip { CodeScanner.isWord($0) || $0 == 0x2E }
scanner.add(.number, from: start)
} else if CodeScanner.isLetter(unit) {
scanner.skip(while: CodeScanner.isWord)
if keywords.contains(scanner.text(from: start).lowercased()) { scanner.add(.keyword, from: start) }
} else {
scanner.index += 1
}
}
return scanner.tokens
}
/// `<<<ID`, `<<<"ID"` or `<<<'ID'` up to the line that starts with ID, indented or not.
private static func heredoc(_ scanner: inout CodeScanner) {
scanner.index += 3
scanner.skip { $0 == 0x20 || $0 == 0x09 }
let quoted = scanner.peekIs("'") || scanner.peekIs("\"")
if quoted { scanner.index += 1 }
let nameStart = scanner.index
scanner.skip(while: CodeScanner.isWord)
let name = scanner.text(from: nameStart)
if quoted { scanner.index += 1 }
guard !name.isEmpty else { return }
while !scanner.atEnd {
scanner.skip { $0 != 0x0A }
guard !scanner.atEnd else { return }
scanner.index += 1
scanner.skip { $0 == 0x20 || $0 == 0x09 }
if scanner.starts(with: name), !(scanner.peek(name.utf16.count).map(CodeScanner.isWord) ?? false) {
scanner.index += name.utf16.count
return
}
}
}
}
+55
View File
@@ -0,0 +1,55 @@
/// XML: tags, attribute names, attribute values, comments and CDATA.
enum XMLTokenizer {
static func tokens(_ body: String) -> [CodeToken] {
var scanner = CodeScanner(body)
while !scanner.atEnd {
let start = scanner.index
if scanner.starts(with: "<!--") {
scanner.skip(past: "-->")
scanner.add(.comment, from: start)
} else if scanner.starts(with: "<![CDATA[") {
scanner.skip(past: "]]>")
scanner.add(.string, from: start)
} else if scanner.peekIs("<"), let next = scanner.peek(1),
CodeScanner.isLetter(next) || next == 0x2F || next == 0x3F || next == 0x21 {
tag(&scanner)
} else {
scanner.index += 1
}
}
return scanner.tokens
}
/// From `<` up to and including `>`, `/>` or `?>`.
private static func tag(_ scanner: inout CodeScanner) {
let start = scanner.index
scanner.index += 1
if scanner.peekIs("/") || scanner.peekIs("?") || scanner.peekIs("!") { scanner.index += 1 }
scanner.skip(while: isName)
scanner.add(.tag, from: start)
while let unit = scanner.peek() {
let piece = scanner.index
if scanner.starts(with: "/>") || scanner.starts(with: "?>") {
scanner.index += 2
scanner.add(.tag, from: piece)
return
} else if scanner.peekIs(">") {
scanner.index += 1
scanner.add(.tag, from: piece)
return
} else if scanner.peekIs("\"") || scanner.peekIs("'") {
scanner.skipQuoted(escapes: false)
scanner.add(.string, from: piece)
} else if CodeScanner.isLetter(unit) {
scanner.skip(while: isName)
scanner.add(.attribute, from: piece)
} else {
scanner.index += 1
}
}
}
private static func isName(_ unit: UInt16) -> Bool {
CodeScanner.isWord(unit) || unit == 0x3A || unit == 0x2D || unit == 0x2E
}
}