Files

157 lines
5.6 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// Markdown, with fenced code in a known language highlighted by that language.
enum MarkdownGrammar {
static func scan(_ lx: inout Lexer, _ st: inout GrammarState) {
let lineStart = lx.i
if case let .fence(marker, count) = st.mode {
fencedLine(&lx, &st, marker: marker, count: count)
return
}
lx.skipSpaces()
let c = lx.peek()
if (c == Unit.backtick || c == Unit.tilde), let count = run(lx, of: c), count >= 3 {
let info = lx.text(lx.i + count, lx.end).trimmingCharacters(in: .whitespaces)
let word = String(info.prefix { !$0.isWhitespace })
lx.i = lx.end
lx.emit(lineStart, lx.end, .comment)
st.mode = .fence(marker: c, count: count)
if let grammar = Grammar(MarkdownParser.fenceLanguage(word), text: "") {
st.child = .grammar(grammar, GrammarState(), until: .fence)
}
return
}
if c == Unit.hash, let count = run(lx, of: c), count <= 6, lx.unit(at: lx.i + count) == 0 || Unit.isSpace(lx.unit(at: lx.i + count)) {
lx.i = lx.end
lx.emit(lineStart, lx.end, .keyword)
return
}
if isRule(lx) {
lx.i = lx.end
lx.emit(lineStart, lx.end, .comment)
return
}
while lx.peek() == Unit.greater {
lx.emit(lx.i, lx.i + 1, .comment)
lx.i += 1
lx.skipSpaces()
}
listMarker(&lx)
inline(&lx)
}
private static func fencedLine(_ lx: inout Lexer, _ st: inout GrammarState, marker: UInt16, count: Int) {
let lineStart = lx.i
var k = lineStart
while k < lx.end && Unit.isSpace(lx.u[k]) { k += 1 }
var closing = k
while closing < lx.end && lx.u[closing] == marker { closing += 1 }
var rest = closing
while rest < lx.end && Unit.isSpace(lx.u[rest]) { rest += 1 }
if closing - k >= count && rest == lx.end {
lx.i = lx.end
lx.emit(lineStart, lx.end, .comment)
st.mode = .code
st.child = nil
return
}
if case let .grammar(grammar, inner, _)? = st.child {
var state = inner
Grammars.scan(grammar, &lx, &state)
st.child = .grammar(grammar, state, until: .fence)
}
lx.i = lx.end
}
/// How many `c` in a row start at the cursor.
private static func run(_ lx: Lexer, of c: UInt16) -> Int? {
var k = lx.i
while k < lx.end && lx.u[k] == c { k += 1 }
return k - lx.i
}
/// Three or more of one of `-`, `*`, `_`, spaces allowed between them.
private static func isRule(_ lx: Lexer) -> Bool {
let first = lx.peek()
guard first == Unit.minus || first == Unit.star || first == Unit.underscore else { return false }
var marks = 0
for k in lx.i..<lx.end {
if lx.u[k] == first {
marks += 1
} else if !Unit.isSpace(lx.u[k]) {
return false
}
}
return marks >= 3
}
/// `-`, `*`, `+` or `1.`/`1)` followed by a space.
private static func listMarker(_ lx: inout Lexer) {
var k = lx.i
let c = lx.peek()
if c == Unit.minus || c == Unit.star || c == Unit.plus {
k += 1
} else {
while k < lx.end && Unit.isDigit(lx.u[k]) { k += 1 }
guard k > lx.i, lx.unit(at: k) == Unit.dot || lx.unit(at: k) == Unit.rparen else { return }
k += 1
}
guard Unit.isSpace(lx.unit(at: k)) else { return }
lx.emit(lx.i, k, .comment)
lx.i = k
}
/// Code spans, link targets and autolinks in running text.
private static func inline(_ lx: inout Lexer) {
while lx.i < lx.end {
let start = lx.i
let c = lx.u[start]
if c == Unit.backslash {
lx.i = min(lx.i + 2, lx.end)
} else if c == Unit.backtick {
let ticks = run(lx, of: c) ?? 1
if let close = findRun(lx, of: c, count: ticks, from: start + ticks) {
lx.i = close + ticks
lx.emit(start, lx.i, .keyword)
} else {
lx.i += ticks
}
} else if c == Unit.rbracket && lx.peek(1) == Unit.lparen {
lx.i += 1
let open = lx.i
while lx.i < lx.end && lx.u[lx.i] != Unit.rparen { lx.i += 1 }
lx.i = min(lx.i + 1, lx.end)
lx.emit(open, lx.i, .variable)
} else if c == Unit.less && Unit.isAlpha(lx.peek(1)) {
var k = start + 1
while k < lx.end && lx.u[k] != Unit.greater && !Unit.isSpace(lx.u[k]) { k += 1 }
if lx.unit(at: k) == Unit.greater && lx.text(start + 1, k).contains(":") {
lx.i = k + 1
lx.emit(start, lx.i, .variable)
} else {
lx.i += 1
}
} else {
lx.i += 1
}
}
}
/// The start of the next run of exactly `count` × `c` from `from`, or nil.
private static func findRun(_ lx: Lexer, of c: UInt16, count: Int, from: Int) -> Int? {
var k = from
while k < lx.end {
if lx.u[k] == c {
var e = k
while e < lx.end && lx.u[e] == c { e += 1 }
if e - k == count { return k }
k = e
} else {
k += 1
}
}
return nil
}
}