156 lines
6.5 KiB
Swift
156 lines
6.5 KiB
Swift
import Foundation
|
|
|
|
/// HTML, XML, SVG and Vue, plus the markup around `<?php` blocks in PHP files.
|
|
enum MarkupGrammar {
|
|
private static let commentOpen = Unit.literal("<!--")
|
|
private static let commentClose = Unit.literal("-->")
|
|
private static let cdataOpen = Unit.literal("<![cdata[")
|
|
private static let cdataClose = Unit.literal("]]>")
|
|
private static let phpOpenTags = [Unit.literal("<?php"), Unit.literal("<?="), Unit.literal("<?")]
|
|
|
|
static func scan(_ lx: inout Lexer, _ st: inout GrammarState, phpIslands: Bool) {
|
|
while lx.i < lx.end {
|
|
switch st.mode {
|
|
case .markupComment:
|
|
if lx.blockComment(from: lx.i, close: commentClose) { st.mode = .code }
|
|
continue
|
|
case .cdata:
|
|
if lx.blockComment(from: lx.i, close: cdataClose) { st.mode = .code }
|
|
continue
|
|
case let .markupTag(name):
|
|
tag(&lx, &st, name: name, phpIslands: phpIslands)
|
|
if st.child != nil { return }
|
|
continue
|
|
case let .markupValue(quote, name):
|
|
value(&lx, &st, quote: quote, tag: name, from: lx.i, phpIslands: phpIslands)
|
|
if st.child != nil { return }
|
|
continue
|
|
default:
|
|
break
|
|
}
|
|
if lx.stop != nil && lx.atStop() { return }
|
|
if phpIslands && openIsland(&lx, &st) { return }
|
|
let start = lx.i
|
|
let c = lx.u[start]
|
|
if c == Unit.less {
|
|
openTag(&lx, &st)
|
|
} else if c == Unit.amp {
|
|
lx.i += 1
|
|
while lx.i < lx.end && (Unit.isIdentPart(lx.u[lx.i]) || lx.u[lx.i] == Unit.hash) { lx.i += 1 }
|
|
if lx.peek() == Unit.semicolon && lx.i > start + 1 {
|
|
lx.i += 1
|
|
lx.emit(start, lx.i, .variable)
|
|
}
|
|
} else {
|
|
lx.i += 1
|
|
while lx.i < lx.end && lx.u[lx.i] != Unit.less && lx.u[lx.i] != Unit.amp { lx.i += 1 }
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Everything that starts with `<` in text: comments, CDATA, doctype, prolog and tags.
|
|
private static func openTag(_ lx: inout Lexer, _ st: inout GrammarState) {
|
|
let start = lx.i
|
|
if lx.starts(with: commentOpen) {
|
|
lx.i += commentOpen.count
|
|
if !lx.blockComment(from: start, close: commentClose) { st.mode = .markupComment }
|
|
return
|
|
}
|
|
if lx.startsIgnoringCase(with: cdataOpen) {
|
|
lx.i += cdataOpen.count
|
|
if !lx.blockComment(from: start, close: cdataClose) { st.mode = .cdata }
|
|
return
|
|
}
|
|
let next = lx.peek(1)
|
|
if next == Unit.bang || next == Unit.question {
|
|
let close = next == Unit.bang ? [Unit.greater] : [Unit.question, Unit.greater]
|
|
lx.i += 2
|
|
_ = lx.blockComment(from: start, close: close)
|
|
return
|
|
}
|
|
var k = start + 1
|
|
let closing = lx.unit(at: k) == Unit.slash
|
|
if closing { k += 1 }
|
|
guard Unit.isAlpha(lx.unit(at: k)) else {
|
|
lx.i += 1
|
|
return
|
|
}
|
|
let nameStart = k
|
|
while k < lx.end && (Unit.isIdentPart(lx.u[k]) || lx.u[k] == Unit.minus || lx.u[k] == Unit.colon || lx.u[k] == Unit.dot) { k += 1 }
|
|
lx.emit(nameStart, k, .number)
|
|
let name = lx.text(nameStart, k).lowercased()
|
|
lx.i = k
|
|
st.mode = .markupTag(closing ? "/" + name : name)
|
|
}
|
|
|
|
/// Attributes up to `>`; a `>` that closes `<script>` or `<style>` hands the body to JavaScript or CSS.
|
|
private static func tag(_ lx: inout Lexer, _ st: inout GrammarState, name: String, phpIslands: Bool) {
|
|
while lx.i < lx.end {
|
|
if phpIslands && openIsland(&lx, &st) { return }
|
|
let start = lx.i
|
|
let c = lx.u[start]
|
|
if Unit.isSpace(c) || c == Unit.equals {
|
|
lx.i += 1
|
|
} else if c == Unit.greater {
|
|
lx.i += 1
|
|
st.mode = .code
|
|
switch name {
|
|
case "script": st.child = .grammar(.javascript(typescript: false, jsx: false), GrammarState(), until: .closingTag("script"))
|
|
case "style": st.child = .grammar(.css, GrammarState(), until: .closingTag("style"))
|
|
default: break
|
|
}
|
|
return
|
|
} else if c == Unit.slash && lx.peek(1) == Unit.greater {
|
|
lx.i += 2
|
|
st.mode = .code
|
|
return
|
|
} else if c == Unit.quote || c == Unit.apostrophe {
|
|
lx.i += 1
|
|
value(&lx, &st, quote: c, tag: name, from: start, phpIslands: phpIslands)
|
|
if st.child != nil || st.mode != .markupTag(name) { return }
|
|
} else if c == Unit.slash {
|
|
lx.i += 1
|
|
} else {
|
|
while lx.i < lx.end && !isAttributeEnd(lx.u[lx.i]) { lx.i += 1 }
|
|
lx.emit(start, lx.i, lx.unit(at: start - 1) == Unit.equals ? .string : .variable)
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func isAttributeEnd(_ c: UInt16) -> Bool {
|
|
Unit.isSpace(c) || c == Unit.equals || c == Unit.greater || c == Unit.slash || c == Unit.quote || c == Unit.apostrophe
|
|
}
|
|
|
|
/// A quoted attribute value; it may run over lines and may hold a PHP island.
|
|
private static func value(_ lx: inout Lexer, _ st: inout GrammarState, quote: UInt16, tag: String, from start: Int, phpIslands: Bool) {
|
|
while lx.i < lx.end {
|
|
if phpIslands && lx.peek() == Unit.less && lx.peek(1) == Unit.question {
|
|
lx.emit(start, lx.i, .string)
|
|
st.mode = .markupValue(quote: quote, tag: tag)
|
|
_ = openIsland(&lx, &st)
|
|
return
|
|
}
|
|
if lx.u[lx.i] == quote {
|
|
lx.i += 1
|
|
lx.emit(start, lx.i, .string)
|
|
st.mode = .markupTag(tag)
|
|
return
|
|
}
|
|
lx.i += 1
|
|
}
|
|
lx.emit(start, lx.i, .string)
|
|
st.mode = .markupValue(quote: quote, tag: tag)
|
|
}
|
|
|
|
/// `<?php`, `<?=` or `<?` starts a PHP island that runs until `?>`.
|
|
private static func openIsland(_ lx: inout Lexer, _ st: inout GrammarState) -> Bool {
|
|
guard lx.peek() == Unit.less, lx.peek(1) == Unit.question else { return false }
|
|
let start = lx.i
|
|
let open = phpOpenTags.first { lx.startsIgnoringCase(with: $0) } ?? phpOpenTags[2]
|
|
lx.i += open.count
|
|
lx.emit(start, lx.i, .keyword)
|
|
st.child = .grammar(.php, GrammarState(), until: .phpClose)
|
|
return true
|
|
}
|
|
}
|