207 lines
8.0 KiB
Swift
207 lines
8.0 KiB
Swift
import Foundation
|
|
|
|
/// Where an embedded grammar hands control back to its host.
|
|
enum Terminator: Hashable, Sendable {
|
|
/// `?>` closes a PHP island inside markup.
|
|
case phpClose
|
|
/// `</name` closes a `<script>` or `<style>` body; the name is lowercase.
|
|
case closingTag(String)
|
|
/// A Markdown fence closes on a line of its own, which the Markdown grammar checks itself.
|
|
case fence
|
|
}
|
|
|
|
/// A cursor over UTF-16 units that collects non-plain tokens for one line at a time.
|
|
///
|
|
/// Grammars scan `i..<end` and must return when `i` reaches `end`, or when `atStop()`
|
|
/// is true at a code position, so the host grammar can take over.
|
|
struct Lexer {
|
|
let u: [UInt16]
|
|
var i: Int
|
|
var end: Int
|
|
var stop: Terminator?
|
|
let symbols: Set<String>
|
|
private(set) var tokens: [Token] = []
|
|
|
|
init(units: [UInt16], symbols: Set<String>) {
|
|
u = units
|
|
i = 0
|
|
end = units.count
|
|
self.symbols = symbols
|
|
}
|
|
|
|
/// The unit `k` places ahead, or 0 past the end of the line.
|
|
@inline(__always) func peek(_ k: Int = 0) -> UInt16 {
|
|
let j = i + k
|
|
return j < end ? u[j] : 0
|
|
}
|
|
|
|
func unit(at j: Int) -> UInt16 {
|
|
j >= 0 && j < end ? u[j] : 0
|
|
}
|
|
|
|
mutating func emit(_ from: Int, _ to: Int, _ role: TokenRole) {
|
|
guard to > from, role != .plain else { return }
|
|
if let last = tokens.last, last.role == role, last.location + last.length == from {
|
|
tokens[tokens.count - 1].length += to - from
|
|
} else {
|
|
tokens.append(Token(location: from, length: to - from, role: role))
|
|
}
|
|
}
|
|
|
|
func text(_ from: Int, _ to: Int) -> String {
|
|
String(decoding: u[from..<to], as: UTF16.self)
|
|
}
|
|
|
|
func starts(with literal: [UInt16], at j: Int? = nil) -> Bool {
|
|
let at = j ?? i
|
|
guard at + literal.count <= end else { return false }
|
|
for k in literal.indices where u[at + k] != literal[k] { return false }
|
|
return true
|
|
}
|
|
|
|
/// ASCII case-insensitive `starts(with:)`; `literal` must be lowercase.
|
|
func startsIgnoringCase(with literal: [UInt16], at j: Int? = nil) -> Bool {
|
|
let at = j ?? i
|
|
guard at + literal.count <= end else { return false }
|
|
for k in literal.indices where Unit.lower(u[at + k]) != literal[k] { return false }
|
|
return true
|
|
}
|
|
|
|
func atStop() -> Bool {
|
|
switch stop {
|
|
case .phpClose: return peek() == Unit.question && peek(1) == Unit.greater
|
|
case let .closingTag(name):
|
|
guard peek() == Unit.less, peek(1) == Unit.slash else { return false }
|
|
return startsIgnoringCase(with: Array(name.utf16), at: i + 2)
|
|
case .fence, nil: return false
|
|
}
|
|
}
|
|
|
|
mutating func skipSpaces() {
|
|
while i < end && Unit.isSpace(u[i]) { i += 1 }
|
|
}
|
|
|
|
/// The first non-space unit from `j` on, or 0.
|
|
func nextNonSpace(from j: Int? = nil) -> UInt16 {
|
|
var k = j ?? i
|
|
while k < end && Unit.isSpace(u[k]) { k += 1 }
|
|
return k < end ? u[k] : 0
|
|
}
|
|
|
|
mutating func identifier(extra: (UInt16) -> Bool = { _ in false }) {
|
|
while i < end && (Unit.isIdentPart(u[i]) || extra(u[i])) { i += 1 }
|
|
}
|
|
|
|
/// A comment to the end of the line, or to the stop marker a PHP `?>` or `</script` sets.
|
|
mutating func lineComment() {
|
|
let start = i
|
|
if stop == nil {
|
|
i = end
|
|
} else {
|
|
while i < end && !atStop() { i += 1 }
|
|
}
|
|
emit(start, i, .comment)
|
|
}
|
|
|
|
/// Consumes up to and including `close`; returns false when the line ends first.
|
|
mutating func blockComment(from start: Int, close: [UInt16]) -> Bool {
|
|
while i < end {
|
|
if starts(with: close) {
|
|
i += close.count
|
|
emit(start, i, .comment)
|
|
return true
|
|
}
|
|
i += 1
|
|
}
|
|
emit(start, i, .comment)
|
|
return false
|
|
}
|
|
|
|
/// A string body up to the closing `quote`, with backslash escapes; false when the line ends first.
|
|
mutating func quoted(from start: Int, quote: UInt16, escapes: Bool = true, role: TokenRole = .string) -> Bool {
|
|
while i < end {
|
|
let c = u[i]
|
|
if escapes && c == Unit.backslash {
|
|
i += 2
|
|
continue
|
|
}
|
|
i += 1
|
|
if c == quote {
|
|
emit(start, i, role)
|
|
return true
|
|
}
|
|
}
|
|
i = min(i, end)
|
|
emit(start, i, role)
|
|
return false
|
|
}
|
|
|
|
/// A numeric literal: hex/octal/binary prefixes, `_` separators, fraction, exponent and a letter suffix.
|
|
mutating func number() {
|
|
let start = i
|
|
if peek() == Unit.zero && Unit.isAlpha(peek(1)) && "xXoObB".utf16.contains(peek(1)) {
|
|
i += 2
|
|
while i < end && (Unit.isHexDigit(u[i]) || u[i] == Unit.underscore) { i += 1 }
|
|
} else {
|
|
while i < end && (Unit.isDigit(u[i]) || u[i] == Unit.underscore) { i += 1 }
|
|
if peek() == Unit.dot && Unit.isDigit(peek(1)) {
|
|
i += 1
|
|
while i < end && (Unit.isDigit(u[i]) || u[i] == Unit.underscore) { i += 1 }
|
|
}
|
|
if (peek() == Unit.lowerE || peek() == Unit.upperE)
|
|
&& (Unit.isDigit(peek(1)) || ((peek(1) == Unit.plus || peek(1) == Unit.minus) && Unit.isDigit(peek(2)))) {
|
|
i += 2
|
|
while i < end && Unit.isDigit(u[i]) { i += 1 }
|
|
}
|
|
}
|
|
while i < end && Unit.isAlpha(u[i]) { i += 1 }
|
|
emit(start, i, .number)
|
|
}
|
|
|
|
/// Consumes a run of operator characters and paints it as a keyword.
|
|
mutating func operatorRun(_ isOperator: (UInt16) -> Bool = Unit.isOperator) {
|
|
let start = i
|
|
while i < end && isOperator(u[i]) { i += 1 }
|
|
emit(start, i, .keyword)
|
|
}
|
|
}
|
|
|
|
enum Literals {
|
|
static let blockClose = Unit.literal("*/")
|
|
static let heredoc = Unit.literal("<<<")
|
|
}
|
|
|
|
/// UTF-16 character classes. Every unit at or above 0x80 counts as an identifier letter,
|
|
/// which keeps surrogate pairs and non-ASCII names in one token.
|
|
enum Unit {
|
|
static let tab: UInt16 = 9, space: UInt16 = 32, bang: UInt16 = 33, quote: UInt16 = 34
|
|
static let hash: UInt16 = 35, dollar: UInt16 = 36, percent: UInt16 = 37, amp: UInt16 = 38
|
|
static let apostrophe: UInt16 = 39, lparen: UInt16 = 40, rparen: UInt16 = 41, star: UInt16 = 42
|
|
static let plus: UInt16 = 43, comma: UInt16 = 44, minus: UInt16 = 45, dot: UInt16 = 46
|
|
static let slash: UInt16 = 47, zero: UInt16 = 48, colon: UInt16 = 58, semicolon: UInt16 = 59
|
|
static let less: UInt16 = 60, equals: UInt16 = 61, greater: UInt16 = 62, question: UInt16 = 63
|
|
static let at: UInt16 = 64, upperE: UInt16 = 69, lbracket: UInt16 = 91, backslash: UInt16 = 92
|
|
static let rbracket: UInt16 = 93, caret: UInt16 = 94, underscore: UInt16 = 95, backtick: UInt16 = 96
|
|
static let lowerE: UInt16 = 101, lbrace: UInt16 = 123, pipe: UInt16 = 124, rbrace: UInt16 = 125
|
|
static let tilde: UInt16 = 126
|
|
|
|
@inline(__always) static func isDigit(_ c: UInt16) -> Bool { c &- 48 < 10 }
|
|
@inline(__always) static func isUpper(_ c: UInt16) -> Bool { c &- 65 < 26 }
|
|
@inline(__always) static func isLower(_ c: UInt16) -> Bool { c &- 97 < 26 }
|
|
@inline(__always) static func isAlpha(_ c: UInt16) -> Bool { isUpper(c) || isLower(c) }
|
|
@inline(__always) static func isHexDigit(_ c: UInt16) -> Bool { isDigit(c) || (c | 0x20) &- 97 < 6 }
|
|
@inline(__always) static func isIdentStart(_ c: UInt16) -> Bool { isAlpha(c) || c == underscore || c >= 0x80 }
|
|
@inline(__always) static func isIdentPart(_ c: UInt16) -> Bool { isIdentStart(c) || isDigit(c) }
|
|
@inline(__always) static func isSpace(_ c: UInt16) -> Bool { c == space || c == tab || c == 13 || c == 12 || c == 11 }
|
|
@inline(__always) static func lower(_ c: UInt16) -> UInt16 { isUpper(c) ? c + 32 : c }
|
|
|
|
static func isOperator(_ c: UInt16) -> Bool {
|
|
switch c {
|
|
case plus, minus, star, slash, percent, equals, amp, pipe, caret, bang, tilde, less, greater, question, colon: true
|
|
default: false
|
|
}
|
|
}
|
|
|
|
static func literal(_ text: String) -> [UInt16] { Array(text.utf16) }
|
|
}
|