Highlight JS keywords

This commit is contained in:
Shadowfacts 2020-08-12 17:17:32 -04:00
parent 8e34e6071f
commit 5ae526934c
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 7 additions and 2 deletions

View File

@ -23,6 +23,7 @@ fileprivate let identifierStarts: CharacterSet = {
}()
fileprivate let operators = CharacterSet(charactersIn: "+-*/<>=")
fileprivate let expressionEnds = CharacterSet(charactersIn: ",]});")
fileprivate let keywords = ["null", "true", "false"]
class JavaScriptHighlighter {
private var text: String!
@ -244,6 +245,9 @@ class JavaScriptHighlighter {
while let char = peek(), identifiers.contains(char) {
consume()
}
let identifier = text[identifierStart..<currentIndex]
let token: TokenType = keywords.contains(String(identifier)) ? .keyword : .identifier
emit(token: token, range: range(from: identifierStart, to: currentIndex))
print("Identifier: '\(text[identifierStart..<currentIndex])'")
}
@ -452,6 +456,7 @@ class JavaScriptHighlighter {
extension JavaScriptHighlighter {
enum TokenType {
case identifier
case keyword
case punctuation
case number
case string(Unicode.Scalar)
@ -463,9 +468,9 @@ extension JavaScriptHighlighter {
case .number:
return .systemBlue
case .punctuation:
return .systemTeal
case .identifier:
return nil
case .keyword, .identifier:
return .systemTeal
}
}
}