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
+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
}
}
}
}