JavaScript Highlighter code cleanup

This commit is contained in:
Shadowfacts 2020-04-03 09:53:12 -04:00
parent 6e4cbfdb6d
commit f27fc8eb9e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 47 additions and 12 deletions

View File

@ -77,8 +77,25 @@ class JavaScriptHighlighter {
return attributed
}
private func emit(token: TokenType, range: NSRange) {
let color: NSColor
switch token {
case .string:
color = .systemRed
case .number:
color = .systemBlue
case .punctuation:
color = .systemTeal
case .identifier:
return
}
attributed.addAttribute(.foregroundColor, value: color, range: range)
}
private func consumeExpression() {
consumeWhitespace()
guard let char = peek() else { return }
if identifierStarts.contains(char) {
@ -103,6 +120,13 @@ class JavaScriptHighlighter {
consume()
}
}
private func consumeWhitespace(newlines: Bool = false) {
let charSet = newlines ? CharacterSet.whitespacesAndNewlines : .whitespaces
while let char = peek(), charSet.contains(char) {
consume()
}
}
private func consumeIdentifier() {
let identifierStart = currentIndex!
@ -124,7 +148,7 @@ class JavaScriptHighlighter {
}
}
print("Number: \(text[numberStart..<currentIndex])")
attributed.addAttribute(.foregroundColor, value: NSColor.systemBlue, range: range(from: numberStart, to: currentIndex))
emit(token: .number, range: range(from: numberStart, to: currentIndex))
}
private func consumeString() {
@ -137,7 +161,7 @@ class JavaScriptHighlighter {
consume() // string closing quote
}
print("String: \(text[stringStart..<currentIndex])")
attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: stringStart, to: currentIndex))
emit(token: .string, range: range(from: stringStart, to: currentIndex))
}
private func consumeTemplateString() {
@ -148,7 +172,7 @@ class JavaScriptHighlighter {
consume() // $
consume() // {
print("Template string fragment: '\(text[stringFragmentStart!..<currentIndex])'")
attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: stringFragmentStart!, to: currentIndex))
emit(token: .string, range: range(from: stringFragmentStart!, to: currentIndex))
consumeTemplateStringExpression()
stringFragmentStart = currentIndex
if currentIndex < text.endIndex && peek() == "}" {
@ -158,7 +182,7 @@ class JavaScriptHighlighter {
stringFragmentStart = stringFragmentStart ?? currentIndex
consume() // `
print("Template string fragment: '\(text[stringFragmentStart!..<currentIndex])'")
attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: stringFragmentStart!, to: currentIndex))
emit(token: .string, range: range(from: stringFragmentStart!, to: currentIndex))
stringFragmentStart = nil
break
} else {
@ -167,7 +191,7 @@ class JavaScriptHighlighter {
}
if let start = stringFragmentStart {
print("Template string fragment: '\(text[start..<currentIndex])'")
attributed.addAttribute(.foregroundColor, value: NSColor.systemRed, range: range(from: start, to: currentIndex))
emit(token: .string, range: range(from: start, to: currentIndex))
}
}
@ -187,7 +211,7 @@ class JavaScriptHighlighter {
private func consumeFunctionCallOrGrouping() {
consume() // (
print("Opening (")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
indent += " "
while currentIndex < text.endIndex && peek() != ")" {
consumeExpression()
@ -196,14 +220,14 @@ class JavaScriptHighlighter {
if currentIndex < text.endIndex {
consume() // )
print("Closing )")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
}
}
private func consumeObject() {
consume() // {
print("Opening {")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
indent += " "
object:
while currentIndex < text.endIndex && peek() != "}" {
@ -226,7 +250,7 @@ class JavaScriptHighlighter {
if currentIndex < text.endIndex {
consume() // }
print("Closing }")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
}
}
@ -244,13 +268,13 @@ class JavaScriptHighlighter {
private func consumeDotLookup() {
consume() // .
print("Dot lookup")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
}
private func consumeArray() {
consume() // [
print("Opening [")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
indent += " "
array:
while currentIndex < text.endIndex && peek() != "]" {
@ -258,6 +282,8 @@ class JavaScriptHighlighter {
while currentIndex < text.endIndex {
if peek() == "," {
consume() // ,
print("Array separator")
emit(token: .punctuation, range: prevCharRange())
break
} else if peek() == "]" {
break array
@ -272,8 +298,17 @@ class JavaScriptHighlighter {
if currentIndex < text.endIndex {
consume() // ]
print("Closing ]")
attributed.addAttribute(.foregroundColor, value: NSColor.systemTeal, range: prevCharRange())
emit(token: .punctuation, range: prevCharRange())
}
}
}
extension JavaScriptHighlighter {
enum TokenType {
case identifier
case punctuation
case number
case string
}
}