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