Highlight JS keywords
This commit is contained in:
parent
8e34e6071f
commit
5ae526934c
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue