diff --git a/MongoView/Synax Highlighting/JavaScriptHighlighter.swift b/MongoView/Synax Highlighting/JavaScriptHighlighter.swift index f147c29..d201cff 100644 --- a/MongoView/Synax Highlighting/JavaScriptHighlighter.swift +++ b/MongoView/Synax Highlighting/JavaScriptHighlighter.swift @@ -19,6 +19,7 @@ fileprivate let identifierStarts: CharacterSet = { return set }() fileprivate let operators = CharacterSet(charactersIn: "+-*/<>=") +fileprivate let expressionEnds = CharacterSet(charactersIn: ",]});") class JavaScriptHighlighter { private let text: String @@ -71,7 +72,7 @@ class JavaScriptHighlighter { ], range: fullRange) currentIndex = text.startIndex - while currentIndex < text.endIndex { + while let char = peek(), !expressionEnds.contains(char) { consumeExpression() } @@ -116,6 +117,8 @@ class JavaScriptHighlighter { consumeArray() } else if char == "." { consumeDotLookup() + } else if char == "?" { + consumeTernaryExpression() } else { consume() } @@ -310,6 +313,22 @@ class JavaScriptHighlighter { } } + func consumeTernaryExpression() { + consume() // ? + print("Ternary expression") + indent += " " + print("Ternary true result") + while let char = peek(), char != ":" { + consumeExpression() // true result + } + consume() // : + print("Ternary false result") + while let char = peek(), !expressionEnds.contains(char) { + consumeExpression() + } + indent = String(indent.dropLast(2)) + } + } extension JavaScriptHighlighter { diff --git a/jstest/main.swift b/jstest/main.swift index 56b6b67..05cc648 100644 --- a/jstest/main.swift +++ b/jstest/main.swift @@ -8,6 +8,6 @@ import Foundation -let source = "{a: 1234 , 'b': 'blah', foo}" +let source = "a ? 'foo' : b ? 'bar' : 'baz'" _ = JavaScriptHighlighter(text: source).highlight()