Compare commits
1 Commits
56efc109d7
...
93e60850f3
Author | SHA1 | Date |
---|---|---|
Shadowfacts | 93e60850f3 |
|
@ -38,14 +38,7 @@ class JavaScriptHighlighter {
|
||||||
return NSRange(from..<to, in: text)
|
return NSRange(from..<to, in: text)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func prevCharRange() -> NSRange {
|
private func peek() -> Unicode.Scalar {
|
||||||
return range(from: text.index(before: currentIndex), to: currentIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func peek() -> Unicode.Scalar? {
|
|
||||||
guard currentIndex < text.endIndex else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return text.unicodeScalars[currentIndex]
|
return text.unicodeScalars[currentIndex]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +48,7 @@ class JavaScriptHighlighter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
private func consume() -> Unicode.Scalar? {
|
private func consume() -> Unicode.Scalar {
|
||||||
let c = peek()
|
let c = peek()
|
||||||
currentIndex = text.index(after: currentIndex)
|
currentIndex = text.index(after: currentIndex)
|
||||||
return c
|
return c
|
||||||
|
@ -79,7 +72,7 @@ class JavaScriptHighlighter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeExpression() {
|
private func consumeExpression() {
|
||||||
guard let char = peek() else { return }
|
let char = peek()
|
||||||
|
|
||||||
if identifierStarts.contains(char) {
|
if identifierStarts.contains(char) {
|
||||||
consumeIdentifier()
|
consumeIdentifier()
|
||||||
|
@ -106,7 +99,7 @@ class JavaScriptHighlighter {
|
||||||
|
|
||||||
private func consumeIdentifier() {
|
private func consumeIdentifier() {
|
||||||
let identifierStart = currentIndex!
|
let identifierStart = currentIndex!
|
||||||
while let char = peek(), identifiers.contains(char) {
|
while currentIndex < text.endIndex && identifiers.contains(peek()) {
|
||||||
consume()
|
consume()
|
||||||
}
|
}
|
||||||
print("Identifier: '\(text[identifierStart..<currentIndex])'")
|
print("Identifier: '\(text[identifierStart..<currentIndex])'")
|
||||||
|
@ -114,12 +107,12 @@ class JavaScriptHighlighter {
|
||||||
|
|
||||||
private func consumeNumber() {
|
private func consumeNumber() {
|
||||||
let numberStart = currentIndex!
|
let numberStart = currentIndex!
|
||||||
while let char = peek(), CharacterSet.decimalDigits.contains(char) {
|
while currentIndex < text.endIndex && CharacterSet.decimalDigits.contains(peek()) {
|
||||||
consume()
|
consume()
|
||||||
}
|
}
|
||||||
if currentIndex < text.endIndex && peek() == "." {
|
if currentIndex < text.endIndex && peek() == "." {
|
||||||
consume()
|
consume()
|
||||||
while let char = peek(), CharacterSet.decimalDigits.contains(char) {
|
while currentIndex < text.endIndex && CharacterSet.decimalDigits.contains(peek()) {
|
||||||
consume()
|
consume()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,14 +173,14 @@ class JavaScriptHighlighter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeOperator() {
|
private func consumeOperator() {
|
||||||
print("Operator: \(peek()!)")
|
print("Operator: \(peek())")
|
||||||
consume()
|
consume()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeFunctionCallOrGrouping() {
|
private func consumeFunctionCallOrGrouping() {
|
||||||
consume() // (
|
consume() // (
|
||||||
print("Opening (")
|
print("Opening (")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
indent += " "
|
indent += " "
|
||||||
while currentIndex < text.endIndex && peek() != ")" {
|
while currentIndex < text.endIndex && peek() != ")" {
|
||||||
consumeExpression()
|
consumeExpression()
|
||||||
|
@ -196,14 +189,14 @@ class JavaScriptHighlighter {
|
||||||
if currentIndex < text.endIndex {
|
if currentIndex < text.endIndex {
|
||||||
consume() // )
|
consume() // )
|
||||||
print("Closing )")
|
print("Closing )")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeObject() {
|
private func consumeObject() {
|
||||||
consume() // {
|
consume() // {
|
||||||
print("Opening {")
|
print("Opening {")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
indent += " "
|
indent += " "
|
||||||
object:
|
object:
|
||||||
while currentIndex < text.endIndex && peek() != "}" {
|
while currentIndex < text.endIndex && peek() != "}" {
|
||||||
|
@ -226,13 +219,13 @@ class JavaScriptHighlighter {
|
||||||
if currentIndex < text.endIndex {
|
if currentIndex < text.endIndex {
|
||||||
consume() // }
|
consume() // }
|
||||||
print("Closing }")
|
print("Closing }")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeObjectKey() {
|
private func consumeObjectKey() {
|
||||||
let keyStart = currentIndex!
|
let keyStart = currentIndex!
|
||||||
while let char = peek(), identifiers.contains(char) {
|
while currentIndex < text.endIndex && identifiers.contains(peek()) {
|
||||||
consume()
|
consume()
|
||||||
}
|
}
|
||||||
print("Object key: '\(text[keyStart..<currentIndex])'")
|
print("Object key: '\(text[keyStart..<currentIndex])'")
|
||||||
|
@ -241,13 +234,13 @@ class JavaScriptHighlighter {
|
||||||
private func consumeDotLookup() {
|
private func consumeDotLookup() {
|
||||||
consume() // .
|
consume() // .
|
||||||
print("Dot lookup")
|
print("Dot lookup")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
}
|
}
|
||||||
|
|
||||||
private func consumeArray() {
|
private func consumeArray() {
|
||||||
consume() // [
|
consume() // [
|
||||||
print("Opening [")
|
print("Opening [")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
indent += " "
|
indent += " "
|
||||||
array:
|
array:
|
||||||
while currentIndex < text.endIndex && peek() != "]" {
|
while currentIndex < text.endIndex && peek() != "]" {
|
||||||
|
@ -269,7 +262,7 @@ class JavaScriptHighlighter {
|
||||||
if currentIndex < text.endIndex {
|
if currentIndex < text.endIndex {
|
||||||
consume() // ]
|
consume() // ]
|
||||||
print("Closing ]")
|
print("Closing ]")
|
||||||
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
|
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: range(from: text.index(before: currentIndex), to: currentIndex))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue